Class Matrix

java.lang.Object
   |
   +----Matrix

class Matrix
extends Object
implements Serializable
A class for the matrix operations we'll have to do. This essentially reimplements a subset of Matlab, only this time in Java!. NOTE: When i started this, I wanted to try and provide a set of methods for operations like "Multiply" that would allow the user to pass in a Matrix object m, and have the method use m as the destination for the results of the operation. This would have reduced memory management overhead significantly (i.e., one could reuse a matrix, and thus not have allocate a new matrix each time), but proved to make the code too complex to use. Plus, it screwed up the bounds-checking code. Thus, the methods I use will typically allocate a new Matrix, and write the results of a given operation into that Matrix, leaving the operands untouched. Assmptions: Matrix is relatively dense, two dimensional, and composed of real numbers. (2D array of doubles used to hold matrix entries) So my idea is: write the user manual that describes everything. 1. How to operate the system (can I extract 3d coords of the prots as well?) 2. Each program - what it does and how. 3. Fill the programs with documentations. 4. Make them work.


Variable Index

 o DEBUG
Set DEBUG to true to print out debugging info
 o INDEX_OF_S
 o INDEX_OF_U
If this matrix happens to be a 3 X 3 matrix, this method will compute the SVD of it, using Jacobi method This is based on code that was generously provided by cvl.
 o INDEX_OF_V
 o m_rgrgElt
The matrix Note that we assume the matrix to be rectangular.

Constructor Index

 o Matrix()
Default constructor So we don't cause problems with constructor chaining
 o Matrix(double[][])
Build an array from a multidimensional array Convenient b/c we can now create a 2D array, initialize it,then turn it into a Matrix
 o Matrix(int, int)
So we can create a matrix with given dimensions
 o Matrix(Matrix)
Copy constructor

Method Index

 o Add(Matrix)
A helper version of the above add that will handle memory allocation for the user
 o Add(Matrix, Matrix)
Adds from this matrix the corresponding entry in the matrix add, and writes the resulting matrix into out.
 o AlignZ(Matrix)
Align the cross porduct of U and V with the Z axis (R1), then Transform U V accordingly into U1 and V1, then Align the bisector of U1 and V1 with the x axis (R2)
 o Compare(Matrix)
returns true if all of the elements of the "this" matrix, and the "M" matrix are equal, returns false otherwise
 o Det3()
Computes the determinant of the 3 by 3 matrix, returning a scalar
 o DivideEltsBy(double)
DivideEltsBy Divide every element in the Matrix by divisor, overwriting the current matrix with the result
 o DivideEltsBy(double, Matrix)
DivideEltsBy Divide every element in the Matrix by divisor, writing the result into the matrix named out (which must have been allocated previously by the caller.
 o eye()
eye If the matrix is square, it'll overwrite the contents with an identity matrix
 o FrobeniusNorm()
Returns the Frobenius Norm of the matrix -- square each element, sum the squares, take the square root of the summation, the result is the 2norm
 o FrobeniusNormOffDiagonal()
Returns the Frobenius Norm of every element in the matrix, EXCEPT for those elements on the main diagonal.
 o GetArray()
GetArray returns the n X m matrix as an array of size [n][m].
 o GetElement(int, int)
Returns an individual element.
 o Givens(double, double)
Computes the Givens rotation of a matrix, where returnedArray[0] == c returnedArray[1] == s
 o isColumnVector()
Returns true iff the matrix is actually a column vector
 o isRowVector()
Returns true iff the matrix is actually a row vector
 o isScalar()
isScalar Returns true iff the matrix has only 1 row and 1 column (thus, 1 element)
 o isSymmetric()
isSymmetric returns true iff the matrix is symmetric
 o isVector()
isVector returns true if the matrix is not a scalar, but does have a single dimension
 o MultEltByElt(Matrix)
A helper version of the above MultEltByElt that will handle memory allocation for the user
 o MultEltByElt(Matrix, Matrix)
multiply this matrix and mul matrix entry by entry, and writes the resulting matrix into out.
 o Multiply(double)
Helper function to multiply the matrix by a constant, but will allocate teh out matrix for the user
 o Multiply(double, Matrix)
Multiplies each element of the matrix by a scalar value
 o Multiply(Matrix)
Multiply the "this" matrix by the matrix M, as in retValue = this * M This helper method automatically allocates the new Matrix for you.
 o Multiply(Matrix, Matrix)
If the matrix stored in the this object is A, and the matrix returned from this method is B, then this method computes B = AM
 o NumCols()
This works because the array is rectangular
 o NumRows()
 o Print()
Helper util to pretty-print hte matrix Probably looks bad when matrix greater than width of screen
 o readObject(ObjectInputStream)
 o SetArray(double[][])
 o SetBlock(int, int, double[][])
A basic mass-copy, only this time for a 2D array
 o SetBlock(int, int, Matrix)
SetBlock is a covenience, so we can copy a smaller Matrix over part of the current one
 o SetBlock(int[], int[], Matrix)
SetBlock to take a smaller matrix, rearrange the rows & columns, and copy them over some of the current matrix.
 o SetElement(int, int, double)
Used to set M[i][j] to 'value'.
 o Submatrix(int, int, int, int)
Helper function - auto-allocates a matrix to put the submatrix into
 o Submatrix(int, int, int, int, Matrix)
Submatrix returns the matrix constructed by copying all elements out of the matrix, whose upper left corner is (offRow, offCol), and whose lower right corner is (offRow+cRows-1, offCol+cCol-1) (inclusive for both).
 o Submatrix(int[], int[])
Much like the last version of SetBlock, which allows for rearranging of the matrix, this edition of Submatrix takes two arrays to indicate which columns and rows the user wants.
 o Subtract(Matrix)
A helper version of the above subtract that will handle memory allocation for the user
 o Subtract(Matrix, Matrix)
Subtracts from this matrix the corresponding entry in the matrix sub, and writes the resulting matrix into out.
 o SVDThreeByThree(double)
 o SVDTwoByTwo(double, double, double, double, double[])
based on code graciously provided by cvl.
 o toString()
toString formats a String, suitable for printing, that expresses the Matrix
 o Trace()
Computes the trace of a matrix, where trace = sum of the elements along the main diagonal (ie, summation from n = 1 to i of m_rgrgElt[n][n]) We need a square matrix, and we'll throw MatrixDimensionException if it isn't
 o Transpose()
returns the tranpose of the matrix, A', This helper function automatically allocates a new Matrix to hold the result Note that A = (A')'
 o Transpose(Matrix)
returns the tranpose of the matrix, A' Note that A = (A')'
 o UnitVectorize()
UnitVectorize Assumes that each row will be treated as a vector, and that we want to find the unit vector between row[i] and row[i+1] For for row[0], we find the unit vector between it and the origin.
 o writeObject(ObjectOutputStream)
 o zero()

Variables

 o DEBUG
 public boolean DEBUG
Set DEBUG to true to print out debugging info

 o m_rgrgElt
 protected double m_rgrgElt[][]
The matrix Note that we assume the matrix to be rectangular. [row][col]

 o INDEX_OF_U
 public static final int INDEX_OF_U
If this matrix happens to be a 3 X 3 matrix, this method will compute the SVD of it, using Jacobi method This is based on code that was generously provided by cvl. The inlined Matlab code should provide guidance to the algorithm.

 o INDEX_OF_S
 public static final int INDEX_OF_S
 o INDEX_OF_V
 public static final int INDEX_OF_V

Constructors

 o Matrix
 public Matrix()
Default constructor So we don't cause problems with constructor chaining

 o Matrix
 public Matrix(int cRows,
               int cCols)
So we can create a matrix with given dimensions

 o Matrix
 public Matrix(double MrgrgElt[][])
Build an array from a multidimensional array Convenient b/c we can now create a 2D array, initialize it,then turn it into a Matrix

 o Matrix
 public Matrix(Matrix M) throws MatrixDimensionException
Copy constructor

Methods

 o GetArray
 public final double[][] GetArray()
GetArray returns the n X m matrix as an array of size [n][m]. Kind of an accessor. Nice for doing ops like multiplication, where we don't want the overhead of a function call on every access.

 o SetArray
 public void SetArray(double d[][])
 o NumRows
 public int NumRows()
 o NumCols
 public int NumCols()
This works because the array is rectangular

 o GetElement
 public final double GetElement(int i,
                                int j)
Returns an individual element.

 o zero
 public void zero() throws MatrixDimensionException
 o isSymmetric
 public boolean isSymmetric() throws MatrixDimensionException
isSymmetric returns true iff the matrix is symmetric

 o isScalar
 public boolean isScalar()
isScalar Returns true iff the matrix has only 1 row and 1 column (thus, 1 element)

 o isVector
 public boolean isVector()
isVector returns true if the matrix is not a scalar, but does have a single dimension

 o isColumnVector
 public boolean isColumnVector()
Returns true iff the matrix is actually a column vector

 o isRowVector
 public boolean isRowVector()
Returns true iff the matrix is actually a row vector

 o SetElement
 public void SetElement(int i,
                        int j,
                        double value)
Used to set M[i][j] to 'value'.

 o SetBlock
 public void SetBlock(int offRow,
                      int offCol,
                      Matrix m) throws MatrixDimensionException
SetBlock is a covenience, so we can copy a smaller Matrix over part of the current one

 o SetBlock
 public void SetBlock(int offRow,
                      int offCol,
                      double NewRgrgElt[][]) throws MatrixDimensionException
A basic mass-copy, only this time for a 2D array

 o SetBlock
 public void SetBlock(int rgRow[],
                      int rgCol[],
                      Matrix m) throws MatrixDimensionException
SetBlock to take a smaller matrix, rearrange the rows & columns, and copy them over some of the current matrix. Matlab has similar functionality in its matrixNew = bigMatrix[ 1:3, 2:4] This does the same thing, only this time in Java!

 o Submatrix
 public Matrix Submatrix(int offRow,
                         int offCol,
                         int cRow,
                         int cCol,
                         Matrix AM) throws MatrixDimensionException
Submatrix returns the matrix constructed by copying all elements out of the matrix, whose upper left corner is (offRow, offCol), and whose lower right corner is (offRow+cRows-1, offCol+cCol-1) (inclusive for both).

 o Submatrix
 public Matrix Submatrix(int offRow,
                         int offCol,
                         int cRow,
                         int cCol) throws MatrixDimensionException
Helper function - auto-allocates a matrix to put the submatrix into

 o Submatrix
 public Matrix Submatrix(int rgRow[],
                         int rgCol[]) throws MatrixDimensionException
Much like the last version of SetBlock, which allows for rearranging of the matrix, this edition of Submatrix takes two arrays to indicate which columns and rows the user wants. The zeroth row of the output matrix contains the entries of the row given in rgRow, the first row from the 1st entry in rgRow, etc,etc

 o toString
 public String toString()
toString formats a String, suitable for printing, that expresses the Matrix

Overrides:
toString in class Object
 o Trace
 public double Trace() throws MatrixDimensionException
Computes the trace of a matrix, where trace = sum of the elements along the main diagonal (ie, summation from n = 1 to i of m_rgrgElt[n][n]) We need a square matrix, and we'll throw MatrixDimensionException if it isn't

 o Det3
 public double Det3() throws MatrixDimensionException
Computes the determinant of the 3 by 3 matrix, returning a scalar

 o DivideEltsBy
 public void DivideEltsBy(double divisor) throws MatrixDimensionException
DivideEltsBy Divide every element in the Matrix by divisor, overwriting the current matrix with the result

 o DivideEltsBy
 public void DivideEltsBy(double divisor,
                          Matrix out) throws MatrixDimensionException
DivideEltsBy Divide every element in the Matrix by divisor, writing the result into the matrix named out (which must have been allocated previously by the caller.

 o eye
 public void eye() throws MatrixDimensionException
eye If the matrix is square, it'll overwrite the contents with an identity matrix

 o FrobeniusNorm
 public double FrobeniusNorm() throws MatrixDimensionException
Returns the Frobenius Norm of the matrix -- square each element, sum the squares, take the square root of the summation, the result is the 2norm

 o FrobeniusNormOffDiagonal
 public double FrobeniusNormOffDiagonal() throws MatrixDimensionException
Returns the Frobenius Norm of every element in the matrix, EXCEPT for those elements on the main diagonal. square each element that isn't on the main diagonal, sum the squares, take the square root of the summation, the result is the 2norm

 o Givens
 public static double[] Givens(double a,
                               double b)
Computes the Givens rotation of a matrix, where returnedArray[0] == c returnedArray[1] == s

 o Multiply
 public Matrix Multiply(Matrix M,
                        Matrix out) throws MatrixDimensionException
If the matrix stored in the this object is A, and the matrix returned from this method is B, then this method computes B = AM

Throws: MatrixDimensionException
If the number of columns in this matrix does not equal the number of rows in matrix M
 o Multiply
 public Matrix Multiply(Matrix M) throws MatrixDimensionException
Multiply the "this" matrix by the matrix M, as in retValue = this * M This helper method automatically allocates the new Matrix for you.

 o Multiply
 public Matrix Multiply(double scalar,
                        Matrix out) throws MatrixDimensionException
Multiplies each element of the matrix by a scalar value

 o Multiply
 public Matrix Multiply(double scalar) throws MatrixDimensionException
Helper function to multiply the matrix by a constant, but will allocate teh out matrix for the user

 o Subtract
 public Matrix Subtract(Matrix sub,
                        Matrix out) throws MatrixDimensionException
Subtracts from this matrix the corresponding entry in the matrix sub, and writes the resulting matrix into out. Thus, we get out = this[i][j] - sub[i][j] for all i,j

 o Subtract
 public Matrix Subtract(Matrix sub) throws MatrixDimensionException
A helper version of the above subtract that will handle memory allocation for the user

 o Add
 public Matrix Add(Matrix add,
                   Matrix out) throws MatrixDimensionException
Adds from this matrix the corresponding entry in the matrix add, and writes the resulting matrix into out. Thus, we get out = this[i][j] + add[i][j] for all i,j

 o Add
 public Matrix Add(Matrix add) throws MatrixDimensionException
A helper version of the above add that will handle memory allocation for the user

 o MultEltByElt
 public Matrix MultEltByElt(Matrix mul,
                            Matrix out) throws MatrixDimensionException
multiply this matrix and mul matrix entry by entry, and writes the resulting matrix into out. Thus, we get out = this[i][j] * mul[i][j] for all i,j

 o MultEltByElt
 public Matrix MultEltByElt(Matrix mul) throws MatrixDimensionException
A helper version of the above MultEltByElt that will handle memory allocation for the user

 o SVDThreeByThree
 public Matrix[] SVDThreeByThree(double Tolerance) throws MatrixDimensionException
 o SVDTwoByTwo
 protected static void SVDTwoByTwo(double w,
                                   double x,
                                   double y,
                                   double z,
                                   double SVD2Output[])
based on code graciously provided by cvl. The array returned by this function contains the following values in the following locations: SVD2Output[0] == c1 SVD2Output[1] == s1 SVD2Output[2] == c2 SVD2Output[3] == s2

 o Transpose
 public Matrix Transpose(Matrix out) throws MatrixDimensionException
returns the tranpose of the matrix, A' Note that A = (A')'

 o Transpose
 public Matrix Transpose() throws MatrixDimensionException
returns the tranpose of the matrix, A', This helper function automatically allocates a new Matrix to hold the result Note that A = (A')'

 o UnitVectorize
 public Matrix UnitVectorize()
UnitVectorize Assumes that each row will be treated as a vector, and that we want to find the unit vector between row[i] and row[i+1] For for row[0], we find the unit vector between it and the origin.

 o Compare
 public boolean Compare(Matrix M) throws MatrixDimensionException
returns true if all of the elements of the "this" matrix, and the "M" matrix are equal, returns false otherwise

Throws: MatrixDimensionException
if the number of rows or columns differ between matrices
 o AlignZ
 public Matrix AlignZ(Matrix V) throws MatrixDimensionException
Align the cross porduct of U and V with the Z axis (R1), then Transform U V accordingly into U1 and V1, then Align the bisector of U1 and V1 with the x axis (R2)

 o Print
 public void Print()
Helper util to pretty-print hte matrix Probably looks bad when matrix greater than width of screen

 o writeObject
 private void writeObject(ObjectOutputStream out) throws IOException
 o readObject
 private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException