Repository navigation
fix: building a quadratic from a sparse expression emits no densify warning - #1195
Merged
Merged
Conversation
Merging this PR will not alter performance
Comparing Footnotes
|
FabianHofmann
marked this pull request as ready for review
November 19, 2026 22:20
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 #1193.
Note
The following content was generated by AI.
Changes proposed in this Pull Request
A quadratic expression has no sparse backing, so building one from a sparse linear expression must convert to dense. The user cannot avoid this, so the
PerformanceWarningis noise. This PR treats every conversion into a quadratic as silent (option 3 of the issue):LinearExpression.to_quadexpr, solin + quad,quad - lin,add_objectiveandModel.from_spec(the PyPSA case)mul(quadratic product)notice is gone)merge(..., cls=QuadraticExpression)QuadraticExpression.groupby(...).sum(), which warned although nothing was densifiedLinear fallbacks still warn as before.
It also fixes a related bug:
lin.add(quad, join=...)andlin.sub(quad, join=...)raisedValueError: Cannot merge linear expressions to QuadraticExpression, while plainlin + quadworked.Design notes
from_specwarned or not depending only on the order of factors:k * x * x + xwarned,x * x * k + xdid not. The copy it flagged is no larger than the dense product built right after it, and a dense model pays the same. Variable × variable products were already silent (1.0 release notes, "Sparse models").to_quadexprkeeps CSR terms) needs a CSR-backedQuadraticExpression, which is option 1: a new store under quadratics,flat,to_matrix, IO and solvers. For PyPSA the gain is small, because the quadratic objective is summed to one row, where dense and CSR are equally compact.CONVERTEDmarker (dense quadratic result, no notice, operand keeps its CSR store) forto_quadexpr, lin + quad, quad − lin, products,powand the factor merge. Thefrom_specobjective test is parametrized over zeroed/kept quadratic coefficients and factor order, and runs on both backends withPerformanceWarningas error. The new cases fail on master and pass here.Checklist
AGENTS.md).doc.doc/release-notes.mdof the upcoming release is included.Generated by Claude Code