Package elki.data

Class VectorUtil


  • public final class VectorUtil
    extends java.lang.Object
    Utility functions for use with vectors.

    Note: obviously, many functions are class methods or database related.

    TODO: add more precise but slower O(n^2) angle computation according to:

    Computing the Angle between Vectors
    P. Schatte
    Journal of Computing, Volume 63, Number 1 (1999)

    Since:
    0.3
    Author:
    Erich Schubert
    • Constructor Detail

      • VectorUtil

        private VectorUtil()
        Fake constructor. Do not instantiate, use static methods.
    • Method Detail

      • randomVector

        public static <V extends NumberVector> V randomVector​(NumberVector.Factory<V> factory,
                                                              int dim,
                                                              java.util.Random r)
        Produce a new vector based on random numbers in [0:1].
        Type Parameters:
        V - vector type
        Parameters:
        factory - Vector factory
        dim - desired dimensionality
        r - Random generator
        Returns:
        new instance
      • randomVector

        public static <V extends NumberVector> V randomVector​(NumberVector.Factory<V> factory,
                                                              int dim)
        Produce a new vector based on random numbers in [0:1].
        Type Parameters:
        V - vector type
        Parameters:
        factory - Vector factory
        dim - desired dimensionality
        Returns:
        new instance
      • angleDense

        public static double angleDense​(NumberVector v1,
                                        NumberVector v2)
        Compute the absolute cosine of the angle between two dense vectors.

        To convert it to radians, use Math.acos(angle)!

        Parameters:
        v1 - first vector
        v2 - second vector
        Returns:
        Angle
      • angleSparse

        public static double angleSparse​(SparseNumberVector v1,
                                         SparseNumberVector v2)
        Compute the angle for sparse vectors.
        Parameters:
        v1 - First vector
        v2 - Second vector
        Returns:
        angle
      • angleSparseDense

        public static double angleSparseDense​(SparseNumberVector v1,
                                              NumberVector v2)
        Compute the angle for a sparse and a dense vector.
        Parameters:
        v1 - Sparse first vector
        v2 - Dense second vector
        Returns:
        angle
      • cosAngle

        public static double cosAngle​(NumberVector v1,
                                      NumberVector v2)
        Compute the absolute cosine of the angle between two vectors.

        To convert it to radians, use Math.acos(angle)!

        Parameters:
        v1 - first vector
        v2 - second vector
        Returns:
        Angle
      • minCosAngle

        public static double minCosAngle​(SpatialComparable v1,
                                         SpatialComparable v2)
        Compute the minimum angle between two rectangles.
        Parameters:
        v1 - first rectangle
        v2 - second rectangle
        Returns:
        Angle
      • angle

        public static double angle​(NumberVector v1,
                                   NumberVector v2,
                                   NumberVector o)
        Compute the angle between two vectors with respect to a reference point.
        Parameters:
        v1 - first vector
        v2 - second vector
        o - Origin
        Returns:
        Angle
      • dotDense

        public static double dotDense​(NumberVector v1,
                                      NumberVector v2)
        Compute the dot product of two dense vectors.
        Parameters:
        v1 - first vector
        v2 - second vector
        Returns:
        dot product
      • dotDense

        public static double dotDense​(NumberVector v1,
                                      double[] v2)
        Compute the dot product of two dense vectors.
        Parameters:
        v1 - first vector
        v2 - second vector
        Returns:
        dot product
      • dotSparse

        public static double dotSparse​(SparseNumberVector v1,
                                       SparseNumberVector v2)
        Compute the dot product for two sparse vectors.
        Parameters:
        v1 - First vector
        v2 - Second vector
        Returns:
        dot product
      • dotSparseDense

        public static double dotSparseDense​(SparseNumberVector v1,
                                            NumberVector v2)
        Compute the dot product for a sparse and a dense vector.
        Parameters:
        v1 - Sparse first vector
        v2 - Dense second vector
        Returns:
        dot product
      • dotSparseDense

        public static double dotSparseDense​(SparseNumberVector v1,
                                            double[] v2)
        Compute the dot product for a sparse and a dense vector.
        Parameters:
        v1 - Sparse first vector
        v2 - Dense second vector
        Returns:
        dot product
      • dot

        public static double dot​(NumberVector v1,
                                 NumberVector v2)
        Compute the dot product of the angle between two vectors.
        Parameters:
        v1 - first vector
        v2 - second vector
        Returns:
        Dot product
      • dot

        public static double dot​(NumberVector v1,
                                 double[] v2)
        Compute the dot product of the angle between two vectors.
        Parameters:
        v1 - first vector
        v2 - second vector
        Returns:
        Dot product
      • minDot

        public static double minDot​(SpatialComparable v1,
                                    SpatialComparable v2)
        Compute the minimum angle between two rectangles, assuming unit length vectors
        Parameters:
        v1 - first rectangle
        v2 - second rectangle
        Returns:
        Angle
      • project

        public static <V extends NumberVector> V project​(V v,
                                                         long[] selectedAttributes,
                                                         NumberVector.Factory<V> factory)
        Project a number vector to the specified attributes.
        Type Parameters:
        V - Vector type
        Parameters:
        v - a NumberVector to project
        selectedAttributes - the attributes selected for projection
        factory - Vector factory
        Returns:
        a new NumberVector as a projection on the specified attributes