1
$\begingroup$

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

$\endgroup$
9
  • $\begingroup$ maybe $X^\top X$ could be useful? $\endgroup$ Jan 4, 2020 at 22:26
  • $\begingroup$ The formula you got is the best you're going to get unless you are interested in speeding up matrix-vector products $\endgroup$
    – whpowell96
    Jan 4, 2020 at 23:18
  • 1
    $\begingroup$ 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 $\endgroup$ Jan 4, 2020 at 23:40
  • 1
    $\begingroup$ Also, your equation involving $y^TX$ is incorrect $\endgroup$ Jan 4, 2020 at 23:41
  • 1
    $\begingroup$ The edit makes sense, but now your latter expression involving $\tilde y$ doesn't make sense $\endgroup$ Jan 5, 2020 at 0:09

1 Answer 1

2
$\begingroup$

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

$\endgroup$
2
  • $\begingroup$ @R A row, as in the question $\endgroup$ Jan 5, 2020 at 14:46
  • $\begingroup$ @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$. $\endgroup$ Jan 5, 2020 at 15:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.