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:
Serializable
,FieldVector<T>
public class SparseFieldVector<T extends FieldElement<T>>
extends Object
implements FieldVector<T>, Serializable
This class implements the
FieldVector
interface with a OpenIntToFieldHashMap
backing store.- Since:
- 2.0
- Version:
- $Revision: 983921 $ $Date: 2010-08-10 12:46:06 +0200 (mar. 10 août 2010) $
- See Also:
-
Constructor Summary
ConstructorsModifierConstructorDescriptionSparseFieldVector
(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.Copy constructor.protected
SparseFieldVector
(SparseFieldVector<T> v, int resize) Build a resized vector, for use with append. -
Method Summary
Modifier and TypeMethodDescriptionadd
(FieldVector<T> v) Compute the sum of this and v.add
(SparseFieldVector<T> v) Optimized method to add sparse vectors.Compute the sum of this and v.append
(FieldVector<T> v) Construct a vector by appending a vector to this vector.append
(SparseFieldVector<T> v) Construct a vector by appending a vector to this vector.Construct a vector by appending a T to this vector.Construct a vector by appending a T array to this vector.protected void
checkVectorDimensions
(int n) Check if instance dimension is equal to some expected value.copy()
Returns a (deep) copy of this.dotProduct
(FieldVector<T> v) Compute the dot product.dotProduct
(T[] v) Compute the dot product.ebeDivide
(FieldVector<T> v) Element-by-element division.Element-by-element division.ebeMultiply
(FieldVector<T> v) Element-by-element multiplication.ebeMultiply
(T[] v) Element-by-element multiplication.boolean
T[]
getData()
Returns vector entries as a T array.int
Returns the size of the vector.getEntry
(int index) Returns the entry in the specified index.getField()
Get the type of field elements of the vector.getSubVector
(int index, int n) Get a subvector from consecutive elements.int
hashCode()
Map an addition operation to each entry.mapAddToSelf
(T d) Map an addition operation to each entry.Map a division operation to each entry.mapDivideToSelf
(T d) Map a division operation to each entry.mapInv()
Map the 1/x function to each entry.Map the 1/x function to each entry.mapMultiply
(T d) Map a multiplication operation to each entry.Map a multiplication operation to each entry.mapSubtract
(T d) Map a subtraction operation to each entry.Map a subtraction operation to each entry.outerProduct
(FieldVector<T> v) Compute the outer product.Optimized method to compute outer product when both vectors are sparse.outerProduct
(T[] v) Compute the outer product.projection
(FieldVector<T> v) Find the orthogonal projection of this vector onto another vector.projection
(T[] v) Find the orthogonal projection of this vector onto another vector.void
Set all elements to a single value.void
Set a single element.void
setSubVector
(int index, FieldVector<T> v) Set a set of consecutive elements.void
setSubVector
(int index, T[] v) Set a set of consecutive elements.subtract
(FieldVector<T> v) Compute this minus v.Optimized method to subtract SparseRealVectors.Compute this minus v.T[]
toArray()
Convert the vector to a T array.
-
Constructor Details
-
SparseFieldVector
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 theappend
method (append(FieldElement)
,append(FieldElement[])
,append(FieldVector)
,append(SparseFieldVector)
) to gather data into this vector.- Parameters:
field
- field to which the elements belong
-
SparseFieldVector
Construct a (dimension)-length vector of zeros.- Parameters:
field
- field to which the elements belongdimension
- Size of the vector
-
SparseFieldVector
Build a resized vector, for use with append.- Parameters:
v
- The original vectorresize
- The amount to resize it
-
SparseFieldVector
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
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
Copy constructor.- Parameters:
v
- The instance to copy from
-
-
Method Details
-
add
Optimized method to add sparse vectors.- Parameters:
v
- vector to add- Returns:
- The sum of
this
andv
- Throws:
IllegalArgumentException
- If the dimensions don't match
-
add
Compute the sum of this and v.- Specified by:
add
in interfaceFieldVector<T extends FieldElement<T>>
- Parameters:
v
- vector to be added- Returns:
- this + v
- Throws:
IllegalArgumentException
- if v is not the same size as this
-
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 vector by appending a vector to this vector.- Specified by:
append
in interfaceFieldVector<T extends FieldElement<T>>
- Parameters:
v
- vector to append to this one.- Returns:
- a new vector
-
append
Construct a vector by appending a T to this vector.- Specified by:
append
in interfaceFieldVector<T extends FieldElement<T>>
- Parameters:
d
- T to append.- Returns:
- a new vector
-
append
Construct a vector by appending a T array to this vector.- Specified by:
append
in interfaceFieldVector<T extends FieldElement<T>>
- Parameters:
a
- T array to append.- Returns:
- a new vector
-
copy
Returns a (deep) copy of this.- Specified by:
copy
in interfaceFieldVector<T extends FieldElement<T>>
- Returns:
- vector copy
-
dotProduct
Compute the dot product.- Specified by:
dotProduct
in 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:
IllegalArgumentException
- if v is not the same size as this
-
dotProduct
Compute the dot product.- Specified by:
dotProduct
in 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:
IllegalArgumentException
- if v is not the same size as this
-
ebeDivide
Element-by-element division.- Specified by:
ebeDivide
in 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:
IllegalArgumentException
- if v is not the same size as this
-
ebeDivide
Element-by-element division.- Specified by:
ebeDivide
in 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:
IllegalArgumentException
- if v is not the same size as this
-
ebeMultiply
Element-by-element multiplication.- Specified by:
ebeMultiply
in 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:
IllegalArgumentException
- if v is not the same size as this
-
ebeMultiply
Element-by-element multiplication.- Specified by:
ebeMultiply
in 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:
IllegalArgumentException
- if v is not the same size as this
-
getData
Returns vector entries as a T array.- Specified by:
getData
in interfaceFieldVector<T extends FieldElement<T>>
- Returns:
- T array of entries
-
getDimension
public int getDimension()Returns the size of the vector.- Specified by:
getDimension
in interfaceFieldVector<T extends FieldElement<T>>
- Returns:
- size
-
getEntry
Returns the entry in the specified index.The index start at 0 and must be lesser than the size, otherwise a
MatrixIndexException
is thrown.- Specified by:
getEntry
in 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:
-
getField
Get the type of field elements of the vector.- Specified by:
getField
in interfaceFieldVector<T extends FieldElement<T>>
- Returns:
- type of field elements of the vector
-
getSubVector
Get a subvector from consecutive elements.- Specified by:
getSubVector
in 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
Map an addition operation to each entry.- Specified by:
mapAdd
in interfaceFieldVector<T extends FieldElement<T>>
- Parameters:
d
- value to be added to each entry- Returns:
- this + d
-
mapAddToSelf
Map an addition operation to each entry.The instance is changed by this method.
- Specified by:
mapAddToSelf
in interfaceFieldVector<T extends FieldElement<T>>
- Parameters:
d
- value to be added to each entry- Returns:
- for convenience, return this
-
mapDivide
Map a division operation to each entry.- Specified by:
mapDivide
in interfaceFieldVector<T extends FieldElement<T>>
- Parameters:
d
- value to divide all entries by- Returns:
- this / d
-
mapDivideToSelf
Map a division operation to each entry.The instance is changed by this method.
- Specified by:
mapDivideToSelf
in interfaceFieldVector<T extends FieldElement<T>>
- Parameters:
d
- value to divide all entries by- Returns:
- for convenience, return this
-
mapInv
Map the 1/x function to each entry.- Specified by:
mapInv
in interfaceFieldVector<T extends FieldElement<T>>
- Returns:
- a vector containing the result of applying the function to each entry
-
mapInvToSelf
Map the 1/x function to each entry.The instance is changed by this method.
- Specified by:
mapInvToSelf
in interfaceFieldVector<T extends FieldElement<T>>
- Returns:
- for convenience, return this
-
mapMultiply
Map a multiplication operation to each entry.- Specified by:
mapMultiply
in interfaceFieldVector<T extends FieldElement<T>>
- Parameters:
d
- value to multiply all entries by- Returns:
- this * d
-
mapMultiplyToSelf
Map a multiplication operation to each entry.The instance is changed by this method.
- Specified by:
mapMultiplyToSelf
in interfaceFieldVector<T extends FieldElement<T>>
- Parameters:
d
- value to multiply all entries by- Returns:
- for convenience, return this
-
mapSubtract
Map a subtraction operation to each entry.- Specified by:
mapSubtract
in interfaceFieldVector<T extends FieldElement<T>>
- Parameters:
d
- value to be subtracted to each entry- Returns:
- this - d
-
mapSubtractToSelf
Map a subtraction operation to each entry.The instance is changed by this method.
- Specified by:
mapSubtractToSelf
in interfaceFieldVector<T extends FieldElement<T>>
- Parameters:
d
- value to be subtracted to each entry- Returns:
- for convenience, return this
-
outerProduct
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:
IllegalArgumentException
- if v is not the same size asthis
-
outerProduct
Compute the outer product.- Specified by:
outerProduct
in 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:
IllegalArgumentException
- if v is not the same size as this
-
outerProduct
Compute the outer product.- Specified by:
outerProduct
in 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:
IllegalArgumentException
- if v is not the same size as this
-
projection
Find the orthogonal projection of this vector onto another vector.- Specified by:
projection
in interfaceFieldVector<T extends FieldElement<T>>
- Parameters:
v
- vector onto which instance must be projected- Returns:
- projection of the instance onto v
- Throws:
IllegalArgumentException
- if v is not the same size as this
-
projection
Find the orthogonal projection of this vector onto another vector.- Specified by:
projection
in interfaceFieldVector<T extends FieldElement<T>>
- Parameters:
v
- vector onto which instance must be projected- Returns:
- projection of the instance onto v
- Throws:
IllegalArgumentException
- if v is not the same size as this
-
set
Set all elements to a single value.- Specified by:
set
in interfaceFieldVector<T extends FieldElement<T>>
- Parameters:
value
- single value to set for all elements
-
setEntry
Set a single element.- Specified by:
setEntry
in 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:
-
setSubVector
Set a set of consecutive elements.- Specified by:
setSubVector
in 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:
-
setSubVector
Set a set of consecutive elements.- Specified by:
setSubVector
in 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:
-
subtract
Optimized method to subtract SparseRealVectors.- Parameters:
v
- The vector to subtract fromthis
- Returns:
- The difference of
this
andv
- Throws:
IllegalArgumentException
- If the dimensions don't match
-
subtract
Compute this minus v.- Specified by:
subtract
in interfaceFieldVector<T extends FieldElement<T>>
- Parameters:
v
- vector to be subtracted- Returns:
- this + v
- Throws:
IllegalArgumentException
- if v is not the same size as this
-
subtract
Compute this minus v.- Specified by:
subtract
in interfaceFieldVector<T extends FieldElement<T>>
- Parameters:
v
- vector to be subtracted- Returns:
- this + v
- Throws:
IllegalArgumentException
- if v is not the same size as this
-
toArray
Convert the vector to a T array.The array is independent from vector data, it's elements are copied.
- Specified by:
toArray
in interfaceFieldVector<T extends FieldElement<T>>
- Returns:
- array containing a copy of vector elements
-
checkVectorDimensions
Check if instance dimension is equal to some expected value.- Parameters:
n
- expected dimension.- Throws:
IllegalArgumentException
- if the dimension is inconsistent with vector size
-
add
Compute the sum of this and v.- Specified by:
add
in interfaceFieldVector<T extends FieldElement<T>>
- Parameters:
v
- vector to be added- Returns:
- this + v
- Throws:
IllegalArgumentException
- if v is not the same size as this
-
hashCode
public int hashCode() -
equals
-