<s>[INST] Evaluating the integral $\int{\frac{x^2-2x-1}{(x-1)^2(x^2+1)}}$ I successfully evaluated the following integral using partial fraction expansion, but am unsure of a few steps.
$$
\int{\frac{x^2-2x-1}{(x-1)^2(x^2+1)}} = \int\left( \frac{A}{x-1} + \frac{B}{(x-1)^2} + \frac{Cx+D}{x^2+1} \right) \\\ \\
\\
x^2-2x-1 = A(x-1)(x^2+1) + B(x^2+1) + (Cx+D)(x-1)^2 \\\ \\
\\
x=1 \implies -2 = 2B \implies B = -1 \\\ \\
\begin{align}
x=i \implies & -2-2i = (D+Ci)(i-1^2) = -2i(D+Ci) \\
    \implies & -C+Di = 1+i \\
    \implies &\ C=-1,\ \ D=1
\end{align} \\\ \\
\\
x^2-2x-1 = A(x-1)(x^2+1) - (x^2+1) - (x-1)^3 \\\ \\
\\
x=0 \implies -1 = -A \implies A = 1 \\\ \\
\\
\int{\frac{x^2-2x-1}{(x-1)^2(x^2+1)}} = \int\left(\frac{1}{x-1} - \frac{1}{(x-1)^2} + \frac{1-x}{x^2+1}\right) = \int \left( \frac{1}{x-1} - \frac{1}{(x-1)^2} - \frac{x}{x^2+1} + \frac{1}{x^2+1} \right) \\\ \\
\\
= \ln|x-1| + \frac{1}{x-1} - \frac{1}{2}\ln|x^2+1| + \arctan(x) + C\\\ \\
$$
My main points of confusion are:


*

*In general, how do you know which denominators to use in step 1?  

*Is it okay to find the variables by plugging in values for $x$? Does this work in general? Is there anything I should be aware of, particularly where $i$ is involved?

 [/INST] In general you factor the denominator (this can be hard), then
every linear factor $(x-a)^r$ gives you $r$ terms $\frac {A_1}{x-a}$, $\frac {A_2}{(x-a)^2}$, $\ldots$, $\frac {A_r}{(x-a)^r}$
every quadratic factor $(x^2+ bx+c)^s$ (it has complex roots, otherwise factor it to linear terms and apply the above) gives you $s$ terms $\frac {B_1 x+C_1}{x^2+bx+c}$,$\frac {B_2x+C_2}{(x^2+bx+c)^2}$, $\ldots$, $\frac {B_sx+C_s}{(x^2+bx+c)^s}$
Next step is to solve for $A_i$, $B_j$ and $C_j$. It's a linear system that has unique solution. In the end integrate all the remaining integrals
</s>