Class VectorUtil


  • public final class VectorUtil
    extends java.lang.Object
    Utilities for computations with numeric arrays
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private VectorUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void add​(float[] u, float[] v)
      Adds the second argument to the first
      static float cosine​(float[] v1, float[] v2)
      Returns the cosine similarity between the two vectors.
      static float dotProduct​(float[] a, float[] b)
      Returns the vector dot product of the two vectors.
      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 squareDistance​(float[] v1, float[] v2)
      Returns the sum of squared differences of the two vectors.
      private static float squareDistanceUnrolled8​(float[] v1, float[] v2, int index)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • VectorUtil

        private VectorUtil()
    • Method Detail

      • dotProduct

        public static float dotProduct​(float[] a,
                                       float[] b)
        Returns the vector dot product of the two vectors.
        Throws:
        java.lang.IllegalArgumentException - if the vectors' dimensions differ.
      • cosine

        public static float cosine​(float[] v1,
                                   float[] v2)
        Returns the cosine similarity between the two vectors.
        Throws:
        java.lang.IllegalArgumentException - if the vectors' dimensions differ.
      • squareDistance

        public static float squareDistance​(float[] v1,
                                           float[] v2)
        Returns the sum of squared differences of the two vectors.
        Throws:
        java.lang.IllegalArgumentException - if the vectors' dimensions differ.
      • squareDistanceUnrolled8

        private static float squareDistanceUnrolled8​(float[] v1,
                                                     float[] v2,
                                                     int index)
      • 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:
        java.lang.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 destination
        v - the vector to add to the destination