-
Notifications
You must be signed in to change notification settings - Fork 101
Expand file tree
/
Copy pathbuild.py
More file actions
48 lines (38 loc) · 1.54 KB
/
build.py
File metadata and controls
48 lines (38 loc) · 1.54 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
# %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 __str__(self):
if self.imag <0:
return '{0} +i{1}.format(self.real, self.imag)'
else:
return '{}+ i{}.format(self.real, self.imag)'
def __add__(self,other):
return complex_number(self.real + other.real,
self.imag + other.imag)
def __sub__(self,other):
return complex_number(self.real - other.real,
self.imag - other.imag)
def __mul__(self,other):
return complex_number(self.real * other.real - self.imag * other.imag,
self.imag * other.real + self.real * other.imag)
def __truediv__(first,second):
#r=(other.real+other.imag)
return((first.real * second.real+first.imag * second.imag)/(second.real*second.real +second.imag*second.imag),
(first.imag * second.real-first.imag * second.imag)/(second.real*second.real +second.imag*second.imag))
def abs(self):
return math.sqrt(self.real**2 + self.imag**2)
def conjugate(self):
return complex_number(self.real, -self.imag)
def argument(self):
return math.degrees(math.atan(self.real/self.imag))