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
simplify(Piecewise) doesn't simplify the conditionals enough #16690
Comments
Hi, I'm a student new to open source project can I look into this issue and work on it?
I would considering to add simplify_logic again to the conditionals in Piecewise()._eval_simplified() after expressions have been merged. For y = 0 ∧ x ≥ y to be further simplified to y = 0 ∧ x ≥ 0, it is beyond the scope of logic_simplify, but simplify() looks like already taken care of this:
|
I think that should be a reasonable fix, though you should try it and see how it affects the tests. Where does the |
I beg to differ. It cannot simplify to |
I revisited this one now. First, my comment above is incorrect. It can indeed be simplified to that. I believe that the things is that the PoS form is selected as it is simpler than the SoP form before any relational simplifications. Then, there are no relational simplifications to be done once the PoS form is selected. (Btw, running simplify on the Piecewise again leads to 0.) |
The condition shouldn't simplify away completely because this should give nan for some possible values: In [6]: p = Piecewise((x*y, And(x >= y, Eq(y, 0))), (x, Eq(x, 0)))
In [7]: p
Out[7]:
⎧x⋅y for y = 0 ∧ x ≥ y
⎨
⎩ x for x = 0
In [8]: simplify(p)
Out[8]: {0 for (x = 0 ∨ y = 0) ∧ (x = 0 ∨ x ≥ 0)
In [9]: simplify(_)
Out[9]: 0
In [10]: p.subs(y, 1)
Out[10]: {x for x = 0
In [11]: p.subs(y, 1).subs(x, 1)
Out[11]: nan |
I was also surprised. I wonder if one should add a Doing that by hand will now give: |
There is always an implicit In [15]: Piecewise((1, False))
Out[15]: nan We need to make sure that conditions don't simplify away if they are not known to be True though. |
Sure, but it is not visible and clearly some of the simplification routines does not consider segments not explicitly there. So although not fixing the underlying problem, it would help, both to make people understand what happens and solve the erroneous behavior. |
Maybe it would indeed help to explicitly add that to the args, although I don't think the printers should print it. |
It might help but really there's just a lot of buggy code in Piecewise. The fundamental tension is between Piecewise as a purely logical construct |
I just remind you of #17443 I may have a bit of time to work on this over the weekend and think it would be quite nice to get it in, even in the current, probably suboptimal condition (the ordering of simplifications is really a problem and I do not think it can be solved rather than heuristically). At least I do not add any Another way would of course be to support multi-variate expressions for There is also the aspect of being able to do something "perfect", but not have time to do it, and to make something that improves the current state in reasonable time. Always adding |
If you can work on refactoring Piecewise then that would be great. I think that the first thing to do is ignore inequalities and focus on equalities and unequalities. Those are actually a more common use of Piecewise anyway and simplifying them is much easier and should not be messed up by logic designed for inequalities. See also #21360 |
I don't think that |
There are two different aspects here: 1) use knowledge of equalities/unequalities to reduce the number of segments, 2) use general simplification techniques to simplify the conditions. I am quite sure that the only was using inequalities is used to simplify the number of segments is e.g. Btw, using #17443 on this gives |
The text was updated successfully, but these errors were encountered: