public class Matrix extends Object implements Serializable, com.helger.commons.lang.ICloneable<Matrix>
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:
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 ();
| Constructor and Description |
|---|
Matrix(double[] aVals,
int nRows)
Construct a matrix from a one-dimensional packed array
|
Matrix(int nRows,
int nCols)
Construct an nRows-by-nCols matrix of zeros.
|
Matrix(int nRows,
int nCols,
double dValue)
Construct an nRows-by-nCols constant matrix.
|
| Modifier and Type | Method and Description |
|---|---|
Matrix |
arrayLeftDivide(Matrix aMatrix)
Element-by-element left division, C = B.
|
Matrix |
arrayLeftDivideEquals(Matrix aMatrix)
Element-by-element left division in place, A = B.
|
Matrix |
arrayRightDivide(Matrix aMatrix)
Element-by-element right division, C = A.
|
Matrix |
arrayRightDivideEquals(Matrix aMatrix)
Element-by-element right division in place, A = A.
|
Matrix |
arrayTimes(Matrix aMatrix)
Element-by-element multiplication, C = A.
|
Matrix |
arrayTimesEquals(Matrix aMatrix)
Element-by-element multiplication in place, A = A.
|
CholeskyDecomposition |
chol()
Cholesky Decomposition
|
double |
cond()
Matrix condition (2 norm)
|
static Matrix |
constructWithCopy(double[][] aArray)
Construct a matrix from a copy of a 2-D array.
|
double |
det()
Matrix determinant
|
EigenvalueDecomposition |
eig()
Eigenvalue Decomposition
|
boolean |
equals(Object o) |
double |
get(int nRow,
int nCol)
Get a single element.
|
double[][] |
getArrayCopy()
Copy the internal two-dimensional array.
|
Matrix |
getClone() |
int |
getColumnDimension()
Get column dimension.
|
double[] |
getColumnPackedCopy()
Make a one-dimensional column packed copy of the internal array.
|
Matrix |
getMatrix(int[] aRows,
int[] aCols)
Get a submatrix.
|
Matrix |
getMatrix(int[] aRows,
int nStartColumnIndex,
int nEndColumnIndex)
Get a submatrix.
|
Matrix |
getMatrix(int nStartRowIndex,
int nEndRowIndex,
int[] aCols)
Get a submatrix.
|
Matrix |
getMatrix(int nStartRowIndex,
int nEndRowIndex,
int nStartColumnIndex,
int nEndColumnIndex)
Get a submatrix.
|
int |
getRowDimension()
Get row dimension.
|
double[] |
getRowPackedCopy()
Make a one-dimensional row packed copy of the internal array.
|
int |
hashCode() |
static Matrix |
identity(int nRows,
int nCols)
Generate identity matrix
|
double[][] |
internalGetArray()
Access the internal two-dimensional array.
|
Matrix |
inverse()
Matrix inverse or pseudoinverse
|
boolean |
isSymmetrical() |
LUDecomposition |
lu()
LU Decomposition
|
Matrix |
minus(Matrix aMatrix)
C = A - B
|
Matrix |
minusEquals(Matrix aMatrix)
A = A - B
|
double |
norm1()
One norm
|
double |
norm2()
Two norm
|
double |
normF()
Frobenius norm
|
double |
normInf()
Infinity norm
|
Matrix |
plus(Matrix aMatrix)
C = A + B
|
Matrix |
plusEquals(Matrix aMatrix)
A = A + B
|
void |
print(PrintWriter aPW,
int nWidth,
int nFractionDigits)
Print the matrix to the output stream.
|
void |
print(PrintWriter aPW,
NumberFormat aFormat,
int nWidth)
Print the matrix to the output stream.
|
QRDecomposition |
qr()
QR Decomposition
|
static Matrix |
random(int nRows,
int nCols)
Generate matrix with random elements
|
int |
rank()
Matrix rank
|
static Matrix |
read(Reader aReader)
Read a matrix from a stream.
|
void |
set(int nRow,
int nCol,
double dValue)
Set a single element.
|
void |
setMatrix(int[] aRows,
int[] aCols,
Matrix aMatrix)
Set a submatrix.
|
void |
setMatrix(int[] aRows,
int nStartColumnIndex,
int nEndColumnIndex,
Matrix aMatrix)
Set a submatrix.
|
void |
setMatrix(int nStartRowIndex,
int nEndRowIndex,
int[] aCols,
Matrix aMatrix)
Set a submatrix.
|
void |
setMatrix(int nStartRowIndex,
int nEndRowIndex,
int nStartColumnIndex,
int nEndColumnIndex,
Matrix aMatrix)
Set a submatrix.
|
Matrix |
solve(Matrix aMatrix)
Solve A*X = B
|
Matrix |
solveTranspose(Matrix aMatrix)
Solve X*A = B, which is also A'*X' = B'
|
SingularValueDecomposition |
svd()
Singular Value Decomposition
|
Matrix |
times(double s)
Multiply a matrix by a scalar, C = s*A
|
Matrix |
times(Matrix aMatrix)
Linear algebraic matrix multiplication, A * B
|
Matrix |
timesEquals(double s)
Multiply a matrix by a scalar in place, A = s*A
|
double |
trace()
Matrix trace.
|
Matrix |
transpose()
Matrix transpose.
|
Matrix |
uminus()
Unary minus
|
public Matrix(@Nonnegative int nRows, @Nonnegative int nCols)
nRows - Number of rows.nCols - Number of columns.public Matrix(@Nonnegative int nRows, @Nonnegative int nCols, double dValue)
nRows - Number of rows.nCols - Number of columns.dValue - Fill the matrix with this scalar value.public Matrix(@Nonnull double[] aVals, @Nonnegative int nRows)
aVals - One-dimensional array of doubles, packed by columns (ala Fortran).nRows - Number of rows.IllegalArgumentException - Array length must be a multiple of nRows.@Nonnull public static Matrix constructWithCopy(@Nonnull double[][] aArray)
aArray - Two-dimensional array of doubles.IllegalArgumentException - All rows must have the same length@Nonnull @ReturnsMutableCopy public Matrix getClone()
getClone in interface com.helger.commons.lang.ICloneable<Matrix>@Nonnull public double[][] internalGetArray()
@Nonnull @ReturnsMutableCopy public double[][] getArrayCopy()
@Nonnull public double[] getColumnPackedCopy()
@Nonnull public double[] getRowPackedCopy()
@Nonnegative public int getRowDimension()
@Nonnegative public int getColumnDimension()
public boolean isSymmetrical()
true if the matrix is symmetrical, meaning number of
rows and columns is identical.public double get(@Nonnegative int nRow, @Nonnegative int nCol)
nRow - Row index.nCol - Column index.@Nonnull @ReturnsMutableCopy public Matrix getMatrix(@Nonnegative int nStartRowIndex, @Nonnegative int nEndRowIndex, @Nonnegative int nStartColumnIndex, @Nonnegative int nEndColumnIndex)
nStartRowIndex - Initial row indexnEndRowIndex - Final row indexnStartColumnIndex - Initial column indexnEndColumnIndex - Final column indexArrayIndexOutOfBoundsException - Submatrix indices@Nonnull @ReturnsMutableCopy public Matrix getMatrix(@Nonnull int[] aRows, @Nonnull int[] aCols)
aRows - Array of row indices.aCols - Array of column indices.ArrayIndexOutOfBoundsException - Submatrix indices@Nonnull @ReturnsMutableCopy public Matrix getMatrix(@Nonnegative int nStartRowIndex, @Nonnegative int nEndRowIndex, @Nonnull int[] aCols)
nStartRowIndex - Initial row indexnEndRowIndex - Final row indexaCols - Array of column indices.ArrayIndexOutOfBoundsException - Submatrix indices@Nonnull @ReturnsMutableCopy public Matrix getMatrix(@Nonnull int[] aRows, @Nonnegative int nStartColumnIndex, @Nonnegative int nEndColumnIndex)
aRows - Array of row indices.nStartColumnIndex - Initial column indexnEndColumnIndex - Final column indexArrayIndexOutOfBoundsException - Submatrix indicespublic void set(@Nonnegative int nRow, @Nonnegative int nCol, double dValue)
nRow - Row index.nCol - Column index.dValue - A(nRow,nCol).public void setMatrix(@Nonnegative int nStartRowIndex, @Nonnegative int nEndRowIndex, @Nonnegative int nStartColumnIndex, @Nonnegative int nEndColumnIndex, @Nonnull Matrix aMatrix)
nStartRowIndex - Initial row indexnEndRowIndex - Final row indexnStartColumnIndex - Initial column indexnEndColumnIndex - Final column indexaMatrix - Matrix(nStartRowIndex:nEndRowIndex,nStartColumnIndex:nEndColumnIndex
)ArrayIndexOutOfBoundsException - Submatrix indicespublic void setMatrix(@Nonnull int[] aRows, @Nonnull int[] aCols, @Nonnull Matrix aMatrix)
aRows - Array of row indices.aCols - Array of column indices.aMatrix - Matrix(aRows(:),aCols(:))ArrayIndexOutOfBoundsException - Submatrix indicespublic void setMatrix(@Nonnull int[] aRows, @Nonnegative int nStartColumnIndex, @Nonnegative int nEndColumnIndex, @Nonnull Matrix aMatrix)
aRows - Array of row indices.nStartColumnIndex - Initial column indexnEndColumnIndex - Final column indexaMatrix - Matrix(aRows(:),nStartColumnIndex:nEndColumnIndexArrayIndexOutOfBoundsException - Submatrix indicespublic void setMatrix(@Nonnegative int nStartRowIndex, @Nonnegative int nEndRowIndex, @Nonnull int[] aCols, @Nonnull Matrix aMatrix)
nStartRowIndex - Initial row indexnEndRowIndex - Final row indexaCols - Array of column indices.aMatrix - Matrix(nStartRowIndex:nEndRowIndex,aCols(:))ArrayIndexOutOfBoundsException - Submatrix indices@Nonnull @ReturnsMutableCopy @CheckReturnValue public Matrix transpose()
public double norm1()
public double norm2()
public double normInf()
public double normF()
@Nonnull @ReturnsMutableCopy public Matrix plus(@Nonnull Matrix aMatrix)
aMatrix - another matrix@Nonnull public Matrix plusEquals(@Nonnull Matrix aMatrix)
aMatrix - another matrix@Nonnull @ReturnsMutableCopy public Matrix minus(@Nonnull Matrix aMatrix)
aMatrix - another matrix@Nonnull public Matrix minusEquals(@Nonnull Matrix aMatrix)
aMatrix - another matrix@Nonnull @ReturnsMutableCopy public Matrix arrayTimes(@Nonnull Matrix aMatrix)
aMatrix - another matrix@Nonnull public Matrix arrayTimesEquals(@Nonnull Matrix aMatrix)
aMatrix - another matrix@Nonnull @ReturnsMutableCopy public Matrix arrayRightDivide(@Nonnull Matrix aMatrix)
aMatrix - another matrix@Nonnull public Matrix arrayRightDivideEquals(@Nonnull Matrix aMatrix)
aMatrix - another matrix@Nonnull @ReturnsMutableCopy public Matrix arrayLeftDivide(@Nonnull Matrix aMatrix)
aMatrix - another matrix@Nonnull public Matrix arrayLeftDivideEquals(@Nonnull Matrix aMatrix)
aMatrix - another matrix@Nonnull @ReturnsMutableCopy public Matrix times(double s)
s - scalar@Nonnull public Matrix timesEquals(double s)
s - scalar@Nonnull @ReturnsMutableCopy public Matrix times(@Nonnull Matrix aMatrix)
aMatrix - another matrixIllegalArgumentException - Matrix inner dimensions must agree.@Nonnull @ReturnsMutableCopy public LUDecomposition lu()
LUDecomposition@Nonnull @ReturnsMutableCopy public QRDecomposition qr()
QRDecomposition@Nonnull @ReturnsMutableCopy public CholeskyDecomposition chol()
CholeskyDecomposition@Nonnull @ReturnsMutableCopy public SingularValueDecomposition svd()
SingularValueDecomposition@Nonnull @ReturnsMutableCopy public EigenvalueDecomposition eig()
EigenvalueDecomposition@Nonnull public Matrix solve(@Nonnull Matrix aMatrix)
aMatrix - right hand side@Nonnull public Matrix solveTranspose(@Nonnull Matrix aMatrix)
aMatrix - right hand side@Nonnull public Matrix inverse()
public double det()
public int rank()
public double cond()
public double trace()
@Nonnull @ReturnsMutableCopy public static Matrix random(@Nonnegative int nRows, @Nonnegative int nCols)
nRows - Number of rows.nCols - Number of columns.@Nonnull @ReturnsMutableCopy public static Matrix identity(@Nonnegative int nRows, @Nonnegative int nCols)
nRows - Number of rows.nCols - Number of columns.public void print(@Nonnull PrintWriter aPW, @Nonnegative int nWidth, @Nonnegative int nFractionDigits)
aPW - Output stream.nWidth - Column width.nFractionDigits - Number of digits after the decimal.public void print(@Nonnull PrintWriter aPW, @Nonnull NumberFormat aFormat, @Nonnegative int nWidth)
aPW - the output stream.aFormat - A formatting object to format the matrix elementsnWidth - Column width.DecimalFormat.setDecimalFormatSymbols(java.text.DecimalFormatSymbols)@Nonnull public static Matrix read(@Nonnull @WillNotClose Reader aReader) throws IOException
aReader - the input stream.IOException - In case of an I/O errorCopyright © 2014–2021 Philip Helger. All rights reserved.