From 1ad1ed35623c2be80ed4bc3b7a08b8c7c8aadb2d Mon Sep 17 00:00:00 2001 From: Soham-KT Date: Sat, 5 Oct 2024 18:38:12 +0530 Subject: [PATCH 01/16] weddle's integration rule --- maths/weddles_rule.py | 109 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 maths/weddles_rule.py diff --git a/maths/weddles_rule.py b/maths/weddles_rule.py new file mode 100644 index 000000000000..ea9b547e8447 --- /dev/null +++ b/maths/weddles_rule.py @@ -0,0 +1,109 @@ +from math import * + + +def get_inputs(): + """ + 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. + + Example: + >>> from unittest.mock import patch + >>> inputs = ['1/(1+x**2)', 1.0, -1.0] + >>> with patch('builtins.input', side_effect=inputs): + ... get_inputs() + ('1/(1+x**2)', 1.0, -1.0) + """ + func = input("Enter function with variable as x: ") + a = float(input("Enter lower limit: ")) + b = float(input("Enter upper limit: ")) + return func, a, b + + +def compute_table(func, a, b, acc): + """ + 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. + a (float): The lower limit of the integral. + b (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). + + Example: + >>> compute_table('1/(1+x**2)', 1, -1, 1) + ([0.5, 0.4235294117647058, 0.36, 0.3076923076923077, 0.26470588235294124, 0.22929936305732482, 0.2], -0.3333333333333333) + """ + h = (b - a) / (acc * 6) + table = [0 for _ in range(acc * 6 + 1)] + for j in range(acc * 6 + 1): + x = a + j / (acc * 6) + table[j] = eval(func) + return table, h + + +def apply_weights(table): + """ + Apply Simpson's rule weights to the values in the table. + + Args: + table (list): A list of computed function values. + + Returns: + list: A list of weighted values. + + Example: + >>> apply_weights([0.0, 0.866, 1.0, 0.866, 0.0, -0.866, -1.0]) + [4.33, 1.0, 5.196, 0.0, -4.33] + """ + add = [] + 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: + add.append(5 * table[i]) + elif i % 6 == 0: + add.append(2 * table[i]) + elif i % 3 == 0 and i % 2 != 0: + add.append(6 * table[i]) + return add + + +def compute_solution(add, table, h): + """ + 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. + h (float): The step size (h) calculated from the limits and accuracy. + + Returns: + float: The final computed integral solution. + + Example: + >>> compute_solution([4.33, 6.0, 0.0, -4.33], [0.0, 0.866, 1.0, 0.866, 0.0, -0.866, -1.0], 0.5235983333333333) + 0.7853975 + """ + return 0.3 * h * (sum(add) + table[0] + table[-1]) + + +if __name__ == "__main__": + from doctest import testmod + testmod() + + func, a, b = get_inputs() + acc = 1 + solution = None + + while acc <= 100000: + table, h = compute_table(func, a, b, acc) + add = apply_weights(table) + solution = compute_solution(add, table, h) + acc *= 10 + + print(f'Solution: {solution}') \ No newline at end of file From 8a4638e12a026a89985df5d574ab2b4aa7ce81cc Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 5 Oct 2024 13:11:27 +0000 Subject: [PATCH 02/16] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/weddles_rule.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/maths/weddles_rule.py b/maths/weddles_rule.py index ea9b547e8447..97ae868a4406 100644 --- a/maths/weddles_rule.py +++ b/maths/weddles_rule.py @@ -94,8 +94,9 @@ def compute_solution(add, table, h): if __name__ == "__main__": from doctest import testmod + testmod() - + func, a, b = get_inputs() acc = 1 solution = None @@ -106,4 +107,4 @@ def compute_solution(add, table, h): solution = compute_solution(add, table, h) acc *= 10 - print(f'Solution: {solution}') \ No newline at end of file + print(f"Solution: {solution}") From cc5911a3ad7a68199a8ba8a046a029dcc4e48519 Mon Sep 17 00:00:00 2001 From: Soham-KT Date: Sun, 6 Oct 2024 10:35:34 +0530 Subject: [PATCH 03/16] checks passed --- maths/weddles_rule.py | 53 +++++++++++++++++++++++++++++++------------ 1 file changed, 39 insertions(+), 14 deletions(-) diff --git a/maths/weddles_rule.py b/maths/weddles_rule.py index 97ae868a4406..03deee89a19e 100644 --- a/maths/weddles_rule.py +++ b/maths/weddles_rule.py @@ -1,4 +1,5 @@ -from math import * +import numpy as np +from sympy import lambdify, symbols, sympify def get_inputs(): @@ -6,7 +7,8 @@ def get_inputs(): 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: 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 @@ -21,6 +23,24 @@ def get_inputs(): return func, a, b +def safe_function_eval(func_str): + """ + Safely evaluates the function by substituting x value using sympy. + + Args: + func_str (str): Function expression as a string. + + Returns: + float: The evaluated function result. + """ + 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, a, b, acc): """ Compute the table of function values based on the limits and accuracy. @@ -35,14 +55,19 @@ def compute_table(func, a, b, acc): tuple: A tuple containing the table of values and the step size (h). Example: - >>> compute_table('1/(1+x**2)', 1, -1, 1) - ([0.5, 0.4235294117647058, 0.36, 0.3076923076923077, 0.26470588235294124, 0.22929936305732482, 0.2], -0.3333333333333333) + >>> compute_table( + ... safe_function_eval('1/(1+x**2)'), 1, -1, 1 + ... ) + (array([0.5 , 0.69230769, 0.9 , 1. , 0.9 , + 0.69230769, 0.5 ]), -0.3333333333333333) """ - h = (b - a) / (acc * 6) - table = [0 for _ in range(acc * 6 + 1)] - for j in range(acc * 6 + 1): - x = a + j / (acc * 6) - table[j] = eval(func) + # Weddle's rule requires number of intervals as a multiple of 6 for accuracy + n_points = acc * 6 + 1 + h = (b - a) / (n_points - 1) + x_vals = np.linspace(a, b, n_points) + + # Evaluate function values at all points + table = func(x_vals) return table, h @@ -86,7 +111,8 @@ def compute_solution(add, table, h): float: The final computed integral solution. Example: - >>> compute_solution([4.33, 6.0, 0.0, -4.33], [0.0, 0.866, 1.0, 0.866, 0.0, -0.866, -1.0], 0.5235983333333333) + >>> compute_solution([4.33, 6.0, 0.0, -4.33], [0.0, 0.866, 1.0, 0.866, 0.0, + ... -0.866, -1.0], 0.5235983333333333) 0.7853975 """ return 0.3 * h * (sum(add) + table[0] + table[-1]) @@ -94,17 +120,16 @@ def compute_solution(add, table, h): if __name__ == "__main__": from doctest import testmod - testmod() - + func, a, b = get_inputs() acc = 1 solution = None - while acc <= 100000: + 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}') \ No newline at end of file From 162595b8a630e794ab87097182b3b9bbc9cf26c3 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 6 Oct 2024 05:08:18 +0000 Subject: [PATCH 04/16] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/weddles_rule.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/maths/weddles_rule.py b/maths/weddles_rule.py index 03deee89a19e..6f25a3def1dc 100644 --- a/maths/weddles_rule.py +++ b/maths/weddles_rule.py @@ -33,7 +33,7 @@ def safe_function_eval(func_str): Returns: float: The evaluated function result. """ - x = symbols('x') + x = symbols("x") func_expr = sympify(func_str) # Convert the function to a callable lambda function @@ -120,8 +120,9 @@ def compute_solution(add, table, h): if __name__ == "__main__": from doctest import testmod + testmod() - + func, a, b = get_inputs() acc = 1 solution = None @@ -132,4 +133,4 @@ def compute_solution(add, table, h): solution = compute_solution(add, table, h) acc *= 10 - print(f'Solution: {solution}') \ No newline at end of file + print(f"Solution: {solution}") From 7ce066c1987eda2bb77ef47eb2741fd60cd57506 Mon Sep 17 00:00:00 2001 From: Soham-KT Date: Sun, 6 Oct 2024 11:36:21 +0530 Subject: [PATCH 05/16] added return type hint --- maths/weddles_rule.py | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/maths/weddles_rule.py b/maths/weddles_rule.py index 6f25a3def1dc..4e8d47c51a97 100644 --- a/maths/weddles_rule.py +++ b/maths/weddles_rule.py @@ -2,7 +2,7 @@ from sympy import lambdify, symbols, sympify -def get_inputs(): +def get_inputs() -> tuple: """ Get user input for the function, lower limit, and upper limit. @@ -23,7 +23,7 @@ def get_inputs(): return func, a, b -def safe_function_eval(func_str): +def safe_function_eval(func_str) -> float: """ Safely evaluates the function by substituting x value using sympy. @@ -41,7 +41,7 @@ def safe_function_eval(func_str): return lambda_func -def compute_table(func, a, b, acc): +def compute_table(func, a, b, acc) -> tuple: """ Compute the table of function values based on the limits and accuracy. @@ -71,7 +71,7 @@ def compute_table(func, a, b, acc): return table, h -def apply_weights(table): +def apply_weights(table) -> list: """ Apply Simpson's rule weights to the values in the table. @@ -98,7 +98,7 @@ def apply_weights(table): return add -def compute_solution(add, table, h): +def compute_solution(add, table, h) -> float: """ Compute the final solution using the weighted values and table. @@ -122,15 +122,15 @@ def compute_solution(add, table, h): from doctest import testmod testmod() - - func, a, b = get_inputs() - acc = 1 - solution = None - - 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}") + + # func, a, b = get_inputs() + # acc = 1 + # solution = None + + # 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}') From 410ab38894122aa6e8451f3669229ebe2d5045e3 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 6 Oct 2024 06:07:32 +0000 Subject: [PATCH 06/16] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/weddles_rule.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/weddles_rule.py b/maths/weddles_rule.py index 4e8d47c51a97..c541d17b9dca 100644 --- a/maths/weddles_rule.py +++ b/maths/weddles_rule.py @@ -122,7 +122,7 @@ def compute_solution(add, table, h) -> float: from doctest import testmod testmod() - + # func, a, b = get_inputs() # acc = 1 # solution = None From 3df440b851000c523f4a4e2a11a6c84a4f340a30 Mon Sep 17 00:00:00 2001 From: Soham-KT Date: Sun, 6 Oct 2024 11:44:47 +0530 Subject: [PATCH 07/16] Added type hints to function parameters and return types --- maths/weddles_rule.py | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/maths/weddles_rule.py b/maths/weddles_rule.py index c541d17b9dca..c74d4d315fac 100644 --- a/maths/weddles_rule.py +++ b/maths/weddles_rule.py @@ -23,7 +23,7 @@ def get_inputs() -> tuple: return func, a, b -def safe_function_eval(func_str) -> float: +def safe_function_eval(func_str: str) -> float: """ Safely evaluates the function by substituting x value using sympy. @@ -32,6 +32,19 @@ def safe_function_eval(func_str) -> float: Returns: float: The evaluated function result. + + 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) @@ -41,7 +54,7 @@ def safe_function_eval(func_str) -> float: return lambda_func -def compute_table(func, a, b, acc) -> tuple: +def compute_table(func: str, a: float, b: float, acc: int) -> tuple: """ Compute the table of function values based on the limits and accuracy. @@ -71,7 +84,7 @@ def compute_table(func, a, b, acc) -> tuple: return table, h -def apply_weights(table) -> list: +def apply_weights(table: list) -> list: """ Apply Simpson's rule weights to the values in the table. @@ -98,7 +111,7 @@ def apply_weights(table) -> list: return add -def compute_solution(add, table, h) -> float: +def compute_solution(add: list, table: list, h: float) -> float: """ Compute the final solution using the weighted values and table. From 56a052678d05dd3d89ec318b73ad507a7472e812 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 6 Oct 2024 06:15:22 +0000 Subject: [PATCH 08/16] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/weddles_rule.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/weddles_rule.py b/maths/weddles_rule.py index c74d4d315fac..93bb3ec722f6 100644 --- a/maths/weddles_rule.py +++ b/maths/weddles_rule.py @@ -32,7 +32,7 @@ def safe_function_eval(func_str: str) -> float: Returns: float: The evaluated function result. - + Examples: >>> f = safe_function_eval('x**2') >>> f(3) From aa66c4b48c596c219e82290657a6e0ec662f406e Mon Sep 17 00:00:00 2001 From: Soham-KT Date: Sun, 6 Oct 2024 11:50:36 +0530 Subject: [PATCH 09/16] added descriptive names --- maths/weddles_rule.py | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/maths/weddles_rule.py b/maths/weddles_rule.py index 93bb3ec722f6..d2564b11582b 100644 --- a/maths/weddles_rule.py +++ b/maths/weddles_rule.py @@ -18,9 +18,9 @@ def get_inputs() -> tuple: ('1/(1+x**2)', 1.0, -1.0) """ func = input("Enter function with variable as x: ") - a = float(input("Enter lower limit: ")) - b = float(input("Enter upper limit: ")) - return func, a, b + lower_limit = float(input("Enter lower limit: ")) + upper_limit = float(input("Enter upper limit: ")) + return func, lower_limit, upper_limit def safe_function_eval(func_str: str) -> float: @@ -32,7 +32,6 @@ def safe_function_eval(func_str: str) -> float: Returns: float: The evaluated function result. - Examples: >>> f = safe_function_eval('x**2') >>> f(3) @@ -54,14 +53,14 @@ def safe_function_eval(func_str: str) -> float: return lambda_func -def compute_table(func: str, a: float, b: float, acc: int) -> tuple: +def compute_table(func: str, lower_limit: float, upper_limit: float, acc: int) -> tuple: """ 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. - a (float): The lower limit of the integral. - b (float): The upper limit of the integral. + 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: @@ -76,8 +75,8 @@ def compute_table(func: str, a: float, b: float, acc: int) -> tuple: """ # Weddle's rule requires number of intervals as a multiple of 6 for accuracy n_points = acc * 6 + 1 - h = (b - a) / (n_points - 1) - x_vals = np.linspace(a, b, n_points) + 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) @@ -111,14 +110,14 @@ def apply_weights(table: list) -> list: return add -def compute_solution(add: list, table: list, h: float) -> float: +def compute_solution(add: list, table: list, 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. - h (float): The step size (h) calculated from the limits and accuracy. + step_size (float): The step size calculated from the limits and accuracy. Returns: float: The final computed integral solution. @@ -128,7 +127,7 @@ def compute_solution(add: list, table: list, h: float) -> float: ... -0.866, -1.0], 0.5235983333333333) 0.7853975 """ - return 0.3 * h * (sum(add) + table[0] + table[-1]) + return 0.3 * step_size * (sum(add) + table[0] + table[-1]) if __name__ == "__main__": From d44d0a810f3e8f3e3456a7c6633445cccb6fce74 Mon Sep 17 00:00:00 2001 From: Soham-KT Date: Sun, 6 Oct 2024 13:50:36 +0530 Subject: [PATCH 10/16] safe eval used --- maths/weddles_rule.py | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/maths/weddles_rule.py b/maths/weddles_rule.py index d2564b11582b..da41f82ec285 100644 --- a/maths/weddles_rule.py +++ b/maths/weddles_rule.py @@ -135,14 +135,15 @@ def compute_solution(add: list, table: list, step_size: float) -> float: testmod() - # func, a, b = get_inputs() - # acc = 1 - # solution = None - - # 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}') + 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 + + print(f'Solution: {solution}') From b13ce589adf905ff953ca9aa067a22f9353a5d3b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 6 Oct 2024 08:21:01 +0000 Subject: [PATCH 11/16] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/weddles_rule.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/weddles_rule.py b/maths/weddles_rule.py index da41f82ec285..0c26a499da7d 100644 --- a/maths/weddles_rule.py +++ b/maths/weddles_rule.py @@ -146,4 +146,4 @@ def compute_solution(add: list, table: list, step_size: float) -> float: solution = compute_solution(add, table, h) acc *= 10 - print(f'Solution: {solution}') + print(f"Solution: {solution}") From 4e29f54e98351b5c609299c7b98d575d6719ae63 Mon Sep 17 00:00:00 2001 From: Soham-KT Date: Sun, 6 Oct 2024 13:52:28 +0530 Subject: [PATCH 12/16] changed parameter hint --- maths/weddles_rule.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/weddles_rule.py b/maths/weddles_rule.py index 0c26a499da7d..7159c466aa00 100644 --- a/maths/weddles_rule.py +++ b/maths/weddles_rule.py @@ -53,7 +53,7 @@ def safe_function_eval(func_str: str) -> float: return lambda_func -def compute_table(func: str, lower_limit: float, upper_limit: float, acc: int) -> tuple: +def compute_table(func: float, lower_limit: float, upper_limit: float, acc: int) -> tuple: """ Compute the table of function values based on the limits and accuracy. From cb4e8dd06bac93ff385bb913f587a94077533272 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 6 Oct 2024 08:23:11 +0000 Subject: [PATCH 13/16] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/weddles_rule.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/maths/weddles_rule.py b/maths/weddles_rule.py index 7159c466aa00..a2408d2c8879 100644 --- a/maths/weddles_rule.py +++ b/maths/weddles_rule.py @@ -53,7 +53,9 @@ def safe_function_eval(func_str: str) -> float: return lambda_func -def compute_table(func: float, lower_limit: float, upper_limit: float, acc: int) -> tuple: +def compute_table( + func: float, lower_limit: float, upper_limit: float, acc: int +) -> tuple: """ Compute the table of function values based on the limits and accuracy. From 2cdd70bdd06e1428db7d2b802880a7c95c44b1a3 Mon Sep 17 00:00:00 2001 From: Soham-KT Date: Sun, 6 Oct 2024 14:55:35 +0530 Subject: [PATCH 14/16] 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 a2408d2c8879..e89300ec767e 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 @@ def compute_solution(add: list, table: list, step_size: float) -> float: 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}") From 10bdd07377841bd65efaffa610569e0803d9d0f8 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 6 Oct 2024 09:28:01 +0000 Subject: [PATCH 15/16] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/weddles_rule.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/maths/weddles_rule.py b/maths/weddles_rule.py index e89300ec767e..c4cc70518571 100644 --- a/maths/weddles_rule.py +++ b/maths/weddles_rule.py @@ -52,8 +52,9 @@ def safe_function_eval(func_str: str) -> Callable: return lambda_func -def compute_table(func: Callable, lower_limit: float, - upper_limit: float, acc: int) -> tuple[np.ndarray, float]: +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. From da0a97536ead490e3213525881f88b2b32a2be07 Mon Sep 17 00:00:00 2001 From: Soham-KT Date: Sun, 6 Oct 2024 15:07:40 +0530 Subject: [PATCH 16/16] changes made in doctest --- maths/weddles_rule.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/maths/weddles_rule.py b/maths/weddles_rule.py index c4cc70518571..a6d8bbf16271 100644 --- a/maths/weddles_rule.py +++ b/maths/weddles_rule.py @@ -39,9 +39,6 @@ def safe_function_eval(func_str: str) -> Callable: >>> 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