## Class SimpleMatrix

- java.lang.Object
- org.ejml.simple.SimpleBase<SimpleMatrix>
- org.ejml.simple.SimpleMatrix

- All Implemented Interfaces:
- Serializable

public class SimpleMatrixextends SimpleBase<SimpleMatrix>

`SimpleMatrix`

is a wrapper around`DenseMatrix64F`

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

`DenseMatrix64F`

and SimpleMatrix in the same code base is easy. To access the internal DenseMatrix64F in a SimpleMatrix simply call`SimpleBase.getMatrix()`

. To turn a DenseMatrix64F into a SimpleMatrix use`wrap(org.ejml.data.DenseMatrix64F)`

. Not all operations in EJML are provided for SimpleMatrix, but can be accessed by extracting the internal DenseMatrix64F.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:
- Serialized Form

### Field Summary

Fields 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.

### Constructor Summary

Constructors Constructor and Description **SimpleMatrix**(DenseMatrix64F orig)Creates a new SimpleMatrix which is a copy of the DenseMatrix64F.**SimpleMatrix**(double[][] 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**(Matrix64F orig)Creates a new SimpleMatrix which is a copy of the Matrix64F.**SimpleMatrix**(SimpleMatrix orig)Creats a new SimpleMatrix which is identical to the original.

### Method Summary

Methods Modifier and Type Method and Description `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`

**random**(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`

**wrap**(DenseMatrix64F internalMat)Creates a new SimpleMatrix with the specified DenseMatrix64F used as its internal matrix.### Methods inherited from class org.ejml.simple.SimpleBase

`combine, conditionP2, copy, determinant, divide, dot, eig, elementMaxAbs, elementMult, elementSum, extractDiag, extractMatrix, extractVector, get, get, getIndex, getMatrix, getNumElements, hasUncountable, insertIntoThis, invert, isIdentical, isInBounds, isVector, iterator, kron, loadBinary, loadCSV, minus, mult, negative, normF, numCols, numRows, plus, plus, print, print, print, printDimensions, pseudoInverse, reshape, saveToFileBinary, saveToFileCSV, scale, set, set, set, set, setColumn, 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:
`DenseMatrix64F.DenseMatrix64F(int, int, boolean, double...)`

#### 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:
`DenseMatrix64F.DenseMatrix64F(double[][])`

#### SimpleMatrix

public SimpleMatrix(int numRows, int numCols)

Creates a new matrix that is initially set to zero with the specified dimensions.- Parameters:
`numRows`

- The number of rows in the matrix.`numCols`

- The number of columns in the matrix.- See Also:
`DenseMatrix64F.DenseMatrix64F(int, int)`

#### 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(DenseMatrix64F orig)

Creates a new SimpleMatrix which is a copy of the DenseMatrix64F.- Parameters:
`orig`

- The original matrix whose value is copied. Not modified.

#### SimpleMatrix

public SimpleMatrix(Matrix64F orig)

Creates a new SimpleMatrix which is a copy of the Matrix64F.- Parameters:
`orig`

- The original matrix whose value is copied. Not modified.

### Method Detail

#### wrap

public static SimpleMatrix wrap(DenseMatrix64F internalMat)

Creates a new SimpleMatrix with the specified DenseMatrix64F used as its internal matrix. This means that the reference is saved and calls made to the returned SimpleMatrix will modify the passed in DenseMatrix64F.- Parameters:
`internalMat`

- The internal DenseMatrix64F 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.identity(int)`

#### 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.extractDiag()`

.- Parameters:
`vals`

- The values of the diagonal elements.- Returns:
- A diagonal matrix.
- See Also:
`CommonOps.diag(double...)`

#### random

public static SimpleMatrix random(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 matrix`numCols`

- The number of columns in the new matrix`minValue`

- Lower bound`maxValue`

- Upper bound`rand`

- The random number generator that's used to fill the matrix. @return The new random matrix.- See Also:
`RandomMatrices.setRandom(DenseMatrix64F,java.util.Random)`

**SCaVis 1.8 © jWork.org**