summation example in docs fails

4 views
Skip to first unread message

Neal Becker

unread,
Nov 13, 2009, 7:28:36 AM11/13/09
to sympy
example here:
http://docs.sympy.org/modules/mpmath/summation.html?highlight=sum

sum(1/factorial(n) for n in range(100))

SympifyError Traceback (most recent call last)

/home/nbecker/jupiter/<ipython console> in <module>()

/usr/lib/python2.6/site-packages/sympy-0.6.5-
py2.6.egg/sympy/concrete/summations.pyc in sum(*args, **kwargs)
159
160 def sum(*args, **kwargs):
--> 161 summation = Sum(*args, **kwargs)
162
163 if isinstance(summation, Sum):

/usr/lib/python2.6/site-packages/sympy-0.6.5-
py2.6.egg/sympy/concrete/summations.pyc in __new__(cls, f, *symbols,
**assumptions)
9
10 def __new__(cls, f, *symbols, **assumptions):
---> 11 f = sympify(f)
12
13 if f.is_Number:

/usr/lib/python2.6/site-packages/sympy-0.6.5-py2.6.egg/sympy/core/sympify.pyc
in sympify(a, locals, convert_xor)
120 a = a.replace('^','**')
121 import ast_parser
--> 122 return ast_parser.parse_expr(a, locals)
123 raise SympifyError("%r is NOT a valid SymPy expression" % a)
124

/usr/lib/python2.6/site-packages/sympy-0.6.5-
py2.6.egg/sympy/core/ast_parser.pyc in parse_expr(s, local_dict)
89 a = parse(s.strip(), mode="eval")
90 except SyntaxError:
---> 91 raise SympifyError("Cannot parse.")
92 a = Transform(local_dict, global_dict).visit(a)
93 e = compile(a, "<string>", "eval")

SympifyError: SympifyError: Cannot parse.

In [14]:

Aaron S. Meurer

unread,
Nov 13, 2009, 11:47:31 AM11/13/09
to sy...@googlegroups.com
That is using Python's sum function, not SymPy's. If you do (in python, not isympy):

>>> from sympy import factorial
>>> from sympy import Symbol
>>> n = Symbol('n', integer=True)
>>> sum(1/factorial(n) for n in range(100))
31710869445015912176908843526535027555643447320787267779096898248431156738548305814867560678144006224158425966541000436701189187481211772088720561290395499/11665776930493019085212404857033337561339496033047702683574120486902199999153739451117682997019564785781712240103402969781398151364608000000000000000000000

I think that example should be fixed at the very least, but aside from that, is there a reason that SymPy's sum cannot act like Python's sum() when it receives a list?

Aaron Meurer
Reply all
Reply to author
Forward
0 new messages