Skip to content

Conversation

@RobbieKiwi
Copy link
Contributor

@RobbieKiwi RobbieKiwi commented Jan 24, 2026

Closes #464

Changes proposed in this Pull Request

Add an example to the docs of LinearExpression.where(..., drop=True)
Add new property to base expression BaseExpression.variable_names

Not addressed / further ideas

In the issue mentioned, I think the reporter wanted to see the unused terms in LinearExpression.__repr__. After having a bit of a look at it, I think this is impossible because the term dim has no coordinate names. There seems to no way to know what variable originally belonged in a position of the term dim if the original variable has been completely masked (replaced with -1).

Maybe when drop is True we could also run .simplify to get rid of any empty positions in the term dim?
Currently drop=True will drop unused index positions along all dimensions apart from the term dim, it might make more sense to drop on all dims?

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 RobbieKiwi changed the title Feature/sparsity and repr Feature/example of drop=True Jan 24, 2026
Copy link
Collaborator

@FabianHofmann FabianHofmann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the pr @RobbieKiwi, one comment

def nvar(self) -> int:
"""
Get the number of unique variables in the linear expression.
Note that nvar <= nterm, as variables can appear multiple times and there can be terms which are completely masked out.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think nvar is a good name here. in model.nvar this relates to the number of reference variables (each single element in a variable array). perhaps let's remove this function for now?

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.

Add example of using where with drop = True

2 participants