jsat.linear
Class SparseVector
- java.lang.Object
-
- jsat.linear.Vec
-
- jsat.linear.SparseVector
-
- All Implemented Interfaces:
- java.io.Serializable, java.lang.Cloneable, java.lang.Iterable<IndexValue>
public class SparseVector extends Vec
Provides a vector implementation that is sparse. It does not allocate space for a vector of the specified size, and only stores non zero values. All values not stored are implicitly zero.
Operations that change several zero values in a sparse vector to non-zero values may have degraded performance.
Sparce vector should never be used unless at least half the values are zero. If more then half the values are non-zero, it will use more memory then an equivalentDenseVector. The more values that are zero in the vector, the better its performance will be.- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor and Description SparseVector(int length)Creates a new sparse vector of the given length that is all zero values.SparseVector(int[] indexes, double[] values, int length, int used)Creates a new sparse vector backed by the given arrays.SparseVector(int length, int capacity)Creates a new sparse vector of the specified length, and pre-allocates enough internal state to holdcapacitynon zero values.SparseVector(java.util.List<java.lang.Double> vals)Creates a new sparse vector of the same length asvalsand sets each value to the values in the list.SparseVector(Vec toCopy)Creates a new sparse vector by copying the values from another
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method and Description voidapplyFunction(Function1D f)Applies the given function to each and every value in the vector.voidapplyIndexFunction(IndexFunction f)Applies the given function to each and every value in the vector.double[]arrayCopy()Creates a new array that contains all the values of this vector in the appropriate indicesSparseVectorclone()voidcopyTo(Vec destination)Copies the values of this Vector into another vectordoubledot(Vec v)Computes the dot product between two vectors, which is equivalent to
Σ thisi*vi
This method should be overloaded for a serious implementation.booleanequals(java.lang.Object obj, double range)doubleget(int index)Gets the value stored at a specific index in the vectorintgetLastNonZeroIndex()Returns the index of the last non-zero value, or -1 if all values are zero.java.util.Iterator<IndexValue>getNonZeroIterator(int start)Returns an iterator that will go over the non zero values starting from the specified index in the given vector.inthashCode()Provides a hashcode for Vectors.voidincrement(int index, double val)Increments the value at the given index by the given value.booleanisSparse()Indicates whether or not this vector is optimized for sparce computation, meaning that most values in the vector are zero - and considered implicit.doublekurtosis()Computes the kurtosis of this vector, which is the 4th moment.intlength()Returns the length of this vectordoublemax()Returns the maximum value stored in this vectordoublemedian()Returns the median value in this vectordoublemin()Returns the minimum value stored in this vectorvoidmultiply(double c, Matrix A, Vec b)If this is vector a, this this computes b = b + c aT*AvoidmutableAdd(double c)Alters this vector such that this = this + c
This method should be overloaded for a serious implementation.voidmutableAdd(double c, Vec v)Alters this vector such that this = this + c * b
This method should be overloaded for a serious implementation.voidmutableDivide(double c)Mutatesthis /= c
This method should be overloaded for a serious implementation.voidmutableMultiply(double c)Mutatesthis *= c
This method should be overloaded for a serious implementation.voidmutablePairwiseDivide(Vec b)Mutatesthisby dividing each value by the value inbthat has the same index
This method should be overloaded for a serious implementation.voidmutablePairwiseMultiply(Vec b)Mutatesthisby multiplying each value by the value inbthat has the same index.intnnz()Computes the number of non zero values in this vectorvoidnormalize()Mutates this vector to be normalized by the L2 normdoublepNorm(double p)Returns the p-norm of this vector.doublepNormDist(double p, Vec y)Returns the p-norm distance between this and another vector y.voidset(int index, double val)Sets the value stored at a specified index in the vectorvoidsetLength(int length)Because sparce vectors do not have most value set, they can have their length increased, and sometimes decreased, without any effort.doubleskewness()Computes the skewness of this vector, which is the 3rd moment.VecsortedCopy()Returns a copy of this array with the values moved around so that they are in sorted orderdoublesum()Computes the sum of the values in this vectorjava.lang.StringtoString()doublevariance()Computes the variance of the values in this vector, which isVec.standardDeviation()2voidzeroOut()Zeroes out all values in this vector
This method should be overloaded for a serious implementation.-
Methods inherited from class jsat.linear.Vec
add, add, canBeMutated, copyToCol, copyToRow, countNaNs, divide, equals, getNonZeroIterator, iterator, mean, multiply, multiply, multiply, mutableAdd, mutableSubtract, mutableSubtract, mutableSubtract, normalized, pairwiseDivide, pairwiseMultiply, random, random, standardDeviation, subtract, subtract, zeros
-
-
-
-
Constructor Detail
-
SparseVector
public SparseVector(int length)
Creates a new sparse vector of the given length that is all zero values.- Parameters:
length- the length of the sparse vector
-
SparseVector
public SparseVector(java.util.List<java.lang.Double> vals)
Creates a new sparse vector of the same length asvalsand sets each value to the values in the list.- Parameters:
vals- the list of values to create a vector from
-
SparseVector
public SparseVector(int length, int capacity)Creates a new sparse vector of the specified length, and pre-allocates enough internal state to holdcapacitynon zero values. The vector itself will start out with all zero values.- Parameters:
length- the length of the sparse vectorcapacity- the number of non zero values to allocate space for
-
SparseVector
public SparseVector(int[] indexes, double[] values, int length, int used)Creates a new sparse vector backed by the given arrays. Modifying the arrays will modify the vector, and no validation will be done. This constructor should only be used in performance necessary scenarios
To make sure the input values are valid, theindexesvalues must be increasing and all values less thanlengthand greater than-1up to the firstusedindices.
All the values stored invaluesmust be non zero and can not be a special value.
usedmust be greater than -1 and less than the length of theindexesandvaluesarrays.
Theindexesandvaluesarrays must be the exact same length- Parameters:
indexes- the array to store the index locations invalues- the array to store the index values inlength- the length of the sparse vectorused- the number of non zero values in the vector taken from the given input arrays.
-
SparseVector
public SparseVector(Vec toCopy)
Creates a new sparse vector by copying the values from another- Parameters:
toCopy- the vector to copy the values of
-
-
Method Detail
-
length
public int length()
Description copied from class:VecReturns the length of this vector
-
setLength
public void setLength(int length)
Because sparce vectors do not have most value set, they can have their length increased, and sometimes decreased, without any effort. The length can always be extended. The length can be reduced down to the size of the largest non zero element.- Parameters:
length- the new length of this vector
-
nnz
public int nnz()
Description copied from class:VecComputes the number of non zero values in this vector
-
increment
public void increment(int index, double val)Increments the value at the given index by the given value.
-
get
public double get(int index)
Description copied from class:VecGets the value stored at a specific index in the vector
-
set
public void set(int index, double val)Description copied from class:VecSets the value stored at a specified index in the vector
-
sortedCopy
public Vec sortedCopy()
Description copied from class:VecReturns a copy of this array with the values moved around so that they are in sorted order- Overrides:
sortedCopyin classVec- Returns:
- a new array in sorted order
-
getLastNonZeroIndex
public int getLastNonZeroIndex()
Returns the index of the last non-zero value, or -1 if all values are zero.- Returns:
- the index of the last non-zero value, or -1 if all values are zero.
-
min
public double min()
Description copied from class:VecReturns the minimum value stored in this vector
-
max
public double max()
Description copied from class:VecReturns the maximum value stored in this vector
-
sum
public double sum()
Description copied from class:VecComputes the sum of the values in this vector
-
variance
public double variance()
Description copied from class:VecComputes the variance of the values in this vector, which isVec.standardDeviation()2
-
median
public double median()
Description copied from class:VecReturns the median value in this vector
-
skewness
public double skewness()
Description copied from class:VecComputes the skewness of this vector, which is the 3rd moment.
-
kurtosis
public double kurtosis()
Description copied from class:VecComputes the kurtosis of this vector, which is the 4th moment.
-
copyTo
public void copyTo(Vec destination)
Description copied from class:VecCopies the values of this Vector into another vector
-
dot
public double dot(Vec v)
Description copied from class:VecComputes the dot product between two vectors, which is equivalent to
Σ thisi*vi
This method should be overloaded for a serious implementation.
-
multiply
public void multiply(double c, Matrix A, Vec b)Description copied from class:VecIf this is vector a, this this computes b = b + c aT*A
-
mutableAdd
public void mutableAdd(double c)
Description copied from class:VecAlters this vector such that this = this + c
This method should be overloaded for a serious implementation.- Overrides:
mutableAddin classVec- Parameters:
c- a scalar constant to add to each value in this vector
-
mutableAdd
public void mutableAdd(double c, Vec v)Description copied from class:VecAlters this vector such that this = this + c * b
This method should be overloaded for a serious implementation.- Overrides:
mutableAddin classVec- Parameters:
c- a scalar constantv- the vector to add to this
-
mutableMultiply
public void mutableMultiply(double c)
Description copied from class:VecMutatesthis *= c
This method should be overloaded for a serious implementation.- Overrides:
mutableMultiplyin classVec- Parameters:
c- the constant to multiply by
-
mutableDivide
public void mutableDivide(double c)
Description copied from class:VecMutatesthis /= c
This method should be overloaded for a serious implementation.- Overrides:
mutableDividein classVec- Parameters:
c- the constant to divide by
-
pNormDist
public double pNormDist(double p, Vec y)Description copied from class:VecReturns the p-norm distance between this and another vector y.
-
pNorm
public double pNorm(double p)
Description copied from class:VecReturns the p-norm of this vector.
-
clone
public SparseVector clone()
-
normalize
public void normalize()
Description copied from class:VecMutates this vector to be normalized by the L2 norm
-
mutablePairwiseMultiply
public void mutablePairwiseMultiply(Vec b)
Description copied from class:VecMutatesthisby multiplying each value by the value inbthat has the same index.
This method should be overloaded for a serious implementation.- Overrides:
mutablePairwiseMultiplyin classVec- Parameters:
b- the vector to pairwise multiply by
-
mutablePairwiseDivide
public void mutablePairwiseDivide(Vec b)
Description copied from class:VecMutatesthisby dividing each value by the value inbthat has the same index
This method should be overloaded for a serious implementation.- Overrides:
mutablePairwiseDividein classVec- Parameters:
b- the vector to pairwise divide by
-
arrayCopy
public double[] arrayCopy()
Description copied from class:VecCreates a new array that contains all the values of this vector in the appropriate indices
-
applyFunction
public void applyFunction(Function1D f)
Description copied from class:VecApplies the given function to each and every value in the vector.
This method should be overloaded for a serious implementation.- Overrides:
applyFunctionin classVec- Parameters:
f- the single variable function to apply
-
applyIndexFunction
public void applyIndexFunction(IndexFunction f)
Description copied from class:VecApplies the given function to each and every value in the vector. The function takes 2 arguments, an arbitrary value, and then an index. The index passed to the function is the index in the array that the value came from.
NOTE: Because negative values are invalid indexes. The given function should return 0.0 when given a negative index, if and only if, f(0,index) = 0 for any valid index. If f(0, index) != 0 for even one value of index, it should return any non zero value when given a negative index.
IE: f(value_i, i) = x
This method should be overloaded for a serious implementation.- Overrides:
applyIndexFunctionin classVec- Parameters:
f- the 2 dimensional index function to apply
-
zeroOut
public void zeroOut()
Description copied from class:VecZeroes out all values in this vector
This method should be overloaded for a serious implementation.
-
getNonZeroIterator
public java.util.Iterator<IndexValue> getNonZeroIterator(int start)
Description copied from class:VecReturns an iterator that will go over the non zero values starting from the specified index in the given vector. The iterator does not support theIterator.remove()method.
This method should be overloaded for a serious implementation.- Overrides:
getNonZeroIteratorin classVec- Parameters:
start- the first index (inclusive) to start returning non-zero values from- Returns:
- an iterator for the non zero index value pairs
-
hashCode
public int hashCode()
Description copied from class:VecProvides a hashcode for Vectors. All vector implementations should return the same result for cases whenVec.equals(java.lang.Object)returns true. Below is the code used for this class
int result = 1;
for (int i = 0; i < length(); i++)
{
double val = get(i);
if(val != 0)
{
long bits = Double.doubleToLongBits(val);
result = 31 * result + (int)(bits ^ (bits >>> 32));
result = 31 * result + i;
}
}
return 31* result + length();
-
isSparse
public boolean isSparse()
Description copied from class:VecIndicates whether or not this vector is optimized for sparce computation, meaning that most values in the vector are zero - and considered implicit. Only non-zero values are stored.
-
-
DataMelt 3.0 © DataMelt by jWork.ORG