Solution (source code)

= Solution

The <canonical momentum> is $\mathbf p=m\dot{\mathbf r}+q\mathbf A$, and the <Legendre transform> gives
$$
H=\mathbf p\cdot\dot{\mathbf r}-L
=\frac{|\mathbf p-q\mathbf A|^2}{2m}+q\phi.
$$
Its <Hamilton's equations> are
$$
\boxed{\dot{\mathbf r}=\frac{\partial H}{\partial\mathbf p}
=\frac{\mathbf p-q\mathbf A}{m}}
$$
and, componentwise,
$$
\boxed{\dot p_i=-\frac{\partial H}{\partial r_i}
=q\dot r_j\partial_iA_j-q\partial_i\phi.}
$$
Combining the second equation with $\mathbf p=m\dot{\mathbf r}+q\mathbf A$ recovers the <Lorentz force law>.