# New blog post on banded matrices and ordinary differential equations using BandedMatrices.jl

**URL:** <https://discourse.julialang.org/t/new-blog-post-on-banded-matrices-and-ordinary-differential-equations-using-bandedmatrices-jl/18564>\
**Category:** Numerics\
**Tags:** announcement\
**Created:** [December 11, 2018, 4:10pm UTC](https://discourse.julialang.org/t/new-blog-post-on-banded-matrices-and-ordinary-differential-equations-using-bandedmatrices-jl/18564 "2018-12-11T16:10:04Z")\
**Posts on this page:** 12\
**Page:** 2

<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:** [December 19, 2018, 1:05pm UTC](https://discourse.julialang.org/t/new-blog-post-on-banded-matrices-and-ordinary-differential-equations-using-bandedmatrices-jl/18564/21 "2018-12-19T13:05:35Z")

</div>

This is called a `BandedBlockBandedMatrix` in BlockBandedMatrices.jl, which is the topic of the next blog post.

---

<div class="post-metadata">

**Author:** ![rveltz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rveltz/32/2707_2.png) [@rveltz](https://discourse.julialang.org/u/rveltz)\
**Post date:** [December 19, 2018, 2:55pm UTC](https://discourse.julialang.org/t/new-blog-post-on-banded-matrices-and-ordinary-differential-equations-using-bandedmatrices-jl/18564/22 "2018-12-19T14:55:13Z")

</div>

Dump question: by using a change of variable, can I bring the far band closer to the diagonal?

---

<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:** [December 19, 2018, 2:59pm UTC](https://discourse.julialang.org/t/new-blog-post-on-banded-matrices-and-ordinary-differential-equations-using-bandedmatrices-jl/18564/23 "2018-12-19T14:59:19Z")

</div>

If it comes from finite differences or similar, you can’t. But there’s Cuthill-McKee algorithm that can calculate the minimi bandwidth permutation for general sparse matrices, which is yet to be implemented: [https://github.com/JuliaMatrices/BandedMatrices.jl/issues/76](https://github.com/JuliaMatrices/BandedMatrices.jl/issues/76)

---

<div class="post-metadata">

**Author:** ![rveltz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rveltz/32/2707_2.png) [@rveltz](https://discourse.julialang.org/u/rveltz)\
**Post date:** [February 8, 2019, 8:41am UTC](https://discourse.julialang.org/t/new-blog-post-on-banded-matrices-and-ordinary-differential-equations-using-bandedmatrices-jl/18564/24 "2019-02-08T08:41:37Z")

</div>

I tried to use your structure `BandedBlockBandedMatrix` for the 2d problem (see 2 answers above) I stated above but I could not do it. Is there a simplified call where you give the bands (which can be far appart) and it automatically asign it to the _good_ blocks in `BandedBlockBandedMatrix`?

---

<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 8, 2019, 11:13am UTC](https://discourse.julialang.org/t/new-blog-post-on-banded-matrices-and-ordinary-differential-equations-using-bandedmatrices-jl/18564/25 "2019-02-08T11:13:02Z")

</div>

I thought you are just doing the FD matrix? That’s pretty easy:

```julia
using BlockBandedMatrices, BandedMatrices, LazyArrays, FillArrays

function finitedifference_2d(n)
    h = 1/n
    D² = BandedMatrix(0 => Fill(-2,n), 1 => Fill(1,n-1), -1 => Fill(1,n-1))/h^2
    D_xx = BandedBlockBandedMatrix(Kron(D², Eye(n)))
    D_yy = BandedBlockBandedMatrix(Kron(Eye(n), D²))
    D_xx + D_yy
end

```

---

<div class="post-metadata">

**Author:** ![rveltz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rveltz/32/2707_2.png) [@rveltz](https://discourse.julialang.org/u/rveltz)\
**Post date:** [February 8, 2019, 11:45am UTC](https://discourse.julialang.org/t/new-blog-post-on-banded-matrices-and-ordinary-differential-equations-using-bandedmatrices-jl/18564/26 "2019-02-08T11:45:55Z")

</div>

No, it is not FD and it is not a kron. The band are `0,1,-1,Nx,-Nx` where the matrix line (column) size is Nx \* Nx. I know the vecteur for each band, say `v0,v1,vm1,vnx,vmnx`.

---

<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 8, 2019, 11:57am UTC](https://discourse.julialang.org/t/new-blog-post-on-banded-matrices-and-ordinary-differential-equations-using-bandedmatrices-jl/18564/27 "2019-02-08T11:57:49Z")

</div>

Oh that won’t work in `BandedBlockBandedMatrix` unless, for example, `A[Nx+1,Nx] == 0`. But since it looks like its an operator acting on a tensor grid, it would be very strange if `A[Nx+1,Nx] ≠ 0`.

---

<div class="post-metadata">

**Author:** ![rveltz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rveltz/32/2707_2.png) [@rveltz](https://discourse.julialang.org/u/rveltz)\
**Post date:** [February 8, 2019, 12:38pm UTC](https://discourse.julialang.org/t/new-blog-post-on-banded-matrices-and-ordinary-differential-equations-using-bandedmatrices-jl/18564/28 "2019-02-08T12:38:18Z")

</div>

the condition `A[Nx+1,Nx] == 0.` is satisfied. I guess my question is how do I pass my bands to `BandedBlockBandedMatrix`?

---

<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 8, 2019, 2:10pm UTC](https://discourse.julialang.org/t/new-blog-post-on-banded-matrices-and-ordinary-differential-equations-using-bandedmatrices-jl/18564/29 "2019-02-08T14:10:45Z")

</div>

I suspect it’s actually easier to generate block bands instead of bands (you are probably combining multiple block-bands into single bands at some point). So for example the following creates FD but you can change your data as appropriate:

```julia
julia> Nx = 3; A = BandedBlockBandedMatrix(Zeros(Nx^2,Nx^2), (Fill(Nx,Nx), Fill(Nx,Nx)), (1,1), (1,1));

julia> for K = 1:Nx
          view(A,Block(K,K))[band(1)] .= 1
          view(A,Block(K,K))[band(0)] .= -4
          view(A,Block(K,K))[band(-1)] .= 1
       end

julia> for K = 1:Nx-1
          view(A,Block(K,K+1))[band(0)] .= 1
          view(A,Block(K+1,K))[band(0)] .= 1   
       end

julia> A
9×9 BandedBlockBandedMatrix{Float64,BlockArrays.PseudoBlockArray{Float64,2,Array{Float64,2}}}:
 -4.0 1.0 ⋅ │ 1.0 0.0 ⋅ │ ⋅ ⋅ ⋅ 
  1.0 -4.0 1.0 │ 0.0 1.0 0.0 │ ⋅ ⋅ ⋅ 
   ⋅ 1.0 -4.0 │ ⋅ 0.0 1.0 │ ⋅ ⋅ ⋅ 
 ──────────────────┼────────────────────┼──────────────────
  1.0 0.0 ⋅ │ -4.0 1.0 ⋅ │ 1.0 0.0 ⋅ 
  0.0 1.0 0.0 │ 1.0 -4.0 1.0 │ 0.0 1.0 0.0
   ⋅ 0.0 1.0 │ ⋅ 1.0 -4.0 │ ⋅ 0.0 1.0
 ──────────────────┼────────────────────┼──────────────────
   ⋅ ⋅ ⋅ │ 1.0 0.0 ⋅ │ -4.0 1.0 ⋅ 
   ⋅ ⋅ ⋅ │ 0.0 1.0 0.0 │ 1.0 -4.0 1.0
   ⋅ ⋅ ⋅ │ ⋅ 0.0 1.0 │ ⋅ 1.0 -4.0

```

---

<div class="post-metadata">

**Author:** ![rveltz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rveltz/32/2707_2.png) [@rveltz](https://discourse.julialang.org/u/rveltz)\
**Post date:** [February 8, 2019, 5:27pm UTC](https://discourse.julialang.org/t/new-blog-post-on-banded-matrices-and-ordinary-differential-equations-using-bandedmatrices-jl/18564/30 "2019-02-08T17:27:56Z")

</div>

> [@dlfivefifty](#):
>
> for K = 1:Nx view(A,Block(K,K))[band(1)] .= 1 view(A,Block(K,K))[band(0)] .= -4 view(A,Block(K,K))[band(-1)] .= 1 end julia\> for K = 1:Nx-1 view(A,Block(K,K+1))[band(0)] .= 1 view(A,Block(K+1,K))[band(0)] .= 1 end

Thank you a lot! Unfortunately, I had not seen [error](https://github.com/JuliaMatrices/BlockBandedMatrices.jl/issues/23)…

---

<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 9, 2019, 12:13am UTC](https://discourse.julialang.org/t/new-blog-post-on-banded-matrices-and-ordinary-differential-equations-using-bandedmatrices-jl/18564/31 "2019-02-09T00:13:08Z")

</div>

Huh?

---

<div class="post-metadata">

**Author:** ![rveltz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rveltz/32/2707_2.png) [@rveltz](https://discourse.julialang.org/u/rveltz)\
**Post date:** [February 9, 2019, 7:12am UTC](https://discourse.julialang.org/t/new-blog-post-on-banded-matrices-and-ordinary-differential-equations-using-bandedmatrices-jl/18564/32 "2019-02-09T07:12:55Z")

</div>

I need to solve `A x = rhs`… I guess I can do as in your example and try Gauss Seidel

[Previous page](https://discourse.julialang.org/t/new-blog-post-on-banded-matrices-and-ordinary-differential-equations-using-bandedmatrices-jl/18564.md?page=1)
