Ask Your Question
0

summing over a list of variables?

asked 2011-07-13 12:26:39 +0100

StevenPollack gravatar image

updated 2011-07-13 12:48:01 +0100

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
1

answered 2011-07-13 13:41:46 +0100

parzan gravatar image

updated 2011-07-13 13:46:00 +0100

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: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

Comments

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 15:50:17 +0100 )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 18:46:59 +0100 )edit

thanks for the reply!

StevenPollack gravatar imageStevenPollack ( 2011-07-20 12:55:10 +0100 )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

Stats

Asked: 2011-07-13 12:26:39 +0100

Seen: 2,730 times

Last updated: Jul 13 '11