Class CSRMatrix

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

public final class CSRMatrix extends Object
Compressed Sparse Row matrix. Storage analogue of scipy.sparse.csr_array: three parallel primitive arrays (indptr, indices, data) together with the logical shape.
 indptr.length == rows + 1
 indptr[i] .. indptr[i+1] is the half-open range of indices/data
     entries belonging to row i.
 indices[k] is the column of the k-th stored entry (0 ≤ indices[k] < cols).
 data[k]    is the value of the k-th stored entry.
 
Within a row the entries are kept sorted by column index -- this matches the canonical form scipy returns from csr_array(...) after sum_duplicates() / sort_indices(), and several operations (e.g. toDense(), matvec(double[])) rely on it being safe to walk the row range linearly. Instances are immutable from the outside: the constructor copies its inputs.
See Also:
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static final class 
    Incremental builder for CSRMatrix using a Dictionary-of-Keys (DOK) representation under the hood.
  • Constructor Summary

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

    Modifier and Type
    Method
    Description
    builder(int rows, int cols)
    Start an incremental DOK-style build.
    int
     
    double[]
     
    static CSRMatrix
    eye(int n)
    n x n identity in CSR.
    static CSRMatrix
    fromDense(double[][] dense)
    Build a CSR matrix from a row-major dense double[][].
    static CSRMatrix
    Build from any Matrix.
    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 CSC of the same matrix (not the transpose).
    double[][]
    Materialize as a dense [rows][cols] array.
    Transpose: returns a CSC matrix with the same non-zeros, rows/columns swapped.

    Methods inherited from class java.lang.Object

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

    • CSRMatrix

      public CSRMatrix(int rows, int cols, int[] indptr, int[] indices, double[] data)
      Construct from raw CSR triplets. Inputs are defensively copied. The arrays must satisfy:
      • indptr.length == rows + 1, monotonically non-decreasing, starting at 0 and ending at indices.length.
      • indices.length == data.length (the number of stored entries).
      • 0 <= indices[k] < cols for every k.
      • Within each row, indices is strictly increasing.
      Parameters:
      rows - number of rows
      cols - number of columns
      indptr - row pointer array, length rows + 1
      indices - column 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:
      a defensive copy of the row pointer array.
    • indices

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

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

      public static CSRMatrix fromMatrix(Matrix m)
      Build from any Matrix. Routes sparse SparseMatrix sources directly to SparseMatrix.toCSR() (no dense materialisation); dense sources go through fromDense(double[][]).
      Parameters:
      m - source matrix
      Returns:
      CSR view of the same content
    • fromDense

      public static CSRMatrix fromDense(double[][] dense)
      Build a CSR matrix from a row-major dense double[][]. Zeros are dropped. The input must be rectangular.
      Parameters:
      dense - [m][n] row-major dense values
      Returns:
      CSR matrix with the same non-zero pattern as dense
    • toDense

      public double[][] toDense()
      Materialize as a dense [rows][cols] array. Newly allocated each call.
      Returns:
      fresh row-major dense view
    • eye

      public static CSRMatrix eye(int n)
      n x n identity in CSR.
      Parameters:
      n - dimension; must be non-negative
      Returns:
      the n x n identity matrix
      See Also:
    • 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 CSRMatrix multiply(double s)
      Element-wise scalar multiply (allocates a new matrix).
      Parameters:
      s - scalar factor
      Returns:
      s * this
    • transpose

      public CSCMatrix transpose()
      Transpose: returns a CSC matrix with the same non-zeros, rows/columns swapped. CSC is the natural output because a CSR row traversal of A is a CSC column traversal of A^T.
      Returns:
      A^T as a CSCMatrix
    • toCSC

      public CSCMatrix toCSC()
      Convert to CSC of the same matrix (not the transpose).
      Returns:
      CSC representation of this matrix
    • builder

      public static CSRMatrix.Builder builder(int rows, int cols)
      Start an incremental DOK-style build. See CSRMatrix.Builder.
      Parameters:
      rows - number of rows in the resulting matrix
      cols - number of columns in the resulting matrix
      Returns:
      a fresh CSRMatrix.Builder