Skip to content

fix: make assert_linequal term-order invariant#640

Closed
FBumann wants to merge 1 commit intoperf/matrix-accessor-rewritefrom
fix/assert-linequal-term-order-upstream
Closed

fix: make assert_linequal term-order invariant#640
FBumann wants to merge 1 commit intoperf/matrix-accessor-rewritefrom
fix/assert-linequal-term-order-upstream

Conversation

@FBumann
Copy link
Copy Markdown
Collaborator

@FBumann FBumann commented Apr 1, 2026

Summary

  • assert_linequal now sorts both sides by variable labels along _term before comparing, so expressions with different term orderings are correctly recognized as semantically equal.
  • Fixes false failures when comparing constraints that went through the CSR round-trip (freeze_constraints=True) against freshly built expressions, since csr.sum_duplicates() sorts terms by variable label.

Reproducer (from #630 comment)

from linopy import Model
from linopy.testing import assert_linequal
import pandas as pd

m = Model(freeze_constraints=True)
coords = pd.RangeIndex(3, name="time")
x = m.add_variables(coords=[coords], name="x")
y = m.add_variables(coords=[coords], name="y")

m.add_constraints(y - x == 0, name="con")

desired = y == x
assert_linequal(m.constraints["con"].lhs, desired.lhs)  # was failing, now passes

Test plan

  • Reproducer passes
  • All 2665 existing tests pass
  • Linter and pre-commit hooks pass

Replaces #639 (was opened from fork, CI didn't trigger fully).

🤖 Generated with Claude Code

Sort both sides by variable labels along _term before comparing, so
expressions with different term orderings (e.g. from CSR round-trip
with freeze_constraints=True) are correctly recognized as equal.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@FBumann
Copy link
Copy Markdown
Collaborator Author

FBumann commented Apr 1, 2026

Closed in favor of #639

@FBumann FBumann closed this Apr 1, 2026
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.

1 participant