"lambda x : f(x)" should read "the function which maps x to f(x)". It has nothing to do with symbolic computations, and exists in Python.
note that "lambda x : x^2" is exactly the same as "lambda y : y^2", which is mathematically very sound. Using the symbolic ring however, if x and y are formal variables then x^2 is not the same as y^2 (though plotting them will give the same thing!) if you think about it, what is *really* a little weird, is that "plot(sin(x), (-1, 1))" should work at all. It assumes you are talking about the function which to x assigns sin(x) (and not, say, the constant function which to anything assigns the formal expression sin(x)). By the way if you fix a value for x, then "plot(sin(x), ...)" gives you a constant plot -- so the trick (of replacing sin(x) by a function) is context-dependent. By contrast, "plot(lambda x : sin(x), ...)" always works. Oh, and the implicit replacement works for symbolic expressions, like sin(x), and not your h(x,n) which is defined as a python function (a very different thing). As you have discovered, these sorts of things become much, much clearer when more than one variable is involved. "plot( x*y, ...)", or "plot(h(x, n))" is just ambiguous. You want to plot the function which to x assigns h(x, n), with n being fixed? well that is precisely lambda x : h(x, n). hope this helps. Pierre On 10 avr, 06:08, ObsessiveMathsFreak <obsessivemathsfr...@gmail.com> wrote: > Partial seems useful, thank you. The Lambda solutions also work. > > But what IS lambda anyway? I don't see that its doing anything other > than being syntactic verbose. > > On Apr 9, 7:24 am, Jason Grout <jason-s...@creativetrax.com> wrote: > > > 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