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

Summation with Piecewise not computed #13547

Closed
asmeurer opened this issue Oct 28, 2017 · 6 comments
Closed

Summation with Piecewise not computed #13547

asmeurer opened this issue Oct 28, 2017 · 6 comments
Labels

Comments

@asmeurer
Copy link
Member

Based on https://stackoverflow.com/questions/46966198/how-to-set-index-as-variables. I believe the loops reduce to

>>> summation(Piecewise((a_x**2 + a_x, (j**2 - j > 1) & (k/2 > 20)), (0, True)), (j, 0, 1000-1), (k, 0, 10000-1), (i, 0, 10000-1))
Sum(Piecewise((a_x**2 + a_x, (k/2 > 20) & (j**2 - j > 1)), (0, True)), (j, 0, 999), (k, 0, 9999), (i, 0, 9999))

which doesn't evaluate or simplify at all. Even if you replace a_x with a number it doesn't evaluate

>>> summation(Piecewise((a_x**2 + a_x, (j**2 - j > 1) & (k/2 > 20)), (0, True)), (j, 0, 1000-1), (k, 0, 10000-1), (i, 0, 10000-1)).subs(a_x, 2).doit()
Sum(Piecewise((6, (k/2 > 20) & (j**2 - j > 1)), (0, True)), (j, 0, 999), (k, 0, 9999), (i, 0, 9999))
>>> summation(Piecewise((a_x**2 + a_x, (j**2 - j > 1) & (k/2 > 20)), (0, True)), (j, 0, 1000-1), (k, 0, 10000-1), (i, 0, 10000-1)).subs(a_x, 2).evalf()
Sum(Piecewise((6, (k/2 > 20) & (j**2 - j > 1)), (0, True)), (j, 0, 999), (k, 0, 9999), (i, 0, 9999))

The answer on the SO post claims it should be 995900780000*(a_x**2 + a_x), which I have not verified.

@smichr
Copy link
Member

smichr commented Oct 30, 2017

Ahh. piecewise_fold gets this wrong, too, because it doesn't consider the interaction between the summation and the conditions.

@smichr
Copy link
Member

smichr commented Oct 30, 2017

Seems to me it should reduce to

summation(a_x**2 + a_x, (j, 2, 1000-1), (k, 41, 10000-1), (i, 0, 10000-1))
99390820000*(a_x**2 + a_x)

@vishalg2235
Copy link
Contributor

Summation limits of 'j' is limiting to total 100 iterations. like, it is computing for (j,0,99),(j,1,100),(j,10,109) and not computing for (j,0,100),(j,10,110) where 'j' iterations are going more than 100 times.

>>>summation(Piecewise((a_x**2 + a_x, (j**2 - j > 1) & (k/2 > 20)), (0, True)), (j, 0, 100-1), (k, 0, 10000-1), (i, 0, 10000-1))
9759820000*a_x**2 + 9759820000*a_x
>>>summation(Piecewise((a_x**2 + a_x, (j**2 - j > 1) & (k/2 > 20)), (0, True)), (j, 0, 1000-1), (k, 0, 10000-1), (i, 0, 10000-1))
Sum(Piecewise((a_x**2 + a_x, (k/2 > 20) & (j**2 - j > 1)), (0, True)), (j, 0, 999), (k, 0, 9999), (i, 0, 9999))
>>>summation(Piecewise((a_x**2 + a_x, (j**2 - j > 1) & (k/2 > 20)), (0, True)), (j, 0, 101-1), (k, 0, 10000-1), (i, 0, 10000-1))
Sum(Piecewise((a_x**2 + a_x, (k/2 > 20) & (j**2 - j > 1)), (0, True)), (j, 0, 100), (k, 0, 9999), (i, 0, 9999))
>>> summation(Piecewise((a_x**2 + a_x, (j**2 - j > 1) & (k/2 > 20)), (0, True)), (j, 1, 101-1), (k, 0, 10000-1), (i, 0, 10000-1))
9859410000*a_x**2 + 9859410000*a_x
>>> summation(Piecewise((a_x**2 + a_x, (j**2 - j > 1) & (k/2 > 20)), (0, True)), (j, 10, 111-1), (k, 0, 10000-1), (i, 0, 10000-1))
Sum(Piecewise((a_x**2 + a_x, (k/2 > 20) & (j**2 - j > 1)), (0, True)), (j, 10, 110), (k, 0, 9999), (i, 0, 9999))
>>> summation(Piecewise((a_x**2 + a_x, (j**2 - j > 1) & (k/2 > 20)), (0, True)), (j, 10, 110-1), (k, 0, 10000-1), (i, 0, 10000-1))
9959000000*a_x**2 + 9959000000*a_x
>>> summation(Piecewise((a_x**2 + a_x, (j**2 - j > 1) & (k/2 > 20)), (0, True)), (j, 10, 120-1), (k, 0, 10000-1), (i, 0, 10000-1))
Sum(Piecewise((a_x**2 + a_x, (k/2 > 20) & (j**2 - j > 1)), (0, True)), (j, 10, 119), (k, 0, 9999), (i, 0, 9999))

Also seems to me it must reduce to

>>>summation(a_x**2 + a_x, (j, 2, 1000-1), (k, 42, 10000-1), (i, 0, 10000-1))
99380840000*a_x**2 + 99380840000*a_x

because :

>>> k = 41
>>> k/2
20

@vishalg2235
Copy link
Contributor

vishalg2235 commented Nov 5, 2017

ping @asmeurer @smichr

@BananaOrbit
Copy link

I'm not sure if this is related but there is also an issue with plotting Piecewise. #13320

@smichr
Copy link
Member

smichr commented Mar 14, 2019

dup of #12631

@smichr smichr closed this as completed Mar 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants