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 x
s 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.
a[b,c]
instead $\endgroup$ – ssch Oct 15 '13 at 12:55D[f[x],f]
, so I wouldn't be bothered by the inconsistency. But it is curious that something different happens. $\endgroup$ – Michael E2 Oct 15 '13 at 14:05a[{b,t},c]
ora[b,t,c]
and format them as $a_{b,t}^c$. $\endgroup$ – Michael E2 Oct 15 '13 at 15:13Subscript[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. $\endgroup$ – rm -rf♦ Oct 15 '13 at 17:28