Using constants wisely in SymPy
Asked Answered
W

2

7

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.

Whiggism answered 11/5, 2013 at 17:10 Comment(0)
L
6

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)

Lannielanning answered 12/5, 2013 at 19:45 Comment(3)
SymPy 0.7.3 is released now, so there's no need to use the development version for this.Lannielanning
Sorry for writing you here, but I have a problem with sympy in osx and I have ask a question here. Since you are user in that site as well, it would be great if you could kindly have a look at that. Thanks.Arlaarlan
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.Lannielanning
V
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.

Vonvona answered 11/5, 2013 at 18:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.