org.mymedialite.datatype
Class SparseMatrix<T>

java.lang.Object
  extended by org.mymedialite.datatype.SparseMatrix<T>
All Implemented Interfaces:
IMatrix<T>
Direct Known Subclasses:
SymmetricSparseMatrix

public class SparseMatrix<T>
extends java.lang.Object
implements IMatrix<T>

Class for storing sparse matrices. The data is stored in row-major mode. Indexes are zero-based. T the matrix element type, must have a default constructor/value


Field Summary
protected  java.util.List<java.util.HashMap<java.lang.Integer,T>> row_list
          List that stores the rows of the matrix.
 
Constructor Summary
SparseMatrix(int num_rows, int num_cols)
          Create a sparse matrix with a given number of rows.
SparseMatrix(int num_rows, int num_cols, T d)
          Create a sparse matrix with a given number of rows.
 
Method Summary
 IMatrix<T> createMatrix(int num_rows, int num_columns)
          Create a matrix with a given number of rows and columns.
 java.util.HashMap<java.lang.Integer,T> get(int x)
          Get a row of the matrix.
 T get(int x, int y)
          Access the elements of the sparse matrix.
 void grow(int num_rows, int num_cols)
          Grows the matrix to the requested size, if necessary.
 boolean isSymmetric()
          True if the matrix is symmetric, false otherwise.
 java.util.List<Pair<java.lang.Integer,java.lang.Integer>> nonEmptyEntryIDs()
          The row and column IDs of non-empty entries in the matrix.
 java.util.HashMap<java.lang.Integer,java.util.HashMap<java.lang.Integer,T>> nonEmptyRows()
          The non-empty rows of the matrix (the ones that contain at least one non-zero entry), with their IDs .
 int numberOfColumns()
          Get the number of columns of the matrix.
 int numberOfNonEmptyEntries()
          The number of non-empty entries in the matrix.
 int numberOfRows()
          Get the number of rows of the matrix.
 void set(int x, int y, T value)
          Set the value at (i,j)
 IMatrix<T> transpose()
          Get the transpose of the matrix, i.e.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

row_list

protected java.util.List<java.util.HashMap<java.lang.Integer,T>> row_list
List that stores the rows of the matrix.

Constructor Detail

SparseMatrix

public SparseMatrix(int num_rows,
                    int num_cols)
Create a sparse matrix with a given number of rows.

Parameters:
num_rows - the number of rows
num_cols - the number of columns

SparseMatrix

public SparseMatrix(int num_rows,
                    int num_cols,
                    T d)
Create a sparse matrix with a given number of rows.

Parameters:
num_rows - the number of rows
num_cols - the number of columns
d - the default value for elements
Method Detail

createMatrix

public IMatrix<T> createMatrix(int num_rows,
                               int num_columns)
Description copied from interface: IMatrix
Create a matrix with a given number of rows and columns.

Specified by:
createMatrix in interface IMatrix<T>
Parameters:
num_rows - the number of rows
num_columns - the number of columns
Returns:
a matrix with num_rows rows and num_column columns

isSymmetric

public boolean isSymmetric()
Description copied from interface: IMatrix
True if the matrix is symmetric, false otherwise.

Specified by:
isSymmetric in interface IMatrix<T>
Returns:
true if the matrix is symmetric, false otherwise

numberOfRows

public int numberOfRows()
Description copied from interface: IMatrix
Get the number of rows of the matrix.

Specified by:
numberOfRows in interface IMatrix<T>
Returns:
the number of rows of the matrix

numberOfColumns

public int numberOfColumns()
Description copied from interface: IMatrix
Get the number of columns of the matrix.

Specified by:
numberOfColumns in interface IMatrix<T>
Returns:
rhe number of columns of the matrix

transpose

public IMatrix<T> transpose()
Description copied from interface: IMatrix
Get the transpose of the matrix, i.e. a matrix where rows and columns are interchanged.

Specified by:
transpose in interface IMatrix<T>
Returns:
the transpose of the matrix (copy)

get

public java.util.HashMap<java.lang.Integer,T> get(int x)
Get a row of the matrix.

Parameters:
x - the row ID

get

public T get(int x,
             int y)
Access the elements of the sparse matrix.

Specified by:
get in interface IMatrix<T>
Parameters:
x - the row ID
y - the column ID
Returns:
the value at (i,j)

set

public void set(int x,
                int y,
                T value)
Description copied from interface: IMatrix
Set the value at (i,j)

Specified by:
set in interface IMatrix<T>
Parameters:
x - the row ID
y - the column ID
value - the value

nonEmptyRows

public java.util.HashMap<java.lang.Integer,java.util.HashMap<java.lang.Integer,T>> nonEmptyRows()
The non-empty rows of the matrix (the ones that contain at least one non-zero entry), with their IDs .


nonEmptyEntryIDs

public java.util.List<Pair<java.lang.Integer,java.lang.Integer>> nonEmptyEntryIDs()
The row and column IDs of non-empty entries in the matrix.

Returns:
The row and column IDs of non-empty entries in the matrix

numberOfNonEmptyEntries

public int numberOfNonEmptyEntries()
The number of non-empty entries in the matrix.

Returns:
The number of non-empty entries in the matrix

grow

public void grow(int num_rows,
                 int num_cols)
Description copied from interface: IMatrix
Grows the matrix to the requested size, if necessary. The new entries are filled with zeros.

Specified by:
grow in interface IMatrix<T>
Parameters:
num_rows - the minimum number of rows
num_cols - the minimum number of columns