Repository navigation
fix: is_constant is True for every expression without variables - #1152
Merged
Merged
Conversation
A sparse store keeps at least one term slot, and a sum can leave a dense store with empty slots, so checking the slot count reported a constant as non-constant. Each store now checks for actual terms.
Merging this PR will not alter performance
Comparing Footnotes
|
FabianHofmann
marked this pull request as ready for review
November 13, 2026 10:01
This was referenced Nov 13, 2026
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.
Closes # (if applicable).
Changes proposed in this Pull Request
Note
The following content was generated by AI.
LinearExpression.is_constantcheckednterm == 0. That misreads an expression without variables that still has term slots:from_constantin a sparse model (since fix(spec): a sparse model built from a spec stays sparse #1145) is not constant;((x * 0) @ w).is_constantisFalseon both backends.Each store now answers
is_constantfrom its terms:csr.nnz == 0(O(1)) for CSR, all labels-1for dense. A zero-cell expression keeps the old answer, sox * xover an empty dimension stays quadratic.Found via PyPSA's sparse CI on linopy master (PyPSA/PyPSA#2129):
from_constant(m, 0)reportedsize == 1on sparse vs0on dense.Why not drop the one-slot floor in
csr_ntermDropping it makes term-less results report 0 slots everywhere, also in dense models (reductions route through CSR). PyPSA then skips zero-cost components in
opex(), which breaks released PyPSA 1.4.0 on dense models.Checklist
AGENTS.md).doc.doc/release-notes.mdof the upcoming release is included.Generated by Claude Code