-
-
Notifications
You must be signed in to change notification settings - Fork 48.7k
Adding algorithm delta-star transformation #12356
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MRJPEREZR
wants to merge
7
commits into
TheAlgorithms:master
Choose a base branch
from
Miranda13:adding-algorithm-delta-star-transformation
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9fb47fa
new file: star_delta_transform.py
MRJPEREZR c5ba7fb
updating DIRECTORY.md
MRJPEREZR 9bd3c3f
fixing typos
MRJPEREZR c8117dc
Merge branch 'adding-algorithm-delta-star-transformation' of github.c…
MRJPEREZR 873dcf0
adding description to r argument
MRJPEREZR 31b4818
adding description to r argument
MRJPEREZR 78929f6
adding description to r argument
MRJPEREZR File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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: | ||
MRJPEREZR marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
MRJPEREZR marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
""" | ||
>>> 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: | ||
MRJPEREZR marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
MRJPEREZR marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
MRJPEREZR marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
""" | ||
>>> 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: | ||
MRJPEREZR marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
MRJPEREZR marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
MRJPEREZR marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
""" | ||
>>> 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(): | ||
MRJPEREZR marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
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() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.