Is there a way to compute the weighted vector inner product xAy with vectors x and y and Matrix A using BLAS/LAPACK while avoiding additional allocations or overwriting the inputs?

I'm happy with answers that assume A is symmetric or x=y.


1 Answer 1


The product $x^TAy$ can be calculated as:

$\sum_{i}\sum_j x_i A_{i,j} y_j$.

Easily implemented as a double for loop. You can additionally avoid a few flops by doing either:

$\sum_{i}x_i \sum_j A_{i,j} y_j$ or

$\sum_{j}y_j\sum_i x_i A_{i,j} $.

You asked to use BLAS, but note that this is a level 2 BLAS routine, which is not actually that much faster than two for/do loops. Some exceptions:

  • Your matrix is huge, meaning you can parallelize
  • Your for loops violate spatial locality.
  • You are using an accelerator

I am not aware of this functionality currently existing in BLAS.

  • $\begingroup$ What's an accelerator? $\endgroup$
    – Bananach
    Commented Oct 19, 2023 at 19:49
  • 2
    $\begingroup$ mostly GPU, but it could also be some other specialized piece of hardware. $\endgroup$ Commented Oct 19, 2023 at 20:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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