-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
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
Fix rewrite(abs) to call rewrite(Abs) #27325
Conversation
✅ Hi, I am the SymPy bot. I'm here to help you write a release notes entry. Please read the guide on how to write release notes. Your release notes are in good order. Here is what the release notes will look like:
This will be added to https://github.com/sympy/sympy/wiki/Release-Notes-for-1.14. Click here to see the pull request description that was parsed.
Update The release notes on the wiki have been updated. |
def test_rewrite_abs(): | ||
from sympy import Piecewise, Eq | ||
assert sign(x).rewrite(abs) == sign(x).rewrite(Abs) | ||
assert sign(x).rewrite(abs) == Piecewise((0, Eq(x, 0)), (x / Abs(x), True)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Usually you might have seen tests in SymPy being framed as test_issue_xx
where xx is the issue number, but something like this being more fundamental we could use test_rewrite_abs
.
My only suggestion here would be to add the issue link either
- "# issue 27327"
- Or even the whole issue link
Because in future we should be able to trace back why this was added and having a comment makes it easier.
sympy/core/tests/test_basic.py
Outdated
|
||
Related to issue 27327: https://github.com/sympy/sympy/issues/27327 | ||
Ensure rewrite(abs) calls rewrite(Abs) and produces expected results. | ||
""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm I guess a link to the issue is enough :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should I remove the comments?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean yeah, a link to the issue should be enough.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay I will do that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Approving but I'll still wait for reviews by other maintainers .... Just to make sure this is the best place to add the condition we're interested in !
Okay thank you. Hello @oscarbenjamin please can you take a look at this PR, it has been approved and it is ready to merge. |
Are there any other functions analogous to |
Thank you for your suggestion! The _eval_rewrite_as_Abs method already exists for both Max and Min in sympy/functions/elementary/miscellaneous.py. These functions rewrite themselves in terms of Abs. To confirm this functionality, I’ve written tests to verify their behavior and they all passed: Basic cases for Max and Min with two arguments. All the tests passed successfully, which confirms the correctness of the existing implementation. Would you like me to include these tests in my PR or handle them in a separate one? |
I mean are there any other functions like |
yes I think the most relevant analogous functions are trigonometric functions (sin/cos) and logarithmic function (log), There might be more |
None of these are builtins though. I don't think we would want to get into The reason I am asking about this is because the code change here makes sense if there is exactly one function for which we would want to do this. If there were others then we would probably want some kind of mapping somewhere. |
Agreed, abs appears to be the only case where this specific functionality is relevant. |
Fom the top of my head ... I can remember python offers these math based intrinsic functions
So yeah maybe we might have some interest in mapping |
Probably it is fine just to go with An alternative approach could just be to add
|
if it is fine to go with abs, will that be all for this PR, or there is a change you want me to incorporate? |
Hmm, maybe let's stick to what was suggested by Aaron in the issue. He says
Which is what @Ytemiloluwa is doing. So lets get this in for now. And maybe address anything that remains if pointed out ! |
Made a small change with respect to the comment (a link as a comment should be enough) @Ytemiloluwa once your squash the commits into a meaningful one, we can get this in. |
I have squashed the commits now and all tests have passed @oscarbenjamin @anutosh491 |
rewrite abs updated test_basic.py updated issue number updated test_rewrite_abs Update test_basic.py
e56a4c3
to
604d61a
Compare
References to other Issues or PRs
Fixes #27323
Brief description of what is fixed or changed
This PR resolves issue #27323 by ensuring that rewrite(abs) correctly maps to rewrite(Abs) for consistency when using the built-in abs function. @asmeurer @oscarbenjamin
Other comments
Changes Made:
Release Notes