<s>[INST] Integral of rational functions. I want to evaluate this integral:

$$\int{\frac{ax+b}{(x^2+2px+q)^n}}dx$$

The book only says to integrate by parts $\int{\dfrac{1}{(x^2+2px+q)^{n-1}}dx}$,
for simplicity if $n = 2$ I get:
$\int{\dfrac{1}{(x^2+2px+q)}dx}=\dfrac{x}{x^2+2px+q}+\int{\dfrac{2x^2+2px}{(x^2+2px+q)^2}}dx$.
Now I don't know what to do.
 [/INST] General description. A partial fraction of the form $$\dfrac{Bx+C}{\left[ \left( x-r\right) ^{2}+s^{2}\right] ^{n}}$$ is integrable by substitution, using the change of variables $x=r+st$. With this method an integral of the form
$$\displaystyle\int \frac{Bx+C}{\left[\left( x-r\right) ^{2}+s^{2}\right] ^{n}}dx\tag{1}$$
is transformed into an integral of the form 
$$\displaystyle\int \frac{Dt+E}{(t^{2}+1)^{n}}dt=D\int \frac{t}{(t^{2}+1)^{n}}
dt+E\int \frac{1}{(t^{2}+1)^{n}}dt\tag{2}.$$
The first integral is a table integral. The second one satisfies a recurrence relation. 
Application to the given integral. Rewrite the integrand as $\dfrac{ax+b}{\left[(x+p)^{2}+q-p^{2}\right]^{n}}$ and make the substitution 
$$
\begin{equation*}
x=-p+\sqrt{q-p^{2}}\, t.\tag{3}
\end{equation*}
$$
Then 
$$
\begin{eqnarray*}
I &=&\int \frac{ax+b}{(x^{2}+2px+q)^{n}}dx=\int \frac{ax+b}{
\left[(x+p)^{2}+q-p^{2}\right]^{n}}dx, \tag{4}\\
&& \\ \\
M &=&a\sqrt{q-p^{2}},\quad N=b-ap,\quad K=\frac{\sqrt{q-p^{2}}}{\left(
q-p^{2}\right) ^{n}} \\ \\
&& \\
I &=&K\int \frac{M}{(t^{2}+1)^{n}}t+\frac{N}{(t^{2}+1)^{n}}dt, \\
&=&KM\int \frac{1}{(t^{2}+1)^{n}}tdt+KN\int \frac{1}{(t^{2}+1)^{n}}dt \\
&=&-\frac{KM}{2(n-1)}\frac{1}{(t^{2}+1)^{n-1}}+KN\int \frac{1}{(t^{2}+1)^{n}}
dt.\tag{5}
\end{eqnarray*}
$$
The last integral can be split into two, the latter being integrable by
parts which generates the recursive relation $(6)$ bellow. For $n\ne 1$, we have:
$$
\begin{eqnarray*}I_n&=&
\int \frac{1}{(t^{2}+1)^{n}}dt =\int \frac{1+t^{2}-t^{2}}{(t^{2}+1)^{n}}
dt\\ &=&\int \frac{1}{(t^{2}+1)^{n-1}}dt-\int \frac{t^{2}}{(t^{2}+1)^{n}}dt \\
&=&\int \frac{1}{(t^{2}+1)^{n-1}}dt+\frac{t}{2(n-1)\left( t^{2}+1\right)
^{n-1}} \\ &\qquad - &\frac{1}{2(n-1)}\int \frac{1}{\left( t^{2}+1\right) ^{n-1}}\,dt \\
&=&\frac{t}{2(n-1)\left( t^{2}+1\right) ^{n-1}}+\frac{2n-3}{2(n-1)}\int 
\frac{1}{\left( t^{2}+1\right) ^{n-1}}\,dt\\
&=&\frac{t}{2(n-1)\left( t^{2}+1\right) ^{n-1}}+\frac{2n-3}{2(n-1)}I_{n-1}.\tag{6}
\end{eqnarray*}
$$
For $n=1$
$$
I_1=\int \frac{1}{t^{2}+1}dt=\arctan t+\text{Constant}.\tag{7}
$$
ADDED. For the particular case $n=2$, we get from $(6),(7)$ 
$$
\begin{eqnarray*}
I_2=\int \frac{1}{(t^{2}+1)^{2}}dt &=&\frac{1}{2}\frac{t}{t^{2}+1}+\frac{1}{2}I_1
 \\
&=&\frac{1}{2}\frac{t}{t^{2}+1}+\frac{1}{2}\arctan t+\text{Constant}.\tag{6$\mathrm{a}$}
\end{eqnarray*}
$$
and from $(5)$
$$
\begin{eqnarray*}
I &=&\int \frac{ax+b}{(x^{2}+2px+q)^{2}}dx \\
&=&-\frac{a}{2}\frac{1}{x^{2}+2px+q}+\frac{b-ap}{2\left( q-p^{2}\right) }
\frac{x+p}{q+x^{2}+2xp}\\ &+& \frac{b-ap}{2\left( q-p^{2}\right) ^{3/2}}\arctan 
\frac{x+p}{\sqrt{q-p^{2}}}+\text{Constant}.
\end{eqnarray*}\tag{5$\mathrm{a}$}
$$
Setting $a=0,b=1$ in $(5\mathrm{a})$ we evaluate your last integral for $q>p^2$: 
$$
\boxed{\begin{eqnarray*}
\int \frac{1}{(x^{2}+2px+q)^{2}}dx&=&\frac{1}{2\left( q-p^{2}\right) }\frac{x+p}{x^{2}+2px+q}\\&+&\frac{1}{2\left(
q-p^{2}\right) ^{3/2}}\arctan \frac{x+p}{\sqrt{q-p^{2}}}+\text{Constant}.
\end{eqnarray*}}
$$
$$\tag{5$\mathrm{b}$}$$
</s>