Class MatrixD
- java.lang.Object
-
- de.gsi.math.matrix.AbstractMatrix
-
- de.gsi.math.matrix.MatrixD
-
- All Implemented Interfaces:
Matrix,java.io.Serializable,java.lang.Cloneable
public class MatrixD extends AbstractMatrix
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 sub-matrices 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:
- Cholesky Decomposition of symmetric, positive definite matrices.
- LU Decomposition of rectangular matrices.
- QR Decomposition of rectangular matrices.
- Singular Value Decomposition of rectangular matrices.
- Eigenvalue Decomposition of both symmetric and nonsymmetric square matrices.
- 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. } }; Matrix A = new Matrix(vals); Matrix b = Matrix.random(3, 1); Matrix x = A.solve(b); Matrix r = A.times(x).minus(b); double rnorm = r.normInf();
- Version:
- 5 August 1998
- Author:
- The MathWorks, Inc. and the National Institute of Standards and Technology.
- See Also:
- Serialized Form
-
-
Field Summary
-
Fields inherited from class de.gsi.math.matrix.AbstractMatrix
m, n
-
-
Constructor Summary
Constructors Constructor Description MatrixD(double[][] A)Construct a matrix from a 2-D array.MatrixD(double[][] A, int m, int n)Construct a matrix quickly without checking arguments.MatrixD(double[] vals, int m)Construct a matrix from a one-dimensional packed arrayMatrixD(double[] vals, int m, boolean rowMajor)Construct a matrix from a one-dimensional packed arrayMatrixD(int m, int n)Construct an m-by-n matrix of zeros.MatrixD(int m, int n, double s)Construct an m-by-n constant matrix.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidapply1DFunction(Function1D func)apply user specified function to each matrix elementMatrixDarrayLeftDivide(MatrixD B)Element-by-element left division, C = A.\BMatrixDarrayLeftDivideEquals(MatrixD B)Element-by-element left division in place, A = A.\BMatrixDarrayRightDivide(MatrixD B)Element-by-element right division, C = A./BMatrixDarrayRightDivideEquals(MatrixD B)Element-by-element right division in place, A = A./BMatrixDarrayTimes(MatrixD B)Element-by-element multiplication, C = A.*BMatrixDarrayTimesEquals(MatrixD B)Element-by-element multiplication in place, A = A.*BCholeskyDecompositionchol()Cholesky Decompositionjava.lang.Objectclone()Clone the Matrix object.doublecond()Matrix condition (2 norm)MatrixDcopy()Make a deep copy of a matrixdoubledet()Matrix determinantEigenvalueDecompositioneig()Eigenvalue Decompositiondoubleget(int i, int j)Get a single element.double[][]getArray()Access the internal two-dimensional array.double[][]getArrayCopy()Copy the internal two-dimensional array.double[]getColumnPackedCopy()Make a one-dimensional column packed copy of the internal array.MatrixDgetMatrix(int[] r, int[] c)Get a sub-matrix.MatrixDgetMatrix(int[] r, int j0, int j1)Get a sub-matrix.MatrixDgetMatrix(int i0, int i1, int[] c)Get a sub-matrix.MatrixDgetMatrix(int i0, int i1, int j0, int j1)Get a sub-matrix.double[]getRowPackedCopy()Make a one-dimensional row Major copy of the internal array.MatrixDinverse()Matrix inverse or pseudo-inverseLUDecompositionlu()LU DecompositionMatrixDminus(MatrixD B)C = A - BMatrixDminusEquals(MatrixD B)A = A - BMatrixDplus(MatrixD B)C = A + BMatrixDplusEquals(MatrixD B)A = A + Bvoidprint(int w, int d)Print the matrix to stdout.voidprint(java.io.PrintWriter output, int w, int d)Print the matrix to the output stream.voidprint(java.io.PrintWriter output, java.text.NumberFormat format, int width)Print the matrix to the output stream.voidprint(java.text.NumberFormat format, int width)Print the matrix to stdout.MatrixDpseudoInverse(double condition)Matrix inversion using the SVD pseudo inverseQRDecompositionqr()QR Decompositionintrank()Matrix rankstatic MatrixDread(java.io.BufferedReader input)Read a matrix from a stream.voidset(int i, int j, double s)Set a single element.voidsetMatrix(int[] r, int[] c, MatrixD X)Set a submatrix.voidsetMatrix(int[] r, int j0, int j1, MatrixD X)Set a submatrix.voidsetMatrix(int i0, int i1, int[] c, MatrixD X)Set a submatrix.voidsetMatrix(int i0, int i1, int j0, int j1, MatrixD X)Set a submatrix.MatrixDsolve(MatrixD B)Solve A*X = BMatrixDsolveTranspose(MatrixD B)Solve X*A = B, which is also A'*X' = B'voidsquareElements()Square individual matrix elementsSingularValueDecompositionsvd()Singular Value DecompositionMatrixDtimes(double s)Multiply a matrix by a scalar, C = s*AMatrixDtimes(MatrixD B)Linear algebraic matrix multiplication, A * BMatrixDtimesEquals(double s)Multiply a matrix by a scalar in place, A = s*Adoubletrace()Matrix trace.MatrixDtranspose()Matrix transpose.MatrixDuminus()Unary minus-
Methods inherited from class de.gsi.math.matrix.AbstractMatrix
checkMatrixDimensions, getColumnDimension, getRowDimension, norm1, norm2, normF, normInf
-
-
-
-
Constructor Detail
-
MatrixD
public MatrixD(double[] vals, int m)Construct a matrix from a one-dimensional packed array- Parameters:
vals- One-dimensional array of doublesm- Number of rows.- Throws:
java.lang.IllegalArgumentException- Array length must be a multiple of m.
-
MatrixD
public MatrixD(double[] vals, int m, boolean rowMajor)Construct a matrix from a one-dimensional packed array- Parameters:
vals- One-dimensional array of doublesm- Number of rows.rowMajor- true: data is stored row-wise (C/C++), false: data is stored column-wise (Fortran)- Throws:
java.lang.IllegalArgumentException- Array length must be a multiple of m.
-
MatrixD
public MatrixD(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
-
MatrixD
public MatrixD(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 columns.
-
MatrixD
public MatrixD(int m, int n)Construct an m-by-n matrix of zeros.- Parameters:
m- Number of rows.n- Number of columns.
-
MatrixD
public MatrixD(int m, int n, double s)Construct an m-by-n constant matrix.- Parameters:
m- Number of rows.n- Number of columns.s- Fill the matrix with this scalar value.
-
-
Method Detail
-
apply1DFunction
public void apply1DFunction(Function1D func)
apply user specified function to each matrix element- Parameters:
func- user-supplied function
-
arrayLeftDivide
public MatrixD arrayLeftDivide(MatrixD B)
Element-by-element left division, C = A.\B- Parameters:
B- another matrix- Returns:
- A.\B
-
arrayLeftDivideEquals
public MatrixD arrayLeftDivideEquals(MatrixD B)
Element-by-element left division in place, A = A.\B- Parameters:
B- another matrix- Returns:
- A.\B
-
arrayRightDivide
public MatrixD arrayRightDivide(MatrixD B)
Element-by-element right division, C = A./B- Parameters:
B- another matrix- Returns:
- A./B
-
arrayRightDivideEquals
public MatrixD arrayRightDivideEquals(MatrixD B)
Element-by-element right division in place, A = A./B- Parameters:
B- another matrix- Returns:
- A./B
-
arrayTimes
public MatrixD arrayTimes(MatrixD B)
Element-by-element multiplication, C = A.*B- Parameters:
B- another matrix- Returns:
- A.*B
-
arrayTimesEquals
public MatrixD arrayTimesEquals(MatrixD B)
Element-by-element multiplication in place, A = A.*B- Parameters:
B- another matrix- Returns:
- A.*B
-
chol
public CholeskyDecomposition chol()
Cholesky Decomposition- Returns:
- CholeskyDecomposition
- See Also:
CholeskyDecomposition
-
clone
public java.lang.Object clone()
Clone the Matrix object.- Overrides:
clonein classjava.lang.Object
-
cond
public double cond()
Matrix condition (2 norm)- Returns:
- ratio of largest to smallest singular value.
-
copy
public MatrixD copy()
Make a deep copy of a matrix- Returns:
- copy of matrix
-
det
public double det()
Matrix determinant- Returns:
- determinant
-
eig
public EigenvalueDecomposition eig()
Eigenvalue Decomposition- Returns:
- EigenvalueDecomposition
- See Also:
EigenvalueDecomposition
-
get
public double get(int i, int j)Get a single element.- Parameters:
i- Row index.j- Column index- Returns:
- A(i,j)
-
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:
- Matrix elements packed in a one-dimensional array by columns.
-
getMatrix
public MatrixD getMatrix(int i0, int i1, int j0, int j1)
Get a sub-matrix.- Parameters:
i0- Initial row indexi1- Final row indexj0- Initial column indexj1- Final column index- Returns:
- A(i0:i1,j0:j1)
- Throws:
java.lang.ArrayIndexOutOfBoundsException- sub-matrix indices
-
getMatrix
public MatrixD getMatrix(int i0, int i1, int[] c)
Get a sub-matrix.- Parameters:
i0- Initial row indexi1- Final row indexc- Array of column indices.- Returns:
- A(i0:i1,c(:))
- Throws:
java.lang.ArrayIndexOutOfBoundsException- Submatrix indices
-
getMatrix
public MatrixD getMatrix(int[] r, int j0, int j1)
Get a sub-matrix.- Parameters:
r- Array of row indices.j0- Initial column indexj1- Final column index- Returns:
- A(r(:),j0:j1)
- Throws:
java.lang.ArrayIndexOutOfBoundsException- Submatrix indices
-
getMatrix
public MatrixD getMatrix(int[] r, int[] c)
Get a sub-matrix.- Parameters:
r- Array of row indices.c- Array of column indices.- Returns:
- A(r(:),c(:))
- Throws:
java.lang.ArrayIndexOutOfBoundsException- Submatrix indices
-
getRowPackedCopy
public double[] getRowPackedCopy()
Make a one-dimensional row Major copy of the internal array.- Returns:
- Matrix elements packed in a one-dimensional array by rows.
-
inverse
public MatrixD inverse()
Matrix inverse or pseudo-inverse- Returns:
- inverse(A) if A is square, pseudo-inverse otherwise.
-
lu
public LUDecomposition lu()
LU Decomposition- Returns:
- LUDecomposition
- See Also:
LUDecomposition
-
minusEquals
public MatrixD minusEquals(MatrixD B)
A = A - B- Parameters:
B- another matrix- Returns:
- A - B
-
plusEquals
public MatrixD plusEquals(MatrixD B)
A = A + B- Parameters:
B- another matrix- Returns:
- A + B
-
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.- Parameters:
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.- 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, 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.- Parameters:
output- Output stream.w- Column width.d- Number of digits after the decimal.
-
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.- Parameters:
output- the output stream.format- A formatting object to format the matrix elementswidth- Column width.- See Also:
DecimalFormat.setDecimalFormatSymbols(java.text.DecimalFormatSymbols)
-
pseudoInverse
public MatrixD pseudoInverse(double condition)
Matrix inversion using the SVD pseudo inverse- Parameters:
condition- condition number- Returns:
- inverse matrix
-
qr
public QRDecomposition qr()
QR Decomposition- Returns:
- QRDecomposition
- See Also:
QRDecomposition
-
rank
public int rank()
Matrix rank- Returns:
- effective numerical rank, obtained from SVD.
-
set
public void set(int i, int j, double s)Set a single element.- Parameters:
i- Row index.j- Column index.s- A(i,j).
-
setMatrix
public void setMatrix(int i0, int i1, int j0, int j1, MatrixD X)Set a submatrix.- Parameters:
i0- Initial row indexi1- Final row indexj0- Initial column indexj1- Final column indexX- A(i0:i1,j0:j1)- Throws:
java.lang.ArrayIndexOutOfBoundsException- Submatrix indices
-
setMatrix
public void setMatrix(int i0, int i1, int[] c, MatrixD X)Set a submatrix.- Parameters:
i0- Initial row indexi1- Final row indexc- Array of column indices.X- A(i0:i1,c(:))- Throws:
java.lang.ArrayIndexOutOfBoundsException- Submatrix indices
-
setMatrix
public void setMatrix(int[] r, int j0, int j1, MatrixD X)Set a submatrix.- Parameters:
r- Array of row indices.j0- Initial column indexj1- Final column indexX- A(r(:),j0:j1)- Throws:
java.lang.ArrayIndexOutOfBoundsException- Submatrix indices
-
setMatrix
public void setMatrix(int[] r, int[] c, MatrixD 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
-
solve
public MatrixD solve(MatrixD B)
Solve A*X = B- Parameters:
B- right hand side- Returns:
- solution if A is square, least squares solution otherwise
-
solveTranspose
public MatrixD solveTranspose(MatrixD 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.
-
squareElements
public void squareElements()
Square individual matrix elements
-
svd
public SingularValueDecomposition svd()
Singular Value Decomposition- Returns:
- SingularValueDecomposition
- See Also:
SingularValueDecomposition
-
times
public MatrixD times(double s)
Multiply a matrix by a scalar, C = s*A- Parameters:
s- scalar- Returns:
- s*A
-
times
public MatrixD times(MatrixD B)
Linear algebraic matrix multiplication, A * B- Parameters:
B- another matrix- Returns:
- Matrix product, A * B
- Throws:
java.lang.IllegalArgumentException- Matrix inner dimensions must agree.
-
timesEquals
public MatrixD timesEquals(double s)
Multiply a matrix by a scalar in place, A = s*A- Parameters:
s- scalar- Returns:
- replace A by s*A
-
trace
public double trace()
Matrix trace.- Returns:
- sum of the diagonal elements.
-
transpose
public MatrixD transpose()
Matrix transpose.- Returns:
- A^{T}
-
uminus
public MatrixD uminus()
Unary minus- Returns:
- -A
-
read
public static MatrixD 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.- Returns:
- matrix object that has been recovered from file
- Throws:
java.io.IOException- in case of troubles ;-)
-
-