|
| 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