-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterpolators.py
186 lines (150 loc) · 6.75 KB
/
interpolators.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import numpy as np
from numpy import linalg
class Cubic:
# constructor function
def __init__(self, Xs = [], fs = [], Integrate = True, Extend = "Linear", Xmin = None, Xmax = None):
self.Xs = Xs
self.fs = fs
self.dimension = len(Xs)
# 0. Computes H
self.H = [self.Xs[i]-self.Xs[i-1] for i in range(1,self.dimension)]
# 1. Populates TH
TH = [[0 for _ in range(self.dimension)] for _ in range(self.dimension)]
for i in range(1,self.dimension-1):
TH[i][i-1],TH[i][i],TH[i][i+1] = [self.H[i-1],2*(self.H[i-1]+self.H[i]),self.H[i]]
# natural end conditions
TH[0][0],TH[-1][-1] = [1,1]
self.TH = TH
# 2. Populates COB
COB = [[0 for _ in range(self.dimension)] for _ in range(self.dimension)]
for i in range(1,self.dimension-1):
COB[i][i-1],COB[i][i],COB[i][i+1] = [3/self.H[i-1],-3/self.H[i-1]-3/self.H[i],3/self.H[i]]
self.COB = np.matrix(COB)
# 3. Solves C in basis of F
CinF = np.matmul(linalg.inv(np.matrix(self.TH)),self.COB)
self.CinF = CinF
# 4. Solves B by b1 and propagation
BinF = [[0 for _ in range(self.dimension)] for _ in range(self.dimension)]
# b0 by Eq(18.21)
BinF[0][0],BinF[0][1] = [-1/self.H[0],1/self.H[0]]
BinF[0] += (-self.H[0]/3)*self.CinF[1]
BinF[-1] = np.matrix(BinF[-1])
# b next by Eq(18.23)
i=1
while i<self.dimension-1:
BinF[i] = BinF[i-1] + self.H[i-1]*(self.CinF[i-1]+self.CinF[i])
i += 1
self.BinF = BinF
# 5. Solves D by Eq(18.18)
DinF = [[0 for _ in range(self.dimension)] for _ in range(self.dimension)]
DinF[-1] = np.matrix(DinF[-1])
i=0
while i<self.dimension-1:
DinF[i] = (self.CinF[i+1]-self.CinF[i])/(3*self.H[i])
i += 1
self.DinF = DinF
# 6. Populates A
AinF = np.zeros((self.dimension, self.dimension))
np.fill_diagonal(AinF, 1)
self.AinF = AinF
# 7. Weight matrix of F for an integral of the interpolated segments
W_inter = []
for i in range(self.dimension-1):
Wi = np.matrix(self.H[i]*self.AinF[i]\
+np.power(self.H[i],2)*self.BinF[i]/2\
+np.power(self.H[i],3)*self.CinF[i]/3\
+np.power(self.H[i],4)*self.DinF[i]/4)
W_inter.append(np.matrix(Wi))
# 8. Extrapolates and Updates Weights
self.W_extra = W_inter
if Extend in ["Linear","Natural"]:
hmin = Xs[0]-Xmin
Amin = hmin*AinF[0]
self.AinF[0] += Amin
Bmin = -np.power(hmin,2)*BinF[0]/2
self.BinF[0] += Bmin
self.Wmin = np.matrix(Amin + Bmin)
hmax = Xmax-Xs[-1]
Amax = hmax*AinF[-2]
self.AinF[-2] += Amax
Bmax = (np.power(hmax+self.H[-1],2)-np.power(self.H[-1],2))*BinF[-2]/2
self.BinF[-2] += Bmax
self.Wmax = np.matrix(Amax + Bmax)
if Extend == "Natural":
Cmin = 0
Dmin = -np.power(hmin,4)*DinF[0]/4
self.DinF[0] += Dmin
self.Wmin += Dmin
self.Wmin = np.matrix(self.Wmin)
Cmax = (np.power(hmax+self.H[-1],3)-np.power(self.H[-1],3))*CinF[-2]/3
self.CinF[-2] += Cmax
self.Wmax += Cmax
Dmax = (np.power(hmax+self.H[-1],4)-np.power(self.H[-1],4))*DinF[-2]/4
self.DinF[-2] += Dmax
self.Wmax += Dmax
self.Wmax = np.matrix(self.Wmax)
W_extra = [self.Wmin]
for i in range(0,self.dimension-1):
W_extra.append(self.W_extra[i])
W_extra.append(self.Wmax)
self.W_extra = W_extra
# 9. Integrates in segments
if Integrate == True:
self.I_seg = np.matmul(self.W_extra,np.array(fs).T)
self.I = sum(self.I_seg)
self.Iint = sum(np.matmul(W_inter,np.array(fs).T))
if Extend in ["Linear","Natural"]:
self.Imin = sum(np.matmul(self.Wmin,np.array(fs).T))
self.Imax = sum(np.matmul(self.Wmax,np.array(fs).T))
class Linear:
# constructor function
def __init__(self, Xs = [], fs = [], Integrate = True, Extend = "Linear", Xmin = None, Xmax = None):
self.Xs = Xs
self.fs = fs
self.dimension = len(Xs)
# 0. Computes H
self.H = [self.Xs[i]-self.Xs[i-1] for i in range(1,self.dimension)]
# 1. Populates A
AinF = np.zeros((self.dimension, self.dimension))
np.fill_diagonal(AinF, 1)
self.AinF = AinF
# 2. Solves B
BinF = [[0 for _ in range(self.dimension)] for _ in range(self.dimension)]
for i in range(1,self.dimension):
BinF[i-1] += (self.AinF[i]-self.AinF[i-1])/self.H[i-1]
BinF[-1] = np.matrix(BinF[-1])
self.BinF = BinF
# 3. Weight matrix of F for an integral of the interpolated segments
W_inter = []
for i in range(self.dimension-1):
Wi = np.matrix(self.H[i]*self.AinF[i]\
+np.power(self.H[i],2)*self.BinF[i]/2)
W_inter.append(np.matrix(Wi))
# 4. Extrapolates and Updates Weights
self.W_extra = W_inter
if Extend in ["Linear"]:
hmin = Xs[0]-Xmin
Amin = hmin*AinF[0]
self.AinF[0] += Amin
Bmin = -np.power(hmin,2)*BinF[0]/2
self.BinF[0] += Bmin
self.Wmin = np.matrix(Amin + Bmin)
hmax = Xmax-Xs[-1]
Amax = hmax*AinF[-2]
self.AinF[-2] += Amax
Bmax = (np.power(hmax+self.H[-1],2)-np.power(self.H[-1],2))*BinF[-2]/2
self.BinF[-2] += Bmax
self.Wmax = np.matrix(Amax + Bmax)
W_extra = [self.Wmin]
for i in range(0,self.dimension-1):
W_extra.append(self.W_extra[i])
W_extra.append(self.Wmax)
self.W_extra = W_extra
# 5. Integrates in segments
if Integrate == True:
self.I_seg = np.matmul(self.W_extra,np.array(fs).T)
self.I = sum(self.I_seg)
self.Iint = sum(np.matmul(W_inter,np.array(fs).T))
if Extend in ["Linear"]:
self.Imin = sum(np.matmul(self.Wmin,np.array(fs).T))
self.Imax = sum(np.matmul(self.Wmax,np.array(fs).T))