Revision history [back]

There a few unrelating problems here. One - assigning a function also reassigns the variables used to define it:

sage: x = 4
sage: x
4
sage: f(x) = x^2
sage: f
x |--> x^2
sage: x
x

This causes your x to dissapear. You just need to give r's variables a different name.

Second - y[j] cannot function as a symbolic expression - I believe unlike Mathematica sage does not implement "indexing" as a symbolic expression (see also here). So 'sum( y[j], j, 0, 1)' cannot work. The way to sum over a list y is simply sum(y), but this still won't solve your first problem. You can do something like

sage: r = lambda k:sum([var('x_%d'%(k+i)) for i in range(4)])
sage: r(6)
x_6 + x_7 + x_8 + x_9
sage: r(2)
x_2 + x_3 + x_4 + x_5

But r(k) will not work for k a variable.

There a few unrelating are two unrelated problems here. One - assigning a function also reassigns the variables used to define it:

sage: x = 4
sage: x
4
sage: f(x) = x^2
sage: f
x |--> x^2
sage: x
x

This causes your x to dissapear. You just need to give r's variables a different name.

Second - y[j] cannot function as a symbolic expression - I believe unlike Mathematica sage does not implement "indexing" as a symbolic expression (see also here). So 'sum( y[j], j, 0, 1)' cannot work. The way to sum over a list y is simply sum(y), but this still won't solve your first problem. You can do something like

sage: r = lambda k:sum([var('x_%d'%(k+i)) for i in range(4)])
sage: r(6)
x_6 + x_7 + x_8 + x_9
sage: r(2)
x_2 + x_3 + x_4 + x_5

But r(k) will not work for k a variable.

There are two unrelated problems here. One - assigning a function also reassigns the variables used to define it:

sage: x = 4
sage: x
4
sage: f(x) = x^2
sage: f
x |--> x^2
sage: x
x

This causes your x to dissapear. You just need to give r's variables a different name.

Second - y[j] cannot function as a symbolic expression - I believe unlike Mathematica sage does not implement "indexing" as a symbolic expression (see also here). So 'sum( y[j], j, 0, 1)' cannot work. The way to sum over a list y is simply sum(y), but this still won't solve your first problem. You can do something like

sage: r = lambda k:sum([var('x_%d'%(k+i)) k:sqrt(sum([var('x_%d'%(k+i)) for i in range(4)])
range(4)]))
sage: r(6)
x_6 r(2)
sqrt(x_2 + x_3 + x_4 + x_5)
sage: r(7)
sqrt(x_10 + x_7 + x_8 + x_9
sage: r(2)
x_2 + x_3 + x_4 + x_5
x_9)

But r(k) will not work for k a variable.