Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Symbolic accumulation

With Maple I can write

seq(add(a[i], i=0..n), n=0..3);

and get

a[0], a[0]+a[1], a[0]+a[1]+a[2], a[0]+a[1]+a[2]+a[3]  (*)

If I write in Sage

for n in range(4):
    add(a[i] for i in range(n))

I get the NameError: global name 'a' is not defined. If I first introduce the variable by a = var('a') then I get the TypeError: 'sage.symbolic.expression.Expression' object does not support indexing.

How can I get the line (*) with Sage?

click to hide/show revision 2
Compared with function declaration.

Symbolic accumulation

With Maple I can write

seq(add(a[i], i=0..n), n=0..3);

and get

a[0], a[0]+a[1], a[0]+a[1]+a[2], a[0]+a[1]+a[2]+a[3]  (*)

If I write in Sage

for n in range(4):
    add(a[i] for i in range(n))

I get the NameError: global name 'a' is not defined. If I first introduce the variable by a = var('a') then I get the TypeError: 'sage.symbolic.expression.Expression' object does not support indexing.

How can I get the line (*) with Sage?

Edit: This works:

f=function('f')
[add(f(i) for i in (0..n)) for n in (0..3)]

gives

[f(0), f(1) + f(0), f(2) + f(1) + f(0), f(3) + f(2) + f(1) + f(0)]

So what I want is an equivalent for lists.

a=list('a')
[add(a[i] for i in (0..n)) for n in (0..3)]