engineerjoe440 / ElectricPy

Electrical Engineering Formulas in Python
https://electricpy.readthedocs.io/en/latest/
MIT License
83 stars 16 forks source link

Phasor Class in `phasor.py` #46

Open Lakshmikanth2001 opened 2 years ago

Lakshmikanth2001 commented 2 years ago

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())
engineerjoe440 commented 2 years ago

This is something I've been interested in exploring. I think it would be worth investigating the value of extending Python's base complex class as a phasor, and formulating the unique methods we need.

Overall, I'm for it! Let's explore what can be done!

engineerjoe440 commented 2 years ago

I've done some exploratory work on this subject. Ultimately, I want to extend the existing functionality from Python's complex type, because I believe it will help set a few guard rails. This work can be explored in https://github.com/engineerjoe440/ElectricPy/blob/dev/investigate-phasor-type-class/electricpy/_phasor.py

I want to acknowledge the length of work you'd gone to on this, however. You've put forth some really tremendous effort! I think if I'm to move forward to this, I'm going to use some of your docstrings, since I like them much more than my own. (mine were very short in some cases).

Please explore and let me know what your thoughts are!

engineerjoe440 commented 4 months ago

I've gone ahead and built a separate Python package called phasors that's available here: https://github.com/engineerjoe440/phasors and provides this exact functionality. I'll be looking to pull that back in for use in this package at some point in the future.