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

Piecewise fails with unevaluated relational #24289

Open
oscarbenjamin opened this issue Nov 20, 2022 · 3 comments
Open

Piecewise fails with unevaluated relational #24289

oscarbenjamin opened this issue Nov 20, 2022 · 3 comments
Labels
functions.elementary.piecewise unevaluated Issues related to unevaluated expressions

Comments

@oscarbenjamin
Copy link
Contributor

In [17]: Piecewise((1, Lt(1, 2, evaluate=False)), (2, True))
---------------------------------------------------------------------------
TypeError: Invalid comparison of non-real zoo

This is dues to the _canonical_coeff function somehow making incorrect presumptions about the relational:

ipdb> ll
     46 def _canonical_coeff(rel):
     47     # return -2*x + 1 < 0 as x > 1/2
     48     # XXX make this part of Relational.canonical?
     49     rel = rel.canonical
     50     if not rel.is_Relational or rel.rhs.is_Boolean:
     51         return rel  # Eq(x, True)
     52     b, l = rel.lhs.as_coeff_Add(rational=True)
     53     m, lhs = l.as_coeff_Mul(rational=True)
     54     rhs = (rel.rhs - b)/m
     55     if m < 0:
     56         return rel.reversed.func(lhs, rhs)
---> 57     return rel.func(lhs, rhs)
     58 

ipdb> p m
0
ipdb> p rhs
zoo
@oscarbenjamin oscarbenjamin added functions.elementary.piecewise unevaluated Issues related to unevaluated expressions labels Nov 20, 2022
@1e9abhi1e10
Copy link
Contributor

Can we change the definition of _canonical_coeff with respect to relational ?
But when we write the1<2 instead of Lt(1, 2, evaluate=False) it doesn't give any error!

In [3] : Piecewise((1,1<2), (2, True))
Out[4] : 1

How should I move forward for opening the PR for that?

@oscarbenjamin
Copy link
Contributor Author

How should I move forward for opening the PR for that?

It isn't possible to write a PR that changes what code the user chooses to execute.

@smichr
Copy link
Member

smichr commented Nov 26, 2022

line 53 above could be changed to m, lhs = l.as_coeff_Mul(rational=True) if l else S.One, l. I wish S.Zero.as_coeff_Mul() gave (1,0) instead of (0, 1)...but that is a way to work around it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
functions.elementary.piecewise unevaluated Issues related to unevaluated expressions
Projects
None yet
Development

No branches or pull requests

3 participants