On Fri, 4 Feb 2011 19:40:34 +0800 "D. S. McNeil" <[email protected]> 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 [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 URL: http://www.sagemath.org
