Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom sorting key for substitute( ) #67

Merged
merged 2 commits into from Dec 22, 2020

Conversation

Upabjojr
Copy link
Contributor

Substitute now accepts custom sorting key to specify custom sorting for elements in Multiset.

This should solve the compatibility problem with SymPy which does not allow comparison operators (>, >=, <, <=) to be evaluated to booleans if the truth of the expression cannot be easily determined.

@coveralls
Copy link

coveralls commented Dec 21, 2020

Coverage Status

Coverage increased (+0.004%) to 96.389% when pulling 6829e16 on Upabjojr:substitute_custom_sorting_key into 5cae3f2 on HPAC:master.

@wheerd
Copy link
Collaborator

wheerd commented Dec 22, 2020

I think adding some global state is not a good idea, I would prefer changing the signature of the substitute function:

def substitute(expression: Union[Expression, Pattern], substitution: Substitution, sort_key=None: Optional[Callable[[Expression], Any]]) -> Replacement:
    ...

That way the behavior of the function remains pure and does not depend on some global state.

@wheerd
Copy link
Collaborator

wheerd commented Dec 22, 2020

@Upabjojr I switch the build from Travis CI to Github Actions. If you rebase your changes, the pipeline should work better :)

@Upabjojr
Copy link
Contributor Author

I think adding some global state is not a good idea, I would prefer changing the signature of the substitute function

I am not very convinced about adding a parameter to substitute, because you would need to add that parameter every time you call substitute with a SymPy expression.

Ideally after calling from sympy.utilities.matchpy_connector import *, everything in MatchPy should work fine with SymPy expressions. A global parameter makes it easier. The alternative would be a wrapper for substitute( ) in SymPy.

Otherwise, would about editing the Multiset library instead and add the sorting key there?

@wheerd
Copy link
Collaborator

wheerd commented Dec 22, 2020

What if you put a substitute function in sympy.utilities.matchpy_connector that calls the matchpy substitute with the correct sort_key?

@Upabjojr
Copy link
Contributor Author

What if you put a substitute function in sympy.utilities.matchpy_connector that calls the matchpy substitute with the correct sort_key?

Well, that would solve the problem if users import substitute( ) correctly. I fear there will still be some confusion about users mixing up the imports. I was still trying to avoid duplicating the definition of substitute( ).

Notice that redefining substitute( ) inside of SymPy could be done without modifying MatchPy. For example, the multiset could be pre-sorted and passed as a list to MatchPy.

@wheerd
Copy link
Collaborator

wheerd commented Dec 22, 2020

Notice that redefining substitute( ) inside of SymPy could be done without modifying MatchPy. For example, the multiset could be pre-sorted and passed as a list to MatchPy.

Isn't that a better approach then anyways?

Relying on and modifying a private variable from outside is against Python conventions and seems like a very brittle solution. An alternative would be to have setter/getter for that global state, even though I would still prefer to make it explicit when calling the function..

@wheerd wheerd merged commit 3fa0e82 into HPAC:master Dec 22, 2020
@wheerd
Copy link
Collaborator

wheerd commented Dec 22, 2020

Thank you for your contribution! 🎉👍

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.

None yet

3 participants