## Algebraically difficult integral

Well, some integrals are not all that hard to evaluate in principle. The one I am posting here should be an open and shut application of the residue theorem, using the unit circle as a contour. The form of the integrand, however, should give a little pause. It turns out that actually computing residues on this one is a slog, but the reward is great as the final expression is a wonder to behold.

$$\int^{2 \pi}_0 d \theta \dfrac{\cos^2 \theta}{|2e^{i\theta}-z|^2} \, \qquad \mbox {when} \, |z| \neq 2.$$

Write $\zeta=e^{i \theta}$ as usual to get, for the integral,

$$-i \frac14 \oint_{|\zeta|=1} \frac{d\zeta}{\zeta} \frac{(\zeta+\zeta^{-1})^2}{|2 \zeta-z|^2} = -i\frac14 \oint_{|\zeta|=1} \frac{d\zeta}{\zeta^2} \frac{(\zeta^2+1)^2}{(2 \zeta-z)(2 -\bar{z} \zeta)}$$

Clearly we have poles at $\zeta=0$, $\zeta=z/2$, and $\zeta=2/\bar{z}$, with the pole at $\zeta=0$ being a double pole, the others simple. The residue of the pole at $\zeta=0$ is

$$\frac{i}{4} \frac{|z|^2+4}{4 z^2}$$

Now, note that we have two cases, corresponding to whether $|z|$ is larger or smaller than $2$. In the former case, we use the pole at $\zeta=2/\bar{z}$ as that is inside $|\zeta|=1$. The residue there is

$$-\frac{i}{4} \frac1{4 \bar{z}^2} \frac{(4+\bar{z}^2)^2}{|z|^2-4}$$

By the residue theorem, the integral for $|z|\gt 2$ is $i 2 \pi$ times the sum of these residues. After some algebra, I get that

$$\int_0^{2 \pi} d\theta \frac{\cos^2{\theta}}{\left | 2 e^{i \theta}-z\right|^2} = \frac{\pi}{|z|^2} \frac{|z|^2+4 \cos{\left (2 \operatorname*{Arg}{z}\right )}}{|z|^2-4} \quad (|z| \gt 2)$$

When $|z| \lt 2$. on the other hand, we use the pole at $\zeta=z/2$ instead. Using similar manipulations, I get that

$$\int_0^{2 \pi} d\theta \frac{\cos^2{\theta}}{\left | 2 e^{i \theta}-z\right|^2} = \frac{\pi}{4} \frac{4+|z|^2 \cos{\left (2 \operatorname*{Arg}{z}\right )}}{4-|z|^2} \quad (|z| \lt 2)$$

It should be noted that the integral may in fact be defined when $z=\pm 2 i$, as there is a removeable singularity in the integrand at $\theta=\pi/2$ or $3 \pi/2$, respectively.
I probably should illustrate the algebra behind the answer, as it is not trivial. I will illustrate the case $|z|\gt 2$. By the residue theorem, the result is
\require{cancel} \begin{align}\int_0^{2 \pi} d\theta \frac{\cos^2{\theta}}{\left | 2 e^{i \theta}-z\right|^2} &= \frac{\pi}{2} \left [\frac1{4 \bar{z}^2} \frac{(4+\bar{z}^2)^2}{|z|^2-4} – \frac{|z|^2+4}{4 z^2} \right ]\\ &= \frac{\pi}{8} \frac{z^2 (4+\bar{z}^2)^2 – \bar{z}^2 (|z|^4-16)}{z^2 \bar{z}^2 (|z|^2-4)}\\ &= \frac{\pi}{8} \frac{16 z^2+ 8 |z|^4 + \color{red}{\cancelto{0}{\color{\gray}{\bar{z}^2 |z|^4}}} – \color{red}{\cancelto{0}{\color{\gray}{\bar{z}^2 |z|^4}}} + 16 \bar{z}^2}{|z|^4 (|z|^2-4)}\\ &= \pi \frac{|z|^4 +2 (z^2+\bar{z}^2) }{|z|^4 (|z|^2-4)}\\ &= \frac{\pi}{|z|^2} \frac{|z|^2 + 4 \frac{(\Re{z})^2-(\Im{z})^2}{(\Re{z})^2+(\Im{z})^2}}{|z|^2-4}\end{align}
The stated result follows. The reader should do the case $|z|\lt 2$ him/herself.