Solution (source code)

= Solution

For $n$ degrees of freedom, <phase space> is the $2n$-dimensional space of <canonical coordinates> $(q_1,\ldots,q_n,p_1,\ldots,p_n)$. Initial data at one point determine a <trajectory> under the <Hamilton equations>.

<Liouville's theorem> in <Hamiltonian> mechanics states that this flow preserves the canonical volume element $d\Gamma=\prod_i dq_i\,dp_i$. Its <divergence> is
$$
\nabla_{q,p}\cdot(\dot q,\dot p)
=\sum_i\left(\frac{\partial^2H}{\partial q_i\partial p_i}-\frac{\partial^2H}{\partial p_i\partial q_i}\right)=0.
$$
If $J(t)$ is the <Jacobian determinant> of the flow from initial data, the determinant evolution formula gives $\dot J=J\nabla\cdot(\dot q,\dot p)=0$. Since $J(0)=1$, it remains one, proving preservation of every advected phase volume. The proof also applies to smooth explicitly time-dependent Hamiltonians.

For an ensemble density $\rho(q,p,t)$, probability conservation and the same vanishing <divergence> give
$$
\boxed{\partial_t\rho+\sum_i\left(\dot q_i\partial_{q_i}\rho+\dot p_i\partial_{p_i}\rho\right)=0,
\quad\text{or}\quad \partial_t\rho+\{\rho,H\}=0.}
$$
Thus the fine-grained density is constant along the <Hamiltonian> <trajectories>, although the ensemble may shear, stretch and develop fine structure. This is not a claim that coordinate-space volume or every lower-dimensional projection is conserved.