public abstract class RealVector extends Object
vector element indexing is 0-based -- e.g., getEntry(0)
returns the first element of the vector.
The code map
and mapToSelf
methods operate on vectors element-wise, i.e. they perform the same
operation (adding a scalar, applying a function ...) on each element in turn. The map
versions create a new
vector to hold the result and do not change the instance. The mapToSelf
version uses the instance itself to
store the results, so the instance is changed by this method. In all cases, the result vector is returned by the
methods, allowing the fluent API style, like this:
RealVector result = v.mapAddToSelf(3.4).mapToSelf(new Tan()).mapToSelf(new Power(2.3));
Modifier and Type | Class and Description |
---|---|
protected class |
RealVector.Entry
An entry in the vector.
|
Constructor and Description |
---|
RealVector() |
Modifier and Type | Method and Description |
---|---|
RealVector |
add(RealVector v)
Compute the sum of this vector and
v . |
void |
addToEntry(int index,
double increment)
Change an entry at the specified index.
|
abstract RealVector |
append(double d)
Construct a new vector by appending a double to this vector.
|
abstract RealVector |
append(RealVector v)
Construct a new vector by appending a vector to this vector.
|
protected void |
checkIndex(int index)
Check if an index is valid.
|
protected void |
checkIndices(int start,
int end)
Checks that the indices of a subvector are valid.
|
protected void |
checkVectorDimensions(int n)
Check if instance dimension is equal to some expected value.
|
protected void |
checkVectorDimensions(RealVector v)
Check if instance and specified vectors have the same dimension.
|
RealVector |
combine(double a,
double b,
RealVector y)
Returns a new vector representing
a * this + b * y , the linear
combination of this and y . |
RealVector |
combineToSelf(double a,
double b,
RealVector y)
Updates
this with the linear combination of this and y . |
abstract RealVector |
copy()
Returns a (deep) copy of this vector.
|
double |
cosine(RealVector v)
Computes the cosine of the angle between this vector and the
argument.
|
double |
dotProduct(RealVector v)
Compute the dot product of this vector with
v . |
boolean |
equals(Object other)
Test for the equality of two real vectors.
|
abstract int |
getDimension()
Returns the size of the vector.
|
double |
getDistance(RealVector v)
Distance between two vectors.
|
abstract double |
getEntry(int index)
Return the entry at the specified index.
|
double |
getL1Distance(RealVector v)
Distance between two vectors.
|
double |
getL1Norm()
Returns the L1 norm of the vector.
|
double |
getLInfDistance(RealVector v)
Distance between two vectors.
|
double |
getLInfNorm()
Returns the L∞ norm of the vector.
|
int |
getMaxIndex()
Get the index of the maximum entry.
|
double |
getMaxValue()
Get the value of the maximum entry.
|
int |
getMinIndex()
Get the index of the minimum entry.
|
double |
getMinValue()
Get the value of the minimum entry.
|
double |
getNorm()
Returns the L2 norm of the vector.
|
abstract RealVector |
getSubVector(int index,
int n)
Get a subvector from consecutive elements.
|
int |
hashCode()
.
|
abstract boolean |
isInfinite()
Check whether any coordinate of this vector is infinite and none are
NaN . |
abstract boolean |
isNaN()
Check whether any coordinate of this vector is
NaN . |
Iterator<RealVector.Entry> |
iterator()
Generic dense iterator.
|
RealVector |
map(UnivariateFunction function)
Acts as if implemented as:
return copy().mapToSelf(function);
Returns a new vector.
|
RealVector |
mapAdd(double d)
Add a value to each entry.
|
RealVector |
mapAddToSelf(double d)
Add a value to each entry.
|
RealVector |
mapDivide(double d)
Divide each entry by the argument.
|
RealVector |
mapDivideToSelf(double d)
Divide each entry by the argument.
|
RealVector |
mapMultiply(double d)
Multiply each entry by the argument.
|
RealVector |
mapMultiplyToSelf(double d)
Multiply each entry.
|
RealVector |
mapSubtract(double d)
Subtract a value from each entry.
|
RealVector |
mapSubtractToSelf(double d)
Subtract a value from each entry.
|
RealVector |
mapToSelf(UnivariateFunction function)
Acts as if it is implemented as:
Entry e = null;
for (Iterator<Entry> it = iterator(); it.hasNext(); e = it.next()) {
e.setValue(function.value(e.getValue()));
}
Entries of this vector are modified in-place by this method.
|
RealMatrix |
outerProduct(RealVector v)
Compute the outer product.
|
RealVector |
projection(RealVector v)
Find the orthogonal projection of this vector onto another vector.
|
void |
set(double value)
Set all elements to a single value.
|
abstract void |
setEntry(int index,
double value)
Set a single element.
|
abstract void |
setSubVector(int index,
RealVector v)
Set a sequence of consecutive elements.
|
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.
|
RealVector |
unitVector()
Creates a unit vector pointing in the direction of this vector.
|
static RealVector |
unmodifiableRealVector(RealVector v)
Returns an unmodifiable view of the specified vector.
|
double |
walkInDefaultOrder(RealVectorChangingVisitor visitor)
Visits (and possibly alters) all entries of this vector in default order
(increasing index).
|
double |
walkInDefaultOrder(RealVectorChangingVisitor visitor,
int start,
int end)
Visits (and possibly alters) some entries of this vector in default order
(increasing index).
|
double |
walkInDefaultOrder(RealVectorPreservingVisitor visitor)
Visits (but does not alter) all entries of this vector in default order
(increasing index).
|
double |
walkInDefaultOrder(RealVectorPreservingVisitor visitor,
int start,
int end)
Visits (but does not alter) some entries of this vector in default order
(increasing index).
|
double |
walkInOptimizedOrder(RealVectorChangingVisitor visitor)
Visits (and possibly alters) all entries of this vector in optimized
order.
|
double |
walkInOptimizedOrder(RealVectorChangingVisitor visitor,
int start,
int end)
Visits (and possibly change) some entries of this vector in optimized
order.
|
double |
walkInOptimizedOrder(RealVectorPreservingVisitor visitor)
Visits (but does not alter) all entries of this vector in optimized
order.
|
double |
walkInOptimizedOrder(RealVectorPreservingVisitor visitor,
int start,
int end)
Visits (but does not alter) some entries of this vector in optimized
order.
|
public abstract int getDimension()
public abstract double getEntry(int index)
index
- Index location of entry to be fetched.index
.OutOfRangeException
- if the index is not valid.setEntry(int, double)
public abstract void setEntry(int index, double value)
index
- element index.value
- new value for the element.OutOfRangeException
- if the index is not valid.getEntry(int)
public void addToEntry(int index, double increment)
index
- Index location of entry to be set.increment
- Value to add to the vector entry.OutOfRangeException
- if the index is not valid.public abstract RealVector append(RealVector v)
v
- vector to append to this one.public abstract RealVector append(double d)
d
- double to append.public abstract RealVector getSubVector(int index, int n)
index
- index of first element.n
- number of elements to be retrieved.OutOfRangeException
- if the index is not valid.NotPositiveException
- if the number of elements is not positive.public abstract void setSubVector(int index, RealVector v)
index
- index of first element to be set.v
- vector containing the values to set.OutOfRangeException
- if the index is not valid.public abstract boolean isNaN()
NaN
.true
if any coordinate of this vector is NaN
, false
otherwise.public abstract boolean isInfinite()
NaN
.true
if any coordinate of this vector is infinite and
none are NaN
, false
otherwise.protected void checkVectorDimensions(RealVector v)
v
- Vector to compare instance with.DimensionMismatchException
- if the vectors do not
have the same dimension.protected void checkVectorDimensions(int n)
n
- Expected dimension.DimensionMismatchException
- if the dimension is
inconsistent with the vector size.protected void checkIndex(int index)
index
- Index to check.OutOfRangeException
- if index
is not valid.protected void checkIndices(int start, int end)
start
- the index of the first entry of the subvectorend
- the index of the last entry of the subvector (inclusive)OutOfRangeException
- if start
of end
are not validNumberIsTooSmallException
- if end < start
public RealVector add(RealVector v)
v
.
Returns a new vector. Does not change instance data.v
- Vector to be added.this
+ v
.DimensionMismatchException
- if v
is not the same size as this
vector.public RealVector subtract(RealVector v)
v
from this vector.
Returns a new vector. Does not change instance data.v
- Vector to be subtracted.this
- v
.DimensionMismatchException
- if v
is not the same size as this
vector.public RealVector mapAdd(double d)
d
- Value to be added to each entry.this
+ d
.public RealVector mapAddToSelf(double d)
d
- Value to be added to each entry.this
.public abstract RealVector copy()
public double dotProduct(RealVector v)
v
.v
- Vector with which dot product should be computedv
.DimensionMismatchException
- if v
is not the same size as this
vector.public double cosine(RealVector v)
v
- Vector.v
.MathArithmeticException
- if this
or v
is the null
vectorDimensionMismatchException
- if the dimensions of this
and v
do not matchpublic double getDistance(RealVector v)
This method computes the distance consistent with the L2 norm, i.e. the square root of the sum of element differences, or Euclidean distance.
v
- Vector to which distance is requested.DimensionMismatchException
- if v
is not the same size as this
vector.getL1Distance(RealVector)
,
getLInfDistance(RealVector)
,
getNorm()
public double getNorm()
The L2 norm is the root of the sum of the squared elements.
getL1Norm()
,
getLInfNorm()
,
getDistance(RealVector)
public double getL1Norm()
The L1 norm is the sum of the absolute values of the elements.
getNorm()
,
getLInfNorm()
,
getL1Distance(RealVector)
public double getLInfNorm()
The L∞ norm is the max of the absolute values of the elements.
getNorm()
,
getL1Norm()
,
getLInfDistance(RealVector)
public double getL1Distance(RealVector v)
This method computes the distance consistent with L1 norm, i.e. the sum of the absolute values of the elements differences.
v
- Vector to which distance is requested.DimensionMismatchException
- if v
is not the same size as this
vector.public double getLInfDistance(RealVector v)
This method computes the distance consistent with L∞ norm, i.e. the max of the absolute values of element differences.
v
- Vector to which distance is requested.DimensionMismatchException
- if v
is not the same size as this
vector.getDistance(RealVector)
,
getL1Distance(RealVector)
,
getLInfNorm()
public int getMinIndex()
NaN
.public double getMinValue()
NaN
if all
entries are NaN
.public int getMaxIndex()
NaN
public double getMaxValue()
NaN
if all
entries are NaN
.public RealVector mapMultiply(double d)
d
- Multiplication factor.this
* d
.public RealVector mapMultiplyToSelf(double d)
d
- Multiplication factor.this
.public RealVector mapSubtract(double d)
d
- Value to be subtracted.this
- d
.public RealVector mapSubtractToSelf(double d)
d
- Value to be subtracted.this
.public RealVector mapDivide(double d)
d
- Value to divide by.this
/ d
.public RealVector mapDivideToSelf(double d)
d
- Value to divide by.this
.public RealMatrix outerProduct(RealVector v)
v
- Vector with which outer product should be computed.v
.public RealVector projection(RealVector v)
v
- vector onto which instance must be projected.v
.DimensionMismatchException
- if v
is not the same size as this
vector.MathArithmeticException
- if this
or v
is the null
vectorpublic void set(double value)
value
- Single value to set for all elements.public double[] toArray()
double
s.
The array is independent from this vector data: the elements
are copied.public RealVector unitVector()
MathArithmeticException
- if the norm is zero.public void unitize()
MathArithmeticException
- if the norm is zero.public Iterator<RealVector.Entry> iterator()
Note: derived classes are required to return an Iterator
that returns non-null RealVector.Entry
objects as
long as Iterator.hasNext()
returns true
.
public RealVector map(UnivariateFunction function)
return copy().mapToSelf(function);Returns a new vector. Does not change instance data.
function
- Function to apply to each entry.public RealVector mapToSelf(UnivariateFunction function)
Entry e = null; for (Iterator<Entry> it = iterator(); it.hasNext(); e = it.next()) { e.setValue(function.value(e.getValue())); }Entries of this vector are modified in-place by this method.
function
- Function to apply to each entry.public RealVector combine(double a, double b, RealVector y)
a * this + b * y
, the linear
combination of this
and y
.
Returns a new vector. Does not change instance data.a
- Coefficient of this
.b
- Coefficient of y
.y
- Vector with which this
is linearly combined.a * this[i] + b * y[i]
for all i
.DimensionMismatchException
- if y
is not the same size as this
vector.public RealVector combineToSelf(double a, double b, RealVector y)
this
with the linear combination of this
and y
.a
- Weight of this
.b
- Weight of y
.y
- Vector with which this
is linearly combined.this
, with components equal to a * this[i] + b * y[i]
for all i
.DimensionMismatchException
- if y
is not the same size as this
vector.public double walkInDefaultOrder(RealVectorPreservingVisitor visitor)
visitor
- the visitor to be used to process the entries of this
vectorRealVectorPreservingVisitor.end()
at the end of the walkpublic double walkInDefaultOrder(RealVectorPreservingVisitor visitor, int start, int end)
visitor
- visitor to be used to process the entries of this vectorstart
- the index of the first entry to be visitedend
- the index of the last entry to be visited (inclusive)RealVectorPreservingVisitor.end()
at the end of the walkNumberIsTooSmallException
- if end < start
.OutOfRangeException
- if the indices are not valid.public double walkInOptimizedOrder(RealVectorPreservingVisitor visitor)
visitor
- the visitor to be used to process the entries of this
vectorRealVectorPreservingVisitor.end()
at the end of the walkpublic double walkInOptimizedOrder(RealVectorPreservingVisitor visitor, int start, int end)
visitor
- visitor to be used to process the entries of this vectorstart
- the index of the first entry to be visitedend
- the index of the last entry to be visited (inclusive)RealVectorPreservingVisitor.end()
at the end of the walkNumberIsTooSmallException
- if end < start
.OutOfRangeException
- if the indices are not valid.public double walkInDefaultOrder(RealVectorChangingVisitor visitor)
visitor
- the visitor to be used to process and modify the entries
of this vectorRealVectorChangingVisitor.end()
at the end of the walkpublic double walkInDefaultOrder(RealVectorChangingVisitor visitor, int start, int end)
visitor
- visitor to be used to process the entries of this vectorstart
- the index of the first entry to be visitedend
- the index of the last entry to be visited (inclusive)RealVectorChangingVisitor.end()
at the end of the walkNumberIsTooSmallException
- if end < start
.OutOfRangeException
- if the indices are not valid.public double walkInOptimizedOrder(RealVectorChangingVisitor visitor)
visitor
- the visitor to be used to process the entries of this
vectorRealVectorChangingVisitor.end()
at the end of the walkpublic double walkInOptimizedOrder(RealVectorChangingVisitor visitor, int start, int end)
visitor
- visitor to be used to process the entries of this vectorstart
- the index of the first entry to be visitedend
- the index of the last entry to be visited (inclusive)RealVectorChangingVisitor.end()
at the end of the walkNumberIsTooSmallException
- if end < start
.OutOfRangeException
- if the indices are not valid.public boolean equals(Object other)
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).
equals
in class Object
other
- 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.MathUnsupportedOperationException
- if this method is not
overridden.public int hashCode()
RealVector
(current implementation throws an
exception).hashCode
in class Object
MathUnsupportedOperationException
- if this method is not
overridden.public static RealVector unmodifiableRealVector(RealVector v)
MathUnsupportedOperationException
. However, the
returned vector is not immutable, since any modification of v
will also change the returned
view.
For example, in the following piece of code
RealVector v = new ArrayRealVector(2); RealVector w = RealVector.unmodifiableRealVector(v); v.setEntry(0, 1.2); v.setEntry(1, -3.4);the changes will be seen in the
w
view of v
.v
- Vector for which an unmodifiable view is to be returned.v
.Copyright © 2019 CNES. All rights reserved.