public class Matrix extends Object
Modifier and Type | Field and Description |
---|---|
protected int |
columns
Number of columns of this matrix.
|
protected double |
dominantEigenvalue
The dominant eigenvalue of this matrix.
|
protected double[] |
dominantEigenvector
An eigenvector associated to the dominant eigenvalue of this matrix.
|
protected EigenvalueDecomposition |
eigenvalueDecomposition
The eigenvalue decomposition of this matrix.
|
static double |
EPSILON
Constant which defines the corridor around 0.0 where a number of type double
is considered as to be zero.
|
protected double[][] |
matrix
Entries of this matrix.
|
protected int |
rows
Number of rows of this matrix.
|
Constructor and Description |
---|
Matrix(double[] vector)
Constructs a matrix (1 × n) matrix from the given one-dimensional array.
|
Matrix(double[][] matrix)
Constructs a matrix from the given two-dimensional array.
|
Matrix(double[][] matrix,
int rows,
int columns)
Constructs a matrix from the given two-dimensional array.
|
Matrix(int[][] matrix)
Constructs a matrix from the given two-dimensional array.
|
Matrix(int rows,
int columns)
Constructs a zero matrix with the given rows and columns.
|
Modifier and Type | Method and Description |
---|---|
Matrix |
add(Matrix b)
Returns the sum of this matrix with the given matrix b.
|
Matrix |
adjugate()
Returns the adjugate of this matrix.
|
double |
cofactor(int i,
int j)
Returns the cofactor Cij = (-1)i + j Mij
of this matrix, where Mij denotes the minor.
|
double |
cond()
Returns the two norm condition number defined as the ratio
smax/smin
of the maximum and the minimum singular value of this matrix.
|
Matrix |
copy()
Returns a copy of this matrix.
|
static Matrix |
createIdentity(int n)
This method creates an (n x n) unity matrix.
|
static Matrix |
createZero(int m,
int n)
Returns the zero (m × n) matrix.
|
Matrix[] |
decomposeQR()
Returns the QR decomposition of this matrix as a pair of matrices.
|
double |
det()
Computes the determinant of this matrix.
|
boolean |
equals(double[] a)
Returns true if and only if all entries of this matrix equal the entries of the specified column vector.
|
boolean |
equals(double[][] a)
Returns true if and only if all entries of this matrix equal the entries of the specified matrix.
|
boolean |
equals(Matrix a)
Returns true if and only if all entries of this matrix equal the entries of the specified matrix.
|
double[] |
getColumn(int j)
Returns the specified column of this matrix.
|
int |
getColumns()
Returns the number of columns of this matrix.
|
double |
getDominantEigenvalue()
Returns the dominant eigenvalue of this matrix with an error
EPSILON . |
double |
getDominantEigenvalue(double error)
Returns the dominant eigenvalue of this matrix with the specified error of approximation.
|
double[] |
getDominantEigenvector()
Returns an eigenvector associated to the dominant eigenvalue of this
matrix with the error
EPSILON of approximation. |
double[] |
getDominantEigenvector(double error)
Returns an eigenvector associated to the dominant eigenvalue of this
matrix with the specified error of approximation.
|
Matrix |
getEigenvectors()
Returns a matrix whose columns are the eigenvectors of this matrix.
|
double[] |
getImagEigenvalues()
Returns an array consisting of the imaginary parts of the eigenvalues of this
matrix.
|
double[][] |
getMatrix()
Returns this matrix as a two-dimensional array.
|
static double |
getNorm(double[] v)
Returns the norm |v| of the vector v.
|
double[] |
getRealEigenvalues()
Returns an array consisting of the real parts of the eigenvalues of this
matrix.
|
double[] |
getRow(int i)
Returns the specified row of this matrix.
|
int |
getRows()
Returns the number of rows of this matrix.
|
double |
getValue(int row,
int col)
Returns the value of the matrix position given by the parameters row and col
in the usual mathematical convention, that is, both indices are
counted from 1 and terminate at the maximum number of rows or columns, respectively.
|
Matrix |
inverse()
Returns the inverse of this matrix.
|
boolean |
isSquare()
Returns whether this matrix is square.
|
boolean |
isSymmetric()
Returns whether this matrix is symmetric.
|
static double[] |
minus(double[] v,
double[] w)
Returns the subtraction of the vectors v and w.
|
Matrix |
minus(Matrix b)
Returns the difference of this matrix with the given matrix b.
|
static double |
multiply(double[] v,
double[] w)
Returns the scalar product of the vectors v and w.
|
static double[] |
multiply(double a,
double[] v)
Returns the product a*v.
|
Matrix |
negative()
Returns the negative of this matrix.
|
double |
norm2()
Returns the two norm ||A||2 of this matrix.
|
Matrix |
plus(Matrix b)
Returns the sum of this matrix with the given matrix b.
|
Matrix |
pow(int n)
Computes the n-th power of this matrix.
|
int |
rank()
Returns the rank of this matrix.
|
void |
setValue(int row,
int col,
double value)
Sets the value into the matrix position given by the parameters row and col
in the usual mathematical convention, that is, both indices are
counted from 1 and terminate at the maximum number of rows or columns, respectively.
|
Matrix |
solve(Matrix b)
This method solves the n linear equation Ax = b,
where A is this matrix and b the given vector.
|
static double[][] |
tensor(double[][][] a)
Returns the tensor product of the matrices a[0], a[1], ..., a[n-1],
i.e., a[0] ⊗ a[1] ... a[n-2] ⊗ a[n-1].
|
static Matrix |
tensor(Matrix[] a)
Returns the tensor product of the matrices a[0], a[1], ..., a[n-1],
i.e., a[0] ⊗ a[1] ... a[n-2] ⊗ a[n-1].
|
double[] |
times(double[] v)
Returns the (right) product of this matrix with the given column vector v.
|
Matrix |
times(Matrix b)
Returns the (right) product of this matrix with the given matrix b.
|
String |
toHTML()
Returns a String representation of this matrix as an HTML table.
|
String |
toHTML(String align)
Returns a String representation of this matrix as an HTML table where each
entry is aligned according to the specified value.
|
String |
toHTML(String align,
boolean showZeros)
Returns a String representation of this matrix as an HTML table where each
entry is aligned according to the specified value.
|
String |
toString()
Returns a String representation of this matrix.
|
double |
trace()
Returns the trace of this matrix.
|
Matrix |
transpose()
Returns the transpose of this matrix.
|
void |
transposeInPlace()
Transposes this matrix in place, that is, without using additional space
to create a new matrix.
|
public static final double EPSILON
x
to zero
should therefore be checked by
if (Math.abs(x) < Matrix.EPSILON) ...instead of
if (x==.0) ...
because of possible rounding errors.
For instance, this is important to decide whether a matrix is invertible.protected final int rows
protected final int columns
protected final double[][] matrix
protected double dominantEigenvalue
getDominantEigenvalue()
.protected double[] dominantEigenvector
getDominantEigenvalue()
.protected EigenvalueDecomposition eigenvalueDecomposition
public Matrix(int rows, int columns)
rows
- the number of rows of this matrixcolumns
- the number of columns of this matrixpublic Matrix(double[][] matrix)
matrix
- the matrix entries as an arraypublic Matrix(int[][] matrix)
matrix
- the matrix entries as an arraypublic Matrix(double[][] matrix, int rows, int columns)
matrix
- the matrix entries as an arrayrows
- number of rowscolumns
- number of columnspublic Matrix(double[] vector)
vector
- the matrix entries as an arraypublic boolean equals(Matrix a)
a
- a matrixpublic boolean equals(double[][] a)
a
- a two-dimensional array viewed as a matrixpublic boolean equals(double[] a)
a
- array viewed as a column vectorpublic double[] getRow(int i)
i
- row numberArrayIndexOutOfBoundsException
- if the index is out of boundspublic double[] getColumn(int j)
j
- column numberArrayIndexOutOfBoundsException
- if the index is out of boundspublic int getRows()
public int getColumns()
public double[][] getMatrix()
public double getValue(int row, int col)
row
- the row index (counting from 1) to be setcol
- the column index (counting from 1) to be setpublic void setValue(int row, int col, double value)
row
- the row index (counting from 1) to be setcol
- the column index (counting from 1) to be setvalue
- the value to be setpublic boolean isSquare()
public boolean isSymmetric()
public Matrix copy()
public double norm2()
||A||2 = max||x||=1 ||Ax||
where ||x|| is the usual Euclidean norm for vectors. In fact, ||A||2 is the maximum singular value of A. This method obtains the norm from thesingular value decomposition
(SVD) of this matrix. This method is convenient to be invoked
if only the norm of the matrix is desired. Since, however,
in particular for large matrices it requires
substantial computational effort, you may be considering to apply more efficiently
the class SingularValueDecomposition
and its methods.SingularValueDecomposition
,
SingularValueDecomposition.norm2()
public int rank()
Singular value decomposition
(SVD) of this matrix.
This method is convenient to be invoked
if only the rank of the matrix is desired. Since, however,
in particular for large matrices it requires
substantial computational effort, you may be considering to apply more efficiently
the class SingularValueDecomposition
and its methods.SingularValueDecomposition
,
SingularValueDecomposition.rank()
public double cond()
Singular value decomposition
(SVD) of this matrix.
This method is convenient to be invoked
if only the condition number of the matrix is desired. Since, however,
in particular for large matrices it requires
substantial computational effort, you may be considering to apply more efficiently
the class SingularValueDecomposition
and its methods.SingularValueDecomposition.cond()
public double det()
IllegalArgumentException
- if this matrix is not squarepublic Matrix pow(int n)
n
- the exponentIllegalArgumentException
- if this matrix is not square, or
if n is negative and not invertible.public double getDominantEigenvalue()
EPSILON
.
The method implements the
power iteration
and works if this matrix has a real-valued dominant eigenvalue.
According to the
Perron-Frobenius theorem
a sufficient condition for this is the matrix being irreducible, for instance
being the adjacency matrix of a
strongly connected graph
where any node is reachable from every other node.
If the power iteration does not converge, this method returns the maximum real part
of all eigenvalues.EPSILON
public double getDominantEigenvalue(double error)
error
- maximum error of approximationpublic double[] getDominantEigenvector()
EPSILON
of approximation.
It invokes the method getDominantEigenvalue()
, for more details see there.EPSILON
public double[] getDominantEigenvector(double error)
getDominantEigenvalue(double)
, for more details see there.error
- maximum error of approximationpublic double[] getRealEigenvalues()
eigenvalue decomposition
of this matrix.IllegalArgumentException
- if this matrix is not squareEigenvalueDecomposition.getRealEigenvalues()
public double[] getImagEigenvalues()
eigenvalue decomposition
of this matrix.IllegalArgumentException
- if this matrix is not squareEigenvalueDecomposition.getImagEigenvalues()
public Matrix getEigenvectors()
eigenvalue decomposition
of this matrix.IllegalArgumentException
- if this matrix is not squareEigenvalueDecomposition.getV()
public Matrix adjugate()
IllegalArgumentException
- if the matrix is not squarepublic Matrix inverse()
IllegalArgumentException
- if the matrix is not invertiblepublic double cofactor(int i, int j)
i
- index of cofactor Cijj
- index of cofactor CijIllegalArgumentException
- if the matrix is not squarepublic Matrix transpose()
public void transposeInPlace()
IllegalArgumentException
- if this matrix is not squarepublic double trace()
IllegalArgumentException
- if this matrix is not squarepublic Matrix add(Matrix b)
b
- the matrix to be added to this matrixpublic Matrix plus(Matrix b)
b
- the matrix to be added to this matrixpublic Matrix negative()
public Matrix minus(Matrix b)
b
- the matrix to be subtracted from this matrixpublic static double[] minus(double[] v, double[] w)
v
- a vectorw
- a vectorIllegalArgumentException
- if number of entries of v and w differpublic Matrix times(Matrix b)
b
- the matrix to multiply this matrix from rightIllegalArgumentException
- if number of columns of this matrix is
different from the number of rows of bpublic double[] times(double[] v)
v
- the column vector to multiply this matrix from rightIllegalArgumentException
- if number of columns of this matrix is
different from the number of rows of vpublic static double[] multiply(double a, double[] v)
a
- a constantv
- a vectorpublic static double multiply(double[] v, double[] w)
v
- a row vectorw
- a column vectorIllegalArgumentException
- if number of entries of v and w differpublic static Matrix tensor(Matrix[] a)
a
- array of several complex matrices a[0], a[1], ..., a[n-1]IllegalArgumentException
- if one of the matrices is emptypublic static double[][] tensor(double[][][] a)
a
- array of several complex matrices a[0], a[1], ..., a[n-1]IllegalArgumentException
- if one of the matrices is emptypublic Matrix solve(Matrix b)
b
- vectorIllegalArgumentException
- if b is not an appropriate column vectorpublic Matrix[] decomposeQR()
IllegalArgumentException
- if this matrix has more columns than rows.public String toHTML(String align, boolean showZeros)
align
- the alignment of each matrix entry, either "left", "center", or "right"showZeros
- flag whether zeros in the matrix are to be displayedpublic String toHTML(String align)
align
- the alignment of each matrix entry, either "left", "center", or "right"public String toHTML()
public String toString()
public static double getNorm(double[] v)
v
- a vectorpublic static Matrix createZero(int m, int n)
m
- the numbers of rowsn
- the number of columnspublic static Matrix createIdentity(int n)
n
- the number of rows and columns