assignments and functions

4 views
Skip to first unread message

Stan Schymanski

unread,
Aug 25, 2008, 5:59:28 AM8/25/08
to sage-support
Dear all,

This subject has been used before, but I was not able to add a reply
to it, probably because it is too old. So, here it is again.

I wondered how python handles assigned variables in function
definitions. For example, I would like to define an existing symbolic
function as a python function in order to do automated calculations
with it. I define y=a*x^2+b*x, then do a def f(x): return y, but f(x)
does not evaluate if I assign values to a or b. However, if I do a def
g(x): return a*x^2+b*x, g(x) evaluates using assigned values for a and
b.

Is there a way to avoid the need to type out the equation in the def
g(x): return ... statement? I hope the example below clarifies what I
mean.

Cheers,
Stan

----------------------------------------------------------------------
| SAGE Version 3.1.1, Release Date: 2008-08-17 |
| Type notebook() for the GUI, and license() for information. |
----------------------------------------------------------------------

sage: var('a b x')
(a, b, x)
sage: y=a*x^2+b*x
sage: def f(x):
....: return y
....:
sage: f(x)
a*x^2 + b*x
sage: a=3
sage: f(x)
a*x^2 + b*x
sage: def g(x):
....: return a*x^2+b*x
....:
sage: g(x)
3*x^2 + b*x
sage: b=5
sage: g(x)
3*x^2 + 5*x
sage: f(x)
a*x^2 + b*x
sage: def h(x):
....: return y.subs(locals())
....:
sage: h(x)
a*x^2 + b*x
sage: g(x)
3*x^2 + 5*x

Only g(x) does what I wanted, but this one was defined by writing out
the function. How can I pass a symbolic function to python that is
stored in y?

Simon King

unread,
Aug 25, 2008, 7:13:52 AM8/25/08
to sage-support
Dear Stan

On Aug 25, 11:59 am, Stan Schymanski <schym...@gmail.com> wrote:
> I wondered how python handles assigned variables in function
> definitions.

As much as i understood a recent thread on sage-devel, several people
would like to have a powerful substitution mechanism in Sage.

Concerning your problem: In the definition of h, you use
y.subs(locals()). But i think this can not work, because locals()
refers to the current name space (or what is the word for it?). So,
unless you define a and b *inside* the definition of h, it wouldn't
work.

However, you define a and b in the *global* name space. So, the
following does work:
sage: var('a b x')
(a, b, x)
sage: y=a*x^2+b*x
sage: def h(x):
....: return y.subs(globals())
....:
sage: h(x)
a*x^2 + b*x
sage: a=2
sage: h(x)
2*x^2 + b*x
sage: b=3
sage: h(x)
2*x^2 + 3*x

Cheers
Simon

Simon King

unread,
Aug 25, 2008, 7:33:05 AM8/25/08
to sage-support
> sage: def h(x):
> ....:     return y.subs(globals())

Oops, this is probably not what you want, becaus h(3) would not give
the expected result. This may be better:

sage: var('a b x')
(a, b, x)
sage: def h(x):
....: return y.subs(locals()).subs(globals())
....:
sage: a=2
sage: b=3
sage: h(x)
2*x^2 + 3*x
sage: var('a b x')
(a, b, x)
sage: h(4)
4*b + 16*a
sage: var('z')
z
sage: x=1
sage: h(z)
a*z^2 + b*z

Explanation: x is defined in the local name space of the function h.
Hence, subs(locals()) replaces x by z (and not by 1!) in the last
example. And then, the remaining contents of y are substitutet.

Yours
Simon

Stan Schymanski

unread,
Aug 25, 2008, 8:17:00 AM8/25/08
to sage-support
Dear Simon,

Thanks a lot for that! I haven't noticed the difference between
subs(locals()) and subs(globals()). This helps a lot.

Stan
Reply all
Reply to author
Forward
0 new messages