Why doesn't Mathematica evaluate the series with recursion relationship as expected?

I define a series $$dg(i)$$ as the $$i$$th derivative of a function $$g[t]$$ for $$i>0$$ and known the first term $$dg(1)=(t-x)g(t)$$. In mathematica, the code is:

dg[t_, i_] := D[dg[t, i - 1], t](*when i>1*);
dg[t_, 1] := (t - x) g[t];


Then I use

Table[{ii, dg[t, ii]}, {ii, 1, 5}] // TableForm //TeXForm


$$\begin{array}{cl} 1 & g(t) (t-x) \\ 2 & (t-x) g'(t)+g(t) \\ 3 & (t-x) g''(t)+2 g'(t) \\ 4 & g^{(3)}(t) (t-x)+3 g''(t) \\ 5 & g^{(4)}(t) (t-x)+4 g^{(3)}(t) \\ \end{array}$$

The question is why $$g'(t)$$ in the second term $$dg(2)$$ is not replaced by $$(t-x) g(t)$$? I need all the terms of the series is expressed only by $$g(t)$$ instead of $$g'(t)$$.

Could you help me? Thank you.

• There is nothing in those definitions to connect g'[t] to dg[1]. – Daniel Lichtblau May 9 at 12:12
• @DanielLichtblau I have modified the question to make it understandable. Thank you. – tanghe2014 May 9 at 14:29
• if dg(t) = (t-x)g(t) why would you use D[]? Just remove that – mikuszefski May 15 at 7:25

Clear[g]
g /: g' = (# - x) g[#] &;
dg[t_, i_] := D[g[t], {t, i}]
Table[dg[t, i] // Simplify, {i, 1, 5}] // TableForm


These polynomials are essentially

Table[y^Floor[n/2] Sum[y^-i n!/(i! 2^i (n - 2 i)!), {i, 0, n/2}], {n, 0, 10}]


with y=t-x, up to a global factor of g[t].

• Thank you. I am not familiar with TagSet[] and &. They are very powerful. I have tried your code. It is laconical and powerful! How about to get the explicit expression of $g^{(n)}(t)$ as the $n$th derivative of $g(t)$? – tanghe2014 May 10 at 0:53
• @tanghe2014 there you go. – AccidentalFourierTransform May 10 at 2:49

It is possible to instead directly give definitions for the derivatives of g:

g' = (# - x) g[#]&;
Derivative[n_?Positive][g] := Derivative[n-1][g']


Then:

FullSimplify @ Column @ Table[Derivative[n][g][t], {n, 0, 5}] //TeXForm


$$\begin{array}{l} g(t) \\ g(t) (t-x) \\ g(t) \left((t-x)^2+1\right) \\ g(t) \left((t-x)^2+3\right) (t-x) \\ g(t) \left((t-x)^4+6 (t-x)^2+3\right) \\ g(t) \left((t-x)^4+10 (t-x)^2+15\right) (t-x) \\ \end{array}$$

in agreement with Accidental's answer. With this approach, you can obtain the series expansions of g just using Series:

Series[g[t], {t, 0, 4}] //TeXForm


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

or

Series[g[t], {t, x, 4}] //TeXForm


$$g(x)+\frac{1}{2} g(x) (t-x)^2+\frac{1}{8} g(x) (t-x)^4+O\left((t-x)^5\right)$$

• Thank you. g' = (# - x) g[#]& is new for me and it is very helpful. Could you give more advice to get the general term of $g^{(n)}(t)$ as the $n$th derivative of $g(t)$? – tanghe2014 May 10 at 0:46

A simple method uses a Rule[] to replace g'[t] with its value:

ClearAll[x, g, dg];
dg[t_, 0] := g[t];
dg[t_, i_Integer /; i > 0] := D[dg[t, i - 1], t] /.
g'[t] -> (t - x) g[t];


As a simple test try

dg[t, 5] // Simplify // InputForm


which returns the result

(15 + 10*(t - x)^2 + (t - x)^4)*(t - x)*g[t]


as it should.

Notice that in this method the derivative of g is completely contained in the dg[] rule. If you try g'[t] it get returned unevaluated. An alternative method is to attach the definition of g' directly to g itself. The code for this using a pure function is

g /: g' = Function[t, (t - x) g[t]];


Now we can use this definition directly so that

D[g[t], {t, n}] == dg[t, n]


evaluates to True for all $$n \ge 0$$.

The $$n\,$$th derivative of $$g(t)$$ is $$g(t)$$ multiplied by an $$n\,$$th degree polynomial in $$(t-x)$$ known as the modified Hermite polynomials -- the coefficients of which are given by the OEIS sequence A099174. Thus, the following code

Expand[ dg[t, n] == g[t] HermiteH[n, I(t-x)/Sqrt[2]]
(-I Sqrt[1/2])^n]


evaluates to True for all $$n\ge 0.$$ Note that using

DSolve[ g'[t] == (t - x)g[t] && g[0] == 1, g[t], t]


give the solution $$\ g(t) = e^{t^2/2 - tx}\$$ which is the exponential generating function for modified Hermite polynomials.

• Thank you. I get your point now. Rule[] is very helpful. Is it possible to get the general expression of $dg(t,n)$ using Mathematica? – tanghe2014 May 10 at 0:41
• @tanghe2014 If you mean by "using Mathematica" I have appended a general expression at the end of my answer. – Somos May 10 at 3:57