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.Tue, 17 Jan 2012 08:09:07 +0100Indexed family of functionshttps://ask.sagemath.org/question/8308/indexed-family-of-functions/I want to create a finite (but large-ish) sequence of symbolic functions f_0(x), f_1(x), etc. (up to, say, f_1000(x)). I can do this for any one function by typing something like:
f_4 = function('f_4', nargs=1)
Short of doing this 1000 times, how can I do this?
(For various reasons, I prefer having the subscript be part of the function's name rather than as a second variable.)Wed, 07 Sep 2011 23:10:58 +0200https://ask.sagemath.org/question/8308/indexed-family-of-functions/Answer by kcrisman for <p>I want to create a finite (but large-ish) sequence of symbolic functions f_0(x), f_1(x), etc. (up to, say, f_1000(x)). I can do this for any one function by typing something like:
f_4 = function('f_4', nargs=1)
Short of doing this 1000 times, how can I do this?
(For various reasons, I prefer having the subscript be part of the function's name rather than as a second variable.)</p>
https://ask.sagemath.org/question/8308/indexed-family-of-functions/?answer=12630#post-id-12630Usually my preferred way to do something like this is use a string formatting substitution. There may be an open ticket for making such things easier, but for now I'd do
sage: for k in range(1001):
function('f_%s'%k,nargs=1)
The percent signs are telling you to append a string, and that the string will the string from the int `k`, respectively.
I don't think you have to use the `f=function('f')` syntax in this case; someone correct me if that's not true.Thu, 08 Sep 2011 10:14:37 +0200https://ask.sagemath.org/question/8308/indexed-family-of-functions/?answer=12630#post-id-12630Comment by pang for <p>Usually my preferred way to do something like this is use a string formatting substitution. There may be an open ticket for making such things easier, but for now I'd do</p>
<pre><code>sage: for k in range(1001):
function('f_%s'%k,nargs=1)
</code></pre>
<p>The percent signs are telling you to append a string, and that the string will the string from the int <code>k</code>, respectively.</p>
<p>I don't think you have to use the <code>f=function('f')</code> syntax in this case; someone correct me if that's not true.</p>
https://ask.sagemath.org/question/8308/indexed-family-of-functions/?comment=20526#post-id-20526if you want to grab all the functions in a list, you can do my_list.append(function('f_%s'%k,nargs=1)) Tue, 17 Jan 2012 08:09:07 +0100https://ask.sagemath.org/question/8308/indexed-family-of-functions/?comment=20526#post-id-20526Comment by jdc for <p>Usually my preferred way to do something like this is use a string formatting substitution. There may be an open ticket for making such things easier, but for now I'd do</p>
<pre><code>sage: for k in range(1001):
function('f_%s'%k,nargs=1)
</code></pre>
<p>The percent signs are telling you to append a string, and that the string will the string from the int <code>k</code>, respectively.</p>
<p>I don't think you have to use the <code>f=function('f')</code> syntax in this case; someone correct me if that's not true.</p>
https://ask.sagemath.org/question/8308/indexed-family-of-functions/?comment=21273#post-id-21273Ah, that's helpful. I was doing such string tricks in other contexts, but I wasn't able to do that for the "y_0 =" part of "y_0 = function('y_0'...)". If that part is unnecessary, that bypasses the main obstacle.Thu, 08 Sep 2011 16:10:17 +0200https://ask.sagemath.org/question/8308/indexed-family-of-functions/?comment=21273#post-id-21273Answer by parzan for <p>I want to create a finite (but large-ish) sequence of symbolic functions f_0(x), f_1(x), etc. (up to, say, f_1000(x)). I can do this for any one function by typing something like:
f_4 = function('f_4', nargs=1)
Short of doing this 1000 times, how can I do this?
(For various reasons, I prefer having the subscript be part of the function's name rather than as a second variable.)</p>
https://ask.sagemath.org/question/8308/indexed-family-of-functions/?answer=12634#post-id-12634It's good to know the `exec` function - a powerful tool for "code generation":
sage: for k in range(1001):
....: exec("f_%d = lambda x:sin(%d*x)"%(k,k))
....:
sage: f_11(2)
sin(22)
I don't know what are the risks of using it (sometimes with great power comes great nagging about "bad programming habits", the most famous example is 'goto' commands).
"exec" also has a less powerful brother "eval", which does not help for your needs, but is also good to know (being less powerful he is probably less dangerous).Sat, 10 Sep 2011 10:51:09 +0200https://ask.sagemath.org/question/8308/indexed-family-of-functions/?answer=12634#post-id-12634Comment by parzan for <p>It's good to know the <code>exec</code> function - a powerful tool for "code generation":</p>
<pre><code>sage: for k in range(1001):
....: exec("f_%d = lambda x:sin(%d*x)"%(k,k))
....:
sage: f_11(2)
sin(22)
</code></pre>
<p>I don't know what are the risks of using it (sometimes with great power comes great nagging about "bad programming habits", the most famous example is 'goto' commands).</p>
<p>"exec" also has a less powerful brother "eval", which does not help for your needs, but is also good to know (being less powerful he is probably less dangerous).</p>
https://ask.sagemath.org/question/8308/indexed-family-of-functions/?comment=21239#post-id-21239Glad to help. I found a post about these matters: http://lucumr.pocoo.org/2011/2/1/exec-in-python/Mon, 19 Sep 2011 02:59:37 +0200https://ask.sagemath.org/question/8308/indexed-family-of-functions/?comment=21239#post-id-21239Comment by jdc for <p>It's good to know the <code>exec</code> function - a powerful tool for "code generation":</p>
<pre><code>sage: for k in range(1001):
....: exec("f_%d = lambda x:sin(%d*x)"%(k,k))
....:
sage: f_11(2)
sin(22)
</code></pre>
<p>I don't know what are the risks of using it (sometimes with great power comes great nagging about "bad programming habits", the most famous example is 'goto' commands).</p>
<p>"exec" also has a less powerful brother "eval", which does not help for your needs, but is also good to know (being less powerful he is probably less dangerous).</p>
https://ask.sagemath.org/question/8308/indexed-family-of-functions/?comment=21258#post-id-21258Ooo... that's good. Yes, I was trying to do with eval to no avail.Tue, 13 Sep 2011 12:56:33 +0200https://ask.sagemath.org/question/8308/indexed-family-of-functions/?comment=21258#post-id-21258