Ask Your Question

# Is there a way to set arbitrary function commuting rules?

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 close merge delete

## 1 Answer

Sort by » oldest newest most voted

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?


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

sage: sage.symbolic.function_factory.function?

more

## Your Answer

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

Add Answer

## Stats

Asked: 2011-11-17 15:13:21 +0200

Seen: 277 times

Last updated: Nov 18 '11