|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.apache.commons.math.linear.BigMatrixImpl
Implementation for BigMatrix
using a BigDecimal[][] array to store entries
and
LU decompostion to support linear system
solution and inverse.
The LU decompostion is performed as needed, to support the following operations:
Usage notes:
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.BigMatrix
interface, matrix element indexing
is 0-based -- e.g., getEntry(0, 0)
returns the element in the first row, first column of the matrix.
Field Summary | |
(package private) static long |
serialVersionUID
Serialization id |
protected static BigDecimal |
TOO_SMALL
Bound to determine effective singularity in LU decomposition |
Constructor Summary | |
BigMatrixImpl()
Creates a matrix with no data |
|
BigMatrixImpl(BigDecimal[] v)
Create a new (column) BigMatrix using v as the
data for the unique column of the v.length x 1 matrix
created. |
|
BigMatrixImpl(BigDecimal[][] d)
Create a new BigMatrix using the data as the underlying
data array. |
|
BigMatrixImpl(double[][] d)
Create a new BigMatrix using the data as the underlying
data array. |
|
BigMatrixImpl(int rowDimension,
int columnDimension)
Create a new BigMatrix with the supplied row and column dimensions. |
|
BigMatrixImpl(String[][] d)
Create a new BigMatrix using the values represented by the strings in data as the underlying data array. |
Method Summary | |
BigMatrix |
add(BigMatrix m)
Compute the sum of this and m . |
BigMatrix |
copy()
Create a new BigMatrix which is a copy of this. |
boolean |
equals(Object object)
Returns true iff object is a
BigMatrixImpl instance with the same dimensions as this
and all corresponding matrix entries are equal. |
BigDecimal[] |
getColumn(int col)
Returns the entries in column number col as an array. |
double[] |
getColumnAsDoubleArray(int col)
Returns the entries in column number col as an array
of double values. |
int |
getColumnDimension()
Returns the number of columns in the matrix. |
BigMatrix |
getColumnMatrix(int column)
Returns the entries in column number column
as a column matrix. |
BigDecimal[][] |
getData()
Returns matrix entries as a two-dimensional array. |
double[][] |
getDataAsDoubleArray()
Returns matrix entries as a two-dimensional array. |
BigDecimal[][] |
getDataRef()
Returns a reference to the underlying data array. |
BigDecimal |
getDeterminant()
Returns the determinant of this matrix. |
BigDecimal |
getEntry(int row,
int column)
Returns the entry in the specified row and column. |
double |
getEntryAsDouble(int row,
int column)
Returns the entry in the specified row and column as a double. |
protected BigMatrix |
getIdentity(int dimension)
Returns dimension x dimension identity matrix. |
protected BigMatrix |
getLUMatrix()
Returns the LU decomposition as a BigMatrix. |
BigDecimal |
getNorm()
Returns the maximum absolute row sum norm of the matrix. |
protected int[] |
getPermutation()
Returns the permutation associated with the lu decomposition. |
int |
getRoundingMode()
Gets the rounding mode for division operations The default is BigDecimal.ROUND_HALF_UP |
BigDecimal[] |
getRow(int row)
Returns the entries in row number row as an array. |
double[] |
getRowAsDoubleArray(int row)
Returns the entries in row number row as an array
of double values. |
int |
getRowDimension()
Returns the number of rows in the matrix. |
BigMatrix |
getRowMatrix(int row)
Returns the entries in row number row
as a row matrix. |
int |
getScale()
Sets the scale for division operations. |
BigMatrix |
getSubMatrix(int[] selectedRows,
int[] selectedColumns)
Gets a submatrix. |
BigMatrix |
getSubMatrix(int startRow,
int endRow,
int startColumn,
int endColumn)
Gets a submatrix. |
BigDecimal |
getTrace()
Returns the trace of the matrix (the sum of the elements on the main diagonal). |
int |
hashCode()
Computes a hashcode for the matrix. |
BigMatrix |
inverse()
Returns the inverse matrix if this matrix is invertible. |
boolean |
isSingular()
Is this a singular matrix? |
boolean |
isSquare()
Is this a square matrix? |
void |
luDecompose()
Computes a new LU decompostion for this matrix, storing the result for use by other methods. |
BigMatrix |
multiply(BigMatrix m)
Returns the result of postmultiplying this by m . |
BigDecimal[] |
operate(BigDecimal[] v)
Returns the result of multiplying this by the vector v . |
BigDecimal[] |
operate(double[] v)
Returns the result of multiplying this by the vector v . |
BigDecimal[] |
preMultiply(BigDecimal[] v)
Returns the (row) vector result of premultiplying this by the vector v . |
BigMatrix |
preMultiply(BigMatrix m)
Returns the result premultiplying this by m . |
BigMatrix |
scalarAdd(BigDecimal d)
Returns the result of adding d to each entry of this. |
BigMatrix |
scalarMultiply(BigDecimal d)
Returns the result multiplying each entry of this by d |
void |
setRoundingMode(int roundingMode)
Sets the rounding mode for decimal divisions. |
void |
setScale(int scale)
Sets the scale for division operations. |
BigDecimal[] |
solve(BigDecimal[] b)
Returns a matrix of (column) solution vectors for linear systems with coefficient matrix = this and constant vectors = columns of b . |
BigMatrix |
solve(BigMatrix b)
Returns a matrix of (column) solution vectors for linear systems with coefficient matrix = this and constant vectors = columns of b . |
BigDecimal[] |
solve(double[] b)
Returns a matrix of (column) solution vectors for linear systems with coefficient matrix = this and constant vectors = columns of b . |
BigMatrix |
subtract(BigMatrix m)
Compute this minus m . |
String |
toString()
|
BigMatrix |
transpose()
Returns the transpose matrix. |
Methods inherited from class java.lang.Object |
clone, finalize, getClass, notify, notifyAll, wait, wait, wait |
Field Detail |
static final long serialVersionUID
protected static BigDecimal TOO_SMALL
Constructor Detail |
public BigMatrixImpl()
public BigMatrixImpl(int rowDimension, int columnDimension)
rowDimension
- the number of rows in the new matrixcolumnDimension
- the number of columns in the new matrixpublic BigMatrixImpl(BigDecimal[][] d)
data
as the underlying
data array.
The input array is copied, not referenced.
d
- data for new matrix
IllegalArgumentException
- if d
is not rectangular
(not all rows have the same length) or empty
NullPointerException
- if d
is nullpublic BigMatrixImpl(double[][] d)
data
as the underlying
data array.
The input array is copied, not referenced.
d
- data for new matrix
IllegalArgumentException
- if d
is not rectangular
(not all rows have the same length) or empty
NullPointerException
- if d
is nullpublic BigMatrixImpl(String[][] d)
data
as the underlying data array.
d
- data for new matrix
IllegalArgumentException
- if d
is not rectangular
(not all rows have the same length) or empty
NullPointerException
- if d
is nullpublic BigMatrixImpl(BigDecimal[] 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 BigMatrix copy()
copy
in interface BigMatrix
public BigMatrix add(BigMatrix m) throws IllegalArgumentException
m
.
add
in interface BigMatrix
m
- matrix to be added
IllegalArgumentException
- if m is not the same size as thispublic BigMatrix subtract(BigMatrix m) throws IllegalArgumentException
m
.
subtract
in interface BigMatrix
m
- matrix to be subtracted
IllegalArgumentException
- if m is not the same size as *thispublic BigMatrix scalarAdd(BigDecimal d)
scalarAdd
in interface BigMatrix
d
- value to be added to each entry
public BigMatrix scalarMultiply(BigDecimal d)
d
scalarMultiply
in interface BigMatrix
d
- value to multiply all entries by
public BigMatrix multiply(BigMatrix m) throws IllegalArgumentException
m
.
multiply
in interface BigMatrix
m
- matrix to postmultiply by
IllegalArgumentException
- if columnDimension(this) != rowDimension(m)public BigMatrix preMultiply(BigMatrix m) throws IllegalArgumentException
m
.
preMultiply
in interface BigMatrix
m
- matrix to premultiply by
IllegalArgumentException
- if rowDimension(this) != columnDimension(m)public BigDecimal[][] getData()
Makes a fresh copy of the underlying data.
getData
in interface BigMatrix
public double[][] getDataAsDoubleArray()
Makes a fresh copy of the underlying data converted to
double
values.
getDataAsDoubleArray
in interface BigMatrix
public BigDecimal[][] getDataRef()
Does not make a fresh copy of the underlying data.
public int getRoundingMode()
BigDecimal.ROUND_HALF_UP
getRoundingMode
in interface BigMatrix
BigDecimal
public void setRoundingMode(int roundingMode)
roundingMode
- BigDecimal
public int getScale()
BigDecimal
public void setScale(int scale)
scale
- BigDecimal
public BigDecimal getNorm()
getNorm
in interface BigMatrix
public BigMatrix getSubMatrix(int startRow, int endRow, int startColumn, int endColumn) throws MatrixIndexException
getSubMatrix
in interface BigMatrix
startRow
- Initial row indexendRow
- Final row indexstartColumn
- Initial column indexendColumn
- Final column index
MatrixIndexException
- if row or column selections are not validpublic BigMatrix getSubMatrix(int[] selectedRows, int[] selectedColumns) throws MatrixIndexException
getSubMatrix
in interface BigMatrix
selectedRows
- Array of row indices must be non-emptyselectedColumns
- Array of column indices must be non-empty
MatrixIndexException
- if supplied row or column index arrays
are not validpublic BigMatrix getRowMatrix(int row) throws MatrixIndexException
row
as a row matrix. Row indices start at 0.
getRowMatrix
in interface BigMatrix
row
- the row to be fetched
MatrixIndexException
- if the specified row index is invalidpublic BigMatrix getColumnMatrix(int column) throws MatrixIndexException
column
as a column matrix. Column indices start at 0.
getColumnMatrix
in interface BigMatrix
column
- the column to be fetched
MatrixIndexException
- if the specified column index is invalidpublic BigDecimal[] getRow(int row) throws MatrixIndexException
row
as an array.
Row indices start at 0. A MatrixIndexException
is thrown
unless 0 <= row < rowDimension.
getRow
in interface BigMatrix
row
- the row to be fetched
MatrixIndexException
- if the specified row index is not validpublic double[] getRowAsDoubleArray(int row) throws MatrixIndexException
row
as an array
of double values.
Row indices start at 0. A MatrixIndexException
is thrown
unless 0 <= row < rowDimension.
getRowAsDoubleArray
in interface BigMatrix
row
- the row to be fetched
MatrixIndexException
- if the specified row index is not validpublic BigDecimal[] getColumn(int col) throws MatrixIndexException
col
as an array.
Column indices start at 0. A MatrixIndexException
is thrown
unless 0 <= column < columnDimension.
getColumn
in interface BigMatrix
col
- the column to be fetched
MatrixIndexException
- if the specified column index is not validpublic double[] getColumnAsDoubleArray(int col) throws MatrixIndexException
col
as an array
of double values.
Column indices start at 0. A MatrixIndexException
is thrown
unless 0 <= column < columnDimension.
getColumnAsDoubleArray
in interface BigMatrix
col
- the column to be fetched
MatrixIndexException
- if the specified column index is not validpublic BigDecimal getEntry(int row, int column) throws MatrixIndexException
Row and column indices start at 0 and must satisfy
0 <= row < rowDimension
0 <= column < columnDimension
MatrixIndexException
is thrown.
getEntry
in interface BigMatrix
row
- row location of entry to be fetchedcolumn
- column location of entry to be fetched
MatrixIndexException
- if the row or column index is not validpublic double getEntryAsDouble(int row, int column) throws MatrixIndexException
Row and column indices start at 0 and must satisfy
0 <= row < rowDimension
0 <= column < columnDimension
MatrixIndexException
is thrown.
getEntryAsDouble
in interface BigMatrix
row
- row location of entry to be fetchedcolumn
- column location of entry to be fetched
MatrixIndexException
- if the row
or column index is not validpublic BigMatrix transpose()
transpose
in interface BigMatrix
public BigMatrix inverse() throws InvalidMatrixException
inverse
in interface BigMatrix
InvalidMatrixException
- if this is not invertiblepublic BigDecimal getDeterminant() throws InvalidMatrixException
getDeterminant
in interface BigMatrix
InvalidMatrixException
- if matrix is not squarepublic boolean isSquare()
isSquare
in interface BigMatrix
public boolean isSingular()
isSingular
in interface BigMatrix
public int getRowDimension()
getRowDimension
in interface BigMatrix
public int getColumnDimension()
getColumnDimension
in interface BigMatrix
public BigDecimal getTrace() throws IllegalArgumentException
getTrace
in interface BigMatrix
IllegalArgumentException
- if this matrix is not square.public BigDecimal[] operate(BigDecimal[] v) throws IllegalArgumentException
v
.
operate
in interface BigMatrix
v
- the vector to operate on
IllegalArgumentException
- if columnDimension != v.size()public BigDecimal[] operate(double[] v) throws IllegalArgumentException
v
.
v
- the vector to operate on
IllegalArgumentException
- if columnDimension != v.size()public BigDecimal[] preMultiply(BigDecimal[] v) throws IllegalArgumentException
v
.
preMultiply
in interface BigMatrix
v
- the row vector to premultiply by
IllegalArgumentException
- if rowDimension != v.size()public BigDecimal[] solve(BigDecimal[] b) throws IllegalArgumentException, InvalidMatrixException
b
.
solve
in interface BigMatrix
b
- array of constants forming RHS of linear systems to
to solve
IllegalArgumentException
- if this.rowDimension != row dimension
InvalidMatrixException
- if this matrix is not square or is singularpublic BigDecimal[] solve(double[] b) throws IllegalArgumentException, InvalidMatrixException
b
.
b
- array of constants forming RHS of linear systems to
to solve
IllegalArgumentException
- if this.rowDimension != row dimension
InvalidMatrixException
- if this matrix is not square or is singularpublic BigMatrix solve(BigMatrix b) throws IllegalArgumentException, InvalidMatrixException
b
.
solve
in interface BigMatrix
b
- matrix of constant vectors forming RHS of linear systems to
to solve
IllegalArgumentException
- if this.rowDimension != row dimension
InvalidMatrixException
- if this matrix is not square or is singularpublic void luDecompose() throws 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.
InvalidMatrixException
- if the matrix is non-square or singular.public String toString()
Object.toString()
public boolean equals(Object object)
object
is a
BigMatrixImpl
instance with the same dimensions as this
and all corresponding matrix entries are equal. BigDecimal.equals
is used to compare corresponding entries.
object
- the object to test equality against.
public int hashCode()
protected BigMatrix getIdentity(int dimension)
dimension x dimension
identity matrix.
dimension
- dimension of identity matrix to generate
protected BigMatrix getLUMatrix() throws 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),
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 |