[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
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

Fix rewrite(abs) to call rewrite(Abs) #27325

Merged
merged 1 commit into from
Dec 3, 2024

Conversation

Ytemiloluwa
Copy link
Contributor
@Ytemiloluwa Ytemiloluwa commented Nov 28, 2024

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:

  • Updated the rewrite method to handle abs as a special case, redirecting it to Abs.
  • Added a test case in sympy/core/tests/test_basic.py to confirm that rewrite(abs) produces the same output as rewrite(Abs).

Release Notes

  • core
    • Fixed rewrite(abs) to call rewrite(Abs), ensuring consistent behavior.

@sympy-bot
Copy link
sympy-bot commented Nov 28, 2024

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:

  • core
    • Fixed rewrite(abs) to call rewrite(Abs), ensuring consistent behavior. (#27325 by @Ytemiloluwa)

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.
<!-- Your title above should be a short description of what
was changed. Do not include the issue number in the title. -->

#### References to other Issues or PRs
<!-- If this pull request fixes an issue, write "Fixes #NNNN" in that exact
format, e.g. "Fixes #1234" (see
https://tinyurl.com/auto-closing for more information). Also, please
write a comment on that issue linking back to this pull request once it is
open. -->

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:

- Updated the rewrite method to handle abs as a special case, redirecting it to Abs.
- Added a test case in sympy/core/tests/test_basic.py to confirm that rewrite(abs) produces the same output as rewrite(Abs).

#### Release Notes

<!-- Write the release notes for this release below between the BEGIN and END
statements. The basic format is a bulleted list with the name of the subpackage
and the release note for this PR. For example:

* solvers
  * Added a new solver for logarithmic equations.

* functions
  * Fixed a bug with log of integers. Formerly, `log(-x)` incorrectly gave `-log(x)`.

* physics.units
  * Corrected a semantical error in the conversion between volt and statvolt which
    reported the volt as being larger than the statvolt.

or if no release note(s) should be included use:

NO ENTRY

See https://github.com/sympy/sympy/wiki/Writing-Release-Notes for more
information on how to write release notes. The bot will check your release
notes automatically to see if they are formatted correctly. -->

<!-- BEGIN RELEASE NOTES -->
* core
     * Fixed rewrite(abs) to call rewrite(Abs), ensuring consistent behavior.
<!-- END RELEASE NOTES -->

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))
Copy link
Member
@anutosh491 anutosh491 Nov 29, 2024

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

  1. "# issue 27327"
  2. 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.


Related to issue 27327: https://github.com/sympy/sympy/issues/27327
Ensure rewrite(abs) calls rewrite(Abs) and produces expected results.
"""
Copy link
Member

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 :)

Copy link
Contributor Author

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?

Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Member
@anutosh491 anutosh491 left a 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 !

@Ytemiloluwa
Copy link
Contributor Author

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.

@oscarbenjamin
Copy link
Collaborator

Are there any other functions analogous to abs where we would want to do this?

@Ytemiloluwa
Copy link
Contributor Author

Are there any other functions analogous to abs where we would want to do this?

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.
Multi-argument cases (e.g., Max(x, y, z)).
Nested Max and Min.
Special cases, such as when arguments are identical (e.g., Max(x, x)).
Explicit numerical values.

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?

@oscarbenjamin
Copy link
Collaborator

I mean are there any other functions like abs where there is a builtin function and a SymPy function and we would want to be able to do the equivalent of .rewrite(abs) but for a different function.

@Ytemiloluwa
Copy link
Contributor Author

I mean are there any other functions like abs where there is a builtin function and a SymPy function and we would want to be able to do the equivalent of .rewrite(abs) but for a different function.

yes I think the most relevant analogous functions are trigonometric functions (sin/cos) and logarithmic function (log), There might be more

@oscarbenjamin
Copy link
Collaborator

yes I think the most relevant analogous functions are trigonometric functions (sin/cos) and logarithmic function (log),

None of these are builtins though. I don't think we would want to get into math.sin, cmath.sin, np.sin etc. Maybe abs is the only case.

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.

@Ytemiloluwa
Copy link
Contributor Author

yes I think the most relevant analogous functions are trigonometric functions (sin/cos) and logarithmic function (log),

None of these are builtins though. I don't think we would want to get into math.sin, cmath.sin, np.sin etc. Maybe abs is the only case.

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.

@anutosh491
Copy link
Member
anutosh491 commented Dec 3, 2024

Fom the top of my head ... I can remember python offers these math based intrinsic functions

abs(x)
pow(x, y)
round(x[, ndigits])
divmod(x, y)
max(*args, key=None)
min(*args, key=None)
int(x[, base])
float(x)

So yeah maybe we might have some interest in mapping pow to Pow and abs to Abs but not sure how helpful this would end up to be. Not too many builtins though if that's what concerning in this discussion.

@oscarbenjamin
Copy link
Collaborator

Probably it is fine just to go with abs then.

An alternative approach could just be to add _eval_rewrite_as_abs to the two classes that support rewriting to Abs:

$ git grep '_eval_rewrite_as_Abs'
sympy/functions/elementary/complexes.py:    def _eval_rewrite_as_Abs(self, arg, **kwargs):
sympy/functions/elementary/miscellaneous.py:    def _eval_rewrite_as_Abs(self, *args, **kwargs):

@Ytemiloluwa
Copy link
Contributor Author

Probably it is fine just to go with abs then.

An alternative approach could just be to add _eval_rewrite_as_abs to the two classes that support rewriting to Abs:

$ git grep '_eval_rewrite_as_Abs'
sympy/functions/elementary/complexes.py:    def _eval_rewrite_as_Abs(self, arg, **kwargs):
sympy/functions/elementary/miscellaneous.py:    def _eval_rewrite_as_Abs(self, *args, **kwargs):

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?

@anutosh491
Copy link
Member

Hmm, maybe let's stick to what was suggested by Aaron in the issue. He says

So it should be special-cased in rewrite to call Abs.

Which is what @Ytemiloluwa is doing. So lets get this in for now. And maybe address anything that remains if pointed out !

@anutosh491
Copy link
Member

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.

@Ytemiloluwa
Copy link
Contributor Author
Ytemiloluwa commented Dec 3, 2024

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
@anutosh491 anutosh491 merged commit 2c7f4aa into sympy:master Dec 3, 2024
43 checks passed
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.

rewrite(abs) should call rewrite(Abs)
5 participants