public class OpenMapRealVector extends SparseRealVector implements Serializable
RealVector
interface with a
OpenIntToDoubleHashMap
backing store.
Caveat: This implementation assumes that, for any x
,
the equality x * 0d == 0d
holds. But it is is not true for
NaN
. Moreover, zero entries will lose their sign.
Some operations (that involve NaN
and/or infinities) may
thus give incorrect results, like multiplications, divisions or
functions mapping.
Modifier and Type | Class and Description |
---|---|
protected class |
OpenMapRealVector.OpenMapEntry
Implementation of
Entry optimized for OpenMap. |
protected class |
OpenMapRealVector.OpenMapSparseIterator
Iterator class to do iteration over just the non-zero elements.
|
RealVector.Entry, RealVector.SparseEntryIterator
Modifier and Type | Field and Description |
---|---|
static double |
DEFAULT_ZERO_TOLERANCE
Default Tolerance for having a value considered zero.
|
Modifier | Constructor and Description |
---|---|
|
OpenMapRealVector()
Build a 0-length vector.
|
|
OpenMapRealVector(double[] values)
Create from an array.
|
|
OpenMapRealVector(Double[] values)
Create from an array.
|
|
OpenMapRealVector(double[] values,
double epsilon)
Create from an array, specifying zero tolerance.
|
|
OpenMapRealVector(Double[] values,
double epsilon)
Create from an array.
|
|
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(OpenMapRealVector v)
Copy constructor.
|
protected |
OpenMapRealVector(OpenMapRealVector v,
int resize)
Build a resized vector, for use with append.
|
|
OpenMapRealVector(RealVector v)
Generic copy constructor.
|
Modifier and Type | Method and Description |
---|---|
OpenMapRealVector |
add(OpenMapRealVector v)
Optimized method to add two OpenMapRealVectors.
|
RealVector |
add(RealVector v)
Compute the sum of this vector and
v . |
OpenMapRealVector |
append(double d)
Construct a new vector by appending a double to this vector.
|
OpenMapRealVector |
append(OpenMapRealVector v)
Optimized method to append a OpenMapRealVector.
|
OpenMapRealVector |
append(RealVector v)
Construct a new vector by appending a vector to this vector.
|
OpenMapRealVector |
copy()
Returns a (deep) copy of this vector.
|
double |
dotProduct(OpenMapRealVector v)
Deprecated.
as of 3.1 (to be removed in 4.0). The computation is
performed by the parent class. The method must be kept to maintain
backwards compatibility.
|
OpenMapRealVector |
ebeDivide(RealVector v)
Element-by-element division.
|
OpenMapRealVector |
ebeMultiply(RealVector v)
Element-by-element multiplication.
|
boolean |
equals(Object obj)
Test for the equality of two real vectors.
|
int |
getDimension()
Returns the size of the vector.
|
double |
getDistance(OpenMapRealVector v)
Optimized method to compute distance.
|
double |
getDistance(RealVector v)
Distance between two vectors.
|
double |
getEntry(int index)
Return the entry at the specified index.
|
double |
getL1Distance(OpenMapRealVector v)
Distance between two vectors.
|
double |
getL1Distance(RealVector v)
Distance between two vectors.
|
double |
getLInfDistance(RealVector v)
Distance between two vectors.
|
double |
getSparsity() |
OpenMapRealVector |
getSubVector(int index,
int n)
Get a subvector from consecutive elements.
|
int |
hashCode()
.
|
protected boolean |
isDefaultValue(double value)
Determine if this value is within epsilon of zero.
|
boolean |
isInfinite()
Check whether any coordinate of this vector is infinite and none are
NaN . |
boolean |
isNaN()
Check whether any coordinate of this vector is
NaN . |
OpenMapRealVector |
mapAdd(double d)
Add a value to each entry.
|
OpenMapRealVector |
mapAddToSelf(double d)
Add a value to each entry.
|
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,
RealVector v)
Set a sequence of consecutive elements.
|
Iterator<RealVector.Entry> |
sparseIterator()
Create a sparse iterator over the vector, which may omit some entries.
|
OpenMapRealVector |
subtract(OpenMapRealVector v)
Optimized method to subtract OpenMapRealVectors.
|
RealVector |
subtract(RealVector v)
Subtract
v from this vector. |
double[] |
toArray()
Convert the vector to an array of
double s. |
void |
unitize()
Converts this vector into a unit vector.
|
OpenMapRealVector |
unitVector()
Creates a unit vector pointing in the direction of this vector.
|
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
public static final double DEFAULT_ZERO_TOLERANCE
public OpenMapRealVector()
OpenMapRealVector(OpenMapRealVector, int)
constructor
or one of the append
method (append(double)
,
append(RealVector)
) to gather data into this vector.public OpenMapRealVector(int dimension)
dimension
- Size of the vector.public OpenMapRealVector(int dimension, double epsilon)
dimension
- Size of the vector.epsilon
- Tolerance below which a value considered zero.protected OpenMapRealVector(OpenMapRealVector v, int resize)
v
- Original vector.resize
- Amount to add.public OpenMapRealVector(int dimension, int expectedSize)
dimension
- Size of the vector.expectedSize
- The expected number of non-zero entries.public OpenMapRealVector(int dimension, int expectedSize, double epsilon)
dimension
- Size of the vector.expectedSize
- Expected number of non-zero entries.epsilon
- Tolerance below which a value is considered zero.public OpenMapRealVector(double[] values)
values
- Set of values to create from.public OpenMapRealVector(double[] values, double epsilon)
values
- Set of values to create from.epsilon
- Tolerance below which a value is considered zero.public OpenMapRealVector(Double[] values)
values
- The set of values to create frompublic OpenMapRealVector(Double[] values, double epsilon)
values
- Set of values to create from.epsilon
- Tolerance below which a value is considered zero.public OpenMapRealVector(OpenMapRealVector v)
v
- Instance to copy from.public OpenMapRealVector(RealVector v)
v
- Instance to copy from.protected boolean isDefaultValue(double value)
value
- Value to testtrue
if this value is within epsilon to zero,
false
otherwise.public RealVector add(RealVector v) throws DimensionMismatchException
v
.
Returns a new vector. Does not change instance data.add
in class RealVector
v
- Vector to be added.this
+ v
.DimensionMismatchException
- if v
is not the same size as
this
vector.public OpenMapRealVector add(OpenMapRealVector v) throws DimensionMismatchException
v
- Vector to add.this
and v
.DimensionMismatchException
- if the dimensions do not match.public OpenMapRealVector append(OpenMapRealVector v)
v
- vector to appendv
to selfpublic OpenMapRealVector append(RealVector v)
append
in class RealVector
v
- vector to append to this one.public OpenMapRealVector append(double d)
append
in class RealVector
d
- double to append.public OpenMapRealVector copy()
copy
in class RealVector
@Deprecated public double dotProduct(OpenMapRealVector v) throws DimensionMismatchException
v
- Vector.v
.DimensionMismatchException
- if v
is not the same size as
this
vector.public OpenMapRealVector ebeDivide(RealVector v) throws DimensionMismatchException
ebeDivide
in class RealVector
v
- Vector by which instance elements must be divided.DimensionMismatchException
- if v
is not the same size as
this
vector.public OpenMapRealVector ebeMultiply(RealVector v) throws DimensionMismatchException
ebeMultiply
in class RealVector
v
- Vector by which instance elements must be multipliedDimensionMismatchException
- if v
is not the same size as
this
vector.public OpenMapRealVector getSubVector(int index, int n) throws NotPositiveException, OutOfRangeException
getSubVector
in class RealVector
index
- index of first element.n
- number of elements to be retrieved.NotPositiveException
- if the number of elements is not positive.OutOfRangeException
- if the index is not valid.public int getDimension()
getDimension
in class RealVector
public double getDistance(OpenMapRealVector v) throws DimensionMismatchException
v
- Vector to compute distance to.this
and v
.DimensionMismatchException
- if the dimensions do not match.public double getDistance(RealVector v) throws DimensionMismatchException
This method computes the distance consistent with the L2 norm, i.e. the square root of the sum of element differences, or Euclidean distance.
getDistance
in class RealVector
v
- Vector to which distance is requested.DimensionMismatchException
- if v
is not the same size as
this
vector.RealVector.getL1Distance(RealVector)
,
RealVector.getLInfDistance(RealVector)
,
RealVector.getNorm()
public double getEntry(int index) throws OutOfRangeException
getEntry
in class RealVector
index
- Index location of entry to be fetched.index
.OutOfRangeException
- if the index is not valid.RealVector.setEntry(int, double)
public double getL1Distance(OpenMapRealVector v) throws DimensionMismatchException
v
- Vector to which distance is requested.v
.DimensionMismatchException
- if the dimensions do not match.public double getL1Distance(RealVector v) throws DimensionMismatchException
This method computes the distance consistent with L1 norm, i.e. the sum of the absolute values of the elements differences.
getL1Distance
in class RealVector
v
- Vector to which distance is requested.DimensionMismatchException
- if v
is not the same size as
this
vector.public double getLInfDistance(RealVector v) throws DimensionMismatchException
This method computes the distance consistent with L∞ norm, i.e. the max of the absolute values of element differences.
getLInfDistance
in class RealVector
v
- Vector to which distance is requested.DimensionMismatchException
- if v
is not the same size as
this
vector.RealVector.getDistance(RealVector)
,
RealVector.getL1Distance(RealVector)
,
RealVector.getLInfNorm()
public boolean isInfinite()
NaN
.isInfinite
in class RealVector
true
if any coordinate of this vector is infinite and
none are NaN
, false
otherwise.public boolean isNaN()
NaN
.isNaN
in class RealVector
true
if any coordinate of this vector is NaN
,
false
otherwise.public OpenMapRealVector mapAdd(double d)
mapAdd
in class RealVector
d
- Value to be added to each entry.this
+ d
.public OpenMapRealVector mapAddToSelf(double d)
mapAddToSelf
in class RealVector
d
- Value to be added to each entry.this
.public void setEntry(int index, double value) throws OutOfRangeException
setEntry
in class RealVector
index
- element index.value
- new value for the element.OutOfRangeException
- if the index is not valid.RealVector.getEntry(int)
public void setSubVector(int index, RealVector v) throws OutOfRangeException
setSubVector
in class RealVector
index
- index of first element to be set.v
- vector containing the values to set.OutOfRangeException
- if the index is not valid.public void set(double value)
set
in class RealVector
value
- Single value to set for all elements.public OpenMapRealVector subtract(OpenMapRealVector v) throws DimensionMismatchException
v
- Vector to subtract from this
.this
and v
.DimensionMismatchException
- if the dimensions do not match.public RealVector subtract(RealVector v) throws DimensionMismatchException
v
from this vector.
Returns a new vector. Does not change instance data.subtract
in class RealVector
v
- Vector to be subtracted.this
- v
.DimensionMismatchException
- if v
is not the same size as
this
vector.public OpenMapRealVector unitVector() throws MathArithmeticException
unitVector
in class RealVector
MathArithmeticException
- if the norm is zero.public void unitize() throws MathArithmeticException
unitize
in class RealVector
MathArithmeticException
- if the norm is zero.public double[] toArray()
double
s.
The array is independent from this vector data: the elements
are copied.toArray
in class RealVector
public int hashCode()
RealVector
(current implementation throws an
exception).
Implementation Note: This works on exact values, and as a result
it is possible for a.subtract(b)
to be the zero vector, while
a.hashCode() != b.hashCode()
.hashCode
in class RealVector
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. NaN
coordinates 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 to
NaN
, the real vector is equal to a vector with all NaN
coordinates.
This method must be overriden by concrete subclasses of
RealVector
(the current implementation throws an exception).
a.subtract(b
} to be the zero vector, while
a.equals(b) == false
.equals
in class RealVector
obj
- Object to test for equality.true
if two vector objects are equal, false
if
other
is null, not an instance of RealVector
, or
not equal to this RealVector
instance.public double getSparsity()
public Iterator<RealVector.Entry> sparseIterator()
Note: derived classes are required to return an Iterator
that
returns non-null RealVector.Entry
objects as long as Iterator.hasNext()
returns true
.
sparseIterator
in class RealVector
Copyright © 2003–2016 The Apache Software Foundation. All rights reserved.