Package io.github.jbellis.jvector.vector
Class VectorUtil
java.lang.Object
io.github.jbellis.jvector.vector.VectorUtil
Utilities for computations with numeric arrays
-
Method Summary
Modifier and TypeMethodDescriptionstatic voidaddInPlace(float[] v1, float[] v2) static floatassembleAndSum(float[] data, int dataBase, byte[] dataOffsets) static floatcosine(byte[] a, byte[] b) Returns the cosine similarity between the two vectors.static floatcosine(float[] a, float[] b) Returns the cosine similarity between the two vectors.static intdotProduct(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 floatdotProduct(float[] a, int aoffset, float[] b, int boffset, int length) static floatdotProductScore(byte[] a, byte[] b) Dot product score computed over signed bytes, scaled to be in [0, 1].static inthammingDistance(long[] v1, long[] v2) static voidl2normalize(float[] v) Modifies the argument to be unit length, dividing by its l2-norm.static voidscale(float[] vector, float multiplier) static intsquareDistance(byte[] a, byte[] b) Returns the sum of squared differences of the two vectors.static floatsquareDistance(float[] a, float[] b) Returns the sum of squared differences of the two vectors.static floatsquareDistance(float[] a, int aoffset, float[] b, int boffset, int length) Returns the sum of squared differences of the two vectors, or subvectors, of the given length.static float[]sub(float[] lhs, float[] rhs) static voidsubInPlace(float[] v1, float[] v2) static floatsum(float[] vector) static float[]
-
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.
-
dotProduct
public static float dotProduct(float[] a, int aoffset, float[] b, int boffset, int length) -
cosine
public static float cosine(float[] a, float[] b) 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[] a, float[] b) Returns the sum of squared differences of the two vectors.- Throws:
IllegalArgumentException- if the vectors' dimensions differ.
-
squareDistance
public static float squareDistance(float[] a, int aoffset, float[] b, int boffset, int length) Returns the sum of squared differences of the two vectors, or subvectors, of the given length. -
squareDistance
public static int squareDistance(byte[] a, byte[] b) Returns the sum of squared differences of the two vectors. -
l2normalize
public static void l2normalize(float[] v) Modifies the argument to be unit length, dividing by its l2-norm. IllegalArgumentException is thrown for zero vectors. -
dotProduct
public static int 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
-
sum
-
sum
public static float sum(float[] vector) -
scale
public static void scale(float[] vector, float multiplier) -
addInPlace
public static void addInPlace(float[] v1, float[] v2) -
subInPlace
public static void subInPlace(float[] v1, float[] v2) -
sub
public static float[] sub(float[] lhs, float[] rhs) -
assembleAndSum
public static float assembleAndSum(float[] data, int dataBase, byte[] dataOffsets) -
hammingDistance
public static int hammingDistance(long[] v1, long[] v2)
-