ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 21 Dec 2011 09:13:38 +0100Defining a function of the form $f(x_1,x_2,...,x_n)=\sum_{i=0}^n x_i$https://ask.sagemath.org/question/8581/defining-a-function-of-the-form-fx_1x_2x_nsum_i0n-x_i/How can I define a function of the form
$$f(x_1,x_2,...,x_n)=\sum_{i=0}^n x_i$$
I tried the following
`sage: n = 2`
`sage: x = [ var('x_%d' % i) for i in range(n) ]`
`sage: f(x)=sum(x[i],i,0,n)`
The last line gives me the error
"TypeError: 'sage.symbolic.expression.Expression' object does not support indexing".Tue, 20 Dec 2011 09:52:14 +0100https://ask.sagemath.org/question/8581/defining-a-function-of-the-form-fx_1x_2x_nsum_i0n-x_i/Answer by Zatrapadoo for <p>How can I define a function of the form
$$f(x_1,x_2,...,x_n)=\sum_{i=0}^n x_i$$
I tried the following <br/>
<code>sage: n = 2</code> <br/>
<code>sage: x = [ var('x_%d' % i) for i in range(n) ]</code> <br/>
<code>sage: f(x)=sum(x[i],i,0,n)</code> <br/>
The last line gives me the error <br/>
"TypeError: 'sage.symbolic.expression.Expression' object does not support indexing".</p>
https://ask.sagemath.org/question/8581/defining-a-function-of-the-form-fx_1x_2x_nsum_i0n-x_i/?answer=13023#post-id-13023What about this? Assume $x$ is a list. Then:
def f(x):
return sum(x)
(I just think it's easier to remember than the answer by DSM.)Tue, 20 Dec 2011 12:41:12 +0100https://ask.sagemath.org/question/8581/defining-a-function-of-the-form-fx_1x_2x_nsum_i0n-x_i/?answer=13023#post-id-13023Comment by DSM for <p>What about this? Assume $x$ is a list. Then:</p>
<pre><code>def f(x):
return sum(x)
</code></pre>
<p>(I just think it's easier to remember than the answer by DSM.)</p>
https://ask.sagemath.org/question/8581/defining-a-function-of-the-form-fx_1x_2x_nsum_i0n-x_i/?comment=20672#post-id-20672Python functions and Sage functions are slightly different -- there are cases where you need to use the latter, and the calling syntax differs -- but I agree there are many times when this would work okay.Wed, 21 Dec 2011 09:13:38 +0100https://ask.sagemath.org/question/8581/defining-a-function-of-the-form-fx_1x_2x_nsum_i0n-x_i/?comment=20672#post-id-20672Answer by DSM for <p>How can I define a function of the form
$$f(x_1,x_2,...,x_n)=\sum_{i=0}^n x_i$$
I tried the following <br/>
<code>sage: n = 2</code> <br/>
<code>sage: x = [ var('x_%d' % i) for i in range(n) ]</code> <br/>
<code>sage: f(x)=sum(x[i],i,0,n)</code> <br/>
The last line gives me the error <br/>
"TypeError: 'sage.symbolic.expression.Expression' object does not support indexing".</p>
https://ask.sagemath.org/question/8581/defining-a-function-of-the-form-fx_1x_2x_nsum_i0n-x_i/?answer=13014#post-id-13014You're getting the TypeError because you're double-assigning x: in your second line, you make it a list of variables, but in the third you redefine it as a symbolic variable. The f(x)=sum(x[i],i,0,n) line is translated as
sage: preparse("f(x) = sum(x[i], i, 0, n")
'__tmp__=var("x"); f = symbolic_expression(sum(x[i], i, Integer(0), n).function(x)'
Unfortunately we can't simply rename x to xx: the "f(x,y) = x+y" syntax requires explicit specification of the variables, and you can't pass it a list. So for a fixed n (which is the only case I really know how to handle offhand) I would probably do this instead:
sage: n = 2
sage: xx = [var('x_%d' % i) for i in range(n)]
sage:
sage: f = sum(xx).function(*xx)
sage: f
(x_0, x_1) |--> x_0 + x_1
[Here I took advantage of the * operator in `*xx`, which turns f(*[a,b,c]) into f(a,b,c).]
Oh, I've just noticed that I used [0..n-1] as the indices and I think you're using [0..n], but that's easy to change.Tue, 20 Dec 2011 10:12:40 +0100https://ask.sagemath.org/question/8581/defining-a-function-of-the-form-fx_1x_2x_nsum_i0n-x_i/?answer=13014#post-id-13014Comment by Nicolas Essis-Breton for <p>You're getting the TypeError because you're double-assigning x: in your second line, you make it a list of variables, but in the third you redefine it as a symbolic variable. The f(x)=sum(x[i],i,0,n) line is translated as</p>
<pre><code>sage: preparse("f(x) = sum(x[i], i, 0, n")
'__tmp__=var("x"); f = symbolic_expression(sum(x[i], i, Integer(0), n).function(x)'
</code></pre>
<p>Unfortunately we can't simply rename x to xx: the "f(x,y) = x+y" syntax requires explicit specification of the variables, and you can't pass it a list. So for a fixed n (which is the only case I really know how to handle offhand) I would probably do this instead:</p>
<pre><code>sage: n = 2
sage: xx = [var('x_%d' % i) for i in range(n)]
sage:
sage: f = sum(xx).function(*xx)
sage: f
(x_0, x_1) |--> x_0 + x_1
</code></pre>
<p>[Here I took advantage of the * operator in <code>*xx</code>, which turns f(*[a,b,c]) into f(a,b,c).]</p>
<p>Oh, I've just noticed that I used [0..n-1] as the indices and I think you're using [0..n], but that's easy to change.</p>
https://ask.sagemath.org/question/8581/defining-a-function-of-the-form-fx_1x_2x_nsum_i0n-x_i/?comment=20679#post-id-20679@DSM Thanks DSM, your answer is enlighteningTue, 20 Dec 2011 10:37:42 +0100https://ask.sagemath.org/question/8581/defining-a-function-of-the-form-fx_1x_2x_nsum_i0n-x_i/?comment=20679#post-id-20679