简体   繁体   中英

What is the fast way to do symbolic integration in sympy

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(eg, 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

The integration speed is a bug in SymPy. You can work around it by calling expand(expr) and integrating that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM