Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/numerical/calculus.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,18 @@ def lagrange_interpolation(points: List[Tuple[float, float]], x: float) -> float
result = 0.0
n = len(points)

# Precompute x and y values for faster access
x_vals = [pt[0] for pt in points]
y_vals = [pt[1] for pt in points]

for i in range(n):
term = points[i][1]
term = y_vals[i]
xi = x_vals[i]
for j in range(n):
if i != j:
term *= (x - points[j][0]) / (points[i][0] - points[j][0])
# Perform division at each step to maintain numerical stability
# and preserve the original behavior (raising ZeroDivisionError)
term *= (x - x_vals[j]) / (xi - x_vals[j])
result += term

return result
Expand Down