Solution (source code)

= Solution

A <Smith normal form> of an $m\times n$ matrix $A$ over a <principal ideal domain> $R$ is a diagonal matrix
$$
D=UAV=\operatorname{diag}(d_1,\ldots,d_r,0,\ldots,0),
\qquad d_1\mid d_2\mid\cdots\mid d_r,
$$
where $U\in GL_m(R)$ and $V\in GL_n(R)$. It is guaranteed to exist for every matrix over a principal ideal domain. The entries $d_i$ are determined up to multiplication by <unit in a ring>[units]; over $\mathbb Z$ they are conventionally chosen positive.