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 Convolution Yields Overlapping Cases #23761

Open
sjtrny opened this issue Jul 12, 2022 · 2 comments
Open

Piecewise Convolution Yields Overlapping Cases #23761

sjtrny opened this issue Jul 12, 2022 · 2 comments

Comments

@sjtrny
Copy link

sjtrny commented Jul 12, 2022

The convolution shown below of two piecewise functions appears to result in overlapping cases with varying degrees of severity, depending on the sympy version:

  • 1.7<=1.9 one overlapping case
  • 1.10.1 multiple overlapping case

Code

from sympy import oo, symbols, integrate, Piecewise, Max, Min, pprint

x = symbols("x", real=True)
y = symbols("y", real=True)

f = Piecewise((1/(-Max(0, y) + Min(1, y + 1)), (x <= -Max(0, y)) & (x >= -Min(1, y + 1))), (0, True))
g = Piecewise((1, (x >= 0) & (x <= 1)), (0, True))

tau = symbols('tau', real=True)
pprint(integrate(f.subs(x, tau) * g.subs(x, x - tau), (tau, -oo, +oo)), use_unicode=False)

Output for 1.7<=1.9

Overlapping case y=0 and y ≤ 0.

/        -Max(-1, x - 1) + Max(-1, x - 1, Min(0, x))           for y = 0 
|                                                                        
|  Max(-1, x - 1, -y - 1)   Max(-1, x - 1, -y - 1, Min(0, x))            
|- ---------------------- + ---------------------------------  for y <= 0
<          y + 1                          y + 1                          
|                                                                        
|        Max(-1, x - 1)   Max(-1, x - 1, Min(0, x, -y))                  
|      - -------------- + -----------------------------        otherwise 
\            1 - y                    1 - y                                             

Output for 1.10

Multiple cases overlapping at y = 0.

/          -Max(-1, x - 1) + Max(-1, x - 1, Min(0, x))                 for y = 0
|                                                                             
|-Max(-1, x - 1, -y - 1) + Max(-1, x - 1, -y - 1, Min(0, x, -y))  for Or(y = 0, y < 0)
|                                                                             
|           Max(-1, x - 1)   Max(-1, x - 1, Min(x, -y))                       
|         - -------------- + --------------------------                for y > 0
|               1 - y                  1 - y                                  
<                                                                             
|         Max(-1, x - 1)   Max(-1, x - 1, Min(0, x, -y))                      
|       - -------------- + -----------------------------               for y >= 0  
|             1 - y                    1 - y                                  
|                                                                             
|   Max(-1, x - 1, -y - 1)   Max(-1, x - 1, -y - 1, Min(0, x))                
| - ---------------------- + ---------------------------------         otherwise
\           y + 1                          y + 1                     
@sjtrny sjtrny changed the title Piecewise Convolution Problems Piecewise Convolution Yields Overlapping Cases Jul 12, 2022
@oscarbenjamin
Copy link
Contributor

The simplified result doesn't have any overlapping cases:

In [7]: res.simplify()
Out[7]: 
⎧-Max(-1, x - 1, -y - 1) + Max(-1, x - 1, -y - 1, Min(0, x, -y))  for y = 0y0
⎪                                                                                  
⎨          Max(-1, x - 1) - Max(-1, x - 1, Min(x, -y))                             
⎪          ───────────────────────────────────────────                otherwisey - 1 

The condition y = 0 or y <= 0 can obviously be simpler though.

Probably there should be better simplification done automatically by integrate (not a full blown simplify but something specific for Piecewise).

@oscargus
Copy link
Contributor

Also note that SymPy does use overlapping cases, evaluated from top to bottom (note that the last condition is typically True, although printed "Otherwise").

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

3 participants