ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 17 Nov 2011 18:40:26 +0100Is there a way to set arbitrary function commuting rules?https://ask.sagemath.org/question/8486/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(x*y) = d(x*y) (multiplication does not commute), but I need d(x+y) = d(x) + d(y) (addition does commute). Any ideas?
Thu, 17 Nov 2011 15:13:21 +0100https://ask.sagemath.org/question/8486/is-there-a-way-to-set-arbitrary-function-commuting-rules/Answer by burcin for <p>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</p>
<pre><code>d = function('d')
var('x y')
</code></pre>
<p>I'd like to specify d(x<em>y) = d(x</em>y) (multiplication does not commute), but I need d(x+y) = d(x) + d(y) (addition does commute). Any ideas?</p>
https://ask.sagemath.org/question/8486/is-there-a-way-to-set-arbitrary-function-commuting-rules/?answer=12915#post-id-12915Here 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?
Thu, 17 Nov 2011 18:40:26 +0100https://ask.sagemath.org/question/8486/is-there-a-way-to-set-arbitrary-function-commuting-rules/?answer=12915#post-id-12915