# how to get continuation of square matrices with periodic diagonal band automatically?

I want to write a function that can automatically analyze any square matrices with periodic band diagonals and give continuations of them. But I can't figure out an elegant way to do this.

Suppose we have written such a function called continuation, and suppose we have an example square matrix mat of order 8 as

$\small \mathtt{mat}=\begin{pmatrix} y+0.2 & 2 \text{t1} & 0.2 & \text{t1} & 0 & 0 & 0 & 0 \\ 2 \text{t1} & 0.2\, -y & 0 & 0.2 & 0 & 0 & 0 & 0 \\ 0.2 & 0 & y+0.2 & 2 \text{t1} & 0.2 & \text{t1} & 0 & 0 \\ \text{t1} & 0.2 & 2 \text{t1} & 0.2\, -y & 0 & 0.2 & 0 & 0 \\ 0 & 0 & 0.2 & 0 & y+0.2 & 2 \text{t1} & 0.2 & \text{t1} \\ 0 & 0 & \text{t1} & 0.2 & 2 \text{t1} & 0.2\, -y & 0 & 0.2 \\ 0 & 0 & 0 & 0 & 0.2 & 0 & y+0.2 & 2 \text{t1} \\ 0 & 0 & 0 & 0 & \text{t1} & 0.2 & 2 \text{t1} & 0.2\, -y \\ \end{pmatrix}$

then continuation[mat][n] can give a square matrix that is a continuation of original mat with order n. For example:

continuation[mat][9]


should give a continuation of mat of order 9 as following

$\small \begin{pmatrix} y+0.2 & 2 \text{t1} & 0.2 & \text{t1} & 0 & 0 & 0 & 0 & 0 \\ 2 \text{t1} & 0.2\, -y & 0 & 0.2 & 0 & 0 & 0 & 0 & 0 \\ 0.2 & 0 & y+0.2 & 2 \text{t1} & 0.2 & \text{t1} & 0 & 0 & 0 \\ \text{t1} & 0.2 & 2 \text{t1} & 0.2\, -y & 0 & 0.2 & 0 & 0 & 0 \\ 0 & 0 & 0.2 & 0 & y+0.2 & 2 \text{t1} & 0.2 & \text{t1} & 0 \\ 0 & 0 & \text{t1} & 0.2 & 2 \text{t1} & 0.2\, -y & 0 & 0.2 & 0 \\ 0 & 0 & 0 & 0 & 0.2 & 0 & y+0.2 & 2 \text{t1} & 0.2 \\ 0 & 0 & 0 & 0 & \text{t1} & 0.2 & 2 \text{t1} & 0.2\, -y & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0.2 & 0 & y+0.2 \\ \end{pmatrix}$

How to write such a continuation function which can automatically analyze square matrices with periodic diagonal band and give general continuation version?

The list representation of mat is here:

mat = {{0.2 + y, 2 t1, 0.2, t1, 0, 0, 0, 0},
{2 t1, 0.2 - y, 0, 0.2, 0, 0, 0, 0},
{0.2, 0, 0.2 + y, 2 t1, 0.2, t1, 0, 0},
{t1, 0.2, 2 t1, 0.2 - y, 0, 0.2, 0, 0},
{0, 0, 0.2, 0, 0.2 + y, 2 t1, 0.2, t1},
{0, 0, t1, 0.2, 2 t1, 0.2 - y, 0, 0.2},
{0, 0, 0, 0, 0.2, 0, 0.2 + y, 2 t1},
{0, 0, 0, 0, t1, 0.2, 2 t1, 0.2 - y}}

• This is quite a complex task, unless you specify what exact type of continuations you mean... You say periodic, so I would guess start by writing a peroid-finding function for lists... Dec 15, 2014 at 11:47

Using the function FindTransientRepeat[], we can do the following:

mat = SparseArray[mat];
m = 10; {lo, up} = MinMax[mat["NonzeroPositions"].{-1, 1}];
sp = SparseArray[Table[With[{d = Diagonal[mat, k], p = Abs[k]},
Band[If[k >= 0, Identity, Reverse][{1, p + 1}]] ->
(PadRight[#1, m - p, #2] & @@ FindTransientRepeat[d, 2])],
{k, lo, up}]];

MatrixForm[sp]


$$\tiny\begin{pmatrix} y+0.2 & 2 \text{t1} & 0.2 & \text{t1} & 0 & 0 & 0 & 0 & 0 & 0 \\ 2 \text{t1} & 0.2\, -y & 0 & 0.2 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0.2 & 0 & y+0.2 & 2 \text{t1} & 0.2 & \text{t1} & 0 & 0 & 0 & 0 \\ \text{t1} & 0.2 & 2 \text{t1} & 0.2\, -y & 0 & 0.2 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0.2 & 0 & y+0.2 & 2 \text{t1} & 0.2 & \text{t1} & 0 & 0 \\ 0 & 0 & \text{t1} & 0.2 & 2 \text{t1} & 0.2\, -y & 0 & 0.2 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0.2 & 0 & y+0.2 & 2 \text{t1} & 0.2 & \text{t1} \\ 0 & 0 & 0 & 0 & \text{t1} & 0.2 & 2 \text{t1} & 0.2\, -y & 0 & 0.2 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0.2 & 0 & y+0.2 & 2 \text{t1} \\ 0 & 0 & 0 & 0 & 0 & 0 & \text{t1} & 0.2 & 2 \text{t1} & 0.2\, -y \end{pmatrix}$$

• Thank you so much for this new FindTransientRepeat[] function +1 Aug 15, 2017 at 7:59

Since no one gave a satisfactory answer, I figured out a way myself which combines Band, Diagonal and SparseArray.

1. The Diagonal[m,k]gives the elements on the $k^{th}$ diagonal of m, illustrated as below:

2. Band in SparseArray can repeat the values cyclically, for example:

SparseArray[Band[{1, 1}, {5, 5}] -> {x, y, z}, {5, 5}] // MatrixForm


gives

$\begin{pmatrix} x & 0 & 0 & 0 & 0 \\ 0 & y & 0 & 0 & 0 \\ 0 & 0 & z & 0 & 0 \\ 0 & 0 & 0 & x & 0 \\ 0 & 0 & 0 & 0 & y \\ \end{pmatrix}$

3. Tally can find the cyclic sequence, since Tally tallies the elements in list, listing all distinct elements together with their multiplicities. for example:

Tally[Diagonal[mat]]


gives

{{0.2 + y, 4}, {0.2 - y, 4}}


so

Tally[Diagonal[mat]][[;;,1]]


give the cyclic sequence {0.2 + y, 0.2 - y}

So combine these 3 features we got a version of the code of continuation as below:

Clear[continuation];
continuation[mat_, order_] :=
Module[{DiagtoBand, orderofmat = Length@mat},

(*DiagtoBand is used for changing diagonal index to Band parameter*)

DiagtoBand[i_] :=
If[Positive[
i], {{1, i + 1}, {order - i, order}}, {{1 - i, 1}, {order,
order + i}}];

(*if the order required is less then the order of mat,
then we just simply ArrayPad it. On the other hand, we continue it. *)

If[order <= orderofmat,
MatrixForm@
Normal@SparseArray[
Table[(Band @@ DiagtoBand[i]) ->
Tally[Diagonal[mat, i]][[;; , 1]], {i, -orderofmat + 1,
orderofmat - 1}], {order, order}]]]


The continuation function is now universal.

continuation[mat,9]


gives

$\small \begin{pmatrix} y+0.2 & 2 \text{t1} & 0.2 & \text{t1} & 0 & 0 & 0 & 0 & 0 \\ 2 \text{t1} & 0.2\, -y & 0 & 0.2 & 0 & 0 & 0 & 0 & 0 \\ 0.2 & 0 & y+0.2 & 2 \text{t1} & 0.2 & \text{t1} & 0 & 0 & 0 \\ \text{t1} & 0.2 & 2 \text{t1} & 0.2\, -y & 0 & 0.2 & 0 & 0 & 0 \\ 0 & 0 & 0.2 & 0 & y+0.2 & 2 \text{t1} & 0.2 & \text{t1} & 0 \\ 0 & 0 & \text{t1} & 0.2 & 2 \text{t1} & 0.2\, -y & 0 & 0.2 & 0 \\ 0 & 0 & 0 & 0 & 0.2 & 0 & y+0.2 & 2 \text{t1} & 0.2 \\ 0 & 0 & 0 & 0 & \text{t1} & 0.2 & 2 \text{t1} & 0.2\, -y & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0.2 & 0 & y+0.2 \\ \end{pmatrix}$

• You might be able to do better with FindLinearRecurrence than with the Tally approach. In any case, glad to see you've come up with something you can use. Dec 16, 2014 at 4:09
• @bills Thank you for suggesting FindLinearRecurrence. But I found that FindLinearRecurrence needs at least 5 elements in a list. So I think I just stick with Tally approach which is easy and understandable. Dec 16, 2014 at 6:08

The functions Band and SparseArray may be what you are looking for:

n = {9, 9};
Normal[SparseArray[{Band[{1, 1}] -> y + 0.2, Band[{1, 3}] -> 0.2,
Band[{1, 2}, n] -> {2 t1, 0}, Band[{3, 1}, n] -> {t1, 0}},  n]] // MatrixForm


Just increase the dimensions in the variable n and the specified pattern(s) continue.

• Thank you! I know band function. But you misunderstood me. I want a function continuation which can automatically analyze any square matrix mat we feed to it, and give general version continuation[mat]? Dec 15, 2014 at 6:19
• What you gave is a manual version. Dec 15, 2014 at 6:27
• So -- you want a function that's like FindSequenceFunction but works on matrices as well? Dec 15, 2014 at 14:29