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:
- 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:
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();
arrayLeftDivide
public Matrix arrayLeftDivide(Matrix B)
Element-by-element left division, C = A.\B
arrayLeftDivideEquals
public Matrix arrayLeftDivideEquals(Matrix B)
Element-by-element left division in place, A = A.\B
arrayRightDivide
public Matrix arrayRightDivide(Matrix B)
Element-by-element right division, C = A./B
arrayRightDivideEquals
public Matrix arrayRightDivideEquals(Matrix B)
Element-by-element right division in place, A = A./B
arrayTimes
public Matrix arrayTimes(Matrix B)
Element-by-element multiplication, C = A.*B
arrayTimesEquals
public Matrix arrayTimesEquals(Matrix B)
Element-by-element multiplication in place, A = A.*B
clone
public Object clone()
Clone the Matrix object.
cond
public double cond()
Matrix condition (2 norm)
- ratio of largest to smallest singular value.
constructWithCopy
public static Matrix constructWithCopy(double[][] A)
Construct a matrix from a copy of a 2-D array.
A
- Two-dimensional array of doubles.
copy
public Matrix copy()
Make a deep copy of a matrix
det
public double det()
Matrix determinant
get
public double get(int i,
int j)
Get a single element.
i
- Row index.j
- Column index.
getArray
public double[][] getArray()
Access the internal two-dimensional array.
- Pointer to the two-dimensional array of matrix elements.
getArrayCopy
public double[][] getArrayCopy()
Copy the internal two-dimensional array.
- Two-dimensional array copy of matrix elements.
getColumnDimension
public int getColumnDimension()
Get column dimension.
- n, the number of columns.
getColumnPackedCopy
public double[] getColumnPackedCopy()
Make a one-dimensional column packed copy of the internal array.
- Matrix elements packed in a one-dimensional array by columns.
getMatrix
public Matrix getMatrix(int i0,
int i1,
int j0,
int j1)
Get a submatrix.
i0
- Initial row indexi1
- Final row indexj0
- Initial column indexj1
- Final column index
getMatrix
public Matrix getMatrix(int i0,
int i1,
int[] c)
Get a submatrix.
i0
- Initial row indexi1
- Final row indexc
- Array of column indices.
getMatrix
public Matrix getMatrix(int[] r,
int j0,
int j1)
Get a submatrix.
r
- Array of row indices.
getMatrix
public Matrix getMatrix(int[] r,
int[] c)
Get a submatrix.
r
- Array of row indices.c
- Array of column indices.
getRowDimension
public int getRowDimension()
Get row dimension.
getRowPackedCopy
public double[] getRowPackedCopy()
Make a one-dimensional row packed copy of the internal array.
- Matrix elements packed in a one-dimensional array by rows.
identity
public static Matrix identity(int m,
int n)
Generate identity matrix
m
- Number of rows.n
- Number of colums.
- An m-by-n matrix with ones on the diagonal and zeros elsewhere.
inverse
public Matrix inverse()
Matrix inverse or pseudoinverse
- inverse(A) if A is square, pseudoinverse otherwise.
norm1
public double norm1()
One norm
norm2
public double norm2()
Two norm
normF
public double normF()
Frobenius norm
- sqrt of sum of squares of all elements.
normInf
public double normInf()
Infinity norm
print
public void print(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.
format
- A Formatting object for individual elements.width
- Field width for each column.
java.text.DecimalFormat.setDecimalFormatSymbols
print
public void print(PrintWriter output,
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.
output
- the output stream.format
- A formatting object to format the matrix elementswidth
- Column width.
java.text.DecimalFormat.setDecimalFormatSymbols
print
public void print(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.
output
- Output stream.w
- Column width.d
- Number of digits after the decimal.
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.
w
- Column width.d
- Number of digits after the decimal.
random
public static Matrix random(int m,
int n)
Generate matrix with random elements
m
- Number of rows.n
- Number of colums.
- An m-by-n matrix with uniformly distributed random elements.
rank
public int rank()
Matrix rank
- effective numerical rank, obtained from SVD.
read
public static Matrix read(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.
input
- the input stream.
set
public void set(int i,
int j,
double s)
Set a single element.
i
- Row index.j
- Column index.s
- A(i,j).
setMatrix
public void setMatrix(int i0,
int i1,
int j0,
int j1,
Matrix X)
Set a submatrix.
i0
- Initial row indexi1
- Final row indexj0
- Initial column indexj1
- Final column indexX
- A(i0:i1,j0:j1)
setMatrix
public void setMatrix(int i0,
int i1,
int[] c,
Matrix X)
Set a submatrix.
i0
- Initial row indexi1
- Final row indexc
- Array of column indices.X
- A(i0:i1,c(:))
setMatrix
public void setMatrix(int[] r,
int j0,
int j1,
Matrix X)
Set a submatrix.
r
- Array of row indices.j0
- Initial column indexj1
- Final column indexX
- A(r(:),j0:j1)
setMatrix
public void setMatrix(int[] r,
int[] c,
Matrix X)
Set a submatrix.
r
- Array of row indices.c
- Array of column indices.X
- A(r(:),c(:))
solve
public Matrix solve(Matrix B)
Solve A*X = B
- solution if A is square, least squares solution otherwise
solveTranspose
public Matrix solveTranspose(Matrix B)
Solve X*A = B, which is also A'*X' = B'
- solution if A is square, least squares solution otherwise.
times
public Matrix times(Matrix B)
Linear algebraic matrix multiplication, A * B
times
public Matrix times(double s)
Multiply a matrix by a scalar, C = s*A
timesEquals
public Matrix timesEquals(double s)
Multiply a matrix by a scalar in place, A = s*A
trace
public double trace()
Matrix trace.
- sum of the diagonal elements.
transpose
public Matrix transpose()
Matrix transpose.
uminus
public Matrix uminus()
Unary minus