diff --git a/fixpoint_approximation/__init__.py b/fixpoint_approximation/__init__.py new file mode 100644 index 0000000..a316fd2 --- /dev/null +++ b/fixpoint_approximation/__init__.py @@ -0,0 +1,34 @@ +# Python module for approximating the fixpoint of special linear functions +# _ _ _ _ +# __ ___ __(_) |_| |_ ___ _ __ | |__ _ _ +# \ \ /\ / / '__| | __| __/ _ \ '_ \ | '_ \| | | | +# \ V V /| | | | |_| || __/ | | | | |_) | |_| | +# \_/\_/ |_| |_|\__|\__\___|_| |_| |_.__/ \__, | +# |___/ +# ____ __ __ _ +# / ___|_ _____ _ __ \ \ / /__ __ _ ___| | +# \___ \ \ / / _ \ '_ \ \ \ / / _ \ / _` |/ _ \ | +# ___) \ V / __/ | | | \ V / (_) | (_| | __/ | +# |____/ \_/ \___|_| |_| \_/ \___/ \__, |\___|_| +# |___/ +# Licensed under the GPLv2 License, Version 2.0 (the "License"); +# Copyright (c) Sven Vogel + + +# iteratively approximate the fixpoint of specific linear functions +def fixpoint_approximation(start, function, iterations): + x = start + for _ in range(iterations): + x = function(x) + + return x + + +# in order to work the linear function has to be in the form: +# ]-1.0, 1.0[ * x + k +def linear_function(x): + return 0.25 * x - 3 + + +def test(): + print("fixpoint approximation: ", fixpoint_approximation(start=1.0, function=linear_function, iterations=400)) diff --git a/linear_approximation/__init__.py b/linear_approximation/__init__.py new file mode 100644 index 0000000..4137a44 --- /dev/null +++ b/linear_approximation/__init__.py @@ -0,0 +1,28 @@ +# Python module for linearly approximating the derivative of any function +# _ _ _ _ +# __ ___ __(_) |_| |_ ___ _ __ | |__ _ _ +# \ \ /\ / / '__| | __| __/ _ \ '_ \ | '_ \| | | | +# \ V V /| | | | |_| || __/ | | | | |_) | |_| | +# \_/\_/ |_| |_|\__|\__\___|_| |_| |_.__/ \__, | +# |___/ +# ____ __ __ _ +# / ___|_ _____ _ __ \ \ / /__ __ _ ___| | +# \___ \ \ / / _ \ '_ \ \ \ / / _ \ / _` |/ _ \ | +# ___) \ V / __/ | | | \ V / (_) | (_| | __/ | +# |____/ \_/ \___|_| |_| \_/ \___/ \__, |\___|_| +# |___/ +# Licensed under the GPLv2 License, Version 2.0 (the "License"); +# Copyright (c) Sven Vogel + +# linearly approximate a functions derivative in an interval +def linear_approximate(interval, function): + return (function(interval[1]) - function(interval[0])) / (interval[1] - interval[0]) + + +# function to linearly approximate +def f(x): + return 3 + x * x - 5 * x + + +def test(): + print("linear approximation: ", linear_approximate([2.0, 3.0], f)) diff --git a/main.py b/main.py new file mode 100644 index 0000000..fc9947e --- /dev/null +++ b/main.py @@ -0,0 +1,23 @@ +# Python file for testing various approximation algorithms +# _ _ _ _ +# __ ___ __(_) |_| |_ ___ _ __ | |__ _ _ +# \ \ /\ / / '__| | __| __/ _ \ '_ \ | '_ \| | | | +# \ V V /| | | | |_| || __/ | | | | |_) | |_| | +# \_/\_/ |_| |_|\__|\__\___|_| |_| |_.__/ \__, | +# |___/ +# ____ __ __ _ +# / ___|_ _____ _ __ \ \ / /__ __ _ ___| | +# \___ \ \ / / _ \ '_ \ \ \ / / _ \ / _` |/ _ \ | +# ___) \ V / __/ | | | \ V / (_) | (_| | __/ | +# |____/ \_/ \___|_| |_| \_/ \___/ \__, |\___|_| +# |___/ +# Licensed under the GPLv2 License, Version 2.0 (the "License"); +# Copyright (c) Sven Vogel + +import fixpoint_approximation +import linear_approximation +import newton_polynom + +# linear_approximation.test() +# fixpoint_approximation.test() +newton_polynom.test() diff --git a/newton_polynom/__init__.py b/newton_polynom/__init__.py new file mode 100644 index 0000000..f71e814 --- /dev/null +++ b/newton_polynom/__init__.py @@ -0,0 +1,40 @@ +# 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])