Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

answered 2012-06-02 10:48:23 -0600

DSM gravatar image

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:

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: parent(f)
Symbolic Ring
sage: f = 1/x^3
sage: f
x^(-3)
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.

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:

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 = 1/x^3
sage: f
x^(-3)
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.