Calculate Hamiltonian from Lagrangian for electromagnetic field I am unable to derive the Hamiltonian for the electromagnetic field, starting out with the Lagrangian
$$
\mathcal{L}=-\frac{1}{4}F^{\mu\nu}F_{\mu\nu}-\frac{1}{2}\partial_\nu A^\nu \partial_\mu A^\mu
$$
I found:
$$
\pi^\mu=F^{\mu 0}-g^{\mu 0}\partial_\nu A^\nu
$$
Now
$$
\mathcal{H}=\pi^\mu\partial_0 A_\mu-\mathcal{L}
$$
Computing this, I arrive at:
$$
\mathcal{H}=-\frac{1}{2}\left[\partial_0 A_\mu\partial_0 A^\mu+\partial_i A_\mu\partial_i A^\mu\right]+\frac{1}{2}\left[\partial_i A_i\partial_j A_j-\partial_j A_i\partial_i A_j\right]
$$
The right answer, according to my exercise-sheet, would be the first to terms. Unfortunately the last two terms do not cancel. I have spent hours on this exercise and I am pretty sure, that I did not commit any mistakes arriving at this result as I double checked several times. My question now is: did I start out right and am I using the right scheme? Is this in principle the way to derive the Hamiltonian, or is it easier to start out with a different Lagrangian maybe using a different gauge? Any other tips are of course also welcome. Maybe the last two terms do actually cancel and I simply don't realize it. Texing my full calculation would take a very long time so I am not going to post it, but as I said, it should be correct. But if everything hints at me having committed a mistake there, I will try again.
Edit:
After reading and thinking through Stephen Blake's answer, I realized that one can get rid of the last two terms in $H$, even though they do not vanish in $\mathcal{H}$. This is done by integrating the last term by parts and dropping the surface term, leaving $A_i\partial_j\partial_i A_j$. One can now proceed to combine the last two terms:
$$
\partial_i A_i\partial_j A_j+A_i\partial_j\partial_i A_j=\partial_i(A_i\partial_j A_j)
$$
This can be converted into a surface integral in $H$ which can be assumed to vanish, leaving us with the desired "effective" $\mathcal{H}$.
 A: There doesn't appear to be anything wrong with user35915's calculation. However, in order to get the desired answer, the canonical momenta needs to be different. Starting from user35915's action,
$$
S=\int d^{4} x\left( -\frac{1}{4}F_{\mu\lambda}F^{\mu\lambda}-\frac{1}{2}A^{\mu}_{,\mu}A^{\lambda}_{,\lambda}\right)
$$
change the second term by integrating by parts and chuck the surface term away to get,
$$
S=-\frac{1}{4}\int d^{4} x( F_{\mu\lambda}F^{\mu\lambda}-2A^{\mu}A^{\lambda}_{,\lambda\mu}) \ .
$$
Now expand the electromagnetic field tensor $F_{\mu\lambda}=A_{\lambda,\mu}-A_{\lambda,\mu}$ and do a bit of swopping dummy indices to get,
$$
S=-\frac{1}{2}\int d^{4}x \eta^{\mu\rho}\eta^{\lambda\sigma}(A_{\lambda,\mu}A_{\sigma,\rho}-A_{\lambda,\mu}A_{\rho,\sigma}-A_{\rho}A_{\lambda,\sigma\mu})\ .
$$
The last two terms can be combined into a surface integral which vanishes at infinity and the final form of the action is only the first term in the last line. The Lagrangian is now,
$$
L=-\frac{1}{2}\int d^{3}x \eta^{\mu\rho}\eta^{\lambda\sigma}A_{\lambda,\mu}A_{\sigma,\rho}=-\frac{1}{2}\int d^{3}x A_{\mu,\lambda}A^{\mu,\lambda}\ .
$$
The reason for getting the Lagrangian in this form is because it looks like the Lagrangian for four scalar fields. The canonical momenta are now,
$$
\pi^{\mu}=-A^{\mu}_{,0}=-\frac{\partial A^{\mu}}{\partial t}
$$
which look like the momenta for four scalar fields. Now, it's straightforward to go over to the desired Hamiltonian,
$$
H=-\frac{1}{2}\int d^{3}x (\pi^{\mu}\pi_{\mu}+A^{\mu}_{,r}A_{\mu,r})
$$
