Sylvester's law of inertia states that for any symmetric matrix $A$ there exist an invertible matrix S such that, $S^T A S = D$, where $D$ is a diagonal matrix which has only entries 0, +1 and −1 along the diagonal.
How can the matrix $S$ be computed such that for symmetric positive diagonal matrix $A$, the diagonal matrix $D$ will be in the following form, $\begin{pmatrix} I_r \\ & 0\\ & & \ddots & \\ & & & & 0 \\ \end{pmatrix}$.
Where $r = \text{rank} (A)$
By the Spectral Theorem, $A$ is orthogonally similar to a diagonal matrix, i.e $$ P^{T}AP=\pmatrix{\lambda_1 \\ & \ddots \\ && \lambda_n} $$ where $\lambda_i$ is an eigenvalue of $A$, and $\space P^{T}P=P^{-1}P=I$. Since $$ \pmatrix{\lambda_1 \\ & \ddots \\ && \lambda_n}=Q\pmatrix{I_r\\ &-I_s\\ && 0}Q $$ where $r$ is the number of positive eigenvalues and $s$ the number of negative eigenvalues. $\lambda_i> 0, \:i\leqslant r,\:\lambda_i< 0,\:r<i\leqslant r+s$ and $\lambda_i= 0, \:i>r+s$. And $$ Q=\pmatrix{{\sqrt{|\lambda_1|}} \\ & \ddots \\ && {\sqrt{|\lambda_{r+s}|}} \\ &&& 1} $$ And $Q^T=Q$. Let $S=PQ^{-1}$. Then $$ S^TAS=\pmatrix{I_r\\ &-I_s\\ && 0} $$