Skip to content
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

Phasor Class in phasor.py #46

Open
Lakshmikanth2001 opened this issue Feb 25, 2022 · 3 comments
Open

Phasor Class in phasor.py #46

Lakshmikanth2001 opened this issue Feb 25, 2022 · 3 comments
Labels
enhancement New feature or request question Further information is requested

Comments

@Lakshmikanth2001
Copy link
Contributor

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
Copy link
Owner

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
Copy link
Owner

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 engineerjoe440 added enhancement New feature or request question Further information is requested labels May 7, 2022
@engineerjoe440
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants