Skip to content

Create apparent_power.py #8664

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

Merged
merged 16 commits into from
Apr 18, 2023
Merged
39 changes: 24 additions & 15 deletions electronics/apparent_power.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,34 @@
import cmath
import math


def apparent_power(real_power: float, reactive_power: float) -> float:
def apparent_power(voltage: float, current: float, voltage_angle: float, current_angle: float) -> complex:
"""
Calculate apparent power from real power and reactive power.

Examples:-
>>> apparent_power(100, 50)
111.80339887498948
>>> apparent_power(100, -50)
111.80339887498948
>>> apparent_power(-100, 50)
111.80339887498948
>>> apparent_power(-100, -50)
111.80339887498948
>>> apparent_power(0, 0)
0.0
Calculate the apparent power in a single-phase AC circuit.

>>> apparent_power(100, 5, 0, 0)
(500+0j)
>>> apparent_power(100, 5, 90, 0)
(3.061616997868383e-14+500j)
>>> apparent_power(100, 5, -45, -60)
(-129.40952255126032-482.9629131445341j)
>>> apparent_power(200, 10, -30, -90)
(-999.9999999999998-1732.0508075688776j)
"""
return math.sqrt(real_power**2 + reactive_power**2)
# Convert angles from degrees to radians
voltage_angle_rad = math.radians(voltage_angle)
current_angle_rad = math.radians(current_angle)

# Convert voltage and current to rectangular form
voltage_rect = cmath.rect(voltage, voltage_angle_rad)
current_rect = cmath.rect(current, current_angle_rad)

# Calculate apparent power
apparent_power = voltage_rect * current_rect

return apparent_power

# Doctests
if __name__ == "__main__":
import doctest

Expand Down