# How to construct non-square sparse matrix with multiple diagonals

**URL:** <https://discourse.julialang.org/t/how-to-construct-non-square-sparse-matrix-with-multiple-diagonals/76456>\
**Category:** New to Julia\
**Tags:** sparse\
**Created:** [February 14, 2022, 8:31pm UTC](https://discourse.julialang.org/t/how-to-construct-non-square-sparse-matrix-with-multiple-diagonals/76456 "2022-02-14T20:31:37Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jinml](https://avatars.discourse-cdn.com/v4/letter/j/91b2a8/32.png) [@jinml](https://discourse.julialang.org/u/jinml)\
**Post date:** [February 14, 2022, 8:31pm UTC](https://discourse.julialang.org/t/how-to-construct-non-square-sparse-matrix-with-multiple-diagonals/76456/1 "2022-02-14T20:31:37Z")

</div>

I need to construct a non-square (N by N+2) matrix with 3 diagonal vectors of length N each, I can’t seem to use `Tridiagonal(vsub,v0,vsuper)` as this function wants to form a square matrix. What is the function that does what I want? I did a google search and did not have any luck.  
Thanks

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [February 14, 2022, 8:47pm UTC](https://discourse.julialang.org/t/how-to-construct-non-square-sparse-matrix-with-multiple-diagonals/76456/2 "2022-02-14T20:47:37Z")

</div>

> [@jinml](#):
>
> I need to construct a non-square (N by N+2) matrix with 3 diagonal vectors of length N each, I can’t seem to use `Tridiagonal(vsub,v0,vsuper)` as this function wants to form a square matrix. What is the function that does what I want? I did a google search and did not have any luck.

[This](https://github.com/JuliaLang/julia/blob/635449dabee81bba315ab066627a98f856141969/stdlib/LinearAlgebra/src/tridiag.jl#L472-L561) is what I found on github. Can’t you fill the diagonals with zero as required?

Edit: fixed mistake referencing `SymTriagonal`.

---

<div class="post-metadata">

**Author:** ![jinml](https://avatars.discourse-cdn.com/v4/letter/j/91b2a8/32.png) [@jinml](https://discourse.julialang.org/u/jinml)\
**Post date:** [February 14, 2022, 9:00pm UTC](https://discourse.julialang.org/t/how-to-construct-non-square-sparse-matrix-with-multiple-diagonals/76456/3 "2022-02-14T21:00:09Z")

</div>

Why are you asking me to twist my math to fit into the software? If I do as you suggested this matrix does not do what I need. Sparse matrices in Matlab can specify a size and truncate the vectors to fit into the matrix if needed. Doing the same to Julia matrices by hand seems to lead to a general sparse matrix that takes more memory than a true tridiagonal due to all the indices being stored.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [February 14, 2022, 9:06pm UTC](https://discourse.julialang.org/t/how-to-construct-non-square-sparse-matrix-with-multiple-diagonals/76456/4 "2022-02-14T21:06:34Z")

</div>

> [@jinml](#):
>
> I need to construct a non-square (N by N+2) matrix with 3 diagonal vectors of length N each

[Use `spdiagm`](https://docs.julialang.org/en/v1/stdlib/SparseArrays/#SparseArrays.spdiagm) or [BandedMatrices.jl](https://github.com/JuliaMatrices/BandedMatrices.jl) (the former is generic to any sparse matrix, analogous to Matlab sparse matrices; the latter is specialized for banded matrices and can be more efficient for that particular structure)

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [February 14, 2022, 9:09pm UTC](https://discourse.julialang.org/t/how-to-construct-non-square-sparse-matrix-with-multiple-diagonals/76456/5 "2022-02-14T21:09:14Z")

</div>

> [@jinml](#):
>
> Why are you asking me to twist my math to fit into the software?

Sorry, there is no MWE to test my ideas and I’m too lazy to write one. I’m mainly interested in BLAS compat, which could be the real issue you are probably pointing out.

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [February 14, 2022, 11:14pm UTC](https://discourse.julialang.org/t/how-to-construct-non-square-sparse-matrix-with-multiple-diagonals/76456/6 "2022-02-14T23:14:36Z")

</div>

> [@stevengj](#):
>
> [Use `spdiagm`](https://docs.julialang.org/en/v1/stdlib/SparseArrays/#SparseArrays.spdiagm) or [BandedMatrices.jl](https://github.com/JuliaMatrices/BandedMatrices.jl) (the former is generic to any sparse matrix, analogous to Matlab sparse matrices; the latter is specialized for banded matrices and can be more efficient for that particular structure)

Sorry for bothering you, is this recommended for the general case (AFAIU BLAS tridiagonal is not restricted to square format)?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [February 14, 2022, 11:56pm UTC](https://discourse.julialang.org/t/how-to-construct-non-square-sparse-matrix-with-multiple-diagonals/76456/7 "2022-02-14T23:56:40Z")

</div>

Tridiagonal matrix operations are basically equivalent to level-1 BLAS, and an optimizing compiler with SIMD enabled can usually do just as well as the hand optimized BLAS libraries for this sort of thing.

Indeed, because tridiagonal matrix operations are linear time, there is a good chance that they aren’t the performance bottleneck in your code.
