# Iterate symbolic expression

I'd like to symbolically iterate this formula $$1,2,...,n$$ times: $$f(z,u)=\frac{z}{1-z}f(z,1)+\frac{zu}{1-zu}f(z,zu)+\frac{z^2u}{1-z^2u}f(z,z^2u).$$

I tried using Nest and RecurrenceTable, but I don't know how to create the initial conditions. Any idea?

Edit: By iterate I mean when you use $$f(z,u)$$ definition to expand it. For example, for the first iteration I would like to get:

$$f(z,u)=\left(\frac{u z^2}{(1-z) (1-u z)}+\frac{z^3}{(1-z) \left(1-u z^2\right)}+\frac{z^2}{(1-z)^2}\right) f(z,1)+\frac{z^2}{(1-z)^2}f(z,z)+\frac{z^3}{(1-z) \left(1-z^2\right)} f\left(z,z^2\right)+\frac{u^2 z^3}{(1-u z) \left(1-u z^2\right)} f\left(z, z^2u\right)+\left(\frac{u z^3}{(1-u z) \left(1-u z^3\right)}+\frac{u z^5}{\left(1-u z^2\right) \left(1-u z^3\right)}\right) f\left(z, z^3u\right)+\frac{z^4}{\left(1-u z^2\right) \left(1-u z^4\right)} f\left(z, z^4u\right)$$

As Wolfram code:

((z^2) /(1-z)^2+(u (z^2) )/((1-z) (1-u z))+(z^3) /((1-z) (1-u z^2)))f[z,1]+z^2/(1-z)^2 f[z,z]+(z^3) /((1-z) (1-z^2)) f[z,z^2]+((u^2) (z^3) )/((1-u z) (1-u z^2)) f[z,z^2 u]+((u (z^3) )/((1-u z) (1-u z^3))+(u (z^5) )/((1-u z^2) (1-u z^3)))f[z,z^3 u ]+(z^4) /((1-u z^2) (1-u z^4)) f[z,z^4 u]

• @MichaelE2 see for my edit. Commented May 24, 2023 at 5:33
• Maybe a scheme like f[0] = f; f[i_Integer?Positive][z_, u_] := (* formula with f[i-1][..] in place of f[..] *); with an index i for how many times still to be nested? Commented May 24, 2023 at 5:39
• @MichaelE2 Got it! Thank you very much Commented May 24, 2023 at 5:56

Method from comment:

f[0] = f;
f[i_Integer?Positive][z_,
u_] := (z/(1 - z) f[i - 1][z, 1] + z u/(1 - z u) f[i - 1][z, z u] +
z^2 u/(1 - z^2 u) f[i - 1][z, z^2 u]);

expr1 = f[3][z, u];


Another way:

ClearAll[f];
frule = f[z_, u_] :>
(z/(1 - z) f[z, 1] + z u/(1 - z u) f[z, z u] +
z^2 u/(1 - z^2 u) f[z, z^2 u]);

expr2 = Quiet[
ReplaceRepeated[f[z, u], frule, MaxIterations -> 3],
ReplaceRepeated::rrlim];


Compare:

expr1 == expr2

(*  True  *)


The problem here is infinite recursion. To break this we may add an additional counter. f[z,u,1] will stop the recursion :

f[z_, u_, 1] := (z/(1 - z) f[z, 1] + z u/(1 - z u) f[z, z u] +
z^2 u/(1 - z^2 u) f[z, z^2 u])
f[z_, u_,
c_] := (z/(1 - z) f[z, 1, c - 1] + z u/(1 - z u) f[z, z u, c - 1] +
z^2 u/(1 - z^2 u) f[z, z^2 u, c - 1])


With this we may now e.g. write:

f[z, u, 2] // Simplify


Or the different coefficients of f[,]:

Coefficient[f[z, u, 2], #] & /@ {f[z, 1], f[z, z], f[z, z^2 u],
f[z, z^3 u], f[z, z^4 u]}


Note that you have an error in the first coefficient, instead of z^2 it should read u z^2.