Solution (source code)

= Solution

Put $C=A+yI$. For every $y$ except the finitely many roots of $\det(A+yI)$, $C$ is invertible and $CB$ is similar to $BC$, since $C^{-1}(CB)C=BC$. Hence
$$
\det(tI-(A+yI)B)=\det(tI-B(A+yI)).
$$
Both sides are polynomials in $y$. Agreement for infinitely many $y$ makes them identical, so evaluating at $y=0$ proves <Characteristic polynomials of AB and BA>:
$$
\boxed{\chi_{AB}(t)=\chi_{BA}(t).}
$$
Their <minimal polynomials> need not agree. For
$$
A=\begin{pmatrix}1&0\\0&0\end{pmatrix},\qquad
B=\begin{pmatrix}0&1\\0&0\end{pmatrix},
$$
we have $AB=B\ne0$, $B^2=0$, and $BA=0$. The two <minimal polynomials> are respectively \b[$t^2$ and $t$], although both characteristic polynomials are $t^2$.

For any $p(t)=\sum_jp_jt^j$, the identity $(BA)^{j+1}=B(AB)^jA$ gives
$$
(BA)p(BA)=B p(AB)A.
$$
Taking $p=m_{AB}$ makes the right-hand side zero. Every annihilating polynomial is divisible by the <minimal polynomial>, so
$$
\boxed{m_{BA}(t)\mid t\,p(t).}
$$
If $AB$ is <diagonalizable>, $p$ has only simple roots. Therefore each nonzero <eigenvalue> of $BA$ has only size-one <Jordan blocks>, and the zero-eigenvalue blocks have size at most two. Squaring kills the latter nilpotent blocks, so $(BA)^2$ is <diagonalizable>, as is $(AB)^2$.

By their common <characteristic polynomial>, $AB$ and $BA$ have the same <eigenvalues> counted with algebraic multiplicity. Squaring produces the same multiset of squared <eigenvalues>, including combined multiplicities if two <eigenvalues> have the same square. The two <diagonalizable> squares therefore have the same diagonal normal form up to permutation. This proves <similarity of squared matrix products with one diagonalizable product>:
$$
\boxed{(AB)^2\text{ and }(BA)^2\text{ are conjugate}.}
$$
It does not imply that $AB$ and $BA$ themselves are conjugate: the possible size-two nilpotent blocks at zero are exactly what squaring removes.