Python-Math-Algorithms/newton_polynom/__init__.py

41 lines
1.3 KiB
Python

# Python module for calculating the newton polynom from given points
# _ _ _ _
# __ ___ __(_) |_| |_ ___ _ __ | |__ _ _
# \ \ /\ / / '__| | __| __/ _ \ '_ \ | '_ \| | | |
# \ V V /| | | | |_| || __/ | | | | |_) | |_| |
# \_/\_/ |_| |_|\__|\__\___|_| |_| |_.__/ \__, |
# |___/
# ____ __ __ _
# / ___|_ _____ _ __ \ \ / /__ __ _ ___| |
# \___ \ \ / / _ \ '_ \ \ \ / / _ \ / _` |/ _ \ |
# ___) \ V / __/ | | | \ V / (_) | (_| | __/ |
# |____/ \_/ \___|_| |_| \_/ \___/ \__, |\___|_|
# |___/
# Licensed under the GPLv2 License, Version 2.0 (the "License");
# Copyright (c) Sven Vogel
def combine(p0, p1):
return (p1[1] - p1[0]) / (p0[1] - p0[0])
def combine_n(*points):
k = len(points) - 1
if k == 1:
return combine(points[0], points[1])
else:
return (combine_n(points[1:k]) - combine_n(points[0:(k - 1)])) / (points[k][0] - points[0][0])
def newton_polynom(*points):
for x in range(len(points)):
print(combine_n(points[0:x]))
for y in range(x):
print(format(" * (x - %s)", points[y][0]))
def test():
newton_polynom([1, 2], [3, 4], [9, -5])