# Strange failure of Series and Derivative

I just spend three hours and posted two Questions trying to figure something out, and it turned out all the confusion was caused by this mysterious quirk. I want to expand g[x,v] in v at v=0, using this relation to convert the derivative wrt v into derivatives wrt x:

$$\frac{dg[x,v]}{dv}=\frac{1}{2}\frac{d^{2}g}{dx^{2}}+\frac{1}{2}\left(\frac{dg}{dx}\right)^{2}$$

When I use the following Derivative definition, Series does not perform the conversion and leaves the result in terms of derivative wrt $v$:

Derivative[q_, Except[0, n_]][g][x, v] :=  D[(D[g[x, v], {x, 2}] + D[g[x, v], x]^2), {x, q}, {v, n - 1}]/2;
Series[g[x, v], {v, 0, 4}]


$g(x,0)+v g^{(0,1)}(x,0)+\frac{1}{2} v^2 g^{(0,2)}(x,0)+\frac{1}{6} v^3 g^{(0,3)}(x,0)+\frac{1}{24} v^4 g^{(0,4)}(x,0)+O\left(v^5\right)$

But if I perform the binomial expansion of the derivative of $g'^2$ by hand and use the following Derivative definition instead, Series works perfectly:

Derivative[m_, Except[0, n_]][t][x_, v_] := ( Derivative[2 + m, n - 1][t][x, v] + Sum[Binomial[n - 1, k] Binomial[m, j]*Derivative[m - j + 1, n - 1 - k][t][x, v]*Derivative[j + 1, k][t][x, v], {k, 0, n - 1}, {j, 0, m}])/2;
Series[t[x, v], {v, 0, 2}]


$t(x,0)+\frac{1}{2}v\left(t^{(1,0)}(x,0)^2+t^{(2,0)}(x,0)\right)+\frac{1}{8} v^2 \left(4 t^{(2,0)}(x,0)t^{(1,0)}(x,0)^2+4t^{(3,0)}(x,0) t^{(1,0)}(x,0)+2t^{(2,0)}(x,0)^2+t^{(4,0)}(x,0)\right)+O\left(v^3\right)$

What gives? What design quirk is making MMa not able to calculate and substitute that derivative? I would like to NOT spend this much time fixing this problem again! :-(

I'll post a possible explanation later, but here's how to make it work: replace v with v_, change the point about which you are expanding by a variable, and then replacing that variable with 0 later:

Derivative[q_, Except[0, n_]][g][x, v_] :=  D[(D[g[x, v], {x, 2}] + D[g[x, v], x]^2), {x, q}, {v, n - 1}]/2;
Series[g[x, v], {v, a, 2}] /. a -> 0


This makes it so that Series will evaluate derivatives about a symbolic point a, and then the rule that you've created will evaluate with a in place of v (which is why I replaced v with the pattern v_). It seems like the problem with Series in the original case is that there's no intermediate evaluation where g[x, v] and derivatives appears: it jumps immediately to g[x, 0] and derivatives. Consider:

Trace[Series[g[x, v], {v, 0, 2}]] • Hey, thanks. I kinda suspected it was something like that, but I couldn't figure out a way to hack around such a problem even to test the theory. This seems like a genuine MMa design "quirk" if we're not ready to use such strong words as "bug". – Jerry Guern Oct 13 '15 at 4:40
• @JerryGuern. Could be, but if you actually peek under the hood at how the "data structure" of Series is defined, things make a little more sense (try to FullForm the result of a Series and you'll see what I mean). In any case, the order in which and the method by which things are evaluated will be at times "quirky" in whatever language you use. In this case, I imagine Series does some pattern matching first, then just puts the matched quantities directly into the "data structure". – march Oct 13 '15 at 4:43
• Yes, that's a fair point. And thanks for the intro to FullForm, which does shed some light on the problem. I guess what I'm really looking for is insight into those quirks so that I can craft work-arounds for them without wasting hours. Even the fellow who gave me the binomial expansion solution that worked didn't know why it worked when my approach didn't. That was frustrating, to put it mildly. – Jerry Guern Oct 13 '15 at 5:16

The issue here is the way Derivative works, as it (effectively) represents the derivative of a pure function with regards to its parameters, based purely on position. In order to make this work, you can return a pure function, using Evaluate to pre-evaluate the derivative, like so:

Derivative[q_, Except[0, n_]][g] := Function[{x, v},
Evaluate[
D[(D[g[x, v], {x, 2}] + D[g[x, v], x]^2), {x, q}, {v, n - 1}]/2]];


Now Series[g[x, v], {v, 0, 2}] yields $$g(x,0)+\frac{1}{2} v \left(g^{(1,0)}(x,0)^2+g^{(2,0)}(x,0)\right)+\frac{1}{8} v^2 \left(4 g^{(2,0)}(x,0) g^{(1,0)}(x,0)^2+4 g^{(3,0)}(x,0) g^{(1,0)}(x,0)+2 g^{(2,0)}(x,0)^2+g^{(4,0)}(x,0)\right)+O\left(v^3\right)$$