Loading [MathJax]/jax/output/HTML-CSS/jax.js

First time here? Check out the FAQ!

Ask Your Question
0

summing over a list of variables?

asked 13 years ago

StevenPollack gravatar image

updated 13 years ago

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:

rk(x1,x2,x3,x4)=(4i=k+1x2i)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?

Preview: (hide)

1 Answer

Sort by » oldest newest most voted
1

answered 13 years ago

parzan gravatar image

updated 13 years ago

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.

Preview: (hide)
link

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 ( 13 years ago )

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 ( 13 years ago )

thanks for the reply!

StevenPollack gravatar imageStevenPollack ( 13 years ago )

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: 13 years ago

Seen: 2,767 times

Last updated: Jul 13 '11