Ask Your Question

Revision history [back]

Here is how you can define an additive function:

sage: import operator
sage: def f_eval(self, arg):
....:     try:
....:         op = arg.operator()
....:     except AttributeError:
....:         return None
....:     if op is operator.add:
....:         return sum(map(self,arg.operands()))
....:     
sage: f = function('f',eval_func=f_eval)
sage: var('x,y')
(x, y)
sage: f(x+y)
f(x) + f(y)
sage: f(x*y)
f(x*y)

The documentation you get from

sage: function?

doesn't isn't very helpful, but this has some examples:

sage: sage.symbolic.function_factory.function?

Here is how you can define an additive function:

sage: import operator
sage: def f_eval(self, arg):
....:     try:
....:         op = arg.operator()
....:     except AttributeError:
....:         return None
....:     if op is operator.add:
....:         return sum(map(self,arg.operands()))
sum(map(self, arg.operands()))
....:     
sage: f = function('f',eval_func=f_eval)
sage: var('x,y')
(x, y)
sage: f(x+y)
f(x) + f(y)
sage: f(x*y)
f(x*y)

The documentation you get from

sage: function?

doesn't isn't very helpful, but this has some examples:

sage: sage.symbolic.function_factory.function?