sugr.linalg
Class MatrixImplJama

java.lang.Object
  extended bysugr.linalg.MatrixImplJama
All Implemented Interfaces:
java.lang.Cloneable, Matrix, java.io.Serializable

public class MatrixImplJama
extends java.lang.Object
implements Matrix

Matrix class. Contains operations of linear algebra: Addition/Substraction, Multiplication, get/set-methods (all double precision floating points), printing, Inversion, submatrices-access, transpose, vectorize.

Construction of matrices is also possible. You are allowed to use LinearAlgebraFactory-methods for constructing matrices and vector. For more information about this class, you are invited to read more here in sugr.linalg.LinearAlgebraFactory

Just a note: In further documentation, matrix A is synonym for this matrix, e.g.:

 		Matrix firstMatrix; 
 		Matrix secondMatrix;
 		Matrix C = firstMatrix.add(secondMatrix);
 
means for add(Matrix B): firstMatrix is this matrix = A, secondMatrix is B

Version:
1.1 (8.2.03): - getMatrix (int, int, int, int) rewritten
1.1.1 (16.2.2003) - Added: comments to svd() - Added: comments to print(int, int)
1.1.2 (2.4.2003) - Added: kronecker-product
1.1.3 (3.4.2003) - Added: slice(i0,i1,j0,j1, Matrix B)
1.1.4 (13.4.2003) - Added: all missing comments

Author:
Daniel Goldbach, Norbert Fischer
See Also:
Serialized Form

Field Summary
protected  Jama.Matrix impl
          Jama.Matrix is embedded into this class - all operations are mapped to that.
private static long serialVersionUID
          Class version number for serialization
 
Constructor Summary
MatrixImplJama()
          Standard-constructor.
MatrixImplJama(double[][] values)
          create a new matrix by a given double precision floating point array.
MatrixImplJama(int no_rows, int no_cols)
          Creates a new matrix containing only zeros
MatrixImplJama(Matrix M)
          create B from A.
 
Method Summary
 java.lang.Object clone()
          Clone a matrix.
 Matrix copy()
          Create a new matrix B, independent from A
 Matrix delColumn(int col)
          delete a column from A.
 Matrix delRow(int row)
          delete a row from A.
 double det()
          Computes the determinant of the matrix
 double[] eig()
          EigenvalueDecomposition of this Matrix
 boolean equals(java.lang.Object obj)
          Test, whether A and B are identical or not
 double get(int row, int col)
           Get a single element
 Vector getColumn(int j)
           
 int getColumnDimension()
           Like the name says :-)
 Matrix getMatrix(int i0, int i1, int j0, int j1)
           Returns a Submatrix with dimension (i1-i0) x (j1-j0) Upper left element begin with indices (0,0)
 Vector getRow(int i)
           
 int getRowDimension()
           Like the name says :-)
 Matrix invert()
           Matrix inverse or pseudoinverse
 boolean isSemiDefinite()
          Test, whether A ist semidefinit or not
 Matrix kronecker(Matrix B)
          Calculates the kronecker-product of two matrices.
 Matrix minus(Matrix B)
           C=A-B
 Matrix mult(double f)
           B=A*f , where f is a scalar
 Matrix mult(Matrix B)
           C=A*B
 Vector mult(Vector v)
           B=A*v, where v is a vector.
 Matrix plus(Matrix B)
           C=A+B
 void print()
           Prints this matrix to stdout
 void print(int w, int d)
           Prints this matrix to stdout
It may be useful to select w > d.
 void printMatlab()
           Prints this matrix in Matlab format to stdout
 Matrix pseudoInverse(int rank)
          returns the pseudo-inverse A^+ .
 Matrix pseudoInverse(Matrix H)
           returns the pseudo-inverse A^+ with given Nullspace
 Matrix[] qr()
           Computes the QR Decomposition of given Matrix
 void set(double value)
           Sets all elements of the matrix to the given value.
 void set(int row, int col, double value)
           Set a single element
 void setColumn(int j, Vector v)
           
 void setRow(int i, Vector v)
           
 Matrix slice(int[] listOfRows, int[] listOfCols)
           Get submatrix with given list of rows and given list of columns.
 void slice(int[] listOfRows, int[] listOfCols, Matrix B)
           As other slice, only: read values from Matrix B and put them in the object at the posisitions of the intersections of given rows and columns.
 Matrix slice(int i0, int i1, int j0, int j1)
          Gets a submatrix of A with B
 void slice(int i0, int i1, int j0, int j1, Matrix B)
          Sets a submatrix
 Vector solve(Vector y)
           
 Matrix[] svd()
           Computes the SingularValueDecomposition of given Matrix with dimension m x n (m>=n), so the result is given by: Matrix = U*S*V^t
 java.lang.String toString()
          Delivers a string with the elements of the matrix.
 Matrix trans()
           Matrix transpose
 Vector vec()
          returns the matrix as a vector: First the first column, then the second column and so on.
 
Methods inherited from class java.lang.Object
finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

serialVersionUID

private static final long serialVersionUID
Class version number for serialization

See Also:
Constant Field Values

impl

protected Jama.Matrix impl
Jama.Matrix is embedded into this class - all operations are mapped to that.

Constructor Detail

MatrixImplJama

public MatrixImplJama(int no_rows,
                      int no_cols)
Creates a new matrix containing only zeros

Parameters:
no_rows - number of rows the matrix should have
no_cols - number of rows the matrix should have

MatrixImplJama

public MatrixImplJama(double[][] values)
create a new matrix by a given double precision floating point array. e.g.:
    double[][] array = {  {0,1,2,3}, 
                          {0,1,2,3}, 
                          {0,1,2,3}};
    Matrix A = new Matrix(array);
 
Matrix A has dimension 3x4.

Parameters:
values - given in double

MatrixImplJama

public MatrixImplJama(Matrix M)
create B from A. Functionally it works like copy


MatrixImplJama

public MatrixImplJama()
Standard-constructor.

Method Detail

plus

public Matrix plus(Matrix B)

C=A+B

Specified by:
plus in interface Matrix
Parameters:
B - Must have same dimensions like this matrix

Returns:
A+B


minus

public Matrix minus(Matrix B)

C=A-B

Specified by:
minus in interface Matrix
Parameters:
B - Must have same dimensions like this matrix

Returns:
A-B


mult

public Matrix mult(Matrix B)

C=A*B

Specified by:
mult in interface Matrix
Parameters:
B - rowDim of B must be the same like colDim from A

Returns:
A*B


trans

public Matrix trans()

Matrix transpose

Specified by:
trans in interface Matrix
Returns:
A'


get

public double get(int row,
                  int col)

Get a single element

Specified by:
get in interface Matrix
Parameters:
row - row-index

col - column-index

Returns:
the element as double


set

public void set(int row,
                int col,
                double value)

Set a single element

Specified by:
set in interface Matrix
Parameters:
row - row-index

col - column-index

value - A(i,j):=value


getRowDimension

public int getRowDimension()

Like the name says :-)

Specified by:
getRowDimension in interface Matrix
Returns:
a int containing the rowDim


getColumnDimension

public int getColumnDimension()

Like the name says :-)

Specified by:
getColumnDimension in interface Matrix
Returns:
a int containing the columnDim


invert

public Matrix invert()

Matrix inverse or pseudoinverse

Specified by:
invert in interface Matrix
Returns:
inverse(A) if A is square, pseudoinverse otherwise.


mult

public Matrix mult(double f)

B=A*f , where f is a scalar

Specified by:
mult in interface Matrix
Parameters:
f - scalar, given as double

Returns:
A*f


mult

public Vector mult(Vector v)

B=A*v, where v is a vector. Dimension of v and column dimension of A must agree

Specified by:
mult in interface Matrix
Parameters:
v - Vector, with dimension the same as column-dimension of A

Returns:
A*v


svd

public Matrix[] svd()

Computes the SingularValueDecomposition of given Matrix with dimension m x n (m>=n), so the result is given by: Matrix = U*S*V^t

Specified by:
svd in interface Matrix
Returns:
a Matrix[] containing 3 elements:
  • Matrix[0] = U (orthogonal, m x n)
  • Matrix[1] = S (the diagonalmatrix, n x n, containing the singularvalues)
  • Matrix[2] = V (orthogonal, n x n)
    (note, that the result is NOT transposed)
e.g.:
 orig_matrix = Matrix[0].mult(Matrix[1]).mult(Matrix[2].trans()); 
gives the original matrix


slice

public Matrix slice(int[] listOfRows,
                    int[] listOfCols)

Get submatrix with given list of rows and given list of columns. If listofRows is emtpy, get complete column, if listOfCols is empty, get complete rows.

Specified by:
slice in interface Matrix
Parameters:
listOfRows - ...

listOfCols - ...

Returns:
a Matrix with ...


slice

public void slice(int[] listOfRows,
                  int[] listOfCols,
                  Matrix B)

As other slice, only: read values from Matrix B and put them in the object at the posisitions of the intersections of given rows and columns.

Specified by:
slice in interface Matrix
Parameters:
listOfRows - ...

listOfCols - ...

B - ...


slice

public void slice(int i0,
                  int i1,
                  int j0,
                  int j1,
                  Matrix B)
Sets a submatrix

Specified by:
slice in interface Matrix
Parameters:
i0 - initial row index (upper left is (0,0) )
i1 - final row index
j0 - initial column index
j1 - final column index
B - Matrix with dimension (i1-i0+1 x j1-j0+1)

vec

public Vector vec()
returns the matrix as a vector: First the first column, then the second column and so on.
 e.g. : if A = | a b |  then A.vec() returns | a |
               | c d |                       | c |
                                             | b |
                                             | d |
 

Specified by:
vec in interface Matrix
Returns:
a Vector with dimension m*n

copy

public Matrix copy()
Create a new matrix B, independent from A

Specified by:
copy in interface Matrix
Returns:
B with B=A

set

public void set(double value)

Sets all elements of the matrix to the given value.

Specified by:
set in interface Matrix
Parameters:
value -


print

public void print(int w,
                  int d)

Prints this matrix to stdout
It may be useful to select w > d. A common value is w=5, d=3

Specified by:
print in interface Matrix
Parameters:
w - describes the width of a single element
d - number of decimals after comma

print

public void print()

Prints this matrix to stdout

Specified by:
print in interface Matrix

printMatlab

public void printMatlab()

Prints this matrix in Matlab format to stdout

Specified by:
printMatlab in interface Matrix

toString

public java.lang.String toString()
Delivers a string with the elements of the matrix.

Specified by:
toString in interface Matrix
Returns:
a string for printing

equals

public boolean equals(java.lang.Object obj)
Test, whether A and B are identical or not

Specified by:
equals in interface Matrix
Parameters:
obj - Matrix to test
Returns:
boolean value true, if A=B or false otherwise

getMatrix

public Matrix getMatrix(int i0,
                        int i1,
                        int j0,
                        int j1)

Returns a Submatrix with dimension (i1-i0) x (j1-j0) Upper left element begin with indices (0,0)

Specified by:
getMatrix in interface Matrix
Parameters:
i0 - Initial row index
i1 - Final row index
j0 - Initial column index
j1 - Final column index


eig

public double[] eig()
EigenvalueDecomposition of this Matrix

Specified by:
eig in interface Matrix
Returns:
a double-Array containing the eigenvalues. They are sorted in so that [0] is the biggest and [n] is the smallest value

isSemiDefinite

public boolean isSemiDefinite()
Test, whether A ist semidefinit or not

Specified by:
isSemiDefinite in interface Matrix
Returns:
boolean value: true, if all eigenvalues >= 0

pseudoInverse

public Matrix pseudoInverse(int rank)
returns the pseudo-inverse A^+ . This is done via SVD with a given rank

Specified by:
pseudoInverse in interface Matrix
Parameters:
rank - the rank which A with the given elements should have
Returns:
A^+

kronecker

public Matrix kronecker(Matrix B)
Calculates the kronecker-product of two matrices. Let A=(a_ij) be a (n x m)-Matrix and B=(a_ij) a (p x q)-Matrix. Then the result-matrix C is a (np x mq)-Matrix with
                  |a_11*B ...  a_1m*B |
              C = |...            ... |
                  |a_n1*B ...  a_nm*B |
 

Specified by:
kronecker in interface Matrix
Parameters:
B - an arbitrary matrix
Returns:
a Matrix with dimension (np x mq)

det

public double det()
Computes the determinant of the matrix

Specified by:
det in interface Matrix
Returns:
determinant of the matrix

getColumn

public Vector getColumn(int j)
Specified by:
getColumn in interface Matrix

getRow

public Vector getRow(int i)
Specified by:
getRow in interface Matrix

setRow

public void setRow(int i,
                   Vector v)
Specified by:
setRow in interface Matrix

setColumn

public void setColumn(int j,
                      Vector v)
Specified by:
setColumn in interface Matrix

solve

public Vector solve(Vector y)
Specified by:
solve in interface Matrix

delColumn

public Matrix delColumn(int col)
delete a column from A.
 e.g. : if A looks like
        | a11 a12 a13 |
        | a21 a22 a23 |    3x3 Matrix
        | a31 a32 a33 |
 
 then A.delColumn(0) returns
            | a12 a13 |
            | a22 a23 |    3x2 Matrix
            | a32 a33 |
 

Specified by:
delColumn in interface Matrix
Parameters:
col - column to delete. Must be between 0 and m-1, if A is nxm
Returns:
nx(m-1) Matrix

delRow

public Matrix delRow(int row)
delete a row from A.
 e.g. : if A looks like
        | a11 a12 a13 |
        | a21 a22 a23 |    3x3 Matrix
        | a31 a32 a33 |
 
 then A.delRow(0) returns
 
        | a21 a22 a23 |    2x3 Matrix
        | a31 a32 a33 |
 

Specified by:
delRow in interface Matrix
Parameters:
row - row to delete. Must be between 0 and n-1, if A is nxm
Returns:
(n-1)xm Matrix

slice

public Matrix slice(int i0,
                    int i1,
                    int j0,
                    int j1)
Gets a submatrix of A with B

Specified by:
slice in interface Matrix
Parameters:
i0 - initial row index (upper left is (0,0) )
i1 - final row index
j0 - initial column index
j1 - final column index

qr

public Matrix[] qr()

Computes the QR Decomposition of given Matrix

Specified by:
qr in interface Matrix
Returns:
a Matrix[] containing 3 elements:
  • Matrix[0] = Q
  • Matrix[1] = R


clone

public java.lang.Object clone()
Clone a matrix.

Specified by:
clone in interface Matrix
Returns:
a copy of the matrix.

pseudoInverse

public Matrix pseudoInverse(Matrix H)
Description copied from interface: Matrix

returns the pseudo-inverse A^+ with given Nullspace

Specified by:
pseudoInverse in interface Matrix
Parameters:
H - nullspace

Returns:
A^+