can't convert -oo to int

34 views
Skip to first unread message

Rainer Dorsch

unread,
Oct 12, 2019, 10:25:03 AM10/12/19
to sympy
Hi,

I have to admit I am new to sympy and deeply impressed what sympy can do.

When experimenting with integrating a Gaussian distribution, I got a backtrace. Just wondering if I did something wrong.

My experiment:

import sympy as sp
sp
.init_printing()

# Define Gaussian distribution
sigma
=sp.Symbol('sigma', positive=True)
mu
=sp.Symbol('mu',domain=sp.S.Reals)
x
=sp.Symbol('x')
f
=1/sp.sqrt(2*sp.pi*sigma**2)*sp.exp(-(x-mu)**2/(2*sigma**2))

# Integrate
g
=sp.integrate(f,x)
h
=g-g.subs(x,-sp.oo)
h

# Simplify full Intervall - 1 is expected
sp
.simplify(h.subs(x,sp.oo))

The output I got
 
rd@h370:~/tmp.nobackup$ python3 sympy-test.py
Traceback (most recent call last):
 
File "sympy-test.py", line 16, in <module>
    sp
.simplify(h.subs(x,sp.oo))
 
File "/usr/lib/python3/dist-packages/sympy/simplify/simplify.py", line 566, in simplify
    expr1
= shorter(_e, _mexpand(_e).cancel())  # issue 6829
 
File "/usr/lib/python3/dist-packages/sympy/core/expr.py", line 3245, in cancel
   
return cancel(self, *gens, **args)
 
File "/usr/lib/python3/dist-packages/sympy/polys/polytools.py", line 6583, in cancel
    f
= factor_terms(f, radical=True)
 
File "/usr/lib/python3/dist-packages/sympy/core/exprtools.py", line 1202, in factor_terms
   
return do(expr)
 
File "/usr/lib/python3/dist-packages/sympy/core/exprtools.py", line 1177, in do
    list_args
= [do(a) for a in Add.make_args(p)]
 
File "/usr/lib/python3/dist-packages/sympy/core/exprtools.py", line 1177, in <listcomp>
    list_args
= [do(a) for a in Add.make_args(p)]
 
File "/usr/lib/python3/dist-packages/sympy/core/exprtools.py", line 1198, in do
   
*[do(a) for a in p.args])
 
File "/usr/lib/python3/dist-packages/sympy/core/exprtools.py", line 1198, in <listcomp>
   
*[do(a) for a in p.args])
 
File "/usr/lib/python3/dist-packages/sympy/core/exprtools.py", line 1167, in do
    newargs
= tuple([do(i) for i in args])
 
File "/usr/lib/python3/dist-packages/sympy/core/exprtools.py", line 1167, in <listcomp>
    newargs
= tuple([do(i) for i in args])
 
File "/usr/lib/python3/dist-packages/sympy/core/exprtools.py", line 1199, in do
    rv
= _keep_coeff(cont, p, clear=clear, sign=sign)
 
File "/usr/lib/python3/dist-packages/sympy/core/mul.py", line 1793, in _keep_coeff
   
if r == int(r):
 
File "/usr/lib/python3/dist-packages/sympy/core/expr.py", line 229, in __int__
   
raise TypeError("can't convert %s to int" % r)
TypeError: can't convert -oo to int
rd@h370:~/tmp.nobackup$

sp.simplify(h.subs(x,sp.oo).subs(mu,0))

works well and returns 1 as expected.

Do I need to restrict mu further or did I hit a sympy issue? Or something completely different?

Any feedback is welcome.


Thanks
Rainer

Kalevi Suominen

unread,
Oct 12, 2019, 11:39:40 AM10/12/19
to sympy
Hi,

Limit values like -oo should not be directly substituted into complicated expressions. Instead of `g.subs(x, -oo)`, one should compute `limit(g, x, -oo)` (which is -1/2).

Kalevi Suominen

Rainer Dorsch

unread,
Oct 12, 2019, 1:23:49 PM10/12/19
to sympy
Thanks, Kalevi, the limit approach works well for the issue above.

But I find another interesting limit issue:
import sympy as sp
sp
.init_printing()


sigma
=sp.Symbol('sigma', positive=True)

mu
=sp.Symbol('mu',domain=sp.S.Reals)
x
=sp.Symbol('x')

C
=sp.Symbol('C')
f
=1/sp.sqrt(2*sp.pi*sigma**2)*sp.exp(-(x-mu)**2/(2*sigma**2))
g
=sp.integrate(f,x)+C

t
=sp.solve(g.subs(x,mu)-1/2,C)[0]

print("Without simplify everything looks ok:")
h
=g.subs(C,t)
print(h)
print(sp.limit(h,x,sp.oo))
print(sp.limit(h,x,-sp.oo))

print("Without simplify the result looks strange:")
h
=sp.simplify(g.subs(C,t))
print(h)
print(sp.limit(h,x,sp.oo))
print(sp.limit(h,x,-sp.oo))



The output I get is

rd@h370:~/Downloads$ python3 ~/Downloads/example_integrate_gauss.py
Without simplify everything looks ok:
erf
(sqrt(2)*(-mu + x)/(2*sigma))/2 + 0.5
1.00000000000000
0
Without simplify the result looks strange:
-erf(sqrt(2)*(mu - x)/(2*sigma))/2 + 0.5
0
0
rd@h370
:~/Downloads$

The simply output still looks ok for me, but the first limit output after applying simplify seems to be wrong (?)

Rainer
Reply all
Reply to author
Forward
0 new messages