Difference between revisions of "SimpleMatrix"
(Created page with " SimpleMatrix is an interface that provides an easy to use object oriented way of doing linear algebra. It is a wrapper around the procedural interface in EJML and was origin...") |
|||
Line 1: | Line 1: | ||
− | |||
SimpleMatrix is an interface that provides an easy to use object oriented way of doing linear algebra. It is a wrapper around the procedural interface in EJML and was originally inspired by [http://math.nist.gov/javanumerics/jama/ Jama]. When using SimpleMatrix, memory management is automatically handled and it allows commands to be chained together using a flow paradigm. Switching between SimpleMatrix and the [[Procedural]] interface is easy, enabling the two programming paradigms to be mixed in the same code base. | SimpleMatrix is an interface that provides an easy to use object oriented way of doing linear algebra. It is a wrapper around the procedural interface in EJML and was originally inspired by [http://math.nist.gov/javanumerics/jama/ Jama]. When using SimpleMatrix, memory management is automatically handled and it allows commands to be chained together using a flow paradigm. Switching between SimpleMatrix and the [[Procedural]] interface is easy, enabling the two programming paradigms to be mixed in the same code base. | ||
Line 30: | Line 29: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | A DenseMatrix64F can also be passed into the SimpleMatrix constructor, but this will copy the input matrix. Unlike with when wrap is used, changed to the new SimpleMatrix will not modify the original DenseMatrix64F. | + | A {{DataDocLink|DenseMatrix64F}} can also be passed into the SimpleMatrix constructor, but this will copy the input matrix. Unlike with when wrap is used, changed to the new SimpleMatrix will not modify the original DenseMatrix64F. |
== Accessors == | == Accessors == | ||
Line 71: | Line 70: | ||
== Other Functions == | == Other Functions == | ||
− | SimpleMatrix provides many other functions. For a complete list see the JavaDoc for | + | SimpleMatrix provides many other functions. For a complete list see the JavaDoc for {{DocLink|org/ejml/simple/SimpleBase.html|SimpleBase}} and {{DocLink|org/ejml/simple/SimpleMatrix.html|SimpleMatrix}. Note that SimpleMatrix extends SimpleBase. |
+ | |||
+ | == Adding Functionality == | ||
+ | |||
+ | You can turn SimpleMatrix into your own data structure and extend its capabilities. See the [[Example_Customizing_SimpleMatrix|example on customizing SimpleMatrix]] for the details. |
Revision as of 07:02, 26 March 2015
SimpleMatrix is an interface that provides an easy to use object oriented way of doing linear algebra. It is a wrapper around the procedural interface in EJML and was originally inspired by Jama. When using SimpleMatrix, memory management is automatically handled and it allows commands to be chained together using a flow paradigm. Switching between SimpleMatrix and the Procedural interface is easy, enabling the two programming paradigms to be mixed in the same code base.
When invoking a function in SimpleMatrix none of the input matrices, including the 'this' matrix, are modified during the function call. There is a slight performance hit when using SimpleMatrix and less control over memory management. See Performance for a comparison of runtime performance of the different interfaces.
Below is a brief overview of SimpleMatrix concepts.
Chaining Operations
When using SimpleMatrix operations can be chained together. Chained operations are often easier to read and write.
public SimpleMatrix process( SimpleMatrix A , SimpleMatrix B ) {
return A.transpose().mult(B).scale(12).invert();
}
is equivalent to the following Matlab code:
C = inv((A' * B)*12.0)
Working with DenseMatrix64F
To convert a [DenseMatrix64F DenseMatrix64F] into a SimpleMatrix call the wrap() function. Then to get access to the internal DenseMatrix64F inside of a SimpleMatrix call getMatrix().
public DenseMatrix64F compute( DenseMatrix64F A , DenseMatrix64F B ) {
SimpleMatrix A_ = SimpleMatrix.wrap(A);
SimpleMatrix B_ = SimpleMatrix.wrap(B);
return A_.mult(B_).getMatrix();
}
A DenseMatrix64F can also be passed into the SimpleMatrix constructor, but this will copy the input matrix. Unlike with when wrap is used, changed to the new SimpleMatrix will not modify the original DenseMatrix64F.
Accessors
- get( row , col )
- set( row , col , value )
- Returns or sets the value of an element at the specified row and column.
- get( index )
- set( index )
- Returns or sets the value of an element at the specified index. Useful for vectors and element-wise operations.
- iterator( boolean rowMajor, int minRow, int minCol, int maxRow, int maxCol )
- An iterator that iterates through the sub-matrix by row or by column.
Submatrices
A submatrix is a matrix whose elements are a subset of another matrix. Several different functions are provided for manipulating submatrices.
- extractMatrix
- Extracts a rectangular submatrix from the original matrix.
- extractDiag
- Creates a column vector containing just the diagonal elements of the matrix.
- extractVector
- Extracts either an entire row or column.
- insertIntoThis
- Inserts the passed in matrix into 'this' matrix.
- combine
- Creates a now matrix that is a combination of the two inputs.
Decompositions
Simplified ways to use popular matrix decompositions is provided. These decompositions provide fewer choices than the equivalent for DenseMatrix64F, but should meet most people needs.
- svd
- Computes the singular value decomposition of 'this' matrix
- eig
- Computes the eigen value decomposition of 'this' matrix
Direct access to other decompositions (e.g. QR and Cholesky) is not provided in SimpleMatrix because solve() and inv() is provided instead. In more advanced applications use the operator interface instead to compute those decompositions.
Solve and Invert
- solve
- Computes the solution to the set of linear equations
- inv
- Computes the inverse of a square matrix
- pinv
- Computes the pseudo-inverse for an arbitrary matrix
See Solving Linear Systems for more details on solving systems of equations.
Other Functions
SimpleMatrix provides many other functions. For a complete list see the JavaDoc for SimpleBase and {{DocLink|org/ejml/simple/SimpleMatrix.html|SimpleMatrix}. Note that SimpleMatrix extends SimpleBase.
Adding Functionality
You can turn SimpleMatrix into your own data structure and extend its capabilities. See the example on customizing SimpleMatrix for the details.