On Thu, Aug 30, 2012 at 8:02 PM, Saurabh Jha <[email protected]> wrote:
> Hi,
>
> In the course of implementing kauers algorithm I wanted to implement a
> function that can take as input a term like summation(f(x), (x, 1, k)) and
> outputs the f(x+1) that is the difference between (f(x), (x, 1, k + 1)) and
> (f(x), (x, 1, k)). Instead, it just returns "summation(f(x), (x, 1, k + 1) -
> summation(f(x), (x, 1, k)" I thought of just returning f(x + 1) directly but
> it will not work for nested sums. Is there any known way in sympy to deal
> with this problem? With this difficulty resolved, the implementation of the
> complete kauers algorithm will be done.
>
A __sub__ method needs to be defined for Sum. Something like this can
get you started:
```
def __sub__(self, other):
if not isinstance(other, Sum):
raise ValueError
if self.variables == other.variables and \
self.function == other.function and \
self.limits[0][1] == other.limits[0][1]:
x, _, s = self.limits[0]
_, _, o = other.limits[0]
dif = s - o
if dif.is_Number:
if dif is S.One:
return self.function.subs(x, s)
elif dif > 0:
return Sum(self.function, (x, o + 1, o + dif))
raise NotImplementedError
```
```
>>> from sympy import *
>>> var('x')
x
>>>
>>> f=Function('f')
>>> Sum(f(x),(x, 1, x + 1)) - Sum(f(x),(x, 1, x))
f(x + 1)
>>> Sum(f(x),(x, 1, x + 3)) - Sum(f(x),(x, 1, x))
Sum(f(x), (x, x + 1, x + 3))
>>> Sum(f(x),(x, 1, x + 3)) - Sum(f(x),(x, 1, x+1))
Sum(f(x), (x, x + 2, x + 3))
>>> Sum(f(x),(x, 1, x + 3)) - Sum(f(x),(x, 1, x+2))
f(x + 3)
>>>
```
Then you think about what __add__ might be...whether Number or only
Integer should be handled...what to do if the lower limits are not the
same or if diff is negative, etc...
/c
--
You received this message because you are subscribed to the Google Groups
"sympy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sympy?hl=en.