[sage-support] plotting box function
hello, why is the below code plotting a flat function rather than a box one? renato def box(x,c): if abs(x) c: return 1 else: return 0 var('x') plot(box(x,1),(x,-3,3)) -- 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
Re: [sage-support] plotting box function
On Fri, Feb 4, 2011 at 11:37 AM, Renato Budinich renn...@gmail.com wrote: hello, why is the below code plotting a flat function rather than a box one? When you do, plot(box(x,1),(x,-3,3)) it evaluates box(x,1) which returns 0 because the variable x is not always less than 1. You need to delay the evaluation of this function: sage: plot(lambda x: box(x,1),(x,-3,3)) or sage: from sage.misc.decorators import specialize sage: f = specialize(c=1)(box) sage: plot(f,(x,-3,3)) or sage: from functools import partial sage: f = partial(box, c=1) sage: plot(f,(x,-3,3)) --Mike -- 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
Re: [sage-support] plotting box function
hello, why is the below code plotting a flat function rather than a box one? There are two things going on. First, in the line plot(box(x,1),(x,-3,3)) box(x,1) is actually being evaluated when the line is executed, and not thereafter. IOW you're computing box(x, 1), which is 0, so the above is equivalent to plot(0, (x, -3, 3)) You might want to stick a print statement inside the box function (print called!) to convince yourself this is true. Second, box(x,1) = 0 because the condition abs(x) 1 is False for a variable x, and so the else is executed. Note that False here translates as I can't prove that it's True: if instead of the else you'd written abs(x) = 1 ,that'd be False too, neither path would get executed, and so the result would be None (what Python returns when there's no explicit return statement.) OTOH, if you call box(x, infinity), you get 1. There are a few ways around this. Probably the most general-purpose solution (which works even when some Sage-specific tricks don't) is to delay the execution of the box function by writing a lambda-function wrapper: plot(lambda x: box(x,1), (x, -3, 3)) which is a short way to avoid having to write a new function def box1(x): return box(x, 1) and then calling plot(box1, (x, -3, 3)). Doug -- Department of Earth Sciences University of Hong Kong -- 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
Re: [sage-support] plotting box function
On Fri, 4 Feb 2011 19:40:34 +0800 D. S. McNeil dsm...@gmail.com wrote: hello, why is the below code plotting a flat function rather than a box one? There are two things going on. First, in the line plot(box(x,1),(x,-3,3)) box(x,1) is actually being evaluated when the line is executed, and not thereafter. IOW you're computing box(x, 1), which is 0, so the above is equivalent to plot(0, (x, -3, 3)) You might want to stick a print statement inside the box function (print called!) to convince yourself this is true. Second, box(x,1) = 0 because the condition abs(x) 1 is False for a variable x, and so the else is executed. Note that False here translates as I can't prove that it's True: if instead of the else you'd written abs(x) = 1 ,that'd be False too, neither path would get executed, and so the result would be None (what Python returns when there's no explicit return statement.) OTOH, if you call box(x, infinity), you get 1. thanks for the clear explanation, I got it. There are a few ways around this. Probably the most general-purpose solution (which works even when some Sage-specific tricks don't) is to delay the execution of the box function by writing a lambda-function wrapper: plot(lambda x: box(x,1), (x, -3, 3)) but why does this way the execution of the function get delayed? renato -- 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
Re: [sage-support] plotting box function
plot(lambda x: box(x,1), (x, -3, 3)) but why does this way the execution of the function get delayed? because lambda is a way to define a function. This works more or less like the following : def MyFunction(x) return box(x,1) plot(MyFunction,(x,-3,3)) See for example http://www.secnetix.de/~olli/Python/lambda_functions.hawk -- 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