-
Notifications
You must be signed in to change notification settings - Fork 0
/
exercise08.py
191 lines (138 loc) · 4.85 KB
/
exercise08.py
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
from numpy import *
from matplotlib.pyplot import *
import math
"""
Define a class ComplexNumber and write an appropriate __init__ method.
"""
class ComplexNumber:
def __init__(self, real=0.0, imag=0.0):
self.real = real
self.imag = imag
"""
TASK2
Write a method which returns the complex number’s real part, and one which
returns the imaginary part.
"""
def real(self):
return self.real
def imag(self):
return self.imag
"""
TASK 3
Write a method is_imaginary and is_real They should return Boolean answers.
"""
def is_imaginary(self):
return self.real == 0 and self.imag != 0
def is_real(self):
return self.imag == 0
"""
TASK 4
Write a representation method which represents a complex number in the mathe-
matical notation a+ib
"""
def __str__(self):
if self.imag >= 0:
return f"{self.real} + {self.imag}i"
else:
return f"{self.real} - {-self.imag}i"
"""
TASK 5
Write a method which returns the complex number’s argument and absolute value.
"""
def get_arg_and_abs(self):
arg = math.atan2(self.imag, self.real)
absolute_val = sqrt(self.real ** 2 + self.imag ** 2)
return arg, absolute_val
"""
TASK 6
Write a method which checks if two complex numbers are equal.
"""
def __eq__(self, other):
return self.real == other.real and self.imag == other.imag
"""
TASK 7
Define methods for operations such as addition, subtraction, multiplication, di-
vision and power of complex numbers. These methods should work also for op-
erations between complex numbers, integers and real numbers (floats). Where
appropriate, make sure that the result is independent of the order of the argu-
ments.
"""
def __add__(self, other):
if isinstance(other, (int, float)):
real_sum = self.real + other
imag_sum = self.imag
elif isinstance(other, ComplexNumber):
real_sum = self.real + other.real
imag_sum = self.imag + other.imag
else:
raise ValueError("Cannot add ComplexNumber with an unsupported type.")
return ComplexNumber(real_sum, imag_sum)
def __radd__(self, other):
return self + other
def __sub__(self, other):
if isinstance(other, (int, float)):
real_sum = self.real - other
imag_sum = self.imag
elif isinstance(other, ComplexNumber):
real_sum = self.real - other.real
imag_sum = self.imag - other.imag
else:
raise ValueError("Cannot add ComplexNumber with an unsupported type.")
return ComplexNumber(real_sum, imag_sum)
def __rsub__(self, other):
if isinstance(other, (int, float)):
real_sum = other - self.real
imag_sum = self.imag
elif isinstance(other, ComplexNumber):
real_sum = other.real - self.real
imag_sum = other.imag - self.imag
else:
raise ValueError("Cannot add ComplexNumber with an unsupported type.")
return ComplexNumber(real_sum, imag_sum)
def __mul__(self, other):
if isinstance(other, (int, float)):
real_sum = other * self.real
imag_sum = other * self.imag
elif isinstance(other, ComplexNumber):
real_sum = other.real * self.real + other.imag * self.imag * -1
imag_sum = other.real * self.imag + other.imag * self.real
else:
raise ValueError("Cannot add ComplexNumber with an unsupported type.")
return ComplexNumber(real_sum, imag_sum)
def __rmul__(self, other):
return self * other
def __truediv__(self, other):
if isinstance(other, (int, float)):
real_sum = self.real / other
imag_sum = self.imag / other
elif isinstance(other, ComplexNumber): # dunno
#real_sum = other.real * self.real + other.imag * self.imag * -1
#imag_sum = other.real * self.imag + other.imag * self.real
pass
else:
raise ValueError("Cannot add ComplexNumber with an unsupported type.")
return ComplexNumber(real_sum, imag_sum)
def __pow__(self, power):
def recursive_pow(current_val, original_val, i):
i -= 1
if i == 0:
return current_val
current_val = current_val * original_val
return recursive_pow(current_val, original_val, i)
return recursive_pow(self, self, power)
first = ComplexNumber(1, 1)
second = 1 + 1j
print(second == first)
c1 = ComplexNumber(3, 4)
c2 = 2.5 + c1
print(c2)
c3 = c1 - 2.5
print("c3 = ", c3)
c4 = 2.5 - c3
print("c4 = ", c4)
c5 = c3 * c4
print("c5 = ", c5)
c6 = c4 ** 3
print("c6 = ", c6)
c7 = c4 / 2
print("c7 = ", c7)