-
Notifications
You must be signed in to change notification settings - Fork 4
/
2.73-2.76 imaginary number.py
109 lines (64 loc) · 2.02 KB
/
2.73-2.76 imaginary number.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
from orderedPair import *
from dataOriented import *
from math import sin, cos, atan
# rectangular imaginary number system
def real_part(z):
return car(z)
def imag_part(z):
return cdr(z)
def make_from_real_imag(x, y):
return cons(x, y)
def magnitude(z):
return ((real_part(z)**2) + (imag_part(z)**2)) ** 0.5
def angle(z):
return atan(imag_part(z)/real_part(z))
def make_from_mag_ang(r, a):
return cons(r*cos(a), r*sin(a))
def tag(x):
return attach_tag('rectangular', x)
put('real_part', 'rectangular', real_part)
put('imag_part', 'rectangular', imag_part)
put('magnitude', 'rectangular', magnitude)
put('angle', 'rectangular', angle)
put('make_from_real_imag', 'rectangular',
lambda x, y: tag(make_from_real_imag(x, y)))
put('make_from_mag_ang', 'rectangular',
lambda r, a: tag(make_from_mag_ang(r, a)))
# polar imaginary number system
def magnitude(z):
return car(z)
def angle(z):
return cdr(z)
def make_from_mag_ang(r, a):
return cons(r, a)
def real_part(z):
return magnitude(z) * cos(angle(z))
def imag_part(z):
return magnitude(z) * sin(angle(z))
def make_from_real_imag(x, y):
return cons((x**2 + y**2)**0.5, atan(y/x))
def tag(x):
return attach_tag('polar', x)
put('real_part', 'polar', real_part)
put('imag_part', 'polar', imag_part)
put('magnitude', 'polar', magnitude)
put('angle', 'polar', angle)
put('make_from_real_imag', 'polar',
lambda x, y: tag(make_from_real_imag(x, y)))
put('make_from_mag_ang', 'polar',
lambda r, a: tag(make_from_mag_ang(r, a)))
# generic imaginary number system
def real_part(z):
return appaly_generic('real_part', z)
def imag_part(z):
return appaly_generic('imag_part', z)
def magnitude(z):
return appaly_generic('magnitude', z)
def angle(z):
return appaly_generic('angle', z)
def make_from_real_imag(x, y):
return get('make_from_real_imag', 'rectangular')(x, y)
def make_from_mag_ang(r, a):
return get('make_from_mag_ang', 'polar')(r, a)
if __name__ == '__main__':
print(op_type_table)