Package de.labathome.trustconstr.sparse
Class CSRMatrix
java.lang.Object
de.labathome.trustconstr.sparse.CSRMatrix
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 ofWithin a row the entries are kept sorted by column index -- this matches the canonical form scipy returns fromindices/dataentries 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.
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 ClassesModifier and TypeClassDescriptionstatic final classIncremental builder forCSRMatrixusing a Dictionary-of-Keys (DOK) representation under the hood. -
Constructor Summary
ConstructorsConstructorDescriptionCSRMatrix(int rows, int cols, int[] indptr, int[] indices, double[] data) Construct from raw CSR triplets. -
Method Summary
Modifier and TypeMethodDescriptionstatic CSRMatrix.Builderbuilder(int rows, int cols) Start an incremental DOK-style build.intcols()double[]data()static CSRMatrixeye(int n) n x nidentity in CSR.static CSRMatrixfromDense(double[][] dense) Build a CSR matrix from a row-major densedouble[][].static CSRMatrixfromMatrix(Matrix m) Build from anyMatrix.int[]indices()int[]indptr()double[]matvec(double[] x) Matrix-vector producty = A x.multiply(double s) Element-wise scalar multiply (allocates a new matrix).intnnz()double[]rmatvec(double[] x) Transposed matrix-vector producty = A^T x.introws()toCSC()Convert to CSC of the same matrix (not the transpose).double[][]toDense()Materialize as a dense[rows][cols]array.Transpose: returns a CSC matrix with the same non-zeros, rows/columns swapped.
-
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 atindices.length.indices.length == data.length(the number of stored entries).0 <= indices[k] < colsfor every k.- Within each row,
indicesis strictly increasing.
- Parameters:
rows- number of rowscols- number of columnsindptr- row pointer array, lengthrows + 1indices- column index for each stored entrydata- 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
Build from anyMatrix. Routes sparseSparseMatrixsources directly toSparseMatrix.toCSR()(no dense materialisation); dense sources go throughfromDense(double[][]).- Parameters:
m- source matrix- Returns:
- CSR view of the same content
-
fromDense
Build a CSR matrix from a row-major densedouble[][]. 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
n x nidentity in CSR.- Parameters:
n- dimension; must be non-negative- Returns:
- the
n x nidentity matrix - See Also:
-
matvec
public double[] matvec(double[] x) Matrix-vector producty = A x. -
rmatvec
public double[] rmatvec(double[] x) Transposed matrix-vector producty = A^T x. -
multiply
Element-wise scalar multiply (allocates a new matrix).- Parameters:
s- scalar factor- Returns:
s * this
-
transpose
Transpose: returns a CSC matrix with the same non-zeros, rows/columns swapped. CSC is the natural output because a CSR row traversal ofAis a CSC column traversal ofA^T.- Returns:
A^Tas aCSCMatrix
-
toCSC
Convert to CSC of the same matrix (not the transpose).- Returns:
- CSC representation of this matrix
-
builder
Start an incremental DOK-style build. SeeCSRMatrix.Builder.- Parameters:
rows- number of rows in the resulting matrixcols- number of columns in the resulting matrix- Returns:
- a fresh
CSRMatrix.Builder
-