Skip to content

fix: is_constant is True for every expression without variables - #1152

Merged
FabianHofmann merged 2 commits into
masterfrom
claude/tender-noether-ht5foe
Nov 13, 2026
Merged

FabianHofmann merged 2 commits into
masterfrom
claude/tender-noether-ht5foe

Conversation

@FabianHofmann

Copy link
Copy Markdown
Collaborator

Closes # (if applicable).

Changes proposed in this Pull Request

Note

The following content was generated by AI.

LinearExpression.is_constant checked nterm == 0. That misreads an expression without variables that still has term slots:

Each store now answers is_constant from its terms: csr.nnz == 0 (O(1)) for CSR, all labels -1 for dense. A zero-cell expression keeps the old answer, so x * x over an empty dimension stays quadratic.

Found via PyPSA's sparse CI on linopy master (PyPSA/PyPSA#2129): from_constant(m, 0) reported size == 1 on sparse vs 0 on dense.

Why not drop the one-slot floor in csr_nterm

Dropping 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

  • 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

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.
@codspeed

codspeed Bot commented Nov 13, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 181 untouched benchmarks
⏩ 181 skipped benchmarks1


Comparing claude/tender-noether-ht5foe (514d7dc) with master (810d296)

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 13, 2026 10:01
@FabianHofmann
FabianHofmann merged commit c8bb12f into master Nov 13, 2026
23 checks passed
@FabianHofmann
FabianHofmann deleted the claude/tender-noether-ht5foe branch November 13, 2026 10:01
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.

2 participants