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_fold does not handle bound variables #12631

Open
dniku opened this issue May 11, 2017 · 7 comments
Open

piecewise_fold does not handle bound variables #12631

dniku opened this issue May 11, 2017 · 7 comments

Comments

@dniku
Copy link

dniku commented May 11, 2017

Sympy 1.0-2 from Manjaro repository.

In [1]: import sympy as sp

In [2]: sp.init_session()
IPython console for SymPy 1.0 (Python 3.6.0-64-bit) (ground types: gmpy)

These commands were executed:
>>> from __future__ import division
>>> from sympy import *
>>> x, y, z, t = symbols('x y z t')
>>> k, m, n = symbols('k m n', integer=True)
>>> f, g, h = symbols('f g h', cls=Function)
>>> init_printing()

Documentation can be found at http://docs.sympy.org/1.0/

In [3]: k, i, n = sp.symbols('k, i, n', integer=True)

In [4]: p = sp.SeqFormula(sp.Piecewise((k, sp.Eq(i, 0)), (0, True)), (i, 0, 5))

In [5]: p
Out[5]: [k, 0, 0, 0, …]

In [6]: sp.piecewise_fold(p)
Out[6]: 
⎧[k, k, k, k, …]  for i = 0
⎨                          
⎩[0, 0, 0, 0, …]  otherwise

Needless to say, [5] and [6] should be identical.

@hanq08
Copy link
Contributor

hanq08 commented May 11, 2017

Maybe I'm missing something but I don't see any error here. Why [5] and [6] should be identical?

picewise_fold() takes an expression containing a piecewise function and returns the expression in piecewise form.

[6] seems to be a correct representation of the piecewise function p defined in [4].

@dniku
Copy link
Author

dniku commented May 11, 2017

@hanq08 in this case, i is a variable bound in the second argument of SeqFormula in [4]. It is an index that is fed into the function within SeqFormula. In [6], i becomes a free variable.

The object defined in [4] is printed in [5]. [6] is different from [5], hence it is not a correct representation of the object defined in [4].

@smichr
Copy link
Member

smichr commented Jul 19, 2017

Does this work with the new piecewise_fold function?

@dniku
Copy link
Author

dniku commented Jul 23, 2017

@smichr I haven't tested it and I probably won't for a long time.

@oscarbenjamin
Copy link
Contributor

I get the same on current master.

@smichr
Copy link
Member

smichr commented Mar 9, 2019

I suspect that functions should have their own _eval_piecewise_fold that can override the assumed simplicity in the folding of a function (see inline notes of piecewise_fold).

@oscargus
Copy link
Contributor

oscargus commented Aug 2, 2019

Maybe an easier to grasp example (found this when I was just about to post a new issue):

In [13]: Integral(Piecewise((1, x >= 1), (2, True)), (x, 0, 2))
Out[13]: 
2                 
⌠                 
⎮ ⎧1  for x ≥ 1   
⎮ ⎨             dx
⎮ ⎩2  otherwise   
⌡                 
0                 

In [14]: Integral(Piecewise((1, x >= 1), (2, True)), (x, 0, 2)).doit()
Out[14]: 3

In [15]: piecewise_fold(Integral(Piecewise((1, x >= 1), (2, True)), (x, 0, 2))).doit()
Out[15]: 
⎧2  for x ≥ 1
⎨            
⎩4  otherwise

It shouldn't be folding if the Piecewise is the argument of an integral or, ideally, do it in a different way

2        1     
⌠        ⌠     
⎮ 1 dx + ⎮ 2 dx
⌡        ⌡     
1        0     

which will be feasible for simple constraints.

For indefinite integrals it is more tricky though.

In [18]: (Integral(Piecewise((1, x >= 1), (2, True)), (x))).doit()
Out[18]: 
⎧ 2⋅x   for x ≤ 1
⎨                
⎩x + 1  otherwise

In [19]: piecewise_fold(Integral(Piecewise((1, x >= 1), (2, True)), (x))).doit()
Out[19]: 
⎧ x   for x ≥ 1
⎨              
⎩2⋅x  otherwise

In [20]: piecewise_fold(Integral(Piecewise((1, x >= 1), (2, True)), (x)).doit())
Out[20]: 
⎧ 2⋅x   for x ≤ 1
⎨                
⎩x + 1  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

5 participants