Class BaseMatrix

java.lang.Object
jline.util.matrix.BaseMatrix
All Implemented Interfaces:
Serializable
Direct Known Subclasses:
DenseMatrix, SparseMatrix

public abstract class BaseMatrix extends Object implements Serializable
Common base class for matrix implementations, providing a unified interface for both dense and sparse matrix operations.

This abstract class defines the common contract that all matrix implementations must support, enabling the delegation pattern in Matrix.java to work with either dense or sparse underlying representations.

The class provides:

  • Basic matrix operations (get, set, dimensions)
  • Common utility methods for matrix manipulation
  • Abstract methods that must be implemented by concrete subclasses
  • Factory methods for creating new instances
Since:
1.0
Author:
QORE Lab, Imperial College London
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    abstract void
    Replaces each value in the matrix with its absolute value, in-place.
    protected abstract org.ejml.data.DMatrix
    addMatrices(double alpha, org.ejml.data.DMatrix A, double beta, org.ejml.data.DMatrix B, org.ejml.data.DMatrix output)
    Performs the operation: output = alpha*A + beta*B.
    abstract boolean
    any()
    Checks if the current matrix contains any non-zero elements.
    protected abstract void
    applyConditionalTransform(double source, double target, double tol, String operation)
    Applies a transformation to matrix elements based on conditions.
    protected abstract void
     
    abstract void
    colIncrease(int col, double a)
    Adds a scalar value to each element in the specified column.
    protected abstract void
    concatColumnsInPlace(org.ejml.data.DMatrix left, org.ejml.data.DMatrix right, org.ejml.data.DMatrix output)
    Concatenates matrices column-wise.
    protected abstract void
    concatRowsInPlace(org.ejml.data.DMatrix top, org.ejml.data.DMatrix bottom, org.ejml.data.DMatrix output)
    Concatenates matrices row-wise.
    abstract BaseMatrix
    Creates and returns a deep copy of this matrix.
    abstract BaseMatrix
    countEachRow(double val)
    Counts occurrences of each unique value in each row.
    protected abstract BaseMatrix
    createNewInstance(int rows, int cols, int nzLength)
    Creates a new instance of the concrete matrix implementation.
    protected abstract double
     
    protected abstract void
    divideInPlace(double scalar)
     
    protected abstract void
    divideMatrix(double scalar, org.ejml.data.DMatrix output)
     
    protected abstract void
    divideRowsByArray(double[] diag, int offset)
     
    protected abstract void
    divideScalarByMatrix(double scalar, org.ejml.data.DMatrix output)
     
    protected abstract double
     
    protected abstract double
     
    protected abstract double
     
    protected abstract org.ejml.data.DMatrix
    elementMult(org.ejml.data.DMatrix A, org.ejml.data.DMatrix B, org.ejml.data.DMatrix output)
     
    protected abstract double
     
    abstract boolean
    Indicates whether some other object is "equal to" this matrix.
    protected abstract void
    extractMatrix(org.ejml.data.DMatrix src, int srcX0, int srcX1, int srcY0, int srcY1, org.ejml.data.DMatrix dst, int dstY0, int dstX0)
     
    protected abstract void
    fillMatrix(double value)
    Fills the matrix with the specified value.
    abstract BaseMatrix
    Returns a matrix containing indices of all non-negative elements.
    abstract double
    get(int row, int col)
    Returns the value at the specified matrix position.
    protected abstract int
    getColumnIndex(int col)
     
    protected abstract int[]
     
    protected abstract Object
    Returns the underlying data structure.
    abstract int
    Returns the number of stored non-zero elements.
    protected abstract int
    getNonZeroRow(int index)
     
    abstract int[]
    Returns array of row indices of non-zero entries.
    abstract int
    Returns the number of non-zero elements in the matrix.
    protected abstract double
    getNonZeroValue(int index)
     
    abstract double[]
    Returns array of non-zero values.
    abstract int
    Returns the number of columns in the matrix.
    int
    Returns the number of non-zero elements in the matrix.
    abstract int
    Returns the number of rows in the matrix.
    abstract boolean
    Checks if the matrix contains duplicate values.
    abstract boolean
    Checks if the matrix contains any finite (non-infinite, non-NaN) values.
    abstract boolean
    Checks if the matrix contains any infinite values.
    abstract boolean
    Checks if more than one finite value exists.
    abstract boolean
    Checks if the matrix contains any NaN values.
    protected abstract org.ejml.data.DMatrix
    multMatrix(org.ejml.data.DMatrix B, org.ejml.data.DMatrix output)
    Performs matrix multiplication with sparse matrices.
    protected abstract void
    Removes zero-valued elements from the matrix with default tolerance.
    protected abstract void
    removeZerosWithTol(double tolerance)
    Removes zero-valued elements from the matrix with specified tolerance.
    abstract void
    reshape(int numRows, int numCols)
    Reshapes the matrix to the specified dimensions.
    protected abstract void
    scaleInPlace(double alpha)
    Scales the matrix in-place by the specified factor.
    protected abstract void
    scaleMatrix(double scalar, org.ejml.data.DMatrix output)
     
    abstract void
    set(int row, int col, double value)
    Sets the value at the specified matrix position.
    protected abstract void
    setColumnIndex(int col, int value)
     
    protected abstract void
    setData(Object newData)
    Sets the underlying data structure.
    protected abstract void
    setNonZeroLength(int length)
     
    protected abstract void
    setNonZeroRow(int index, int row)
     
    protected abstract void
    setNonZeroValue(int index, double value)
     
    abstract void
    shrinkNumCols(int newmax)
    Reduce the maximum number of columns by setting the internal column count.
    abstract void
    shrinkNumRows(int newmax)
    Reduce the maximum number of rows by setting the internal row count.
    protected abstract org.ejml.data.DMatrix
     
    protected abstract org.ejml.data.DMatrix
     
    Returns a string representation of the matrix.
    protected abstract void
    transposeMatrix(org.ejml.data.DMatrix output)
    Computes the transpose of this matrix.
    abstract void
    Sets all elements in the matrix to zero.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • BaseMatrix

      public BaseMatrix()
  • Method Details

    • absEq

      public abstract void absEq()
      Replaces each value in the matrix with its absolute value, in-place.
    • addMatrices

      protected abstract org.ejml.data.DMatrix addMatrices(double alpha, org.ejml.data.DMatrix A, double beta, org.ejml.data.DMatrix B, org.ejml.data.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
    • any

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

      protected abstract 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.
    • changeSign

      protected abstract void changeSign()
    • copy

      public abstract BaseMatrix copy()
      Creates and returns a deep copy of this matrix.
      Returns:
      a new matrix that is a copy of this instance
    • colIncrease

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

      protected abstract void concatColumnsInPlace(org.ejml.data.DMatrix left, org.ejml.data.DMatrix right, org.ejml.data.DMatrix output)
      Concatenates matrices column-wise.
      Parameters:
      left - the left matrix
      right - the right matrix
      output - the result matrix
    • concatRowsInPlace

      protected abstract void concatRowsInPlace(org.ejml.data.DMatrix top, org.ejml.data.DMatrix bottom, org.ejml.data.DMatrix output)
      Concatenates matrices row-wise.
      Parameters:
      top - the top matrix
      bottom - the bottom matrix
      output - the result matrix
    • countEachRow

      public abstract BaseMatrix countEachRow(double val)
      Counts occurrences of each unique value in each row.
    • createNewInstance

      protected abstract BaseMatrix createNewInstance(int rows, int cols, int nzLength)
      Creates a new instance of the concrete 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
      nzLength - the initial capacity for non-zero elements (may be ignored by dense matrices)
      Returns:
      a new instance of the concrete matrix type
    • determinant

      protected abstract double determinant()
    • divideInPlace

      protected abstract void divideInPlace(double scalar)
    • divideMatrix

      protected abstract void divideMatrix(double scalar, org.ejml.data.DMatrix output)
    • divideRowsByArray

      protected abstract void divideRowsByArray(double[] diag, int offset)
    • divideScalarByMatrix

      protected abstract void divideScalarByMatrix(double scalar, org.ejml.data.DMatrix output)
    • elementMax

      protected abstract double elementMax()
    • elementMaxAbs

      protected abstract double elementMaxAbs()
    • elementMin

      protected abstract double elementMin()
    • elementMult

      protected abstract org.ejml.data.DMatrix elementMult(org.ejml.data.DMatrix A, org.ejml.data.DMatrix B, org.ejml.data.DMatrix output)
    • elementSum

      protected abstract double elementSum()
    • equals

      public abstract boolean equals(Object obj)
      Indicates whether some other object is "equal to" this matrix.
      Overrides:
      equals in class Object
      Parameters:
      obj - the reference object with which to compare
      Returns:
      true if this object is the same as the obj argument; false otherwise
    • extractMatrix

      protected abstract void extractMatrix(org.ejml.data.DMatrix src, int srcX0, int srcX1, int srcY0, int srcY1, org.ejml.data.DMatrix dst, int dstY0, int dstX0)
    • fillMatrix

      protected abstract void fillMatrix(double value)
      Fills the matrix with the specified value.
      Parameters:
      value - the value to fill the matrix with
    • findNonNegative

      public abstract BaseMatrix findNonNegative()
      Returns a matrix containing indices of all non-negative elements.
    • get

      public abstract 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)
    • getColumnIndex

      protected abstract int getColumnIndex(int col)
    • getColumnIndicesArray

      protected abstract int[] getColumnIndicesArray()
    • getData

      protected abstract Object getData()
      Returns the underlying data structure. For sparse matrices, returns DMatrix. For dense matrices, may return a wrapped DMatrixRMaj or converted sparse representation.
      Returns:
      the underlying data structure
    • setData

      protected abstract void setData(Object newData)
      Sets the underlying data structure. Implementation depends on the concrete matrix type.
      Parameters:
      newData - the new data structure
    • getNonZeroLength

      public abstract 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
    • setNonZeroLength

      protected abstract void setNonZeroLength(int length)
    • getNonZeroRow

      protected abstract int getNonZeroRow(int index)
    • getNonZeroRows

      public abstract 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
    • getNonZeroValue

      protected abstract double getNonZeroValue(int index)
    • getNonZeroValues

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

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

      public abstract int getNumCols()
      Returns the number of columns in the matrix.
      Returns:
      the number of columns
    • getNumNonZeros

      public int getNumNonZeros()
      Returns the number of non-zero elements in the matrix. This is an alias for getNonZeros().
      Returns:
      the number of non-zero elements
    • getNumRows

      public abstract int getNumRows()
      Returns the number of rows in the matrix.
      Returns:
      the number of rows
    • hasDuplicates

      public abstract boolean hasDuplicates()
      Checks if the matrix contains duplicate values.
      Returns:
      true if duplicates exist
    • hasFinite

      public abstract boolean hasFinite()
      Checks if the matrix contains any finite (non-infinite, non-NaN) values.
      Returns:
      true if at least one finite value exists
    • hasInfinite

      public abstract boolean hasInfinite()
      Checks if the matrix contains any infinite values.
      Returns:
      true if at least one infinite value exists
    • hasMultipleFinite

      public abstract boolean hasMultipleFinite()
      Checks if more than one finite value exists.
      Returns:
      true if more than one finite value exists
    • hasNaN

      public abstract boolean hasNaN()
      Checks if the matrix contains any NaN values.
      Returns:
      true if at least one NaN exists
    • multMatrix

      protected abstract org.ejml.data.DMatrix multMatrix(org.ejml.data.DMatrix B, org.ejml.data.DMatrix output)
      Performs matrix multiplication with sparse matrices.
      Parameters:
      B - the right matrix
      output - the result matrix
    • removeZeros

      protected abstract 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

      protected abstract void removeZerosWithTol(double tolerance)
      Removes zero-valued elements from the matrix with specified tolerance.
      Parameters:
      tolerance - the tolerance for considering values as zero
    • reshape

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

      protected abstract void scaleInPlace(double alpha)
      Scales the matrix in-place by the specified factor.
      Parameters:
      alpha - the scaling factor
    • scaleMatrix

      protected abstract void scaleMatrix(double scalar, org.ejml.data.DMatrix output)
    • set

      public abstract 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
    • setColumnIndex

      protected abstract void setColumnIndex(int col, int value)
    • setNonZeroRow

      protected abstract void setNonZeroRow(int index, int row)
    • setNonZeroValue

      protected abstract void setNonZeroValue(int index, double value)
    • shrinkNumCols

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

      public abstract void shrinkNumRows(int newmax)
      Reduce the maximum number of rows by setting the internal row count.
      Parameters:
      newmax - the new maximum number of rows
    • sumColsRaw

      protected abstract org.ejml.data.DMatrix sumColsRaw()
    • sumRowsRaw

      protected abstract org.ejml.data.DMatrix sumRowsRaw()
    • toString

      public String toString()
      Returns a string representation of the matrix. Elements are formatted to 4 decimal places in a grid layout.
      Overrides:
      toString in class Object
      Returns:
      a formatted string representation of the matrix
    • transposeMatrix

      protected abstract void transposeMatrix(org.ejml.data.DMatrix output)
      Computes the transpose of this matrix.
      Parameters:
      output - the transposed matrix
    • zero

      public abstract void zero()
      Sets all elements in the matrix to zero.