org.apache.mahout.math
Class SequentialAccessSparseVector

java.lang.Object
  extended by org.apache.mahout.math.AbstractVector
      extended by org.apache.mahout.math.SequentialAccessSparseVector
All Implemented Interfaces:
java.lang.Cloneable, java.lang.Iterable<Vector.Element>, Vector

public class SequentialAccessSparseVector
extends AbstractVector

Implements vector that only stores non-zero doubles as a pair of parallel arrays (OrderedIntDoubleMapping), one int[], one double[]. If there are k non-zero elements in the vector, this implementation has O(log(k)) random-access read performance, and O(k) random-access write performance, which is far below that of the hashmap based RandomAccessSparseVector. This class is primarily used for operations where the all the elements will be accessed in a read-only fashion sequentially: methods which operate not via get() or set(), but via iterateNonZero(), such as (but not limited to) :

Note that the Vector passed to these above methods may (and currently, are) be used in a random access fashion, so for example, calling SequentialAccessSparseVector.dot(SequentialAccessSparseVector) is slow. TODO: this need not be the case - both are ordered, so this should be very fast if implmented in this class

See OrderedIntDoubleMapping


Nested Class Summary
 
Nested classes/interfaces inherited from class org.apache.mahout.math.AbstractVector
AbstractVector.LocalElement
 
Nested classes/interfaces inherited from interface org.apache.mahout.math.Vector
Vector.Element
 
Field Summary
 
Fields inherited from class org.apache.mahout.math.AbstractVector
lengthSquared
 
Constructor Summary
SequentialAccessSparseVector()
          For serialization purposes only.
SequentialAccessSparseVector(int cardinality)
           
SequentialAccessSparseVector(int cardinality, int size)
           
SequentialAccessSparseVector(SequentialAccessSparseVector other)
           
SequentialAccessSparseVector(SequentialAccessSparseVector other, boolean shallowCopy)
           
SequentialAccessSparseVector(Vector other)
           
 
Method Summary
 Vector assign(Vector other)
          Assign the other vector values to the receiver
 SequentialAccessSparseVector clone()
          Return a copy of the recipient
 double dot(Vector x)
          Return the dot product of the recipient and the argument
 int getNumNondefaultElements()
          Return the number of values in the recipient
 double getQuick(int index)
          Return the value at the given index, without checking bounds
 boolean isDense()
           
 boolean isSequentialAccess()
           
 java.util.Iterator<Vector.Element> iterateNonZero()
          Iterates over all non-zero elements.
 java.util.Iterator<Vector.Element> iterator()
          Iterates over all elements

* NOTE: Implementations may choose to reuse the Element returned for performance reasons, so if you need a copy of it, you should call Vector.getElement(int) for the given index

 SequentialAccessSparseVector like()
          Return an empty vector of the same underlying class as the receiver
protected  Matrix matrixLike(int rows, int columns)
          Subclasses must override to return an appropriately sparse or dense result
 Vector minus(Vector that)
          Return a new vector containing the element by element difference of the recipient and the argument
 void setQuick(int index, double value)
          Set the value at the given index, without checking bounds
 java.lang.String toString()
           
 
Methods inherited from class org.apache.mahout.math.AbstractVector
addTo, aggregate, aggregate, asFormatString, assign, assign, assign, assign, assign, cross, decodeVector, divide, dotSelf, equals, get, getDistanceSquared, getElement, getLengthSquared, hashCode, logNormalize, logNormalize, logNormalize, maxValue, maxValueIndex, minValue, minValueIndex, norm, normalize, normalize, plus, plus, set, size, times, times, viewPart, zSum
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

SequentialAccessSparseVector

public SequentialAccessSparseVector()
For serialization purposes only.


SequentialAccessSparseVector

public SequentialAccessSparseVector(int cardinality)

SequentialAccessSparseVector

public SequentialAccessSparseVector(int cardinality,
                                    int size)

SequentialAccessSparseVector

public SequentialAccessSparseVector(Vector other)

SequentialAccessSparseVector

public SequentialAccessSparseVector(SequentialAccessSparseVector other,
                                    boolean shallowCopy)

SequentialAccessSparseVector

public SequentialAccessSparseVector(SequentialAccessSparseVector other)
Method Detail

matrixLike

protected Matrix matrixLike(int rows,
                            int columns)
Description copied from class: AbstractVector
Subclasses must override to return an appropriately sparse or dense result

Specified by:
matrixLike in class AbstractVector
Parameters:
rows - the row cardinality
columns - the column cardinality
Returns:
a Matrix

clone

public SequentialAccessSparseVector clone()
Description copied from interface: Vector
Return a copy of the recipient

Specified by:
clone in interface Vector
Overrides:
clone in class AbstractVector
Returns:
a new Vector

assign

public Vector assign(Vector other)
Description copied from interface: Vector
Assign the other vector values to the receiver

Specified by:
assign in interface Vector
Overrides:
assign in class AbstractVector
Parameters:
other - a Vector
Returns:
the modified receiver

toString

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

isDense

public boolean isDense()
Returns:
false

isSequentialAccess

public boolean isSequentialAccess()
Returns:
true

getQuick

public double getQuick(int index)
Description copied from interface: Vector
Return the value at the given index, without checking bounds

Parameters:
index - an int index
Returns:
the double at the index

setQuick

public void setQuick(int index,
                     double value)
Description copied from interface: Vector
Set the value at the given index, without checking bounds

Parameters:
index - an int index into the receiver
value - a double value to set

getNumNondefaultElements

public int getNumNondefaultElements()
Description copied from interface: Vector
Return the number of values in the recipient

Returns:
an int

like

public SequentialAccessSparseVector like()
Description copied from interface: Vector
Return an empty vector of the same underlying class as the receiver

Returns:
a Vector

iterateNonZero

public java.util.Iterator<Vector.Element> iterateNonZero()
Description copied from interface: Vector
Iterates over all non-zero elements.

NOTE: Implementations may choose to reuse the Element returned for performance reasons, so if you need a copy of it, you should call Vector.getElement(int) for the given index

Returns:
An Iterator over all non-zero elements

iterator

public java.util.Iterator<Vector.Element> iterator()
Description copied from interface: Vector
Iterates over all elements

* NOTE: Implementations may choose to reuse the Element returned for performance reasons, so if you need a copy of it, you should call Vector.getElement(int) for the given index

Returns:
An Iterator over all elements

dot

public double dot(Vector x)
Description copied from interface: Vector
Return the dot product of the recipient and the argument

Specified by:
dot in interface Vector
Overrides:
dot in class AbstractVector
Parameters:
x - a Vector
Returns:
a new Vector

minus

public Vector minus(Vector that)
Description copied from interface: Vector
Return a new vector containing the element by element difference of the recipient and the argument

Specified by:
minus in interface Vector
Overrides:
minus in class AbstractVector
Parameters:
that - a Vector
Returns:
a new Vector


Copyright © 2008-2010 The Apache Software Foundation. All Rights Reserved.