# SimpleMatrix

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.

## Contents

## 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 DMatrixRMaj

To convert a DMatrixRMaj into a SimpleMatrix call the wrap() function. Then to get access to the internal DMatrixRMaj inside of a SimpleMatrix call getMatrix().

```
public DMatrixRMaj compute( DMatrixRMaj A , DMatrixRMaj B ) {
SimpleMatrix A_ = SimpleMatrix.wrap(A);
SimpleMatrix B_ = SimpleMatrix.wrap(B);
return (DMatrixRMaj)A_.mult(B_).getMatrix();
}
```

A DMatrixRMaj 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 DMatrixRMaj.

## 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 DMatrixRMaj, 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 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.