Solution (source code)

= Solution

The <Morse lemma> states that if $p$ is a <nondegenerate critical point> of a smooth real function $f$ on an $m$-dimensional <smooth manifold>, there are smooth coordinates $y_1,\ldots,y_m$, centered at $p$, such that
$$
\boxed{f(y)=f(p)-\sum_{i=1}^{\lambda}y_i^2+\sum_{i=\lambda+1}^m y_i^2.}
$$
Here $\lambda$ is the <Morse index>, the number of negative directions of the <Hessian matrix> at $p$. The equality is exact on a neighborhood, not merely a second-order expansion.

To prove it, start with any coordinates putting $p=0$. Since $df(0)=0$, integral <Taylor theorem> gives
$$
f(x)-f(0)=\sum_{i,j=1}^m a_{ij}(x)x_ix_j,\qquad a_{ij}(x)=\int_0^1(1-t)\,\partial_i\partial_jf(tx)\,dt.
$$
The <matrix> $A(x)=(a_{ij}(x))$ is smooth and symmetric, with $A(0)=\tfrac12\operatorname{Hess}f(0)$. By <Sylvester's law of inertia>, a constant invertible linear coordinate change makes $A(0)=\operatorname{diag}(-I_\lambda,I_{m-\lambda})$.

We perform completion of squares with coefficients depending smoothly on the original $x$. The first pivot $a_{11}(x)$ stays nonzero and keeps its sign on a sufficiently small neighborhood. Algebraically,
$$
x^TA(x)x=a_{11}(x)\left(x_1+\sum_{j>1}\frac{a_{1j}(x)}{a_{11}(x)}x_j\right)^2+\sum_{i,j>1}\left(a_{ij}(x)-\frac{a_{i1}(x)a_{1j}(x)}{a_{11}(x)}\right)x_ix_j.
$$
The remaining coefficient matrix is the <Schur complement>. At zero it is the remaining signed diagonal matrix, so its first pivot is also nonzero nearby. Continue recursively, shrinking the neighborhood finitely many times. Every pivot $d_i(x)$ is smooth and nonzero, with sign $\epsilon_i=-1$ for $i\leq\lambda$ and $+1$ thereafter. Absorb $|d_i(x)|$ into the square by its smooth positive square root. This produces a smooth triangular matrix $B(x)$ with nonzero diagonal such that
$$
x^TA(x)x=\sum_i\epsilon_i\bigl(B(x)x\bigr)_i^2.
$$
All coefficients here are functions of the original $x$; no circular coordinate substitution is involved. Define $\Phi(x)=B(x)x$. Its derivative at zero is $D\Phi(0)=B(0)$, which is invertible. The <inverse function theorem> makes $y=\Phi(x)$ a smooth coordinate system and gives the asserted exact quadratic form. The number of negative squares agrees with the <Hessian matrix> signature and hence with the <Morse index>. This is the <smooth completing-square proof of the Morse lemma>.