<s>[INST] Calculating real integrals using complex functions and residue theory. $\int_0^{\pi}\frac{\cos^2x}{1-a\sin^2x}dx$, where $0<a<1$. 
My professor thought us some ways of calculating the following type of real integrals using residue theorem in complex analysis: 
$\int_0^{2\pi}P(\sin x,\cos x)dx=\int_Cf(z)dz$, $C$ is the unit circle, and we are using $z=e^{ix}$, $x\in[0,2\pi]$, and so on. 
First, in order to match the bounds of the integral I change the variable $x=2t$, so $t\in[0,2\pi]$. Then, I used the following formulas: 
$\cos x=\frac{z^2+1}{2z}$ and $\sin x=\frac{z^2-1}{2iz}$ 
But the integrand was so ugly, I could not solve the integral. Is there any other way of calculating this integral? Can you help me? Thanks for any help.
 [/INST] Use the half-angle formulas to simplify the integrand:
$$
\int_0^\pi \frac{\cos^2 x}{1-a\sin^2 x}dx = \int_0^\pi \frac{1+\cos(2x)}{2-a+a\cos(2x)}dx = \frac{1}{2a}\int_0^{2\pi} \frac{1+\cos(t)}{\frac{2}{a}-1+\cos(t)}dt.
$$
Then apply $z = e^{it}$:
$$
\frac{1}{2a}\int_0^{2\pi} \frac{1+\cos(t)}{b+\cos(t)}dt = \frac{1}{2a}\int_C \frac{1+(z^2+1)/(2z)}{b+(z^2+1)/(2z)}\frac{dz}{iz} = \frac{1}{2ia}\int_C\frac{(z+1)^2}{z(z^2 + 2bz + 1)}dz,
$$
where I've written $b = 2/a -1$ to save space. This has residues at $0$, $-b+\sqrt{b^2-1}$, and $-b-\sqrt{b^2-1}$. Only the first two are in the unit circle and contribute to the integral, so expressing in terms of residues we have
$$
\int_0^\pi \frac{\cos^2 x}{1-a\sin^2 x}dx =\frac{\pi}{a}\left(\mathrm{Res}\left[\frac{(1+z)^2}{z(z^2+2bz+1)},0\right] + \mathrm{Res}\left[\frac{(1+z)^2}{z(z^2+2bz+1)},-b+\sqrt{b^2-1}\right]\right).
$$
I'll let you do the fancy residue stuff, but the end result is
$$
\int_0^\pi \frac{\cos^2 x}{1-a\sin^2 x}dx = \frac{\pi}{1+\sqrt{1-a}}
$$
</s>