org.openmali.vecmath2
Class MatrixMxNf

java.lang.Object
  extended by org.openmali.vecmath2.MatrixMxNf
All Implemented Interfaces:
java.lang.Cloneable
Direct Known Subclasses:
Matrix3f, Matrix4f

public class MatrixMxNf
extends java.lang.Object
implements java.lang.Cloneable

A float precision, general, real, two dimensional m x n matrix class. Row and column numbering is zero-based. The representation is row major. Inspired by Kenji Hiranabe's GMatrix implementation


Field Summary
protected  int colSkip
          A matrix might be part of another, larger matrix.
protected  int dataBegin
          A matrix might be part of another, larger matrix.
protected  boolean[] isDirty
           
protected  int roTrick
          This is used to costlessly make the Matrix read-only.
protected  float[] values
          The data of the Matrix
 
Constructor Summary
protected MatrixMxNf(boolean readOnly, int rows, int cols)
          Constructs an nRow by nCol identity matrix.
protected MatrixMxNf(boolean readOnly, int rows, int cols, float[] values)
          Constructs an rows by cols matrix initialized to the values in the matrix array.
protected MatrixMxNf(boolean readOnly, int dataBegin, int colskip, int rows, int cols, float[] values, boolean[] isDirty)
          Hidden constructor for #sharedSubMatrixMxNf(MatrixMxNf, int, int) and #getSharedSubMatrix(int, int, int, int).
protected MatrixMxNf(boolean readOnly, MatrixMxNf matrix)
          Constructs a new MatrixMxNf and copies the initial values from the parameter matrix.
  MatrixMxNf(int rows, int cols)
          Constructs an nRow by nCol identity matrix.
  MatrixMxNf(int rows, int cols, float[] values)
          Constructs an nRow by nCol matrix initialized to the values in the matrix array.
protected MatrixMxNf(int dataBegin, int colskip, int rows, int cols, float[] values)
          Hidden constructor for #sharedSubMatrixMxNf(MatrixMxNf, int, int) and #getSharedSubMatrix(int, int, int, int).
  MatrixMxNf(MatrixMxNf matrix)
          Constructs a new GMatrix and copies the initial values from the parameter matrix.
 
Method Summary
 MatrixMxNf add(float scalar)
          Adds a scalar to this Matrix.
 MatrixMxNf add(int row, int column, float summand)
          Adds the given summand to the element identified by row and column.
 MatrixMxNf add(MatrixMxNf m2)
          Sets the value of this matrix to sum of itself and matrix m2.
 MatrixMxNf add(MatrixMxNf m1, MatrixMxNf m2)
          Sets the value of this matrix to the matrix sum of matrices m1 and m2.
 MatrixMxNf asReadOnly()
           
 MatrixMxNf clone()
          
 float cond()
          Matrix condition (2 norm)
 void copySubMatrix(int rowSource, int colSource, int numRows, int numCols, int rowDest, int colDest, MatrixMxNf target)
          Copies a sub-matrix derived from this matrix into the target matrix.
 int deserialize(int pos, byte[] buffer)
          Deserializes this instanc'es data from the byte array.
 MatrixMxNf div(int row, int column, float divisor)
          Devides the element identified by row and column with the given divisor.
 boolean epsilonEquals(MatrixMxNf mat2, float epsilon)
          Returns true if the L-infinite distance between this matrix and matrix m1 is less than or equal to the epsilon parameter, otherwise returns false.
 boolean equals(MatrixMxNf mat2)
          Returns true if all of the data members of Matrix4d m1 are equal to the corresponding data members in this Matrix4d.
 boolean equals(java.lang.Object o)
          Returns true if the Object o1 is of type GMatrix and all of the data members of t1 are equal to the corresponding data members in this Matrix.
 float get(int row, int column)
          Retrieves the value at the specified row and column of this matrix.
protected  float get(int row, int colSkip, int column)
           
 void get(Matrix3f mat)
          Places the values in the upper 3x3 of this Matrix into the matrix mat.
 void get(Matrix4f mat)
          Places the values in the upper 4x4 of this GMatrix into the matrix mat.
 void get(MatrixMxNf mat)
          Places the values in the this matrix into the matrix mat; mat should be at least as large as this Matrix.
 void getColumn(int col, float[] buffer)
          Places the values of the specified column into the array parameter.
 void getColumn(int col, TupleNf<?> tuple)
          Places the values of the specified column into the vector parameter.
 void getColumnMajor(float[] values)
          Sets the value of this matrix to the values found in the array parameter.
 void getColumnMajor(float[] values, int offset)
          Sets the value of this matrix to the values found in the array parameter.
 int getNumCols()
           
 int getNumRows()
           
 MatrixMxNf getReadOnly()
           
 void getRow(int row, float[] buffer)
          Places the values of the specified row into the array parameter.
 void getRow(int row, TupleNf<?> tuple)
          Places the values of the specified row into the vector parameter.
 void getRowMajor(float[] values)
          Sets the value of this matrix to the values found in the array parameter.
 void getRowMajor(float[] values, int offset)
          Sets the value of this matrix to the values found in the array parameter.
protected  int getSerializationBufferSize()
           
 MatrixMxNf getSharedSubMatrix(boolean readOnly, int beginRow, int beginCol, int rows, int cols)
          Creates a Submatrix of mat, that begins in beginRow and beginCol.
 MatrixMxNf getSharedSubMatrix(int beginRow, int beginCol, int rows, int cols)
          Creates a Submatrix of mat, that begins in beginRow and beginCol.
 int hashCode()
          Returns a hash number based on the data values in this object.
 MatrixMxNf identityMinus()
          Subtracts this matrix from the identity matrix and puts the values back into this (this = I - this).
 void interpolate(MatrixMxNf m1, MatrixMxNf m2, float alpha)
          Interpolates each value of this Matrix by the value alpha.
 MatrixMxNf invert()
          Inverts this matrix in place.
 MatrixMxNf invert(MatrixMxNf m)
          Inverts matrix m and places the new values into this matrix.
 boolean isDirty()
           
 boolean isPositiveDefinite()
          Tests whether the matrix is positive definite.
 boolean isPositiveDefinite(int n)
          Tests whether a submatrix is positive definite.
 boolean isReadOnly()
           
 boolean isSquare()
           
 boolean isSubMatrix()
           
 int LUD(MatrixMxNf lu, VectorNf permutation)
          LU Decomposition; this matrix must be a square matrix; the LU GMatrix parameter must be the same size as this matrix.
 MatrixMxNf mul(float scalar)
          Multiplies this Matrix with a scalar.
 MatrixMxNf mul(int row, int column, float factor)
          Multiplies the element identified by row and column with the given factor.
 MatrixMxNf mul(MatrixMxNf mat2)
          Sets the value of this matrix to the result of multiplying itself with matrix m1 (this = this * m1).
 MatrixMxNf mul(MatrixMxNf m1, MatrixMxNf m2)
          Sets the value of this matrix to the result of multiplying the two argument matrices together (this = m1 * m2).
 MatrixMxNf mul(TupleNf<?> v1, TupleNf<?> v2)
          Computes the outer product of the two vectors; multiplies the the first vector by the transpose of the second vector and places the matrix result into this matrix.
 MatrixMxNf mulComp(MatrixMxNf mat2)
          Sets the value of this matrix to the result of multiplying this matrix and the other one componentwisely (this = this x m2).
 MatrixMxNf mulComp(MatrixMxNf m1, MatrixMxNf m2)
          Sets the value of this matrix to the result of multiplying the two argument matrices componentwisely (this = m1 x m2).
 MatrixMxNf mulTransposeBoth(MatrixMxNf m1, MatrixMxNf m2)
          Multiplies the transpose of matrix m1 times the transpose of matrix m2, and places the result into this.
 MatrixMxNf mulTransposeLeft(MatrixMxNf m1, MatrixMxNf m2)
          Multiplies the transpose of matrix m1 times the matrix m2, and places the result into this.
 MatrixMxNf mulTransposeRight(MatrixMxNf m1, MatrixMxNf m2)
          Multiplies matrix m1 times the transpose of matrix m2, and places the result into this.
 MatrixMxNf negate()
          Negates the value of this matrix: this = -this.
 MatrixMxNf negate(MatrixMxNf m)
          Sets the value of this matrix to the negation of the Matrix parameter.
static MatrixMxNf newReadOnly(int rows, int cols)
          Constructs an nRow by nCol identity matrix.
static MatrixMxNf newReadOnly(int rows, int cols, float[] values)
          Constructs an nRow by nCol matrix initialized to the values in the matrix array.
static MatrixMxNf newReadOnly(MatrixMxNf matrix)
          Constructs a new GMatrix and copies the initial values from the parameter matrix.
 float norm1()
          Calculates the One-norm.
 float norm2()
          Calculates the One-norm.
 float normFrobenius()
          Calculates the Frobenius-norm.
 float normInfinity()
          Calculates the Infinity-norm.
 int rank()
          Computes the effective numerical rank, obtained from SVD.
 void readExternal(java.io.ObjectInput in)
           
 int serialize(int pos, byte[] buffer)
          Serializes this instanc'es data into the byte array.
 MatrixMxNf set(int row, int column, float value)
          Modifies the value at the specified row and column of this matrix.
protected  MatrixMxNf set(int row, int colSkip, int column, float value)
           
 void set(Matrix3f mat)
          Sets the value of this matrix to that of the Matrix3f provided.
 void set(Matrix4f mat)
          Sets the value of this matrix to that of the Matrix4f provided.
 void set(MatrixMxNf mat)
          Sets the value of this matrix to the values found in matrix mat.
 boolean setClean()
          Marks this Matrix non-dirty.
 MatrixMxNf setColumn(int col, float[] values)
          Copies the values from the array into the specified column of this matrix.
 MatrixMxNf setColumn(int col, TupleNf<?> tuple)
          Copy the values from the array into the specified column of this matrix.
 void setColumnMajor(float[] values)
          Sets the value of this matrix to the values found in the array parameter.
 void setColumnMajor(float[] values, int offset)
          Sets the value of this matrix to the values found in the array parameter.
 MatrixMxNf setIdentity()
          Sets this GMatrix to the identity matrix.
 MatrixMxNf setRow(int row, float[] values)
          Copies the values from the array into the specified row of this matrix.
 MatrixMxNf setRow(int row, TupleNf<?> tuple)
          Copies the values from the array into the specified row of this matrix.
 void setRowMajor(float[] values)
          Sets the value of this matrix to the values found in the array parameter.
 void setRowMajor(float[] values, int offset)
          Sets the value of this matrix to the values found in the array parameter.
 MatrixMxNf setScale(float scale)
          Sets this matrix to a uniform scale matrix; all of the values are reset.
 MatrixMxNf setZero()
          Sets all the values in this matrix to zero.
static MatrixMxNf sharedSubMatrixMxNf(MatrixMxNf mat, int beginRow, int beginCol, int rows, int cols)
          Creates a shared Submatrix of mat.
static MatrixMxNf sharedSubMatrixMxNf(MatrixMxNf mat, int beginRow, int beginCol, int rows, int cols, boolean readOnly)
          Creates a shared Submatrix of mat.
 void solve(MatrixMxNf m2, MatrixMxNf result)
          Solves this * X = m2.
 boolean solve(TupleNf<?> b, TupleNf<?> passback)
          Performs gaussian elimination with parial pivoting.
 MatrixMxNf sub(float scalar)
          Subtracts a scalar from this Matrix.
 MatrixMxNf sub(int row, int column, float value)
          Subtracts the given summand from the element identified by row and column.
 MatrixMxNf sub(MatrixMxNf m2)
          Sets the value of this matrix to the matrix difference of itself and matrix m2 (this = this - m2).
 MatrixMxNf sub(MatrixMxNf m1, MatrixMxNf m2)
          Sets the value of this matrix to the matrix difference of matrices m1 and m2 (this = m1 - m2).
 int SVD(MatrixMxNf u, MatrixMxNf w, MatrixMxNf v)
          Finds the singular value decomposition (SVD) of this matrix such that this = U*W*transpose(V); and returns the rank of this matrix; the values of U,W,V are all overwritten.
 java.lang.String toString()
          
 float trace()
           
 MatrixMxNf transpose()
          Transposes this matrix in place, this can only be done on square matrices.
 MatrixMxNf transpose(MatrixMxNf mat)
          Places the matrix values of the transpose of matrix mat into this matrix.
 void writeExternal(java.io.ObjectOutput out)
           
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

values

protected final float[] values
The data of the Matrix. (1D array. The (i, j) element is stored in elementData[ i * col + j ])


dataBegin

protected final int dataBegin
A matrix might be part of another, larger matrix. Because we don't want to copy changes on the backing-matrix everytime to the submatrix, they share the same data-array.
dataBegin describes, where the data of this matrix begins.
For normal use this is 0.


colSkip

protected final int colSkip
A matrix might be part of another, larger matrix. Because we don't want to copy changes on the backing-matrix everytime to the submatrix, they share the same data-array.
colskip describes, how many columns the submatrix has, so we really are able to simulate the submatrix.
For normal use this is equal to cols.


roTrick

protected final int roTrick
This is used to costlessly make the Matrix read-only. In case of a read-only Matrix this value will be the negative ten-th of values.length. This will cause an ArrayIndexOutOfBoundsException when a read-only Matrix gets manipulated.


isDirty

protected final boolean[] isDirty
Constructor Detail

MatrixMxNf

protected MatrixMxNf(boolean readOnly,
                     int rows,
                     int cols)
Constructs an nRow by nCol identity matrix. Note that even though row and column numbering begins with zero, nRow and nCol will be one larger than the maximum possible matrix index values.

Parameters:
readOnly -
rows - number of rows in this matrix.
cols - number of columns in this matrix.

MatrixMxNf

protected MatrixMxNf(boolean readOnly,
                     int rows,
                     int cols,
                     float[] values)
Constructs an rows by cols 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 even though row and column numbering begins with zero, nRow and nCol will be one larger than the maximum possible matrix index values.

Parameters:
readOnly -
rows - number of rows in this matrix.
cols - number of columns in this matrix.
values - a 1D array that specifies a matrix in row major fashion

MatrixMxNf

protected MatrixMxNf(boolean readOnly,
                     MatrixMxNf matrix)
Constructs a new MatrixMxNf and copies the initial values from the parameter matrix.

Parameters:
readOnly -
matrix - the source of the initial values of the new GMatrix

MatrixMxNf

protected MatrixMxNf(boolean readOnly,
                     int dataBegin,
                     int colskip,
                     int rows,
                     int cols,
                     float[] values,
                     boolean[] isDirty)
Hidden constructor for #sharedSubMatrixMxNf(MatrixMxNf, int, int) and #getSharedSubMatrix(int, int, int, int).

Parameters:
readOnly -
dataBegin -
colskip -
rows -
cols -
values -
isDirty -

MatrixMxNf

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

Parameters:
rows - number of rows in this matrix.
cols - number of columns in this matrix.

MatrixMxNf

public MatrixMxNf(int rows,
                  int cols,
                  float[] values)
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 even though row and column numbering begins with zero, nRow and nCol will be one larger than the maximum possible matrix index values.

Parameters:
rows - number of rows in this matrix.
cols - number of columns in this matrix.
values - a 1D array that specifies a matrix in row major fashion

MatrixMxNf

public MatrixMxNf(MatrixMxNf matrix)
Constructs a new GMatrix and copies the initial values from the parameter matrix.

Parameters:
matrix - the source of the initial values of the new GMatrix

MatrixMxNf

protected MatrixMxNf(int dataBegin,
                     int colskip,
                     int rows,
                     int cols,
                     float[] values)
Hidden constructor for #sharedSubMatrixMxNf(MatrixMxNf, int, int) and #getSharedSubMatrix(int, int, int, int).

Parameters:
dataBegin -
colskip -
values -
Method Detail

getNumRows

public final int getNumRows()
Returns:
number of rows in this matrix

getNumCols

public final int getNumCols()
Returns:
number of columns in this matrix

isSquare

public final boolean isSquare()
Returns:
Is this Matrix a square-Matrix?

isReadOnly

public final boolean isReadOnly()
Returns:
Is this Matrix a read-only-Matrix?

setClean

public final boolean setClean()
Marks this Matrix non-dirty. Any value-manipulation will mark it dirty again.

Returns:
the old value

isDirty

public final boolean isDirty()
Returns:
This Matrix' dirty-flag

isSubMatrix

public final boolean isSubMatrix()
Returns:
true, if this Matrix is backed by another (usually larger) matrix.

set

public final MatrixMxNf set(int row,
                            int column,
                            float value)
Modifies the value at the specified row and column of this matrix.

Parameters:
row - the row number to be modified (zero indexed)
column - the column number to be modified (zero indexed)
value - the new matrix element value
Returns:
itself

set

protected final MatrixMxNf set(int row,
                               int colSkip,
                               int column,
                               float value)

get

public final float get(int row,
                       int column)
Retrieves the value at the specified row and column of this matrix.

Parameters:
row - the row number to be retrieved (zero indexed)
column - the column number to be retrieved (zero indexed)
Returns:
the value at the indexed element

get

protected final float get(int row,
                          int colSkip,
                          int column)

add

public final MatrixMxNf add(int row,
                            int column,
                            float summand)
Adds the given summand to the element identified by row and column.

Parameters:
row - the row number to be modified (zero indexed)
column - the column number to be modified (zero indexed)
summand - the value to add
Returns:
itself

sub

public final MatrixMxNf sub(int row,
                            int column,
                            float value)
Subtracts the given summand from the element identified by row and column.

Parameters:
row - the row number to be modified (zero indexed)
column - the column number to be modified (zero indexed)
value - the value to subtract
Returns:
itself

mul

public final MatrixMxNf mul(int row,
                            int column,
                            float factor)
Multiplies the element identified by row and column with the given factor.

Parameters:
row - the row number to be modified (zero indexed)
column - the column number to be modified (zero indexed)
factor - the factor to multiply by
Returns:
itself

div

public final MatrixMxNf div(int row,
                            int column,
                            float divisor)
Devides the element identified by row and column with the given divisor.

Parameters:
row - the row number to be modified (zero indexed)
column - the column number to be modified (zero indexed)
divisor - the factor to multiply by
Returns:
itself

setRow

public final MatrixMxNf setRow(int row,
                               float[] values)
Copies the values from the array into the specified row of this matrix.

Parameters:
row - the row of this matrix into which the array values will be copied.
values - the source array
Returns:
itself

setRow

public final MatrixMxNf setRow(int row,
                               TupleNf<?> tuple)
Copies the values from the array into the specified row of this matrix.

Parameters:
row - the row of this matrix into which the vector values will be copied.
tuple - the source vector
Returns:
itself

getRow

public final void getRow(int row,
                         float[] buffer)
Places the values of the specified row into the array parameter.

Parameters:
row - the target row number
buffer - the array into which the row values will be placed

getRow

public final void getRow(int row,
                         TupleNf<?> tuple)
Places the values of the specified row into the vector parameter.

Parameters:
row - the target row number
tuple - the vector into which the row values will be placed

setColumn

public final MatrixMxNf setColumn(int col,
                                  float[] values)
Copies the values from the array into the specified column of this matrix.

Parameters:
col - the column of this matrix into which the array values will be copied.
values - the source array
Returns:
itself

setColumn

public final MatrixMxNf setColumn(int col,
                                  TupleNf<?> tuple)
Copy the values from the array into the specified column of this matrix.

Parameters:
col - the column of this matrix into which the vector values will be copied.
tuple - the source vector
Returns:
itself

getColumn

public final void getColumn(int col,
                            float[] buffer)
Places the values of the specified column into the array parameter.

Parameters:
col - the target column number
buffer - the array into which the column values will be placed

getColumn

public final void getColumn(int col,
                            TupleNf<?> tuple)
Places the values of the specified column into the vector parameter.

Parameters:
col - the target column number
tuple - the vector into which the column values will be placed

setRowMajor

public final void setRowMajor(float[] values,
                              int offset)
Sets the value of this matrix to the values found in the array parameter. The values are copied in one row at a time, in row major fashion. The array should be at least equal in length to the number of matrix rows times the number of matrix columns in this matrix.

Parameters:
values - the row major source array
offset - the offset in the (source) values array

setRowMajor

public final void setRowMajor(float[] values)
Sets the value of this matrix to the values found in the array parameter. The values are copied in one row at a time, in row major fashion. The array should be at least equal in length to the number of matrix rows times the number of matrix columns in this matrix.

Parameters:
values - the row major source array

setColumnMajor

public final void setColumnMajor(float[] values,
                                 int offset)
Sets the value of this matrix to the values found in the array parameter. The values are copied in one row at a time, in row major fashion. The array should be at least equal in length to the number of matrix rows times the number of matrix columns in this matrix.

Parameters:
values - the column major source array
offset - the offset in the (source) values array

setColumnMajor

public final void setColumnMajor(float[] values)
Sets the value of this matrix to the values found in the array parameter. The values are copied in one row at a time, in row major fashion. The array should be at least equal in length to the number of matrix rows times the number of matrix columns in this matrix.

Parameters:
values - the column major source array

set

public void set(Matrix3f mat)
Sets the value of this matrix to that of the Matrix3f provided.

Parameters:
mat - the source matrix

set

public final void set(Matrix4f mat)
Sets the value of this matrix to that of the Matrix4f provided.

Parameters:
mat - the source matrix

set

public final void set(MatrixMxNf mat)
Sets the value of this matrix to the values found in matrix mat.

Parameters:
mat - the source matrix

getRowMajor

public final void getRowMajor(float[] values,
                              int offset)
Sets the value of this matrix to the values found in the array parameter. The values are copied in one row at a time, in row major fashion. The array should be at least equal in length to the number of matrix rows times the number of matrix columns in this matrix.

Parameters:
values - the row major source array
offset - the offset in the (target) values array

getRowMajor

public final void getRowMajor(float[] values)
Sets the value of this matrix to the values found in the array parameter. The values are copied in one row at a time, in row major fashion. The array should be at least equal in length to the number of matrix rows times the number of matrix columns in this matrix.

Parameters:
values - the row major source array

getColumnMajor

public final void getColumnMajor(float[] values,
                                 int offset)
Sets the value of this matrix to the values found in the array parameter. The values are copied in one row at a time, in row major fashion. The array should be at least equal in length to the number of matrix rows times the number of matrix columns in this matrix.

Parameters:
values - the column major source array
offset - the offset in the (target) values array

getColumnMajor

public final void getColumnMajor(float[] values)
Sets the value of this matrix to the values found in the array parameter. The values are copied in one row at a time, in row major fashion. The array should be at least equal in length to the number of matrix rows times the number of matrix columns in this matrix.

Parameters:
values - the column major source array

get

public void get(Matrix3f mat)
Places the values in the upper 3x3 of this Matrix into the matrix mat.

Parameters:
mat - The matrix that will hold the new values

get

public final void get(Matrix4f mat)
Places the values in the upper 4x4 of this GMatrix into the matrix mat.

Parameters:
mat - The matrix that will hold the new values

get

public final void get(MatrixMxNf mat)
Places the values in the this matrix into the matrix mat; mat should be at least as large as this Matrix.

Parameters:
mat - The matrix that will hold the new values

setScale

public MatrixMxNf setScale(float scale)
Sets this matrix to a uniform scale matrix; all of the values are reset.

Parameters:
scale - the new scale value
Returns:
itself

setZero

public final MatrixMxNf setZero()
Sets all the values in this matrix to zero.

Returns:
itself

setIdentity

public final MatrixMxNf setIdentity()
Sets this GMatrix to the identity matrix.

Returns:
itself

negate

public final MatrixMxNf negate()
Negates the value of this matrix: this = -this.

Returns:
itself

negate

public final MatrixMxNf negate(MatrixMxNf m)
Sets the value of this matrix to the negation of the Matrix parameter.

Parameters:
m - The source matrix
Returns:
itself

identityMinus

public final MatrixMxNf identityMinus()
Subtracts this matrix from the identity matrix and puts the values back into this (this = I - this).

Returns:
itself

invert

public MatrixMxNf invert()
Inverts this matrix in place.

Returns:
itself

invert

public MatrixMxNf invert(MatrixMxNf m)
Inverts matrix m and places the new values into this matrix. Matrix m is not modified.

Parameters:
m - the matrix to be inverted
Returns:
itself

transpose

public MatrixMxNf transpose()
Transposes this matrix in place, this can only be done on square matrices. Attempting to call this method on a non-square matrix throws a runtime exception. See the other transpose method for transposing non-square matrices.

Returns:
itself

transpose

public MatrixMxNf transpose(MatrixMxNf mat)
Places the matrix values of the transpose of matrix mat into this matrix.
If matrix mat is MxN then this matrix must be NxM to hold the transpose.

Parameters:
mat - the matrix to be transposed (but not modified)
Returns:
itself

norm1

public float norm1()
Calculates the One-norm.

Returns:
maximum column sum.

norm2

public final float norm2()
Calculates the One-norm.

Returns:
maximum singular value.

normInfinity

public float normInfinity()
Calculates the Infinity-norm.

Returns:
maximum maximum row sum.

normFrobenius

public float normFrobenius()
Calculates the Frobenius-norm.

Returns:
sqrt of sum of squares of all elements.

isPositiveDefinite

public boolean isPositiveDefinite(int n)
Tests whether a submatrix is positive definite. This tests the upper-left nxn submatrix.

Parameters:
n - the size of the submatrix to be tested
Returns:
true if the submatrix is positive definite, and false otherwise

isPositiveDefinite

public boolean isPositiveDefinite()
Tests whether the matrix is positive definite.

Returns:
true if the matrix is positive definite, and false otherwise

trace

public final float trace()
Returns:
the trace of this matrix.

rank

public final int rank()
Computes the effective numerical rank, obtained from SVD.

Returns:
Matrix rank

cond

public final float cond()
Matrix condition (2 norm)

Returns:
ratio of largest to smallest singular value.

copySubMatrix

public final void copySubMatrix(int rowSource,
                                int colSource,
                                int numRows,
                                int numCols,
                                int rowDest,
                                int colDest,
                                MatrixMxNf target)
Copies a sub-matrix derived from this matrix into the target matrix. The upper left of the sub-matrix is located at (rowSource, colSource); the lower right of the sub-matrix is located at (lastRowSource,lastColSource). The sub-matrix is copied into the the target matrix starting at (rowDest, colDest).

Parameters:
rowSource - the top-most row of the sub-matrix
colSource - the left-most column of the sub-matrix
numRows - the number of rows in the sub-matrix
numCols - the number of columns in the sub-matrix
rowDest - the top-most row of the position of the copied sub-matrix within the target matrix
colDest - the left-most column of the position of the copied sub-matrix within the target matrix
target - the matrix into which the sub-matrix will be copied

add

public final MatrixMxNf add(float scalar)
Adds a scalar to this Matrix.

Parameters:
scalar -
Returns:
itself

add

public final MatrixMxNf add(MatrixMxNf m2)
Sets the value of this matrix to sum of itself and matrix m2.

Parameters:
m2 - the other matrix
Returns:
itself

add

public final MatrixMxNf add(MatrixMxNf m1,
                            MatrixMxNf m2)
Sets the value of this matrix to the matrix sum of matrices m1 and m2.

Parameters:
m1 - the first matrix
m2 - the second matrix
Returns:
itself

sub

public final MatrixMxNf sub(float scalar)
Subtracts a scalar from this Matrix.

Parameters:
scalar -
Returns:
itself

sub

public final MatrixMxNf sub(MatrixMxNf m2)
Sets the value of this matrix to the matrix difference of itself and matrix m2 (this = this - m2).

Parameters:
m2 - the other matrix
Returns:
itself

sub

public final MatrixMxNf sub(MatrixMxNf m1,
                            MatrixMxNf m2)
Sets the value of this matrix to the matrix difference of matrices m1 and m2 (this = m1 - m2).

Parameters:
m1 - the first matrix
m2 - the second matrix
Returns:
itself

mul

public MatrixMxNf mul(float scalar)
Multiplies this Matrix with a scalar.

Parameters:
scalar -
Returns:
itself

mul

public MatrixMxNf mul(MatrixMxNf m1,
                      MatrixMxNf m2)
Sets the value of this matrix to the result of multiplying the two argument matrices together (this = m1 * m2).

Parameters:
m1 - the first matrix
m2 - the second matrix
Returns:
itself

mul

public MatrixMxNf mul(MatrixMxNf mat2)
Sets the value of this matrix to the result of multiplying itself with matrix m1 (this = this * m1).

Parameters:
mat2 - the other matrix
Returns:
itself

mulComp

public final MatrixMxNf mulComp(MatrixMxNf m1,
                                MatrixMxNf m2)
Sets the value of this matrix to the result of multiplying the two argument matrices componentwisely (this = m1 x m2).

Parameters:
m1 - the first matrix
m2 - the second matrix
Returns:
itself

mulComp

public final MatrixMxNf mulComp(MatrixMxNf mat2)
Sets the value of this matrix to the result of multiplying this matrix and the other one componentwisely (this = this x m2).

Parameters:
mat2 - the other matrix
Returns:
itself

mul

public MatrixMxNf mul(TupleNf<?> v1,
                      TupleNf<?> v2)
Computes the outer product of the two vectors; multiplies the the first vector by the transpose of the second vector and places the matrix result into this matrix. This matrix must be as big or bigger than getSize(v1) x getSize(v2).

Parameters:
v1 - the first vector, treated as a row vector
v2 - the second vector, treated as a column vector
Returns:
itself

mulTransposeBoth

public MatrixMxNf mulTransposeBoth(MatrixMxNf m1,
                                   MatrixMxNf m2)
Multiplies the transpose of matrix m1 times the transpose of matrix m2, and places the result into this.

Parameters:
m1 - The matrix on the left hand side of the multiplication
m2 - The matrix on the right hand side of the multiplication
Returns:
itself

mulTransposeLeft

public MatrixMxNf mulTransposeLeft(MatrixMxNf m1,
                                   MatrixMxNf m2)
Multiplies the transpose of matrix m1 times the matrix m2, and places the result into this.

Parameters:
m1 - The matrix on the left hand side of the multiplication
m2 - The matrix on the right hand side of the multiplication
Returns:
itself

mulTransposeRight

public MatrixMxNf mulTransposeRight(MatrixMxNf m1,
                                    MatrixMxNf m2)
Multiplies matrix m1 times the transpose of matrix m2, and places the result into this.
Not alias-save!

Returns:
itself

solve

public final void solve(MatrixMxNf m2,
                        MatrixMxNf result)
Solves this * X = m2.

Parameters:
m2 - right hand side
result - if this is square, least squares solution otherwise

solve

public boolean solve(TupleNf<?> b,
                     TupleNf<?> passback)
Performs gaussian elimination with parial pivoting. Input matrix must be a square matrix, input RHS must be a vector of the same size. the passback bust be the size of the matrix. Each equation should be encoded as a row of the matrix eg 2x + 3y = 7 5x - 3y = 8 [2, 3 5,-3] = [7,8]' [cooeffecients] = [rhs] the result, passback, will contain instanciationsof the unkowns that satisfy the equations or if degenerate, the method will return false
IMPORTANT: This method will change b and this Matrix!
mmh VectorNf.LUDBackSolve(MatrixMxNf, VectorNf, VectorNf) seems to do the same. For large Matrices it is probably even more GC-friendly.


SVD

public int SVD(MatrixMxNf u,
               MatrixMxNf w,
               MatrixMxNf v)
Finds the singular value decomposition (SVD) of this matrix such that this = U*W*transpose(V); and returns the rank of this matrix; the values of U,W,V are all overwritten. Note that the matrix V is output as V, and not transpose(V). If this matrix is mxn, then U is mxm, W is a diagonal matrix that is mxn, and V is nxn. Using the notation W = diag(w), then the inverse of this matrix is: inverse(this) = V*diag(1/w)*tranpose(U), where diag(1/w) is the same matrix as W except that the reciprocal of each of the diagonal components is used.

Parameters:
u - The computed U matrix in the equation this = U*W*transpose(V)
w - The computed W matrix in the equation this = U*W*transpose(V)
v - The computed V matrix in the equation this = U*W*transpose(V)
Returns:
The rank of this matrix.

LUD

public int LUD(MatrixMxNf lu,
               VectorNf permutation)
LU Decomposition; this matrix must be a square matrix; the LU GMatrix parameter must be the same size as this matrix. The matrix LU will be overwritten as the combination of a lower diagonal and upper diagonal matrix decompostion of this matrix; the diagonal elements of L (unity) are not stored. The VectorNf parameter records the row permutation effected by the partial pivoting, and is used as a parameter to the VectorNf method LUDBackSolve to solve sets of linear equations. This method returns +/- 1 depending on whether the number of row interchanges was even or odd, respectively.

Parameters:
permutation - The row permutation effected by the partial pivoting
Returns:
+-1 depending on whether the number of row interchanges was even or odd respectively

interpolate

public void interpolate(MatrixMxNf m1,
                        MatrixMxNf m2,
                        float alpha)
Interpolates each value of this Matrix by the value alpha. Mxy = M1xy + ( ( M2xy - M1xy ) * alpha )

Parameters:
m1 -
m2 -
alpha -

hashCode

public int hashCode()
Returns a hash number based on the data values in this object. Two different GMatrix objects with identical data values (ie, returns true for equals(GMatrix) ) will return the same hash number. Two objects with different data members may return the same hash value, although this is not likely.

Overrides:
hashCode in class java.lang.Object
Returns:
the integer hash value

equals

public boolean equals(MatrixMxNf mat2)
Returns true if all of the data members of Matrix4d m1 are equal to the corresponding data members in this Matrix4d.

Parameters:
mat2 - The matrix with which the comparison is made.
Returns:
true or false

equals

public boolean equals(java.lang.Object o)
Returns true if the Object o1 is of type GMatrix and all of the data members of t1 are equal to the corresponding data members in this Matrix.

Overrides:
equals in class java.lang.Object
Parameters:
o - the object with which the comparison is made.

epsilonEquals

public boolean epsilonEquals(MatrixMxNf mat2,
                             float epsilon)
Returns true if the L-infinite distance between this matrix and matrix m1 is less than or equal to the epsilon parameter, otherwise returns false. The L-infinite distance is equal to MAX[i=0,1,2, . . .n ; j=0,1,2, . . .n ; abs(this.m(i,j) - m1.m(i,j)] .

Parameters:
mat2 - The matrix to be compared to this matrix
epsilon - the threshold value

toString

public java.lang.String toString()

Overrides:
toString in class java.lang.Object

clone

public MatrixMxNf clone()

Overrides:
clone in class java.lang.Object

getSharedSubMatrix

public MatrixMxNf getSharedSubMatrix(boolean readOnly,
                                     int beginRow,
                                     int beginCol,
                                     int rows,
                                     int cols)
Creates a Submatrix of mat, that begins in beginRow and beginCol.
Example: let mat be a 4x4 matrix, and we want to have a 2x2 submatrix at position (1,2):
x x x x
x x y y
x x y y
x x x x
the y's mark the fetched Submatrix.
The produced submatrix works on the same data array as mat, so changes are seen on the other one respectively.

Parameters:
readOnly -
beginRow - the row to start this matrix at
beginCol - the column to start this matrix at
rows -
cols -
Returns:
the new shared submatrix
See Also:
MatrixMxNf(int, int, int, int, float[])

getSharedSubMatrix

public MatrixMxNf getSharedSubMatrix(int beginRow,
                                     int beginCol,
                                     int rows,
                                     int cols)
Creates a Submatrix of mat, that begins in beginRow and beginCol.
Example: let mat be a 4x4 matrix, and we want to have a 2x2 submatrix at position (1,2):
x x x x
x x y y
x x y y
x x x x
the y's mark the fetched Submatrix.
The produced submatrix works on the same data array as mat, so changes are seen on the other one respectively.

Parameters:
beginRow - the row to start this matrix at
beginCol - the column to start this matrix at
rows -
cols -
Returns:
the new shared submatrix

asReadOnly

public MatrixMxNf asReadOnly()
Returns:
a new instance sharing the values array with this instance. The new instance is read-only. Changes to this instance will be reflected in the new read-only-instance.
See Also:
getReadOnly()

getReadOnly

public MatrixMxNf getReadOnly()
Returns:
a single instance sharing the values array with this instance (one unique instance per 'master-instance'). The instance is read-only. Changes to this instance will be reflected in the read-only-instance.
See Also:
asReadOnly()

serialize

public int serialize(int pos,
                     byte[] buffer)
Serializes this instanc'es data into the byte array.

Parameters:
pos -
buffer -
Returns:
the incremented position

deserialize

public int deserialize(int pos,
                       byte[] buffer)
Deserializes this instanc'es data from the byte array.

Parameters:
pos -
buffer -
Returns:
the incremented position

getSerializationBufferSize

protected int getSerializationBufferSize()
Returns:
the necessary size for a serialization byte array.

writeExternal

public void writeExternal(java.io.ObjectOutput out)
                   throws java.io.IOException
Throws:
java.io.IOException

readExternal

public void readExternal(java.io.ObjectInput in)
                  throws java.io.IOException,
                         java.lang.ClassNotFoundException
Throws:
java.io.IOException
java.lang.ClassNotFoundException

newReadOnly

public static MatrixMxNf newReadOnly(int rows,
                                     int cols)
Constructs an nRow by nCol identity matrix. Note that even though row and column numbering begins with zero, nRow and nCol will be one larger than the maximum possible matrix index values.

Parameters:
rows - number of rows in this matrix.
cols - number of columns in this matrix.

newReadOnly

public static MatrixMxNf newReadOnly(int rows,
                                     int cols,
                                     float[] values)
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 even though row and column numbering begins with zero, nRow and nCol will be one larger than the maximum possible matrix index values.

Parameters:
rows - number of rows in this matrix.
cols - number of columns in this matrix.
values - a 1D array that specifies a matrix in row major fashion

newReadOnly

public static MatrixMxNf newReadOnly(MatrixMxNf matrix)
Constructs a new GMatrix and copies the initial values from the parameter matrix.

Parameters:
matrix - the source of the initial values of the new GMatrix

sharedSubMatrixMxNf

public static MatrixMxNf sharedSubMatrixMxNf(MatrixMxNf mat,
                                             int beginRow,
                                             int beginCol,
                                             int rows,
                                             int cols,
                                             boolean readOnly)
Creates a shared Submatrix of mat. Does the same as getSharedSubMatrix(int, int, int, int).

Parameters:
mat -
beginRow -
beginCol -
rows -
cols -
Returns:
the shared matrix

sharedSubMatrixMxNf

public static MatrixMxNf sharedSubMatrixMxNf(MatrixMxNf mat,
                                             int beginRow,
                                             int beginCol,
                                             int rows,
                                             int cols)
Creates a shared Submatrix of mat. Does the same as getSharedSubMatrix(int, int, int, int).

Parameters:
mat -
beginRow -
beginCol -
rows -
cols -
Returns:
the shared matrix