-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathptdsys.py
53 lines (40 loc) · 1.28 KB
/
ptdsys.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
'''
File with a tridiagonal periodic solver of linear systems.
Dependencies:
numpy 1.19.4
Programmed by Manuel Giménez de Castro. Advisor: Prof Dr. Alexandre Megiorin Roma.
General Description:
Solves Mx = D, where M is a tridiagonal periodic matrix.
Computational parameters:
A: lower diagonal: NDARRAY
B: diagonal: NDARRAY
C: higher diagonal: NDARRAY
D: right side of the equations: NDARRAY
Notes:
This is based on Alexandre Roma's ptdsys.f implemented in FORTRAN.
'''
import numpy as np
def ptdsys(A,B,C,D):
#checks if every shape is the same
if not (A.shape == B.shape == C.shape == D.shape):
raise ValueError("[ERROR] A, B, C or D shapes don't match.")
N = A.shape[0]
B0 = B.copy()
C0 = C.copy()
XA = np.zeros(N+1, dtype='float64')
X = np.zeros(N+1, dtype='float64') # vector with answer
XA[0] = XA[N] = 1
C0[0] = 0
for j in range(1,N):
B0[j] -= A[j]*C0[j-1]
C0[j] /= B0[j]
X[j] = (D[j]+A[j]*X[j-1])/B0[j]
XA[j] = (A[j]*XA[j-1])/B0[j]
for j in range(N-1, 0,-1):
X[j] += C0[j]*X[j+1]
XA[j] += C0[j]*XA[j+1]
L=(A[0]*X[N-1]+C[0]*X[1]+D[0])/(B[0]-A[0]*XA[N-1]-C[0]*XA[1])
X[0] = L
for j in range(1,N):
X[j] += L*XA[j]
return X[:-1]