Hi, i’m trying to teach my self machine learning by going through the book “An introduction to Statistical Learning”, and got stuck on one of the exercise questions.

In the attached image above, why am i able to move the summation sign down from the numerator(line 1) to have it apply to the whole term (line 2 and line3)?

My understanding of line 1 is that $$X_i$$ is to be multiplied by $$\frac {\sum_{i=1}^n X_iY_i} {\sum_{i=1}^n X_i^2}$$

However, i see that on line 2, the summation is moved down, and becomes $$\sum_{i=1}^n \frac{(X_iY_i)X_i}{\sum_{i=1}^n X_i^2}$$

My first question is:

Is $$X_i\frac {\sum_{i=1}^n X_iY_i} {\sum_{i=1}^n X_i^2}$$ equivalent to $$\sum_{i=1}^n \frac{(X_iY_i)X_i}{\sum_{i=1}^n X_i^2}$$ and that moving $$\sum_{i=1}^n$$ down from the numerator in line 1 to how it is in line 2 does not change anything? I was under the impression that the meaning of the equation on line 2 means to sum up all instances of $$\frac{(X_iY_i)X_i}{\sum_{i=1}^n X_i^2}$$ while the equation on line 1 is to sum up $$(X_iY_i)$$ and then multiply it by $$X_i$$ which are 2 different things?

My 2nd question is if shifting $$Y_i$$ down from the numerator is equivalent to just rewriting (5 x 3 x 2) as (5 x 2 x 3)?

For ease of reference, the question is as follows:-

and the answer i’m referring to is as follows: