-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathspline.py
161 lines (126 loc) · 6.52 KB
/
spline.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
# -*- coding: utf-8 -*-
"""
Created on Thu Aug 27 13:57:14 2020
@author: sun.fa
"""
import numpy as np
import warnings
warnings.filterwarnings("ignore", category=RuntimeWarning)
warnings.filterwarnings("ignore", category=FutureWarning)
# ======================================
# class of Cubic B-spline basis
# ======================================
class splineBasis():
def __init__(self, knots, t_measurement, t_collocation):
"""
Get the basis matrices and derivative basis matrices of cubic b-spline
with given control points
knots: position of control points on time domain
t_measurement: measurement points on time domain
t_collocation: collocation points on time domain
"""
super(splineBasis, self).__init__()
self.knots = knots
self.t_m = t_measurement
self.t_c = t_collocation
def get_measurement(self):
"""
construct cubic b spline basis matrix for measurement basis
"""
b_spline_0 = np.zeros([len(self.t_m), len(self.knots)-1])
for i in range(b_spline_0.shape[0]):
for j in range(b_spline_0.shape[1]):
if self.t_m[i] >= self.knots[j] and self.t_m[i] < self.knots[j+1]:
b_spline_0[i, j] = 1
b_spline_1 = np.zeros([len(self.t_m), len(self.knots)-2])
for i in range(b_spline_1.shape[0]):
for j in range(b_spline_1.shape[1]):
basis_1 = (self.t_m[i] - self.knots[j]) / (self.knots[j+1] - self.knots[j]) * b_spline_0[i,j]
basis_2 = (self.knots[j+2] - self.t_m[i]) / (self.knots[j+2] - self.knots[j+1]) * b_spline_0[i,j+1]
if np.isnan(basis_1):
basis_1 = 0.0
if np.isnan(basis_2):
basis_2 = 0.0
b_spline_1[i,j] = basis_1 + basis_2
b_spline_2 = np.zeros([len(self.t_m), len(self.knots)-3])
for i in range(b_spline_2.shape[0]):
for j in range(b_spline_2.shape[1]):
basis_1 = (self.t_m[i] - self.knots[j]) / (self.knots[j+2] - self.knots[j]) * b_spline_1[i,j]
basis_2 = (self.knots[j+3] - self.t_m[i]) / (self.knots[j+3] - self.knots[j+1]) * b_spline_1[i,j+1]
if np.isnan(basis_1):
basis_1 = 0.0
if np.isnan(basis_2):
basis_2 = 0.0
b_spline_2[i,j] = basis_1 + basis_2
b_spline_3 = np.zeros([len(self.t_m), len(self.knots)-4])
for i in range(b_spline_3.shape[0]):
for j in range(b_spline_3.shape[1]):
basis_1 = (self.t_m[i] - self.knots[j]) / (self.knots[j+3] - self.knots[j]) * b_spline_2[i,j]
basis_2 = (self.knots[j+4] - self.t_m[i]) / (self.knots[j+4] - self.knots[j+1]) * b_spline_2[i,j+1]
if np.isnan(basis_1):
basis_1 = 0.0
if np.isnan(basis_2):
basis_2 = 0.0
b_spline_3[i,j] = basis_1 + basis_2
b_spline_3_dt = np.zeros([len(self.t_m), len(self.knots)-4])
for i in range(b_spline_3_dt.shape[0]):
for j in range(b_spline_3_dt.shape[1]):
basis_1 = b_spline_2[i,j] / (self.knots[j+3] - self.knots[j])
basis_2 = b_spline_2[i,j+1] / (self.knots[j+4] - self.knots[j+1])
if np.isnan(basis_1):
basis_1 = 0.0
if np.isnan(basis_2):
basis_2 = 0.0
b_spline_3_dt[i,j] = 3 * (basis_1 - basis_2)
return b_spline_3, b_spline_3_dt
def get_collocation(self):
"""
construct cubic b spline basis and its derivative basis matrices
for collocation domain
"""
b_spline_0 = np.zeros([len(self.t_c), len(self.knots)-1])
for i in range(b_spline_0.shape[0]):
for j in range(b_spline_0.shape[1]):
if self.t_c[i] >= self.knots[j] and self.t_c[i] < self.knots[j+1]:
b_spline_0[i, j] = 1
b_spline_1 = np.zeros([len(self.t_c), len(self.knots)-2])
for i in range(b_spline_1.shape[0]):
for j in range(b_spline_1.shape[1]):
basis_1 = (self.t_c[i] - self.knots[j]) / (self.knots[j+1] - self.knots[j]) * b_spline_0[i,j]
basis_2 = (self.knots[j+2] - self.t_c[i]) / (self.knots[j+2] - self.knots[j+1]) * b_spline_0[i,j+1]
if np.isnan(basis_1):
basis_1 = 0.0
if np.isnan(basis_2):
basis_2 = 0.0
b_spline_1[i,j] = basis_1 + basis_2
b_spline_2 = np.zeros([len(self.t_c), len(self.knots)-3])
for i in range(b_spline_2.shape[0]):
for j in range(b_spline_2.shape[1]):
basis_1 = (self.t_c[i] - self.knots[j]) / (self.knots[j+2] - self.knots[j]) * b_spline_1[i,j]
basis_2 = (self.knots[j+3] - self.t_c[i]) / (self.knots[j+3] - self.knots[j+1]) * b_spline_1[i,j+1]
if np.isnan(basis_1):
basis_1 = 0.0
if np.isnan(basis_2):
basis_2 = 0.0
b_spline_2[i,j] = basis_1 + basis_2
b_spline_3 = np.zeros([len(self.t_c), len(self.knots)-4])
for i in range(b_spline_3.shape[0]):
for j in range(b_spline_3.shape[1]):
basis_1 = (self.t_c[i] - self.knots[j]) / (self.knots[j+3] - self.knots[j]) * b_spline_2[i,j]
basis_2 = (self.knots[j+4] - self.t_c[i]) / (self.knots[j+4] - self.knots[j+1]) * b_spline_2[i,j+1]
if np.isnan(basis_1):
basis_1 = 0.0
if np.isnan(basis_2):
basis_2 = 0.0
b_spline_3[i,j] = basis_1 + basis_2
b_spline_3_dt = np.zeros([len(self.t_c), len(self.knots)-4])
for i in range(b_spline_3_dt.shape[0]):
for j in range(b_spline_3_dt.shape[1]):
basis_1 = b_spline_2[i,j] / (self.knots[j+3] - self.knots[j])
basis_2 = b_spline_2[i,j+1] / (self.knots[j+4] - self.knots[j+1])
if np.isnan(basis_1):
basis_1 = 0.0
if np.isnan(basis_2):
basis_2 = 0.0
b_spline_3_dt[i,j] = 3 * (basis_1 - basis_2)
return b_spline_3, b_spline_3_dt