Class CsrRingMatrix<T extends Ring<T>>

  • Constructor Details

    • CsrRingMatrix

      public CsrRingMatrix(Shape shape, T[] entries, int[] rowPointers, int[] colIndices)
      Creates a sparse CSR matrix with the specified shape, non-zero data, row pointers, and non-zero column indices.
      Parameters:
      shape - Shape of this tensor.
      entries - The non-zero data of this CSR matrix.
      rowPointers - The row pointers for the non-zero values in the sparse CSR matrix.

      rowPointers[i] indicates the starting index within data and colData of all values in row i.

      colIndices - Column indices for each non-zero value in this sparse CSR matrix. Must satisfy data.length == colData.length.
    • CsrRingMatrix

      public CsrRingMatrix(Shape shape, List<T> entries, List<Integer> rowPointers, List<Integer> colIndices)
      Creates a sparse CSR matrix with the specified shape, non-zero data, row pointers, and non-zero column indices.
      Parameters:
      shape - Shape of this tensor.
      entries - The non-zero data of this CSR matrix.
      rowPointers - The row pointers for the non-zero values in the sparse CSR matrix.

      rowPointers[i] indicates the starting index within data and colData of all values in row i.

      colIndices - Column indices for each non-zero value in this sparse CSR matrix. Must satisfy data.length == colData.length.
    • CsrRingMatrix

      public CsrRingMatrix(Shape shape, T ringElement)
      Constructs a sparse CSR matrix representing the zero matrix for the field which ringElement belongs to.
      Parameters:
      shape - Shape of the CSR matrix to construct.
      ringElement - Element of the field which the entries of this
  • Method Details

    • makeLikeTensor

      public CsrRingMatrix<T> makeLikeTensor(Shape shape, T[] entries, int[] rowPointers, int[] colIndices)
      Constructs a sparse CSR tensor of the same type as this tensor with the specified non-zero data and indices.
      Specified by:
      makeLikeTensor in class AbstractCsrSemiringMatrix<CsrRingMatrix<T extends Ring<T>>,RingMatrix<T extends Ring<T>>,CooRingVector<T extends Ring<T>>,T extends Ring<T>>
      Parameters:
      shape - Shape of the matrix.
      entries - Non-zero data of the CSR matrix.
      rowPointers - Row pointers for the non-zero values in the CSR matrix.
      colIndices - Non-zero column indices of the CSR matrix.
      Returns:
      A sparse CSR tensor of the same type as this tensor with the specified non-zero data and indices.
    • makeLikeTensor

      public CsrRingMatrix<T> makeLikeTensor(Shape shape, List<T> entries, List<Integer> rowPointers, List<Integer> colIndices)
      Constructs a CSR matrix with the specified shape, non-zero data, and non-zero indices.
      Specified by:
      makeLikeTensor in class AbstractCsrSemiringMatrix<CsrRingMatrix<T extends Ring<T>>,RingMatrix<T extends Ring<T>>,CooRingVector<T extends Ring<T>>,T extends Ring<T>>
      Parameters:
      shape - Shape of the matrix.
      entries - Non-zero values of the CSR matrix.
      rowPointers - Row pointers for the non-zero values in the CSR matrix.
      colIndices - Non-zero column indices of the CSR matrix.
      Returns:
      A CSR matrix with the specified shape, non-zero data, and non-zero indices.
    • makeLikeDenseTensor

      public RingMatrix<T> makeLikeDenseTensor(Shape shape, T[] entries)
      Constructs a dense matrix which is of a similar type to this sparse CSR matrix.
      Specified by:
      makeLikeDenseTensor in class AbstractCsrSemiringMatrix<CsrRingMatrix<T extends Ring<T>>,RingMatrix<T extends Ring<T>>,CooRingVector<T extends Ring<T>>,T extends Ring<T>>
      Parameters:
      shape - Shape of the dense matrix.
      entries - Entries of the dense matrix.
      Returns:
      A dense matrix which is of a similar type to this sparse CSR matrix with the specified shape and data.
    • makeLikeCooMatrix

      public CooRingMatrix<T> makeLikeCooMatrix(Shape shape, T[] entries, int[] rowIndices, int[] colIndices)

      Constructs a sparse COO matrix of a similar type to this sparse CSR matrix.

      Note: this method constructs a new COO matrix with the specified data and indices. It does not convert this matrix to a CSR matrix. To convert this matrix to a sparse COO matrix use toCoo().

      Specified by:
      makeLikeCooMatrix in class AbstractCsrSemiringMatrix<CsrRingMatrix<T extends Ring<T>>,RingMatrix<T extends Ring<T>>,CooRingVector<T extends Ring<T>>,T extends Ring<T>>
      Parameters:
      shape - Shape of the COO matrix.
      entries - Non-zero data of the COO matrix.
      rowIndices - Non-zero row indices of the sparse COO matrix.
      colIndices - Non-zero column indices of the Sparse COO matrix.
      Returns:
      A sparse COO matrix of a similar type to this sparse CSR matrix.
    • toCoo

      public CooRingMatrix<T> toCoo()
      Converts this sparse CSR matrix to an equivalent sparse COO matrix.
      Specified by:
      toCoo in class AbstractCsrSemiringMatrix<CsrRingMatrix<T extends Ring<T>>,RingMatrix<T extends Ring<T>>,CooRingVector<T extends Ring<T>>,T extends Ring<T>>
      Returns:
      A sparse COO matrix equivalent to this sparse CSR matrix.
    • makeLikeTensor

      public CsrRingMatrix<T> makeLikeTensor(Shape shape, T[] entries)
      Constructs a tensor of the same type as this tensor with the given the shape and data. The resulting tensor will also have the same non-zero indices as this tensor.
      Specified by:
      makeLikeTensor in interface TensorOverSemiring<CsrRingMatrix<T extends Ring<T>>,RingMatrix<T extends Ring<T>>,T extends Ring<T>[],T extends Ring<T>>
      Specified by:
      makeLikeTensor in class AbstractTensor<CsrRingMatrix<T extends Ring<T>>,T extends Ring<T>[],T extends Ring<T>>
      Parameters:
      shape - Shape of the tensor to construct.
      entries - Entries of the tensor to construct.
      Returns:
      A tensor of the same type and with the same non-zero indices as this tensor with the given the shape and data.
    • tensorDot

      public RingTensor<T> tensorDot(CsrRingMatrix<T> src2, int[] aAxes, int[] bAxes)
      Computes the tensor contraction of this tensor with a specified tensor over the specified set of axes. That is, computes the sum of products between the two tensors along the specified set of axes.
      Parameters:
      src2 - Tensor to contract with this tensor.
      aAxes - Axes along which to compute products for this tensor.
      bAxes - Axes along which to compute products for src2 tensor.
      Returns:
      The tensor dot product over the specified axes.
      Throws:
      IllegalArgumentException - If the two tensors shapes do not match along the specified axes pairwise in aAxes and bAxes.
      IllegalArgumentException - If aAxes and bAxes do not match in length, or if any of the axes are out of bounds for the corresponding tensor.
    • mult

      public RingVector<T> mult(CooRingVector<T> b)
      Computes the matrix-vector multiplication of a vector with this matrix.
      Parameters:
      b - Vector in the matrix-vector multiplication.
      Returns:
      The result of multiplying this matrix with b.
      Throws:
      LinearAlgebraException - If the number of columns in this matrix do not equal the size of b.
    • getRow

      public CooRingVector<T> getRow(int rowIdx, int colStart, int colEnd)
      Gets a range of a row of this matrix.
      Parameters:
      rowIdx - The index of the row to get.
      colStart - The staring column of the row range to get (inclusive).
      colEnd - The ending column of the row range to get (exclusive).
      Returns:
      A vector containing the elements of the specified row over the range [colStart, colEnd).
      Throws:
      IllegalArgumentException - If rowIdx < 0 || rowIdx >= this.numRows() or colStart < 0 || colStart >= numCols or colEnd < colStart || colEnd > numCols.
    • getCol

      public CooRingVector<T> getCol(int colIdx, int rowStart, int rowEnd)
      Gets a range of a column of this matrix.
      Parameters:
      colIdx - The index of the column to get.
      rowStart - The staring row of the column range to get (inclusive).
      rowEnd - The ending row of the column range to get (exclusive).
      Returns:
      A vector containing the elements of the specified column over the range [rowStart, rowEnd).
      Throws:
      IllegalArgumentException - If colIdx < 0 || colIdx >= this.numCols() or rowStart < 0 || rowStart >= numRows or rowEnd < rowStart || rowEnd > numRows.
    • getDiag

      public CooRingVector<T> getDiag(int diagOffset)
      Gets the elements of this matrix along the specified diagonal.
      Parameters:
      diagOffset - The diagonal to get within this matrix.
      • If diagOffset == 0: Then the elements of the principle diagonal are collected.
      • If diagOffset < 0: Then the elements of the sub-diagonal diagOffset below the principle diagonal are collected.
      • If diagOffset > 0: Then the elements of the super-diagonal diagOffset above the principle diagonal are collected.
      Returns:
      The elements of the specified diagonal as a vector.
    • accept

      public <R> R accept(MatrixVisitor<R> visitor)
      Accepts a visitor that implements the MatrixVisitor interface. This method is part of the "Visitor Pattern" and allows operations to be performed on the matrix without modifying the matrix's class directly.
      Type Parameters:
      R - The return type of the visitor's operation.
      Parameters:
      visitor - The visitor implementing the operation to be performed.
      Returns:
      The result of the visitor's operation, typically another matrix or a scalar value.
      Throws:
      NullPointerException - if the visitor is null.
    • toTensor

      public CooRingTensor<T> toTensor()
      Converts this CSR matrix to an equivalent sparse COO tensor.
      Specified by:
      toTensor in class AbstractCsrSemiringMatrix<CsrRingMatrix<T extends Ring<T>>,RingMatrix<T extends Ring<T>>,CooRingVector<T extends Ring<T>>,T extends Ring<T>>
      Returns:
      An sparse COO tensor equivalent to this CSR matrix.
    • toTensor

      public CooRingTensor<T> toTensor(Shape shape)
      Converts this CSR matrix to an equivalent COO tensor with the specified shape.
      Overrides:
      toTensor in class AbstractCsrSemiringMatrix<CsrRingMatrix<T extends Ring<T>>,RingMatrix<T extends Ring<T>>,CooRingVector<T extends Ring<T>>,T extends Ring<T>>
      Parameters:
      shape - @return A COO tensor equivalent to this CSR matrix which has been reshaped to newShape
      Returns:
      A COO tensor equivalent to this CSR matrix which has been reshaped to newShape
    • abs

      public CsrMatrix abs()
      Computes the element-wise absolute value of this tensor.
      Returns:
      The element-wise absolute value of this tensor.
    • dropZeros

      public CsrRingMatrix<T> dropZeros()
      Drops any explicit zeros in this sparse COO matrix.
      Returns:
      A copy of this Csr matrix with any explicitly stored zeros removed.
    • coalesce

      public CsrRingMatrix<T> coalesce()
      Coalesces this sparse CSR matrix. An uncoalesced matrix is a sparse matrix with multiple data for a single index. This method will ensure that each index only has one non-zero value by summing duplicated data. If another form of aggregation other than summing is desired, use coalesce(BinaryOperator).
      Returns:
      A new coalesced sparse CSR matrix which is equivalent to this CSR matrix.
      See Also:
    • coalesce

      public CsrRingMatrix<T> coalesce(BinaryOperator<T> aggregator)
      Coalesces this sparse COO matrix. An uncoalesced matrix is a sparse matrix with multiple data for a single index. This method will ensure that each index only has one non-zero value by aggregating duplicated data using aggregator.
      Parameters:
      aggregator - Custom aggregation function to combine multiple.
      Returns:
      A new coalesced sparse COO matrix which is equivalent to this COO matrix.
      See Also:
    • toString

      public String toString()
      Formats this sparse matrix as a human-readable string.
      Overrides:
      toString in class Object
      Returns:
      A human-readable string representing this sparse matrix.