# Why is D[a[b][c],a[b]] equal to 1[c]?

Background: I had the same issue as described here, and I went to extend Mr.Wizard’s code such that a[b][c] would be shown as $a_b^c$.

Issue: To my surprise, Mathematica evaluates D[a[b][c],a[b]] to 1[c] instead of 0. Here, a[b] and a[b][c] are meant to be unrelated variables.

Example: I have an expression which contains a[b][c], which stands for (is printed as, thanks to the code linked in Background) $a^c_b$, where $c$ is an index, not a power. When I take partial derivatives with respect to a[b], which is supposed to stand for an unrelated variable $a_b$, the term containing a[b][c] should be zero.

I was expecting the result to be simply 0, which would

• be consistent with D[f[x],f] evaluating to 0, which I find sensible, given that f in the first argument is an identifier of a function (which involves only the variable x), while the right one is a variable
• make sense assuming that in general, e.g., c and c[1] and c[2] are completely distinct variables, even if they share the same Head.
• make sense given that the actually returned result 1[c] seems to have no use case.

Questions:

1. Why does Mathematica interpret it that way? I am a Mathematica Newbie and eager to learn, so don’t gloss over details, please.

It seems as if [c] was a factor, while it isn’t, because this is evidently disallowed:

D[a[b]*[c],a[b]]
Syntax::sntxf: "a[b]*" cannot be followed by "[c]".
Syntax::tsntxi: "[c]" is incomplete; more input is needed.
Syntax::sntxi: Incomplete expression; more input is needed .

2. What can I do to make Mathematica interpret it such that this derivative is zero? I would like to continue to be able to define functions such as a[t_][b] := f[t].

• SubValues can be counterintuitive, I think it will be easiest to use a[b,c] instead – ssch Oct 15 '13 at 12:55
• Oh, I see. The question is about why does Mathematica (seem to) behave inconsistently. Personally, I can't make sense of D[f[x],f], so I wouldn't be bothered by the inconsistency. But it is curious that something different happens. – Michael E2 Oct 15 '13 at 14:05
• @Michael Well, my question 2 has practical relevance for me. I have an expression which contains a[b][c], which stands for $a_b^c$, where c is an index, not a power. When I take partial derivatives for a[b], which is a different variable, the term containing a[b][c] should be zero. – LCC Oct 15 '13 at 14:43
• Thanks for the clarification. I was interpreting the expression the way Mathematica would, not how you meant it. I think ssch's suggestion is a good one. You can use a[{b,t},c] or a[b,t,c] and format them as $a_{b,t}^c$. – Michael E2 Oct 15 '13 at 15:13
• @LCC I think what you're seeing is the limits of forcing a programming language to go against its syntax and rules and act like pencil-and-paper mathematics. Sure, you might be able to find someone to hack a solution for this problem, but you'll definitely run into others. I always try to tell users wanting to use Subscript[a, i] as variables that it's generally more trouble than it's worth... sadly, many are drawn in by the looks of having a Mathematica notebook appear like a page from a textbook. – rm -rf Oct 15 '13 at 17:28

There is a system option that controls whether Mathematica will differentiates heads:

SystemOptions["DifferentiationOptions"->"DifferentiateHeads"]


If you set this to False, then Mathematica will not try to differentiate heads:

InternalWithLocalSettings[
D[a[b][c], a[b]],
]


0

This is an answer to question 1, a description of how the derivative operator D works. It explains the behavior observed by the OP. However, there seem to be others cases in which I feel the result is faulty.

The first thing to point out is that heads are treated as if they represent functions. Strictly speaking, this is not the whole story, which will be seen in one of the examples below. However for the purposes of understanding how Mathematica differentiates, it is a good initial hypothesis. That means a[b][c] will be interpreted as if a[b] is to be evaluated at c; in this situation a[b] represents a function. If it is to represent a variable, too, then there's a conflict. I think ssch recommend a good way around it, but I'll leave question 2 to others for the time being.

The derivative of f[x1, x2, ..., xn] with respect to a variable t is normally a sum of a partial derivatives of f times the derivative of the corresponding xs with respect to t. But in Mathematica, the head f can be an expression and might depend on t. In such a case, we have a function $F(t, x_1, ..., x_n) = f_t(x_1,...,x_n)$ and the derivative with respect to $t$ will include the partial derivative of $F$ with respect to the coordinate $t$: $${\partial \over \partial t} F(t, x_1, ..., x_n) = {\partial F\over \partial t} + {\partial F\over \partial x_1}{\partial x_1 \over \partial t} + \cdots + {\partial F\over \partial x_n}{\partial x_n \over \partial t}$$ The term ${\partial F / \partial t}$ is the derivative of the head $f_t$. In Mathematica, this translates to the derivative D[f, t] (see illustrations below). The case when f is a Symbol seems to be a special case. It is as if Mathematica assumes such a head cannot depend on a variable, so treats it as if its partial derivative is zero, even when the variable is f as in D[f[x], f].

### Illustrations

A. The expression (# + t^2) &[Sin[t]] evaluates immediately to Sin[t] + t^2, so the result below is not surprising.

D[(# + t^2) &[Sin[t]], t]
(* 2 t + Cos[t] *)


We can get the same thing if we have an undefined function as the head which we later replace after applying D:

df = D[f[t][Sin[t]], t]
(* Cos[t] Derivative[1][f[t]][Sin[t]] + Derivative[1][f][t][Sin[t]] *)

df /. f -> (t \[Function] (# + t^2 &))
(* 2 t + Cos[t] *)


B. An example showing a partial derivative of the head

D[f[t, s][t^2], t]
(* 2 t Derivative[1][f[t, s]][t^2] + Derivative[1, 0][f][t, s][t^2] *)


C. Here is another example. Both seem to be equivalent expression. Plotting them produces the same graph. But the derivative of the first one won't plot. The first term does not evaluate to a real number, because the values of the If statement are just numbers, not functions. The second works, because the If evaluates to a function. It seems faulty to me that the first one does not work. I suppose it is because Sin and Cos are not functions, but merely symbols, albeit with transformation rules attached to them. Still, it seems wrong that the derivative of an expression representing a real-valued, almost-everywhere differentiable function does not itself represent a real-valued function.

D[If[t > 0, Sin, Cos][t^2], t]
D[If[t > 0, Sin[#] &, Cos[#] &][t^2], t]

(* If[t > 0, 0, 0][t^2] + 2 t Derivative[1][If[t > 0, Sin, Cos]][t^2] *)
(* If[t > 0, 0 &, 0 &][t^2] + 2 t Derivative[1][If[t > 0, Sin[#1] &, Cos[#1] &]][t^2] *)

GraphicsRow[{
Plot[Evaluate[{#, D[#, t]} &@ If[t > 0, Sin, Cos][t^2]],
{t, -5, 5}, PlotRange -> 5],
Plot[Evaluate[{#, D[#, t]} &@ If[t > 0, Sin[#] &, Cos[#] &][t^2]],
{t, -5, 5}, PlotRange -> 5]
}]


D. As I mentioned, for some reason, D[f[t], f] gives 0 when f is a Symbol. Here's something interesting. If we hold the head f, then the head is not a Symbol. Releasing the hold, we get a result similar to the OP D[a[b][c],a[b]]:

D[Hold[f][t], f]
(* D[Hold[f], f][t] *)

D[Hold[f][t], f] // ReleaseHold
(* 1[t] *)


### D[a[b][c], a[b]]

In the original answer, I neglected to directly address how Mathematica deals with the OP's particular example D[a[b][c], a[b]]. To my mind, it is similar to the first example under illustration C. The head a[b] is not a Symbol and so Mathematica treats it under the general rule for $F(t, x_1, \dots, x_n)$ given above. The symbol correspondence is as follows:

\begin{align} t &\leftrightarrow {\tt a[b]} \\ x_1 & \leftrightarrow {\tt c} \\ f_t & \leftrightarrow {\tt f[a[b]] = a[b]} \\ F & \leftrightarrow {\tt F[a[b], c] = a[b][c]} \end{align}

Note that in my mind, f[t] in foregoing discussion might represent any expression in t such as t^2 etc. In this case, it could be considered the identity function at a[b]. The derivative follows the formula $\partial_t F(t, x(t)) = (\partial_t F) + (\partial_x F)(\partial_t x)$. Applied to a[b][c] the formula translates to the following code:

(D[a[b], a[b]])[c] + D[a[b][c], c] D[c, a[b]]
(* 1[c] *)


which yields the same result as D[a[b][c], a[b]]. The problem with the result arises because the derivative of the head a[b] does not yield an expression that can be treated as a function. The is the same problem with the first example in C, in which the derivative of Sin and Cos were the number 0 instead of the function 0 &.

The same formula applied to D[f[x], f] would be

D[f, f][x] + D[f[x], x] D[x, f]
(* 1[x] *)


which does not agree with D[f[x], f] as the OP has noted.

I would assert that 1[x] is the more correct answer. I say "more" only because I have yet to imagine a case which D[f[x], f] makes mathematical sense. As I understand the context of the OP's question, a[b][c] and a[b] are to be treated as independent variables. But I don't think one can get around the fact that the basic syntax of Mathematica forces the head a[b] of the first to be treated as the exact same expression as the second. For instance, if a[b] is temporarily set to a numerical value, then the head of a[b][c] becomes a number:

Block[{a},
a[b] = 2;
a[b][c]
]
(* 2[c] *)


I'm afraid I'm belaboring this point, so I'll leave it there. The main point is that with the description I've given of how D operates on expressions such as a[b][c], the result 1[c] is sensible.

### Summary

If one can have heads that depend on variables, then Mathematica will need to differentiate them, as it does, according to the rules of calculus. The remaining question is why are heads that are symbols differentiated differently. Why doesn't D[f[t], f] evaluate to 1[t]? I do not have a definitive (or authoritative) answer to that question.

• Thank you for your answer. I hope to not seem ungrateful, but I have to point out some issues. From the third paragraph on through Illustrations A trough E, you take the derivative w.r.t. an argument of f. Certainly, I completely agree on the results there, but this is not the issue in question. The question in Illustration A would have been: does the result of D[f[t][Sin[t]], f[t]] make sense? I don’t think it does. – LCC Oct 17 '13 at 9:29
• But in D, I think you have found an illustration of the step in which the Derivative errs. I argue that between (a)D[Hold[f][t], f] and (b)D[Hold[f], f][t] something goes wrong: in (a) f is basically any expression which “admits” a DownValue or SubValue (given that we apply [t] to the result), so it might be a function or a symbol or similar. It is not, however, simply a variable f, which is multiplied with [c], since this is syntactically illegal. However, in (b), the fact that f has to be applied to [t] is lost: when the Hold is released, f has become a variable. – LCC Oct 17 '13 at 9:52
• It boils down to that: f[t] in the first argument of D[f[t][Sin[t]], f[t]] is a function, f[t] in the second argument is a variable, and they just happen to carry the same name. In my view, the fact that the boundary between functions and variables is muddy in Mathematica is both a weakness and a strength. – LCC Oct 17 '13 at 10:33
• @LCC You don't seem ungrateful. I don't think seeking to understand can in itself seem ungrateful. Quite the contrary. I hope you understand that the time I have to give to these answers is limited. I overlooked including your example because I was tired and pressed. I think one issue is that Mathematica's syntax doesn't work the way you'd like it to. I don't think that can be fixed for you, but I do think the output can be formatted however you please. – Michael E2 Oct 17 '13 at 13:06
• Yes, thank you, I was able to fix the formatting issue, as ssch suggested. Regarding your intuition that D[f[x],f] should be 1[x]`, I would like to invite you (and everyone who is still following the discussion) to forget for 5 minutes Mathematica and reason about, say, the cosine function $\cos(x)$. How does $\cos(x)$ change if I vary the variable "cos"? Please note that $\cos(x)$ is not a function of the variable "cos" but only of "x". Now, according to your intuition, the rate of change of $\cos(x)$ when varying the variable "cos" is $1(x)$. What functional form can $1(x)$ have? – LCC Oct 17 '13 at 16:02