# What's the most efficient way to compute the matrix product A'\*B\*A for general B?

**URL:** https://discourse.julialang.org/t/whats-the-most-efficient-way-to-compute-the-matrix-product-a-b-a-for-general-b/79821
**Category:** Numerics
**Tags:** linearalgebra
**Created:** [April 22, 2022, 9:00am UTC](https://discourse.julialang.org/t/whats-the-most-efficient-way-to-compute-the-matrix-product-a-b-a-for-general-b/79821 "2022-04-22T09:00:11Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)
#### Post date: [April 22, 2022, 9:00am UTC](https://discourse.julialang.org/t/whats-the-most-efficient-way-to-compute-the-matrix-product-a-b-a-for-general-b/79821/1 "2022-04-22T09:00:11Z")

</div>

The matrix `A` has orthonormal columns, but is rectangular (tall). Currently, I allocate caches `C` and `D`, and evaluate

```julia
mul!(D, A', mul!(C, B, A)

```

In my use case, `A` and `B` are somewhat large matrices, so I’d ideally like to avoid allocating the intermediate matrix `C`. I was wondering if there’s a way to use the structure of `A` to come up with a smarter algorithm? Unfortunately, `B` is a general (maybe complex) matrix, and doesn’t have an obvious structure.

---

<div class="post-metadata">

### Author: ![jd-foster](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jd-foster/32/35824_2.png) [@jd-foster](https://discourse.julialang.org/u/jd-foster)
#### Post date: [April 22, 2022, 11:00am UTC](https://discourse.julialang.org/t/whats-the-most-efficient-way-to-compute-the-matrix-product-a-b-a-for-general-b/79821/2 "2022-04-22T11:00:22Z")

</div>

By your setup, we know that B is square, but we can’t assume anything about symmetry?

Is A very “thin” relative to B? Could it be feasible to compute a truncated SVD to the number if columns of A, then transform the singular vectors by A?

---

<div class="post-metadata">

### Author: ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)
#### Post date: [April 22, 2022, 11:12am UTC](https://discourse.julialang.org/t/whats-the-most-efficient-way-to-compute-the-matrix-product-a-b-a-for-general-b/79821/3 "2022-04-22T11:12:07Z")

</div>

A and B are roughly similar-sized (eg. if `B` is 3000 x 3000, `A` is 3000 x 2880). B is indeed square, but there’s no symmetry.

---

<div class="post-metadata">

### Author: ![fph](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fph/32/17159_2.png) [@fph](https://discourse.julialang.org/u/fph)
#### Post date: [April 23, 2022, 8:33am UTC](https://discourse.julialang.org/t/whats-the-most-efficient-way-to-compute-the-matrix-product-a-b-a-for-general-b/79821/4 "2022-04-23T08:33:36Z")

</div>

Do you have to compute each product only once, or multiple times with the same B (e.g. A\_i^\* B A\_i for i=1,2,\dots) or with the same A?

---

<div class="post-metadata">

### Author: ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)
#### Post date: [April 23, 2022, 9:34am UTC](https://discourse.julialang.org/t/whats-the-most-efficient-way-to-compute-the-matrix-product-a-b-a-for-general-b/79821/5 "2022-04-23T09:34:31Z")

</div>

I do need to compute the product multiple times for different `B`, but the same `A`.

---

<div class="post-metadata">

### Author: ![JM\_Beckers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jm_beckers/32/22482_2.png) [@JM\_Beckers](https://discourse.julialang.org/u/JM_Beckers)
#### Post date: [April 23, 2022, 11:39am UTC](https://discourse.julialang.org/t/whats-the-most-efficient-way-to-compute-the-matrix-product-a-b-a-for-general-b/79821/6 "2022-04-23T11:39:13Z")

</div>

If you need to perform the multiplications really a lot of times AND matrix A has an SVD decomposition A = U S V’ which can be truncated without altering the representation of A, then you might try to pay the price for the SVD decomposition and gain during the calculation of A’B A since there you multiply smaller matrices.
