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>

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

This class represents sparse vector which stores the non-zero values only.


Nested Class Summary
static class SparseVector.Entry
          This class represents an entry in a SparseVector.
static class SparseVector.Iterator
          This wrapper class overrides the Iterator.remove() method for throwing exception when it is called.
 
Constructor Summary
SparseVector(int size)
          Create an instance of sparse vector of the specified size.
SparseVector(int size, int[] indices, double[] values)
          Create an instance of sparse vector with non-zero values.
SparseVector(SparseVector that)
          Copy constructor.
 
Method Summary
 Vector add(double scalar)
          v + s Add a scalar to all entries in the vector v.
 Vector add(Vector that)
          this + that
 double angle(Vector that)
          Measure the angle between this and that.
 SparseVector deepCopy()
          The implementation can return 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)
          this / that Divide this by that, entry-by-entry.
 int dropTolerance(double tolerance)
          Remove non-zero entries x whose magnitude is less than or equal to the tolerance, i.e., (|x| <= tolerance).
 double get(int index)
          Get the value at position index.
 double innerProduct(Vector that)
          Inner product in the Euclidean space is the dot product.
 java.util.Iterator<SparseVector.Entry> iterator()
           
 Vector minus(double scalar)
          v - s Subtract a scalar from all entries in the vector v.
 Vector minus(Vector that)
          this - that
 Vector multiply(Vector v)
          this * that Multiply this by that, entry-by-entry.
 int nnz()
          Get the number of non-zero entries in the matrix.
 double norm()
          Compute the length or magnitude or Euclidean norm of a vector, namely, ||v||.
 double norm(int p)
          Compute the norm of a vector.
 Vector opposite()
          Get the opposite of this vector.
 Vector pow(double scalar)
          v ^ s Take the exponentiation of all entries in the vector v by a scalar.
 Vector scaled(double scalar)
          scalar * this Here is a way to get a unit version of the vector: vector.scaled(1. / vector.norm())
 Vector scaled(Real scalar)
          scalar * that If scalar is 1, it simply returns itself.
 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()
          Get a copy of all vector entries in the form of a 1D double[] array.
 java.lang.String toString()
           
 Vector 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)
Create an instance of sparse vector of the specified size.

Parameters:
size - the size of the vector

SparseVector

public SparseVector(int size,
                    int[] indices,
                    double[] values)
Create an instance of sparse vector with non-zero values.

Parameters:
size - the size of the vector
indices - the indices of the input 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 length of this vector

get

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

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

set

public void set(int index,
                double value)
Description copied from interface: Vector
Change the value of an entry in this vector.

The indices are, for example,: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ......

Note that we count from 1, NOT 0.

This is the only method that may change the entries of a vector.

Specified by:
set in interface Vector
Parameters:
index - the entry to change
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:
a vector which is the sum of this and 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:
a vector which is the difference between this and that

multiply

public Vector multiply(Vector v)
Description copied from interface: Vector
this * that

Multiply this by that, entry-by-entry.

Specified by:
multiply in interface Vector
Parameters:
v - a vector
Returns:
this * that, entry-by-entry

divide

public Vector divide(Vector that)
Description copied from interface: Vector
this / that

Divide this by that, entry-by-entry.

Specified by:
divide in interface Vector
Parameters:
that - a vector
Returns:
this / that, entry-by-entry

add

public Vector add(double scalar)
Description copied from interface: Vector
v + s

Add a scalar to all entries in the vector v.

Specified by:
add in interface Vector
Parameters:
scalar - s
Returns:
v + s

minus

public Vector minus(double scalar)
Description copied from interface: Vector
v - s

Subtract a scalar from all entries in the vector v.

Specified by:
minus in interface Vector
Parameters:
scalar - s
Returns:
v - s

innerProduct

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

Dot product is an operation which takes two vectors over the real numbers R and returns a real-valued scalar quantity.

Specified by:
innerProduct in interface HilbertSpace<Vector,Real>
Specified by:
innerProduct in interface Vector
Parameters:
that - a vector
Returns:
dot product of two vectors, this and that, i.e.,
this ∙ that
See Also:
Wikipedia: Dot product

pow

public Vector pow(double scalar)
Description copied from interface: Vector
v ^ s

Take the exponentiation of all entries in the vector v by a scalar.

Specified by:
pow in interface Vector
Parameters:
scalar - s
Returns:
v ^ s

scaled

public Vector scaled(double scalar)
Description copied from interface: Vector
scalar * this

Here is a way to get a unit version of the vector:

vector.scaled(1. / vector.norm())

Specified by:
scaled in interface Vector
Parameters:
scalar - a double scalar
Returns:
a vector which is scaled by scalar

scaled

public Vector scaled(Real scalar)
Description copied from interface: Vector
scalar * that

If scalar is 1, it simply returns itself. So, 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:
scalar - a Real number scalar
Returns:
a vector which is scaled by scalar
See Also:
Wikipedia: Scalar multiplication

opposite

public Vector 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
Compute the norm of a vector. When p is finite,
 |v|p = ∑ [(abs(vi)^p)]^(1/p)
 

When p is +∞ (Integer.MAX_VALUE),

 |v|p = max(abs(vi))
 

When p is -∞ (Integer.MIN_VALUE),

 |v|p = min(abs(vi))
 

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 between this and that.

That is,

this ∙ that = ||this|| * ||that|| cos(angle)

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

ZERO

public Vector 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:
a 0-vector, which has the same length as this vector

toArray

public double[] toArray()
Description copied from interface: Vector
Get a copy of all vector entries in the form of a 1D double[] array.

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 can return 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

nnz

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

Specified by:
nnz in interface SparseStructure
Returns:
the number of non-zero entries in the matrix

dropTolerance

public int dropTolerance(double tolerance)
Description copied from interface: SparseStructure
Remove non-zero entries x whose magnitude is less than or equal to the tolerance, i.e., (|x| <= tolerance).

Specified by:
dropTolerance in interface SparseStructure
Parameters:
tolerance - the tolerance for non-zeros
Returns:
the number of non-zeros removed

toString

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

SuanShu, a Java numerical and statistical library

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