Updates:
        Labels: NeedsReview

Comment #1 on issue 1499 by asmeurer: Sum function seems to fail
http://code.google.com/p/sympy/issues/detail?id=1499

The reason that this is happening is that the limits of Sum are not being  
substituted with subs.

Putting them in directly works as expected:
>>> m = Symbol('m')
>>> I = Symbol('I')
>>> s = Sum(m*exp(m), (m, -2, 2))
>>> s
Sum(m*exp(m), (m, -2, 2))
>>> s.evalf() # I believe this is the right result
16.8578440186757
>>> s = Sum(m*exp(m), (m, -2, 2)).doit()
>>> print s # This looks right too
E - exp(-1) - 2*exp(-2) + 2*exp(2)

You can see that this is the case by looking at the source for  
Sum._eval_subs():
from line 154 in sympy/concrete/summations.py:
     def _eval_subs(self, old, new):
         return Sum(self.args[0].subs(old, new), *self.args[1])

As you can see, self.args[1], which is the limits, is not substituted with  
subs here.

I have attached a patch which fixes and adds a test.
Here is what your test case goes with my patch.  I assume that this is the  
behavior you were wanting:
>>> from sympy import *
>>> m = Symbol('m')
>>> I = Symbol('I')
>>> s = Sum(exp(m), (m, -I, I))
>>> # m should be -2 .. 2
... s = s.subs(I,2).doit()
>>> print(s)
1 + E + exp(-1) + exp(-2) + exp(2)
>>> # prints: (exp(-I) - exp(1 + I))/(1 - E)
... s.subs(I,2).evalf()
11.6105526517978
>>> # 11.61... the correct result
... # now just slightly different:
... s = Sum(m*exp(m), (m, -I, I))
>>> # m should be -2 .. 2
... s = s.subs(I,2).doit()
>>> print(s)
E - exp(-1) - 2*exp(-2) + 2*exp(2)
>>> # Sum(m*exp(m), (m, -I, I)), so it did not expand
... # infinite loop here:
... s.subs(I, 2).evalf()
16.8578440186757

My patch doesn't substitute the summation variable, because you could get
>>> Sum(m*exp(m), (m, -2, 2)).subs(m,2) # Try substituting for a number
<Traceback>
ValueError: Invalid summation variable or limits
>>> Sum(m*exp(m), (m, -2 2)).subs(x*y, 2) # Try substituting for a Mul
<Traceback>
ValueError: Invalid summation variable or limits
...
and so on.

That would only happen if you tried to substitute the summation variable  
for anything that isn't a Symbol.  What do you
think?  Should subs substitute the summation variable too?  It could be  
useful if you want to change the name of the
summation variable in a Sum.

Attachments:
        0001-Made-Sum._eval_subs-substitute-the-limits-of-a-sum.patch  1.9 KB

--
You received this message because you are listed in the owner
or CC fields of this issue, or because you starred this issue.
You may adjust your issue notification preferences at:
http://code.google.com/hosting/settings

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sympy-issues" group.
To post to this group, send email to sympy-issues@googlegroups.com
To unsubscribe from this group, send email to 
sympy-issues+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sympy-issues?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to