Class VectorOperations


  • public final class VectorOperations
    extends Object
    Operations for working with vectors.
    • Constructor Detail

      • VectorOperations

        public VectorOperations()
    • Method Detail

      • cosineSimilarity

        public static float cosineSimilarity​(@Nonnull
                                             List<Float> x,
                                             @Nonnull
                                             List<Float> y)
        Calculates the cosine similarity of two vectors. The vectors must be equal in length and have non-zero norm.
        Parameters:
        x - First vector, which is not modified
        y - Second vector, which is not modified
        Returns:
        The cosine similarity of the two vectors
      • cosineDistance

        public static double cosineDistance​(List<Float> x,
                                            List<Float> y)
        Calculates the cosine distance of two vectors. The vectors must be equal in length and have non-zero norm.
        Parameters:
        x - First vector, which is not modified
        y - Second vector, which is not modified
        Returns:
        The cosine distance of the two vectors
      • euclideanDistance

        public static float euclideanDistance​(@Nonnull
                                              List<Float> x,
                                              @Nonnull
                                              List<Float> y)
        Calculates the Euclidean distance between two vectors.
        Parameters:
        x - First vector, which is not modified
        y - Second vector, which is not modified
        Returns:
        The Euclidean distance between the two vectors
      • divide

        public static List<Float> divide​(@Nonnull
                                         List<Float> vector,
                                         float divisor)
        Divides the elements of the vector by the divisor.
        Parameters:
        vector - Vector to divide, which is not modified
        divisor - Divisor to apply to each element of the vector
        Returns:
        A new vector with the elements divided by the divisor
      • dot

        public static float dot​(@Nonnull
                                List<Float> x,
                                @Nonnull
                                List<Float> y)
        Calculates the dot product of two vectors.
        Parameters:
        x - First vector, which is not modified
        y - Second vector, which is not modified
        Returns:
        The dot product of the two vectors
      • euclideanLength

        public static float euclideanLength​(@Nonnull
                                            List<Float> vector)
        Calculates the Euclidean length of a vector.
        Parameters:
        vector - Vector to calculate the length of, which is not modified
        Returns:
        The Euclidean length of the vector
      • multiply

        public static List<Float> multiply​(@Nonnull
                                           List<Float> vector,
                                           float multiplier)
        Multiplies the elements of the vector by the multiplier.
        Parameters:
        vector - Vector to multiply, which is not modified
        multiplier - Multiplier to apply to each element of the vector
        Returns:
        A new vector with the elements multiplied by the multiplier
      • normalize

        public static List<Float> normalize​(@Nonnull
                                            List<Float> vector)
        Normalizes the vector such that the Euclidean length is 1.
        Parameters:
        vector - Vector to normalize, which is not modified
        Returns:
        A new, normalized vector
      • exactSimilaritySearch

        public static <Record> List<VectorSearchResult<Record>> exactSimilaritySearch​(List<Record> records,
                                                                                      List<Float> vector,
                                                                                      VectorStoreRecordVectorField vectorField,
                                                                                      DistanceFunction distanceFunction,
                                                                                      VectorSearchOptions options)
        Performs an exact similarity search on a list of records using a vector field.
        Type Parameters:
        Record - The type of the records.
        Parameters:
        records - The records to search.
        vector - The vector to search for.
        vectorField - The vector field to use for the search.
        distanceFunction - The distance function to use for the search.
        options - The search options.
        Returns:
        The search results.