Skip to content

fix(repr): show the last terms and a single constant in truncated expressions - #1023

Open
Blizzeq wants to merge 2 commits into
PyPSA:masterfrom
Blizzeq:fix/truncated-expression-repr
Open

Blizzeq wants to merge 2 commits into
PyPSA:masterfrom
Blizzeq:fix/truncated-expression-repr

Conversation

@Blizzeq

@Blizzeq Blizzeq commented Oct 7, 2026

Copy link
Copy Markdown

Changes proposed in this Pull Request

This PR fixes the string representation of long expressions so that the constant is printed only once and variables in quadratic tails are correctly aligned.

Note

The following content was generated by AI.

When an expression has more terms than display_max_terms, the truncated representation printed the constant twice, once before the ellipsis and once at the end (e.g., +1 x[0] + 2 x[1] + 3 x[2] + 5 ... +8 x[7] + 9 x[8] + 10 x[9] + 5). Additionally, for quadratic expressions, the variables in the tail were incorrectly matched because the array was sliced along the wrong dimension. I fixed this by slicing the variables along the term axis (v[..., -truncate:]) and explicitly omitting the constant from the first half of the string. I added two exact string-matching tests for the representations. The release notes entry is included and the PR number will be added in a follow-up commit. The code changes and tests were originally drafted with the help of Claude.

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.rst of the upcoming release is included.
  • I consent to the release of this PR's code under the MIT license.

…ressions

The tail of a truncated expression sliced the variable labels along the
first axis, which for a quadratic expression is the factor axis, so the
terms after the ellipsis repeated the first variables next to the last
coefficients. The constant was also printed before the ellipsis as well as
at the end.
@codspeed

codspeed Bot commented Oct 7, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 181 untouched benchmarks
⏩ 181 skipped benchmarks1


Comparing Blizzeq:fix/truncated-expression-repr (8065120) with master (ff5436f)

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. ↩

This branch has not been deployed

No deployments
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