de.lmu.ifi.dbs.elki.math.linearalgebra
Class Vector

java.lang.Object
  extended by de.lmu.ifi.dbs.elki.math.linearalgebra.Vector
All Implemented Interfaces:
MatrixLike<Vector>, Serializable, Cloneable
Direct Known Subclasses:
Centroid

public class Vector
extends Object
implements MatrixLike<Vector>, Serializable

Provides a vector object that encapsulates an m x 1 - matrix object.

See Also:
Serialized Form

Field Summary
protected  double[] elements
          Array for internal storage of elements.
private static long serialVersionUID
          Serial version
 
Constructor Summary
Vector(double... values)
          Construct a vector from a given array.
Vector(int m)
          Provides an m x 1 vector.
 
Method Summary
private  void checkDimensions(Vector v)
          Check if this.getDimensionality() == v.getDimensionality().
 Vector clone()
          Clone the Vector object.
 Vector copy()
          Returns a copy of this vector.
 boolean equals(Object obj)
           
 double euclideanLength()
          Returns the length of this vector.
 double get(int i)
          Returns the value at the specified row.
 double get(int i, int j)
          Get a single element.
 double[] getArrayCopy()
          Copy the internal two-dimensional array.
 double[] getArrayRef()
          Access the internal two-dimensional array.
 int getColumnDimensionality()
          Returns the dimensionality of the columns of this matrix.
 Vector getColumnVector(int i)
          Returns the ith column of this matrix as vector.
 int getDimensionality()
          Returns the dimensionality of this vector.
 int getRowDimensionality()
          Returns the dimensionality of the rows of this matrix.
 int hashCode()
           
 Vector increment(int i, int j, double s)
          Increments a single element.
 Vector inverseVector()
          Inverts every element of the vector.
 Vector minus(Vector v)
          Returns this vector minus the specified vector v.
 Vector minusEquals(double d)
          Subtract a constant value from all dimensions.
 Vector minusEquals(Vector B)
          A = A - B
 Vector minusTimes(Vector v, double s)
          Returns this vector minus the specified vector v times s.
 Vector minusTimesEquals(Vector B, double s)
          A = A - s * B
 Vector normalize()
          Normalizes this vector to the length of 1.0.
 double normF()
          Frobenius norm
 Vector plus(Vector v)
          Returns a new vector which is the result of this vector plus the specified vector.
 Vector plusEquals(double d)
          Add a constant value to all dimensions.
 Vector plusEquals(Vector B)
          A = A + B
 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.
 Vector plusTimesEquals(Vector B, double s)
          A = A + s * B
 Vector projection(Matrix v)
          Projects this row vector into the subspace formed by the specified matrix v.
static Vector randomNormalizedVector(int dimensionality)
          Returns a randomly created vector of length 1.0
 double scalarProduct(Vector v)
          Returns the scalar product of this vector and the specified vector v.
 Vector set(int i, double value)
          Sets the value at the specified row.
 Vector set(int i, int j, double s)
          Set a single element.
 void setZero()
          Reset the Vector to 0.
 Vector sqrtVector()
          Square roots every element of the vector.
 Vector times(double s)
          Returns a new vector which is the result of this vector multiplied by the specified scalar.
 Matrix times(Matrix B)
          Linear algebraic matrix multiplication, A * B
 Vector timesEquals(double s)
          Multiply a matrix by a scalar in place, A = s*A
 Matrix timesTranspose(Matrix B)
          Linear algebraic matrix multiplication, A * B^T
 String toString()
          Returns a string representation of this vector.
 String toStringNoWhitespace()
          Returns a string representation of this vector without adding extra whitespace
 Matrix transpose()
          Matrix transpose.
 Matrix transposeTimes(Matrix B)
          Linear algebraic matrix multiplication, AT * B
 double transposeTimes(Vector B)
          Linear algebraic matrix multiplication, AT * B
static Vector unitVector(int dimensionality, int i)
          Returns the ith unit vector of the specified dimensionality.
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

serialVersionUID

private static final long serialVersionUID
Serial version

See Also:
Constant Field Values

elements

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

Constructor Detail

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 Detail

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.

Specified by:
copy in interface MatrixLike<Vector>
Returns:
a copy of this vector

clone

public Vector clone()
Clone the Vector object.

Specified by:
clone in interface MatrixLike<Vector>
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.

Returns:
the dimensionality of this vector

getRowDimensionality

public final int getRowDimensionality()
Description copied from interface: MatrixLike
Returns the dimensionality of the rows of this matrix.

Specified by:
getRowDimensionality in interface MatrixLike<Vector>
Returns:
m, the number of rows.

getColumnDimensionality

public final int getColumnDimensionality()
Description copied from interface: MatrixLike
Returns the dimensionality of the columns of this matrix.

Specified by:
getColumnDimensionality in interface MatrixLike<Vector>
Returns:
n, the number of columns.

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

get

public final double get(int i,
                        int j)
Description copied from interface: MatrixLike
Get a single element.

Specified by:
get in interface MatrixLike<Vector>
Parameters:
i - Row index.
j - Column index.
Returns:
A(i,j)

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

set

public final Vector set(int i,
                        int j,
                        double s)
Description copied from interface: MatrixLike
Set a single element.

Specified by:
set in interface MatrixLike<Vector>
Parameters:
i - Row index.
j - Column index.
s - A(i,j).

increment

public final Vector increment(int i,
                              int j,
                              double s)
Description copied from interface: MatrixLike
Increments a single element.

Specified by:
increment in interface MatrixLike<Vector>
Parameters:
i - the row index
j - the column index
s - the increment value: A(i,j) = A(i.j) + s.

getColumnVector

public final Vector getColumnVector(int i)
Description copied from interface: MatrixLike
Returns the ith column of this matrix as vector.

Specified by:
getColumnVector in interface MatrixLike<Vector>
Parameters:
i - the index of the column to be returned
Returns:
the ith column of this matrix

transpose

public final Matrix transpose()
Description copied from interface: MatrixLike
Matrix transpose.

Specified by:
transpose in interface MatrixLike<Vector>
Returns:
AT

plus

public final Vector plus(Vector v)
Returns a new vector which is the result of this vector plus the specified vector.

Specified by:
plus in interface MatrixLike<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.

Specified by:
plusTimes in interface MatrixLike<Vector>
Parameters:
v - the vector to be added
s - the scalar
Returns:
the resulting vector

plusEquals

public final Vector plusEquals(Vector B)
A = A + B

Specified by:
plusEquals in interface MatrixLike<Vector>
Parameters:
B - another matrix
Returns:
A + B in this Matrix

plusTimesEquals

public final Vector plusTimesEquals(Vector B,
                                    double s)
A = A + s * B

Specified by:
plusTimesEquals in interface MatrixLike<Vector>
Parameters:
B - another matrix
s - Scalar
Returns:
A + s * B in this Matrix

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.

Specified by:
minus in interface MatrixLike<Vector>
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.

Specified by:
minusTimes in interface MatrixLike<Vector>
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

Specified by:
minusEquals in interface MatrixLike<Vector>
Parameters:
B - another matrix
Returns:
A - B in this Matrix

minusTimesEquals

public final Vector minusTimesEquals(Vector B,
                                     double s)
A = A - s * B

Specified by:
minusTimesEquals in interface MatrixLike<Vector>
Parameters:
B - another matrix
s - Scalar
Returns:
A - s * B in this Matrix

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.

Specified by:
times in interface MatrixLike<Vector>
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

Specified by:
timesEquals in interface MatrixLike<Vector>
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
Throws:
IllegalArgumentException - Matrix inner dimensions must agree.

transposeTimes

public final Matrix transposeTimes(Matrix B)
Linear algebraic matrix multiplication, AT * B

Parameters:
B - another matrix
Returns:
Matrix product, AT * B
Throws:
IllegalArgumentException - Matrix inner dimensions must agree.

transposeTimes

public final double transposeTimes(Vector B)
Linear algebraic matrix multiplication, AT * B

Parameters:
B - another vector
Returns:
Matrix product, AT * B
Throws:
IllegalArgumentException - Matrix inner dimensions must agree.

timesTranspose

public final Matrix timesTranspose(Matrix B)
Linear algebraic matrix multiplication, A * B^T

Parameters:
B - another matrix
Returns:
Matrix product, A * B^T
Throws:
IllegalArgumentException - Matrix inner dimensions must agree.

scalarProduct

public final double scalarProduct(Vector v)
Returns the scalar product of this vector and the specified vector v.

Parameters:
v - the vector
Returns:
double the scalar product of this vector and v

inverseVector

public final Vector inverseVector()
Inverts every element of the vector.

Returns:
the resulting vector

sqrtVector

public final Vector sqrtVector()
Square roots every element of the vector.

Returns:
the resulting vector

euclideanLength

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

Returns:
the length of this vector

normF

public double normF()
Frobenius norm

Returns:
sqrt of sum of squares of all elements.

normalize

public final Vector normalize()
Normalizes this vector to the length of 1.0.


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
Throws:
IllegalArgumentException - if this matrix is no row vector, i.e. this matrix has more than one column or this matrix and v have different length of rows

checkDimensions

private final void checkDimensions(Vector v)
Check if this.getDimensionality() == v.getDimensionality().

Throws:
IllegalArgumentException - if the dimensions do not agree

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.

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.


Release 0.4.0 (2011-09-20_1324)