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.Tue, 21 Mar 2017 02:49:08 +0100derivative of non-commuting symbolic producthttps://ask.sagemath.org/question/37003/derivative-of-non-commuting-symbolic-product/Consider the product rule $\frac d{dt}[A(t)B(t)]=\dot{A}(t)B(t)+A(t)\dot{B}(t)$ with $A$ and $B$ not commuting, e.g, matrix valued. I'd like to replicate this in sage, however, I don't see how I can specify that $A$ and $B$ do not commute. So far I have
<pre>
var('t')
A=function('A')(t)
B=function('B')(t)
diff(A*B,t)
</pre>
which yields
`B(t)*diff(A(t), t) + A(t)*diff(B(t), t)`. But here sage has assumed that the operators and their derivatives do commute. Not what I want.
I did look into `sage.symbolic.function_factory.function` and the like, but could not find anything about products. Am I overlooking something or is this currently not possible?Mon, 20 Mar 2017 15:57:40 +0100https://ask.sagemath.org/question/37003/derivative-of-non-commuting-symbolic-product/Answer by Björn for <p>Consider the product rule $\frac d{dt}[A(t)B(t)]=\dot{A}(t)B(t)+A(t)\dot{B}(t)$ with $A$ and $B$ not commuting, e.g, matrix valued. I'd like to replicate this in sage, however, I don't see how I can specify that $A$ and $B$ do not commute. So far I have</p>
<pre>var('t')
A=function('A')(t)
B=function('B')(t)
diff(A*B,t)
</pre>
<p>which yields
<code>B(t)*diff(A(t), t) + A(t)*diff(B(t), t)</code>. But here sage has assumed that the operators and their derivatives do commute. Not what I want.</p>
<p>I did look into <code>sage.symbolic.function_factory.function</code> and the like, but could not find anything about products. Am I overlooking something or is this currently not possible?</p>
https://ask.sagemath.org/question/37003/derivative-of-non-commuting-symbolic-product/?answer=37011#post-id-37011I realise I may just define a new operation "non-commutative product" (ncp for short) that takes two arguments and respects the product rule, as well as some basic simplifications.
<pre>
var('t s')
def e_func(self, *args, **kwds):
if args[0]==1:
return args[1]
elif args[1]==1:
return args[0]
elif args[0]*args[1]==0:
return 0
else:
pass
ncp = function('ncp', nargs=2,
tderivative_func=lambda self, *args, **kwds:\
ncp(diff(args[0],kwds['diff_param']),args[1])+ncp(args[0],diff(args[1],kwds['diff_param'])),\
eval_func=e_func,
print_func=lambda self,*args: format(args[0])+' . '+format(args[1]) # denote ncp by .
)
print ncp(t^2,3*s^3).derivative(t)
print ncp(t,3*s^3).derivative(t)
print ncp(t^2,3*s^3/t).derivative(t)
</pre>
Which produces
<pre>
t^2 . 3*s^3
2*t . 3*s^3
3*s^3
t^2 . -3*s^3/t^2 + 2*t . 3*s^3/t
</pre>
I would very much prefer a more "built-in" solution over this hack. Also, without further modification this approach does not work for products of three or more factors. For that reason I'll leave the acceptance of an answer open for a more concise solution.
Tue, 21 Mar 2017 02:49:08 +0100https://ask.sagemath.org/question/37003/derivative-of-non-commuting-symbolic-product/?answer=37011#post-id-37011