-
Notifications
You must be signed in to change notification settings - Fork 19
Open
Labels
enhancementNew feature or requestNew feature or requestquestionFurther information is requestedFurther information is requested
Description
Can we replace all our functional
code into objected-oriented
fashion so that it will be really intuitive for users to just add, sub, mul, eq phasor
>>> p1 = Phasor(3, 120)
>>> p2 = Phasor(4, 120)
>>> p1 + p2
Phasor(7, 120)
>> p1 - p2
>> p1 * p2
class Phasor:
"""
Complex Phasor Generator.
Generates the standard Pythonic complex representation
of a phasor voltage or current when given the magnitude
and angle of the specific voltage or current.
"""
def __init__(self, mag: float, ang: float) -> None:
"""Initialize the phasor.
Parameters
----------
mag : float
The magnitude of the phasor
ang : float
The angle of the phasor in degrees
"""
self.mag = mag
self.ang = _np.radians(ang)
def __add__(self, other: 'Phasor') -> 'Phasor':
"""Return the sum of the phasors.
Parameters
----------
other : object
The other phasor to add to the current phasor
Returns
-------
object
The sum of the two phasors
"""
if isinstance(other, Phasor):
a = _c.rect(self.mag, self.ang)
b = _c.rect(other.mag, other.ang)
return Phasor(_np.abs(a + b), _np.radians(_np.angle(a + b)))
else:
return ValueError("Phasor can only be added to another phasor")
def __sub__(self, other: 'Phasor') -> 'Phasor':
"""Return the difference of the phasors.
Parameters
----------
other : object
The other phasor to subtract from the current phasor
Returns
-------
object
The difference of the two phasors
"""
if isinstance(other, Phasor):
a = _c.rect(self.mag, self.ang)
b = _c.rect(other.mag, other.ang)
return Phasor(_np.abs(a - b), _np.radians(_np.angle(a + b)))
else:
return ValueError("Phasor can only be subtracted from another phasor")
def __mul__(self, other: 'Phasor') -> 'Phasor':
"""Return the product of the phasors.
Parameters
----------
other: object
The other phasor to subtract from the current phasor
Returns
-------
object
The difference of the two phasors
"""
return Phasor(self.mag * other.mag, self.ang + other.ang)
def __eq__(self, __o: 'Phasor') -> bool:
"""Return True if the phasors are equal.
Parameters
----------
__o : Phasor
The other Phasor object to compare to the current phasor
Returns
-------
bool
True if the phasors are equal, False otherwise
"""
if isinstance(__o, Phasor):
return self.mag == __o.mag and self.ang == __o.ang
else:
return False
def __str__(self) -> str:
"""Return the string representation of the phasor."""
return _cprint(self())
Metadata
Metadata
Assignees
Labels
enhancementNew feature or requestNew feature or requestquestionFurther information is requestedFurther information is requested