1+ from collections .abc import Callable
2+
13import numpy as np
24from sympy import lambdify , symbols , sympify
35
46
5- def get_inputs () -> tuple :
7+ def get_inputs () -> tuple [ str , float , float ] :
68 """
79 Get user input for the function, lower limit, and upper limit.
810
911 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.
1214
1315 Example:
1416 >>> from unittest.mock import patch
@@ -23,50 +25,47 @@ def get_inputs() -> tuple:
2325 return func , lower_limit , upper_limit
2426
2527
26- def safe_function_eval (func_str : str ) -> float :
28+ def safe_function_eval (func_str : str ) -> Callable :
2729 """
2830 Safely evaluates the function by substituting x value using sympy.
2931
3032 Args:
3133 func_str (str): Function expression as a string.
3234
3335 Returns:
34- float: The evaluated function result.
36+ Callable: A callable lambda function for numerical evaluation.
37+
3538 Examples:
3639 >>> f = safe_function_eval('x**2')
3740 >>> f(3)
3841 9
39-
4042 >>> f = safe_function_eval('sin(x)')
4143 >>> round(f(3.14), 2)
4244 0.0
43-
4445 >>> f = safe_function_eval('x + x**2')
4546 >>> f(2)
4647 6
4748 """
4849 x = symbols ("x" )
4950 func_expr = sympify (func_str )
50-
51- # Convert the function to a callable lambda function
5251 lambda_func = lambdify (x , func_expr , modules = ["numpy" ])
5352 return lambda_func
5453
5554
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 ]:
5957 """
6058 Compute the table of function values based on the limits and accuracy.
6159
6260 Args:
63- func (str ): The mathematical function with the variable 'x' as a string .
61+ func (Callable ): The mathematical function as a callable .
6462 lower_limit (float): The lower limit of the integral.
6563 upper_limit (float): The upper limit of the integral.
6664 acc (int): The number of subdivisions for accuracy.
6765
6866 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).
7069
7170 Example:
7271 >>> compute_table(
@@ -79,21 +78,19 @@ def compute_table(
7978 n_points = acc * 6 + 1
8079 h = (upper_limit - lower_limit ) / (n_points - 1 )
8180 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
8582 return table , h
8683
8784
88- def apply_weights (table : list ) -> list :
85+ def apply_weights (table : list [ float ] ) -> list [ float ] :
8986 """
90- Apply Simpson 's rule weights to the values in the table.
87+ Apply Weddle 's rule weights to the values in the table.
9188
9289 Args:
93- table (list ): A list of computed function values.
90+ table (List[float] ): A list of computed function values.
9491
9592 Returns:
96- list : A list of weighted values.
93+ List[float] : A list of weighted values.
9794
9895 Example:
9996 >>> 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:
103100 for i in range (1 , len (table ) - 1 ):
104101 if i % 2 == 0 and i % 3 != 0 :
105102 add .append (table [i ])
106- if i % 2 != 0 and i % 3 != 0 :
103+ elif i % 2 != 0 and i % 3 != 0 :
107104 add .append (5 * table [i ])
108105 elif i % 6 == 0 :
109106 add .append (2 * table [i ])
@@ -112,13 +109,13 @@ def apply_weights(table: list) -> list:
112109 return add
113110
114111
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 :
116113 """
117114 Compute the final solution using the weighted values and table.
118115
119116 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.
122119 step_size (float): The step size calculated from the limits and accuracy.
123120
124121 Returns:
@@ -137,15 +134,15 @@ def compute_solution(add: list, table: list, step_size: float) -> float:
137134
138135 testmod ()
139136
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
143140
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
150147
151- print (f"Solution: { solution } " )
148+ # print(f"Solution: {solution}")
0 commit comments