javax.robotics.vecmath
Class Matrix

java.lang.Object
  extended by javax.vecmath.GMatrix
      extended by javax.robotics.vecmath.Matrix
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable

public class Matrix
extends javax.vecmath.GMatrix

A double precision, general, dynamically-resizable, two-dimensional matrix class. Row and column numbering begins with zero. The representation is row major. This class extends printing functionality of GMatrix class

Since:
1.0.1
Version:
5/10/2005
Author:
Carmine Lia
See Also:
Serialized Form

Field Summary
static short DEFINITE_NEGATIVE
          Type of matrix.
static short DEFINITE_POSITIVE
          Type of matrix.
static short SEMIDEFINITE_NEGATIVE
          Type of matrix.
static short SEMIDEFINITE_POSITIVE
          Type of matrix.
 
Constructor Summary
Matrix(javax.vecmath.GMatrix matrix)
          Constructs a new matrix with the same values as the GMatrix parameter.
Matrix(int n)
          Constructs an n by n identity matrix.
Matrix(int n, double[] matrix)
          Constructs an n by n square matrix initialized to the values in the matrix array.
Matrix(int nRow, int nCol)
          Constructs an nRow by NCol identity matrix.
Matrix(int nRow, int nCol, double[] matrix)
          Constructs an nRow by nCol matrix initialized to the values in the matrix array.
Matrix(Matrix matrix)
          Constructs a new matrix with the same values as the Matrix parameter.
 
Method Summary
 Matrix arrayLeftDivide(Matrix B)
          Element-by-element left division, C = this.
 Matrix arrayLeftDivideEquals(Matrix B)
          Element-by-element left division in place, this = this.
 Matrix arrayRightDivide(Matrix B)
          Element-by-element right division, C = this.
 Matrix arrayRightDivideEquals(Matrix B)
          Element-by-element right division in place, this = this.
static short checkEigenValue(RVector d)
          Check the eigenvalues.
 RVector eig()
          Eigenvalues of a nxn real matrix.
 void eig(Matrix D, Matrix V)
          Eigenvalues and eigenvectors of a nxn real matrix.
 void eig(Matrix D, Matrix V, RVector d, RVector e)
          Eigenvalues and eigenvectors of a nxn real matrix.
 void eig(RVector d)
          Eigenvalues of a nxn real matrix.
 void get(double[] matrix)
          Gets this matrix as mono dimensional array
 void get(double[][] matrix)
          Gets this matrix as bi-dimensional array
 RVector getDiagonal()
          Gets main diagonal of this matrix.
 void getSubMatrix(int startRow, int endRow, int startCol, int endCol, Matrix M)
          Gets sub matrix from this Matrix.
 Matrix inverse()
          Computes inverse or pseudoinverse of this matrix
 Matrix inverse(double eps, double lambdaMax)
          Computes Damped Least Square inverse of this matrix using SVD decomposition.
 boolean isSymmetric()
          Check if this matrix is symmetric
 Matrix lyap(Matrix C)
          Computes the solution of Lyapunov equation using eigenvalue decomposition.
 void mul(DMatrix D)
          Sets the value of this matrix to the result of multiplying itself with diagonal matrix D (this = this * D).
 void printf()
          Prints to standard out
 void printf(java.io.PrintStream out)
          Prints to PrintStream
 void printf(java.io.PrintStream out, java.lang.String format)
          Prints to PrintStream according to the format string
 void printf(java.lang.String format)
          Prints to standard out according to the format string.
 Matrix pseudoInverse()
          Computes pseudo inverse of this matrix using SVD decomposition.
 void set(double[][] matrix)
          Sets this matrix from bi-dimensional array
 void setDiagonal(RVector diag)
          Sets the diagonal of this matrix.
 void setOnes()
          Sets all elements of this matrix to one
 void splitColumns(double[]... v)
          Splits columns of this matrix in multiple arrays.
 void splitColumns(RVector... v)
          Splits columns of this matrix in multiple vectors.
 void splitRows(double[]... v)
          Splits rows of this matrix in multiple arrays.
 void splitRows(RVector... v)
          Splits rows of this matrix in multiple vectors.
 int svd(Matrix Q, Matrix U, Matrix V)
          Finds Singular Values Decomposition SVD of this matrix.
 int svd(RVector s)
          Finds the singular value of this matrix.
 java.lang.String toString()
          Returns a string that contains the values of this Matrix.
 java.lang.String toString(java.lang.String format)
          Returns a string that contains the values of this Matrix, according to the format string
 
Methods inherited from class javax.vecmath.GMatrix
add, add, clone, copySubMatrix, epsilonEquals, epsilonEquals, equals, equals, get, get, get, get, get, getColumn, getColumn, getElement, getNumCol, getNumRow, getRow, getRow, hashCode, identityMinus, invert, invert, LUD, mul, mul, mul, mulTransposeBoth, mulTransposeLeft, mulTransposeRight, negate, negate, set, set, set, set, set, set, setColumn, setColumn, setElement, setIdentity, setRow, setRow, setScale, setSize, setZero, sub, sub, SVD, trace, transpose, transpose
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

SEMIDEFINITE_NEGATIVE

public static final short SEMIDEFINITE_NEGATIVE
Type of matrix.

See Also:
eig(Matrix, Matrix, RVector, RVector), Constant Field Values

DEFINITE_NEGATIVE

public static final short DEFINITE_NEGATIVE
Type of matrix.

See Also:
eig(Matrix, Matrix, RVector, RVector), Constant Field Values

SEMIDEFINITE_POSITIVE

public static final short SEMIDEFINITE_POSITIVE
Type of matrix.

See Also:
eig(Matrix, Matrix, RVector, RVector), Constant Field Values

DEFINITE_POSITIVE

public static final short DEFINITE_POSITIVE
Type of matrix.

See Also:
eig(Matrix, Matrix, RVector, RVector), Constant Field Values
Constructor Detail

Matrix

public Matrix(javax.vecmath.GMatrix matrix)
Constructs a new matrix with the same values as the GMatrix parameter.

Parameters:
matrix - the source matrix

Matrix

public Matrix(Matrix matrix)
Constructs a new matrix with the same values as the Matrix parameter.

Parameters:
matrix - the source matrix

Matrix

public Matrix(int n)
Constructs an n by n identity matrix.

Parameters:
n - number of rows and columns in this matrix.

Matrix

public Matrix(int nRow,
              int nCol)
Constructs an nRow by NCol identity matrix. Note that because row and column numbering begins with zero, nRow and nCol will be one larger than the maximum possible matrix index values.

Parameters:
nRow - number of rows in this matrix.
nCol - number of columns in this matrix.

Matrix

public Matrix(int n,
              double[] matrix)
Constructs an n by n square matrix initialized to the values in the matrix array. The array values are copied in one row at a time in row major fashion. The array should be at least n*n in length. Note that because row and column numbering begins with zero, nRow and nCol will be one larger than the maximum possible matrix index values.

Parameters:
n - number of rows and columns in this matrix.
matrix - the array that specifies a matrix in row major fashion

Matrix

public Matrix(int nRow,
              int nCol,
              double[] matrix)
Constructs an nRow by nCol matrix initialized to the values in the matrix array. The array values are copied in one row at a time in row major fashion. The array should be at least nRow*nCol in length. Note that because row and column numbering begins with zero, nRow and nCol will be one larger than the maximum possible matrix index values.

Parameters:
nRow - number of rows in this matrix.
nCol - number of columns in this matrix.
matrix - the array that specifies a matrix in row major fashion
Method Detail

printf

public void printf()
Prints to standard out


printf

public void printf(java.lang.String format)
Prints to standard out according to the format string.

The format specifiers for numeric types have the following syntax: %[width][.precision]conversion

Example:
v.printf("%5.3f");

Parameters:
format - The format string
See Also:
PrintStream.format(java.lang.String, java.lang.Object[])

printf

public void printf(java.io.PrintStream out)
Prints to PrintStream

Parameters:
out - The PrintStream object.

printf

public void printf(java.io.PrintStream out,
                   java.lang.String format)
Prints to PrintStream according to the format string

Parameters:
out - The PrintStream object.
format - The format string.
See Also:
printf(String)

toString

public java.lang.String toString()
Returns a string that contains the values of this Matrix.

Overrides:
toString in class javax.vecmath.GMatrix
Returns:
The string.

toString

public java.lang.String toString(java.lang.String format)
Returns a string that contains the values of this Matrix, according to the format string

Parameters:
format - The format string
Returns:
The string
See Also:
printf(String)

svd

public int svd(RVector s)
Finds the singular value of this matrix. This is the jacobi version

Author: G. Jungman, Java version: Carmine Lia

Algorithm due to J.C. Nash, Compact Numerical Methods for Computers (New York: Wiley and Sons, 1979), chapter 3. See also Algorithm 4.1 in James Demmel, Kresimir Veselic, "Jacobi's Method is more accurate than QR", Lapack Working Note 15 (LAWN15), October 1989. Available from netlib. http://netlib.org/lapack/ in the lawns or lawnspdf directories

Based on code by Arthur Kosowsky, Rutgers University kosowsky@physics.rutgers.edu

Another relevant paper is, P.P.M. De Rijk, "A One-Sided Jacobi Algorithm for computing the singular value decomposition on a vector computer", SIAM Journal of Scientific and Statistical Computing, Vol 10, No 2, pp 359-371, March 1989.

Parameters:
s - RVector of n singular values
Returns:
Rank of this matrix, if negative an error occurred.

svd

public int svd(Matrix Q,
               Matrix U,
               Matrix V)
Finds Singular Values Decomposition SVD of this matrix.

Factorise this M x N matrix,
this = U Q V^T
where U is a column-orthogonal M x N matrix (U^T U = I), Q is a diagonal N x N matrix, and V is an N x N orthogonal matrix (V^T V = V V^T = I).

Parameters:
Q - the n x n diagonal matrix of singular values
U - the column-orthogonal m x n matrix
V - the orthogonal n x n matrix
Returns:
the rank of this matrix.
See Also:
SingularValueDecomposition

eig

public void eig(Matrix D,
                Matrix V)
Eigenvalues and eigenvectors of a nxn real matrix.

Parameters:
D - the nxn block diagonal eigenvalue matrix
V - the nxn eigenvector matrix
See Also:
EigenvalueDecomposition

eig

public void eig(Matrix D,
                Matrix V,
                RVector d,
                RVector e)
Eigenvalues and eigenvectors of a nxn real matrix.

Parameters:
D - the nxn block diagonal eigenvalue matrix
V - the nxn eigenvector matrix
d - the real part of the eigenvalues
e - the imaginary parts of the eigenvalues
See Also:
EigenvalueDecomposition

eig

public void eig(RVector d)
Eigenvalues of a nxn real matrix.

Parameters:
d - the real part of the eigenvalues
See Also:
EigenvalueDecomposition

eig

public RVector eig()
Eigenvalues of a nxn real matrix.

Returns:
d the real part of the eigenvalues
See Also:
EigenvalueDecomposition

checkEigenValue

public static short checkEigenValue(RVector d)
Check the eigenvalues.

Parameters:
d - the vector of real part of eigenvalues.
Returns:
the type of matrix: (semi)-definite negative/positive otherwise -1.
See Also:
DEFINITE_NEGATIVE, DEFINITE_POSITIVE, SEMIDEFINITE_NEGATIVE, SEMIDEFINITE_POSITIVE

pseudoInverse

public Matrix pseudoInverse()
Computes pseudo inverse of this matrix using SVD decomposition.

Returns:
the pseudo inverse matrix.

inverse

public Matrix inverse(double eps,
                      double lambdaMax)
Computes Damped Least Square inverse of this matrix using SVD decomposition.

A-1 = AT*(A*AT + λ2*I)-1
where λ2 is the damped factor equals 1 - (σr/ε)*λ2max if σr< ε, equals 0 if σr≥ ε; σr is the smallest not null singular value.

Parameters:
eps - the area of singular region.
lambdaMax - the maximum damped factor.
Returns:
the inverse matrix, zero matrix if rank=0.

inverse

public Matrix inverse()
Computes inverse or pseudoinverse of this matrix

Returns:
inverse if this is square, pseudoinverse otherwise.

lyap

public Matrix lyap(Matrix C)
Computes the solution of Lyapunov equation using eigenvalue decomposition. The return matrix is the solution of equation:
A*X + X*A' = -C
where the matrix A is this matrix, the matrix X is the solution and C is the input matrix. Both the matrices must be square with same dimensions.

Warning! the function fail if the eigenvalue of A are complex.

Parameters:
C - input matrix of equation
Returns:
the matrix solution of equation.

setOnes

public void setOnes()
Sets all elements of this matrix to one


getDiagonal

public RVector getDiagonal()
Gets main diagonal of this matrix.

Returns:
RVector equals diagonal of matrix

setDiagonal

public void setDiagonal(RVector diag)
Sets the diagonal of this matrix. The number of elements placed is the minimum between this.nRow, this.nCol, RVector.size

Parameters:
diag - elements to be placed

get

public void get(double[] matrix)
Gets this matrix as mono dimensional array

Parameters:
matrix - the array

get

public void get(double[][] matrix)
Gets this matrix as bi-dimensional array

Parameters:
matrix - the array copied

set

public void set(double[][] matrix)
Sets this matrix from bi-dimensional array

Parameters:
matrix - the source array

getSubMatrix

public void getSubMatrix(int startRow,
                         int endRow,
                         int startCol,
                         int endCol,
                         Matrix M)
Gets sub matrix from this Matrix.

Parameters:
startRow -
endRow -
startCol -
endCol -
M - the sub matrix

arrayRightDivide

public Matrix arrayRightDivide(Matrix B)
Element-by-element right division, C = this./B

Parameters:
B - another matrix
Returns:
this./B

arrayRightDivideEquals

public Matrix arrayRightDivideEquals(Matrix B)
Element-by-element right division in place, this = this./B

Parameters:
B - another matrix
Returns:
this./B

arrayLeftDivide

public Matrix arrayLeftDivide(Matrix B)
Element-by-element left division, C = this.\B

Parameters:
B - another matrix
Returns:
this.\B

arrayLeftDivideEquals

public Matrix arrayLeftDivideEquals(Matrix B)
Element-by-element left division in place, this = this.\B

Parameters:
B - another matrix
Returns:
this.\B

isSymmetric

public boolean isSymmetric()
Check if this matrix is symmetric

Returns:
true if this matrix is symmetric

mul

public void mul(DMatrix D)
Sets the value of this matrix to the result of multiplying itself with diagonal matrix D (this = this * D).

Parameters:
D - the input diagonal matrix.

splitColumns

public void splitColumns(RVector... v)
Splits columns of this matrix in multiple vectors.

Parameters:
v - the destination array of vectors

splitColumns

public void splitColumns(double[]... v)
Splits columns of this matrix in multiple arrays.

Parameters:
v - the destination array of arrays

splitRows

public void splitRows(RVector... v)
Splits rows of this matrix in multiple vectors.

Parameters:
v - the destination array of vectors

splitRows

public void splitRows(double[]... v)
Splits rows of this matrix in multiple arrays.

Parameters:
v - the destination array of arrays