Find the contour integral around unit circle. Evaluate the below integral by turning it into a contour integral around a unit circle:
$$\int_{0}^{\pi}\frac{\cos2\phi}{1-2a \cos\phi + a^2} d\phi$$ $where\;a\neq \pm1$
 A: Let $\gamma(a) = -\frac{1+a^2}{2a}$. Since the integral is even, we can write it as
$$
\int_0^{\pi}\frac{\cos(2\phi)}{1-2a\cos(\phi)+a^2}d\phi=\frac{-1}{4a}\int_0^{2\pi}\frac{\cos(2\phi)}{\cos(\phi)+\gamma}d\phi
$$
Recall that $\cos(\phi) = \frac{e^{i\phi}+e^{-i\phi}}{2}$. Then let $z = e^{i\phi}$ and $\cos(2\phi)=\Re\{e^{2i\phi}\}$ so $dz=ie^{i\phi}d\phi$ or $d\phi = \frac{dz}{iz}$. Therefore, we can write the integral now as
$$
\frac{-1}{2ai}\oint_C\frac{z^2}{z^2+2\gamma z+1}dz
$$
The poles occur when $z=-\gamma\pm\sqrt{\gamma^2-1}$. Plugging in $\gamma(a) = -\frac{1+a^2}{2a}$, we get that the poles are at $z=a,1/a$. By the Residue theorem, 
\begin{align}
\int_0^{\pi}\frac{\cos(2\phi)}{1-2a\cos(\phi)+a^2}d\phi &= 
\begin{cases}
\frac{-\pi}{a}\lim_{z\to a}(z-a)\frac{z^2}{z^2+2\gamma z+1},&\lvert a\rvert < 1\\
\frac{-\pi}{a}\lim_{z\to 1/a}(z-1/a)\frac{z^2}{z^2+2\gamma z+1},&\lvert a\rvert > 1
\end{cases}\\
&=
\begin{cases}
\Re\Bigl\{\frac{a^2\pi}{a^2-1}\Bigr\},&\lvert a\rvert < 1\\
\Re\Bigl\{\frac{\pi}{a^2(a^2-1)}\Bigr\},&\lvert a\rvert > 1
\end{cases}
\end{align}
A: First observe that by the cosines law (using the isomorphism between $\mathbb{R}^2$ and $\mathbb{C}$):
$$
\|1-ae^{ix}\|^2 = \|1\|^2 + \|a\|^2 -2\|1\|\cdot \|a\|\cos x =  1 + a^2 -2a\cos x.
$$
So the denominator is measuring the squared distance between the elements of the circle of radius $a$ centered at the origin and $1$.
On the other hand, the image of $2[0,\pi]$ is $2\pi$, so we only need to reparametrize the numerator.
So, parametrizing the unit circle as $\gamma(x)=e^{ix}$, we get that:
$$
I 
= \int_0^{2\pi} \frac{\cos x}{\|1-ae^{i\frac{x}{2}}\|^2}\left(-ie^{-ix}\right)\left(ie^{ix}\right) dx 
= -i\int_{S^1} \frac{\Re(z)}{z\|1-a\sqrt{z}\|^2} dz.
$$
