diff --git a/rozniczkowanie/rozniczkowanie.py b/rozniczkowanie/rozniczkowanie.py index 6a3d1af..c0e69ac 100644 --- a/rozniczkowanie/rozniczkowanie.py +++ b/rozniczkowanie/rozniczkowanie.py @@ -1,33 +1,65 @@ +import math +from itertools import zip_longest -# x = [0.1, 0.3, 0.5, 0.7, 0.9, 1.1, 1.3] y = [20, 22.222222, 8, 4.081633, 2.469136, 1.652893, 0.83665] +# # z instrukcji, tylko do testu # x = [0.6, 0.7, 0.8, 0.9, 1.0] # y = [-0.51083, -0.35667, -0.22314, -0.10536, 0.0] -def oblicz(x): +h = math.ceil(x[1] - x[0]) + +def print_table(*columns): + def generate_headers(n): + headers = ["x", "f(x)"] + superscripts = ["\u2070", "\u00B9", "\u00B2", "\u00B3", "\u2074", "\u2075", "\u2076", "\u2077", "\u2078", "\u2079"] + for i in range(1, n): + exp = ''.join(superscripts[int(digit)] for digit in str(i)) + headers.append(f"∇{exp}f(x)") + return headers + headers = generate_headers(len(columns)) + + max_widths = [ + max(len(str(cell)) for cell in [header] + list(column)) + for header, column in zip(headers, columns) + ] + + def format_row(row): + return " | ".join(f"{str(cell).ljust(width)}" for cell, width in zip(row, max_widths)) + + print(format_row(headers)) + print("-" * (sum(max_widths) + 3 * (len(headers) - 1))) # Separator line + + for row in zip_longest(*columns, fillvalue=" "): + print(format_row(row)) + + print("-" * (sum(max_widths) + 3 * (len(headers) - 1))) + +def oblicz(array): result = [] - for i in range(len(x) - 1): - arr = [round(y[i] - y[i - 1], 5) for i in range(len(y) - 1, 0, -1)] + current = array + for _ in range(len(array) - 1): + arr = [round(current[j] - current[j - 1], 5) for j in range(len(current) - 1, 0, -1)] + arr.reverse() + result.append(arr) + current = arr + return result +def wynik(array): + temp = [] + suma = 0 + for x in array: + temp.append(x[-1]) + for x in temp: + if temp.index(x) == 0: + suma += x + elif temp.index(x) >= 1: + tik = 1/(temp.index(x) + 1) + suma += x*tik + print(f"f⁽¹⁾(x) = {round(suma*(1/h),5)}") - -h = x[1] - x[0] - -d2f = [] -for i in range(len(df) -1, -0, -1): - d2f.append(round(df[i] - df[i-1], 5)) - -d3f = [] -for i in range(len(d2f) -1, -0, -1): - d3f.append(round(d2f[i] - d2f[i-1], 5)) - -print(d3f) - - - - - - -f = 1/h \ No newline at end of file +result = oblicz(y) +columns = [x, y] + result +print_table(*columns) +wynik(result) \ No newline at end of file