| java.lang.Object weka.core.matrix.Matrix
All known Subclasses: weka.classifiers.functions.pace.PaceMatrix,
Matrix | public class Matrix implements Cloneable,Serializable(Code) | | 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:
- Solve a linear system A x = b and compute the residual norm, ||b - A x||.
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();
Adapted from the JAMA package. Additional methods are tagged with the
@author tag.
author: The Mathworks and NIST author: Fracpete (fracpete at waikato dot ac dot nz) version: $Revision: 1.7 $ |
Field Summary | |
protected double[][] | A Array for internal storage of elements. | protected int | mn Row and column dimensions. |
Constructor Summary | |
public | Matrix(int m, int n) Construct an m-by-n matrix of zeros. | public | Matrix(int m, int n, double s) Construct an m-by-n constant matrix. | public | Matrix(double[][] A) Construct a matrix from a 2-D array. | public | Matrix(double[][] A, int m, int n) Construct a matrix quickly without checking arguments. | public | Matrix(double vals, int m) | public | Matrix(Reader r) Reads a matrix from a reader. |
Method Summary | |
public Matrix | arrayLeftDivide(Matrix B) | public Matrix | arrayLeftDivideEquals(Matrix B) | public Matrix | arrayRightDivide(Matrix B) | public Matrix | arrayRightDivideEquals(Matrix B) | public Matrix | arrayTimes(Matrix B) | public Matrix | arrayTimesEquals(Matrix B) | public CholeskyDecomposition | chol() | public Object | clone() Clone the Matrix object. | public double | cond() | public static Matrix | constructWithCopy(double[][] A) Construct a matrix from a copy of a 2-D array. | public Matrix | copy() | public double | det() | public EigenvalueDecomposition | eig() | public double | get(int i, int j) Get a single element.
Parameters: i - Row index. Parameters: j - Column index. | public double[][] | getArray() Access the internal two-dimensional array. | public double[][] | getArrayCopy() Copy the internal two-dimensional array. | public int | getColumnDimension() Get column dimension. | public double[] | getColumnPackedCopy() Make a one-dimensional column packed copy of the internal array. | public Matrix | getMatrix(int i0, int i1, int j0, int j1) Get a submatrix. | public Matrix | getMatrix(int[] r, int[] c) Get a submatrix.
Parameters: r - Array of row indices. Parameters: c - Array of column indices. | public Matrix | getMatrix(int i0, int i1, int[] c) Get a submatrix.
Parameters: i0 - Initial row index Parameters: i1 - Final row index Parameters: c - Array of column indices. | public Matrix | getMatrix(int[] r, int j0, int j1) Get a submatrix. | public int | getRowDimension() Get row dimension. | public double[] | getRowPackedCopy() Make a one-dimensional row packed copy of the internal array. | public static Matrix | identity(int m, int n) Generate identity matrix
Parameters: m - Number of rows. Parameters: n - Number of colums. | public Matrix | inverse() | public boolean | isSquare() returns whether the matrix is a square matrix or not. | public boolean | isSymmetric() Returns true if the matrix is symmetric. | public LUDecomposition | lu() | public static void | main(String[] args) Main method for testing this class. | public Matrix | minus(Matrix B) | public Matrix | minusEquals(Matrix B) | public double | norm1() | public double | norm2() | public double | normF() | public double | normInf() | public static Matrix | parseMatlab(String matlab) creates a matrix from the given Matlab string. | public Matrix | plus(Matrix B) | public Matrix | plusEquals(Matrix B) | public void | print(int w, int d) Print the matrix to stdout. | public void | print(PrintWriter output, int w, int d) Print the matrix to the output stream. | public void | print(NumberFormat format, int width) Print the matrix to stdout. | public void | print(PrintWriter output, NumberFormat format, int width) Print the matrix to the output stream. | public QRDecomposition | qr() | public static Matrix | random(int m, int n) Generate matrix with random elements
Parameters: m - Number of rows. Parameters: n - Number of colums. | public int | rank() | public static Matrix | read(BufferedReader input) Read a matrix from a stream. | public LinearRegression | regression(Matrix y, double ridge) Performs a (ridged) linear regression. | final public LinearRegression | regression(Matrix y, double[] w, double ridge) Performs a weighted (ridged) linear regression. | public void | set(int i, int j, double s) Set a single element. | public void | setMatrix(int i0, int i1, int j0, int j1, Matrix X) Set a submatrix. | public void | setMatrix(int[] r, int[] c, Matrix X) Set a submatrix. | public void | setMatrix(int[] r, int j0, int j1, Matrix X) Set a submatrix. | public void | setMatrix(int i0, int i1, int[] c, Matrix X) Set a submatrix. | public Matrix | solve(Matrix B) | public Matrix | solveTranspose(Matrix B) | public 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. | public SingularValueDecomposition | svd() | public Matrix | times(double s) | public Matrix | times(Matrix B) | public Matrix | timesEquals(double s) | public 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]. | public String | toString() Converts a matrix to a string. | public double | trace() Matrix trace. | public Matrix | transpose() Matrix transpose. | public Matrix | uminus() | public void | write(Writer w) Writes out a matrix. |
A | protected double[][] A(Code) | | Array for internal storage of elements.
|
mn | protected int mn(Code) | | Row and column dimensions.
|
Matrix | public Matrix(int m, int n)(Code) | | Construct an m-by-n matrix of zeros.
Parameters: m - Number of rows. Parameters: n - Number of colums. |
Matrix | public Matrix(int m, int n, double s)(Code) | | Construct an m-by-n constant matrix.
Parameters: m - Number of rows. Parameters: n - Number of colums. Parameters: s - Fill the matrix with this scalar value. |
Matrix | public Matrix(double[][] A, int m, int n)(Code) | | Construct a matrix quickly without checking arguments.
Parameters: A - Two-dimensional array of doubles. Parameters: m - Number of rows. Parameters: n - Number of colums. |
Matrix | public Matrix(double vals, int m)(Code) | | Construct a matrix from a one-dimensional packed array
Parameters: vals - One-dimensional array of doubles, packed by columns (alaFortran). Parameters: m - Number of rows. throws: IllegalArgumentException - Array length must be a multiple of m. |
Matrix | public Matrix(Reader r) throws Exception(Code) | | Reads a matrix from a reader. The first line in the file should
contain the number of rows and columns. Subsequent lines
contain elements of the matrix.
(FracPete: taken from old weka.core.Matrix class)
Parameters: r - the reader containing the matrix throws: Exception - if an error occurs See Also: Matrix.write(Writer) |
arrayLeftDivide | public Matrix arrayLeftDivide(Matrix B)(Code) | | Element-by-element left division, C = A.\B
Parameters: B - another matrix A.\B |
arrayLeftDivideEquals | public Matrix arrayLeftDivideEquals(Matrix B)(Code) | | Element-by-element left division in place, A = A.\B
Parameters: B - another matrix A.\B |
arrayRightDivide | public Matrix arrayRightDivide(Matrix B)(Code) | | Element-by-element right division, C = A./B
Parameters: B - another matrix A./B |
arrayRightDivideEquals | public Matrix arrayRightDivideEquals(Matrix B)(Code) | | Element-by-element right division in place, A = A./B
Parameters: B - another matrix A./B |
arrayTimes | public Matrix arrayTimes(Matrix B)(Code) | | Element-by-element multiplication, C = A.*B
Parameters: B - another matrix A.*B |
arrayTimesEquals | public Matrix arrayTimesEquals(Matrix B)(Code) | | Element-by-element multiplication in place, A = A.*B
Parameters: B - another matrix A.*B |
cond | public double cond()(Code) | | Matrix condition (2 norm)
ratio of largest to smallest singular value. |
constructWithCopy | public static Matrix constructWithCopy(double[][] A)(Code) | | Construct a matrix from a copy of a 2-D array.
Parameters: A - Two-dimensional array of doubles. throws: IllegalArgumentException - All rows must have the same length |
det | public double det()(Code) | | Matrix determinant
determinant |
getArray | public double[][] getArray()(Code) | | Access the internal two-dimensional array.
Pointer to the two-dimensional array of matrix elements. |
getArrayCopy | public double[][] getArrayCopy()(Code) | | Copy the internal two-dimensional array.
Two-dimensional array copy of matrix elements. |
getColumnDimension | public int getColumnDimension()(Code) | | Get column dimension.
n, the number of columns. |
getColumnPackedCopy | public double[] getColumnPackedCopy()(Code) | | 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)(Code) | | Get a submatrix.
Parameters: i0 - Initial row index Parameters: i1 - Final row index Parameters: j0 - Initial column index Parameters: j1 - Final column index A(i0:i1,j0:j1) throws: ArrayIndexOutOfBoundsException - Submatrix indices |
getMatrix | public Matrix getMatrix(int[] r, int[] c)(Code) | | Get a submatrix.
Parameters: r - Array of row indices. Parameters: c - Array of column indices. A(r(:),c(:)) throws: ArrayIndexOutOfBoundsException - Submatrix indices |
getMatrix | public Matrix getMatrix(int i0, int i1, int[] c)(Code) | | Get a submatrix.
Parameters: i0 - Initial row index Parameters: i1 - Final row index Parameters: c - Array of column indices. A(i0:i1,c(:)) throws: ArrayIndexOutOfBoundsException - Submatrix indices |
getMatrix | public Matrix getMatrix(int[] r, int j0, int j1)(Code) | | Get a submatrix.
Parameters: r - Array of row indices. Parameters: j0 - Initial column index Parameters: j1 - Final column index A(r(:),j0:j1) throws: ArrayIndexOutOfBoundsException - Submatrix indices |
getRowDimension | public int getRowDimension()(Code) | | Get row dimension.
m, the number of rows. |
getRowPackedCopy | public double[] getRowPackedCopy()(Code) | | 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)(Code) | | Generate identity matrix
Parameters: m - Number of rows. Parameters: n - Number of colums. An m-by-n matrix with ones on the diagonal and zeros elsewhere. |
inverse | public Matrix inverse()(Code) | | Matrix inverse or pseudoinverse
inverse(A) if A is square, pseudoinverse otherwise. |
isSquare | public boolean isSquare()(Code) | | returns whether the matrix is a square matrix or not.
true if the matrix is a square matrix |
isSymmetric | public boolean isSymmetric()(Code) | | Returns true if the matrix is symmetric.
(FracPete: taken from old weka.core.Matrix class)
boolean true if matrix is symmetric. |
main | public static void main(String[] args)(Code) | | Main method for testing this class.
|
minusEquals | public Matrix minusEquals(Matrix B)(Code) | | A = A - B
Parameters: B - another matrix A - B |
norm1 | public double norm1()(Code) | | One norm
maximum column sum. |
norm2 | public double norm2()(Code) | | Two norm
maximum singular value. |
normF | public double normF()(Code) | | Frobenius norm
sqrt of sum of squares of all elements. |
normInf | public double normInf()(Code) | | Infinity norm
maximum row sum. |
parseMatlab | public static Matrix parseMatlab(String matlab) throws Exception(Code) | | creates a matrix from the given Matlab string.
Parameters: matlab - the matrix in matlab format the matrix represented by the given string See Also: Matrix.toMatlab() |
plusEquals | public Matrix plusEquals(Matrix B)(Code) | | A = A + B
Parameters: B - another matrix A + B |
print | public void print(int w, int d)(Code) | | Print the matrix to stdout. Line the elements up in columns
with a Fortran-like 'Fw.d' style format.
Parameters: w - Column width. Parameters: d - Number of digits after the decimal. |
print | public void print(PrintWriter output, int w, int d)(Code) | | Print the matrix to the output stream. Line the elements up in
columns with a Fortran-like 'Fw.d' style format.
Parameters: output - Output stream. Parameters: w - Column width. Parameters: d - Number of digits after the decimal. |
print | public void print(NumberFormat format, int width)(Code) | | 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.
Parameters: format - A Formatting object for individual elements. Parameters: width - Field width for each column. See Also: java.text.DecimalFormat.setDecimalFormatSymbols |
print | public void print(PrintWriter output, NumberFormat format, int width)(Code) | | 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.
Parameters: output - the output stream. Parameters: format - A formatting object to format the matrix elements Parameters: width - Column width. See Also: java.text.DecimalFormat.setDecimalFormatSymbols |
random | public static Matrix random(int m, int n)(Code) | | Generate matrix with random elements
Parameters: m - Number of rows. Parameters: n - Number of colums. An m-by-n matrix with uniformly distributed random elements. |
rank | public int rank()(Code) | | Matrix rank
effective numerical rank, obtained from SVD. |
read | public static Matrix read(BufferedReader input) throws java.io.IOException(Code) | | 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.
Note: This format differs from the one that can be read via the
Matrix(Reader) constructor! For that format, the write(Writer) method
is used (from the original weka.core.Matrix class).
Parameters: input - the input stream. See Also: Matrix.Matrix(Reader) See Also: Matrix.write(Writer) |
regression | public LinearRegression regression(Matrix y, double ridge)(Code) | | Performs a (ridged) linear regression.
(FracPete: taken from old weka.core.Matrix class)
Parameters: y - the dependent variable vector Parameters: ridge - the ridge parameter the coefficients throws: IllegalArgumentException - if not successful |
regression | final public LinearRegression regression(Matrix y, double[] w, double ridge)(Code) | | Performs a weighted (ridged) linear regression.
(FracPete: taken from old weka.core.Matrix class)
Parameters: y - the dependent variable vector Parameters: w - the array of data point weights Parameters: ridge - the ridge parameter the coefficients throws: IllegalArgumentException - if the wrong number of weights wereprovided. |
set | public void set(int i, int j, double s)(Code) | | Set a single element.
Parameters: i - Row index. Parameters: j - Column index. Parameters: s - A(i,j). throws: ArrayIndexOutOfBoundsException - |
setMatrix | public void setMatrix(int i0, int i1, int j0, int j1, Matrix X)(Code) | | Set a submatrix.
Parameters: i0 - Initial row index Parameters: i1 - Final row index Parameters: j0 - Initial column index Parameters: j1 - Final column index Parameters: X - A(i0:i1,j0:j1) throws: ArrayIndexOutOfBoundsException - Submatrix indices |
setMatrix | public void setMatrix(int[] r, int[] c, Matrix X)(Code) | | Set a submatrix.
Parameters: r - Array of row indices. Parameters: c - Array of column indices. Parameters: X - A(r(:),c(:)) throws: ArrayIndexOutOfBoundsException - Submatrix indices |
setMatrix | public void setMatrix(int[] r, int j0, int j1, Matrix X)(Code) | | Set a submatrix.
Parameters: r - Array of row indices. Parameters: j0 - Initial column index Parameters: j1 - Final column index Parameters: X - A(r(:),j0:j1) throws: ArrayIndexOutOfBoundsException - Submatrix indices |
setMatrix | public void setMatrix(int i0, int i1, int[] c, Matrix X)(Code) | | Set a submatrix.
Parameters: i0 - Initial row index Parameters: i1 - Final row index Parameters: c - Array of column indices. Parameters: X - A(i0:i1,c(:)) throws: ArrayIndexOutOfBoundsException - Submatrix indices |
solve | public Matrix solve(Matrix B)(Code) | | Solve A*X = B
Parameters: B - right hand side solution if A is square, least squares solution otherwise |
solveTranspose | public Matrix solveTranspose(Matrix B)(Code) | | Solve X*A = B, which is also A'*X' = B'
Parameters: B - right hand side solution if A is square, least squares solution otherwise. |
sqrt | public Matrix sqrt()(Code) | | 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. This can be problematic with large
matrices.
Examples:
-
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}});
sqrt(A) |
times | public Matrix times(double s)(Code) | | Multiply a matrix by a scalar, C = s*A
Parameters: s - scalar s*A |
timesEquals | public Matrix timesEquals(double s)(Code) | | Multiply a matrix by a scalar in place, A = s*A
Parameters: s - scalar replace A by s*A |
toMatlab | public String toMatlab()(Code) | | 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].
the matrix in Matlab single line format |
toString | public String toString()(Code) | | Converts a matrix to a string.
(FracPete: taken from old weka.core.Matrix class)
the converted string |
trace | public double trace()(Code) | | Matrix trace.
sum of the diagonal elements. |
transpose | public Matrix transpose()(Code) | | Matrix transpose.
A' |
write | public void write(Writer w) throws Exception(Code) | | Writes out a matrix. The format can be read via the Matrix(Reader)
constructor.
(FracPete: taken from old weka.core.Matrix class)
Parameters: w - the output Writer throws: Exception - if an error occurs See Also: Matrix.Matrix(Reader) |
|
|