Reputation: 21
I want to evaluate an integral of the form:
using python sympy
, however, I am not able to do so.
This is what I have implemented:
import sympy as sym
r,a,b = sym.symbols('r,a,b')
I = r/sym.sqrt((a-r)*(r-b))
I_1 = sym.integrate (I,r)
print (I_1)
The result that I am getting is: Integral(r/sqrt(-(-a + r)*(-b + r)), r)
Thus, it is not evaluating the integral, rather just simplifying the equation.
Can anyone help me with this?
Upvotes: 2
Views: 1039
Reputation: 34
To simplifying integral you can use:
sympy.pprint(sympy.Integral(I, x))
To solve integral you can use:
sympy.pprint(integrate(I, x))
Upvotes: 0
Reputation: 14530
It is possible to compute an antiderivative with a substitution:
In [1]: import sympy as sym
...: r,a,b = sym.symbols('r,a,b')
...: I = r/sym.sqrt((a-r)*(r-b))
...: I_1 = sym.integrate (I,r)
...: print (I_1)
Integral(r/sqrt(-(-a + r)*(-b + r)), r)
In [2]: t = symbols('t')
In [3]: tsubs = r - (a+b)/2
In [4]: Integral(I, r)
Out[4]:
⌠
⎮ r
⎮ ──────────────────── dr
⎮ __________________
⎮ ╲╱ (a - r)⋅(-b + r)
⌡
In [5]: Integral(I, r).transform(tsubs, t)
Out[5]:
⌠
⎮ a b
⎮ ─ + ─ + t
⎮ 2 2
⎮ ───────────────────────────── dt
⎮ _________________________
⎮ ╱ ⎛a b ⎞ ⎛a b ⎞
⎮ ╱ ⎜─ - ─ - t⎟⋅⎜─ - ─ + t⎟
⎮ ╲╱ ⎝2 2 ⎠ ⎝2 2 ⎠
⌡
In [6]: Integral(I, r).transform(tsubs, t).doit()
Out[6]:
⎛⎧ ⎛ _________________⎞ ⎞ ⎛⎧ ⎛ _________________⎞ ⎞
⎜⎪ ⎜ ╱ 1 ⎟ 2 2 ⎟ ⎜⎪ ⎜ ╱ 1 ⎟ 2 2 ⎟
a⋅⎜⎨asin⎜2⋅t⋅ ╱ ─────────────── ⎟ for a - 2⋅a⋅b + b > 0⎟ b⋅⎜⎨asin⎜2⋅t⋅ ╱ ─────────────── ⎟ for a - 2⋅a⋅b + b > 0⎟ ________________________
⎜⎪ ⎜ ╱ 2 2 ⎟ ⎟ ⎜⎪ ⎜ ╱ 2 2 ⎟ ⎟ ╱ 2 2 2
⎝⎩ ⎝ ╲╱ a - 2⋅a⋅b + b ⎠ ⎠ ⎝⎩ ⎝ ╲╱ a - 2⋅a⋅b + b ⎠ ⎠ ╲╱ a - 2⋅a⋅b + b - 4⋅t
────────────────────────────────────────────────────────────── + ────────────────────────────────────────────────────────────── - ───────────────────────────
2 2 2
In [7]: Integral(I, r).transform(tsubs, t).doit().subs(t, tsubs)
Out[7]:
⎛⎧ ⎛ _________________⎞ ⎞ ⎛⎧ ⎛ _________________⎞ ⎞ ____________________________________
⎜⎪ ⎜ ⎛ a b ⎞ ╱ 1 ⎟ 2 2 ⎟ ⎜⎪ ⎜ ⎛ a b ⎞ ╱ 1 ⎟ 2 2 ⎟ ╱ 2
a⋅⎜⎨asin⎜2⋅⎜- ─ - ─ + r⎟⋅ ╱ ─────────────── ⎟ for a - 2⋅a⋅b + b > 0⎟ b⋅⎜⎨asin⎜2⋅⎜- ─ - ─ + r⎟⋅ ╱ ─────────────── ⎟ for a - 2⋅a⋅b + b > 0⎟ ╱ 2 2 ⎛ a b ⎞
⎜⎪ ⎜ ⎝ 2 2 ⎠ ╱ 2 2 ⎟ ⎟ ⎜⎪ ⎜ ⎝ 2 2 ⎠ ╱ 2 2 ⎟ ⎟ ╱ a - 2⋅a⋅b + b - 4⋅⎜- ─ - ─ + r⎟
⎝⎩ ⎝ ╲╱ a - 2⋅a⋅b + b ⎠ ⎠ ⎝⎩ ⎝ ╲╱ a - 2⋅a⋅b + b ⎠ ⎠ ╲╱ ⎝ 2 2 ⎠
────────────────────────────────────────────────────────────────────────── + ────────────────────────────────────────────────────────────────────────── - ─────────────────────────────────────────
2 2 2
In [8]: factor(piecewise_fold(_), deep=True)
Out[8]:
⎧ ⎛ _________________⎞ ⎛ _________________⎞
⎪ ⎜ ╱ 1 ⎟ ⎜ ╱ 1 ⎟
⎪a⋅asin⎜(-a - b + 2⋅r)⋅ ╱ ─────────────── ⎟ b⋅asin⎜(-a - b + 2⋅r)⋅ ╱ ─────────────── ⎟
⎨ ⎜ ╱ 2 2 ⎟ ⎜ ╱ 2 2 ⎟
⎪ ⎝ ╲╱ a - 2⋅a⋅b + b ⎠ ⎝ ╲╱ a - 2⋅a⋅b + b ⎠ ____________________ 2
⎪───────────────────────────────────────────── + ───────────────────────────────────────────── - ╲╱ -(-a + r)⋅(-b + r) for (a - b) > 0
⎩ 2 2
The result comes out with a condition that is basically equivalent to a != b
(if a and b are assumed real). You can use .args[0][0]
to extract just the expression part.
Upvotes: 3
Reputation: 4506
sympy
cannot compute this integral.
You're looking at a manually created indefinite integral of:
which is now complex.
Or a maxima created integal of:
Either way, all algorithms and options have been exhausted, and it failed on every try. Some integrals are just too difficult for sympy
to solve.
Upvotes: 2