Package org.apache.commons.math.linear
Class SparseFieldVector<T extends FieldElement<T>>
- java.lang.Object
 - 
- org.apache.commons.math.linear.SparseFieldVector<T>
 
 
- 
- Type Parameters:
 T- the type of the field elements
- All Implemented Interfaces:
 java.io.Serializable,FieldVector<T>
public class SparseFieldVector<T extends FieldElement<T>> extends java.lang.Object implements FieldVector<T>, java.io.Serializable
This class implements theFieldVectorinterface with aOpenIntToFieldHashMapbacking store.- Since:
 - 2.0
 - See Also:
 - Serialized Form
 
 
- 
- 
Constructor Summary
Constructors Constructor Description SparseFieldVector(Field<T> field)Build a 0-length vector.SparseFieldVector(Field<T> field, int dimension)Construct a (dimension)-length vector of zeros.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. 
- 
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description FieldVector<T>add(FieldVector<T> v)Compute the sum of this and v.FieldVector<T>add(SparseFieldVector<T> v)Optimized method to add sparse vectors.FieldVector<T>add(T[] v)Compute the sum of this and v.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.FieldVector<T>append(T[] a)Construct a vector by appending a T array to this vector.FieldVector<T>copy()Returns a (deep) copy of this.TdotProduct(FieldVector<T> v)Compute the dot product.TdotProduct(T[] v)Compute the dot product.FieldVector<T>ebeDivide(FieldVector<T> v)Element-by-element division.FieldVector<T>ebeDivide(T[] v)Element-by-element division.FieldVector<T>ebeMultiply(FieldVector<T> v)Element-by-element multiplication.FieldVector<T>ebeMultiply(T[] v)Element-by-element multiplication.booleanequals(java.lang.Object obj)T[]getData()Returns vector entries as a T array.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.FieldMatrix<T>outerProduct(T[] v)Compute the outer product.FieldVector<T>projection(FieldVector<T> v)Find the orthogonal projection of this vector onto another vector.FieldVector<T>projection(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.voidsetSubVector(int index, T[] v)Set a set of consecutive elements.FieldVector<T>subtract(FieldVector<T> v)Compute this minus v.SparseFieldVector<T>subtract(SparseFieldVector<T> v)Optimized method to subtract SparseRealVectors.FieldVector<T>subtract(T[] v)Compute this minus v.T[]toArray()Convert the vector to a T array. 
 - 
 
- 
- 
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 the
SparseFieldVector(SparseFieldVector, int)constructor or one of theappendmethod (append(FieldElement),append(FieldElement[]),append(FieldVector),append(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 (dimension)-length vector of zeros.- Parameters:
 field- field to which the elements belongdimension- Size of the vector
 
- 
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 belongdimension- The size of the vectorexpectedSize- The expected number of non-zero entries
 
- 
SparseFieldVector
public SparseFieldVector(Field<T> field, T[] values)
Create from a Field array. Only non-zero entries will be stored- Parameters:
 field- field to which the elements belongvalues- The set of values to create from
 
- 
SparseFieldVector
public SparseFieldVector(SparseFieldVector<T> v)
Copy constructor.- Parameters:
 v- The instance to copy from
 
 - 
 
- 
Method Detail
- 
add
public FieldVector<T> add(SparseFieldVector<T> v) throws java.lang.IllegalArgumentException
Optimized method to add sparse vectors.- Parameters:
 v- vector to add- Returns:
 - The sum of 
thisandv - Throws:
 java.lang.IllegalArgumentException- If the dimensions don't match
 
- 
add
public FieldVector<T> add(T[] v) throws java.lang.IllegalArgumentException
Compute the sum of this and v.- Specified by:
 addin interfaceFieldVector<T extends FieldElement<T>>- Parameters:
 v- vector to be added- Returns:
 - this + v
 - Throws:
 java.lang.IllegalArgumentException- if v is not the same size as this
 
- 
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)
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
 
 
- 
append
public FieldVector<T> append(T[] a)
Construct a vector by appending a T array to this vector.- Specified by:
 appendin interfaceFieldVector<T extends FieldElement<T>>- Parameters:
 a- T array to append.- Returns:
 - a new vector
 
 
- 
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 java.lang.IllegalArgumentException
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 between instance and v
 - Throws:
 java.lang.IllegalArgumentException- if v is not the same size as this
 
- 
dotProduct
public T dotProduct(T[] v) throws java.lang.IllegalArgumentException
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 between instance and v
 - Throws:
 java.lang.IllegalArgumentException- if v is not the same size as this
 
- 
ebeDivide
public FieldVector<T> ebeDivide(FieldVector<T> v) throws java.lang.IllegalArgumentException
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 all i
 - Throws:
 java.lang.IllegalArgumentException- if v is not the same size as this
 
- 
ebeDivide
public FieldVector<T> ebeDivide(T[] v) throws java.lang.IllegalArgumentException
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 all i
 - Throws:
 java.lang.IllegalArgumentException- if v is not the same size as this
 
- 
ebeMultiply
public FieldVector<T> ebeMultiply(FieldVector<T> v) throws java.lang.IllegalArgumentException
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 all i
 - Throws:
 java.lang.IllegalArgumentException- if v is not the same size as this
 
- 
ebeMultiply
public FieldVector<T> ebeMultiply(T[] v) throws java.lang.IllegalArgumentException
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 all i
 - Throws:
 java.lang.IllegalArgumentException- if v is not the same size as this
 
- 
getData
public T[] getData()
Returns vector entries as a T array.- Specified by:
 getDatain interfaceFieldVector<T extends FieldElement<T>>- Returns:
 - T array of entries
 
 
- 
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 MatrixIndexException
Returns the entry in the specified index.The index start at 0 and must be lesser than the size, otherwise a
MatrixIndexExceptionis thrown.- Specified by:
 getEntryin interfaceFieldVector<T extends FieldElement<T>>- Parameters:
 index- index location of entry to be fetched- Returns:
 - vector entry at index
 - Throws:
 MatrixIndexException- 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 MatrixIndexException
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:
 MatrixIndexException- if the index is inconsistent with vector size
 
- 
mapAdd
public FieldVector<T> mapAdd(T d)
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
 
 
- 
mapAddToSelf
public FieldVector<T> mapAddToSelf(T d)
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
 
 
- 
mapDivide
public FieldVector<T> mapDivide(T d)
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
 
 
- 
mapDivideToSelf
public FieldVector<T> mapDivideToSelf(T d)
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
 
 
- 
mapInv
public FieldVector<T> mapInv()
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
 
 
- 
mapInvToSelf
public FieldVector<T> mapInvToSelf()
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
 
 
- 
mapMultiply
public FieldVector<T> mapMultiply(T d)
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
 
 
- 
mapMultiplyToSelf
public FieldVector<T> mapMultiplyToSelf(T d)
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
 
 
- 
mapSubtract
public FieldVector<T> mapSubtract(T d)
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
 
 
- 
mapSubtractToSelf
public FieldVector<T> mapSubtractToSelf(T d)
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
 
 
- 
outerProduct
public FieldMatrix<T> outerProduct(SparseFieldVector<T> v) throws java.lang.IllegalArgumentException
Optimized method to compute outer product when both vectors are sparse.- Parameters:
 v- vector with which outer product should be computed- Returns:
 - the square matrix outer product between instance and v
 - Throws:
 java.lang.IllegalArgumentException- if v is not the same size asthis
 
- 
outerProduct
public FieldMatrix<T> outerProduct(T[] v) throws java.lang.IllegalArgumentException
Compute the outer product.- Specified by:
 outerProductin interfaceFieldVector<T extends FieldElement<T>>- Parameters:
 v- vector with which outer product should be computed- Returns:
 - the square matrix outer product between instance and v
 - Throws:
 java.lang.IllegalArgumentException- if v is not the same size as this
 
- 
outerProduct
public FieldMatrix<T> outerProduct(FieldVector<T> v) throws java.lang.IllegalArgumentException
Compute the outer product.- Specified by:
 outerProductin interfaceFieldVector<T extends FieldElement<T>>- Parameters:
 v- vector with which outer product should be computed- Returns:
 - the square matrix outer product between instance and v
 - Throws:
 java.lang.IllegalArgumentException- if v is not the same size as this
 
- 
projection
public FieldVector<T> projection(FieldVector<T> v) throws java.lang.IllegalArgumentException
Find the orthogonal projection of this vector onto another vector.- Specified by:
 projectionin interfaceFieldVector<T extends FieldElement<T>>- Parameters:
 v- vector onto which instance must be projected- Returns:
 - projection of the instance onto v
 - Throws:
 java.lang.IllegalArgumentException- if v is not the same size as this
 
- 
projection
public FieldVector<T> projection(T[] v) throws java.lang.IllegalArgumentException
Find the orthogonal projection of this vector onto another vector.- Specified by:
 projectionin interfaceFieldVector<T extends FieldElement<T>>- Parameters:
 v- vector onto which instance must be projected- Returns:
 - projection of the instance onto v
 - Throws:
 java.lang.IllegalArgumentException- if v is not the same size as this
 
- 
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
 
- 
setEntry
public void setEntry(int index, T value) throws MatrixIndexExceptionSet a single element.- Specified by:
 setEntryin interfaceFieldVector<T extends FieldElement<T>>- Parameters:
 index- element index.value- new value for the element.- Throws:
 MatrixIndexException- if the index is inconsistent with vector size- See Also:
 FieldVector.getEntry(int)
 
- 
setSubVector
public void setSubVector(int index, FieldVector<T> v) throws MatrixIndexExceptionSet 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:
 MatrixIndexException- if the index is inconsistent with vector size- See Also:
 FieldVector.setSubVector(int, FieldElement[])
 
- 
setSubVector
public void setSubVector(int index, T[] v) throws MatrixIndexExceptionSet 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:
 MatrixIndexException- if the index is inconsistent with vector size- See Also:
 FieldVector.setSubVector(int, FieldVector)
 
- 
subtract
public SparseFieldVector<T> subtract(SparseFieldVector<T> v) throws java.lang.IllegalArgumentException
Optimized method to subtract SparseRealVectors.- Parameters:
 v- The vector to subtract fromthis- Returns:
 - The difference of 
thisandv - Throws:
 java.lang.IllegalArgumentException- If the dimensions don't match
 
- 
subtract
public FieldVector<T> subtract(FieldVector<T> v) throws java.lang.IllegalArgumentException
Compute this minus v.- Specified by:
 subtractin interfaceFieldVector<T extends FieldElement<T>>- Parameters:
 v- vector to be subtracted- Returns:
 - this + v
 - Throws:
 java.lang.IllegalArgumentException- if v is not the same size as this
 
- 
subtract
public FieldVector<T> subtract(T[] v) throws java.lang.IllegalArgumentException
Compute this minus v.- Specified by:
 subtractin interfaceFieldVector<T extends FieldElement<T>>- Parameters:
 v- vector to be subtracted- Returns:
 - this + v
 - Throws:
 java.lang.IllegalArgumentException- if v is not the same size as this
 
- 
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
 
 
- 
add
public FieldVector<T> add(FieldVector<T> v) throws java.lang.IllegalArgumentException
Compute the sum of this and v.- Specified by:
 addin interfaceFieldVector<T extends FieldElement<T>>- Parameters:
 v- vector to be added- Returns:
 - this + v
 - Throws:
 java.lang.IllegalArgumentException- if v is not the same size as this
 
- 
hashCode
public int hashCode()
- Overrides:
 hashCodein classjava.lang.Object
 
- 
equals
public boolean equals(java.lang.Object obj)
- Overrides:
 equalsin classjava.lang.Object
 
 - 
 
 -