On 4/8/11 2:00 PM, John H Palmieri wrote:
On Friday, April 8, 2011 11:03:14 AM UTC-7, ObsessiveMathsFreak wrote:
I have a python type function taking two variables is defined in such
a say that accidental evaluation is a possibility. Here is a
simplified version
def h(x,n):
if x>2:
return n-x
else:
return n*x-2
How can functions like this be plotted over x for a constant value of
n in sage?
sage: plot(lambda x: h(x,3), (x, 0, 4))
works for me.
Another approach that supplies default arguments for the *first*
variables is to use functools.partial:
def h(x,n):
if x>2:
return n-x
else:
return n*x-2
from functools import partial
plot(partial(h,1),(n,-1,1))
This effectively plots h(1,n), where n goes from -1 to 1.
Note that we can't do
plot(partial(h,x=1),(n,-1,1))
since plot calls the function by positional arguments, rather than
keyword arguments (i.e., this last plot calls h like this:
h(-.5434344,x=1), and so we get two values for x). I think this is a
bug; I think if the variable is specified in the plot range, the
function should be called with a keyword argument, so that the function
would be called as h(n=-.5434344,x=1). I believe I even have a patch
from late last year somewhere on my laptop that changes this behavior to
call a function using keyword arguments if the variable is specified in
the plot range. Once this bug is fixed, then doing plot(partial(h,x=1),
(n,-1,1)) would work.
Thanks,
Jason
--
To post to this group, send email to sage-support@googlegroups.com
To unsubscribe from this group, send email to
sage-support+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/sage-support
URL: http://www.sagemath.org