# Block sparse matrix multiplication with repeated blocks

**URL:** <https://discourse.julialang.org/t/block-sparse-matrix-multiplication-with-repeated-blocks/38107>\
**Category:** Numerics\
**Created:** [April 23, 2020, 9:06pm UTC](https://discourse.julialang.org/t/block-sparse-matrix-multiplication-with-repeated-blocks/38107 "2020-04-23T21:06:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jlchan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlchan/32/10958_2.png) [@jlchan](https://discourse.julialang.org/u/jlchan)\
**Post date:** [April 23, 2020, 9:06pm UTC](https://discourse.julialang.org/t/block-sparse-matrix-multiplication-with-repeated-blocks/38107/1 "2020-04-23T21:06:59Z")

</div>

I’m trying to find a good way to compute V^TAV where A is a sparse CSC matrix A and V = I \otimes B is a block diagonal matrix with repeating dense blocks (size of B is around \mathbb{R}^{20 \times 10}).

Looping through blocks A\_i and computing B^TA\_iB seems about 2x slower than just storing V as a sparse matrix, but I got about 2x speedup using dense matrix operations by popping A into a dense array \tilde{A}

 ![X X XX](https://global.discourse-cdn.com/julialang/original/3X/3/4/3441838fea5b4438c64d5cbeaafd8a798e6d0122.png)  
and BLAS-3 multiplying \tilde{A} by B^T, B using `reshape` and `permutedims`.

This seems a little awkward to me, and I’m wondering if there are nicer “Julian” solutions that I’ve overlooked.

---

<div class="post-metadata">

**Author:** ![dlfivefifty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlfivefifty/32/1959_2.png) [@dlfivefifty](https://discourse.julialang.org/u/dlfivefifty)\
**Post date:** [April 24, 2020, 7:44am UTC](https://discourse.julialang.org/t/block-sparse-matrix-multiplication-with-repeated-blocks/38107/2 "2020-04-24T07:44:50Z")

</div>

You could look at BlockArrays.jl, BlockDiagonals.jl, BlockBandedMatrices.jl to see if they help.

---

<div class="post-metadata">

**Author:** ![jlchan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlchan/32/10958_2.png) [@jlchan](https://discourse.julialang.org/u/jlchan)\
**Post date:** [April 25, 2020, 10:49pm UTC](https://discourse.julialang.org/t/block-sparse-matrix-multiplication-with-repeated-blocks/38107/3 "2020-04-25T22:49:41Z")

</div>

Thanks @dlfivefifty - I actually started with BlockArrays, and the convenient notation and functionality helped me get things working faster. I switched back to SparseMatrixCSC because it was faster without the conversion from BlockArrays (even after adapting [this trick](https://github.com/JuliaArrays/BlockArrays.jl/issues/78)).

---

<div class="post-metadata">

**Author:** ![dlfivefifty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlfivefifty/32/1959_2.png) [@dlfivefifty](https://discourse.julialang.org/u/dlfivefifty)\
**Post date:** [April 26, 2020, 5:43am UTC](https://discourse.julialang.org/t/block-sparse-matrix-multiplication-with-repeated-blocks/38107/4 "2020-04-26T05:43:11Z")

</div>

For that size of matrix you are unlikely to beat dense BLAS I think

---

<div class="post-metadata">

**Author:** ![jlchan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlchan/32/10958_2.png) [@jlchan](https://discourse.julialang.org/u/jlchan)\
**Post date:** [April 26, 2020, 6:51pm UTC](https://discourse.julialang.org/t/block-sparse-matrix-multiplication-with-repeated-blocks/38107/5 "2020-04-26T18:51:09Z")

</div>

Thanks! Appreciate the feedback.
