Ask Your Question

summing over a list of variables?

asked 2011-07-13 05:26:39 -0500

StevenPollack gravatar image

updated 2011-07-13 05:48:01 -0500

Hi all,

I'm new to SAGE (I'm trying to move over from Mathematica), and I was wondering if someone could help me define the following function:

$$ r_k(x_1, x_2, x_3, x_4) = \left( \sum_{i=k+1}^{4} x_i^2 \right)^{1/2} $$

I figured I could do something like:

var('k,j', domain=NN)
x = [var('x_{index}'.format(index=i)) for i in range(4)]  
r(k,x) = sqrt( sum( x[j]^2, j, k, 3 ) )

But this returns the error:

TypeError: unable to convert x (=j) to an integer

Worse yet, when I enter sage:x, it returns x, and forgets x was a list of variables. And to complicate things, when I try

y = [1,2,3]
sum( y[j], j, 0, 1)

I get the error

TypeError: unable to convert x (=j) to an integer

even after I do "clear_vars()" and try everything all over again.

Can anyone tell me what's going on?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2011-07-13 06:41:46 -0500

parzan gravatar image

updated 2011-07-13 06:46:00 -0500

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

sage: x = 4
sage: x
sage: f(x) = x^2
sage: f
x |--> x^2
sage: 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:sqrt(sum([var('x_%d'%(k+i)) for i in range(4)]))
sage: r(2)
sqrt(x_2 + x_3 + x_4 + x_5)
sage: r(7)
sqrt(x_10 + x_7 + x_8 + x_9)

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

edit flag offensive delete link more


While I'm a bit bummed that r(k) won't work in general. It's not a big deal -- r(k) is to be used inside of a product, anyway so it'll never stand unevaluated. If I define the following lambda function, r = lambda k: sqrt(sum([var('x_{index}'.format(index=i))^2 for i in [k..3]])), I get exactly what I want. It's just a little hacky. Is there no cleaner way to do this? P.S. will we ever get indexing implemented (like in mathematica)? It seems to be the smoothest way to make these kinds of function.

StevenPollack gravatar imageStevenPollack ( 2011-07-13 08:50:17 -0500 )edit

I think as of now mathematica (and maple and maxima) are ahead of sage in this aspect (at least according to the answers I recieved to the question linked above). Actually, since sage contains maxima you could use it (e.g., maxima('sqrt(sum(x[i]^2,i,k,k+3))') ), but you would not succeed in converting it to a sage object (by maxima('sqrt(sum(x[i]^2,i,k,k+3))').sage() ).

parzan gravatar imageparzan ( 2011-07-13 11:46:59 -0500 )edit

thanks for the reply!

StevenPollack gravatar imageStevenPollack ( 2011-07-20 05:55:10 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2011-07-13 05:26:39 -0500

Seen: 1,475 times

Last updated: Jul 13 '11