Reputation: 53
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
Upvotes: 5
Views: 1236
Reputation: 91640
The integration speed is a bug in SymPy. You can work around it by calling expand(expr)
and integrating that.
Upvotes: 4