# What should be the correct result of this product $(Y-XW)^T(Y-XW)$

i usually come across this equation and I am not able to understand it entirely, here X, W and Y are matrices of size N*D, D*1, and N*1 resp. $(Y-XW)^T(Y-XW)$.

As per me it should be equal to $Y^TY-Y^TXW-W^TX^TY+W^TX^TXW$ but actually it comes out to be $Y^TY-2W^TX^TY+W^TX^TXW$. I simply could not find any explanation for this.

The reason this works is because $W^TX^TY$ is a $1 \times 1$ matrix. It is symmetric, and it appears either once transposed and once as is or twice as is.