Class DenseMatrix

  • All Implemented Interfaces:
    java.io.Serializable

    
    public abstract class DenseMatrix
    extends BaseMatrix
                        

    Base class for dense matrix implementations, containing the core data structure and methods that directly manipulate the underlying dense matrix representation.

    This class encapsulates all interactions with EJML's DMatrixRMaj and CommonOps_DDRM classes, providing a clean abstraction layer for dense matrix operations. It serves as the foundation for concrete dense matrix implementations.

    The class provides:

    • Basic matrix operations (get, set, dimensions)
    • Encapsulated CommonOps_DDRM method calls
    • Memory management for dense matrix data structures
    • Utility methods for matrix manipulation

    Dense matrices store all elements (including zeros) explicitly, making them suitable for matrices with a high percentage of non-zero elements or when dense linear algebra operations are required.

    Since:

    1.0

    Author:

    QORE Lab, Imperial College London

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected DMatrixRMaj data
    • Constructor Summary

      Constructors 
      Constructor Description
      DenseMatrix(int numRows, int numCols) Constructs a dense matrix with specified dimensions.
      DenseMatrix(DMatrixRMaj matrix) Constructs a dense matrix by copying an existing EJML dense matrix.
      DenseMatrix(DMatrix matrix) Constructs a dense matrix by copying an existing DMatrix.
      DenseMatrix(DenseMatrix matrix) Copy constructor for creating a new dense matrix from an existing one.
      DenseMatrix() Protected constructor for delayed initialization.
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
    • Method Summary

      Modifier and Type Method Description
      DMatrixRMaj getData() Returns the underlying EJML dense matrix data structure.
      void setData(DMatrixRMaj newData) Sets the underlying EJML dense matrix data structure.
      static void addMatricesInPlaceStatic(double alpha, DMatrix A, double beta, DMatrix B, DMatrix output)
      static DMatrixRMaj addMatricesStatic(double alpha, DMatrix A, double beta, DMatrix B, DMatrix output)
      static DMatrixRMaj addStatic(DMatrixRMaj A, DMatrixRMaj B, DMatrixRMaj output) Performs element-wise addition of two dense matrices: output = A + B.
      static void changeSignStatic(DMatrix input, DMatrix output)
      static double determinantStatic(DMatrix matrix)
      static DMatrixRMaj elementMultStatic(DMatrix A, DMatrix B, DMatrix output)
      static void extractMatrixStatic(DMatrix src, int srcX0, int srcX1, int srcY0, int srcY1, DMatrix dst, int dstY0, int dstX0)
      static void fillMatrixStatic(DMatrix matrix, double value)
      static DMatrixRMaj identityStatic(int size) Creates an identity matrix of the specified size.
      static void invertStatic(DMatrix input, DMatrix output)
      static DMatrixRMaj kronStatic(DMatrixRMaj A, DMatrixRMaj B, DMatrixRMaj output) Computes the Kronecker product of two dense matrices: A ⊗ B.
      static DMatrixRMaj multMatrixStatic(DMatrix A, DMatrix B, DMatrix output)
      static void scaleMatrixStatic(double scalar, DMatrix input, DMatrix output)
      static void transposeMatrixStatic(DMatrix input, DMatrix output)
      void absEq() Replaces each value in the matrix with its absolute value, in-place.
      void add(double alpha, DMatrix A, double beta, DMatrix B, DMatrix output) Performs the operation: output = alpha*A + beta*B.
      DMatrix addMatrices(double alpha, DMatrix A, double beta, DMatrix B, DMatrix output) Performs the operation: output = alpha*A + beta*B.
      void addMatricesInPlace(double alpha, DMatrix A, double beta, DMatrix B, DMatrix output)
      boolean any() Checks if the current matrix contains any non-zero elements.
      void applyConditionalTransform(double source, double target, double tol, String operation) Applies a transformation to matrix elements based on conditions.
      void changeSign()
      abstract BaseMatrix copy() Creates and returns a deep copy of this dense matrix.
      void colIncrease(int col, double a) Adds a scalar value to each element in the specified column.
      void concatColumnsInPlace(DMatrix left, DMatrix right, DMatrix output) Concatenates matrices column-wise.
      void concatRowsInPlace(DMatrix top, DMatrix bottom, DMatrix output) Concatenates matrices row-wise.
      BaseMatrix countEachRow(double val) Counts occurrences of each unique value in each row.
      abstract BaseMatrix createNewInstance(int rows, int cols, int nzLength) Creates a new instance of the concrete dense matrix implementation.
      double determinant()
      void divideInPlace(double scalar)
      void divideMatrix(double scalar, DMatrix output)
      void divideRowsByArray(Array<double> diag, int offset)
      void divideScalarByMatrix(double scalar, DMatrix output)
      double elementMax()
      double elementMaxAbs()
      double elementMin()
      DMatrix elementMult(DMatrix A, DMatrix B, DMatrix output)
      double elementSum()
      abstract boolean equals(Object obj) Indicates whether some other object is "equal to" this dense matrix.
      void extractMatrix(DMatrix src, int srcX0, int srcX1, int srcY0, int srcY1, DMatrix dst, int dstY0, int dstX0)
      void fillMatrix(double value) Fills the matrix with the specified value.
      BaseMatrix findNonNegative() Returns a matrix containing indices of all non-negative elements.
      double get(int row, int col) Returns the value at the specified matrix position.
      int getColumnIndex(int col)
      Array<int> getColumnIndicesArray()
      void setData(DMatrix newData) Sets the underlying EJML dense matrix data structure.
      void setData(Object newData) Sets the underlying data structure.
      int getNonZeroLength() Returns the number of stored non-zero elements.
      void setNonZeroLength(int length)
      int getNonZeroRow(int index)
      Array<int> getNonZeroRows() Returns array of row indices of non-zero entries.
      double getNonZeroValue(int index)
      Array<double> getNonZeroValues() Returns array of non-zero values.
      int getNonZeros() Returns the number of non-zero elements in the matrix.
      int getNumCols() Returns the number of columns in the matrix.
      int getNumRows() Returns the number of rows in the matrix.
      boolean hasDuplicates() Checks if the matrix contains duplicate values.
      boolean hasFinite() Checks if the matrix contains any finite (non-infinite, non-NaN) values.
      boolean hasInfinite() Checks if the matrix contains any infinite values.
      boolean hasMultipleFinite() Checks if more than one finite value exists.
      boolean hasNaN() Checks if the matrix contains any NaN values.
      void mult(DMatrix A, DMatrix B, DMatrix output) Performs matrix multiplication: output = A * B.
      DMatrix multMatrix(DMatrix B, DMatrix output) Performs matrix multiplication with sparse matrices.
      void removeZeros() Removes zero-valued elements from the matrix with default tolerance.
      void removeZerosWithTol(double tolerance) Removes zero-valued elements from the matrix with specified tolerance.
      void reshape(int numRows, int numCols) Reshapes the matrix to the specified dimensions.
      void scaleInPlace(double alpha) Scales the matrix in-place by the specified factor.
      void scaleMatrix(double scalar, DMatrix output)
      void set(int row, int col, double value) Sets the value at the specified matrix position.
      void setColumnIndex(int col, int value)
      void setNonZeroRow(int index, int row)
      void setNonZeroValue(int index, double value)
      void shrinkNumCols(int newmax) Reduce the maximum number of columns by setting the internal column count.
      void shrinkNumRows(int newmax) Reduce the maximum number of rows by setting the internal row count.
      DMatrix sumColsRaw()
      DMatrix sumRowsRaw()
      String toString() Returns a string representation of the matrix.
      void transpose(DMatrix input, DMatrix output) Computes the transpose of the input matrix.
      void transposeMatrix(DMatrix output) Computes the transpose of this matrix.
      void zero() Sets all elements in the matrix to zero.
      • Methods inherited from class jline.util.matrix.BaseMatrix

        getData, getNumNonZeros
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • DenseMatrix

        DenseMatrix(int numRows, int numCols)
        Constructs a dense matrix with specified dimensions.
        Parameters:
        numRows - the number of rows in the matrix
        numCols - the number of columns in the matrix
      • DenseMatrix

        DenseMatrix(DMatrixRMaj matrix)
        Constructs a dense matrix by copying an existing EJML dense matrix.
        Parameters:
        matrix - the EJML dense matrix to copy
      • DenseMatrix

        DenseMatrix(DMatrix matrix)
        Constructs a dense matrix by copying an existing DMatrix.
        Parameters:
        matrix - the DMatrix to copy
      • DenseMatrix

        DenseMatrix(DenseMatrix matrix)
        Copy constructor for creating a new dense matrix from an existing one.
        Parameters:
        matrix - the dense matrix to copy
      • DenseMatrix

        DenseMatrix()
        Protected constructor for delayed initialization.
    • Method Detail

      • getData

         DMatrixRMaj getData()

        Returns the underlying EJML dense matrix data structure. This method provides direct access to the internal data for subclasses.

        Returns:

        the underlying DMatrixRMaj instance

      • setData

         void setData(DMatrixRMaj newData)

        Sets the underlying EJML dense matrix data structure. This method allows subclasses to replace the internal data.

        Parameters:
        newData - the new DMatrixRMaj instance to use
      • addMatricesInPlaceStatic

         static void addMatricesInPlaceStatic(double alpha, DMatrix A, double beta, DMatrix B, DMatrix output)
      • addMatricesStatic

         static DMatrixRMaj addMatricesStatic(double alpha, DMatrix A, double beta, DMatrix B, DMatrix output)
      • addStatic

         static DMatrixRMaj addStatic(DMatrixRMaj A, DMatrixRMaj B, DMatrixRMaj output)

        Performs element-wise addition of two dense matrices: output = A + B.

        Parameters:
        A - the first matrix
        B - the second matrix
        output - the result matrix (can be null to create a new matrix)
        Returns:

        the sum matrix

      • changeSignStatic

         static void changeSignStatic(DMatrix input, DMatrix output)
      • elementMultStatic

         static DMatrixRMaj elementMultStatic(DMatrix A, DMatrix B, DMatrix output)
      • extractMatrixStatic

         static void extractMatrixStatic(DMatrix src, int srcX0, int srcX1, int srcY0, int srcY1, DMatrix dst, int dstY0, int dstX0)
      • identityStatic

         static DMatrixRMaj identityStatic(int size)

        Creates an identity matrix of the specified size. An identity matrix has ones on the main diagonal and zeros elsewhere.

        Parameters:
        size - the number of rows and columns in the identity matrix
        Returns:

        a new identity matrix

      • invertStatic

         static void invertStatic(DMatrix input, DMatrix output)
      • kronStatic

         static DMatrixRMaj kronStatic(DMatrixRMaj A, DMatrixRMaj B, DMatrixRMaj output)

        Computes the Kronecker product of two dense matrices: A ⊗ B. The Kronecker product creates a larger matrix where each element of A is multiplied by the entire matrix B.

        Parameters:
        A - the left matrix
        B - the right matrix
        output - the result matrix (can be null to create a new matrix)
        Returns:

        the Kronecker product matrix

      • multMatrixStatic

         static DMatrixRMaj multMatrixStatic(DMatrix A, DMatrix B, DMatrix output)
      • scaleMatrixStatic

         static void scaleMatrixStatic(double scalar, DMatrix input, DMatrix output)
      • absEq

         void absEq()

        Replaces each value in the matrix with its absolute value, in-place.

      • add

         void add(double alpha, DMatrix A, double beta, DMatrix B, DMatrix output)

        Performs the operation: output = alpha*A + beta*B.

        Parameters:
        alpha - scalar coefficient for matrix A
        A - first input matrix
        beta - scalar coefficient for matrix B
        B - second input matrix
        output - the result matrix
      • addMatrices

         DMatrix addMatrices(double alpha, DMatrix A, double beta, DMatrix B, DMatrix output)

        Performs the operation: output = alpha*A + beta*B.

        Parameters:
        alpha - scalar coefficient for matrix A
        A - first input matrix
        beta - scalar coefficient for matrix B
        B - second input matrix
        output - the result matrix
      • addMatricesInPlace

         void addMatricesInPlace(double alpha, DMatrix A, double beta, DMatrix B, DMatrix output)
      • any

         boolean any()

        Checks if the current matrix contains any non-zero elements.

        Returns:

        true if at least one non-zero element exists; false otherwise.

      • applyConditionalTransform

         void applyConditionalTransform(double source, double target, double tol, String operation)

        Applies a transformation to matrix elements based on conditions. Replaces elements matching the condition with the target value.

      • copy

         abstract BaseMatrix copy()

        Creates and returns a deep copy of this dense matrix.

        Returns:

        a new dense matrix that is a copy of this instance

      • colIncrease

         void colIncrease(int col, double a)

        Adds a scalar value to each element in the specified column.

        Parameters:
        col - Column index to update.
        a - Scalar value to add to each element in the column.
      • concatColumnsInPlace

         void concatColumnsInPlace(DMatrix left, DMatrix right, DMatrix output)

        Concatenates matrices column-wise.

        Parameters:
        left - the left matrix
        right - the right matrix
        output - the result matrix
      • concatRowsInPlace

         void concatRowsInPlace(DMatrix top, DMatrix bottom, DMatrix output)

        Concatenates matrices row-wise.

        Parameters:
        top - the top matrix
        bottom - the bottom matrix
        output - the result matrix
      • createNewInstance

         abstract BaseMatrix createNewInstance(int rows, int cols, int nzLength)

        Creates a new instance of the concrete dense matrix implementation. This factory method allows the base class to create instances of the correct subtype.

        Parameters:
        rows - the number of rows for the new matrix
        cols - the number of columns for the new matrix
        Returns:

        a new instance of the concrete dense matrix type

      • divideMatrix

         void divideMatrix(double scalar, DMatrix output)
      • elementMult

         DMatrix elementMult(DMatrix A, DMatrix B, DMatrix output)
      • equals

         abstract boolean equals(Object obj)

        Indicates whether some other object is "equal to" this dense matrix.

        Parameters:
        obj - the reference object with which to compare
        Returns:

        true if this object is the same as the obj argument; false otherwise

      • extractMatrix

         void extractMatrix(DMatrix src, int srcX0, int srcX1, int srcY0, int srcY1, DMatrix dst, int dstY0, int dstX0)
      • fillMatrix

         void fillMatrix(double value)

        Fills the matrix with the specified value.

        Parameters:
        value - the value to fill the matrix with
      • get

         double get(int row, int col)

        Returns the value at the specified matrix position.

        Parameters:
        row - the row index
        col - the column index
        Returns:

        the value at position (row, col)

      • setData

         void setData(DMatrix newData)

        Sets the underlying EJML dense matrix data structure. This method allows subclasses to replace the internal data.

        Parameters:
        newData - the new DMatrix instance to use
      • setData

         void setData(Object newData)

        Sets the underlying data structure. Implementation depends on the concrete matrix type.

        Parameters:
        newData - the new data structure
      • getNonZeroLength

         int getNonZeroLength()

        Returns the number of stored non-zero elements. This may be different from actual non-zero count for dense matrices.

        Returns:

        number of stored non-zero elements

      • getNonZeroRows

         Array<int> getNonZeroRows()

        Returns array of row indices of non-zero entries. For dense matrices, this may compute row indices dynamically. For sparse matrices, this returns the compressed row index array.

        Returns:

        array of row indices

      • getNonZeroValues

         Array<double> getNonZeroValues()

        Returns array of non-zero values. For dense matrices, this may return all values or only actual non-zeros. For sparse matrices, this returns the compressed storage array.

        Returns:

        array of non-zero values

      • getNonZeros

         int getNonZeros()

        Returns the number of non-zero elements in the matrix. For dense matrices, this counts actual non-zero values. For sparse matrices, this returns the stored non-zero count.

        Returns:

        the number of non-zero elements

      • getNumCols

         int getNumCols()

        Returns the number of columns in the matrix.

        Returns:

        the number of columns

      • getNumRows

         int getNumRows()

        Returns the number of rows in the matrix.

        Returns:

        the number of rows

      • hasDuplicates

         boolean hasDuplicates()

        Checks if the matrix contains duplicate values.

        Returns:

        true if duplicates exist

      • hasFinite

         boolean hasFinite()

        Checks if the matrix contains any finite (non-infinite, non-NaN) values.

        Returns:

        true if at least one finite value exists

      • hasInfinite

         boolean hasInfinite()

        Checks if the matrix contains any infinite values.

        Returns:

        true if at least one infinite value exists

      • hasMultipleFinite

         boolean hasMultipleFinite()

        Checks if more than one finite value exists.

        Returns:

        true if more than one finite value exists

      • hasNaN

         boolean hasNaN()

        Checks if the matrix contains any NaN values.

        Returns:

        true if at least one NaN exists

      • mult

         void mult(DMatrix A, DMatrix B, DMatrix output)

        Performs matrix multiplication: output = A * B.

        Parameters:
        A - the left matrix
        B - the right matrix
        output - the result matrix
      • multMatrix

         DMatrix multMatrix(DMatrix B, DMatrix output)

        Performs matrix multiplication with sparse matrices.

        Parameters:
        B - the right matrix
        output - the result matrix
      • removeZeros

         void removeZeros()

        Removes zero-valued elements from the matrix with default tolerance. For dense matrices, this may be a no-op or convert to sparse representation.

      • removeZerosWithTol

         void removeZerosWithTol(double tolerance)

        Removes zero-valued elements from the matrix with specified tolerance.

        Parameters:
        tolerance - the tolerance for considering values as zero
      • reshape

         void reshape(int numRows, int numCols)

        Reshapes the matrix to the specified dimensions.

        Parameters:
        numRows - the new number of rows
        numCols - the new number of columns
      • scaleInPlace

         void scaleInPlace(double alpha)

        Scales the matrix in-place by the specified factor.

        Parameters:
        alpha - the scaling factor
      • scaleMatrix

         void scaleMatrix(double scalar, DMatrix output)
      • set

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

        Sets the value at the specified matrix position.

        Parameters:
        row - the row index
        col - the column index
        value - the value to set
      • shrinkNumCols

         void shrinkNumCols(int newmax)

        Reduce the maximum number of columns by setting the internal column count.

        Parameters:
        newmax - the new maximum number of columns
      • shrinkNumRows

         void shrinkNumRows(int newmax)

        Reduce the maximum number of rows by setting the internal row count.

        Parameters:
        newmax - the new maximum number of rows
      • toString

         String toString()

        Returns a string representation of the matrix. Elements are formatted to 4 decimal places in a grid layout.

        Returns:

        a formatted string representation of the matrix

      • transpose

         void transpose(DMatrix input, DMatrix output)

        Computes the transpose of the input matrix.

        Parameters:
        input - the matrix to transpose
        output - the transposed matrix
      • transposeMatrix

         void transposeMatrix(DMatrix output)

        Computes the transpose of this matrix.

        Parameters:
        output - the transposed matrix
      • zero

         void zero()

        Sets all elements in the matrix to zero.