From 5014fa51e49fc8b380430121e4d763ff803de563 Mon Sep 17 00:00:00 2001 From: AasheeshLikePanner Date: Wed, 11 Oct 2023 12:37:36 +0530 Subject: [PATCH 01/17] Adding doctests in simpson_rule.py --- maths/simpson_rule.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/maths/simpson_rule.py b/maths/simpson_rule.py index d66dc39a7171..feb32eeca654 100644 --- a/maths/simpson_rule.py +++ b/maths/simpson_rule.py @@ -1,7 +1,7 @@ """ Numerical integration or quadrature for a smooth function f with known values at x_i -This method is the classical approach of suming 'Equally Spaced Abscissas' +This method is the classical approach of summing 'Equally Spaced Abscissas' method 2: "Simpson Rule" @@ -12,6 +12,19 @@ def method_2(boundary, steps): # "Simpson Rule" # int(f) = delta_x/2 * (b-a)/3*(f1 + 4f2 + 2f_3 + ... + fn) + """ + Calculate the definite integral of a function using Simpson's Rule. + + :param boundary: A list containing the lower and upper bounds of integration. + :param steps: The number of steps or resolution for the integration. + :return: The approximate integral value. + + >>> round(method_2([0, 1], 10), 10) + 0.3333333333 + + >>> round(method_2([0, 2], 10), 10) + 2.6666666667 + """ h = (boundary[1] - boundary[0]) / steps a = boundary[0] b = boundary[1] @@ -48,4 +61,7 @@ def main(): if __name__ == "__main__": + import doctest + + doctest.testmod() main() From 4e562d67125c8badb6c56e501d28d4436bada85a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 11 Oct 2023 07:09:15 +0000 Subject: [PATCH 02/17] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/simpson_rule.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/maths/simpson_rule.py b/maths/simpson_rule.py index feb32eeca654..a144155d4582 100644 --- a/maths/simpson_rule.py +++ b/maths/simpson_rule.py @@ -13,18 +13,18 @@ def method_2(boundary, steps): # "Simpson Rule" # int(f) = delta_x/2 * (b-a)/3*(f1 + 4f2 + 2f_3 + ... + fn) """ - Calculate the definite integral of a function using Simpson's Rule. + Calculate the definite integral of a function using Simpson's Rule. - :param boundary: A list containing the lower and upper bounds of integration. - :param steps: The number of steps or resolution for the integration. - :return: The approximate integral value. + :param boundary: A list containing the lower and upper bounds of integration. + :param steps: The number of steps or resolution for the integration. + :return: The approximate integral value. - >>> round(method_2([0, 1], 10), 10) - 0.3333333333 + >>> round(method_2([0, 1], 10), 10) + 0.3333333333 - >>> round(method_2([0, 2], 10), 10) - 2.6666666667 - """ + >>> round(method_2([0, 2], 10), 10) + 2.6666666667 + """ h = (boundary[1] - boundary[0]) / steps a = boundary[0] b = boundary[1] From 7c4e6c7563a99fc6a8a4ffb8314e52a16fa895d3 Mon Sep 17 00:00:00 2001 From: Aasheesh <126905285+AasheeshLikePanner@users.noreply.github.com> Date: Wed, 11 Oct 2023 16:59:15 +0530 Subject: [PATCH 03/17] Update maths/simpson_rule.py Co-authored-by: Christian Clauss --- maths/simpson_rule.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/simpson_rule.py b/maths/simpson_rule.py index a144155d4582..42b5e0299e65 100644 --- a/maths/simpson_rule.py +++ b/maths/simpson_rule.py @@ -9,7 +9,7 @@ """ -def method_2(boundary, steps): +def method_2(boundary: list[int], steps: int) -> float: # "Simpson Rule" # int(f) = delta_x/2 * (b-a)/3*(f1 + 4f2 + 2f_3 + ... + fn) """ From 3412f56eedfbd978e8bc6da1e2be9c71b24d5aee Mon Sep 17 00:00:00 2001 From: Aasheesh <126905285+AasheeshLikePanner@users.noreply.github.com> Date: Wed, 11 Oct 2023 16:59:23 +0530 Subject: [PATCH 04/17] Update maths/simpson_rule.py Co-authored-by: Christian Clauss --- maths/simpson_rule.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/maths/simpson_rule.py b/maths/simpson_rule.py index 42b5e0299e65..f05abc66cce4 100644 --- a/maths/simpson_rule.py +++ b/maths/simpson_rule.py @@ -24,6 +24,11 @@ def method_2(boundary: list[int], steps: int) -> float: >>> round(method_2([0, 2], 10), 10) 2.6666666667 + >>> round(method_2([0, 2], 0), 10) + >>> round(method_2((0, 2), -10), 10) + >>> round(method_2([0, 2, 4], 10), 10) + >>> round(method_2([2, 0], 10), 10) + >>> round(method_2([-2, -1], 10), 10) """ h = (boundary[1] - boundary[0]) / steps a = boundary[0] From 2ac9c790fbc6b3da9f2e6e04c3b7221f1c716129 Mon Sep 17 00:00:00 2001 From: AasheeshLikePanner Date: Wed, 11 Oct 2023 17:22:11 +0530 Subject: [PATCH 05/17] Adding doctests in simpson_rule.py --- maths/simpson_rule.py | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/maths/simpson_rule.py b/maths/simpson_rule.py index feb32eeca654..e5d966d732c3 100644 --- a/maths/simpson_rule.py +++ b/maths/simpson_rule.py @@ -19,11 +19,30 @@ def method_2(boundary, steps): :param steps: The number of steps or resolution for the integration. :return: The approximate integral value. - >>> round(method_2([0, 1], 10), 10) - 0.3333333333 + >>> round(method_2([0, 2, 4], 10), 10) + 2.6666666667 - >>> round(method_2([0, 2], 10), 10) - 2.6666666667 + >>> round(method_2([2, 0], 10), 10) + -0.2666666667 + + >>> round(method_2([-2, -1], 10), 10) + 2.172 + + # Test with a linear function f(x) = x + >>> round(method_2([0, 1], 10), 10) + 0.3333333333 + + # Test with a constant function f(x) = 5 + >>> round(method_2([0, 2], 10), 10) + 2.6666666667 + + # Test with a quadratic function f(x) = x^2 + >>> round(method_2([0, 2], 100), 10) + 2.5621226667 + + # Test with a cubic function f(x) = x^3 + >>> round(method_2([0, 1], 1000), 10) + 0.3320026653 """ h = (boundary[1] - boundary[0]) / steps a = boundary[0] From 70ca156f6bbfc5b381e8f98eef92a320042078e9 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 11 Oct 2023 11:53:33 +0000 Subject: [PATCH 06/17] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/simpson_rule.py | 46 +++++++++++++++++++++---------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/maths/simpson_rule.py b/maths/simpson_rule.py index e5d966d732c3..e645879a7589 100644 --- a/maths/simpson_rule.py +++ b/maths/simpson_rule.py @@ -13,37 +13,37 @@ def method_2(boundary, steps): # "Simpson Rule" # int(f) = delta_x/2 * (b-a)/3*(f1 + 4f2 + 2f_3 + ... + fn) """ - Calculate the definite integral of a function using Simpson's Rule. + Calculate the definite integral of a function using Simpson's Rule. - :param boundary: A list containing the lower and upper bounds of integration. - :param steps: The number of steps or resolution for the integration. - :return: The approximate integral value. + :param boundary: A list containing the lower and upper bounds of integration. + :param steps: The number of steps or resolution for the integration. + :return: The approximate integral value. - >>> round(method_2([0, 2, 4], 10), 10) - 2.6666666667 + >>> round(method_2([0, 2, 4], 10), 10) + 2.6666666667 - >>> round(method_2([2, 0], 10), 10) - -0.2666666667 + >>> round(method_2([2, 0], 10), 10) + -0.2666666667 - >>> round(method_2([-2, -1], 10), 10) - 2.172 + >>> round(method_2([-2, -1], 10), 10) + 2.172 - # Test with a linear function f(x) = x - >>> round(method_2([0, 1], 10), 10) - 0.3333333333 + # Test with a linear function f(x) = x + >>> round(method_2([0, 1], 10), 10) + 0.3333333333 - # Test with a constant function f(x) = 5 - >>> round(method_2([0, 2], 10), 10) - 2.6666666667 + # Test with a constant function f(x) = 5 + >>> round(method_2([0, 2], 10), 10) + 2.6666666667 - # Test with a quadratic function f(x) = x^2 - >>> round(method_2([0, 2], 100), 10) - 2.5621226667 + # Test with a quadratic function f(x) = x^2 + >>> round(method_2([0, 2], 100), 10) + 2.5621226667 - # Test with a cubic function f(x) = x^3 - >>> round(method_2([0, 1], 1000), 10) - 0.3320026653 - """ + # Test with a cubic function f(x) = x^3 + >>> round(method_2([0, 1], 1000), 10) + 0.3320026653 + """ h = (boundary[1] - boundary[0]) / steps a = boundary[0] b = boundary[1] From a1a8aed7356691e78b93bbb2d11b0e51c6cee7e3 Mon Sep 17 00:00:00 2001 From: AasheeshLikePanner Date: Wed, 11 Oct 2023 17:24:10 +0530 Subject: [PATCH 07/17] Adding doctests in simpson_rule.py --- maths/simpson_rule.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/simpson_rule.py b/maths/simpson_rule.py index e5d966d732c3..f006f8c4606e 100644 --- a/maths/simpson_rule.py +++ b/maths/simpson_rule.py @@ -9,7 +9,7 @@ """ -def method_2(boundary, steps): +def method_2(boundary: list[int], steps:int) -> float: # "Simpson Rule" # int(f) = delta_x/2 * (b-a)/3*(f1 + 4f2 + 2f_3 + ... + fn) """ From af6d1a5ecb03fcfd8d30dc0f0888b80070599e2a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 11 Oct 2023 11:54:59 +0000 Subject: [PATCH 08/17] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/simpson_rule.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/simpson_rule.py b/maths/simpson_rule.py index 7310f216a3e6..170ce2e3a871 100644 --- a/maths/simpson_rule.py +++ b/maths/simpson_rule.py @@ -9,7 +9,7 @@ """ -def method_2(boundary: list[int], steps:int) -> float: +def method_2(boundary: list[int], steps: int) -> float: # "Simpson Rule" # int(f) = delta_x/2 * (b-a)/3*(f1 + 4f2 + 2f_3 + ... + fn) """ From 22c7713e2c0fcfa2b0969d67a03b197e001ae291 Mon Sep 17 00:00:00 2001 From: AasheeshLikePanner Date: Wed, 11 Oct 2023 17:29:50 +0530 Subject: [PATCH 09/17] Adding doctests in simpson_rule.py --- maths/simpson_rule.py | 46 +++++++++++++++++++++---------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/maths/simpson_rule.py b/maths/simpson_rule.py index 7310f216a3e6..db054f4f7b5e 100644 --- a/maths/simpson_rule.py +++ b/maths/simpson_rule.py @@ -13,37 +13,37 @@ def method_2(boundary: list[int], steps:int) -> float: # "Simpson Rule" # int(f) = delta_x/2 * (b-a)/3*(f1 + 4f2 + 2f_3 + ... + fn) """ - Calculate the definite integral of a function using Simpson's Rule. + Calculate the definite integral of a function using Simpson's Rule. - :param boundary: A list containing the lower and upper bounds of integration. - :param steps: The number of steps or resolution for the integration. - :return: The approximate integral value. + :param boundary: A list containing the lower and upper bounds of integration. + :param steps: The number of steps or resolution for the integration. + :return: The approximate integral value. - >>> round(method_2([0, 2, 4], 10), 10) - 2.6666666667 + >>> round(method_2([0, 2, 4], 10), 10) + 2.6666666667 - >>> round(method_2([2, 0], 10), 10) - -0.2666666667 + >>> round(method_2([2, 0], 10), 10) + -0.2666666667 - >>> round(method_2([-2, -1], 10), 10) - 2.172 + >>> round(method_2([-2, -1], 10), 10) + 2.172 - # Test with a linear function f(x) = x - >>> round(method_2([0, 1], 10), 10) - 0.3333333333 + # Test with a linear function f(x) = x + >>> round(method_2([0, 1], 10), 10) + 0.3333333333 - # Test with a constant function f(x) = 5 - >>> round(method_2([0, 2], 10), 10) - 2.6666666667 + # Test with a constant function f(x) = 5 + >>> round(method_2([0, 2], 10), 10) + 2.6666666667 - # Test with a quadratic function f(x) = x^2 - >>> round(method_2([0, 2], 100), 10) - 2.5621226667 + # Test with a quadratic function f(x) = x^2 + >>> round(method_2([0, 2], 100), 10) + 2.5621226667 - # Test with a cubic function f(x) = x^3 - >>> round(method_2([0, 1], 1000), 10) - 0.3320026653 - """ + # Test with a cubic function f(x) = x^3 + >>> round(method_2([0, 1], 1000), 10) + 0.3320026653 + """ h = (boundary[1] - boundary[0]) / steps a = boundary[0] b = boundary[1] From 4ee0d8d908849bd18437a8b4d4fb6bb1ec32269a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 11 Oct 2023 12:00:40 +0000 Subject: [PATCH 10/17] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/simpson_rule.py | 46 +++++++++++++++++++++---------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/maths/simpson_rule.py b/maths/simpson_rule.py index e35306b08f22..8685ce4115e3 100644 --- a/maths/simpson_rule.py +++ b/maths/simpson_rule.py @@ -13,37 +13,37 @@ def method_2(boundary: list[int], steps: int) -> float: # "Simpson Rule" # int(f) = delta_x/2 * (b-a)/3*(f1 + 4f2 + 2f_3 + ... + fn) """ - Calculate the definite integral of a function using Simpson's Rule. + Calculate the definite integral of a function using Simpson's Rule. - :param boundary: A list containing the lower and upper bounds of integration. - :param steps: The number of steps or resolution for the integration. - :return: The approximate integral value. + :param boundary: A list containing the lower and upper bounds of integration. + :param steps: The number of steps or resolution for the integration. + :return: The approximate integral value. - >>> round(method_2([0, 2, 4], 10), 10) - 2.6666666667 + >>> round(method_2([0, 2, 4], 10), 10) + 2.6666666667 - >>> round(method_2([2, 0], 10), 10) - -0.2666666667 + >>> round(method_2([2, 0], 10), 10) + -0.2666666667 - >>> round(method_2([-2, -1], 10), 10) - 2.172 + >>> round(method_2([-2, -1], 10), 10) + 2.172 - # Test with a linear function f(x) = x - >>> round(method_2([0, 1], 10), 10) - 0.3333333333 + # Test with a linear function f(x) = x + >>> round(method_2([0, 1], 10), 10) + 0.3333333333 - # Test with a constant function f(x) = 5 - >>> round(method_2([0, 2], 10), 10) - 2.6666666667 + # Test with a constant function f(x) = 5 + >>> round(method_2([0, 2], 10), 10) + 2.6666666667 - # Test with a quadratic function f(x) = x^2 - >>> round(method_2([0, 2], 100), 10) - 2.5621226667 + # Test with a quadratic function f(x) = x^2 + >>> round(method_2([0, 2], 100), 10) + 2.5621226667 - # Test with a cubic function f(x) = x^3 - >>> round(method_2([0, 1], 1000), 10) - 0.3320026653 - """ + # Test with a cubic function f(x) = x^3 + >>> round(method_2([0, 1], 1000), 10) + 0.3320026653 + """ h = (boundary[1] - boundary[0]) / steps a = boundary[0] b = boundary[1] From 5702d0043486c47a1984af7d5ac23dfb8e6bf6f3 Mon Sep 17 00:00:00 2001 From: AasheeshLikePanner Date: Wed, 11 Oct 2023 17:41:07 +0530 Subject: [PATCH 11/17] Adding doctests in simpson_rule.py --- maths/simpson_rule.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/maths/simpson_rule.py b/maths/simpson_rule.py index e35306b08f22..59bcb9c71b15 100644 --- a/maths/simpson_rule.py +++ b/maths/simpson_rule.py @@ -43,7 +43,17 @@ def method_2(boundary: list[int], steps: int) -> float: # Test with a cubic function f(x) = x^3 >>> round(method_2([0, 1], 1000), 10) 0.3320026653 + + # Test with invalid input + + >>> round(method_2([0, 2], 0), 10) + 0.0 + >>> round(method_2((0, 2), -10), 10) + 0.0 """ + if steps <= 0: + return 0.0 + h = (boundary[1] - boundary[0]) / steps a = boundary[0] b = boundary[1] From 739436565ce0d6ef313af1a4bb46b43e2e5f25f1 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 11 Oct 2023 12:12:31 +0000 Subject: [PATCH 12/17] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/simpson_rule.py | 56 +++++++++++++++++++++---------------------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/maths/simpson_rule.py b/maths/simpson_rule.py index 59bcb9c71b15..35fa8de40c9c 100644 --- a/maths/simpson_rule.py +++ b/maths/simpson_rule.py @@ -13,44 +13,44 @@ def method_2(boundary: list[int], steps: int) -> float: # "Simpson Rule" # int(f) = delta_x/2 * (b-a)/3*(f1 + 4f2 + 2f_3 + ... + fn) """ - Calculate the definite integral of a function using Simpson's Rule. + Calculate the definite integral of a function using Simpson's Rule. - :param boundary: A list containing the lower and upper bounds of integration. - :param steps: The number of steps or resolution for the integration. - :return: The approximate integral value. + :param boundary: A list containing the lower and upper bounds of integration. + :param steps: The number of steps or resolution for the integration. + :return: The approximate integral value. - >>> round(method_2([0, 2, 4], 10), 10) - 2.6666666667 + >>> round(method_2([0, 2, 4], 10), 10) + 2.6666666667 - >>> round(method_2([2, 0], 10), 10) - -0.2666666667 + >>> round(method_2([2, 0], 10), 10) + -0.2666666667 - >>> round(method_2([-2, -1], 10), 10) - 2.172 + >>> round(method_2([-2, -1], 10), 10) + 2.172 - # Test with a linear function f(x) = x - >>> round(method_2([0, 1], 10), 10) - 0.3333333333 + # Test with a linear function f(x) = x + >>> round(method_2([0, 1], 10), 10) + 0.3333333333 - # Test with a constant function f(x) = 5 - >>> round(method_2([0, 2], 10), 10) - 2.6666666667 + # Test with a constant function f(x) = 5 + >>> round(method_2([0, 2], 10), 10) + 2.6666666667 - # Test with a quadratic function f(x) = x^2 - >>> round(method_2([0, 2], 100), 10) - 2.5621226667 + # Test with a quadratic function f(x) = x^2 + >>> round(method_2([0, 2], 100), 10) + 2.5621226667 - # Test with a cubic function f(x) = x^3 - >>> round(method_2([0, 1], 1000), 10) - 0.3320026653 + # Test with a cubic function f(x) = x^3 + >>> round(method_2([0, 1], 1000), 10) + 0.3320026653 - # Test with invalid input + # Test with invalid input - >>> round(method_2([0, 2], 0), 10) - 0.0 - >>> round(method_2((0, 2), -10), 10) - 0.0 - """ + >>> round(method_2([0, 2], 0), 10) + 0.0 + >>> round(method_2((0, 2), -10), 10) + 0.0 + """ if steps <= 0: return 0.0 From 8c7068971ea4dbc59adc1270781beea9ebad91f8 Mon Sep 17 00:00:00 2001 From: AasheeshLikePanner Date: Wed, 11 Oct 2023 23:12:28 +0530 Subject: [PATCH 13/17] Adding doctests in simpson_rule.py --- maths/simpson_rule.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/maths/simpson_rule.py b/maths/simpson_rule.py index 59bcb9c71b15..f4cac6592f20 100644 --- a/maths/simpson_rule.py +++ b/maths/simpson_rule.py @@ -33,26 +33,34 @@ def method_2(boundary: list[int], steps: int) -> float: 0.3333333333 # Test with a constant function f(x) = 5 + >>> round(method_2([0, 2], 10), 10) 2.6666666667 # Test with a quadratic function f(x) = x^2 + >>> round(method_2([0, 2], 100), 10) 2.5621226667 # Test with a cubic function f(x) = x^3 + >>> round(method_2([0, 1], 1000), 10) 0.3320026653 # Test with invalid input >>> round(method_2([0, 2], 0), 10) - 0.0 + Traceback (most recent call last): + ... + ZeroDivisionError: Number of steps must be greater than zero + >>> round(method_2((0, 2), -10), 10) - 0.0 + Traceback (most recent call last): + ... + ZeroDivisionError: Number of steps must be greater than zero """ if steps <= 0: - return 0.0 + raise ZeroDivisionError("Number of steps must be greater than zero") h = (boundary[1] - boundary[0]) / steps a = boundary[0] From ec0358984ca2e6fc264b105bc45a5e0270ded8a3 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 11 Oct 2023 17:44:12 +0000 Subject: [PATCH 14/17] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/simpson_rule.py | 66 +++++++++++++++++++++---------------------- 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/maths/simpson_rule.py b/maths/simpson_rule.py index f4cac6592f20..7a10c4d8b59f 100644 --- a/maths/simpson_rule.py +++ b/maths/simpson_rule.py @@ -13,54 +13,54 @@ def method_2(boundary: list[int], steps: int) -> float: # "Simpson Rule" # int(f) = delta_x/2 * (b-a)/3*(f1 + 4f2 + 2f_3 + ... + fn) """ - Calculate the definite integral of a function using Simpson's Rule. + Calculate the definite integral of a function using Simpson's Rule. - :param boundary: A list containing the lower and upper bounds of integration. - :param steps: The number of steps or resolution for the integration. - :return: The approximate integral value. + :param boundary: A list containing the lower and upper bounds of integration. + :param steps: The number of steps or resolution for the integration. + :return: The approximate integral value. - >>> round(method_2([0, 2, 4], 10), 10) - 2.6666666667 + >>> round(method_2([0, 2, 4], 10), 10) + 2.6666666667 - >>> round(method_2([2, 0], 10), 10) - -0.2666666667 + >>> round(method_2([2, 0], 10), 10) + -0.2666666667 - >>> round(method_2([-2, -1], 10), 10) - 2.172 + >>> round(method_2([-2, -1], 10), 10) + 2.172 - # Test with a linear function f(x) = x - >>> round(method_2([0, 1], 10), 10) - 0.3333333333 + # Test with a linear function f(x) = x + >>> round(method_2([0, 1], 10), 10) + 0.3333333333 - # Test with a constant function f(x) = 5 + # Test with a constant function f(x) = 5 - >>> round(method_2([0, 2], 10), 10) - 2.6666666667 + >>> round(method_2([0, 2], 10), 10) + 2.6666666667 - # Test with a quadratic function f(x) = x^2 + # Test with a quadratic function f(x) = x^2 - >>> round(method_2([0, 2], 100), 10) - 2.5621226667 + >>> round(method_2([0, 2], 100), 10) + 2.5621226667 - # Test with a cubic function f(x) = x^3 + # Test with a cubic function f(x) = x^3 - >>> round(method_2([0, 1], 1000), 10) - 0.3320026653 + >>> round(method_2([0, 1], 1000), 10) + 0.3320026653 - # Test with invalid input + # Test with invalid input - >>> round(method_2([0, 2], 0), 10) - Traceback (most recent call last): - ... - ZeroDivisionError: Number of steps must be greater than zero + >>> round(method_2([0, 2], 0), 10) + Traceback (most recent call last): + ... + ZeroDivisionError: Number of steps must be greater than zero - >>> round(method_2((0, 2), -10), 10) - Traceback (most recent call last): - ... - ZeroDivisionError: Number of steps must be greater than zero - """ + >>> round(method_2((0, 2), -10), 10) + Traceback (most recent call last): + ... + ZeroDivisionError: Number of steps must be greater than zero + """ if steps <= 0: - raise ZeroDivisionError("Number of steps must be greater than zero") + raise ZeroDivisionError("Number of steps must be greater than zero") h = (boundary[1] - boundary[0]) / steps a = boundary[0] From a9b57a86bd9853685f5c789693750b3bf1d25d14 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Wed, 11 Oct 2023 19:58:05 +0200 Subject: [PATCH 15/17] Update simpson_rule.py --- maths/simpson_rule.py | 79 ++++++++++++++++++------------------------- 1 file changed, 33 insertions(+), 46 deletions(-) diff --git a/maths/simpson_rule.py b/maths/simpson_rule.py index 7a10c4d8b59f..7e68f60d80bf 100644 --- a/maths/simpson_rule.py +++ b/maths/simpson_rule.py @@ -14,50 +14,37 @@ def method_2(boundary: list[int], steps: int) -> float: # int(f) = delta_x/2 * (b-a)/3*(f1 + 4f2 + 2f_3 + ... + fn) """ Calculate the definite integral of a function using Simpson's Rule. - - :param boundary: A list containing the lower and upper bounds of integration. - :param steps: The number of steps or resolution for the integration. - :return: The approximate integral value. - - >>> round(method_2([0, 2, 4], 10), 10) - 2.6666666667 - - >>> round(method_2([2, 0], 10), 10) - -0.2666666667 - - >>> round(method_2([-2, -1], 10), 10) - 2.172 - - # Test with a linear function f(x) = x - >>> round(method_2([0, 1], 10), 10) - 0.3333333333 - - # Test with a constant function f(x) = 5 - - >>> round(method_2([0, 2], 10), 10) - 2.6666666667 - - # Test with a quadratic function f(x) = x^2 - - >>> round(method_2([0, 2], 100), 10) - 2.5621226667 - - # Test with a cubic function f(x) = x^3 - - >>> round(method_2([0, 1], 1000), 10) - 0.3320026653 - - # Test with invalid input - - >>> round(method_2([0, 2], 0), 10) - Traceback (most recent call last): - ... - ZeroDivisionError: Number of steps must be greater than zero - - >>> round(method_2((0, 2), -10), 10) - Traceback (most recent call last): - ... - ZeroDivisionError: Number of steps must be greater than zero + :param boundary: A list containing the lower and upper bounds of integration. + :param steps: The number of steps or resolution for the integration. + :return: The approximate integral value. + + >>> round(method_2([0, 2, 4], 10), 10) + 2.6666666667 + >>> round(method_2([2, 0], 10), 10) + -0.2666666667 + >>> round(method_2([-2, -1], 10), 10) + 2.172 + # Test with a linear function f(x) = x + >>> round(method_2([0, 1], 10), 10) + 0.3333333333 + # Test with a constant function f(x) = 5 + >>> round(method_2([0, 2], 10), 10) + 2.6666666667 + # Test with a quadratic function f(x) = x^2 + >>> round(method_2([0, 2], 100), 10) + 2.5621226667 + # Test with a cubic function f(x) = x^3 + >>> round(method_2([0, 1], 1000), 10) + 0.3320026653 + # Test with invalid input + >>> round(method_2([0, 2], 0), 10) + Traceback (most recent call last): + ... + ZeroDivisionError: Number of steps must be greater than zero + >>> round(method_2((0, 2), -10), 10) + Traceback (most recent call last): + ... + ZeroDivisionError: Number of steps must be greater than zero """ if steps <= 0: raise ZeroDivisionError("Number of steps must be greater than zero") @@ -91,8 +78,8 @@ def f(x): # enter your function here def main(): a = 0.0 # Lower bound of integration b = 1.0 # Upper bound of integration - steps = 10.0 # define number of steps or resolution - boundary = [a, b] # define boundary of integration + steps = 10.0 # number of steps or resolution + boundary = [a, b] # boundary of integration y = method_2(boundary, steps) print(f"y = {y}") From dc36fafe43d4d384b3a7724d37b83c5d6ce70ec5 Mon Sep 17 00:00:00 2001 From: AasheeshLikePanner Date: Wed, 11 Oct 2023 23:28:31 +0530 Subject: [PATCH 16/17] Adding doctests in simpson_rule.py --- maths/simpson_rule.py | 70 ++++++++++++++++++++----------------------- 1 file changed, 33 insertions(+), 37 deletions(-) diff --git a/maths/simpson_rule.py b/maths/simpson_rule.py index f4cac6592f20..98c18363889e 100644 --- a/maths/simpson_rule.py +++ b/maths/simpson_rule.py @@ -13,54 +13,50 @@ def method_2(boundary: list[int], steps: int) -> float: # "Simpson Rule" # int(f) = delta_x/2 * (b-a)/3*(f1 + 4f2 + 2f_3 + ... + fn) """ - Calculate the definite integral of a function using Simpson's Rule. + Calculate the definite integral of a function using Simpson's Rule. - :param boundary: A list containing the lower and upper bounds of integration. - :param steps: The number of steps or resolution for the integration. - :return: The approximate integral value. + :param boundary: A list containing the lower and upper bounds of integration. + :param steps: The number of steps or resolution for the integration. + :return: The approximate integral value. - >>> round(method_2([0, 2, 4], 10), 10) - 2.6666666667 + >>> round(method_2([0, 2, 4], 10), 10) + 2.6666666667 - >>> round(method_2([2, 0], 10), 10) - -0.2666666667 + >>> round(method_2([2, 0], 10), 10) + -0.2666666667 - >>> round(method_2([-2, -1], 10), 10) - 2.172 + >>> round(method_2([-2, -1], 10), 10) + 2.172 - # Test with a linear function f(x) = x - >>> round(method_2([0, 1], 10), 10) - 0.3333333333 + # Test with a linear function f(x) = x + >>> round(method_2([0, 1], 10), 10) + 0.3333333333 - # Test with a constant function f(x) = 5 + # Test with a constant function f(x) = 5 + >>> round(method_2([0, 2], 10), 10) + 2.6666666667 - >>> round(method_2([0, 2], 10), 10) - 2.6666666667 + # Test with a quadratic function f(x) = x^2 + >>> round(method_2([0, 2], 100), 10) + 2.5621226667 - # Test with a quadratic function f(x) = x^2 + # Test with a cubic function f(x) = x^3 + >>> round(method_2([0, 1], 1000), 10) + 0.3320026653 - >>> round(method_2([0, 2], 100), 10) - 2.5621226667 + # Test with invalid input + >>> round(method_2([0, 2], 0), 10) + Traceback (most recent call last): + ... + ZeroDivisionError: Number of steps must be greater than zero - # Test with a cubic function f(x) = x^3 - - >>> round(method_2([0, 1], 1000), 10) - 0.3320026653 - - # Test with invalid input - - >>> round(method_2([0, 2], 0), 10) - Traceback (most recent call last): - ... - ZeroDivisionError: Number of steps must be greater than zero - - >>> round(method_2((0, 2), -10), 10) - Traceback (most recent call last): - ... - ZeroDivisionError: Number of steps must be greater than zero - """ + >>> round(method_2([0, 2], -10), 10) + Traceback (most recent call last): + ... + ZeroDivisionError: Number of steps must be greater than zero + """ if steps <= 0: - raise ZeroDivisionError("Number of steps must be greater than zero") + raise ZeroDivisionError("Number of steps must be greater than zero") h = (boundary[1] - boundary[0]) / steps a = boundary[0] From 47b85a36d691340046763ac75c0fba6c0cc1a0b0 Mon Sep 17 00:00:00 2001 From: AasheeshLikePanner Date: Wed, 11 Oct 2023 23:43:27 +0530 Subject: [PATCH 17/17] Adding doctests in simpson_rule.py --- maths/simpson_rule.py | 18 ++---------------- 1 file changed, 2 insertions(+), 16 deletions(-) diff --git a/maths/simpson_rule.py b/maths/simpson_rule.py index 98c18363889e..e75fb557a2f5 100644 --- a/maths/simpson_rule.py +++ b/maths/simpson_rule.py @@ -14,42 +14,28 @@ def method_2(boundary: list[int], steps: int) -> float: # int(f) = delta_x/2 * (b-a)/3*(f1 + 4f2 + 2f_3 + ... + fn) """ Calculate the definite integral of a function using Simpson's Rule. - :param boundary: A list containing the lower and upper bounds of integration. :param steps: The number of steps or resolution for the integration. :return: The approximate integral value. >>> round(method_2([0, 2, 4], 10), 10) 2.6666666667 - >>> round(method_2([2, 0], 10), 10) -0.2666666667 - >>> round(method_2([-2, -1], 10), 10) 2.172 - - # Test with a linear function f(x) = x >>> round(method_2([0, 1], 10), 10) 0.3333333333 - - # Test with a constant function f(x) = 5 >>> round(method_2([0, 2], 10), 10) 2.6666666667 - - # Test with a quadratic function f(x) = x^2 >>> round(method_2([0, 2], 100), 10) 2.5621226667 - - # Test with a cubic function f(x) = x^3 >>> round(method_2([0, 1], 1000), 10) 0.3320026653 - - # Test with invalid input >>> round(method_2([0, 2], 0), 10) Traceback (most recent call last): ... ZeroDivisionError: Number of steps must be greater than zero - >>> round(method_2([0, 2], -10), 10) Traceback (most recent call last): ... @@ -87,8 +73,8 @@ def f(x): # enter your function here def main(): a = 0.0 # Lower bound of integration b = 1.0 # Upper bound of integration - steps = 10.0 # define number of steps or resolution - boundary = [a, b] # define boundary of integration + steps = 10.0 # number of steps or resolution + boundary = [a, b] # boundary of integration y = method_2(boundary, steps) print(f"y = {y}")