added several modules

This commit is contained in:
Sven Vogel 2023-04-25 15:37:03 +02:00
parent c864ab7451
commit 7310527c78
4 changed files with 125 additions and 0 deletions

View File

@ -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))

View File

@ -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))

23
main.py Normal file
View File

@ -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()

View File

@ -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])