<s>[INST] Evaluating $\int_0^{\pi/2} \frac{a}{a^2+\cos^2 \theta} \, d\theta$ I want to evaluate 
$$
\int_0^{\pi/2} \frac{a}{a^2+\cos^2 \theta} \, d\theta
$$
and here is what Wolfram alpha gave me:
$$
\int_0^{\pi/2} \frac{a}{a^2+\cos^2 \theta} \, d\theta=\frac{\tan^{-1} \left(\frac{a \tan x}{\sqrt{a^2+1}}\right)}{\sqrt{1+a^2}}
$$
Seeing the answer, I substituted $\cos \theta = \tan x$ hoping something good would happen, but in the end it didn't lead anywhere, assuming I didn't make any mistake. But after that I'm knid of hopelessly lost.
What's the magic trick here?
 [/INST] For the sake of completeness I present another common technique.
Suppose we are trying to evaluate
$$\int_0^{\pi/2} \frac{a}{a^2+\cos^2\theta} \; d\theta
= \frac{1}{4}
\int_0^{2\pi} \frac{a}{a^2+\cos^2\theta} \; d\theta.$$
Introduce $z=\exp(i\theta)$ so that $dz=iz\;d\theta$
to get
$$\frac{1}{4} \int_{|z|=1}
\frac{a}{a^2+(z+1/z)^2/4} \frac{1}{iz} \; dz
\\ = \frac{1}{4} \int_{|z|=1}
\frac{az^2}{z^2a^2+(z^2+1)^2/4} \frac{1}{iz} \; dz
\\ = \frac{a}{i} \int_{|z|=1}
\frac{z}{4z^2a^2+(z^2+1)^2} \; dz
\\ = \frac{a}{i} \int_{|z|=1}
\frac{z}{z^4+(4a^2+2)z^2+1} \; dz.$$
Call the function $f(z).$ The poles are at
$$\pm \sqrt{-2a^2-1 \pm 2a \sqrt{a^2+1}}$$
which is
$$\pm \sqrt{-2a^2-1 \pm 2a^2 \sqrt{1+1/a^2}}$$
When $a>1$ we have
$$\sqrt{1+1/a^2} = 1 + \frac{1}{2} 1/a^2 - \frac{1}{8} 1/a^4
+ \frac{1}{16} 1/a^6 + \cdots$$
Therefore
$$\rho_{1,2} = \pm \sqrt{-2a^2-1 + 2a^2 \sqrt{1+1/a^2}}
= \pm \sqrt{-\frac{1}{8} 1/a^4 + \frac{1}{16} 1/a^6 + \cdots}$$
so that these two poles are inside the unit circle.
On the other hand,
$$\rho_{3,4} = \pm \sqrt{-2a^2-1 - 2a^2 \sqrt{1+1/a^2}}
= \pm \sqrt{-4a^2 - 2  + \cdots}$$
so that these two poles are outside the unit circle.

It follows that the integral is given by
$$\frac{a}{i} \times
2\pi i \times
(\mathrm{Res}_{z=\rho_1} f(z) + \mathrm{Res}_{z=\rho_2} f(z))$$
or
$$2a\pi \times
(\mathrm{Res}_{z=\rho_1} f(z) + \mathrm{Res}_{z=\rho_2} f(z)).$$
These poles are simple so we get
$$\mathrm{Res}_{z=\rho_{1,2}} f(z)
=\rho_{1,2} \frac{1}{4\rho_{1,2}^3+2(4a^2+2)\rho_{1,2}}
=\frac{1}{4\rho_{1,2}^2+2(4a^2+2)}.$$
This is
$$\frac{1}{4(-2a^2-1)+ 8 a^2\sqrt{1+1/a^2} + 2(4a^2+2)}$$
or
$$\frac{1}{8 a^2\sqrt{1+1/a^2}}
= \frac{1}{8a \sqrt{a^2+1}}.$$
This finally yields for the integral 
$$2a\pi \times 2 \times \frac{1}{8a \sqrt{a^2+1}}
= \frac{\pi}{2\sqrt{a^2+1}}.$$
</s>