-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecitation5.py
More file actions
130 lines (109 loc) · 3.37 KB
/
Recitation5.py
File metadata and controls
130 lines (109 loc) · 3.37 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
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
class Complex:
def __init__(self,r,i):
self._real = r
self._imag = i
def __str__(self):
if self._imag>=0:
return f"{self._real} + {self._imag}i"
else:
return f"{self._real} - {abs(self._imag)}i"
def conjugate(self):
ans = Complex(self._real, -1*self._imag)
return ans
def __mul__(self,other):
if isinstance(other, Complex):
real_part = self._real*other._real-self._imag*other._imag
imag_part = self._real*other._imag+self._imag*other._real
ans = Complex(real_part, imag_part)
else:
ans = Complex(self._real*other, self._imag*other)
return ans
def __rmul__(self,other):
return self * other
class Real(Complex):
'''
>>> x = Real(3)
>>> y = Real(8)
>>> z = Complex(2, 5)
>>> op1 = x * y
>>> print(op1)
24 + 0i
>>> type(op1)
<class '__main__.Real'>
>>> op2 = x * 2
>>> print(op2)
6 + 0i
>>> type(op2)
<class '__main__.Real'>
>>> op3 = 2 * y
>>> print(op3)
16 + 0i
>>> type(op3)
<class '__main__.Real'>
>>> op4 = x * z
>>> print(op4)
6 + 15i
>>> type(op4)
<class '__main__.Complex'>
>>> x = Real(3)
>>> int(x) * [1,2,3]
[1, 2, 3, 1, 2, 3, 1, 2, 3]
>>> y = int(x)
>>> y
3
>>> isinstance(y, int)
True
>>> isinstance(y, Real)
False
>>> z = float(x)
>>> z
3.0
>>> isinstance(z, float)
True
>>> isinstance(z, Real)
False
'''
def __init__(self, value):
super().__init__(value, 0)
#Handles multiplicaton between Real and Real numbers, Real and Complex
#and Real * int or Real * float
#Delegates all other cases to complex value
def __mul__(self, other):
if isinstance(other, Real):
return Real(self._real * other._real)
elif isinstance(other, int) or isinstance(other, float):
return Real(self._real * other)
else:
return super().__mul__(other) #Delegate all other cases to complex class
def __rmul__(self, other):
return self * other
#If it is a real or complex class, compares coordinates to see if they are equal
#Will return false in all other cases
def __eq__(self, other):
''' Returns True if other is a Real object that has the same value or if other is
a Complex object with _imag=0 and same value for _real, False otherwise
>>> Real(4) == Real(4)
True
>>> Real(4) == Real(4.0)
True
>>> Real(5) == Complex(5, 0)
True
>>> Real(5) == Complex(5, 12)
False
>>> Real(5) == 5.5
False
'''
# YOUR CODE STARTS HERE
if isinstance(other, Real) or isinstance(other, Complex):
return (self._real == other._real and self._imag == other._imag)
else:
return False
#Converts real value to int
def __int__(self):
return int(self._real)
#Converts real value to float
def __float__(self):
return float(self._real)
if __name__=='__main__':
import doctest
doctest.testmod()