-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheecomplex.py
205 lines (153 loc) · 7.66 KB
/
eecomplex.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import math
from math import radians, degrees
class EEComplex:
'''
In physics and engineering, a phasor (a portmanteau of phase vector)
is a complex number representing a sinusoidal function whose
amplitude (A), angular frequency (ω), and initial phase (θ) are time-invariant.
Impedance values can also be saved in the form of phasors, altough they do not have an angular frequency
Admitances as well
To capture all of these in 1 simple object, we use the EEComplex.
'''
def __init__(self, complexNumber: complex, units: list[list[str],list[str]], frequency: float = None) -> None:
self.units = units
self.complex = complexNumber
self.frequency = frequency
self.roundTo = 2
self.eecomplexChecker()
self.unitChecker()
# When using the classmethods, you should just give a string for unit, this unit will end up in the nominator
@classmethod
def fromComplex(cls, realComponent: float, imaginaryComponent: float, unit: str, frequency: float = None):
complexNumber = complex(realComponent, imaginaryComponent)
# Check if the unit given is a singular or plural
if isinstance(unit, str):
units = [[unit], []]
else:
units = unit
return cls(complexNumber, units, frequency)
@classmethod
def fromPolar(cls, magnitude: float, argument: float, unit: str, frequency: float = None):
realComponent = math.cos(radians(argument))*magnitude
imaginaryComponent = math.sin(radians(argument))*magnitude
complexNumber = complex(realComponent, imaginaryComponent)
# Check if the unit given is a singular or plural
if isinstance(unit, str):
units = [[unit], []]
else:
units = unit
return cls(complexNumber, units, frequency)
# Check if it has a frequency, if so unit can only be V or A
def eecomplexChecker(self):
for i in self.units[0]:
if i not in ["V", "A", "Ohm", "S"]:
raise ValueError("Nominator must only contain be V, A, S or Ohm")
for i in self.units[1]:
if i not in ["V", "A", "Ohm", "S"]:
raise ValueError("Denominator must only contain V, A, S or Ohm")
# I really want a better way to do this, but I can't think of one right now.
def unitChecker(self):
nominator = self.units[0]
denominator = self.units[1]
#Remove the common units from the nominator and denominator
commonUnits = [value for value in nominator if value in denominator]
for unit in commonUnits:
nominator.remove(unit)
denominator.remove(unit)
#Check if combination of units is another unit
# U = ZI, I = U/Z, Z = U/I
if "Ohm" in nominator and "A" in nominator:
nominator.remove("Ohm")
nominator.remove("A")
nominator.append("V")
if "V" in nominator and "Ohm" in denominator:
nominator.remove("V")
denominator.remove("Ohm")
nominator.append("A")
if "V" in nominator and "A" in denominator:
nominator.remove("V")
denominator.remove("A")
nominator.append("Ohm")
# V = A/S, A = V/S, S = V/A
# Might add more later
def getRealComponent(self):
return self.complex.real
def getImaginaryComponent(self):
return self.complex.imag
def getMagnitude(self):
return abs(self.complex)
def getArgument(self):
return degrees(math.atan(self.getImaginaryComponent()/self.getRealComponent()))
# Is this neccecary? I can check the unit using [nominator,denominator]
def unitCheck(self, unitToCheck: str) -> bool:
if not (self.units[0].count(unitToCheck) and len(self.units[0]) == 1) and not self.units[1]:
return False
else:
return True
def __str__(self):
nominatorString = ""
denominatorString = ""
for i in self.units[0]:
nominatorString += str(i)
for i in self.units[1]:
denominatorString += str(i)
if denominatorString == "":
unitsstring = nominatorString
elif nominatorString == "":
unitsstring = "1/" + denominatorString
else:
unitsstring = nominatorString + "/" + denominatorString
return f"{round(self.getRealComponent(),self.roundTo)} + j{round(self.getImaginaryComponent(),self.roundTo)} {unitsstring} | {round(self.getMagnitude(), self.roundTo)}∠{round(self.getArgument(),self.roundTo)}° {unitsstring}"
def __add__(self, other: "EEComplex") -> "EEComplex":
if self.frequency != other.frequency:
return ValueError("Frequencies must be the same!")
if self.units != other.units:
return ValueError("Units must be the same!")
real = self.getRealComponent() + other.getRealComponent()
imaginary = self.getImaginaryComponent() + other.getImaginaryComponent()
return EEComplex.fromComplex(real, imaginary, self.units, self.frequency)
def __radd__(self, other):
if other == 0:
return self
else:
return other.__add__(self)
def __sub__(self, other: "EEComplex") -> "EEComplex":
if self.frequency != other.frequency:
return ValueError("Frequencies must be the same!")
if self.units != other.units:
return ValueError("Units must be the same!")
real = self.getRealComponent() - other.getRealComponent()
imaginary = self.getImaginaryComponent() - other.getImaginaryComponent()
return EEComplex.fromComplex(real, imaginary, self.units, self.frequency)
def __mul__(self, other: "EEComplex") -> "EEComplex":
#MULTIPLYING TWO PHASORS WITH DIFFERENT FREQUENCY is not allowed
# if you are trying to multiply with an impedance, it ignores the frequency check
if (self.frequency != other.frequency) and (other.frequency != None or self.frequency != None):
return ValueError("Frequencies must be the same!")
#It sets the frequency of the new phasor to the frequency of the not-impedance value
if self.frequency == None:
frequency = other.frequency
else:
frequency = self.frequency
argument = self.getArgument() + other.getArgument()
magnitude = self.getMagnitude() * other.getMagnitude()
nominator = self.units[0] + other.units[0]
denominator = self.units[1] + other.units[1]
units = [[nominator], [denominator]]
return EEComplex.fromPolar(magnitude, argument, units, frequency)
def __truediv__(self, other: "EEComplex") -> "EEComplex":
#DIVIDING TWO PHASORS WITH DIFFERENT FREQUENCY is not allowed
# if you are trying to multiply with an impedance, it ignores the frequency check
if (self.frequency != other.frequency) and (other.frequency != None or self.frequency != None):
return ValueError("Frequencies must be the same!")
#It sets the frequency of the new phasor to the frequency of the not-impedance value
if self.frequency == None:
frequency = other.frequency
else:
frequency = self.frequency
argument = self.getArgument() - other.getArgument()
magnitude = self.getMagnitude() / other.getMagnitude()
nominator = self.units[0] + other.units[1]
denominator = self.units[1] + other.units[0]
units = [nominator, denominator]
return EEComplex.fromPolar(magnitude, argument, units, frequency)