Package org.ejml.data

Class DMatrixRMaj

All Implemented Interfaces:
Serializable, DMatrix, Matrix, ReshapeMatrix

public class DMatrixRMaj extends DMatrix1Row

DMatrixRMaj is a row matrix with real elements that are 64-bit floats. A matrix is the fundamental data structure in linear algebra. Unlike a sparse matrix, there is no compression in a row matrix and every element is stored in memory. This allows for fast reads and writes to the matrix.

The matrix is stored internally in a row-major 1D array format:

data[ y*numCols + x ] = data[y][x]

For example:
data =

 a[0]  a[1]   a[2]   a[3]
 a[4]  a[5]   a[6]   a[7]
 a[8]  a[9]   a[10]  a[11]
 a[12] a[13]  a[14]  a[15]
 
See Also:
  • Field Summary

    Fields inherited from class org.ejml.data.DMatrixD1

    data, numCols, numRows
  • Constructor Summary

    Constructors
    Constructor
    Description
    Default constructor that creates a 0 by 0 matrix.
    DMatrixRMaj(double[] data)
    Creates a column vector the same length as this array
    DMatrixRMaj(double[][] data)
    Creates a matrix with the values and shape defined by the 2D array 'data'.
    DMatrixRMaj(int length)
    This declares an array that can store a matrix up to the specified length.
    DMatrixRMaj(int numRows, int numCols)
    Creates a new Matrix with the specified shape whose elements initially have the value of zero.
    DMatrixRMaj(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.
    Creates a new DMatrixRMaj which contains the same information as the provided Matrix64F.
    Creates a new matrix which is equivalent to the provided matrix.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    add(int row, int col, double value)
    Adds 'value' to the specified element in the matrix.

    aij = aij + value
    Creates and returns a matrix which is identical to this one.
    create(int numRows, int numCols)
    Creates a new matrix of the same type with the specified shape
    Creates a new matrix with the same shape as this matrix
    void
    fill(double value)
    Sets all elements equal to the specified value.
    double
    get(int row, int col)
    Returns the value of the specified matrix element.
    double[][]
    Export this matrix using a 2D array representation.
    int
    getIndex(int row, int col)
    Returns the internal array index for the specified row and column.
    Returns the type of matrix
    boolean
    isInBounds(int row, int col)
    Determines if the specified element is inside the bounds of the Matrix.
    void
    reshape(int numRows, int numCols, boolean saveValues)
    Changes the number of rows and columns in the matrix, allowing its size to grow or shrink.
    void
    set(double[][] input)
    Assigns this matrix using a 2D array representation
    void
    set(int numRows, int numCols, boolean rowMajor, double... data)
    Sets this matrix equal to the matrix encoded in the array.
    void
    set(int row, int col, double value)
    Assigns the element in the Matrix to the specified value.
    void
    setTo(Matrix original)
    Sets this matrix to be identical to the 'original' matrix passed in.
    Converts the array into a string format for display purposes.
    double
    unsafe_get(int row, int col)
    Same as DMatrix.get(int, int) but does not perform bounds check on input parameters.
    void
    unsafe_set(int row, int col, double value)
    Same as Matrix.setTo(org.ejml.data.Matrix) but does not perform bounds check on input parameters.
    wrap(int numRows, int numCols, double[] data)
    Creates a new DMatrixRMaj around the provided data.
    void
    Sets all elements equal to zero.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait

    Methods inherited from interface org.ejml.data.DMatrix

    getNumElements
  • Constructor Details

    • DMatrixRMaj

      public DMatrixRMaj(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:
      DenseMatrix a = new DenseMatrix(2,2,true,new double[]{1,2,3,4});
      DenseMatrix b = new DenseMatrix(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.
    • DMatrixRMaj

      public DMatrixRMaj(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.
    • DMatrixRMaj

      public DMatrixRMaj(double[] data)
      Creates a column vector the same length as this array
      Parameters:
      data - elements in vector. copied
    • DMatrixRMaj

      public DMatrixRMaj(int numRows, int numCols)
      Creates a new Matrix with the specified shape whose elements initially have the value of zero.
      Parameters:
      numRows - The number of rows in the matrix.
      numCols - The number of columns in the matrix.
    • DMatrixRMaj

      public DMatrixRMaj(DMatrixRMaj orig)
      Creates a new matrix which is equivalent to the provided matrix. Note that the length of the data will be determined by the shape of the matrix.
      Parameters:
      orig - The matrix which is to be copied. This is not modified or saved.
    • DMatrixRMaj

      public DMatrixRMaj(int length)
      This declares an array that can store a matrix up to the specified length. This is use full when a matrix's size will be growing and it is desirable to avoid reallocating memory.
      Parameters:
      length - The size of the matrice's data array.
    • DMatrixRMaj

      public DMatrixRMaj()
      Default constructor that creates a 0 by 0 matrix.
    • DMatrixRMaj

      public DMatrixRMaj(DMatrix mat)
      Creates a new DMatrixRMaj which contains the same information as the provided Matrix64F.
      Parameters:
      mat - Matrix whose values will be copied. Not modified.
  • Method Details

    • wrap

      public static DMatrixRMaj wrap(int numRows, int numCols, double[] data)
      Creates a new DMatrixRMaj around the provided data. The data must encode a row-major matrix. Any modification to the returned matrix will modify the provided data.
      Parameters:
      numRows - Number of rows in the matrix.
      numCols - Number of columns in the matrix.
      data - Data that is being wrapped. Referenced Saved.
      Returns:
      A matrix which references the provided data internally.
    • reshape

      public void reshape(int numRows, int numCols, boolean saveValues)
      Description copied from class: DMatrixD1

      Changes the number of rows and columns in the matrix, allowing its size to grow or shrink. If the saveValues flag is set to true, then the previous values will be maintained, but reassigned to new elements in a row-major ordering. If saveValues is false values will only be maintained when the requested size is less than or equal to the internal array size. The primary use for this function is to encourage data reuse and avoid unnecessarily declaring and initialization of new memory.

      Examples:
      [ 1 2 ; 3 4 ] → reshape( 2 , 3 , true ) = [ 1 2 3 ; 4 0 0 ]
      [ 1 2 ; 3 4 ] → reshape( 1 , 2 , true ) = [ 1 2 ]
      [ 1 2 ; 3 4 ] → reshape( 1 , 2 , false ) = [ 1 2 ]
      [ 1 2 ; 3 4 ] → reshape( 2 , 3 , false ) = [ 0 0 0 ; 0 0 0 ]

      Specified by:
      reshape in class DMatrixD1
      Parameters:
      numRows - The new number of rows in the matrix.
      numCols - The new number of columns in the matrix.
      saveValues - If true then the value of each element will be save using a row-major reordering. Typically this should be false.
    • set

      public void set(int row, int col, double value)

      Assigns the element in the Matrix to the specified value. Performs a bounds check to make sure the requested element is part of the matrix.

      aij = value

      Parameters:
      row - The row of the element.
      col - The column of the element.
      value - The element's new value.
    • unsafe_set

      public void unsafe_set(int row, int col, double value)
      Description copied from interface: DMatrix
      Same as Matrix.setTo(org.ejml.data.Matrix) but does not perform bounds check on input parameters. This results in about a 25% speed increase but potentially sacrifices stability and makes it more difficult to track down simple errors. It is not recommended that this function be used, except in highly optimized code where the bounds are implicitly being checked.
      Parameters:
      row - Matrix element's row index..
      col - Matrix element's column index.
      value - The element's new value.
    • add

      public void add(int row, int col, double value)

      Adds 'value' to the specified element in the matrix.

      aij = aij + value

      Parameters:
      row - The row of the element.
      col - The column of the element.
      value - The value that is added to the element
    • get

      public double get(int row, int col)
      Returns the value of the specified matrix element. Performs a bounds check to make sure the requested element is part of the matrix.
      Parameters:
      row - The row of the element.
      col - The column of the element.
      Returns:
      The value of the element.
    • unsafe_get

      public double unsafe_get(int row, int col)
      Description copied from interface: DMatrix
      Same as DMatrix.get(int, int) but does not perform bounds check on input parameters. This results in about a 25% speed increase but potentially sacrifices stability and makes it more difficult to track down simple errors. It is not recommended that this function be used, except in highly optimized code where the bounds are implicitly being checked.
      Parameters:
      row - Matrix element's row index..
      col - Matrix element's column index.
      Returns:
      The specified element's value.
    • getIndex

      public int getIndex(int row, int col)
      Description copied from class: DMatrixD1
      Returns the internal array index for the specified row and column.
      Specified by:
      getIndex in class DMatrixD1
      Parameters:
      row - Row index.
      col - Column index.
      Returns:
      Internal array index.
    • isInBounds

      public boolean isInBounds(int row, int col)
      Determines if the specified element is inside the bounds of the Matrix.
      Parameters:
      row - The element's row.
      col - The element's column.
      Returns:
      True if it is inside the matrices bound, false otherwise.
    • set

      public void set(int numRows, int numCols, boolean rowMajor, double... data)
      Sets this matrix equal to the matrix encoded in the array.
      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.
    • zero

      public void zero()
      Sets all elements equal to zero.
    • fill

      public void fill(double value)
      Sets all elements equal to the specified value.
    • copy

      public DMatrixRMaj copy()
      Creates and returns a matrix which is identical to this one.
      Returns:
      A new identical matrix.
    • setTo

      public void setTo(Matrix original)
      Description copied from interface: Matrix
      Sets this matrix to be identical to the 'original' matrix passed in.
    • toString

      public String toString()

      Converts the array into a string format for display purposes. The conversion is done using MatrixIO.print(java.io.PrintStream, DMatrix).

      Overrides:
      toString in class Object
      Returns:
      String representation of the matrix.
    • createLike

      public DMatrixRMaj createLike()
      Description copied from interface: Matrix
      Creates a new matrix with the same shape as this matrix
    • create

      public DMatrixRMaj create(int numRows, int numCols)
      Description copied from interface: Matrix
      Creates a new matrix of the same type with the specified shape
    • getType

      public MatrixType getType()
      Description copied from interface: Matrix
      Returns the type of matrix
    • set

      public void set(double[][] input)
      Assigns this matrix using a 2D array representation
      Parameters:
      input - 2D array which this matrix will be set to
    • get2DData

      public double[][] get2DData()
      Export this matrix using a 2D array representation.
      Returns:
      2D representation of the matrix
      See Also: