Skip to content

Commit 9681ad0

Browse files
rohan472000pre-commit-ci[bot]cclauss
authored andcommitted
Create apparent_power.py (TheAlgorithms#8664)
* Create apparent_power.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update apparent_power.py * Update apparent_power.py * Update apparent_power.py * Update electronics/apparent_power.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update electronics/apparent_power.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update apparent_power.py * Update electronics/apparent_power.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update apparent_power.py * Update apparent_power.py * Update apparent_power.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update apparent_power.py * Update apparent_power.py * Update apparent_power.py --------- 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 e270556 commit 9681ad0

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

electronics/apparent_power.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import cmath
2+
import math
3+
4+
5+
def apparent_power(
6+
voltage: float, current: float, voltage_angle: float, current_angle: float
7+
) -> complex:
8+
"""
9+
Calculate the apparent power in a single-phase AC circuit.
10+
11+
>>> apparent_power(100, 5, 0, 0)
12+
(500+0j)
13+
>>> apparent_power(100, 5, 90, 0)
14+
(3.061616997868383e-14+500j)
15+
>>> apparent_power(100, 5, -45, -60)
16+
(-129.40952255126027-482.9629131445341j)
17+
>>> apparent_power(200, 10, -30, -90)
18+
(-999.9999999999998-1732.0508075688776j)
19+
"""
20+
# Convert angles from degrees to radians
21+
voltage_angle_rad = math.radians(voltage_angle)
22+
current_angle_rad = math.radians(current_angle)
23+
24+
# Convert voltage and current to rectangular form
25+
voltage_rect = cmath.rect(voltage, voltage_angle_rad)
26+
current_rect = cmath.rect(current, current_angle_rad)
27+
28+
# Calculate apparent power
29+
return voltage_rect * current_rect
30+
31+
32+
if __name__ == "__main__":
33+
import doctest
34+
35+
doctest.testmod()

0 commit comments

Comments
 (0)