简体   繁体   English

线性代数与稀疏三芳基马利克斯

[英]Linear algebra with a sparse triagonol marix

I have a tridiagonal matrix A which I want to make a sparse matrix and then solve a linear system Ax =b using scipy, how is this done. 我有一个三对角矩阵A,我想制作一个稀疏矩阵,然后使用scipy求解线性系统Ax = b,这是怎么做的。

i have tried the following code, which dosent work. 我试过下面的代码,哪些工作。


from scipy.sparse import dia_matrix
from scipy.sparse.linalg import spsolve_triangular

N = 10


diag = np.zeros(N) + 2
udiag = np.zeros(N) + 1
A = dia_matrix(([diag, udiag, udiag], [0, 1, -1]), shape=(N, N))


b = np.ones(N)

print(A.todense())
print(b)


x = spsolve_triangular(A, b)

print(x)

The problem comes from the fact that your matrix is not triangular but tridiagonal. 问题来自您的矩阵不是三角形而是三对角的事实。 As such you have to use the spsolve function instead of the spsolve_triangular function. 因此,您必须使用spsolve函数而不是spsolve_triangular函数。

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

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