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

Develop Phasor Class #116

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions electricpy/_phasor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
################################################################################
"""Test Phasor Class."""
################################################################################

# Import Required Packages
import sys as _sys
import numpy as _np
import cmath as _c

class Phasor(complex):
"""
An extension of the Python `complex` type for work in polar coordinates.

This class can be used in place of, or alongside the standard `complex` type
in Python to represent complex numbers as phasors (magnitude and angle) for
scientific computation and electrical engineering analysis and work.

Examples
--------
>>> from electricpy._phasor import Phasor
>>> Phasor(67, 120) # 67 volts at angle 120 degrees
Phasor(magnitude=67, angle=120)
>>> volt = Phasor(67, 120)
>>> print(volt)
67 ∠ 120°

Parameters
----------
magnitude: float
The phasor magnitude to express.
angle: float
The phasor angle to express, in degrees.

Properties
----------
mag: float
The phasor magnitude, also commonly known as its absolute value.
ang: float
The phasor angle, expressed in degrees.
real: float
The real component of the complex value.
imag: float
The imaginary component of the complex value.
"""

def __new__(self, magnitude, angle=None):
"""
Phasor Constructor.
"""
# Handle Passing a Complex Type Directly to Phasor
if isinstance(magnitude, complex):
magnitude, ang_r = _c.polar(magnitude)
angle = _np.degrees(ang_r)
return complex.__new__(
self,
real=(magnitude * _np.cos(_np.radians(angle))),
imag=(magnitude * _np.sin(_np.radians(angle)))
)

@property
def mag(self):
"""Phasor magnitude evaluation."""
return _np.absolute(self)

@property
def ang(self):
"""Phasor angle evaluation in degrees."""
return _np.degrees(_np.angle(self))

def __repr__(self):
"""Represent the Phasor."""
return f"Phasor(magnitude={self.mag}, angle={self.ang})"

def __str__(self):
"""Stringlify the Phasor."""
angle_denotion = "∠"
if _sys.stdout.encoding != "utf-8":
angle_denotion = "/_"
return f"{self.mag} {angle_denotion} {self.ang}°"

def __round__(self, ndigits=0):
"""Round the Phasor."""
return Phasor(
round(self.mag, ndigits=ndigits),
round(self.ang, ndigits=ndigits)
)

@staticmethod
def from_arraylike(arraylike):
"""
Phasor Constructor for Casting from Array Like Object.

Use this method to create a new Phasor object from a two-item (2) long
arraylike object, (e.g., a `tuple`, `list`, or NumPy array).
"""
return Phasor(*arraylike)
3 changes: 1 addition & 2 deletions electricpy/phasors.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ def phs(ang):
"""
Complex Phase Angle Generator.

Generate a complex value given the phase angle
for the complex value.
Generate a complex value given the phase angle for the complex value.

Same as `phase`.

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from setuptools import setup

setup()
setup()
Loading