-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomplex.py
More file actions
119 lines (88 loc) · 3.24 KB
/
Copy pathcomplex.py
File metadata and controls
119 lines (88 loc) · 3.24 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
class Complex:
def __init__(self, real, virl):
self.real = real
self.virl = virl
def __str__(self):
if self.real == 0 and self.virl == 0:
return "0"
elif self.real == 0:
return str(self.virl) + "j"
elif self.virl == 0:
return str(self.real)
elif self.virl > 0:
if self.virl == 1:
return str(self.real) + "+j"
else:
return str(self.real) + "+" + str(self.virl) + "j"
else:
if self.virl == -1:
return str(self.real) + "-j"
else:
return str(self.real) + str(self.virl) + "j"
def __add__(self, other):
if isinstance(other, Complex):
return Complex(self.real + other.real, self.virl + other.virl)
elif isinstance(other, int) or isinstance(other, float):
return self + Complex(other, 0)
else: # something is wrong
raise TypeError('unsuported operand type')
def __sub__(self, other):
if isinstance(other, Complex):
return Complex(self.real - other.real, self.virl - other.virl)
elif isinstance(other, int) or isinstance(other, float):
return self - Complex(other, 0)
else: # something is wrong
raise TypeError('unsuported operand type')
def __neg__(self, other):
return Complex(-self.real, -self.virl)
def __mul__(self, other):
if isinstance(other, Complex):
real = self.real * other.real - self.virl * other.virl
virl = self.real * other.virl + self.virl * other.real
return Complex(real, virl)
elif isinstance(other, int) or isinstance(other, float):
return self * Complex(other, 0)
else: # something is wrong
raise TypeError('unsuported operand type')
def __truediv__(self, other):
if isinstance(other, Complex):
result = self * Complex(other.real, -other.virl)
ratio = other.real * other.real + other.virl * other.virl
result.real /= ratio
result.virl /= ratio
return result
elif isinstance(other, int) or isinstance(other, float):
return self / Complex(other, 0)
else: # something is wrong
raise TypeError('unsuported operand type')
def __radd__(self, other):
return self + other
def __rsub__(self, other):
return Complex(0, 0) + other - self
def __rmul__(self, other):
return self * other
def __rtruediv__(self, other):
return Complex(1, 0) * other / self
# test program
def main():
a = Complex(1, 2)
b = Complex(3, 4)
print("a = %s" % (a))
print("b = %s" % (b))
# operators
# two complex
print("a + b = %s" % (a + b))
print("a - b = %s" % (a - b))
print("a * b = %s" % (a * b))
print("a / b = %s" % (a / b))
# complex with double
print("a + 1 = %s" % (a + 1))
print("a - 1 = %s" % (a - 1))
print("a * 2 = %s" % (a * 2))
print("a / 2 = %s" % (a / 2))
# double with complex
print("-3 + b = %s" % (-3 + b))
print("3 - b = %s" % (3 - b))
print("3 * b = %s" % (3 * b))
print("3 / b = %s" % (3 / b))
main()