some inconsistencies with summation

17 views
Skip to first unread message

Saurabh Jha

unread,
Aug 30, 2012, 10:17:04 AM8/30/12
to sy...@googlegroups.com
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.

Sincerely,
- Saurabh Jha

Chris Smith

unread,
Aug 30, 2012, 10:51:13 AM8/30/12
to sy...@googlegroups.com
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

Aaron Meurer

unread,
Aug 30, 2012, 12:43:57 PM8/30/12
to sy...@googlegroups.com
The question is, do we want Sum to automatically combine like that?
If not, then I would add it as a different method than __sub__.

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

Matthew Rocklin

unread,
Aug 30, 2012, 1:26:54 PM8/30/12
to sy...@googlegroups.com
Doesn't this call for some sort of Sum_simplify? 

I don't like the idea of overriding __sub__. I do like the idea of looking at an expression tree and seeing if it can be simplified. Is there a mechanism in SymPy to write down these simplifications and have them applied when you call simplify? I'm not very familiar with how simplify is organized. 

Chris Smith

unread,
Aug 30, 2012, 2:37:08 PM8/30/12
to sy...@googlegroups.com
On Thu, Aug 30, 2012 at 10:28 PM, Aaron Meurer <asme...@gmail.com> wrote:
> The question is, do we want Sum to automatically combine like that?
> If not, then I would add it as a different method than __sub__.
>

`_eval_expand_basic`, perhaps?

Aaron Meurer

unread,
Aug 30, 2012, 2:56:19 PM8/30/12
to sy...@googlegroups.com
This doesn't really make sense as expansion, though.

This is closely related to
http://code.google.com/p/sympy/issues/detail?id=2297.

Aaron Meurer

Chris Smith

unread,
Aug 30, 2012, 2:59:13 PM8/30/12
to sy...@googlegroups.com
On Fri, Aug 31, 2012 at 12:41 AM, Aaron Meurer <asme...@gmail.com> wrote:
> This doesn't really make sense as expansion, though.
>
> This is closely related to
> http://code.google.com/p/sympy/issues/detail?id=2297.
>

If you expanded the sums you would obtain the single `f(x+1)`-like
term. It's just a thought and `_basic` is suppose to be used for
expansions that don't fit other categories...it's hardly used (if at
all), however.

Aaron Meurer

unread,
Sep 1, 2012, 12:51:23 PM9/1/12
to sy...@googlegroups.com
This also doesn't work well with expand() because the method would
have to be on Add to work. I think the best solution would be to
write a function that takes an expression with summations and tries to
combine them.

Aaron Meurer

Saurabh Jha

unread,
Sep 1, 2012, 3:30:39 PM9/1/12
to sympy
I am trying to make some way through it. I am trying to implement a
function that takes as input summation(f(x), (x, 1, k)) or something
like that and returns f(x+1), because that's the output needed. This
function will then replace finite_diff of sympy/sympy/series/kauers.py

Saurabh Jha

On Sep 1, 9:51 pm, Aaron Meurer <asmeu...@gmail.com> wrote:
> This also doesn't work well with expand() because the method would
> have to be on Add to work.  I think the best solution would be to
> write a function that takes an expression with summations and tries to
> combine them.
>
> Aaron Meurer
>
>
>
>
>
>
>
> On Thu, Aug 30, 2012 at 12:59 PM, Chris Smith <smi...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages