All Packages  Class Hierarchy  This Package  Previous  Next  Index  WEKA's home

Class weka.classifiers.functions.pace.Matrix

java.lang.Object
    |
    +----weka.classifiers.functions.pace.Matrix

public class Matrix
extends java.lang.Object
implements java.lang.Cloneable, java.io.Serializable
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:

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();

Version:
5 August 1998
Author:
The MathWorks, Inc. and the National Institute of Standards and Technology.

Constructor Index

 o Matrix(double[][])
Construct a matrix from a 2-D array.
 o Matrix(double[][], int, int)
Construct a matrix quickly without checking arguments.
 o Matrix(double[], int)
Construct a matrix from a one-dimensional packed array
 o Matrix(int, int)
Construct an m-by-n matrix of zeros.
 o Matrix(int, int, double)
Construct an m-by-n constant matrix.

Method Index

 o arrayLeftDivide(Matrix)
Element-by-element left division, C = A.\B
 o arrayLeftDivideEquals(Matrix)
Element-by-element left division in place, A = A.\B
 o arrayRightDivide(Matrix)
Element-by-element right division, C = A./B
 o arrayRightDivideEquals(Matrix)
Element-by-element right division in place, A = A./B
 o arrayTimes(Matrix)
Element-by-element multiplication, C = A.*B
 o arrayTimesEquals(Matrix)
Element-by-element multiplication in place, A = A.*B
 o clone()
Clone the Matrix object.
 o constructWithCopy(double[][])
Construct a matrix from a copy of a 2-D array.
 o copy()
Make a deep copy of a matrix
 o get(int, int)
Get a single element.
 o getArray()
Access the internal two-dimensional array.
 o getArrayCopy()
Copy the internal two-dimensional array.
 o getColumnDimension()
Get column dimension.
 o getColumnPackedCopy()
Make a one-dimensional column packed copy of the internal array.
 o getMatrix(int[], int[])
Get a submatrix.
 o getMatrix(int[], int, int)
Get a submatrix.
 o getMatrix(int, int, int[])
Get a submatrix.
 o getMatrix(int, int, int, int)
Get a submatrix.
 o getRowDimension()
Get row dimension.
 o getRowPackedCopy()
Make a one-dimensional row packed copy of the internal array.
 o identity(int, int)
Generate identity matrix
 o minus(Matrix)
C = A - B
 o minusEquals(Matrix)
A = A - B
 o norm1()
One norm
 o normF()
Frobenius norm
 o normInf()
Infinity norm
 o plus(Matrix)
C = A + B
 o plusEquals(Matrix)
A = A + B
 o print(int, int)
Print the matrix to stdout.
 o print(NumberFormat, int)
Print the matrix to stdout.
 o print(PrintWriter, int, int)
Print the matrix to the output stream.
 o print(PrintWriter, NumberFormat, int)
Print the matrix to the output stream.
 o random(int, int)
Generate matrix with random elements
 o read(BufferedReader)
Read a matrix from a stream.
 o set(int, int, double)
Set a single element.
 o setMatrix(int[], int[], Matrix)
Set a submatrix.
 o setMatrix(int[], int, int, Matrix)
Set a submatrix.
 o setMatrix(int, int, int[], Matrix)
Set a submatrix.
 o setMatrix(int, int, int, int, Matrix)
Set a submatrix.
 o times(double)
Multiply a matrix by a scalar, C = s*A
 o times(Matrix)
Linear algebraic matrix multiplication, A * B
 o timesEquals(double)
Multiply a matrix by a scalar in place, A = s*A
 o trace()
Matrix trace.
 o transpose()
Matrix transpose.
 o uminus()
Unary minus

Constructor Detail

 o Matrix
public Matrix(int m,
              int n)
          Construct an m-by-n matrix of zeros.
Parameters:
m - Number of rows.
n - Number of colums.
 o Matrix
public Matrix(int m,
              int n,
              double s)
          Construct an m-by-n constant matrix.
Parameters:
m - Number of rows.
n - Number of colums.
s - Fill the matrix with this scalar value.
 o Matrix
public Matrix(double A[][])
          Construct a matrix from a 2-D array.
Parameters:
A - Two-dimensional array of doubles.
Throws:
java.lang.IllegalArgumentException - All rows must have the same length
See Also:
constructWithCopy
 o Matrix
public Matrix(double A[][],
              int m,
              int n)
          Construct a matrix quickly without checking arguments.
Parameters:
A - Two-dimensional array of doubles.
m - Number of rows.
n - Number of colums.
 o Matrix
public Matrix(double vals[],
              int m)
          Construct a matrix from a one-dimensional packed array
Parameters:
vals - One-dimensional array of doubles, packed by columns (ala Fortran).
m - Number of rows.
Throws:
java.lang.IllegalArgumentException - Array length must be a multiple of m.

Method Detail

 o constructWithCopy
public static Matrix constructWithCopy(double A[][])
          Construct a matrix from a copy of a 2-D array.
Parameters:
A - Two-dimensional array of doubles.
Throws:
java.lang.IllegalArgumentException - All rows must have the same length
 o copy
public Matrix copy()
          Make a deep copy of a matrix
 o clone
public java.lang.Object clone()
          Clone the Matrix object.
 o getArray
public double[][] getArray()
          Access the internal two-dimensional array.
Returns:
Pointer to the two-dimensional array of matrix elements.
 o getArrayCopy
public double[][] getArrayCopy()
          Copy the internal two-dimensional array.
Returns:
Two-dimensional array copy of matrix elements.
 o getColumnPackedCopy
public double[] getColumnPackedCopy()
          Make a one-dimensional column packed copy of the internal array.
Returns:
Matrix elements packed in a one-dimensional array by columns.
 o getRowPackedCopy
public double[] getRowPackedCopy()
          Make a one-dimensional row packed copy of the internal array.
Returns:
Matrix elements packed in a one-dimensional array by rows.
 o getRowDimension
public int getRowDimension()
          Get row dimension.
Returns:
m, the number of rows.
 o getColumnDimension
public int getColumnDimension()
          Get column dimension.
Returns:
n, the number of columns.
 o get
public double get(int i,
                  int j)
          Get a single element.
Parameters:
i - Row index.
j - Column index.
Returns:
A(i,j)
Throws:
java.lang.ArrayIndexOutOfBoundsException -
 o getMatrix
public Matrix getMatrix(int i0,
                        int i1,
                        int j0,
                        int j1)
          Get a submatrix.
Parameters:
i0 - Initial row index
i1 - Final row index
j0 - Initial column index
j1 - Final column index
Returns:
A(i0:i1,j0:j1)
Throws:
java.lang.ArrayIndexOutOfBoundsException - Submatrix indices
 o getMatrix
public Matrix getMatrix(int r[],
                        int c[])
          Get a submatrix.
Parameters:
r - Array of row indices.
c - Array of column indices.
Returns:
A(r(:),c(:))
Throws:
java.lang.ArrayIndexOutOfBoundsException - Submatrix indices
 o getMatrix
public Matrix getMatrix(int i0,
                        int i1,
                        int c[])
          Get a submatrix.
Parameters:
i0 - Initial row index
i1 - Final row index
c - Array of column indices.
Returns:
A(i0:i1,c(:))
Throws:
java.lang.ArrayIndexOutOfBoundsException - Submatrix indices
 o getMatrix
public Matrix getMatrix(int r[],
                        int j0,
                        int j1)
          Get a submatrix.
Parameters:
r - Array of row indices.
i0 - Initial column index
i1 - Final column index
Returns:
A(r(:),j0:j1)
Throws:
java.lang.ArrayIndexOutOfBoundsException - Submatrix indices
 o set
public void set(int i,
                int j,
                double s)
          Set a single element.
Parameters:
i - Row index.
j - Column index.
s - A(i,j).
Throws:
java.lang.ArrayIndexOutOfBoundsException -
 o setMatrix
public void setMatrix(int i0,
                      int i1,
                      int j0,
                      int j1,
                      Matrix X)
          Set a submatrix.
Parameters:
i0 - Initial row index
i1 - Final row index
j0 - Initial column index
j1 - Final column index
X - A(i0:i1,j0:j1)
Throws:
java.lang.ArrayIndexOutOfBoundsException - Submatrix indices
 o setMatrix
public void setMatrix(int r[],
                      int c[],
                      Matrix X)
          Set a submatrix.
Parameters:
r - Array of row indices.
c - Array of column indices.
X - A(r(:),c(:))
Throws:
java.lang.ArrayIndexOutOfBoundsException - Submatrix indices
 o setMatrix
public void setMatrix(int r[],
                      int j0,
                      int j1,
                      Matrix X)
          Set a submatrix.
Parameters:
r - Array of row indices.
j0 - Initial column index
j1 - Final column index
X - A(r(:),j0:j1)
Throws:
java.lang.ArrayIndexOutOfBoundsException - Submatrix indices
 o setMatrix
public void setMatrix(int i0,
                      int i1,
                      int c[],
                      Matrix X)
          Set a submatrix.
Parameters:
i0 - Initial row index
i1 - Final row index
c - Array of column indices.
X - A(i0:i1,c(:))
Throws:
java.lang.ArrayIndexOutOfBoundsException - Submatrix indices
 o transpose
public Matrix transpose()
          Matrix transpose.
Returns:
A'
 o norm1
public double norm1()
          One norm
Returns:
maximum column sum.
 o normInf
public double normInf()
          Infinity norm
Returns:
maximum row sum.
 o normF
public double normF()
          Frobenius norm
Returns:
sqrt of sum of squares of all elements.
 o uminus
public Matrix uminus()
          Unary minus
Returns:
-A
 o plus
public Matrix plus(Matrix B)
          C = A + B
Parameters:
B - another matrix
Returns:
A + B
 o plusEquals
public Matrix plusEquals(Matrix B)
          A = A + B
Parameters:
B - another matrix
Returns:
A + B
 o minus
public Matrix minus(Matrix B)
          C = A - B
Parameters:
B - another matrix
Returns:
A - B
 o minusEquals
public Matrix minusEquals(Matrix B)
          A = A - B
Parameters:
B - another matrix
Returns:
A - B
 o arrayTimes
public Matrix arrayTimes(Matrix B)
          Element-by-element multiplication, C = A.*B
Parameters:
B - another matrix
Returns:
A.*B
 o arrayTimesEquals
public Matrix arrayTimesEquals(Matrix B)
          Element-by-element multiplication in place, A = A.*B
Parameters:
B - another matrix
Returns:
A.*B
 o arrayRightDivide
public Matrix arrayRightDivide(Matrix B)
          Element-by-element right division, C = A./B
Parameters:
B - another matrix
Returns:
A./B
 o arrayRightDivideEquals
public Matrix arrayRightDivideEquals(Matrix B)
          Element-by-element right division in place, A = A./B
Parameters:
B - another matrix
Returns:
A./B
 o arrayLeftDivide
public Matrix arrayLeftDivide(Matrix B)
          Element-by-element left division, C = A.\B
Parameters:
B - another matrix
Returns:
A.\B
 o arrayLeftDivideEquals
public Matrix arrayLeftDivideEquals(Matrix B)
          Element-by-element left division in place, A = A.\B
Parameters:
B - another matrix
Returns:
A.\B
 o times
public Matrix times(double s)
          Multiply a matrix by a scalar, C = s*A
Parameters:
s - scalar
Returns:
s*A
 o timesEquals
public Matrix timesEquals(double s)
          Multiply a matrix by a scalar in place, A = s*A
Parameters:
s - scalar
Returns:
replace A by s*A
 o times
public Matrix times(Matrix B)
          Linear algebraic mat"> random
public static Matrix random(int m,
                            int n)
          Generate matrix with random elements
Parameters:
m - Number of rows.
n - Number of colums.
Returns:
An m-by-n matrix with uniformly distributed random elements.
 o identity
public static Matrix identity(int m,
                              int n)
          Generate identity matrix
Parameters:
m - Number of rows.
n - Number of colums.
Returns:
An m-by-n matrix with ones on the diagonal and zeros elsewhere.
 o print
public void print(int w,
                  int d)
          Print the matrix to stdout. Line the elements up in columns with a Fortran-like 'Fw.d' style format.
Parameters:
w - Column width.
d - Number of digits after the decimal.
 o print
public void print(java.io.PrintWriter output,
                  int w,
                  int d)
          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.
w - Column width.
d - Number of digits after the decimal.
 o print
public void print(java.text.NumberFormat format,
                  int width)
          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.
width - Field width for each column.
See Also:
setDecimalFormatSymbols
 o print
public void print(java.io.PrintWriter output,
                  java.text.NumberFormat format,
                  int width)
          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.
format - A formatting object to format the matrix elements
width - Column width.
See Also:
setDecimalFormatSymbols
 o read
public static Matrix read(java.io.BufferedReader input) throws java.io.IOException
          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.
Parameters:
input - the input stream.

All Packages  Class Hierarchy  This Package  Previous  Next  Index  WEKA's home