Package org.hipparchus.linear
Class SparseFieldVector<T extends FieldElement<T>>
- java.lang.Object
-
- org.hipparchus.linear.SparseFieldVector<T>
-
- Type Parameters:
T- the type of the field elements
- All Implemented Interfaces:
Serializable,FieldVector<T>
public class SparseFieldVector<T extends FieldElement<T>> extends Object implements FieldVector<T>, Serializable
This class implements theFieldVectorinterface with aOpenIntToFieldHashMapbacking store.Caveat: This implementation assumes that, for any
x, the equalityx * 0d == 0dholds. But it is is not true forNaN. Moreover, zero entries will lose their sign. Some operations (that involveNaNand/or infinities) may thus give incorrect results.- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Modifier Constructor Description SparseFieldVector(Field<T> field)Build a 0-length vector.SparseFieldVector(Field<T> field, int dimension)Construct a vector of zeroes.SparseFieldVector(Field<T> field, int dimension, int expectedSize)Build a vector with known the sparseness (for advanced use only).SparseFieldVector(Field<T> field, T[] values)Create from a Field array.SparseFieldVector(SparseFieldVector<T> v)Copy constructor.protectedSparseFieldVector(SparseFieldVector<T> v, int resize)Build a resized vector, for use with append.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description FieldVector<T>add(FieldVector<T> v)Compute the sum ofthisandv.FieldVector<T>add(SparseFieldVector<T> v)Optimized method to add sparse vectors.FieldVector<T>append(FieldVector<T> v)Construct a vector by appending a vector to this vector.FieldVector<T>append(SparseFieldVector<T> v)Construct a vector by appending a vector to this vector.FieldVector<T>append(T d)Construct a vector by appending a T to this vector.protected voidcheckVectorDimensions(int n)Check if instance dimension is equal to some expected value.FieldVector<T>copy()Returns a (deep) copy of this.TdotProduct(FieldVector<T> v)Compute the dot product.FieldVector<T>ebeDivide(FieldVector<T> v)Element-by-element division.FieldVector<T>ebeMultiply(FieldVector<T> v)Element-by-element multiplication.booleanequals(Object obj)intgetDimension()Returns the size of the vector.TgetEntry(int index)Returns the entry in the specified index.Field<T>getField()Get the type of field elements of the vector.FieldVector<T>getSubVector(int index, int n)Get a subvector from consecutive elements.inthashCode()FieldVector<T>mapAdd(T d)Map an addition operation to each entry.FieldVector<T>mapAddToSelf(T d)Map an addition operation to each entry.FieldVector<T>mapDivide(T d)Map a division operation to each entry.FieldVector<T>mapDivideToSelf(T d)Map a division operation to each entry.FieldVector<T>mapInv()Map the 1/x function to each entry.FieldVector<T>mapInvToSelf()Map the 1/x function to each entry.FieldVector<T>mapMultiply(T d)Map a multiplication operation to each entry.FieldVector<T>mapMultiplyToSelf(T d)Map a multiplication operation to each entry.FieldVector<T>mapSubtract(T d)Map a subtraction operation to each entry.FieldVector<T>mapSubtractToSelf(T d)Map a subtraction operation to each entry.FieldMatrix<T>outerProduct(FieldVector<T> v)Compute the outer product.FieldMatrix<T>outerProduct(SparseFieldVector<T> v)Optimized method to compute outer product when both vectors are sparse.FieldVector<T>projection(FieldVector<T> v)Find the orthogonal projection of this vector onto another vector.voidset(T value)Set all elements to a single value.voidsetEntry(int index, T value)Set a single element.voidsetSubVector(int index, FieldVector<T> v)Set a set of consecutive elements.FieldVector<T>subtract(FieldVector<T> v)Computethisminusv.SparseFieldVector<T>subtract(SparseFieldVector<T> v)Optimized method to computethisminusv.T[]toArray()Convert the vector to a T array.TwalkInDefaultOrder(FieldVectorChangingVisitor<T> visitor)Visits (and possibly alters) all entries of this vector in default order (increasing index).TwalkInDefaultOrder(FieldVectorChangingVisitor<T> visitor, int start, int end)Visits (and possibly alters) some entries of this vector in default order (increasing index).TwalkInDefaultOrder(FieldVectorPreservingVisitor<T> visitor)Visits (but does not alter) all entries of this vector in default order (increasing index).TwalkInDefaultOrder(FieldVectorPreservingVisitor<T> visitor, int start, int end)Visits (but does not alter) some entries of this vector in default order (increasing index).TwalkInOptimizedOrder(FieldVectorChangingVisitor<T> visitor)Visits (and possibly alters) all entries of this vector in optimized order.TwalkInOptimizedOrder(FieldVectorChangingVisitor<T> visitor, int start, int end)Visits (and possibly change) some entries of this vector in optimized order.TwalkInOptimizedOrder(FieldVectorPreservingVisitor<T> visitor)Visits (but does not alter) all entries of this vector in optimized order.TwalkInOptimizedOrder(FieldVectorPreservingVisitor<T> visitor, int start, int end)Visits (but does not alter) some entries of this vector in optimized order.
-
-
-
Constructor Detail
-
SparseFieldVector
public SparseFieldVector(Field<T> field)
Build a 0-length vector. Zero-length vectors may be used to initialize construction of vectors by data gathering. We start with zero-length and use either theSparseFieldVector(SparseFieldVector, int)constructor or one of theappendmethod (append(FieldVector)orappend(SparseFieldVector)) to gather data into this vector.- Parameters:
field- Field to which the elements belong.
-
SparseFieldVector
public SparseFieldVector(Field<T> field, int dimension)
Construct a vector of zeroes.- Parameters:
field- Field to which the elements belong.dimension- Size of the vector.
-
SparseFieldVector
protected SparseFieldVector(SparseFieldVector<T> v, int resize)
Build a resized vector, for use with append.- Parameters:
v- Original vectorresize- Amount to add.
-
SparseFieldVector
public SparseFieldVector(Field<T> field, int dimension, int expectedSize)
Build a vector with known the sparseness (for advanced use only).- Parameters:
field- Field to which the elements belong.dimension- Size of the vector.expectedSize- Expected number of non-zero entries.
-
SparseFieldVector
public SparseFieldVector(Field<T> field, T[] values) throws NullArgumentException
Create from a Field array. Only non-zero entries will be stored.- Parameters:
field- Field to which the elements belong.values- Set of values to create from.- Throws:
NullArgumentException- if values is null
-
SparseFieldVector
public SparseFieldVector(SparseFieldVector<T> v)
Copy constructor.- Parameters:
v- Instance to copy.
-
-
Method Detail
-
add
public FieldVector<T> add(SparseFieldVector<T> v) throws MathIllegalArgumentException
Optimized method to add sparse vectors.- Parameters:
v- Vector to add.- Returns:
this + v.- Throws:
MathIllegalArgumentException- ifvis not the same size asthis.
-
append
public FieldVector<T> append(SparseFieldVector<T> v)
Construct a vector by appending a vector to this vector.- Parameters:
v- Vector to append to this one.- Returns:
- a new vector.
-
append
public FieldVector<T> append(FieldVector<T> v)
Construct a vector by appending a vector to this vector.- Specified by:
appendin interfaceFieldVector<T extends FieldElement<T>>- Parameters:
v- vector to append to this one.- Returns:
- a new vector
-
append
public FieldVector<T> append(T d) throws NullArgumentException
Construct a vector by appending a T to this vector.- Specified by:
appendin interfaceFieldVector<T extends FieldElement<T>>- Parameters:
d- T to append.- Returns:
- a new vector
- Throws:
NullArgumentException- if d is null
-
copy
public FieldVector<T> copy()
Returns a (deep) copy of this.- Specified by:
copyin interfaceFieldVector<T extends FieldElement<T>>- Returns:
- vector copy
-
dotProduct
public T dotProduct(FieldVector<T> v) throws MathIllegalArgumentException
Compute the dot product.- Specified by:
dotProductin interfaceFieldVector<T extends FieldElement<T>>- Parameters:
v- vector with which dot product should be computed- Returns:
- the scalar dot product of
thisandv - Throws:
MathIllegalArgumentException- ifvis not the same size asthis
-
ebeDivide
public FieldVector<T> ebeDivide(FieldVector<T> v) throws MathRuntimeException
Element-by-element division.- Specified by:
ebeDividein interfaceFieldVector<T extends FieldElement<T>>- Parameters:
v- vector by which instance elements must be divided- Returns:
- a vector containing
this[i] / v[i]for alli - Throws:
MathIllegalArgumentException- ifvis not the same size asthisMathRuntimeException- if one entry ofvis zero.
-
ebeMultiply
public FieldVector<T> ebeMultiply(FieldVector<T> v) throws MathIllegalArgumentException
Element-by-element multiplication.- Specified by:
ebeMultiplyin interfaceFieldVector<T extends FieldElement<T>>- Parameters:
v- vector by which instance elements must be multiplied- Returns:
- a vector containing
this[i] * v[i]for alli - Throws:
MathIllegalArgumentException- ifvis not the same size asthis
-
getDimension
public int getDimension()
Returns the size of the vector.- Specified by:
getDimensionin interfaceFieldVector<T extends FieldElement<T>>- Returns:
- size
-
getEntry
public T getEntry(int index) throws MathIllegalArgumentException
Returns the entry in the specified index.- Specified by:
getEntryin interfaceFieldVector<T extends FieldElement<T>>- 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:
FieldVector.setEntry(int, FieldElement)
-
getField
public Field<T> getField()
Get the type of field elements of the vector.- Specified by:
getFieldin interfaceFieldVector<T extends FieldElement<T>>- Returns:
- type of field elements of the vector
-
getSubVector
public FieldVector<T> getSubVector(int index, int n) throws MathIllegalArgumentException
Get a subvector from consecutive elements.- Specified by:
getSubVectorin interfaceFieldVector<T extends FieldElement<T>>- 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.
-
mapAdd
public FieldVector<T> mapAdd(T d) throws NullArgumentException
Map an addition operation to each entry.- Specified by:
mapAddin interfaceFieldVector<T extends FieldElement<T>>- Parameters:
d- value to be added to each entry- Returns:
this + d- Throws:
NullArgumentException- ifdisnull.
-
mapAddToSelf
public FieldVector<T> mapAddToSelf(T d) throws NullArgumentException
Map an addition operation to each entry.The instance is changed by this method.
- Specified by:
mapAddToSelfin interfaceFieldVector<T extends FieldElement<T>>- Parameters:
d- value to be added to each entry- Returns:
- for convenience, return
this - Throws:
NullArgumentException- ifdisnull.
-
mapDivide
public FieldVector<T> mapDivide(T d) throws NullArgumentException, MathRuntimeException
Map a division operation to each entry.- Specified by:
mapDividein interfaceFieldVector<T extends FieldElement<T>>- Parameters:
d- value to divide all entries by- Returns:
this / d- Throws:
NullArgumentException- ifdisnull.MathRuntimeException- ifdis zero.
-
mapDivideToSelf
public FieldVector<T> mapDivideToSelf(T d) throws NullArgumentException, MathRuntimeException
Map a division operation to each entry.The instance is changed by this method.
- Specified by:
mapDivideToSelfin interfaceFieldVector<T extends FieldElement<T>>- Parameters:
d- value to divide all entries by- Returns:
- for convenience, return
this - Throws:
NullArgumentException- ifdisnull.MathRuntimeException- ifdis zero.
-
mapInv
public FieldVector<T> mapInv() throws MathRuntimeException
Map the 1/x function to each entry.- Specified by:
mapInvin interfaceFieldVector<T extends FieldElement<T>>- Returns:
- a vector containing the result of applying the function to each entry.
- Throws:
MathRuntimeException- if one of the entries is zero.
-
mapInvToSelf
public FieldVector<T> mapInvToSelf() throws MathRuntimeException
Map the 1/x function to each entry.The instance is changed by this method.
- Specified by:
mapInvToSelfin interfaceFieldVector<T extends FieldElement<T>>- Returns:
- for convenience, return
this - Throws:
MathRuntimeException- if one of the entries is zero.
-
mapMultiply
public FieldVector<T> mapMultiply(T d) throws NullArgumentException
Map a multiplication operation to each entry.- Specified by:
mapMultiplyin interfaceFieldVector<T extends FieldElement<T>>- Parameters:
d- value to multiply all entries by- Returns:
this * d- Throws:
NullArgumentException- ifdisnull.
-
mapMultiplyToSelf
public FieldVector<T> mapMultiplyToSelf(T d) throws NullArgumentException
Map a multiplication operation to each entry.The instance is changed by this method.
- Specified by:
mapMultiplyToSelfin interfaceFieldVector<T extends FieldElement<T>>- Parameters:
d- value to multiply all entries by- Returns:
- for convenience, return
this - Throws:
NullArgumentException- ifdisnull.
-
mapSubtract
public FieldVector<T> mapSubtract(T d) throws NullArgumentException
Map a subtraction operation to each entry.- Specified by:
mapSubtractin interfaceFieldVector<T extends FieldElement<T>>- Parameters:
d- value to be subtracted to each entry- Returns:
this - d- Throws:
NullArgumentException- ifdisnull
-
mapSubtractToSelf
public FieldVector<T> mapSubtractToSelf(T d) throws NullArgumentException
Map a subtraction operation to each entry.The instance is changed by this method.
- Specified by:
mapSubtractToSelfin interfaceFieldVector<T extends FieldElement<T>>- Parameters:
d- value to be subtracted to each entry- Returns:
- for convenience, return
this - Throws:
NullArgumentException- ifdisnull
-
outerProduct
public FieldMatrix<T> outerProduct(SparseFieldVector<T> v)
Optimized method to compute outer product when both vectors are sparse.- Parameters:
v- vector with which outer product should be computed- Returns:
- the matrix outer product between instance and v
-
outerProduct
public FieldMatrix<T> outerProduct(FieldVector<T> v)
Compute the outer product.- Specified by:
outerProductin interfaceFieldVector<T extends FieldElement<T>>- Parameters:
v- vector with which outer product should be computed- Returns:
- the matrix outer product between instance and v
-
projection
public FieldVector<T> projection(FieldVector<T> v) throws MathRuntimeException
Find the orthogonal projection of this vector onto another vector.- Specified by:
projectionin interfaceFieldVector<T extends FieldElement<T>>- Parameters:
v- vector onto whichthismust be projected- Returns:
- projection of
thisontov - Throws:
MathIllegalArgumentException- ifvis not the same size asthisMathRuntimeException- ifvis the null vector.
-
set
public void set(T value)
Set all elements to a single value.- Specified by:
setin interfaceFieldVector<T extends FieldElement<T>>- Parameters:
value- single value to set for all elements- Throws:
NullArgumentException- if value is null
-
setEntry
public void setEntry(int index, T value) throws MathIllegalArgumentException, NullArgumentExceptionSet a single element.- Specified by:
setEntryin interfaceFieldVector<T extends FieldElement<T>>- Parameters:
index- element index.value- new value for the element.- Throws:
NullArgumentException- if value is nullMathIllegalArgumentException- if the index is not valid.- See Also:
FieldVector.getEntry(int)
-
setSubVector
public void setSubVector(int index, FieldVector<T> v) throws MathIllegalArgumentExceptionSet a set of consecutive elements.- Specified by:
setSubVectorin interfaceFieldVector<T extends FieldElement<T>>- Parameters:
index- index of first element to be set.v- vector containing the values to set.- Throws:
MathIllegalArgumentException- if the index is not valid.
-
subtract
public SparseFieldVector<T> subtract(SparseFieldVector<T> v) throws MathIllegalArgumentException
Optimized method to computethisminusv.- Parameters:
v- vector to be subtracted- Returns:
this - v- Throws:
MathIllegalArgumentException- ifvis not the same size asthis.
-
subtract
public FieldVector<T> subtract(FieldVector<T> v) throws MathIllegalArgumentException
Computethisminusv.- Specified by:
subtractin interfaceFieldVector<T extends FieldElement<T>>- Parameters:
v- vector to be subtracted- Returns:
this - v- Throws:
MathIllegalArgumentException- ifvis not the same size asthis
-
toArray
public T[] toArray()
Convert the vector to a T array.The array is independent from vector data, it's elements are copied.
- Specified by:
toArrayin interfaceFieldVector<T extends FieldElement<T>>- Returns:
- array containing a copy of vector elements
-
checkVectorDimensions
protected void checkVectorDimensions(int n) throws MathIllegalArgumentExceptionCheck if instance dimension is equal to some expected value.- Parameters:
n- Expected dimension.- Throws:
MathIllegalArgumentException- if the dimensions do not match.
-
add
public FieldVector<T> add(FieldVector<T> v) throws MathIllegalArgumentException
Compute the sum ofthisandv.- Specified by:
addin interfaceFieldVector<T extends FieldElement<T>>- Parameters:
v- vector to be added- Returns:
this + v- Throws:
MathIllegalArgumentException- ifvis not the same size asthis
-
walkInDefaultOrder
public T walkInDefaultOrder(FieldVectorPreservingVisitor<T> visitor)
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
FieldVectorPreservingVisitor.end()at the end of the walk
-
walkInDefaultOrder
public T walkInDefaultOrder(FieldVectorPreservingVisitor<T> visitor, int start, int end) throws MathIllegalArgumentException
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
FieldVectorPreservingVisitor.end()at the end of the walk - Throws:
MathIllegalArgumentException- ifend < start.MathIllegalArgumentException- if the indices are not valid.
-
walkInOptimizedOrder
public T walkInOptimizedOrder(FieldVectorPreservingVisitor<T> visitor)
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
FieldVectorPreservingVisitor.end()at the end of the walk
-
walkInOptimizedOrder
public T walkInOptimizedOrder(FieldVectorPreservingVisitor<T> 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.- 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
FieldVectorPreservingVisitor.end()at the end of the walk - Throws:
MathIllegalArgumentException- ifend < start.MathIllegalArgumentException- if the indices are not valid.
-
walkInDefaultOrder
public T walkInDefaultOrder(FieldVectorChangingVisitor<T> visitor)
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
FieldVectorChangingVisitor.end()at the end of the walk
-
walkInDefaultOrder
public T walkInDefaultOrder(FieldVectorChangingVisitor<T> visitor, int start, int end) throws MathIllegalArgumentException
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
FieldVectorChangingVisitor.end()at the end of the walk - Throws:
MathIllegalArgumentException- ifend < start.MathIllegalArgumentException- if the indices are not valid.
-
walkInOptimizedOrder
public T walkInOptimizedOrder(FieldVectorChangingVisitor<T> visitor)
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
FieldVectorChangingVisitor.end()at the end of the walk
-
walkInOptimizedOrder
public T walkInOptimizedOrder(FieldVectorChangingVisitor<T> 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.- 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
FieldVectorChangingVisitor.end()at the end of the walk - Throws:
MathIllegalArgumentException- ifend < start.MathIllegalArgumentException- if the indices are not valid.
-
-