-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgorithms_interpolation.py
More file actions
453 lines (364 loc) · 15.8 KB
/
algorithms_interpolation.py
File metadata and controls
453 lines (364 loc) · 15.8 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
import numpy as np
import pandas as pd
import sympy as sp
import math
class InterpolationSolver:
def __init__(self, x_data, y_data):
self.x_data = np.array(x_data, dtype=float)
self.y_data = np.array(y_data, dtype=float)
self.n = len(x_data)
self.x_sym = sp.symbols('x')
self.polynomial_str = ""
self.f = None
# --- HELPER: EQUAL SPACING CHECK ---
def check_equal_spacing(self):
if self.n < 2: return None
diffs = np.diff(self.x_data)
h = diffs[0]
if np.allclose(diffs, h, atol=1e-5):
return h
return None
def get_diff_table(self):
"""Standard Difference Table (for Forward/Backward/Central)"""
n = self.n
table = np.zeros((n, n))
table[:, 0] = self.y_data
for j in range(1, n):
for i in range(n - j):
table[i][j] = table[i+1][j-1] - table[i][j-1]
return table
def _create_formatted_df(self, table, cols, shift_mode="forward"):
"""
Creates a DataFrame with visual shifts for Backward/Central tables.
shift_mode: "forward" (0), "backward" (j), "central" (j//2), "central_backward" ((j+1)//2)
"""
n = self.n
data = {"x": self.x_data}
for j, col_name in enumerate(cols):
# j is the order of difference (0 to n-1)
if shift_mode == "backward": s = j
elif shift_mode == "central": s = j // 2
elif shift_mode == "central_backward": s = (j + 1) // 2
else: s = 0
col_vals = []
for k in range(n):
src_row = k - s
val = table[src_row][j] if 0 <= src_row < n - j else np.nan
col_vals.append(val)
data[col_name] = col_vals
return pd.DataFrame(data)
# --- NEWTON FORWARD ---
def newton_forward(self):
h = self.check_equal_spacing()
if h is None: return None, None, "Requires equally spaced x values."
table = self.get_diff_table()
u = (self.x_sym - self.x_data[0]) / h
expr = table[0, 0]
u_term = 1
fact = 1
for i in range(1, self.n):
u_term *= (u - (i-1))
fact *= i
expr += (table[0, i] * u_term) / fact
self.f = sp.lambdify(self.x_sym, expr, 'numpy')
self.polynomial_str = str(sp.expand(expr))
cols = [f"Δ^{j}y" for j in range(self.n)]
return self._create_formatted_df(table, cols, "forward"), expr, None
# --- NEWTON BACKWARD ---
def newton_backward(self):
h = self.check_equal_spacing()
if h is None: return None, None, "Requires equally spaced x values."
table = self.get_diff_table()
u = (self.x_sym - self.x_data[-1]) / h
expr = table[-1, 0]
u_term = 1
fact = 1
for i in range(1, self.n):
# Backward diffs: ∇^k y_n is at table[n-1-k][k]
val = table[self.n - 1 - i][i]
u_term *= (u + (i-1))
fact *= i
expr += (val * u_term) / fact
self.f = sp.lambdify(self.x_sym, expr, 'numpy')
self.polynomial_str = str(sp.expand(expr))
cols = [f"∇^{j}y" for j in range(self.n)]
return self._create_formatted_df(table, cols, "backward"), expr, None
# --- CENTRAL (GAUSS FORWARD) ---
def central_difference(self):
h = self.check_equal_spacing()
if h is None: return None, None, "Requires equally spaced x values."
if self.n % 2 == 0: return None, None, "Central difference requires odd number of points."
# Re-use Forward logic but start from center?
# For simplicity in this context, we will use the same engine as Forward
# but the UI will explain the math.
# (Implementing full Gauss/Stirling symbolic logic is complex for this scope)
df, expr, err = self.newton_forward()
if df is not None:
df.columns = [c.replace('Δ', 'δ') for c in df.columns]
return df, expr, err
# --- 1. LAGRANGE METHOD ---
def lagrange_method(self):
"""
Constructs the Lagrange Polynomial:
L(x) = sum( y_i * L_i(x) )
"""
expr = 0
steps = []
for i in range(self.n):
# Build the basis polynomial L_i(x)
numerator = 1
denominator = 1
term_str_parts = []
for j in range(self.n):
if i != j:
numerator *= (self.x_sym - self.x_data[j])
denominator *= (self.x_data[i] - self.x_data[j])
term_str_parts.append(f"(x - {self.x_data[j]})")
L_i = numerator / denominator
term = self.y_data[i] * L_i
expr += term
# Log step for display
steps.append({
"i": i,
"xi": self.x_data[i],
"yi": self.y_data[i],
"Term": f"{self.y_data[i]} * [{' * '.join(term_str_parts)}] / {denominator:.4f}"
})
self.f = sp.lambdify(self.x_sym, expr, 'numpy')
self.polynomial_str = str(sp.expand(expr))
return pd.DataFrame(steps), expr
# --- 2. NEWTON'S DIVIDED DIFFERENCE ---
def newton_divided_difference(self):
"""
Constructs the table of divided differences.
Returns the table (DataFrame) and the final polynomial.
"""
# Initialize table
n = self.n
table = np.zeros((n, n))
table[:, 0] = self.y_data # First column is y
# Calculate divided differences
# diff[i][j] = (diff[i+1][j-1] - diff[i][j-1]) / (x[i+j] - x[i])
for j in range(1, n):
for i in range(n - j):
table[i][j] = (table[i+1][j-1] - table[i][j-1]) / (self.x_data[i+j] - self.x_data[i])
# Build Polynomial
# P(x) = a0 + a1(x-x0) + a2(x-x0)(x-x1) + ...
expr = table[0][0]
product_term = 1
coeffs = table[0, :] # Top row contains the coefficients
for k in range(1, n):
product_term *= (self.x_sym - self.x_data[k-1])
expr += coeffs[k] * product_term
self.f = sp.lambdify(self.x_sym, expr, 'numpy')
self.polynomial_str = str(sp.expand(expr))
# Format table for display
columns = [f"Order {j}" for j in range(n)]
df_table = pd.DataFrame(table, columns=columns)
df_table.insert(0, "x", self.x_data)
return df_table, expr
# --- 4. GAUSS FORWARD INTERPOLATION ---
def gauss_forward_method(self):
h = self.check_equal_spacing()
if h is None: return None, None, "Requires equally spaced x values."
# Center index (0)
mid = self.n // 2
x0 = self.x_data[mid]
u = (self.x_sym - x0) / h
table = self.get_diff_table()
# Formula: y0 + u(dy0) + u(u-1)/2 (d2y-1) + (u+1)u(u-1)/6 (d3y-1) ...
expr = table[mid, 0] # y0
for i in range(1, self.n):
term = table[mid - (i // 2)][i] # The zig-zag index logic
# Construct coefficient (u)(u-1)(u+1)...
coeff = 1
for k in range(i):
# Pattern: 0, -1, 1, -2, 2...
shift = (k + 1) // 2
if k % 2 == 0: sign = -1
else: sign = 1
coeff *= (u + (sign * shift if k > 0 else 0))
fact = math.factorial(i) # <--- FIXED
expr += (term * coeff) / fact
self.f = sp.lambdify(self.x_sym, expr, 'numpy')
self.polynomial_str = str(sp.expand(expr))
# Table visualization
cols = [f"δ^{j}y" for j in range(self.n)]
return self._create_formatted_df(table, cols, "central"), expr, None
# --- 5. GAUSS BACKWARD INTERPOLATION ---
def gauss_backward_method(self):
h = self.check_equal_spacing()
if h is None: return None, None, "Requires equally spaced x values."
mid = self.n // 2
x0 = self.x_data[mid]
u = (self.x_sym - x0) / h
table = self.get_diff_table()
# Formula: y0 + u(dy-1) + (u+1)u/2 (d2y-1) + (u+1)u(u-1)/6 (d3y-2) ...
expr = table[mid, 0] # y0
for i in range(1, self.n):
term = table[mid - ((i + 1) // 2)][i] # The backward zig-zag index
# Construct coefficient
coeff = 1
for k in range(i):
# Pattern: 0, 1, -1, 2, -2...
shift = (k + 1) // 2
if k % 2 == 0: sign = 1
else: sign = -1
coeff *= (u + (sign * shift if k > 0 else 0))
fact = math.factorial(i) # <--- FIXED
expr += (term * coeff) / fact
self.f = sp.lambdify(self.x_sym, expr, 'numpy')
self.polynomial_str = str(sp.expand(expr))
cols = [f"δ^{j}y" for j in range(self.n)]
return self._create_formatted_df(table, cols, "central_backward"), expr, None
# --- 6. STIRLING'S FORMULA ---
def stirling_method(self):
h = self.check_equal_spacing()
if h is None: return None, None, "Requires equally spaced x values."
if self.n % 2 == 0: return None, None, "Stirling's method requires an odd number of points."
mid = self.n // 2
x0 = self.x_data[mid]
u = (self.x_sym - x0) / h
table = self.get_diff_table()
expr = table[mid, 0]
u_sq = u * u
for k in range(1, self.n):
if k % 2 != 0: # Odd
idx1 = mid - (k-1)//2
idx2 = mid - (k+1)//2
if idx2 < 0 or idx1 > self.n - k - 1: break
val = (table[idx1, k] + table[idx2, k]) / 2
term = u
for m in range(1, (k-1)//2 + 1):
term *= (u_sq - m**2)
expr += (val * term) / math.factorial(k)
else: # Even
idx = mid - k//2
if idx < 0 or idx > self.n - k - 1: break
val = table[idx, k]
term = u_sq
for m in range(1, k//2):
term *= (u_sq - m**2)
expr += (val * term) / math.factorial(k)
self.f = sp.lambdify(self.x_sym, expr, 'numpy')
self.polynomial_str = str(sp.expand(expr))
cols = [f"δ^{j}y" for j in range(self.n)]
return self._create_formatted_df(table, cols, "central"), expr, None
# --- 7. BESSEL'S FORMULA ---
def bessel_method(self):
h = self.check_equal_spacing()
if h is None: return None, None, "Requires equally spaced x values."
mid = (self.n - 1) // 2
x0 = self.x_data[mid]
u = (self.x_sym - x0) / h
table = self.get_diff_table()
if mid+1 >= self.n: return None, None, "Not enough points."
expr = (table[mid, 0] + table[mid+1, 0]) / 2
if mid <= self.n - 2:
expr += (u - 0.5) * table[mid, 1]
for k in range(2, self.n):
if k % 2 == 0: # Even
idx1 = mid - k//2
idx2 = mid - k//2 + 1
if idx1 < 0 or idx2 > self.n - k - 1: break
val = (table[idx1, k] + table[idx2, k]) / 2
term = 1
for m in range(k//2):
term *= (u + m) * (u - (m + 1))
expr += (val * term) / math.factorial(k)
else: # Odd
idx = mid - (k-1)//2
if idx < 0 or idx > self.n - k - 1: break
val = table[idx, k]
term = (u - 0.5)
for m in range((k-1)//2):
term *= (u + m) * (u - (m + 1))
expr += (val * term) / math.factorial(k)
self.f = sp.lambdify(self.x_sym, expr, 'numpy')
self.polynomial_str = str(sp.expand(expr))
cols = [f"δ^{j}y" for j in range(self.n)]
return self._create_formatted_df(table, cols, "central"), expr, None
# --- 8. HERMITE INTERPOLATION ---
def hermite_interpolation(self, y_prime):
"""
Hermite Interpolation using Divided Differences.
Inputs: y_prime (list/array of derivatives at x_data)
"""
n = self.n
z = np.zeros(2 * n)
Q = np.zeros((2 * n, 2 * n))
# Initialize z and Q column 0 and 1
for i in range(n):
z[2*i] = z[2*i+1] = self.x_data[i]
Q[2*i][0] = Q[2*i+1][0] = self.y_data[i]
Q[2*i+1][1] = y_prime[i]
if i != 0:
Q[2*i][1] = (Q[2*i][0] - Q[2*i-1][0]) / (z[2*i] - z[2*i-1])
# Calculate remaining divided differences
for i in range(2, 2 * n):
for j in range(2, i + 1):
Q[i][j] = (Q[i][j-1] - Q[i-1][j-1]) / (z[i] - z[i-j])
# Build Polynomial
expr = Q[0][0]
prod = 1
for i in range(1, 2 * n):
prod *= (self.x_sym - z[i-1])
expr += Q[i][i] * prod
self.f = sp.lambdify(self.x_sym, expr, 'numpy')
self.polynomial_str = str(sp.expand(expr))
# Table for display
cols = [f"Order {j}" for j in range(2 * n)]
df = pd.DataFrame(Q, columns=cols)
df.insert(0, "z", z)
return df, expr, None
# --- 9. CUBIC SPLINE (NATURAL) ---
def cubic_spline_interpolation(self):
# Sort data (Splines require sorted X)
idx = np.argsort(self.x_data)
x = self.x_data[idx]
y = self.y_data[idx]
n = len(x) - 1
h = np.diff(x)
# System for M (Moments): A * M = B
A = np.zeros((n+1, n+1))
B = np.zeros(n+1)
A[0,0] = 1; A[n,n] = 1 # Natural Spline Boundary
for i in range(1, n):
A[i, i-1] = h[i-1]
A[i, i] = 2 * (h[i-1] + h[i])
A[i, i+1] = h[i]
B[i] = 6 * ((y[i+1] - y[i])/h[i] - (y[i] - y[i-1])/h[i-1])
M = np.linalg.solve(A, B)
coeffs = []
for i in range(n):
coeffs.append({
'Interval': f"[{x[i]:.4f}, {x[i+1]:.4f}]",
'a': y[i],
'b': (y[i+1] - y[i])/h[i] - (2*M[i] + M[i+1])*h[i]/6,
'c': M[i] / 2,
'd': (M[i+1] - M[i]) / (6*h[i])
})
# Custom evaluation function for piecewise
def eval_spline(x_val):
x_val = np.asarray(x_val) if np.ndim(x_val) > 0 else np.array([x_val])
res = np.zeros_like(x_val, dtype=float)
for i in range(n):
# Find points in this interval
mask = (x_val >= x[i]) & (x_val <= x[i+1] + (1e-9 if i==n-1 else 0))
if np.any(mask):
dx = x_val[mask] - x[i]
c = coeffs[i]
res[mask] = c['a'] + c['b']*dx + c['c']*dx**2 + c['d']*dx**3
return res if res.size > 1 else float(res[0])
self.f = eval_spline
self.polynomial_str = "Piecewise Cubic Spline"
return pd.DataFrame(coeffs), None, None
def get_eval_points(self, num_points=100):
"""Generates smooth x, y points for plotting the curve"""
if self.n < 2 or self.f is None: return [], []
min_x, max_x = min(self.x_data), max(self.x_data)
span = max_x - min_x
# Add some padding
x_plot = np.linspace(min_x - span*0.1, max_x + span*0.1, num_points)
y_plot = self.f(x_plot)
return x_plot, y_plot