Package cc.mallet.types
Class SparseVector
- java.lang.Object
-
- cc.mallet.types.SparseVector
-
- All Implemented Interfaces:
ConstantMatrix
,Vector
,java.io.Serializable
- Direct Known Subclasses:
FeatureVector
,HashedSparseVector
,IndexedSparseVector
public class SparseVector extends java.lang.Object implements ConstantMatrix, Vector, java.io.Serializable
A vector that allocates memory only for non-zero values. When you create a SparseVector, you pass in a list of indices. These are the only elements of the vector you will be allowed to change. The rest are fixed at 0. The interface to Sparse vector uses the concept of a location, which is an integer in the range 0..numLocations which can be mapped to the index (and value) of a non zero element of the vector. A SparseVector can be sparse or dense depending on whether or not an array if indices is specified at construction time. If the SparseVector is dense, the mapping from location to index is the identity mapping. The type of the value an element in a SparseVector (or FeatureVector) can be double or binary (0.0 or 1.0), depending on whether an array of doubles is specified at contruction time.- Author:
- Andrew McCallum mccallum@cs.umass.edu
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description protected boolean
hasInfinite
protected int[]
indices
If the vector is sparse, then both indices and values are sparse.protected double[]
values
-
Constructor Summary
Constructors Constructor Description SparseVector()
An empty vector, with all zero valuesSparseVector(double[] values)
SparseVector(double[] values, boolean copy)
SparseVector(int[] indices)
SparseVector(int[] indices, boolean copy)
SparseVector(int[] indices, boolean copy, boolean checkIndicesSorted)
SparseVector(int[] indices, boolean copy, boolean checkIndicesSorted, boolean removeDuplicates, boolean binary)
SparseVector(int[] indices, double[] values)
SparseVector(int[] indices, double[] values, boolean copy)
SparseVector(int[] indices, double[] values, boolean copy, boolean checkIndicesSorted)
SparseVector(int[] indices, double[] values, boolean copy, boolean checkIndicesSorted, boolean removeDuplicates)
SparseVector(int[] indices, double[] values, int capacity, int size, boolean copy, boolean checkIndicesSorted, boolean removeDuplicates)
If "indices" is null, the vector will be dense.SparseVector(int[] indices, int capacity, int size, boolean copy, boolean checkIndicesSorted, boolean removeDuplicates)
SparseVector(int size, double fillValue)
SparseVector(Alphabet dict, PropertyList pl, boolean binary)
SparseVector(Alphabet dict, PropertyList pl, boolean binary, boolean growAlphabet)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description double
absNorm()
void
addTo(double[] accumulator)
void
addTo(double[] accumulator, double scale)
void
arrayCopyFrom(double[] a)
Copy values from an array into this vector.int
arrayCopyFrom(double[] a, int startingArrayLocation)
Copy values from an array starting at a particular location into this vector.int
arrayCopyInto(double[] array, int startingArrayLocation)
Copy the contents of this vector into an array starting at a particular location.ConstantMatrix
cloneMatrix()
CLONINGConstantMatrix
cloneMatrixZeroed()
double
dotProduct(double[] v)
VECTOR OPERATIONSdouble
dotProduct(ConstantMatrix m)
double
dotProduct(DenseVector v)
double
dotProduct(SparseVector v)
double
extendedDotProduct(DenseVector v)
double
extendedDotProduct(SparseVector v)
int
getDimensions(int[] sizes)
int[]
getIndices()
int
getNumDimensions()
ACCESSORSdouble[]
getValues()
void
incrementValue(int index, double value)
Increments this[index] by value.int
indexAtLocation(int location)
double
infinityNorm()
boolean
isBinary()
boolean
isInfinite()
boolean
isNaN()
boolean
isNaNOrInfinite()
int
location(int index)
void
makeBinary()
void
makeNonBinary()
void
map(java.lang.reflect.Method f)
Applies the method argument to each value in a non-binary vector.int
numLocations()
double
oneNorm()
void
plusEqualsSparse(SparseVector v)
For each index i that is present in this vector, set this[i] += v[i].void
plusEqualsSparse(SparseVector v, double factor)
For each index i that is present in this vector, set this[i] += factor * v[i].void
print()
protected void
removeDuplicates(int numDuplicates)
void
setAll(double v)
Sets every present index in the vector to v.void
setValue(int index, double value)
Sets the value at the given index.void
setValueAtLocation(int location, double value)
Sets the value at the given location.int
singleIndex(int[] indices)
int
singleSize()
void
singleToIndices(int i, int[] indices)
double
singleValue(int i)
protected void
sortIndices()
void
timesEquals(double factor)
Scale all elements by the same factor.void
timesEqualsSparse(SparseVector v)
For each index i that is present in this vector, set this[i] *= v[i].void
timesEqualsSparse(SparseVector v, double factor)
For each index i that is present in this vector, set this[i] *= factor * v[i].void
timesEqualsSparseZero(SparseVector v, double factor)
For each index i that is present in this vector, set this[i] *= factor * v[i].java.lang.String
toString()
java.lang.String
toString(boolean onOneLine)
double
twoNorm()
double
value(int index)
double
value(int[] indices)
double
valueAtLocation(int location)
SparseVector
vectorAdd(SparseVector v, double scale)
-
-
-
Field Detail
-
indices
protected int[] indices
If the vector is sparse, then both indices and values are sparse. Indices into these arrays are called ``locations'' in the below. The indices[] array maps locations to indices of the (virtual) dense array that's being represented. value[] maps locations to values.
-
values
protected double[] values
-
hasInfinite
protected boolean hasInfinite
-
-
Constructor Detail
-
SparseVector
public SparseVector(int[] indices, double[] values, int capacity, int size, boolean copy, boolean checkIndicesSorted, boolean removeDuplicates)
If "indices" is null, the vector will be dense. If "values" is null, the vector will be binary. The capacity and size arguments are used by AugmentableFeatureVector.
-
SparseVector
public SparseVector(double[] values, boolean copy)
-
SparseVector
public SparseVector(double[] values)
-
SparseVector
public SparseVector(int size, double fillValue)
-
SparseVector
public SparseVector(int[] indices, double[] values, boolean copy, boolean checkIndicesSorted, boolean removeDuplicates)
-
SparseVector
public SparseVector(int[] indices, double[] values)
-
SparseVector
public SparseVector(int[] indices, double[] values, boolean copy)
-
SparseVector
public SparseVector(int[] indices, double[] values, boolean copy, boolean checkIndicesSorted)
-
SparseVector
public SparseVector(int[] indices, boolean copy, boolean checkIndicesSorted, boolean removeDuplicates, boolean binary)
-
SparseVector
public SparseVector(int[] indices, int capacity, int size, boolean copy, boolean checkIndicesSorted, boolean removeDuplicates)
-
SparseVector
public SparseVector(int[] indices, boolean copy, boolean checkIndicesSorted)
-
SparseVector
public SparseVector(int[] indices, boolean copy)
-
SparseVector
public SparseVector(int[] indices)
-
SparseVector
public SparseVector()
An empty vector, with all zero values
-
SparseVector
public SparseVector(Alphabet dict, PropertyList pl, boolean binary, boolean growAlphabet)
-
SparseVector
public SparseVector(Alphabet dict, PropertyList pl, boolean binary)
-
-
Method Detail
-
isBinary
public boolean isBinary()
-
makeBinary
public void makeBinary()
-
makeNonBinary
public void makeNonBinary()
-
getNumDimensions
public int getNumDimensions()
ACCESSORS- Specified by:
getNumDimensions
in interfaceConstantMatrix
-
getDimensions
public int getDimensions(int[] sizes)
- Specified by:
getDimensions
in interfaceConstantMatrix
-
getIndices
public int[] getIndices()
-
getValues
public double[] getValues()
-
numLocations
public int numLocations()
- Specified by:
numLocations
in interfaceConstantMatrix
-
location
public int location(int index)
- Specified by:
location
in interfaceConstantMatrix
-
valueAtLocation
public double valueAtLocation(int location)
- Specified by:
valueAtLocation
in interfaceConstantMatrix
-
indexAtLocation
public int indexAtLocation(int location)
- Specified by:
indexAtLocation
in interfaceConstantMatrix
-
value
public double value(int[] indices)
- Specified by:
value
in interfaceConstantMatrix
-
addTo
public void addTo(double[] accumulator, double scale)
-
addTo
public void addTo(double[] accumulator)
-
singleIndex
public int singleIndex(int[] indices)
- Specified by:
singleIndex
in interfaceConstantMatrix
-
singleToIndices
public void singleToIndices(int i, int[] indices)
- Specified by:
singleToIndices
in interfaceConstantMatrix
-
singleValue
public double singleValue(int i)
- Specified by:
singleValue
in interfaceConstantMatrix
-
singleSize
public int singleSize()
- Specified by:
singleSize
in interfaceConstantMatrix
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
toString
public java.lang.String toString(boolean onOneLine)
-
cloneMatrix
public ConstantMatrix cloneMatrix()
CLONING- Specified by:
cloneMatrix
in interfaceConstantMatrix
-
cloneMatrixZeroed
public ConstantMatrix cloneMatrixZeroed()
-
plusEqualsSparse
public void plusEqualsSparse(SparseVector v)
For each index i that is present in this vector, set this[i] += v[i]. If v has indices that are not present in this, these are just ignored.
-
plusEqualsSparse
public void plusEqualsSparse(SparseVector v, double factor)
For each index i that is present in this vector, set this[i] += factor * v[i]. If v has indices that are not present in this, these are just ignored.
-
timesEqualsSparse
public void timesEqualsSparse(SparseVector v)
For each index i that is present in this vector, set this[i] *= v[i]. If v has indices that are not present in this, these are just ignored.
-
timesEqualsSparse
public void timesEqualsSparse(SparseVector v, double factor)
For each index i that is present in this vector, set this[i] *= factor * v[i]. If v has indices that are not present in this, these are just ignored.
-
timesEqualsSparseZero
public void timesEqualsSparseZero(SparseVector v, double factor)
For each index i that is present in this vector, set this[i] *= factor * v[i]. If v has indices that are not present in this, these are set to zero
-
timesEquals
public void timesEquals(double factor)
Scale all elements by the same factor.
-
incrementValue
public void incrementValue(int index, double value) throws java.lang.IllegalArgumentException
Increments this[index] by value.- Throws:
java.lang.IllegalArgumentException
- If index is not present.
-
setAll
public void setAll(double v)
Sets every present index in the vector to v.
-
setValue
public void setValue(int index, double value) throws java.lang.IllegalArgumentException
Sets the value at the given index.- Throws:
java.lang.IllegalArgumentException
- If index is not present.
-
setValueAtLocation
public void setValueAtLocation(int location, double value)
Sets the value at the given location.
-
arrayCopyFrom
public final void arrayCopyFrom(double[] a)
Copy values from an array into this vector. The array should have the same size as the vector
-
arrayCopyFrom
public final int arrayCopyFrom(double[] a, int startingArrayLocation)
Copy values from an array starting at a particular location into this vector. The array must have at least as many values beyond the starting location as there are in the vector.- Returns:
- Next uncopied location in the array.
-
map
public final void map(java.lang.reflect.Method f) throws java.lang.IllegalAccessException, java.lang.Throwable
Applies the method argument to each value in a non-binary vector. The method should both accept a Double as an argument and return a Double.- Throws:
java.lang.IllegalArgumentException
- If the method argument has an inappropriate signature.java.lang.UnsupportedOperationException
- If vector is binaryjava.lang.IllegalAccessException
- If the method is inaccessiblejava.lang.Throwable
- If the method throws an exception it is relayed
-
arrayCopyInto
public final int arrayCopyInto(double[] array, int startingArrayLocation)
Copy the contents of this vector into an array starting at a particular location.- Returns:
- Next available location in the array
-
dotProduct
public double dotProduct(double[] v)
VECTOR OPERATIONS
-
dotProduct
public double dotProduct(ConstantMatrix m)
- Specified by:
dotProduct
in interfaceConstantMatrix
-
dotProduct
public double dotProduct(DenseVector v)
-
extendedDotProduct
public double extendedDotProduct(DenseVector v)
-
dotProduct
public double dotProduct(SparseVector v)
-
extendedDotProduct
public double extendedDotProduct(SparseVector v)
-
vectorAdd
public SparseVector vectorAdd(SparseVector v, double scale)
-
oneNorm
public double oneNorm()
- Specified by:
oneNorm
in interfaceConstantMatrix
-
absNorm
public double absNorm()
- Specified by:
absNorm
in interfaceConstantMatrix
-
twoNorm
public double twoNorm()
- Specified by:
twoNorm
in interfaceConstantMatrix
-
infinityNorm
public double infinityNorm()
- Specified by:
infinityNorm
in interfaceConstantMatrix
-
print
public void print()
- Specified by:
print
in interfaceConstantMatrix
-
isNaN
public boolean isNaN()
- Specified by:
isNaN
in interfaceConstantMatrix
-
isInfinite
public boolean isInfinite()
-
isNaNOrInfinite
public boolean isNaNOrInfinite()
-
sortIndices
protected void sortIndices()
-
removeDuplicates
protected void removeDuplicates(int numDuplicates)
-
-