Top Description Inners Constructors Methods
org.apache.commons.math3.linear

public abstract Class RealVector

extends Object
Class Inheritance
Known Direct Subclasses
org.apache.commons.math3.linear.SparseRealVector, org.apache.commons.math3.linear.ArrayRealVector
Imports
java.util.Iterator, .NoSuchElementException, org.apache.commons.math3.exception.MathUnsupportedOperationException, .DimensionMismatchException, .NotPositiveException, .NumberIsTooSmallException, .OutOfRangeException, .MathArithmeticException, org.apache.commons.math3.analysis.FunctionUtils, .UnivariateFunction, org.apache.commons.math3.analysis.function.Add, .Multiply, .Divide, org.apache.commons.math3.exception.util.LocalizedFormats, org.apache.commons.math3.util.FastMath

Class defining a real-valued vector with basic algebraic operations.

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 and Inner Type Summary

Modifier and TypeClass 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 that are zero.

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public RealVector

Returns:

this + v.
add
(RealVector
Vector to be added.
v
)

Compute the sum of this vector and v.

public void
addToEntry(int
Index location of entry to be set.
index
,
double
Value to add to the vector entry.
increment
)

Change an entry at the specified index.

public abstract RealVector

Returns:

a new vector.
append
(RealVector
vector to append to this one.
v
)

Construct a new vector by appending a vector to this vector.

public abstract RealVector

Returns:

a new vector.
append
(double
double to append.
d
)

Construct a new vector by appending a double to this vector.

protected void
checkIndex(final int
Index to check.
index
)

Check if an index is valid.

protected void
checkIndices(final int
the index of the first entry of the subvector
start
,
final int
the index of the last entry of the subvector (inclusive)
end
)

Checks that the indices of a subvector are valid.

protected void
checkVectorDimensions(RealVector
Vector to compare instance with.
v
)

Check if instance and specified vectors have the same dimension.

protected void
checkVectorDimensions(int
Expected dimension.
n
)

Check if instance dimension is equal to some expected value.

public RealVector

Returns:

a vector containing a * this[i] + b * y[i] for all i.
combine
(double
Coefficient of this.
a
,
double
Coefficient of y.
b
,
RealVector
Vector with which this is linearly combined.
y
)

Returns a new vector representing a * this + b * y, the linear combination of this and y.

public RealVector

Returns:

this, with components equal to a * this[i] + b * y[i] for all i.
combineToSelf
(double
Weight of this.
a
,
double
Weight of y.
b
,
RealVector
Vector with which this is linearly combined.
y
)

Updates this with the linear combination of this and y.

public abstract RealVector

Returns:

a vector copy.
copy
()

Returns a (deep) copy of this vector.

public double

Returns:

the cosine of the angle between this vector and v.
cosine
(RealVector
Vector.
v
)

Computes the cosine of the angle between this vector and the argument.

public double

Returns:

the scalar dot product between this instance and v.
dotProduct
(RealVector
Vector with which dot product should be computed
v
)

Compute the dot product of this vector with v.

public abstract RealVector

Returns:

a vector containing this[i] / v[i] for all i.
ebeDivide
(RealVector
Vector by which instance elements must be divided.
v
)

Element-by-element division.

public abstract RealVector

Returns:

a vector containing this[i] * v[i] for all i.
ebeMultiply
(RealVector
Vector by which instance elements must be multiplied
v
)

Element-by-element multiplication.

public boolean

Returns:

true if two vector objects are equal, false if other is null, not an instance of RealVector, or not equal to this RealVector instance.
equals
(Object
Object to test for equality.
other
)

Overrides java.lang.Object.equals.

Test for the equality of two real vectors.

public abstract int

Returns:

the size of this vector.
getDimension
()

Returns the size of the vector.

public double

Returns:

the distance between two vectors.
getDistance
(RealVector
Vector to which distance is requested.
v
)

Distance between two vectors.

public abstract double

Returns:

the vector entry at index.
getEntry
(int
Index location of entry to be fetched.
index
)

Return the entry at the specified index.

public double

Returns:

the distance between two vectors.
getL1Distance
(RealVector
Vector to which distance is requested.
v
)

Distance between two vectors.

public double

Returns:

the norm.
getL1Norm
()

Returns the L1 norm of the vector.

public double

Returns:

the distance between two vectors.
getLInfDistance
(RealVector
Vector to which distance is requested.
v
)

Distance between two vectors.

public double

Returns:

the norm.
getLInfNorm
()

Returns the L∞ norm of the vector.

public int

Returns:

the index of the maximum entry or -1 if vector length is 0 or all entries are NaN
getMaxIndex
()

Get the index of the maximum entry.

public double

Returns:

the value of the maximum entry or NaN if all entries are NaN.
getMaxValue
()

Get the value of the maximum entry.

public int

Returns:

the index of the minimum entry or -1 if vector length is 0 or all entries are NaN.
getMinIndex
()

Get the index of the minimum entry.

public double

Returns:

the value of the minimum entry or NaN if all entries are NaN.
getMinValue
()

Get the value of the minimum entry.

public double

Returns:

the norm.
getNorm
()

Returns the L2 norm of the vector.

public abstract RealVector

Returns:

a vector containing n elements.
getSubVector
(int
index of first element.
index
,
int
number of elements to be retrieved.
n
)

Get a subvector from consecutive elements.

public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for this object.

public abstract boolean

Returns:

true if any coordinate of this vector is infinite and none are NaN, false otherwise.
isInfinite
()

Check whether any coordinate of this vector is infinite and none are NaN.

public abstract boolean

Returns:

true if any coordinate of this vector is NaN, false otherwise.
isNaN
()

Check whether any coordinate of this vector is NaN.

public Iterator<RealVector.Entry>

Returns:

a dense iterator.
iterator
()

Generic dense iterator.

public RealVector

Returns:

a new vector.
map
(UnivariateFunction
Function to apply to each entry.
function
)

Acts as if implemented as:

 return copy().mapToSelf(function);
public RealVector

Returns:

this + d.
mapAdd
(double
Value to be added to each entry.
d
)

Add a value to each entry.

public RealVector

Returns:

this.
mapAddToSelf
(double
Value to be added to each entry.
d
)

Add a value to each entry.

public RealVector

Returns:

this / d.
mapDivide
(double
Value to divide by.
d
)

Divide each entry by the argument.

public RealVector

Returns:

this.
mapDivideToSelf
(double
Value to divide by.
d
)

Divide each entry by the argument.

public RealVector

Returns:

this * d.
mapMultiply
(double
Multiplication factor.
d
)

Multiply each entry by the argument.

public RealVector

Returns:

this.
mapMultiplyToSelf
(double
Multiplication factor.
d
)

Multiply each entry.

public RealVector

Returns:

this - d.
mapSubtract
(double
Value to be subtracted.
d
)

Subtract a value from each entry.

public RealVector

Returns:

this.
mapSubtractToSelf
(double
Value to be subtracted.
d
)

Subtract a value from each entry.

public RealVector

Returns:

a reference to this vector.
mapToSelf
(UnivariateFunction
Function to apply to each entry.
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()));
 }
public RealMatrix

Returns:

the matrix outer product between this instance and v.
outerProduct
(RealVector
Vector with which outer product should be computed.
v
)

Compute the outer product.

public RealVector

Returns:

projection of the instance onto v.
projection
(final RealVector
vector onto which instance must be projected.
v
)

Find the orthogonal projection of this vector onto another vector.

public void
set(double
Single value to set for all elements.
value
)

Set all elements to a single value.

public abstract void
setEntry(int
element index.
index
,
double
new value for the element.
value
)

Set a single element.

public abstract void
setSubVector(int
index of first element to be set.
index
,
RealVector
vector containing the values to set.
v
)

Set a sequence of consecutive elements.

public Iterator<RealVector.Entry>

Returns:

a sparse iterator.
sparseIterator
()

Create a sparse iterator over the vector, which may omit some entries.

public RealVector

Returns:

this - v.
subtract
(RealVector
Vector to be subtracted.
v
)

Subtract v from this vector.

public double[]

Returns:

an array containing a copy of the vector elements.
toArray
()

Convert the vector to an array of doubles.

public void
unitize()

Converts this vector into a unit vector.

public RealVector

Returns:

a unit vector pointing in direction of this vector.
unitVector
()

Creates a unit vector pointing in the direction of this vector.

public static RealVector

Returns:

an unmodifiable view of v.
unmodifiableRealVector
(final RealVector
Vector for which an unmodifiable view is to be returned.
v
)

Returns an unmodifiable view of the specified vector.

public double

Returns:

the value returned by RealVectorPreservingVisitor#end() at the end of the walk
walkInDefaultOrder
(final RealVectorPreservingVisitor
the visitor to be used to process the entries of this vector
visitor
)

Visits (but does not alter) all entries of this vector in default order (increasing index).

public double

Returns:

the value returned by RealVectorPreservingVisitor#end() at the end of the walk
walkInDefaultOrder
(final RealVectorPreservingVisitor
visitor to be used to process the entries of this vector
visitor
,
final int
the index of the first entry to be visited
start
,
final int
the index of the last entry to be visited (inclusive)
end
)

Visits (but does not alter) some entries of this vector in default order (increasing index).

public double

Returns:

the value returned by RealVectorChangingVisitor#end() at the end of the walk
walkInDefaultOrder
(final RealVectorChangingVisitor
the visitor to be used to process and modify the entries of this vector
visitor
)

Visits (and possibly alters) all entries of this vector in default order (increasing index).

public double

Returns:

the value returned by RealVectorChangingVisitor#end() at the end of the walk
walkInDefaultOrder
(final RealVectorChangingVisitor
visitor to be used to process the entries of this vector
visitor
,
final int
the index of the first entry to be visited
start
,
final int
the index of the last entry to be visited (inclusive)
end
)

Visits (and possibly alters) some entries of this vector in default order (increasing index).

public double

Returns:

the value returned by RealVectorPreservingVisitor#end() at the end of the walk
walkInOptimizedOrder
(final RealVectorPreservingVisitor
the visitor to be used to process the entries of this vector
visitor
)

Visits (but does not alter) all entries of this vector in optimized order.

public double

Returns:

the value returned by RealVectorPreservingVisitor#end() at the end of the walk
walkInOptimizedOrder
(final RealVectorPreservingVisitor
visitor to be used to process the entries of this vector
visitor
,
final int
the index of the first entry to be visited
start
,
final int
the index of the last entry to be visited (inclusive)
end
)

Visits (but does not alter) some entries of this vector in optimized order.

public double

Returns:

the value returned by RealVectorChangingVisitor#end() at the end of the walk
walkInOptimizedOrder
(final RealVectorChangingVisitor
the visitor to be used to process the entries of this vector
visitor
)

Visits (and possibly alters) all entries of this vector in optimized order.

public double

Returns:

the value returned by RealVectorChangingVisitor#end() at the end of the walk
walkInOptimizedOrder
(final RealVectorChangingVisitor
visitor to be used to process the entries of this vector
visitor
,
final int
the index of the first entry to be visited
start
,
final int
the index of the last entry to be visited (inclusive)
end
)

Visits (and possibly change) some entries of this vector in optimized order.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAlltoStringwaitwaitwait