feat(binar): add sparse ↔ dense BitMatrix conversion#47
Open
jmbr wants to merge 4 commits into
Open
Conversation
d8dab5f to
7d82cbd
Compare
Add four methods to BitMatrix for converting between dense matrix storage and sparse column/row representations via IndexSet: - from_sparse_columns(&[IndexSet], row_count) -> Self - from_sparse_rows(&[IndexSet], column_count) -> Self - sparse_columns(&self) -> Vec<IndexSet> - sparse_rows(&self) -> Vec<IndexSet> Implementations are on AlignedBitMatrix with BitMatrix delegating. Includes proptest round-trip tests and edge-case coverage. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Add PyO3 wrappers and .pyi stubs for: - BitMatrix.from_sparse_columns(columns, row_count) - BitMatrix.from_sparse_rows(rows, column_count) - BitMatrix.sparse_columns() -> list[list[int]] - BitMatrix.sparse_rows() -> list[list[int]] Python uses list[list[int]] rather than Vec<IndexSet> since IndexSet is not exposed to Python. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Both from_sparse_rows and from_sparse_columns now take (row_count, column_count) instead of inferring one dimension from the data length. This allows constructing matrices with trailing zero rows or columns, matching the scipy.sparse convention of an explicit shape parameter. Invalid inputs (too many entries or out-of-bounds indices) return SparseConversionError instead of panicking. The Python bindings translate this to ValueError.
a9d4eed to
2134817
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Add methods for converting between dense
BitMatrixand sparsecolumn/row representations via
IndexSet, and expose them in thePython bindings.
Motivation
BitMatrixhas no way to construct a matrix from sparse column or rowdescriptions, or to decompose one into its column/row supports.
Consumers that work with sparse GF(2) matrices (e.g., syndrome
indicators, check matrices) must set bits one at a time.
Changes
Rust API (
AlignedBitMatrix+BitMatrix):from_sparse_columns(&[IndexSet], row_count, column_count) -> Result<Self, SparseConversionError>from_sparse_rows(&[IndexSet], row_count, column_count) -> Result<Self, SparseConversionError>sparse_columns(&self) -> Vec<IndexSet>sparse_rows(&self) -> Vec<IndexSet>Both constructors take an explicit
(row_count, column_count)shape(matching the scipy.sparse convention), allowing trailing zero
rows/columns. Invalid inputs return
SparseConversionError.Python API (
BitMatrix):from_sparse_columns(columns, row_count, column_count)from_sparse_rows(rows, row_count, column_count)sparse_columns() -> list[list[int]]sparse_rows() -> list[list[int]]Invalid inputs raise
ValueError.Testing
sparse_columnsandsparse_rowscargo clippy-allclean,cargo fmtclean,mypy.stubtestclean