1
$\begingroup$

Let's say I have a $N$ $d \times 1$ (column) vectors, $x_1, x_2,\dots,x_N$, and a $d \times d$ matrix, $A$ ($A$ can probably be symmetric and positive definite if it helps somehow). I want to end up with the ($N \times 1$) vector $$ v = \begin{bmatrix} x_1^T A x_1 \\ x_2^T A x_2 \\ \vdots \\ x_N^T A x_N\end{bmatrix}$$

I know that one way to do this is to just define a ($d \times N$) matrix $$ X = \begin{bmatrix}x_1, x_2, \dots, x_N\end{bmatrix}$$ so that each of my $x_i$ vectors is just a column of $X$ and then calculate $$ v = diag(X^TAX) $$

But it seems like calculating all of the off-diagonal values is kind of a waste (I'm doing this a lot on a computer). Is there a more clever way to calculate $v$?

$\endgroup$
1
$\begingroup$

If you can do Hadamard product in your programming language, then compute the Hadamard product $B$ of $AX$ and $X$, where $X$ is the matrix of your $x_i$ vectors. Then multiplying $B$ by a vector of all $1$s should given you what you want.

$\endgroup$
  • $\begingroup$ I learned a new term today! Hadamard product! $\endgroup$ – dang Mar 10 '15 at 15:31
  • $\begingroup$ Also, this is exactly what I was looking for. Thanks! $\endgroup$ – dang Mar 10 '15 at 15:31

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.