# Strange behavior of BlockDiagonal constructor with Vector of BandedMatrices

**URL:** <https://discourse.julialang.org/t/strange-behavior-of-blockdiagonal-constructor-with-vector-of-bandedmatrices/83409>\
**Category:** General Usage\
**Created:** [June 27, 2022, 3:26pm UTC](https://discourse.julialang.org/t/strange-behavior-of-blockdiagonal-constructor-with-vector-of-bandedmatrices/83409 "2022-06-27T15:26:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Arrigo\_Benedetti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/arrigo_benedetti/32/25545_2.png) [@Arrigo\_Benedetti](https://discourse.julialang.org/u/Arrigo_Benedetti)\
**Post date:** [June 27, 2022, 3:26pm UTC](https://discourse.julialang.org/t/strange-behavior-of-blockdiagonal-constructor-with-vector-of-bandedmatrices/83409/1 "2022-06-27T15:26:11Z")

</div>

As you can see from the MWE I have to call `D = BlockDiagonal([Dbv[1], Dbv[2], Dbv[3]])` instead of `D = BlockDiagonal(Dbv)` . It seems that some type information is lost when the Vector is banded matrices is created, but then it is recovered by calling the constructor with `[Dbv[1], Dbv[2], Dbv[3]]` …

```julia
using BandedMatrices
using BlockDiagonals

ix = [9, 16, 22]
Dbv = []
iold = 0
for i in ix
    Db = BandedMatrix{Float64}(undef, (i-iold-1, i-iold), (0,1))
    Db[band(0)] .= -1
    Db[band(1)] .= 1
    iold = i
    push!(Dbv, copy(Db))
end

```

```julia
Dbv[1]

8×9 BandedMatrix{Float64} with bandwidths (0, 1):
 -1.0 1.0 ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ 
   ⋅ -1.0 1.0 ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ 
   ⋅ ⋅ -1.0 1.0 ⋅ ⋅ ⋅ ⋅ ⋅ 
   ⋅ ⋅ ⋅ -1.0 1.0 ⋅ ⋅ ⋅ ⋅ 
   ⋅ ⋅ ⋅ ⋅ -1.0 1.0 ⋅ ⋅ ⋅ 
   ⋅ ⋅ ⋅ ⋅ ⋅ -1.0 1.0 ⋅ ⋅ 
   ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ -1.0 1.0 ⋅ 
   ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ -1.0 1.0

```

```julia
D = BlockDiagonal(Dbv)

MethodError: no method matching BlockDiagonal(::Vector{Any})
Closest candidates are:
  BlockDiagonal(::BlockDiagonal) at ~/.julia/packages/BlockDiagonals/Y9q2S/src/blockdiagonal.jl:20
  BlockDiagonal(::Vector{V}) where {T, V<:AbstractMatrix{T}} at ~/.julia/packages/BlockDiagonals/Y9q2S/src/blockdiagonal.jl:16

Stacktrace:
 [1] top-level scope
   @ In[3]:1
 [2] eval
   @ ./boot.jl:373 [inlined]
 [3] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
   @ Base ./loading.jl:1196

```

```julia
D = BlockDiagonal([Dbv[1], Dbv[2], Dbv[3]])

19×22 BlockDiagonal{Float64, BandedMatrix{Float64, Matrix{Float64}, Base.OneTo{Int64}}}:
 -1.0 1.0 0.0 0.0 0.0 0.0 … 0.0 0.0 0.0 0.0 0.0 0.0
  0.0 -1.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
  0.0 0.0 -1.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
  0.0 0.0 0.0 -1.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
  0.0 0.0 0.0 0.0 -1.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0
  0.0 0.0 0.0 0.0 0.0 -1.0 … 0.0 0.0 0.0 0.0 0.0 0.0
  0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
  0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
  0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
  0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
  0.0 0.0 0.0 0.0 0.0 0.0 … 0.0 0.0 0.0 0.0 0.0 0.0
  0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
  0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
  0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
  0.0 0.0 0.0 0.0 0.0 0.0 -1.0 1.0 0.0 0.0 0.0 0.0
  0.0 0.0 0.0 0.0 0.0 0.0 … 0.0 -1.0 1.0 0.0 0.0 0.0
  0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 -1.0 1.0 0.0 0.0
  0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 -1.0 1.0 0.0
  0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 -1.0 1.0

```

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [June 27, 2022, 3:29pm UTC](https://discourse.julialang.org/t/strange-behavior-of-blockdiagonal-constructor-with-vector-of-bandedmatrices/83409/2 "2022-06-27T15:29:35Z")

</div>

> [@Arrigo\_Benedetti](#):
>
> `Dbv = []`

The type information isn’t lost, you explicitly removed it. When you do `[]`, you create a `Vector{Any}`.

EDIT: actually instead of “removed” I should have said you explicitly asked Julia not to create it in the first place.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [June 27, 2022, 3:29pm UTC](https://discourse.julialang.org/t/strange-behavior-of-blockdiagonal-constructor-with-vector-of-bandedmatrices/83409/3 "2022-06-27T15:29:52Z")

</div>

`Dbv = []` creates a `Vector{Any}`. You want `Dbv = BandedMatrix[]`

---

<div class="post-metadata">

**Author:** ![Arrigo\_Benedetti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/arrigo_benedetti/32/25545_2.png) [@Arrigo\_Benedetti](https://discourse.julialang.org/u/Arrigo_Benedetti)\
**Post date:** [June 27, 2022, 3:35pm UTC](https://discourse.julialang.org/t/strange-behavior-of-blockdiagonal-constructor-with-vector-of-bandedmatrices/83409/4 "2022-06-27T15:35:25Z")

</div>

Got it, thanks!
