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?
--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/sage-support
URLs: http://www.sagemath.org
-~----------~----~----~----~------~----~------~--~---