mpv2
Class SparseMatrix

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

public class SparseMatrix
extends AllMatrices
implements java.lang.Cloneable, java.io.Serializable

This class is a sparse matrix implementation.

The class SparseMatrices contains stores the matrix elements in a way well suitable to sparse matrices, i.e. most of the entries are zero. For each non-zero element a structure with 5 fields are stored: the row index, the coluimn index, the value, a pointer to next in same row and a pointer to next in same column. Thus the sparsness factor, i.e. number of non-zero elements divided by total number of elements, should be lower that 0.2 to get any effect, and lower than 0.1 to get significant improvement related to the Matrix-class. Read access, especially to a row or column is effective, while write access, especially to elements which are zero, are slower.
There is a quite large number of methods available for matrices in general and in this class specially, see the AllMatrices class for a general overview. Here is a brief overview for added methods:

See Also:
Serialized Form

Field Summary
 
Fields inherited from class mpv2.AllMatrices
K, N
 
Constructor Summary
SparseMatrix(AllMatrices B)
          Construct a new matrix from another matrix (of any kind)
SparseMatrix(double[][] A)
          Construct a matrix from a 2-D array.
SparseMatrix(double[] vals, int m)
          Construct a matrix from a one-dimensional packed array
SparseMatrix(int m, int n)
          Construct an m-by-n sparse matrix of zeros.
 
Method Summary
 void addColumn(int k, double factor, double[] x)
          Add a column of the matrix multiplied by a factor to x, x = x + A[][k] Legal range of the integer argument is 0 <= k < K.
 double get(int n, int k)
          Get a single element.
 int getCapacity()
          Returns current size (capacity for non-zero values) in sparse matrix.
 double[] getColumn(int k)
          Copy a column of the matrix Legal range of the integer argument is 0 <= k < K.
 void getColumn(int k, double[] d)
          Sets argument d to a column of the matrix, i.e. a dictionary element or atom.
 double[] getColumnPackedCopy()
          Make a one-dimensional column packed copy of the matrix.
 int getNonZeroCount()
          Returns number of non-zeros elements in sparse matrix.
 double[] getRow(int n)
          Copy a row of the matrix Legal range of the integer argument is 0 <= n < N.
 void getRow(int n, double[] r)
          Copy a row of the matrix Legal range of the integer argument is 0 <= n < N.
 double[] getRowPackedCopy()
          Make a one-dimensional row packed copy of the matrix.
 double innerProduct(int k1, int k2)
          Returns the inner product of two dictionary elements, i.e. matrix column vectors.
 double normF()
          Frobenius norm
 void set(int n, int k, double val)
          Set an entry (a value) in the dictionary.
 void setAll(double[] vals)
          Set all entries of to matrix to the supplied new values If argument is wrong length an IllegalArgumentException is thrown.
 JamaMatrix times(AllMatrices B)
          Multiplies the matrix by an another matrix, return C = A * B
 void times(double[] y, double[] x)
          Multiplies the dictionary D by array y.
 JamaMatrix transposeTimes(AllMatrices C)
          Multiplies the transposed matrix by an another matrix, return B = A' * C
 void transposeTimes(double[] x, double[] y)
          Multiplies the transposed dictionary D' by array x.
 
Methods inherited from class mpv2.AllMatrices
chol, columnNorm0, columnNorm1, columnNorm2, columnNormInf, cond, det, eig, eqConstant, eqCopy, eqCopy, eqCopy, eqCopy, eqCopy, eqDifference, eqEProduct, eqEQuotient, eqIdentity, eqInverse, eqIProduct, eqNegate, eqOnes, eqPermuteColumns, eqPermuteRows, eqProduct, eqProduct, eqRandom, eqScaleColumns, eqScaleRows, eqSum, eqTProduct, eqTranspose, eqZeros, getAll, getAll, getColumnDimension, getK, getN, getRowDimension, getSubMatrix, getSubMatrix, getSubMatrix, getSubMatrix, getValue, lu, lup, norm1, norm2, normInf, pluseqOuterProduct, print, print, print, print, qr, rank, setColumn, setRow, setValue, sumAll, svd, times, times, times, trace, transposeTimes, transposeTimes, transposeTimes
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SparseMatrix

public SparseMatrix(int m,
                    int n)
Construct an m-by-n sparse matrix of zeros.

Parameters:
m - Number of rows.
n - Number of colums.

SparseMatrix

public SparseMatrix(double[][] A)
Construct a matrix from a 2-D array.

Parameters:
A - Two-dimensional array of doubles.
Throws:
java.lang.IllegalArgumentException - All rows must have the same length

SparseMatrix

public SparseMatrix(double[] vals,
                    int m)
Construct a matrix from a one-dimensional packed array

Parameters:
vals - One-dimensional array of doubles, packed by columns (ala Fortran).
m - Number of rows, i.e. length of columns.
Throws:
java.lang.IllegalArgumentException - Array length must be a multiple of m.

SparseMatrix

public SparseMatrix(AllMatrices B)
Construct a new matrix from another matrix (of any kind)

Parameters:
B - a matrix of any kind, class is a subclass of AllMatrices
Method Detail

get

public double get(int n,
                  int k)
Get a single element.

Specified by:
get in class AllMatrices
Parameters:
n - row number for the returned entry value.
k - column number for the returned entry value.
Returns:
A(n,k)
Throws:
java.lang.ArrayIndexOutOfBoundsException

set

public void set(int n,
                int k,
                double val)
Set an entry (a value) in the dictionary. Note that the row, i.e. position in the column, is given first, the second argument is the column number i.e. the number of the dictionary atom. We should have: 0 <= n < N and 0 <= k < K. If any argument is outside legal range nothing is done.

Specified by:
set in class AllMatrices
Parameters:
n - row number for the entry value to be changed.
k - column number for the entry value to be changed.
val - the value to be put into the given entry of the dictionary.

getNonZeroCount

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


getCapacity

public int getCapacity()
Returns current size (capacity for non-zero values) in sparse matrix.


getColumnPackedCopy

public double[] getColumnPackedCopy()
Make a one-dimensional column packed copy of the matrix.

Returns:
Matrix elements packed in a one-dimensional array by columns.

getRowPackedCopy

public double[] getRowPackedCopy()
Make a one-dimensional row packed copy of the matrix.

Returns:
Matrix elements packed in a one-dimensional array by rows.

setAll

public void setAll(double[] vals)
Set all entries of to matrix to the supplied new values If argument is wrong length an IllegalArgumentException is thrown.

Overrides:
setAll in class AllMatrices
Parameters:
vals - One-dimensional array of doubles, packed by columns (ala Fortran).

innerProduct

public double innerProduct(int k1,
                           int k2)
Returns the inner product of two dictionary elements, i.e. matrix column vectors. Legal range is 0 <= k1 < K and 0 <= k2 < K. If k1 or k2 are out of range, 0.0 should be returned.

Overrides:
innerProduct in class AllMatrices
Parameters:
k1 - number for the first dictionary element.
k2 - number for the second dictionary element.

getColumn

public double[] getColumn(int k)
Copy a column of the matrix Legal range of the integer argument is 0 <= k < K. If argument is out of range a length N array of zeros should be returned.
The corresponding Matlab expression would be: A(:,k+1).

Overrides:
getColumn in class AllMatrices
Parameters:
k - number of the column in the matrix
Returns:
the column

getColumn

public void getColumn(int k,
                      double[] d)
Sets argument d to a column of the matrix, i.e. a dictionary element or atom. Legal range of the integer argument is 0 <= k < K. If argument is out of range a length N array of zeros should be returned.
The corresponding Matlab expression would be: D(:,k+1).

Overrides:
getColumn in class AllMatrices
Parameters:
k - number of the column in dictionary, i.e. matrix D.
d - the given column of matrix D.

addColumn

public void addColumn(int k,
                      double factor,
                      double[] x)
Add a column of the matrix multiplied by a factor to x, x = x + A[][k] Legal range of the integer argument is 0 <= k < K.
The corresponding Matlab expression would be: x = x+f*A(:,k+1). Note that this function can not be used from Matlab, use getColumn(k) to get A(:,k+1) and calculate the new x in Matlab.

Overrides:
addColumn in class AllMatrices
Parameters:
k - number of the column the matrix A.
factor - a factor to multiply the column vector by.
x - an array of length N.

getRow

public double[] getRow(int n)
Copy a row of the matrix Legal range of the integer argument is 0 <= n < N. If argument is out of range a length K array of zeros should be returned.
The corresponding Matlab expression would be: A(n+1,:).

Overrides:
getRow in class AllMatrices
Parameters:
n - number of the row in the matrix A.
Returns:
the row

getRow

public void getRow(int n,
                   double[] r)
Copy a row of the matrix Legal range of the integer argument is 0 <= n < N. If argument is out of range a length K array of zeros should be returned.
The corresponding Matlab expression would be: r = A(n+1,:).

Overrides:
getRow in class AllMatrices
Parameters:
n - number of the row in the matrix A.
r - the given row of matrix A.

normF

public double normF()
Frobenius norm

Overrides:
normF in class AllMatrices
Returns:
sqrt of sum of squares of all elements.

transposeTimes

public void transposeTimes(double[] x,
                           double[] y)
Multiplies the transposed dictionary D' by array x. D is the matrix representing the dictionary, each column is a dictionary element. An array y is returned.
The dimensions are: D is N×K, x is N×1, and y is K×1.
The corresponding mulitplication in Matlab would be: y = D'*x.

Overrides:
transposeTimes in class AllMatrices
Parameters:
x - the signal (column vector) that is multiplied by the transposed dictionary.
y - the results as an array of length K.

times

public void times(double[] y,
                  double[] x)
Multiplies the dictionary D by array y. D is the matrix representing the dictionary, each column is a dictionary element. An array x is returned.
The dimensions are: D is N×K, x is N×1, and y is K×1.
The corresponding mulitplication in Matlab would be: x = D*y.

Overrides:
times in class AllMatrices
Parameters:
y - the coefficient vector that is multiplied by the dictionary.
x - the results as an array of length N.

times

public JamaMatrix times(AllMatrices B)
Multiplies the matrix by an another matrix, return C = A * B

Parameters:
B - another matrix
Returns:
Matrix product, A * B, as a JamaMatrix object
Throws:
java.lang.IllegalArgumentException - Matrix inner dimensions must agree.

transposeTimes

public JamaMatrix transposeTimes(AllMatrices C)
Multiplies the transposed matrix by an another matrix, return B = A' * C

Parameters:
C - another matrix
Returns:
Matrix product, A' * C, as a JamaMatrix object
Throws:
java.lang.IllegalArgumentException - Matrix inner dimensions must agree.