python is a beginner.
I do not know how to deal with the following phenomenon.
Can you tell me

  • I want to integrate using Sympy in python.
  • I want to put an if statement in the expression that does the integration.
File "/usr/local/lib/python3.6/dist-packages/sympy/core/relational.py", line 384, in __nonzero__
    raise TypeError("cannot determine truth value of Relational")
TypeError: cannot determine truth value of Relational
Applicable source code
import sympy
def func(r):
    if r >0.5: #<-the above error occurs here
        result = sympy.sin(r)/(sympy.pi*r) * sympy.cos(r)
        result = sympy.sin(r)/(sympy.pi*r) * sympy.sin(r)
    return result
if __name__ =='__main__':
    r = sympy.Symbol('r')
    result = sympy.integrate(func(r) ,(r,-sympy.oo,sympy.oo))
What I tried

When I checked it, there was a function called Piecewise, so I tried using it, but I got an error.

Supplementary information (FW/tool ​​version, etc.)

OS: Windows10 WSL1 Ubuntu18.04
python version:3.6.9

  • Answer # 1

    It can be integrated using Piecewise.

    import sympy as sp
    r = sp.Symbol('r', real=True)
    f = sp.Piecewise((sp.sin(r)/(sp.pi * r) * sp.cos(r), r >= 0.5),
                     (sp.sin(r)/(sp.pi * r) * sp.sin(r), r<0.5))
    sp.integrate(f, (r, -sp.oo, sp.oo))