-
Notifications
You must be signed in to change notification settings - Fork 114
/
Copy path5th_Roots_of_Unity.py
47 lines (36 loc) · 1.4 KB
/
5th_Roots_of_Unity.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
import numpy as np
import matplotlib.pyplot as plt
def calculate_nth_roots_of_unity(n):
# Calculate the nth roots of unity
k_values = np.arange(n)
theta_values = 2 * np.pi * k_values / n
roots = np.cos(theta_values) + 1j * np.sin(theta_values)
return roots
def plot_complex_numbers_on_plane(numbers, title="Complex Numbers on the Complex Plane"):
fig, ax = plt.subplots()
# Plot complex numbers on the complex plane
ax.scatter(numbers.real, numbers.imag, color='b', label='nth Roots of Unity')
ax.axhline(y=0, color='k', linewidth=0.5)
ax.axvline(x=0, color='k', linewidth=0.5)
ax.set_xlabel('Real')
ax.set_ylabel('Imaginary')
ax.set_title(title)
ax.grid(True)
ax.legend()
plt.show()
def sum_of_nth_roots_of_unity(n):
# Calculate the sum of nth roots of unity
roots = calculate_nth_roots_of_unity(n)
return np.sum(roots)
if __name__ == "__main__":
# Example: nth Roots of Unity
n = 5
nth_roots = calculate_nth_roots_of_unity(n)
print(f"nth Roots of Unity (n={n}):")
for idx, root in enumerate(nth_roots):
print(f"Root {idx+1}: {root}")
# 2D Visualization of nth Roots of Unity
plot_complex_numbers_on_plane(nth_roots, title=f"{n}th Roots of Unity")
# Calculate and print the sum of nth roots of unity
sum_of_roots = sum_of_nth_roots_of_unity(n)
print(f"Sum of {n}th Roots of Unity: {sum_of_roots}")