# How to calculate a vector of quadratic forms (matrix algebra)

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

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.