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 int
cardinality
-
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 Vector
add(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
cardinality()
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
euclideanNorm()
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(java.util.Collection<? extends java.lang.Number> list)
Creates newSparseVector
from collectionstatic SparseVector
fromCSV(java.lang.String csv)
ParsesSparseVector
from the given CSV string.static SparseVector
fromMap(java.util.Map<java.lang.Integer,? extends java.lang.Number> map, int length)
Creates newSparseVector
from given index-value mapstatic SparseVector
fromMatrixMarket(java.lang.String mm)
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
infinityNorm()
Calculates an Infinity norm of this vector.boolean
isZeroAt(int i)
Whether or not the specified element is zero.double
manhattanNorm()
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.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
nonZeroIterator()
Returns a non-zero vector iterator.static SparseVector
random(int length, double density, java.util.Random random)
<T extends Vector>
Tto(VectorFactory<T> factory)
Converts this vector using the givenfactory
.Matrix
toColumnMatrix()
Converts this vector to matrix with only one column.Matrix
toDiagonalMatrix()
Converts this vector to a diagonal matrix.java.lang.String
toMatrixMarket(java.text.NumberFormat formatter)
Converts this vector into the string in Matrix Market format using the givenformatter
;Matrix
toRowMatrix()
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
-
-
-
-
Method Detail
-
zero
public static SparseVector zero(int length)
Creates a zeroSparseVector
of 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 newSparseVector
from the givenarray
with compressing (copying) the underlying array.
-
fromCSV
public static SparseVector fromCSV(java.lang.String csv)
ParsesSparseVector
from 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)
ParsesSparseVector
from 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 newSparseVector
from 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 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
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 givenprocedure
to each non-zero element of this vector.- Parameters:
procedure
- the vector procedure
-
add
public Vector add(double value)
Description copied from class:Vector
Adds givenvalue
(v) to this vector (X).
-
multiply
public Vector multiply(double value)
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
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:Vector
Converts this vector using the givenfactory
.
-
hashCode
public int hashCode()
Description copied from class:Vector
Calculates the hash-code of this vector.
-
apply
public <T> T apply(VectorOperation<T> operation)
Description copied from class:Vector
Pipes this vector to a givenoperation
.
-
apply
public <T> T apply(VectorVectorOperation<T> operation, Vector that)
Description copied from class:Vector
Pipes this vector to a givenoperation
.
-
apply
public <T> T apply(VectorMatrixOperation<T> operation, Matrix that)
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
public 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
public 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
public Matrix toDiagonalMatrix()
Description copied from class:Vector
Converts this vector to a diagonal matrix.- Specified by:
toDiagonalMatrix
in classVector
- Returns:
- a diagonal matrix
-
toMatrixMarket
public java.lang.String toMatrixMarket(java.text.NumberFormat formatter)
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.
-
-