public class CosineDistance extends Object implements DistanceMetric
Constructor and Description |
---|
CosineDistance() |
Modifier and Type | Method and Description |
---|---|
CosineDistance |
clone() |
static double |
cosineToDistance(double cosAngle)
This method converts the cosine distance in [-1, 1] to a valid distance
metric in the range [0, 1]
|
double |
dist(int a,
int b,
List<? extends Vec> vecs,
List<Double> cache)
Computes the distance between 2 vectors in the original list of vectors.
|
double |
dist(int a,
Vec b,
List<? extends Vec> vecs,
List<Double> cache)
Computes the distance between one vector in the original list of vectors
with that of another vector not from the original list.
|
double |
dist(int a,
Vec b,
List<Double> qi,
List<? extends Vec> vecs,
List<Double> cache)
Computes the distance between one vector in the original list of vectors
with that of another vector not from the original list, but had
information generated by
DistanceMetric.getQueryInfo(jsat.linear.Vec) . |
double |
dist(Vec a,
Vec b)
Computes the distance between 2 vectors.
|
static double |
distanceToCosine(double dist)
This method converts the distance obtained with
cosineToDistance(double) back into the cosine angle |
List<Double> |
getAccelerationCache(List<? extends Vec> vecs)
Returns a cache of double values associated with the given list of
vectors in the given order.
|
List<Double> |
getAccelerationCache(List<? extends Vec> vecs,
ExecutorService threadpool)
Returns a cache of double values associated with the given list of
vectors in the given order.
|
List<Double> |
getQueryInfo(Vec q)
Pre computes query information that would have be generated if the query
was a member of the original list of vectors when calling
DistanceMetric.getAccelerationCache(java.util.List) . |
boolean |
isIndiscemible()
Returns true if this distance metric obeys the rule that, for any x and y ∈ S
d(x, y) = 0 if and only if x = y |
boolean |
isSubadditive()
Returns true if this distance metric obeys the rule that, for any x, y, and z ∈ S
d(x, z) ≤ d(x, y) + d(y, z) |
boolean |
isSymmetric()
Returns true if this distance metric obeys the rule that, for any x, y, and z ∈ S
d(x, y) = d(y, x) |
double |
metricBound()
All metrics must return values greater than or equal to 0.
|
boolean |
supportsAcceleration()
Indicates if this distance metric supports building an acceleration cache
using the
DistanceMetric.getAccelerationCache(java.util.List) and associated
distance methods. |
String |
toString()
Returns a descriptive name of the Distance Metric in use
|
public double dist(Vec a, Vec b)
DistanceMetric
dist
in interface DistanceMetric
a
- the first vectorb
- the second vectorpublic boolean isSymmetric()
DistanceMetric
isSymmetric
in interface DistanceMetric
public boolean isSubadditive()
DistanceMetric
isSubadditive
in interface DistanceMetric
public boolean isIndiscemible()
DistanceMetric
isIndiscemible
in interface DistanceMetric
public double metricBound()
DistanceMetric
Double.POSITIVE_INFINITY
is a valid return value.metricBound
in interface DistanceMetric
public String toString()
DistanceMetric
toString
in interface DistanceMetric
toString
in class Object
public CosineDistance clone()
clone
in interface DistanceMetric
clone
in class Object
public boolean supportsAcceleration()
DistanceMetric
DistanceMetric.getAccelerationCache(java.util.List)
and associated
distance methods. By default this method will return false
. If
true
, then a cache can be obtained from this distance metric and
used in conjunction with DistanceMetric.dist(int, jsat.linear.Vec,
java.util.List, java.util.List)
and DistanceMetric.dist(int, int,
java.util.List, java.util.List)
to perform distance computations.supportsAcceleration
in interface DistanceMetric
true
if cache acceleration is supported for this metric,
false
otherwise.public List<Double> getAccelerationCache(List<? extends Vec> vecs)
DistanceMetric
null
will be
returned.getAccelerationCache
in interface DistanceMetric
vecs
- the list of vectors to build an acceleration cache forpublic List<Double> getAccelerationCache(List<? extends Vec> vecs, ExecutorService threadpool)
DistanceMetric
null
will be
returned.getAccelerationCache
in interface DistanceMetric
vecs
- the list of vectors to build an acceleration cache forthreadpool
- source of threads for parallel computation of result.
This may be null
, which means the
singled threaded
version
may be used.public double dist(int a, int b, List<? extends Vec> vecs, List<Double> cache)
DistanceMetric
null
, then
DistanceMetric.dist(jsat.linear.Vec, jsat.linear.Vec)
will be called directly.dist
in interface DistanceMetric
a
- the index of the first vectorb
- the index of the second vectorvecs
- the list of vectors used to build the cachecache
- the cache associated with the given list of vectorspublic double dist(int a, Vec b, List<? extends Vec> vecs, List<Double> cache)
DistanceMetric
null
, then
DistanceMetric.dist(jsat.linear.Vec, jsat.linear.Vec)
will be called directly.dist
in interface DistanceMetric
a
- the index of the vector in the cacheb
- the other vectorvecs
- the list of vectors used to build the cachecache
- the cache associated with the given list of vectorspublic List<Double> getQueryInfo(Vec q)
DistanceMetric
DistanceMetric.getAccelerationCache(java.util.List)
. This can then be used if
a large number of distance computations are going to be done against
points in the original set for a point that is outside the original space.
null
will be
returned.getQueryInfo
in interface DistanceMetric
q
- the query point to generate cache information forpublic double dist(int a, Vec b, List<Double> qi, List<? extends Vec> vecs, List<Double> cache)
DistanceMetric
DistanceMetric.getQueryInfo(jsat.linear.Vec)
.
null
, then
DistanceMetric.dist(jsat.linear.Vec, jsat.linear.Vec)
will be called directly.dist
in interface DistanceMetric
a
- the index of the vector in the cacheb
- the other vectorqi
- the query information about bvecs
- the list of vectors used to build the cachecache
- the cache associated with the given list of vectorspublic static double cosineToDistance(double cosAngle)
cosAngle
- the cosine similarity in [-1, 1]public static double distanceToCosine(double dist)
cosineToDistance(double)
back into the cosine angledist
- the distance value in [0, 1]Copyright © 2017. All rights reserved.