0

I need help in building a block tridiagonal matrix using scipy sparse.

What I mean by that is for a square matrix B,

I need to create

[[B I 0 0 0]
 [I B I 0 0]
 [0 I B I 0]
 [0 0 I B I]
 [0 0 0 I B]]

Now, I want this to be programmatically done since the size of the matrix may vary.

Thanks!

jtitusj
  • 3,046
  • 3
  • 24
  • 40

1 Answers1

3

Solved it!

I just used scipy.sparse.bmat in conjunction with list comprehensions.

A = sparse.bmat([[B if i == j else np.eye(n) if abs(i-j)==1
                else None for i in range(n)]
                for j in range(n)], format='bsr')

Where B is an nxn matrix.

jtitusj
  • 3,046
  • 3
  • 24
  • 40
  • Im looking for the same thing, but i want the zise of ther matrix to be general, so that n is not necessarly the same size of B. Any tips? I have tryed to to change ur code but get wird reslts. Here is posted question: https://stackoverflow.com/questions/52737631/create-block-tridiagonal-matrix – JustANoob Oct 10 '18 at 10:59