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

Fixed issue with simplifying oo + imaginary #22024

Merged
merged 2 commits into from Sep 5, 2021

Conversation

oscargus
Copy link
Contributor

@oscargus oscargus commented Sep 5, 2021

References to other Issues or PRs

Closes #22020

Brief description of what is fixed or changed

Simplifying expressions of the type oo + I/2 used to throw an error. Not anymore.

Other comments

To be honest, I do not fully understand the logic behind the code I added, but it seems to work and avoids the int(c) that was the cause of all evil here.

Release Notes

  • simplify
    • Fixed issue with simplifying infinity plus rational imaginary.

@oscargus oscargus added this to the SymPy 1.9 milestone Sep 5, 2021
@sympy-bot
Copy link

sympy-bot commented Sep 5, 2021

Hi, I am the SymPy bot (v161). 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:

  • simplify
    • Fixed issue with simplifying infinity plus rational imaginary. (#22024 by @oscargus)

This will be added to https://github.com/sympy/sympy/wiki/Release-Notes-for-1.10.

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. -->
Closes #22020

#### Brief description of what is fixed or changed
Simplifying expressions of the type `oo + I/2` used to throw an error. Not anymore.

#### Other comments
To be honest, I do not fully understand the logic behind the code I added, but it seems to work and avoids the `int(c)` that was the cause of all evil here.

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

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 -->
* simplify
     * Fixed issue with simplifying infinity plus rational imaginary.
<!-- END RELEASE NOTES -->

Update

The release notes on the wiki have been updated.

@github-actions
Copy link

github-actions bot commented Sep 5, 2021

Benchmark results from GitHub Actions

Lower numbers are good, higher numbers are bad. A ratio less than 1
means a speed up and greater than 1 means a slowdown. Green lines
beginning with + are slowdowns (the PR is slower then master or
master is slower than the previous release). Red lines beginning
with - are speedups.

Significantly changed benchmark results (PR vs master)

Significantly changed benchmark results (master vs previous release)

Full benchmark results can be found as artifacts in GitHub Actions
(click on checks at the top of the PR).

@@ -2112,7 +2112,7 @@ def _keep_coeff(coeff, factors, clear=True, sign=False):
if not clear and coeff.is_Rational and coeff.q != 1:
args = [i.as_coeff_Mul() for i in factors.args]
args = [(_keep_coeff(c, coeff), m) for c, m in args]
if any(c == int(c) for c, _ in args):
if any(not c.is_infinite and c == int(c) for c, _ in args):
Copy link
Member

Choose a reason for hiding this comment

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

The condition should probably be something like c.is_Number as int(c) is not implemented for finite complex numbers:

In [23]: I.is_infinite                                                          
Out[23]: False

In [24]: int(I)                                                                 
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)

TypeError: can't convert complex to int

In [25]: I.is_Number                                                            
Out[25]: False

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ahh, I only noted the oo error, but that clearly makes sense. (For some reason the code doesn't come here for e.g. oo + I, only for oo+I/2 etc, and apparently not for many other cases with complex numbers...)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

However, as oo is a Number, I am not sure exactly what it should be. Right now it works and it seems like c rarely (if ever) an imaginary/complex number.

Also, c should by sympified as far as I can tell, so not obvious what the purpose of c == int(c) is? Check that c is an integer or something else? Maybe is_Integer 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.

I tried is_Integer only and so far so good. Pushing to run the complete test set.

Copy link
Member

Choose a reason for hiding this comment

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

Also, c should by sympified as far as I can tell, so not obvious what the purpose of c == int(c) is

Yes, c was generated from as_coeff_Mul so it will be sympified. Even I am not sure why the int was used.

@smichr smichr merged commit 719bb15 into sympy:master Sep 5, 2021
@oscargus oscargus deleted the simplifyooplusi branch September 6, 2021 07:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Comparing two operations that contain log sometimes leads to TypeError exception
4 participants