Class SimpleMatrix
- All Implemented Interfaces:
Serializable
,ConstMatrix<SimpleMatrix>
SimpleMatrix
is a wrapper around a primitive matrix type
(for example, DMatrixRMaj
or FMatrixSparseCSC
) 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 a primitive matrix 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
matrix.
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/
- See Also:
-
Field Summary
Modifier and TypeFieldDescriptionstatic final int
A simplified way to reference the last row or column in the matrix for some functions.Fields inherited from class org.ejml.simple.SimpleBase
convertType, mat, ops
-
Constructor Summary
ModifierConstructorDescriptionprotected
Constructor for internal library use only.SimpleMatrix
(double[] data) Creates a column vector with the values and shape defined by the 1D array 'data'.SimpleMatrix
(double[][] data) Creates a matrix with the values and shape defined by the 2D array 'data'.SimpleMatrix
(float[] data) Creates a column vector with the values and shape defined by the 1D array 'data'.SimpleMatrix
(float[][] data) Creates a matrix with the values and shape defined by the 2D array '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) Creates a new matrix which has the same value as the matrix encoded in the provided array.SimpleMatrix
(int numRows, int numCols, Class type) SimpleMatrix
(int numRows, int numCols, MatrixType type) Create a simple matrix of the specified typeSimpleMatrix
(Matrix orig) Creates a new SimpleMatrix which is a copy of the Matrix.SimpleMatrix
(SimpleMatrix orig) Creates a new SimpleMatrix which is identical to the original. -
Method Summary
Modifier and TypeMethodDescriptionprotected SimpleMatrix
createMatrix
(int numRows, int numCols, MatrixType type) Used internally for creating new instances of SimpleMatrix.static SimpleMatrix
diag
(double... vals) Creates a matrix where all but the diagonal elements are zero.static SimpleMatrix
Creates a real valued diagonal matrix of the specified typestatic SimpleMatrix
filled
(int numRows, int numCols, double a) Returns a filled matrix (numRows x numCols) of the value a.static SimpleMatrix
identity
(int width) Creates a new identity matrix with the specified size.static SimpleMatrix
static SimpleMatrix
ones
(int numRows, int numCols) Returns a matrix of ones.static SimpleMatrix
random
(int numRows, int numCols) Creates a DDRM random matrix with values from 0.0 to 1.0.static SimpleMatrix
random_CDRM
(int numRows, int numCols) Creates a CDRM random matrix with values from 0.0 to 1.0.static SimpleMatrix
random_CDRM
(int numRows, int numCols, float minValue, float maxValue, Random rand) static SimpleMatrix
random_DDRM
(int numRows, int numCols) Creates a DDRM random matrix with values from 0.0 to 1.0.static SimpleMatrix
random_DDRM
(int numRows, int numCols, double minValue, double maxValue, 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) Creates a FDRM random matrix with values from 0.0 to 1.0.static SimpleMatrix
random_FDRM
(int numRows, int numCols, float minValue, float maxValue, Random rand) static SimpleMatrix
random_ZDRM
(int numRows, int numCols) Creates a ZDRM random matrix with values from 0.0 to 1.0.static SimpleMatrix
random_ZDRM
(int numRows, int numCols, double minValue, double maxValue, Random rand) static SimpleMatrix
randomNormal
(SimpleMatrix covariance, Random random) Creates a new vector which is drawn from a multivariate normal distribution with zero mean and the provided covariance.static SimpleMatrix
Creates a new SimpleMatrix with the specified DMatrixRMaj used as its internal matrix.protected SimpleMatrix
wrapMatrix
(Matrix m) Methods inherited from class org.ejml.simple.SimpleBase
bits, cols, combine, concatColumns, concatRows, conditionP2, conjugate, convertToComplex, convertToDense, convertToSparse, copy, createComplexMatrix, createLike, createRealMatrix, determinant, determinantComplex, diag, divide, dot, eig, elementDiv, elementExp, elementLog, elementMax, elementMaxAbs, elementMin, elementMinAbs, elementMult, elementOp, elementOp, elementPower, elementPower, elementSum, elementSumComplex, equation, extractMatrix, extractVector, fill, fillComplex, get, get, get, getCDRM, getColumn, getDDRM, getDSCC, getFDRM, getFSCC, getImaginary, getIndex, getMatrix, getNumCols, getNumRows, getReal, getRow, getType, getZDRM, hasUncountable, imaginary, insertIntoThis, invert, invoke, isIdentical, isInBounds, isVector, iterator, kron, loadCSV, lookupOps, magnitude, minus, minus, minusComplex, mult, negative, normF, numCols, numRows, plus, plus, plus, plusComplex, print, print, printDimensions, pseudoInverse, real, reshape, rows, saveToFileCSV, saveToMatrixMarket, scale, scaleComplex, set, set, set, set, setColumn, setColumn, setMatrix, setRow, setRow, setTo, solve, svd, svd, toArray2, toString, trace, traceComplex, transpose, transposeConjugate, zero
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface org.ejml.simple.ConstMatrix
getImag, getNumElements, imag
-
Field Details
-
END
public static final int ENDA simplified way to reference the last row or column in the matrix for some functions.- See Also:
-
-
Constructor Details
-
SimpleMatrix
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.- Parameters:
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.- See Also:
-
SimpleMatrix
public SimpleMatrix(int numRows, int numCols, boolean rowMajor, float... 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 float[]{1,2,3,4});
SimpleMatrix b = new SimpleMatrix(2,2,true,1,2,3,4);
Both are equivalent.- Parameters:
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.- See Also:
-
SimpleMatrix
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 ]- Parameters:
data
- 2D array representation of the matrix. Not modified.- See Also:
-
SimpleMatrix
public SimpleMatrix(float[][] 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 ]- Parameters:
data
- 2D array representation of the matrix. Not modified.- See Also:
-
SimpleMatrix
public SimpleMatrix(double[] data) Creates a column vector with the values and shape defined by the 1D array 'data'.- Parameters:
data
- 1D array representation of the vector. Not modified.
-
SimpleMatrix
public SimpleMatrix(float[] data) Creates a column vector with the values and shape defined by the 1D array 'data'.- Parameters:
data
- 1D array representation of the vector. Not modified.
-
SimpleMatrix
public SimpleMatrix(int numRows, int numCols) Creates a new matrix that is initially set to zero with the specified dimensions. This will wrap aDMatrixRMaj
.- Parameters:
numRows
- The number of rows in the matrix.numCols
- The number of columns in the matrix.
-
SimpleMatrix
-
SimpleMatrix
Create a simple matrix of the specified type- Parameters:
numRows
- The number of rows in the matrix.numCols
- The number of columns in the matrix.type
- The matrix type
-
SimpleMatrix
Creates a new SimpleMatrix which is identical to the original.- Parameters:
orig
- The matrix which is to be copied. Not modified.
-
SimpleMatrix
Creates a new SimpleMatrix which is a copy of the Matrix.- Parameters:
orig
- The original matrix whose value is copied. Not modified.
-
SimpleMatrix
protected SimpleMatrix()Constructor for internal library use only. Nothing is configured and is intended for serialization.
-
-
Method Details
-
wrap
Creates a new SimpleMatrix with the specified DMatrixRMaj used as its internal matrix. This means that the reference is saved and calls made to the returned SimpleMatrix will modify the passed in DMatrixRMaj.- Parameters:
internalMat
- The internal DMatrixRMaj of the returned SimpleMatrix. Will be modified.
-
filled
Returns a filled matrix (numRows x numCols) of the value a.- Parameters:
numRows
- The number of numRows.numCols
- The number of columns.a
- The number to fill the matrix with.- Returns:
- A matrix filled with the value a.
-
ones
Returns a matrix of ones.- Parameters:
numRows
- The number of numRows.numCols
- The number of columns.- Returns:
- A matrix of ones.
-
identity
Creates a new identity matrix with the specified size.- Parameters:
width
- The width and height of the matrix.- Returns:
- An identity matrix.
- See Also:
-
identity
-
diag
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()
.- Parameters:
vals
- The values of the diagonal elements.- Returns:
- A diagonal matrix.
- See Also:
-
diag
Creates a real valued diagonal matrix of the specified type -
random_DDRM
public static SimpleMatrix random_DDRM(int numRows, int numCols, double minValue, double maxValue, Random rand) Creates a new SimpleMatrix with random elements drawn from a uniform distribution from minValue to maxValue.
- Parameters:
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.- See Also:
-
random
Creates a DDRM random matrix with values from 0.0 to 1.0. Random number generator isThreadLocalRandom.current()
. -
random_DDRM
Creates a DDRM random matrix with values from 0.0 to 1.0. Random number generator isThreadLocalRandom.current()
. -
random_FDRM
public static SimpleMatrix random_FDRM(int numRows, int numCols, float minValue, float maxValue, Random rand) -
random_FDRM
Creates a FDRM random matrix with values from 0.0 to 1.0. Random number generator isThreadLocalRandom.current()
. -
random_ZDRM
public static SimpleMatrix random_ZDRM(int numRows, int numCols, double minValue, double maxValue, Random rand) -
random_ZDRM
Creates a ZDRM random matrix with values from 0.0 to 1.0. Random number generator isThreadLocalRandom.current()
. -
random_CDRM
public static SimpleMatrix random_CDRM(int numRows, int numCols, float minValue, float maxValue, Random rand) -
random_CDRM
Creates a CDRM random matrix with values from 0.0 to 1.0. Random number generator isThreadLocalRandom.current()
. -
randomNormal
Creates a new vector which is drawn from a multivariate normal distribution with zero mean and the provided covariance.
- Parameters:
covariance
- Covariance of the multivariate normal distribution- Returns:
- Vector randomly drawn from the distribution
- See Also:
-
createMatrix
Description copied from class:SimpleBase
Used internally for creating new instances of SimpleMatrix. If SimpleMatrix is extended by another class this function should be overridden so that the returned matrices are of the correct type.- Specified by:
createMatrix
in classSimpleBase<SimpleMatrix>
- Parameters:
numRows
- number of rows in the new matrix.numCols
- number of columns in the new matrix.type
- Type of matrix it should create- Returns:
- A new matrix.
-
wrapMatrix
- Specified by:
wrapMatrix
in classSimpleBase<SimpleMatrix>
-