Class OpenMapRealVector
- All Implemented Interfaces:
Serializable
,RealVector
,SparseRealVector
RealVector
interface with a OpenIntToDoubleHashMap
backing store.- Since:
- 2.0
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from interface org.apache.commons.math.linear.RealVector
RealVector.Entry
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final double
Default Tolerance for having a value considered zero. -
Constructor Summary
ConstructorsConstructorDescriptionBuild a 0-length vector.OpenMapRealVector
(double[] values) Create from a double array.OpenMapRealVector
(double[] values, double epsilon) Create from a double array, specifying zero tolerance.OpenMapRealVector
(int dimension) Construct a (dimension)-length vector of zeros.OpenMapRealVector
(int dimension, double epsilon) Construct a (dimension)-length vector of zeros, specifying zero tolerance.OpenMapRealVector
(int dimension, int expectedSize) Build a vector with known the sparseness (for advanced use only).OpenMapRealVector
(int dimension, int expectedSize, double epsilon) Build a vector with known the sparseness and zero tolerance setting (for advanced use only).OpenMapRealVector
(Double[] values) Create from a Double array.OpenMapRealVector
(Double[] values, double epsilon) Create from a Double array.Copy constructor.Generic copy constructor. -
Method Summary
Modifier and TypeMethodDescriptionOptimized method to add two OpenMapRealVectors.add
(RealVector v) Compute the sum of this vector andv
.append
(double d) Construct a vector by appending a double to this vector.append
(double[] a) Construct a vector by appending a double array to this vector.Optimized method to append a OpenMapRealVector.append
(RealVector v) Construct a vector by appending a vector to this vector.copy()
Returns a (deep) copy of this vector.double
Optimized method to compute the dot product with an OpenMapRealVector.double
Compute the dot product.ebeDivide
(double[] v) Element-by-element division.Element-by-element division.ebeMultiply
(double[] v) Element-by-element multiplication.Element-by-element multiplication.boolean
Implementation Note: This performs an exact comparison, and as a result it is possible fora.subtract(b
} to be the zero vector, whilea.equals(b) == false
.double[]
getData()
Returns vector entries as a double array.int
Returns the size of the vector.double
getDistance
(double[] v) Distance between two vectors.double
Optimized method to compute distance.double
Distance between two vectors.double
getEntry
(int index) Returns the entry in the specified index.double
getL1Distance
(double[] v) Distance between two vectors.double
Distance between two vectors.double
Distance between two vectors.double
getLInfDistance
(double[] v) Distance between two vectors.double
Distance between two vectors.double
Deprecated.double
getSubVector
(int index, int n) Get a subvector from consecutive elements.int
hashCode()
boolean
Check whether any coordinate of this vector is infinite and none areNaN
.boolean
isNaN()
Check whether any coordinate of this vector isNaN
.mapAdd
(double d) Add a value to each entry.mapAddToSelf
(double d) Add a value to each entry.outerProduct
(double[] v) Compute the outer product.projection
(double[] v) Find the orthogonal projection of this vector onto another vector.Find the orthogonal projection of this vector onto another vector.void
set
(double value) Set all elements to a single value.void
setEntry
(int index, double value) Set a single element.void
setSubVector
(int index, double[] v) Set a set of consecutive elements.void
setSubVector
(int index, RealVector v) Set a set of consecutive elements.Specialized implementations may choose to not iterate over all dimensions, either because those values are unset, or are equal to defaultValue(), or are small enough to be ignored for the purposes of iteration.subtract
(double[] v) Subtractv
from this vector.Optimized method to subtract OpenMapRealVectors.Subtractv
from this vector.double[]
toArray()
Convert the vector to a double array.void
unitize()
Converts this vector into a unit vector.Creates a unit vector pointing in the direction of this vector.Methods inherited from class org.apache.commons.math.linear.AbstractRealVector
add, dotProduct, getL1Norm, getLInfNorm, getMaxIndex, getMaxValue, getMinIndex, getMinValue, getNorm, iterator, map, mapAbs, mapAbsToSelf, mapAcos, mapAcosToSelf, mapAsin, mapAsinToSelf, mapAtan, mapAtanToSelf, mapCbrt, mapCbrtToSelf, mapCeil, mapCeilToSelf, mapCos, mapCosh, mapCoshToSelf, mapCosToSelf, mapDivide, mapDivideToSelf, mapExp, mapExpm1, mapExpm1ToSelf, mapExpToSelf, mapFloor, mapFloorToSelf, mapInv, mapInvToSelf, mapLog, mapLog10, mapLog10ToSelf, mapLog1p, mapLog1pToSelf, mapLogToSelf, mapMultiply, mapMultiplyToSelf, mapPow, mapPowToSelf, mapRint, mapRintToSelf, mapSignum, mapSignumToSelf, mapSin, mapSinh, mapSinhToSelf, mapSinToSelf, mapSqrt, mapSqrtToSelf, mapSubtract, mapSubtractToSelf, mapTan, mapTanh, mapTanhToSelf, mapTanToSelf, mapToSelf, mapUlp, mapUlpToSelf, outerProduct
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.apache.commons.math.linear.RealVector
add, dotProduct, getL1Norm, getLInfNorm, getNorm, iterator, map, mapAbs, mapAbsToSelf, mapAcos, mapAcosToSelf, mapAsin, mapAsinToSelf, mapAtan, mapAtanToSelf, mapCbrt, mapCbrtToSelf, mapCeil, mapCeilToSelf, mapCos, mapCosh, mapCoshToSelf, mapCosToSelf, mapDivide, mapDivideToSelf, mapExp, mapExpm1, mapExpm1ToSelf, mapExpToSelf, mapFloor, mapFloorToSelf, mapInv, mapInvToSelf, mapLog, mapLog10, mapLog10ToSelf, mapLog1p, mapLog1pToSelf, mapLogToSelf, mapMultiply, mapMultiplyToSelf, mapPow, mapPowToSelf, mapRint, mapRintToSelf, mapSignum, mapSignumToSelf, mapSin, mapSinh, mapSinhToSelf, mapSinToSelf, mapSqrt, mapSqrtToSelf, mapSubtract, mapSubtractToSelf, mapTan, mapTanh, mapTanhToSelf, mapTanToSelf, mapToSelf, mapUlp, mapUlpToSelf, outerProduct
-
Field Details
-
DEFAULT_ZERO_TOLERANCE
public static final double DEFAULT_ZERO_TOLERANCEDefault Tolerance for having a value considered zero.- See Also:
-
-
Constructor Details
-
OpenMapRealVector
public OpenMapRealVector()Build a 0-length vector.Zero-length vectors may be used to initialized construction of vectors by data gathering. We start with zero-length and use either the
OpenMapRealVector(OpenMapRealVector, int)
constructor or one of theappend
method (append(double)
,append(double[])
,append(RealVector)
) to gather data into this vector. -
OpenMapRealVector
public OpenMapRealVector(int dimension) Construct a (dimension)-length vector of zeros.- Parameters:
dimension
- size of the vector
-
OpenMapRealVector
public OpenMapRealVector(int dimension, double epsilon) Construct a (dimension)-length vector of zeros, specifying zero tolerance.- Parameters:
dimension
- Size of the vectorepsilon
- The tolerance for having a value considered zero
-
OpenMapRealVector
public OpenMapRealVector(int dimension, int expectedSize) Build a vector with known the sparseness (for advanced use only).- Parameters:
dimension
- The size of the vectorexpectedSize
- The expected number of non-zero entries
-
OpenMapRealVector
public OpenMapRealVector(int dimension, int expectedSize, double epsilon) Build a vector with known the sparseness and zero tolerance setting (for advanced use only).- Parameters:
dimension
- The size of the vectorexpectedSize
- The expected number of non-zero entriesepsilon
- The tolerance for having a value considered zero
-
OpenMapRealVector
public OpenMapRealVector(double[] values) Create from a double array. Only non-zero entries will be stored- Parameters:
values
- The set of values to create from
-
OpenMapRealVector
public OpenMapRealVector(double[] values, double epsilon) Create from a double array, specifying zero tolerance. Only non-zero entries will be stored- Parameters:
values
- The set of values to create fromepsilon
- The tolerance for having a value considered zero
-
OpenMapRealVector
Create from a Double array. Only non-zero entries will be stored- Parameters:
values
- The set of values to create from
-
OpenMapRealVector
Create from a Double array. Only non-zero entries will be stored- Parameters:
values
- The set of values to create fromepsilon
- The tolerance for having a value considered zero
-
OpenMapRealVector
Copy constructor.- Parameters:
v
- The instance to copy from
-
OpenMapRealVector
Generic copy constructor.- Parameters:
v
- The instance to copy from
-
-
Method Details
-
add
Compute the sum of this vector andv
.- Specified by:
add
in interfaceRealVector
- Overrides:
add
in classAbstractRealVector
- Parameters:
v
- Vector to be added.- Returns:
this
+v
.- Throws:
IllegalArgumentException
-
add
Optimized method to add two OpenMapRealVectors. Copies the larger vector, iterates over the smaller.- Parameters:
v
- Vector to add with- Returns:
- The sum of
this
withv
- Throws:
IllegalArgumentException
- If the dimensions don't match
-
append
Optimized method to append a OpenMapRealVector.- Parameters:
v
- vector to append- Returns:
- The result of appending
v
to self
-
append
Construct a vector by appending a vector to this vector.- Specified by:
append
in interfaceRealVector
- Parameters:
v
- vector to append to this one.- Returns:
- a new vector
-
append
Construct a vector by appending a double to this vector.- Specified by:
append
in interfaceRealVector
- Parameters:
d
- double to append.- Returns:
- a new vector
-
append
Construct a vector by appending a double array to this vector.- Specified by:
append
in interfaceRealVector
- Parameters:
a
- double array to append.- Returns:
- a new vector
-
copy
Returns a (deep) copy of this vector.- Specified by:
copy
in interfaceRealVector
- Specified by:
copy
in classAbstractRealVector
- Returns:
- a vector copy.
- Since:
- 2.1
-
dotProduct
Optimized method to compute the dot product with an OpenMapRealVector. Iterates over the smaller of the two.- Parameters:
v
- The vector to compute the dot product with- Returns:
- The dot product of
this
andv
- Throws:
IllegalArgumentException
- If the dimensions don't match
-
dotProduct
Compute the dot product.- Specified by:
dotProduct
in interfaceRealVector
- Overrides:
dotProduct
in classAbstractRealVector
- Parameters:
v
- vector with which dot product should be computed- Returns:
- the scalar dot product between instance and v
- Throws:
IllegalArgumentException
-
ebeDivide
Element-by-element division.- Specified by:
ebeDivide
in interfaceRealVector
- Parameters:
v
- vector by which instance elements must be divided- Returns:
- a vector containing this[i] / v[i] for all i
- Throws:
IllegalArgumentException
-
ebeDivide
Element-by-element division.- Specified by:
ebeDivide
in interfaceRealVector
- Overrides:
ebeDivide
in classAbstractRealVector
- Parameters:
v
- vector by which instance elements must be divided- Returns:
- a vector containing this[i] / v[i] for all i
- Throws:
IllegalArgumentException
-
ebeMultiply
Element-by-element multiplication.- Specified by:
ebeMultiply
in interfaceRealVector
- Parameters:
v
- vector by which instance elements must be multiplied- Returns:
- a vector containing this[i] * v[i] for all i
- Throws:
IllegalArgumentException
-
ebeMultiply
Element-by-element multiplication.- Specified by:
ebeMultiply
in interfaceRealVector
- Overrides:
ebeMultiply
in classAbstractRealVector
- Parameters:
v
- vector by which instance elements must be multiplied- Returns:
- a vector containing this[i] * v[i] for all i
- Throws:
IllegalArgumentException
-
getSubVector
Get a subvector from consecutive elements.- Specified by:
getSubVector
in interfaceRealVector
- Parameters:
index
- index of first element.n
- number of elements to be retrieved.- Returns:
- a vector containing n elements.
- Throws:
MatrixIndexException
-
getData
public double[] getData()Returns vector entries as a double array.- Specified by:
getData
in interfaceRealVector
- Overrides:
getData
in classAbstractRealVector
- Returns:
- double array of entries
-
getDimension
public int getDimension()Returns the size of the vector.- Specified by:
getDimension
in interfaceRealVector
- Returns:
- size
-
getDistance
Optimized method to compute distance.- Parameters:
v
- The vector to compute distance to- Returns:
- The distance from
this
andv
- Throws:
IllegalArgumentException
- If the dimensions don't match
-
getDistance
Distance between two vectors.This method computes the distance consistent with the L2 norm, i.e. the square root of the sum of elements differences, or euclidian distance.
- Specified by:
getDistance
in interfaceRealVector
- Overrides:
getDistance
in classAbstractRealVector
- Parameters:
v
- vector to which distance is requested- Returns:
- distance between two vectors.
- Throws:
IllegalArgumentException
- See Also:
-
getDistance
Distance between two vectors.This method computes the distance consistent with the L2 norm, i.e. the square root of the sum of elements differences, or euclidian distance.
- Specified by:
getDistance
in interfaceRealVector
- Overrides:
getDistance
in classAbstractRealVector
- Parameters:
v
- vector to which distance is requested- Returns:
- distance between two vectors.
- Throws:
IllegalArgumentException
- See Also:
-
getEntry
Returns the entry in the specified index.- Specified by:
getEntry
in interfaceRealVector
- Parameters:
index
- Index location of entry to be fetched.- Returns:
- the vector entry at
index
. - Throws:
MatrixIndexException
- See Also:
-
getL1Distance
Distance between two vectors.This method computes the distance consistent with L1 norm, i.e. the sum of the absolute values of elements differences.
- Parameters:
v
- vector to which distance is requested- Returns:
- distance between two vectors.
-
getL1Distance
Distance between two vectors.This method computes the distance consistent with L1 norm, i.e. the sum of the absolute values of elements differences.
- Specified by:
getL1Distance
in interfaceRealVector
- Overrides:
getL1Distance
in classAbstractRealVector
- Parameters:
v
- vector to which distance is requested- Returns:
- distance between two vectors.
- Throws:
IllegalArgumentException
- See Also:
-
getL1Distance
Distance between two vectors.This method computes the distance consistent with L1 norm, i.e. the sum of the absolute values of elements differences.
- Specified by:
getL1Distance
in interfaceRealVector
- Overrides:
getL1Distance
in classAbstractRealVector
- Parameters:
v
- vector to which distance is requested- Returns:
- distance between two vectors.
- Throws:
IllegalArgumentException
- See Also:
-
getLInfDistance
Distance between two vectors.This method computes the distance consistent with L∞ norm, i.e. the max of the absolute values of elements differences.
- Specified by:
getLInfDistance
in interfaceRealVector
- Overrides:
getLInfDistance
in classAbstractRealVector
- Parameters:
v
- vector to which distance is requested- Returns:
- distance between two vectors.
- Throws:
IllegalArgumentException
- See Also:
-
getLInfDistance
Distance between two vectors.This method computes the distance consistent with L∞ norm, i.e. the max of the absolute values of elements differences.
- Specified by:
getLInfDistance
in interfaceRealVector
- Overrides:
getLInfDistance
in classAbstractRealVector
- Parameters:
v
- vector to which distance is requested- Returns:
- distance between two vectors.
- Throws:
IllegalArgumentException
- See Also:
-
isInfinite
public boolean isInfinite()Check whether any coordinate of this vector is infinite and none areNaN
.- Specified by:
isInfinite
in interfaceRealVector
- Returns:
true
if any coordinate of this vector is infinite and none areNaN
,false
otherwise.
-
isNaN
public boolean isNaN()Check whether any coordinate of this vector isNaN
.- Specified by:
isNaN
in interfaceRealVector
- Returns:
true
if any coordinate of this vector isNaN
,false
otherwise.
-
mapAdd
Add a value to each entry.- Specified by:
mapAdd
in interfaceRealVector
- Overrides:
mapAdd
in classAbstractRealVector
- Parameters:
d
- Value to be added to each entry.- Returns:
this
+d
.
-
mapAddToSelf
Add a value to each entry. The instance is changed in-place.- Specified by:
mapAddToSelf
in interfaceRealVector
- Overrides:
mapAddToSelf
in classAbstractRealVector
- Parameters:
d
- Value to be added to each entry.- Returns:
this
.
-
outerProduct
Compute the outer product.- Specified by:
outerProduct
in interfaceRealVector
- Overrides:
outerProduct
in classAbstractRealVector
- Parameters:
v
- vector with which outer product should be computed- Returns:
- the square matrix outer product between instance and v
- Throws:
IllegalArgumentException
-
projection
Find the orthogonal projection of this vector onto another vector.- Specified by:
projection
in interfaceRealVector
- Parameters:
v
- vector onto which instance must be projected- Returns:
- projection of the instance onto v
- Throws:
IllegalArgumentException
-
projection
Find the orthogonal projection of this vector onto another vector.- Specified by:
projection
in interfaceRealVector
- Overrides:
projection
in classAbstractRealVector
- Parameters:
v
- vector onto which instance must be projected- Returns:
- projection of the instance onto v
- Throws:
IllegalArgumentException
-
setEntry
Set a single element.- Specified by:
setEntry
in interfaceRealVector
- Parameters:
index
- element index.value
- new value for the element.- Throws:
MatrixIndexException
- See Also:
-
setSubVector
Set a set of consecutive elements.- Specified by:
setSubVector
in interfaceRealVector
- Overrides:
setSubVector
in classAbstractRealVector
- Parameters:
index
- index of first element to be set.v
- vector containing the values to set.- Throws:
MatrixIndexException
- See Also:
-
setSubVector
Set a set of consecutive elements.- Specified by:
setSubVector
in interfaceRealVector
- Overrides:
setSubVector
in classAbstractRealVector
- Parameters:
index
- index of first element to be set.v
- vector containing the values to set.- Throws:
MatrixIndexException
- See Also:
-
set
public void set(double value) Set all elements to a single value.- Specified by:
set
in interfaceRealVector
- Overrides:
set
in classAbstractRealVector
- Parameters:
value
- single value to set for all elements
-
subtract
Optimized method to subtract OpenMapRealVectors.- Parameters:
v
- The vector to subtract fromthis
- Returns:
- The difference of
this
andv
- Throws:
IllegalArgumentException
- If the dimensions don't match
-
subtract
Subtractv
from this vector.- Specified by:
subtract
in interfaceRealVector
- Overrides:
subtract
in classAbstractRealVector
- Parameters:
v
- Vector to be subtracted.- Returns:
this
-v
.- Throws:
IllegalArgumentException
-
subtract
Subtractv
from this vector.- Specified by:
subtract
in interfaceRealVector
- Overrides:
subtract
in classAbstractRealVector
- Parameters:
v
- Vector to be subtracted.- Returns:
this
-v
.- Throws:
IllegalArgumentException
-
unitVector
Creates a unit vector pointing in the direction of this vector.The instance is not changed by this method.
- Specified by:
unitVector
in interfaceRealVector
- Overrides:
unitVector
in classAbstractRealVector
- Returns:
- a unit vector pointing in direction of this vector
-
unitize
public void unitize()Converts this vector into a unit vector.The instance itself is changed by this method.
- Specified by:
unitize
in interfaceRealVector
- Overrides:
unitize
in classAbstractRealVector
-
toArray
public double[] toArray()Convert the vector to a double array.The array is independent from vector data, it's elements are copied.
- Specified by:
toArray
in interfaceRealVector
- Overrides:
toArray
in classAbstractRealVector
- Returns:
- array containing a copy of vector elements
-
hashCode
public int hashCode()Implementation Note: This works on exact values, and as a result it is possible for
a.subtract(b)
to be the zero vector, whilea.hashCode() != b.hashCode()
. -
equals
Implementation Note: This performs an exact comparison, and as a result it is possible for
a.subtract(b
} to be the zero vector, whilea.equals(b) == false
. -
getSparcity
Deprecated.as of 2.2 replaced by the correctly spelledgetSparsity()
- Returns:
- the percentage of none zero elements as a decimal percent.
-
getSparsity
public double getSparsity()- Returns:
- the percentage of none zero elements as a decimal percent.
- Since:
- 2.2
-
sparseIterator
Specialized implementations may choose to not iterate over all dimensions, either because those values are unset, or are equal to defaultValue(), or are small enough to be ignored for the purposes of iteration. No guarantees are made about order of iteration. In dense implementations, this method will often delegate toRealVector.iterator()
.- Specified by:
sparseIterator
in interfaceRealVector
- Overrides:
sparseIterator
in classAbstractRealVector
- Returns:
- a sparse iterator
-
getSparsity()