public class Matrix
extends java.lang.Object
Note that this builds on Java arrays, and that it is not particularly efficient at storing sparse matrices.
There is no standard, generic matrix class in Java. Java3D has 3-dimensional matrices, but does not support non-square or higher-order matrices.
Constructor and Description |
---|
Matrix(double[][] values)
Creates a new instance of Matrix from a 2-D array of doubles.
|
Matrix(int rows,
int cols)
Creates a new instance of Matrix of the given dimensions.
|
Modifier and Type | Method and Description |
---|---|
protected boolean |
doDimensionsMatch(Matrix that)
This method compares two
Matrix dimensions and returns
true if the dimensions match, and false if they do not. |
boolean |
equals(java.lang.Object o)
Checks if the Object passed in is equal to this
Matrix . |
double[][] |
getArray()
Returns this Matrix as a 2-D array of doubles.
|
int |
getColumnCount()
Returns the number of columns in this
Matrix . |
double |
getDeterminant()
Returns determinant of this
Matrix by calling
getDeterminant on getLUDecomposition
from LUDecomposition . |
Matrix |
getDuplicate()
Returns a duplicate, seperate object, copy of this
Matrix . |
double |
getElement(int i,
int j)
Returns a single element in this
Matrix . |
static Matrix |
getIdentityMatrix(int rows,
int cols)
Generates an m-by-n identity matrix with ones on the diagonal and zeros
elsewhere.
|
Matrix |
getInverse()
Returns the inverse of this
Matrix if this Matrix
is not singular. |
LUDecomposition |
getLUDecomposition()
Returns an LU Decomposition object of this
Matrix . |
int |
getRowCount()
Returns the number of rows in this
Matrix . |
Matrix |
getSubMatrix(int startRow,
int endRow,
int startCol,
int endCol)
Returns the submatrix formed by selecting the given rows and columns of
this
Matrix . |
Matrix |
getSubMatrixByRows(int[] rows,
int startCol,
int endCol)
Returns the submatrix formed by selecting the given rows and columns of
this
Matrix . |
Matrix |
getTranspose()
Returns the transpose of a matrix.
|
Matrix |
minusMatrix(Matrix that)
Subtracts the given
Matrix from this Matrix . |
Matrix |
plusMatrix(Matrix that)
Adds two Matricies together.
|
void |
setElement(int i,
int j,
double value)
Sets a single element at the given indexed location.
|
protected void |
throwDimensionException(Matrix that)
This method is a helper method that throws an IllegalArgumentException
if the dimensions of the array dimensions do not match.
|
Matrix |
timesMatrix(Matrix that)
This method performs linear algebraic matrix multiplication,
this
Matrix * that Matrix . |
Matrix |
timesScalar(double scalar)
Multiplies this
Matrix by a scalar. |
java.lang.String |
toString()
Returns a string representation of the object.
|
void |
toSystemOut()
Dumps the numerical values of the matrix to System.out in a simple format.
|
public Matrix(int rows, int cols) throws java.lang.IllegalArgumentException
rows
- number of rows.cols
- number of columns.java.lang.IllegalArgumentException
- if the rows or column lengths
are not positive.public Matrix(double[][] values) throws java.lang.IllegalArgumentException
values
- The 2-D array of doubles.java.lang.IllegalArgumentException
- if the values are null or have
no entries.public Matrix getDuplicate()
Matrix
.Matrix
.public double[][] getArray()
public int getRowCount()
Matrix
.Matrix
.public int getColumnCount()
Matrix
.Matrix
.public double getElement(int i, int j)
Matrix
.i
- the row index.j
- the column index.Matrix
.public boolean equals(java.lang.Object o)
Matrix
.
Returns true if the Object is equal and false if it is not.equals
in class java.lang.Object
o
- the object to compare to this Matrix
.Matrix
.public double getDeterminant()
Matrix
by calling
getDeterminant
on getLUDecomposition
from LUDecomposition
.public static Matrix getIdentityMatrix(int rows, int cols)
rows
- the number of rows.cols
- the number of columns.public Matrix getInverse() throws NonInvertibleMatrixException
Matrix
if this Matrix
is not singular.Matrix
.NonInvertibleMatrixException
public LUDecomposition getLUDecomposition()
Matrix
.Matrix
.public Matrix getSubMatrix(int startRow, int endRow, int startCol, int endCol)
Matrix
.startRow
- the starting row index.endRow
- the ending row index.startCol
- the starting column index.endCol
- the ending column index.public Matrix getSubMatrixByRows(int[] rows, int startCol, int endCol)
Matrix
.rows
- an array of row indices.startCol
- the starting column index.endCol
- the ending column index.public void setElement(int i, int j, double value)
i
- the row index where the element is to be placed.j
- the column index where the element is to be placed.value
- the element to be placed at A(i,j).public Matrix getTranspose()
Matrix
.public Matrix plusMatrix(Matrix that)
that
- the Matrix to add to this Matrix
.Matrix
.public Matrix minusMatrix(Matrix that)
Matrix
from this Matrix
.
This method will throw an IllegalArgumentException if the dimensions of
the array dimensions do not match.that
- the Matrix to subtract from this Matrix
.Matrix
.public Matrix timesScalar(double scalar)
Matrix
by a scalar.scalar
- the scalar to multiply.Matrix
.public Matrix timesMatrix(Matrix that)
Matrix
* that Matrix
.
This method will throw an IllegalArgumentException if the dimensions of
the array dimensions do not match.that
- the Matrix
to multiply.Matrix
.protected boolean doDimensionsMatch(Matrix that)
Matrix
dimensions and returns
true if the dimensions match, and false if they do not.that
- the Matrix
to compare dimensions.protected void throwDimensionException(Matrix that)
that
- the Matrix
which dimensions do not match.public java.lang.String toString()
toString
in class java.lang.Object
public void toSystemOut()
Copyright 2004-2006 Ocean Optics, Inc. All Rights Reserved.