com.aliasi.matrix
Class DenseVector

java.lang.Object
  extended by com.aliasi.matrix.AbstractVector
      extended by com.aliasi.matrix.DenseVector
All Implemented Interfaces:
Vector, Serializable

public class DenseVector
extends AbstractVector
implements Serializable

A DenseVector is a vector implementation suitable for vectors with primarily non-zero values. The dimensioanality of a dense vector is set at construction time and immutable afterwards. Values may be specified at construction time or given default values. Values may be set later.

Implementation Note: A dense vector represents the values with an array of primitive double values.

Serialization

A dense vector may be serialized and deserialized. The object read back in will be an instance of DenseVector.

Since:
LingPipe2.0
Version:
3.8.1
Author:
Bob Carpenter
See Also:
Serialized Form

Constructor Summary
DenseVector(DenseVector v)
          Constructs a deep copy of the specified vector.
DenseVector(double[] values)
          Construct a dense vector with the specified values.
DenseVector(int numDimensions)
          Construct a dense vector with the specified number of dimensions.
DenseVector(Vector v)
          Cosntruct a deep copy of the specified vector as a dense vector.
 
Method Summary
 Vector add(Vector v)
          Returns the result of adding the specified vector to this vector.
 double dotProduct(Vector v)
          Returns the dot product (inner product) of this vector with the specified vector.
 void increment(double scale, Vector v)
          Adds the specified vector multiplied by the specified scalar to this vector.
 int numDimensions()
          Returns the number of dimensions for this dense vector.
 void setValue(int dimension, double value)
          Sets the value of the specified dimension to the specified value.
 String toString()
          Returns a string representation of the values in this vector.
 double value(int dimension)
          Returns the value of this dense vector for the specified dimension.
 
Methods inherited from class com.aliasi.matrix.AbstractVector
cosine, equals, hashCode, length, nonZeroDimensions
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

DenseVector

public DenseVector(int numDimensions)
Construct a dense vector with the specified number of dimensions. All values will be set to 0.0 initially.

Parameters:
numDimensions - The number of dimensions in this vector.
Throws:
IllegalArgumentException - If the number of dimensions is not positive.

DenseVector

public DenseVector(double[] values)
Construct a dense vector with the specified values. The number of dimensions will be equal to the length of the specified array of values. The specified values are copied, so subsequent changes to the specified values are not reflected in this class.

Parameters:
values - Array of values for the vector.
Throws:
IllegalArgumentException - If the specified values array is zero length.

DenseVector

public DenseVector(DenseVector v)
Constructs a deep copy of the specified vector.

Parameters:
v - Vector to copy.

DenseVector

public DenseVector(Vector v)
Cosntruct a deep copy of the specified vector as a dense vector.

Parameters:
v - Vector to copy.
Method Detail

dotProduct

public double dotProduct(Vector v)
Description copied from class: AbstractVector
Returns the dot product (inner product) of this vector with the specified vector.

Implementation Note: This method iterates over the dimensions, accessing values for this vector and the specified vector for each dimension.

Specified by:
dotProduct in interface Vector
Overrides:
dotProduct in class AbstractVector
Parameters:
v - The specified vector.
Returns:
The dot product of this vector with the specified vector.

setValue

public void setValue(int dimension,
                     double value)
Sets the value of the specified dimension to the specified value.

Specified by:
setValue in interface Vector
Overrides:
setValue in class AbstractVector
Parameters:
dimension - The specified dimension.
value - The new value for the specified dimension.
Throws:
IndexOutOfBoundsException - If the dimension is less than 0 or greather than or equal to the number of dimensions of this vector.

numDimensions

public int numDimensions()
Returns the number of dimensions for this dense vector. The dimensionality is set at construction time and is immutable.

Specified by:
numDimensions in interface Vector
Specified by:
numDimensions in class AbstractVector
Returns:
The number of dimensions of this vector.

increment

public void increment(double scale,
                      Vector v)
Adds the specified vector multiplied by the specified scalar to this vector.

Implementation Note: This class implements this operationg sparsely if the specified argument vector is an instance of SparseFloatVector. Otherwise, it visits every dimension of the specified vector and this vector.

Specified by:
increment in interface Vector
Overrides:
increment in class AbstractVector
Parameters:
scale - The scalar multiplier for the added vector.
v - Vector to scale and add to this vector.
Throws:
IllegalArgumentException - If the specified vector is not of the same dimensionality as this vector.

add

public Vector add(Vector v)
Description copied from class: AbstractVector
Returns the result of adding the specified vector to this vector.

Implementation Note: The result is a dense vector and this method iterates over the dimensions adding. Subclasses may override this with a more specific implementation and then fall back on this implementation for the general case.

Specified by:
add in interface Vector
Overrides:
add in class AbstractVector
Parameters:
v - Vector to add to this vector.
Returns:
The result of adding the specified vector to this vector.

value

public double value(int dimension)
Returns the value of this dense vector for the specified dimension.

Specified by:
value in interface Vector
Specified by:
value in class AbstractVector
Parameters:
dimension - Specified dimension.
Returns:
The value of this vector for the specified dimension.
Throws:
IndexOutOfBoundsException - If the dimension is less than 0 or greather than or equal to the number of dimensions of this vector.

toString

public String toString()
Returns a string representation of the values in this vector.

Overrides:
toString in class Object
Returns:
A string representation of this vector.