Skip to content

Commit 729df6c

Browse files
authored
Update adams_bashforth.py
1 parent c5a24b2 commit 729df6c

File tree

1 file changed

+30
-32
lines changed

1 file changed

+30
-32
lines changed

maths/numerical_analysis/adams_bashforth.py

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,39 +6,13 @@
66
Author : Ravi Kumar
77
"""
88
from collections.abc import Callable
9+
from dataclasses import dataclass
910

1011
import numpy as np
1112

1213

14+
@dataclass
1315
class AdamsBashforth:
14-
def __init__(
15-
self,
16-
func: Callable[[float, float], float],
17-
x_initials: list[float],
18-
y_initials: list[float],
19-
step_size: float,
20-
x_final: float,
21-
) -> None:
22-
self.func = func
23-
self.x_initials = x_initials
24-
self.y_initials = y_initials
25-
self.step_size = step_size
26-
self.x_final = x_final
27-
28-
if x_initials[-1] >= x_final:
29-
raise ValueError(
30-
"The final value of x must be greater than the initial values of x."
31-
)
32-
33-
if step_size <= 0:
34-
raise ValueError("Step size must be positive.")
35-
36-
if not all(
37-
round(x1 - x0, 10) == step_size
38-
for x0, x1 in zip(x_initials, x_initials[1:])
39-
):
40-
raise ValueError("x-values must be equally spaced according to step size.")
41-
4216
"""
4317
args:
4418
func: An ordinary differential equation (ODE) as function of x and y.
@@ -54,7 +28,7 @@ def __init__(
5428
>>> AdamsBashforth(f, [0, 0.2, 1], [0, 0, 0.04], 0.2, 1).step_2()
5529
Traceback (most recent call last):
5630
...
57-
ValueError: The final value of x must be greater than the all initial values of x.
31+
ValueError: The final value of x must be greater than the initial values of x.
5832
5933
>>> def f(x, y):
6034
... return x + y
@@ -65,12 +39,33 @@ def __init__(
6539
6640
>>> def f(x, y):
6741
... return x
68-
>>> AdamsBashforth(f,[0,0.2,0.4,0.6,0.8],[0,0,0.04,0.128 0.307],-0.2,1).step_5()
42+
>>> AdamsBashforth(f,[0,0.2,0.4,0.6,0.8],[0,0,0.04,0.128,0.307],-0.2,1).step_5()
6943
Traceback (most recent call last):
7044
...
7145
ValueError: Step size must be positive.
7246
"""
7347

48+
func: Callable[[float, float], float]
49+
x_initials: list[float]
50+
y_initials: list[float]
51+
step_size: float
52+
x_final: float
53+
54+
def __post_init__(self) -> None:
55+
if self.x_initials[-1] >= self.x_final:
56+
raise ValueError(
57+
"The final value of x must be greater than the initial values of x."
58+
)
59+
60+
if self.step_size <= 0:
61+
raise ValueError("Step size must be positive.")
62+
63+
if not all(
64+
round(x1 - x0, 10) == self.step_size
65+
for x0, x1 in zip(self.x_initials, self.x_initials[1:])
66+
):
67+
raise ValueError("x-values must be equally spaced according to step size.")
68+
7469
def step_2(self) -> np.ndarray:
7570
"""
7671
>>> def f(x, y):
@@ -149,7 +144,8 @@ def step_4(self) -> np.ndarray:
149144
"""
150145
>>> def f(x,y):
151146
... return x + y
152-
>>> y = AdamsBashforth(f, [0, 0.2, 0.4, 0.6], [0, 0, 0.04, 0.128], 0.2, 1).step_4()
147+
>>> y = AdamsBashforth(
148+
... f, [0, 0.2, 0.4, 0.6], [0, 0, 0.04, 0.128], 0.2, 1).step_4()
153149
>>> y[4]
154150
0.30699999999999994
155151
>>> y[5]
@@ -194,7 +190,9 @@ def step_5(self) -> np.ndarray:
194190
"""
195191
>>> def f(x,y):
196192
... return x + y
197-
>>> y = AdamsBashforth(f, [0, 0.2, 0.4, 0.6, 0.8], [0, 0.02140, 0.02140, 0.22211, 0.42536], 0.2, 1).step_5()
193+
>>> y = AdamsBashforth(
194+
... f, [0, 0.2, 0.4, 0.6, 0.8], [0, 0.02140, 0.02140, 0.22211, 0.42536],
195+
... 0.2, 1).step_5()
198196
>>> y[-1]
199197
0.05436839444444452
200198

0 commit comments

Comments
 (0)