简体   繁体   English

使用scipy.sparse的三对角块矩阵

[英]Tridiagonal block matrix using scipy.sparse

I need help in building a block tridiagonal matrix using scipy sparse. 我需要使用scipy sparse构建块三对角矩阵的帮助。

What I mean by that is for a square matrix B, 我的意思是针对平方矩阵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! 谢谢!

Solved it! 解决了!

I just used scipy.sparse.bmat in conjunction with list comprehensions. 我只是将scipy.sparse.bmat与列表推导结合使用。

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. 其中B是一个nxn矩阵。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM