Package org.la4j.vector.sparse
Class CompressedVector
- java.lang.Object
-
- org.la4j.Vector
-
- org.la4j.vector.SparseVector
-
- org.la4j.vector.sparse.CompressedVector
-
- All Implemented Interfaces:
java.lang.Iterable<java.lang.Double>
public class CompressedVector extends SparseVector
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.CompressedVectorstores 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
Fields Modifier and Type Field Description private int[]indicesprivate static intMINIMUM_SIZEprivate double[]valuesprivate static byteVECTOR_TAG-
Fields inherited from class org.la4j.vector.SparseVector
cardinality
-
-
Constructor Summary
Constructors Constructor Description CompressedVector()CompressedVector(int length)CompressedVector(int length, int capacity)CompressedVector(int length, int cardinality, double[] values, int[] indices)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private intalign(int length, int capacity)VectorblankOfLength(int length)Creates a blank (an empty vector) copy of this vector with the givenlength.VectorcopyOfLength(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(java.util.Collection<? extends java.lang.Number> list)Creates newCompressedVectorfrom collectionstatic CompressedVectorfromCSV(java.lang.String csv)ParsesCompressedVectorfrom the given CSV string.static CompressedVectorfromMap(java.util.Map<java.lang.Integer,? extends java.lang.Number> map, int length)Creates newCompressedVectorfrom index-value mapstatic CompressedVectorfromMatrixMarket(java.lang.String mm)ParsesCompressedVectorfrom 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)VectorIteratoriterator()Returns a vector iterator.booleannonZeroAt(int i)* Whether or not the specified element is not zero.VectorIteratornonZeroIterator()Returns a non-zero vector iterator.static CompressedVectorrandom(int length, double density, java.util.Random random)private 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 org.la4j.vector.SparseVector
add, apply, apply, apply, cardinality, density, ensureIndexIsInBounds, euclideanNorm, foldNonZero, get, hashCode, infinityNorm, isZeroAt, manhattanNorm, max, min, multiply, toColumnMatrix, toDiagonalMatrix, toMatrixMarket, toRowMatrix
-
Methods inherited from class org.la4j.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, update
-
-
-
-
Field Detail
-
VECTOR_TAG
private static final byte VECTOR_TAG
- See Also:
- Constant Field Values
-
MINIMUM_SIZE
private static final int MINIMUM_SIZE
- See Also:
- Constant Field Values
-
values
private double[] values
-
indices
private int[] indices
-
-
Method Detail
-
zero
public static CompressedVector zero(int length)
Creates a zeroCompressedVectorof the givenlength.
-
zero
public static CompressedVector zero(int length, int capacity)
-
random
public static CompressedVector random(int length, double density, java.util.Random random)
-
fromArray
public static CompressedVector fromArray(double[] array)
Creates a newCompressedVectorfrom the givenarraywith compressing (copying) the underlying array.
-
fromBinary
public static CompressedVector fromBinary(byte[] array)
DecodesCompressedVectorfrom the given bytearray.- Parameters:
array- the byte array representing a vector- Returns:
- a decoded vector
-
fromCSV
public static CompressedVector fromCSV(java.lang.String csv)
ParsesCompressedVectorfrom the given CSV string.- Parameters:
csv- the CSV string representing a vector- Returns:
- a parsed vector
-
fromMatrixMarket
public static CompressedVector fromMatrixMarket(java.lang.String mm)
ParsesCompressedVectorfrom the given Matrix Market string.- Parameters:
mm- the string in Matrix Market format- Returns:
- a parsed vector
-
fromCollection
public static CompressedVector fromCollection(java.util.Collection<? extends java.lang.Number> list)
Creates newCompressedVectorfrom collection- Parameters:
list- value list- Returns:
- created vector
-
fromMap
public static CompressedVector fromMap(java.util.Map<java.lang.Integer,? extends java.lang.Number> map, int length)
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
public void set(int i, double value)Description copied from class:VectorSets the specified element of this matrix to givenvalue.
-
setAll
public void setAll(double value)
Description copied from class:VectorSets all elements of this vector to givenvalue.
-
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
public Vector copyOfLength(int length)
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
public void each(VectorProcedure procedure)
Description copied from class:VectorApplies givenprocedureto each element of this vector.
-
eachNonZero
public void eachNonZero(VectorProcedure procedure)
Description copied from class:SparseVectorApplies givenprocedureto each non-zero element of this vector.- Overrides:
eachNonZeroin classSparseVector- Parameters:
procedure- the vector procedure
-
updateAt
public void updateAt(int i, VectorFunction function)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
public <T extends Vector> T to(VectorFactory<T> factory)
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
public Vector blankOfLength(int length)
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
public byte[] toBinary()
Description copied from class:VectorEncodes this vector into a byte array.
-
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
public VectorIterator nonZeroIterator()
Description copied from class:SparseVectorReturns a non-zero vector iterator.- Specified by:
nonZeroIteratorin classSparseVector- Returns:
- a non-zero vector iterator
-
iterator
public VectorIterator iterator()
Description copied from class:VectorReturns a vector iterator.
-
-