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 doubleDefault 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.doubleOptimized method to compute the dot product with an OpenMapRealVector.doubleCompute 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.booleanImplementation 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.intReturns the size of the vector.doublegetDistance(double[] v) Distance between two vectors.doubleOptimized method to compute distance.doubleDistance between two vectors.doublegetEntry(int index) Returns the entry in the specified index.doublegetL1Distance(double[] v) Distance between two vectors.doubleDistance between two vectors.doubleDistance between two vectors.doublegetLInfDistance(double[] v) Distance between two vectors.doubleDistance between two vectors.doubleDeprecated.doublegetSubVector(int index, int n) Get a subvector from consecutive elements.inthashCode()booleanCheck whether any coordinate of this vector is infinite and none areNaN.booleanisNaN()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.voidset(double value) Set all elements to a single value.voidsetEntry(int index, double value) Set a single element.voidsetSubVector(int index, double[] v) Set a set of consecutive elements.voidsetSubVector(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) Subtractvfrom this vector.Optimized method to subtract OpenMapRealVectors.Subtractvfrom this vector.double[]toArray()Convert the vector to a double array.voidunitize()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, outerProductMethods inherited from class java.lang.Object
getClass, notify, notifyAll, toString, wait, wait, waitMethods 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 theappendmethod (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:
addin interfaceRealVector- Overrides:
addin 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
thiswithv - 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
vto self
-
append
Construct a vector by appending a vector to this vector.- Specified by:
appendin 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:
appendin interfaceRealVector- Parameters:
d- double to append.- Returns:
- a new vector
-
append
Construct a vector by appending a double array to this vector.- Specified by:
appendin interfaceRealVector- Parameters:
a- double array to append.- Returns:
- a new vector
-
copy
Returns a (deep) copy of this vector.- Specified by:
copyin interfaceRealVector- Specified by:
copyin 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
thisandv - Throws:
IllegalArgumentException- If the dimensions don't match
-
dotProduct
Compute the dot product.- Specified by:
dotProductin interfaceRealVector- Overrides:
dotProductin 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:
ebeDividein 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:
ebeDividein interfaceRealVector- Overrides:
ebeDividein 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:
ebeMultiplyin 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:
ebeMultiplyin interfaceRealVector- Overrides:
ebeMultiplyin 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:
getSubVectorin 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:
getDatain interfaceRealVector- Overrides:
getDatain classAbstractRealVector- Returns:
- double array of entries
-
getDimension
public int getDimension()Returns the size of the vector.- Specified by:
getDimensionin interfaceRealVector- Returns:
- size
-
getDistance
Optimized method to compute distance.- Parameters:
v- The vector to compute distance to- Returns:
- The distance from
thisandv - 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:
getDistancein interfaceRealVector- Overrides:
getDistancein 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:
getDistancein interfaceRealVector- Overrides:
getDistancein 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:
getEntryin 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:
getL1Distancein interfaceRealVector- Overrides:
getL1Distancein 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:
getL1Distancein interfaceRealVector- Overrides:
getL1Distancein 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:
getLInfDistancein interfaceRealVector- Overrides:
getLInfDistancein 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:
getLInfDistancein interfaceRealVector- Overrides:
getLInfDistancein 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:
isInfinitein interfaceRealVector- Returns:
trueif any coordinate of this vector is infinite and none areNaN,falseotherwise.
-
isNaN
public boolean isNaN()Check whether any coordinate of this vector isNaN.- Specified by:
isNaNin interfaceRealVector- Returns:
trueif any coordinate of this vector isNaN,falseotherwise.
-
mapAdd
Add a value to each entry.- Specified by:
mapAddin interfaceRealVector- Overrides:
mapAddin 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:
mapAddToSelfin interfaceRealVector- Overrides:
mapAddToSelfin classAbstractRealVector- Parameters:
d- Value to be added to each entry.- Returns:
this.
-
outerProduct
Compute the outer product.- Specified by:
outerProductin interfaceRealVector- Overrides:
outerProductin 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:
projectionin 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:
projectionin interfaceRealVector- Overrides:
projectionin 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:
setEntryin interfaceRealVector- Parameters:
index- element index.value- new value for the element.- Throws:
MatrixIndexException- See Also:
-
setSubVector
Set a set of consecutive elements.- Specified by:
setSubVectorin interfaceRealVector- Overrides:
setSubVectorin 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:
setSubVectorin interfaceRealVector- Overrides:
setSubVectorin 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:
setin interfaceRealVector- Overrides:
setin 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
thisandv - Throws:
IllegalArgumentException- If the dimensions don't match
-
subtract
Subtractvfrom this vector.- Specified by:
subtractin interfaceRealVector- Overrides:
subtractin classAbstractRealVector- Parameters:
v- Vector to be subtracted.- Returns:
this-v.- Throws:
IllegalArgumentException
-
subtract
Subtractvfrom this vector.- Specified by:
subtractin interfaceRealVector- Overrides:
subtractin 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:
unitVectorin interfaceRealVector- Overrides:
unitVectorin 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:
unitizein interfaceRealVector- Overrides:
unitizein 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:
toArrayin interfaceRealVector- Overrides:
toArrayin 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:
sparseIteratorin interfaceRealVector- Overrides:
sparseIteratorin classAbstractRealVector- Returns:
- a sparse iterator
-
getSparsity()