Class VectorUtil

java.lang.Object
io.github.jbellis.jvector.vector.VectorUtil

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

    Modifier and Type
    Method
    Description
    static void
    addInPlace(float[] v1, float[] v2)
     
    static float
    assembleAndSum(float[] data, int dataBase, byte[] dataOffsets)
     
    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
    dotProduct(float[] a, int aoffset, float[] b, int boffset, int length)
     
    static float
    dotProductScore(byte[] a, byte[] b)
    Dot product score computed over signed bytes, scaled to be in [0, 1].
    static int
    hammingDistance(long[] v1, long[] v2)
     
    static void
    l2normalize(float[] v)
    Modifies the argument to be unit length, dividing by its l2-norm.
    static void
    scale(float[] vector, float multiplier)
     
    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.
    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.
    static float[]
    sub(float[] lhs, float[] rhs)
     
    static void
    subInPlace(float[] v1, float[] v2)
     
    static float
    sum(float[] vector)
     
    static float[]
    sum(List<float[]> vectors)
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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 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
    • sum

      public static float[] sum(List<float[]> vectors)
    • 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)