sim.util.mantissa.linalg
Class Matrix

java.lang.Object
  extended by sim.util.mantissa.linalg.Matrix
All Implemented Interfaces:
java.io.Serializable
Direct Known Subclasses:
GeneralMatrix, SquareMatrix

public abstract class Matrix
extends java.lang.Object
implements java.io.Serializable

This class factor all services common to matrices.

This class is the base class of all matrix implementations, it is also the base class of the SquareMatrix class which adds methods specific to square matrices.

This class both handles the storage of matrix elements and implements the classical operations on matrices (addition, substraction, multiplication, transposition). It relies on two protected methods (getRangeForRow(int) and getRangeForColumn(int)) to get tight loop bounds for matrices with known structures full of zeros. These methods should be implemented by derived classes to provide information about their specific shape to the general algorithms implemented by this abstract class.

This file is from the "Mantissa" Java software package found at http://www.spaceroots.org/software/mantissa/index.html. The license is included at the end of the source file.

See Also:
Serialized Form

Field Summary
protected  int columns
          number of columns of the matrix.
protected  double[] data
          array of the matrix elements.
protected  int rows
          number of rows of the matrix.
 
Constructor Summary
protected Matrix(int rows, int columns)
          Simple constructor.
  Matrix(int rows, int columns, double[] data)
          Simple constructor.
protected Matrix(Matrix m)
          Copy constructor.
 
Method Summary
 Matrix add(Matrix m)
          Add a matrix to the instance.
abstract  Matrix duplicate()
          Polymorphic copy operator.
 int getColumns()
          Get the number of columns of the matrix.
 double getElement(int i, int j)
          Get a matrix element.
protected abstract  sim.util.mantissa.linalg.NonNullRange getRangeForColumn(int j)
          Set a range to the non null part covered by a column.
protected abstract  sim.util.mantissa.linalg.NonNullRange getRangeForRow(int i)
          Set a range to the non null part covered by a row.
 int getRows()
          Get the number of rows of the matrix.
 Matrix getTranspose()
          Compute the transpose of the instance.
 Matrix mul(double a)
          Multiply the instance by a scalar.
 Matrix mul(Matrix m)
          Multiply the instance by a matrix.
 void selfMul(double a)
          Multiply the instance by a scalar.
 void setElement(int i, int j, double value)
          Set a matrix element.
 Matrix sub(Matrix m)
          Substract a matrix from the instance.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

rows

protected final int rows
number of rows of the matrix.


columns

protected final int columns
number of columns of the matrix.


data

protected final double[] data
array of the matrix elements. the elements are stored in a one dimensional array, row after row

Constructor Detail

Matrix

protected Matrix(int rows,
                 int columns)
Simple constructor. Build a matrix with null elements.

Parameters:
rows - number of rows of the matrix
columns - number of columns of the matrix

Matrix

public Matrix(int rows,
              int columns,
              double[] data)
Simple constructor. Build a matrix with specified elements.

Parameters:
rows - number of rows of the matrix
columns - number of columns of the matrix
data - table of the matrix elements (stored row after row)

Matrix

protected Matrix(Matrix m)
Copy constructor.

Parameters:
m - matrix to copy
Method Detail

duplicate

public abstract Matrix duplicate()
Polymorphic copy operator. This method build a new object of the same type of the instance. It is somewhat similar to the Object.clone() method, except that it has public access, it doesn't throw any specific exception and it returns a Matrix.

See Also:
Object.clone()

getRows

public int getRows()
Get the number of rows of the matrix.

Returns:
number of rows
See Also:
getColumns()

getColumns

public int getColumns()
Get the number of columns of the matrix.

Returns:
number of columns
See Also:
getRows()

getElement

public double getElement(int i,
                         int j)
Get a matrix element.

Parameters:
i - row index, from 0 to rows - 1
j - column index, from 0 to cols - 1
Returns:
value of the element
Throws:
java.lang.ArrayIndexOutOfBoundsException - if the indices are wrong
See Also:
setElement(int, int, double)

setElement

public void setElement(int i,
                       int j,
                       double value)
Set a matrix element.

Parameters:
i - row index, from 0 to rows - 1
j - column index, from 0 to cols - 1
value - value of the element
Throws:
java.lang.ArrayIndexOutOfBoundsException - if the indices are wrong
See Also:
getElement(int, int)

add

public Matrix add(Matrix m)
Add a matrix to the instance. This method adds a matrix to the instance. It returns a new matrix and does not modify the instance.

Parameters:
m - matrix to add
Returns:
a new matrix containing the result
Throws:
java.lang.IllegalArgumentException - if there is a dimension mismatch

sub

public Matrix sub(Matrix m)
Substract a matrix from the instance. This method substracts a matrix from the instance. It returns a new matrix and does not modify the instance.

Parameters:
m - matrix to substract
Returns:
a new matrix containing the result
Throws:
java.lang.IllegalArgumentException - if there is a dimension mismatch

mul

public Matrix mul(Matrix m)
Multiply the instance by a matrix. This method multiplies the instance by a matrix. It returns a new matrix and does not modify the instance.

Parameters:
m - matrix by which to multiply
Returns:
a new matrix containing the result
Throws:
java.lang.IllegalArgumentException - if there is a dimension mismatch

mul

public Matrix mul(double a)
Multiply the instance by a scalar. This method multiplies the instance by a scalar. It returns a new matrix and does not modify the instance.

Parameters:
a - scalar by which to multiply
Returns:
a new matrix containing the result
See Also:
selfMul(double)

selfMul

public void selfMul(double a)
Multiply the instance by a scalar. This method multiplies the instance by a scalar. It does modify the instance.

Parameters:
a - scalar by which to multiply
See Also:
mul(double)

getTranspose

public Matrix getTranspose()
Compute the transpose of the instance. This method transposes the instance. It returns a new matrix and does not modify the instance.

Returns:
a new matrix containing the result

getRangeForRow

protected abstract sim.util.mantissa.linalg.NonNullRange getRangeForRow(int i)
Set a range to the non null part covered by a row.

Parameters:
i - index of the row
Returns:
range of non nul elements in the specified row
See Also:
getRangeForColumn(int)

getRangeForColumn

protected abstract sim.util.mantissa.linalg.NonNullRange getRangeForColumn(int j)
Set a range to the non null part covered by a column.

Parameters:
j - index of the column
Returns:
range of non nul elements in the specified column
See Also:
getRangeForRow(int)

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object