Class Matrix
- java.lang.Object
- 
- jhplot.math.kalman.jama.Matrix
 
- 
- All Implemented Interfaces:
- java.io.Serializable, java.lang.Cloneable
 
 
 public class Matrix extends java.lang.Object implements java.lang.Cloneable, java.io.SerializableJama = 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: - 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();
 - See Also:
- Serialized Form
 
- 
- 
Constructor SummaryConstructors Constructor and Description Matrix(double[][] A)Construct a matrix from a 2-D array.Matrix(double[][] A, int m, int n)Construct a matrix quickly without checking arguments.Matrix(double[] vals, int m)Construct a matrix from a one-dimensional packed arrayMatrix(int m, int n)Construct an m-by-n matrix of zeros.Matrix(int m, int n, double s)Construct an m-by-n constant matrix.
 - 
Method SummaryAll Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method and Description MatrixarrayLeftDivide(Matrix B)Element-by-element left division, C = A.\BMatrixarrayLeftDivideEquals(Matrix B)Element-by-element left division in place, A = A.\BMatrixarrayRightDivide(Matrix B)Element-by-element right division, C = A./BMatrixarrayRightDivideEquals(Matrix B)Element-by-element right division in place, A = A./BMatrixarrayTimes(Matrix B)Element-by-element multiplication, C = A.*BMatrixarrayTimesEquals(Matrix B)Element-by-element multiplication in place, A = A.*Bjava.lang.Objectclone()Clone the Matrix object.static MatrixconstructWithCopy(double[][] A)Construct a matrix from a copy of a 2-D array.Matrixcopy()Make a deep copy of a matrixdoubledet()Matrix determinantMatrixgemm(Matrix B, Matrix C, double alpha, double beta)Generalized linear algebraic matrix-matrix multiplication (of A); C = alpha*A x B + beta*C.doubleget(int i, int j)Get a single element.double[][]getArray()Access the internal two-dimensional array.double[][]getArrayCopy()Copy the internal two-dimensional array.intgetColumnDimension()Get column dimension.double[]getColumnPackedCopy()Make a one-dimensional column packed copy of the internal array.MatrixgetMatrix(int[] r, int[] c)Get a submatrix.MatrixgetMatrix(int[] r, int j0, int j1)Get a submatrix.MatrixgetMatrix(int i0, int i1, int[] c)Get a submatrix.MatrixgetMatrix(int i0, int i1, int j0, int j1)Get a submatrix.intgetRowDimension()Get row dimension.double[]getRowPackedCopy()Make a one-dimensional row packed copy of the internal array.Matrixidentity()Generate identity matrixstatic Matrixidentity(int m, int n)Generate identity matrixstatic Matrixidentity(int m, int n, double value)Generate identity matrixMatrixinverse()Matrix inverse or pseudoinverseMatrixminus(Matrix B)C = A - BMatrixminusEquals(Matrix B)A = A - Bdoublenorm1()One normdoublenormF()Frobenius normdoublenormInf()Infinity normMatrixplus(Matrix B)C = A + BMatrixplusEquals(Matrix B)A = A + Bvoidprint(int w, int d)Print the matrix to stdout.voidprint(java.text.NumberFormat format, int width)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.QRDecompositionqr()QR Decompositionstatic Matrixrandom(int m, int n)Generate matrix with random elementsstatic Matrixread(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, Matrix X)Set a submatrix.voidsetMatrix(int[] r, int j0, int j1, Matrix X)Set a submatrix.voidsetMatrix(int i0, int i1, int[] c, Matrix X)Set a submatrix.voidsetMatrix(int i0, int i1, int j0, int j1, Matrix X)Set a submatrix.Matrixsolve(Matrix B)Solve A*X = BMatrixsolveTranspose(Matrix B)Solve X*A = B, which is also A'*X' = B'Matrixtimes(double s)Multiply a matrix by a scalar, C = s*AMatrixtimes(Matrix B)Linear algebraic matrix multiplication, A * BMatrixtimesEquals(double s)Multiply a matrix by a scalar in place, A = s*Ajava.lang.StringtoString()Overrides the ObjecttoString()method.java.lang.StringtoString(int w, int d)Hybrid toString.doubletrace()Matrix trace.Matrixtranspose()Matrix transpose.Matrixuminus()Unary minus
 
- 
- 
- 
Constructor Detail- 
Matrixpublic Matrix(int m, int n)Construct an m-by-n matrix of zeros.- Parameters:
- m- Number of rows.
- n- Number of colums.
 
 - 
Matrixpublic Matrix(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.
 
 - 
Matrixpublic Matrix(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[][])
 
 - 
Matrixpublic Matrix(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.
 
 - 
Matrixpublic Matrix(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.
 
 
- 
 - 
Method Detail- 
constructWithCopypublic static Matrix 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
 
 - 
copypublic Matrix copy() Make a deep copy of a matrix
 - 
clonepublic java.lang.Object clone() Clone the Matrix object.- Overrides:
- clonein class- java.lang.Object
 
 - 
getArraypublic double[][] getArray() Access the internal two-dimensional array.- Returns:
- Pointer to the two-dimensional array of matrix elements.
 
 - 
getArrayCopypublic double[][] getArrayCopy() Copy the internal two-dimensional array.- Returns:
- Two-dimensional array copy of matrix elements.
 
 - 
getColumnPackedCopypublic double[] getColumnPackedCopy() Make a one-dimensional column packed copy of the internal array.- Returns:
- Matrix elements packed in a one-dimensional array by columns.
 
 - 
getRowPackedCopypublic double[] getRowPackedCopy() Make a one-dimensional row packed copy of the internal array.- Returns:
- Matrix elements packed in a one-dimensional array by rows.
 
 - 
getRowDimensionpublic int getRowDimension() Get row dimension.- Returns:
- m, the number of rows.
 
 - 
getColumnDimensionpublic int getColumnDimension() Get column dimension.- Returns:
- n, the number of columns.
 
 - 
getpublic double get(int i, int j)Get a single element.- Parameters:
- i- Row index.
- j- Column index.
- Returns:
- A(i,j)
- Throws:
- java.lang.ArrayIndexOutOfBoundsException
 
 - 
getMatrixpublic Matrix 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
 
 - 
getMatrixpublic Matrix 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
 
 - 
getMatrixpublic Matrix 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
 
 - 
getMatrixpublic Matrix getMatrix(int[] r, int j0, int j1) Get a submatrix.- Parameters:
- r- Array of row indices.
- i0- Initial column index
- i1- Final column index
- Returns:
- A(r(:),j0:j1)
- Throws:
- java.lang.ArrayIndexOutOfBoundsException- Submatrix indices
 
 - 
setpublic void set(int i, int j, double s)Set a single element.- Parameters:
- i- Row index.
- j- Column index.
- s- A(i,j).
- Throws:
- java.lang.ArrayIndexOutOfBoundsException
 
 - 
setMatrixpublic void setMatrix(int i0, int i1, int j0, int j1, Matrix 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
 
 - 
setMatrixpublic void setMatrix(int[] r, int[] c, Matrix 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
 
 - 
setMatrixpublic void setMatrix(int[] r, int j0, int j1, Matrix 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
 
 - 
setMatrixpublic void setMatrix(int i0, int i1, int[] c, Matrix 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
 
 - 
transposepublic Matrix transpose() Matrix transpose.- Returns:
- A'
 
 - 
norm1public double norm1() One norm- Returns:
- maximum column sum.
 
 - 
normInfpublic double normInf() Infinity norm- Returns:
- maximum row sum.
 
 - 
normFpublic double normF() Frobenius norm- Returns:
- sqrt of sum of squares of all elements.
 
 - 
uminuspublic Matrix uminus() Unary minus- Returns:
- -A
 
 - 
plusEqualspublic Matrix plusEquals(Matrix B) A = A + B- Parameters:
- B- another matrix
- Returns:
- A + B
 
 - 
minusEqualspublic Matrix minusEquals(Matrix B) A = A - B- Parameters:
- B- another matrix
- Returns:
- A - B
 
 - 
arrayTimespublic Matrix arrayTimes(Matrix B) Element-by-element multiplication, C = A.*B- Parameters:
- B- another matrix
- Returns:
- A.*B
 
 - 
arrayTimesEqualspublic Matrix arrayTimesEquals(Matrix B) Element-by-element multiplication in place, A = A.*B- Parameters:
- B- another matrix
- Returns:
- A.*B
 
 - 
arrayRightDividepublic Matrix arrayRightDivide(Matrix B) Element-by-element right division, C = A./B- Parameters:
- B- another matrix
- Returns:
- A./B
 
 - 
arrayRightDivideEqualspublic Matrix arrayRightDivideEquals(Matrix B) Element-by-element right division in place, A = A./B- Parameters:
- B- another matrix
- Returns:
- A./B
 
 - 
arrayLeftDividepublic Matrix arrayLeftDivide(Matrix B) Element-by-element left division, C = A.\B- Parameters:
- B- another matrix
- Returns:
- A.\B
 
 - 
arrayLeftDivideEqualspublic Matrix arrayLeftDivideEquals(Matrix B) Element-by-element left division in place, A = A.\B- Parameters:
- B- another matrix
- Returns:
- A.\B
 
 - 
timespublic Matrix times(double s) Multiply a matrix by a scalar, C = s*A- Parameters:
- s- scalar
- Returns:
- s*A
 
 - 
timesEqualspublic Matrix timesEquals(double s) Multiply a matrix by a scalar in place, A = s*A- Parameters:
- s- scalar
- Returns:
- replace A by s*A
 
 - 
timespublic Matrix times(Matrix 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.
 
 - 
qrpublic QRDecomposition qr() QR Decomposition- Returns:
- QRDecomposition
- See Also:
- QRDecomposition
 
 - 
solvepublic Matrix solve(Matrix B) Solve A*X = B- Parameters:
- B- right hand side
- Returns:
- solution if A is square, least squares solution otherwise
 
 - 
solveTransposepublic Matrix solveTranspose(Matrix 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.
 
 - 
inversepublic Matrix inverse() Matrix inverse or pseudoinverse- Returns:
- inverse(A) if A is square, pseudoinverse otherwise.
 
 - 
detpublic double det() Matrix determinant- Returns:
- determinant
 
 - 
tracepublic double trace() Matrix trace.- Returns:
- sum of the diagonal elements.
 
 - 
randompublic static Matrix 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.
 
 - 
identitypublic static Matrix 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.
 
 - 
printpublic 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.
 
 - 
printpublic 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.
 
 - 
printpublic 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)
 
 - 
printpublic 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 elements
- width- Column width.
- See Also:
- DecimalFormat.setDecimalFormatSymbols(java.text.DecimalFormatSymbols)
 
 - 
readpublic static Matrix 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
 
 - 
gemmpublic Matrix gemm(Matrix B, Matrix C, double alpha, double beta) Generalized linear algebraic matrix-matrix multiplication (of A); C = alpha*A x B + beta*C.Matrix shapes: A(m x n), B(n x p), C(m x p). After potential transpositions and shape conformance check; C[i,j] = alpha*Sum(A[i,k] * B[k,j]) + beta*C[i,j], k=0..n-1. Use: 
 C = A.gemm(B, null, 1, 1); // similar to A.times(B);
 C = A.transpose().gemm(B.transpose(), C, 1, -1);- Parameters:
- B- the second source matrix.
- C- the matrix where results are to be stored. Set this parameter to null to indicate that a new result matrix shall be constructed.
- alpha- a scale factor.
- beta- a result scale factor.
- Returns:
- C product matrix (for convenience only).
- Throws:
- java.lang.IllegalArgumentException- if B.rows() != A.columns().
- java.lang.IllegalArgumentException- if C.rows() != A.rows() || C.columns() != B.columns().
- java.lang.IllegalArgumentException- if A == C || B == C.
 
 - 
toStringpublic java.lang.String toString(int w, int d)Hybrid toString.- See Also:
- print(int w, int d)
 
 - 
toStringpublic java.lang.String toString() Overrides the ObjecttoString()method.- Overrides:
- toStringin class- java.lang.Object
- Returns:
- String tab-separated rows on separate lines
 
 - 
identitypublic Matrix identity() Generate identity matrix- Returns:
- An m-by-n matrix with ones on the diagonal and zeros elsewhere.
 
 - 
identitypublic static Matrix identity(int m, int n, double value) 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.
 
 
- 
 
- 
DMelt 3.0 © DataMelt by jWork.ORG