Package org.la4j.vector
Class SparseVector
- java.lang.Object
-
- org.la4j.Vector
-
- org.la4j.vector.SparseVector
-
- All Implemented Interfaces:
java.lang.Iterable<java.lang.Double>
- Direct Known Subclasses:
CompressedVector
public abstract class SparseVector extends Vector
A sparse vector. A vector represents an array of elements. It can be re-sized. A sparse data structure does not store blank elements, and instead just stores elements with values. A sparse data structure can be initialized with a large length but take up no storage until the space is filled with non-zero elements. However, there is a performance cost. Fetch/store operations take O(log n) time instead of the O(1) time of a dense data structure.
-
-
Field Summary
Fields Modifier and Type Field Description protected intcardinality
-
Constructor Summary
Constructors Constructor Description SparseVector(int length)SparseVector(int length, int cardinality)
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description Vectoradd(double value)Adds givenvalue(v) to this vector (X).<T> Tapply(VectorMatrixOperation<T> operation, Matrix that)Pipes this vector to a givenoperation.<T> Tapply(VectorOperation<T> operation)Pipes this vector to a givenoperation.<T> Tapply(VectorVectorOperation<T> operation, Vector that)Pipes this vector to a givenoperation.intcardinality()Returns the cardinality (the number of non-zero elements) of this sparse vector.doubledensity()Returns the density (non-zero elements divided by total elements) of this sparse vector.voideachNonZero(VectorProcedure procedure)Applies givenprocedureto each non-zero element of this vector.protected voidensureIndexIsInBounds(int i)Ensures the provided index is in the bounds of thisSparseVector.doubleeuclideanNorm()Calculates an Euclidean norm of this vector.doublefoldNonZero(VectorAccumulator accumulator)Folds non-zero elements of this vector with givenaccumulator.static SparseVectorfromArray(double[] array)Creates a newSparseVectorfrom the givenarraywith compressing (copying) the underlying array.static SparseVectorfromCollection(java.util.Collection<? extends java.lang.Number> list)Creates newSparseVectorfrom collectionstatic SparseVectorfromCSV(java.lang.String csv)ParsesSparseVectorfrom the given CSV string.static SparseVectorfromMap(java.util.Map<java.lang.Integer,? extends java.lang.Number> map, int length)Creates newSparseVectorfrom given index-value mapstatic SparseVectorfromMatrixMarket(java.lang.String mm)ParsesSparseVectorfrom the given Matrix Market string.doubleget(int i)Gets the specified element of this vector.abstract doublegetOrElse(int i, double defaultValue)Gets the specified element, or adefaultValueif there is no actual element at indexiin this sparse vector.inthashCode()Calculates the hash-code of this vector.doubleinfinityNorm()Calculates an Infinity norm of this vector.booleanisZeroAt(int i)Whether or not the specified element is zero.doublemanhattanNorm()Calculates a Manhattan norm of this vector.doublemax()Searches for the maximum value of the elements of this vector.doublemin()Searches for the minimum value of the elements of this vector.Vectormultiply(double value)Multiplies this vector (X) by givenvalue(v).abstract booleannonZeroAt(int i)* Whether or not the specified element is not zero.abstract VectorIteratornonZeroIterator()Returns a non-zero vector iterator.static SparseVectorrandom(int length, double density, java.util.Random random)<T extends Vector>
Tto(VectorFactory<T> factory)Converts this vector using the givenfactory.MatrixtoColumnMatrix()Converts this vector to matrix with only one column.MatrixtoDiagonalMatrix()Converts this vector to a diagonal matrix.java.lang.StringtoMatrixMarket(java.text.NumberFormat formatter)Converts this vector into the string in Matrix Market format using the givenformatter;MatrixtoRowMatrix()Converts this vector to matrix with only one row.static SparseVectorzero(int length)Creates a zeroSparseVectorof the givenlength.static SparseVectorzero(int length, int capacity)-
Methods inherited from class org.la4j.Vector
add, blank, blankOfLength, constant, copy, copyOfLength, divide, each, ensureLengthIsCorrect, equals, equals, fail, fold, hadamardProduct, innerProduct, is, iterator, length, mkString, mkString, multiply, non, norm, outerProduct, product, random, select, set, setAll, shuffle, slice, sliceLeft, sliceRight, subtract, subtract, sum, swapElements, toBinary, toCSV, toCSV, toDenseVector, toMatrixMarket, toSparseVector, toString, transform, unit, update, updateAt
-
-
-
-
Method Detail
-
zero
public static SparseVector zero(int length)
Creates a zeroSparseVectorof the givenlength.
-
zero
public static SparseVector zero(int length, int capacity)
-
random
public static SparseVector random(int length, double density, java.util.Random random)
-
fromArray
public static SparseVector fromArray(double[] array)
Creates a newSparseVectorfrom the givenarraywith compressing (copying) the underlying array.
-
fromCSV
public static SparseVector fromCSV(java.lang.String csv)
ParsesSparseVectorfrom the given CSV string.- Parameters:
csv- the CSV string representing a vector- Returns:
- a parsed vector
-
fromMatrixMarket
public static SparseVector fromMatrixMarket(java.lang.String mm)
ParsesSparseVectorfrom the given Matrix Market string.- Parameters:
mm- the string in Matrix Market format- Returns:
- a parsed vector
-
fromCollection
public static SparseVector fromCollection(java.util.Collection<? extends java.lang.Number> list)
Creates newSparseVectorfrom collection- Parameters:
list- value list- Returns:
- created vector
-
fromMap
public static SparseVector fromMap(java.util.Map<java.lang.Integer,? extends java.lang.Number> map, int length)
Creates newSparseVectorfrom given index-value map- Parameters:
map-- Returns:
-
cardinality
public int cardinality()
Returns the cardinality (the number of non-zero elements) of this sparse vector.- Returns:
- the cardinality of this vector
-
density
public double density()
Returns the density (non-zero elements divided by total elements) of this sparse vector.- Returns:
- the density of this vector
-
get
public double get(int i)
Description copied from class:VectorGets the specified element of this vector.
-
getOrElse
public abstract double getOrElse(int i, double defaultValue)Gets the specified element, or adefaultValueif there is no actual element at indexiin this sparse vector.- Parameters:
i- the element's indexdefaultValue- the default value- Returns:
- the element of this vector or a default value
-
isZeroAt
public boolean isZeroAt(int i)
Whether or not the specified element is zero.- Parameters:
i- element's index- Returns:
trueif specified element is zero,falseotherwise
-
nonZeroAt
public abstract boolean nonZeroAt(int i)
* Whether or not the specified element is not zero.- Parameters:
i- element's index- Returns:
trueif specified element is zero,falseotherwise
-
foldNonZero
public double foldNonZero(VectorAccumulator accumulator)
Folds non-zero elements of this vector with givenaccumulator.- Parameters:
accumulator- the vector accumulator- Returns:
- the accumulated value
-
eachNonZero
public void eachNonZero(VectorProcedure procedure)
Applies givenprocedureto each non-zero element of this vector.- Parameters:
procedure- the vector procedure
-
add
public Vector add(double value)
Description copied from class:VectorAdds givenvalue(v) to this vector (X).
-
multiply
public Vector multiply(double value)
Description copied from class:VectorMultiplies this vector (X) by givenvalue(v).
-
max
public double max()
Description copied from class:VectorSearches for the maximum value of the elements of this vector.
-
min
public double min()
Description copied from class:VectorSearches for the minimum value of the elements of this vector.
-
euclideanNorm
public double euclideanNorm()
Description copied from class:VectorCalculates an Euclidean norm of this vector.- Overrides:
euclideanNormin classVector- Returns:
- an Euclidean norm
-
manhattanNorm
public double manhattanNorm()
Description copied from class:VectorCalculates a Manhattan norm of this vector.- Overrides:
manhattanNormin classVector- Returns:
- a Manhattan norm
-
infinityNorm
public double infinityNorm()
Description copied from class:VectorCalculates an Infinity norm of this vector.- Overrides:
infinityNormin classVector- Returns:
- an Infinity norm
-
nonZeroIterator
public abstract VectorIterator nonZeroIterator()
Returns a non-zero vector iterator.- Returns:
- a non-zero vector iterator
-
to
public <T extends Vector> T to(VectorFactory<T> factory)
Description copied from class:VectorConverts this vector using the givenfactory.
-
hashCode
public int hashCode()
Description copied from class:VectorCalculates the hash-code of this vector.
-
apply
public <T> T apply(VectorOperation<T> operation)
Description copied from class:VectorPipes this vector to a givenoperation.
-
apply
public <T> T apply(VectorVectorOperation<T> operation, Vector that)
Description copied from class:VectorPipes this vector to a givenoperation.
-
apply
public <T> T apply(VectorMatrixOperation<T> operation, Matrix that)
Description copied from class:VectorPipes this vector to a givenoperation.- Specified by:
applyin classVector- Type Parameters:
T- the result type- Parameters:
operation- the vector-matrix operation (an operation that takes vector and matrix and returnsT)that- the right hand matrix for the given operation- Returns:
- the result of an operation applied to this vector and
thatmatrix
-
toRowMatrix
public Matrix toRowMatrix()
Description copied from class:VectorConverts this vector to matrix with only one row.- Specified by:
toRowMatrixin classVector- Returns:
- the row matrix
-
toColumnMatrix
public Matrix toColumnMatrix()
Description copied from class:VectorConverts this vector to matrix with only one column.- Specified by:
toColumnMatrixin classVector- Returns:
- the column matrix
-
toDiagonalMatrix
public Matrix toDiagonalMatrix()
Description copied from class:VectorConverts this vector to a diagonal matrix.- Specified by:
toDiagonalMatrixin classVector- Returns:
- a diagonal matrix
-
toMatrixMarket
public java.lang.String toMatrixMarket(java.text.NumberFormat formatter)
Description copied from class:VectorConverts this vector into the string in Matrix Market format using the givenformatter;- Specified by:
toMatrixMarketin classVector- Parameters:
formatter- the number formater- Returns:
- a Matrix Market string representing this vector
-
ensureIndexIsInBounds
protected void ensureIndexIsInBounds(int i)
Ensures the provided index is in the bounds of thisSparseVector.- Parameters:
i- The index to check.
-
-