-
Notifications
You must be signed in to change notification settings - Fork 101
Expand file tree
/
Copy pathbuild.py
More file actions
65 lines (42 loc) · 1.61 KB
/
build.py
File metadata and controls
65 lines (42 loc) · 1.61 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
55
56
57
58
59
# %load q01_create_class/build.py
import pandas as pd
import numpy as np
import math
'write your solution here'
class complex_number:
def __init__(self, x, y):
self.real = float(x)
self.imag = float(y)
def __str__(self):
operator = '+'
if self.imag < 0:
operator = '-'
return '%.1f %s %.1fi'%((self.real), operator, abs(self.imag))
def __add__(self, other):
new_x = self.real + other.real
new_y = self.imag + other.imag
return complex_number(new_x, new_y)
def __sub__(self, other):
new_x = self.real - other.real
new_y = self.imag - other.imag
return complex_number(new_x, new_y)
def __mul__(self, other):
new_x = (self.real * other.real) - (self.imag * other.imag)
new_y = (self.real * other.imag) + (self.imag * other.real)
return complex_number(new_x, new_y)
def __truediv__(self, other):
new_x = (self.real * other.real) + (self.imag * other.imag)
new_y = (self.imag * other.real) - (self.real * other.imag)
denominator = ((other.real ** 2) + (other.imag ** 2))
new_x = new_x / denominator
new_y = new_y / denominator
return (new_x,new_y)
def __abs__(self):
return self.abs()
def abs(self):
squares = (self.real ** 2) + (self.imag ** 2)
return math.sqrt(squares)
def conjugate(self):
return complex_number(self.real, -self.imag)
def argument(self):
return ((180/math.pi)*(math.atan2(self.imag, self.real)))