Пример #1
0
 def __mul__(self, other):
     if isinstance(other, numbers.Real):
         return type(self)(self.real * other, self.imag * other)
     if isinstance(other, numbers.Complex):
         return type(self)(self.real * other.real - self.imag * other.imag,
                           self.real * other.imag + self.imag * other.real)
     return NotImplemented
Пример #2
0
 def __truediv__(self, other):
     if isinstance(other, numbers.Real):
         return type(self)(self.real / other, self.imag / other)
     if isinstance(other, numbers.Complex):
         denom = other.real**2 + other.imag**2
         return type(self)(
             (self.real * other.real + self.imag * other.imag) / denom,
             (self.imag * other.real - self.real * other.imag) / denom)
     return NotImplemented
Пример #3
0
 def __rpow__(self, other):
     if isinstance(other, numbers.Complex):
         a = ((other**2)
              **(self.real / 2)) * math.e**(-self.imag * cmath.phase(other))
         b = 0.5 * self.imag * math.log(other**
                                        2) + self.real * cmath.phase(other)
         return type(self)(1, 0) * (a * cmath.cos(b)) + type(self)(
             0, 1) * (a * cmath.sin(b))
     return NotImplemented
Пример #4
0
 def __pow__(self, other):
     i = type(self)(0, 1)
     one = type(self)(1, 0)
     if isinstance(other, numbers.Real):
         r = abs(self)
         theta = cmath.phase(self)
         return (r**other) * (one * math.cos(other * theta) + i *
                              (math.sin(other * theta)))
     elif isinstance(other, numbers.Complex):
         r = abs(self)
         theta = cmath.phase(self)
         return (r**other) * (one * cmath.cos(other * theta) + i *
                              (cmath.sin(other * theta)))
     return NotImplemented
Пример #5
0
 def __pos__(self):
     return type(self)(self)
Пример #6
0
 def __neg__(self):
     return type(self)(-self.real, -self.imag)
Пример #7
0
 def __add__(self, other):
     if isinstance(other, numbers.Complex):
         return type(self)(self.real + other.real, self.imag + other.imag)
     if isinstance(other, numbers.Real):
         return type(self)(self.real + other, self.imag)
     return NotImplemented
Пример #8
0
 def __reversed__(self):
     return type(self)(self.imag, self.real)
Пример #9
0
 def __rmod__(self, other):
     if isinstance(other, numbers.Complex):
         return other + self * type(self).__ceil__(-other / self)
     return NotImplemented
Пример #10
0
 def __round__(self, n=None):
     return type(self)(round(self.real, n), round(self.imag, n))
Пример #11
0
 def __ceil__(self):
     return type(self)(math.ceil(self.real), math.ceil(self.imag))
Пример #12
0
 def __floor__(self):
     return type(self)(math.floor(self.real), math.floor(self.imag))
Пример #13
0
 def __repr__(self):
     return '{type.__name__}({self.real!r}, {self.imag!r})'.format(
         self=self, type=type(self))
Пример #14
0
 def conjugate(self):
     return type(self)(self.real, -self.imag)