# How to assign the following rule?

Consider the following object: function[x,a,b]. It is a symbol carrying one "coordinate" x and two indices a,b.

I would like to make the rule Der

D[function[x,a,b],function[x,c,f]] -> KroneckerDelta[a, c]KroneckerDelta[b,f]


in order to apply it to an arbitrary object obj containing function and its powers (a scalar expression, a matrix, etc.), Der[obj,function[x,c,f]]. Say, an example of the object is

obj=function[x,a,b]*function[x,a,c]*coefficient[b,c] +function[x,a,c]^2


For which the result of applying the rule should be

Der[obj,function[x,c,f]] = KroneckerDelta[a,c]KroneckerDelta[b,f]function[x,a,c]coefficient[b,c] + KroneckerDelta[a,c]KroneckerDelta[c,f]function[x,a,b]coefficient[b,c]+2function[x,a,c]*KroneckerDelta[a,c]KroneckerDelta[c,f]


Could you please tell me how to do this?

My attempt is to use UpSetDelayed:

D[function[x_, a_, b_], function[x_, c_, f_]] ^:=
KroneckerDelta[a, c] KroneckerDelta[b, f]


It works if derivating just function[x, a, b], but fails if there is any difference from the pattern. Say, if function[x, a, b] is a summand in some scalar expression, or it is inside a matrix (please see also this question).

• Note just for clarity that you are using UpSetDelayed and not TagSetDelayed Aug 1, 2023 at 12:29

You can make use of the NonConstants option to D:

function /: D[function[x,a_,b_], function[x,c_,f_], OptionsPattern[]] := If[
MemberQ[OptionValue[NonConstants], function],
KroneckerDelta[a,c] KroneckerDelta[b,f],
0
]


Then either:

D[obj, function[x, c, f], NonConstants->{function}]


coefficient[b, c] function[x, a, c] KroneckerDelta[a, c] KroneckerDelta[b, f] + coefficient[b, c] function[x, a, b] KroneckerDelta[a, c] KroneckerDelta[c, f] + 2 function[x, a, c] KroneckerDelta[a, c] KroneckerDelta[c, f]

or:

SetOptions[D, NonConstants->{function}];
D[obj, function[x, c, f]]


coefficient[b, c] function[x, a, c] KroneckerDelta[a, c] KroneckerDelta[b, f] + coefficient[b, c] function[x, a, b] KroneckerDelta[a, c] KroneckerDelta[c, f] + 2 function[x, a, c] KroneckerDelta[a, c] KroneckerDelta[c, f]

should do what you want.