Class SparseMatrix

java.lang.Object
de.labathome.trustconstr.matrix.Matrix
de.labathome.trustconstr.matrix.SparseMatrix

public class SparseMatrix extends Matrix
Sparse matrix backed by a Dictionary-of-Keys (DOK) representation: a HashMap keyed by the encoded (row, col) index. Supports cheap incremental setAsDouble(double, long, long) (the equivalent of scipy's lil_matrix build phase) and converts to CSRMatrix for compute-heavy paths via toCSR().

Compute kernels (mtimes(Matrix), plus(Matrix) etc.) materialise to dense or convert to CSR internally -- no attempt is made to keep results sparse. The trust-constr port only relies on sparsity in a handful of well-known places (Jacobian storage and the AUGMENTED_SYSTEM KKT factorization, both of which already drop into the CSR fast paths).

  • Constructor Details

    • SparseMatrix

      public SparseMatrix(int rows, int cols)
      Allocate a fresh empty sparse matrix.
      Parameters:
      rows - number of rows
      cols - number of columns
  • Method Details

    • isSparse

      public boolean isSparse()
      Overrides:
      isSparse in class Matrix
      Returns:
      true if this is a sparse matrix
    • getRowCount

      public long getRowCount()
      Specified by:
      getRowCount in class Matrix
      Returns:
      number of rows
    • getColumnCount

      public long getColumnCount()
      Specified by:
      getColumnCount in class Matrix
      Returns:
      number of columns
    • getAsDouble

      public double getAsDouble(long row, long col)
      Specified by:
      getAsDouble in class Matrix
      Parameters:
      row - 0-based row index
      col - 0-based column index
      Returns:
      entry at (row, col)
    • setAsDouble

      public void setAsDouble(double v, long row, long col)
      Specified by:
      setAsDouble in class Matrix
      Parameters:
      v - value to store
      row - 0-based row index
      col - 0-based column index
    • nnz

      public int nnz()
      Returns:
      number of stored (non-zero) entries
    • toCSR

      public CSRMatrix toCSR()
      Materialise this sparse matrix to a CSRMatrix (canonical CSR form: column indices sorted within each row, no explicit zeros).
      Returns:
      a CSR view of the same content
    • copy

      public Matrix copy()
      Specified by:
      copy in class Matrix
      Returns:
      a deep copy of this matrix
    • mtimes

      public Matrix mtimes(Matrix other)
      Specified by:
      mtimes in class Matrix
      Parameters:
      other - right operand
      Returns:
      matrix product this * other
    • plus

      public Matrix plus(Matrix other)
      Specified by:
      plus in class Matrix
      Parameters:
      other - right operand
      Returns:
      element-wise sum this + other
    • minus

      public Matrix minus(Matrix other)
      Specified by:
      minus in class Matrix
      Parameters:
      other - right operand
      Returns:
      element-wise difference this - other
    • times

      public Matrix times(double alpha)
      Specified by:
      times in class Matrix
      Parameters:
      alpha - scalar
      Returns:
      alpha * this
    • times

      public Matrix times(Matrix elementwise)
      Specified by:
      times in class Matrix
      Parameters:
      elementwise - same-shape factor
      Returns:
      Hadamard product this .* elementwise
    • divide

      public Matrix divide(double alpha)
      Specified by:
      divide in class Matrix
      Parameters:
      alpha - scalar
      Returns:
      this / alpha
    • divide

      public Matrix divide(Matrix elementwise)
      Specified by:
      divide in class Matrix
      Parameters:
      elementwise - same-shape divisor
      Returns:
      Hadamard division this ./ elementwise
    • transpose

      public Matrix transpose()
      Specified by:
      transpose in class Matrix
      Returns:
      the transpose of this matrix
    • abs

      public Matrix abs()
      Specified by:
      abs in class Matrix
      Returns:
      element-wise absolute value as a fresh matrix
    • norm2

      public double norm2()
      Specified by:
      norm2 in class Matrix
      Returns:
      Frobenius norm
    • normInf

      public double normInf()
      Specified by:
      normInf in class Matrix
      Returns:
      infinity-norm (largest absolute entry)