|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object commonSense.math.linear.Matrix
This class is based on the Apache.org commons.math package, of to be precise:
RleaMatrixImpl
Differences with that class are:
Implementation for Matrix using a double[][] array to store entries and LU decomposition to support linear system solution and inverse.
The LU decomposition is performed as needed, to support the following operations:
Usage note :
The LU decomposition is stored and reused on subsequent
calls. If matrix data are modified using any of the
public setXxx methods, the saved decomposition is
discarded. If data are modified via references to the
underlying array obtained using getDataRef()
,
then the stored LU decomposition will not be discarded.
In this case, you need to explicitly invoke
LUDecompose()
to recompute the
decomposition before using any of the methods above.
Numbering of the rows and columns is as default for an JAVA array, starting with 0 with the highest value for either row-1 or column-1. This facilitates programming of modules which give back an array to construct subMatrices from the original matrix.
Field Summary | |
protected static double |
TOO_SMALL
Bound to determine effective singularity in LU decomposition |
Constructor Summary | |
Matrix()
Creates a matrix with no data |
|
Matrix(double[] v)
Create a new (column) Matrix using v
as the data for the unique column of the
v.length x 1 matrix created.
|
|
Matrix(double[][] d)
Create a new Matrix using the data
as the underlying data array.
|
|
Matrix(int rowDimension,
int columnDimension)
Create a new Matrix with the supplied row and column dimensions. |
Method Summary | |
Matrix |
add(Matrix m)
Compute the sum of this and m . |
double[] |
columnMeans()
Gives the means of each colums of the matrix. |
Matrix |
copy()
Create a new Matrix which is a copy of this. |
double[] |
getColumn(int col)
Returns the entries in column number col
as an array. |
int |
getColumnDimension()
|
Matrix |
getColumnMatrix(int col)
Returns the entries in column number col
as a Matrix object. |
double[][] |
getData()
Returns matrix entries as a two-dimensional array. |
double[][] |
getDataRef()
Returns a reference to the underlying data array. |
double |
getDeterminant()
|
double |
getEntry(int row,
int column)
Returns the entry in the specified row and column. |
protected Matrix |
getIdentity(int dimension)
Returns dimension x dimension identity
matrix. |
protected Matrix |
getLUMatrix()
Returns the LU decomposition as a Matrix. |
double |
getNorm()
|
protected int[] |
getPermutation()
Returns the permutation associated with the lu decomposition. |
double[] |
getRow(int row)
Returns the entries in row number row
as an array. |
int |
getRowDimension()
|
Matrix |
getRowMatrix(int row)
Returns the entries in row number row
as a Matrix object. |
Matrix |
getSubMatrix(int[] rows,
int[] columns)
Get a submatrix. |
Matrix |
getSubMatrix(int[] rows,
int startColumn,
int endColumn)
Get a submatrix. |
Matrix |
getSubMatrix(int startRow,
int endRow,
int[] columns)
Get a submatrix. |
Matrix |
getSubMatrix(int startRow,
int endRow,
int startColumn,
int endColumn)
Get a submatrix. |
double |
getTrace()
|
Matrix |
inverse()
Returns the inverse matrix if this matrix is invertible. |
boolean |
isSingular()
|
boolean |
isSquare()
|
void |
luDecompose()
Computes a new LU decompostion for this matrix, storing the result for use by other methods. |
Matrix |
multiply(Matrix m)
Returns the result of postmultiplying this by m . |
double[] |
operate(double[] v)
|
double[] |
preMultiply(double[] v)
|
Matrix |
preMultiply(Matrix m)
Returns the result premultiplying this by m . |
double[] |
rowMeans()
Gives the means of each row of the matrix. |
Matrix |
scalarAdd(double d)
Returns the result of adding d to each entry of this. |
Matrix |
scalarMultiply(double d)
Returns the result multiplying each entry of this by d |
void |
setData(double[][] inData)
Overwrites the underlying data for the matrix with a fresh copy of inData . |
void |
setDataRef(double[][] inData)
Overwrites the underlying data for the matrix with a reference to inData .
|
void |
setEntry(int row,
int column,
double value)
Sets the entry in the specified row and column to the specified value. |
double[] |
solve(double[] b)
Returns a matrix of (column) solution vectors for linear systems with coefficient matrix = this and constant vectors = columns of b . |
Matrix |
solve(Matrix b)
Returns a matrix of (column) solution vectors for linear systems with coefficient matrix = this and constant vectors = columns of b . |
Matrix |
subtract(Matrix m)
Compute this minus m . |
java.lang.String |
toSquareString()
|
java.lang.String |
toString()
|
Matrix |
transpose()
Returns the transpose matrix. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
protected static double TOO_SMALL
Constructor Detail |
public Matrix()
public Matrix(int rowDimension, int columnDimension)
rowDimension
- the number of rows in the new
matrixcolumnDimension
- the number of columns in the
new matrixpublic Matrix(double[][] d)
data
as the underlying data array.
The input array is copied, not referenced.
d
- data for new matrixpublic Matrix(double[] v)
v
as the data for the unique column of the
v.length x 1
matrix created.
The input array is copied, not referenced.
v
- column vector holding data for new matrixMethod Detail |
public Matrix copy()
public Matrix add(Matrix m) throws java.lang.IllegalArgumentException
m
.
m
- matrix to be added
java.lang.IllegalArgumentException
- if m is not the same
size as thispublic Matrix subtract(Matrix m) throws java.lang.IllegalArgumentException
m
.
m
- matrix to be subtracted
java.lang.IllegalArgumentException
- if m is not the same
size as *thispublic Matrix scalarAdd(double d)
d
- value to be added to each entry
public Matrix scalarMultiply(double d)
d
d
- value to multiply all entries by
public Matrix multiply(Matrix m) throws java.lang.IllegalArgumentException
m
.
m
- matrix to postmultiply by
java.lang.IllegalArgumentException
- if
columnDimension(this) != rowDimension(m)public Matrix preMultiply(Matrix m) throws java.lang.IllegalArgumentException
m
.
m
- matrix to premultiply by
java.lang.IllegalArgumentException
- if
rowDimension(this) != columnDimension(m)public double[][] getData()
Makes a fresh copy of the underlying data.
public void setData(double[][] inData)
inData
.
inData
- 2-dimensional array of entriespublic double[][] getDataRef()
Does not make a fresh copy of the underlying data.
public void setDataRef(double[][] inData)
inData
.
Does not make a fresh copy of data
.
inData
- 2-dimensional array of entriespublic double getNorm()
public double[] getRow(int row) throws org.apache.commons.math.linear.MatrixIndexException
row
as an array.
row
- the row to be fetched
org.apache.commons.math.linear.MatrixIndexException
- if the specified row is
greater than the number of rows in this
matrixpublic Matrix getRowMatrix(int row) throws org.apache.commons.math.linear.MatrixIndexException
row
as a Matrix object.
row
- the row to be fetched
org.apache.commons.math.linear.MatrixIndexException
- if the specified row is
greater than the number of rows in this
matrixpublic double[] getColumn(int col) throws org.apache.commons.math.linear.MatrixIndexException
col
as an array.
col
- column to fetch
org.apache.commons.math.linear.MatrixIndexException
- if the specified column
is greater than the number of columns in
this matrixpublic Matrix getColumnMatrix(int col) throws org.apache.commons.math.linear.MatrixIndexException
col
as a Matrix object.
col
- column to fetch
org.apache.commons.math.linear.MatrixIndexException
- if the specified column
is greater than the number of columns in
this matrixpublic double getEntry(int row, int column) throws org.apache.commons.math.linear.MatrixIndexException
row
- row location of entry to be fetchedcolumn
- column location of entry to be fetched
org.apache.commons.math.linear.MatrixIndexException
- if the specified
coordinate is outside the dimensions of
this matrixpublic void setEntry(int row, int column, double value) throws org.apache.commons.math.linear.MatrixIndexException
row
- row location of entry to be setcolumn
- column location of entry to be setvalue
- value to set
org.apache.commons.math.linear.MatrixIndexException
- if the specified
coordinate is outside he dimensions of
this matrixpublic Matrix transpose()
public Matrix inverse() throws org.apache.commons.math.linear.InvalidMatrixException
org.apache.commons.math.linear.InvalidMatrixException
- if this is not
invertiblepublic double getDeterminant() throws org.apache.commons.math.linear.InvalidMatrixException
org.apache.commons.math.linear.InvalidMatrixException
- if matrix is not
squarepublic boolean isSquare()
public boolean isSingular()
public int getRowDimension()
public int getColumnDimension()
public double getTrace() throws java.lang.IllegalArgumentException
java.lang.IllegalArgumentException
- if the matrix is not
squarepublic double[] operate(double[] v) throws java.lang.IllegalArgumentException
v
- vector to operate on
java.lang.IllegalArgumentException
- if columnDimension !=
v.lengthpublic double[] preMultiply(double[] v) throws java.lang.IllegalArgumentException
v
- vector to premultiply by
java.lang.IllegalArgumentException
- if rowDimension !=
v.lengthpublic double[] solve(double[] b) throws java.lang.IllegalArgumentException, org.apache.commons.math.linear.InvalidMatrixException
b
.
b
- array of constant forming RHS of linear
systems to to solve
java.lang.IllegalArgumentException
- if this.rowDimension !=
row dimension
org.apache.commons.math.linear.InvalidMatrixException
- if this matrix is not
square or is singularpublic Matrix solve(Matrix b) throws java.lang.IllegalArgumentException, org.apache.commons.math.linear.InvalidMatrixException
b
.
b
- matrix of constant vectors forming RHS of
linear systems to to solve
java.lang.IllegalArgumentException
- if this.rowDimension !=
row dimension
org.apache.commons.math.linear.InvalidMatrixException
- if this matrix is not
square or is singularpublic void luDecompose() throws org.apache.commons.math.linear.InvalidMatrixException
Implementation Note :
Uses
Crout's algortithm , with partial pivoting.
Usage Note :
This method should rarely be invoked directly. Its
only use is to force recomputation of the LU
decomposition when changes have been made to the
underlying data using direct array references.
Changes made using setXxx methods will trigger
recomputation when needed automatically.
org.apache.commons.math.linear.InvalidMatrixException
- if the matrix is
non-square or singular.public double[] rowMeans()
public double[] columnMeans()
public Matrix getSubMatrix(int startRow, int endRow, int startColumn, int endColumn) throws org.apache.commons.math.linear.MatrixIndexException
startRow
- Initial row indexendRow
- Final row indexstartColumn
- Initial column indexendColumn
- Final column index
org.apache.commons.math.linear.MatrixIndexException
- matrix dimension
mismatchpublic Matrix getSubMatrix(int[] rows, int[] columns) throws org.apache.commons.math.linear.MatrixIndexException
rows
- Array of row indices.columns
- Array of column indices.
org.apache.commons.math.linear.MatrixIndexException
- matrix dimension
mismatchpublic Matrix getSubMatrix(int startRow, int endRow, int[] columns) throws org.apache.commons.math.linear.MatrixIndexException
startRow
- Initial row indexendRow
- Final row indexcolumns
- Array of column indices.
org.apache.commons.math.linear.MatrixIndexException
- matrix dimension
mismatchpublic Matrix getSubMatrix(int[] rows, int startColumn, int endColumn) throws org.apache.commons.math.linear.MatrixIndexException
rows
- Array of row indices.startColumn
- Initial column indexendColumn
- Final column index
org.apache.commons.math.linear.MatrixIndexException
- matrix dimension
mismatchpublic java.lang.String toSquareString()
Object.toString()
public java.lang.String toString()
Object.toString()
protected Matrix getIdentity(int dimension)
dimension x dimension
identity
matrix.
dimension
- dimension of identity matrix to
generate
protected Matrix getLUMatrix() throws org.apache.commons.math.linear.InvalidMatrixException
The matrix returned is a compact representation of the LU decomposition. Elements below the main diagonal correspond to entries of the "L" matrix; elements on and above the main diagonal correspond to entries of the "U" matrix.
Example:
Returned matrix L U 2 3 1 1 0 0 2 3 1 5 4 6 5 1 0 0 4 6 1 7 8 1 7 1 0 0 8The L and U matrices satisfy the matrix equation LU = permuteRows(this),
org.apache.commons.math.linear.InvalidMatrixException
- if the matrix is
non-square or singular.protected int[] getPermutation()
Example: permutation = [1, 2, 0] means current 2nd row is first, current third row is second and current first row is last.
Returns a fresh copy of the array.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |