# Broadcasting for a block diagonal matrix

**URL:** <https://discourse.julialang.org/t/broadcasting-for-a-block-diagonal-matrix/34024>\
**Category:** General Usage\
**Created:** [January 31, 2020, 12:17pm UTC](https://discourse.julialang.org/t/broadcasting-for-a-block-diagonal-matrix/34024 "2020-01-31T12:17:06Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![teored90](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/teored90/32/5142_2.png) [@teored90](https://discourse.julialang.org/u/teored90)\
**Post date:** [January 31, 2020, 12:17pm UTC](https://discourse.julialang.org/t/broadcasting-for-a-block-diagonal-matrix/34024/1 "2020-01-31T12:17:06Z")

</div>

I would like to implement a more efficient broadcast for basic arithmetic operators for the `BlockDiagonal` type of [BlockDiagonals.jl](https://github.com/invenia/BlockDiagonals.jl). At the moment the broadcasting causes many allocations, I suppose because it falls back to base broadcasting.

```julia
julia> using BlockDiagonals
using BenchmarkTools

N1, N2, N3 = 3, 4, 5
N = N1 + N2 + N3

b1 = BlockDiagonal([rand(N1, N1), rand(N2, N2), rand(N3, N3)])
b2 = BlockDiagonal([rand(N1, N1), rand(N2, N2), rand(N3, N3)])

julia> @btime b3 = $(b1) .* $(b2);
  39.218 μs (867 allocations: 90.19 KiB)

julia> @btime b3 = $(Matrix(b1)) .* $(Matrix(b2));
  201.480 ns (1 allocation: 1.22 KiB)

```

The idea would be that when `b1` and `b2` have compatible block sizes, all these operations should be done block by block.

I have read the documentation on customizing overloading, but I have to say that I’m a bit lost. I have never dealt with custom types in Julia. Can someone point me to how this can be done in a neat way, or show a similar example?

---

<div class="post-metadata">

**Author:** ![Juan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juan/32/7657_2.png) [@Juan](https://discourse.julialang.org/u/Juan)\
**Post date:** [January 31, 2020, 7:18pm UTC](https://discourse.julialang.org/t/broadcasting-for-a-block-diagonal-matrix/34024/2 "2020-01-31T19:18:25Z")

</div>

I have never used BlockDiagonals.jl but…

`@btime b6 = Matrix($b1) .* Matrix($b2);`

it seems to be much faster than your first option, though not as fast as the other one.

---

<div class="post-metadata">

**Author:** ![teored90](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/teored90/32/5142_2.png) [@teored90](https://discourse.julialang.org/u/teored90)\
**Post date:** [February 1, 2020, 12:50pm UTC](https://discourse.julialang.org/t/broadcasting-for-a-block-diagonal-matrix/34024/3 "2020-02-01T12:50:53Z")

</div>

Well that is expected because you put the conversion to Matrix outside of the interpolations and thus it is benchmarked by `@btime`, hence making it slower than my second example. In any case, my question is more about implementing the broadcasting interface.

---

<div class="post-metadata">

**Author:** ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)\
**Post date:** [February 1, 2020, 1:08pm UTC](https://discourse.julialang.org/t/broadcasting-for-a-block-diagonal-matrix/34024/4 "2020-02-01T13:08:08Z")

</div>

For this kind of missing feature, I recommend opening an issue, better to discuss there since it will lead to a PR likely,  
(wether by you or the maintainers)  
So keeping the discussion of how to do it in one place is useful.

---

<div class="post-metadata">

**Author:** ![teored90](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/teored90/32/5142_2.png) [@teored90](https://discourse.julialang.org/u/teored90)\
**Post date:** [February 1, 2020, 2:12pm UTC](https://discourse.julialang.org/t/broadcasting-for-a-block-diagonal-matrix/34024/5 "2020-02-01T14:12:01Z")

</div>

[I did](https://github.com/invenia/BlockDiagonals.jl/issues/35), but still, it would be nice for my personal knowledge to get a hint on how it is done.

---

<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:** [February 1, 2020, 7:39pm UTC](https://discourse.julialang.org/t/broadcasting-for-a-block-diagonal-matrix/34024/6 "2020-02-01T19:39:55Z")

</div>

You can look at the implementation of broadcasting in BlockBandedMatrices.jl:

[https://github.com/JuliaMatrices/BlockBandedMatrices.jl/blob/master/src/broadcast.jl](https://github.com/JuliaMatrices/BlockBandedMatrices.jl/blob/master/src/broadcast.jl)
