public class SimpleMatrix extends SimpleBase<SimpleMatrix>
SimpleMatrix
is a wrapper around DMatrixRMaj
that provides an
easy to use object oriented interface for performing matrix operations. It is designed to be
more accessible to novice programmers and provide a way to rapidly code up solutions by simplifying
memory management and providing easy to use functions.
Most functions in SimpleMatrix do not modify the original matrix. Instead they
create a new SimpleMatrix instance which is modified and returned. This greatly simplifies memory
management and writing of code in general. It also allows operations to be chained, as is shown
below:
SimpleMatrix K = P.mult(H.transpose().mult(S.invert()));
Working with both DMatrixRMaj
and SimpleMatrix in the same code base is easy.
To access the internal DMatrixRMaj in a SimpleMatrix simply call SimpleBase.getMatrix()
.
To turn a DMatrixRMaj into a SimpleMatrix use wrap(org.ejml.data.Matrix)
. Not
all operations in EJML are provided for SimpleMatrix, but can be accessed by extracting the internal
DMatrixRMaj.
EXTENDING: SimpleMatrix contains a list of narrowly focused functions for linear algebra. To harness
the functionality for another application and to the number of functions it supports it is recommended
that one extends SimpleBase
instead. This way the returned matrix type's of SimpleMatrix functions
will be of the appropriate types. See StatisticsMatrix inside of the examples directory.
PERFORMANCE: The disadvantage of using this class is that it is more resource intensive, since it creates a new matrix each time an operation is performed. This makes the JavaVM work harder and Java automatically initializes the matrix to be all zeros. Typically operations on small matrices or operations that have a runtime linear with the number of elements are the most affected. More computationally intensive operations have only a slight unnoticeable performance loss. MOST PEOPLE SHOULD NOT WORRY ABOUT THE SLIGHT LOSS IN PERFORMANCE.
It is hard to judge how significant the performance hit will be in general. Often the performance hit is insignificant since other parts of the application are more processor intensive or the bottle neck is a more computationally complex operation. The best approach is benchmark and then optimize the code.
If SimpleMatrix is extended then the protected function {link #createMatrix} should be extended and return the child class. The results of SimpleMatrix operations will then be of the correct matrix type.
The object oriented approach used in SimpleMatrix was originally inspired by Jama. http://math.nist.gov/javanumerics/jama/
Modifier and Type | Field and Description |
---|---|
static int |
END
A simplified way to reference the last row or column in the matrix for some functions.
|
convertType, mat, ops
Modifier | Constructor and Description |
---|---|
protected |
SimpleMatrix()
Constructor for internal library use only.
|
|
SimpleMatrix(double[][] data)
Creates a matrix with the values and shape defined by the 2D array 'data'.
|
|
SimpleMatrix(float[][] data) |
|
SimpleMatrix(int numRows,
int numCols)
Creates a new matrix that is initially set to zero with the specified dimensions.
|
|
SimpleMatrix(int numRows,
int numCols,
boolean rowMajor,
double[] data)
Creates a new matrix which has the same value as the matrix encoded in the
provided array.
|
|
SimpleMatrix(int numRows,
int numCols,
boolean rowMajor,
float[] data) |
|
SimpleMatrix(int numRows,
int numCols,
java.lang.Class type) |
|
SimpleMatrix(int numRows,
int numCols,
org.ejml.data.MatrixType type)
Create a simple matrix of the specified type
|
|
SimpleMatrix(org.ejml.data.Matrix orig)
Creates a new SimpleMatrix which is a copy of the Matrix.
|
|
SimpleMatrix(SimpleMatrix orig)
Creats a new SimpleMatrix which is identical to the original.
|
Modifier and Type | Method and Description |
---|---|
protected SimpleMatrix |
createMatrix(int numRows,
int numCols,
org.ejml.data.MatrixType type)
Used internally for creating new instances of SimpleMatrix.
|
static SimpleMatrix |
diag(java.lang.Class type,
double... vals)
Creates a real valued diagonal matrix of the specified type
|
static SimpleMatrix |
diag(double... vals)
Creates a matrix where all but the diagonal elements are zero.
|
static SimpleMatrix |
identity(int width)
Creates a new identity matrix with the specified size.
|
static SimpleMatrix |
identity(int width,
java.lang.Class type) |
static SimpleMatrix |
random_DDRM(int numRows,
int numCols,
double minValue,
double maxValue,
java.util.Random rand)
Creates a new SimpleMatrix with random elements drawn from a uniform distribution from minValue to maxValue.
|
static SimpleMatrix |
random_FDRM(int numRows,
int numCols,
float minValue,
float maxValue,
java.util.Random rand) |
static SimpleMatrix |
randomNormal(SimpleMatrix covariance,
java.util.Random random)
Creates a new vector which is drawn from a multivariate normal distribution with zero mean
and the provided covariance.
|
static SimpleMatrix |
wrap(org.ejml.data.Matrix internalMat)
Creates a new SimpleMatrix with the specified DMatrixRMaj used as its internal matrix.
|
protected SimpleMatrix |
wrapMatrix(org.ejml.data.Matrix m) |
bits, cols, combine, concatColumns, concatRows, conditionP2, copy, createLike, determinant, diag, divide, dot, eig, elementDiv, elementExp, elementLog, elementMaxAbs, elementMult, elementPower, elementPower, elementSum, equation, extractMatrix, extractVector, fill, get, get, get, getCDRM, getDDRM, getDSCC, getFDRM, getFSCC, getIndex, getMatrix, getNumElements, getType, getZDRM, hasUncountable, insertIntoThis, invert, isIdentical, isInBounds, isVector, iterator, kron, loadBinary, loadCSV, lookupOps, minus, minus, mult, negative, normF, numCols, numRows, plus, plus, plus, print, print, print, printDimensions, pseudoInverse, reshape, rows, saveToFileBinary, saveToFileCSV, scale, set, set, set, set, setColumn, setMatrix, setRow, solve, svd, svd, toString, trace, transpose, zero
public static final int END
public SimpleMatrix(int numRows, int numCols, boolean rowMajor, double[] data)
Creates a new matrix which has the same value as the matrix encoded in the provided array. The input matrix's format can either be row-major or column-major.
Note that 'data' is a variable argument type, so either 1D arrays or a set of numbers can be
passed in:
SimpleMatrix a = new SimpleMatrix(2,2,true,new double[]{1,2,3,4});
SimpleMatrix b = new SimpleMatrix(2,2,true,1,2,3,4);
Both are equivalent.
numRows
- The number of rows.numCols
- The number of columns.rowMajor
- If the array is encoded in a row-major or a column-major format.data
- The formatted 1D array. Not modified.DMatrixRMaj.DMatrixRMaj(int, int, boolean, double...)
public SimpleMatrix(int numRows, int numCols, boolean rowMajor, float[] data)
public SimpleMatrix(double[][] data)
Creates a matrix with the values and shape defined by the 2D array 'data'.
It is assumed that 'data' has a row-major formatting:
data[ row ][ column ]
data
- 2D array representation of the matrix. Not modified.DMatrixRMaj.DMatrixRMaj(double[][])
public SimpleMatrix(float[][] data)
public SimpleMatrix(int numRows, int numCols)
DMatrixRMaj
.numRows
- The number of rows in the matrix.numCols
- The number of columns in the matrix.public SimpleMatrix(int numRows, int numCols, java.lang.Class type)
public SimpleMatrix(int numRows, int numCols, org.ejml.data.MatrixType type)
numRows
- The number of rows in the matrix.numCols
- The number of columns in the matrix.type
- The matrix typepublic SimpleMatrix(SimpleMatrix orig)
orig
- The matrix which is to be copied. Not modified.public SimpleMatrix(org.ejml.data.Matrix orig)
orig
- The original matrix whose value is copied. Not modified.protected SimpleMatrix()
public static SimpleMatrix wrap(org.ejml.data.Matrix internalMat)
internalMat
- The internal DMatrixRMaj of the returned SimpleMatrix. Will be modified.public static SimpleMatrix identity(int width)
width
- The width and height of the matrix.CommonOps_DDRM.identity(int)
public static SimpleMatrix identity(int width, java.lang.Class type)
public static SimpleMatrix diag(double... vals)
Creates a matrix where all but the diagonal elements are zero. The values of the diagonal elements are specified by the parameter 'vals'.
To extract the diagonal elements from a matrix see SimpleBase.diag()
.
vals
- The values of the diagonal elements.CommonOps_DDRM.diag(double...)
public static SimpleMatrix diag(java.lang.Class type, double... vals)
public static SimpleMatrix random_DDRM(int numRows, int numCols, double minValue, double maxValue, java.util.Random rand)
Creates a new SimpleMatrix with random elements drawn from a uniform distribution from minValue to maxValue.
numRows
- The number of rows in the new matrixnumCols
- The number of columns in the new matrixminValue
- Lower boundmaxValue
- Upper boundrand
- The random number generator that's used to fill the matrix. @return The new random matrix.RandomMatrices_DDRM.fillUniform(DMatrixRMaj,java.util.Random)
public static SimpleMatrix random_FDRM(int numRows, int numCols, float minValue, float maxValue, java.util.Random rand)
public static SimpleMatrix randomNormal(SimpleMatrix covariance, java.util.Random random)
Creates a new vector which is drawn from a multivariate normal distribution with zero mean and the provided covariance.
covariance
- Covariance of the multivariate normal distributionCovarianceRandomDraw_DDRM
protected SimpleMatrix createMatrix(int numRows, int numCols, org.ejml.data.MatrixType type)
SimpleBase
createMatrix
in class SimpleBase<SimpleMatrix>
numRows
- number of rows in the new matrix.numCols
- number of columns in the new matrix.type
- Type of matrix it should createprotected SimpleMatrix wrapMatrix(org.ejml.data.Matrix m)
wrapMatrix
in class SimpleBase<SimpleMatrix>