Solution (source code)

= Solution

Let $Av=\alpha v$. The commutation relation gives $A(Bv)=B(Av)=\alpha Bv$, so $B$ preserves the <eigenspace> of $A$ with <eigenvalue> $\alpha$. Because it is a <nondegenerate eigenvalue>, the <eigenspace> is one-dimensional, and $Bv=\beta v$ for some real $\beta$. This includes $Bv=0$. Thus the <eigenvectors> of $A$ form a common <eigenbasis>, and
$$
Uv=(\alpha+i\beta)v.
$$
Writing the corresponding unit-modulus <eigenvalue> of $U$ as $\zeta=e^{i\theta}$ gives
$$
\boxed{\alpha=\operatorname{Re}\zeta=\cos\theta,\qquad\beta=\operatorname{Im}\zeta=\sin\theta.}
$$
The pairing refers to the same common <eigenvector>; independent reorderings of the two real spectra would lose this information. The <simultaneous diagonalization> can also be constructed without requiring <simple eigenvalues> by diagonalizing $B$ within each <eigenspace> of $A$.