diff --git a/acceleration.py b/acceleration.py index baf30ed..07ba5ad 100644 --- a/acceleration.py +++ b/acceleration.py @@ -1,42 +1,47 @@ import math +import numpy as np -def Aitken_tranform(items: list, steps=-1) -> list: +# SET DATA TYPE +DT = np.dtype('float64') # float 64 bits + + +def Aitken_tranform(items: np.ndarray, steps=-1) -> np.ndarray: if steps == -1: steps = int((len(items) - 1) / 2) - + for _ in range(steps): - acel = [] + acel = np.zeros(len(items) - 2, dtype=DT) for i in range(0, len(items) - 2): - acel.append((items[i] * items[i+2] - items[i+1]**2) / \ - (items[i+2] - 2* items[i+1] + items[i])) + acel[i] = (items[i] * items[i+2] - items[i+1]**2) / \ + (items[i+2] - 2 * items[i+1] + items[i]) items = acel if len(acel) < 3: return acel - + return acel -def Richardson_transform(items: list, p=1, steps=-1) -> list: +def Richardson_transform(items: np.ndarray, p=1, steps=-1) -> np.ndarray: """Receive a p that represents the power of the Richardson transform""" if steps == -1: steps = int(math.log2(len(items))) - 1 for _ in range(steps): - acel = [] + acel = np.zeros(int(len(items)/2), dtype=DT) for i in range(0, int(len(items)/2)): - acel.append(items[2*i] + (items[2*i] - items[i]) / (2**p - 1)) + acel[i] = items[2*i] + (items[2*i] - items[i]) / (2**p - 1) items = acel p = p + 1 return acel -def Epsilon_transfom(items: list, steps=-1) -> list: +def Epsilon_transfom(items: np.ndarray, steps=-1) -> np.ndarray: # Initial values - aux = [0 for _ in range(len(items)+1)] + aux = np.zeros(len(items)+1, dtype=DT) acel = items if steps == -1: @@ -54,15 +59,16 @@ def Epsilon_transfom(items: list, steps=-1) -> list: return acel - -def G_transform(items: list, steps=-1) -> list: +def G_transform(items: np.ndarray, steps=-1) -> np.ndarray: # Initial values - aux1 = [1 for _ in range(len(items))] - aux2 = [items[0]] + [items[i+1] - items[i] for i in range(len(items)-1)] - acel = items + aux1 = np.ones(len(items), dtype=DT) + aux2 = np.zeros(len(items), dtype=DT) - if steps == -1: - steps = len(items) - 1 + aux2[0] = items[0] + for i in range(1, len(items)): + aux2[i] = items[i] - items[i-1] + + acel = items for _ in range(steps): for i in range(len(aux1) - 2): @@ -73,12 +79,10 @@ def G_transform(items: list, steps=-1) -> list: aux2[i] = aux2[i+1] * (aux1[i+1] / aux1[i] - 1) aux2 = aux2[:-1] - - print(f"{len(aux1)}, {len(aux2)}, {len(acel)}") for i in range(len(acel) - 2): acel[i] = acel[i] - aux2[i] * (acel[i+1] - acel[i])/(aux2[i+1] - aux2[i]) acel = acel[:-1] - + return acel diff --git a/test.py b/test.py index 54c109e..aa728a7 100644 --- a/test.py +++ b/test.py @@ -1,21 +1,21 @@ import math +import numpy as np from acceleration import Aitken_tranform, Richardson_transform, Epsilon_transfom, G_transform -def square_series(n: int) -> list: - series = [1.0] +# SET DATA TYPE +DT = np.dtype('float64') # float 64 bits - for i in range(2, n+1): - series.append(series[-1] + 1/(i)**2) +def square_series(n: int) -> np.ndarray: + """Zeta(2) series, sum of math.pi**2 / 6""" + series = np.zeros(n, dtype=DT) + series[0] = 1 + + for i in range(1, n): + series[i] = series[i-1] + 1/(i+1)**2 return series if __name__ == "__main__": - for i in range(1, 6): - initial_series = abs(math.pi**2 / 6 - square_series(10**i)[-1]) - acceration_series = abs(math.pi**2 / 6 - Aitken_tranform(square_series(10**i))[-1]) - - print(f"Error in initial series with n={10**i}: {initial_series}") - print(f"Errro in acceleration series with n={10**i}: {acceration_series}") - print("\n") - - print(Epsilon_transfom(square_series(10))) + print(math.pi**2 / 6) + print(square_series(10000)[-1]) + print(G_transform(np.array(square_series(10000)), steps=100)[-1])