How to construct non-square sparse matrix with multiple diagonals

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 is what I found on github. Can’t you fill the diagonals with zero as required?

Edit: fixed mistake referencing SymTriagonal.

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.

Use spdiagm or 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, 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.

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

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.

1 Like