Explanation of an integration trick for $\int \frac{a_1 \cos x + b_1 \sin x}{a\cos x + b\sin x}dx$ I'm not sure how to formulate my question correctly. Basically it comes from solving the integral:
$$
\int \frac{a_1 \cos x + b_1 \sin x}{a\cos x + b\sin x}dx\\
a^2 + b^2 \ne 0
$$
I haven't been able to solve the integral without the trick I've found after a while. This trick suggests to rewrite:
$$
a_1 \cos x + b_1 \sin x = \frac{a_1a + b_1b}{a^2 + b^2}(a\cos x + b\sin x) + \frac{a_1b - ab_1}{a^2 + b^2}(b\cos x - a\sin x)\tag{1}
$$
After using this trick the integral becomes almost elementary:
$$
I = \int \frac{a_1a + b_1b}{a^2 + b^2} dx + \int \frac{a_1b - ab_1}{a^2 + b^2}\frac{b\cos x - a\sin x}{a\cos x + b\sin x} dx
$$
The first part is trivial, for the second one substitute $u = a\sin x + b\cos x$.
My question is how on earth one could arrive at $(1)$. Is that some sort of well-known expression that I just missed?
Going from RHS to LHS in $(1)$ is easy, but how do I make it the other way round?
Thank you!

Any linear combination of sine waves with the same period and different phase shifts can be written as a single sine wave with that same period and a suitable phase shift.
\begin{align}
& A\cos(x+\varphi) + B\cos(x+ \psi) \\[8pt]
= {} & A\big(\cos x\cos\varphi - \sin x \sin\varphi\big) \\
& {} + B\big(\cos x\cos\psi - \sin x \sin \psi\big) \\[8pt]
= {} & C\cos x + D\sin x
\end{align}
where
\begin{align}
C & = A\cos\varphi + B \cos\psi \\[8pt]
D & = -A\sin\varphi - B\sin\psi
\end{align}
and then
\begin{align}
& C\cos x + D\sin x \\[8pt]
= {} & \sqrt{C^2+D^2} \left( \frac C {\sqrt{C^2+D^2}} \cos x + \frac D {\sqrt{C^2+D^2}} \sin x\right) \\[8pt]
= {} & \sqrt{C^2+D^2} \big( E\cos x + F\sin x\big).
\end{align}
We now have $E^2+F^2=1$ so $E= \cos\chi$ and $F=\sin\chi$ for some angle $\chi.$ Thus we have
\begin{align}
& E\cos x + F\sin x \\[8pt]
= {} & \cos\chi\cos x + \sin\chi\sin x \tag 1 \\[8pt]
= {} & \cos(x-\chi).
\end{align}
Line $(1)$ above is what you have in the problem you're facing.
