mpv2
Class JamaMatrix

java.lang.Object
  extended by mpv2.AllMatrices
      extended by mpv2.JamaMatrix
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable

public class JamaMatrix
extends AllMatrices
implements java.lang.Cloneable, java.io.Serializable

Jama = Java Matrix class.

The Java Matrix Class provides the fundamental operations of numerical linear algebra. Various constructors create Matrices from two dimensional arrays of double precision floating point numbers. Various "gets" and "sets" provide access to submatrices and matrix elements. Several methods implement basic matrix arithmetic, including matrix addition and multiplication, matrix norms, and element-by-element array operations. Methods for reading and printing matrices are also included. All the operations in this version of the Matrix Class involve real matrices. Complex matrices may be handled in a future version.

Five fundamental matrix decompositions, which consist of pairs or triples of matrices, permutation vectors, and the like, produce results in five decomposition classes. These decompositions are accessed by the Matrix class to compute solutions of simultaneous linear equations, determinants, inverses and other matrix functions. The five decompositions are:

Example of use:

Solve a linear system A x = b and compute the residual norm, ||b - A x||.

      double[][] vals = {{1.,2.,3},{4.,5.,6.},{7.,8.,10.}};
      JamaMatrix A = new JamaMatrix(vals);
      JamaMatrix b = JamaMatrix.random(3,1);
      JamaMatrix x = A.solve(b);
      JamaMatrix r = A.times(x).minus(b);
      double rnorm = r.normInf();

See Also:
Serialized Form

Field Summary
 
Fields inherited from class mpv2.AllMatrices
K, N
 
Constructor Summary
JamaMatrix(AllMatrices B)
          Construct a new matrix from another matrix (of any kind) if argument B is an object of class JamaMatrix this is the same as (deep) copy (or clone).
JamaMatrix(double[][] A)
          Construct a matrix from a 2-D array.
JamaMatrix(double[][] A, int m, int n)
          Construct a matrix quickly without checking arguments.
JamaMatrix(double[] vals, int m)
          Construct a matrix from a one-dimensional packed array
JamaMatrix(int m, int n)
          Construct an m-by-n matrix of zeros.
JamaMatrix(int m, int n, double s)
          Construct an m-by-n constant matrix.
 
Method Summary
 JamaMatrix arrayLeftDivide(JamaMatrix B)
          Element-by-element left division, C = A.
 JamaMatrix arrayLeftDivideEquals(JamaMatrix B)
          Element-by-element left division in place, A = A.
 JamaMatrix arrayRightDivide(JamaMatrix B)
          Element-by-element right division, C = A.
 JamaMatrix arrayRightDivideEquals(JamaMatrix B)
          Element-by-element right division in place, A = A.
 JamaMatrix arrayTimes(JamaMatrix B)
          Element-by-element multiplication, C = A.
 JamaMatrix arrayTimesEquals(JamaMatrix B)
          Element-by-element multiplication in place, A = A.
 CholeskyDecomposition chol()
          Cholesky Decomposition
 java.lang.Object clone()
          Clone the JamaMatrix object.
 double cond()
          Matrix condition (2 norm)
static JamaMatrix constructWithCopy(double[][] A)
          Construct a matrix from a copy of a 2-D array.
 JamaMatrix copy()
          Make a deep copy of a matrix
 double det()
          Matrix determinant
 EigenvalueDecomposition eig()
          Eigenvalue Decomposition
 double get(int i, int j)
          Get a single element.
 void getAll(double[] vals)
          Get all entries of the matrix into argument vals in a column-packed way.
 double[][] getArray()
          Access the internal two-dimensional array.
 double[][] getArrayCopy()
          Copy the internal two-dimensional array.
 int getColumnDimension()
          Get column dimension.
 double[] getColumnPackedCopy()
          Make a one-dimensional column packed copy of the internal array.
 JamaMatrix getMatrix(int[] r, int[] c)
          Get a submatrix.
 JamaMatrix getMatrix(int[] r, int j0, int j1)
          Get a submatrix.
 JamaMatrix getMatrix(int i0, int i1, int[] c)
          Get a submatrix.
 JamaMatrix getMatrix(int i0, int i1, int j0, int j1)
          Get a submatrix.
 void getRow(int rowNumber, double[] x)
          Get a row of the matrix into argument x.
 int getRowDimension()
          Get row dimension.
 double[] getRowPackedCopy()
          Make a one-dimensional row packed copy of the internal array.
static JamaMatrix identity(int m, int n)
          Generate identity matrix
 JamaMatrix inverse()
          Matrix inverse or pseudoinverse
 LUDecomposition lu()
          LU Decomposition
 JamaMatrix minus(JamaMatrix B)
          C = A - B
 JamaMatrix minusEquals(JamaMatrix B)
          A = A - B
 double norm1()
          One norm
 double norm2()
          Two norm
 double normF()
          Frobenius norm
 double normInf()
          Infinity norm
 JamaMatrix plus(JamaMatrix B)
          C = A + B
 JamaMatrix plusEquals(JamaMatrix B)
          A = A + B
 void print(int w, int d)
          Print the matrix to stdout.
 void print(java.text.NumberFormat format, int width)
          Print the matrix to stdout.
 void print(java.io.PrintWriter output, int w, int d)
          Print the matrix to the output stream.
 void print(java.io.PrintWriter output, java.text.NumberFormat format, int width)
          Print the matrix to the output stream.
 QRDecomposition qr()
          QR Decomposition
static JamaMatrix random(int m, int n)
          Generate matrix with random elements
 int rank()
          Matrix rank
static JamaMatrix read(java.io.BufferedReader input)
          Read a matrix from a stream.
 void set(int i, int j, double s)
          Set a single element.
 void setAll(double[] vals)
          Set all entries of the matrix to the supplied new values.
 void setMatrix(int[] r, int[] c, JamaMatrix X)
          Set a submatrix.
 void setMatrix(int[] r, int j0, int j1, JamaMatrix X)
          Set a submatrix.
 void setMatrix(int i0, int i1, int[] c, JamaMatrix X)
          Set a submatrix.
 void setMatrix(int i0, int i1, int j0, int j1, JamaMatrix X)
          Set a submatrix.
 void setRow(int rowNumber, double[] x)
          Copy an array (x) into a row of the matrix.
 JamaMatrix solve(JamaMatrix B)
          Solve A*X = B
 JamaMatrix solveTranspose(JamaMatrix B)
          Solve X*A = B, which is also A'*X' = B'
 SingularValueDecomposition svd()
          Singular Value Decomposition
 JamaMatrix times(double s)
          Multiply a matrix by a scalar, C = s*A
 JamaMatrix times(JamaMatrix B)
          Linear algebraic matrix multiplication, A * B
 JamaMatrix timesEquals(double s)
          Multiply a matrix by a scalar in place, A = s*A
 double trace()
          Matrix trace.
 JamaMatrix transpose()
          JamaMatrix transpose.
 JamaMatrix uminus()
          Unary minus
 
Methods inherited from class mpv2.AllMatrices
addColumn, columnNorm0, columnNorm1, columnNorm2, columnNormInf, eqConstant, eqCopy, eqCopy, eqCopy, eqCopy, eqCopy, eqDifference, eqEProduct, eqEQuotient, eqIdentity, eqInverse, eqIProduct, eqNegate, eqOnes, eqPermuteColumns, eqPermuteRows, eqProduct, eqProduct, eqRandom, eqScaleColumns, eqScaleRows, eqSum, eqTProduct, eqTranspose, eqZeros, getAll, getColumn, getColumn, getK, getN, getRow, getSubMatrix, getSubMatrix, getSubMatrix, getSubMatrix, getValue, innerProduct, lup, pluseqOuterProduct, setColumn, setValue, sumAll, times, times, times, times, transposeTimes, transposeTimes, transposeTimes, transposeTimes
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JamaMatrix

public JamaMatrix(int m,
                  int n)
Construct an m-by-n matrix of zeros.

Parameters:
m - Number of rows.
n - Number of colums.

JamaMatrix

public JamaMatrix(int m,
                  int n,
                  double s)
Construct an m-by-n constant matrix.

Parameters:
m - Number of rows.
n - Number of colums.
s - Fill the matrix with this scalar value.

JamaMatrix

public JamaMatrix(double[][] A)
Construct a matrix from a 2-D array.

Parameters:
A - Two-dimensional array of doubles.
Throws:
java.lang.IllegalArgumentException - All rows must have the same length
See Also:
constructWithCopy(double[][])

JamaMatrix

public JamaMatrix(double[][] A,
                  int m,
                  int n)
Construct a matrix quickly without checking arguments.

Parameters:
A - Two-dimensional array of doubles.
m - Number of rows.
n - Number of colums.

JamaMatrix

public JamaMatrix(double[] vals,
                  int m)
Construct a matrix from a one-dimensional packed array

Parameters:
vals - One-dimensional array of doubles, packed by columns (ala Fortran).
m - Number of rows.
Throws:
java.lang.IllegalArgumentException - Array length must be a multiple of m.

JamaMatrix

public JamaMatrix(AllMatrices B)
Construct a new matrix from another matrix (of any kind) if argument B is an object of class JamaMatrix this is the same as (deep) copy (or clone). Note that argument B may be another kind of matrix, ex. an object of class SparseMatrix or BandMatrix or any subclass of AllMatrices

Parameters:
B - a matrix of any kind, class is a subclass of AllMatrices
See Also:
copy()
Method Detail

getRow

public void getRow(int rowNumber,
                   double[] x)
Get a row of the matrix into argument x.

Overrides:
getRow in class AllMatrices
Parameters:
rowNumber - number of the row in the matrix.
x - is set to the given row of matrix.
Throws:
java.lang.IllegalArgumentException

getAll

public void getAll(double[] vals)
Get all entries of the matrix into argument vals in a column-packed way.

Overrides:
getAll in class AllMatrices
Parameters:
vals - an one-dimensional array where the values are copied into
Throws:
java.lang.IllegalArgumentException

setRow

public void setRow(int rowNumber,
                   double[] x)
Copy an array (x) into a row of the matrix. Legal range of the integer argument is 0 <= n < N.

Overrides:
setRow in class AllMatrices
Parameters:
rowNumber - number of the row in the matrix A.
x - the given row of matrix A.
Throws:
java.lang.IllegalArgumentException

setAll

public void setAll(double[] vals)
Set all entries of the matrix to the supplied new values. If argument is wrong length an IllegalArgumentException is thrown.

Overrides:
setAll in class AllMatrices
Parameters:
vals - One-dimensional array of doubles, packed by columns (ala Fortran).
Throws:
java.lang.IllegalArgumentException

constructWithCopy

public static JamaMatrix constructWithCopy(double[][] A)
Construct a matrix from a copy of a 2-D array.

Parameters:
A - Two-dimensional array of doubles.
Throws:
java.lang.IllegalArgumentException - All rows must have the same length

copy

public JamaMatrix copy()
Make a deep copy of a matrix


clone

public java.lang.Object clone()
Clone the JamaMatrix object.

Overrides:
clone in class java.lang.Object

getArray

public double[][] getArray()
Access the internal two-dimensional array.

Returns:
Pointer to the two-dimensional array of matrix elements.

getArrayCopy

public double[][] getArrayCopy()
Copy the internal two-dimensional array.

Returns:
Two-dimensional array copy of matrix elements.

getColumnPackedCopy

public double[] getColumnPackedCopy()
Make a one-dimensional column packed copy of the internal array.

Returns:
JamaMatrix elements packed in a one-dimensional array by columns.

getRowPackedCopy

public double[] getRowPackedCopy()
Make a one-dimensional row packed copy of the internal array.

Returns:
JamaMatrix elements packed in a one-dimensional array by rows.

getRowDimension

public int getRowDimension()
Get row dimension.

Overrides:
getRowDimension in class AllMatrices
Returns:
m, the number of rows.

getColumnDimension

public int getColumnDimension()
Get column dimension.

Overrides:
getColumnDimension in class AllMatrices
Returns:
n, the number of columns.

get

public double get(int i,
                  int j)
Get a single element.

Specified by:
get in class AllMatrices
Parameters:
i - Row index.
j - Column index.
Returns:
A(i,j)
Throws:
java.lang.ArrayIndexOutOfBoundsException

getMatrix

public JamaMatrix getMatrix(int i0,
                            int i1,
                            int j0,
                            int j1)
Get a submatrix.

Parameters:
i0 - Initial row index
i1 - Final row index
j0 - Initial column index
j1 - Final column index
Returns:
A(i0:i1,j0:j1)
Throws:
java.lang.ArrayIndexOutOfBoundsException - Submatrix indices

getMatrix

public JamaMatrix getMatrix(int[] r,
                            int[] c)
Get a submatrix.

Parameters:
r - Array of row indices.
c - Array of column indices.
Returns:
A(r(:),c(:))
Throws:
java.lang.ArrayIndexOutOfBoundsException - Submatrix indices

getMatrix

public JamaMatrix getMatrix(int i0,
                            int i1,
                            int[] c)
Get a submatrix.

Parameters:
i0 - Initial row index
i1 - Final row index
c - Array of column indices.
Returns:
A(i0:i1,c(:))
Throws:
java.lang.ArrayIndexOutOfBoundsException - Submatrix indices

getMatrix

public JamaMatrix getMatrix(int[] r,
                            int j0,
                            int j1)
Get a submatrix.

Parameters:
r - Array of row indices.
j0 - Initial column index
j1 - Final column index
Returns:
A(r(:),j0:j1)
Throws:
java.lang.ArrayIndexOutOfBoundsException - Submatrix indices

set

public void set(int i,
                int j,
                double s)
Set a single element.

Specified by:
set in class AllMatrices
Parameters:
i - Row index.
j - Column index.
s - A(i,j).
Throws:
java.lang.ArrayIndexOutOfBoundsException

setMatrix

public void setMatrix(int i0,
                      int i1,
                      int j0,
                      int j1,
                      JamaMatrix X)
Set a submatrix.

Parameters:
i0 - Initial row index
i1 - Final row index
j0 - Initial column index
j1 - Final column index
X - A(i0:i1,j0:j1)
Throws:
java.lang.ArrayIndexOutOfBoundsException - Submatrix indices

setMatrix

public void setMatrix(int[] r,
                      int[] c,
                      JamaMatrix X)
Set a submatrix.

Parameters:
r - Array of row indices.
c - Array of column indices.
X - A(r(:),c(:))
Throws:
java.lang.ArrayIndexOutOfBoundsException - Submatrix indices

setMatrix

public void setMatrix(int[] r,
                      int j0,
                      int j1,
                      JamaMatrix X)
Set a submatrix.

Parameters:
r - Array of row indices.
j0 - Initial column index
j1 - Final column index
X - A(r(:),j0:j1)
Throws:
java.lang.ArrayIndexOutOfBoundsException - Submatrix indices

setMatrix

public void setMatrix(int i0,
                      int i1,
                      int[] c,
                      JamaMatrix X)
Set a submatrix.

Parameters:
i0 - Initial row index
i1 - Final row index
c - Array of column indices.
X - A(i0:i1,c(:))
Throws:
java.lang.ArrayIndexOutOfBoundsException - Submatrix indices

transpose

public JamaMatrix transpose()
JamaMatrix transpose.

Returns:
A'

norm1

public double norm1()
One norm

Overrides:
norm1 in class AllMatrices
Returns:
maximum column sum.

norm2

public double norm2()
Two norm

Overrides:
norm2 in class AllMatrices
Returns:
maximum singular value.

normInf

public double normInf()
Infinity norm

Overrides:
normInf in class AllMatrices
Returns:
maximum row sum.

normF

public double normF()
Frobenius norm

Overrides:
normF in class AllMatrices
Returns:
sqrt of sum of squares of all elements.

uminus

public JamaMatrix uminus()
Unary minus

Returns:
-A

plus

public JamaMatrix plus(JamaMatrix B)
C = A + B

Parameters:
B - another matrix
Returns:
A + B

plusEquals

public JamaMatrix plusEquals(JamaMatrix B)
A = A + B

Parameters:
B - another matrix
Returns:
A + B

minus

public JamaMatrix minus(JamaMatrix B)
C = A - B

Parameters:
B - another matrix
Returns:
A - B

minusEquals

public JamaMatrix minusEquals(JamaMatrix B)
A = A - B

Parameters:
B - another matrix
Returns:
A - B

arrayTimes

public JamaMatrix arrayTimes(JamaMatrix B)
Element-by-element multiplication, C = A.*B

Parameters:
B - another matrix
Returns:
A.*B

arrayTimesEquals

public JamaMatrix arrayTimesEquals(JamaMatrix B)
Element-by-element multiplication in place, A = A.*B

Parameters:
B - another matrix
Returns:
A.*B

arrayRightDivide

public JamaMatrix arrayRightDivide(JamaMatrix B)
Element-by-element right division, C = A./B

Parameters:
B - another matrix
Returns:
A./B

arrayRightDivideEquals

public JamaMatrix arrayRightDivideEquals(JamaMatrix B)
Element-by-element right division in place, A = A./B

Parameters:
B - another matrix
Returns:
A./B

arrayLeftDivide

public JamaMatrix arrayLeftDivide(JamaMatrix B)
Element-by-element left division, C = A.\B

Parameters:
B - another matrix
Returns:
A.\B

arrayLeftDivideEquals

public JamaMatrix arrayLeftDivideEquals(JamaMatrix B)
Element-by-element left division in place, A = A.\B

Parameters:
B - another matrix
Returns:
A.\B

times

public JamaMatrix times(double s)
Multiply a matrix by a scalar, C = s*A

Parameters:
s - scalar
Returns:
s*A

timesEquals

public JamaMatrix timesEquals(double s)
Multiply a matrix by a scalar in place, A = s*A

Parameters:
s - scalar
Returns:
replace A by s*A

times

public JamaMatrix times(JamaMatrix B)
Linear algebraic matrix multiplication, A * B

Parameters:
B - another matrix
Returns:
JamaMatrix product, A * B
Throws:
java.lang.IllegalArgumentException - Matrix inner dimensions must agree.

lu

public LUDecomposition lu()
LU Decomposition

Overrides:
lu in class AllMatrices
Returns:
LUDecomposition
See Also:
LUDecomposition

qr

public QRDecomposition qr()
QR Decomposition

Overrides:
qr in class AllMatrices
Returns:
QRDecomposition
See Also:
QRDecomposition

chol

public CholeskyDecomposition chol()
Cholesky Decomposition

Overrides:
chol in class AllMatrices
Returns:
CholeskyDecomposition
See Also:
CholeskyDecomposition

svd

public SingularValueDecomposition svd()
Singular Value Decomposition

Overrides:
svd in class AllMatrices
Returns:
SingularValueDecomposition
See Also:
SingularValueDecomposition

eig

public EigenvalueDecomposition eig()
Eigenvalue Decomposition

Overrides:
eig in class AllMatrices
Returns:
EigenvalueDecomposition
See Also:
EigenvalueDecomposition

solve

public JamaMatrix solve(JamaMatrix B)
Solve A*X = B

Parameters:
B - right hand side
Returns:
solution if A is square, least squares solution otherwise

solveTranspose

public JamaMatrix solveTranspose(JamaMatrix B)
Solve X*A = B, which is also A'*X' = B'

Parameters:
B - right hand side
Returns:
solution if A is square, least squares solution otherwise.

inverse

public JamaMatrix inverse()
Matrix inverse or pseudoinverse

Returns:
inverse(A) if A is square, pseudoinverse otherwise.

det

public double det()
Matrix determinant

Overrides:
det in class AllMatrices
Returns:
determinant

rank

public int rank()
Matrix rank

Overrides:
rank in class AllMatrices
Returns:
effective numerical rank, obtained from SVD.

cond

public double cond()
Matrix condition (2 norm)

Overrides:
cond in class AllMatrices
Returns:
ratio of largest to smallest singular value.

trace

public double trace()
Matrix trace.

Overrides:
trace in class AllMatrices
Returns:
sum of the diagonal elements.

random

public static JamaMatrix random(int m,
                                int n)
Generate matrix with random elements

Parameters:
m - Number of rows.
n - Number of colums.
Returns:
An m-by-n matrix with uniformly distributed random elements.

identity

public static JamaMatrix identity(int m,
                                  int n)
Generate identity matrix

Parameters:
m - Number of rows.
n - Number of colums.
Returns:
An m-by-n matrix with ones on the diagonal and zeros elsewhere.

print

public void print(int w,
                  int d)
Print the matrix to stdout. Line the elements up in columns with a Fortran-like 'Fw.d' style format.

Overrides:
print in class AllMatrices
Parameters:
w - Column width.
d - Number of digits after the decimal.

print

public void print(java.io.PrintWriter output,
                  int w,
                  int d)
Print the matrix to the output stream. Line the elements up in columns with a Fortran-like 'Fw.d' style format.

Overrides:
print in class AllMatrices
Parameters:
output - Output stream.
w - Column width.
d - Number of digits after the decimal.

print

public void print(java.text.NumberFormat format,
                  int width)
Print the matrix to stdout. Line the elements up in columns. Use the format object, and right justify within columns of width characters. Note that is the matrix is to be read back in, you probably will want to use a NumberFormat that is set to US Locale.

Overrides:
print in class AllMatrices
Parameters:
format - A Formatting object for individual elements.
width - Field width for each column.
See Also:
DecimalFormat.setDecimalFormatSymbols(java.text.DecimalFormatSymbols)

print

public void print(java.io.PrintWriter output,
                  java.text.NumberFormat format,
                  int width)
Print the matrix to the output stream. Line the elements up in columns. Use the format object, and right justify within columns of width characters. Note that is the matrix is to be read back in, you probably will want to use a NumberFormat that is set to US Locale.

Overrides:
print in class AllMatrices
Parameters:
output - the output stream.
format - A formatting object to format the matrix elements
width - Column width.
See Also:
DecimalFormat.setDecimalFormatSymbols(java.text.DecimalFormatSymbols)

read

public static JamaMatrix read(java.io.BufferedReader input)
                       throws java.io.IOException
Read a matrix from a stream. The format is the same the print method, so printed matrices can be read back in (provided they were printed using US Locale). Elements are separated by whitespace, all the elements for each row appear on a single line, the last row is followed by a blank line.

Parameters:
input - the input stream.
Throws:
java.io.IOException