# Replacing powers of $n$-th partial sums in the output of a function with $S[i]$ for $i = 1,\ldots,n$

I've written some simple code that, for various $N \geq 1$ and $k = 1,\ldots,N+1$ computes the $n$-th derivative of $\log S_N$ where $S_N(z) = \sum_{k=0}^N \frac{1}{k!}z^k$ as follows:

f[z_, N_] := Sum[z^k/k!, {k, 0, N}]
g[z_, N_] := Log[f[z, N]]
t = Table[Evaluate[D[g[z, 7], {z, n}]], {n, 8}]


Here I can vary $7$ and $8$ to be any $N$ and $N+1$ (resp.). For a shorter example, the output if $7$ and $8$ are replaced by $3$ and $4$ is

$$\left\{\frac{\frac{z^2}{2}+z+1}{\frac{z^3}{6}+\frac{z^2}{2}+z+1},\frac{z+1}{\frac{z^3}{6}+\frac{z^2}{2}+z+1}-\frac{\left(\frac{z^2}{2}+z+1\right)^2}{\left(\frac{z^3}{6}+\frac{z^2}{2}+z+1\right)^2},\frac{2 \left(\frac{z^2}{2}+z+1\right)^3}{\left(\frac{z^3}{6}+\frac{z^2}{2}+z+1\right)^3}-\frac{3 (z+1) \left(\frac{z^2}{2}+z+1\right)}{\left(\frac{z^3}{6}+\frac{z^2}{2}+z+1\right)^2}+\frac{1}{\frac{z^3}{6}+\frac{z^2}{2}+z+1},-\frac{6 \left(\frac{z^2}{2}+z+1\right)^4}{\left(\frac{z^3}{6}+\frac{z^2}{2}+z+1\right)^4}+\frac{12 (z+1) \left(\frac{z^2}{2}+z+1\right)^2}{\left(\frac{z^3}{6}+\frac{z^2}{2}+z+1\right)^3}-\frac{4 \left(\frac{z^2}{2}+z+1\right)}{\left(\frac{z^3}{6}+\frac{z^2}{2}+z+1\right)^2}-\frac{3 (z+1)^2}{\left(\frac{z^3}{6}+\frac{z^2}{2}+z+1\right)^2}\right\}$$

Since $S_m'(z) = S_{m-1}(z)$ for all $m \geq 1$, these expressions will always $\mathbb{Z}$-linear combinations of quotients of powers of $S_m$ for varying $m$, and I would like to find a way to tell Mathematica to replace each occurrence of $S_m(z)$ with the symbol f[z,m]. Is this possible? I've seen other questions on simple substitutions using /. but none where the replacement scheme seemed easily adapted to this problem. In particular, the replacement scheme would need to be greedy to avoid, e.g., replacing instances of $S_m(z)$ with \$f[z,m-2] + (1/(m-1)!z^{m-1}) + (1/m!z^m).

Is there a way to make this happen?

Edit: This post seems quite relevant. (I had missed it when searching.) I will see if it can be applied here.

• Try something like t /. Table[f[z, y] -> kk[z, y], {y, Range[3, 0, -1]}] for your {3,4} example (kk represents f, to use f directly, you'll need to fiddle with HoldForm et al.) – ciao Apr 3 '14 at 0:13
• Might want to have a look at PolynomialReduce. It's designed for algebraic replacement. One caveat is that you might have to separately consider numerators and denominators. – Daniel Lichtblau Jul 2 '14 at 23:57

I would try this, step by step:

t = Table[Evaluate[D[Log@h[z, 3], {z, w}]], {w, 4}]  (wait with defining f)
t /. Derivative[d_,0][h][z,k_]:> x[z,d,k]/. h[z_,k_]:> x[z,0,k]


(using FullForm of the derivative). Now apply (repeatedly)

% //. x[z,d_,k_] :> x[z, d-1, k-1] /; d >= 1 && k >= 1


and, as a last step, restore the 'two-argument' form, using y as placeholder for your (unevaluated) f :

% /. x[z, 0, v_] :> y[z, v] /. (x[z, u_, 0] /; u > 0) -> 0


Is this what you were looking for? If you prefer modifying the output of your original z-polynomial form, then use:

t = Table[Evaluate[D[Log@f[z, 3], {z, w}]], {w, 4}] (*followed by  *)
t /. Plus[1,x__]:>If[PolynomialQ[Plus[1,x],z],w[Exponent[Plus[1,x],z]],Plus[1,x]]