# How to vectorize outer product of column vectors?

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$$

• maybe $X^\top X$ could be useful? Jan 4, 2020 at 22:26
• The formula you got is the best you're going to get unless you are interested in speeding up matrix-vector products Jan 4, 2020 at 23:18
• 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$. This is not usually what the term "vectorize" refers to, but perhaps this is the kind of thing you're looking for Jan 4, 2020 at 23:40
• Also, your equation involving $y^TX$ is incorrect Jan 4, 2020 at 23:41
• The edit makes sense, but now your latter expression involving $\tilde y$ doesn't make sense Jan 5, 2020 at 0:09

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$$.
• @RodrigodeAzevedo ${\bf{x}}_i$ is a column vector, meaning that it has dimension $p \times 1$. However, each ${\bf{x}}_i$, when transposed is a row of the matrix $X$. Jan 5, 2020 at 15:11