The piecewise function integral gives an incorrect result

Using the latest version of sympy (0.7.6), I get the following bad result when defining a function integral with support [0, y):

from sympy import * a,b,c,x,z = symbols("a,b,c,x,z",real = True) y = Symbol("y",real=True,positive=True) inner = Piecewise((0,(x>=y)|(x<0)|(b>c)),(a,True)) I = Integral(inner,(x,0,z)) Eq(I,I.doit()) 

This is not true, since the actual result should swap the last two cases. This can be confirmed by checking the derivative:

 Derivative(I.doit(),z).doit().simplify().subs(z,x) 

which decreases to 0 everywhere.

Interestingly, when deleting condition (b>c) by replacing inner = Piecewise((0,(x>=y)|(x<0)),(a,True)) I get a TypeError error:

 TypeError: cannot determine truth value of -oo < y 

Am I using the library incorrectly or is this a serious mistake?

+6
source share
1 answer

Yes, sympy 0.7.6 is wrong in this case, and in some other cases. In general, I do not know of any symbolic mathematical package that I would trust to make a calculus with piecewise defined functions.

Please note that although

 inner = Piecewise((0, (x>=y)|(x<0)), (a,True)) 

throws TypeError during integration, logically equivalent definition

 inner = Piecewise((a, (x<y)&(x>=0)), (0,True)) 

leads to the correct result

 Piecewise((a*z, And(z < y, z >= 0)), (0, And(z <= 0, z >= -oo)), (a*y, True)) 

By the way, the previous version, sympy 0.7.5, handles

 inner = Piecewise( (0, (x>=y)|(x<0)), (a,True) ) 

without TypeError, creating the correct result (in another form):

 Piecewise((0, z <= 0), (a*y, z >= y), (a*z, True)) 

Here is another simpler example of erroneous behavior:

 >>> Integral(Piecewise((1,(x<1)|(z<x)), (0,True)) ,(x,0,2)).doit() -Max(0, Min(2, Max(0, z))) + 3 >>> Integral(Piecewise((1,(x<1)|(x>z)), (0,True)) ,(x,0,2)).doit() -Max(0, Min(2, Max(1, z))) + 3 

The first result is incorrect (for example, it fails at z = 0). The second is correct. The only difference between the two formulas is: z<x vs x>z .

+4
source

Source: https://habr.com/ru/post/986932/


All Articles