How to vectorize the evaluation of a quadratic form (x' * A * x)?

matlab, matrix, vectorization

Solution

One way to think about it is that you are trying to take a bunch of dot products between the vectors in `X` and the vectors in `AX`. Matlab has a function for that:

N = 10; % number of x's
M = 100; % length of x's
X = rand(M,N);
A = rand(M, M);

% way 1
way1 = diag(X' * A * X);

% way 2
way2 = dot(X, A*X)';

% compare
[way1 way2]

Problem

If I have a matrix `A` and I want to evaluate `x' * A * x` for multiple values of `x`, how can I vectorize this? (I could do `X' * A * X` and take the diagonal, but this is clearly inefficient.)

Original source