-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpolynomial_interpolation.py
More file actions
482 lines (382 loc) · 16 KB
/
Copy pathpolynomial_interpolation.py
File metadata and controls
482 lines (382 loc) · 16 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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
"""Polynomial interpolation experiments in Python.
The module contains a reusable implementation of Newton interpolation with
divided differences and a command-line experiment inspired by a scientific
computing activity. It compares:
- Barycentric polynomial interpolation
- Lagrange polynomial interpolation
- Newton polynomial interpolation
- Cubic spline interpolation
The default experiment evaluates sin(x), Runge's function and exp(-20*x**2) on
[-1, 1] with equispaced and Chebyshev nodes.
"""
from __future__ import annotations
import argparse
import csv
from dataclasses import dataclass
from pathlib import Path
from time import perf_counter
from typing import Callable, Iterable
import matplotlib.pyplot as plt
import numpy as np
try:
from scipy.interpolate import (
BarycentricInterpolator,
InterpolatedUnivariateSpline,
lagrange,
)
except ImportError: # pragma: no cover - exercised only without SciPy.
BarycentricInterpolator = None
InterpolatedUnivariateSpline = None
lagrange = None
Array = np.ndarray
Function = Callable[[Array], Array]
@dataclass(frozen=True)
class TestFunction:
"""Function used by the interpolation benchmark."""
key: str
label: str
function: Function
@dataclass(frozen=True)
class InterpolationResult:
"""Numerical result for one function/node/method combination."""
function: str
n_nodes: int
node_type: str
method: str
max_error: float
l2_error: float
build_time: float
eval_time: float
@property
def total_time(self) -> float:
"""Return construction time plus evaluation time."""
return self.build_time + self.eval_time
def f_sin(x: Array) -> Array:
"""Return sin(x)."""
return np.sin(x)
def f_runge(x: Array) -> Array:
"""Return Runge's classical interpolation test function."""
return 1 / (1 + 25 * x**2)
def f_gaussian(x: Array) -> Array:
"""Return exp(-20*x**2)."""
return np.exp(-20 * x**2)
TEST_FUNCTIONS = (
TestFunction("sin", "f1(x)=sin(x)", f_sin),
TestFunction("runge", "f2(x)=1/(1+25x**2)", f_runge),
TestFunction("gaussian", "f3(x)=exp(-20x**2)", f_gaussian),
)
def equispaced_nodes(a: float, b: float, n: int) -> Array:
"""Return n equispaced nodes on [a, b]."""
validate_interval(a, b)
validate_node_count(n)
return np.linspace(a, b, n)
def chebyshev_nodes(a: float, b: float, n: int) -> Array:
"""Return n Chebyshev roots mapped from [-1, 1] to [a, b]."""
validate_interval(a, b)
validate_node_count(n)
k = np.arange(1, n + 1)
roots = np.cos((2 * k - 1) * np.pi / (2 * n))
return 0.5 * (b - a) * roots + 0.5 * (a + b)
def divided_difference_table(x_values: Iterable[float],
y_values: Iterable[float]) -> Array:
"""Return the Newton divided-difference table."""
x, y = validated_xy(x_values, y_values)
n = len(x)
table = np.zeros((n, n), dtype=float)
table[:, 0] = y
for order in range(1, n):
for row in range(n - order):
numerator = table[row + 1, order - 1] - table[row, order - 1]
denominator = x[row + order] - x[row]
table[row, order] = numerator / denominator
return table
def newton_coefficients(x_values: Iterable[float],
y_values: Iterable[float]) -> Array:
"""Return Newton-form polynomial coefficients."""
return divided_difference_table(x_values, y_values)[0, :]
def evaluate_newton_polynomial(coefficients: Iterable[float],
x_nodes: Iterable[float],
x_eval: Iterable[float] | float) -> Array:
"""Evaluate a Newton-form interpolation polynomial."""
coefficients = np.asarray(coefficients, dtype=float)
x_nodes = np.asarray(x_nodes, dtype=float)
x_eval = np.asarray(x_eval, dtype=float)
if coefficients.ndim != 1 or x_nodes.ndim != 1:
raise ValueError("coefficients and x_nodes must be one-dimensional.")
if len(coefficients) != len(x_nodes):
raise ValueError("coefficients and x_nodes must have the same length.")
if len(coefficients) == 0:
raise ValueError("At least one coefficient is required.")
result = np.full_like(x_eval, coefficients[-1], dtype=float)
for i in range(len(coefficients) - 2, -1, -1):
result = result * (x_eval - x_nodes[i]) + coefficients[i]
return result
def interpolate_newton(x_values: Iterable[float],
y_values: Iterable[float],
x_eval: Iterable[float] | float) -> Array:
"""Build and evaluate the Newton interpolating polynomial."""
coefficients = newton_coefficients(x_values, y_values)
return evaluate_newton_polynomial(coefficients, x_values, x_eval)
def error_metrics(y_true: Array, y_approx: Array,
x_grid: Array) -> tuple[float, float]:
"""Return maximum absolute error and approximate L2 error."""
error = np.abs(y_approx - y_true)
return float(np.max(error)), float(np.sqrt(np.trapezoid(error**2, x_grid)))
def measure_time(operation: Callable[[], object],
repetitions: int = 20) -> tuple[object, float]:
"""Return operation result and median runtime in seconds."""
if repetitions <= 0:
raise ValueError("repetitions must be positive.")
timings = []
result = None
for _ in range(repetitions):
start = perf_counter()
result = operation()
timings.append(perf_counter() - start)
return result, float(np.median(timings))
def run_experiment(n_values: Iterable[int] = (11, 21),
node_types: Iterable[str] = ("equispaced", "chebyshev"),
x_range: tuple[float, float] = (-1.0, 1.0),
grid_size: int = 1000,
repetitions: int = 20,
make_plots: bool = False,
output_dir: Path | None = None) -> list[InterpolationResult]:
"""Run the full interpolation comparison."""
a, b = x_range
validate_interval(a, b)
if grid_size < 2:
raise ValueError("grid_size must be at least 2.")
x_grid = np.linspace(a, b, grid_size)
results: list[InterpolationResult] = []
for test_function in TEST_FUNCTIONS:
y_true = test_function.function(x_grid)
for n in n_values:
for node_type in node_types:
x_nodes = make_nodes(node_type, a, b, n)
y_nodes = test_function.function(x_nodes)
x_nodes, y_nodes = sort_nodes(x_nodes, y_nodes)
method_values = evaluate_methods(
x_nodes, y_nodes, x_grid, repetitions
)
for method, (y_approx, build_time, eval_time) in method_values:
max_error, l2_error = error_metrics(
y_true, y_approx, x_grid
)
results.append(
InterpolationResult(
function=test_function.label,
n_nodes=n,
node_type=node_type,
method=method,
max_error=max_error,
l2_error=l2_error,
build_time=build_time,
eval_time=eval_time,
)
)
if make_plots:
plot_comparison(
test_function.label,
node_type,
x_grid,
y_true,
x_nodes,
y_nodes,
method_values,
output_dir,
)
return results
def evaluate_methods(x_nodes: Array, y_nodes: Array, x_grid: Array,
repetitions: int) -> list[tuple[str, tuple[Array,
float,
float]]]:
"""Build and evaluate every available interpolation method."""
values = []
if BarycentricInterpolator is not None:
interpolator, build_time = measure_time(
lambda: BarycentricInterpolator(x_nodes, y_nodes), repetitions
)
y_approx, eval_time = measure_time(
lambda: interpolator(x_grid), repetitions
)
values.append(("barycentric", (y_approx, build_time, eval_time)))
if lagrange is not None:
polynomial, build_time = measure_time(
lambda: lagrange(x_nodes, y_nodes), repetitions
)
y_approx, eval_time = measure_time(lambda: polynomial(x_grid),
repetitions)
values.append(("lagrange", (y_approx, build_time, eval_time)))
coefficients, build_time = measure_time(
lambda: newton_coefficients(x_nodes, y_nodes), repetitions
)
y_approx, eval_time = measure_time(
lambda: evaluate_newton_polynomial(coefficients, x_nodes, x_grid),
repetitions,
)
values.append(("newton", (y_approx, build_time, eval_time)))
if InterpolatedUnivariateSpline is not None:
spline, build_time = measure_time(
lambda: InterpolatedUnivariateSpline(x_nodes, y_nodes, k=3),
repetitions,
)
y_approx, eval_time = measure_time(lambda: spline(x_grid), repetitions)
values.append(("cubic_spline", (y_approx, build_time, eval_time)))
return values
def make_nodes(node_type: str, a: float, b: float, n: int) -> Array:
"""Create interpolation nodes by name."""
if node_type == "equispaced":
return equispaced_nodes(a, b, n)
if node_type == "chebyshev":
return chebyshev_nodes(a, b, n)
raise ValueError(f"Unknown node type: {node_type}")
def sort_nodes(x_nodes: Array, y_nodes: Array) -> tuple[Array, Array]:
"""Return nodes and values sorted by x."""
idx = np.argsort(x_nodes)
return x_nodes[idx], y_nodes[idx]
def write_results_csv(results: list[InterpolationResult],
output_file: Path) -> None:
"""Write experiment results to a CSV file."""
output_file.parent.mkdir(parents=True, exist_ok=True)
with output_file.open("w", newline="", encoding="utf-8") as file:
writer = csv.DictWriter(file, fieldnames=result_fieldnames())
writer.writeheader()
for row in results:
writer.writerow(result_to_dict(row))
def print_results_table(results: list[InterpolationResult]) -> None:
"""Print a compact table with all experiment results."""
headers = result_fieldnames()
rows = [result_to_dict(result) for result in results]
widths = {
header: max(len(header), *(len(str(row[header])) for row in rows))
for header in headers
}
print(" | ".join(header.ljust(widths[header]) for header in headers))
print("-+-".join("-" * widths[header] for header in headers))
for row in rows:
print(" | ".join(str(row[header]).ljust(widths[header])
for header in headers))
def plot_comparison(function_label: str,
node_type: str,
x_grid: Array,
y_true: Array,
x_nodes: Array,
y_nodes: Array,
method_values: list[tuple[str, tuple[Array,
float,
float]]],
output_dir: Path | None = None) -> None:
"""Plot one function/node comparison."""
plt.figure(figsize=(9, 5.5))
plt.plot(x_grid, y_true, color="black", label="Real function")
styles = {
"barycentric": "r--",
"lagrange": "b-.",
"newton": "g:",
"cubic_spline": "m-",
}
for method, (y_approx, _, _) in method_values:
plt.plot(x_grid, y_approx, styles.get(method, "-"), label=method)
plt.scatter(x_nodes, y_nodes, color="black", s=25, label="Nodes", zorder=3)
plt.title(f"{function_label} with {len(x_nodes)} {node_type} nodes")
plt.xlabel("x")
plt.ylabel("y")
plt.grid(True, alpha=0.35)
plt.legend()
plt.tight_layout()
if output_dir is None:
plt.show()
else:
output_dir.mkdir(parents=True, exist_ok=True)
filename = safe_filename(f"{function_label}_{len(x_nodes)}_{node_type}")
plt.savefig(output_dir / f"{filename}.png", dpi=160)
plt.close()
def result_fieldnames() -> list[str]:
"""Return CSV/table field names."""
return [
"function",
"n_nodes",
"node_type",
"method",
"max_error",
"l2_error",
"build_time",
"eval_time",
"total_time",
]
def result_to_dict(result: InterpolationResult) -> dict[str, str | int]:
"""Convert one result to printable/CSV values."""
return {
"function": result.function,
"n_nodes": result.n_nodes,
"node_type": result.node_type,
"method": result.method,
"max_error": f"{result.max_error:.6e}",
"l2_error": f"{result.l2_error:.6e}",
"build_time": f"{result.build_time:.6e}",
"eval_time": f"{result.eval_time:.6e}",
"total_time": f"{result.total_time:.6e}",
}
def safe_filename(value: str) -> str:
"""Return a filesystem-friendly filename fragment."""
return "".join(char if char.isalnum() else "_" for char in value).strip("_")
def validated_xy(x_values: Iterable[float],
y_values: Iterable[float]) -> tuple[Array, Array]:
"""Validate interpolation data and return NumPy arrays."""
x = np.asarray(x_values, dtype=float)
y = np.asarray(y_values, dtype=float)
if x.ndim != 1 or y.ndim != 1:
raise ValueError("x_values and y_values must be one-dimensional.")
if len(x) != len(y):
raise ValueError("x_values and y_values must have the same length.")
if len(x) == 0:
raise ValueError("At least one interpolation point is required.")
if len(np.unique(x)) != len(x):
raise ValueError("x_values must not contain repeated nodes.")
return x, y
def validate_interval(a: float, b: float) -> None:
"""Validate an interpolation interval."""
if not a < b:
raise ValueError("The interval must satisfy a < b.")
def validate_node_count(n: int) -> None:
"""Validate the number of interpolation nodes."""
if n < 2:
raise ValueError("At least two interpolation nodes are required.")
def parse_args() -> argparse.Namespace:
"""Parse command-line arguments."""
parser = argparse.ArgumentParser(
description="Compare Newton, Lagrange, barycentric and cubic spline "
"interpolation on scientific-computing examples."
)
parser.add_argument("--nodes", nargs="+", type=int, default=[11, 21],
help="Node counts to test. Default: 11 21.")
parser.add_argument("--grid-size", type=int, default=1000,
help="Number of evaluation points. Default: 1000.")
parser.add_argument("--repetitions", type=int, default=20,
help="Timing repetitions. Default: 20.")
parser.add_argument("--plot", action="store_true",
help="Show comparison plots.")
parser.add_argument("--save-plots", type=Path,
help="Directory where plots will be saved as PNG.")
parser.add_argument("--csv", type=Path,
help="Optional CSV output path for the results.")
return parser.parse_args()
def main() -> None:
"""Run the command-line experiment."""
args = parse_args()
output_dir = args.save_plots
results = run_experiment(
n_values=args.nodes,
grid_size=args.grid_size,
repetitions=args.repetitions,
make_plots=args.plot or output_dir is not None,
output_dir=output_dir,
)
print_results_table(results)
if args.csv is not None:
write_results_csv(results, args.csv)
print(f"\nResults saved to {args.csv}")
if BarycentricInterpolator is None:
print("\nSciPy was not found. Only the Newton method was executed.")
if __name__ == "__main__":
main()