Spivak's Calculus Chapter 15 Problem 13(b) some manipulation of Integral and trigonometric functions Even though I finished most exercises before this chapter, this Problem 13(b) still like an alien language to me, I can't follow it's answer start from the third line, and have no clue of what the result is showing me, if you have time to read this long problem, please help.

In chapter 15, For later use, Problem 12 gives the results:
$\int_{-\pi}^\pi \sin\,mx\,\sin\,nx\,dx=\begin{cases}0,m\neq n \\ \pi,m=n, \end{cases}$ 
$\int_{-\pi}^\pi \cos\,mx\,\cos\,nx\,dx=\begin{cases}0,m\neq n \\ \pi,m=n,\end{cases}$ 
$\int_{-\pi}^\pi \sin\,mx\,\cos\,nx\,dx=0 $
Problem 13:  
(a)   If $f$ is integrable on $[-\pi,\pi]$, show that the minimum value of $\int_{-\pi}^\pi\bigl( f(x)-a\,\cos\,nx\bigr)^2\,dx$
  occurs when $a=\frac{1}{\pi}\int_{-\pi}^\pi f(x)\,\cos\,nx\,dx$,  
and the minimum value of $\int_{-\pi}^\pi\bigl( f(x)-a\,\sin\,nx\bigr)^2\,dx$
  when $a=\frac{1}{\pi}\int_{-\pi}^\pi f(x)\,\sin\,nx\,dx$
(b)   Define
$$a_n=\frac{1}{\pi}\int_{-\pi}^\pi f(x)\,\cos\,nx\,dx,\qquad n=0,1,2,...$$
$$b_n=\frac{1}{\pi}\int_{-\pi}^\pi f(x)\,\sin\,nx\,dx,\qquad n=1,2,3,...$$ 
Show that if $c_i$ and $d_i$ are any numbers, then
  $$\int_{-\pi}^\pi \biggl(f(x)-\biggr[\frac{c_0}{2}+\sum_{n=1}^N\,c_n\,\cos\,nx+d_n\,\sin\,nx\biggl]\biggr)^2dx  $$
$$=\int_{-\pi}^\pi[f(x)]^2\,dx-2\pi\biggl(\frac{a_0 c_0}{2}+\sum_{n=1}^Na_nc_n+b_nd_n\biggr)+\pi\biggl(\frac{c_0^2}{2}+\sum_{n=1}^N c_n^2+d_n^2\biggr)$$
$$=\int_{-\pi}^\pi[f(x)]^2\,dx-\pi\biggl(\frac{a_0^2}{2}+\sum_{n=1}^Na_n^2+b_n^2\biggr) \qquad\qquad\qquad\qquad\qquad
\qquad\qquad\qquad+\pi\biggl(\Bigl(\frac{c_0}{\sqrt 2}-\frac{a_0}{\sqrt 2}\Bigr)^2+\sum_{n=1}^N(c_n-a_n)^2+(d_n-b_n)^2\biggr),$$
thus showing that the first integral is smallest when $a_i=c_i$ and $b_i=d_i$. In other words, among all "linear combinations" of the functions $s_n(x)=\sin\,nx$ and $c_n(x)=\cos\,nx$ for $1\le n\le N$, the particular function $$g(x)=\frac{a_0}{2}+\sum_{n=1}^Na_n\,\cos\,nx+b_n\,\sin\,nx$$ has the "closest fit" to $f$ on $[-\pi,\pi]$.

In the answer book, answer for problem 13(b) is blow:

$$\int_{-\pi}^\pi\biggl(f(x)-\biggl[\frac{c_0}{2}+\sum_{n=1}^Nc_n\,\cos\,nx+d_n\,\sin\,nx\biggr]\biggr)^2\,dx=$$
$$\int_{-\pi}^\pi[f(x)]^2\,dx-2\int_{-\pi}^\pi f(x)\biggl[\frac{c_0}{2}+\sum_{n=1}^Nc_n\,\cos\,nx+d_n\,\sin\,nx\biggr]\,dx$$
$$+\int_{-\pi}^\pi\biggl[\frac{c_0^2}{4}+\sum_{n=1}^Nc_n^2\,\cos^2\,nx+d_n^2\,\sin^2\,nx\biggr]dx$$
$$+\int_{-\pi}^\pi\sum_{n,m=1}^Nc_nd_m\,\cos\,nx\,\sin\,mx\,dx$$
$$+\int_{-\pi}^\pi c_0\sum_{n=1}^Nc_n \cos\,nx+d_n \sin\,nx\,dx$$
$$=\int_{-\pi}^\pi[f(x)]^2\,dx-2\pi\biggl(\frac{a_0c_0}{2}+\sum_{n=1}^Na_nc_n+b_nd_n\biggr)+\pi\biggl(\frac{c_0^2}{2}+\sum_{n=1}^Nc_n^2+d_n^2\biggl).$$
  using Problem 12, the definition of $a_n$ and $b_n$, and the fact that the last integral vanishes because $\int_{-\pi}^\pi \cos\,nx\,dx=\int_{-\pi}^\pi \sin\,nx\,dx=0$,the second equality follows by algebra.

I can't understand the algebra start from the third line of the answer, and can't see any meaning from the resulted equation. can you give me more details than the answer book? Thank so much! I don't think I can understand it by myself for now.
 A: There is a general rule of summations which shows that $\displaystyle\left(\sum_{i=1}^n \gamma_i\right)^2=\sum_{i=1}^n \left(\gamma_i\right)^2+2\sum_{1\leq i\lt j}^n \left(\gamma_i\gamma_j\right) \quad (\dagger_1)$. You can intuitively understand this rule by visualizing a square matrix with columns $\gamma_1,\gamma_2,\cdots,\gamma_n$ and rows $\gamma_1,\gamma_2,\cdots,\gamma_n$. Letting $\gamma_i=c_i\cos(ix)+d_i\sin(ix)$, we can then apply $(\dagger_1)$ to determine $\displaystyle\left(\sum_{i=1}^n c_i\cos(ix)+d_i\sin(ix)\right)^2$. Skipping some preliminary algebra, we have:
\begin{align}=\left[\sum_{n=1}^N c_n^2\cos^2(nx)+d_n^2\sin^2(nx)\right]+\left[\color{red}{2\sum_{n=1}^Nc_n\cos(nx)d_n\sin(nx)}\right]+\left[\color{red}{2\sum_{1\leq i \lt j}^Nc_i\cos(ix)d_j\sin(jx)+c_j\cos(jx)d_i\sin(ix)}\right]+\left[2\sum_{1\leq i\lt j}^Nc_ic_j\cos(ix)\cos(jx)+d_id_j\sin(ix)\sin(jx)\right] \end{align}
The $\color{red}{\text{red}}$ terms can be combined (use a matrix with columns $d_1\sin(1x),d_2\sin(2x),\cdots,d_N\sin(Nx)$ and rows $c_1\cos(1x),c_2\cos(2x),\cdots,c_N\cos(Nx)$ to understand this equivalence) which gives us:
\begin{align}=\left[\sum_{n=1}^N c_n^2\cos^2(nx)+d_n^2\sin^2(nx)\right]+\left[2\sum_{1\leq n,1\leq m}^Nc_nd_m\cos(nx)\sin(mx)\right]+\left[\color{blue}{2\sum_{1\leq i\lt j}^Nc_ic_j\cos(ix)\cos(jx)+d_id_j\sin(ix)\sin(jx)}\right]\end{align}
Note that the indexing for the $\color{blue}{\text{blue}}$ terms (i.e. $1 \leq i \lt j$) allows us to apply our findings  from Problem 12. Specifically, we know that if $i \neq j$, then $\displaystyle \int_{-\pi}^{\pi}\sin(ix)\sin(jx)dx=0$ and $\displaystyle \int_{-\pi}^{\pi}\cos(ix)\cos(jx)dx=0$. Of course, then, we must have that $\displaystyle\int_{-\pi}^{\pi}\left[2\sum_{1\leq i\lt j}^Nc_ic_j\cos(ix)\cos(jx)+d_id_j\sin(ix)\sin(jx)\right]dx=0$. Therefore, we need only consider the following terms:
\begin{align}=\left[\sum_{n=1}^N c_n^2\cos^2(nx)+d_n^2\sin^2(nx)\right]+\left[\color{green}{2\sum_{1\leq n,1\leq m}^Nc_nd_m\cos(nx)\sin(mx)}\right]\end{align}
Note that Problem 12 also lets us claim that the $\color{green}{\text{green}}$ terms can be ignored because for any $n,m \in \mathbb N$, we have that $\displaystyle \int_{-\pi}^{\pi}\sin(mx)\cos(nx)dx=0$. Therefore, we must have that $\displaystyle \int_{-\pi}^{\pi}\left[2\sum_{1\leq n,1\leq m}^Nc_nd_m\cos(nx)\sin(mx)\right]dx=0$

Using our above work, we see that to determine what $\displaystyle \int_{-\pi}^{\pi} \left(f(x)-\left[\frac{c_0}{2}+\sum_{n=1}^N\left(c_n\cos(nx)+d_n\sin(nx)\right)\right]\right)^2dx$ evaluates to, we only need to track the following terms:

$\displaystyle \int_{-\pi}^{\pi}f(x)^2dx-2\int_{-\pi}^{\pi}f(x)\cdot \left[\frac{c_0}{2}+\sum_{n=1}^N\left(c_n\cos(nx)+d_n\sin(nx)\right)\right]dx+\int_{-\pi}^{\pi}\frac{c_0^2}{4}dx+\int_{-\pi}^{\pi}\left[\sum_{n=1}^N c_n^2\cos^2(nx)+d_n^2\sin^2(nx)\right]dx+\int_{-\pi}^{\pi}\left[c_0\cdot\left(\sum_{n=1}^N c_n\cos(nx)+d_n\sin(nx)\right)\right]dx$

As the answer key states (although we have not yet proven this formally anywhere in the Spivak exercises yet), for any $n \in \mathbb N: \displaystyle \int_{-\pi}^{\pi} \cos(nx)=\int_{-\pi}^{\pi} \sin(nx)=0$. This means that we can further refine the above expression to:
$$\int_{-\pi}^{\pi}f(x)^2dx-2\int_{-\pi}^{\pi}f(x)\cdot \left[\frac{c_0}{2}+\sum_{n=1}^N\left(c_n\cos(nx)+d_n\sin(nx)\right)\right]dx+\int_{-\pi}^{\pi}\frac{c_0^2}{4}dx+\int_{-\pi}^{\pi}\left[\sum_{n=1}^N c_n^2\cos^2(nx)+d_n^2\sin^2(nx)\right]dx$$
In order to see the next round of simplifications, it will be easier if we expand some terms.

The first term we will  expand is: $\displaystyle -2\int_{-\pi}^{\pi}f(x)\cdot \left[\frac{c_0}{2}+\sum_{n=1}^N\left(c_n\cos(nx)+d_n\sin(nx)\right)\right]dx$
We proceed as follows:
\begin{align}&=-2\int_{-\pi}^{\pi}\frac{f(x)c_0}{2}dx-2\int_{-\pi}^{\pi}\left[\sum_{n=1}^N\left(c_n f(x)\cos(nx)+d_nf(x)\sin(nx)\right)\right]dx \\&=-2\int_{-\pi}^{\pi}\frac{f(x)c_0}{2}dx-2\int_{-\pi}^{\pi}\left[c_1f(x)\cos(1\cdot x)+\cdots +c_Nf(x)\cos(Nx)+d_1f(x)\sin(1\cdot x)+\cdots+d_N\sin(Nx)\right]dx\end{align}
Recall from the problem prompt how $a_n$ and $b_n$ are defined.  Further, note that the indexing for the '$n$' in the context of $a_n$ and $b_n$ extends down to $n=0$. Given that $a_0=\displaystyle \frac{1}{\pi}\int_{-\pi}^{\pi}f(x)\cos(0)dx=\frac{1}{\pi}\int_{-\pi}^{\pi}f(x)dx$, we can rewrite the above expression as:
\begin{align}=-2\pi a_0\cdot \frac{c_0}{2}-2\pi\left(c_1a_1+\cdots+c_Na_N+d_1b_1+\cdots+d_Nb_N\right)\end{align}
, which the text book writes as: $\displaystyle -2\pi\left(\frac{a_0c_0}{2}+\sum_{n=1}^{N}a_nc_n+b_nd_n\right)$

The second term we will expand is:
$$\int_{-\pi}^{\pi}\left[\sum_{n=1}^N c_n^2\cos^2(nx)+d_n^2\sin^2(nx)\right]dx$$, which we can rewrite as:
\begin{align}&=\int_{-\pi}^{\pi}\left[c_1^2\cos(1\cdot x)\cos(1\cdot x)+\cdots+c_N^2\cos(Nx)\cos(Nx)+d_1\sin(1\cdot x)\sin(1\cdot x)+\cdots+d_N\sin(Nx)\sin(Nx) \right]\end{align}
Given that the integral will distribute to each summand, we can apply our findings from problem 12 one last time. Specifically, for all $n,m \in \mathbb N:$ if $n=m$, then we have that  $\displaystyle \int_{-\pi}^{\pi}\cos(nx)\cos(mx)dx=\int_{-\pi}^{\pi}\sin(nx)\sin(mx)=\pi$.
This means that our above expression simplifies to:
\begin{align}&=\pi c_1^2+\cdots+\pi c_N^2+\pi d_1^2+\cdots+\pi d_N^2\end{align}, which the book writes as $\displaystyle \pi\left(\sum_{n=1}^Nc_n^2+d_n^2\right)$.

The final term to consider is $\displaystyle \int_{-\pi}^{\pi}\frac{c_0^2}{4}dx$. Given that this is just a constant, we know that this evaluates to $2\pi\cdot\frac{c_0^2}{4}=\pi \frac{c_0^2}{2} $.
With all of the above work, we can finally rewrite the below equation:
$$\int_{-\pi}^{\pi}f(x)^2dx-2\int_{-\pi}^{\pi}f(x)\cdot \left[\frac{c_0}{2}+\sum_{n=1}^N\left(c_n\cos(nx)+d_n\sin(nx)\right)\right]dx+\int_{-\pi}^{\pi}\frac{c_0^2}{4}dx+\int_{-\pi}^{\pi}\left[\sum_{n=1}^N c_n^2\cos^2(nx)+d_n^2\sin^2(nx)\right]dx$$
into the form used by the book, which is:

$$\int_{-\pi}^{\pi}f(x)^2dx-2\pi\left(\frac{a_0c_0}{2}+\sum_{n=1}^{N}a_nc_n+b_nd_n\right)+\pi\left(\frac{c_0^2}{2}+\sum_{n=1}^Nc_n^2+d_n^2\right)$$

To get this expression in its final form just amounts to some fairly contrived algebraic manipulations.
