Symbolic integration slow with SymPy

I am not an expert SymPy, but I have successfully used it in some of my lectures in recent years. However, sometimes this happens very slowly with symbolic integration. Here's an example that Mathematicacalculates almost instantly, while it SymPytakes a long time (more than half a minute) for it on my machine.

from sympy import *
x = symbols("x")

def coeff (f, k, var = x):
    return integrate(f(var) * cos(k * var), (var, -pi, pi)) / pi

f = lambda x: (11*sin(x) + 6*sin(2*x) + 2*sin(3*x))/10

[coeff(f, k) for k in range(0, 5)]

Am I doing something wrong or is this the expected behavior? Is there any trick to speed this up?

SymPyversion 1.0, Pythonis 3.5.1 64-bit (Anaconda) on Windows.

+4
source share
1 answer

, SymPy sin-cos (. [1], [2]) :

,

In [58]: fprod(f, 4)
Out[58]: (11*sin(x)/10 + 3*sin(2*x)/5 + sin(3*x)/5)*cos(4*x)

In [59]: FU['TR8'](fprod(f, 4))
Out[59]: -sin(x)/10 - 3*sin(2*x)/10 - 11*sin(3*x)/20 + 11*sin(5*x)/20 + 3*sin(6*x)/10 + sin(7*x)/10

.


:

import sympy as sym
x = sym.symbols("x")


def f(x):
    return (11*sym.sin(x) + 6*sym.sin(2*x) + 2*sym.sin(3*x))/10

def fprod(f, k, var=x):
    return f(var) * sym.cos(k * var)

FU = sym.FU
def coeff (f, k, var=x):
    return sym.integrate(FU['TR8'](fprod(f, k)), (var, -sym.pi, sym.pi)) / sym.pi

[coeff(f, k) for k in range(0, 5)]

FU['TR8']:

In [52]: %timeit [coeff(f, k) for k in range(0, 5)]
10 loops, best of 3: 78.8 ms per loop

( FU['TR8']):

In [54]: %timeit [coeff(f, k) for k in range(0, 5)]
1 loop, best of 3: 19.8 s per loop
+4

Source: https://habr.com/ru/post/1690511/


All Articles