Created
May 7, 2020 14:05
-
-
Save kaisugi/a8246fcab453df6e7d034d13c70fed4b to your computer and use it in GitHub Desktop.
Tri-Diagonal Matrix Algorithm (三重対角行列Aの連立一次方程式Ax=bの解のO(n)のアルゴリズム)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Tri-Diagonal Matrix Algorithm | |
def tdma(A, b): | |
n = b.size | |
p = [] | |
q = [] | |
p.append(A[0][1] / A[0][0]) | |
q.append(b[0] / A[0][0]) | |
for i in range(1, n): | |
if i != n-1: | |
p.append(A[i][i+1] / (A[i][i] - A[i][i-1] * p[i-1])) | |
q.append((b[i] - A[i][i-1] * q[i-1]) / (A[i][i] - A[i][i-1] * p[i-1])) | |
ans = np.zeros(n) | |
ans[n-1] = q[n-1] | |
for i in range(n-2, -1, -1): | |
ans[i] = q[i] - p[i] * ans[i+1] | |
return ans |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
reference: https://sites.lsa.umich.edu/shixumeng/wp-content/uploads/sites/629/2020/02/Thomas.pdf