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:
Cloneable, 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) :

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
 SequentialAccessSparseVector clone()
          Return a copy of the recipient
 double getIteratorAdvanceCost()
          Gets an estimate of the cost (in number of operations) it takes to advance an iterator through the nonzero elements of this vector.
 double getLookupCost()
          Gets an estimate of the cost (in number of operations) it takes to lookup a random element in this vector.
 int getNumNondefaultElements()
          Return the number of values in the recipient which are not the default value.
 double getQuick(int index)
          Warning! This takes O(log n) time as it does a binary search behind the scenes! Only use it when STRICTLY necessary.
 void incrementQuick(int index, double increment)
          Increment the value at the given index by the given value.
 boolean isAddConstantTime()
          Return true iff adding a new (nonzero) element takes constant time for this vector.
 boolean isDense()
           
 boolean isSequentialAccess()
           
 Iterator<Vector.Element> iterateNonZero()
          Iterates over all non-zero elements.
 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 AbstractVector.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
 void mergeUpdates(OrderedIntDoubleMapping updates)
          Merge a set of (index, value) pairs into the vector.
 void setQuick(int index, double value)
          Warning! This takes O(log n) time as it does a binary search behind the scenes! Only use it when STRICTLY necessary.
 String toString()
           
 
Methods inherited from class org.apache.mahout.math.AbstractVector
aggregate, aggregate, all, asFormatString, assign, assign, assign, assign, assign, assign, createOptimizedCopy, cross, divide, dot, dotSelf, equals, get, getDistanceSquared, getElement, getLengthSquared, getNumNonZeroElements, hashCode, invalidateCachedLength, logNormalize, logNormalize, logNormalize, maxValue, maxValueIndex, minus, minValue, minValueIndex, nonZeroes, norm, normalize, normalize, plus, plus, set, size, sparseVectorToString, times, times, toString, 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

mergeUpdates

public void mergeUpdates(OrderedIntDoubleMapping updates)
Description copied from interface: Vector
Merge a set of (index, value) pairs into the vector.

Parameters:
updates - an ordered mapping of indices to values to be merged in.

toString

public 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)
Warning! This takes O(log n) time as it does a binary search behind the scenes! Only use it when STRICTLY necessary.

Parameters:
index - an int index.
Returns:
the value at that position in the vector.

setQuick

public void setQuick(int index,
                     double value)
Warning! This takes O(log n) time as it does a binary search behind the scenes! Only use it when STRICTLY necessary.

Parameters:
index - an int index.
value - a double value to set

incrementQuick

public void incrementQuick(int index,
                           double increment)
Description copied from interface: Vector
Increment the value at the given index by the given value.

Specified by:
incrementQuick in interface Vector
Overrides:
incrementQuick in class AbstractVector
Parameters:
index - an int index into the receiver
increment - sets the value at the given index to value + increment;

like

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

Returns:
a Vector

getNumNondefaultElements

public int getNumNondefaultElements()
Description copied from interface: Vector
Return the number of values in the recipient which are not the default value. For instance, for a sparse vector, this would be the number of non-zero values.

Returns:
an int

getLookupCost

public double getLookupCost()
Description copied from interface: Vector
Gets an estimate of the cost (in number of operations) it takes to lookup a random element in this vector.


getIteratorAdvanceCost

public double getIteratorAdvanceCost()
Description copied from interface: Vector
Gets an estimate of the cost (in number of operations) it takes to advance an iterator through the nonzero elements of this vector.


isAddConstantTime

public boolean isAddConstantTime()
Description copied from interface: Vector
Return true iff adding a new (nonzero) element takes constant time for this vector.


iterateNonZero

public Iterator<Vector.Element> iterateNonZero()
Description copied from class: AbstractVector
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 AbstractVector.getElement(int) for the given index

Specified by:
iterateNonZero in class AbstractVector
Returns:
An Iterator over all non-zero elements

iterator

public Iterator<Vector.Element> iterator()
Description copied from class: AbstractVector
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 AbstractVector.getElement(int) for the given index

Specified by:
iterator in class AbstractVector
Returns:
An Iterator over all elements


Copyright © 2008–2014 The Apache Software Foundation. All rights reserved.