com.aliasi.matrix
Class AbstractVector

java.lang.Object
  extended by com.aliasi.matrix.AbstractVector
All Implemented Interfaces:
Vector
Direct Known Subclasses:
DenseVector, SparseFloatVector

public abstract class AbstractVector
extends Object
implements Vector

An AbstractVector implements most of a vector's functionality in terms of methods for dimensionality and values.

Since:
LingPipe2.0
Version:
3.5.1
Author:
Bob Carpenter

Constructor Summary
protected AbstractVector()
          Construct an abstract vector.
 
Method Summary
 Vector add(Vector v)
          Returns the result of adding the specified vector to this vector.
 double cosine(Vector v)
          Returns the cosine of this vector and the specified vector.
 double dotProduct(Vector v)
          Returns the dot product (inner product) of this vector with the specified vector.
 boolean equals(Object that)
          Returns true if the specified object is a vector with the same dimensionality and values as this vector.
 int hashCode()
          Returns the hash code for this vector according to the specification.
 void increment(double scale, Vector v)
          Adds the specified vector multiplied by the specified scalar to this vector.
 double length()
          Returns the length of this vector.
 int[] nonZeroDimensions()
          Returns an array with the non-zero dimensions of this vector.
abstract  int numDimensions()
          Returns the number of dimensions of this vector.
 void setValue(int dimension, double value)
          Throws an unsupported operation exception.
abstract  double value(int dimension)
          The value of this vector for the specified dimension.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AbstractVector

protected AbstractVector()
Construct an abstract vector.

Method Detail

nonZeroDimensions

public int[] nonZeroDimensions()
Returns an array with the non-zero dimensions of this vector. The implementation in this class is makes two passes over the input data, first collecting the count of non-zero dimensions, and then filling in the values.

Specified by:
nonZeroDimensions in interface Vector
Returns:
The non-zero dimensions of this vector.

increment

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

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

numDimensions

public abstract int numDimensions()
Returns the number of dimensions of this vector. Concrete subclasses must implement at least this method and value(int).

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

value

public abstract double value(int dimension)
The value of this vector for the specified dimension. Concrete subclasses must implement at least this method and numDimensions().

Specified by:
value in interface Vector
Parameters:
dimension - Dimension whose value is returned.
Returns:
Value of this vector for the specified dimension.
Throws:
IndexOutOfBoundsException - If the dimension is less than zero or greater than or equal to the number of dimensions.

setValue

public void setValue(int dimension,
                     double value)
Throws an unsupported operation exception. Subclasses with mutable values should override this method.

Specified by:
setValue in interface Vector
Parameters:
dimension - Ignored.
value - Ignored.
Throws:
UnsupportedOperationException - Always.

length

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

the implementation iterates over the dimensions once accessing each value.

Specified by:
length in interface Vector
Returns:
The length of this vector.

add

public Vector add(Vector v)
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
Parameters:
v - Vector to add to this vector.
Returns:
The result of adding the specified vector to this vector.
Throws:
IllegalArgumentException - If the specified vector does not have the same dimensionality as this vector.

dotProduct

public double dotProduct(Vector v)
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
Parameters:
v - The specified vector.
Returns:
The dot product of this vector with the specified vector.
Throws:
IllegalArgumentException - If the specified vector is not of the same dimensionality as this vector.

cosine

public double cosine(Vector v)
Returns the cosine of this vector and the specified vector.

Implementation Note: This method iterates over the dimensions once and accesses the value of each vector once per dimension.

Specified by:
cosine in interface Vector
Parameters:
v - The specified vector.
Returns:
The cosine of this vector with the specified vector.
Throws:
IllegalArgumentException - If the specified vector is not of the same dimensionality as this vector.

equals

public boolean equals(Object that)
Returns true if the specified object is a vector with the same dimensionality and values as this vector. Note that labels are not considered for equality. This implementation is consistent with hash codes.

Specified by:
equals in interface Vector
Overrides:
equals in class Object
Parameters:
that - Specified object.
Returns:
true if the specified object is a vector with the same dimensionality and values as this vector.

hashCode

public int hashCode()
Returns the hash code for this vector according to the specification. This hash code is compatible with equality. Implementation Note: The implementation iterates over the dimensions accessing the value of each dimension once.

Specified by:
hashCode in interface Vector
Overrides:
hashCode in class Object
Returns:
The hash code for this vector.