SuanShu, a Java numerical and statistical library

com.numericalmethod.suanshu.vector.doubles.dense
Class DenseVector

java.lang.Object
  extended by com.numericalmethod.suanshu.vector.doubles.dense.DenseVector
All Implemented Interfaces:
DeepCopyable, AbelianGroup<Vector>, BanachSpace<Vector,Real>, HilbertSpace<Vector,Real>, VectorSpace<Vector,Real>, Vector
Direct Known Subclasses:
Basis

public class DenseVector
extends java.lang.Object

This class implements the standard double dense vector representation.

See Also:
Wikipedia: Euclidean vector

Field Summary
 int length
          length of this vector
 
Constructor Summary
DenseVector(DenseVector vector)
          Copy constructor performing a deep copy.
DenseVector(double... data)
          Construct a vector from a 1D double array.
DenseVector(int length)
          Construct a vector of length length.
DenseVector(int length, double value)
          Construct a vector of length length initialized with value value.
DenseVector(Matrix A)
          Construct a vector from a column matrix.
DenseVector(Vector v)
           
 
Method Summary
 DenseVector add(DenseVector that)
          this + that
 Vector add(double scalar)
          v + s Add a scalar to all entries in the vector v.
 Vector add(Vector that)
          this + that
 double angle(Vector that)
          Measure the angle between this and that.
static DenseVector as(Vector v)
          Convert a vector to type DenseVector.
 DenseVector deepCopy()
          The implementation can return an instance created from this by the copy constructor of the class, or just this if the instance itself is immutable.
 DenseVector divide(DenseVector v)
          this / v Divide this by v, entry-by-entry.
 Vector divide(Vector that)
          this / that Divide this by that, entry-by-entry.
 boolean equals(java.lang.Object obj)
          IsVector the equality of two vectors, i.e., whether all entries are equal.
 double get(int index)
          Get the value at position index.
 int hashCode()
           
 double innerProduct(Vector that)
          Inner product in the Euclidean space is the dot product.
 DenseVector minus(DenseVector that)
          this - that
 Vector minus(double scalar)
          v - s Subtract a scalar from all entries in the vector v.
 Vector minus(Vector that)
          this - that
 DenseVector multiply(DenseVector v)
          this * v Multiply this by v, entry-by-entry.
 Vector multiply(Vector v)
          this * that Multiply this by that, entry-by-entry.
 double norm()
          Compute the length or magnitude or Euclidean norm of a vector, namely, ||v||.
 double norm(int p)
          Compute the norm of a vector.
 Vector opposite()
          Get the opposite of this vector.
 Vector pow(double scalar)
          v ^ s Take the exponentiation of all entries in the vector v by a scalar.
 Vector scaled(double scalar)
          scalar * this Here is a way to get a unit version of the vector: vector.scaled(1. / vector.norm())
 Vector scaled(Real scalar)
          scalar * that If scalar is 1, it simply returns itself.
 void set(int from, DenseVector replacement)
          Replace v[from : replacement.length] by a replacement starting at position from.
 void set(int index, double value)
          Change the value of an entry in this vector.
 int size()
          Get the length of this vector.
 double[] toArray()
          Get a copy of all vector entries in the form of a 1D double[] array.
 java.lang.String toString()
           
 DenseVector ZERO()
          Get a 0-vector that has the same length as this vector.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

length

public final int length
length of this vector

Constructor Detail

DenseVector

public DenseVector(int length)
Construct a vector of length length.

Parameters:
length - length of this vector

DenseVector

public DenseVector(int length,
                   double value)
Construct a vector of length length initialized with value value.

Parameters:
length - length of this vector
value - initial values

DenseVector

public DenseVector(double... data)
Construct a vector from a 1D double array.

Parameters:
data - the 1D array input

DenseVector

public DenseVector(Matrix A)
Construct a vector from a column matrix.

Parameters:
A - a n x 1 column matrix

DenseVector

public DenseVector(Vector v)

DenseVector

public DenseVector(DenseVector vector)
Copy constructor performing a deep copy.

Parameters:
vector - the vector to be copied
Method Detail

as

public static DenseVector as(Vector v)
Convert a vector to type DenseVector. The returned value is independent and can be modified anyhow.

Parameters:
v - a vector
Returns:
a DenseMatrix

size

public int size()
Description copied from interface: Vector
Get the length of this vector.

Returns:
the length of this vector

set

public void set(int index,
                double value)
Description copied from interface: Vector
Change the value of an entry in this vector.

The indices are, for example,: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ......

Note that we count from 1, NOT 0.

This is the only method that may change the entries of a vector.

Parameters:
index - the entry to change
value - the value to change to

set

public void set(int from,
                DenseVector replacement)
Replace v[from : replacement.length] by a replacement starting at position from.

Parameters:
from - starting position of the replacement
replacement - an vector to substitute part of this vector
Throws:
java.lang.IllegalArgumentException - if the replacement length exceeds the end of this vector

get

public double get(int index)
Description copied from interface: Vector
Get the value at position index.

Parameters:
index - position of a vector entry
Returns:
v[index]

add

public Vector add(Vector that)
Description copied from interface: Vector
this + that

Specified by:
add in interface AbelianGroup<Vector>
Specified by:
add in interface Vector
Parameters:
that - a vector
Returns:
a vector which is the sum of this and that

add

public DenseVector add(DenseVector that)
this + that

Parameters:
that - a vector
Returns:
a vector which is the sum of this and that

minus

public Vector minus(Vector that)
Description copied from interface: Vector
this - that

Specified by:
minus in interface AbelianGroup<Vector>
Specified by:
minus in interface Vector
Parameters:
that - a vector
Returns:
a vector which is the difference between this and that

minus

public DenseVector minus(DenseVector that)
this - that

Parameters:
that - a vector
Returns:
a vector which is the difference between this and that

scaled

public Vector scaled(double scalar)
Description copied from interface: Vector
scalar * this

Here is a way to get a unit version of the vector:

vector.scaled(1. / vector.norm())

Specified by:
scaled in interface Vector
Parameters:
scalar - a double scalar
Returns:
a vector which is scaled by scalar

multiply

public Vector multiply(Vector v)
Description copied from interface: Vector
this * that

Multiply this by that, entry-by-entry.

Specified by:
multiply in interface Vector
Parameters:
v - a vector
Returns:
this * that, entry-by-entry

multiply

public DenseVector multiply(DenseVector v)
this * v

Multiply this by v, entry-by-entry.

Parameters:
v - a vector
Returns:
this * v, entry-by-entry

divide

public Vector divide(Vector that)
Description copied from interface: Vector
this / that

Divide this by that, entry-by-entry.

Specified by:
divide in interface Vector
Parameters:
that - a vector
Returns:
this / that, entry-by-entry

divide

public DenseVector divide(DenseVector v)
this / v

Divide this by v, entry-by-entry.

Parameters:
v - a vector
Returns:
this / v, entry-by-entry

add

public Vector add(double scalar)
Description copied from interface: Vector
v + s

Add a scalar to all entries in the vector v.

Parameters:
scalar - s
Returns:
v + s

minus

public Vector minus(double scalar)
Description copied from interface: Vector
v - s

Subtract a scalar from all entries in the vector v.

Parameters:
scalar - s
Returns:
v - s

pow

public Vector pow(double scalar)
Description copied from interface: Vector
v ^ s

Take the exponentiation of all entries in the vector v by a scalar.

Specified by:
pow in interface Vector
Parameters:
scalar - s
Returns:
v ^ s

norm

public double norm(int p)
Description copied from interface: Vector
Compute the norm of a vector. When p is finite,
 |v|p = ∑ [(abs(vi)^p)]^(1/p)
 

When p is +∞ (Integer.MAX_VALUE),

 |v|p = max(abs(vi))
 

When p is -∞ (Integer.MIN_VALUE),

 |v|p = min(abs(vi))
 

Parameters:
p - an integer, Integer.MAX_VALUE, or Integer.MIN_VALUE
Returns:
|v|p

innerProduct

public double innerProduct(Vector that)
Description copied from interface: Vector
Inner product in the Euclidean space is the dot product.

Dot product is an operation which takes two vectors over the real numbers R and returns a real-valued scalar quantity.

Specified by:
innerProduct in interface HilbertSpace<Vector,Real>
Specified by:
innerProduct in interface Vector
Parameters:
that - a vector
Returns:
dot product of two vectors, this and that, i.e.,
this ∙ that
See Also:
Wikipedia: Dot product

ZERO

public DenseVector ZERO()
Description copied from interface: Vector
Get a 0-vector that has the same length as this vector.

Returns:
a 0-vector, which has the same length as this vector

toArray

public double[] toArray()
Description copied from interface: Vector
Get a copy of all vector entries in the form of a 1D double[] array.

Returns:
a copy of all vector entries as a double[]

deepCopy

public DenseVector deepCopy()
Description copied from interface: DeepCopyable
The implementation can return an instance created from this by the copy constructor of the class, or just this if the instance itself is immutable.

Returns:
an independent (deep) copy of the instance

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

equals

public boolean equals(java.lang.Object obj)
IsVector the equality of two vectors, i.e., whether all entries are equal.

Overrides:
equals in class java.lang.Object
Parameters:
obj - an vector
Returns:
true iff all this and obj are equal, entry-by-entry

hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object

scaled

public Vector scaled(Real scalar)
Description copied from interface: Vector
scalar * that

If scalar is 1, it simply returns itself. So, here is a way to get a unit version of the vector:

vector.scaled(1. / vector.norm())

Specified by:
scaled in interface VectorSpace<Vector,Real>
Specified by:
scaled in interface Vector
Parameters:
scalar - a Real number scalar
Returns:
a vector which is scaled by scalar
See Also:
Wikipedia: Scalar multiplication

opposite

public Vector opposite()
Description copied from interface: Vector
Get the opposite of this vector.

Specified by:
opposite in interface AbelianGroup<Vector>
Specified by:
opposite in interface Vector
Returns:
-v
See Also:
Wikipedia: Additive inverse

angle

public double angle(Vector that)
Description copied from interface: Vector
Measure the angle between this and that.

That is,

this ∙ that = ||this|| * ||that|| cos(angle)

Specified by:
angle in interface HilbertSpace<Vector,Real>
Specified by:
angle in interface Vector
Parameters:
that - a vector
Returns:
the angle between this and that

norm

public double norm()
Description copied from interface: Vector
Compute the length or magnitude or Euclidean norm of a vector, namely, ||v||.

Specified by:
norm in interface BanachSpace<Vector,Real>
Specified by:
norm in interface Vector
Returns:
the Euclidean norm
See Also:
Wikipedia: Norm (mathematics)

SuanShu, a Java numerical and statistical library

Copyright © 2011 Numerical Method Inc. Ltd. All Rights Reserved.