Simultaneous left and right multiplication of a matrix

Hello,

I am computing a positive definite matrix H (in my case it is a Hessian) and I would like to transform it into another basis according to P^THP with P an upper triangular matrix. Is there an optimized routine to do this, i.e. better than mul!(H, P', H); mul!(H, H, P)

The construction of H and P is not relevant here, it’s just to have a working example.

using LinearAlgebra
using Statistics
X = randn(1000, 200)
Y = randn(1000, 200)

H = cov(X)+I

P = UpperTriangular(qr(cov(Y)+I).R)

mul!(H, P', H)
mul!(H, H, P)

Note that you shouldn’t alias H in mul!: instead use rmul! and lmul!:

lmul!(P', H)
rmul!(H, P)

There isn’t a way to do this that I know of, but if you want the Cholesky factor of P^THP, then you could just apply P directly to the Cholesky factor U of H = U^TU, since P^T H P = (UP)^T (UP)

1 Like