Hi Timo,

On Sun, 5 Aug 2012 10:12:41 -0700 (PDT)
Timo Kluck <tkl...@gmail.com> wrote:

> I think that the distinction between
> 
> def f(x):
>     return x^2
> 
> and
> 
> f(x) = x^2
> 
> is a common source of confusion, and everyone will run into it when
> trying to define piecewise functions. It would be possible to
> introduce a decorator, say @symbolic_function, that makes sure that
> 
> @symbolic_function(x)
> def f(x):
>     return x^2
> 
> makes f a symbolic function. I've done just that in this published 
> worksheet:
> http://www.sagenb.org/home/pub/4919

This looks great. Can you upload your code to #7636 on trac.

http://trac.sagemath.org/sage_trac/ticket/7636


> As you can see, it works with piecewise functions, too. Of course, it 
> should probably do something sane with multiple variables, class
> methods, etc, but it seems possible.
> 
> I can probably produce a patch for this, but I would like some
> feedback first.

On Sun, 5 Aug 2012 10:28:43 -0700 (PDT)
Timo Kluck <tkl...@gmail.com> wrote:

> Sorry, I didn't realize that. Here's the code.
> 
> ---
> def symbolic_function(variables, *args, **kwds):
>     def result(f):
>         def evalf_func(self,x,parent=None):
>             if parent == None:
>                 return f(x)
>             else:
>                 return parent(f(x))
>         return function(f.__name__, variables, evalf_func=evalf_func, 
> *args, **kwds).operator()
>         
>     return result
> 
> @symbolic_function(x)
> def steps(x):
>     if x < 0:
>         return -x
>     elif x < 3:
>         return x
>     else:
>         return 3

It might be better to use the function_factory method from
sage.symbolic.function_factory instead of the top level function()
method. This would remove the need to specify the names of the
variables, then get the symbolic function using .operator().

You can get the number of arguments with f.__code__.co_argcount.


Cheers,
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



Reply via email to