Theorem: Primitive of Reciprocal of p by Sine of a x plus q by Cosine of a x plus r

:$\ds \int \frac {\d x} {p \sin a x + q \cos a x + r} = \begin{cases}
\ds \frac 2 {a \sqrt {r^2 - p^2 - q^2} } \map \arctan {\frac {p + \paren {r - q} \tan \dfrac {a x} 2} {\sqrt {r^2 - p^2 - q^2} } } + C & : p^2 + q^2 < r^2 \\
\ds \frac 1 {a \sqrt {p^2 + q^2 - r^2} } \ln \size {\frac {p - \sqrt {p^2 + q^2 - r^2} + \paren {r - q} \tan \dfrac {a x} 2} {p + \sqrt {p^2 + q^2 - r^2} + \paren {r - q} \tan \dfrac {a x} 2} } + C & : p^2 + q^2 > r^2
\end{cases}$

Proof:
Let $u = \tan \dfrac {a x} 2$.
Then we have:
{{begin-eqn}}
{{eqn | l = \d x
      | r = \dfrac {2 \rd u} {a \paren {1 + u^2} }
}}
{{eqn | l = \sin a x
      | r = \dfrac {2 u} {1 + u^2}
}}
{{eqn | l = \cos a x
      | r = \dfrac {1 - u^2} {1 + u^2}
}}
{{end-eqn}}
Hence:
{{begin-eqn}}
{{eqn | l = \int \frac {\d x} {p \sin a x + q \cos a x + r}
      | r = \int \dfrac {2 \rd u} {a \paren {1 + u^2} } \frac 1 {p \dfrac {2 u} {1 + u^2} + q \dfrac {1 - u^2} {1 + u^2} + r}
      | c = Weierstrass Substitution
}}
{{eqn | r = \dfrac 2 a \int \frac {\d u} {2 p u + q \paren {1 - u^2} + r \paren {1 + q^2} }
      | c = 
}}
{{eqn | r = \dfrac 2 a \int \frac {\d u} {\paren {r - q} u^2 + 2 p u + \paren {r + q} }
      | c = 
}}
{{eqn | r = \dfrac 2 {a \paren {r - q} } \int \frac {\d u} {u^2 + \dfrac {2 p} {r - q} u + \dfrac {r + q} {r - q} }
      | c = 
}}
{{eqn | r = \dfrac 2 {a \paren {r - q} } \int \frac {\d u} {\paren {u + \dfrac p {r - q} }^2 - \dfrac {p^2} {\paren {r - q}^2} + \dfrac {r + q} {r - q} }
      | c = completing the square
}}
{{eqn | r = \dfrac 2 {a \paren {r - q} } \int \frac {\d u} {\paren {u + \dfrac p {r - q} }^2 + \dfrac {r^2 - \paren {p^2 + q^2} } {\paren {r - q}^2} }
      | c = rearranging
}}
{{eqn | n = 1
      | r = \dfrac 2 {a \paren {r - q} } \int \frac {\d z} {z^2 + \dfrac {r^2 - \paren {p^2 + q^2} } {\paren {r - q}^2} }
      | c = Integration by Substitution: substituting $z = u + \dfrac p {r - q}$: $\d z = \d u$
}}
{{end-eqn}}
There are $2$ cases: $r^2 > p^2 + q^2$ and $r^2 < p^2 + q^2$.
First suppose $r^2 > p^2 + q^2$.
Then:
:$r^2 - \paren {p^2 + q^2} > 0$
and so $(1)$ may be written as:
{{begin-eqn}}
{{eqn | l = \int \frac {\d x} {p \sin a x + q \cos a x + r}
      | r = \dfrac 2 {a \paren {r - q} } \int \frac {\d z} {z^2 + \paren {\dfrac {\sqrt {r^2 - p^2 - q^2} } {r - q} } }
      | c = 
}}
{{eqn | r = \dfrac 2 {a \paren {r - q} } \dfrac {r - q} {\sqrt {r^2 - p^2 - q^2} } \map \arctan {\dfrac {\paren {r - q} z} {\sqrt {r^2 - p^2 - q^2} } } + C
      | c = Primitive of $\dfrac 1 {x^2 + a^2}$
}}
{{eqn | r = \dfrac 2 {a \sqrt {r^2 - p^2 - q^2} } \map \arctan {\dfrac {\paren {r - q} \paren {u + \dfrac p {r - q} } } {\sqrt {r^2 - p^2 - q^2} } } + C
      | c = substituting back for $z$
}}
{{eqn | r = \dfrac 2 {a \sqrt {r^2 - p^2 - q^2} } \map \arctan {\dfrac {p + \paren {r - q} \tan \dfrac {a x} 2} {\sqrt {r^2 - p^2 - q^2} } } + C
      | c = simplifying and substituting back for $u$
}}
{{end-eqn}}
Next suppose $r^2 < p^2 + q^2$.
Then:
:$r^2 - \paren {p^2 + q^2} < 0$
and so $(1)$ may be written as:
{{begin-eqn}}
{{eqn | l = \int \frac {\d x} {p \sin a x + q \cos a x + r}
      | r = \dfrac 2 {a \paren {r - q} } \int \frac {\d z} {z^2 - \paren {\dfrac {\sqrt {p^2 + q^2 - r^2} } {r - q} } }
      | c = 
}}
{{eqn | r = \dfrac 2 {a \paren {r - q} } \dfrac {r - q} {2 \sqrt {p^2 + q^2 - r^2} } \ln \size {\frac {z - \dfrac {\sqrt {p^2 + q^2 - r^2} } {r - q} } {z + \dfrac {\sqrt {p^2 + q^2 - r^2} } {r - q} } } + C
      | c = Primitive of $\dfrac 1 {x^2 - a^2}$
}}
{{eqn | r = \dfrac 1 {a \sqrt {p^2 + q^2 - r^2} } \ln \size {\frac {\paren {r - q} \paren {u + \dfrac p {r - q} } - \sqrt {p^2 + q^2 - r^2} } {\paren {r - q} \paren {u + \dfrac p {r - q} } + \sqrt {p^2 + q^2 - r^2} } } + C
      | c = substituting back for $z$
}}
{{eqn | r = \dfrac 1 {a \sqrt {p^2 + q^2 - r^2} } \ln \size {\frac {p - \sqrt {p^2 + q^2 - r^2} + \paren {r - q} \tan \dfrac {a x} 2} {p + \sqrt {p^2 + q^2 - r^2} + \paren {r - q} \tan \dfrac {a x} 2} } + C
      | c = simplifying and substituting back for $u$
}}
{{end-eqn}}
{{qed}}