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.Thu, 07 Jun 2012 15:28:44 +0200creating a group of similar functions with similar nameshttps://ask.sagemath.org/question/9027/creating-a-group-of-similar-functions-with-similar-names/ namelist = ['f'+str(i) for i in range(10)]; namelist
for i in range(len(namelist)):
namelist[i] = lambda x: 1/x^i
1 - create a list of function names (f0, f1, f2, ...)
2 - for each item in namelist
3 - take that that item and make it a function that raises *x* to a negative power equal to the position of that item (f0(x) = 1/x^0, f1(x) = 1/x^1, ...)
Is this possible?
EDIT:
Made some progress by changing the type of namelist[i] to Expression by var(i)...Sat, 02 Jun 2012 15:26:12 +0200https://ask.sagemath.org/question/9027/creating-a-group-of-similar-functions-with-similar-names/Answer by niles for <pre><code>namelist = ['f'+str(i) for i in range(10)]; namelist
for i in range(len(namelist)):
namelist[i] = lambda x: 1/x^i
</code></pre>
<p>1 - create a list of function names (f0, f1, f2, ...)</p>
<p>2 - for each item in namelist</p>
<p>3 - take that that item and make it a function that raises <em>x</em> to a negative power equal to the position of that item (f0(x) = 1/x^0, f1(x) = 1/x^1, ...)</p>
<p>Is this possible?</p>
<p>EDIT:</p>
<p>Made some progress by changing the type of namelist[i] to Expression by var(i)...</p>
https://ask.sagemath.org/question/9027/creating-a-group-of-similar-functions-with-similar-names/?answer=13647#post-id-13647Do you really not want to just have a function of two variables, such as the following?
sage: def f(i,x):
....: return x^(-i)
sage: f(2,2)
1/4
sage: f(3,2)
1/8
I think there is some problem with making a list of lambda functions, but you could use the idea of a "factory" -- a function that returns another function:
sage: def named_function_factory(i):
....: def f(x):
....: return x^(-i)
....: return f
....:
sage: functionlist = [named_function_factory(i) for i in range(10)]
sage: functionlist[2](2)
1/4
sage: functionlist[3](2)
1/8
But note that this is really not so different from just using a function of two variables, `i` and `x`, so maybe I still haven't answered your question. But I also don't see an appreciable difference between typing "`functionnamei(x)`" and "`functionname[i](x`)" or "`functionname(i,x)`" for that matter. These are of course quite different in terms of their data types, but is that so significant here?
Sat, 02 Jun 2012 17:39:26 +0200https://ask.sagemath.org/question/9027/creating-a-group-of-similar-functions-with-similar-names/?answer=13647#post-id-13647Comment by daniel.e2718 for <p>Do you really not want to just have a function of two variables, such as the following?</p>
<pre><code>sage: def f(i,x):
....: return x^(-i)
sage: f(2,2)
1/4
sage: f(3,2)
1/8
</code></pre>
<p>I think there is some problem with making a list of lambda functions, but you could use the idea of a "factory" -- a function that returns another function:</p>
<pre><code>sage: def named_function_factory(i):
....: def f(x):
....: return x^(-i)
....: return f
....:
sage: functionlist = [named_function_factory(i) for i in range(10)]
sage: functionlist[2](2)
1/4
sage: functionlist[3](2)
1/8
</code></pre>
<p>But note that this is really not so different from just using a function of two variables, <code>i</code> and <code>x</code>, so maybe I still haven't answered your question. But I also don't see an appreciable difference between typing "<code>functionnamei(x)</code>" and "<code>functionname[i](x</code>)" or "<code>functionname(i,x)</code>" for that matter. These are of course quite different in terms of their data types, but is that so significant here?</p>
https://ask.sagemath.org/question/9027/creating-a-group-of-similar-functions-with-similar-names/?comment=19700#post-id-19700I see what you mean regarding "functionnamei(x)", "functionname[i](x)", about "functionname(i,x)."
I think I ask questions a bit prematurely... haha. Thanks anyway!Sat, 02 Jun 2012 20:37:17 +0200https://ask.sagemath.org/question/9027/creating-a-group-of-similar-functions-with-similar-names/?comment=19700#post-id-19700Answer by DSM for <pre><code>namelist = ['f'+str(i) for i in range(10)]; namelist
for i in range(len(namelist)):
namelist[i] = lambda x: 1/x^i
</code></pre>
<p>1 - create a list of function names (f0, f1, f2, ...)</p>
<p>2 - for each item in namelist</p>
<p>3 - take that that item and make it a function that raises <em>x</em> to a negative power equal to the position of that item (f0(x) = 1/x^0, f1(x) = 1/x^1, ...)</p>
<p>Is this possible?</p>
<p>EDIT:</p>
<p>Made some progress by changing the type of namelist[i] to Expression by var(i)...</p>
https://ask.sagemath.org/question/9027/creating-a-group-of-similar-functions-with-similar-names/?answer=13648#post-id-13648You're hitting a lot of subtle bits about the way Sage's type system works. There are at least three types of function-like things:
Sage Expressions -- not functions, but you can substitute into them, so it's not entirely unlike one -- which live in the Symbolic Ring:
sage: x = var("x")
sage: f = 1/x^3
sage: f
x^(-3)
sage: parent(f)
Symbolic Ring
sage: f.subs(x=3)
1/27
sage: f(x=3)
1/27
Sage functions:
sage: g(x) = 1/x^3
sage: parent(g)
Callable function ring with arguments (x,)
sage: g(3)
1/27
and Python functions (including lambdas):
sage: def h(x): return 1/x^3
....:
sage: parent(h)
<type 'function'>
sage: h(3)
1/27
Where possible, it's usually desirable to stick with the Sage objects because they have lots of useful methods inside them whereas the Python functions don't.
You seem to be trying to make a number of objects which have *names* like 'f0', 'f1', 'f2', etc, and put them into the namespace. You *could* do that, but it's generally a bad idea, so I won't explain how. The usual rule is "Don't put your data in variable names!"
It's easy to make a list of Expressions or Sage functions instead:
sage: fs = [1/x^i for i in range(10)]
sage: fs
[1, 1/x, x^(-2), x^(-3), x^(-4), x^(-5), x^(-6), x^(-7), x^(-8), x^(-9)]
sage: fs[3]
x^(-3)
sage: fs[3](3)
1/27
sage: gs = [(1/x^i).function(x) for i in range(10)]
sage: gs
[x |--> 1, x |--> 1/x, x |--> x^(-2), x |--> x^(-3), x |--> x^(-4), x |--> x^(-5), x |--> x^(-6), x |--> x^(-7), x |--> x^(-8), x |--> x^(-9)]
sage: gs[3](x=3)
1/27
Note that I've used the `.function` syntax to inline what "g(x)=1/x^3" does.
Sat, 02 Jun 2012 17:48:23 +0200https://ask.sagemath.org/question/9027/creating-a-group-of-similar-functions-with-similar-names/?answer=13648#post-id-13648Comment by daniel.e2718 for <p>You're hitting a lot of subtle bits about the way Sage's type system works. There are at least three types of function-like things:</p>
<p>Sage Expressions -- not functions, but you can substitute into them, so it's not entirely unlike one -- which live in the Symbolic Ring:</p>
<pre><code>sage: x = var("x")
sage: f = 1/x^3
sage: f
x^(-3)
sage: parent(f)
Symbolic Ring
sage: f.subs(x=3)
1/27
sage: f(x=3)
1/27
</code></pre>
<p>Sage functions:</p>
<pre><code>sage: g(x) = 1/x^3
sage: parent(g)
Callable function ring with arguments (x,)
sage: g(3)
1/27
</code></pre>
<p>and Python functions (including lambdas):</p>
<pre><code>sage: def h(x): return 1/x^3
....:
sage: parent(h)
<type 'function'>
sage: h(3)
1/27
</code></pre>
<p>Where possible, it's usually desirable to stick with the Sage objects because they have lots of useful methods inside them whereas the Python functions don't.</p>
<p>You seem to be trying to make a number of objects which have <em>names</em> like 'f0', 'f1', 'f2', etc, and put them into the namespace. You <em>could</em> do that, but it's generally a bad idea, so I won't explain how. The usual rule is "Don't put your data in variable names!"</p>
<p>It's easy to make a list of Expressions or Sage functions instead:</p>
<pre><code>sage: fs = [1/x^i for i in range(10)]
sage: fs
[1, 1/x, x^(-2), x^(-3), x^(-4), x^(-5), x^(-6), x^(-7), x^(-8), x^(-9)]
sage: fs[3]
x^(-3)
sage: fs[3](3)
1/27
sage: gs = [(1/x^i).function(x) for i in range(10)]
sage: gs
[x |--> 1, x |--> 1/x, x |--> x^(-2), x |--> x^(-3), x |--> x^(-4), x |--> x^(-5), x |--> x^(-6), x |--> x^(-7), x |--> x^(-8), x |--> x^(-9)]
sage: gs[3](x=3)
1/27
</code></pre>
<p>Note that I've used the <code>.function</code> syntax to inline what "g(x)=1/x^3" does.</p>
https://ask.sagemath.org/question/9027/creating-a-group-of-similar-functions-with-similar-names/?comment=19701#post-id-19701Thanks again for a detailed response! You've helped me realize (though I already knew) that I know very little about programming and such. The more you know, the more you know you don't know...Sat, 02 Jun 2012 20:31:41 +0200https://ask.sagemath.org/question/9027/creating-a-group-of-similar-functions-with-similar-names/?comment=19701#post-id-19701Comment by calc314 for <p>You're hitting a lot of subtle bits about the way Sage's type system works. There are at least three types of function-like things:</p>
<p>Sage Expressions -- not functions, but you can substitute into them, so it's not entirely unlike one -- which live in the Symbolic Ring:</p>
<pre><code>sage: x = var("x")
sage: f = 1/x^3
sage: f
x^(-3)
sage: parent(f)
Symbolic Ring
sage: f.subs(x=3)
1/27
sage: f(x=3)
1/27
</code></pre>
<p>Sage functions:</p>
<pre><code>sage: g(x) = 1/x^3
sage: parent(g)
Callable function ring with arguments (x,)
sage: g(3)
1/27
</code></pre>
<p>and Python functions (including lambdas):</p>
<pre><code>sage: def h(x): return 1/x^3
....:
sage: parent(h)
<type 'function'>
sage: h(3)
1/27
</code></pre>
<p>Where possible, it's usually desirable to stick with the Sage objects because they have lots of useful methods inside them whereas the Python functions don't.</p>
<p>You seem to be trying to make a number of objects which have <em>names</em> like 'f0', 'f1', 'f2', etc, and put them into the namespace. You <em>could</em> do that, but it's generally a bad idea, so I won't explain how. The usual rule is "Don't put your data in variable names!"</p>
<p>It's easy to make a list of Expressions or Sage functions instead:</p>
<pre><code>sage: fs = [1/x^i for i in range(10)]
sage: fs
[1, 1/x, x^(-2), x^(-3), x^(-4), x^(-5), x^(-6), x^(-7), x^(-8), x^(-9)]
sage: fs[3]
x^(-3)
sage: fs[3](3)
1/27
sage: gs = [(1/x^i).function(x) for i in range(10)]
sage: gs
[x |--> 1, x |--> 1/x, x |--> x^(-2), x |--> x^(-3), x |--> x^(-4), x |--> x^(-5), x |--> x^(-6), x |--> x^(-7), x |--> x^(-8), x |--> x^(-9)]
sage: gs[3](x=3)
1/27
</code></pre>
<p>Note that I've used the <code>.function</code> syntax to inline what "g(x)=1/x^3" does.</p>
https://ask.sagemath.org/question/9027/creating-a-group-of-similar-functions-with-similar-names/?comment=19667#post-id-19667Will do! Thanks for your patience...I'm still learning the etiquette for this question/answer system.Thu, 07 Jun 2012 15:28:44 +0200https://ask.sagemath.org/question/9027/creating-a-group-of-similar-functions-with-similar-names/?comment=19667#post-id-19667Comment by calc314 for <p>You're hitting a lot of subtle bits about the way Sage's type system works. There are at least three types of function-like things:</p>
<p>Sage Expressions -- not functions, but you can substitute into them, so it's not entirely unlike one -- which live in the Symbolic Ring:</p>
<pre><code>sage: x = var("x")
sage: f = 1/x^3
sage: f
x^(-3)
sage: parent(f)
Symbolic Ring
sage: f.subs(x=3)
1/27
sage: f(x=3)
1/27
</code></pre>
<p>Sage functions:</p>
<pre><code>sage: g(x) = 1/x^3
sage: parent(g)
Callable function ring with arguments (x,)
sage: g(3)
1/27
</code></pre>
<p>and Python functions (including lambdas):</p>
<pre><code>sage: def h(x): return 1/x^3
....:
sage: parent(h)
<type 'function'>
sage: h(3)
1/27
</code></pre>
<p>Where possible, it's usually desirable to stick with the Sage objects because they have lots of useful methods inside them whereas the Python functions don't.</p>
<p>You seem to be trying to make a number of objects which have <em>names</em> like 'f0', 'f1', 'f2', etc, and put them into the namespace. You <em>could</em> do that, but it's generally a bad idea, so I won't explain how. The usual rule is "Don't put your data in variable names!"</p>
<p>It's easy to make a list of Expressions or Sage functions instead:</p>
<pre><code>sage: fs = [1/x^i for i in range(10)]
sage: fs
[1, 1/x, x^(-2), x^(-3), x^(-4), x^(-5), x^(-6), x^(-7), x^(-8), x^(-9)]
sage: fs[3]
x^(-3)
sage: fs[3](3)
1/27
sage: gs = [(1/x^i).function(x) for i in range(10)]
sage: gs
[x |--> 1, x |--> 1/x, x |--> x^(-2), x |--> x^(-3), x |--> x^(-4), x |--> x^(-5), x |--> x^(-6), x |--> x^(-7), x |--> x^(-8), x |--> x^(-9)]
sage: gs[3](x=3)
1/27
</code></pre>
<p>Note that I've used the <code>.function</code> syntax to inline what "g(x)=1/x^3" does.</p>
https://ask.sagemath.org/question/9027/creating-a-group-of-similar-functions-with-similar-names/?comment=19673#post-id-19673Thank you, @DSM, for the extremely useful explanation. I do have a follow-up question. How do you classify functions defined with the piecewise command. This seems to be a separate class. When I use the parent() command on a piecewise function, Sage says it is an "instance." Can you help me understand what that means? Thanks!Wed, 06 Jun 2012 23:01:14 +0200https://ask.sagemath.org/question/9027/creating-a-group-of-similar-functions-with-similar-names/?comment=19673#post-id-19673Comment by DSM for <p>You're hitting a lot of subtle bits about the way Sage's type system works. There are at least three types of function-like things:</p>
<p>Sage Expressions -- not functions, but you can substitute into them, so it's not entirely unlike one -- which live in the Symbolic Ring:</p>
<pre><code>sage: x = var("x")
sage: f = 1/x^3
sage: f
x^(-3)
sage: parent(f)
Symbolic Ring
sage: f.subs(x=3)
1/27
sage: f(x=3)
1/27
</code></pre>
<p>Sage functions:</p>
<pre><code>sage: g(x) = 1/x^3
sage: parent(g)
Callable function ring with arguments (x,)
sage: g(3)
1/27
</code></pre>
<p>and Python functions (including lambdas):</p>
<pre><code>sage: def h(x): return 1/x^3
....:
sage: parent(h)
<type 'function'>
sage: h(3)
1/27
</code></pre>
<p>Where possible, it's usually desirable to stick with the Sage objects because they have lots of useful methods inside them whereas the Python functions don't.</p>
<p>You seem to be trying to make a number of objects which have <em>names</em> like 'f0', 'f1', 'f2', etc, and put them into the namespace. You <em>could</em> do that, but it's generally a bad idea, so I won't explain how. The usual rule is "Don't put your data in variable names!"</p>
<p>It's easy to make a list of Expressions or Sage functions instead:</p>
<pre><code>sage: fs = [1/x^i for i in range(10)]
sage: fs
[1, 1/x, x^(-2), x^(-3), x^(-4), x^(-5), x^(-6), x^(-7), x^(-8), x^(-9)]
sage: fs[3]
x^(-3)
sage: fs[3](3)
1/27
sage: gs = [(1/x^i).function(x) for i in range(10)]
sage: gs
[x |--> 1, x |--> 1/x, x |--> x^(-2), x |--> x^(-3), x |--> x^(-4), x |--> x^(-5), x |--> x^(-6), x |--> x^(-7), x |--> x^(-8), x |--> x^(-9)]
sage: gs[3](x=3)
1/27
</code></pre>
<p>Note that I've used the <code>.function</code> syntax to inline what "g(x)=1/x^3" does.</p>
https://ask.sagemath.org/question/9027/creating-a-group-of-similar-functions-with-similar-names/?comment=19671#post-id-19671@calc314: if you have a followup, please make it a separate question-- that way lots of people who can answer will see it. It's easy to miss questions buried in comments. :^)Thu, 07 Jun 2012 10:13:10 +0200https://ask.sagemath.org/question/9027/creating-a-group-of-similar-functions-with-similar-names/?comment=19671#post-id-19671