Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the fast way to do symbolic integration in sympy

Tags:

python

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

like image 731
sunhex Avatar asked Nov 15 '16 14:11

sunhex


People also ask

Is SymPy fast?

SymPy (as of now) is purely Python-based and hence slow.

How do you integrate with SymPy?

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.

What is doit () in Python?

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.

How do you take the derivative of a SymPy?

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.


1 Answers

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

like image 178
asmeurer Avatar answered Oct 06 '22 15:10

asmeurer