Class ArrayRealVector
- All Implemented Interfaces:
Serializable
RealVector
interface with a double array.- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class org.hipparchus.linear.RealVector
RealVector.Entry, RealVector.SparseEntryIterator
-
Constructor Summary
ConstructorDescriptionBuild a 0-length vector.ArrayRealVector
(double[] d) Construct a vector from an array, copying the input array.ArrayRealVector
(double[] d, boolean copyArray) Create a new ArrayRealVector using the input array as the underlying data array.ArrayRealVector
(double[] v1, double[] v2) Construct a vector by appending one vector to another vector.ArrayRealVector
(double[] d, int pos, int size) Construct a vector from part of a array.ArrayRealVector
(double[] v1, ArrayRealVector v2) Construct a vector by appending one vector to another vector.ArrayRealVector
(int size) Construct a vector of zeroes.ArrayRealVector
(int size, double preset) Construct a vector with preset values.ArrayRealVector
(Double[] d) Construct a vector from an array.ArrayRealVector
(Double[] d, int pos, int size) Construct a vector from part of an array.Construct a vector from another vector, using a deep copy.ArrayRealVector
(ArrayRealVector v, boolean deep) Construct a vector from another vector.ArrayRealVector
(ArrayRealVector v1, double[] v2) Construct a vector by appending one vector to another vector.Construct a vector by appending one vector to another vector.ArrayRealVector
(ArrayRealVector v1, RealVector v2) Construct a vector by appending one vector to another vector.Construct a vector from another vector, using a deep copy.ArrayRealVector
(RealVector v1, ArrayRealVector v2) Construct a vector by appending one vector to another vector. -
Method Summary
Modifier and TypeMethodDescriptionadd
(RealVector v) Compute the sum of this vector andv
.void
addToEntry
(int index, double increment) Change an entry at the specified index.append
(double in) Construct a new vector by appending a double to this vector.Construct a vector by appending a vector to this vector.append
(RealVector v) Construct a new vector by appending a vector to this vector.protected void
checkVectorDimensions
(int n) Check if instance dimension is equal to some expected value.protected void
Check 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 ofthis
andy
.combineToSelf
(double a, double b, RealVector y) Updatesthis
with the linear combination ofthis
andy
.copy()
Returns a (deep) copy of this vector.double
Compute the dot product of this vector withv
.Element-by-element division.Element-by-element multiplication.boolean
Test for the equality of two real vectors.double[]
Get a reference to the underlying data array.int
Returns the size of the vector.double
Distance between two vectors.double
getEntry
(int index) Return the entry at the specified index.double
Distance between two vectors.double
Returns the L1 norm of the vector.double
Distance between two vectors.double
Returns the L∞ norm of the vector.double
getNorm()
Returns the L2 norm of the vector.getSubVector
(int index, int n) Get a subvector from consecutive elements.int
hashCode()
.boolean
Check whether any coordinate of this vector is infinite and none areNaN
.boolean
isNaN()
Check if any coordinate of this vector isNaN
.map
(UnivariateFunction function) Acts as if implemented as:mapAddToSelf
(double d) Add a value to each entry.mapDivideToSelf
(double d) Divide each entry by the argument.mapMultiplyToSelf
(double d) Multiply 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.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, double[] v) Set a set of consecutive elements.void
setSubVector
(int index, RealVector v) Set a sequence of consecutive elements.Subtractv
from this vector.double[]
toArray()
Convert the vector to an array ofdouble
s.toString()
double
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
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
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
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.Methods inherited from class org.hipparchus.linear.RealVector
checkIndex, checkIndices, cosine, getMaxIndex, getMaxValue, getMinIndex, getMinValue, iterator, mapAdd, mapDivide, mapMultiply, mapSubtract, projection, sparseIterator, unitize, unitVector, unmodifiableRealVector
-
Constructor Details
-
ArrayRealVector
public ArrayRealVector()Build a 0-length vector. Zero-length vectors may be used to initialized construction of vectors by data gathering. We start with zero-length and use either theArrayRealVector(ArrayRealVector, ArrayRealVector)
constructor or one of theappend
method (append(double)
,append(ArrayRealVector)
) to gather data into this vector. -
ArrayRealVector
public ArrayRealVector(int size) Construct a vector of zeroes.- Parameters:
size
- Size of the vector.
-
ArrayRealVector
public ArrayRealVector(int size, double preset) Construct a vector with preset values.- Parameters:
size
- Size of the vectorpreset
- All entries will be set with this value.
-
ArrayRealVector
public ArrayRealVector(double[] d) Construct a vector from an array, copying the input array.- Parameters:
d
- Array.
-
ArrayRealVector
Create a new ArrayRealVector using the input array as the underlying data array. If an array is built specially in order to be embedded in a ArrayRealVector and not used directly, thecopyArray
may be set tofalse
. This will prevent the copying and improve performance as no new array will be built and no data will be copied.- Parameters:
d
- Data for the new vector.copyArray
- iftrue
, the input array will be copied, otherwise it will be referenced.- Throws:
NullArgumentException
- ifd
isnull
.- See Also:
-
ArrayRealVector
public ArrayRealVector(double[] d, int pos, int size) throws MathIllegalArgumentException, NullArgumentException Construct a vector from part of a array.- Parameters:
d
- Array.pos
- Position of first entry.size
- Number of entries to copy.- Throws:
NullArgumentException
- ifd
isnull
.MathIllegalArgumentException
- if the size ofd
is less thanpos + size
.
-
ArrayRealVector
Construct a vector from an array.- Parameters:
d
- Array ofDouble
s.
-
ArrayRealVector
public ArrayRealVector(Double[] d, int pos, int size) throws MathIllegalArgumentException, NullArgumentException Construct a vector from part of an array.- Parameters:
d
- Array.pos
- Position of first entry.size
- Number of entries to copy.- Throws:
NullArgumentException
- ifd
isnull
.MathIllegalArgumentException
- if the size ofd
is less thanpos + size
.
-
ArrayRealVector
Construct a vector from another vector, using a deep copy.- Parameters:
v
- vector to copy.- Throws:
NullArgumentException
- ifv
isnull
.
-
ArrayRealVector
Construct a vector from another vector, using a deep copy.- Parameters:
v
- Vector to copy.- Throws:
NullArgumentException
- ifv
isnull
.
-
ArrayRealVector
Construct a vector from another vector.- Parameters:
v
- Vector to copy.deep
- Iftrue
perform a deep copy, otherwise perform a shallow copy.
-
ArrayRealVector
Construct a vector by appending one vector to another vector.- Parameters:
v1
- First vector (will be put in front of the new vector).v2
- Second vector (will be put at back of the new vector).
-
ArrayRealVector
Construct a vector by appending one vector to another vector.- Parameters:
v1
- First vector (will be put in front of the new vector).v2
- Second vector (will be put at back of the new vector).
-
ArrayRealVector
Construct a vector by appending one vector to another vector.- Parameters:
v1
- First vector (will be put in front of the new vector).v2
- Second vector (will be put at back of the new vector).
-
ArrayRealVector
Construct a vector by appending one vector to another vector.- Parameters:
v1
- First vector (will be put in front of the new vector).v2
- Second vector (will be put at back of the new vector).
-
ArrayRealVector
Construct a vector by appending one vector to another vector.- Parameters:
v1
- First vector (will be put in front of the new vector).v2
- Second vector (will be put at back of the new vector).
-
ArrayRealVector
public ArrayRealVector(double[] v1, double[] v2) Construct a vector by appending one vector to another vector.- Parameters:
v1
- first vector (will be put in front of the new vector)v2
- second vector (will be put at back of the new vector)
-
-
Method Details
-
copy
Returns a (deep) copy of this vector.- Specified by:
copy
in classRealVector
- Returns:
- a vector copy.
-
add
Compute the sum of this vector andv
. Returns a new vector. Does not change instance data.- Overrides:
add
in classRealVector
- Parameters:
v
- Vector to be added.- Returns:
this
+v
.- Throws:
MathIllegalArgumentException
- ifv
is not the same size asthis
vector.
-
subtract
Subtractv
from this vector. Returns a new vector. Does not change instance data.- Overrides:
subtract
in classRealVector
- Parameters:
v
- Vector to be subtracted.- Returns:
this
-v
.- Throws:
MathIllegalArgumentException
- ifv
is not the same size asthis
vector.
-
map
Acts as if implemented as:return copy().mapToSelf(function);
Returns a new vector. Does not change instance data.- Overrides:
map
in classRealVector
- 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<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.- Overrides:
mapToSelf
in classRealVector
- Parameters:
function
- Function to apply to each entry.- Returns:
- a reference to this vector.
-
mapAddToSelf
Add a value to each entry. The instance is changed in-place.- Overrides:
mapAddToSelf
in classRealVector
- Parameters:
d
- Value to be added to each entry.- Returns:
this
.
-
mapSubtractToSelf
Subtract a value from each entry. The instance is changed in-place.- Overrides:
mapSubtractToSelf
in classRealVector
- Parameters:
d
- Value to be subtracted.- Returns:
this
.
-
mapMultiplyToSelf
Multiply each entry. The instance is changed in-place.- Overrides:
mapMultiplyToSelf
in classRealVector
- Parameters:
d
- Multiplication factor.- Returns:
this
.
-
mapDivideToSelf
Divide each entry by the argument. The instance is changed in-place.- Overrides:
mapDivideToSelf
in classRealVector
- Parameters:
d
- Value to divide by.- Returns:
this
.
-
ebeMultiply
Element-by-element multiplication.- Specified by:
ebeMultiply
in classRealVector
- Parameters:
v
- Vector by which instance elements must be multiplied- Returns:
- a vector containing this[i] * v[i] for all i.
- Throws:
MathIllegalArgumentException
- ifv
is not the same size asthis
vector.
-
ebeDivide
Element-by-element division.- Specified by:
ebeDivide
in classRealVector
- Parameters:
v
- Vector by which instance elements must be divided.- Returns:
- a vector containing this[i] / v[i] for all i.
- Throws:
MathIllegalArgumentException
- ifv
is not the same size asthis
vector.
-
getDataRef
public double[] getDataRef()Get a reference to the underlying data array. This method does not make a fresh copy of the underlying data.- Returns:
- the array of entries.
-
dotProduct
Compute the dot product of this vector withv
.- Overrides:
dotProduct
in classRealVector
- Parameters:
v
- Vector with which dot product should be computed- Returns:
- the scalar dot product between this instance and
v
. - Throws:
MathIllegalArgumentException
- ifv
is not the same size asthis
vector.
-
getNorm
public double getNorm()Returns the L2 norm of the vector.The L2 norm is the root of the sum of the squared elements.
- Overrides:
getNorm
in classRealVector
- 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.
- Overrides:
getL1Norm
in classRealVector
- 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.
- Overrides:
getLInfNorm
in classRealVector
- Returns:
- the norm.
- See Also:
-
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.
- Overrides:
getDistance
in classRealVector
- Parameters:
v
- Vector to which distance is requested.- Returns:
- the distance between two vectors.
- Throws:
MathIllegalArgumentException
- ifv
is not the same size asthis
vector.- 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.
- Overrides:
getL1Distance
in classRealVector
- Parameters:
v
- Vector to which distance is requested.- Returns:
- the distance between two vectors.
- Throws:
MathIllegalArgumentException
- ifv
is not the same size asthis
vector.
-
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.
- Overrides:
getLInfDistance
in classRealVector
- Parameters:
v
- Vector to which distance is requested.- Returns:
- the distance between two vectors.
- Throws:
MathIllegalArgumentException
- ifv
is not the same size asthis
vector.- See Also:
-
outerProduct
Compute the outer product.- Overrides:
outerProduct
in classRealVector
- Parameters:
v
- Vector with which outer product should be computed.- Returns:
- the matrix outer product between this instance and
v
.
-
getEntry
Return the entry at the specified index.- Specified by:
getEntry
in classRealVector
- Parameters:
index
- Index location of entry to be fetched.- Returns:
- the vector entry at
index
. - Throws:
MathIllegalArgumentException
- if the index is not valid.- See Also:
-
getDimension
public int getDimension()Returns the size of the vector.- Specified by:
getDimension
in classRealVector
- Returns:
- the size of this vector.
-
append
Construct a new vector by appending a vector to this vector.- Specified by:
append
in classRealVector
- Parameters:
v
- vector to append to this one.- Returns:
- a new vector.
-
append
Construct a 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.- Specified by:
append
in classRealVector
- Parameters:
in
- double to append.- Returns:
- a new vector.
-
getSubVector
Get a subvector from consecutive elements.- Specified by:
getSubVector
in classRealVector
- Parameters:
index
- index of first element.n
- number of elements to be retrieved.- Returns:
- a vector containing n elements.
- Throws:
MathIllegalArgumentException
- if the index is not valid.
-
setEntry
Set a single element.- Specified by:
setEntry
in classRealVector
- Parameters:
index
- element index.value
- new value for the element.- Throws:
MathIllegalArgumentException
- if the index is not valid.- See Also:
-
addToEntry
Change an entry at the specified index.- Overrides:
addToEntry
in classRealVector
- Parameters:
index
- Index location of entry to be set.increment
- Value to add to the vector entry.- Throws:
MathIllegalArgumentException
- if the index is not valid.
-
setSubVector
Set a sequence of consecutive elements.- Specified by:
setSubVector
in classRealVector
- Parameters:
index
- index of first element to be set.v
- vector containing the values to set.- Throws:
MathIllegalArgumentException
- if the index is not valid.
-
setSubVector
Set a set of consecutive elements.- Parameters:
index
- Index of first element to be set.v
- Vector containing the values to set.- Throws:
MathIllegalArgumentException
- if the index is inconsistent with the vector size.
-
set
public void set(double value) Set all elements to a single value.- Overrides:
set
in classRealVector
- Parameters:
value
- Single value to set for all elements.
-
toArray
public double[] toArray()Convert the vector to an array ofdouble
s. The array is independent from this vector data: the elements are copied.- Overrides:
toArray
in classRealVector
- Returns:
- an array containing a copy of the vector elements.
-
toString
-
checkVectorDimensions
Check if instance and specified vectors have the same dimension.- Overrides:
checkVectorDimensions
in classRealVector
- Parameters:
v
- Vector to compare instance with.- Throws:
MathIllegalArgumentException
- if the vectors do not have the same dimension.
-
checkVectorDimensions
Check if instance dimension is equal to some expected value.- Overrides:
checkVectorDimensions
in classRealVector
- Parameters:
n
- Expected dimension.- Throws:
MathIllegalArgumentException
- if the dimension is inconsistent with vector size.
-
isNaN
public boolean isNaN()Check if any coordinate of this vector isNaN
.- Specified by:
isNaN
in classRealVector
- Returns:
true
if any coordinate of this vector isNaN
,false
otherwise.
-
isInfinite
public boolean isInfinite()Check whether any coordinate of this vector is infinite and none areNaN
.- Specified by:
isInfinite
in classRealVector
- Returns:
true
if any coordinate of this vector is infinite and none areNaN
,false
otherwise.
-
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.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 toNaN
, the real vector is equal to a vector with allNaN
coordinates.This method must be overriden by concrete subclasses of
RealVector
(the current implementation throws an exception).- Overrides:
equals
in classRealVector
- Parameters:
other
- Object to test for equality.- Returns:
true
if two vector objects are equal,false
ifother
is null, not an instance ofRealVector
, or not equal to thisRealVector
instance.
-
hashCode
public int hashCode(). This method must be overriden by concrete subclasses ofRealVector
(current implementation throws an exception). AllNaN
values have the same hash code.- Overrides:
hashCode
in classRealVector
-
combine
public ArrayRealVector combine(double a, double b, RealVector y) throws MathIllegalArgumentException Returns a new vector representinga * this + b * y
, the linear combination ofthis
andy
. Returns a new vector. Does not change instance data.- Overrides:
combine
in classRealVector
- Parameters:
a
- Coefficient ofthis
.b
- Coefficient ofy
.y
- Vector with whichthis
is linearly combined.- Returns:
- a vector containing
a * this[i] + b * y[i]
for alli
. - Throws:
MathIllegalArgumentException
- ify
is not the same size asthis
vector.
-
combineToSelf
public ArrayRealVector combineToSelf(double a, double b, RealVector y) throws MathIllegalArgumentException Updatesthis
with the linear combination ofthis
andy
.- Overrides:
combineToSelf
in classRealVector
- Parameters:
a
- Weight ofthis
.b
- Weight ofy
.y
- Vector with whichthis
is linearly combined.- Returns:
this
, with components equal toa * this[i] + b * y[i]
for alli
.- Throws:
MathIllegalArgumentException
- ify
is not the same size asthis
vector.
-
walkInDefaultOrder
Visits (but does not alter) all entries of this vector in default order (increasing index).- Overrides:
walkInDefaultOrder
in classRealVector
- 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
-
walkInDefaultOrder
public double walkInDefaultOrder(RealVectorPreservingVisitor visitor, int start, int end) throws MathIllegalArgumentException Visits (but does not alter) some entries of this vector in default order (increasing index).- Overrides:
walkInDefaultOrder
in classRealVector
- 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:
MathIllegalArgumentException
- ifend < start
.
-
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. In this implementation, the optimized order is the default order.- Overrides:
walkInOptimizedOrder
in classRealVector
- 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
-
walkInOptimizedOrder
public double walkInOptimizedOrder(RealVectorPreservingVisitor visitor, int start, int end) throws MathIllegalArgumentException 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. In this implementation, the optimized order is the default order.- Overrides:
walkInOptimizedOrder
in classRealVector
- 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:
MathIllegalArgumentException
- ifend < start
.
-
walkInDefaultOrder
Visits (and possibly alters) all entries of this vector in default order (increasing index).- Overrides:
walkInDefaultOrder
in classRealVector
- 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
-
walkInDefaultOrder
public double walkInDefaultOrder(RealVectorChangingVisitor visitor, int start, int end) throws MathIllegalArgumentException Visits (and possibly alters) some entries of this vector in default order (increasing index).- Overrides:
walkInDefaultOrder
in classRealVector
- 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:
MathIllegalArgumentException
- ifend < start
.
-
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. In this implementation, the optimized order is the default order.- Overrides:
walkInOptimizedOrder
in classRealVector
- 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
-
walkInOptimizedOrder
public double walkInOptimizedOrder(RealVectorChangingVisitor visitor, int start, int end) throws MathIllegalArgumentException 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. In this implementation, the optimized order is the default order.- Overrides:
walkInOptimizedOrder
in classRealVector
- 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:
MathIllegalArgumentException
- ifend < start
.
-