Hi Jason, On Tue, 17 Nov 2009 22:46:40 -0600 Jason Grout <jason-s...@creativetrax.com> wrote:
<snip> > So I did the following, which seemed to work, but also seemed a bit > kludgy: > > from functools import wraps > def eval_if_numeric(f): > @wraps(f) > def my_f(*args,**kwds): > try: > all(CC(a) for a in args) > except TypeError: > return None > return f(*args,**kwds) > return my_f > > @eval_if_numeric > def eval_f(x,t,**kwds): > if x<=0: > return 0 > else: > return sin(x+t)/x > > f=function('f', nargs=2,eval_func=eval_f) > plot(f(t,t),(t,-10,10)) After applying #7490, you can achieve the same with this: def eval_f(self, x, t): try: return self._evalf_(RR(x), RR(t), RR) except TypeError: return None def evalf_f(self, x, t, parent=None): if x<0: return 0 else: return parent(sin(x+t)/x) g = function('g', nargs=2, eval_func=eval_f, evalf_func=evalf_f) > Also, I just discovered that the following does work: > > def eval_f(x,t,*args,**kwds): > if x<0: > return 0 > else: > return n(sin(x+t)/x) > > f=function('f', nargs=2,evalf_func=eval_f) > > plot(f(t,t),(t,-10,10)) > > but then f(1,1) just returns f(1,1), not sin(2). > > What if we made it so that there was (yet another?) eval function > that guaranteed that inputs would not be symbolic expressions, but > numbers---basically what my decorator above was doing. Using this > idea, then: > > def eval_f(x,t,*args,**kwds): > if x<0: > return 0 > else: > return sin(x+t)/x > > f=function('f', nargs=2,eval_num_func=eval_f) > > would have the following properties: > > sage: f(x,1) > f(x,1) > sage: f(1,1) > sin(2) > sage: f(-1,t) > f(-1,t) > sage: f(-1,1) > 0 The default behavior for symbolic functions is to keep things unevaluated if the input is exact. Making this consistent is #1158 and #4102 on trac. We can add an option evaluate_numeric to the function_factory() constructor to change this default. > 3. The documentation to "function" is horribly out of date, at least > regarding the parameters you bring up in the example above. (Yes, I > know, submit a patch...I'm just bringing it up...) I added a brief description of the customizable methods to the docstring you get with function? with the patch #7491. Thanks. Burcin -- To post to this group, send an email to sage-devel@googlegroups.com To unsubscribe from this group, send an email to sage-devel-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-devel URL: http://www.sagemath.org