public class Matrix extends java.lang.Object implements java.lang.Cloneable, java.io.Serializable, RevisionHandler
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();
@author
tag.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 array
|
Matrix(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.
|
Matrix(java.io.Reader r)
Reads a matrix from a reader.
|
Modifier and Type | Method and Description |
---|---|
Matrix |
arrayLeftDivide(Matrix B)
Element-by-element left division, C = A.\B
|
Matrix |
arrayLeftDivideEquals(Matrix B)
Element-by-element left division in place, A = A.\B
|
Matrix |
arrayRightDivide(Matrix B)
Element-by-element right division, C = A./B
|
Matrix |
arrayRightDivideEquals(Matrix B)
Element-by-element right division in place, A = A./B
|
Matrix |
arrayTimes(Matrix B)
Element-by-element multiplication, C = A.*B
|
Matrix |
arrayTimesEquals(Matrix B)
Element-by-element multiplication in place, A = A.*B
|
CholeskyDecomposition |
chol()
Cholesky Decomposition
|
java.lang.Object |
clone()
Clone the Matrix object.
|
double |
cond()
Matrix condition (2 norm)
|
static Matrix |
constructWithCopy(double[][] A)
Construct a matrix from a copy of a 2-D array.
|
Matrix |
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.
|
int |
getColumnDimension()
Get column dimension.
|
double[] |
getColumnPackedCopy()
Make a one-dimensional column packed copy of the internal array.
|
Matrix |
getMatrix(int[] r,
int[] c)
Get a submatrix.
|
Matrix |
getMatrix(int[] r,
int j0,
int j1)
Get a submatrix.
|
Matrix |
getMatrix(int i0,
int i1,
int[] c)
Get a submatrix.
|
Matrix |
getMatrix(int i0,
int i1,
int j0,
int j1)
Get a submatrix.
|
java.lang.String |
getRevision()
Returns the revision string.
|
int |
getRowDimension()
Get row dimension.
|
double[] |
getRowPackedCopy()
Make a one-dimensional row packed copy of the internal array.
|
static Matrix |
identity(int m,
int n)
Generate identity matrix
|
Matrix |
inverse()
Matrix inverse or pseudoinverse
|
boolean |
isSquare()
returns whether the matrix is a square matrix or not.
|
boolean |
isSymmetric()
Returns true if the matrix is symmetric.
|
LUDecomposition |
lu()
LU Decomposition
|
static void |
main(java.lang.String[] args)
Main method for testing this class.
|
Matrix |
minus(Matrix B)
C = A - B
|
Matrix |
minusEquals(Matrix B)
A = A - B
|
double |
norm1()
One norm
|
double |
norm2()
Two norm
|
double |
normF()
Frobenius norm
|
double |
normInf()
Infinity norm
|
static Matrix |
parseMatlab(java.lang.String matlab)
creates a matrix from the given Matlab string.
|
Matrix |
plus(Matrix B)
C = A + B
|
Matrix |
plusEquals(Matrix B)
A = A + B
|
void |
print(int w,
int d)
Print the matrix to stdout.
|
void |
print(java.text.NumberFormat format,
int width)
Print the matrix to stdout.
|
void |
print(java.io.PrintWriter output,
int w,
int d)
Print the matrix to the output stream.
|
void |
print(java.io.PrintWriter output,
java.text.NumberFormat format,
int width)
Print the matrix to the output stream.
|
QRDecomposition |
qr()
QR Decomposition
|
static Matrix |
random(int m,
int n)
Generate matrix with random elements
|
int |
rank()
Matrix rank
|
static Matrix |
read(java.io.BufferedReader input)
Read a matrix from a stream.
|
LinearRegression |
regression(Matrix y,
double ridge)
Performs a (ridged) linear regression.
|
LinearRegression |
regression(Matrix y,
double[] w,
double ridge)
Performs a weighted (ridged) linear regression.
|
void |
set(int i,
int j,
double s)
Set a single element.
|
void |
setMatrix(int[] r,
int[] c,
Matrix X)
Set a submatrix.
|
void |
setMatrix(int[] r,
int j0,
int j1,
Matrix X)
Set a submatrix.
|
void |
setMatrix(int i0,
int i1,
int[] c,
Matrix X)
Set a submatrix.
|
void |
setMatrix(int i0,
int i1,
int j0,
int j1,
Matrix X)
Set a submatrix.
|
Matrix |
solve(Matrix B)
Solve A*X = B
|
Matrix |
solveTranspose(Matrix B)
Solve X*A = B, which is also A'*X' = B'
|
Matrix |
sqrt()
returns the square root of the matrix, i.e., X from the equation
X*X = A.
Steps in the Calculation (see sqrtm in Matlab):perform eigenvalue decomposition [V,D]=eig(A) take the square root of all elements in D (only the ones with positive sign are considered for further computation) S=sqrt(D) calculate the root X=V*S/V, which can be also written as X=(V'\(V*S)')' Note: since this method uses other high-level methods, it generates several instances of matrices. |
SingularValueDecomposition |
svd()
Singular Value Decomposition
|
Matrix |
times(double s)
Multiply a matrix by a scalar, C = s*A
|
Matrix |
times(Matrix B)
Linear algebraic matrix multiplication, A * B
|
Matrix |
timesEquals(double s)
Multiply a matrix by a scalar in place, A = s*A
|
java.lang.String |
toMatlab()
converts the Matrix into a single line Matlab string: matrix is enclosed
by parentheses, rows are separated by semicolon and single cells by
blanks, e.g., [1 2; 3 4].
|
java.lang.String |
toString()
Converts a matrix to a string.
|
double |
trace()
Matrix trace.
|
Matrix |
transpose()
Matrix transpose.
|
Matrix |
uminus()
Unary minus
|
void |
write(java.io.Writer w)
Writes out a matrix.
|
public Matrix(int m, int n)
m
- Number of rows.n
- Number of colums.public Matrix(int m, int n, double s)
m
- Number of rows.n
- Number of colums.s
- Fill the matrix with this scalar value.public Matrix(double[][] A)
A
- Two-dimensional array of doubles.java.lang.IllegalArgumentException
- All rows must have the same lengthconstructWithCopy(double[][])
public Matrix(double[][] A, int m, int n)
A
- Two-dimensional array of doubles.m
- Number of rows.n
- Number of colums.public Matrix(double[] vals, int m)
vals
- One-dimensional array of doubles, packed by columns (ala
Fortran).m
- Number of rows.java.lang.IllegalArgumentException
- Array length must be a multiple of m.public Matrix(java.io.Reader r) throws java.lang.Exception
r
- the reader containing the matrixjava.lang.Exception
- if an error occurswrite(Writer)
public static Matrix constructWithCopy(double[][] A)
A
- Two-dimensional array of doubles.java.lang.IllegalArgumentException
- All rows must have the same lengthpublic Matrix copy()
public java.lang.Object clone()
clone
in class java.lang.Object
public double[][] getArray()
public double[][] getArrayCopy()
public double[] getColumnPackedCopy()
public double[] getRowPackedCopy()
public int getRowDimension()
public int getColumnDimension()
public double get(int i, int j)
i
- Row index.j
- Column index.java.lang.ArrayIndexOutOfBoundsException
public Matrix getMatrix(int i0, int i1, int j0, int j1)
i0
- Initial row indexi1
- Final row indexj0
- Initial column indexj1
- Final column indexjava.lang.ArrayIndexOutOfBoundsException
- Submatrix indicespublic Matrix getMatrix(int[] r, int[] c)
r
- Array of row indices.c
- Array of column indices.java.lang.ArrayIndexOutOfBoundsException
- Submatrix indicespublic Matrix getMatrix(int i0, int i1, int[] c)
i0
- Initial row indexi1
- Final row indexc
- Array of column indices.java.lang.ArrayIndexOutOfBoundsException
- Submatrix indicespublic Matrix getMatrix(int[] r, int j0, int j1)
r
- Array of row indices.j0
- Initial column indexj1
- Final column indexjava.lang.ArrayIndexOutOfBoundsException
- Submatrix indicespublic void set(int i, int j, double s)
i
- Row index.j
- Column index.s
- A(i,j).java.lang.ArrayIndexOutOfBoundsException
public void setMatrix(int i0, int i1, int j0, int j1, Matrix X)
i0
- Initial row indexi1
- Final row indexj0
- Initial column indexj1
- Final column indexX
- A(i0:i1,j0:j1)java.lang.ArrayIndexOutOfBoundsException
- Submatrix indicespublic void setMatrix(int[] r, int[] c, Matrix X)
r
- Array of row indices.c
- Array of column indices.X
- A(r(:),c(:))java.lang.ArrayIndexOutOfBoundsException
- Submatrix indicespublic void setMatrix(int[] r, int j0, int j1, Matrix X)
r
- Array of row indices.j0
- Initial column indexj1
- Final column indexX
- A(r(:),j0:j1)java.lang.ArrayIndexOutOfBoundsException
- Submatrix indicespublic void setMatrix(int i0, int i1, int[] c, Matrix X)
i0
- Initial row indexi1
- Final row indexc
- Array of column indices.X
- A(i0:i1,c(:))java.lang.ArrayIndexOutOfBoundsException
- Submatrix indicespublic boolean isSymmetric()
public boolean isSquare()
public Matrix transpose()
public double norm1()
public double norm2()
public double normInf()
public double normF()
public Matrix uminus()
public Matrix plusEquals(Matrix B)
B
- another matrixpublic Matrix minusEquals(Matrix B)
B
- another matrixpublic Matrix arrayTimes(Matrix B)
B
- another matrixpublic Matrix arrayTimesEquals(Matrix B)
B
- another matrixpublic Matrix arrayRightDivide(Matrix B)
B
- another matrixpublic Matrix arrayRightDivideEquals(Matrix B)
B
- another matrixpublic Matrix arrayLeftDivide(Matrix B)
B
- another matrixpublic Matrix arrayLeftDivideEquals(Matrix B)
B
- another matrixpublic Matrix times(double s)
s
- scalarpublic Matrix timesEquals(double s)
s
- scalarpublic Matrix times(Matrix B)
B
- another matrixjava.lang.IllegalArgumentException
- Matrix inner dimensions must agree.public LUDecomposition lu()
LUDecomposition
public QRDecomposition qr()
QRDecomposition
public CholeskyDecomposition chol()
CholeskyDecomposition
public SingularValueDecomposition svd()
SingularValueDecomposition
public EigenvalueDecomposition eig()
EigenvalueDecomposition
public Matrix solve(Matrix B)
B
- right hand sidepublic Matrix solveTranspose(Matrix B)
B
- right hand sidepublic Matrix inverse()
public Matrix sqrt()
sqrtm
in Matlab):X = 5 -4 1 0 0 -4 6 -4 1 0 1 -4 6 -4 1 0 1 -4 6 -4 0 0 1 -4 5 sqrt(X) = 2 -1 -0 -0 -0 -1 2 -1 0 -0 0 -1 2 -1 0 -0 0 -1 2 -1 -0 -0 -0 -1 2 Matrix m = new Matrix(new double[][]{{5,-4,1,0,0},{-4,6,-4,1,0},{1,-4,6,-4,1},{0,1,-4,6,-4},{0,0,1,-4,5}});
X = 7 10 15 22 sqrt(X) = 1.5667 1.7408 2.6112 4.1779 Matrix m = new Matrix(new double[][]{{7, 10},{15, 22}});
public LinearRegression regression(Matrix y, double ridge)
y
- the dependent variable vectorridge
- the ridge parameterjava.lang.IllegalArgumentException
- if not successfulpublic final LinearRegression regression(Matrix y, double[] w, double ridge)
y
- the dependent variable vectorw
- the array of data point weightsridge
- the ridge parameterjava.lang.IllegalArgumentException
- if the wrong number of weights were
provided.public double det()
public int rank()
public double cond()
public double trace()
public static Matrix random(int m, int n)
m
- Number of rows.n
- Number of colums.public static Matrix identity(int m, int n)
m
- Number of rows.n
- Number of colums.public void print(int w, int d)
w
- Column width.d
- Number of digits after the decimal.public void print(java.io.PrintWriter output, int w, int d)
output
- Output stream.w
- Column width.d
- Number of digits after the decimal.public void print(java.text.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(java.io.PrintWriter output, java.text.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 static Matrix read(java.io.BufferedReader input) throws java.io.IOException
input
- the input stream.java.io.IOException
Matrix(Reader)
,
write(Writer)
public void write(java.io.Writer w) throws java.lang.Exception
w
- the output Writerjava.lang.Exception
- if an error occursMatrix(Reader)
public java.lang.String toString()
toString
in class java.lang.Object
public java.lang.String toMatlab()
public static Matrix parseMatlab(java.lang.String matlab) throws java.lang.Exception
matlab
- the matrix in matlab formatjava.lang.Exception
toMatlab()
public java.lang.String getRevision()
getRevision
in interface RevisionHandler
public static void main(java.lang.String[] args)