public class SparseVector extends Vec
DenseVector
. The more values that are zero in the vector,
the better its performance will be.Modifier and Type | Field and Description |
---|---|
protected int[] |
indexes
The mapping to true index values
|
protected int |
used
number of indices used in this vector
|
protected double[] |
values
The Corresponding values for each index
|
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 hold
capacity non zero values. |
SparseVector(List<Double> vals)
Creates a new sparse vector of the same length as
vals and sets
each value to the values in the list. |
SparseVector(Vec toCopy)
Creates a new sparse vector by copying the values from another
|
Modifier and Type | Method and Description |
---|---|
void |
applyFunction(Function f)
Applies the given function to each and every value in the vector.
|
void |
applyIndexFunction(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 indices
|
SparseVector |
clone() |
void |
copyTo(Vec destination)
Copies the values of this Vector into another vector
|
double |
dot(Vec v)
Computes the dot product between two vectors, which is equivalent to
Σ thisi*vi This method should be overloaded for a serious implementation. |
boolean |
equals(Object obj,
double range) |
double |
get(int index)
Gets the value stored at a specific index in the vector
|
int |
getLastNonZeroIndex()
Returns the index of the last non-zero value, or -1 if all values are zero.
|
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.
|
int |
hashCode()
Provides a hashcode for Vectors.
|
void |
increment(int index,
double val)
Increments the value at the given index by the given value.
|
boolean |
isSparse()
Indicates whether or not this vector is optimized for sparce computation,
meaning that most values in the vector are zero - and considered
implicit.
|
double |
kurtosis()
Computes the kurtosis of this vector, which is the 4th moment.
|
int |
length()
Returns the length of this vector
|
double |
max()
Returns the maximum value stored in this vector
|
double |
median()
Returns the median value in this vector
|
double |
min()
Returns the minimum value stored in this vector
|
void |
multiply(double c,
Matrix A,
Vec b)
If this is vector a, this this computes b = b + c aT*A
|
void |
mutableAdd(double c)
Alters this vector such that
this = this + c
This method should be overloaded for a serious implementation. |
void |
mutableAdd(double c,
Vec v)
Alters this vector such that
this = this + c * b
This method should be overloaded for a serious implementation. |
void |
mutableDivide(double c)
Mutates
this /= c
This method should be overloaded for a serious implementation. |
void |
mutableMultiply(double c)
Mutates
this *= c
This method should be overloaded for a serious implementation. |
void |
mutablePairwiseDivide(Vec b)
Mutates
this by dividing each value by the value in b
that has the same index
This method should be overloaded for a serious implementation. |
void |
mutablePairwiseMultiply(Vec b)
Mutates
this by multiplying each value by the value in b
that has the same index. |
int |
nnz()
Computes the number of non zero values in this vector
|
void |
normalize()
Mutates this vector to be normalized by the L2 norm
|
double |
pNorm(double p)
Returns the p-norm of this vector.
|
double |
pNormDist(double p,
Vec y)
Returns the p-norm distance between this and another vector y.
|
void |
set(int index,
double val)
Sets the value stored at a specified index in the vector
|
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.
|
double |
skewness()
Computes the skewness of this vector, which is the 3rd moment.
|
Vec |
sortedCopy()
Returns a copy of this array with the values moved around so that they are in sorted order
|
double |
sum()
Computes the sum of the values in this vector
|
String |
toString() |
double |
variance()
Computes the variance of the values in this vector, which is
Vec.standardDeviation() 2 |
void |
zeroOut()
Zeroes out all values in this vector
This method should be overloaded for a serious implementation. |
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
finalize, getClass, notify, notifyAll, wait, wait, wait
forEach, spliterator
protected int used
protected int[] indexes
protected double[] values
public SparseVector(int length)
length
- the length of the sparse vectorpublic SparseVector(List<Double> vals)
vals
and sets
each value to the values in the list.vals
- the list of values to create a vector frompublic SparseVector(int length, int capacity)
capacity
non zero values. The
vector itself will start out with all zero values.length
- the length of the sparse vectorcapacity
- the number of non zero values to allocate space forpublic SparseVector(int[] indexes, double[] values, int length, int used)
indexes
values must
be increasing and all values less than length
and greater than
-1
up to the first used
indices.values
must be non zero and can not be a
special value. used
must be greater than -1 and less than the length of the
indexes
and values
arrays. indexes
and values
arrays must be the exact same
lengthindexes
- 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.public SparseVector(Vec toCopy)
toCopy
- the vector to copy the values ofpublic int length()
Vec
public void setLength(int length)
length
- the new length of this vectorpublic int nnz()
Vec
public void increment(int index, double val)
public double get(int index)
Vec
public void set(int index, double val)
Vec
public Vec sortedCopy()
Vec
sortedCopy
in class Vec
public int getLastNonZeroIndex()
public double min()
Vec
public double max()
Vec
public double sum()
Vec
public double variance()
Vec
Vec.standardDeviation()
2public double median()
Vec
public double skewness()
Vec
public double kurtosis()
Vec
public void copyTo(Vec destination)
Vec
public double dot(Vec v)
Vec
public void multiply(double c, Matrix A, Vec b)
Vec
public void mutableAdd(double c)
Vec
mutableAdd
in class Vec
c
- a scalar constant to add to each value in this vectorpublic void mutableAdd(double c, Vec v)
Vec
mutableAdd
in class Vec
c
- a scalar constantv
- the vector to add to thispublic void mutableMultiply(double c)
Vec
this *= c
mutableMultiply
in class Vec
c
- the constant to multiply bypublic void mutableDivide(double c)
Vec
this /= c
mutableDivide
in class Vec
c
- the constant to divide bypublic double pNormDist(double p, Vec y)
Vec
public double pNorm(double p)
Vec
public SparseVector clone()
public void normalize()
Vec
public void mutablePairwiseMultiply(Vec b)
Vec
this
by multiplying each value by the value in b
that has the same index.
mutablePairwiseMultiply
in class Vec
b
- the vector to pairwise multiply bypublic void mutablePairwiseDivide(Vec b)
Vec
this
by dividing each value by the value in b
that has the same index
mutablePairwiseDivide
in class Vec
b
- the vector to pairwise divide bypublic double[] arrayCopy()
Vec
public void applyFunction(Function f)
Vec
applyFunction
in class Vec
f
- the single variable function to applypublic void applyIndexFunction(IndexFunction f)
Vec
applyIndexFunction
in class Vec
f
- the 2 dimensional index function to applypublic void zeroOut()
Vec
public Iterator<IndexValue> getNonZeroIterator(int start)
Vec
Iterator.remove()
method.
getNonZeroIterator
in class Vec
start
- the first index (inclusive) to start returning non-zero
values frompublic int hashCode()
Vec
Vec.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();
public boolean isSparse()
Vec
Copyright © 2017. All rights reserved.