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!

You could also do it in a standard way. Since this is a rational function of trigonometric functions, we know trigonometric substitutions work. In this case, $u=\tan x$ is a good one. We have $\mathrm{d}u=\sec^2 x\mathrm{d}x$, $\mathrm{d}x=\frac{\mathrm{d}u}{1+u^2}$ and so
$$ \begin{align}
\int \frac{a_1 \cos x + b_1 \sin x}{a\cos x + b\sin x}\,\mathrm{d}x &= \int \frac{a_1 + b_1 \tan x}{a + b\tan x}\,\mathrm{d}x \\
&= \int \frac{a_1 + b_1u}{(a + bu)(1+u^2)}\,\mathrm{d}u
\end{align}$$
Then you find partial fraction decomposition
$$\frac{a_1 + b_1u}{(a + bu)(1+u^2)}=\frac{A}{a + bu}+\frac{Bu+C}{1+u^2} $$
and the integral is
$$\frac Ab\ln|a+bu|+\frac B2\ln(1+u^2)+C\tan^{-1}(u)+\text{constant} $$
which simplifies (using the values of $A,B$ and $C$) to
$$\frac Ab \ln|a\cos x+b\sin x|+Cx+\text{constant}$$
Note that after back-substituting $u=\tan x$, you need to get rid of the tangent because it introduces discontinuities at $\cos x=0$, and you don't have these in the integrand. 
