edu.stanford.rsl.konrad.numerics
Class SimpleMatrix

java.lang.Object
  extended by edu.stanford.rsl.konrad.numerics.SimpleMatrix
All Implemented Interfaces:
java.io.Serializable

public class SimpleMatrix
extends java.lang.Object
implements java.io.Serializable

Author:
Andreas Keil
See Also:
Serialized Form

Nested Class Summary
static class SimpleMatrix.InversionType
          Set the algorithm to be used during inversion
static class SimpleMatrix.MatrixNormType
           
 
Field Summary
static SimpleMatrix I_2
           
static SimpleMatrix I_3
           
static SimpleMatrix I_4
           
 
Constructor Summary
SimpleMatrix()
          Create an empty matrix.
SimpleMatrix(double[][] otherBuffer)
          Creates a new matrix from 2x2 double array
SimpleMatrix(int rows, int cols)
          Create a matrix with row rows and column cols
SimpleMatrix(Jama.Matrix other)
          Creates a new matrix from a jama matrix
SimpleMatrix(SimpleMatrix otherMat)
          Creates a new matrix from another
SimpleMatrix(java.lang.String str)
          Creates a new matrix from string data
 
Method Summary
 void add(double addend)
          Add addend to all entries in matrix in place
 void add(SimpleMatrix... addends)
          Method to add a set of matrices to this matrix in place.
 void addToElement(int row, int col, double addend)
          Add addend to entry at [row,col] in place
 SimpleMatrix clone()
           
 double[][] copyAsDoubleArray()
           
 void copyTo(double[][] otherBuffer)
          Copy matrix entries to supplied 2x2 double array
 double determinant()
           
 void divideBy(double divisor)
          Divide all entries in matrix by divisor in place
 SimpleMatrix dividedBy(double divisor)
          Divide all entries in matrix by divisor [current matrix is not updated]
 void divideElementBy(int row, int col, double divisor)
          Divide divisor from entry at [row,col] in place
 void divideElementWiseBy(SimpleMatrix other)
          ordered division of matrix entries in place
 void fill(double value)
          Sets all matrix entries to the given value.
 SimpleVector getCol(int col)
          Retrieve column from index col of matrix
 int getCols()
           
 SimpleVector getDiag()
           
 double getElement(int row, int col)
          Retrieve matrix entry in the specified row and column
 java.lang.String getMatrixSerialization()
          return a serialized equivalent of this matix
 SimpleVector getRow(int row)
          Retrieve row from index row of matrix
 int getRows()
           
 SimpleVector getSubCol(int firstRow, int col, int sizeRows)
          Returns a vector containing a sub column in current matrix.
 SimpleMatrix getSubMatrix(int[] selectRows, int[] selectCols)
          Creates a new sub matrix with entries from ordered rows and ordered columns provided
 SimpleMatrix getSubMatrix(int deleteRow, int deleteCol)
           
 SimpleMatrix getSubMatrix(int firstRow, int firstCol, int sizeRows, int sizeCols)
          Creates a new sub matrix of this matrix
 SimpleVector getSubRow(int row, int firstCol, int sizeCols)
          Returns a vector containing a sub row in current matrix.
 void identity()
          Sets the matrix to the identity matrix, i.e.
 void init(double[][] otherBuffer)
          Initialize matrix with data from 2x2 double array
 void init(int rows, int cols)
          Initialize zero matrix
 void init(SimpleMatrix otherMat)
          Initialize matrix with data from supplied matrix
 void init(java.lang.String str)
          Initialize matrix with data string of the form []
 SimpleMatrix inverse(SimpleMatrix.InversionType inversionType)
          Inverts the given matrix using the specified inversion method.
 boolean isIdentity(double delta)
          Determines if matrix is an identity matrix
 boolean isOrthogonal(double maxErr)
          Determines if matrix is orthogonal
 boolean isRigidMotion2D(double maxErr)
          Determines if matrix is a rigid motion matrix
 boolean isRigidMotion3D(double maxErr)
          Determines if matrix is a rigid motion matrix
 boolean isRotation2D(double maxErr)
          Determines if matrix is a rotation matrix
 boolean isRotation3D(double maxErr)
          Determines if matrix is a rotation matrix
 boolean isSingular(double delta)
           
 boolean isSpecialOrthogonal(double maxErr)
           
 boolean isSquare()
           
 boolean isUpperTriangular()
          Test for upper triangularity of a matrix.
 SimpleMatrix multipliedBy(double factor)
          Multiply factor to all entries in matrix [current matrix is not updated]
 void multiplyBy(double factor)
          Multiply factor to all entries in matrix in place
 void multiplyElementBy(int row, int col, double factor)
          Multiply factor to entry at [row,col] in place
 void multiplyElementWiseBy(SimpleMatrix other)
          ordered multiplication of matrix entries in place
 void negate()
          multiply all the entries in a matrix by -1 in place.
 SimpleMatrix negated()
           
 double norm(SimpleMatrix.MatrixNormType normType)
           
 void ones()
          Sets all matrix entries to 1.0.
 void randomize(double min, double max)
          Assigns random values to the entries of the matrix.
 void setColValue(int col, SimpleVector newCol)
          Replace col with newCol
 void setDiagValue(SimpleVector diag)
          Replace diagonal entries of matrix with diag
 void setElementValue(int row, int col, double val)
          Replaces matrix entry in the specified row and column with given value
 void setMatrixSerialization(java.lang.String str)
          Initialize matrix using a serialized equivalent
 void setRowValue(int row, SimpleVector newRow)
          Replace row with newRow
 void setSubColValue(int firstRow, int col, SimpleVector subCol)
          Replace the entries of sub column starting at [col,firstRow] with subCol
 void setSubMatrixValue(int firstRow, int firstCol, SimpleMatrix subMatrix)
          Replaces matrix entries starting at firsRow and firstCol with entries from subMatrix
 void setSubRowValue(int row, int firstCol, SimpleVector subRow)
          Replace the entries of sub row starting at [row,firstCol] with subRow
 void subtract(double subtrahend)
          Subtract subtrahend from all entries in matrix in place
 void subtract(SimpleMatrix... subtrahends)
          Method to subtract a set of matrices to this matrix in place.
 void subtractFromElement(int row, int col, double subtrahend)
          Subtract subtrahend from entry at [row,col] in place
 java.lang.String toString()
           
 void transpose()
          Performs a matrix transpose in place.
 SimpleMatrix transposed()
           
 void zeros()
          Sets all matrix entries to 0.0.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

I_2

public static final SimpleMatrix I_2

I_3

public static final SimpleMatrix I_3

I_4

public static final SimpleMatrix I_4
Constructor Detail

SimpleMatrix

public SimpleMatrix()
Create an empty matrix.


SimpleMatrix

public SimpleMatrix(int rows,
                    int cols)
Create a matrix with row rows and column cols

Parameters:
rows - is number of rows in matrix
cols - is number of columns in matrix

SimpleMatrix

public SimpleMatrix(SimpleMatrix otherMat)
Creates a new matrix from another

Parameters:
otherMat - is matrix to be copied

SimpleMatrix

public SimpleMatrix(double[][] otherBuffer)
Creates a new matrix from 2x2 double array

Parameters:
otherBuffer -

SimpleMatrix

public SimpleMatrix(java.lang.String str)
Creates a new matrix from string data

Parameters:
str - is dat string

SimpleMatrix

public SimpleMatrix(Jama.Matrix other)
Creates a new matrix from a jama matrix

Parameters:
other -
Method Detail

init

public void init(int rows,
                 int cols)
Initialize zero matrix

Parameters:
rows - number of rows
cols - number of columns

init

public void init(SimpleMatrix otherMat)
Initialize matrix with data from supplied matrix

Parameters:
otherMat - is source matrix

init

public void init(double[][] otherBuffer)
Initialize matrix with data from 2x2 double array

Parameters:
otherBuffer - is source double array

init

public void init(java.lang.String str)
Initialize matrix with data string of the form []

Parameters:
str - is data string

clone

public SimpleMatrix clone()
Overrides:
clone in class java.lang.Object

copyAsDoubleArray

public double[][] copyAsDoubleArray()
Returns:
2x2 double array containing matrix entries

copyTo

public void copyTo(double[][] otherBuffer)
Copy matrix entries to supplied 2x2 double array

Parameters:
otherBuffer - is 2x2 double array to be populated with matrix entries

getRows

public int getRows()
Returns:
number of rows in matrix

getCols

public int getCols()
Returns:
number of columns in matrix

fill

public void fill(double value)
Sets all matrix entries to the given value.


zeros

public void zeros()
Sets all matrix entries to 0.0.


ones

public void ones()
Sets all matrix entries to 1.0.


randomize

public void randomize(double min,
                      double max)
Assigns random values to the entries of the matrix. Values are uniformly distributed in the given interval [min, max).

Parameters:
min - The lower bound of the interval the values are drawn from.
max - The upper bound of the interval the values are drawn from. Note that value max itself is excluded from the interval and therefore never assigned.

identity

public void identity()
Sets the matrix to the identity matrix, i.e. diagonal entries are set to 1.0, others to 0.0.


getElement

public double getElement(int row,
                         int col)
Retrieve matrix entry in the specified row and column

Parameters:
row - is row containing entry
col - is column containing entry
Returns:
entry in the specified row and column

setElementValue

public void setElementValue(int row,
                            int col,
                            double val)
Replaces matrix entry in the specified row and column with given value

Parameters:
row - is row containing entry to be replaced
col - is column containing entry to be replaced
val - is value to replace matrix entry in the specified row and column

getSubMatrix

public SimpleMatrix getSubMatrix(int firstRow,
                                 int firstCol,
                                 int sizeRows,
                                 int sizeCols)
Creates a new sub matrix of this matrix

Parameters:
firstRow - is the first row of entries to be copied to sub matrix
firstCol - is first column of entries to be copied to sub matrix
sizeRows - is number of rows to be copied starting from first row
sizeCols - is number of columns to be copied starting from first column
Returns:
a sub matrix of this matrix

getSubMatrix

public SimpleMatrix getSubMatrix(int[] selectRows,
                                 int[] selectCols)
Creates a new sub matrix with entries from ordered rows and ordered columns provided

Parameters:
selectRows - is ordered array containing rows to be copied
selectCols - is ordered array containing columns to be copied
Returns:
a sub matrix of this matrix

getSubMatrix

public SimpleMatrix getSubMatrix(int deleteRow,
                                 int deleteCol)

setSubMatrixValue

public void setSubMatrixValue(int firstRow,
                              int firstCol,
                              SimpleMatrix subMatrix)
Replaces matrix entries starting at firsRow and firstCol with entries from subMatrix

Parameters:
firstRow - is row of first element to be replaced
firstCol - is column of first element to be replaced
subMatrix - is sub matrix containing entries to replace matrix entries

getSubRow

public SimpleVector getSubRow(int row,
                              int firstCol,
                              int sizeCols)
Returns a vector containing a sub row in current matrix.

Parameters:
row - is row containing desired sub row
firstCol - is the starting column of desired sub row
sizeCols - is number of columns in sub row
Returns:
vector containing sub row of row, from firstCol to firstCol + sizeCols

setSubRowValue

public void setSubRowValue(int row,
                           int firstCol,
                           SimpleVector subRow)
Replace the entries of sub row starting at [row,firstCol] with subRow

Parameters:
row - is row containing desired sub row
firstCol - is the starting column of desired sub row
subRow - is vector containing new entries

getRow

public SimpleVector getRow(int row)
Retrieve row from index row of matrix

Parameters:
row - is index of row to retrieved
Returns:
row vector

setRowValue

public void setRowValue(int row,
                        SimpleVector newRow)
Replace row with newRow

Parameters:
row - is index of row to be replaced
newRow - is vector containing new entries

getSubCol

public SimpleVector getSubCol(int firstRow,
                              int col,
                              int sizeRows)
Returns a vector containing a sub column in current matrix.

Parameters:
col - is index of column containing desired sub column
firstRow - is the starting column of desired sub column
sizeRows - is number of columns in sub column
Returns:
vector containing sub column of col, from firstCol to firstRow + sizeRows

setSubColValue

public void setSubColValue(int firstRow,
                           int col,
                           SimpleVector subCol)
Replace the entries of sub column starting at [col,firstRow] with subCol

Parameters:
col - is index of column containing desired sub column
firstRow - is the starting row of desired sub column
subCol - is vector containing new entries

getCol

public SimpleVector getCol(int col)
Retrieve column from index col of matrix

Parameters:
col - is index of column to retrieved
Returns:
column vector

setColValue

public void setColValue(int col,
                        SimpleVector newCol)
Replace col with newCol

Parameters:
col - is index of column to be replaced
newCol - is vector containing new entries

getDiag

public SimpleVector getDiag()
Returns:
vector containing diagonal entries of matrix

setDiagValue

public void setDiagValue(SimpleVector diag)
Replace diagonal entries of matrix with diag

Parameters:
diag - is vector containing new entries

addToElement

public void addToElement(int row,
                         int col,
                         double addend)
Add addend to entry at [row,col] in place

Parameters:
row - of entry to be updated
col - of entry to be updated
addend - is value to be added to entry at [row,col]

subtractFromElement

public void subtractFromElement(int row,
                                int col,
                                double subtrahend)
Subtract subtrahend from entry at [row,col] in place

Parameters:
row - of entry to be updated
col - of entry to be updated
subtrahend - is value to be subtracted from entry at [row,col]

multiplyElementBy

public void multiplyElementBy(int row,
                              int col,
                              double factor)
Multiply factor to entry at [row,col] in place

Parameters:
row - of entry to be updated
col - of entry to be updated
factor - is value to be multiplied to entry at [row,col]

divideElementBy

public void divideElementBy(int row,
                            int col,
                            double divisor)
Divide divisor from entry at [row,col] in place

Parameters:
row - of entry to be updated
col - of entry to be updated
divisor - is value to be divided from entry at [row,col]

add

public void add(double addend)
Add addend to all entries in matrix in place

Parameters:
addend - is value to be added to all entries in matrix

subtract

public void subtract(double subtrahend)
Subtract subtrahend from all entries in matrix in place

Parameters:
subtrahend - is value to be subtracted from all entries in matrix

multiplyBy

public void multiplyBy(double factor)
Multiply factor to all entries in matrix in place

Parameters:
factor - is value to be multiplied to all entries in matrix

multipliedBy

public SimpleMatrix multipliedBy(double factor)
Multiply factor to all entries in matrix [current matrix is not updated]

Parameters:
factor - is value to be multiplied to all entries in matrix
Returns:
new matrix with updated entries

divideBy

public void divideBy(double divisor)
Divide all entries in matrix by divisor in place

Parameters:
divisor - is value to be divided from all entries in matrix

dividedBy

public SimpleMatrix dividedBy(double divisor)
Divide all entries in matrix by divisor [current matrix is not updated]

Parameters:
divisor - is value to be divided from all entries in matrix
Returns:
new matrix with updated entries

add

public void add(SimpleMatrix... addends)
Method to add a set of matrices to this matrix in place.

Parameters:
addends - are set of matrices to be added to this matrix.

subtract

public void subtract(SimpleMatrix... subtrahends)
Method to subtract a set of matrices to this matrix in place.

Parameters:
subtrahends - are set of matrices to be from this matrix.

multiplyElementWiseBy

public void multiplyElementWiseBy(SimpleMatrix other)
ordered multiplication of matrix entries in place

Parameters:
other -

divideElementWiseBy

public void divideElementWiseBy(SimpleMatrix other)
ordered division of matrix entries in place

Parameters:
other -

negate

public void negate()
multiply all the entries in a matrix by -1 in place.


negated

public SimpleMatrix negated()
Returns:
a copy of this matrix with all entries multiplied by -1

transpose

public void transpose()
Performs a matrix transpose in place.


transposed

public SimpleMatrix transposed()
Returns:
a transposed copy of this matrix.

norm

public double norm(SimpleMatrix.MatrixNormType normType)

determinant

public double determinant()
Returns:
the determinant of this matrix

isSquare

public boolean isSquare()
Returns:
true if matrix is a square matrix i.e numRows = numCols

isIdentity

public boolean isIdentity(double delta)
Determines if matrix is an identity matrix

Parameters:
delta - is error tolerance
Returns:
true is matrix

isSingular

public boolean isSingular(double delta)

isUpperTriangular

public boolean isUpperTriangular()
Test for upper triangularity of a matrix. This function does not test for squareness. Matrices with zeros below the main diagonal are also considered upper triangular as long as all of the non-square extension is filled with zeros.

Parameters:
M - The input matrix to be checked for upper triangularity.
Returns:
true if M is upper triangular, false otherwise

isOrthogonal

public boolean isOrthogonal(double maxErr)
Determines if matrix is orthogonal

Parameters:
maxErr - is tolerance
Returns:
true if matrix is orthogonal

isSpecialOrthogonal

public boolean isSpecialOrthogonal(double maxErr)

isRotation2D

public boolean isRotation2D(double maxErr)
Determines if matrix is a rotation matrix

Parameters:
maxErr -
Returns:
true if matrix is 2x2 and a rotation matrix, otherwise an illegalArgumentException is thrown

isRotation3D

public boolean isRotation3D(double maxErr)
Determines if matrix is a rotation matrix

Parameters:
maxErr -
Returns:
true if matrix is 3x3 and a rotation matrix, otherwise an illegalArgumentException is thrown

isRigidMotion2D

public boolean isRigidMotion2D(double maxErr)
Determines if matrix is a rigid motion matrix

Parameters:
maxErr -
Returns:
true if matrix is 2x2 and a rotation matrix, otherwise an illegalArgumentException is thrown

isRigidMotion3D

public boolean isRigidMotion3D(double maxErr)
Determines if matrix is a rigid motion matrix

Parameters:
maxErr -
Returns:
true if matrix is 2x2 and a rotation matrix, otherwise an illegalArgumentException is thrown

inverse

public SimpleMatrix inverse(SimpleMatrix.InversionType inversionType)
Inverts the given matrix using the specified inversion method. The type of inversion has to be specified by the user and should be chosen depending on the matrix' properties. Warning: Better use the Solvers class for solving linear algebra problems. Explicitly computing the inverse of a matrix is usually not needed and often yields numerically worse results.

Parameters:
M - The matrix to be inverted.
inversionType - The type of inversion to be used.
Returns:
The inverse of M.

getMatrixSerialization

public java.lang.String getMatrixSerialization()
return a serialized equivalent of this matix


setMatrixSerialization

public void setMatrixSerialization(java.lang.String str)
Initialize matrix using a serialized equivalent

Parameters:
str -

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object