Skip to content

fix: building a quadratic from a sparse expression emits no densify warning - #1195

Merged
FabianHofmann merged 6 commits into
masterfrom
fix/quadexpr-no-densify-notice
Nov 20, 2026
Merged

FabianHofmann merged 6 commits into
masterfrom
fix/quadexpr-no-densify-notice

Conversation

@FabianHofmann

@FabianHofmann FabianHofmann commented Nov 19, 2026 •

Copy link
Copy Markdown
Collaborator

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 PerformanceWarning is noise. This PR treats every conversion into a quadratic as silent (option 3 of the issue):

  • LinearExpression.to_quadexpr, so lin + quad, quad - lin, add_objective and Model.from_spec (the PyPSA case)
  • expression × expression products (the mul(quadratic product) notice is gone)
  • merge(..., cls=QuadraticExpression)
  • QuadraticExpression.groupby(...).sum(), which warned although nothing was densified

Linear fallbacks still warn as before.

It also fixes a related bug: lin.add(quad, join=...) and lin.sub(quad, join=...) raised ValueError: Cannot merge linear expressions to QuadraticExpression, while plain lin + quad worked.

Design notes
  • Why also drop the product notice? Without it, from_spec warned or not depending only on the order of factors: k * x * x + x warned, x * x * k + x did 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").
  • Why not options 1/2? Option 2 (to_quadexpr keeps CSR terms) needs a CSR-backed QuadraticExpression, 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.
  • Tests: the conformance suite gets a CONVERTED marker (dense quadratic result, no notice, operand keeps its CSR store) for to_quadexpr, lin + quad, quad − lin, products, pow and the factor merge. The from_spec objective test is parametrized over zeroed/kept quadratic coefficients and factor order, and runs on both backends with PerformanceWarning as error. The new cases fail on master and pass here.
  • Local run: 6556 passed, 0 failed.
  • Two agent review rounds were run on the diff; their findings are applied.

Checklist

  • AI-generated content is marked (see AGENTS.md).
  • Code changes are sufficiently documented; i.e. new functions contain docstrings and further explanations may be given in doc.
  • Unit tests for new features were added (if applicable).
  • A note for the release notes doc/release-notes.md of the upcoming release is included.
  • I consent to the release of this PR's code under the MIT license.

Generated by Claude Code

@codspeed

codspeed Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 181 untouched benchmarks
⏩ 181 skipped benchmarks1


Comparing fix/quadexpr-no-densify-notice (bd3245c) with master (99e123d)

Open in CodSpeed

Footnotes

  1. 181 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@FabianHofmann
FabianHofmann marked this pull request as ready for review November 19, 2026 22:20
@FabianHofmann
FabianHofmann merged commit 9b1b998 into master Nov 20, 2026
23 checks passed
@FabianHofmann
FabianHofmann deleted the fix/quadexpr-no-densify-notice branch November 20, 2026 09:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sparse: adding a linear to a quadratic expression densifies it and warns the user

2 participants