Class CompressedVector
java.lang.Object
org.la4j.Vector
org.la4j.vector.SparseVector
org.la4j.vector.sparse.CompressedVector
A basic sparse vector implementation using underlying value and index arrays.
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.
CompressedVector stores the underlying data in a two arrays: A values
array and a indices array. The values array matches the indices array, and
they're both sorted by the indices array. To get a value at an index, the
index is found in the indices array with a binary search and the respective
value is obtained from the values array.-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate int[]private static final intprivate double[]private static final byteFields inherited from class SparseVector
cardinality -
Constructor Summary
ConstructorsConstructorDescriptionCompressedVector(int length) CompressedVector(int length, int capacity) CompressedVector(int length, int cardinality, double[] values, int[] indices) -
Method Summary
Modifier and TypeMethodDescriptionprivate intalign(int length, int capacity) blankOfLength(int length) Creates a blank (an empty vector) copy of this vector with the givenlength.copyOfLength(int length) Copies this vector into the new vector with specifiedlength.voideach(VectorProcedure procedure) Applies givenprocedureto each element of this vector.voideachNonZero(VectorProcedure procedure) Applies givenprocedureto each non-zero element of this vector.static CompressedVectorfromArray(double[] array) Creates a newCompressedVectorfrom the givenarraywith compressing (copying) the underlying array.static CompressedVectorfromBinary(byte[] array) DecodesCompressedVectorfrom the given bytearray.static CompressedVectorfromCollection(Collection<? extends Number> list) Creates newCompressedVectorfrom collectionstatic CompressedVectorParsesCompressedVectorfrom the given CSV string.static CompressedVectorCreates newCompressedVectorfrom index-value mapstatic CompressedVectorParsesCompressedVectorfrom the given Matrix Market string.doublegetOrElse(int i, double defaultValue) Gets the specified element, or adefaultValueif there is no actual element at indexiin this sparse vector.private voidgrowUp()private voidinsert(int k, int i, double value) iterator()Returns a vector iterator.booleannonZeroAt(int i) * Whether or not the specified element is not zero.Returns a non-zero vector iterator.static CompressedVectorprivate voidremove(int k) private intsearchForIndex(int i) Does the binary searching to find the position in the value array given it's index.voidset(int i, double value) Sets the specified element of this matrix to givenvalue.voidsetAll(double value) Sets all elements of this vector to givenvalue.voidswapElements(int i, int j) Swaps the specified elements of this vector.<T extends Vector>
Tto(VectorFactory<T> factory) Converts this vector using the givenfactory.byte[]toBinary()Encodes this vector into a byte array.voidupdateAt(int i, VectorFunction function) Updates the specified element of this vector by applying givenfunction.static CompressedVectorzero(int length) Creates a zeroCompressedVectorof the givenlength.static CompressedVectorzero(int length, int capacity) Methods inherited from class SparseVector
add, apply, apply, apply, cardinality, density, ensureIndexIsInBounds, euclideanNorm, foldNonZero, get, hashCode, infinityNorm, isZeroAt, manhattanNorm, max, min, multiply, toColumnMatrix, toDiagonalMatrix, toMatrixMarket, toRowMatrixMethods inherited from class Vector
add, blank, constant, copy, divide, ensureLengthIsCorrect, equals, equals, fail, fold, hadamardProduct, innerProduct, is, length, mkString, mkString, multiply, non, norm, outerProduct, product, random, select, shuffle, slice, sliceLeft, sliceRight, subtract, subtract, sum, toCSV, toCSV, toDenseVector, toMatrixMarket, toSparseVector, toString, transform, unit, updateMethods inherited from interface Iterable
forEach, spliterator
-
Field Details
-
VECTOR_TAG
private static final byte VECTOR_TAG- See Also:
-
MINIMUM_SIZE
private static final int MINIMUM_SIZE- See Also:
-
values
private double[] values -
indices
private int[] indices
-
-
Constructor Details
-
CompressedVector
public CompressedVector() -
CompressedVector
public CompressedVector(int length) -
CompressedVector
public CompressedVector(int length, int capacity) -
CompressedVector
public CompressedVector(int length, int cardinality, double[] values, int[] indices)
-
-
Method Details
-
zero
Creates a zeroCompressedVectorof the givenlength. -
zero
-
random
-
fromArray
Creates a newCompressedVectorfrom the givenarraywith compressing (copying) the underlying array. -
fromBinary
DecodesCompressedVectorfrom the given bytearray.- Parameters:
array- the byte array representing a vector- Returns:
- a decoded vector
-
fromCSV
ParsesCompressedVectorfrom the given CSV string.- Parameters:
csv- the CSV string representing a vector- Returns:
- a parsed vector
-
fromMatrixMarket
ParsesCompressedVectorfrom the given Matrix Market string.- Parameters:
mm- the string in Matrix Market format- Returns:
- a parsed vector
-
fromCollection
Creates newCompressedVectorfrom collection- Parameters:
list- value list- Returns:
- created vector
-
fromMap
Creates newCompressedVectorfrom index-value map- Parameters:
map- index-value maplength- vector length- Returns:
- created vector
-
getOrElse
public double getOrElse(int i, double defaultValue) Description copied from class:SparseVectorGets the specified element, or adefaultValueif there is no actual element at indexiin this sparse vector.- Specified by:
getOrElsein classSparseVector- Parameters:
i- the element's indexdefaultValue- the default value- Returns:
- the element of this vector or a default value
-
set
-
setAll
-
swapElements
public void swapElements(int i, int j) Description copied from class:VectorSwaps the specified elements of this vector.- Overrides:
swapElementsin classVector- Parameters:
i- element's indexj- element's index
-
copyOfLength
Description copied from class:VectorCopies this vector into the new vector with specifiedlength.- Specified by:
copyOfLengthin classVector- Parameters:
length- the length of new vector- Returns:
- the copy of this vector with new length
-
each
Description copied from class:VectorApplies givenprocedureto each element of this vector. -
eachNonZero
Description copied from class:SparseVectorApplies givenprocedureto each non-zero element of this vector.- Overrides:
eachNonZeroin classSparseVector- Parameters:
procedure- the vector procedure
-
updateAt
Description copied from class:VectorUpdates the specified element of this vector by applying givenfunction. -
nonZeroAt
public boolean nonZeroAt(int i) Description copied from class:SparseVector* Whether or not the specified element is not zero.- Specified by:
nonZeroAtin classSparseVector- Parameters:
i- element's index- Returns:
trueif specified element is zero,falseotherwise
-
to
Description copied from class:VectorConverts this vector using the givenfactory.- Overrides:
toin classSparseVector- Type Parameters:
T- type of the result vector- Parameters:
factory- the factory that creates an output vector- Returns:
- a converted vector
-
blankOfLength
Description copied from class:VectorCreates a blank (an empty vector) copy of this vector with the givenlength.- Specified by:
blankOfLengthin classVector- Parameters:
length- the length of the blank vector- Returns:
- blank vector
-
toBinary
-
searchForIndex
private int searchForIndex(int i) Does the binary searching to find the position in the value array given it's index.- Parameters:
i- the index to search for- Returns:
- the position in the value array
-
insert
private void insert(int k, int i, double value) -
remove
private void remove(int k) -
growUp
private void growUp() -
align
private int align(int length, int capacity) -
nonZeroIterator
Description copied from class:SparseVectorReturns a non-zero vector iterator.- Specified by:
nonZeroIteratorin classSparseVector- Returns:
- a non-zero vector iterator
-
iterator
-