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