Ask Your Question

Is there a way to set arbitrary function commuting rules?

asked 2011-11-17 08:13:21 -0500

tcfisher gravatar image

I am trying to perform a derivation using discrete difference operations, but I don't want to actually define my discrete operator. What I would like to do, is specify

d = function('d')
var('x y')

I'd like to specify d(xy) = d(xy) (multiplication does not commute), but I need d(x+y) = d(x) + d(y) (addition does commute). Any ideas?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2011-11-17 11:40:26 -0500

updated 2011-11-18 00:54:06 -0500

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)

The documentation you get from

sage: function?

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

sage: sage.symbolic.function_factory.function?
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2011-11-17 08:13:21 -0500

Seen: 205 times

Last updated: Nov 18 '11