Matrix notation for weighted sum of squares

While going through page 1 of Lecture 24: Weighted and Generalized Least Squares [PDF], I got the following questions. Weighted sum of squares is defined as below: $$\sum_{i = 0}^{n}{w_i(Y_i - X_ib)^2}$$ And this could be written in matrix notation as follows $$(Y−Xb)'W(Y−Xb)$$

Only thing I did not get is how $W$ (weights) got into the middle? And why should it be a diagonal matrix?

I get that $\sum_{}x^2$ could be written as $X^T * X$. But how $\sum_{}wx^2$ is written as $X^TWX$ , any clue?

• @RodrigodeAzevedo Thank you for the edits, I get that $\sum_{}x^2$ could be written as $X^T * X$. But how $\sum_{}wx^2$ is written as $X^TWX$ , any clue?? – Sonnu Jul 8 '18 at 0:51
• To weight the rows of a matrix, left-multiply by a diagonal matrix. To weight the columns of a matrix, right-multiply by a diagonal matrix. – Rodrigo de Azevedo Jul 8 '18 at 0:57
• – Rodrigo de Azevedo Jul 8 '18 at 2:19