Skip to content

Commit 0fd1ccb

Browse files
RobisLVpre-commit-ci[bot]cclauss
authored
Adding inductive reactance calculation (TheAlgorithms#6625)
* Adding inductive reactance calculation * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * from math import pi * 0007957747154594767 * 36420441699332 * 2199114857512855 Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss <cclauss@me.com>
1 parent 39e5bc5 commit 0fd1ccb

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

Diff for: electronics/ind_reactance.py

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# https://en.wikipedia.org/wiki/Electrical_reactance#Inductive_reactance
2+
from __future__ import annotations
3+
4+
from math import pi
5+
6+
7+
def ind_reactance(
8+
inductance: float, frequency: float, reactance: float
9+
) -> dict[str, float]:
10+
"""
11+
Calculate inductive reactance, frequency or inductance from two given electrical
12+
properties then return name/value pair of the zero value in a Python dict.
13+
14+
Parameters
15+
----------
16+
inductance : float with units in Henries
17+
18+
frequency : float with units in Hertz
19+
20+
reactance : float with units in Ohms
21+
22+
>>> ind_reactance(-35e-6, 1e3, 0)
23+
Traceback (most recent call last):
24+
...
25+
ValueError: Inductance cannot be negative
26+
27+
>>> ind_reactance(35e-6, -1e3, 0)
28+
Traceback (most recent call last):
29+
...
30+
ValueError: Frequency cannot be negative
31+
32+
>>> ind_reactance(35e-6, 0, -1)
33+
Traceback (most recent call last):
34+
...
35+
ValueError: Inductive reactance cannot be negative
36+
37+
>>> ind_reactance(0, 10e3, 50)
38+
{'inductance': 0.0007957747154594767}
39+
40+
>>> ind_reactance(35e-3, 0, 50)
41+
{'frequency': 227.36420441699332}
42+
43+
>>> ind_reactance(35e-6, 1e3, 0)
44+
{'reactance': 0.2199114857512855}
45+
46+
"""
47+
48+
if (inductance, frequency, reactance).count(0) != 1:
49+
raise ValueError("One and only one argument must be 0")
50+
if inductance < 0:
51+
raise ValueError("Inductance cannot be negative")
52+
if frequency < 0:
53+
raise ValueError("Frequency cannot be negative")
54+
if reactance < 0:
55+
raise ValueError("Inductive reactance cannot be negative")
56+
if inductance == 0:
57+
return {"inductance": reactance / (2 * pi * frequency)}
58+
elif frequency == 0:
59+
return {"frequency": reactance / (2 * pi * inductance)}
60+
elif reactance == 0:
61+
return {"reactance": 2 * pi * frequency * inductance}
62+
else:
63+
raise ValueError("Exactly one argument must be 0")
64+
65+
66+
if __name__ == "__main__":
67+
import doctest
68+
69+
doctest.testmod()

0 commit comments

Comments
 (0)