Advertisement
AllanRocha

Complex Number

Mar 6th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.36 KB | None | 0 0
  1. import math
  2.  
  3. class Complex():
  4.  
  5.     def __init__(self, im=0, re=0, ang=0, mod=0):
  6.         if im == 0 and re == 0:
  7.             self.ang = ang
  8.             self.mod = mod
  9.             self.im = mod * math.sin(math.radians(self.ang))
  10.             self.re = mod * math.cos(math.radians(self.ang))
  11.         else:
  12.             self.im = im
  13.             self.re = re
  14.             self.ang = math.degrees(math.atan(im/re))
  15.             self.mod = math.sqrt(im ** 2 + re ** 2)
  16.    
  17.     def __add__(self, complex_b):
  18.         n_im = self.im + complex_b.im
  19.         n_re = self.re + complex_b.re
  20.         return Complex(im=n_im, re=n_re)
  21.    
  22.     def __sub__(self, complex_b):
  23.         n_im = self.im - complex_b.im
  24.         n_re = self.re - complex_b.re
  25.         return Complex(im=n_im, re=n_re)
  26.    
  27.     def __mul__(self, complex_b):
  28.         n_mod = self.mod * complex_b.mod
  29.         n_ang = self.ang + complex_b.ang
  30.         return Complex(ang=n_ang, mod=n_mod)
  31.    
  32.     def __truediv__(self, complex_b):
  33.         n_mod = self.mod / complex_b.mod
  34.         n_ang = self.ang - complex_b.ang
  35.         return Complex(ang=n_ang, mod=n_mod)
  36.  
  37.     def __str__(self):
  38.         return "{:.3} {:.3}j // {:.3}/{:.3}º".format(self.re, self.im, self.mod, self.ang)
  39.  
  40. v1 = Complex(re=2, im=2)
  41. v2 = Complex(re=4, im=2)
  42. print(v1 * v2)
  43.  
  44. # Defina as variáveis que vão ser usadas abaixo!
  45. # :)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement