public class SparseFloatVector extends AbstractVector implements Serializable
SparseFloatVector implements an immutable sparse
vector with values represented as single-precision floating point
numbers. Sparse vectors are specified in terms of mappings from
integer dimensions to single-precision floating-point values. The
constructor allows the number of dimensions to be set, or to be
inferred as the largest dimension with a value in the mapping.
Dimensions for which no value is specified in the map provided to
the constructor will have values of 0.0.
A deep copy is made of the map provided to the constructor, so that changes to the specified map do not affect this vector and changes to this vector do not affect the map.
Implementation Note: The underlying data is stored in a
pair of parallel arrays, one containing integer indexes and the
other values of type float. The constructor computes
and stores the fixed number of dimensions. The constructor also
stores the length of the vector by walking over the values. Dot
products between sparse vectors are computed at double-precision by
walking over the indices and doing a merge, which is the most
efficient approach if the vectors are roughly the same size. Dot
products with other vector implementations are computed by
iterating over the indexes in the sparse vector and looking up the
corresponding values in the argument vector. Cosines are computed
by dividing dot products by lengths.
Equality versus other sparse float vectors only considers indexes with values. Hash codes also only consider indexes with values, computing a shift and mask as well as an integer multiply and add for each dimension.
| Constructor and Description |
|---|
SparseFloatVector(int[] keys,
float[] values,
int numDimensions)
Construct a sparse floating point vector with the specified
keys defined at the specified values with the specified number
of dimensions.
|
SparseFloatVector(Map<Integer,? extends Number> map)
Construct a sparse vector from the specified map.
|
SparseFloatVector(Map<Integer,? extends Number> map,
int numDimensions)
Constructs a sparse vector from the specified map with the
specified number of dimensions.
|
| Modifier and Type | Method and Description |
|---|---|
Vector |
add(Vector v)
Returns the result of adding the specified vector to this
vector.
|
double |
cosine(Vector v)
Returns the cosine of this vector and the specified vector.
|
double |
dotProduct(Vector v)
Returns the dot product (inner product) of this vector with the
specified vector.
|
boolean |
equals(Object that)
Returns true if the specified object is a vector
with the same dimensionality and values as this vector.
|
int |
hashCode()
Returns the hash code for this sparse float vector.
|
void |
increment(double scale,
Vector v)
This operation is not supported for sparse vectors.
|
double |
length()
Returns the length of this vector.
|
int[] |
nonZeroDimensions()
Returns the array of dimensions that have non-zero values.
|
int |
numDimensions()
Returns the number of dimensions of this vector.
|
String |
toString() |
double |
value(int dimension)
The value of this vector for the specified dimension.
|
setValuepublic SparseFloatVector(Map<Integer,? extends Number> map)
map - Mapping from dimensions to values.IllegalArgumentException - If there are negative keys.public SparseFloatVector(Map<Integer,? extends Number> map, int numDimensions)
map - Mapping from dimensions to values.numDimensions - Number of dimensions for the constructed vector.IllegalArgumentException - If there are negative keys, or if the
specified number of dimensions is negative, or if the specified number of
dimensions is not greater than or equal to the largest integer key.public SparseFloatVector(int[] keys,
float[] values,
int numDimensions)
keys - Array of keys indicating the defined dimensions.values - Array of values for specified dimensions.numDimensions - The dimensionality of the constructed vector.IllegalArgumentException - If the keys are not in ascending order,
if a key is negative, if two keys are the same, or if a key is greater
than or equal to the number of dimensions.public int numDimensions()
AbstractVectorAbstractVector.value(int).numDimensions in interface VectornumDimensions in class AbstractVectorpublic int[] nonZeroDimensions()
Warning:The ret8urned array is the actual set of dimensions used for this vector implementation, so should not be modified. Modifications result in a vector in an illegal states if the dimensions don't remain sorted and within the range of the dimensionality of this vector.
nonZeroDimensions in interface VectornonZeroDimensions in class AbstractVectorpublic void increment(double scale,
Vector v)
increment in interface Vectorincrement in class AbstractVectorscale - Ignored.v - Ignored.UnsupportedOperationException - Always.public double value(int dimension)
AbstractVectorAbstractVector.numDimensions().value in interface Vectorvalue in class AbstractVectordimension - Dimension whose value is returned.public double length()
AbstractVectorthe implementation iterates over the dimensions once accessing each value.
length in interface Vectorlength in class AbstractVectorpublic Vector add(Vector v)
AbstractVectorImplementation Note: The result is a dense vector and this method iterates over the dimensions adding. Subclasses may override this with a more specific implementation and then fall back on this implementation for the general case.
add in interface Vectoradd in class AbstractVectorv - Vector to add to this vector.public double dotProduct(Vector v)
AbstractVectorImplementation Note: This method iterates over the dimensions, accessing values for this vector and the specified vector for each dimension.
dotProduct in interface VectordotProduct in class AbstractVectorv - The specified vector.public boolean equals(Object that)
Implementation Note: This method requires a get and comparison for each dimension with a non-zero value in this vector.
equals in interface Vectorequals in class AbstractVectorthat - Specified object.true if the specified object is a vector
with the same dimensionality and values as this vector.public int hashCode()
Implementation Note: hashing requires a long integer shift and mask, as well as a normal integer multiply and add for each dimension with a value.
hashCode in interface VectorhashCode in class AbstractVectorpublic double cosine(Vector v)
AbstractVectorImplementation Note: This method iterates over the dimensions once and accesses the value of each vector once per dimension.
cosine in interface Vectorcosine in class AbstractVectorv - The specified vector.Copyright © 2016 Alias-i, Inc.. All rights reserved.