# Efficient (possibly preallocated) chain of Kronecker products

**URL:** https://discourse.julialang.org/t/efficient-possibly-preallocated-chain-of-kronecker-products/29328
**Category:** Numerics
**Tags:** question
**Created:** [September 30, 2019, 2:52pm UTC](https://discourse.julialang.org/t/efficient-possibly-preallocated-chain-of-kronecker-products/29328 "2019-09-30T14:52:08Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [September 30, 2019, 2:52pm UTC](https://discourse.julialang.org/t/efficient-possibly-preallocated-chain-of-kronecker-products/29328/1 "2019-09-30T14:52:08Z")

</div>

I need to calculate something not unlike

```julia
reduce(kron, As...)

```

where `As` are 3–8 matrices. If it helps, I can preallocate for the output. Elements of `As` are dense, the length of `As` is known at compile time (unrolling is possible), etc.

I am wondering if there is an efficient algorithm for this, and whether this is implemented in any library.

Preliminary pencil-and-paper calculations suggest that a lot of interim results can be reused, and I can traverse efficiently with a `CartesianIndices`, but I thought I would ask here before reinventing the wheel (if there is a good algorithm but it is not yet implemented, I will implement it and make it publicly available in a package).

---

<div class="post-metadata">

### Author: ![jkbest2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jkbest2/32/7350_2.png) [@jkbest2](https://discourse.julialang.org/u/jkbest2)
#### Post date: [September 30, 2019, 3:39pm UTC](https://discourse.julialang.org/t/efficient-possibly-preallocated-chain-of-kronecker-products/29328/2 "2019-09-30T15:39:01Z")

</div>

Maybe [Kronecker.jl](https://github.com/MichielStock/Kronecker.jl) would be helpful?

---

<div class="post-metadata">

### Author: ![MichelJuillard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/micheljuillard/32/10555_2.png) [@MichelJuillard](https://discourse.julialang.org/u/MichelJuillard)
#### Post date: [September 30, 2019, 8:10pm UTC](https://discourse.julialang.org/t/efficient-possibly-preallocated-chain-of-kronecker-products/29328/3 "2019-09-30T20:10:32Z")

</div>

I have some work in progress here: [https://github.com/MichelJuillard/dynare.jl/blob/master/src/linalg/KroneckerUtils.jl](https://github.com/MichelJuillard/dynare.jl/blob/master/src/linalg/KroneckerUtils.jl). It isn’t entirely optimized., Maybe it is of help. The original reference is [https://www.cnb.cz/export/sites/cnb/en/economic-research/.galleries/research\_publications/cnb\_wp/cnbwp\_2005\_10.pdf](https://www.cnb.cz/export/sites/cnb/en/economic-research/.galleries/research_publications/cnb_wp/cnbwp_2005_10.pdf) Look at Appendix C p. 18.
