TITLE: Matrices: characterizing pairs $(AB, BA)$
QUESTION [27 upvotes]: Let $A$ be an $m\times n$-matrix and $B$ an $n \times m$-matrix over the same field. Consider the matrices $C=AB$ and $D=BA$. It is probably well known (and not difficult to show) that the only difference between the canonic rational forms of $C$ and $D$ are nilpotent blocks (blocks with minimal polynomial $x^k$). (In particular, these compensate the different dimensions of $C$ and $D$.)
I'm interested in the converse question:  

Given an $m\times m$-matrix $C$ and an $n\times n$-matrix $D$, what are necessary and sufficient conditions that there exist matrices $A$ and $B$ such that $C=AB$ and $D=BA$?   

One may assume without loss of generality that $C$ and $D$ are both nilpotent. I'm thinking of a characterization in terms of the Jordan normal forms of $C$ and $D$. These in turn are characterized by their block sizes. In fact, an equivalent version can be stated as a question on partitions:  

Suppose $\lambda=(\lambda_1 \geq\lambda_2\geq \dotsc )$ and $\mu= (\mu_1 \geq \mu_2 \geq \dotsc)$ are partitions of the integers $m$ and $n$. When are there matrices $A$ and $B$ such that the blocks in the jordan normal forms of $AB$ and $BA$ belonging to the eigenvalue $0$ have sizes $\lambda_1, \dotsc$ and $\mu_1, \dotsc$, respectively?  

These are not arbitrary, for example, the quotient of the minimal polynomials must be in $\{1, x^{\pm 1}\}$, meaning that $|\lambda_1-\mu_1| \leq 1$.
This problem seems so natural that I think it has been addressed somewhere (not in the linear algebra books I looked into, however), so in particular I would appreciate a reference.  
EDIT: I have now seen that $|\lambda_i -\mu_i|\leq 1$ for all $i$ is sufficient (and this is easy, since we may assume $C$ and $D$ in Jordan form, and then reduce to the case of one Jordan block). I guess it's necessary, too. Does anyone know a reference for this? And are there any nontrivial mathematical applications of this situation?

REPLY [12 votes]: By accident, I came across a note in the American Mathematical Monthly (C. R. Johnson and E. A. Schreiner, The Relationship between AB and BA, vol. 103 (MR)), where this is treated. It seems that this was first treated in the following article:

Harley Flanders, Elementary divisors of $AB$ and $BA$, Proc. Amer. Math. Soc. 2 (1951), p.871--874 (MR).

In fact, $\lvert\lambda_i-\mu_i\rvert\leq 1$ is the exact condition; this is sometimes called "Flanders's theorem". This gives a keyword to search for on Math Reviews, etc., but probably this has been rediscovered more than once. (Note that the titles above don't contain very specific keywords.)