# 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

var('t')
A=function('A')(t)
B=function('B')(t)
diff(A*B,t)


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?

edit retag close merge delete

Sort by » oldest newest most voted

I 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.

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)


Which produces

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


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.

more