Module math

Class Vector

java.lang.Object
de.lmu.ifi.dbs.elki.math.linearalgebra.Vector
All Implemented Interfaces:
FeatureVector<Double>, NumberVector<Double>, SpatialComparable

public class Vector extends Object implements NumberVector<Double>
Provides a vector object that encapsulates an m x 1 - matrix object.
Author:
Elke Achtert
  • Field Details

    • elements

      protected final double[] elements
      Array for internal storage of elements.
    • ERR_VEC_DIMENSIONS

      public static final String ERR_VEC_DIMENSIONS
      Error message (in assertions!) when vector dimensionalities do not agree.
      See Also:
    • ERR_MATRIX_INNERDIM

      public static final String ERR_MATRIX_INNERDIM
      Error message (in assertions!) when matrix dimensionalities do not agree.
      See Also:
  • Constructor Details

    • Vector

      public Vector(double... values)
      Construct a vector from a given array.
      Parameters:
      values - array of doubles
    • Vector

      public Vector(int m)
      Provides an m x 1 vector.
      Parameters:
      m - the number of rows
  • Method Details

    • randomNormalizedVector

      public static final Vector randomNormalizedVector(int dimensionality)
      Returns a randomly created vector of length 1.0.
      Parameters:
      dimensionality - dimensionality
      Returns:
      the dimensionality of the vector
    • unitVector

      public static final Vector unitVector(int dimensionality, int i)
      Returns the ith unit vector of the specified dimensionality.
      Parameters:
      dimensionality - the dimensionality of the vector
      i - the index
      Returns:
      the ith unit vector of the specified dimensionality
    • copy

      public final Vector copy()
      Returns a copy of this vector.
      Returns:
      a copy of this vector
    • clone

      public Vector clone()
      Overrides:
      clone in class Object
    • getArrayRef

      public final double[] getArrayRef()
      Access the internal two-dimensional array.
      Returns:
      Pointer to the two-dimensional array of matrix elements.
    • getArrayCopy

      public final double[] getArrayCopy()
      Copy the internal two-dimensional array.
      Returns:
      Two-dimensional array copy of matrix elements.
    • getDimensionality

      public final int getDimensionality()
      Returns the dimensionality of this vector.
      Specified by:
      getDimensionality in interface FeatureVector<Double>
      Specified by:
      getDimensionality in interface SpatialComparable
      Returns:
      the dimensionality of this vector
    • get

      public final double get(int i)
      Returns the value at the specified row.
      Parameters:
      i - the row index
      Returns:
      the value at row i
    • set

      public final Vector set(int i, double value)
      Sets the value at the specified row.
      Parameters:
      i - the row index
      value - the value to be set
      Returns:
      the modified vector
    • plus

      public final Vector plus(Vector v)
      Returns a new vector which is the result of this vector plus the specified vector.
      Parameters:
      v - the vector to be added
      Returns:
      the resulting vector
    • plusTimes

      public final Vector plusTimes(Vector v, double s)
      Returns a new vector which is the result of this vector plus the specified vector times the given factor.
      Parameters:
      v - the vector to be added
      s - the scalar
      Returns:
      the resulting vector
    • plusEquals

      public final Vector plusEquals(Vector b)
      a = a + b.
      Parameters:
      b - another vector
      Returns:
      a + b in this vector
    • plusTimesEquals

      public final Vector plusTimesEquals(Vector b, double s)
      a = a + s * b.
      Parameters:
      b - another vector
      s - Scalar
      Returns:
      a + s * b in this vector
    • plusEquals

      public final Vector plusEquals(double d)
      Add a constant value to all dimensions.
      Parameters:
      d - Value to add
      Returns:
      Modified vector
    • minus

      public final Vector minus(Vector v)
      Returns this vector minus the specified vector v.
      Parameters:
      v - the vector to be subtracted from this vector
      Returns:
      this vector minus the specified vector v
    • minusTimes

      public final Vector minusTimes(Vector v, double s)
      Returns this vector minus the specified vector v times s.
      Parameters:
      v - the vector to be subtracted from this vector
      s - the scaling factor
      Returns:
      this vector minus the specified vector v
    • minusEquals

      public final Vector minusEquals(Vector b)
      a = a - b.
      Parameters:
      b - another vector
      Returns:
      a - b in this vector
    • minusTimesEquals

      public final Vector minusTimesEquals(Vector b, double s)
      a = a - s * b.
      Parameters:
      b - another vector
      s - Scalar
      Returns:
      a - s * b in this vector
    • minusEquals

      public final Vector minusEquals(double d)
      Subtract a constant value from all dimensions.
      Parameters:
      d - Value to subtract
      Returns:
      Modified vector
    • times

      public final Vector times(double s)
      Returns a new vector which is the result of this vector multiplied by the specified scalar.
      Parameters:
      s - the scalar to be multiplied
      Returns:
      the resulting vector
    • timesEquals

      public final Vector timesEquals(double s)
      Multiply a matrix by a scalar in place, A = s*A.
      Parameters:
      s - scalar
      Returns:
      replace A by s*A
    • times

      public final Matrix times(Matrix B)
      Linear algebraic matrix multiplication, A * B.
      Parameters:
      B - another matrix
      Returns:
      Matrix product, A * B
    • transposeTimes

      public final Matrix transposeTimes(Matrix B)
      Linear algebraic matrix multiplication, AT * B.
      Parameters:
      B - another matrix
      Returns:
      Matrix product, AT * B
    • transposeTimesTimes

      public final double transposeTimesTimes(Matrix B, Vector c)
      Linear algebraic matrix multiplication, aT * B * c.
      Parameters:
      B - matrix
      c - vector on the right
      Returns:
      Matrix product, aT * B * c
    • transposeTimes

      public final double transposeTimes(Vector B)
      Linear algebraic matrix multiplication, AT * B.
      Parameters:
      B - another vector
      Returns:
      Matrix product, AT * B
    • timesTranspose

      public final Matrix timesTranspose(Matrix B)
      Linear algebraic matrix multiplication, A * B^T.
      Parameters:
      B - another matrix
      Returns:
      Matrix product, A * B^T
    • timesTranspose

      public final Matrix timesTranspose(Vector B)
      Linear algebraic matrix multiplication, A * B^T.
      Parameters:
      B - another matrix
      Returns:
      Matrix product, A * B^T
    • euclideanLength

      public final double euclideanLength()
      Returns the length of this vector.
      Returns:
      the length of this vector
    • normalize

      public final Vector normalize()
      Normalizes this vector to the length of 1.0.
      Returns:
      this vector
    • projection

      public final Vector projection(Matrix v)
      Projects this row vector into the subspace formed by the specified matrix v.
      Parameters:
      v - the subspace matrix
      Returns:
      the projection of p into the subspace formed by v
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • toString

      public final String toString()
      Returns a string representation of this vector.
      Specified by:
      toString in interface FeatureVector<Double>
      Overrides:
      toString in class Object
      Returns:
      a string representation of this vector.
    • toStringNoWhitespace

      public final String toStringNoWhitespace()
      Returns a string representation of this vector without adding extra whitespace.
      Returns:
      a string representation of this vector.
    • setZero

      public void setZero()
      Reset the Vector to 0.
    • rotate90Equals

      public Vector rotate90Equals()
      Rotate vector by 90 degrees.
      Returns:
      self, for operation chaining.
    • cross3D

      public Vector cross3D(Vector other)
      Cross product for 3d vectors, i.e. this x other
      Parameters:
      other - Other vector
      Returns:
      Cross product of this vector and the other vector
    • getMin

      public double getMin(int dimension)
      Description copied from interface: SpatialComparable
      Returns the minimum coordinate at the specified dimension.
      Specified by:
      getMin in interface SpatialComparable
      Parameters:
      dimension - the dimension for which the coordinate should be returned, where 0 ≤ dimension < getDimensionality()
      Returns:
      the minimum coordinate at the specified dimension
    • getMax

      public double getMax(int dimension)
      Description copied from interface: SpatialComparable
      Returns the maximum coordinate at the specified dimension.
      Specified by:
      getMax in interface SpatialComparable
      Parameters:
      dimension - the dimension for which the coordinate should be returned, where 0 ≤ dimension < getDimensionality()
      Returns:
      the maximum coordinate at the specified dimension
    • getValue

      @Deprecated public Double getValue(int dimension)
      Deprecated.
      Description copied from interface: FeatureVector
      Returns the value in the specified dimension.
      Specified by:
      getValue in interface FeatureVector<Double>
      Specified by:
      getValue in interface NumberVector<Double>
      Parameters:
      dimension - the desired dimension, where 0 ≤ dimension ≤ this.getDimensionality()-1
      Returns:
      the value in the specified dimension
    • doubleValue

      public double doubleValue(int dimension)
      Description copied from interface: NumberVector
      Returns the value in the specified dimension as double. Note: this might seem redundant with respect to getValue(dim).doubleValue(), but usually this is much more efficient due to boxing/unboxing cost.
      Specified by:
      doubleValue in interface NumberVector<Double>
      Parameters:
      dimension - the desired dimension, where 0 ≤ dimension < this.getDimensionality()
      Returns:
      the value in the specified dimension
    • floatValue

      public float floatValue(int dimension)
      Description copied from interface: NumberVector
      Returns the value in the specified dimension as float. Note: this might seem redundant with respect to getValue(dim).floatValue(), but usually this is much more efficient due to boxing/unboxing cost.
      Specified by:
      floatValue in interface NumberVector<Double>
      Parameters:
      dimension - the desired dimension, where 0 ≤ dimension < this.getDimensionality()
      Returns:
      the value in the specified dimension
    • intValue

      public int intValue(int dimension)
      Description copied from interface: NumberVector
      Returns the value in the specified dimension as int. Note: this might seem redundant with respect to getValue(dim).intValue(), but usually this is much more efficient due to boxing/unboxing cost.
      Specified by:
      intValue in interface NumberVector<Double>
      Parameters:
      dimension - the desired dimension, where 0 ≤ dimension < this.getDimensionality()
      Returns:
      the value in the specified dimension
    • longValue

      public long longValue(int dimension)
      Description copied from interface: NumberVector
      Returns the value in the specified dimension as long. Note: this might seem redundant with respect to getValue(dim).longValue(), but usually this is much more efficient due to boxing/unboxing cost.
      Specified by:
      longValue in interface NumberVector<Double>
      Parameters:
      dimension - the desired dimension, where 0 ≤ dimension < this.getDimensionality()
      Returns:
      the value in the specified dimension
    • shortValue

      public short shortValue(int dimension)
      Description copied from interface: NumberVector
      Returns the value in the specified dimension as short. Note: this might seem redundant with respect to getValue(dim).shortValue(), but usually this is much more efficient due to boxing/unboxing cost.
      Specified by:
      shortValue in interface NumberVector<Double>
      Parameters:
      dimension - the desired dimension, where 0 ≤ dimension < this.getDimensionality()
      Returns:
      the value in the specified dimension
    • byteValue

      public byte byteValue(int dimension)
      Description copied from interface: NumberVector
      Returns the value in the specified dimension as byte. Note: this might seem redundant with respect to getValue(dim).byteValue(), but usually this is much more efficient due to boxing/unboxing cost.
      Specified by:
      byteValue in interface NumberVector<Double>
      Parameters:
      dimension - the desired dimension, where 0 ≤ dimension < this.getDimensionality()
      Returns:
      the value in the specified dimension
    • getColumnVector

      public Vector getColumnVector()
      Description copied from interface: NumberVector
      Returns a Vector representing in one column and getDimensionality() rows the values of this NumberVector of V.
      Specified by:
      getColumnVector in interface NumberVector<Double>
      Returns:
      a Matrix representing in one column and getDimensionality() rows the values of this NumberVector of V