[Math] commutative vector matrix multiplication

matricesvectors

I've come across the following derivation:

$$n^{-2} X^T \mathbf 1 \mathbf 1^{T}X = n^{-1} X^T \mathbf 1 n^{-1}X^T \mathbf 1$$

where $\mathbf 1$ is a $n \times 1$ vector and $\mathbf X$ a $n \times p$ matrix

My question is why can you just transpose $\mathbf X$ and $\mathbf 1$ and change their order? Isn't matrix multiplication not commutative?

So my derivation would be:

$$n^{-2} X^T \mathbf 1 \mathbf 1^{T}X = n^{-1} n^{-1} X^T \mathbf 1 \mathbf 1^TX = n^{-1} X^T \mathbf 1 n^{-1} \mathbf 1^T X $$

which then requires to flip $\mathbf 1'$ and $\mathbf X$ and transpose it to end up with the desired result.

EDIT:

Sorry for the confusion, I made a mistake and totally didn't see the second transpose in the book. So the derivation is correct and the "trick" they used is the one mentioned by @blue. Thanks a lot!
Here's the correct formula:

$$n^{-2} X^T \mathbf 1 \mathbf 1^{T}X = (n^{-1} X^T \mathbf 1) (n^{-1}X^T \mathbf 1)^T$$

and the way to get there:

$$n^{-2} X^T \mathbf 1 \mathbf 1^{T}X = n^{-1} n^{-1} X^T \mathbf 1 \mathbf 1^TX = n^{-1} X^T \mathbf 1 (n^{-1} \mathbf 1^T X)^{TT} = n^{-1} X^T \mathbf 1 (n^{-1} (\mathbf 1^T X)^T)^{T} = (n^{-1} X^T \mathbf 1 )(n^{-1} X^T \mathbf 1)^{T}$$

Best Answer

I'm not sure from the notation you are using but I think this uses the fact that the product of the transpose of an $n$-vector and another vector is a real number. You can transpose a real number without changing it, thus $x^T y = (x^T y)^T = y^T x$ for pair of vectors $x$, $y$.