An equality in Hilbert spaces To understand a proof in functional analysis I need to understand why the following equation is true:
$$\lVert x\rVert^2 - \sum_{j=1}^n |x_i|^2 = \Biggl\lVert x-\sum_{i=1}^nx_ie_i\Biggr\rVert^2$$
Where $x\in H$ ($H$ a Hilbert space) and $x_i= \langle x,e_i\rangle$ and $e_i$ is an orthonormal system.
Can someone explain me why this equality is true?
Thanks in advance!
 A: \begin{align}
\left\|x-\sum \limits_{i=1}^{n}\langle x,e_i\rangle e_i\right\|^2&=\langle x-\sum \limits_{i=1}^{n}\langle x,e_i\rangle e_i\,,x-\sum \limits_{i=1}^{n}\langle x,e_i\rangle e_i\ \rangle
\\
&=\langle x,x\rangle+\underbrace{\langle \sum \limits_{i=1}^{n}\langle x,e_i\rangle e_i,\sum \limits_{i=1}^{n}\langle x,e_i\rangle e_i\rangle}_{\text{(by Pythagoras Theorem)}}-\langle x,\sum \limits_{i=1}^{n}\langle x,e_i\rangle e_i\rangle-\langle \sum \limits_{i=1}^{n}\langle x,e_i\rangle e_i,x\rangle \hspace{-10 mm} 
\\
&=\|x\|^2+\sum\limits_{i=1}^{n}|\langle x,e_i\rangle|^2\|e_i\|^2-\sum \limits_{i=1}^{n}\overline{\langle x,e_i\rangle}\langle \overline{e_i},\overline{x}\rangle-\sum \limits_{i=1}^{n}\langle x,e_i\rangle\langle e_i,x\rangle
\\
&=\|x\|^2+\sum\limits_{i=1}^{n}|\langle x,e_i\rangle|^2-\sum\limits_{i=1}^{n}|\langle x,e_i\rangle|^2-\sum\limits_{i=1}^{n}|\langle x,e_i\rangle|^2
\\
&=\|x\|^2-\sum\limits_{i=1}^{n}|\langle x,e_i\rangle|^2
\end{align}
A: For $1\le j\le n$,
$$
\begin{align}
\left\langle x-\sum_{k=1}^n\langle x,e_k\rangle e_k,x\right\rangle
&=\langle x,x\rangle-\sum_{k=1}^n\langle x,e_k\rangle\langle x,e_k\rangle\\
&=\|x\|^2-\sum_{k=1}^n\langle x,e_k\rangle^2\tag{1}
\end{align}
$$
and
$$
\begin{align}
\left\langle x-\sum_{k=1}^n\langle x,e_k\rangle e_k,e_j\right\rangle
&=\langle x,e_j\rangle-\sum_{k=1}^n\langle x,e_k\rangle\overbrace{\langle e_k,e_j\rangle}^{\delta_{j,k}}\\
&=\langle x,e_j\rangle-\langle x,e_j\rangle\\[9pt]
&=0\tag{2}
\end{align}
$$
Putting together $(1)$ and $(2)$ yields
$$
\begin{align}
\left\|x-\sum_{k=1}^n\langle x,e_k\rangle e_k\right\|^2
&=\left\langle x-\sum_{k=1}^n\langle x,e_k\rangle e_k,x-\sum_{k=1}^n\langle x,e_k\rangle e_k\right\rangle\\[6pt]
&=\|x\|^2-\sum_{k=1}^n\langle x,e_j\rangle^2\tag{3}
\end{align}
$$
A: The main thing: $y = y_1 + \ldots + y_m$ and the $y_i$'s are paiwise orthogonal ( an orthogonal decomposition) then:
$$||y||^2 = ||y_1||^2 + \cdots + ||y_m||^2 $$
It's easy to check that if $e_1$, $\ldots$, $e_n$ is an $\it{orthonormal}$ family then  $x - \sum_{i=1}^n \langle x, e_i \rangle e_i$ is perpendicular to all the $e_i$'s and so we have for any scalars $\delta_i$ the orthogonal decomposition
$$x-\sum \delta_i e_i = \sum_{i=1}^n (\langle x, e_i \rangle - \delta_i)e_i + (x - \sum_{i=1}^n \langle x, e_i \rangle e_i)$$
and  so
$$||x - \sum \delta_i e_i||^2 = \sum_{i=1}^n |\delta_i - \langle x, e_i \rangle|^2 + ||x - \sum_{i=1}^n \langle x, e_i \rangle e_i||^2$$
Therefore $\sum_{i=1}^n \langle x, e_i \rangle e_i$ is the (unique) closest point to $x$ in the span of $e_i$.
