<s>[INST] Evaluate the definite integral $\int_0^{2\pi}\arctan\left((a\cos x + b\sin x)^2\right)dx$ Writing:
Integrate[ArcTan[(a Cos[x] + b Sin[x])^2], {x, 0, 2 Pi}, Assumptions -> a^2 + b^2 > 0]


$$\int_0^{2\pi}\arctan\left((a\cos x + b\sin x)^2\right)dx,$$
  where $a $ and $b $ are real numbers.

I get:
2 Pi ArcTan[Sqrt[1/2 (-1 + Sqrt[1 + (a^2 + b^2)^2])]]


$$2\pi\arctan\sqrt{\frac{\sqrt{1 + (a^2 + b^2)^2}-1}2} $$

How to derive this result on paper?
 [/INST] *

*We have $a\cos x+b\sin x=r\cos(x-\phi)$, where $$r=\sqrt{a^2+b^2},\quad\cos\phi=a/r,\quad\sin\phi=b/r,$$ and we can simply replace $x-\phi$ by $x$ in the integrand (because of its $2\pi$-periodicity). Denoting $c=(a^2+b^2)/2$, we see that the given integral is equal to $$\int_{0}^{2\pi}\arctan(2c\cos^2 x)\,dx=\int_{0}^{2\pi}\arctan\big(c(1+\cos x)\big)\,dx.$$


*Recall that, for $d\in\mathbb{C}$ such that $|d|<1$, $$\int_{0}^{2\pi}\ln(1-2d\cos x+d^2)\,dx=0$$ (assuming the principal branch taken). This can be seen, after $$1-2d\cos x+d^2=(1-de^{ix})(1-de^{-ix}),$$ as an application of the Cauchy integral theorem. (Alternatively, one can use the above and the power series for $\ln(1+z)$, or even just split $\int_{0}^{2\pi}=\int_{0}^{\pi}+\int_{\pi}^{2\pi}$ and substitute $x=y+\pi$ in the second integral, to get $I(d)=I(d^2)/2$ from which $I(d)=0$ follows easily.) This implies $$\int_{0}^{2\pi}\ln(1+d\cos x)\,dx=2\pi\ln\frac{1+\sqrt{1-d^2}}{2}.$$


*Write $$\arctan\big(c(1+\cos x)\big)=\frac{1}{2i}\ln\frac{1+ic}{1-ic}\frac{1+d\cos x}{1+\bar{d}\cos x},\qquad d=\frac{ic}{1+ic}$$ (where $\bar{d}$ is complex conjugate to $d$); the integral then equals $$2\pi\arg(1+ic+\sqrt{1+2ic})=2\pi\arctan\frac{c+v}{1+u}=2\pi\arctan v,$$ where $\sqrt{1+2ic}=u+iv$, and we use $u=c/v$. This is the answer.
</s>