In sympy I have an integral which returns a Piecewise object, e.g.
In [2]: from sympy.abc import x,y,z
In [3]: test = exp(-x**2/z**2)
In [4]: itest = integrate(test,(x,0,oo))
In [5]: itest
Out[5]:
⎧ ___
⎪ ╲╱ π ⋅z │ ⎛ 1 ⎞│ π
⎪ ─────── for │periodic_argument⎜──────────────, ∞⎟│ ≤ ─
⎪ 2 │ ⎜ 2 ⎟│ 2
⎪ │ ⎝polar_lift (z) ⎠│
⎪
⎪∞
⎪⌠
⎨⎮ 2
⎪⎮ -x
⎪⎮ ───
⎪⎮ 2
⎪⎮ z
⎪⎮ ℯ dx otherwise
⎪⌡
⎪0
⎩
I would like to extract just the first branch of this piecewise equation, in other words, I would like to be able to do something like itest.parts(0)
to extract simply sqrt(pi)*z/2
. I can't seem to find any way to do this, but perhaps I am using the wrong search terms in the documentation. Any ideas?
Edit
Poking around a bit, I've managed to find that if I do itest.args[0][0]
I can extract this expression. This seems like a bit of a hack, however. Is there a better approach?
integrate(expression, limit) method, we can find the integration of mathematical expressions using limits in the form of variables by using sympy. integrate(expression, limit) method. Return : Return integration of mathematical expression.
With the help of sympy. log() function, we can simplify the principal branch of the natural logarithm. Logarithms are taken with the natural base, e. To get a logarithm of a different base b, use log(x, y), which is essentially short-hand for log(x) / log(y).
Symbol() function's argument is a string containing symbol which can be assigned to a variable. A symbol may be of more than one alphabets. SymPy also has a Symbols() function that can define multiple symbols at once. String contains names of variables separated by comma or space.
In general, using .args
is the correct way to access parts of an expression.
In this case, though, there is an option to integrate
that will let you ignore convergence conditions
In [39]: integrate(test, (x, 0, oo), conds='none')
Out[39]:
___
╲╱ π ⋅z
───────
2
Also, if you explicitly set the assumptions that you know on your variables, often the convergence conditions resolve themselves (it doesn't seem to happen in this case for any simple assumptions on z
, though). For example, if you knew that z
was real, use z = Symbol('z', real=True)
. Usually assuming that things are real, or even better positive, when you know it will help a lot in ensuring convergence.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With