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

Use assumption system instead of structural equality check in __bool__ method of Expression domain element #20428

Merged

Conversation

ehren
Copy link
Contributor

@ehren ehren commented Nov 14, 2020

References to other Issues or PRs

Brief description of what is fixed or changed

Fixes #20427

Other comments

fixes an unstripped zero in result from clear_denoms that would create a Poly that prints like the zero Poly but behaves inconsistently

Release Notes

  • polys
    • Use assumption system instead of structural equality check in __bool__ method of Expression domain element (fixing cases where Poly(...).is_zero would incorrectly return False)

@sympy-bot
Copy link

sympy-bot commented Nov 14, 2020

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:

  • polys
    • Use assumption system instead of structural equality check in __bool__ method of Expression domain element (fixing cases where Poly(...).is_zero would incorrectly return False) (#20428 by @ehren)

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

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


#### Brief description of what is fixed or changed

Fixes #20427 

#### Other comments

fixes an unstripped zero in result from clear_denoms that would create a Poly that prints like the zero Poly but behaves inconsistently

#### Release Notes

<!-- Write the release notes for this release below. 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 -->

* polys
    * Use assumption system instead of structural equality check in `__bool__` method of `Expression` domain element (fixing cases where `Poly(...).is_zero` would incorrectly return `False`)

<!-- END RELEASE NOTES -->

Update

The release notes on the wiki have been updated.

@codecov
Copy link

codecov bot commented Nov 14, 2020

Codecov Report

Merging #20428 (22c48bd) into master (026221a) will decrease coverage by 0.005%.
The diff coverage is 100.000%.

@@              Coverage Diff              @@
##            master    #20428       +/-   ##
=============================================
- Coverage   75.752%   75.747%   -0.006%     
=============================================
  Files          673       673               
  Lines       174119    174120        +1     
  Branches     41109     41108        -1     
=============================================
- Hits        131900    131892        -8     
- Misses       36505     36507        +2     
- Partials      5714      5721        +7     

@jksuom
Copy link
Member

jksuom commented Nov 14, 2020

It seems that dup_strip should have removed EX(0) when the polynomial representation was created by dup_from_dict. It fails because the condition f[0] is True for a non-simplified element of EX:

if not f or f[0]:
return f
i = 0
for cf in f:
if cf:
break
else:
i += 1
return f[i:]

The __bool__ method of EX
def __bool__(f):
return f.ex != 0
should probably be changed to return not f.ex.is_zero.

ehren added a commit to ehren/sympy that referenced this pull request Nov 14, 2020
@ehren ehren changed the title Fix unstripped leading zero in list representation of result from clear_denoms() Use assumption system instead of structural equality check in __bool__ method of Expression domain element Nov 14, 2020
@ehren
Copy link
Contributor Author

ehren commented Nov 14, 2020

should probably be changed to return not f.ex.is_zero.

That's a better fix that also removes the need for the conversion to expression in rsolve_poly in #20426

@@ -1458,6 +1458,15 @@ def test_Poly_rat_clear_denoms():
assert f.rat_clear_denoms(g) == (f, g)


def test_issue_20427():
f = Poly(-117968192370600*18**(S(1)/3)/(217603955769048*(24201 + 253*sqrt(9165))**(S(1)/3) + 2273005839412*sqrt(9165)*(24201 + 253*sqrt(9165))**(S(1)/3)) - 15720318185*2**(S(2)/3)*3**(S(1)/3)*(24201 + 253*sqrt(9165))**(S(2)/3)/(217603955769048*(24201 + 253*sqrt(9165))**(S(1)/3) + 2273005839412*sqrt(9165)*(24201 + 253*sqrt(9165))**(S(1)/3)) + 15720318185*12**(S(1)/3)*(24201 + 253*sqrt(9165))**(S(2)/3)/(217603955769048*(24201 + 253*sqrt(9165))**(S(1)/3) + 2273005839412*sqrt(9165)*(24201 + 253*sqrt(9165))**(S(1)/3)) + 117968192370600*2**(S(1)/3)*3**(S(2)/3)/(217603955769048*(24201 + 253*sqrt(9165))**(S(1)/3) + 2273005839412*sqrt(9165)*(24201 + 253*sqrt(9165))**(S(1)/3)), x)
Copy link
Member

Choose a reason for hiding this comment

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

Can you split this long line? It looks like ca. 10 lines would be needed. Backslashes are not necessary.

@@ -1458,6 +1458,15 @@ def test_Poly_rat_clear_denoms():
assert f.rat_clear_denoms(g) == (f, g)


def test_issue_20427():
f = Poly(-117968192370600*18**(S(1)/3)/(217603955769048*(24201 + 253*sqrt(9165))**(S(1)/3) + 2273005839412*sqrt(9165)*(24201 + 253*sqrt(9165))**(S(1)/3)) - 15720318185*2**(S(2)/3)*3**(S(1)/3)*(24201 + 253*sqrt(9165))**(S(2)/3)/(217603955769048*(24201 + 253*sqrt(9165))**(S(1)/3) + 2273005839412*sqrt(9165)*(24201 + 253*sqrt(9165))**(S(1)/3)) + 15720318185*12**(S(1)/3)*(24201 + 253*sqrt(9165))**(S(2)/3)/(217603955769048*(24201 + 253*sqrt(9165))**(S(1)/3) + 2273005839412*sqrt(9165)*(24201 + 253*sqrt(9165))**(S(1)/3)) + 117968192370600*2**(S(1)/3)*3**(S(2)/3)/(217603955769048*(24201 + 253*sqrt(9165))**(S(1)/3) + 2273005839412*sqrt(9165)*(24201 + 253*sqrt(9165))**(S(1)/3)), x)
coeff, poly = f.clear_denoms()
Copy link
Member

Choose a reason for hiding this comment

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

Maybe a single test would suffice: assert f == Poly(0, x, domain='EX').

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. That also clears up some confusion in my previous commit: Poly(...).is_zero is indeed 2 valued; Poly(y, x).is_zero is False (is not the zero poly) doesn't imply y.is_zero is False (there also shouldn't be a problem with how Poly(...).is_zero is used currently in rsolve).

@jksuom
Copy link
Member

jksuom commented Nov 17, 2020

This looks good. Can you squash the commits?

@ehren ehren force-pushed the constant-poly-invalid-rep-after-clear-denoms branch from 7344f27 to e35d42b Compare November 17, 2020 13:42
…_ method of ExpressionDomain Expression

Avoids unstripped 0 bug
@ehren ehren force-pushed the constant-poly-invalid-rep-after-clear-denoms branch from e35d42b to 22c48bd Compare November 17, 2020 13:52
@ehren
Copy link
Contributor Author

ehren commented Nov 17, 2020

squashed and reworded commit message.

@jksuom
Copy link
Member

jksuom commented Nov 17, 2020

Thanks! Merging.

@jksuom jksuom merged commit cb21c77 into sympy:master Nov 17, 2020
@ehren
Copy link
Contributor Author

ehren commented Nov 17, 2020

Thanks for the fix suggestion and review!

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.

Result from clear_denoms() prints like zero poly but behaves wierdly (due to unstripped DMP)
4 participants