# Simultaneous left and right multiplication of a matrix

**URL:** <https://discourse.julialang.org/t/simultaneous-left-and-right-multiplication-of-a-matrix/44823>\
**Category:** Numerics\
**Created:** [August 12, 2020, 8:33pm UTC](https://discourse.julialang.org/t/simultaneous-left-and-right-multiplication-of-a-matrix/44823 "2020-08-12T20:33:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mleprovost](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mleprovost/32/7166_2.png) [@mleprovost](https://discourse.julialang.org/u/mleprovost)\
**Post date:** [August 12, 2020, 8:33pm UTC](https://discourse.julialang.org/t/simultaneous-left-and-right-multiplication-of-a-matrix/44823/1 "2020-08-12T20:33:02Z")

</div>

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.

```julia
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)

```

---

<div class="post-metadata">

**Author:** ![simonbyrne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simonbyrne/32/19_2.png) [@simonbyrne](https://discourse.julialang.org/u/simonbyrne)\
**Post date:** [August 12, 2020, 11:09pm UTC](https://discourse.julialang.org/t/simultaneous-left-and-right-multiplication-of-a-matrix/44823/2 "2020-08-12T23:09:41Z")

</div>

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

```julia
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)
