## Class VectorUtil

• ```public final class VectorUtil
extends Object```
Utilities for computations with numeric arrays
• ### Method Summary

All Methods
Modifier and Type Method Description
`static void` ```add​(float[] u, float[] v)```
Adds the second argument to the first
`static float[]` `checkFinite​(float[] v)`
Checks if a float vector only has finite components.
`static float` ```cosine​(byte[] a, byte[] b)```
Returns the cosine similarity between the two vectors.
`static float` ```cosine​(float[] a, float[] b)```
Returns the cosine similarity between the two vectors.
`static int` ```dotProduct​(byte[] a, byte[] b)```
Dot product computed over signed bytes.
`static float` ```dotProduct​(float[] a, float[] b)```
Returns the vector dot product of the two vectors.
`static float` ```dotProductScore​(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 float` `scaleMaxInnerProductScore​(float vectorDotProductSimilarity)`
`static int` ```squareDistance​(byte[] a, byte[] b)```
Returns the sum of squared differences of the two vectors.
`static float` ```squareDistance​(float[] a, float[] b)```
Returns the sum of squared differences of the two vectors.
• ### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`
• ### Method Detail

• #### 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[] 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 int 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 normalize
`throwOnZero` - whether to throw an exception when `v` has all zeros
Returns:
the input array after normalization
Throws:
`IllegalArgumentException` - when the vector is all zero and throwOnZero is true

```public static void add​(float[] u,
float[] v)```
Adds the second argument to the first
Parameters:
`u` - the destination
`v` - the vector to add to the destination
• #### dotProduct

```public static int dotProduct​(byte[] a,
byte[] b)```
Dot product computed over signed bytes.
Parameters:
`a` - bytes containing a vector
`b` - 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 vector
`b` - bytes containing another vector, of the same dimension
Returns:
the value of the similarity function applied to the two vectors
• #### scaleMaxInnerProductScore

`public static float scaleMaxInnerProductScore​(float vectorDotProductSimilarity)`
Parameters:
`vectorDotProductSimilarity` - the raw similarity between two vectors
Returns:
A scaled score preventing negative scores for maximum-inner-product
• #### checkFinite

`public static float[] checkFinite​(float[] v)`
Checks if a float vector only has finite components.
Parameters:
`v` - bytes containing a vector
Returns:
the vector for call-chaining
Throws:
`IllegalArgumentException` - if any component of vector is not finite