org.apache.commons.math.linear
Class RealMatrixImpl

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

public class RealMatrixImpl
extends Object
implements RealMatrix, Serializable

Implementation for RealMatrix using a double[][] 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 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.

Version:
$Revision: 1.9 $ $Date: 2003/11/23 20:16:17 $
See Also:
Serialized Form

Constructor Summary
RealMatrixImpl()
          Creates a matrix with no data
RealMatrixImpl(double[] v)
          Create a new (column) RealMatrix using v as the data for the unique column of the v.length x 1 matrix created.
RealMatrixImpl(double[][] d)
          Create a new RealMatrix using the data as the underlying data array.
RealMatrixImpl(int rowDimension, int columnDimension)
          Create a new RealMatrix with the supplied row and column dimensions.
 
Method Summary
 RealMatrix add(RealMatrix m)
          Compute the sum of this and m.
 RealMatrix copy()
          Create a new RealMatrix which is a copy of this.
 double[] getColumn(int col)
          Returns the entries in column number col as an array.
 int getColumnDimension()
          Returns the number of columns in the matrix.
 double[][] getData()
          Returns matrix entries as a two-dimensional array.
 double[][] getDataRef()
          Returns a reference to the underlying data array.
 double getDeterminant()
          Returns the determinant of this matrix.
 double getEntry(int row, int column)
          Returns the entry in the specified row and column.
protected  RealMatrix getIdentity(int dimension)
          Returns dimension x dimension identity matrix.
 double getNorm()
          Returns the maximum absolute row sum norm of the matrix.
 int getRank()
          Returns the rank of the matrix.
 double[] getRow(int row)
          Returns the entries in row number row as an array.
 int getRowDimension()
          Returns the number of rows in the matrix.
 double getTrace()
          Returns the trace of the matrix (the sum of the elements on the main diagonal).
 RealMatrix inverse()
          Returns the inverse of this matrix.
 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.
 RealMatrix multiply(RealMatrix m)
          Returns the result postmultiplying this by m.
 double[] operate(double[] v)
          Returns the result of multiplying this by the vector v.
 RealMatrix preMultiply(double[] v)
          Returns the result of premultiplying this by the vector v.
 RealMatrix scalarAdd(double d)
          Returns the result of adding d to each entry of this.
 RealMatrix 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 the solution vector for a linear system with coefficient matrix = this and constant vector = b.
 RealMatrix solve(RealMatrix b)
          Uses LU decomposition, performing the composition if the matrix has not been decomposed, or if there have been changes to the matrix since the last decomposition.
 RealMatrix subtract(RealMatrix m)
          Compute this minus m.
 String toString()
           
 RealMatrix transpose()
          Returns the transpose of this matrix.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

RealMatrixImpl

public RealMatrixImpl()
Creates a matrix with no data


RealMatrixImpl

public RealMatrixImpl(int rowDimension,
                      int columnDimension)
Create a new RealMatrix 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

RealMatrixImpl

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

The input array is copied, not referenced.

Parameters:
d - data for new matrix

RealMatrixImpl

public RealMatrixImpl(double[] v)
Create a new (column) RealMatrix 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 RealMatrix copy()
Create a new RealMatrix which is a copy of this.

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

add

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

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

subtract

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

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

getRank

public int getRank()
Returns the rank of the matrix.

Specified by:
getRank in interface RealMatrix
Returns:
the rank of this matrix

scalarAdd

public RealMatrix scalarAdd(double d)
Returns the result of adding d to each entry of this.

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

scalarMultiply

public RealMatrix scalarMultiply(double d)
Returns the result multiplying each entry of this by d

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

multiply

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

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

getData

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

Makes a fresh copy of the underlying data.

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

setData

public void setData(double[][] inData)
Overwrites the underlying data for the matrix with a fresh copy of inData.

Specified by:
setData in interface RealMatrix
Parameters:
inData - 2-dimensional array of entries

getDataRef

public double[][] 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

setDataRef

public void setDataRef(double[][] inData)
Overwrites the underlying data for the matrix with a reference to inData.

Does not make a fresh copy of data.

Parameters:
inData - 2-dimensional array of entries

getNorm

public double getNorm()
Description copied from interface: RealMatrix
Returns the maximum absolute row sum norm of the matrix.

Specified by:
getNorm in interface RealMatrix
Returns:
norm

getRow

public double[] getRow(int row)
                throws IllegalArgumentException
Description copied from interface: RealMatrix
Returns the entries in row number row as an array.

Specified by:
getRow in interface RealMatrix
Parameters:
row - the row to be fetched
Returns:
array of entries in the row
Throws:
IllegalArgumentException - if row > rowDimension or row < 1

getColumn

public double[] getColumn(int col)
                   throws IllegalArgumentException
Description copied from interface: RealMatrix
Returns the entries in column number col as an array.

Specified by:
getColumn in interface RealMatrix
Parameters:
col - column to fetch
Returns:
array of entries in the column
Throws:
IllegalArgumentException - if column > columnDimension or column < 1

getEntry

public double getEntry(int row,
                       int column)
                throws IllegalArgumentException
Description copied from interface: RealMatrix
Returns the entry in the specified row and column.

Specified by:
getEntry in interface RealMatrix
Parameters:
row - row location of entry to be fetched
column - column location of entry to be fetched
Returns:
matrix entry in row,column
Throws:
IllegalArgumentException - if entry does not exist

setEntry

public void setEntry(int row,
                     int column,
                     double value)
              throws IllegalArgumentException
Description copied from interface: RealMatrix
Sets the entry in the specified row and column to the specified value.

Specified by:
setEntry in interface RealMatrix
Parameters:
row - row location of entry to be set
column - column location of entry to be set
value - value to set
Throws:
IllegalArgumentException - if entry does not exist

transpose

public RealMatrix transpose()
Description copied from interface: RealMatrix
Returns the transpose of this matrix.

Specified by:
transpose in interface RealMatrix
Returns:
transpose matrix

inverse

public RealMatrix inverse()
                   throws IllegalArgumentException
Description copied from interface: RealMatrix
Returns the inverse of this matrix.

Specified by:
inverse in interface RealMatrix
Returns:
inverse matrix
Throws:
IllegalArgumentException - if this is not invertible

getDeterminant

public double getDeterminant()
                      throws IllegalArgumentException
Description copied from interface: RealMatrix
Returns the determinant of this matrix.

Specified by:
getDeterminant in interface RealMatrix
Returns:
determinant
Throws:
IllegalArgumentException - if matrix is not square

isSquare

public boolean isSquare()
Description copied from interface: RealMatrix
Is this a square matrix?

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

isSingular

public boolean isSingular()
Description copied from interface: RealMatrix
Is this a singular matrix?

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

getRowDimension

public int getRowDimension()
Description copied from interface: RealMatrix
Returns the number of rows in the matrix.

Specified by:
getRowDimension in interface RealMatrix
Returns:
rowDimension

getColumnDimension

public int getColumnDimension()
Description copied from interface: RealMatrix
Returns the number of columns in the matrix.

Specified by:
getColumnDimension in interface RealMatrix
Returns:
columnDimension

getTrace

public double getTrace()
                throws IllegalArgumentException
Description copied from interface: RealMatrix
Returns the trace of the matrix (the sum of the elements on the main diagonal).

Specified by:
getTrace in interface RealMatrix
Returns:
trace
Throws:
IllegalArgumentException - if the matrix is not square

operate

public double[] operate(double[] v)
                 throws IllegalArgumentException
Description copied from interface: RealMatrix
Returns the result of multiplying this by the vector v.

Specified by:
operate in interface RealMatrix
Parameters:
v - vector to operate on
Returns:
resulting vector
Throws:
IllegalArgumentException - if columnDimension != v.length

preMultiply

public RealMatrix preMultiply(double[] v)
                       throws IllegalArgumentException
Description copied from interface: RealMatrix
Returns the result of premultiplying this by the vector v.

Specified by:
preMultiply in interface RealMatrix
Parameters:
v - vector to premultiply by
Returns:
resulting matrix
Throws:
IllegalArgumentException - if rowDimension != v.length

solve

public double[] solve(double[] b)
               throws IllegalArgumentException
Description copied from interface: RealMatrix
Returns the solution vector for a linear system with coefficient matrix = this and constant vector = b.

Specified by:
solve in interface RealMatrix
Parameters:
b - constant vector
Returns:
vector of solution values to AX = b, where A is this
Throws:
IllegalArgumentException - if rowDimension != b.length or matrix is singular

solve

public RealMatrix solve(RealMatrix b)
                 throws IllegalArgumentException
Uses LU decomposition, performing the composition if the matrix has not been decomposed, or if there have been changes to the matrix since the last decomposition.

Specified by:
solve in interface RealMatrix
Parameters:
b - the constant vector
Returns:
solution matrix
Throws:
IllegalArgumentException - if this is singular or dimensions do not match.

LUDecompose

public void LUDecompose()
                 throws IllegalArgumentException
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:
IllegalArgumentException - if the matrix is singular

toString

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

getIdentity

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

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


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