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

Custom`sum /: D[Custom`sum[idx_, a_, b_, exprs_], y_] := Custom`sum[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[Custom`sum[idx, 1, 2, func[idx, y]]], y]

Mathematica graphics

where via chain rule I would expect

1/Sqrt[Custom`sum[idx, 1, 2, func[idx, y]]]*1/2*Custom`sum[idx, 1, 2, Derivative[0,1][func][idx, y]]]

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

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

How can I achieve the same with

(* (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?

  • $\begingroup$ 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. $\endgroup$
    – halirutan
    Jun 16, 2018 at 0:59
  • 1
    $\begingroup$ 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. $\endgroup$
    – halirutan
    Jun 16, 2018 at 1:04

2 Answers 2


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

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

  "DifferentiationOptions" -> "ExcludedFunctions" -> Append[exf, Custom`sum]];

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

D[Custom`sum[idx, 1, 2, func[idx, y]], y] // InputForm

(* Custom`sum[idx, 1, 2, Derivative[0, 1][func][idx, y]] *)

D[Sqrt @ Custom`sum[idx, 1, 2, func[idx, y]], y] // InputForm

(* Custom`sum[idx, 1, 2, Derivative[0, 1][func][idx, y]] /
  (2*Sqrt[Custom`sum[idx, 1, 2, func[idx, y]]]) *)
  • $\begingroup$ Thank you very much :) $\endgroup$
    – halirutan
    Jun 16, 2018 at 11:46
  • $\begingroup$ 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. $\endgroup$
    – Telperion
    Jun 16, 2018 at 13:18

The question halirutan linked to was very helpful. By setting

SetOptions[D, NonConstants -> {Custom`sum}];

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 -> {Custom`sum}];

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

D[Sqrt @ Custom`sum[idx, 1, 2, func1[idx, x]], x] // InputForm

(* Custom`sum[idx, 1, 2, Derivative[0, 1][func1][idx, x]]/(2*Sqrt[Cutom`sum[idx, 1, 2, func1[idx, x]]]) *)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.