Class OpenMapRealVector
- java.lang.Object
-
- org.apache.commons.math4.legacy.linear.RealVector
-
- org.apache.commons.math4.legacy.linear.SparseRealVector
-
- org.apache.commons.math4.legacy.linear.OpenMapRealVector
-
- All Implemented Interfaces:
Serializable
public class OpenMapRealVector extends SparseRealVector implements Serializable
This class implements theRealVectorinterface with aOpenIntToDoubleHashMapbacking store.Caveat: This implementation assumes that, for any
x, the equalityx * 0d == 0dholds. But it is is not true forNaN. Moreover, zero entries will lose their sign. Some operations (that involveNaNand/or infinities) may thus give incorrect results, like multiplications, divisions or functions mapping.- Since:
- 2.0
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected classOpenMapRealVector.OpenMapEntryImplementation ofEntryoptimized for OpenMap.protected classOpenMapRealVector.OpenMapSparseIteratorIterator class to do iteration over just the non-zero elements.-
Nested classes/interfaces inherited from class org.apache.commons.math4.legacy.linear.RealVector
RealVector.Entry, RealVector.SparseEntryIterator
-
-
Field Summary
Fields Modifier and Type Field Description static doubleDEFAULT_ZERO_TOLERANCEDefault Tolerance for having a value considered zero.
-
Constructor Summary
Constructors Modifier Constructor Description OpenMapRealVector()Build a 0-length vector.OpenMapRealVector(double[] values)Create from an array.OpenMapRealVector(double[] values, double epsilon)Create from an array, specifying zero tolerance.OpenMapRealVector(int dimension)Construct a vector of zeroes.OpenMapRealVector(int dimension, double epsilon)Construct a vector of zeroes, 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 an array.OpenMapRealVector(Double[] values, double epsilon)Create from an array.OpenMapRealVector(OpenMapRealVector v)Copy constructor.protectedOpenMapRealVector(OpenMapRealVector v, int resize)Build a resized vector, for use with append.OpenMapRealVector(RealVector v)Generic copy constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description OpenMapRealVectoradd(OpenMapRealVector v)Optimized method to add two OpenMapRealVectors.RealVectoradd(RealVector v)Compute the sum of this vector andv.OpenMapRealVectorappend(double d)Construct a new vector by appending a double to this vector.OpenMapRealVectorappend(OpenMapRealVector v)Optimized method to append a OpenMapRealVector.OpenMapRealVectorappend(RealVector v)Construct a new vector by appending a vector to this vector.OpenMapRealVectorcopy()Returns a (deep) copy of this vector.OpenMapRealVectorebeDivide(RealVector v)Element-by-element division.OpenMapRealVectorebeMultiply(RealVector v)Element-by-element multiplication.booleanequals(Object obj)Test for the equality of two real vectors.intgetDimension()Returns the size of the vector.doublegetDistance(OpenMapRealVector v)Optimized method to compute distance.doublegetDistance(RealVector v)Distance between two vectors.doublegetEntry(int index)Return the entry at the specified index.doublegetL1Distance(OpenMapRealVector v)Distance between two vectors.doublegetL1Distance(RealVector v)Distance between two vectors.doublegetLInfDistance(RealVector v)Distance between two vectors.doublegetSparsity()OpenMapRealVectorgetSubVector(int index, int n)Get a subvector from consecutive elements.inthashCode().protected booleanisDefaultValue(double value)Determine if this value is within epsilon of zero.booleanisInfinite()Check whether any coordinate of this vector is infinite and none areNaN.booleanisNaN()Check whether any coordinate of this vector isNaN.OpenMapRealVectormapAdd(double d)Add a value to each entry.OpenMapRealVectormapAddToSelf(double d)Add a value to each entry.voidset(double value)Set all elements to a single value.voidsetEntry(int index, double value)Set a single element.voidsetSubVector(int index, RealVector v)Set a sequence of consecutive elements.Iterator<RealVector.Entry>sparseIterator()Create a sparse iterator over the vector, which may omit some entries.OpenMapRealVectorsubtract(OpenMapRealVector v)Optimized method to subtract OpenMapRealVectors.RealVectorsubtract(RealVector v)Subtractvfrom this vector.double[]toArray()Convert the vector to an array ofdoubles.voidunitize()Converts this vector into a unit vector.OpenMapRealVectorunitVector()Creates a unit vector pointing in the direction of this vector.-
Methods inherited from class org.apache.commons.math4.legacy.linear.RealVector
addToEntry, checkIndex, checkIndices, checkVectorDimensions, checkVectorDimensions, combine, combineToSelf, cosine, dotProduct, getL1Norm, getLInfNorm, getMaxIndex, getMaxValue, getMinIndex, getMinValue, getNorm, iterator, map, mapDivide, mapDivideToSelf, mapMultiply, mapMultiplyToSelf, mapSubtract, mapSubtractToSelf, mapToSelf, outerProduct, projection, unmodifiableRealVector, walkInDefaultOrder, walkInDefaultOrder, walkInDefaultOrder, walkInDefaultOrder, walkInOptimizedOrder, walkInOptimizedOrder, walkInOptimizedOrder, walkInOptimizedOrder
-
-
-
-
Field Detail
-
DEFAULT_ZERO_TOLERANCE
public static final double DEFAULT_ZERO_TOLERANCE
Default Tolerance for having a value considered zero.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
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 theOpenMapRealVector(OpenMapRealVector, int)constructor or one of theappendmethod (append(double),append(RealVector)) to gather data into this vector.
-
OpenMapRealVector
public OpenMapRealVector(int dimension)
Construct a vector of zeroes.- Parameters:
dimension- Size of the vector.
-
OpenMapRealVector
public OpenMapRealVector(int dimension, double epsilon)
Construct a vector of zeroes, specifying zero tolerance.- Parameters:
dimension- Size of the vector.epsilon- Tolerance below which a value considered zero.
-
OpenMapRealVector
protected OpenMapRealVector(OpenMapRealVector v, int resize)
Build a resized vector, for use with append.- Parameters:
v- Original vector.resize- Amount to add.
-
OpenMapRealVector
public OpenMapRealVector(int dimension, int expectedSize)
Build a vector with known the sparseness (for advanced use only).- Parameters:
dimension- Size of the vector.expectedSize- 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- Size of the vector.expectedSize- Expected number of non-zero entries.epsilon- Tolerance below which a value is considered zero.
-
OpenMapRealVector
public OpenMapRealVector(double[] values)
Create from an array. Only non-zero entries will be stored.- Parameters:
values- Set of values to create from.
-
OpenMapRealVector
public OpenMapRealVector(double[] values, double epsilon)
Create from an array, specifying zero tolerance. Only non-zero entries will be stored.- Parameters:
values- Set of values to create from.epsilon- Tolerance below which a value is considered zero.
-
OpenMapRealVector
public OpenMapRealVector(Double[] values)
Create from an 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 an array. Only non-zero entries will be stored.- Parameters:
values- Set of values to create from.epsilon- Tolerance below which a value is considered zero.
-
OpenMapRealVector
public OpenMapRealVector(OpenMapRealVector v)
Copy constructor.- Parameters:
v- Instance to copy from.
-
OpenMapRealVector
public OpenMapRealVector(RealVector v)
Generic copy constructor.- Parameters:
v- Instance to copy from.
-
-
Method Detail
-
isDefaultValue
protected boolean isDefaultValue(double value)
Determine if this value is within epsilon of zero.- Parameters:
value- Value to test- Returns:
trueif this value is within epsilon to zero,falseotherwise.- Since:
- 2.1
-
add
public RealVector add(RealVector v) throws DimensionMismatchException
Compute the sum of this vector andv. Returns a new vector. Does not change instance data.- Overrides:
addin classRealVector- Parameters:
v- Vector to be added.- Returns:
this+v.- Throws:
DimensionMismatchException- ifvis not the same size asthisvector.
-
add
public OpenMapRealVector add(OpenMapRealVector v) throws DimensionMismatchException
Optimized method to add two OpenMapRealVectors. It copies the larger vector, then iterates over the smaller.- Parameters:
v- Vector to add.- Returns:
- the sum of
thisandv. - Throws:
DimensionMismatchException- if the dimensions do not match.
-
append
public OpenMapRealVector append(OpenMapRealVector v)
Optimized method to append a OpenMapRealVector.- Parameters:
v- vector to append- Returns:
- The result of appending
vto self
-
append
public OpenMapRealVector append(RealVector v)
Construct a new vector by appending a vector to this vector.- Specified by:
appendin classRealVector- Parameters:
v- vector to append to this one.- Returns:
- a new vector.
-
append
public OpenMapRealVector append(double d)
Construct a new vector by appending a double to this vector.- Specified by:
appendin classRealVector- Parameters:
d- double to append.- Returns:
- a new vector.
-
copy
public OpenMapRealVector copy()
Returns a (deep) copy of this vector.- Specified by:
copyin classRealVector- Returns:
- a vector copy.
- Since:
- 2.1
-
ebeDivide
public OpenMapRealVector ebeDivide(RealVector v) throws DimensionMismatchException
Element-by-element division.- Specified by:
ebeDividein classRealVector- Parameters:
v- Vector by which instance elements must be divided.- Returns:
- a vector containing this[i] / v[i] for all i.
- Throws:
DimensionMismatchException- ifvis not the same size asthisvector.
-
ebeMultiply
public OpenMapRealVector ebeMultiply(RealVector v) throws DimensionMismatchException
Element-by-element multiplication.- Specified by:
ebeMultiplyin classRealVector- Parameters:
v- Vector by which instance elements must be multiplied- Returns:
- a vector containing this[i] * v[i] for all i.
- Throws:
DimensionMismatchException- ifvis not the same size asthisvector.
-
getSubVector
public OpenMapRealVector getSubVector(int index, int n) throws NotPositiveException, OutOfRangeException
Get a subvector from consecutive elements.- Specified by:
getSubVectorin classRealVector- Parameters:
index- index of first element.n- number of elements to be retrieved.- Returns:
- a vector containing n elements.
- Throws:
NotPositiveException- if the number of elements is not positive.OutOfRangeException- if the index is not valid.
-
getDimension
public int getDimension()
Returns the size of the vector.- Specified by:
getDimensionin classRealVector- Returns:
- the size of this vector.
-
getDistance
public double getDistance(OpenMapRealVector v) throws DimensionMismatchException
Optimized method to compute distance.- Parameters:
v- Vector to compute distance to.- Returns:
- the distance from
thisandv. - Throws:
DimensionMismatchException- if the dimensions do not match.
-
getDistance
public double getDistance(RealVector v) throws DimensionMismatchException
Distance between two vectors.This method computes the distance consistent with the L2 norm, i.e. the square root of the sum of element differences, or Euclidean distance.
- Overrides:
getDistancein classRealVector- Parameters:
v- Vector to which distance is requested.- Returns:
- the distance between two vectors.
- Throws:
DimensionMismatchException- ifvis not the same size asthisvector.- See Also:
RealVector.getL1Distance(RealVector),RealVector.getLInfDistance(RealVector),RealVector.getNorm()
-
getEntry
public double getEntry(int index) throws OutOfRangeException
Return the entry at the specified index.- Specified by:
getEntryin classRealVector- Parameters:
index- Index location of entry to be fetched.- Returns:
- the vector entry at
index. - Throws:
OutOfRangeException- if the index is not valid.- See Also:
RealVector.setEntry(int, double)
-
getL1Distance
public double getL1Distance(OpenMapRealVector v) throws DimensionMismatchException
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 this vector and
v. - Throws:
DimensionMismatchException- if the dimensions do not match.
-
getL1Distance
public double getL1Distance(RealVector v) throws DimensionMismatchException
Distance between two vectors.This method computes the distance consistent with L1 norm, i.e. the sum of the absolute values of the elements differences.
- Overrides:
getL1Distancein classRealVector- Parameters:
v- Vector to which distance is requested.- Returns:
- the distance between two vectors.
- Throws:
DimensionMismatchException- ifvis not the same size asthisvector.
-
getLInfDistance
public double getLInfDistance(RealVector v) throws DimensionMismatchException
Distance between two vectors.This method computes the distance consistent with L∞ norm, i.e. the max of the absolute values of element differences.
- Overrides:
getLInfDistancein classRealVector- Parameters:
v- Vector to which distance is requested.- Returns:
- the distance between two vectors.
- Throws:
DimensionMismatchException- ifvis not the same size asthisvector.- See Also:
RealVector.getDistance(RealVector),RealVector.getL1Distance(RealVector),RealVector.getLInfNorm()
-
isInfinite
public boolean isInfinite()
Check whether any coordinate of this vector is infinite and none areNaN.- Specified by:
isInfinitein classRealVector- 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 classRealVector- Returns:
trueif any coordinate of this vector isNaN,falseotherwise.
-
mapAdd
public OpenMapRealVector mapAdd(double d)
Add a value to each entry. Returns a new vector. Does not change instance data.- Overrides:
mapAddin classRealVector- Parameters:
d- Value to be added to each entry.- Returns:
this+d.
-
mapAddToSelf
public OpenMapRealVector mapAddToSelf(double d)
Add a value to each entry. The instance is changed in-place.- Overrides:
mapAddToSelfin classRealVector- Parameters:
d- Value to be added to each entry.- Returns:
this.
-
setEntry
public void setEntry(int index, double value) throws OutOfRangeException
Set a single element.- Specified by:
setEntryin classRealVector- Parameters:
index- element index.value- new value for the element.- Throws:
OutOfRangeException- if the index is not valid.- See Also:
RealVector.getEntry(int)
-
setSubVector
public void setSubVector(int index, RealVector v) throws OutOfRangeException
Set a sequence of consecutive elements.- Specified by:
setSubVectorin classRealVector- Parameters:
index- index of first element to be set.v- vector containing the values to set.- Throws:
OutOfRangeException- if the index is not valid.
-
set
public void set(double value)
Set all elements to a single value.- Overrides:
setin classRealVector- Parameters:
value- Single value to set for all elements.
-
subtract
public OpenMapRealVector subtract(OpenMapRealVector v) throws DimensionMismatchException
Optimized method to subtract OpenMapRealVectors.- Parameters:
v- Vector to subtract fromthis.- Returns:
- the difference of
thisandv. - Throws:
DimensionMismatchException- if the dimensions do not match.
-
subtract
public RealVector subtract(RealVector v) throws DimensionMismatchException
Subtractvfrom this vector. Returns a new vector. Does not change instance data.- Overrides:
subtractin classRealVector- Parameters:
v- Vector to be subtracted.- Returns:
this-v.- Throws:
DimensionMismatchException- ifvis not the same size asthisvector.
-
unitVector
public OpenMapRealVector unitVector() throws MathArithmeticException
Creates a unit vector pointing in the direction of this vector. The instance is not changed by this method.- Overrides:
unitVectorin classRealVector- Returns:
- a unit vector pointing in direction of this vector.
- Throws:
MathArithmeticException- if the norm is zero.
-
unitize
public void unitize() throws MathArithmeticException
Converts this vector into a unit vector. The instance itself is changed by this method.- Overrides:
unitizein classRealVector- Throws:
MathArithmeticException- if the norm is zero.
-
toArray
public double[] toArray()
Convert the vector to an array ofdoubles. The array is independent from this vector data: the elements are copied.- Overrides:
toArrayin classRealVector- Returns:
- an array containing a copy of the vector elements.
-
hashCode
public int hashCode()
. This method must be overridden by concrete subclasses ofRealVector(current implementation throws an exception). Implementation Note: This works on exact values, and as a result it is possible fora.subtract(b)to be the zero vector, whilea.hashCode() != b.hashCode().- Overrides:
hashCodein classRealVector
-
equals
public boolean equals(Object obj)
Test for the equality of two real vectors. If all coordinates of two real vectors are exactly the same, and none are
NaN, the two real vectors are considered to be equal.NaNcoordinates are considered to affect globally the vector and be equals to each other - i.e, if either (or all) coordinates of the real vector are equal toNaN, the real vector is equal to a vector with allNaNcoordinates.This method must be overridden by concrete subclasses of
Implementation Note: This performs an exact comparison, and as a result it is possible forRealVector(the current implementation throws an exception).a.subtract(b} to be the zero vector, whilea.equals(b) == false.- Overrides:
equalsin classRealVector- Parameters:
obj- Object to test for equality.- Returns:
trueif two vector objects are equal,falseifotheris null, not an instance ofRealVector, or not equal to thisRealVectorinstance.
-
getSparsity
public double getSparsity()
- Returns:
- the percentage of none zero elements as a decimal percent.
- Since:
- 2.2
-
sparseIterator
public Iterator<RealVector.Entry> sparseIterator()
Create a sparse iterator over the vector, which may omit some entries. The ommitted entries are either exact zeroes (for dense implementations) or are the entries which are not stored (for real sparse vectors). No guarantees are made about order of iteration.Note: derived classes are required to return an
Iteratorthat returns non-nullRealVector.Entryobjects as long asIterator.hasNext()returnstrue.- Overrides:
sparseIteratorin classRealVector- Returns:
- a sparse iterator.
-
-