prea.datastructure
Class SparseMatrix

java.lang.Object
  extended by prea.datastructure.SparseMatrix

public class SparseMatrix
extends java.lang.Object

This class implements sparse matrix, containing empty values for most space.

Since:
2012. 3. 26
Version:
1.1
Author:
Joonseok Lee

Field Summary
private  SparseVector[] cols
          The array of column references.
private  int M
          The number of rows.
private  int N
          The number of columns.
private  SparseVector[] rows
          The array of row references.
 
Constructor Summary
SparseMatrix(int m, int n)
          Construct an empty sparse matrix, with a given size.
SparseMatrix(SparseMatrix sm)
          Construct an empty sparse matrix, with data copied from another sparse matrix.
 
Method Summary
 SparseMatrix add(double alpha)
          Scalar addition.
 double average()
          Average of every element.
 SparseMatrix cholesky()
          Calculate Cholesky decomposition of the matrix.
 SparseMatrix covariance()
          Generate a covariance matrix of the current matrix.
 SparseVector diagonal()
          Return items in the diagonal in vector form.
 SparseMatrix exp(double alpha)
          Exponential of a given constant.
 SparseVector getCol(int index)
          Return a copy of a given column.
 SparseVector getColRef(int index)
          Return a reference of a given column.
 SparseVector getRow(int index)
          Return a copy of a given row.
 SparseVector getRowRef(int index)
          Return a reference of a given row.
 double getValue(int i, int j)
          Retrieve a stored value from the given index.
 SparseMatrix inverse()
          Calculate inverse matrix.
 int itemCount()
          Actual number of items in the matrix.
 int[] length()
          Capacity of this matrix.
static SparseMatrix makeIdentity(int n)
          Generate an identity matrix with the given size.
 SparseMatrix partInverse(int[] indexList)
          Inverse of matrix only with indices in indexList.
 SparseMatrix partMinus(SparseMatrix B, int[] indexList)
          Matrix subtraction (A = A - B) only with indices in indexList.
 SparseMatrix partPlus(SparseMatrix B, int[] indexList)
          Matrix summation (A = A + B) only with indices in indexList.
 SparseMatrix partScale(double alpha, int[] indexList)
          Scalar Multiplication only with indices in indexList.
 SparseVector partTimes(SparseVector x, int[] indexList)
          Matrix-vector product (b = Ax) only with indices in indexList.
 SparseMatrix plus(SparseMatrix B)
          Matrix-matrix sum (C = A + B)
 SparseMatrix scale(double alpha)
          Scalar subtraction (aX).
 void selfAdd(double alpha)
          Scalar addition on the matrix itself.
 void selfScale(double alpha)
          Scalar subtraction (aX) on the matrix itself.
 void selfTimes(SparseMatrix B)
          Matrix-matrix product (A = AB), without using extra memory.
 void setSize(int m, int n)
          Set a new size of the matrix.
 void setValue(int i, int j, double value)
          Set a new value at the given index.
 double stdev()
          Standard Deviation of every element.
 SparseMatrix times(SparseMatrix B)
          Matrix-matrix product (C = AB)
 SparseVector times(SparseVector x)
          Matrix-vector product (b = Ax)
 DenseMatrix toDenseMatrix()
          Convert the matrix into the array-based dense representation.
 DenseMatrix toDenseSubset(int[] indexList)
          Convert the matrix into the array-based dense representation, but only with the selected indices.
 DenseMatrix toDenseSubset(int[] rowList, int[] colList)
          Convert the matrix into the array-based dense representation, but only with the selected indices, both rows and columns separately.
 java.lang.String toString()
          Convert the matrix to a printable string.
 SparseMatrix transpose()
          The transpose of the matrix.
 double variance()
          Variance of every element.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

M

private int M
The number of rows.


N

private int N
The number of columns.


rows

private SparseVector[] rows
The array of row references.


cols

private SparseVector[] cols
The array of column references.

Constructor Detail

SparseMatrix

public SparseMatrix(int m,
                    int n)
Construct an empty sparse matrix, with a given size.

Parameters:
m - The number of rows.
n - The number of columns.

SparseMatrix

public SparseMatrix(SparseMatrix sm)
Construct an empty sparse matrix, with data copied from another sparse matrix.

Parameters:
sm - The matrix having data being copied.
Method Detail

getValue

public double getValue(int i,
                       int j)
Retrieve a stored value from the given index.

Parameters:
i - The row index to retrieve.
j - The column index to retrieve.
Returns:
The value stored at the given index.

setValue

public void setValue(int i,
                     int j,
                     double value)
Set a new value at the given index.

Parameters:
i - The row index to store new value.
j - The column index to store new value.
value - The value to store.

getRowRef

public SparseVector getRowRef(int index)
Return a reference of a given row. Make sure to use this method only for read-only purpose.

Parameters:
index - The row index to retrieve.
Returns:
A reference to the designated row.

getRow

public SparseVector getRow(int index)
Return a copy of a given row. Use this if you do not want to affect to original data.

Parameters:
index - The row index to retrieve.
Returns:
A reference to the designated row.

getColRef

public SparseVector getColRef(int index)
Return a reference of a given column. Make sure to use this method only for read-only purpose.

Parameters:
index - The column index to retrieve.
Returns:
A reference to the designated column.

getCol

public SparseVector getCol(int index)
Return a copy of a given column. Use this if you do not want to affect to original data.

Parameters:
index - The column index to retrieve.
Returns:
A reference to the designated column.

toDenseMatrix

public DenseMatrix toDenseMatrix()
Convert the matrix into the array-based dense representation.

Returns:
The dense version of same matrix.

toDenseSubset

public DenseMatrix toDenseSubset(int[] indexList)
Convert the matrix into the array-based dense representation, but only with the selected indices.

Parameters:
indexList - The list of indices converting to dense matrix.
Returns:
The dense representation of same matrix, with given indices.

toDenseSubset

public DenseMatrix toDenseSubset(int[] rowList,
                                 int[] colList)
Convert the matrix into the array-based dense representation, but only with the selected indices, both rows and columns separately.

Parameters:
rowList - The list of row indices converting to dense matrix.
colList - The list of column indices converting to dense matrix.
Returns:
The dense representation of same matrix, with given indices.

length

public int[] length()
Capacity of this matrix.

Returns:
An array containing the length of this matrix. Index 0 contains row count, while index 1 column count.

itemCount

public int itemCount()
Actual number of items in the matrix.

Returns:
The number of items in the matrix.

setSize

public void setSize(int m,
                    int n)
Set a new size of the matrix.

Parameters:
m - The new row count.
n - The new column count.

diagonal

public SparseVector diagonal()
Return items in the diagonal in vector form.

Returns:
Diagonal vector from the matrix.

average

public double average()
Average of every element. It ignores non-existing values.

Returns:
The average value.

variance

public double variance()
Variance of every element. It ignores non-existing values.

Returns:
The variance value.

stdev

public double stdev()
Standard Deviation of every element. It ignores non-existing values.

Returns:
The standard deviation value.

scale

public SparseMatrix scale(double alpha)
Scalar subtraction (aX).

Parameters:
alpha - The scalar value to be multiplied to this matrix.
Returns:
The resulting matrix after scaling.

selfScale

public void selfScale(double alpha)
Scalar subtraction (aX) on the matrix itself. This is used for minimizing memory usage.

Parameters:
alpha - The scalar value to be multiplied to this matrix.

add

public SparseMatrix add(double alpha)
Scalar addition.

Parameters:
alpha - The scalar value to be added to this matrix.
Returns:
The resulting matrix after addition.

selfAdd

public void selfAdd(double alpha)
Scalar addition on the matrix itself.

Parameters:
alpha - The scalar value to be added to this matrix.

exp

public SparseMatrix exp(double alpha)
Exponential of a given constant.

Parameters:
alpha - The exponent.
Returns:
The resulting exponential matrix.

transpose

public SparseMatrix transpose()
The transpose of the matrix. This is simply implemented by interchanging row and column each other.

Returns:
The transpose of the matrix.

times

public SparseVector times(SparseVector x)
Matrix-vector product (b = Ax)

Parameters:
x - The vector to be multiplied to this matrix.
Returns:
The resulting vector after multiplication.
Throws:
java.lang.RuntimeException - when dimensions disagree

times

public SparseMatrix times(SparseMatrix B)
Matrix-matrix product (C = AB)

Parameters:
B - The matrix to be multiplied to this matrix.
Returns:
The resulting matrix after multiplication.
Throws:
java.lang.RuntimeException - when dimensions disagree

selfTimes

public void selfTimes(SparseMatrix B)
Matrix-matrix product (A = AB), without using extra memory.

Parameters:
B - The matrix to be multiplied to this matrix.
Throws:
java.lang.RuntimeException - when dimensions disagree

plus

public SparseMatrix plus(SparseMatrix B)
Matrix-matrix sum (C = A + B)

Parameters:
B - The matrix to be added to this matrix.
Returns:
The resulting matrix after summation.
Throws:
java.lang.RuntimeException - when dimensions disagree

makeIdentity

public static SparseMatrix makeIdentity(int n)
Generate an identity matrix with the given size.

Parameters:
n - The size of requested identity matrix.
Returns:
An identity matrix with the size of n by n.

inverse

public SparseMatrix inverse()
Calculate inverse matrix.

Returns:
The inverse of current matrix.
Throws:
java.lang.RuntimeException - when dimensions disagree.

cholesky

public SparseMatrix cholesky()
Calculate Cholesky decomposition of the matrix.

Returns:
The Cholesky decomposition result.
Throws:
java.lang.RuntimeException - when matrix is not square.

covariance

public SparseMatrix covariance()
Generate a covariance matrix of the current matrix.

Returns:
The covariance matrix of the current matrix.

partScale

public SparseMatrix partScale(double alpha,
                              int[] indexList)
Scalar Multiplication only with indices in indexList.

Parameters:
alpha - The scalar to be multiplied to this matrix.
indexList - The list of indices to be applied summation.
Returns:
The resulting matrix after scaling.

partPlus

public SparseMatrix partPlus(SparseMatrix B,
                             int[] indexList)
Matrix summation (A = A + B) only with indices in indexList.

Parameters:
B - The matrix to be added to this matrix.
indexList - The list of indices to be applied summation.
Returns:
The resulting matrix after summation.
Throws:
java.lang.RuntimeException - when dimensions disagree.

partMinus

public SparseMatrix partMinus(SparseMatrix B,
                              int[] indexList)
Matrix subtraction (A = A - B) only with indices in indexList.

Parameters:
B - The matrix to be subtracted from this matrix.
indexList - The list of indices to be applied subtraction.
Returns:
The resulting matrix after subtraction.
Throws:
java.lang.RuntimeException - when dimensions disagree.

partTimes

public SparseVector partTimes(SparseVector x,
                              int[] indexList)
Matrix-vector product (b = Ax) only with indices in indexList.

Parameters:
x - The vector to be multiplied to this matrix.
indexList - The list of indices to be applied multiplication.
Returns:
The resulting vector after multiplication.

partInverse

public SparseMatrix partInverse(int[] indexList)
Inverse of matrix only with indices in indexList.

Parameters:
indexList - The list of indices to be applied multiplication.
Returns:
The resulting inverse matrix.
Throws:
java.lang.RuntimeException - when dimensions disagree.

toString

public java.lang.String toString()
Convert the matrix to a printable string.

Overrides:
toString in class java.lang.Object
Returns:
The resulted string in the form of "(1, 2: 5.0) (2, 4: 4.5)"