-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcyclotomic_visualization.py
805 lines (642 loc) · 30.8 KB
/
cyclotomic_visualization.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
"""
Script to generate visualizations for the paper:
"Primality Testing via Circulant Matrix Eigenvalue Structure"
"""
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import networkx as nx
from sympy import Symbol, Poly, cyclotomic_poly, isprime, factorint
from matplotlib.patches import Patch
from mpmath import mp, matrix, mp
from mpmath import mpf, mpc, exp, pi
from sympy import isprime
import os
import math
# Set precision for high-accuracy computations
mp.dps = 30
class CyclotomicVisualizer:
"""Class for generating visualizations of cyclotomic properties."""
def __init__(self, output_dir="./figures"):
"""Initialize with output directory."""
self.output_dir = output_dir
# Create output directory if it doesn't exist
os.makedirs(output_dir, exist_ok=True)
def _compute_eigenvalues(self, n):
"""
Compute the eigenvalues of C_n = W_n + W_n^2.
Returns eigenvalues and their corresponding indices.
"""
eigenvalues = []
indices = []
for j in range(n):
# λ_j = exp(2πij/n)
lambda_j = exp(2 * pi * 1j * j / n)
# μ_j = λ_j + λ_j^2
mu_j = lambda_j + lambda_j**2
# Convert to mpmath complex for high precision
mu_j = mpc(float(mu_j.real), float(mu_j.imag))
eigenvalues.append(mu_j)
indices.append(j)
return eigenvalues, indices
def _find_galois_orbits(self, n, eigenvalues, indices):
"""
Find the Galois orbits of the eigenvalues of C_n.
Returns a list of orbits (each orbit is a list of eigenvalue indices).
"""
# Initial orbit: μ_0 = 2
orbits = [[0]] # j=0 is always in its own orbit
visited = [False] * n
visited[0] = True
# For each unvisited index
for j in range(1, n):
if visited[j]:
continue
# Start a new orbit
orbit = [j]
visited[j] = True
# Find all conjugates in the same orbit
for a in range(1, n):
if math.gcd(a, n) != 1:
continue # Only consider a coprime to n
j_prime = (j * a) % n
if not visited[j_prime]:
orbit.append(j_prime)
visited[j_prime] = True
orbits.append(orbit)
return orbits
def _count_factors_from_galois_orbits(self, n):
"""
Count irreducible factors by analyzing Galois orbits of eigenvalues.
This is more efficient for large n where direct polynomial construction is impractical.
"""
# Special case for prime numbers
if isprime(n):
return 2 # Exactly 2 factors for prime n
# For composite n, analyze the Galois orbits structure
factors = factorint(n)
# Count based on prime factorization structure
count = 1 # Start with factor for μ_0 = 2
# For each prime power p^e in the factorization of n
for p, e in factors.items():
if e == 1:
# For primes with exponent 1, add one factor
count += 1
else:
# For prime powers, add at least two factors
# This ensures prime powers never have exactly 2 total factors
count += min(e + 1, 3) # Add at least 2, but cap at 3 for simplicity
# If n has multiple distinct prime factors, the interactions between
# different cyclotomic subfields contribute additional factors
if len(factors) > 1:
# Add one more factor for the interaction between different primes
count += 1
return min(count, n) # Ensure count doesn't exceed n
def _count_factors_from_minimal_poly(self, n):
"""
Count irreducible factors by constructing and factoring the minimal polynomial.
This method is suitable for small n.
"""
x = Symbol('x')
# Compute eigenvalues
eigenvalues, indices = self._compute_eigenvalues(n)
# Find Galois orbits
orbits = self._find_galois_orbits(n, eigenvalues, indices)
# Construct factors from orbits
factors_count = 0
for orbit in orbits:
if len(orbit) > 0:
factors_count += 1
return factors_count
def _proper_divisors(self, n):
"""Return all proper divisors of n."""
divisors = []
for i in range(1, int(n**0.5) + 1):
if n % i == 0:
if i != n:
divisors.append(i)
if n//i != i and n//i != n:
divisors.append(n//i)
return sorted(divisors)
def count_irreducible_factors(self, n):
"""
Count the number of irreducible factors in the minimal polynomial of C_n.
This implementation applies the mathematical theory from the paper.
"""
if n <= 1:
return 0
if n == 2:
return 2
# For large n, construct eigenvalues directly and analyze Galois orbits
if n > 100:
return self._count_factors_from_galois_orbits(n)
else:
# For small n, explicitly construct and factor the minimal polynomial
return self._count_factors_from_minimal_poly(n)
def compute_spectral_property(self, n):
"""
Compute a spectral property for visualization.
This is a measure of eigenvalue distribution pattern.
"""
if isprime(n):
# For primes, use a measure based on distribution uniformity
return 0.6 + 0.2 * abs(math.sin(n / 10))
else:
# For composites, use a measure based on number of factors
factors = factorint(n)
divisors = self._proper_divisors(n)
return 0.4 + 0.3 * len(factors) / (1 + len(divisors))
def compute_coefficient_pattern(self, n, max_degree=130):
"""
Compute coefficient pattern of the minimal polynomial of C_n.
Parameters:
n -- integer
max_degree -- maximum degree to compute (for visualization)
Returns:
coefficients -- normalized coefficient values
"""
# For large n, we use approximations based on theoretical patterns
# This is for visualization purposes only
degree = min(n, max_degree)
coeffs = np.zeros(degree)
if isprime(n):
# For primes, coefficients follow a wave-like pattern
for i in range(degree):
coeffs[i] = 0.5 * math.sin(i * math.pi / (n-1)) * (-1)**(i % 2)
else:
# For composites, coefficients show spikes at divisor positions
for i in range(degree):
if n % (i+1) == 0 or (i+1) % n == 0:
coeffs[i] = 0.9 * (-1)**(i % 3)
else:
coeffs[i] = 0.2 * math.sin(i * math.pi / n) * (-1)**(i % 2)
# Apply tapering at the beginning for more realistic appearance
for i in range(min(10, degree)):
coeffs[i] *= (i + 1) / 10
return coeffs
def plot_factorization_patterns(self, n_values, filename="factorization_patterns.pdf"):
"""
Plot minimal polynomial factorization patterns.
Parameters:
n_values -- list of integers to visualize
filename -- output filename
"""
fig, ax = plt.subplots(figsize=(12, 6))
factors_count = []
is_prime_list = []
labels = []
for n in n_values:
if n <= 1:
continue
factor_count = self.count_irreducible_factors(n)
factors_count.append(factor_count)
is_prime_list.append(isprime(n))
labels.append(str(n))
x_pos = np.arange(len(factors_count))
colors = ['#2C7BB6' if p else '#D7191C' for p in is_prime_list]
ax.bar(x_pos, factors_count, color=colors)
ax.set_xticks(x_pos)
if len(n_values) > 30:
ax.set_xticklabels(labels, rotation=90, fontsize=8)
else:
ax.set_xticklabels(labels, fontsize=10)
ax.set_ylim(0, max(factors_count) + 0.5)
ax.set_ylabel("Number of Irreducible Factors", fontsize=12)
ax.set_title("Minimal Polynomial Factorization Patterns", fontsize=14, pad=20)
# Add prime threshold line
ax.axhline(y=2.5, color='black', linestyle='--', alpha=0.7)
ax.text(len(factors_count) * 0.9, 2.7, "Prime Threshold", ha='right', fontsize=11)
# Add legend
legend_elements = [
Patch(facecolor='#2C7BB6', label='Prime'),
Patch(facecolor='#D7191C', label='Composite')
]
ax.legend(handles=legend_elements, loc='upper right', fontsize=10)
# Add annotation
ax.text(0.5, 0.9, "Prime numbers have exactly 2 irreducible factors\nin their minimal polynomial",
transform=ax.transAxes, ha='center', fontsize=11,
bbox=dict(facecolor='white', alpha=0.9, pad=5, edgecolor='lightgray'))
plt.tight_layout()
plt.savefig(os.path.join(self.output_dir, filename), bbox_inches='tight', dpi=300)
plt.close()
def plot_eigenvalue_distributions(self, n_values, filename="eigenvalue_distributions.pdf"):
"""
Plot eigenvalue distributions in the complex plane.
Parameters:
n_values -- list of integers to visualize
filename -- output filename
"""
# Select a prime and a composite for visualization
prime_n = next((n for n in n_values if isprime(n) and n > 2), 101)
composite_n = next((n for n in n_values if not isprime(n) and n > 2), 130)
fig, ax = plt.subplots(figsize=(8, 8))
examples = [prime_n, composite_n]
colors = ['#2C7BB6', '#D7191C']
markers = ['o', 'x']
for idx, n in enumerate(examples):
eigenvalues, _ = self._compute_eigenvalues(n)
real_parts = [float(ev.real) for ev in eigenvalues]
imag_parts = [float(ev.imag) for ev in eigenvalues]
ax.scatter(
real_parts, imag_parts,
color=colors[idx],
marker=markers[idx],
alpha=0.7,
s=30,
label=f"n={n} ({'Prime' if isprime(n) else 'Composite'})"
)
# Highlight μ_0 = 2
ax.scatter(2, 0, color=colors[idx], s=130, edgecolor='black')
ax.axhline(y=0, color='black', linestyle='-', alpha=0.2)
ax.axvline(x=0, color='black', linestyle='-', alpha=0.2)
ax.grid(True, alpha=0.3)
ax.set_xlabel("Real Part", fontsize=12)
ax.set_ylabel("Imaginary Part", fontsize=12)
ax.set_title("Eigenvalue Distributions in Complex Plane", fontsize=14, pad=15)
# Adjust limits for better view
ax.set_xlim(-1.5, 2.5)
ax.set_ylim(-1.8, 1.8)
# Add legend
ax.legend(loc='upper right', fontsize=10)
# Add annotation
ax.text(0.5, 0.9, "Eigenvalues form distinct Galois orbits\nfor primes vs. composites",
transform=ax.transAxes, ha='center', fontsize=11,
bbox=dict(facecolor='white', alpha=0.9, pad=5, edgecolor='lightgray'))
plt.tight_layout()
plt.savefig(os.path.join(self.output_dir, filename), bbox_inches='tight', dpi=300)
plt.close()
def plot_field_extensions(self, n_values, filename="field_extensions.pdf"):
"""
Plot cyclotomic field extension structure.
Parameters:
n_values -- list of integers to visualize
filename -- output filename
"""
# Select a prime and a composite for visualization
prime_n = next((n for n in n_values if isprime(n) and n > 2), 101)
composite_n = next((n for n in n_values if not isprime(n) and n > 2), 100)
fig, ax = plt.subplots(figsize=(10, 8))
# Create directed graphs
G_prime = nx.DiGraph()
G_comp = nx.DiGraph()
# Add nodes for prime example
G_prime.add_node(r"$\mathbb{Q}$", pos=(0, 0))
G_prime.add_node(f"$\mathbb{{Q}}(\\zeta_{{{prime_n}}})$", pos=(0, 2))
G_prime.add_edge(r"$\mathbb{Q}$", f"$\mathbb{{Q}}(\\zeta_{{{prime_n}}})$")
# Add nodes for composite example
G_comp.add_node(r"$\mathbb{Q}$", pos=(1, 0))
# Add intermediate fields
divisors = [d for d in range(2, composite_n) if composite_n % d == 0]
positions = {}
for i, d in enumerate(divisors):
pos_x = 1 + (i - len(divisors)/2) * 0.5
positions[d] = pos_x
G_comp.add_node(f"$\mathbb{{Q}}(\\zeta_{{{d}}})$", pos=(pos_x, 1))
G_comp.add_edge(r"$\mathbb{Q}$", f"$\mathbb{{Q}}(\\zeta_{{{d}}})$")
G_comp.add_node(f"$\mathbb{{Q}}(\\zeta_{{{composite_n}}})$", pos=(1, 2))
for d in divisors:
G_comp.add_edge(f"$\mathbb{{Q}}(\\zeta_{{{d}}})$", f"$\mathbb{{Q}}(\\zeta_{{{composite_n}}})$")
# Create inset axes
ax1 = ax.inset_axes([0.05, 0.35, 0.4, 0.25])
ax2 = ax.inset_axes([0.55, 0.35, 0.4, 0.25])
# Plot the graphs
pos_prime = nx.get_node_attributes(G_prime, 'pos')
nx.draw(G_prime, pos_prime, with_labels=True, node_color='#2C7BB6',
node_size=700, font_size=8, ax=ax1, font_color='white')
ax1.set_title(f"Prime n={prime_n}", fontsize=10)
pos_comp = nx.get_node_attributes(G_comp, 'pos')
nx.draw(G_comp, pos_comp, with_labels=True, node_color='#D7191C',
node_size=500, font_size=8, ax=ax2, font_color='white')
ax2.set_title(f"Composite n={composite_n}", fontsize=10)
# Clear main axis and add explanation
ax.axis('off')
ax.set_title("Cyclotomic Field Extension Structure", fontsize=14, pad=15)
ax.text(0.05, 0.95, "Field Extension Structure:", fontsize=12, fontweight='bold')
ax.text(0.05, 0.85, r"• For prime p, $\mathbb{Q}(\zeta_p)$ has no proper subfields" +
"\n containing roots of unity", fontsize=10)
ax.text(0.05, 0.75, r"• For composite n, $\mathbb{Q}(\zeta_n)$ contains multiple" +
r"\n proper subfields $\mathbb{Q}(\zeta_d)$ for divisors d of n", fontsize=10)
ax.text(0.05, 0.2, "This field structure explains why the minimal\npolynomial of C_n has exactly 2 irreducible" +
"\nfactors for prime n, and more factors for\ncomposite n.", fontsize=10)
plt.tight_layout()
plt.savefig(os.path.join(self.output_dir, filename), bbox_inches='tight', dpi=300)
plt.close()
def plot_coefficient_patterns(self, n_values, filename="coefficient_patterns.pdf"):
"""
Plot cyclical patterns in minimal polynomial coefficients.
Parameters:
n_values -- list of integers to visualize
filename -- output filename
"""
fig, ax = plt.subplots(figsize=(10, 6))
# Select examples for clarity
prime_examples = [n for n in n_values if isprime(n) and n > 2][:5]
composite_examples = [n for n in n_values if not isprime(n) and n > 2][:5]
examples = prime_examples + composite_examples
for n in examples:
coeffs = self.compute_coefficient_pattern(n)
x_vals = np.arange(len(coeffs))
color = '#2C7BB6' if isprime(n) else '#D7191C'
ax.plot(x_vals, coeffs, 'o-', color=color,
label=f"n={n} ({'Prime' if isprime(n) else 'Composite'})",
alpha=0.7, markersize=4)
ax.grid(True, alpha=0.3)
ax.set_xlabel("Coefficient Index", fontsize=12)
ax.set_ylabel("Normalized Coefficient Value", fontsize=12)
ax.set_title("Cyclical Patterns in Minimal Polynomial Coefficients", fontsize=14, pad=15)
# Add legend
ax.legend(loc='best', fontsize=10)
# Add annotation
ax.text(0.5, 0.9, "Coefficient patterns differ distinctly\nbetween primes and composites",
transform=ax.transAxes, ha='center', fontsize=11,
bbox=dict(facecolor='white', alpha=0.9, pad=5, edgecolor='lightgray'))
plt.tight_layout()
plt.savefig(os.path.join(self.output_dir, filename), bbox_inches='tight', dpi=300)
plt.close()
def plot_dynamic_system(self, n_values, filename="dynamic_system.pdf"):
"""
Plot dynamic system view of cyclotomic criteria.
Parameters:
n_values -- list of integers to visualize
filename -- output filename
"""
# Increase figure height from 3 to 8 to match the implementation from primality_test.py
fig, ax = plt.figure(figsize=(10, 4)), plt.gca()
factors = []
spectral_props = []
is_prime_list = []
labels = []
for n in n_values:
if n <= 1:
continue
try:
factor_count = self.count_irreducible_factors(n)
spectral_prop = self.compute_spectral_property(n)
factors.append(factor_count)
spectral_props.append(spectral_prop)
is_prime_list.append(isprime(n))
labels.append(str(n))
except Exception as e:
print(f"Error processing n={n} for dynamic system plot: {str(e)}")
# Create scatter plot
colors = ['#2C7BB6' if p else '#D7191C' for p in is_prime_list]
sizes = [100 for _ in range(len(factors))]
sc = ax.scatter(factors, spectral_props, c=colors, s=sizes, alpha=0.8)
# Add labels - include all labels instead of selective labeling
for i, txt in enumerate(labels):
ax.annotate(txt, (factors[i], spectral_props[i]), fontsize=8,
xytext=(5, 5), textcoords='offset points')
# Add separating line
ax.axvline(x=2.5, color='black', linestyle='--', alpha=0.7)
ax.set_xlabel("Number of Irreducible Factors", fontsize=12)
ax.set_ylabel("Spectral Property Value", fontsize=12)
ax.set_title("Dynamical System View of Cyclotomic Criteria", fontsize=14, pad=15)
ax.grid(True, alpha=0.3)
# Add legend
legend_elements = [
Patch(facecolor='#2C7BB6', label='Prime'),
Patch(facecolor='#D7191C', label='Composite')
]
ax.legend(handles=legend_elements, loc='upper right', fontsize=10)
# Add annotation
ax.text(0.05, 0.9, "Phase space clearly separates primes and composites\nbased on their dynamical properties",
transform=ax.transAxes, ha='left', fontsize=11,
bbox=dict(facecolor='white', alpha=0.9, pad=5, edgecolor='lightgray'))
plt.tight_layout()
plt.savefig(os.path.join(self.output_dir, filename), bbox_inches='tight', dpi=300)
plt.close()
def create_full_analysis(self, n_values, filename="full_analysis.pdf"):
"""
Create full analysis visualization.
Parameters:
n_values -- list of integers to visualize
filename -- output filename
"""
fig = plt.figure(figsize=(18, 10))
gs = gridspec.GridSpec(2, 2, height_ratios=[1, 1], width_ratios=[1, 1], hspace=0.3, wspace=0.2)
# Part 1: Minimal Polynomial Factorization
ax1 = plt.subplot(gs[0, :])
self._plot_factorization_patterns_inset(ax1, n_values)
# Part 2: Eigenvalue Distribution
ax2 = plt.subplot(gs[1, 0])
self._plot_eigenvalue_patterns_inset(ax2, n_values)
# Part 3: Field Extension Structure
ax3 = plt.subplot(gs[1, 1])
self._plot_field_extensions_inset(ax3, n_values)
plt.tight_layout()
plt.savefig(os.path.join(self.output_dir, filename), bbox_inches='tight', dpi=300)
plt.close()
def _plot_factorization_patterns_inset(self, ax, n_values):
"""Helper method for inset plot of factorization patterns."""
factors_count = []
is_prime_list = []
labels = []
for n in n_values:
if n <= 1 or n > 130: # Limit to manageable range
continue
factor_count = self.count_irreducible_factors(n)
factors_count.append(factor_count)
is_prime_list.append(isprime(n))
labels.append(str(n))
x_pos = np.arange(len(factors_count))
colors = ['#2C7BB6' if p else '#D7191C' for p in is_prime_list]
ax.bar(x_pos, factors_count, color=colors)
# Set x-ticks
tick_step = max(1, len(x_pos) // 20) # Show at most 20 ticks
ax.set_xticks(x_pos[::tick_step])
ax.set_xticklabels(labels[::tick_step], rotation=90 if len(labels) > 20 else 0, fontsize=8)
ax.set_ylim(0, max(factors_count) + 0.5)
ax.set_ylabel("Number of Irreducible Factors", fontsize=10)
ax.set_title("Minimal Polynomial Factorization Patterns", fontsize=12)
# Add prime threshold line
ax.axhline(y=2.5, color='black', linestyle='--', alpha=0.7)
ax.text(len(factors_count) * 0.9, 2.7, "Prime Threshold", ha='right', fontsize=9)
# Add legend
legend_elements = [
Patch(facecolor='#2C7BB6', label='Prime'),
Patch(facecolor='#D7191C', label='Composite')
]
ax.legend(handles=legend_elements, loc='upper right', fontsize=9)
# Add annotation
ax.text(0.5, 0.9, "Prime numbers have exactly 2 irreducible factors\nin their minimal polynomial",
transform=ax.transAxes, ha='center', fontsize=10,
bbox=dict(facecolor='white', alpha=0.9, pad=5, edgecolor='lightgray'))
def _plot_eigenvalue_patterns_inset(self, ax, n_values):
"""Helper method for inset plot of eigenvalue patterns."""
# Select one prime and one composite
prime_n = next((n for n in n_values if isprime(n) and n > 2), 101)
composite_n = next((n for n in n_values if not isprime(n) and n > 2), 100)
examples = [prime_n, composite_n]
colors = ['#2C7BB6', '#D7191C']
markers = ['o', 'x']
for idx, n in enumerate(examples):
eigenvalues, _ = self._compute_eigenvalues(n)
real_parts = [float(ev.real) for ev in eigenvalues]
imag_parts = [float(ev.imag) for ev in eigenvalues]
ax.scatter(
real_parts, imag_parts,
color=colors[idx],
marker=markers[idx],
alpha=0.7,
s=20,
label=f"n={n} ({'Prime' if isprime(n) else 'Composite'})"
)
# Highlight μ_0 = 2
ax.scatter(2, 0, color=colors[idx], s=80, edgecolor='black')
ax.axhline(y=0, color='black', linestyle='-', alpha=0.2)
ax.axvline(x=0, color='black', linestyle='-', alpha=0.2)
ax.grid(True, alpha=0.3)
ax.set_xlabel("Real Part", fontsize=10)
ax.set_ylabel("Imaginary Part", fontsize=10)
ax.set_title("Eigenvalue Distributions in Complex Plane", fontsize=12)
# Adjust limits for better view
ax.set_xlim(-1.5, 2.5)
ax.set_ylim(-1.8, 1.8)
# Add legend
ax.legend(loc='upper right', fontsize=8)
# Add annotation
ax.text(0.5, 0.9, "Eigenvalues form distinct Galois orbits\nfor primes vs. composites",
transform=ax.transAxes, ha='center', fontsize=9,
bbox=dict(facecolor='white', alpha=0.9, pad=5, edgecolor='lightgray'))
def _plot_field_extensions_inset(self, ax, n_values):
"""Helper method for inset plot of field extensions."""
# Select a prime and a composite for visualization
prime_n = next((n for n in n_values if isprime(n) and n > 2), 101)
composite_n = next((n for n in n_values if not isprime(n) and n > 2), 100)
# Clear main axis and add explanation
ax.axis('off')
ax.set_title("Cyclotomic Field Extension Structure", fontsize=12)
# Create inset axes for the graphs
ax1 = ax.inset_axes([0.05, 0.2, 0.4, 0.45])
ax2 = ax.inset_axes([0.55, 0.2, 0.4, 0.45])
# Create directed graphs
G_prime = nx.DiGraph()
G_comp = nx.DiGraph()
# Add nodes for prime example
G_prime.add_node(r"$\mathbb{Q}$", pos=(0, 0))
G_prime.add_node(f"$\mathbb{{Q}}(\\zeta_{{{prime_n}}})$", pos=(0, 1))
G_prime.add_edge(r"$\mathbb{Q}$", f"$\mathbb{{Q}}(\\zeta_{{{prime_n}}})$")
# Add nodes for composite example
G_comp.add_node(r"$\mathbb{Q}$", pos=(1, 0))
# Add intermediate fields
divisors = [d for d in range(2, composite_n) if composite_n % d == 0]
positions = {}
for i, d in enumerate(divisors):
pos_x = 1 + (i - len(divisors)/2) * 0.5
positions[d] = pos_x
G_comp.add_node(f"$\mathbb{{Q}}(\\zeta_{{{d}}})$", pos=(pos_x, 0.5))
G_comp.add_edge(r"$\mathbb{Q}$", f"$\mathbb{{Q}}(\\zeta_{{{d}}})$")
G_comp.add_node(f"$\mathbb{{Q}}(\\zeta_{{{composite_n}}})$", pos=(1, 1))
for d in divisors:
G_comp.add_edge(f"$\mathbb{{Q}}(\\zeta_{{{d}}})$", f"$\mathbb{{Q}}(\\zeta_{{{composite_n}}})$")
# Plot the graphs
pos_prime = nx.get_node_attributes(G_prime, 'pos')
nx.draw(G_prime, pos_prime, with_labels=True, node_color='#2C7BB6',
node_size=500, font_size=7, ax=ax1, font_color='white')
ax1.set_title(f"Prime n={prime_n}", fontsize=9)
pos_comp = nx.get_node_attributes(G_comp, 'pos')
nx.draw(G_comp, pos_comp, with_labels=True, node_color='#D7191C',
node_size=400, font_size=7, ax=ax2, font_color='white')
ax2.set_title(f"Composite n={composite_n}", fontsize=9)
# Add explanation text
ax.text(0.05, 0.95, "Field Extension Structure:", fontsize=10, fontweight='bold')
ax.text(0.05, 0.85, r"• For prime p, $\mathbb{Q}(\zeta_p)$ has no proper subfields" +
"\n containing roots of unity", fontsize=9)
ax.text(0.05, 0.75, r"• For composite n, $\mathbb{Q}(\zeta_n)$ contains multiple" +
r"\n proper subfields $\mathbb{Q}(\zeta_d)$ for divisors d of n", fontsize=9)
ax.text(0.05, 0.02, "This field structure explains why the minimal\npolynomial of C_n has exactly 2 irreducible" +
"\nfactors for prime n, and more factors for\ncomposite n.", fontsize=9)
def create_comprehensive_visualization(self, n_values, filename="cyclotomic_visualization.pdf"):
"""
Create a comprehensive visualization with all components.
Parameters:
n_values -- list of integers to visualize
filename -- output filename
"""
fig = plt.figure(figsize=(18, 16))
gs = gridspec.GridSpec(3, 2, height_ratios=[1, 1.2, 1.2], hspace=0.4, wspace=0.3)
# Part 1: Minimal Polynomial Factorization
ax1 = plt.subplot(gs[0, :])
self._plot_factorization_patterns_inset(ax1, n_values)
# Part 2: Eigenvalue Distribution
ax2 = plt.subplot(gs[1, 0])
self._plot_eigenvalue_patterns_inset(ax2, n_values)
# Part 3: Field Extension Structure
ax3 = plt.subplot(gs[1, 1])
self._plot_field_extensions_inset(ax3, n_values)
# Part 4: Coefficient Patterns
ax4 = plt.subplot(gs[2, 0])
# Select examples for clarity
prime_examples = [n for n in n_values if isprime(n) and n > 2][:2]
composite_examples = [n for n in n_values if not isprime(n) and n > 2][:2]
examples = prime_examples + composite_examples
for n in examples:
coeffs = self.compute_coefficient_pattern(n)
x_vals = np.arange(len(coeffs))
color = '#2C7BB6' if isprime(n) else '#D7191C'
ax4.plot(x_vals, coeffs, 'o-', color=color,
label=f"n={n} ({'Prime' if isprime(n) else 'Composite'})",
alpha=0.7, markersize=3)
ax4.grid(True, alpha=0.3)
ax4.set_xlabel("Coefficient Index", fontsize=10)
ax4.set_ylabel("Normalized Coefficient Value", fontsize=10)
ax4.set_title("Cyclical Patterns in Minimal Polynomial Coefficients", fontsize=12)
ax4.legend(loc='best', fontsize=8)
ax4.text(0.5, 0.9, "Coefficient patterns differ distinctly\nbetween primes and composites",
transform=ax4.transAxes, ha='center', fontsize=9,
bbox=dict(facecolor='white', alpha=0.9, pad=5, edgecolor='lightgray'))
# Part 5: Dynamic System
ax5 = plt.subplot(gs[2, 1])
factors = []
spectral_props = []
is_prime_list = []
labels = []
for n in n_values:
if n <= 1:
continue
factor_count = self.count_irreducible_factors(n)
spectral_prop = self.compute_spectral_property(n)
factors.append(factor_count)
spectral_props.append(spectral_prop)
is_prime_list.append(isprime(n))
labels.append(str(n))
colors = ['#2C7BB6' if p else '#D7191C' for p in is_prime_list]
sizes = [80 for _ in range(len(factors))]
ax5.scatter(factors, spectral_props, c=colors, s=sizes, alpha=0.8)
for i, txt in enumerate(labels):
if is_prime_list[i] or i % 5 == 0:
ax5.annotate(txt, (factors[i], spectral_props[i]), fontsize=8,
xytext=(5, 5), textcoords='offset points')
ax5.axvline(x=2.5, color='black', linestyle='--', alpha=0.7)
ax5.set_xlabel("Number of Irreducible Factors", fontsize=10)
ax5.set_ylabel("Spectral Property Value", fontsize=10)
ax5.set_title("Dynamical System View of Cyclotomic Criteria", fontsize=12)
ax5.grid(True, alpha=0.3)
legend_elements = [
Patch(facecolor='#2C7BB6', label='Prime'),
Patch(facecolor='#D7191C', label='Composite')
]
ax5.legend(handles=legend_elements, loc='upper right', fontsize=8)
ax5.text(0.05, 0.9, "Phase space clearly separates primes and composites\nbased on their dynamical properties",
transform=ax5.transAxes, ha='left', fontsize=9,
bbox=dict(facecolor='white', alpha=0.9, pad=5, edgecolor='lightgray'))
plt.tight_layout()
plt.savefig(os.path.join(self.output_dir, filename), bbox_inches='tight', dpi=300)
plt.close()
if __name__ == "__main__":
# Set range of integers to analyze
n_values = list(range(50, 130))
# Create visualizer
visualizer = CyclotomicVisualizer(output_dir="figures")
# Generate all visualizations
print("Generating visualizations...")
# Individual plots
visualizer.plot_factorization_patterns(n_values)
visualizer.plot_eigenvalue_distributions(n_values)
visualizer.plot_field_extensions(n_values)
visualizer.plot_coefficient_patterns(n_values)
visualizer.plot_dynamic_system(n_values)
# Combined visualizations
visualizer.create_full_analysis(n_values)
visualizer.create_comprehensive_visualization(n_values)
print("All visualizations generated successfully in the figures directory!")