I'm doing a project involving lots of symbolic integration.
The functions are something like the erlang probability distribution function.
Here is a simple example of the task.
https://s18.postimg.org/gd7t4bv95/gif_latex.gif
Here is the code for the task above:
import sympy as sym
t=sym.Symbol('t')
t1=sym.Symbol('t1')
t2=sym.Symbol('t2')
###integration for t2
expr=( 1-sym.exp(-(t-t2)) )*( 1-sym.exp(-(t-t2)) )*sym.exp(-t2)
expr=sym.integrate(expr,(t2,0,t))
###substitution and integration for t1
expr=expr.subs(t,t-t1) * (1-sym.exp(-(t-t1)))*sym.exp(-t1)
expr=sym.integrate(expr,(t1,0,t))
Here is a little complicated result:
https://s11.postimg.org/x9tw8kw8j/untitle.png
Thus, to implement on sympy, I use integrate() and subs() most of the time.
However, the speed is really slow. When I have 5 variables(e.g., from t_1 to t_5), I need to wait a little bit. But when I have 10 variables, I cannot finish the computation.
The code is a quite complicated, but I am sure that the bottleneck is the integration. After all, from the sample result, one can imagine how demanding the task will be.
I there any good way to boost the integration in sympy? Especially for functions like the exponential family
Thanks
SymPy (as of now) is purely Python-based and hence slow.
The SymPy package contains integrals module. It implements methods to calculate definite and indefinite integrals of expressions. The integrate() method is used to compute both definite and indefinite integrals. To compute an indefinite or primitive integral, just pass the variable after the expression.
doit allows you to easily define ad-hoc tasks, helping you to organize all your project related tasks in an unified easy-to-use & discoverable way.
To take derivatives, use the diff function. diff can take multiple derivatives at once. To take multiple derivatives, pass the variable as many times as you wish to differentiate, or pass a number after the variable.
The integration speed is a bug in SymPy. You can work around it by calling expand(expr)
and integrating that.
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