Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 687
user541686 Avatar asked Feb 17 '15 18:02

user541686


1 Answers

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]
like image 176
Tokkot Avatar answered Sep 28 '22 08:09

Tokkot