# Efficient calculation of block matrices with only the Linear Map given

**URL:** <https://discourse.julialang.org/t/efficient-calculation-of-block-matrices-with-only-the-linear-map-given/85814>\
**Category:** Specific Domains\
**Tags:** linearalgebra\
**Created:** [August 16, 2022, 9:30am UTC](https://discourse.julialang.org/t/efficient-calculation-of-block-matrices-with-only-the-linear-map-given/85814 "2022-08-16T09:30:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![roflmaostc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roflmaostc/32/30123_2.png) [@roflmaostc](https://discourse.julialang.org/u/roflmaostc)\
**Post date:** [August 16, 2022, 9:30am UTC](https://discourse.julialang.org/t/efficient-calculation-of-block-matrices-with-only-the-linear-map-given/85814/1 "2022-08-16T09:30:16Z")

</div>

Hi!

I have the situation that I construct block matrices where I want to avoid to explicitly have them.  
For example, with Fourier transforms we can either use slow matrices or fast FFTs:

```julia
using FFTW
using LinearAlgebra

z = zeros((2,2))
# my true vectors are 1_000_000 elements long
x1 = randn((2,))
x2 = randn((2,))

# FFT plan
p = plan_fft(x1)
p_mat = fft(I(2), (1,))

# slow mat mul
M = [p_mat z; z p_mat]
res1 = M * [x1; x2] 

# fast FFT
res2 = [p * x1; p * x2]

#### 
julia> res1 == res2
true

```

The issue is that my matrices are not necessarily block diagonals, it can be any kind of diagonal matrix. Also my matrix is not quadratic.

And finally I would like to invert the matrix `M` which obviously does not work straightforwardly since I can’t access matrix elements but only the forward pass `M * x`.  
But maybe we can calculate the pseudoinverse because we should be able to calculate `M' *x`?

Is there any kind of package allowing to generate the block matrices and efficient calculations?  
For example, it probably is perfect if I could write: `(@efficient_block_matrix [p 0; 0 p]) * [x1; x2]`

Best,

Felix

_Existing packages:_

- [BlockArrays.jl](https://github.com/JuliaArrays/BlockArrays.jl) allowing only dense matrices
- [BandedMatrices.jl](https://github.com/JuliaMatrices/BandedMatrices.jl) same here

---

<div class="post-metadata">

**Author:** ![SteffenPL](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steffenpl/32/206270_2.png) [@SteffenPL](https://discourse.julialang.org/u/SteffenPL)\
**Post date:** [August 16, 2022, 9:51am UTC](https://discourse.julialang.org/t/efficient-calculation-of-block-matrices-with-only-the-linear-map-given/85814/2 "2022-08-16T09:51:21Z")

</div>

Would something like [Home · LinearMaps.jl](https://julialinearalgebra.github.io/LinearMaps.jl/stable/) help? There are also some types which look suitable: [Types and methods · LinearMaps.jl](https://julialinearalgebra.github.io/LinearMaps.jl/stable/types/#BlockMap-and-BlockDiagonalMap)  
(Maybe there is a more suitable package, but with LinearMaps you can define yourself how the linear map is defined and it plays along well with other packages.)

---

<div class="post-metadata">

**Author:** ![roflmaostc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roflmaostc/32/30123_2.png) [@roflmaostc](https://discourse.julialang.org/u/roflmaostc)\
**Post date:** [August 16, 2022, 9:55am UTC](https://discourse.julialang.org/t/efficient-calculation-of-block-matrices-with-only-the-linear-map-given/85814/3 "2022-08-16T09:55:33Z")

</div>

Thanks!  
Yeah, I forgot to add LinearMaps.jl here.

Yes, it kind of helps but I still would need to implement the matching of the blocks with the vectors, wouldn’t I? (not super hard).

---

<div class="post-metadata">

**Author:** ![SteffenPL](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steffenpl/32/206270_2.png) [@SteffenPL](https://discourse.julialang.org/u/SteffenPL)\
**Post date:** [August 16, 2022, 10:04am UTC](https://discourse.julialang.org/t/efficient-calculation-of-block-matrices-with-only-the-linear-map-given/85814/4 "2022-08-16T10:04:03Z")

</div>

I’m not sure if I understand your intended application, but are you looking for something like this type:

> **[GitHub - SebastianAment/BlockFactorizations.jl: This package contains a data...](https://github.com/SebastianAment/BlockFactorizations.jl#block-matrix-with-diagonal-blocks)**
>
> This package contains a data structure that wraps a matrix of matrices or factorizations and acts like the matrix resulting from concatenating the input matrices without allocating further memory. ...

---

<div class="post-metadata">

**Author:** ![dkarrasch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dkarrasch/32/7410_2.png) [@dkarrasch](https://discourse.julialang.org/u/dkarrasch)\
**Post date:** [August 20, 2022, 11:31am UTC](https://discourse.julialang.org/t/efficient-calculation-of-block-matrices-with-only-the-linear-map-given/85814/5 "2022-08-20T11:31:31Z")

</div>

No, you don’t, that’s all included.
