使用Python-SymPy在给定条件下分析计算函数的积分。

huangapple go评论94阅读模式
英文:

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:

  1. from sympy import sqrt, symbols, integrate, pi
  2. x=symbols('x',real=True, positive=True)
  3. rho, r=symbols('rho r', real=True, positive=True)
  4. f=sqrt(rho**2-(x-r)**2)
  5. integrate(f,(x,r,r+rho)).simplify()

SymPy tells me the result is the one in the picture:
使用Python-SymPy在给定条件下分析计算函数的积分。

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:

  1. from sympy import sqrt, symbols, integrate, pi
  2. x=symbols('x',real=True, positive=True)
  3. rho, r=symbols('rho r', real=True, positive=True)
  4. f=sqrt(rho**2-(x-r)**2)
  5. integrate(f,(x,r,r+rho)).simplify()

SymPy tells me the result is the one in the picture:
使用Python-SymPy在给定条件下分析计算函数的积分。

however I want to evaluate only the second one. How can I do that?

答案1

得分: 1

根据这个评论,你可以将不等式作为假设传递给refine

  1. print(refine(integrate(f, (x, r, r + rho)).simplify(), (r - x)**2/rho**2 > 1))
  2. # 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:

  1. print(refine(integrate(f,(x,r,r+rho)).simplify(), (r - x)**2/rho**2 > 1))
  2. # 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给出的内容:

  1. In [10]: i = integrate(f, (x, r, r + rho)).simplify()
  2. In [11]: i
  3. Out[11]:
  4. r + ρ
  5. 2 2 2 2 4 2 2
  6. ρ (r - x) ρ ρ (r - x) 3(r - x) (r - x)
  7. ⎪─────────────────────── - ─────────────────────────────── - ─────────────────────── - ─────────────────────── + ─────────────────────── for ──────── > 1
  8. 3/2 ____________ ____________ __________________ 3/2 __________________ 2
  9. 2 2 2⋅╲╱ -r - ρ + x ⋋⋓⋂⋈⋐⋏⋍⋚⋏⋈⋍⋌⋋⋚⋃⋔⋈⋋⋚⋌⋃⋏⋃⋍⋎⋂⋋⋚⋌⋍⋏⋃⋍⋌⋏⋈⋏⋐⋏⋋⋚⋍⋂⋏⋈⋋⋚⋏⋎⋏⋏⋎⋋⋚⋏⋍⋌⋏⋏⋃⋎⋋⋎⋋⋐⋍⋚⋏⋌⋏⋐⋍⋍⋃⋋⋎⋂⋚⋐⋍⋎⋋⋏⋎⋏⋏⋍⋋⋚⋌⋍⋏⋎⋏⋏⋎⋋⋚⋏⋍⋏⋍⋎⋋⋚⋏⋃⋍⋃⋏⋏⋐⋏⋍⋍⋃⋏⋎⋋⋚⋏⋃⋎⋋⋏⋎⋏⋏⋍⋋⋚⋌⋍⋏⋎⋏⋏⋎⋋⋚⋏⋍⋏⋍⋎⋋⋚⋏⋃⋍⋃⋏⋏⋐⋏⋍⋍⋃⋏⋎⋋⋚⋏⋃⋎⋋⋏⋎⋏⋏⋍⋋⋚⋌⋍⋏⋎⋏⋏⋎⋋⋚⋏⋍⋏⋍⋎⋋⋚⋏⋃⋍⋃⋏⋏⋐⋏⋍⋍⋃⋏⋎⋋⋚⋏⋃⋎⋋⋏⋎⋏⋏⋍⋋⋚⋌⋍⋏⋎⋏⋏⋎⋋⋚⋏⋍⋏⋍⋎⋋⋚⋏⋃⋍⋃⋏⋏⋐⋏⋍⋍⋃⋏⋎⋋⋚⋏⋃⋎⋋⋏⋎⋏⋏⋍⋋⋚⋌⋍⋏⋎⋏⋏⋎⋋⋚⋏⋍⋏⋍⋎⋋⋚⋏⋃⋍⋃⋏⋏⋐⋏⋍⋍⋃⋏⋎⋋⋚⋏⋃⋎⋋⋏⋎⋏⋏⋍⋋⋚⋌⋍⋏⋎⋏⋏⋎⋋⋚⋏⋍⋏⋍⋎⋋⋚⋏⋃⋍⋃⋏⋏⋐⋏⋍⋍⋃⋏⋎⋋⋚⋏⋃⋎⋋⋏⋎⋏⋏⋍⋋⋚⋌⋍⋏⋎⋏⋏⋎⋋⋚⋏⋍⋏⋍⋎⋋⋚⋏⋃⋍⋃⋏⋏⋐⋏⋍⋍⋃⋏⋎⋋⋚⋏⋃⋎⋋⋏⋎⋏⋏⋍⋋⋚⋌⋍⋏⋎⋏⋏⋎⋋⋚⋏⋍⋏⋍⋎⋋⋚⋏⋃⋍⋃⋏⋏⋐⋏⋍⋍⋃⋏⋎⋋⋚⋏⋃
  10. <details>
  11. <summary>英文:</summary>
  12. 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

  1. 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

  1. 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

  1. 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

  1. </details>
  2. # 答案3
  3. **得分**: 1
  4. 好的,我明白你现在在问什么。在这里,我不确定为什么 `integrate` 返回它所返回的结果,但手动积分方法将计算这个积分:
  5. ```python
  6. In [3]: f.integrate((x, r, r+rho), manual=True)
  7. Out[3]:
  8. ____________________________________
  9. 2 ╱ 2 2 2
  10. π⋅ρ ρ⋅╲╱ - r + 2⋅r⋅(r + ρ) + ρ - (r + ρ)
  11. ──── + ─────────────────────────────────────────
  12. 4 2
  13. In [4]: f.integrate((x, r, r+rho), manual=True).simplify()
  14. Out[4]:
  15. 2
  16. π⋅ρ
  17. ────
  18. 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:

  1. In [3]: f.integrate((x, r, r+rho), manual=True)
  2. Out[3]:
  3. ____________________________________
  4. 2 2 2 2
  5. π⋅ρ ρ⋅╲╱ - r + 2r⋅(r + ρ) + ρ - (r + ρ)
  6. ──── + ─────────────────────────────────────────
  7. 4 2
  8. In [4]: f.integrate((x, r, r+rho), manual=True).simplify()
  9. Out[4]:
  10. 2
  11. π⋅ρ
  12. ────
  13. 4

That is using SymPy 1.11.1.

huangapple
  • 本文由 发表于 2023年3月4日 10:01:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75633210.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定