Class SimpleMatrix

All Implemented Interfaces:
Serializable, ConstMatrix<SimpleMatrix>

public class SimpleMatrix extends SimpleBase<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 Details

    • END

      public static final int END
      A 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 a DMatrixRMaj.
      Parameters:
      numRows - The number of rows in the matrix.
      numCols - The number of columns in the matrix.
    • SimpleMatrix

      public SimpleMatrix(int numRows, int numCols, Class type)
    • SimpleMatrix

      public SimpleMatrix(int numRows, int numCols, 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)
      Creates a new SimpleMatrix which is identical to the original.
      Parameters:
      orig - The matrix which is to be copied. Not modified.
    • SimpleMatrix

      public SimpleMatrix(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 Details

    • wrap

      public static SimpleMatrix wrap(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.
    • filled

      public static SimpleMatrix filled(int numRows, int numCols, double a)
      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

      public static SimpleMatrix ones(int numRows, int numCols)
      Returns a matrix of ones.
      Parameters:
      numRows - The number of numRows.
      numCols - The number of columns.
      Returns:
      A matrix of ones.
    • 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:
    • identity

      public static SimpleMatrix identity(int width, 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:
    • diag

      public static SimpleMatrix diag(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, 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:
    • random

      public static SimpleMatrix random(int numRows, int numCols)
      Creates a DDRM random matrix with values from 0.0 to 1.0. Random number generator is ThreadLocalRandom.current().
    • random_DDRM

      public static SimpleMatrix random_DDRM(int numRows, int numCols)
      Creates a DDRM random matrix with values from 0.0 to 1.0. Random number generator is ThreadLocalRandom.current().
    • random_FDRM

      public static SimpleMatrix random_FDRM(int numRows, int numCols, float minValue, float maxValue, Random rand)
    • random_FDRM

      public static SimpleMatrix random_FDRM(int numRows, int numCols)
      Creates a FDRM random matrix with values from 0.0 to 1.0. Random number generator is ThreadLocalRandom.current().
    • random_ZDRM

      public static SimpleMatrix random_ZDRM(int numRows, int numCols, double minValue, double maxValue, Random rand)
    • random_ZDRM

      public static SimpleMatrix random_ZDRM(int numRows, int numCols)
      Creates a ZDRM random matrix with values from 0.0 to 1.0. Random number generator is ThreadLocalRandom.current().
    • random_CDRM

      public static SimpleMatrix random_CDRM(int numRows, int numCols, float minValue, float maxValue, Random rand)
    • random_CDRM

      public static SimpleMatrix random_CDRM(int numRows, int numCols)
      Creates a CDRM random matrix with values from 0.0 to 1.0. Random number generator is ThreadLocalRandom.current().
    • randomNormal

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

      Parameters:
      covariance - Covariance of the multivariate normal distribution
      Returns:
      Vector randomly drawn from the distribution
      See Also:
    • createMatrix

      protected SimpleMatrix createMatrix(int numRows, int numCols, MatrixType type)
      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 class SimpleBase<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(Matrix m)
      Specified by:
      wrapMatrix in class SimpleBase<SimpleMatrix>