From 2cdd70bdd06e1428db7d2b802880a7c95c44b1a3 Mon Sep 17 00:00:00 2001 From: Soham-KT Date: Sun, 6 Oct 2024 14:55:35 +0530 Subject: [PATCH] updated function signatures, type hints, and docstrings; modified function implementations and variable names. --- maths/weddles_rule.py | 67 +++++++++++++++++++++---------------------- 1 file changed, 32 insertions(+), 35 deletions(-) diff --git a/maths/weddles_rule.py b/maths/weddles_rule.py index a2408d2c8..e89300ec7 100644 --- a/maths/weddles_rule.py +++ b/maths/weddles_rule.py @@ -1,14 +1,16 @@ +from collections.abc import Callable + import numpy as np from sympy import lambdify, symbols, sympify -def get_inputs() -> tuple: +def get_inputs() -> tuple[str, float, float]: """ Get user input for the function, lower limit, and upper limit. Returns: - tuple: A tuple containing the function as a string, the lower limit (a), - and the upper limit (b) as floats. + Tuple[str, float, float]: A tuple containing the function as a string, + the lower limit (a), and the upper limit (b) as floats. Example: >>> from unittest.mock import patch @@ -23,7 +25,7 @@ def get_inputs() -> tuple: return func, lower_limit, upper_limit -def safe_function_eval(func_str: str) -> float: +def safe_function_eval(func_str: str) -> Callable: """ Safely evaluates the function by substituting x value using sympy. @@ -31,42 +33,39 @@ def safe_function_eval(func_str: str) -> float: func_str (str): Function expression as a string. Returns: - float: The evaluated function result. + Callable: A callable lambda function for numerical evaluation. + Examples: >>> f = safe_function_eval('x**2') >>> f(3) 9 - >>> f = safe_function_eval('sin(x)') >>> round(f(3.14), 2) 0.0 - >>> f = safe_function_eval('x + x**2') >>> f(2) 6 """ x = symbols("x") func_expr = sympify(func_str) - - # Convert the function to a callable lambda function lambda_func = lambdify(x, func_expr, modules=["numpy"]) return lambda_func -def compute_table( - func: float, lower_limit: float, upper_limit: float, acc: int -) -> tuple: +def compute_table(func: Callable, lower_limit: float, + upper_limit: float, acc: int) -> tuple[np.ndarray, float]: """ Compute the table of function values based on the limits and accuracy. Args: - func (str): The mathematical function with the variable 'x' as a string. + func (Callable): The mathematical function as a callable. lower_limit (float): The lower limit of the integral. upper_limit (float): The upper limit of the integral. acc (int): The number of subdivisions for accuracy. Returns: - tuple: A tuple containing the table of values and the step size (h). + Tuple[np.ndarray, float]: A tuple containing the table + of values and the step size (h). Example: >>> compute_table( @@ -79,21 +78,19 @@ def compute_table( n_points = acc * 6 + 1 h = (upper_limit - lower_limit) / (n_points - 1) x_vals = np.linspace(lower_limit, upper_limit, n_points) - - # Evaluate function values at all points - table = func(x_vals) + table = func(x_vals) # Evaluate function values at all points return table, h -def apply_weights(table: list) -> list: +def apply_weights(table: list[float]) -> list[float]: """ - Apply Simpson's rule weights to the values in the table. + Apply Weddle's rule weights to the values in the table. Args: - table (list): A list of computed function values. + table (List[float]): A list of computed function values. Returns: - list: A list of weighted values. + List[float]: A list of weighted values. Example: >>> 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: for i in range(1, len(table) - 1): if i % 2 == 0 and i % 3 != 0: add.append(table[i]) - if i % 2 != 0 and i % 3 != 0: + elif i % 2 != 0 and i % 3 != 0: add.append(5 * table[i]) elif i % 6 == 0: add.append(2 * table[i]) @@ -112,13 +109,13 @@ def apply_weights(table: list) -> list: return add -def compute_solution(add: list, table: list, step_size: float) -> float: +def compute_solution(add: list[float], table: list[float], step_size: float) -> float: """ Compute the final solution using the weighted values and table. Args: - add (list): A list of weighted values from apply_weights. - table (list): A list of function values. + add (List[float]): A list of weighted values from apply_weights. + table (List[float]): A list of function values. step_size (float): The step size calculated from the limits and accuracy. Returns: @@ -137,15 +134,15 @@ if __name__ == "__main__": testmod() - func_str, a, b = get_inputs() - acc = 1 - solution = None + # func_str, a, b = get_inputs() + # acc = 1 + # solution = None - func = safe_function_eval(func_str) - while acc <= 100_000: - table, h = compute_table(func, a, b, acc) - add = apply_weights(table) - solution = compute_solution(add, table, h) - acc *= 10 + # func = safe_function_eval(func_str) + # while acc <= 100_000: + # table, h = compute_table(func, a, b, acc) + # add = apply_weights(table) + # solution = compute_solution(add, table, h) + # acc *= 10 - print(f"Solution: {solution}") + # print(f"Solution: {solution}")