user27047503
user27047503

Reputation: 1

Fill an array with matrices along thr k-th diagonal

Is there an elegant way or function to fill a square 2D-array along its k-th diagonal with sub-matrices?

I am looking for something similar to what scpiy.linalg.block_diag() does but for lets say the first "diagonal" above the main "diagonal".

I am trying to fill an array like here:

Matrix

where each of the elements in H is a 8x8 matrix itself.

H is an 16nx16n array, where n a natural number. I have already filled the diagonal with the already mentioned method


H = sc.linalg.block_diag(h_bulk_plane + hS_upper,*[h_bulk_plane]*(2*(n-1)), h_bulk_plane - hS_upper)

But I can't find a good way to fill its upper/lower diagonal.

Upvotes: 0

Views: 56

Answers (2)

Armali
Armali

Reputation: 19375

I can't find a good way to fill its upper/lower diagonal.

I don't know whether you consider this a good way; anyway, the upper/lower diagonal is the main diagonal of the upper right and the lower left partial matrix, respectively (each one block row and one block column smaller). Knowing this and knowing that the non-diagonal blocks of H are zero after initialization with sc.linalg.block_diag, we can literally add the upper/lower diagonals after also having constructed their partial matrixes with sc.linalg.block_diag. If e. g. these are stored as Hud and Hld, this is done by

H[:-8, 8:] += Hud
H[8:, :-8] += Hld

Upvotes: 0

Laurenz Gladbach
Laurenz Gladbach

Reputation: 1

What you can observe is, that on the main diagonal the indices have the shape i,i. For the case of the k-th diagonal, you can just take the size like 8 for 8x8 matrices and decide if its above the main diagonal or not. Take the first index like (i,0) or (0, i) and iterate by just adding 1 to both coordinates until one of the values is >= to the size.

Upvotes: 0

Related Questions