ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 17 Nov 2011 11:40:26 -0600Is there a way to set arbitrary function commuting rules?http://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 08:13:21 -0600http://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>
http://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 11:40:26 -0600http://ask.sagemath.org/question/8486/is-there-a-way-to-set-arbitrary-function-commuting-rules/?answer=12915#post-id-12915