Abstractions sparse - CyrilB1531/lodestar GitHub Wiki

Home › Abstractions

The shared sparse primitive — Lodestar.Abstractions

Text vectorization produces a matrix that is almost entirely zeros: a thousand documents over a twenty-thousand-word vocabulary is twenty million cells of which perhaps forty thousand are not zero. CsrMatrix stores the forty thousand.

It lives in a package of its own because more than one package needs it and they do not need each other. Lodestar.Text's vectorizers produce one; a decomposition consumes one; neither should oblige a caller to take the other's distances, stemmers, tokenizers and JSON. Decision 0003 records that move and what it cost.

The package has no dependencies, no I/O and nothing to configure. Its code is CsrMatrix and SparseNorm with the helpers they compile, and, beside the data types, the records' structural Equals and GetHashCode with the ValueEquality helper they call, UndefinedMetricException's constructors and the constructors of the instrumental and panel designs; since 0.2.0 it also holds the public data types the packages declare that carry no logic — options, results and enums, each under its original namespace — so that upgrading one package never duplicates a type (#1142).

Compressed sparse row, in one paragraph

Three arrays. Values holds the non-zero cells, row by row. ColumnIndices holds the column each one sits in — ascending within a row in every matrix this repository builds, though the type accepts any order. RowPointers has RowCount + 1 entries and delimits the rows: row i occupies the values from RowPointers[i] up to RowPointers[i + 1]. Reading a row is a slice; reading a column is not, which is what makes the two products below the operations worth having.

Member What it does
CsrMatrix The matrix: three arrays, and what the layout guarantees.
CsrMatrix(rowCount, columnCount, values, columnIndices, rowPointers) Builds a matrix from its three arrays, refusing any that do not describe one.
CsrMatrix.CreateUnchecked Wraps three arrays already valid by construction, without the constructor's checks.
CsrMatrix.Multiply The matrix times a dense vector, or times a dense block.
CsrMatrix.TransposeMultiply The transposed matrix times a dense block, without building the transpose.
CsrMatrix.NormalizeRows Divide every row by its own norm, in place.
CsrMatrix.RowL1Norm The sum of one row's absolute values.
CsrMatrix.RowL2Norm The Euclidean length of one row.
CsrMatrix.ToDense The same matrix with its zeros written out.
SparseNorm Which norm CsrMatrix.NormalizeRows divides each row by.