SuanShu, a Java numerical and statistical library

com.numericalmethod.suanshu.matrix.doubles.matrixtype.sparse
Class SparseVector

java.lang.Object
  extended by com.numericalmethod.suanshu.matrix.doubles.matrixtype.sparse.SparseVector
All Implemented Interfaces:
DeepCopyable, AbelianGroup<Vector>, BanachSpace<Vector,Real>, HilbertSpace<Vector,Real>, VectorSpace<Vector,Real>, SparseStructure, Vector, java.lang.Iterable<SparseVector.Entry>
Direct Known Subclasses:
Basis

public class SparseVector
extends java.lang.Object
implements Vector, SparseStructure, java.lang.Iterable<SparseVector.Entry>

A sparse vector stores only non-zero values.


Nested Class Summary
static class SparseVector.Entry
          This is an entry in a SparseVector.
static class SparseVector.Iterator
          This wrapper class overrides the Iterator.remove() method to throw an exception when called.
 
Constructor Summary
SparseVector(int size)
          Construct a sparse vector.
SparseVector(int size, int[] indices, double[] values)
          Construct a sparse vector.
SparseVector(SparseVector that)
          Copy constructor.
 
Method Summary
 Vector add(double c)
          Add a constant to all entries in this vector.
 SparseVector add(SparseVector that)
           
 Vector add(Vector that)
          \(this + that\)
 double angle(Vector that)
          Measure the angle, \(\theta\), between this and that.
 SparseVector deepCopy()
          The implementation returns an instance created from this by the copy constructor of the class, or just this if the instance itself is immutable.
 Vector divide(Vector that)
          Divide this by that, entry-by-entry.
 double get(int index)
          Get the value at position i.
 double innerProduct(SparseVector that)
           
 double innerProduct(Vector that)
          Inner product in the Euclidean space is the dot product.
 java.util.Iterator<SparseVector.Entry> iterator()
           
 Vector minus(double c)
          Subtract a constant from all entries in this vector.
 SparseVector minus(SparseVector that)
           
 Vector minus(Vector that)
          \(this - that\)
 SparseVector multiply(SparseVector that)
           
 SparseVector multiply(Vector that)
          Multiply this by that, entry-by-entry.
 int nNonZeros()
          Get the number of non-zero entries in the structure.
 double norm()
          Compute the length or magnitude or Euclidean norm of a vector, namely, \(\|v\|\).
 double norm(int p)
          Get the norm of a vector.
 SparseVector opposite()
          Get the opposite of this vector.
 Vector pow(double c)
          Take the exponentiation of all entries in this vector, entry-by-entry.
 SparseVector scaled(double c)
          Scale this vector by a constant, entry-by-entry.
 SparseVector scaled(Real c)
          Scale this vector by a constant, entry-by-entry.
 void set(int index, double value)
          Change the value of an entry in this vector.
 int size()
          Get the length of this vector.
 double[] toArray()
          Cast this vector into a 1D double[].
 java.lang.String toString()
           
 SparseVector ZERO()
          Get a 0-vector that has the same length as this vector.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

SparseVector

public SparseVector(int size)
Construct a sparse vector.

Parameters:
size - the size of the vector

SparseVector

public SparseVector(int size,
                    int[] indices,
                    double[] values)
Construct a sparse vector.

Parameters:
size - the size of the vector
indices - the indices of the non-zero values
values - the non-zero values

SparseVector

public SparseVector(SparseVector that)
Copy constructor.

Parameters:
that - the vector to be copied
Method Detail

size

public int size()
Description copied from interface: Vector
Get the length of this vector.

Specified by:
size in interface Vector
Returns:
the vector length

get

public double get(int index)
Description copied from interface: Vector
Get the value at position i.

Specified by:
get in interface Vector
Parameters:
index - the position of a vector entry
Returns:
v[i]

set

public void set(int index,
                double value)
Description copied from interface: Vector
Change the value of an entry in this vector. This is the only method that may change the entries of a vector.

Specified by:
set in interface Vector
Parameters:
index - the index of the entry to change. The indices are counting from 1, NOT 0.
value - the value to change to

iterator

public java.util.Iterator<SparseVector.Entry> iterator()
Specified by:
iterator in interface java.lang.Iterable<SparseVector.Entry>

add

public Vector add(Vector that)
Description copied from interface: Vector
\(this + that\)

Specified by:
add in interface AbelianGroup<Vector>
Specified by:
add in interface Vector
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:
minus in interface AbelianGroup<Vector>
Specified by:
minus in interface Vector
Parameters:
that - a vector
Returns:
\(this - that\)

minus

public SparseVector minus(SparseVector that)

multiply

public SparseVector multiply(Vector that)
Description copied from interface: Vector
Multiply this by that, entry-by-entry.

Specified by:
multiply in interface Vector
Parameters:
that - a vector
Returns:
\(this \cdot that\)

multiply

public SparseVector multiply(SparseVector that)

divide

public Vector divide(Vector that)
Description copied from interface: Vector
Divide this by that, entry-by-entry.

Specified by:
divide in interface Vector
Parameters:
that - a vector
Returns:
\(this / that\)

add

public Vector add(double c)
Description copied from interface: Vector
Add a constant to all entries in this vector.

Specified by:
add in interface Vector
Parameters:
c - a constant
Returns:
\(v + c\)

minus

public Vector minus(double c)
Description copied from interface: Vector
Subtract a constant from all entries in this vector.

Specified by:
minus in interface Vector
Parameters:
c - a constant
Returns:
\(v - c\)

innerProduct

public double innerProduct(Vector that)
Description copied from interface: Vector
Inner product in the Euclidean space is the dot product.

Specified by:
innerProduct in interface HilbertSpace<Vector,Real>
Specified by:
innerProduct in interface Vector
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: Vector
Take the exponentiation of all entries in this vector, entry-by-entry.

Specified by:
pow in interface Vector
Parameters:
c - a constant
Returns:
\(v ^ s\)

scaled

public SparseVector scaled(double c)
Description copied from interface: Vector
Scale 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:
scaled in interface Vector
Parameters:
c - a constant
Returns:
\(c \times this\)

scaled

public SparseVector scaled(Real c)
Description copied from interface: Vector
Scale 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:
scaled in interface VectorSpace<Vector,Real>
Specified by:
scaled in interface Vector
Parameters:
c - a constant
Returns:
\(c \times this\)
See Also:
Wikipedia: Scalar multiplication

opposite

public SparseVector opposite()
Description copied from interface: Vector
Get the opposite of this vector.

Specified by:
opposite in interface AbelianGroup<Vector>
Specified by:
opposite in interface Vector
Returns:
-v
See Also:
Wikipedia: Additive inverse

norm

public double norm()
Description copied from interface: Vector
Compute the length or magnitude or Euclidean norm of a vector, namely, \(\|v\|\).

Specified by:
norm in interface BanachSpace<Vector,Real>
Specified by:
norm in interface Vector
Returns:
the Euclidean norm
See Also:
Wikipedia: Norm (mathematics)

norm

public double norm(int p)
Description copied from interface: Vector
Get the norm of a vector.

Specified by:
norm in interface Vector
Parameters:
p - an integer, Integer.MAX_VALUE, or Integer.MIN_VALUE
Returns:
\(\|v\|_p\)

angle

public double angle(Vector that)
Description copied from interface: Vector
Measure the angle, \(\theta\), between this and that. That is, \[ this \cdot that = \|this\| \times \|that\| \times \cos \theta \]

Specified by:
angle in interface HilbertSpace<Vector,Real>
Specified by:
angle in interface Vector
Parameters:
that - a vector
Returns:
the angle, \(\theta\), between this and that

ZERO

public SparseVector ZERO()
Description copied from interface: Vector
Get a 0-vector that has the same length as this vector.

Specified by:
ZERO in interface AbelianGroup<Vector>
Specified by:
ZERO in interface Vector
Returns:
the 0-vector

toArray

public double[] toArray()
Description copied from interface: Vector
Cast this vector into a 1D double[].

Specified by:
toArray in interface Vector
Returns:
a copy of all vector entries as a double[]

deepCopy

public SparseVector deepCopy()
Description copied from interface: DeepCopyable
The implementation returns an instance created from this by the copy constructor of the class, or just this if the instance itself is immutable.

Specified by:
deepCopy in interface DeepCopyable
Specified by:
deepCopy in interface Vector
Returns:
an independent (deep) copy of the instance

nNonZeros

public int nNonZeros()
Description copied from interface: SparseStructure
Get the number of non-zero entries in the structure.

Specified by:
nNonZeros in interface SparseStructure
Returns:
the number of non-zero entries in the structure

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

SuanShu, a Java numerical and statistical library

Copyright © 2012 Numerical Method Inc. Ltd. All Rights Reserved.