org.apache.commons.math.linear
Class BigMatrixImpl

java.lang.Object
  extended byorg.apache.commons.math.linear.BigMatrixImpl
All Implemented Interfaces:
BigMatrix, Serializable

public class BigMatrixImpl
extends Object
implements BigMatrix, Serializable

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:

Version:
$Revision: 1.10 $ $Date: 2004/11/07 20:19:22 $
See Also:
Serialized Form

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

serialVersionUID

static final long serialVersionUID
Serialization id

See Also:
Constant Field Values

TOO_SMALL

protected static BigDecimal TOO_SMALL
Bound to determine effective singularity in LU decomposition

Constructor Detail

BigMatrixImpl

public BigMatrixImpl()
Creates a matrix with no data


BigMatrixImpl

public BigMatrixImpl(int rowDimension,
                     int columnDimension)
Create a new BigMatrix with the supplied row and column dimensions.

Parameters:
rowDimension - the number of rows in the new matrix
columnDimension - the number of columns in the new matrix

BigMatrixImpl

public BigMatrixImpl(BigDecimal[][] d)
Create a new BigMatrix using the data as the underlying data array.

The input array is copied, not referenced.

Parameters:
d - data for new matrix
Throws:
IllegalArgumentException - if d is not rectangular (not all rows have the same length) or empty
NullPointerException - if d is null

BigMatrixImpl

public BigMatrixImpl(double[][] d)
Create a new BigMatrix using the data as the underlying data array.

The input array is copied, not referenced.

Parameters:
d - data for new matrix
Throws:
IllegalArgumentException - if d is not rectangular (not all rows have the same length) or empty
NullPointerException - if d is null

BigMatrixImpl

public BigMatrixImpl(String[][] d)
Create a new BigMatrix using the values represented by the strings in data as the underlying data array.

Parameters:
d - data for new matrix
Throws:
IllegalArgumentException - if d is not rectangular (not all rows have the same length) or empty
NullPointerException - if d is null

BigMatrixImpl

public 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.

The input array is copied, not referenced.

Parameters:
v - column vector holding data for new matrix
Method Detail

copy

public BigMatrix copy()
Create a new BigMatrix which is a copy of this.

Specified by:
copy in interface BigMatrix
Returns:
the cloned matrix

add

public BigMatrix add(BigMatrix m)
              throws IllegalArgumentException
Compute the sum of this and m.

Specified by:
add in interface BigMatrix
Parameters:
m - matrix to be added
Returns:
this + m
Throws:
IllegalArgumentException - if m is not the same size as this

subtract

public BigMatrix subtract(BigMatrix m)
                   throws IllegalArgumentException
Compute this minus m.

Specified by:
subtract in interface BigMatrix
Parameters:
m - matrix to be subtracted
Returns:
this + m
Throws:
IllegalArgumentException - if m is not the same size as *this

scalarAdd

public BigMatrix scalarAdd(BigDecimal d)
Returns the result of adding d to each entry of this.

Specified by:
scalarAdd in interface BigMatrix
Parameters:
d - value to be added to each entry
Returns:
d + this

scalarMultiply

public BigMatrix scalarMultiply(BigDecimal d)
Returns the result multiplying each entry of this by d

Specified by:
scalarMultiply in interface BigMatrix
Parameters:
d - value to multiply all entries by
Returns:
d * this

multiply

public BigMatrix multiply(BigMatrix m)
                   throws IllegalArgumentException
Returns the result of postmultiplying this by m.

Specified by:
multiply in interface BigMatrix
Parameters:
m - matrix to postmultiply by
Returns:
this*m
Throws:
IllegalArgumentException - if columnDimension(this) != rowDimension(m)

preMultiply

public BigMatrix preMultiply(BigMatrix m)
                      throws IllegalArgumentException
Returns the result premultiplying this by m.

Specified by:
preMultiply in interface BigMatrix
Parameters:
m - matrix to premultiply by
Returns:
m * this
Throws:
IllegalArgumentException - if rowDimension(this) != columnDimension(m)

getData

public BigDecimal[][] getData()
Returns matrix entries as a two-dimensional array.

Makes a fresh copy of the underlying data.

Specified by:
getData in interface BigMatrix
Returns:
2-dimensional array of entries

getDataAsDoubleArray

public double[][] getDataAsDoubleArray()
Returns matrix entries as a two-dimensional array.

Makes a fresh copy of the underlying data converted to double values.

Specified by:
getDataAsDoubleArray in interface BigMatrix
Returns:
2-dimensional array of entries

getDataRef

public BigDecimal[][] getDataRef()
Returns a reference to the underlying data array.

Does not make a fresh copy of the underlying data.

Returns:
2-dimensional array of entries

getRoundingMode

public int getRoundingMode()
Gets the rounding mode for division operations The default is BigDecimal.ROUND_HALF_UP

Specified by:
getRoundingMode in interface BigMatrix
Returns:
the rounding mode.
See Also:
BigDecimal

setRoundingMode

public void setRoundingMode(int roundingMode)
Sets the rounding mode for decimal divisions.

Parameters:
roundingMode -
See Also:
BigDecimal

getScale

public int getScale()
Sets the scale for division operations. The default is 64

Returns:
the scale
See Also:
BigDecimal

setScale

public void setScale(int scale)
Sets the scale for division operations.

Parameters:
scale -
See Also:
BigDecimal

getNorm

public BigDecimal getNorm()
Returns the maximum absolute row sum norm of the matrix.

Specified by:
getNorm in interface BigMatrix
Returns:
norm

getSubMatrix

public BigMatrix getSubMatrix(int startRow,
                              int endRow,
                              int startColumn,
                              int endColumn)
                       throws MatrixIndexException
Gets a submatrix. Rows and columns are indicated counting from 0 to n-1.

Specified by:
getSubMatrix in interface BigMatrix
Parameters:
startRow - Initial row index
endRow - Final row index
startColumn - Initial column index
endColumn - Final column index
Returns:
The subMatrix containing the data of the specified rows and columns
Throws:
MatrixIndexException - if row or column selections are not valid

getSubMatrix

public BigMatrix getSubMatrix(int[] selectedRows,
                              int[] selectedColumns)
                       throws MatrixIndexException
Gets a submatrix. Rows and columns are indicated counting from 0 to n-1.

Specified by:
getSubMatrix in interface BigMatrix
Parameters:
selectedRows - Array of row indices must be non-empty
selectedColumns - Array of column indices must be non-empty
Returns:
The subMatrix containing the data in the specified rows and columns
Throws:
MatrixIndexException - if supplied row or column index arrays are not valid

getRowMatrix

public BigMatrix getRowMatrix(int row)
                       throws MatrixIndexException
Returns the entries in row number row as a row matrix. Row indices start at 0.

Specified by:
getRowMatrix in interface BigMatrix
Parameters:
row - the row to be fetched
Returns:
row matrix
Throws:
MatrixIndexException - if the specified row index is invalid

getColumnMatrix

public BigMatrix getColumnMatrix(int column)
                          throws MatrixIndexException
Returns the entries in column number column as a column matrix. Column indices start at 0.

Specified by:
getColumnMatrix in interface BigMatrix
Parameters:
column - the column to be fetched
Returns:
column matrix
Throws:
MatrixIndexException - if the specified column index is invalid

getRow

public BigDecimal[] getRow(int row)
                    throws MatrixIndexException
Returns the entries in row number row as an array.

Row indices start at 0. A MatrixIndexException is thrown unless 0 <= row < rowDimension.

Specified by:
getRow in interface BigMatrix
Parameters:
row - the row to be fetched
Returns:
array of entries in the row
Throws:
MatrixIndexException - if the specified row index is not valid

getRowAsDoubleArray

public double[] getRowAsDoubleArray(int row)
                             throws MatrixIndexException
Returns the entries in row number row as an array of double values.

Row indices start at 0. A MatrixIndexException is thrown unless 0 <= row < rowDimension.

Specified by:
getRowAsDoubleArray in interface BigMatrix
Parameters:
row - the row to be fetched
Returns:
array of entries in the row
Throws:
MatrixIndexException - if the specified row index is not valid

getColumn

public BigDecimal[] getColumn(int col)
                       throws MatrixIndexException
Returns the entries in column number col as an array.

Column indices start at 0. A MatrixIndexException is thrown unless 0 <= column < columnDimension.

Specified by:
getColumn in interface BigMatrix
Parameters:
col - the column to be fetched
Returns:
array of entries in the column
Throws:
MatrixIndexException - if the specified column index is not valid

getColumnAsDoubleArray

public double[] getColumnAsDoubleArray(int col)
                                throws MatrixIndexException
Returns the entries in column number col as an array of double values.

Column indices start at 0. A MatrixIndexException is thrown unless 0 <= column < columnDimension.

Specified by:
getColumnAsDoubleArray in interface BigMatrix
Parameters:
col - the column to be fetched
Returns:
array of entries in the column
Throws:
MatrixIndexException - if the specified column index is not valid

getEntry

public BigDecimal getEntry(int row,
                           int column)
                    throws MatrixIndexException
Returns the entry in the specified row and column.

Row and column indices start at 0 and must satisfy

otherwise a MatrixIndexException is thrown.

Specified by:
getEntry in interface BigMatrix
Parameters:
row - row location of entry to be fetched
column - column location of entry to be fetched
Returns:
matrix entry in row,column
Throws:
MatrixIndexException - if the row or column index is not valid

getEntryAsDouble

public double getEntryAsDouble(int row,
                               int column)
                        throws MatrixIndexException
Returns the entry in the specified row and column as a double.

Row and column indices start at 0 and must satisfy

otherwise a MatrixIndexException is thrown.

Specified by:
getEntryAsDouble in interface BigMatrix
Parameters:
row - row location of entry to be fetched
column - column location of entry to be fetched
Returns:
matrix entry in row,column
Throws:
MatrixIndexException - if the row or column index is not valid

transpose

public BigMatrix transpose()
Returns the transpose matrix.

Specified by:
transpose in interface BigMatrix
Returns:
transpose matrix

inverse

public BigMatrix inverse()
                  throws InvalidMatrixException
Returns the inverse matrix if this matrix is invertible.

Specified by:
inverse in interface BigMatrix
Returns:
inverse matrix
Throws:
InvalidMatrixException - if this is not invertible

getDeterminant

public BigDecimal getDeterminant()
                          throws InvalidMatrixException
Returns the determinant of this matrix.

Specified by:
getDeterminant in interface BigMatrix
Returns:
determinant
Throws:
InvalidMatrixException - if matrix is not square

isSquare

public boolean isSquare()
Is this a square matrix?

Specified by:
isSquare in interface BigMatrix
Returns:
true if the matrix is square (rowDimension = columnDimension)

isSingular

public boolean isSingular()
Is this a singular matrix?

Specified by:
isSingular in interface BigMatrix
Returns:
true if the matrix is singular

getRowDimension

public int getRowDimension()
Returns the number of rows in the matrix.

Specified by:
getRowDimension in interface BigMatrix
Returns:
rowDimension

getColumnDimension

public int getColumnDimension()
Returns the number of columns in the matrix.

Specified by:
getColumnDimension in interface BigMatrix
Returns:
columnDimension

getTrace

public BigDecimal getTrace()
                    throws IllegalArgumentException
Returns the trace of the matrix (the sum of the elements on the main diagonal).

Specified by:
getTrace in interface BigMatrix
Returns:
trace
Throws:
IllegalArgumentException - if this matrix is not square.

operate

public BigDecimal[] operate(BigDecimal[] v)
                     throws IllegalArgumentException
Returns the result of multiplying this by the vector v.

Specified by:
operate in interface BigMatrix
Parameters:
v - the vector to operate on
Returns:
this*v
Throws:
IllegalArgumentException - if columnDimension != v.size()

operate

public BigDecimal[] operate(double[] v)
                     throws IllegalArgumentException
Returns the result of multiplying this by the vector v.

Parameters:
v - the vector to operate on
Returns:
this*v
Throws:
IllegalArgumentException - if columnDimension != v.size()

preMultiply

public BigDecimal[] preMultiply(BigDecimal[] v)
                         throws IllegalArgumentException
Returns the (row) vector result of premultiplying this by the vector v.

Specified by:
preMultiply in interface BigMatrix
Parameters:
v - the row vector to premultiply by
Returns:
v*this
Throws:
IllegalArgumentException - if rowDimension != v.size()

solve

public BigDecimal[] solve(BigDecimal[] b)
                   throws IllegalArgumentException,
                          InvalidMatrixException
Returns a matrix of (column) solution vectors for linear systems with coefficient matrix = this and constant vectors = columns of b.

Specified by:
solve in interface BigMatrix
Parameters:
b - array of constants forming RHS of linear systems to to solve
Returns:
solution array
Throws:
IllegalArgumentException - if this.rowDimension != row dimension
InvalidMatrixException - if this matrix is not square or is singular

solve

public BigDecimal[] solve(double[] b)
                   throws IllegalArgumentException,
                          InvalidMatrixException
Returns a matrix of (column) solution vectors for linear systems with coefficient matrix = this and constant vectors = columns of b.

Parameters:
b - array of constants forming RHS of linear systems to to solve
Returns:
solution array
Throws:
IllegalArgumentException - if this.rowDimension != row dimension
InvalidMatrixException - if this matrix is not square or is singular

solve

public BigMatrix solve(BigMatrix b)
                throws IllegalArgumentException,
                       InvalidMatrixException
Returns a matrix of (column) solution vectors for linear systems with coefficient matrix = this and constant vectors = columns of b.

Specified by:
solve in interface BigMatrix
Parameters:
b - matrix of constant vectors forming RHS of linear systems to to solve
Returns:
matrix of solution vectors
Throws:
IllegalArgumentException - if this.rowDimension != row dimension
InvalidMatrixException - if this matrix is not square or is singular

luDecompose

public void luDecompose()
                 throws InvalidMatrixException
Computes a new LU decompostion for this matrix, storing the result for use by other methods.

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.

Throws:
InvalidMatrixException - if the matrix is non-square or singular.

toString

public String toString()
See Also:
Object.toString()

equals

public 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.equals is used to compare corresponding entries.

Parameters:
object - the object to test equality against.
Returns:
true if object equals this

hashCode

public int hashCode()
Computes a hashcode for the matrix.

Returns:
hashcode for matrix

getIdentity

protected BigMatrix getIdentity(int dimension)
Returns dimension x dimension identity matrix.

Parameters:
dimension - dimension of identity matrix to generate
Returns:
identity matrix

getLUMatrix

protected BigMatrix getLUMatrix()
                         throws InvalidMatrixException
Returns the LU decomposition as a BigMatrix. Returns a fresh copy of the cached LU matrix if this has been computed; otherwise the composition is computed and cached for use by other methods. Since a copy is returned in either case, changes to the returned matrix do not affect the LU decomposition property.

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  8          
 
The L and U matrices satisfy the matrix equation LU = permuteRows(this),
where permuteRows reorders the rows of the matrix to follow the order determined by the permutation property.

Returns:
LU decomposition matrix
Throws:
InvalidMatrixException - if the matrix is non-square or singular.

getPermutation

protected int[] getPermutation()
Returns the permutation associated with the lu decomposition. The entries of the array represent a permutation of the numbers 0, ... , nRows - 1.

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.

Returns:
the permutation


Copyright © 2003-2004 The Apache Software Foundation. All Rights Reserved.