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.
-
DEBUG
- Set DEBUG to true to print out debugging info
-
INDEX_OF_S
-
-
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.
-
INDEX_OF_V
-
-
m_rgrgElt
- The matrix
Note that we assume the matrix to be rectangular.
-
Matrix()
- Default constructor
So we don't cause problems with constructor chaining
-
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
-
Matrix(int, int)
- So we can create a matrix with given dimensions
-
Matrix(Matrix)
- Copy constructor
-
Add(Matrix)
- A helper version of the above add that will handle memory allocation for
the user
-
Add(Matrix, Matrix)
- Adds from this matrix the corresponding entry in the matrix
add, and writes the resulting matrix into out.
-
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)
-
Compare(Matrix)
- returns true if all of the elements of the "this" matrix, and the
"M" matrix are equal, returns false otherwise
-
Det3()
- Computes the determinant of the 3 by 3 matrix, returning a scalar
-
DivideEltsBy(double)
- DivideEltsBy
Divide every element in the Matrix by divisor, overwriting the current
matrix with the result
-
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.
-
eye()
-
eye
If the matrix is square, it'll overwrite the contents with an identity
matrix
-
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
-
FrobeniusNormOffDiagonal()
- Returns the Frobenius Norm of every element in the matrix, EXCEPT for
those elements on the main diagonal.
-
GetArray()
- GetArray returns the n X m matrix as an array of size [n][m].
-
GetElement(int, int)
- Returns an individual element.
-
Givens(double, double)
- Computes the Givens rotation of a matrix, where
returnedArray[0] == c
returnedArray[1] == s
-
isColumnVector()
- Returns true iff the matrix is actually a column vector
-
isRowVector()
- Returns true iff the matrix is actually a row vector
-
isScalar()
- isScalar
Returns true iff the matrix has only 1 row and 1 column (thus, 1 element)
-
isSymmetric()
- isSymmetric returns true iff the matrix is symmetric
-
isVector()
- isVector returns true if the matrix is not a scalar, but
does have a single dimension
-
MultEltByElt(Matrix)
- A helper version of the above MultEltByElt that will handle memory allocation for
the user
-
MultEltByElt(Matrix, Matrix)
- multiply this matrix and mul matrix entry by entry,
and writes the resulting matrix into out.
-
Multiply(double)
- Helper function to multiply the matrix by a constant, but will
allocate teh out matrix for the user
-
Multiply(double, Matrix)
- Multiplies each element of the matrix by a scalar value
-
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.
-
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
-
NumCols()
- This works because the array is rectangular
-
NumRows()
-
-
Print()
-
Helper util to pretty-print hte matrix
Probably looks bad when matrix greater than width of screen
-
readObject(ObjectInputStream)
-
-
SetArray(double[][])
-
-
SetBlock(int, int, double[][])
- A basic mass-copy, only this time for a 2D array
-
SetBlock(int, int, Matrix)
- SetBlock is a covenience, so we can copy a smaller Matrix over part
of the current one
-
SetBlock(int[], int[], Matrix)
- SetBlock to take a smaller matrix, rearrange the rows & columns, and copy them
over some of the current matrix.
-
SetElement(int, int, double)
- Used to set M[i][j] to 'value'.
-
Submatrix(int, int, int, int)
- Helper function - auto-allocates a matrix to put the submatrix into
-
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).
-
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.
-
Subtract(Matrix)
- A helper version of the above subtract that will handle memory allocation for
the user
-
Subtract(Matrix, Matrix)
- Subtracts from this matrix the corresponding entry in the matrix
sub, and writes the resulting matrix into out.
-
SVDThreeByThree(double)
-
-
SVDTwoByTwo(double, double, double, double, double[])
-
based on code graciously provided by cvl.
-
toString()
- toString formats a String, suitable for printing, that expresses
the Matrix
-
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
-
Transpose()
- returns the tranpose of the matrix, A', This helper function automatically
allocates a new Matrix to hold the result
Note that A = (A')'
-
Transpose(Matrix)
- returns the tranpose of the matrix, A'
Note that A = (A')'
-
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.
-
writeObject(ObjectOutputStream)
-
-
zero()
-
DEBUG
public boolean DEBUG
- Set DEBUG to true to print out debugging info
m_rgrgElt
protected double m_rgrgElt[][]
- The matrix
Note that we assume the matrix to be rectangular.
[row][col]
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.
INDEX_OF_S
public static final int INDEX_OF_S
INDEX_OF_V
public static final int INDEX_OF_V
Matrix
public Matrix()
- Default constructor
So we don't cause problems with constructor chaining
Matrix
public Matrix(int cRows,
int cCols)
- So we can create a matrix with given dimensions
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
Matrix
public Matrix(Matrix M) throws MatrixDimensionException
- Copy constructor
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.
SetArray
public void SetArray(double d[][])
NumRows
public int NumRows()
NumCols
public int NumCols()
- This works because the array is rectangular
GetElement
public final double GetElement(int i,
int j)
- Returns an individual element.
zero
public void zero() throws MatrixDimensionException
isSymmetric
public boolean isSymmetric() throws MatrixDimensionException
- isSymmetric returns true iff the matrix is symmetric
isScalar
public boolean isScalar()
- isScalar
Returns true iff the matrix has only 1 row and 1 column (thus, 1 element)
isVector
public boolean isVector()
- isVector returns true if the matrix is not a scalar, but
does have a single dimension
isColumnVector
public boolean isColumnVector()
- Returns true iff the matrix is actually a column vector
isRowVector
public boolean isRowVector()
- Returns true iff the matrix is actually a row vector
SetElement
public void SetElement(int i,
int j,
double value)
- Used to set M[i][j] to 'value'.
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
SetBlock
public void SetBlock(int offRow,
int offCol,
double NewRgrgElt[][]) throws MatrixDimensionException
- A basic mass-copy, only this time for a 2D array
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!
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).
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
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
toString
public String toString()
- toString formats a String, suitable for printing, that expresses
the Matrix
- Overrides:
- toString in class Object
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
Det3
public double Det3() throws MatrixDimensionException
- Computes the determinant of the 3 by 3 matrix, returning a scalar
DivideEltsBy
public void DivideEltsBy(double divisor) throws MatrixDimensionException
- DivideEltsBy
Divide every element in the Matrix by divisor, overwriting the current
matrix with the result
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.
eye
public void eye() throws MatrixDimensionException
- eye
If the matrix is square, it'll overwrite the contents with an identity
matrix
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
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
Givens
public static double[] Givens(double a,
double b)
- Computes the Givens rotation of a matrix, where
returnedArray[0] == c
returnedArray[1] == s
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
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.
Multiply
public Matrix Multiply(double scalar,
Matrix out) throws MatrixDimensionException
- Multiplies each element of the matrix by a scalar value
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
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
Subtract
public Matrix Subtract(Matrix sub) throws MatrixDimensionException
- A helper version of the above subtract that will handle memory allocation for
the user
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
Add
public Matrix Add(Matrix add) throws MatrixDimensionException
- A helper version of the above add that will handle memory allocation for
the user
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
MultEltByElt
public Matrix MultEltByElt(Matrix mul) throws MatrixDimensionException
- A helper version of the above MultEltByElt that will handle memory allocation for
the user
SVDThreeByThree
public Matrix[] SVDThreeByThree(double Tolerance) throws MatrixDimensionException
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
Transpose
public Matrix Transpose(Matrix out) throws MatrixDimensionException
- returns the tranpose of the matrix, A'
Note that A = (A')'
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')'
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.
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
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)
Print
public void Print()
- Helper util to pretty-print hte matrix
Probably looks bad when matrix greater than width of screen
writeObject
private void writeObject(ObjectOutputStream out) throws IOException
readObject
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException