sympyの積分(integrate)で、piecewiseが使えた。

例1の(3)の範囲の判定方法がありますか？教えて下さい。
(0 < x) & (x <3) ), (2 < x) & (x <4) )

# original link

＞Piecewiseを用いて積分できます。

https://teratail.com/questions/281288

# 例1(4分割)

https://qiita.com/mrrclb48z/items/12c5790aee4baac90008


以下サイトに、ソースコードを貼り付けて、Evaluateです。

https://live.sympy.org/


```python
from sympy import *
var("x y")
f=Piecewise((2*x                               , (0 <= x) & (x <=2) ),
            (solve((x-2)**2+(y-2)**2-2**2,y)[1], (2 <= x) & (x <=4) )  )
g=0.5*x
ans = integrate(f-g,(x,0,4))
print("# (1) <=<=<=<= 積分結果",ans,"{:.5f}".format(float(ans)))
#
f=Piecewise((2*x                               , (0 < x) & (x <2) ),
            (solve((x-2)**2+(y-2)**2-2**2,y)[1], (2 < x) & (x <4) )  )
g=0.5*x
ans = integrate(f-g,(x,0,4))
print("# (2) < < < <  積分結果",ans,"{:.5f}".format(float(ans)))
#
f=Piecewise((2*x                               , (0 < x) & (x <3) ),
            (solve((x-2)**2+(y-2)**2-2**2,y)[1], (2 < x) & (x <4) )  )
g=0.5*x
ans = integrate(f-g,(x,0,4))
print("# (3) ? ? ? ?  積分結果",ans,"{:.5f}".format(float(ans)))
# (1) <=<=<=<= 積分結果 1.0*pi + 4.0 7.14159
# (2) < < < <  積分結果 1.0*pi + 4.0 7.14159
# (3) ? ? ? ?  積分結果 -sqrt(3)/2 + 0.666666666666667*pi + 7.0 8.22837
```
# 例2(3分割)

```python
from sympy import *
var("x y")
f=Piecewise((2*x                               , x<=2 ),
            (solve((x-2)**2+(y-2)**2-2**2,y)[1], x<=4 )  )
g=0.5*x
ans = integrate(f-g,(x,0,4))
print("# (1) <= 積分結果",ans,"{:.5f}".format(float(ans)))
#
f=Piecewise((2*x                               , x<2 ),
            (solve((x-2)**2+(y-2)**2-2**2,y)[1], x<4 )  )
g=0.5*x
ans = integrate(f-g,(x,0,4))
print("# (2) <  積分結果",ans,"{:.5f}".format(float(ans)))
f=Piecewise((2*x                               , x<=2 ),
            (solve((x-2)**2+(y-2)**2-2**2,y)[1], 2<=x )  )
g=0.5*x
ans = integrate(f-g,(x,0,4))
print("# (3) <= 積分結果",ans,"{:.5f}".format(float(ans)))
#
f=Piecewise((2*x                               , x<2 ),
            (solve((x-2)**2+(y-2)**2-2**2,y)[1], 2<x )  )
g=0.5*x
ans = integrate(f-g,(x,0,4))
print("# (4) <  積分結果",ans,"{:.5f}".format(float(ans)))
# (1) <= 積分結果 1.0*pi + 4.0 7.14159
# (2) <  積分結果 1.0*pi + 4.0 7.14159
# (3) <= 積分結果 1.0*pi + 4.0 7.14159
# (4) <  積分結果 1.0*pi + 4.0 7.14159
```
# 例3(3分割)

```python
from sympy import *
var("x y")
f=Piecewise((2*x                               , x <=2 ),
            (solve((x-2)**2+(y-2)**2-2**2,y)[1], x <=4 )  )
g=0.5*x
ans = integrate(f-g,(x,0,4))
print("# (1) <= 積分結果",ans,"{:.5f}".format(float(ans)))
#
f=Piecewise((2*x                               , x <2 ),
            (solve((x-2)**2+(y-2)**2-2**2,y)[1], x <4 )  )
g=0.5*x
ans = integrate(f-g,(x,0,4))
print("# (2) <  積分結果",ans,"{:.5f}".format(float(ans)))
f=Piecewise((2*x                               , x <=2 ),
            (solve((x-2)**2+(y-2)**2-2**2,y)[1], 2 <=x )  )
g=0.5*x
ans = integrate(f-g,(x,0,4))
print("# (3) <= 積分結果",ans,"{:.5f}".format(float(ans)))
#
f=Piecewise((2*x                               , x <2 ),
            (solve((x-2)**2+(y-2)**2-2**2,y)[1], 2 <x )  )
g=0.5*x
ans = integrate(f-g,(x,0,4))
print("# (4) <  積分結果",ans,"{:.5f}".format(float(ans)))
# (1) <= 積分結果 1.0*pi + 4.0 7.14159
# (2) <  積分結果 1.0*pi + 4.0 7.14159
# (3) <= 積分結果 1.0*pi + 4.0 7.14159
# (4) <  積分結果 1.0*pi + 4.0 7.14159
```

# 例4(3分割)

```python
from sympy import *
var("x y")
f=Piecewise((solve((x-2)**2+(y-2)**2-2**2,y)[1], x <=4 ),
            (2*x                               , x <=2 ))
g=0.5*x
ans = integrate(f-g,(x,0,4))
print("# (1) <= 積分結果",ans,"{:.5f}".format(float(ans)))
#
f=Piecewise((solve((x-2)**2+(y-2)**2-2**2,y)[1], x <4 ),
            (2*x                               , x <2 ))
g=0.5*x
ans = integrate(f-g,(x,0,4))
print("# (2) <  積分結果",ans,"{:.5f}".format(float(ans)))
f=Piecewise((solve((x-2)**2+(y-2)**2-2**2,y)[1], 2 <=x ),
            (2*x                               , x <=2 ))
g=0.5*x
ans = integrate(f-g,(x,0,4))
print("# (3) <= 積分結果",ans,"{:.5f}".format(float(ans)))
#
f=Piecewise((solve((x-2)**2+(y-2)**2-2**2,y)[1], 2 <x ),
            (2*x                               , x <2 ))
g=0.5*x
ans = integrate(f-g,(x,0,4))
print("# (4) <  積分結果",ans,"{:.5f}".format(float(ans)))
# (1) <= 積分結果 4.0 + 2.0*pi 10.28319
# (2) <  積分結果 4.0 + 2.0*pi 10.28319
# (3) <= 積分結果 1.0*pi + 4.0 7.14159
# (4) <  積分結果 1.0*pi + 4.0 7.14159
```

# 参考
sympy>piecewis

https://docs.sympy.org/latest/modules/functions/elementary.html#piecewis