Class CSCMatrix

java.lang.Object
de.labathome.trustconstr.sparse.CSCMatrix

public final class CSCMatrix extends Object
Compressed Sparse Column matrix. Storage analogue of scipy.sparse.csc_array. Layout mirrors CSRMatrix with rows and columns swapped:
 indptr.length == cols + 1
 indptr[j] .. indptr[j+1] is the half-open range of indices/data
     entries belonging to column j.
 indices[k] is the row of the k-th stored entry (0 ≤ indices[k] < rows).
 data[k]    is the value of the k-th stored entry.
 
Within a column the entries are kept sorted by row index. The CSC format is needed by trust-constr in two places:
  1. The KKT projection block (projections.py:99) is materialised in CSC because scipy.sparse.linalg.factorized prefers it.
  2. A^T of a CSR Jacobian is naturally CSC of the same data.
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    CSCMatrix(int rows, int cols, int[] indptr, int[] indices, double[] data)
    Construct from raw CSC triplets.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
     
    double[]
     
    static CSCMatrix
    eye(int n)
    n x n identity in CSC.
    static CSCMatrix
    fromDense(double[][] dense)
    Build CSC from a dense 2D array; column-major scan, but the input is still given as row-major dense[i][j].
    int[]
     
    int[]
     
    double[]
    matvec(double[] x)
    Matrix-vector product y = A x.
    multiply(double s)
    Element-wise scalar multiply (allocates a new matrix).
    int
    nnz()
     
    double[]
    rmatvec(double[] x)
    Transposed matrix-vector product y = A^T x.
    int
     
    Convert to CSR of the same matrix (not the transpose).
    double[][]
    Materialise as a dense [rows][cols] array.
    Transpose.

    Methods inherited from class java.lang.Object

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

    • CSCMatrix

      public CSCMatrix(int rows, int cols, int[] indptr, int[] indices, double[] data)
      Construct from raw CSC triplets. Inputs are defensively copied. See the class-level invariants for shape and ordering requirements.
      Parameters:
      rows - number of rows
      cols - number of columns
      indptr - column pointer array, length cols + 1
      indices - row index for each stored entry
      data - value for each stored entry
  • Method Details

    • rows

      public int rows()
      Returns:
      number of rows
    • cols

      public int cols()
      Returns:
      number of columns
    • nnz

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

      public int[] indptr()
      Returns:
      defensive copy of the column pointer array
    • indices

      public int[] indices()
      Returns:
      defensive copy of the row index array
    • data

      public double[] data()
      Returns:
      defensive copy of the value array
    • fromDense

      public static CSCMatrix fromDense(double[][] dense)
      Build CSC from a dense 2D array; column-major scan, but the input is still given as row-major dense[i][j].
      Parameters:
      dense - [m][n] row-major dense values
      Returns:
      CSC matrix with the same non-zero pattern as dense
    • toDense

      public double[][] toDense()
      Materialise as a dense [rows][cols] array.
      Returns:
      fresh row-major dense view
    • eye

      public static CSCMatrix eye(int n)
      n x n identity in CSC.
      Parameters:
      n - dimension; must be non-negative
      Returns:
      the n x n identity matrix
    • matvec

      public double[] matvec(double[] x)
      Matrix-vector product y = A x.
      Parameters:
      x - input vector of length cols()
      Returns:
      result vector of length rows()
    • rmatvec

      public double[] rmatvec(double[] x)
      Transposed matrix-vector product y = A^T x.
      Parameters:
      x - input vector of length rows()
      Returns:
      result vector of length cols()
    • multiply

      public CSCMatrix multiply(double s)
      Element-wise scalar multiply (allocates a new matrix).
      Parameters:
      s - scalar factor
      Returns:
      s * this
    • transpose

      public CSRMatrix transpose()
      Transpose. Same nnz, rows and columns swapped. The natural output is CSR because a CSC column traversal of A is a CSR row traversal of A^T.
      Returns:
      A^T as a CSRMatrix
    • toCSR

      public CSRMatrix toCSR()
      Convert to CSR of the same matrix (not the transpose).
      Returns:
      CSR representation of this matrix