Class RealVector
- Direct Known Subclasses:
ArrayRealVector,SparseRealVector
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));
- Since:
- 2.1
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected classAn entry in the vector.protected classThis 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 that are zero. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionadd(RealVector v) Compute the sum of this vector andv.voidaddToEntry(int index, double increment) Change an entry at the specified index.abstract RealVectorappend(double d) Construct a new vector by appending a double to this vector.abstract RealVectorappend(RealVector v) Construct a new vector by appending a vector to this vector.protected voidcheckIndex(int index) Check if an index is valid.protected voidcheckIndices(int start, int end) Checks that the indices of a subvector are valid.protected voidcheckVectorDimensions(int n) Check if instance dimension is equal to some expected value.protected voidCheck if instance and specified vectors have the same dimension.combine(double a, double b, RealVector y) Returns a new vector representinga * this + b * y, the linear combination ofthisandy.combineToSelf(double a, double b, RealVector y) Updatesthiswith the linear combination ofthisandy.abstract RealVectorcopy()Returns a (deep) copy of this vector.doublecosine(RealVector v) Computes the cosine of the angle between this vector and the argument.doubleCompute the dot product of this vector withv.abstract RealVectorElement-by-element division.abstract RealVectorElement-by-element multiplication.booleanTest for the equality of two real vectors.abstract intReturns the size of the vector.doubleDistance between two vectors.abstract doublegetEntry(int index) Return the entry at the specified index.doubleDistance between two vectors.doubleReturns the L1 norm of the vector.doubleDistance between two vectors.doubleReturns the L∞ norm of the vector.intGet the index of the maximum entry.doubleGet the value of the maximum entry.intGet the index of the minimum entry.doubleGet the value of the minimum entry.doublegetNorm()Returns the L2 norm of the vector.abstract RealVectorgetSubVector(int index, int n) Get a subvector from consecutive elements.inthashCode().abstract booleanCheck whether any coordinate of this vector is infinite and none areNaN.abstract booleanisNaN()Check whether any coordinate of this vector isNaN.iterator()Generic dense iterator.map(UnivariateFunction function) Acts as if implemented as:mapAdd(double d) Add a value to each entry.mapAddToSelf(double d) Add a value to each entry.mapDivide(double d) Divide each entry by the argument.mapDivideToSelf(double d) Divide each entry by the argument.mapMultiply(double d) Multiply each entry by the argument.mapMultiplyToSelf(double d) Multiply each entry.mapSubtract(double d) Subtract a value from each entry.mapSubtractToSelf(double d) Subtract a value from each entry.mapToSelf(UnivariateFunction function) Acts as if it is implemented as:Compute the outer product.Find the orthogonal projection of this vector onto another vector.voidset(double value) Set all elements to a single value.abstract voidsetEntry(int index, double value) Set a single element.abstract voidsetSubVector(int index, RealVector v) Set a sequence of consecutive elements.Create a sparse iterator over the vector, which may omit some entries.Subtractvfrom this vector.double[]toArray()Convert the vector to an array ofdoubles.voidunitize()Converts this vector into a unit vector.Creates a unit vector pointing in the direction of this vector.static RealVectorReturns an unmodifiable view of the specified vector.doubleVisits (and possibly alters) all entries of this vector in default order (increasing index).doublewalkInDefaultOrder(RealVectorChangingVisitor visitor, int start, int end) Visits (and possibly alters) some entries of this vector in default order (increasing index).doubleVisits (but does not alter) all entries of this vector in default order (increasing index).doublewalkInDefaultOrder(RealVectorPreservingVisitor visitor, int start, int end) Visits (but does not alter) some entries of this vector in default order (increasing index).doubleVisits (and possibly alters) all entries of this vector in optimized order.doublewalkInOptimizedOrder(RealVectorChangingVisitor visitor, int start, int end) Visits (and possibly change) some entries of this vector in optimized order.doubleVisits (but does not alter) all entries of this vector in optimized order.doublewalkInOptimizedOrder(RealVectorPreservingVisitor visitor, int start, int end) Visits (but does not alter) some entries of this vector in optimized order.
-
Constructor Details
-
RealVector
public RealVector()
-
-
Method Details
-
getDimension
public abstract int getDimension()Returns the size of the vector.- Returns:
- the size of this vector.
-
getEntry
Return the entry at the specified index.- 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:
-
setEntry
Set a single element.- Parameters:
index- element index.value- new value for the element.- Throws:
OutOfRangeException- if the index is not valid.- See Also:
-
addToEntry
Change an entry at the specified index.- Parameters:
index- Index location of entry to be set.increment- Value to add to the vector entry.- Throws:
OutOfRangeException- if the index is not valid.- Since:
- 3.0
-
append
Construct a new vector by appending a vector to this vector.- Parameters:
v- vector to append to this one.- Returns:
- a new vector.
-
append
Construct a new vector by appending a double to this vector.- Parameters:
d- double to append.- Returns:
- a new vector.
-
getSubVector
public abstract RealVector getSubVector(int index, int n) throws NotPositiveException, OutOfRangeException Get a subvector from consecutive elements.- Parameters:
index- index of first element.n- number of elements to be retrieved.- Returns:
- a vector containing n elements.
- Throws:
OutOfRangeException- if the index is not valid.NotPositiveException- if the number of elements is not positive.
-
setSubVector
Set a sequence of consecutive elements.- Parameters:
index- index of first element to be set.v- vector containing the values to set.- Throws:
OutOfRangeException- if the index is not valid.
-
isNaN
public abstract boolean isNaN()Check whether any coordinate of this vector isNaN.- Returns:
trueif any coordinate of this vector isNaN,falseotherwise.
-
isInfinite
public abstract boolean isInfinite()Check whether any coordinate of this vector is infinite and none areNaN.- Returns:
trueif any coordinate of this vector is infinite and none areNaN,falseotherwise.
-
checkVectorDimensions
Check if instance and specified vectors have the same dimension.- Parameters:
v- Vector to compare instance with.- Throws:
DimensionMismatchException- if the vectors do not have the same dimension.
-
checkVectorDimensions
Check if instance dimension is equal to some expected value.- Parameters:
n- Expected dimension.- Throws:
DimensionMismatchException- if the dimension is inconsistent with the vector size.
-
checkIndex
Check if an index is valid.- Parameters:
index- Index to check.- Throws:
OutOfRangeException- ifindexis not valid.
-
checkIndices
protected void checkIndices(int start, int end) throws NumberIsTooSmallException, OutOfRangeException Checks that the indices of a subvector are valid.- Parameters:
start- the index of the first entry of the subvectorend- the index of the last entry of the subvector (inclusive)- Throws:
OutOfRangeException- ifstartofendare not validNumberIsTooSmallException- ifend < start- Since:
- 3.1
-
add
Compute the sum of this vector andv. Returns a new vector. Does not change instance data.- Parameters:
v- Vector to be added.- Returns:
this+v.- Throws:
DimensionMismatchException- ifvis not the same size asthisvector.
-
subtract
Subtractvfrom this vector. Returns a new vector. Does not change instance data.- Parameters:
v- Vector to be subtracted.- Returns:
this-v.- Throws:
DimensionMismatchException- ifvis not the same size asthisvector.
-
mapAdd
Add a value to each entry. Returns a new vector. Does not change instance data.- 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.- Parameters:
d- Value to be added to each entry.- Returns:
this.
-
copy
Returns a (deep) copy of this vector.- Returns:
- a vector copy.
-
dotProduct
Compute the dot product of this vector withv.- Parameters:
v- Vector with which dot product should be computed- Returns:
- the scalar dot product between this instance and
v. - Throws:
DimensionMismatchException- ifvis not the same size asthisvector.
-
cosine
Computes the cosine of the angle between this vector and the argument.- Parameters:
v- Vector.- Returns:
- the cosine of the angle between this vector and
v. - Throws:
MathArithmeticException- ifthisorvis the null vectorDimensionMismatchException- if the dimensions ofthisandvdo not match
-
ebeDivide
Element-by-element division.- 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
Element-by-element multiplication.- 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.
-
getDistance
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.
- Parameters:
v- Vector to which distance is requested.- Returns:
- the distance between two vectors.
- Throws:
DimensionMismatchException- ifvis not the same size asthisvector.- See Also:
-
getNorm
public double getNorm()Returns the L2 norm of the vector.The L2 norm is the root of the sum of the squared elements.
- Returns:
- the norm.
- See Also:
-
getL1Norm
public double getL1Norm()Returns the L1 norm of the vector.The L1 norm is the sum of the absolute values of the elements.
- Returns:
- the norm.
- See Also:
-
getLInfNorm
public double getLInfNorm()Returns the L∞ norm of the vector.The L∞ norm is the max of the absolute values of the elements.
- Returns:
- the norm.
- 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 the elements differences.
- Parameters:
v- Vector to which distance is requested.- Returns:
- the distance between two vectors.
- Throws:
DimensionMismatchException- ifvis not the same size asthisvector.
-
getLInfDistance
Distance between two vectors.This method computes the distance consistent with L∞ norm, i.e. the max of the absolute values of element differences.
- Parameters:
v- Vector to which distance is requested.- Returns:
- the distance between two vectors.
- Throws:
DimensionMismatchException- ifvis not the same size asthisvector.- See Also:
-
getMinIndex
public int getMinIndex()Get the index of the minimum entry.- Returns:
- the index of the minimum entry or -1 if vector length is 0
or all entries are
NaN.
-
getMinValue
public double getMinValue()Get the value of the minimum entry.- Returns:
- the value of the minimum entry or
NaNif all entries areNaN.
-
getMaxIndex
public int getMaxIndex()Get the index of the maximum entry.- Returns:
- the index of the maximum entry or -1 if vector length is 0
or all entries are
NaN
-
getMaxValue
public double getMaxValue()Get the value of the maximum entry.- Returns:
- the value of the maximum entry or
NaNif all entries areNaN.
-
mapMultiply
Multiply each entry by the argument. Returns a new vector. Does not change instance data.- Parameters:
d- Multiplication factor.- Returns:
this*d.
-
mapMultiplyToSelf
Multiply each entry. The instance is changed in-place.- Parameters:
d- Multiplication factor.- Returns:
this.
-
mapSubtract
Subtract a value from each entry. Returns a new vector. Does not change instance data.- Parameters:
d- Value to be subtracted.- Returns:
this-d.
-
mapSubtractToSelf
Subtract a value from each entry. The instance is changed in-place.- Parameters:
d- Value to be subtracted.- Returns:
this.
-
mapDivide
Divide each entry by the argument. Returns a new vector. Does not change instance data.- Parameters:
d- Value to divide by.- Returns:
this/d.
-
mapDivideToSelf
Divide each entry by the argument. The instance is changed in-place.- Parameters:
d- Value to divide by.- Returns:
this.
-
outerProduct
Compute the outer product.- Parameters:
v- Vector with which outer product should be computed.- Returns:
- the matrix outer product between this instance and
v.
-
projection
public RealVector projection(RealVector v) throws DimensionMismatchException, MathArithmeticException Find the orthogonal projection of this vector onto another vector.- Parameters:
v- vector onto which instance must be projected.- Returns:
- projection of the instance onto
v. - Throws:
DimensionMismatchException- ifvis not the same size asthisvector.MathArithmeticException- ifthisorvis the null vector
-
set
public void set(double value) Set all elements to a single value.- Parameters:
value- Single value to set for all elements.
-
toArray
public double[] toArray()Convert the vector to an array ofdoubles. The array is independent from this vector data: the elements are copied.- Returns:
- an array containing a copy of the vector elements.
-
unitVector
Creates a unit vector pointing in the direction of this vector. The instance is not changed by this method.- Returns:
- a unit vector pointing in direction of this vector.
- Throws:
MathArithmeticException- if the norm is zero.
-
unitize
Converts this vector into a unit vector. The instance itself is changed by this method.- Throws:
MathArithmeticException- if the norm is zero.
-
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.- Returns:
- a sparse iterator.
-
iterator
Generic dense iterator. Iteration is in increasing order of the vector index.Note: derived classes are required to return an
Iteratorthat returns non-nullRealVector.Entryobjects as long asIterator.hasNext()returnstrue.- Returns:
- a dense iterator.
-
map
Acts as if implemented as:return copy().mapToSelf(function);
Returns a new vector. Does not change instance data.- Parameters:
function- Function to apply to each entry.- Returns:
- a new vector.
-
mapToSelf
Acts as if it is implemented as:Entry e = null; for(Iterator
Entries of this vector are modified in-place by this method.it = iterator(); it.hasNext(); e = it.next()) { e.setValue(function.value(e.getValue())); } - Parameters:
function- Function to apply to each entry.- Returns:
- a reference to this vector.
-
combine
Returns a new vector representinga * this + b * y, the linear combination ofthisandy. Returns a new vector. Does not change instance data.- Parameters:
a- Coefficient ofthis.b- Coefficient ofy.y- Vector with whichthisis linearly combined.- Returns:
- a vector containing
a * this[i] + b * y[i]for alli. - Throws:
DimensionMismatchException- ifyis not the same size asthisvector.
-
combineToSelf
Updatesthiswith the linear combination ofthisandy.- Parameters:
a- Weight ofthis.b- Weight ofy.y- Vector with whichthisis linearly combined.- Returns:
this, with components equal toa * this[i] + b * y[i]for alli.- Throws:
DimensionMismatchException- ifyis not the same size asthisvector.
-
walkInDefaultOrder
Visits (but does not alter) all entries of this vector in default order (increasing index).- Parameters:
visitor- the visitor to be used to process the entries of this vector- Returns:
- the value returned by
RealVectorPreservingVisitor.end()at the end of the walk - Since:
- 3.1
-
walkInDefaultOrder
public double walkInDefaultOrder(RealVectorPreservingVisitor visitor, int start, int end) throws NumberIsTooSmallException, OutOfRangeException Visits (but does not alter) some entries of this vector in default order (increasing index).- Parameters:
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)- Returns:
- the value returned by
RealVectorPreservingVisitor.end()at the end of the walk - Throws:
NumberIsTooSmallException- ifend < start.OutOfRangeException- if the indices are not valid.- Since:
- 3.1
-
walkInOptimizedOrder
Visits (but does not alter) all entries of this vector in optimized order. The order in which the entries are visited is selected so as to lead to the most efficient implementation; it might depend on the concrete implementation of this abstract class.- Parameters:
visitor- the visitor to be used to process the entries of this vector- Returns:
- the value returned by
RealVectorPreservingVisitor.end()at the end of the walk - Since:
- 3.1
-
walkInOptimizedOrder
public double walkInOptimizedOrder(RealVectorPreservingVisitor visitor, int start, int end) throws NumberIsTooSmallException, OutOfRangeException Visits (but does not alter) some entries of this vector in optimized order. The order in which the entries are visited is selected so as to lead to the most efficient implementation; it might depend on the concrete implementation of this abstract class.- Parameters:
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)- Returns:
- the value returned by
RealVectorPreservingVisitor.end()at the end of the walk - Throws:
NumberIsTooSmallException- ifend < start.OutOfRangeException- if the indices are not valid.- Since:
- 3.1
-
walkInDefaultOrder
Visits (and possibly alters) all entries of this vector in default order (increasing index).- Parameters:
visitor- the visitor to be used to process and modify the entries of this vector- Returns:
- the value returned by
RealVectorChangingVisitor.end()at the end of the walk - Since:
- 3.1
-
walkInDefaultOrder
public double walkInDefaultOrder(RealVectorChangingVisitor visitor, int start, int end) throws NumberIsTooSmallException, OutOfRangeException Visits (and possibly alters) some entries of this vector in default order (increasing index).- Parameters:
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)- Returns:
- the value returned by
RealVectorChangingVisitor.end()at the end of the walk - Throws:
NumberIsTooSmallException- ifend < start.OutOfRangeException- if the indices are not valid.- Since:
- 3.1
-
walkInOptimizedOrder
Visits (and possibly alters) all entries of this vector in optimized order. The order in which the entries are visited is selected so as to lead to the most efficient implementation; it might depend on the concrete implementation of this abstract class.- Parameters:
visitor- the visitor to be used to process the entries of this vector- Returns:
- the value returned by
RealVectorChangingVisitor.end()at the end of the walk - Since:
- 3.1
-
walkInOptimizedOrder
public double walkInOptimizedOrder(RealVectorChangingVisitor visitor, int start, int end) throws NumberIsTooSmallException, OutOfRangeException Visits (and possibly change) some entries of this vector in optimized order. The order in which the entries are visited is selected so as to lead to the most efficient implementation; it might depend on the concrete implementation of this abstract class.- Parameters:
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)- Returns:
- the value returned by
RealVectorChangingVisitor.end()at the end of the walk - Throws:
NumberIsTooSmallException- ifend < start.OutOfRangeException- if the indices are not valid.- Since:
- 3.1
-
equals
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 overriden by concrete subclasses of
RealVector(the current implementation throws an exception).- Overrides:
equalsin classObject- Parameters:
other- Object to test for equality.- Returns:
trueif two vector objects are equal,falseifotheris null, not an instance ofRealVector, or not equal to thisRealVectorinstance.- Throws:
MathUnsupportedOperationException- if this method is not overridden.
-
hashCode
. This method must be overriden by concrete subclasses ofRealVector(current implementation throws an exception).- Overrides:
hashCodein classObject- Throws:
MathUnsupportedOperationException- if this method is not overridden.
-
unmodifiableRealVector
Returns an unmodifiable view of the specified vector. The returned vector has read-only access. An attempt to modify it will result in aMathUnsupportedOperationException. However, the returned vector is not immutable, since any modification ofvwill also change the returned view. For example, in the following piece of codeRealVector 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 thewview ofv.- Parameters:
v- Vector for which an unmodifiable view is to be returned.- Returns:
- an unmodifiable view of
v.
-