mpv2
Class SparseVector

java.lang.Object
  extended by mpv2.SparseVector
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable

public class SparseVector
extends java.lang.Object
implements java.lang.Cloneable, java.io.Serializable

This class is a sparse vector implementation.

The class SparseVector class stores only the non-zero elements of the vector. Read access is fast, but write access (set a new non-zero element) need to change the whole vector.

See Also:
Serialized Form

Constructor Summary
SparseVector(double[] v)
          Construct a sparse vector from an array.
SparseVector(int n)
          Construct a length n sparse vector of zeros.
SparseVector(int n, int[] ind, double[] vals)
          Construct a length n sparse vector with non-zero indices and values supplied.
 
Method Summary
 void addToArray(double factor, double[] x)
          Add this sparse vector multiplied by a factor to the given array.
 java.lang.Object clone()
          Clone the SparseVector object.
 SparseVector copy()
          Make a deep copy of a SparseVector.
 double[] get()
          Get the whole vector as an array
 double get(int n)
          Get a vector element.
 int[] getIndices()
          Access the non-zero indices
 int getLength()
          Returns the lengt of the sparse vector
 int getN()
          Returns the lengt of the sparse vector
 int getNonZeroCount()
          Returns number of non-zeros elements in sparse matrix.
 double[] getSubVector(int i0, int i1)
          Get a subvector as an array
 double[] getValues()
          Access the non-zero values
 double innerProduct()
          innerProduct of this sparse vector to itself
 double innerProduct(double[] v)
          innerProduct of this and an array if lengths do not match, the shortest vector is extended with zeros
 double innerProduct(SparseVector v)
          innerProduct of this and another sparse vector if lengths do not match, the shortest vector is extended with zeros
 int norm0()
          The pseudo-zero-norm, do not count any zeros stored (which may happen)
 double norm1()
          One norm
 double norm2()
          Two norm
 double normInf()
          Inf norm
 void print()
          Print the sparse vector to stdout.
 void print(int w, int d)
          Print the sparse vector to stdout.
 void print(java.text.NumberFormat format, int width)
          Print the sparse vector to stdout.
 void print(java.io.PrintWriter output, int w, int d)
          Print the sparse vector to stdout.
 void print(java.io.PrintWriter output, java.text.NumberFormat format, int width)
          Print the sparse vector to stdout.
 void set(int n, double val)
          Set a vector element.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SparseVector

public SparseVector(int n)
Construct a length n sparse vector of zeros.

Parameters:
n - length of vector

SparseVector

public SparseVector(double[] v)
Construct a sparse vector from an array.

Parameters:
v - array of doubles.

SparseVector

public SparseVector(int n,
                    int[] ind,
                    double[] vals)
Construct a length n sparse vector with non-zero indices and values supplied. If some indices are equal, then one of the corresponding values are used

Parameters:
n - length of vector
ind - indices for the non-zero values
vals - the non-zero values
Method Detail

copy

public SparseVector copy()
Make a deep copy of a SparseVector.


clone

public java.lang.Object clone()
Clone the SparseVector object.

Overrides:
clone in class java.lang.Object

get

public double[] get()
Get the whole vector as an array

Returns:
vector v of length N

getSubVector

public double[] getSubVector(int i0,
                             int i1)
Get a subvector as an array

Returns:
vector v of length (i1-i0+1)

get

public double get(int n)
Get a vector element.

Parameters:
n - index number
Returns:
vector value v[n]

getValues

public double[] getValues()
Access the non-zero values

Returns:
pointer to value array

getIndices

public int[] getIndices()
Access the non-zero indices

Returns:
pointer to index array

set

public void set(int n,
                double val)
Set a vector element.

Parameters:
n - index number
val - the value to be put into the given entry of the dictionary.
Throws:
java.lang.ArrayIndexOutOfBoundsException

getN

public int getN()
Returns the lengt of the sparse vector


getLength

public int getLength()
Returns the lengt of the sparse vector


getNonZeroCount

public int getNonZeroCount()
Returns number of non-zeros elements in sparse matrix.


addToArray

public void addToArray(double factor,
                       double[] x)
Add this sparse vector multiplied by a factor to the given array.

Parameters:
factor - a factor to multiply the column vector by.
x - an array

norm0

public int norm0()
The pseudo-zero-norm, do not count any zeros stored (which may happen)

Returns:
number of non-zero values

norm1

public double norm1()
One norm

Returns:
sum of absulute values

norm2

public double norm2()
Two norm

Returns:
sum of squared values

normInf

public double normInf()
Inf norm

Returns:
maximum absulute value

innerProduct

public double innerProduct()
innerProduct of this sparse vector to itself

Returns:
innerProduct

innerProduct

public double innerProduct(double[] v)
innerProduct of this and an array if lengths do not match, the shortest vector is extended with zeros

Returns:
innerProduct

innerProduct

public double innerProduct(SparseVector v)
innerProduct of this and another sparse vector if lengths do not match, the shortest vector is extended with zeros

Returns:
innerProduct

print

public void print()
Print the sparse vector to stdout.


print

public void print(int w,
                  int d)
Print the sparse vector to stdout. Fortran-like 'Fw.d' style format.

Parameters:
w - Column width.
d - Number of digits after the decimal.

print

public void print(java.io.PrintWriter output,
                  int w,
                  int d)
Print the sparse vector to stdout. Fortran-like 'Fw.d' style format.

Parameters:
output - Output stream.
w - Column width.
d - Number of digits after the decimal.

print

public void print(java.text.NumberFormat format,
                  int width)
Print the sparse vector to stdout. Use the format object, and right justify within columns of width characters. Note that if the sparse vector is to be read back in, you probably will want to use a NumberFormat that is set to US Locale.

Parameters:
format - A Formatting object for individual elements.
width - Field width for each column.
See Also:
DecimalFormat.setDecimalFormatSymbols(java.text.DecimalFormatSymbols)

print

public void print(java.io.PrintWriter output,
                  java.text.NumberFormat format,
                  int width)
Print the sparse vector to stdout. Use the format object, and right justify within columns of width characters. Note that if the sparse vector is to be read back in, you probably will want to use a NumberFormat that is set to US Locale.

Parameters:
output - the output stream.
format - A formatting object to format the matrix elements
width - Column width.
See Also:
DecimalFormat.setDecimalFormatSymbols(java.text.DecimalFormatSymbols)