perf: use single-step sparse matrix slicing in MatrixAccessor#618
Open
MaykThewessen wants to merge 1 commit intoPyPSA:masterfrom
Open
perf: use single-step sparse matrix slicing in MatrixAccessor#618MaykThewessen wants to merge 1 commit intoPyPSA:masterfrom
MaykThewessen wants to merge 1 commit intoPyPSA:masterfrom
Conversation
Replace double-slicing pattern A[clabels][:, vlabels] with single-step A[np.ix_(clabels, vlabels)] in MatrixAccessor.A and MatrixAccessor.Q. The double-slice creates an intermediate sparse matrix (selecting rows first, then columns), which allocates temporary storage proportional to the full matrix. np.ix_() performs both row and column selection in a single operation, avoiding the intermediate allocation. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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
Replace the double-slicing pattern in
MatrixAccessor.AandMatrixAccessor.Qwith single-stepnp.ix_()indexing.Before:
After:
Motivation
The double-slice
A[rows][:, cols]creates an intermediate sparse matrix after the first slice, then slices again.np.ix_()expresses the row+column selection as a single operation, avoiding the intermediate allocation. For large constraint matrices (~1.38M rows × ~593K cols), this reduces memory churn.Context
See #198 (comment) — item 4 in the priority list.
Test plan
test_matrices.py— all 4 tests pass (shape validation, masked models, duplicated variables, float coefficients)test_io.py::test_to_highspy— passestest_optimization.pyhighs-direct — 24/25 pass (one pre-existing failure)🤖 Generated with Claude Code