Vectorize outer product of column vectors

algorithmslinear algebraoptimization

Suppose I have a matrix $X \in\mathbb{R}^{n\times p}$
$$
X=
\begin{pmatrix}
{\bf{x}}_1^\top \\
\vdots\\
{\bf{x}}_n^\top
\end{pmatrix}
$$

made of $n$ column vectors ${\bf{x}}_i\in\mathbb{R}^{p\times 1}$. Suppose also I have another vector ${\bf{y}}\in\mathbb{R}^{n\times 1}$ and I want to compute
$$
\sum_{i=1}^n y_i {\bf{x}}_i {\bf{x}}_i^\top
$$

How can I "vectorize" this?

I thought about somehow creating a matrix containing all the outer products and then multiplying this matrix by ${\bf{y}}$ and then summing up the elements. But I'm not sure how to go about it.

My Working

So far I realized the following:
$$
X^\top X =
\begin{pmatrix}
{\bf{x}}_1, \ldots, {\bf{x}}_n\\
\end{pmatrix}
\begin{pmatrix}
{\bf{x}}_1^\top \\
\vdots\\
{\bf{x}}_n^\top
\end{pmatrix} = {\bf{x}}_1{\bf{x}}_1^\top + \ldots +{\bf{x}}_n{\bf{x}}_n^\top = \sum_{i=1}^n {\bf{x}}_i{\bf{x}}_i^\top
$$

Extra Working

I also just realized this.

$$
{\bf{y}}^\top X =
\begin{pmatrix}
y_1 & \cdots & y_n
\end{pmatrix}
\begin{pmatrix}
{\bf{x}}_1^\top \\
\vdots \\
{\bf{x}}_n^\top
\end{pmatrix} = y_1{\bf{x}}_1^\top + \cdots + y_n{\bf{x}}_n^\top
$$

Best Answer

You can write $$ \sum_{i=1}^n y_i {\bf{x}}_i {\bf{x}}_i^\top = X^T \operatorname{diag}(y) X. $$ where $\operatorname{diag}(y)$ is the diagonal matrix whose entries on the diagonal are $y$.

Related Question