|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object marf.math.Matrix
public class Matrix
Provides linear math and storage matrix operations. This class implements a 2-dimensional matrix of doubles; internally implemented as a single-dimensional array with a common linear algebra operations as well as methods to cut/add rows or columns and glue matrices together.
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 and MathTestApp
serves that purpose.
Vector
,
Serialized FormNested Class Summary | |
---|---|
static 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 double |
DEFAULT_ERROR_DELTA
Default precision delta to compute nearly-identity matrices or other error-based calculations. |
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(Matrix poMatrix)
A-la copy-constructor. |
Method Summary | |
---|---|
Matrix |
add(double pdNum)
Adds a scalar to the matrix: this = this + N. |
static Matrix |
add(Matrix poLHSMatrix,
double pdNum)
Adds a scalar to the matrix: M1 = M + N. |
static Matrix |
add(Matrix poLHSMatrix,
Matrix poRHSMatrix)
M3 = M1 + M2. |
protected Matrix |
applyAdd(Matrix poLHSMatrix,
Matrix poRHSMatrix,
int i,
int j)
Actually applies the addition of operation to two elements of a matrix. |
protected Matrix |
applyMinus(Matrix poLHSMatrix,
Matrix poRHSMatrix,
int i,
int j)
|
protected Matrix |
applyMultiply(Matrix poLHSMatrix,
Matrix poRHSMatrix)
|
protected Matrix |
applyMultiply(Matrix poLHSMatrix,
Matrix poRHSMatrix,
int i,
int j)
|
java.lang.Object |
clone()
Implements Clonable interface. |
static Matrix |
crop(double[] padMatrixToCrop,
int piRows,
int piCols,
int piLeft,
int piTop,
int piRight,
int piBottom)
|
boolean |
crop(int piLeft,
int piTop,
int piRight,
int piBottom)
A general routine that allows arbitrary cropping of a matrix. |
boolean |
cutColumn(int piColNum)
Shrinks a matrix by removing a specified column from it. |
boolean |
cutFirstColumn()
|
boolean |
cutFirstRow()
|
boolean |
cutLastColumn()
|
boolean |
cutLastRow()
|
boolean |
cutRow(int piRowNum)
Cuts a specified row away from the matrix. |
void |
display()
Outputs matrix in text format to STDOUT. |
Matrix |
divide(double pdNum)
this = this / d. |
static Matrix |
divide(Matrix poMatrix,
double pdNum)
M = M / d. |
static Matrix |
divide(Matrix poLHSMatrix,
Matrix poRHSMatrix)
|
boolean |
equals(Matrix poMatrix)
this == M. |
static boolean |
equals(Matrix poLHSMatrix,
Matrix poRHSMatrix)
M1 == M2. |
boolean |
equals(java.lang.Object poMatrix)
|
void |
exhaustMatrix()
"Destroys" the internal array matrix by setting it to null . |
static Matrix |
extend(double[] padMatrixToExtend,
int piMatrixToExtendRows,
int piMatrixToExtendCols,
double[] padExtensionMatrix,
int piExtensionMatrixRows,
int piExtensionMatrixCols,
Matrix.Direction poDirection)
Refactors matrix array extension to operate on raw arrays. |
boolean |
extend(Matrix poExtensionMatrix)
Extends this matrix by another (by gluing two matrices together) assuming the extension direction of Direction.EAST . |
boolean |
extend(Matrix poExtensionMatrix,
Matrix.Direction poDirection)
Extends this matrix by another (by gluing two matrices together) given the extension direction. |
int |
getCols()
Retrieves number of the columns in this matrix. |
Vector |
getColumn(int piColNum)
|
double |
getDeterminant()
Calculates determinant of this matrix using Laplace's formula. |
double |
getElement(int piRow,
int piCol)
Retrieves matrix element's value given row and column. |
int |
getElements()
Returns number of the elements of the matrix. |
static java.lang.String |
getMARFSourceCodeRevision()
Retrieves class' revision. |
double[][] |
getMatrix2D()
Returns internal matrix representation as 2-dimensional array of doubles. |
double[] |
getMatrixArray()
Allows getting internal array representing the matrix data. |
protected Matrix |
getNewMatrix()
|
protected Matrix |
getNewMatrix(int piRows,
int piCols)
|
Vector |
getRow(int piRowNum)
|
int |
getRows()
Retrieves number of the rows in this matrix. |
boolean |
inverse()
Matrix inversion. |
boolean |
isIdentity()
Checks for strict identity matrix. |
boolean |
isNearlyIdentity()
Tests nearly-identity matrices with the default delta. |
boolean |
isNearlyIdentity(double pdDelta)
Tests nearly-identity matrices with the specified delta. |
boolean |
isReduced()
Allows examine if this matrix was row-reduced already. |
void |
loadColumn(int piColNum,
Vector poVector)
Allows loading a row of a matrix with values from a vector. |
void |
loadRow(int piRowNum,
Vector poVector)
Allows loading a row of a matrix with values from a vector. |
boolean |
makeIdentity()
Makes current matrix an identity one. |
Matrix |
minus(double pdNum)
Subtracts a scalar from the matrix's each element: this = this - N. |
Matrix |
minus(Matrix poMatrix)
this = this - M. |
static Matrix |
minus(Matrix poLHSMatrix,
double pdNum)
Subtracts a scalar from the matrix's each element: M1 = M - N. |
static Matrix |
minus(Matrix poLHSMatrix,
Matrix poRHSMatrix)
M3 = M1 - M2. |
static Matrix |
minusUnary(Matrix poMatrix)
M1 = -M. |
Matrix |
multiply(double pdNum)
this = this * d. |
static Matrix |
multiply(double pdNum,
Matrix poMatrix)
M1 = d * M. |
Matrix |
multiply(Matrix poMatrix)
M1 = this * M. |
static Matrix |
multiply(Matrix poMatrix,
double pdNum)
M1 = M * d. |
static Matrix |
multiply(Matrix poLHSMatrix,
Matrix poRHSMatrix)
M3 = M1 * M2. |
static Vector |
multiply(Matrix poMatrix,
Vector poVector)
V2 = M * V1. |
Vector |
multiply(Vector poVector)
V1 = this * V. |
Matrix |
pow(int piPow)
|
static Matrix |
pow(Matrix poMatrix,
int piPow)
|
Vector |
rotate(Vector poPointToRotate,
double[] padRotateFactors)
Rotates a vector according to the given axis' factor and angle's degree. |
Vector |
rotate(Vector poPointToRotate,
double pdRotateX,
double pdRotateY,
double pdRotateZ,
double pdRotateAngle)
Rotates a vector according to the given axis' factor and angle's degree. |
Vector |
rotate(Vector poPointToRotate,
Vector poRotateFactors)
Rotates a vector according to the given axis' factor and angle's degree. |
boolean |
rowOperation(double pdA,
int piRc)
Performs a '+' linear row operation given the A and Rc parameters. |
boolean |
rowOperation(double pdA,
int piRc,
char pcOp,
double pdB,
int piRp)
Performs a specified linear row operation given the parameters. |
boolean |
rowReduce()
Applies row-reduce operation in this matrix. |
Vector |
scale(Vector poPointToScale,
double[] padScaleFactors)
Returns a scaled vector. |
Vector |
scale(Vector poPointToScale,
double pdScaleX,
double pdScaleY,
double pdScaleZ)
Returns a scaled vector. |
Vector |
scale(Vector poPointToScale,
Vector poScaleFactors)
Returns a scaled vector. |
void |
setAll()
Sets all elements of the matrix to a default of 0.0. |
void |
setAll(double pdFillValue)
Sets all elements of the matrix to the specified value. |
void |
setAllRandom()
Sets all elements of the matrix at pseudo-random order. |
void |
setCols(int piCols)
Allows setting number of columns of the matrix. |
void |
setElement(int piRow,
int piCol,
double pdValue)
Sets matrix element's value at specified position. |
void |
setMatrix2D(double[][] padd2DMatrix)
Sets internal array based on 2-dimensional parameter. |
void |
setMatrixArray(double[] padNewMatrix)
Allows setting the new internal array of matrix data from the parameter. |
void |
setRows(int piRows)
Allows setting number of rows of the matrix. |
Vector |
shear(Vector poPointToShear,
double[] padShearFactors)
Returns a sheared vector. |
Vector |
shear(Vector poPointToShear,
double pdShearX,
double pdShearY,
double pdShearZ)
Returns a sheared vector. |
Vector |
shear(Vector poPointToShear,
Vector poShearFactors)
Returns a sheared vector. |
int |
size()
Analogous to getElements() . |
java.lang.String |
toString()
Textual representation of the matrix. |
Vector |
translate(Vector poPointToTranslate,
double[] padTranslateVals)
Translates a vector according to the given size. |
Vector |
translate(Vector poPointToTranslate,
double pdTranslateX,
double pdTranslateY,
double pdTranslateZ)
Translates a vector according to the given size. |
Vector |
translate(Vector poPointToTranslate,
Vector poTranslateVals)
Translates a vector according to the given size. |
boolean |
transpose()
Matrix transpose. |
Methods inherited from class java.lang.Object |
---|
finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
public static final int DEFAULT_ROWS
public static final int DEFAULT_COLS
public static final double DEFAULT_ERROR_DELTA
protected int iRows
protected int iCols
protected double[] adMatrix
Constructor Detail |
---|
public Matrix()
Matrix(Matrix.DEFAULT_ROWS, Matrix.DEFAULT_COLS)
.
Matrix(int, int)
public Matrix(int piRows, int piCols)
Matrix(piRows, piCols, 0.0)
.
piRows
- custom # of rows.piCols
- custom # of columns.Matrix(int, int, double)
public Matrix(int piRows, int piCols, double pdFillValue)
piRows
- custom # of rows.piCols
- custom # of columns.pdFillValue
- initial value of all the elements of the matrix.public Matrix(Matrix poMatrix)
poMatrix
- Matrix object to copy data members off frompublic Matrix(double[] pad1DMatrix)
Matrix(pad1DMatrix, false)
.
pad1DMatrix
- vector's elementsMatrix(double[], boolean)
public Matrix(double[] pad1DMatrix, boolean pbTransposed)
pad1DMatrix
- vector's elementspbTransposed
- true if transposed, false otherwisepublic Matrix(double[][] padd2DMatrix)
padd2DMatrix
- initial array elementssetMatrix2D(double[][])
Method Detail |
---|
public void exhaustMatrix()
null
.
Also, the garbage collector is hinted to collect as soon as it can with
System.gc()
. If called very often, may actually slowdown
the system during the garbage collection runs.
The cardinality of the matrix is not affected and a new instance of it
is not created; use setMatrixArray()
to set the new matrix
data.
setMatrixArray(double[])
,
setMatrix2D(double[][])
,
System.gc()
public final int getRows()
public final int getCols()
public void setRows(int piRows)
piRows
- new number of rowspublic void setCols(int piCols)
piCols
- new number of cloumnspublic final double[] getMatrixArray()
public void setMatrixArray(double[] padNewMatrix)
padNewMatrix
- new matrix data
java.lang.NullPointerException
- if the parameter is null
public void setAll()
public void setAll(double pdFillValue)
pdFillValue
- the value to fill the matrix with.public void setAllRandom()
Double.MAX_VALUE
public final int getElements()
public final int size()
getElements()
.
getElements()
public final double getElement(int piRow, int piCol)
piRow
- row index of the elementpiCol
- column index of the element
java.lang.ArrayIndexOutOfBoundsException
- if at least one of the indices is out of rangepublic void setElement(int piRow, int piCol, double pdValue)
piRow
- row index of the element to setpiCol
- column index of the element to setpdValue
- the value of the element
java.lang.ArrayIndexOutOfBoundsException
- if at least one of the indices is out of rangepublic void loadRow(int piRowNum, Vector poVector)
piRowNum
- index of the row to loadpoVector
- the values to take
java.lang.ArrayIndexOutOfBoundsException
- if the row number is out
of range or there are insufficient number of elements in the vectorpublic Vector getRow(int piRowNum)
piRowNum
-
public void loadColumn(int piColNum, Vector poVector)
piColNum
- index of the column to loadpoVector
- the values to take
java.lang.ArrayIndexOutOfBoundsException
- if the column number is out
of range or there are insufficient number of elements in the vectorpublic Vector getColumn(int piColNum)
piColNum
-
public final double getDeterminant()
public boolean rowOperation(double pdA, int piRc)
pdA
- ApiRc
- Rc
true
if the operation was successfulpublic boolean rowOperation(double pdA, int piRc, char pcOp, double pdB, int piRp)
pdA
- ApiRc
- RcpcOp
- operation ('+' or '-')pdB
- BpiRp
- Rb
true
if the operation was successfulpublic boolean rowReduce()
true
if the operation was successfulpublic boolean isReduced()
true
if the matrix if row-reducedrowReduce()
public boolean extend(Matrix poExtensionMatrix)
Direction.EAST
. NOTICE, if doubling
the same this
matrix, make a copy for the argument instead.
poExtensionMatrix
- the matrix to extend the current by
true
if the operation was successfulMatrix.Direction.EAST
public boolean extend(Matrix poExtensionMatrix, Matrix.Direction poDirection)
poExtensionMatrix
- the matrix to extend the current bypoDirection
- in which direction the extension should happen
true
if the operation was successfulMatrix.Direction
public static Matrix extend(double[] padMatrixToExtend, int piMatrixToExtendRows, int piMatrixToExtendCols, double[] padExtensionMatrix, int piExtensionMatrixRows, int piExtensionMatrixCols, Matrix.Direction poDirection)
padMatrixToExtend
- piMatrixToExtendRows
- piMatrixToExtendCols
- padExtensionMatrix
- piExtensionMatrixRows
- piExtensionMatrixCols
- poDirection
-
public boolean crop(int piLeft, int piTop, int piRight, int piBottom)
piLeft
- left index border to crop afterpiTop
- top index border to crop afterpiRight
- right index border to crop afterpiBottom
- bottom index border to crop after
true
if the cropping was successfulpublic static Matrix crop(double[] padMatrixToCrop, int piRows, int piCols, int piLeft, int piTop, int piRight, int piBottom)
padMatrixToCrop
- piRows
- piCols
- piLeft
- piTop
- piRight
- piBottom
-
public boolean cutRow(int piRowNum)
piRowNum
- row to remove
true
if cutting was successfulpublic boolean cutLastRow()
public boolean cutFirstRow()
public boolean cutColumn(int piColNum)
piColNum
- a valid index of the column to be removed.
true
if the column was actually removed; false
otherwisepublic boolean cutLastColumn()
public boolean cutFirstColumn()
public void display()
public java.lang.String toString()
toString
in class java.lang.Object
protected Matrix getNewMatrix()
protected Matrix getNewMatrix(int piRows, int piCols)
piRows
- piCols
-
public boolean inverse()
true
if matrix was possible to invert, false
otherwisepublic boolean transpose()
true
if matrix was transposed, false
otherwisepublic boolean makeIdentity()
true
if the operation completed successfullypublic final boolean isIdentity()
false
. If imprecise values are allowed,
consider using isNearlyIdentity()
.
true
if the matrix is strictly identityisNearlyIdentity()
,
isNearlyIdentity(double)
public final boolean isNearlyIdentity(double pdDelta)
pdDelta
- the FP error to assume when checking for identity
true
if the matrix is nearly identitypublic final boolean isNearlyIdentity()
true
if the matrix is nearly identityDEFAULT_ERROR_DELTA
public static Matrix add(Matrix poLHSMatrix, Matrix poRHSMatrix)
poLHSMatrix
- M1poRHSMatrix
- M2
protected Matrix applyAdd(Matrix poLHSMatrix, Matrix poRHSMatrix, int i, int j)
poLHSMatrix
- poRHSMatrix
- i
- j
- public Matrix add(double pdNum)
pdNum
- the scalar N
public static Matrix add(Matrix poLHSMatrix, double pdNum)
poLHSMatrix
- MpdNum
- the scalar N
public static Matrix minus(Matrix poLHSMatrix, Matrix poRHSMatrix)
poLHSMatrix
- M1poRHSMatrix
- M2
protected Matrix applyMinus(Matrix poLHSMatrix, Matrix poRHSMatrix, int i, int j)
poLHSMatrix
- poRHSMatrix
- i
- j
- public static Matrix minusUnary(Matrix poMatrix)
poMatrix
- M
public Matrix minus(Matrix poMatrix)
poMatrix
- M
public Matrix minus(double pdNum)
pdNum
- the scalarpublic static Matrix minus(Matrix poLHSMatrix, double pdNum)
pdNum
- the scalarpublic Matrix multiply(Matrix poMatrix)
poMatrix
- M
public static Matrix multiply(Matrix poLHSMatrix, Matrix poRHSMatrix)
poLHSMatrix
- M1poRHSMatrix
- M2
protected Matrix applyMultiply(Matrix poLHSMatrix, Matrix poRHSMatrix)
protected Matrix applyMultiply(Matrix poLHSMatrix, Matrix poRHSMatrix, int i, int j)
poLHSMatrix
- poRHSMatrix
- i
- j
- public static Vector multiply(Matrix poMatrix, Vector poVector)
poMatrix
- MpoVector
- V1
public Vector multiply(Vector poVector)
poVector
- V
public static Matrix multiply(Matrix poMatrix, double pdNum)
poMatrix
- MpdNum
- d
public Matrix multiply(double pdNum)
pdNum
- d
public static Matrix multiply(double pdNum, Matrix poMatrix)
pdNum
- dpoMatrix
- M
public static Matrix divide(Matrix poMatrix, double pdNum)
poMatrix
- MpdNum
- d
public static Matrix divide(Matrix poLHSMatrix, Matrix poRHSMatrix)
poLHSMatrix
- poRHSMatrix
-
public Matrix divide(double pdNum)
pdNum
- dpublic Matrix pow(int piPow)
piPow
-
public static Matrix pow(Matrix poMatrix, int piPow)
poMatrix
- piPow
-
public boolean equals(Matrix poMatrix)
poMatrix
- M
true
if this and parameter matrices are equalpublic static boolean equals(Matrix poLHSMatrix, Matrix poRHSMatrix)
poLHSMatrix
- M1poRHSMatrix
- M2
true
of the matrices are equalpublic boolean equals(java.lang.Object poMatrix)
equals
in class java.lang.Object
Object.equals(java.lang.Object)
public double[][] getMatrix2D()
public void setMatrix2D(double[][] padd2DMatrix)
padd2DMatrix
- the two dimensional array of doubles to convert frompublic java.lang.Object clone()
clone
in class java.lang.Object
Object.clone()
public Vector scale(Vector poPointToScale, Vector poScaleFactors)
poPointToScale
- the vector of objects to be scaled.poScaleFactors
- the vector of objects containing the vector's scaling factors
java.lang.IllegalArgumentException
- if the sizes of the two vectors are differentpublic Vector scale(Vector poPointToScale, double[] padScaleFactors)
poPointToScale
- the vector of objects to be scaled.padScaleFactors
- the array of doubles containing the vector's scaling factors
java.lang.IllegalArgumentException
- if the sizes of the array and the vector are differentpublic Vector scale(Vector poPointToScale, double pdScaleX, double pdScaleY, double pdScaleZ)
poPointToScale
- the vector of objects to be scaled.pdScaleX
- the X axis scaled factor of doublepdScaleY
- the Y axis scaled factor of doublepdScaleZ
- the Z axis scaled factor of double
public Vector translate(Vector poPointToTranslate, Vector poTranslateVals)
poPointToTranslate
- the vector of objects to be translated.poTranslateVals
- the vector of objects that contains the vector's translating values
java.lang.IllegalArgumentException
- if the sizes of the array and the vector are differentpublic Vector translate(Vector poPointToTranslate, double[] padTranslateVals)
poPointToTranslate
- the vector of objects to be translated.padTranslateVals
- the Array of doubles that contains the vector's translating values
java.lang.IllegalArgumentException
- if the sizes of the array and the vector are differentpublic Vector translate(Vector poPointToTranslate, double pdTranslateX, double pdTranslateY, double pdTranslateZ)
poPointToTranslate
- the vector of objects to be translated.pdTranslateX
- the X axis translated factor of doublepdTranslateY
- the Y axis translated factor of doublepdTranslateZ
- the Z axis translated factor of double
java.lang.IllegalArgumentException
- if the sizes of the vector is not one bigger than the number of scaled factorspublic Vector rotate(Vector poPointToRotate, Vector poRotateFactors)
poPointToRotate
- the vector of objects to be rotatedpoRotateFactors
- the vector of objects that contains the vector's rotating factors
java.lang.IllegalArgumentException
- if the sizes of two vectors are differentpublic Vector rotate(Vector poPointToRotate, double[] padRotateFactors)
poPointToRotate
- the vector of objects to be rotated.padRotateFactors
- the Array of doubles that contains the vector's rotating values
java.lang.IllegalArgumentException
- if the sizes of the array and the vector are differentpublic Vector rotate(Vector poPointToRotate, double pdRotateX, double pdRotateY, double pdRotateZ, double pdRotateAngle)
poPointToRotate
- the vector of objects to be rotated.pdRotateX
- the X axis rotated factor of doublepdRotateY
- the Y axis rotated factor of doublepdRotateZ
- the Z axis rotated factor of doublepdRotateAngle
- the rotate angle of degree around one axis
java.lang.IllegalArgumentException
- if the sizes of the vector is not one bigger than the number of rotated factorspublic Vector shear(Vector poPointToShear, Vector poShearFactors)
poPointToShear
- the vector of objects to be sheared.poShearFactors
- the vector of objects containing the vector's shearing factors
java.lang.IllegalArgumentException
- if the sizes of the two vectors are differentpublic Vector shear(Vector poPointToShear, double[] padShearFactors)
poPointToShear
- the vector of objects to be shearedpadShearFactors
- the array of doubles containing the vector's shearing factors
java.lang.IllegalArgumentException
- if the sizes of the array and the vector are differentpublic Vector shear(Vector poPointToShear, double pdShearX, double pdShearY, double pdShearZ)
poPointToShear
- the vector of objects to be shearedpdShearX
- the X axis sheared factor of doublepdShearY
- the Y axis sheared factor of doublepdShearZ
- the Z axis sheared factor of double
public static java.lang.String getMARFSourceCodeRevision()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |