Class SparseVector
- java.lang.Object
-
- dev.nm.algebra.linear.matrix.doubles.matrixtype.sparse.SparseVector
-
- All Implemented Interfaces:
SparseStructure,Vector,AbelianGroup<Vector>,BanachSpace<Vector,Real>,HilbertSpace<Vector,Real>,VectorSpace<Vector,Real>,DeepCopyable,Iterable<SparseVector.Entry>
- Direct Known Subclasses:
Basis
public class SparseVector extends Object implements Vector, SparseStructure, Iterable<SparseVector.Entry>
A sparse vector stores only non-zero values.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classSparseVector.EntryThis is an entry in aSparseVector.static classSparseVector.IteratorThis wrapper class overrides theIterator.remove()method to throw an exception when called.
-
Constructor Summary
Constructors Constructor Description SparseVector(double... v)Constructs a sparse vector from adouble[].SparseVector(int size)Constructs a sparse vector.SparseVector(int size, int[] indices, double[] values)Constructs a sparse vector.SparseVector(int size, Collection<SparseVector.Entry> entries)Constructs a sparse vector.SparseVector(SparseVector that)Copy constructor.SparseVector(Vector v)Constructs a sparse vector from a vector.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Vectoradd(double c)Add a constant to all entries in this vector.SparseVectoradd(SparseVector that)Vectoradd(Vector that)\(this + that\)doubleangle(Vector that)Measure the angle, \(\theta\), betweenthisandthat.SparseVectordeepCopy()The implementation returns an instance created fromthisby the copy constructor of the class, or justthisif the instance itself is immutable.Vectordivide(Vector that)Dividethisbythat, entry-by-entry.booleanequals(Object obj)doubleget(int index)Get the value at position i.inthashCode()doubleinnerProduct(SparseVector that)doubleinnerProduct(Vector that)Inner product in the Euclidean space is the dot product.Iterator<SparseVector.Entry>iterator()VectorleftMultiply(Matrix A)Left multiplies a matrix.Vectorminus(double c)Subtract a constant from all entries in this vector.SparseVectorminus(SparseVector that)Vectorminus(Vector that)\(this - that\)SparseVectormultiply(SparseVector that)SparseVectormultiply(Vector that)Multiplythisbythat, entry-by-entry.intnNonZeros()Get the number of non-zero entries in the structure.doublenorm()Compute the length or magnitude or Euclidean norm of a vector, namely, \(\|v\|\).doublenorm(double p)Gets the \(L^p\)-norm \(\|v\|_p\) of this vector.SparseVectoropposite()Get the opposite of this vector.Vectorpow(double c)Take the exponentiation of all entries in this vector, entry-by-entry.SparseVectorscaled(double c)Scale this vector by a constant, entry-by-entry.SparseVectorscaled(Real c)Scale this vector by a constant, entry-by-entry.voidset(int index, double value)Change the value of an entry in this vector.intsize()Get the length of this vector.double[]toArray()Cast this vector into a 1Ddouble[].StringtoString()SparseVectorZERO()Get a 0-vector that has the same length as this vector.-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
-
-
-
Constructor Detail
-
SparseVector
public SparseVector(int size)
Constructs a sparse vector.- Parameters:
size- the size of the vector
-
SparseVector
public SparseVector(int size, int[] indices, double[] values)Constructs a sparse vector.- Parameters:
size- the size of the vectorindices- the indices of the non-zero valuesvalues- the non-zero values
-
SparseVector
public SparseVector(int size, Collection<SparseVector.Entry> entries)Constructs a sparse vector.- Parameters:
size- the size of the vectorentries- the sparse vector entries
-
SparseVector
public SparseVector(double... v)
Constructs a sparse vector from adouble[].- Parameters:
v- a vector
-
SparseVector
public SparseVector(Vector v)
Constructs a sparse vector from a vector.- Parameters:
v- a vector
-
SparseVector
public SparseVector(SparseVector that)
Copy constructor.- Parameters:
that- the vector to be copied
-
-
Method Detail
-
size
public int size()
Description copied from interface:VectorGet the length of this vector.
-
get
public double get(int index)
Description copied from interface:VectorGet the value at position i.
-
set
public void set(int index, double value)Description copied from interface:VectorChange the value of an entry in this vector. This is the only method that may change the entries of a vector.
-
iterator
public Iterator<SparseVector.Entry> iterator()
- Specified by:
iteratorin interfaceIterable<SparseVector.Entry>
-
add
public Vector add(Vector that)
Description copied from interface:Vector\(this + that\)- Specified by:
addin interfaceAbelianGroup<Vector>- Specified by:
addin interfaceVector- Parameters:
that- a vector- Returns:
- \(this + that\)
-
add
public SparseVector add(SparseVector that)
-
minus
public Vector minus(Vector that)
Description copied from interface:Vector\(this - that\)- Specified by:
minusin interfaceAbelianGroup<Vector>- Specified by:
minusin interfaceVector- Parameters:
that- a vector- Returns:
- \(this - that\)
-
minus
public SparseVector minus(SparseVector that)
-
multiply
public SparseVector multiply(Vector that)
Description copied from interface:VectorMultiplythisbythat, entry-by-entry.
-
multiply
public SparseVector multiply(SparseVector that)
-
leftMultiply
public Vector leftMultiply(Matrix A)
Left multiplies a matrix. That is, A * x, where A is the matrix and x is this sparse vector. This may speed up the operation as there are fewer multiplications.- Parameters:
A- the matrix on the left- Returns:
- the multiplication product
-
divide
public Vector divide(Vector that)
Description copied from interface:VectorDividethisbythat, entry-by-entry.
-
add
public Vector add(double c)
Description copied from interface:VectorAdd a constant to all entries in this vector.
-
minus
public Vector minus(double c)
Description copied from interface:VectorSubtract a constant from all entries in this vector.
-
innerProduct
public double innerProduct(Vector that)
Description copied from interface:VectorInner product in the Euclidean space is the dot product.- Specified by:
innerProductin interfaceHilbertSpace<Vector,Real>- Specified by:
innerProductin interfaceVector- Parameters:
that- a vector- Returns:
- \(this \cdot that\)
- See Also:
- Wikipedia: Dot product
-
innerProduct
public double innerProduct(SparseVector that)
-
pow
public Vector pow(double c)
Description copied from interface:VectorTake the exponentiation of all entries in this vector, entry-by-entry.
-
scaled
public SparseVector scaled(double c)
Description copied from interface:VectorScale this vector by a constant, entry-by-entry. Here is a way to get a unit version of the vector:vector.scaled(1. / vector.norm())
-
scaled
public SparseVector scaled(Real c)
Description copied from interface:VectorScale this vector by a constant, entry-by-entry. Here is a way to get a unit version of the vector:vector.scaled(1. / vector.norm())- Specified by:
scaledin interfaceVector- Specified by:
scaledin interfaceVectorSpace<Vector,Real>- Parameters:
c- a constant- Returns:
- \(c \times this\)
- See Also:
- Wikipedia: Scalar multiplication
-
opposite
public SparseVector opposite()
Description copied from interface:VectorGet the opposite of this vector.- Specified by:
oppositein interfaceAbelianGroup<Vector>- Specified by:
oppositein interfaceVector- Returns:
- -v
- See Also:
- Wikipedia: Additive inverse
-
norm
public double norm()
Description copied from interface:VectorCompute the length or magnitude or Euclidean norm of a vector, namely, \(\|v\|\).- Specified by:
normin interfaceBanachSpace<Vector,Real>- Specified by:
normin interfaceVector- Returns:
- the Euclidean norm
- See Also:
- Wikipedia: Norm (mathematics)
-
norm
public double norm(double p)
Description copied from interface:VectorGets the \(L^p\)-norm \(\|v\|_p\) of this vector.- When p is finite, \(\|v\|_p = \sum_{i}|v_i^p|^\frac{1}{p}\).
- When p is \(+\infty\) (
Double.POSITIVE_INFINITY), \(\|v\|_p = \max|v_i|\). - When p is \(-\infty\) (
Double.NEGATIVE_INFINITY), \(\|v\|_p = \min|v_i|\).
- Specified by:
normin interfaceVector- Parameters:
p- p ≥ 1, orDouble.POSITIVE_INFINITYorDouble.NEGATIVE_INFINITY- Returns:
- \(\|v\|_p\)
- See Also:
- Wikipedia: Norm (mathematics)
-
angle
public double angle(Vector that)
Description copied from interface:VectorMeasure the angle, \(\theta\), betweenthisandthat. That is, \[ this \cdot that = \|this\| \times \|that\| \times \cos \theta \]
-
ZERO
public SparseVector ZERO()
Description copied from interface:VectorGet a 0-vector that has the same length as this vector.- Specified by:
ZEROin interfaceAbelianGroup<Vector>- Specified by:
ZEROin interfaceVector- Returns:
- the 0-vector
-
toArray
public double[] toArray()
Description copied from interface:VectorCast this vector into a 1Ddouble[].
-
deepCopy
public SparseVector deepCopy()
Description copied from interface:DeepCopyableThe implementation returns an instance created fromthisby the copy constructor of the class, or justthisif the instance itself is immutable.- Specified by:
deepCopyin interfaceDeepCopyable- Specified by:
deepCopyin interfaceVector- Returns:
- an independent (deep) copy of the instance
-
nNonZeros
public int nNonZeros()
Description copied from interface:SparseStructureGet the number of non-zero entries in the structure.- Specified by:
nNonZerosin interfaceSparseStructure- Returns:
- the number of non-zero entries in the structure
-
-