Class AbstractCsrSemiringMatrix<T extends AbstractCsrSemiringMatrix<T,U,V,W>,U extends AbstractDenseSemiringMatrix<U,?,W>,V extends AbstractCooSemiringVector<V,?,?,U,W>,W extends Semiring<W>>

java.lang.Object
org.flag4j.arrays.backend.AbstractTensor<T,W[],W>
org.flag4j.arrays.backend.semiring_arrays.AbstractCsrSemiringMatrix<T,U,V,W>
Type Parameters:
T - Type of this CSR field matrix.
U - Type of dense field matrix equivalent to T.
V - Type of vector equivalent to V.
Y - Type of field element of this matrix.
All Implemented Interfaces:
Serializable, MatrixMixin<T,U,V,W>, SemiringTensorMixin<T,U,W>, TensorOverSemiring<T,U,W[],W>
Direct Known Subclasses:
AbstractCsrRingMatrix, CsrSemiringMatrix

public abstract class AbstractCsrSemiringMatrix<T extends AbstractCsrSemiringMatrix<T,U,V,W>,U extends AbstractDenseSemiringMatrix<U,?,W>,V extends AbstractCooSemiringVector<V,?,?,U,W>,W extends Semiring<W>> extends AbstractTensor<T,W[],W> implements SemiringTensorMixin<T,U,W>, MatrixMixin<T,U,V,W>

A sparse matrix stored in compressed sparse row (CSR) format. The AbstractTensor.data of this CSR matrix are elements of a Semiring.

The non-zero data and non-zero indices of a CSR matrix are mutable but the AbstractTensor.shape and total number of non-zero data is fixed.

Sparse matrices allow for the efficient storage of and ops on matrices that contain many zero values.

A sparse CSR matrix is stored as:

  • The full shape of the matrix.
  • The non-zero AbstractTensor.data of the matrix. All other data in the matrix are assumed to be zero. Zero values can also explicitly be stored in AbstractTensor.data.
  • The row pointers of the non-zero values in the CSR matrix. Has size numRows + 1
  • rowPointers[i] indicates the starting index within data and colData of all values in row i.

  • The column indices of the non-zero values in the sparse matrix.

Note: many ops assume that the data of the CSR matrix are sorted lexicographically by the row and column indices. (i.e.) by row indices first then column indices. However, this is not explicitly verified. Any ops implemented in this class will preserve the lexicographical sorting.

If indices need to be sorted, call sortIndices().

See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    final int[]
    Column indices for non-zero values of this sparse CSR matrix.
    final int
    Number of non-zero data in this CSR matrix.
    final int
    The number of columns in this matrix.
    final int
    The number of rows in this matrix.
    final int[]
    Pointers indicating starting index of each row within the colIndices and AbstractTensor.data arrays.
    protected final double
    The sparsity of this matrix.
    protected W
    The zero element for the semiring that this tensor's elements belong to.

    Fields inherited from class org.flag4j.arrays.backend.AbstractTensor

    data, rank, shape
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    AbstractCsrSemiringMatrix(Shape shape, W[] entries, int[] rowPointers, int[] colIndices)
    Creates a sparse CSR matrix with the specified shape, non-zero data, row pointers, and non-zero column indices.
  • Method Summary

    Modifier and Type
    Method
    Description
    add(T b)
    Computes the element-wise sum between two tensors of the same shape.
    Stacks matrices along rows.
    Augments a vector to this matrix.
    Creates a deep copy of this tensor.
    int
    Gets the length of the data array which backs this matrix.
    double
    Gets the density of this matrix as a decimal percentage.
    div(T b)
    Warning: throws UnsupportedOperationException as division is not defined for general semiring matrices.
    Computes the element-wise multiplication of two tensors of the same shape.
    Flattens tensor to single dimension while preserving order of data.
    flatten(int axis)
    Flattens a tensor along the specified axis.
    get(int... indices)
    Gets the element of this tensor at the specified indices.
    get(int row, int col)
    Gets the element of this matrix at this specified row and col.
    getSlice(int rowStart, int rowEnd, int colStart, int colEnd)
    Gets a specified slice of this matrix.
    getTriL(int diagOffset)
    Extracts the lower-triangular portion of this matrix with a specified diagonal offset.
    getTriU(int diagOffset)
    Extracts the upper-triangular portion of this matrix with a specified diagonal offset.
    Gets the zero element for the semiring of this tensor.
    H()
    Computes the Hermitian transpose of this matrix.
    boolean
    Checks if a matrix is Hermitian.
    boolean
    isI()
    Checks if this matrix is the identity matrix.
    boolean
    Checks if this matrix is orthogonal.
    boolean
    Checks if a matrix is symmetric.
    boolean
    Checks if this matrix is lower triangular.
    boolean
    Checks if this matrix is upper triangular.
    makeLikeCooMatrix(Shape shape, W[] entries, int[] rowIndices, int[] colIndices)
    Constructs a sparse COO matrix of a similar type to this sparse CSR matrix.
    abstract U
    makeLikeDenseTensor(Shape shape, W[] entries)
    Constructs a dense matrix which is of a similar type to this sparse CSR matrix.
    abstract T
    makeLikeTensor(Shape shape, List<W> entries, List<Integer> rowPointers, List<Integer> colIndices)
    Constructs a CSR matrix with the specified shape, non-zero data, and non-zero indices.
    abstract T
    makeLikeTensor(Shape shape, W[] 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.
    mult(T b)
    Computes the matrix multiplication between two matrices.
    Computes the matrix multiplication between two sparse CSR matrices and stores the result in a sparse matrix.
    Multiplies this matrix with the transpose of the b tensor as if by this.mult(b.T()).
    int
    Gets the number of columns in this matrix.
    int
    Gets the number of rows in this matrix.
    removeCol(int colIndex)
    Removes a specified column from this matrix.
    removeCols(int... colIndices)
    Removes a specified set of columns from this matrix.
    removeRow(int rowIndex)
    Removes a specified row from this matrix.
    removeRows(int... rowIndices)
    Removes a specified set of rows from this matrix.
    reshape(Shape newShape)
    Copies and reshapes this tensor.
    set(W value, int... indices)
    Sets the element of this tensor at the specified indices.
    set(W value, int row, int col)
    Sets an index of this matrix to the specified value.
    setCol(V col, int colIdx)
    Sets a specified column of this matrix to a vector.
    setRow(V row, int rowIdx)
    Sets a specified row of this matrix to a vector.
    setSliceCopy(T values, int rowStart, int colStart)
    Creates a copy of this matrix and sets a slice of the copy to the specified values.
    void
    setZeroElement(W zeroElement)
    Sets the zero element for the semiring of this tensor.
    void
    Sorts the indices of this tensor in lexicographical order while maintaining the associated value for each index.
    double
    Gets the sparsity of this matrix as a decimal percentage.
    stack(T b)
    Stacks matrices along columns.
    sub(T b)
    Warning: throws UnsupportedOperationException as subtraction is not defined for general semiring matrices.
    swapCols(int colIndex1, int colIndex2)
    Swaps specified columns in the matrix.
    swapRows(int rowIndex1, int rowIndex2)
    Swaps specified rows in the matrix.
    T()
    Computes the transpose of a tensor by exchanging the first and last axes of this tensor.
    T(int... axes)
    Computes the transpose of this tensor.
    T(int axis1, int axis2)
    Computes the transpose of a tensor by exchanging axis1 and axis2.
    tensorTr(int axis1, int axis2)
    Computes the generalized trace of this tensor along the specified axes.
    Converts this sparse CSR matrix to an equivalent sparse COO matrix.
    Converts this sparse CSR matrix to an equivalent dense matrix.
    Converts this CSR matrix to an equivalent sparse COO tensor.
    toTensor(Shape shape)
    Converts this CSR matrix to an equivalent COO tensor with the specified shape.
    Converts this sparse CSR matrix to an equivalent vector.
    tr()
    Computes the trace of this matrix.

    Methods inherited from class org.flag4j.arrays.backend.AbstractTensor

    getData, getRank, getShape, makeLikeTensor, reshape, sameShape, totalEntries

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface org.flag4j.arrays.backend.MatrixMixin

    accept, fib, getCol, getCol, getDiag, getDiag, getRow, getRow, getShape, getTriL, getTriU, isDiag, isSquare, isTri, isVector, mult, stack, trace, vectorType

    Methods inherited from interface org.flag4j.arrays.backend.semiring_arrays.SemiringTensorMixin

    add, addEq, argmax, argmin, isOnes, isZeros, makeEmptyDataArray, max, min, mult, multEq, prod, sum

    Methods inherited from interface org.flag4j.arrays.backend.semiring_arrays.TensorOverSemiring

    getData, getRank, getShape, makeLikeTensor, tensorDot, tensorDot, tensorDot, tensorDot, tensorTr