Solution (source code)

= Solution

Define the <canonical momenta> by
$$
\boxed{p_i=\frac{\partial L}{\partial\dot q_i}(\mathbf q,\dot{\mathbf q},t),\qquad i=1,\ldots,n.}
$$
To obtain an ordinary unconstrained <Hamiltonian>, assume a <regular Lagrangian>: the velocity <Hessian matrix> $\bigl(\partial^2L/\partial\dot q_i\partial\dot q_j\bigr)$ is invertible. The <inverse function theorem> then lets us solve locally for $\dot{\mathbf q}=\mathbf v(\mathbf q,\mathbf p,t)$. The <phase space> is the <cotangent bundle> $T^*\mathcal Q$, with local <canonical coordinates> $(\mathbf q,\mathbf p)$ and dimension $2n$. The <Legendre transform in mechanics> defines
$$
\boxed{H(\mathbf q,\mathbf p,t)=\sum_{i=1}^np_iv_i(\mathbf q,\mathbf p,t)
-L\bigl(\mathbf q,\mathbf v(\mathbf q,\mathbf p,t),t\bigr).}
$$
The velocities in this formula have been eliminated in favour of <phase space> variables. A <hyperregular Lagrangian> makes this construction global; regularity alone guarantees it locally. <Hamilton's equations> are
$$
\boxed{\dot q_i=\frac{\partial H}{\partial p_i},\qquad
\dot p_i=-\frac{\partial H}{\partial q_i},\qquad i=1,\ldots,n.}
$$