# Problems with TagSet

I am new with TagSet and TagSetDelayed and I'm not understanding properly how they works. I introduced the following definition:

KK /: D[KK @@ Thread[t@Range[8]], t[j_]] := KKd @@ Join[{j}, Thread[t@Range[8]]]


The following expression returns the correct result

In[]:= D[KK @@ Thread[t@Range[8]], t[3]]
Out[]:= KKd[3, t[1], t[2], t[3], t[4], t[5], t[6], t[7], t[8]]


If now I try something slightly different

In[]:=D[Exp[KK @@ Thread[t @ Range[8]]], t[3]] //InputForm

Out[]:=E^KK[t[1], t[2], t[3], t[4], t[5], t[6], t[7], t[8]]*
Derivative[0, 0, 1, 0, 0, 0, 0, 0][KK][t[1], t[2], t[3], t[4],  t[5], t[6], t[7], t[8]]


Why in the second example the code does not return the expected result (i.e. Exp[KK[t[1], t[2], t[3], t[4], t[5], t[6], t[7], t[8]]] KKd[3, t[1], t[2], t[3], t[4], t[5], t[6], t[7], t[8]])?

Also, is there a way to use Range in a general way? I mean define something like

KK /: D[KK @@ Thread[t@Range[n_]], t[j_]] := KKd @@ Join[{j}, Thread[t@Range[n]]]


Now it returns "Range specification in Range[n_] does not have appropriate bounds".

You can give UpValues for Derivative instead (as alluded to by Daniel):

KK /: Derivative[0, 0, 1, 0, 0, 0, 0, 0][KK] = KKd[3, ##]&;


Then:

D[Exp[KK @@ Thread[t @ Range[8]]], t[3]]


E^KK[t[1], t[2], t[3], t[4], t[5], t[6], t[7], t[8]] KKd[3, t[1], t[2], t[3], t[4], t[5], t[6], t[7], t[8]]

To define UpValues for each possible argument, you can do:

Table[
With[{i=i}, KK /: (Derivative @@ UnitVector[8,i])[KK] = KKd[i, ##]&],
{i, 8}
];


Another idea is to use Condition:

KK /: Derivative[a__][KK] := With[{i = First @ FirstPosition[{a}, 1]},
KKd[i, ##]& /; MatchQ[Sort @ Tally[{a}], {{0, _}, {1, 1}}]
]


Look at the definition of KK:

?? KK


You defined the derivative of KK with specific arguments relative to t[_]

Now consider:

 D[Exp[KK @@ Thread[t@Range[8]]], t[3]]


Here we need the derivative of KK relative to the third argument. These arguments are now considered general and not covered by your definition.

You need to define the derivative relative to general arguments to make this work:

• Thanks! So this mean that I have to introduce definitions for derivatives with respect to each argument if I want to obtain the correct result for a generic t[_]? Dec 1, 2021 at 13:17
• Yes, However, note, t[] is not generic. You would need e.g.: t1 or x1_... Dec 1, 2021 at 14:17