From 2bbc27d83c5ae3a076a7d0085a4bfdd5ab5e497e Mon Sep 17 00:00:00 2001 From: Julian Perez Ramirez Date: Sat, 19 Oct 2024 20:26:24 +0200 Subject: [PATCH 1/6] adding doctests to trapezoidal_rule.py --- maths/trapezoidal_rule.py | 48 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 45 insertions(+), 3 deletions(-) diff --git a/maths/trapezoidal_rule.py b/maths/trapezoidal_rule.py index 9a4ddc8af66b..0186629ee378 100644 --- a/maths/trapezoidal_rule.py +++ b/maths/trapezoidal_rule.py @@ -5,13 +5,25 @@ method 1: "extended trapezoidal rule" +int(f) = dx/2 * (f1 + 2f2 + ... + fn) """ def method_1(boundary, steps): - # "extended trapezoidal rule" - # int(f) = dx/2 * (f1 + 2f2 + ... + fn) + """ + Apply the extended trapezoidal rule to approximate the integral of function f(x) + over the interval defined by 'boundary' with the number of 'steps'. + + Args: + boundary (list of floats): A list containing the start and end values [a, b]. + steps (int): The number of steps or subintervals. + Returns: + float: Approximation of the integral of f(x) over [a, b]. + Examples: + >>> method_1([0, 1], 10) + 0.3349999999999999 + """ h = (boundary[1] - boundary[0]) / steps a = boundary[0] b = boundary[1] @@ -26,13 +38,40 @@ def method_1(boundary, steps): def make_points(a, b, h): + """ + Generates points between 'a' and 'b' with step size 'h', excluding the end points. + Args: + a (float): Start value + b (float): End value + h (float): Step size + Examples: + >>> list(make_points(0, 10, 2.5)) + [2.5, 5.0, 7.5] + + >>> list(make_points(0, 10, 2)) + [2, 4, 6, 8] + + >>> list(make_points(1, 21, 5)) + [6, 11, 16] + + >>> list(make_points(1, 5, 2)) + [3] + + >>> list(make_points(1, 4, 3)) + [] + """ x = a + h - while x < (b - h): + while x <= (b - h): yield x x = x + h def f(x): # enter your function here + """ + Example: + >>> f(2) + 4 + """ y = (x - 0) * (x - 0) return y @@ -47,4 +86,7 @@ def main(): if __name__ == "__main__": + import doctest + + doctest.testmod() main() From c19694810426d1be3d8c70f171b58cab4f33a863 Mon Sep 17 00:00:00 2001 From: Julian Perez Ramirez Date: Thu, 7 Nov 2024 10:40:02 +0100 Subject: [PATCH 2/6] adding algorithm delta-star transformation --- electronics/star_delta_transform.py | 125 ++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 electronics/star_delta_transform.py diff --git a/electronics/star_delta_transform.py b/electronics/star_delta_transform.py new file mode 100644 index 000000000000..62dc925fc460 --- /dev/null +++ b/electronics/star_delta_transform.py @@ -0,0 +1,125 @@ +""" +In electrical engineering, the Y-Δ transform, also written wye-delta and also known by +many other names, is a mathematical technique to simplify the analysis of an electrical +network. +The name derives from the shapes of the circuit diagrams, which look respectively like +the letter Y and the Greek capital letter Δ. This circuit transformation theory was +published by Arthur Edwin Kennelly in 1899. It is widely used in analysis of three-phase +electric power circuits. + +The Y-Δ transform can be considered a special case of the star-mesh transform for three +resistors. In mathematics, the Y-Δ transform plays an important role in theory of +circular planar graphs. + +Source: https://en.wikipedia.org/wiki/Y-%CE%94_transform +""" + +from sys import exit +from unittest import mock + + +def delta_to_wye(r: list) -> dict: + """ + >>> from_delta_to_wye([2.0, 3.0, 4.0]) + {'r1': 1.3333333333333333, 'r2': 0.8888888888888888, 'r3': 0.6666666666666666} + """ + r_wye: dict = {} + ra, rb, rc = r[0], r[1], r[2] + r_wye.update({"r1": rb * rc / (ra + rb + rc)}) + r_wye.update({"r2": ra * rc / (ra + rb + rc)}) + r_wye.update({"r3": ra * rb / (ra + rb + rc)}) + return r_wye + + +def wye_to_delta(r: list) -> dict: + """ + >>> from_wye_to_delta([2.0, 3.0, 4.0]) + {'ra': 13.0, 'rb': 8.666666666666666, 'rc': 6.5} + """ + r1, r2, r3 = r[0], r[1], r[2] + r_delta: dict = {} + r_delta.update({"ra": (r1 * r2 + r2 * r3 + r3 * r1) / r1}) + r_delta.update({"rb": (r1 * r2 + r2 * r3 + r3 * r1) / r2}) + r_delta.update({"rc": (r1 * r2 + r2 * r3 + r3 * r1) / r3}) + return r_delta + + +def transform(mode: int, r: list) -> dict: + """ + >>> transform(1, [4.0, 5.0, 6.0]) + {'r1': 2.0, 'r2': 1.6, 'r3': 1.3333333333333333} + + >>> transform(2, [4.0, 5.0, 6.0]) + {'ra': 18.5, 'rb': 14.8, 'rc': 12.333333333333334} + """ + r_transformed = {} + if mode == 1: + r_transformed = delta_to_wye(r) + elif mode == 2: + r_transformed = wye_to_delta(r) + return r_transformed + + +def get_type_transform(): + mode: int = 0 + try: + print(""" + 1. From delta to wye + 2. From wye to delta + """) + mode = int(input("? --> ")) + except ValueError: + print("Invalid Value. Only int inputs are accepted") + exit() + return mode + + +def get_resistors_values(mode: int) -> list: + r: list = [] + print("Select conversion (type 1 or 2)") + try: + if mode == 1: + r = list( + map( + float, input("Resistant values (format ra rb rc): ").strip().split() + ) + )[:3] + elif mode == 2: + r = list( + map( + float, input("Resistant values (format r1 r2 r3): ").strip().split() + ) + )[:3] + else: + print("Incorrect selected option. Valid option 1 or 2") + except ValueError: + print("Invalid Value. Only int inputs are accepted") + exit() + return r + + +def test_get_type_transformation() -> None: + with mock.patch("builtins.input", return_value="1"): + m = get_type_transform() + assert m == 1 + + +def test_get_resistors_values() -> None: + with mock.patch("builtins.input", return_value="2 4 8"): + r = get_resistors_values(2) + assert r == [2.0, 4.0, 8.0] + + +def main() -> None: + print("star - delta transform") + mode = get_type_transform() + r = get_resistors_values(mode) + r_transformed = transform(mode, r) + print(f"Result: '{r_transformed}'") + + +if __name__ == "__main__": + from doctest import testmod + + testmod() + main() From 431cbe4f37539bb9be5dfff65bea9d248f3c1b21 Mon Sep 17 00:00:00 2001 From: MRJPEREZR Date: Thu, 7 Nov 2024 09:40:29 +0000 Subject: [PATCH 3/6] updating DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index f0a34a553946..2a5ac5584fb4 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -422,6 +422,7 @@ * [Resistor Color Code](electronics/resistor_color_code.py) * [Resistor Equivalence](electronics/resistor_equivalence.py) * [Resonant Frequency](electronics/resonant_frequency.py) + * [Star Delta Transform](electronics/star_delta_transform.py) * [Wheatstone Bridge](electronics/wheatstone_bridge.py) ## File Transfer From 5d8b3f3eddfb583da624ca8242d24d1098af9108 Mon Sep 17 00:00:00 2001 From: Julian Perez Ramirez Date: Thu, 7 Nov 2024 11:09:47 +0100 Subject: [PATCH 4/6] delete file star_delta_transform.py --- electronics/star_delta_transform.py | 125 ---------------------------- 1 file changed, 125 deletions(-) delete mode 100644 electronics/star_delta_transform.py diff --git a/electronics/star_delta_transform.py b/electronics/star_delta_transform.py deleted file mode 100644 index 62dc925fc460..000000000000 --- a/electronics/star_delta_transform.py +++ /dev/null @@ -1,125 +0,0 @@ -""" -In electrical engineering, the Y-Δ transform, also written wye-delta and also known by -many other names, is a mathematical technique to simplify the analysis of an electrical -network. -The name derives from the shapes of the circuit diagrams, which look respectively like -the letter Y and the Greek capital letter Δ. This circuit transformation theory was -published by Arthur Edwin Kennelly in 1899. It is widely used in analysis of three-phase -electric power circuits. - -The Y-Δ transform can be considered a special case of the star-mesh transform for three -resistors. In mathematics, the Y-Δ transform plays an important role in theory of -circular planar graphs. - -Source: https://en.wikipedia.org/wiki/Y-%CE%94_transform -""" - -from sys import exit -from unittest import mock - - -def delta_to_wye(r: list) -> dict: - """ - >>> from_delta_to_wye([2.0, 3.0, 4.0]) - {'r1': 1.3333333333333333, 'r2': 0.8888888888888888, 'r3': 0.6666666666666666} - """ - r_wye: dict = {} - ra, rb, rc = r[0], r[1], r[2] - r_wye.update({"r1": rb * rc / (ra + rb + rc)}) - r_wye.update({"r2": ra * rc / (ra + rb + rc)}) - r_wye.update({"r3": ra * rb / (ra + rb + rc)}) - return r_wye - - -def wye_to_delta(r: list) -> dict: - """ - >>> from_wye_to_delta([2.0, 3.0, 4.0]) - {'ra': 13.0, 'rb': 8.666666666666666, 'rc': 6.5} - """ - r1, r2, r3 = r[0], r[1], r[2] - r_delta: dict = {} - r_delta.update({"ra": (r1 * r2 + r2 * r3 + r3 * r1) / r1}) - r_delta.update({"rb": (r1 * r2 + r2 * r3 + r3 * r1) / r2}) - r_delta.update({"rc": (r1 * r2 + r2 * r3 + r3 * r1) / r3}) - return r_delta - - -def transform(mode: int, r: list) -> dict: - """ - >>> transform(1, [4.0, 5.0, 6.0]) - {'r1': 2.0, 'r2': 1.6, 'r3': 1.3333333333333333} - - >>> transform(2, [4.0, 5.0, 6.0]) - {'ra': 18.5, 'rb': 14.8, 'rc': 12.333333333333334} - """ - r_transformed = {} - if mode == 1: - r_transformed = delta_to_wye(r) - elif mode == 2: - r_transformed = wye_to_delta(r) - return r_transformed - - -def get_type_transform(): - mode: int = 0 - try: - print(""" - 1. From delta to wye - 2. From wye to delta - """) - mode = int(input("? --> ")) - except ValueError: - print("Invalid Value. Only int inputs are accepted") - exit() - return mode - - -def get_resistors_values(mode: int) -> list: - r: list = [] - print("Select conversion (type 1 or 2)") - try: - if mode == 1: - r = list( - map( - float, input("Resistant values (format ra rb rc): ").strip().split() - ) - )[:3] - elif mode == 2: - r = list( - map( - float, input("Resistant values (format r1 r2 r3): ").strip().split() - ) - )[:3] - else: - print("Incorrect selected option. Valid option 1 or 2") - except ValueError: - print("Invalid Value. Only int inputs are accepted") - exit() - return r - - -def test_get_type_transformation() -> None: - with mock.patch("builtins.input", return_value="1"): - m = get_type_transform() - assert m == 1 - - -def test_get_resistors_values() -> None: - with mock.patch("builtins.input", return_value="2 4 8"): - r = get_resistors_values(2) - assert r == [2.0, 4.0, 8.0] - - -def main() -> None: - print("star - delta transform") - mode = get_type_transform() - r = get_resistors_values(mode) - r_transformed = transform(mode, r) - print(f"Result: '{r_transformed}'") - - -if __name__ == "__main__": - from doctest import testmod - - testmod() - main() From 729f2126dcec09de023b5b2a607ab2154d1f952e Mon Sep 17 00:00:00 2001 From: MRJPEREZR Date: Thu, 7 Nov 2024 10:12:48 +0000 Subject: [PATCH 5/6] updating DIRECTORY.md --- DIRECTORY.md | 1 - 1 file changed, 1 deletion(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index 2a5ac5584fb4..f0a34a553946 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -422,7 +422,6 @@ * [Resistor Color Code](electronics/resistor_color_code.py) * [Resistor Equivalence](electronics/resistor_equivalence.py) * [Resonant Frequency](electronics/resonant_frequency.py) - * [Star Delta Transform](electronics/star_delta_transform.py) * [Wheatstone Bridge](electronics/wheatstone_bridge.py) ## File Transfer From 769e8dd50db750553b1d34dc1c7907d573c558f4 Mon Sep 17 00:00:00 2001 From: Julian Perez Ramirez Date: Thu, 7 Nov 2024 11:14:50 +0100 Subject: [PATCH 6/6] modified: ../DIRECTORY.md --- DIRECTORY.md | 1 - 1 file changed, 1 deletion(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index 2a5ac5584fb4..f0a34a553946 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -422,7 +422,6 @@ * [Resistor Color Code](electronics/resistor_color_code.py) * [Resistor Equivalence](electronics/resistor_equivalence.py) * [Resonant Frequency](electronics/resonant_frequency.py) - * [Star Delta Transform](electronics/star_delta_transform.py) * [Wheatstone Bridge](electronics/wheatstone_bridge.py) ## File Transfer