6

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.

Dieter Menne
  • 10,076
  • 44
  • 67

2 Answers2

5

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)

asmeurer
  • 86,894
  • 26
  • 169
  • 240
  • SymPy 0.7.3 is released now, so there's no need to use the development version for this. – asmeurer Aug 10 '13 at 01:18
  • Sorry for writing you here, but I have a problem with sympy in osx and I have ask a question [here](http://apple.stackexchange.com/questions/135522/iterm2-unicode-and-sympy). Since you are user in that site as well, it would be great if you could kindly have a look at that. Thanks. – Pouya Jun 19 '14 at 14:00
  • In the future, you can just tag questions with the sympy tag, and I will get a notification for it, as I am subscribed to that tag. – asmeurer Jun 19 '14 at 16:00
1

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.

Krastanov
  • 6,479
  • 3
  • 29
  • 42