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.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
Fields Modifier and Type Field Description private int[]
indices
private static int
MINIMUM_SIZE
private double[]
values
private static byte
VECTOR_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 int
align(int length, int capacity)
Vector
blankOfLength(int length)
Creates a blank (an empty vector) copy of this vector with the givenlength
.Vector
copyOfLength(int length)
Copies this vector into the new vector with specifiedlength
.void
each(VectorProcedure procedure)
Applies givenprocedure
to each element of this vector.void
eachNonZero(VectorProcedure procedure)
Applies givenprocedure
to each non-zero element of this vector.static CompressedVector
fromArray(double[] array)
Creates a newCompressedVector
from the givenarray
with compressing (copying) the underlying array.static CompressedVector
fromBinary(byte[] array)
DecodesCompressedVector
from the given bytearray
.static CompressedVector
fromCollection(java.util.Collection<? extends java.lang.Number> list)
Creates newCompressedVector
from collectionstatic CompressedVector
fromCSV(java.lang.String csv)
ParsesCompressedVector
from the given CSV string.static CompressedVector
fromMap(java.util.Map<java.lang.Integer,? extends java.lang.Number> map, int length)
Creates newCompressedVector
from index-value mapstatic CompressedVector
fromMatrixMarket(java.lang.String mm)
ParsesCompressedVector
from the given Matrix Market string.double
getOrElse(int i, double defaultValue)
Gets the specified element, or adefaultValue
if there is no actual element at indexi
in this sparse vector.private void
growUp()
private void
insert(int k, int i, double value)
VectorIterator
iterator()
Returns a vector iterator.boolean
nonZeroAt(int i)
* Whether or not the specified element is not zero.VectorIterator
nonZeroIterator()
Returns a non-zero vector iterator.static CompressedVector
random(int length, double density, java.util.Random random)
private void
remove(int k)
private int
searchForIndex(int i)
Does the binary searching to find the position in the value array given it's index.void
set(int i, double value)
Sets the specified element of this matrix to givenvalue
.void
setAll(double value)
Sets all elements of this vector to givenvalue
.void
swapElements(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.void
updateAt(int i, VectorFunction function)
Updates the specified element of this vector by applying givenfunction
.static CompressedVector
zero(int length)
Creates a zeroCompressedVector
of the givenlength
.static CompressedVector
zero(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 zeroCompressedVector
of 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 newCompressedVector
from the givenarray
with compressing (copying) the underlying array.
-
fromBinary
public static CompressedVector fromBinary(byte[] array)
DecodesCompressedVector
from the given bytearray
.- Parameters:
array
- the byte array representing a vector- Returns:
- a decoded vector
-
fromCSV
public static CompressedVector fromCSV(java.lang.String csv)
ParsesCompressedVector
from 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)
ParsesCompressedVector
from 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 newCompressedVector
from 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 newCompressedVector
from 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:SparseVector
Gets the specified element, or adefaultValue
if there is no actual element at indexi
in this sparse vector.- Specified by:
getOrElse
in 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:Vector
Sets the specified element of this matrix to givenvalue
.
-
setAll
public void setAll(double value)
Description copied from class:Vector
Sets all elements of this vector to givenvalue
.
-
swapElements
public void swapElements(int i, int j)
Description copied from class:Vector
Swaps the specified elements of this vector.- Overrides:
swapElements
in classVector
- Parameters:
i
- element's indexj
- element's index
-
copyOfLength
public Vector copyOfLength(int length)
Description copied from class:Vector
Copies this vector into the new vector with specifiedlength
.- Specified by:
copyOfLength
in 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:Vector
Applies givenprocedure
to each element of this vector.
-
eachNonZero
public void eachNonZero(VectorProcedure procedure)
Description copied from class:SparseVector
Applies givenprocedure
to each non-zero element of this vector.- Overrides:
eachNonZero
in classSparseVector
- Parameters:
procedure
- the vector procedure
-
updateAt
public void updateAt(int i, VectorFunction function)
Description copied from class:Vector
Updates 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:
nonZeroAt
in classSparseVector
- Parameters:
i
- element's index- Returns:
true
if specified element is zero,false
otherwise
-
to
public <T extends Vector> T to(VectorFactory<T> factory)
Description copied from class:Vector
Converts this vector using the givenfactory
.- Overrides:
to
in 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:Vector
Creates a blank (an empty vector) copy of this vector with the givenlength
.- Specified by:
blankOfLength
in classVector
- Parameters:
length
- the length of the blank vector- Returns:
- blank vector
-
toBinary
public byte[] toBinary()
Description copied from class:Vector
Encodes 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:SparseVector
Returns a non-zero vector iterator.- Specified by:
nonZeroIterator
in classSparseVector
- Returns:
- a non-zero vector iterator
-
iterator
public VectorIterator iterator()
Description copied from class:Vector
Returns a vector iterator.
-
-