gov.sandia.cognition.math.matrix.mtj
Class SparseVector

java.lang.Object
  extended by gov.sandia.cognition.util.AbstractCloneableSerializable
      extended by gov.sandia.cognition.math.AbstractRing<VectorType>
          extended by gov.sandia.cognition.math.matrix.AbstractVectorSpace<Vector,VectorEntry>
              extended by gov.sandia.cognition.math.matrix.AbstractVector
                  extended by gov.sandia.cognition.math.matrix.mtj.AbstractMTJVector
                      extended by gov.sandia.cognition.math.matrix.mtj.SparseVector
All Implemented Interfaces:
Vector, Vectorizable, VectorSpace<Vector,VectorEntry>, Ring<Vector>, CloneableSerializable, Serializable, Cloneable, Iterable<VectorEntry>

@CodeReview(reviewer="Justin Basilico",
            date="2006-07-27",
            changesNeeded=false,
            comments="Looks good.")
public class SparseVector
extends AbstractMTJVector

A vector that only stores the nonzero elements, relies on MTJ's SparseVector. It has fast traversal by skipping nonzero elements but has slow indexing.

Since:
1.0
Author:
Kevin R. Dixon
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from interface gov.sandia.cognition.math.matrix.VectorSpace
VectorSpace.Entry
 
Field Summary
 
Fields inherited from class gov.sandia.cognition.math.matrix.AbstractVector
DEFAULT_DELIMITER
 
Constructor Summary
protected SparseVector(int numDimensions)
          Creates a new instance of SparseVector with no initial size.
protected SparseVector(int numDimensions, int initialNonZeros)
          Creates a new instance of SparseVector with a specified initial size.
protected SparseVector(no.uib.cipr.matrix.sparse.SparseVector internalVector)
          Creates a new copy of SparseVector.
protected SparseVector(Vector vector)
          Creates a new copy of SparseVector.
 
Method Summary
 void compact()
          Compacts the SparseVector, getting rid of any zero'ed elements.
 double euclideanDistanceSquared(Vector other)
          Squared Euclidean distance between this and other, which is the 2-norm between the difference of the Vectors
protected  no.uib.cipr.matrix.sparse.SparseVector getInternalVector()
          Getter for internalVector
 int getNumElementsUsed()
          Gets the number of elements used inside the sparse vector, equals the allocation size of the vector.
 boolean isSparse()
          Returns true if this vector has a potentially sparse underlying structure.
 SparseRowMatrix outerProduct(AbstractMTJVector other)
          Computes the outer matrix product between the two vectors
 void setElement(int index, double value)
          Sets the zero-based indexed element in the Vector from the specified value
protected  void setInternalVector(no.uib.cipr.matrix.sparse.SparseVector internalVector)
          Setter for the internal MTJ vector.
 SparseVector stack(Vector other)
          Stacks "other" below "this" and returns the stacked Vector
 SparseVector subVector(int minIndex, int maxIndex)
          Gets a subvector of "this", specified by the inclusive indices
 String toString()
          Prints the SparseVector in "Index: Value" format.
 
Methods inherited from class gov.sandia.cognition.math.matrix.mtj.AbstractMTJVector
clone, dotProduct, dotProduct, dotTimesEquals, getDimensionality, getElement, iterator, minusEquals, minusEquals, norm2, norm2Squared, outerProduct, plusEquals, plusEquals, scaledMinusEquals, scaledPlusEquals, scaledPlusEquals, scaleEquals, setInternalVector, times, times, zero
 
Methods inherited from class gov.sandia.cognition.math.matrix.AbstractVector
assertDimensionalityEquals, assertEqualDimensionality, assertSameDimensionality, checkSameDimensionality, convertFromVector, convertToVector, dotDivide, dotDivideEquals, equals, equals, hashCode, toArray, toString, toString, transform, transformEquals, transformNonZeros, transformNonZerosEquals
 
Methods inherited from class gov.sandia.cognition.math.matrix.AbstractVectorSpace
angle, cosine, euclideanDistance, isUnitVector, isUnitVector, isZero, norm, norm1, normInfinity, sum, unitVector, unitVectorEquals
 
Methods inherited from class gov.sandia.cognition.math.AbstractRing
dotTimes, isZero, minus, negative, negativeEquals, plus, scale, scaledMinus, scaledMinusEquals, scaledPlus
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface gov.sandia.cognition.math.matrix.VectorSpace
angle, cosine, euclideanDistance, isUnitVector, isUnitVector, norm, norm1, normInfinity, sum, unitVector, unitVectorEquals
 
Methods inherited from interface gov.sandia.cognition.math.Ring
dotTimes, isZero, isZero, minus, negative, negativeEquals, plus, scale, scaledMinus, scaledMinusEquals, scaledPlus
 

Constructor Detail

SparseVector

protected SparseVector(int numDimensions)
Creates a new instance of SparseVector with no initial size.

Parameters:
numDimensions - Maximum number of entries in the SparseVector.

SparseVector

protected SparseVector(int numDimensions,
                       int initialNonZeros)
Creates a new instance of SparseVector with a specified initial size.

Parameters:
numDimensions - Maximum number of entries in the SparseVector.
initialNonZeros - Initial size of the SparseVector.

SparseVector

protected SparseVector(no.uib.cipr.matrix.sparse.SparseVector internalVector)
Creates a new copy of SparseVector.

Parameters:
internalVector - Internal MTJ-based vector to set into this.

SparseVector

protected SparseVector(Vector vector)
Creates a new copy of SparseVector.

Parameters:
vector - Vector to copy into this, will not be modified.
Method Detail

getNumElementsUsed

public int getNumElementsUsed()
Gets the number of elements used inside the sparse vector, equals the allocation size of the vector.

Returns:
The number of nonzero elements in the SparseVector.

setElement

public void setElement(int index,
                       double value)
Description copied from interface: Vector
Sets the zero-based indexed element in the Vector from the specified value

Specified by:
setElement in interface Vector
Overrides:
setElement in class AbstractMTJVector
Parameters:
index - zero-based index
value - value to set the element in the Vector

getInternalVector

protected no.uib.cipr.matrix.sparse.SparseVector getInternalVector()
Description copied from class: AbstractMTJVector
Getter for internalVector

Overrides:
getInternalVector in class AbstractMTJVector
Returns:
MTJ-based internal vector that does the heavy lifting

setInternalVector

protected void setInternalVector(no.uib.cipr.matrix.sparse.SparseVector internalVector)
Setter for the internal MTJ vector.

Parameters:
internalVector - Internal MTJ-based vector.

euclideanDistanceSquared

public double euclideanDistanceSquared(Vector other)
Description copied from interface: VectorSpace
Squared Euclidean distance between this and other, which is the 2-norm between the difference of the Vectors

Parameters:
other - Vector to which to compute the squared distance, must be the same dimension as this
Returns:
this.minus( other ).norm2Squared(), which is [0,\infty)

outerProduct

public SparseRowMatrix outerProduct(AbstractMTJVector other)
Description copied from class: AbstractMTJVector
Computes the outer matrix product between the two vectors

Specified by:
outerProduct in class AbstractMTJVector
Parameters:
other - post-multiplied Vector with which to compute the outer product
Returns:
Outer matrix product, which will have dimensions (this.getDimensionality x other.getDimensionality) and WILL BE singular

toString

public String toString()
Prints the SparseVector in "Index: Value" format.

Specified by:
toString in interface Vector
Overrides:
toString in class AbstractVector
Returns:
String containing the representation of the SparseVector

compact

public void compact()
Compacts the SparseVector, getting rid of any zero'ed elements.


stack

public SparseVector stack(Vector other)
Description copied from interface: Vector
Stacks "other" below "this" and returns the stacked Vector

Parameters:
other - Vector to stack below "this"
Returns:
stacked Vector

isSparse

public boolean isSparse()
Description copied from interface: Vector
Returns true if this vector has a potentially sparse underlying structure. This can indicate that it is faster to only process the non-zero elements rather than to do dense operations on it. Of course, even with a sparse structure, there may be no zero elements or conversely even with a non-sparse (dense) structure there may be many zero elements.

Returns:
True if the vector has a potentially sparse structure. Otherwise, false.

subVector

public SparseVector subVector(int minIndex,
                              int maxIndex)
Description copied from interface: Vector
Gets a subvector of "this", specified by the inclusive indices

Parameters:
minIndex - minimum index to get (inclusive)
maxIndex - maximum index to get (inclusive)
Returns:
vector of dimension (maxIndex-minIndex+1)