Package org.la4j.vector
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> T
apply
(VectorMatrixOperation<T> operation, Matrix that) Pipes this vector to a givenoperation
.<T> T
apply
(VectorOperation<T> operation) Pipes this vector to a givenoperation
.<T> T
apply
(VectorVectorOperation<T> operation, Vector that) Pipes this vector to a givenoperation
.int
Returns the cardinality (the number of non-zero elements) of this sparse vector.double
density()
Returns the density (non-zero elements divided by total elements) of this sparse vector.void
eachNonZero
(VectorProcedure procedure) Applies givenprocedure
to each non-zero element of this vector.protected void
ensureIndexIsInBounds
(int i) Ensures the provided index is in the bounds of thisSparseVector
.double
Calculates an Euclidean norm of this vector.double
foldNonZero
(VectorAccumulator accumulator) Folds non-zero elements of this vector with givenaccumulator
.static SparseVector
fromArray
(double[] array) Creates a newSparseVector
from the givenarray
with compressing (copying) the underlying array.static SparseVector
fromCollection
(Collection<? extends Number> list) Creates newSparseVector
from collectionstatic SparseVector
ParsesSparseVector
from the given CSV string.static SparseVector
Creates newSparseVector
from given index-value mapstatic SparseVector
ParsesSparseVector
from the given Matrix Market string.double
get
(int i) Gets the specified element of this vector.abstract double
getOrElse
(int i, double defaultValue) Gets the specified element, or adefaultValue
if there is no actual element at indexi
in this sparse vector.int
hashCode()
Calculates the hash-code of this vector.double
Calculates an Infinity norm of this vector.boolean
isZeroAt
(int i) Whether or not the specified element is zero.double
Calculates a Manhattan norm of this vector.double
max()
Searches for the maximum value of the elements of this vector.double
min()
Searches for the minimum value of the elements of this vector.multiply
(double value) Multiplies this vector (X) by givenvalue
(v).abstract boolean
nonZeroAt
(int i) * Whether or not the specified element is not zero.abstract VectorIterator
Returns 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 SparseVector
zero
(int length) Creates a zeroSparseVector
of the givenlength
.static SparseVector
zero
(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
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.lang.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 zeroSparseVector
of the givenlength
. -
zero
-
random
-
fromArray
Creates a newSparseVector
from the givenarray
with compressing (copying) the underlying array. -
fromCSV
ParsesSparseVector
from the given CSV string.- Parameters:
csv
- the CSV string representing a vector- Returns:
- a parsed vector
-
fromMatrixMarket
ParsesSparseVector
from the given Matrix Market string.- Parameters:
mm
- the string in Matrix Market format- Returns:
- a parsed vector
-
fromCollection
Creates newSparseVector
from collection- Parameters:
list
- value list- Returns:
- created vector
-
fromMap
Creates newSparseVector
from 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:Vector
Gets the specified element of this vector. -
getOrElse
public abstract double getOrElse(int i, double defaultValue) Gets the specified element, or adefaultValue
if there is no actual element at indexi
in 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:
true
if specified element is zero,false
otherwise
-
nonZeroAt
public abstract boolean nonZeroAt(int i) * Whether or not the specified element is not zero.- Parameters:
i
- element's index- Returns:
true
if specified element is zero,false
otherwise
-
foldNonZero
Folds non-zero elements of this vector with givenaccumulator
.- Parameters:
accumulator
- the vector accumulator- Returns:
- the accumulated value
-
eachNonZero
Applies givenprocedure
to each non-zero element of this vector.- Parameters:
procedure
- the vector procedure
-
add
Description copied from class:Vector
Adds givenvalue
(v) to this vector (X). -
multiply
Description copied from class:Vector
Multiplies this vector (X) by givenvalue
(v). -
max
public double max()Description copied from class:Vector
Searches for the maximum value of the elements of this vector. -
min
public double min()Description copied from class:Vector
Searches for the minimum value of the elements of this vector. -
euclideanNorm
public double euclideanNorm()Description copied from class:Vector
Calculates an Euclidean norm of this vector.- Overrides:
euclideanNorm
in classVector
- Returns:
- an Euclidean norm
-
manhattanNorm
public double manhattanNorm()Description copied from class:Vector
Calculates a Manhattan norm of this vector.- Overrides:
manhattanNorm
in classVector
- Returns:
- a Manhattan norm
-
infinityNorm
public double infinityNorm()Description copied from class:Vector
Calculates an Infinity norm of this vector.- Overrides:
infinityNorm
in classVector
- Returns:
- an Infinity norm
-
nonZeroIterator
Returns a non-zero vector iterator.- Returns:
- a non-zero vector iterator
-
to
Description copied from class:Vector
Converts this vector using the givenfactory
. -
hashCode
public int hashCode()Description copied from class:Vector
Calculates the hash-code of this vector. -
apply
Description copied from class:Vector
Pipes this vector to a givenoperation
. -
apply
Description copied from class:Vector
Pipes this vector to a givenoperation
. -
apply
Description copied from class:Vector
Pipes this vector to a givenoperation
.- Specified by:
apply
in 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
that
matrix
-
toRowMatrix
Description copied from class:Vector
Converts this vector to matrix with only one row.- Specified by:
toRowMatrix
in classVector
- Returns:
- the row matrix
-
toColumnMatrix
Description copied from class:Vector
Converts this vector to matrix with only one column.- Specified by:
toColumnMatrix
in classVector
- Returns:
- the column matrix
-
toDiagonalMatrix
Description copied from class:Vector
Converts this vector to a diagonal matrix.- Specified by:
toDiagonalMatrix
in classVector
- Returns:
- a diagonal matrix
-
toMatrixMarket
Description copied from class:Vector
Converts this vector into the string in Matrix Market format using the givenformatter
;- Specified by:
toMatrixMarket
in 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.
-