idl.tmt.representation.matrix
Class TmtMatrix

java.lang.Object
  |
  +--idl.tmt.representation.matrix.TmtMatrix
All Implemented Interfaces:
java.io.Serializable

public class TmtMatrix
extends java.lang.Object
implements java.io.Serializable

Encapsulates matrix representation and manipulation via the colt library.

This class contains many constructors, allowing the initialization of matrix operations from a variety of data structures, allowing both sparse and dense representation.

To insulate users from the details of colt, a TmTMatrix contains functionality for basic data analysis. For example, the class can compute, store, and provide access to the SVD of the matrix it contains. Likewise it can pre- or post-multiply itself with another TmtMatrix.

Author:
miles
See Also:
Serialized Form

Field Summary
private  boolean isSparse
           
private  SingularValueDecomposition mySVD
           
protected  DoubleMatrix2D theMatrix
           
 
Constructor Summary
TmtMatrix(double[][] denseMatrix)
          constructor for a dense matrix from a 2D array of doubles
TmtMatrix(DoubleMatrix2D newMatrix)
          constructor to build a matrix from a colt-formatted version
TmtMatrix(int rows, int cols, java.util.ArrayList rowInd, java.util.ArrayList colInd, java.util.ArrayList values)
          constructor for a sparse matrix from data stored in memory
TmtMatrix(int rows, int cols, boolean sparse)
          constructor for a basic zero matrix (dense)
TmtMatrix(java.lang.String fileName, boolean sparse)
          constructor for reading a matrix (sparse or dense) from disk when we know the number of rows and columns beforehand
TmtMatrix(java.lang.String fileName, int rows, int cols, boolean sparse)
          constructor for reading a matrix (sparse or dense) from disk when we know the number of rows and columns beforehand
TmtMatrix(TmtMatrix m)
           
 
Method Summary
 TmtMatrix addMatrix(TmtMatrix b)
          returns the matrix sum of this matrix with another matrix
 int columnCount()
          access to the number of columns in the matrix
 TmtVector getColumn(int i)
          get a column from the matrix
 TmtMatrix getColumnCentered()
          returns column-centered version of the matrix
 TmtMatrix getColumnNormalized()
          returns column-normalized (unit length) version of the matrix
 TmtMatrix getColumns(int start, int end)
          construct a new matrix containing a subset of the columns of this matrix
 TmtMatrix getCorrelationMatrix()
          computes and returns the correlation matrix of this matrix
 TmtMatrix getLeftSingularVectors()
          returns a TmtMatrix containing the left singular vectors of this matrix
 DoubleMatrix2D getMatrix()
           
 TmtMatrix getRightSingularVectors()
          returns a TmtMatrix containing the right singular vectors of this matrix
 TmtVector getRow(int i)
          get a row from the matrix
 TmtMatrix getRows(int start, int end)
          construct a new matrix containing a subset of the columns of this matrix
 TmtMatrix getSingularValues()
          returns a diagonal TmtMatrix containing the singular values of this matrix
 double getValue(int row, int column)
           
 TmtMatrix postMultiplyBy(TmtMatrix b)
          postmultiply this matrix by another matrix B
 TmtMatrix preMultiplyBy(TmtMatrix b)
          premuliply this matrix by another matrix B
 int rowCount()
          access to the number of rows in the matrix
 TmtMatrix scalarMultiply(double d)
          returns the product of this matrix times a scalar
 void showMatrix()
          prints the matrix to stdout
 void svd()
          computes the singular value decomposition for this matrix, storing the results as a member of this matrix's class
 Instances toWekaInstances()
          converts this matrix into a set of Weka Instances for further processing.
 TmtMatrix transpose()
          creates a new matrix that is the transpose of this one
 double zSum()
          returns the sum of all elements of the matrix
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Field Detail

theMatrix

protected DoubleMatrix2D theMatrix

mySVD

private SingularValueDecomposition mySVD

isSparse

private boolean isSparse
Constructor Detail

TmtMatrix

public TmtMatrix(int rows,
                 int cols,
                 boolean sparse)
constructor for a basic zero matrix (dense)
Parameters:
rows -  
cols -  
sparse -  

TmtMatrix

public TmtMatrix(double[][] denseMatrix)
constructor for a dense matrix from a 2D array of doubles
Parameters:
denseMatrix -  

TmtMatrix

public TmtMatrix(int rows,
                 int cols,
                 java.util.ArrayList rowInd,
                 java.util.ArrayList colInd,
                 java.util.ArrayList values)
          throws BadDimensionException
constructor for a sparse matrix from data stored in memory
Parameters:
rows - the number of rows
cols - the number of columns
rowInd - an arrayList of row indices of nonzeros
colInd - an arrayList of column indicies of nonzeros
values - an arrayList of the nonzero values

TmtMatrix

public TmtMatrix(java.lang.String fileName,
                 int rows,
                 int cols,
                 boolean sparse)
constructor for reading a matrix (sparse or dense) from disk when we know the number of rows and columns beforehand
Parameters:
fileName -  
rows -  
cols -  
sparse -  

TmtMatrix

public TmtMatrix(java.lang.String fileName,
                 boolean sparse)
constructor for reading a matrix (sparse or dense) from disk when we know the number of rows and columns beforehand
Parameters:
fileName -  
rows -  
cols -  
sparse -  

TmtMatrix

public TmtMatrix(DoubleMatrix2D newMatrix)
constructor to build a matrix from a colt-formatted version
Parameters:
newMatrix - a matrix from colt

TmtMatrix

public TmtMatrix(TmtMatrix m)
Method Detail

showMatrix

public void showMatrix()
prints the matrix to stdout

rowCount

public int rowCount()
access to the number of rows in the matrix
Returns:
the total number of rows in the matrix

columnCount

public int columnCount()
access to the number of columns in the matrix
Returns:
the total number of columns in the matrix

transpose

public TmtMatrix transpose()
creates a new matrix that is the transpose of this one
Returns:
a NEW matrix initialized as the transpose of this matrix

getColumn

public TmtVector getColumn(int i)
get a column from the matrix
Parameters:
i - the (0-based) index of the column to retrieve
Returns:
a TmtVector containing the column

getRow

public TmtVector getRow(int i)
get a row from the matrix
Parameters:
i - the (0-based) index of the row to retrieve
Returns:
a TmtVector containing the row

getValue

public double getValue(int row,
                       int column)

getColumns

public TmtMatrix getColumns(int start,
                            int end)
construct a new matrix containing a subset of the columns of this matrix
Parameters:
start - the (0-indexed) starting point of the subset
end - the end point
Returns:
a TmtMatrix containing the desired columns

getRows

public TmtMatrix getRows(int start,
                         int end)
construct a new matrix containing a subset of the columns of this matrix
Parameters:
start - the (0-indexed) starting point of the subset
end - the end point
Returns:
a TmtMatrix containing the desired columns

getColumnCentered

public TmtMatrix getColumnCentered()
returns column-centered version of the matrix
Returns:
version of this matrix centered around column means

getColumnNormalized

public TmtMatrix getColumnNormalized()
returns column-normalized (unit length) version of the matrix
Returns:
version of this matrix with columns normalized to unit length

zSum

public double zSum()
returns the sum of all elements of the matrix
Returns:
the sum in double format

getMatrix

public DoubleMatrix2D getMatrix()

addMatrix

public TmtMatrix addMatrix(TmtMatrix b)
returns the matrix sum of this matrix with another matrix
Parameters:
b - the matrix to add
Returns:
the matrix sum

scalarMultiply

public TmtMatrix scalarMultiply(double d)
returns the product of this matrix times a scalar
Parameters:
d - the scalar to multiply by
Returns:
the product matrix

preMultiplyBy

public TmtMatrix preMultiplyBy(TmtMatrix b)
premuliply this matrix by another matrix B
Parameters:
b - the matrix to premultiply by
Returns:
the matrix product B . thisMatrix

postMultiplyBy

public TmtMatrix postMultiplyBy(TmtMatrix b)
postmultiply this matrix by another matrix B
Parameters:
b - the matrix to postmultiply by
Returns:
the matrix product thisMatrix . B

getCorrelationMatrix

public TmtMatrix getCorrelationMatrix()
computes and returns the correlation matrix of this matrix
Returns:
the p X p correlation matrix of this n X p matrix

svd

public void svd()
computes the singular value decomposition for this matrix, storing the results as a member of this matrix's class

getLeftSingularVectors

public TmtMatrix getLeftSingularVectors()
returns a TmtMatrix containing the left singular vectors of this matrix
Returns:
the left singular vectors in TmtFormat

getRightSingularVectors

public TmtMatrix getRightSingularVectors()
returns a TmtMatrix containing the right singular vectors of this matrix
Returns:
the right singular vectors in TmtFormat

getSingularValues

public TmtMatrix getSingularValues()
returns a diagonal TmtMatrix containing the singular values of this matrix
Returns:
the singular values in diagonal TmtFormat

toWekaInstances

public Instances toWekaInstances()
converts this matrix into a set of Weka Instances for further processing. This version of the method assumes that the attribute names are unknown, assigning them and the dataset name dummy values.
Returns:
an object of type Instances, with instances on the rows and attributes on the columns