forked from gc3-uzh-ch/python-course
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex08a.py
executable file
·64 lines (52 loc) · 1.62 KB
/
ex08a.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
#! /usr/bin/env python
class ComplexNum(object):
"A complex number z = z1 + i * z2."
def __init__(self, z1, z2):
self.re = z1
self.im = z2
def __add__(self, other):
return ComplexNum(
self.re+other.re, self.im+other.im)
def __mul__(self, other):
return ComplexNum(
self.re*other.re - self.im*other.im,
self.re*other.im + self.im*other.re)
def __str__(self):
return ("%g + i%g" % (self.re, self.im))
def __eq__(self, other):
return (self.re == other.re) and (self.im == other.im)
def to_power(self, exponent):
"""
Raise this complex number to integer power `exponent`.
Non-recursive version.
"""
z = ComplexNum(1,0)
for i in range(exponent):
z = z*self
return z
def to_power_recursive(self, n):
"""
Raise this complex number to integer power `n`.
Recursive version; i.e., use mathematical definition of exponentiation.
"""
if n == 0:
return ComplexNum(1,0)
else:
return self*self.to_power_recursive(n-1)
if __name__ == '__main__':
z = ComplexNum(1,1)
# z^0 is 1
assert z.to_power(0) == ComplexNum(1,0)
# z^1 is z
assert z.to_power(1) == z
# for small powers, we can spell out the product in full
assert z.to_power(2) == z*z
assert z.to_power(3) == z*z*z
## same set of tests, but for the "recursive" version
# z^0 is 1
assert z.to_power_recursive(0) == ComplexNum(1,0)
# z^1 is z
assert z.to_power_recursive(1) == z
# for small powers, we can spell out the product in full
assert z.to_power_recursive(2) == z*z
assert z.to_power_recursive(3) == z*z*z