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.
|
protected class |
RealVector.SparseEntryIterator
This class should rarely be used, but is here to provide
a default implementation of sparseIterator(), which is implemented
by walking over the entries, skipping those whose values are the default one.
|
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 |
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 . |
abstract RealVector |
ebeDivide(RealVector v)
Element-by-element division.
|
abstract RealVector |
ebeMultiply(RealVector v)
Element-by-element multiplication.
|
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.
|
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:
|
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:
|
RealMatrix |
outerProduct(RealVector v)
Compute the outer product.
|
abstract 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.
|
Iterator<RealVector.Entry> |
sparseIterator()
Create a sparse iterator over the vector, which may omit some entries.
|
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.
|
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.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.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
.public abstract RealVector ebeDivide(RealVector v)
v
- Vector by which instance elements must be divided.DimensionMismatchException
- if v
is not the same size as this vector.public abstract RealVector ebeMultiply(RealVector v)
v
- Vector by which instance elements must be multipliedDimensionMismatchException
- if v
is not the same size as this vector.public 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 Euclidian 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 abstract RealVector projection(RealVector v)
v
- vector onto which instance must be projected.v
.DimensionMismatchException
- if v
is not the same size as this vector.public 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()
ArithmeticException
- if the norm is null
.public void unitize()
MathArithmeticException
- if the norm is zero.public Iterator<RealVector.Entry> sparseIterator()
iterator()
.public Iterator<RealVector.Entry> iterator()
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(IteratorEntries of this vector are modified in-place by this method.it = iterator(); it.hasNext(); e = it.next()) { e.setValue(function.value(e.getValue())); }
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 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 © 2003-2012 Apache Software Foundation. All Rights Reserved.