# Chain rule when taking derivatives with UpValues

I have a summation function, for which I have defined derivation via

Customsum /: D[Customsum[idx_, a_, b_, exprs_], y_] := Customsum[idx, a, b D[expr, y]];


where idx is the index with bounds a and b. This does not seem to work, though when the sum is inside another function. For example, I get

D[Sqrt[Customsum[idx, 1, 2, func[idx, y]]], y]


where via chain rule I would expect

1/Sqrt[Customsum[idx, 1, 2, func[idx, y]]]*1/2*Customsum[idx, 1, 2, Derivative[0,1][func][idx, y]]]


To give an even simpler example, look what happens with Plus

D[f[g[x,1]+g[x,2]],x]
(* (f^′)[g[x,1]+g[x,2]] ((g^(1,0))[x,1]+(g^(1,0))[x,2]) *)


How can I achieve the same with

D[f[sum[g[x,i]]],x]
(* (f^′)[sum[g[x,i]]] (sum^′)[g[x,i]] (g^(1,0))[x,i] *)


Is there a way to get the chain rule to work for these cases?

• I fixed the typos in your question because I believe it is a good one. I currently see no direct way to do this, but defining UpValues to sum with D will not work. If you have a wrapping function like your Sqrt, then the intermediate expressions only consist of Derivative and there is no D anymore that matches AFAIK. But I might of course be wrong. – halirutan Jun 16 '18 at 0:59
• You might also want to look at this question as it is very similar to yours, but I'm not sure your problem can be solved with the information there. So don't claim it is a duplicate, but it might be worth reading. – halirutan Jun 16 '18 at 1:04

There is a system option that provides a list of excluded functions for differentiation. If you add Customsum to the list you should be able to use your definition as expected.

exf = "ExcludedFunctions" /. ("DifferentiationOptions" /. SystemOptions[]);

SetSystemOptions[
"DifferentiationOptions" -> "ExcludedFunctions" -> Append[exf, Customsum]];

Customsum /: D[Customsum[idx_, a_, b_, exprs_], y_] := Customsum[idx, a, b, D[exprs, y]]

D[Customsum[idx, 1, 2, func[idx, y]], y] // InputForm

(* Customsum[idx, 1, 2, Derivative[0, 1][func][idx, y]] *)

D[Sqrt @ Customsum[idx, 1, 2, func[idx, y]], y] // InputForm

(* Customsum[idx, 1, 2, Derivative[0, 1][func][idx, y]] /
(2*Sqrt[Customsum[idx, 1, 2, func[idx, y]]]) *)

• Thank you very much :) – halirutan Jun 16 '18 at 11:46
• Thanks! It seems, though, as if the other definitions for Custom'sum are messing with this method, so that I get zero again for the Sqrt case. Therefore I'll probably stick with the NonConstant solution I posted below. – Telperion Jun 16 '18 at 13:18

SetOptions[D, NonConstants -> {Customsum}];


a D[Custom'sum[idx,a,b,func[idx,y],y,NonConstants->{Custom'sum}] appears during the evaluation of the derivative, and the UpValue, slightly modified to include the NonConstants->{Custom'sum}, works again:

SetOptions[D, NonConstants -> {Customsum}];

Customsum /: D[Customsum[idx_, a_, b_, exprs_], y_, c___] := Customsum[idx, a, b , D[exprs, y, c]];

D[Sqrt @ Customsum[idx, 1, 2, func1[idx, x]], x] // InputForm

(* Customsum[idx, 1, 2, Derivative[0, 1][func1][idx, x]]/(2*Sqrt[Cutomsum[idx, 1, 2, func1[idx, x]]]) *)