OK, though I've done this many times, given that coding this in Mathematica requires one to have a good enough understanding for the core language, let me do a favor:
Clear[myD, mySum]
myD[a_ b_, c_] := a myD[b, c] + b myD[a, c]
(* Add the rule for addition just in case: *)
myD[a_ + b_, c_] := myD[a, c] + myD[b, c]
myD[mySum[a_, b__], x_] := mySum[myD[a, x], b]
myD[(h_ /; Context[h] =!= "System`")[a__], x_] :=
With[{l = Length@{a}},
Total@MapThread[
Derivative[Sequence @@ #][h][a] myD[#2, x] &, {IdentityMatrix@
l, {a}}]]
myD[Subscript[a_, i___, j1_, k___], Subscript[a_, i___, j2_, k___]] :=
KroneckerDelta[j1, j2]
myD[Subscript[a_, __], Subscript[b_, __]] := 0
mySum[KroneckerDelta@OrderlessPatternSequence[i_, j_] expr_, jL___,
j_, jR___] := mySum[expr /. j -> i, jL, jR]
mySum[a_] := a
MakeBoxes[mySum[a__], fmt_] ^:=
With[{arg = MakeBoxes[#, fmt] & /@ {a}},
With[{funcbody =
MakeBoxes[Sum[a], fmt] /. Thread[{a}
Thread[arg -> (Slot /@ Length@Range@Length@{a})]},
TemplateBox[MakeBoxes[#, fmt] & /@ {a}TemplateBox[arg, "mySum",
DisplayFunction -> (funcbody &)]]]]]
Usage:
myD[mySum[Subscript[a, i, j] Subscript[x, j], j], Subscript[a, i, k]]
myD[Subscript[x, k] mySum[g[Subscript[a, i, j],
Subscript[x, j]], i, j], Subscript[x, n]]
You can press Ctrl+Shift+t to transform the output to TraditionalForm
, and Ctrl+Shift+n to transform the output to StandardForm
.