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

revisit Piecewise auto-simplification #25587

Open
smichr opened this issue Aug 24, 2023 · 0 comments
Open

revisit Piecewise auto-simplification #25587

smichr opened this issue Aug 24, 2023 · 0 comments

Comments

@smichr
Copy link
Member

smichr commented Aug 24, 2023

Consider the following two Piecewise expresions: one with mostly nonstrict relationships:

p1 = Piecewise(
(nan, x <= 0), 
(20*x - 3*(x - 6)**2/2 - 176, (x >= 0) & (x >= 2) & (x>= 4) & (x >= 6) & (x < 10)), 
(100 - 26*x, (x >= 0) & (x >= 2) & (x >= 4) & (x < 10)), 
    (16*x - 3*(x - 6)**2/2 - 176, (x >= 2) & (x >= 4) & (x >= 6) & (x < 10)), 
    (100 - 30*x, (x >= 2) & (x >= 4) & (x < 10)), 
(30*x - 3*(x - 6)**2/2 - 196, (x>= 0) & (x >= 4) & (x >= 6) & (x < 10)), 
(80 - 16*x, (x >= 0) & (x >= 4) & (x <10)), 
    (26*x - 3*(x - 6)**2/2 - 196, (x >= 4) & (x >= 6) & (x < 10)), 
    (80 - 20*x, (x >= 4) & (x < 10)), 
(40*x - 3*(x - 6)**2/2 - 256, (x >= 0) & (x >= 2) & (x >= 6) & (x < 10)), 
(20 - 6*x, (x >= 0) & (x >= 2) & (x < 10)), 
    (36*x - 3*(x - 6)**2/2 - 256, (x >= 2) & (x >= 6) & (x < 10)), 
    (20 - 10*x, (x >= 2) & (x < 10)), 
(50*x - 3*(x - 6)**2/2 - 276, (x >= 0) & (x >= 6) & (x < 10)), 
(4*x, (x >= 0) & (x < 10)), 
    (46*x - 3*(x - 6)**2/2 - 276, (x >= 6) & (x < 10)), 
    (0, x < 10), 
(nan,True))

p2 = Piecewise(
(nan, x <= 0), 
(20*x - 3*(x - 6)**2/2 - 176, (x > 0) & (x > 2) & (x> 4) & (x > 6) & (x < 10)), 
(100 - 26*x, (x > 0) & (x > 2) & (x > 4) & (x < 10)), 
    (16*x - 3*(x - 6)**2/2 - 176, (x > 2) & (x > 4) & (x > 6) & (x < 10)), 
    (100 - 30*x, (x > 2) & (x > 4) & (x < 10)), 
(30*x - 3*(x - 6)**2/2 - 196, (x> 0) & (x > 4) & (x > 6) & (x < 10)), 
(80 - 16*x, (x > 0) & (x > 4) & (x <10)), 
    (26*x - 3*(x - 6)**2/2 - 196, (x > 4) & (x > 6) & (x < 10)), 
    (80 - 20*x, (x > 4) & (x < 10)), 
(40*x - 3*(x - 6)**2/2 - 256, (x > 0) & (x > 2) & (x > 6) & (x < 10)), 
(20 - 6*x, (x > 0) & (x > 2) & (x < 10)), 
    (36*x - 3*(x - 6)**2/2 - 256, (x > 2) & (x > 6) & (x < 10)), 
    (20 - 10*x, (x > 2) & (x < 10)), 
(50*x - 3*(x - 6)**2/2 - 276, (x > 0) & (x > 6) & (x < 10)), 
(4*x, (x > 0) & (x < 10)), 
    (46*x - 3*(x - 6)**2/2 - 276, (x > 6) & (x < 10)), 
    (0, x < 10), 
(nan,True))

>>> [len(i.args) for i in (p1,p2)]
[18, 10]

Notice that in p1 there is no collapsing of redundant args (the ones that are indented). This leads to a spurious "0 expression" near the end that prevents solve from solving this expression for x = 10/3.

If simple univariate clauses like (x > 0) & (x > 6) & (x < 10) would auto-simplify to (x > 6) & (x < 10) this would help. Is that too much automatic evaluation, however?

Also, I see that boolalg.simplify_univariate and piecewise.piecewise_simplify_arguments have some overlap and one can perhaps use the other.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant