-
Notifications
You must be signed in to change notification settings - Fork 101
Expand file tree
/
Copy pathbuild.py
More file actions
56 lines (41 loc) · 1.48 KB
/
build.py
File metadata and controls
56 lines (41 loc) · 1.48 KB
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
# %load q01_create_class/build.py
import pandas as pd
import numpy as np
import math
'write your solution here'
class complex_number:
'''The complex number class.
Attributes:
attr1 (x): Real part of complex number.
attr2 (y): Imaginary part of complex number.
'''
def __init__(self,real,imag):
self.real = real
self.imag = imag
def __add__(self, other):
x=self.real+other.real
y=self.imag+other.imag
return complex_number(x,y)
def __sub__(self,other):
x=self.real-other.real
y=self.imag-other.imag
return complex_number(x,y)
def __mul__(self,other):
x=(self.real*other.real)-(self.imag*other.imag)
y= (other.real*self.imag)+(self.real*other.imag)
return complex_number(x,y)
def __truediv__(self,other):
x=((self.real*other.real)+(self.imag*other.imag))/((other.real**2+ other.imag**2))
y=((self.imag*other.real)-(self.real*other.imag))/((other.real**2+ other.imag**2))
return x,y
def __str__(self):
if '-' in str(self.imag):
return '{0}-i{1}'.format(self.real,-self.imag)
else :
return '{0}+i{1}'.format(self.real,self.imag)
def abs(self):
return (math.sqrt(float(self.real)**2+float(self.imag)**2))
def conjugate(self):
return complex_number((self.real),(self.imag*-1))
def argument(self):
return math.degrees(math.atan(self.imag/self.real))