ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 20 Jul 2011 05:55:10 -0500summing over a list of variables?http://ask.sagemath.org/question/8222/summing-over-a-list-of-variables/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?Wed, 13 Jul 2011 05:26:39 -0500http://ask.sagemath.org/question/8222/summing-over-a-list-of-variables/Answer by parzan for <p>Hi all, </p>
<p>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:</p>
<p>$$ r_k(x_1, x_2, x_3, x_4) = \left( \sum_{i=k+1}^{4} x_i^2 \right)^{1/2} $$</p>
<p>I figured I could do something like:</p>
<pre><code>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 ) )
</code></pre>
<p>But this returns the error:</p>
<blockquote>
<p>TypeError: unable to convert x (=j) to
an integer</p>
</blockquote>
<p>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</p>
<pre><code>y = [1,2,3]
sum( y[j], j, 0, 1)
</code></pre>
<p>I get the error </p>
<blockquote>
<p>TypeError: unable to convert x (=j) to an integer</p>
</blockquote>
<p>even after I do "clear_vars()" and try everything all over again.</p>
<p>Can anyone tell me what's going on?</p>
http://ask.sagemath.org/question/8222/summing-over-a-list-of-variables/?answer=12515#post-id-12515There 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](http://ask.sagemath.org/question/611/implicitly-defining-a-sequence-of-variables)).
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.
Wed, 13 Jul 2011 06:41:46 -0500http://ask.sagemath.org/question/8222/summing-over-a-list-of-variables/?answer=12515#post-id-12515Comment by StevenPollack for <p>There are two unrelated problems here.
One - assigning a function also reassigns the variables used to define it:</p>
<pre><code>sage: x = 4
sage: x
4
sage: f(x) = x^2
sage: f
x |--> x^2
sage: x
x
</code></pre>
<p>This causes your x to dissapear. You just need to give r's variables a different name.</p>
<p>Second - y[j] cannot function as a symbolic expression - I believe unlike Mathematica sage does not implement "indexing" as a symbolic expression (see also <a href="http://ask.sagemath.org/question/611/implicitly-defining-a-sequence-of-variables">here</a>).
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</p>
<pre><code>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)
</code></pre>
<p>But r(k) will not work for k a variable.</p>
http://ask.sagemath.org/question/8222/summing-over-a-list-of-variables/?comment=21479#post-id-21479While 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.Wed, 13 Jul 2011 08:50:17 -0500http://ask.sagemath.org/question/8222/summing-over-a-list-of-variables/?comment=21479#post-id-21479Comment by parzan for <p>There are two unrelated problems here.
One - assigning a function also reassigns the variables used to define it:</p>
<pre><code>sage: x = 4
sage: x
4
sage: f(x) = x^2
sage: f
x |--> x^2
sage: x
x
</code></pre>
<p>This causes your x to dissapear. You just need to give r's variables a different name.</p>
<p>Second - y[j] cannot function as a symbolic expression - I believe unlike Mathematica sage does not implement "indexing" as a symbolic expression (see also <a href="http://ask.sagemath.org/question/611/implicitly-defining-a-sequence-of-variables">here</a>).
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</p>
<pre><code>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)
</code></pre>
<p>But r(k) will not work for k a variable.</p>
http://ask.sagemath.org/question/8222/summing-over-a-list-of-variables/?comment=21477#post-id-21477I 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() ).Wed, 13 Jul 2011 11:46:59 -0500http://ask.sagemath.org/question/8222/summing-over-a-list-of-variables/?comment=21477#post-id-21477Comment by StevenPollack for <p>There are two unrelated problems here.
One - assigning a function also reassigns the variables used to define it:</p>
<pre><code>sage: x = 4
sage: x
4
sage: f(x) = x^2
sage: f
x |--> x^2
sage: x
x
</code></pre>
<p>This causes your x to dissapear. You just need to give r's variables a different name.</p>
<p>Second - y[j] cannot function as a symbolic expression - I believe unlike Mathematica sage does not implement "indexing" as a symbolic expression (see also <a href="http://ask.sagemath.org/question/611/implicitly-defining-a-sequence-of-variables">here</a>).
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</p>
<pre><code>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)
</code></pre>
<p>But r(k) will not work for k a variable.</p>
http://ask.sagemath.org/question/8222/summing-over-a-list-of-variables/?comment=21455#post-id-21455thanks for the reply!Wed, 20 Jul 2011 05:55:10 -0500http://ask.sagemath.org/question/8222/summing-over-a-list-of-variables/?comment=21455#post-id-21455