27 lines
911 B
Python
27 lines
911 B
Python
from typing import Sequence
|
|
import numpy as np
|
|
|
|
"""Compute divided difference table in matrix form for Newton interpolation (Series 3)."""
|
|
def poly_mat(items: Sequence[tuple[float, float]]) -> np.ndarray:
|
|
"""
|
|
Compute the divided differences table (Newton interpolation) as a NumPy array.
|
|
|
|
Parameters:
|
|
items: Sequence of (x, y) data points.
|
|
Returns:
|
|
A 2D NumPy array of shape (N, N) containing the divided differences table.
|
|
"""
|
|
x_vals, y_vals = zip(*items)
|
|
N = len(items)
|
|
table = np.zeros((N, N), dtype=float)
|
|
# Zeroth column: y values
|
|
table[0, :] = y_vals
|
|
|
|
# Iterate over every column
|
|
for col in range(1, N):
|
|
for row in range(1, col + 1):
|
|
numerator = table[row - 1, col] - table[row - 1, col - 1]
|
|
denominator = x_vals[col] - x_vals[col - row]
|
|
table[row, col] = numerator / denominator
|
|
return table
|