java.lang.Object
org.apache.lucene.util.VectorUtil
Utilities for computations with numeric arrays
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidadd(float[] u, float[] v) Adds the second argument to the firststatic floatcosine(byte[] a, byte[] b) Returns the cosine similarity between the two vectors.static floatcosine(float[] v1, float[] v2) Returns the cosine similarity between the two vectors.static floatdotProduct(byte[] a, byte[] b) Dot product computed over signed bytes.static floatdotProduct(float[] a, float[] b) Returns the vector dot product of the two vectors.static floatdotProductScore(byte[] a, byte[] b) Dot product score computed over signed bytes, scaled to be in [0, 1].static float[]l2normalize(float[] v) Modifies the argument to be unit length, dividing by its l2-norm.static float[]l2normalize(float[] v, boolean throwOnZero) Modifies the argument to be unit length, dividing by its l2-norm.static floatsquareDistance(byte[] a, byte[] b) Returns the sum of squared differences of the two vectors.static floatsquareDistance(float[] v1, float[] v2) Returns the sum of squared differences of the two vectors.private static floatsquareDistanceUnrolled(float[] v1, float[] v2, int index) static BytesReftoBytesRef(float[] vector) Convert a floating point vector to an array of bytes using casting; the vector values should be in [-128,127]
-
Constructor Details
-
VectorUtil
private VectorUtil()
-
-
Method Details
-
dotProduct
public static float dotProduct(float[] a, float[] b) Returns the vector dot product of the two vectors.- Throws:
IllegalArgumentException- if the vectors' dimensions differ.
-
cosine
public static float cosine(float[] v1, float[] v2) Returns the cosine similarity between the two vectors.- Throws:
IllegalArgumentException- if the vectors' dimensions differ.
-
cosine
public static float cosine(byte[] a, byte[] b) Returns the cosine similarity between the two vectors. -
squareDistance
public static float squareDistance(float[] v1, float[] v2) Returns the sum of squared differences of the two vectors.- Throws:
IllegalArgumentException- if the vectors' dimensions differ.
-
squareDistanceUnrolled
private static float squareDistanceUnrolled(float[] v1, float[] v2, int index) -
squareDistance
public static float squareDistance(byte[] a, byte[] b) Returns the sum of squared differences of the two vectors. -
l2normalize
public static float[] l2normalize(float[] v) Modifies the argument to be unit length, dividing by its l2-norm. IllegalArgumentException is thrown for zero vectors.- Returns:
- the input array after normalization
-
l2normalize
public static float[] l2normalize(float[] v, boolean throwOnZero) Modifies the argument to be unit length, dividing by its l2-norm.- Parameters:
v- the vector to normalizethrowOnZero- whether to throw an exception whenvhas all zeros- Returns:
- the input array after normalization
- Throws:
IllegalArgumentException- when the vector is all zero and throwOnZero is true
-
add
public static void add(float[] u, float[] v) Adds the second argument to the first- Parameters:
u- the destinationv- the vector to add to the destination
-
dotProduct
public static float dotProduct(byte[] a, byte[] b) Dot product computed over signed bytes.- Parameters:
a- bytes containing a vectorb- bytes containing another vector, of the same dimension- Returns:
- the value of the dot product of the two vectors
-
dotProductScore
public static float dotProductScore(byte[] a, byte[] b) Dot product score computed over signed bytes, scaled to be in [0, 1].- Parameters:
a- bytes containing a vectorb- bytes containing another vector, of the same dimension- Returns:
- the value of the similarity function applied to the two vectors
-
toBytesRef
Convert a floating point vector to an array of bytes using casting; the vector values should be in [-128,127]- Parameters:
vector- a vector- Returns:
- a new BytesRef containing the vector's values cast to byte.
-