Module ojalgo

Class R064CSR

java.lang.Object
org.ojalgo.matrix.store.R064CSR
All Implemented Interfaces:
Group, Group.Additive<MatrixStore<Double>>, NormedVectorSpace<MatrixStore<Double>,Double>, Operation, Operation.Addition<MatrixStore<Double>>, Operation.Multiplication<MatrixStore<Double>>, Operation.Subtraction<MatrixStore<Double>>, ScalarOperation, ScalarOperation.Addition<MatrixStore<Double>,Double>, ScalarOperation.Division<MatrixStore<Double>,Double>, ScalarOperation.Multiplication<MatrixStore<Double>,Double>, ScalarOperation.Subtraction<MatrixStore<Double>,Double>, VectorSpace<MatrixStore<Double>,Double>, Matrix2D<Double,MatrixStore<Double>>, ElementsSupplier<Double>, MatrixStore<Double>, SparseStructure2D, Access1D<Double>, Access1D.Aggregatable<Double>, Access1D.Collectable<Double,Mutate1D>, Access1D.Sliceable<Double>, Access1D.Visitable<Double>, Access2D<Double>, Access2D.Aggregatable<Double>, Access2D.Collectable<Double,TransformableRegion<Double>>, Access2D.Sliceable<Double>, Access2D.Visitable<Double>, Operate2D<Double,ElementsSupplier<Double>>, Structure1D, Structure2D, Structure2D.Logical<Access2D<Double>,MatrixStore<Double>>, Structure2D.ReducibleTo1D<ElementsSupplier<Double>>

public final class R064CSR extends Object
A compressed sparse row (CSR) matrix store implementation for double precision values. This format is efficient for row-wise operations and matrix-vector multiplication.

The CSR format uses three arrays to store the matrix:

  • values[] - stores the non-zero values
  • columnIndices[] - stores the column index for each non-zero value
  • rowPointers[] - stores the starting position in values/columnIndices arrays for each row

This format is particularly efficient for:

  • Row-wise access and operations
  • Matrix-vector multiplication
  • Iterating over non-zero elements row by row
Other sparse types are more dynamic and may be used as CSR builders. Each of SparseStore, RowsSupplier and ColumnsSupplier feature direct conversion to R064CSR. In particular RowsSupplier could be of interest in this case. Furthermore, R064CSR.Builder is provided for incrementally constructing a CSR matrix.
Author:
apete
  • Field Details

    • indices

      public final int[] indices
    • pointers

      public final int[] pointers
    • values

      public final double[] values
  • Constructor Details

    • R064CSR

      public R064CSR(int nbRows, int nbCols, double[] elementValues, int[] columnIndices, int[] rowPointers)
      Creates a new CSR matrix store.
      Parameters:
      nbRows - The number of rows in the matrix
      nbCols - The number of columns in the matrix
      elementValues - Array containing the non-zero values
      columnIndices - Array containing the column index for each non-zero value
      rowPointers - Array containing the starting position in values/columnIndices for each row
    • R064CSR

      public R064CSR(int nbRows, int nbCols, int capacity)
      Parameters:
      nbRows - The number of rows
      nbCols - The number of columns
      capacity - The maximum capacity (number of non-zero elements)
  • Method Details

    • newBuilder

      public static R064CSR.Builder newBuilder()
    • newBuilder

      public static R064CSR.Builder newBuilder(int nbRows, int nbCols)
    • copyCSR

      public R064CSR copyCSR()
      Creates a deep copy of this CSR matrix store.
    • doubleValue

      public double doubleValue(int row, int col)
      Gets the value at the specified row and column. Returns 0.0 if the element is not stored (i.e., is zero).
      Parameters:
      row - The row index
      col - The column index
      Returns:
      The value at the specified position, or 0.0 if not stored
    • firstInColumn

      public int firstInColumn(int col)
      Description copied from interface: Structure2D
      The default value is simply 0, and if all elements are zeros then this.countRows().
      Parameters:
      col - The column index
      Returns:
      The row index of the first non-zero element in the specified column
    • firstInRow

      public int firstInRow(int row)
      Description copied from interface: Structure2D
      The default value is simply 0, and if all elements are zeros then this.countColumns().
      Returns:
      The column index of the first non-zero element in the specified row
    • limitOfColumn

      public int limitOfColumn(int col)
      Description copied from interface: Structure2D
      The default value is simply this.countRows(), and if all elements are zeros then 0.
      Specified by:
      limitOfColumn in interface Structure2D
      Returns:
      The row index of the first zero element, after all non-zeros, in the specified column (index of the last non-zero + 1)
    • limitOfRow

      public int limitOfRow(int row)
      Description copied from interface: Structure2D
      The default value is simply this.countColumns(), and if all elements are zeros then 0.
      Specified by:
      limitOfRow in interface Structure2D
      Returns:
      The column index of the first zero element, after all non-zeros, in the specified row (index of the last non-zero + 1)
    • multiply

      public void multiply(Access1D<Double> right, TransformableRegion<Double> target)
      Performs matrix-vector multiplication using the CSR format. This implementation is optimized for the CSR format by iterating over non-zero elements row by row and accumulating the results.
      Parameters:
      right - The vector to multiply with
      target - The target vector to store the result
    • nonzeros

      public R064CSR.NonZeroView nonzeros()
      Description copied from interface: Access1D
      Similar to Access1D.elements() but avoids elements that are structurally known to be zero. (That does not eliminate all zero-values from this view.) With an arbitrary (dense) unstructured implementation the Access1D.nonzeros() and Access1D.elements() methods do the same thing! Only some specific implementations are able to actually exploit structure/sparsity to view fewer elements.
    • toCSC

      public R064CSC toCSC()
    • toCSR

      public R064CSR toCSR()
    • transpose

      public R064CSC transpose()
      Returns:
      A transposed matrix instance.
    • capacity

      public final int capacity()
    • countNonzeros

      public final int countNonzeros()
      Specified by:
      countNonzeros in interface SparseStructure2D
    • get

      public final Double get(int row, int col)
      Specified by:
      get in interface MatrixStore<Double>
    • equals

      public boolean equals(Object obj)
    • hashCode

      public int hashCode()
    • physical

      public final PhysicalStore.Factory<Double,?> physical()
    • countColumns

      public long countColumns()
      Description copied from interface: Structure2D
      Only need to implement if the structure may contain more than Integer.MAX_VALUE elements.
      Specified by:
      countColumns in interface Structure2D
      Returns:
      The number of columns
    • countRows

      public long countRows()
      Description copied from interface: Structure2D
      Only need to implement if the structure may contain more than Integer.MAX_VALUE elements.
      Specified by:
      countRows in interface Structure2D
      Returns:
      The number of rows
    • getColDim

      public final int getColDim()
      Specified by:
      getColDim in interface Structure2D
      Returns:
      The number of columns
    • getMaxDim

      public final int getMaxDim()
      Specified by:
      getMaxDim in interface Structure2D
    • getMinDim

      public final int getMinDim()
      Specified by:
      getMinDim in interface Structure2D
    • getRowDim

      public final int getRowDim()
      Specified by:
      getRowDim in interface Structure2D
      Returns:
      The number of rows
    • multiplyBoth

      public Double multiplyBoth(Access1D<Double> leftAndRight)
      Description copied from interface: MatrixStore
      Assumes [leftAndRight] is a vector and will calulate [leftAndRight]H[this][leftAndRight]
      Specified by:
      multiplyBoth in interface MatrixStore<N extends Comparable<N>>
      Parameters:
      leftAndRight - The argument vector
      Returns:
      A scalar (extracted from the resulting 1 x 1 matrix)
    • toString

      public final String toString()
      Overrides:
      toString in class Object
    • isPrimitive

      protected final boolean isPrimitive()