6
6
Author : Ravi Kumar
7
7
"""
8
8
from collections .abc import Callable
9
+ from dataclasses import dataclass
9
10
10
11
import numpy as np
11
12
12
13
14
+ @dataclass
13
15
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
-
42
16
"""
43
17
args:
44
18
func: An ordinary differential equation (ODE) as function of x and y.
@@ -54,7 +28,7 @@ def __init__(
54
28
>>> AdamsBashforth(f, [0, 0.2, 1], [0, 0, 0.04], 0.2, 1).step_2()
55
29
Traceback (most recent call last):
56
30
...
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.
58
32
59
33
>>> def f(x, y):
60
34
... return x + y
@@ -65,12 +39,33 @@ def __init__(
65
39
66
40
>>> def f(x, y):
67
41
... 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()
69
43
Traceback (most recent call last):
70
44
...
71
45
ValueError: Step size must be positive.
72
46
"""
73
47
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
+
74
69
def step_2 (self ) -> np .ndarray :
75
70
"""
76
71
>>> def f(x, y):
@@ -149,7 +144,8 @@ def step_4(self) -> np.ndarray:
149
144
"""
150
145
>>> def f(x,y):
151
146
... 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()
153
149
>>> y[4]
154
150
0.30699999999999994
155
151
>>> y[5]
@@ -194,7 +190,9 @@ def step_5(self) -> np.ndarray:
194
190
"""
195
191
>>> def f(x,y):
196
192
... 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()
198
196
>>> y[-1]
199
197
0.05436839444444452
200
198
0 commit comments