Class Matrix
- java.lang.Object
-
- org.hortonmachine.gears.utils.math.matrixes.Matrix
-
- Direct Known Subclasses:
ColumnVector,RowVector,SquareMatrix
public class Matrix extends Object
From: Java Number Cruncher The Java Programmer's Guide to Numerical Computation by Ronald Mak The matrix class.
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Matrixadd(Matrix m)Add another matrix to this matrix.doubleat(int r, int c)Get the value of element [r,c] in the matrix.intcolumnCount()Get the column count.double[][]copyValues2D()Copy the values of this matrix.ColumnVectorgetColumn(int c)Get a column of this matrix.RowVectorgetRow(int r)Get a row of this matrix.Matrixmultiply(double k)Multiply this matrix by a constant.ColumnVectormultiply(ColumnVector cv)Multiply this matrix by a column vector: this*cvMatrixmultiply(Matrix m)Multiply this matrix by another matrix.RowVectormultiply(RowVector rv)Multiply a row vector by this matrix: rv*thisintrowCount()Get the row count.protected voidset(double[][] values)Set this matrix from a 2-d array of values.voidset(int r, int c, double value)Set the value of element [r,c].voidsetColumn(ColumnVector cv, int c)Set a column of this matrix from a column vector.voidsetRow(RowVector rv, int r)Set a row of this matrix from a row vector.Matrixsubtract(Matrix m)Subtract another matrix from this matrix.Matrixtranspose()Return the transpose of this matrix.double[][]values()Copy the values of this matrix.
-
-
-
Constructor Detail
-
Matrix
protected Matrix()
Default constructor.
-
Matrix
public Matrix(int rowCount, int colCount)Constructor.- Parameters:
rowCount- the number of rowscolCount- the number of columns
-
Matrix
public Matrix(double[][] values)
Constructor.- Parameters:
values- the 2-d array of values
-
-
Method Detail
-
rowCount
public int rowCount()
Get the row count.- Returns:
- the row count
-
columnCount
public int columnCount()
Get the column count.- Returns:
- the column count
-
at
public double at(int r, int c) throws MatrixExceptionGet the value of element [r,c] in the matrix.- Parameters:
r- the row indexc- the column index- Returns:
- the value
- Throws:
numbercruncher.MatrixException- for an invalid indexMatrixException
-
getRow
public RowVector getRow(int r) throws MatrixException
Get a row of this matrix.- Parameters:
r- the row index- Returns:
- the row as a row vector
- Throws:
numbercruncher.MatrixException- for an invalid indexMatrixException
-
getColumn
public ColumnVector getColumn(int c) throws MatrixException
Get a column of this matrix.- Parameters:
c- the column index- Returns:
- the column as a column vector
- Throws:
numbercruncher.MatrixException- for an invalid indexMatrixException
-
values
public double[][] values()
Copy the values of this matrix.- Returns:
- the values
-
copyValues2D
public double[][] copyValues2D()
Copy the values of this matrix.- Returns:
- the copied values
-
set
public void set(int r, int c, double value) throws MatrixExceptionSet the value of element [r,c].- Parameters:
r- the row indexc- the column indexvalue- the value- Throws:
numbercruncher.MatrixException- for an invalid indexMatrixException
-
set
protected void set(double[][] values)
Set this matrix from a 2-d array of values. If the rows do not have the same length, then the matrix column count is the length of the shortest row.- Parameters:
values- the 2-d array of values
-
setRow
public void setRow(RowVector rv, int r) throws MatrixException
Set a row of this matrix from a row vector.- Parameters:
rv- the row vectorr- the row index- Throws:
numbercruncher.MatrixException- for an invalid index or an invalid vector sizeMatrixException
-
setColumn
public void setColumn(ColumnVector cv, int c) throws MatrixException
Set a column of this matrix from a column vector.- Parameters:
cv- the column vectorc- the column index- Throws:
numbercruncher.MatrixException- for an invalid index or an invalid vector sizeMatrixException
-
transpose
public Matrix transpose()
Return the transpose of this matrix.- Returns:
- the transposed matrix
-
add
public Matrix add(Matrix m) throws MatrixException
Add another matrix to this matrix.- Parameters:
m- the matrix addend- Returns:
- the sum matrix
- Throws:
numbercruncher.MatrixException- for invalid sizeMatrixException
-
subtract
public Matrix subtract(Matrix m) throws MatrixException
Subtract another matrix from this matrix.- Parameters:
m- the matrix subrrahend- Returns:
- the difference matrix
- Throws:
numbercruncher.MatrixException- for invalid sizeMatrixException
-
multiply
public Matrix multiply(double k)
Multiply this matrix by a constant.- Parameters:
k- the constant- Returns:
- the product matrix
-
multiply
public Matrix multiply(Matrix m) throws MatrixException
Multiply this matrix by another matrix.- Parameters:
m- the matrix multiplier- Returns:
- the product matrix
- Throws:
numbercruncher.MatrixException- for invalid sizeMatrixException
-
multiply
public ColumnVector multiply(ColumnVector cv) throws MatrixException
Multiply this matrix by a column vector: this*cv- Parameters:
cv- the column vector- Returns:
- the product column vector
- Throws:
numbercruncher.MatrixException- for invalid sizeMatrixException
-
multiply
public RowVector multiply(RowVector rv) throws MatrixException
Multiply a row vector by this matrix: rv*this- Parameters:
rv- the row vector- Returns:
- the product row vector
- Throws:
numbercruncher.MatrixException- for invalid sizeMatrixException
-
-