org.mymedialite.datatype
Class Matrix<T>

java.lang.Object
  extended by org.mymedialite.datatype.Matrix<T>
Type Parameters:
T - the type of the matrix entries
All Implemented Interfaces:
IMatrix<T>

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

Class for storing dense matrices. The data is stored in row-major mode. Indexes are zero-based.


Field Summary
 java.lang.Object[] data
          Data array: data is stored in columns.
 int dim1
          Dimension 1, the number of rows
 int dim2
          Dimension 2, the number of columns
 
Constructor Summary
Matrix(int dim1, int dim2)
          Initializes a new instance of the Matrix class
Matrix(int dim1, int dim2, T d)
          Initializes a new instance of the Matrix class
Matrix(java.util.List<java.util.List<T>> data)
          Constructor that takes a list of lists to initialize the matrix.
Matrix(Matrix<T> matrix)
          Copy constructor.
 
Method Summary
 void addRows(int num_rows)
          Enlarges the matrix to num_rows rows Do nothing if num_rows is less than dim1.
 IMatrix<T> createMatrix(int num_rows, int num_columns)
          Create a matrix with a given number of rows and columns.
 T get(int i, int j)
          Get the value at (i,j)
 java.util.List<T> getColumn(int j)
          Returns a copy of the j-th column of the matrix
 java.util.List<T> getRow(int i)
          Returns a copy of the i-th row of the matrix
 void grow(int num_rows, int num_cols)
          Grows the matrix to the requested size, if necessary The new entries are filled with zeros.
 boolean isSymmetric()
          True if the matrix is symmetric, false otherwise.
 int numberOfColumns()
          Get the number of columns of the matrix.
 int numberOfRows()
          Get the number of rows of the matrix.
 void set(int i, int j, T value)
          Set the value at (i,j)
 void setColumn(int j, java.util.List<T> column)
          Sets the values of the j-th column to the values in a given array
 void setColumnToOneValue(int j, T v)
          Sets an entire column to a specified value
 void setRow(int i, java.util.List<T> row)
          Sets the values of the i-th row to the values in a given array
 void setRowToOneValue(int i, T v)
          Sets an entire row to a specified value
 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

data

public java.lang.Object[] data
Data array: data is stored in columns.


dim1

public int dim1
Dimension 1, the number of rows


dim2

public int dim2
Dimension 2, the number of columns

Constructor Detail

Matrix

public Matrix(int dim1,
              int dim2)
Initializes a new instance of the Matrix class

Parameters:
dim1 - the number of rows
dim2 - the number of columns

Matrix

public Matrix(int dim1,
              int dim2,
              T d)
Initializes a new instance of the Matrix class

Parameters:
dim1 - the number of rows
dim2 - the number of columns
d - the default value for the elements

Matrix

public Matrix(Matrix<T> matrix)
Copy constructor. Creates a deep copy of the given matrix.

Parameters:
matrix - the matrix to be copied

Matrix

public Matrix(java.util.List<java.util.List<T>> data)
Constructor that takes a list of lists to initialize the matrix.

Parameters:
data - a list of lists of T
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

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)

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

get

public T get(int i,
             int j)
Description copied from interface: IMatrix
Get the value at (i,j)

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

set

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

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

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

getRow

public java.util.List<T> getRow(int i)
Returns a copy of the i-th row of the matrix

Parameters:
i - the row ID
Returns:
a List containing the row data

getColumn

public java.util.List<T> getColumn(int j)
Returns a copy of the j-th column of the matrix

Parameters:
j - the column ID
Returns:
T[] containing the column data

setRow

public void setRow(int i,
                   java.util.List<T> row)
Sets the values of the i-th row to the values in a given array

Parameters:
i - the row ID
row - A of length dim1

setColumn

public void setColumn(int j,
                      java.util.List<T> column)
Sets the values of the j-th column to the values in a given array

Parameters:
j - the column ID
column - A T[] of length dim2

addRows

public void addRows(int num_rows)
Enlarges the matrix to num_rows rows Do nothing if num_rows is less than dim1. The new entries are filled with zeros.

Parameters:
num_rows - the minimum number of rows

grow

public void grow(int num_rows,
                 int num_cols)
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

setRowToOneValue

public void setRowToOneValue(int i,
                             T v)
Sets an entire row to a specified value

Parameters:
v - the value to be used
i - the row ID

setColumnToOneValue

public void setColumnToOneValue(int j,
                                T v)
Sets an entire column to a specified value

Parameters:
v - the value to be used
j - the column ID