Class SparseVector
java.lang.Object
org.la4j.Vector
org.la4j.vector.SparseVector
- Direct Known Subclasses:
CompressedVector
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 -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionadd(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.intReturns 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.doubleCalculates 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(Collection<? extends Number> list) Creates newSparseVectorfrom collectionstatic SparseVectorParsesSparseVectorfrom the given CSV string.static SparseVectorCreates newSparseVectorfrom given index-value mapstatic SparseVectorParsesSparseVectorfrom 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.doubleCalculates an Infinity norm of this vector.booleanisZeroAt(int i) Whether or not the specified element is zero.doubleCalculates 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.multiply(double value) Multiplies this vector (X) by givenvalue(v).abstract booleannonZeroAt(int i) * Whether or not the specified element is not zero.abstract VectorIteratorReturns a non-zero vector iterator.static SparseVector<T extends Vector>
Tto(VectorFactory<T> factory) Converts this vector using the givenfactory.Converts this vector to matrix with only one column.Converts this vector to a diagonal matrix.toMatrixMarket(NumberFormat formatter) Converts this vector into the string in Matrix Market format using the givenformatter;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 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, updateAtMethods inherited from interface Iterable
forEach, spliterator
-
Field Details
-
cardinality
protected int cardinality
-
-
Constructor Details
-
SparseVector
public SparseVector(int length) -
SparseVector
public SparseVector(int length, int cardinality)
-
-
Method Details
-
zero
Creates a zeroSparseVectorof the givenlength. -
zero
-
random
-
fromArray
Creates a newSparseVectorfrom the givenarraywith compressing (copying) the underlying array. -
fromCSV
ParsesSparseVectorfrom the given CSV string.- Parameters:
csv- the CSV string representing a vector- Returns:
- a parsed vector
-
fromMatrixMarket
ParsesSparseVectorfrom the given Matrix Market string.- Parameters:
mm- the string in Matrix Market format- Returns:
- a parsed vector
-
fromCollection
Creates newSparseVectorfrom collection- Parameters:
list- value list- Returns:
- created vector
-
fromMap
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
-
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
Folds non-zero elements of this vector with givenaccumulator.- Parameters:
accumulator- the vector accumulator- Returns:
- the accumulated value
-
eachNonZero
Applies givenprocedureto each non-zero element of this vector.- Parameters:
procedure- the vector procedure
-
add
-
multiply
-
max
-
min
-
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
Returns a non-zero vector iterator.- Returns:
- a non-zero vector iterator
-
to
Description copied from class:VectorConverts this vector using the givenfactory. -
hashCode
-
apply
Description copied from class:VectorPipes this vector to a givenoperation. -
apply
Description copied from class:VectorPipes this vector to a givenoperation. -
apply
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
Description copied from class:VectorConverts this vector to matrix with only one row.- Specified by:
toRowMatrixin classVector- Returns:
- the row matrix
-
toColumnMatrix
Description copied from class:VectorConverts this vector to matrix with only one column.- Specified by:
toColumnMatrixin classVector- Returns:
- the column matrix
-
toDiagonalMatrix
Description copied from class:VectorConverts this vector to a diagonal matrix.- Specified by:
toDiagonalMatrixin classVector- Returns:
- a diagonal matrix
-
toMatrixMarket
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.
-