Last step derivation of hamiltonian of particle in electromagnetic field My textbook quantum mechanics does an analogues derivation of the hamiltonian as given here, but I'm struggling to understand the last step: The final obtained hamiltonian is (in my textbook's case)
$$H = \frac{1}{2}m\dot{\vec{x}} + eV$$
with V the electromagnetic potential, my professor (and the link) then claims that this is the same as:
$$H = \frac{1}{2m}(\vec{p} - \frac{e}{c}\vec{A})^2 + eV$$
but when I expand the bottom equation I get:
$$\frac{1}{2}m\dot{\vec{x}} + eV -\frac{e}{2mc}(\vec{A}\cdot\vec{p} + \vec{p}\cdot\vec{A}) + \frac{1}{2m}\left(\frac{e}{c}\right)^2A^2$$
How do the two terms on the right side cancel out?
Thanks in advance.
 A: The mistake you're making is in assuming that $$p_x = m \dot{x}.$$ This is not true. $m \dot{x}$ is known as the kinetic momentum, while $p_x$ is known as the conjugate momentum (to the coordinate $x$). In most (all?) problems in Classical Mechanics these two concepts are equal, but they aren't the same in general.
The conjugate momentum is obtained from the Lagrangian, using the relation $$p_x = \frac{\partial \mathcal{L}}{\partial \dot{x}}.$$
In most "usual" problems in Classical Mechanics, the only dependence the Lagrangian has on velocity is in the kinetic energy term, and thus $$\frac{\partial \mathcal{L}}{\partial \dot{x}} = \frac{\partial}{\partial \dot{x}}\left(\frac{1}{2}m\dot{x}^2\right) = m \dot{x},$$
but in electromagnetism, the Lagrangian is given by: $$\mathcal{L}_\text{em} = \frac{1}{2} m \dot{x}^2 - qV + q \dot{x}A_x,$$ and so $$p_x = \frac{\partial \mathcal{L}_\text{em}}{\partial \dot{x}} = m \dot{x} + q A_x.$$
As a result, $m \dot{x} \neq  p_x$, but rather $$m \dot{x} = p_x - qA_x.$$ Using this in formula for the Hamiltonian: $$\mathcal{H} = \frac{1}{2}m \dot{x}^2 + qV = \frac{(p_x - qA_x)^2}{2 m} + q V.$$
