Solution (source code)

= Solution

Multiply the <partial fraction decomposition> by $(x+1)(x+2)^2$ to obtain a <polynomial identity>:
$$
x^2+1=a(x+2)^2+b(x+1)(x+2)-5(x+1).
$$
At $x=-1$, this gives $a=2$. Comparing the coefficients of $x^2$ then gives $a+b=1$, so
$$
\boxed{a=2,\qquad b=-1.}
$$
The remaining coefficients agree: $4a+3b-5=0$ and $4a+2b-5=1$. The original rational identity is understood away from its <poles> $-1$ and $-2$.