public class MatrixD extends AbstractMatrix
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:
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();
m, n| Constructor and 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 array
|
MatrixD(double[] vals,
int m,
boolean rowMajor)
Construct a matrix from a one-dimensional packed array
|
MatrixD(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.
|
| Modifier and Type | Method and Description |
|---|---|
void |
apply1DFunction(Function1D func)
apply user specified function to each matrix element
|
MatrixD |
arrayLeftDivide(MatrixD B)
Element-by-element left division, C = A.\B
|
MatrixD |
arrayLeftDivideEquals(MatrixD B)
Element-by-element left division in place, A = A.\B
|
MatrixD |
arrayRightDivide(MatrixD B)
Element-by-element right division, C = A./B
|
MatrixD |
arrayRightDivideEquals(MatrixD B)
Element-by-element right division in place, A = A./B
|
MatrixD |
arrayTimes(MatrixD B)
Element-by-element multiplication, C = A.*B
|
MatrixD |
arrayTimesEquals(MatrixD B)
Element-by-element multiplication in place, A = A.*B
|
CholeskyDecomposition |
chol()
Cholesky Decomposition
|
Object |
clone()
Clone the Matrix object.
|
double |
cond()
Matrix condition (2 norm)
|
MatrixD |
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.
|
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.
|
MatrixD |
getMatrix(int[] r,
int[] c)
Get a sub-matrix.
|
MatrixD |
getMatrix(int[] r,
int j0,
int j1)
Get a sub-matrix.
|
MatrixD |
getMatrix(int i0,
int i1,
int[] c)
Get a sub-matrix.
|
MatrixD |
getMatrix(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.
|
MatrixD |
inverse()
Matrix inverse or pseudo-inverse
|
LUDecomposition |
lu()
LU Decomposition
|
MatrixD |
minus(MatrixD B)
C = A - B
|
MatrixD |
minusEquals(MatrixD B)
A = A - B
|
MatrixD |
plus(MatrixD B)
C = A + B
|
MatrixD |
plusEquals(MatrixD B)
A = A + B
|
void |
print(int w,
int d)
Print the matrix to stdout.
|
void |
print(NumberFormat format,
int width)
Print the matrix to stdout.
|
void |
print(PrintWriter output,
int w,
int d)
Print the matrix to the output stream.
|
void |
print(PrintWriter output,
NumberFormat format,
int width)
Print the matrix to the output stream.
|
MatrixD |
pseudoInverse(double condition)
Matrix inversion using the SVD pseudo inverse
|
QRDecomposition |
qr()
QR Decomposition
|
int |
rank()
Matrix rank
|
static MatrixD |
read(BufferedReader input)
Read a matrix from a stream.
|
void |
set(int i,
int j,
double s)
Set a single element.
|
void |
setMatrix(int[] r,
int[] c,
MatrixD X)
Set a submatrix.
|
void |
setMatrix(int[] r,
int j0,
int j1,
MatrixD X)
Set a submatrix.
|
void |
setMatrix(int i0,
int i1,
int[] c,
MatrixD X)
Set a submatrix.
|
void |
setMatrix(int i0,
int i1,
int j0,
int j1,
MatrixD X)
Set a submatrix.
|
MatrixD |
solve(MatrixD B)
Solve A*X = B
|
MatrixD |
solveTranspose(MatrixD B)
Solve X*A = B, which is also A'*X' = B'
|
void |
squareElements()
Square individual matrix elements
|
SingularValueDecomposition |
svd()
Singular Value Decomposition
|
MatrixD |
times(double s)
Multiply a matrix by a scalar, C = s*A
|
MatrixD |
times(MatrixD B)
Linear algebraic matrix multiplication, A * B
|
MatrixD |
timesEquals(double s)
Multiply a matrix by a scalar in place, A = s*A
|
double |
trace()
Matrix trace.
|
MatrixD |
transpose()
Matrix transpose.
|
MatrixD |
uminus()
Unary minus
|
checkMatrixDimensions, getColumnDimension, getRowDimension, norm1, norm2, normF, normInfpublic MatrixD(double[] vals,
int m)
vals - One-dimensional array of doublesm - Number of rows.IllegalArgumentException - Array length must be a multiple of m.public MatrixD(double[] vals,
int m,
boolean rowMajor)
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)IllegalArgumentException - Array length must be a multiple of m.public MatrixD(double[][] A)
A - Two-dimensional array of doubles.IllegalArgumentException - All rows must have the same lengthpublic MatrixD(double[][] A,
int m,
int n)
A - Two-dimensional array of doubles.m - Number of rows.n - Number of columns.public MatrixD(int m,
int n)
m - Number of rows.n - Number of columns.public MatrixD(int m,
int n,
double s)
m - Number of rows.n - Number of columns.s - Fill the matrix with this scalar value.public void apply1DFunction(Function1D func)
func - user-supplied functionpublic MatrixD arrayLeftDivide(MatrixD B)
B - another matrixpublic MatrixD arrayLeftDivideEquals(MatrixD B)
B - another matrixpublic MatrixD arrayRightDivide(MatrixD B)
B - another matrixpublic MatrixD arrayRightDivideEquals(MatrixD B)
B - another matrixpublic MatrixD arrayTimes(MatrixD B)
B - another matrixpublic MatrixD arrayTimesEquals(MatrixD B)
B - another matrixpublic CholeskyDecomposition chol()
CholeskyDecompositionpublic double cond()
public MatrixD copy()
public double det()
public EigenvalueDecomposition eig()
EigenvalueDecompositionpublic double get(int i,
int j)
i - Row index.j - Column indexpublic double[][] getArray()
public double[][] getArrayCopy()
public double[] getColumnPackedCopy()
public MatrixD getMatrix(int i0, int i1, int j0, int j1)
i0 - Initial row indexi1 - Final row indexj0 - Initial column indexj1 - Final column indexArrayIndexOutOfBoundsException - sub-matrix indicespublic MatrixD getMatrix(int i0, int i1, int[] c)
i0 - Initial row indexi1 - Final row indexc - Array of column indices.ArrayIndexOutOfBoundsException - Submatrix indicespublic MatrixD getMatrix(int[] r, int j0, int j1)
r - Array of row indices.j0 - Initial column indexj1 - Final column indexArrayIndexOutOfBoundsException - Submatrix indicespublic MatrixD getMatrix(int[] r, int[] c)
r - Array of row indices.c - Array of column indices.ArrayIndexOutOfBoundsException - Submatrix indicespublic double[] getRowPackedCopy()
public MatrixD inverse()
public LUDecomposition lu()
LUDecompositionpublic MatrixD minusEquals(MatrixD B)
B - another matrixpublic MatrixD plusEquals(MatrixD B)
B - another matrixpublic void print(int w,
int d)
w - Column width.d - Number of digits after the decimal.public void print(NumberFormat format, int width)
format - A Formatting object for individual elements.width - Field width for each column.DecimalFormat.setDecimalFormatSymbols(java.text.DecimalFormatSymbols)public void print(PrintWriter output, int w, int d)
output - Output stream.w - Column width.d - Number of digits after the decimal.public void print(PrintWriter output, NumberFormat format, int width)
output - the output stream.format - A formatting object to format the matrix elementswidth - Column width.DecimalFormat.setDecimalFormatSymbols(java.text.DecimalFormatSymbols)public MatrixD pseudoInverse(double condition)
condition - condition numberpublic QRDecomposition qr()
QRDecompositionpublic int rank()
public void set(int i,
int j,
double s)
i - Row index.j - Column index.s - A(i,j).public void setMatrix(int i0,
int i1,
int j0,
int j1,
MatrixD X)
i0 - Initial row indexi1 - Final row indexj0 - Initial column indexj1 - Final column indexX - A(i0:i1,j0:j1)ArrayIndexOutOfBoundsException - Submatrix indicespublic void setMatrix(int i0,
int i1,
int[] c,
MatrixD X)
i0 - Initial row indexi1 - Final row indexc - Array of column indices.X - A(i0:i1,c(:))ArrayIndexOutOfBoundsException - Submatrix indicespublic void setMatrix(int[] r,
int j0,
int j1,
MatrixD X)
r - Array of row indices.j0 - Initial column indexj1 - Final column indexX - A(r(:),j0:j1)ArrayIndexOutOfBoundsException - Submatrix indicespublic void setMatrix(int[] r,
int[] c,
MatrixD X)
r - Array of row indices.c - Array of column indices.X - A(r(:),c(:))ArrayIndexOutOfBoundsException - Submatrix indicespublic MatrixD solve(MatrixD B)
B - right hand sidepublic MatrixD solveTranspose(MatrixD B)
B - right hand sidepublic void squareElements()
public SingularValueDecomposition svd()
SingularValueDecompositionpublic MatrixD times(double s)
s - scalarpublic MatrixD times(MatrixD B)
B - another matrixIllegalArgumentException - Matrix inner dimensions must agree.public MatrixD timesEquals(double s)
s - scalarpublic double trace()
public MatrixD transpose()
public MatrixD uminus()
public static MatrixD read(BufferedReader input) throws IOException
input - the input stream.IOException - in case of troubles ;-)Copyright © 2020 GSI Helmholtzzentrum für Schwerionenforschung GmbH. All rights reserved.