Skip to content

Conversation

@RobbieKiwi
Copy link
Contributor

@RobbieKiwi RobbieKiwi commented Dec 9, 2025

Changes proposed in this Pull Request

Add a method to LinearExpression to reduce duplicate terms
I.e 2x + x -> 3x

Some ideas:

  • Add this also to Quadratic expression
  • Automatically densify terms when simplifying? 2x + x + y - y -> 3x
  • Automatically simplify when performing addition/subtraction
  • Create a "ConstantExpression" type, so that there are three types (Constant, Linear, Quadratic) based on the highest order of variable (0, 1, 2). Maybe simplify could also cast the expression to the relevant type (E.g a LinearExpression with no variable term would be cast to ConstantExpression)

Checklist

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

RobbieKiwi and others added 6 commits December 9, 2025 22:52
- Fix misleading comment/error message (coefficient is 6, not 5)
- Add test for full cancellation (x - x = 0)
- Add test for partial cancellation (2x - 2x + 3y = 3y)
@FabianHofmann FabianHofmann merged commit fccfc6e into PyPSA:master Dec 18, 2025
21 checks passed
@RobbieKiwi
Copy link
Contributor Author

@FabianHofmann Any feedback on the other ideas?

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