csli.util.math.matrix
Class AbstractMatrix

java.lang.Object
  extended by java.util.AbstractCollection<Double>
      extended by csli.util.math.matrix.AbstractMatrix
All Implemented Interfaces:
Matrix, Iterable<Double>, Collection<Double>
Direct Known Subclasses:
ArrayMatrix

public abstract class AbstractMatrix
extends AbstractCollection<Double>
implements Matrix

Implements most Matrix methods. Derived classes only need to supply numRows, numCols, get, and (optionally) set.

Author:
jefe

Nested Class Summary
 
Nested classes/interfaces inherited from interface csli.util.math.matrix.Matrix
Matrix.SingularMatrixException
 
Constructor Summary
AbstractMatrix()
           
 
Method Summary
 Matrix add(Matrix M)
           
 Matrix addIP(Matrix M)
           
 Matrix appendRight(Matrix M)
          Create a new matrix which contains the original matrix A and the argument B side by side, as [A B].
 Range cols()
           
 boolean equals(Object obj)
           
 Matrix fill(int top, int left, Matrix M)
          Fill this matrix with the data from the matrix M, starting from coordinates top,left.
 double[] getCol(int j)
          Return a double array containing the matrix's jth column.
 double[] getRow(int i)
          Return a double array containing the matrix's ith row.
 Matrix inverse()
           
 boolean isIdentity()
           
 boolean isZero()
           
 Iterator<Double> iterator()
           
 Matrix leftDivide(Matrix M)
          Matrix left-division: equal to A^(-1)*M.
 Matrix multiply(double c)
           
 Matrix multiply(Matrix M)
          Returns the product of this matrix with M.
 Matrix multiplyIP(double c)
           
 Matrix ref()
          row echelon form.
 Range rows()
           
 Matrix rref()
          Puts this matrix in reduced row echelon form using Gauss-Jordan elimination.
 void set(int i, int j, double val)
           
 void setCol(int j, double[] col)
          Sets the jth column of the matrix equal to col.
 Matrix setDiag(double value)
          Set all the diagonal entries of the matrix equal to value.
 void setRow(int i, double[] row)
          Sets the ith row of the matrix equal to row.
 int size()
           
 Matrix submatrix(int top, int left)
           
 Matrix submatrix(int top, int left, int rows, int cols)
           
 Matrix subtract(Matrix M)
           
 Matrix subtractIP(Matrix M)
           
 String toString()
           
 Matrix transpose()
           
 
Methods inherited from class java.util.AbstractCollection
add, addAll, clear, contains, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArray
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface csli.util.math.matrix.Matrix
get, numCols, numRows
 
Methods inherited from interface java.util.Collection
add, addAll, clear, contains, containsAll, hashCode, isEmpty, remove, removeAll, retainAll, toArray, toArray
 

Constructor Detail

AbstractMatrix

public AbstractMatrix()
Method Detail

set

public void set(int i,
                int j,
                double val)
Specified by:
set in interface Matrix

rows

public Range rows()
Specified by:
rows in interface Matrix

cols

public Range cols()
Specified by:
cols in interface Matrix

isZero

public boolean isZero()
Specified by:
isZero in interface Matrix

isIdentity

public boolean isIdentity()
Specified by:
isIdentity in interface Matrix

equals

public boolean equals(Object obj)
Specified by:
equals in interface Matrix
Specified by:
equals in interface Collection<Double>
Overrides:
equals in class Object

multiply

public Matrix multiply(Matrix M)
Returns the product of this matrix with M. Throws an IllegalArgumentException if this.numCols != M.numRows.

Specified by:
multiply in interface Matrix
Parameters:
M -
Returns:
a this.numRows x M.numCols matrix.

add

public Matrix add(Matrix M)
Specified by:
add in interface Matrix

subtract

public Matrix subtract(Matrix M)
Specified by:
subtract in interface Matrix

multiply

public Matrix multiply(double c)
Specified by:
multiply in interface Matrix

addIP

public Matrix addIP(Matrix M)
Specified by:
addIP in interface Matrix

subtractIP

public Matrix subtractIP(Matrix M)
Specified by:
subtractIP in interface Matrix

multiplyIP

public Matrix multiplyIP(double c)
Specified by:
multiplyIP in interface Matrix

setDiag

public Matrix setDiag(double value)
Description copied from interface: Matrix
Set all the diagonal entries of the matrix equal to value.

Specified by:
setDiag in interface Matrix
Returns:

submatrix

public Matrix submatrix(int top,
                        int left)
Specified by:
submatrix in interface Matrix

submatrix

public Matrix submatrix(int top,
                        int left,
                        int rows,
                        int cols)
Specified by:
submatrix in interface Matrix

appendRight

public Matrix appendRight(Matrix M)
Create a new matrix which contains the original matrix A and the argument B side by side, as [A B].

Specified by:
appendRight in interface Matrix
Parameters:
M -
Returns:

inverse

public Matrix inverse()
               throws Matrix.SingularMatrixException
Specified by:
inverse in interface Matrix
Throws:
Matrix.SingularMatrixException

leftDivide

public Matrix leftDivide(Matrix M)
                  throws Matrix.SingularMatrixException
Matrix left-division: equal to A^(-1)*M. Calculated by appending M to the right side of A, then row-reducing, and returning the result of the operation on M. If A is not invertible, throw a SingularMatrixException.

Specified by:
leftDivide in interface Matrix
Throws:
Matrix.SingularMatrixException

rref

public Matrix rref()
Puts this matrix in reduced row echelon form using Gauss-Jordan elimination. Note: this method is destructive: the original matrix is lost.

Specified by:
rref in interface Matrix
Returns:
The matrix in RREF.

ref

public Matrix ref()
row echelon form. Note: this method is destructive: the original matrix is lost.

Specified by:
ref in interface Matrix
Returns:

getRow

public double[] getRow(int i)
Return a double array containing the matrix's ith row.

Specified by:
getRow in interface Matrix
Parameters:
i -
Returns:

getCol

public double[] getCol(int j)
Return a double array containing the matrix's jth column.

Specified by:
getCol in interface Matrix
Parameters:
j -
Returns:

setRow

public void setRow(int i,
                   double[] row)
Sets the ith row of the matrix equal to row. If row.length != numCols(), throws an IllegalArgumentException.

Specified by:
setRow in interface Matrix
Parameters:
i -
row -

setCol

public void setCol(int j,
                   double[] col)
Sets the jth column of the matrix equal to col. If col.length != numRows(), throws an IllegalArgumentException.

Specified by:
setCol in interface Matrix
Parameters:
j -
col -

transpose

public Matrix transpose()
Specified by:
transpose in interface Matrix
Returns:
the transpose of the matrix.

fill

public Matrix fill(int top,
                   int left,
                   Matrix M)
Fill this matrix with the data from the matrix M, starting from coordinates top,left.

Specified by:
fill in interface Matrix
Parameters:
M -
i -
j -
Returns:

toString

public String toString()
Overrides:
toString in class AbstractCollection<Double>

size

public int size()
Specified by:
size in interface Collection<Double>
Specified by:
size in class AbstractCollection<Double>

iterator

public Iterator<Double> iterator()
Specified by:
iterator in interface Iterable<Double>
Specified by:
iterator in interface Collection<Double>
Specified by:
iterator in class AbstractCollection<Double>