英文:
Analytically calculate integral of a function only under a given condition with Python-SymPy
问题
I am trying to analytically calculate the integral of a function using this code:
from sympy import sqrt, symbols, integrate, pi
x=symbols('x',real=True, positive=True)
rho, r=symbols('rho r', real=True, positive=True)
f=sqrt(rho**2-(x-r)**2)
integrate(f,(x,r,r+rho)).simplify()
SymPy tells me the result is the one in the picture:

however I want to evaluate only the second one. How can I do that?
英文:
I am trying to analytically calculate the integral of a function using this code:
from sympy import sqrt, symbols, integrate, pi
x=symbols('x',real=True, positive=True)
rho, r=symbols('rho r', real=True, positive=True)
f=sqrt(rho**2-(x-r)**2)
integrate(f,(x,r,r+rho)).simplify()
SymPy tells me the result is the one in the picture:

however I want to evaluate only the second one. How can I do that?
答案1
得分: 1
print(refine(integrate(f, (x, r, r + rho)).simplify(), (r - x)**2/rho**2 > 1))
# Integral(I*sqrt(r**2 - 2*r*x - rho**2 + x**2), (x, r, r + rho))
(我不确定为什么这个没有文档记录)
英文:
According to this comment, you can pass inequalities as an assumption to refine:
print(refine(integrate(f,(x,r,r+rho)).simplify(), (r - x)**2/rho**2 > 1))
# Integral(I*sqrt(r**2 - 2*r*x - rho**2 + x**2), (x, r, r + rho))
(I'm not sure why this is undocumented)
答案2
得分: 1
以下是SymPy给出的内容:
In [10]: i = integrate(f, (x, r, r + rho)).simplify()
In [11]: i
Out[11]: 
r + ρ                                                                                                                                                              
  ⌠                                                                                                                                                                
  ⎮   ⎧        2        2                         2                             2                            4                          2                  2       
  ⎮   ⎪     ⅈ⋅ρ ⋅(r - x)                       ⅈ⋅ρ                           ⅈ⋅ρ                    ⅈ⋅(r - x)                3⋅ⅈ⋅(r - x)            (r - x)        
  ⎮   ⎪─────────────────────── - ─────────────────────────────── - ─────────────────────── - ─────────────────────── + ───────────────────────  for ──────── > 1   
  ⎮   ⎪                    3/2       ____________   ____________        __________________                       3/2        __________________          2          
  ⎮   ⎪  ⎛   2           2⎞      2⋅╲╱ -r - ρ + x ⋋⋓⋂⋈⋐⋏⋍⋚⋏⋈⋍⋌⋋⋚⋃⋔⋈⋋⋚⋌⋃⋏⋃⋍⋎⋂⋋⋚⋌⋍⋏⋃⋍⋌⋏⋈⋏⋐⋏⋋⋚⋍⋂⋏⋈⋋⋚⋏⋎⋏⋏⋎⋋⋚⋏⋍⋌⋏⋏⋃⋎⋋⋎⋋⋐⋍⋚⋏⋌⋏⋐⋍⋍⋃⋋⋎⋂⋚⋐⋍⋎⋋⋏⋎⋏⋏⋍⋋⋚⋌⋍⋏⋎⋏⋏⋎⋋⋚⋏⋍⋏⋍⋎⋋⋚⋏⋃⋍⋃⋏⋏⋐⋏⋍⋍⋃⋏⋎⋋⋚⋏⋃⋎⋋⋏⋎⋏⋏⋍⋋⋚⋌⋍⋏⋎⋏⋏⋎⋋⋚⋏⋍⋏⋍⋎⋋⋚⋏⋃⋍⋃⋏⋏⋐⋏⋍⋍⋃⋏⋎⋋⋚⋏⋃⋎⋋⋏⋎⋏⋏⋍⋋⋚⋌⋍⋏⋎⋏⋏⋎⋋⋚⋏⋍⋏⋍⋎⋋⋚⋏⋃⋍⋃⋏⋏⋐⋏⋍⋍⋃⋏⋎⋋⋚⋏⋃⋎⋋⋏⋎⋏⋏⋍⋋⋚⋌⋍⋏⋎⋏⋏⋎⋋⋚⋏⋍⋏⋍⋎⋋⋚⋏⋃⋍⋃⋏⋏⋐⋏⋍⋍⋃⋏⋎⋋⋚⋏⋃⋎⋋⋏⋎⋏⋏⋍⋋⋚⋌⋍⋏⋎⋏⋏⋎⋋⋚⋏⋍⋏⋍⋎⋋⋚⋏⋃⋍⋃⋏⋏⋐⋏⋍⋍⋃⋏⋎⋋⋚⋏⋃⋎⋋⋏⋎⋏⋏⋍⋋⋚⋌⋍⋏⋎⋏⋏⋎⋋⋚⋏⋍⋏⋍⋎⋋⋚⋏⋃⋍⋃⋏⋏⋐⋏⋍⋍⋃⋏⋎⋋⋚⋏⋃⋎⋋⋏⋎⋏⋏⋍⋋⋚⋌⋍⋏⋎⋏⋏⋎⋋⋚⋏⋍⋏⋍⋎⋋⋚⋏⋃⋍⋃⋏⋏⋐⋏⋍⋍⋃⋏⋎⋋⋚⋏⋃⋎⋋⋏⋎⋏⋏⋍⋋⋚⋌⋍⋏⋎⋏⋏⋎⋋⋚⋏⋍⋏⋍⋎⋋⋚⋏⋃⋍⋃⋏⋏⋐⋏⋍⋍⋃⋏⋎⋋⋚⋏⋃
<details>
<summary>英文:</summary>
Here is what SymPy gives:
In [10]: i = integrate(f,(x,r,r+rho)).simplify()
In [11]: i
Out[11]:
r + ρ                                                                                                                                                           
⌠                                                                                                                                                             
⎮   ⎧        2        2                         2                             2                            4                          2                  2    
⎮   ⎪     ⅈ⋅ρ ⋅(r - x)                       ⅈ⋅ρ                           ⅈ⋅ρ                    ⅈ⋅(r - x)                3⋅ⅈ⋅(r - x)            (r - x)     
⎮   ⎪─────────────────────── - ─────────────────────────────── - ─────────────────────── - ─────────────────────── + ───────────────────────  for ──────── > 1
⎮   ⎪                    3/2       ____________   ____________        __________________                       3/2        __________________          2       
⎮   ⎪  ⎛   2           2⎞      2⋅╲╱ -r - ρ + x ⋅╲╱ -r + ρ + x        ╱    2           2      ⎛   2           2⎞          ╱    2           2          ρ        
⎮   ⎨2⋅⎝- ρ  + (-r + x) ⎠                                        2⋅╲╱  - ρ  + (-r + x)     2⋅⎝- ρ  + (-r + x) ⎠      2⋅╲╱  - ρ  + (-r + x)                     dx
⎮   ⎪                                                                                                                                                         
⎮   ⎪                                                             _______________                                                                             
⎮   ⎪                                                            ╱  2          2                                                                              
⎮   ⎪                                                          ╲╱  ρ  - (r - x)                                                                  otherwise    
⎮   ⎩                                                                                                                                                         
⌡                                                                                                                                                             
r
You say that you want the second case. In general we can extract cases from a Piecewise using .args but we need to bring the Piecewise out to top lvel using `piecewise_fold` first:
In [12]: piecewise_fold(i)
Out[12]:
⎧r + ρ                                                                                                                                                            
⎪  ⌠                                                                                                                                                              
⎪  ⎮   ⎛        2        2                         2                             2                            4                          2     ⎞                2 
⎪  ⎮   ⎜     ⅈ⋅ρ ⋅(r - x)                       ⅈ⋅ρ                           ⅈ⋅ρ                    ⅈ⋅(r - x)                3⋅ⅈ⋅(r - x)      ⎟         (r - x)  
⎪  ⎮   ⎜─────────────────────── - ─────────────────────────────── - ─────────────────────── - ─────────────────────── + ───────────────────────⎟ dx  for ──────── > 1
⎪  ⎮   ⎜                    3/2       ____________   ____________        __________________                       3/2        __________________⎟             2    
⎪  ⎮   ⎜  ⎛   2           2⎞      2⋅╲╱ -r - ρ + x ⋅╲╱ -r + ρ + x        ╱    2           2      ⎛   2           2⎞          ╱    2           2 ⎟            ρ     
⎪  ⎮   ⎝2⋅⎝- ρ  + (-r + x) ⎠                                        2⋅╲╱  - ρ  + (-r + x)     2⋅⎝- ρ  + (-r + x) ⎠      2⋅╲╱  - ρ  + (-r + x)  ⎠                  
⎪  ⌡                                                                                                                                                              
⎨  r                                                                                                                                                              
⎪                                                                                                                                                                 
⎪                                                           r + ρ                                                                                                 
⎪                                                             ⌠                                                                                                   
⎪                                                             ⎮      _______________                                                                              
⎪                                                             ⎮     ╱  2          2                                                                               
⎪                                                             ⎮   ╲╱  ρ  - (r - x)   dx                                                                 otherwise 
⎪                                                             ⌡                                                                                                   
⎪                                                             r                                                                                                   
⎩
In [13]: piecewise_fold(i).args[0][0]
Out[13]:
r + ρ                                                                                                                                          
⌠                                                                                                                                            
⎮   ⎛        2        2                         2                             2                            4                          2     ⎞
⎮   ⎜     ⅈ⋅ρ ⋅(r - x)                       ⅈ⋅ρ                           ⅈ⋅ρ                    ⅈ⋅(r - x)                3⋅ⅈ⋅(r - x)      ⎟
⎮   ⎜─────────────────────── - ─────────────────────────────── - ─────────────────────── - ─────────────────────── + ───────────────────────⎟ dx
⎮   ⎜                    3/2       ____________   ____________        __________________                       3/2        __________________⎟
⎮   ⎜  ⎛   2           2⎞      2⋅╲╱ -r - ρ + x ⋅╲╱ -r + ρ + x        ╱    2           2      ⎛   2           2⎞          ╱    2           2 ⎟
⎮   ⎝2⋅⎝- ρ  + (-r + x) ⎠                                        2⋅╲╱  - ρ  + (-r + x)     2⋅⎝- ρ  + (-r + x) ⎠      2⋅╲╱  - ρ  + (-r + x)  ⎠
⌡                                                                                                                                            
r
In [14]: piecewise_fold(i).args[1][0]
Out[14]:
r + ρ                   
⌠                     
⎮      _______________
⎮     ╱  2          2 
⎮   ╲╱  ρ  - (r - x)   dx
⌡                     
r
That seems to be what you wanted. However it is just an unevaluated integral. If that is actually what you want then you should use `Integral` rather than `integrate` in the first place:
In [15]: Integral(f,(x,r,r+rho))
Out[15]:
r + ρ                    
⌠                      
⎮      ________________
⎮     ╱  2           2 
⎮   ╲╱  ρ  - (-r + x)   dx
⌡                      
r
The difference between `integrate` and `Integral` is that `Integral` just represents the integral symbolically whereas `integrate` will attempt to *compute* the integral ideally giving an expression that does not involve integrals:
In [16]: Integral(x, x)
Out[16]:
⌠  
⎮ x dx
⌡
In [17]: integrate(x, x)
Out[17]:
2
x
──
2
In [18]: Integral(x, x).doit()
Out[18]:
2
x
──
2
</details>
# 答案3
**得分**: 1
好的,我明白你现在在问什么。在这里,我不确定为什么 `integrate` 返回它所返回的结果,但手动积分方法将计算这个积分:
```python
In [3]: f.integrate((x, r, r+rho), manual=True)
Out[3]: 
            ____________________________________
   2       ╱    2                  2          2 
π⋅ρ    ρ⋅╲╱  - r  + 2⋅r⋅(r + ρ) + ρ  - (r + ρ)  
──── + ─────────────────────────────────────────
 4                         2                    
In [4]: f.integrate((x, r, r+rho), manual=True).simplify()
Out[4]: 
   2
π⋅ρ 
────
 4  
这是使用 SymPy 1.11.1。
英文:
Okay, I see what you are asking now. Here I am not sure why integrate returns the result that it does but the manual integration method will compute this integral:
In [3]: f.integrate((x, r, r+rho), manual=True)
Out[3]: 
            ____________________________________
   2       ╱    2                  2          2 
π⋅ρ    ρ⋅╲╱  - r  + 2⋅r⋅(r + ρ) + ρ  - (r + ρ)  
──── + ─────────────────────────────────────────
 4                         2                    
In [4]: f.integrate((x, r, r+rho), manual=True).simplify()
Out[4]: 
   2
π⋅ρ 
────
 4  
That is using SymPy 1.11.1.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论