# Expand function in terms of complicated auxiliary variable?

I have the following two-variable function involving a large product:

Z[p_, q_] := (p^(-1)*(1 - p)^2)*
Product[((1 - p*(q)^m)^2*(1 - p^(-1)*q^m)^2)/((1 - q^m)^4), {m, 1, 200}];


and I'm hoping to take the following expansion in the variable q where the coefficients are functions of the variable p:

Series[Z[p, q], {q, 0, 5}]


However, I actually would like the coefficients of this expansion to be not functions of p, but rather of the auxiliary variable

z = p^(-1)*(1-p)^2


Is there a nice, clean way to carry this out? I think I would know what to do if I could solve for p in terms of z, but that clearly won't work here. I should also mention that I'm only thinking of this as a formal object I'm expanding, so I definitely don't want to worry about whether or not things are honest "functions", branch cuts, or anything like that!

• Can't you do a /. replace? – Feyre Nov 20 '16 at 18:51
• @Feyre That won't do 'cause /. seeks for exact patterns like in a FullForm. So FullSimplify @ Series[Z[p, q], {q, 0, 1}] /. p^(-1)*(1 - p)^2 -> z does nothing. – corey979 Nov 20 '16 at 19:40

If I got your question right, I see no big problem here. Solve p as a function of z and substitute.

    In[6]:= sol = Solve[z == p^(-1)*(1 - p)^2, p]

Out[6]= {{p -> 1/2 (2 + z - Sqrt[z] Sqrt[4 + z])}, {p ->
1/2 (2 + z + Sqrt[z] Sqrt[4 + z])}}

In[30]:= ser1[q_, z_] =
Series[Z[p /. First@sol, q], {q, 0, 5}] // Normal // FullSimplify

Out[30]= z - 2 q (1 + 2 q) (1 + q + 2 q^2 + 3 q^3) z^2 +
q^2 (1 + q (8 + q (23 + 56 q))) z^3 - 2 q^4 (1 + 6 q) z^4

In[31]:= ser2[q_, z_] =
Series[Z[p /. Last@sol, q], {q, 0, 5}] // Normal // FullSimplify

Out[31]= z - 2 q (1 + 2 q) (1 + q + 2 q^2 + 3 q^3) z^2 +
q^2 (1 + q (8 + q (23 + 56 q))) z^3 - 2 q^4 (1 + 6 q) z^4


Solution ser1 and ser2 are equal.

   In[32]:= ser1[q, z] - ser2[q, z] // Simplify

Out[32]= 0

In[33]:= ser1[0, z] // Simplify

Out[33]= z

In[34]:= Plot3D[ser1[q, z], {q, -2, 2}, {z, -3, 3}, PlotRange -> All]


Try the following. Here is your expressions:

   Z[p_, q_] := (p^(-1)*(1 - p)^2)*
Product[((1 - p*(q)^m)^2*(1 - p^(-1)*q^m)^2)/((1 - q^m)^4), {m, 1,
200}];

expr = Series[Z[p, q], {q, 0, 5}] // Normal;


This expresses pin terms of z:

sl = Solve[z == p^(-1)*(1 - p)^2, p][[1, 1]]

(*  p -> 1/2 (2 + z - Sqrt[z] Sqrt[4 + z])  *)


and this substitutes:

  expr1=expr /. sl // Simplify

(*   (1/((2 + z -
Sqrt[z] Sqrt[4 + z])^4))2 (z - Sqrt[z] Sqrt[4 + z])^2 (-2 - 9 z -
6 z^2 - z^3 + 3 Sqrt[z] Sqrt[4 + z] + 4 z^(3/2) Sqrt[4 + z] +
z^(5/2) Sqrt[4 + z]) (-1 + 2 q z - q^2 (-6 + z) z -
8 q^3 (-1 + z) z + q^4 z (14 - 23 z + 2 z^2) +
4 q^5 z (3 - 14 z + 3 z^2))  *)


Here is the coefficient of the expansion for, say, q^5:

 Coefficient[expr1, q^5]

(* (8 z (3 - 14 z + 3 z^2) (z - Sqrt[z] Sqrt[4 + z])^2 (-2 - 9 z -
6 z^2 - z^3 + 3 Sqrt[z] Sqrt[4 + z] + 4 z^(3/2) Sqrt[4 + z] +
z^(5/2) Sqrt[4 + z]))/(2 + z - Sqrt[z] Sqrt[4 + z])^4    *)


Have fun!