Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions electronics/hopkinsons_law.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# https://en.wikipedia.org/wiki/Magnetic_circuit#Hopkinson's_law
from __future__ import annotations


def hopkinsons_law(
magnetomotive_force: float, flux: float, reluctance: float
) -> dict[str, float]:
"""
Apply Hopkinson's Law, on any two given electrical values, which can be magnetomotive force, flux,
and reluctance, and then in a Python dict return name/value pair of the zero value.

>>> hopkinsons_law(magnetomotive_force=10, reluctance=5, flux=0)
{'flux': 2.0}
>>> hopkinsons_law(magnetomotive_force=0, flux=0, reluctance=10)
Traceback (most recent call last):
...
ValueError: One and only one argument must be 0
>>> hopkinsons_law(magnetomotive_force=0, flux=1, reluctance=-2)
Traceback (most recent call last):
...
ValueError: Reluctance cannot be negative
>>> hopkinsons_law(reluctance=0, magnetomotive_force=-10, flux=1)
{'reluctance': -10.0}
>>> hopkinsons_law(magnetomotive_force=0, flux=-1.5, reluctance=2)
{'magnetomotive_force': -3.0}
"""
if (magnetomotive_force, flux, reluctance).count(0) != 1:
raise ValueError("One and only one argument must be 0")
if reluctance < 0:
raise ValueError("Reluctance cannot be negative")
if magnetomotive_force == 0:
return {"magnetomotive_force": float(flux * reluctance)}
elif flux == 0:
return {"flux": magnetomotive_force / reluctance}
elif reluctance == 0:
return {"reluctance": magnetomotive_force / flux}
else:
raise ValueError("Exactly one argument must be 0")


if __name__ == "__main__":
import doctest

doctest.testmod()