1
+ from collections .abc import Callable
2
+
1
3
import numpy as np
2
4
from sympy import lambdify , symbols , sympify
3
5
4
6
5
- def get_inputs () -> tuple :
7
+ def get_inputs () -> tuple [ str , float , float ] :
6
8
"""
7
9
Get user input for the function, lower limit, and upper limit.
8
10
9
11
Returns:
10
- tuple : A tuple containing the function as a string, the lower limit (a) ,
11
- and the upper limit (b) as floats.
12
+ Tuple[str, float, float] : A tuple containing the function as a string,
13
+ the lower limit (a), and the upper limit (b) as floats.
12
14
13
15
Example:
14
16
>>> from unittest.mock import patch
@@ -23,50 +25,47 @@ def get_inputs() -> tuple:
23
25
return func , lower_limit , upper_limit
24
26
25
27
26
- def safe_function_eval (func_str : str ) -> float :
28
+ def safe_function_eval (func_str : str ) -> Callable :
27
29
"""
28
30
Safely evaluates the function by substituting x value using sympy.
29
31
30
32
Args:
31
33
func_str (str): Function expression as a string.
32
34
33
35
Returns:
34
- float: The evaluated function result.
36
+ Callable: A callable lambda function for numerical evaluation.
37
+
35
38
Examples:
36
39
>>> f = safe_function_eval('x**2')
37
40
>>> f(3)
38
41
9
39
-
40
42
>>> f = safe_function_eval('sin(x)')
41
43
>>> round(f(3.14), 2)
42
44
0.0
43
-
44
45
>>> f = safe_function_eval('x + x**2')
45
46
>>> f(2)
46
47
6
47
48
"""
48
49
x = symbols ("x" )
49
50
func_expr = sympify (func_str )
50
-
51
- # Convert the function to a callable lambda function
52
51
lambda_func = lambdify (x , func_expr , modules = ["numpy" ])
53
52
return lambda_func
54
53
55
54
56
- def compute_table (
57
- func : float , lower_limit : float , upper_limit : float , acc : int
58
- ) -> tuple :
55
+ def compute_table (func : Callable , lower_limit : float ,
56
+ upper_limit : float , acc : int ) -> tuple [np .ndarray , float ]:
59
57
"""
60
58
Compute the table of function values based on the limits and accuracy.
61
59
62
60
Args:
63
- func (str ): The mathematical function with the variable 'x' as a string .
61
+ func (Callable ): The mathematical function as a callable .
64
62
lower_limit (float): The lower limit of the integral.
65
63
upper_limit (float): The upper limit of the integral.
66
64
acc (int): The number of subdivisions for accuracy.
67
65
68
66
Returns:
69
- tuple: A tuple containing the table of values and the step size (h).
67
+ Tuple[np.ndarray, float]: A tuple containing the table
68
+ of values and the step size (h).
70
69
71
70
Example:
72
71
>>> compute_table(
@@ -79,21 +78,19 @@ def compute_table(
79
78
n_points = acc * 6 + 1
80
79
h = (upper_limit - lower_limit ) / (n_points - 1 )
81
80
x_vals = np .linspace (lower_limit , upper_limit , n_points )
82
-
83
- # Evaluate function values at all points
84
- table = func (x_vals )
81
+ table = func (x_vals ) # Evaluate function values at all points
85
82
return table , h
86
83
87
84
88
- def apply_weights (table : list ) -> list :
85
+ def apply_weights (table : list [ float ] ) -> list [ float ] :
89
86
"""
90
- Apply Simpson 's rule weights to the values in the table.
87
+ Apply Weddle 's rule weights to the values in the table.
91
88
92
89
Args:
93
- table (list ): A list of computed function values.
90
+ table (List[float] ): A list of computed function values.
94
91
95
92
Returns:
96
- list : A list of weighted values.
93
+ List[float] : A list of weighted values.
97
94
98
95
Example:
99
96
>>> apply_weights([0.0, 0.866, 1.0, 0.866, 0.0, -0.866, -1.0])
@@ -103,7 +100,7 @@ def apply_weights(table: list) -> list:
103
100
for i in range (1 , len (table ) - 1 ):
104
101
if i % 2 == 0 and i % 3 != 0 :
105
102
add .append (table [i ])
106
- if i % 2 != 0 and i % 3 != 0 :
103
+ elif i % 2 != 0 and i % 3 != 0 :
107
104
add .append (5 * table [i ])
108
105
elif i % 6 == 0 :
109
106
add .append (2 * table [i ])
@@ -112,13 +109,13 @@ def apply_weights(table: list) -> list:
112
109
return add
113
110
114
111
115
- def compute_solution (add : list , table : list , step_size : float ) -> float :
112
+ def compute_solution (add : list [ float ] , table : list [ float ] , step_size : float ) -> float :
116
113
"""
117
114
Compute the final solution using the weighted values and table.
118
115
119
116
Args:
120
- add (list ): A list of weighted values from apply_weights.
121
- table (list ): A list of function values.
117
+ add (List[float] ): A list of weighted values from apply_weights.
118
+ table (List[float] ): A list of function values.
122
119
step_size (float): The step size calculated from the limits and accuracy.
123
120
124
121
Returns:
@@ -137,15 +134,15 @@ def compute_solution(add: list, table: list, step_size: float) -> float:
137
134
138
135
testmod ()
139
136
140
- func_str , a , b = get_inputs ()
141
- acc = 1
142
- solution = None
137
+ # func_str, a, b = get_inputs()
138
+ # acc = 1
139
+ # solution = None
143
140
144
- func = safe_function_eval (func_str )
145
- while acc <= 100_000 :
146
- table , h = compute_table (func , a , b , acc )
147
- add = apply_weights (table )
148
- solution = compute_solution (add , table , h )
149
- acc *= 10
141
+ # func = safe_function_eval(func_str)
142
+ # while acc <= 100_000:
143
+ # table, h = compute_table(func, a, b, acc)
144
+ # add = apply_weights(table)
145
+ # solution = compute_solution(add, table, h)
146
+ # acc *= 10
150
147
151
- print (f"Solution: { solution } " )
148
+ # print(f"Solution: {solution}")
0 commit comments