# How to Store a Banded Matrix by Diagonal

I'm taking a graduate level independent study course this semester in Matrix Computations. I'm not getting much support from the professor, so am turning to the excellent StackExchange community for help (how's that for buttering you up).

The section I'm on right now shows how a symmetric matrix can be stored-by-diagonal (as the book puts it) by storing the main diagonal and each sub diagonal in a one dimension array then doing some creative indexing to get the values out of the array. So,

$$A = \begin{pmatrix} 1 & 2 & 3 \\ 2 & 4 & 5 \\ 3 & 5 & 6\end{pmatrix}$$

would get stored like $A.\mathrm{diag} = [ 1 4 6 2 5 3 ]$. Then if $i \ge j$ the elements of $A$ would be

$$a_{i+k,i} = A.\mathrm{diag}\left(i + nk - \frac{k(k-1)}{2}\right)$$

The problem I am stuck on asks me to come up with a scheme to store a banded matrix in a similar fashion. I can see how to store it by storing the main diagonal, then the first subdiagonal, then the next subdiagonal, ... , then the first superdiagonal, etc. What I can't seem to figure out is how to come up with an indexing scheme that will get the $a_{ij}$ element out of the array.

In other words

$$B = \begin{pmatrix} 1 & 2 & 3 & 0 & 0 \\ 4 & 5 & 6 & 7 & 0 \\ 0 & 8 & 9 & 1 & 2 \\ 0 & 0 & 3 & 4 & 5 \\ 0 & 0 & 0 & 6 & 7 \end{pmatrix}$$

would get stored in an array like

$$B.\mathrm{diag} =\begin{bmatrix}1 & 5 & 9 & 4 & 7 & 4 & 8 & 3 & 6 & 2 & 6 & 1 & 5 & 3 & 7 & 2\end{bmatrix}$$

My question then is what is the right hand side of

$$b_{ij} = B.\mathrm{diag}(???)$$

I'm less interested in the actual answer and more interested in how to figure out the index. Thanks in advance for any help you can provide.

-
Also, if anyone can tell me what I did wrong to break the LaTeX rendering I would be grateful. Hopefully you can figure out the missing matrix elements. –  Dave Kincaid Oct 3 '11 at 17:56
The structure for $B.\mathrm{diag}$ looks off; usually one would put in sub- or superdiagonals first before diagonals... e.g. $[4\,8\,3\,6\,1\,5\,9\dots]$ –  J. M. is back. Oct 3 '11 at 18:01
A last bit before I go to bed: apart from the dimension of your matrix and the indices, your $B.\mathrm{diag}$ should also take the "lower bandwidth" and "upper bandwidth" as arguments. For your example, $b$ has a lower bandwidth of $1$ and an upper bandwidth of $2$. The triangular numbers will definitely have to appear somewhere... –  J. M. is back. Oct 3 '11 at 18:47
This is too old to migrate, but you might want to delete and repost on Computational Science –  robjohn Jan 23 '13 at 20:56