feat: add BaseExpression.has_terms property#743
Merged
Conversation
Boolean array, true at slots with at least one live term (vars != -1), regardless of the constant. Gives downstream code a public way to find empty constraint rows for masking, without reaching into the internal vars / _term representation. Closes #741 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Collaborator
Author
Collaborator
FabianHofmann
left a comment
There was a problem hiding this comment.
this makes very much sense
Per review: TestHasTerms with basic/masking, const-divergence, merge+reindex, constant-only, and quadratic cases as methods. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
FabianHofmann
approved these changes
Jun 3, 2026
FBumann
added a commit
that referenced
this pull request
Jun 3, 2026
Brings in the coords-as-truth stack (#732), the alignment.py module split (#742), has_terms (#743), and the MatrixAccessor caching (#716). Conflict resolutions (consistent rule: keep this branch's v1/legacy dispatch structure, use master's conversion calls inside it): - expressions.py: _add_constant, _apply_constant_op_{v1,legacy}, and to_constraint use broadcast_to_coords(..., strict=False) instead of as_dataarray; SUPPORTED_CONSTANT_TYPES -> CONSTANT_TYPES. - variables.py: to_linexpr converts via broadcast_to_coords(strict=False), then applies the v1/legacy absence handling. - __init__.py: align from linopy.alignment + LinopySemanticsWarning export. Test adaptations for post-#732 APIs and semantics: - test_legacy_violations: name the MultiIndex coords entry (required since #732). - test_linear_expression: the masked-addend tails of test_nterm and test_variable_names pin legacy absence behavior; split into @pytest.mark.legacy / @pytest.mark.v1 pairs (section 6 divergence). Full suite under both semantics: 6446 passed, 514 skipped. Co-Authored-By: Claude Opus 4.8 (1M context) <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.
Closes #741.
Adds a public
BaseExpression.has_termsproperty: a boolean array, true at slots with at least one live term (vars != -1), regardless of the constant.Motivation
PyPSA's nodal balance constraint currently reaches into linopy internals to find constraint rows with no variables (pypsa/optimization/constraints.py#L1166):
With this PR it becomes:
Relation to
isnull()isnull()tests absence (no terms and no constant);has_termsignores the constant. The two diverge at slots whose constant was revived byfillna()— present, but still without terms. Covered bytest_linear_expression_has_terms_ignores_const.Changes
has_termsproperty onBaseExpression(inherited byLinearExpressionandQuadraticExpression), with doctestwhere(), divergence fromisnull()afterfillna(), the outer-merge + reindex pattern, constant-only expressions, and quadratic expressions (linear terms with one factor== -1must count as live)🤖 Generated with Claude Code