-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcircle_chords.py
More file actions
103 lines (71 loc) · 2.21 KB
/
Copy pathcircle_chords.py
File metadata and controls
103 lines (71 loc) · 2.21 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
from complex import *
import matplotlib.pyplot as plt
from math import sqrt, isclose
from operator import mul
from functools import reduce
import sys
def get_unity_roots(n):
"""
Finds the nth roots of unity
:param n:
:return:
"""
z = Complex(1)
return roots(n, z)
def plot_circle():
"""
Plots a unit circle
:return:
"""
fig = plt.figure(figsize=(8, 8))
plt.xticks([-1, 1])
plt.yticks([-1, 1])
ax = fig.add_subplot(1, 1, 1)
# I used this: https://stackoverflow.com/questions/2409774/how-can-i-produce-student-style-graphs-using-matplotlib
# for some of the formatting
ax.spines['left'].set_position('zero')
ax.spines['right'].set_color('none')
ax.spines['bottom'].set_position('zero')
ax.spines['top'].set_color('none')
ax.set_facecolor('xkcd:light gray')
circ = plt.Circle((0, 0), radius=1, edgecolor='black', facecolor='None')
ax.add_patch(circ)
plt.axis('scaled')
return fig, ax
def plot_roots(unity, ax):
"""
Plots the roots of unity and converts roots of unity from polar to rectangular form
:param unity: roots of unity
:param ax: axes for the figure used for plotting
:return:
"""
roots_rect = []
for root in unity:
z = rect(root[0], root[1])
roots_rect.append(z)
ax.plot(z.re, z.im, 'ro')
return roots_rect
def plot_chords(roots_rect, ax):
"""
Plots the chords from (1,0) to the other n-1 roots of unity
:param roots_rect: roots of unity in rectangular form
:param ax:
:return: list of lengths of the n-1 chords
"""
base = roots_rect[0]
lengths = []
for root in roots_rect[1:]:
ax.plot([base.re, root.re], [base.im, root.im], 'r')
length = sqrt((base.re - root.re) ** 2 + (base.im - root.im) ** 2)
lengths.append(length)
return lengths
if __name__ == '__main__':
n = int(sys.argv[1])
unity = get_unity_roots(n)
fig, ax = plot_circle()
roots_rect = plot_roots(unity, ax)
lengths = plot_chords(roots_rect, ax)
prod = reduce(mul, lengths)
final_str = 'The product of the lengths of the chords is {} and n is {}'.format(prod, n)
print(final_str)
plt.show()