jsat.linear
Class Vec
- java.lang.Object
-
- jsat.linear.Vec
-
- All Implemented Interfaces:
- java.io.Serializable, java.lang.Cloneable, java.lang.Iterable<IndexValue>
- Direct Known Subclasses:
- ConcatenatedVec, ConstantVector, DenseVector, Poly2Vec, RandomVector, ScaledVector, ShiftedVec, SparseVector, SubVector, VecPaired, VecWithNorm
public abstract class Vec extends java.lang.Object implements java.lang.Cloneable, java.lang.Iterable<IndexValue>, java.io.Serializable
Vec is a object representing the math concept of a vector. A vector could be either sparse or dense, where sparse vectors have a high number of zero values that are not explicitly stored.
This abstract class provides a large number of pre-implemented methods. Some of which are implemented only for a dense vector, or may not be completely efficient for the underlying implementation. Methods that should be considered for overloading by an implementation will be indicated in the documentation.- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor and Description Vec()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method and Description Vecadd(double c)Returns a new vector that is the result ofthis + cVecadd(Vec b)Returns a new vector that is the result ofthis + bvoidapplyFunction(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 indicesbooleancanBeMutated()Indicates whether or not this vector can be mutated.abstract Vecclone()voidcopyTo(Vec destination)Copies the values of this Vector into another vectorvoidcopyToCol(Matrix A, int col)Copies the values of this vector into a column of another Matrix.voidcopyToRow(Matrix A, int row)Copies the values of this vector into a row of another MatrixintcountNaNs()Vecdivide(double c)Returns a new vector that is the result ofthis / cdoubledot(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)booleanequals(java.lang.Object obj, double range)abstract doubleget(int index)Gets the value stored at a specific index in the vectorjava.util.Iterator<IndexValue>getNonZeroIterator()Returns an iterator that will go over the non zero values in the given vector.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 stored at a specified index in the vectorabstract booleanisSparse()Indicates whether or not this vector is optimized for sparce computation, meaning that most values in the vector are zero - and considered implicit.java.util.Iterator<IndexValue>iterator()doublekurtosis()Computes the kurtosis of this vector, which is the 4th moment.abstract intlength()Returns the length of this vectordoublemax()Returns the maximum value stored in this vectordoublemean()Computes the mean value of all values stored in this vectordoublemedian()Returns the median value in this vectordoublemin()Returns the minimum value stored in this vectorVecmultiply(double c)Returns a new vector that is the result ofthis * cvoidmultiply(double c, Matrix A, Vec b)If this is vector a, this this computes b = b + c aT*AVecmultiply(Matrix A)Returns a new vector that is the result of the vector matrix product thisTAvoidmultiply(Matrix A, Vec b)If this is vector a, this this computes b = b + 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 b)Alters this vector such that this = this + c * b
This method should be overloaded for a serious implementation.voidmutableAdd(Vec b)Alters this vector such that this = this + bvoidmutableDivide(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.voidmutableSubtract(double c)Alters this vector such that this = this - cvoidmutableSubtract(double c, Vec b)Alters this vector such that this = this - c * bvoidmutableSubtract(Vec b)Alters this vector such that this = this - bintnnz()Computes the number of non zero values in this vectorvoidnormalize()Mutates this vector to be normalized by the L2 normVecnormalized()Returns a new vector that is the result of normalizing this vector by the L2 normVecpairwiseDivide(Vec b)Returns a new vector that is the result of dividing each value inthisby the value in the same index inbVecpairwiseMultiply(Vec b)Returns a new vector that is the result of multiplying each value inthisby its corresponding value inbdoublepNorm(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.static Vecrandom(int length)Creates a dense vector full of random values in the range [0, 1]static Vecrandom(int length, java.util.Random rand)Creates a dense vector full of random values in the range [0, 1]abstract voidset(int index, double val)Sets the value stored at a specified index in the vectordoubleskewness()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 orderdoublestandardDeviation()Computes the standard deviation of the values in this vectorVecsubtract(double c)Returns a new vector that is the result ofthis - cVecsubtract(Vec b)Returns a new vector that is the result ofthis - bdoublesum()Computes the sum of the values in this vectorjava.lang.StringtoString()doublevariance()Computes the variance of the values in this vector, which isstandardDeviation()2voidzeroOut()Zeroes out all values in this vector
This method should be overloaded for a serious implementation.static Veczeros(int length)Creates a dense vector full of zeros.
-
-
-
Method Detail
-
length
public abstract int length()
Returns the length of this vector- Returns:
- the length of this vector
-
countNaNs
public int countNaNs()
- Returns:
- the number of NaNs present in this vector
-
canBeMutated
public boolean canBeMutated()
Indicates whether or not this vector can be mutated. Iffalse, any method that contains "mutate" will not work.
By default, this returnstrue- Returns:
trueif the vector supports being altered,falseother wise.
-
nnz
public int nnz()
Computes the number of non zero values in this vector- Returns:
- the number of non zero values stored
-
get
public abstract double get(int index)
Gets the value stored at a specific index in the vector- Parameters:
index- the index to access- Returns:
- the double value in the vector
- Throws:
java.lang.IndexOutOfBoundsException- if the index given is greater than or equal to itslength()
-
set
public abstract void set(int index, double val)Sets the value stored at a specified index in the vector- Parameters:
index- the index to accessval- the value to store in the index- Throws:
java.lang.IndexOutOfBoundsException- if the index given is greater than or equal to itslength()
-
increment
public void increment(int index, double val)Increments the value stored at a specified index in the vector- Parameters:
index- the index to accessval- the value to store in the index- Throws:
java.lang.IndexOutOfBoundsException- if the index given is greater than or equal to itslength()
-
add
public Vec add(double c)
Returns a new vector that is the result ofthis + c- Parameters:
c- the constant to add- Returns:
- the result of adding
ctothis
-
add
public Vec add(Vec b)
Returns a new vector that is the result ofthis + b- Parameters:
b- the vector to add- Returns:
- the result of
b + this
-
subtract
public Vec subtract(double c)
Returns a new vector that is the result ofthis - c- Parameters:
c- the constant to subtract- Returns:
- the result of
this - c
-
subtract
public Vec subtract(Vec b)
Returns a new vector that is the result ofthis - b- Parameters:
b- the vector to subtract fromthis- Returns:
- the result of
this - b
-
pairwiseMultiply
public Vec pairwiseMultiply(Vec b)
Returns a new vector that is the result of multiplying each value inthisby its corresponding value inb- Parameters:
b- the vector to pairwise multiply by- Returns:
- the result of the pairwise multiplication of
bonto the values ofthis
-
multiply
public Vec multiply(double c)
Returns a new vector that is the result ofthis * c- Parameters:
c- the constant to multiply by- Returns:
- the result of
this * c
-
multiply
public Vec multiply(Matrix A)
Returns a new vector that is the result of the vector matrix product thisTA- Parameters:
A- the matrix to multiply with- Returns:
- the vector matrix product
-
multiply
public void multiply(Matrix A, Vec b)
If this is vector a, this this computes b = b + aT*A- Parameters:
A- the matrix to multiple byb- the vector to mutate by adding the result to
-
multiply
public void multiply(double c, Matrix A, Vec b)If this is vector a, this this computes b = b + c aT*A- Parameters:
c- the constant factor to multiply byA- the matrix to multiple byb- the vector to mutate by adding the result to
-
pairwiseDivide
public Vec pairwiseDivide(Vec b)
Returns a new vector that is the result of dividing each value inthisby the value in the same index inb- Parameters:
b- the vector to pairwise divide by- Returns:
- the result of pairwise division of
thisbyb
-
divide
public Vec divide(double c)
Returns a new vector that is the result ofthis / c- Parameters:
c- the constant to divide by- Returns:
- the result of
this / c
-
mutableAdd
public void mutableAdd(double c)
Alters this vector such that this = this + c
This method should be overloaded for a serious implementation.- Parameters:
c- a scalar constant to add to each value in this vector
-
mutableAdd
public void mutableAdd(double c, Vec b)Alters this vector such that this = this + c * b
This method should be overloaded for a serious implementation.- Parameters:
c- a scalar constantb- the vector to add to this
-
mutableAdd
public void mutableAdd(Vec b)
Alters this vector such that this = this + b- Parameters:
b- the vector to add to this- Throws:
java.lang.ArithmeticException- if the vectors do not have the same length
-
mutableSubtract
public void mutableSubtract(double c)
Alters this vector such that this = this - c- Parameters:
c- the scalar constant to subtract from all values in this vector
-
mutableSubtract
public void mutableSubtract(double c, Vec b)Alters this vector such that this = this - c * b- Parameters:
c- a scalar constantb- the vector to subtract from this- Throws:
java.lang.ArithmeticException- if the vectors do not have the same length
-
mutableSubtract
public void mutableSubtract(Vec b)
Alters this vector such that this = this - b- Parameters:
b- the vector to subtract from this- Throws:
java.lang.ArithmeticException- if the vectors are not the same length
-
mutablePairwiseMultiply
public void mutablePairwiseMultiply(Vec b)
Mutatesthisby multiplying each value by the value inbthat has the same index.
This method should be overloaded for a serious implementation.- Parameters:
b- the vector to pairwise multiply by
-
mutableMultiply
public void mutableMultiply(double c)
Mutatesthis *= c
This method should be overloaded for a serious implementation.- Parameters:
c- the constant to multiply by
-
mutablePairwiseDivide
public void mutablePairwiseDivide(Vec b)
Mutatesthisby dividing each value by the value inbthat has the same index
This method should be overloaded for a serious implementation.- Parameters:
b- the vector to pairwise divide by
-
mutableDivide
public void mutableDivide(double c)
Mutatesthis /= c
This method should be overloaded for a serious implementation.- Parameters:
c- the constant to divide by
-
sortedCopy
public Vec sortedCopy()
Returns a copy of this array with the values moved around so that they are in sorted order- Returns:
- a new array in sorted order
-
min
public double min()
Returns the minimum value stored in this vector- Returns:
- the minimum value in this vector
-
max
public double max()
Returns the maximum value stored in this vector- Returns:
- the maximum value in this vector
-
sum
public double sum()
Computes the sum of the values in this vector- Returns:
- the sum of this vector's values
-
mean
public double mean()
Computes the mean value of all values stored in this vector- Returns:
- the mean value
-
standardDeviation
public double standardDeviation()
Computes the standard deviation of the values in this vector- Returns:
- the standard deviation
-
variance
public double variance()
Computes the variance of the values in this vector, which isstandardDeviation()2- Returns:
- the variance
-
median
public double median()
Returns the median value in this vector- Returns:
- the median
-
skewness
public double skewness()
Computes the skewness of this vector, which is the 3rd moment.- Returns:
- the skewness
-
kurtosis
public double kurtosis()
Computes the kurtosis of this vector, which is the 4th moment.- Returns:
- the kurtosis
-
isSparse
public abstract 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. Only non-zero values are stored.- Returns:
- true if the vector is sparce, false otherwise.
-
copyTo
public void copyTo(Vec destination)
Copies the values of this Vector into another vector- Parameters:
destination- the vector to store the values in.- Throws:
java.lang.ArithmeticException- if the vectors are not of the same length
-
copyToRow
public void copyToRow(Matrix A, int row)
Copies the values of this vector into a row of another Matrix- Parameters:
A- the matrix to store the contents of this vector inrow- the row of the matrix to store the values to- Throws:
java.lang.ArithmeticException- if the columns of the matrix is not the same as the length of this vector.
-
copyToCol
public void copyToCol(Matrix A, int col)
Copies the values of this vector into a column of another Matrix.- Parameters:
A- the matrix to store the contents of this vector incol- the column of the matrix to store the values to
-
clone
public abstract Vec clone()
- Overrides:
clonein classjava.lang.Object
-
normalized
public Vec normalized()
Returns a new vector that is the result of normalizing this vector by the L2 norm- Returns:
- a normalized version of this vector
-
normalize
public void normalize()
Mutates this vector to be normalized by the L2 norm
-
applyFunction
public void applyFunction(Function1D f)
Applies the given function to each and every value in the vector.
This method should be overloaded for a serious implementation.- Parameters:
f- the single variable function to apply
-
applyIndexFunction
public void applyIndexFunction(IndexFunction f)
Applies 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.- Parameters:
f- the 2 dimensional index function to apply
-
pNormDist
public double pNormDist(double p, Vec y)Returns the p-norm distance between this and another vector y.- Parameters:
p- the distance type. 2 is the common valuey- the other vector to compare against- Returns:
- the p-norm distance
-
pNorm
public double pNorm(double p)
Returns the p-norm of this vector.- Parameters:
p- the norm type. 2 is a common value- Returns:
- the p-norm of this vector
-
dot
public 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.- Parameters:
v- the other vector- Returns:
- the dot product of this vector and another
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
equals
public boolean equals(java.lang.Object obj)
- Overrides:
equalsin classjava.lang.Object
-
equals
public boolean equals(java.lang.Object obj, double range)
-
arrayCopy
public double[] arrayCopy()
Creates a new array that contains all the values of this vector in the appropriate indices- Returns:
- a new array that is a copy of this vector
-
iterator
public java.util.Iterator<IndexValue> iterator()
- Specified by:
iteratorin interfacejava.lang.Iterable<IndexValue>
-
getNonZeroIterator
public java.util.Iterator<IndexValue> getNonZeroIterator()
Returns an iterator that will go over the non zero values in the given vector. The iterator does not support theIterator.remove()method.- Returns:
- an iterator for the non zero index value pairs.
-
getNonZeroIterator
public 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. The iterator does not support theIterator.remove()method.
This method should be overloaded for a serious implementation.- Parameters:
start- the first index (inclusive) to start returning non-zero values from- Returns:
- an iterator for the non zero index value pairs
-
zeroOut
public void zeroOut()
Zeroes out all values in this vector
This method should be overloaded for a serious implementation.
-
hashCode
public int hashCode()
Provides a hashcode for Vectors. All vector implementations should return the same result for cases whenequals(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();
- Overrides:
hashCodein classjava.lang.Object- Returns:
- the hash code for a vector
-
random
public static Vec random(int length)
Creates a dense vector full of random values in the range [0, 1]- Parameters:
length- the length of the random vector to create- Returns:
- a random vector of the specified length
-
random
public static Vec random(int length, java.util.Random rand)
Creates a dense vector full of random values in the range [0, 1]- Parameters:
length- the length of the random vector to createrand- the source of randomness- Returns:
- a random vector of the specified length
-
zeros
public static Vec zeros(int length)
Creates a dense vector full of zeros.- Parameters:
length- the length of the vector to create- Returns:
- a vector of zeros
-
-
DataMelt 3.0 © DataMelt by jWork.ORG