Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using constants wisely in SymPy

I tried the following on SymPy Live

b,c,t = symbols('b c t')
g = 1/(1+exp(c*(b-t)))
integrate(g,t)

The result is Integral(1/(exp(c*(b - t)) + 1), t) which I understand as "could not handle this".

However, when I try

g = 1/(1+exp(0.1*(b-t)))
integrate(g,t)

I get:

1.0*t + 10.0*log(exp(-0.1*b) + exp(-0.1*t))

and I can easily replace the 0.1 and 10 by c and 1/c. What did I do wrong to make SymPy choke on c but handle 0.1?

Edited

I just noted that

g = 1/(1+exp(c*b-c*t)))

can be handled by integrate.

like image 622
Dieter Menne Avatar asked May 11 '13 17:05

Dieter Menne


2 Answers

The integration algorithm in SymPy 0.7.2 is a heuristic version of the Risch algorithm that very sensitive to the form of the input expression. In the next release of SymPy (or the git master if you want it now), work has begun on the full Risch algorithm, which does not have this issue.

In [3]: b,c,t = symbols('b c t')

In [4]: g = 1/(1+exp(c*(b-t)))

In [5]: integrate(g,t)
Out[5]:
       ⎛ c⋅(b - t)    ⎞
    log⎝ℯ          + 1⎠
t + ───────────────────
             c

In [9]: g = 1/(1+exp(c*b-c*t))

In [11]: integrate(g, t)
Out[11]:
       ⎛ b⋅c - c⋅t    ⎞
    log⎝ℯ          + 1⎠
t + ───────────────────
             c

You will still see this issue sometimes, because not all integrals are handled by the part of the Risch algorithm that has been implemented so far, so it falls back to the heuristic version.

(To be completely precise, there is another algorithm too, using Meijer G-functions, but does not work for this integrand. It too is somewhat heuristic, and so can depend on the form of the input)

like image 81
asmeurer Avatar answered Oct 16 '22 10:10

asmeurer


While I can not answer why 1/(1+exp(c*b-c*t))) works while 1/(1+exp(c*(b-t))) does not, if we take this for a given one can explain why c=<a_number> works.

There are numerous automatic simplifications that SymPy does. It simplifies 'float*sum' by expanding the sum, but it does not simplify 'symbol*sum'. You can check https://github.com/sympy/sympy/wiki/automatic-simplification for more information on autosimplification.

While this explains part of the problem, it does not explain why when considering only symbols one of the integral works while the other does not.

like image 22
Krastanov Avatar answered Oct 16 '22 09:10

Krastanov