Class SimpleMatrix
- java.lang.Object
-
- org.ejml.simple.SimpleBase<SimpleMatrix>
-
- org.ejml.simple.SimpleMatrix
-
- All Implemented Interfaces:
java.io.Serializable
public class SimpleMatrix extends SimpleBase<SimpleMatrix>
SimpleMatrixis a wrapper aroundDMatrixRMajthat 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
DMatrixRMajand SimpleMatrix in the same code base is easy. To access the internal DMatrixRMaj in a SimpleMatrix simply callSimpleBase.getMatrix(). To turn a DMatrixRMaj into a SimpleMatrix usewrap(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
SimpleBaseinstead. 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:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static intENDA 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
Constructors Modifier Constructor Description protectedSimpleMatrix()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 typeSimpleMatrix(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.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected SimpleMatrixcreateMatrix(int numRows, int numCols, org.ejml.data.MatrixType type)Used internally for creating new instances of SimpleMatrix.static SimpleMatrixdiag(double... vals)Creates a matrix where all but the diagonal elements are zero.static SimpleMatrixdiag(java.lang.Class type, double... vals)Creates a real valued diagonal matrix of the specified typestatic SimpleMatrixidentity(int width)Creates a new identity matrix with the specified size.static SimpleMatrixidentity(int width, java.lang.Class type)static SimpleMatrixrandom_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 SimpleMatrixrandom_FDRM(int numRows, int numCols, float minValue, float maxValue, java.util.Random rand)static SimpleMatrixrandomNormal(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 SimpleMatrixwrap(org.ejml.data.Matrix internalMat)Creates a new SimpleMatrix with the specified DMatrixRMaj used as its internal matrix.protected SimpleMatrixwrapMatrix(org.ejml.data.Matrix m)-
Methods inherited from class org.ejml.simple.SimpleBase
bits, cols, combine, concatColumns, concatRows, conditionP2, convertToDense, convertToSparse, copy, createLike, determinant, diag, divide, dot, eig, elementDiv, elementExp, elementLog, elementMaxAbs, elementMinAbs, elementMult, elementPower, elementPower, elementSum, equation, extractMatrix, extractVector, fill, get, get, get, getCDRM, getDDRM, getDSCC, getFDRM, getFSCC, getIndex, getMatrix, getNumElements, getType, getZDRM, hasUncountable, insertIntoThis, invert, invoke, isIdentical, isInBounds, isVector, iterator, kron, loadBinary, loadCSV, lookupOps, minus, minus, mult, negative, normF, numCols, numRows, plus, plus, plus, print, print, printDimensions, pseudoInverse, reshape, rows, saveToFileBinary, saveToFileCSV, scale, set, set, set, set, setColumn, setMatrix, setRow, solve, svd, svd, toString, trace, transpose, zero
-
-
-
-
Field Detail
-
END
public static final int END
A simplified way to reference the last row or column in the matrix for some functions.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
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:
DMatrixRMaj(int, int, boolean, double...)
-
SimpleMatrix
public SimpleMatrix(int numRows, int numCols, boolean rowMajor, float[] data)
-
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:
DMatrixRMaj(double[][])
-
SimpleMatrix
public SimpleMatrix(float[][] data)
-
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
public SimpleMatrix(int numRows, int numCols, java.lang.Class type)
-
SimpleMatrix
public SimpleMatrix(int numRows, int numCols, org.ejml.data.MatrixType type)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
public SimpleMatrix(SimpleMatrix orig)
Creats a new SimpleMatrix which is identical to the original.- Parameters:
orig- The matrix which is to be copied. Not modified.
-
SimpleMatrix
public SimpleMatrix(org.ejml.data.Matrix orig)
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 Detail
-
wrap
public static SimpleMatrix wrap(org.ejml.data.Matrix internalMat)
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.
-
identity
public static SimpleMatrix identity(int width)
Creates a new identity matrix with the specified size.- Parameters:
width- The width and height of the matrix.- Returns:
- An identity matrix.
- See Also:
CommonOps_DDRM.identity(int)
-
identity
public static SimpleMatrix identity(int width, java.lang.Class type)
-
diag
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().- Parameters:
vals- The values of the diagonal elements.- Returns:
- A diagonal matrix.
- See Also:
CommonOps_DDRM.diag(double...)
-
diag
public static SimpleMatrix diag(java.lang.Class type, double... vals)
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, java.util.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:
RandomMatrices_DDRM.fillUniform(DMatrixRMaj,java.util.Random)
-
random_FDRM
public static SimpleMatrix random_FDRM(int numRows, int numCols, float minValue, float maxValue, java.util.Random rand)
-
randomNormal
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.
- Parameters:
covariance- Covariance of the multivariate normal distribution- Returns:
- Vector randomly drawn from the distribution
- See Also:
CovarianceRandomDraw_DDRM
-
createMatrix
protected SimpleMatrix createMatrix(int numRows, int numCols, org.ejml.data.MatrixType type)
Description copied from class:SimpleBaseUsed 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:
createMatrixin 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
protected SimpleMatrix wrapMatrix(org.ejml.data.Matrix m)
- Specified by:
wrapMatrixin classSimpleBase<SimpleMatrix>
-
-