marf.math
Class Matrix

java.lang.Object
  |
  +--marf.math.Matrix
All Implemented Interfaces:
java.lang.Cloneable
Direct Known Subclasses:
Vector

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

Algebraic operations on matrices. Adapted from my graphics project written in C++.

NOTE: this class provides a lot of useful and working functionality, but requires a lot of improvements. In particular in performance, documentation, and styles and consistency in operators. Some missing features will be added/filled in as well. Requires a lot of thorough testing.

$Id: Matrix.java,v 1.20 2005/06/01 01:57:16 mokhov Exp $

Since:
0.3.0
Version:
$Revision: 1.20 $
Author:
Serguei Mokhov

Nested Class Summary
protected  class Matrix.Direction
          Indicates the direction in which a matrix to be extended.
 
Field Summary
protected  double[] adMatrix
          Matrix itself.
static int DEFAULT_COLS
          Default dimension of X of matrix is 4.
static int DEFAULT_ROWS
          Default dimension of Y of matrix is 4.
protected  int iCols
          Actual number of columns in the matrix.
protected  int iRows
          Actual number of rows in the matrix.
 
Constructor Summary
Matrix()
          Default constructor.
Matrix(double[] pad1DMatrix)
          This constructor makes up a vector.
Matrix(double[][] padd2DMatrix)
          Constructs a 2D matrix from a 2D array.
Matrix(double[] pad1DMatrix, boolean pbTransposed)
          This constructor makes up a transposed vector.
Matrix(int piRows, int piCols)
          Constructor with custom dimensions.
Matrix(int piRows, int piCols, double pdFillValue)
          Constructor with custom dimensions and initial fill value.
Matrix(marf.math.Matrix poMatrix)
          A-la copy-constructor.
 
Method Summary
static marf.math.Matrix add(marf.math.Matrix poLHSMatrix, marf.math.Matrix poRHSMatrix)
          M3 = M1 + M2.
 java.lang.Object clone()
          Implements Clonable interface.
 boolean crop(int piLeft, int piTop, int piRight, int piBottom)
           
 boolean cutColumn(int piColNum)
          Shrinks a matrix by removing a specified colum from it.
 boolean cutRow(int piRowNum)
           
 void display()
          Outputs matrix in text format to STDOUT.
static void divide(marf.math.Matrix poMatrix, double pdNum)
          M = M / d.
 boolean equals(marf.math.Matrix poMatrix)
          this == M.
static boolean equals(marf.math.Matrix poLHSMatrix, marf.math.Matrix poRHSMatrix)
          M1 == M2.
 void exhaustMatrix()
          "Destroys" the internal array matrix by setting it to null.
 boolean extend(marf.math.Matrix poExtensionMatrix)
           
 boolean extend(marf.math.Matrix poExtensionMatrix, Matrix.Direction pDirection)
           
 int getCols()
           
 double getDeterminant()
          Calculates determinant of this matrix.
 double getElement(int piRow, int piCol)
           
 int getElements()
          Returns number of the elements of the matrix.
 double[][] getMatrix2D()
          Returns internal matrix representation as 2-dimensional array of doubles.
 double[] getMatrixArray()
           
static java.lang.String getRevision()
          Retrieves class' revision.
 int getRows()
           
 boolean inverse()
          Matrix inversion.
 boolean isIdentity()
          Checks for strict identity matrix.
 boolean isNearlyIdentity()
           
 boolean isNearlyIdentity(double pdDelta)
           
 boolean isReduced()
           
 void loadRow(int piRowNum, marf.math.Vector poVector)
           
 boolean makeIdentity()
          Makes current matrix an identity one.
 marf.math.Matrix minus(marf.math.Matrix poMatrix)
          this - M.
static marf.math.Matrix minus(marf.math.Matrix poLHSMatrix, marf.math.Matrix poRHSMatrix)
          M3 = M1 - M2.
static marf.math.Matrix minusUnary(marf.math.Matrix poMatrix)
          M = -M.
 void multiply(double pdNum)
          this = this * d.
static void multiply(double pdNum, marf.math.Matrix poMatrix)
          M1 = d * M.
 marf.math.Matrix multiply(marf.math.Matrix poMatrix)
          M1 = this * M.
static void multiply(marf.math.Matrix poMatrix, double pdNum)
          M = M * d.
static marf.math.Matrix multiply(marf.math.Matrix poLHSMatrix, marf.math.Matrix poRHSMatrix)
          M3 = M1 * M2.
 boolean rowOperation(double pdA, int piRc)
           
 boolean rowOperation(double piA, int piRc, char pcOp, double piB, int piRp)
           
 boolean rowReduce()
           
 void setAll()
          Default of 0.0.
 void setAll(double pdFillValue)
          Sets all elements of the matrix to the specified value.
 void setCols(int piCols)
           
 void setElement(int piRow, int piCol, double pdValue)
           
 void setMatrix2D(double[][] padd2DMatrix)
          Sets internal array based on 2-dimensional parameter.
 void setMatrixArray(double[] padNewMatrix)
           
 void setRows(int piRows)
           
 int size()
          Analoguous to getElements().
 java.lang.String toString()
          Textual representation of the matrix.
 boolean transpose()
          Matrix transpose.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

DEFAULT_ROWS

public static final int DEFAULT_ROWS
Default dimension of Y of matrix is 4.

See Also:
Constant Field Values

DEFAULT_COLS

public static final int DEFAULT_COLS
Default dimension of X of matrix is 4.

See Also:
Constant Field Values

iRows

protected int iRows
Actual number of rows in the matrix.


iCols

protected int iCols
Actual number of columns in the matrix.


adMatrix

protected double[] adMatrix
Matrix itself.

Constructor Detail

Matrix

public Matrix()
Default constructor. Equivalent to Matrix(Matrix.DEFAULT_ROWS, Matrix.DEFAULT_COLS).


Matrix

public Matrix(int piRows,
              int piCols)
Constructor with custom dimensions. Equivalent to Matrix(piRows, piCols, 0.0).

Parameters:
piRows - custom # of rows.
piCols - custom # of columns.

Matrix

public Matrix(int piRows,
              int piCols,
              double pdFillValue)
Constructor with custom dimensions and initial fill value.

Parameters:
piRows - custom # of rows.
piCols - custom # of columns.
pdFillValue - initial value of all the elements of the matrix.

Matrix

public Matrix(marf.math.Matrix poMatrix)
A-la copy-constructor.

Parameters:
poMatrix - Matrix object to copy data members off from

Matrix

public Matrix(double[] pad1DMatrix)
This constructor makes up a vector. Equivalent to Matrix(pad1DMatrix, false).

Parameters:
pad1DMatrix - vector's elements

Matrix

public Matrix(double[] pad1DMatrix,
              boolean pbTransposed)
This constructor makes up a transposed vector.

Parameters:
pad1DMatrix - vector's elements
pbTransposed - true if transposed, false otherwise

Matrix

public Matrix(double[][] padd2DMatrix)
Constructs a 2D matrix from a 2D array. Internally calls setMatrix2D().

Parameters:
padd2DMatrix - initial array elements
See Also:
setMatrix2D(double[][])
Method Detail

exhaustMatrix

public void exhaustMatrix()
"Destroys" the internal array matrix by setting it to null. Part of the leftover API from the C++ code.


getRows

public final int getRows()
Returns:
number of rows

getCols

public final int getCols()
Returns:
number of columns

setRows

public void setRows(int piRows)

setCols

public void setCols(int piCols)

getMatrixArray

public final double[] getMatrixArray()

setMatrixArray

public void setMatrixArray(double[] padNewMatrix)

setAll

public void setAll()
Default of 0.0.


setAll

public void setAll(double pdFillValue)
Sets all elements of the matrix to the specified value.


getElements

public final int getElements()
Returns number of the elements of the matrix. Part of the original C++ API.


size

public final int size()
Analoguous to getElements().

Since:
0.3.0

getElement

public final double getElement(int piRow,
                               int piCol)
Throws:
java.lang.ArrayIndexOutOfBoundsException

setElement

public void setElement(int piRow,
                       int piCol,
                       double pdValue)
Throws:
java.lang.ArrayIndexOutOfBoundsException

loadRow

public void loadRow(int piRowNum,
                    marf.math.Vector poVector)

getDeterminant

public final double getDeterminant()
Calculates determinant of this matrix.


rowOperation

public boolean rowOperation(double pdA,
                            int piRc)

rowOperation

public boolean rowOperation(double piA,
                            int piRc,
                            char pcOp,
                            double piB,
                            int piRp)

rowReduce

public boolean rowReduce()

isReduced

public boolean isReduced()

extend

public boolean extend(marf.math.Matrix poExtensionMatrix)

extend

public boolean extend(marf.math.Matrix poExtensionMatrix,
                      Matrix.Direction pDirection)

crop

public boolean crop(int piLeft,
                    int piTop,
                    int piRight,
                    int piBottom)

cutRow

public boolean cutRow(int piRowNum)

cutColumn

public boolean cutColumn(int piColNum)
Shrinks a matrix by removing a specified colum from it.

Parameters:
piColNum - a valid index of the column to be removed.
Returns:
true if the column was actually removed; false otherwise

display

public void display()
Outputs matrix in text format to STDOUT.


toString

public java.lang.String toString()
Textual representation of the matrix.

Overrides:
toString in class java.lang.Object
Returns:
String representation

inverse

public boolean inverse()
Matrix inversion.

Returns:
true if matrix was reduced, false otherwise

transpose

public boolean transpose()
Matrix transpose.

Returns:
true if matrix was transposed, false otherwise

makeIdentity

public boolean makeIdentity()
Makes current matrix an identity one.


isIdentity

public final boolean isIdentity()
Checks for strict identity matrix. Any diviations due to errors in floarting point arithmetic will return false.


isNearlyIdentity

public final boolean isNearlyIdentity(double pdDelta)

isNearlyIdentity

public final boolean isNearlyIdentity()

add

public static marf.math.Matrix add(marf.math.Matrix poLHSMatrix,
                                   marf.math.Matrix poRHSMatrix)
M3 = M1 + M2.


minus

public static marf.math.Matrix minus(marf.math.Matrix poLHSMatrix,
                                     marf.math.Matrix poRHSMatrix)
M3 = M1 - M2.


minusUnary

public static marf.math.Matrix minusUnary(marf.math.Matrix poMatrix)
M = -M.


minus

public marf.math.Matrix minus(marf.math.Matrix poMatrix)
this - M.


multiply

public marf.math.Matrix multiply(marf.math.Matrix poMatrix)
M1 = this * M.


multiply

public static marf.math.Matrix multiply(marf.math.Matrix poLHSMatrix,
                                        marf.math.Matrix poRHSMatrix)
M3 = M1 * M2.


multiply

public static void multiply(marf.math.Matrix poMatrix,
                            double pdNum)
M = M * d.


multiply

public void multiply(double pdNum)
this = this * d.


multiply

public static void multiply(double pdNum,
                            marf.math.Matrix poMatrix)
M1 = d * M.


divide

public static void divide(marf.math.Matrix poMatrix,
                          double pdNum)
M = M / d.


equals

public boolean equals(marf.math.Matrix poMatrix)
this == M.


equals

public static boolean equals(marf.math.Matrix poLHSMatrix,
                             marf.math.Matrix poRHSMatrix)
M1 == M2.


getMatrix2D

public double[][] getMatrix2D()
Returns internal matrix representation as 2-dimensional array of doubles.


setMatrix2D

public void setMatrix2D(double[][] padd2DMatrix)
Sets internal array based on 2-dimensional parameter. Works under assumptions that all rows are of the same length.


clone

public java.lang.Object clone()
Implements Clonable interface. Creates a deep copy of the object by cloning the internal matrix array.

Overrides:
clone in class java.lang.Object

getRevision

public static java.lang.String getRevision()
Retrieves class' revision.

Returns:
revision string