Solution (source code)

= Solution

By the <real spectral theorem>, the real <symmetric matrix> $A$ has an orthonormal basis of <eigenvector>[eigenvectors]. In that basis the associated <quadratic form> is
$$
q(x)=\lambda_1x_1^2+\cdots+\lambda_nx_n^2.
$$
Rescaling the coordinates belonging to nonzero <eigenvalue>[eigenvalues] changes every positive coefficient to $+1$ and every negative coefficient to $-1$. Hence the diagonal normal form has one positive square for each positive eigenvalue and one negative square for each negative eigenvalue. By <Sylvester's law of inertia>, these counts do not depend on the diagonalizing basis, so
$$
\boxed{\operatorname{signature}H
=\#\{\lambda_i>0\}-\#\{\lambda_i<0\}}.
$$

The numerical eigenvalues are not invariant under a general <change of basis>, because the matrix changes by congruence rather than similarity. For example, on a one-dimensional space let $q(e)=1$. Its matrix in the basis $(e)$ is $(1)$, whereas in the basis $(2e)$ it is $(4)$. The eigenvalue changes from $1$ to $4$, although its sign, and therefore the signature, is unchanged.