MathematicaForPrediction at WordPress​
​Wolfram Repository resources

Introduction

In this notebook we show how to make neat chord plots of primitive roots generation sequences. Primitive roots a generators of cyclic multiplicative integer groups modulo
n
. See the built-in Wolfram Language functions PrimitiveRoot and PrimitiveRootList. We follow the ideas presented in “Modular Arithmetic Visualizations” by Peter Karpov.
Remark: The basis representation section follows "Re-exploring the structure of Chinese character images", [AAn1]; the movie exporting section follows “Rorschach mask animations projected over 3D surfaces”, [AAn2].
Remark: The motivation for finding and making nice primary root trails came from on working on Number theory neat examples discussed in [AAv1, AAv2].

Procedure outline

1
.
Try to figure out neat examples to visualize primitive roots.
1
.
1
.
Browse Wolfram Demonstrations.
1
.
2
.
Search World Wide Web.
2
.
Program a few versions of circle chords based visualization routines.
2
.
1
.
Called chord trail plots below.
3
.
Marvel at chord trail plots for larger moduli.
3
.
1
.
Make multiple collections of them.
3
.
2
.
Look into number of primitive roots distributions.
4
.
Consider making animations of the collections.
4
.
1
.
The animations should not be “chaotic” -- they should have some inherent visual flow in them.
5
.
Consider different ways of sorting chord trail plots.
5
.
1
.
Using number theoretic arguments.
5
.
1
.
1
.
Yeah, would be nice, but requires too much head scratching and LLM-ing.
5
.
2
.
Convert plots to images and sort them.
5
.
2
.
1
.
Some might say that that is a “brute force” application.
5
.
2
.
2
.
Simple image sort does not work.
6
.
Latent Semantic Analysis (LSA) application.
6
.
1
.
After failing to sort the chord trail image collections by “simple” means, the idea applying LSA came to mind.
6
.
2
.
LSA being, of course, a favorite technique that was applied to sorting images multiple times in the past, in different contexts, [AAn1, AAn3, AAn4, AAn5, AAv3].
6
.
3
.
Also, having a nice (monadic) paclet for doing LSA, [AAp1], helps a lot.
7
.
Make the animations and marvel at them.
8
.
Export the chord trail plots animations for different moduli to movies and GIFs and upload them.
9
.
Make a blog post (notebook).

Chord plot

It is fairly easy to program a chord plot using Graph:
In[]:=
(*Modulusandprimiviteroot*)​​n=509;r=128;​​(*Coordinatesofthechordsplot*)​​coords=AssociationThread[Range[n],Table[{Cos[2πk/(n-1)+π/2],Sin[2πk/(n-1)+π/2]},{k,0,n-1}]];​​(*Graphedges*)​​edges=UndirectedEdge@@@Partition[PowerMod[r,#,n]&/@Range[n],2,1];​​(*Graph*)​​Graph[edges,VertexCoordinates->coords,VertexSize->0,EdgeStyle->AbsoluteThickness[0.6]]
Out[]=
We make the function ChordTrailsGraph (see Section “Setup” below) encapsulating the code above. Here is an example:
In[]:=
ChordTrailsGraph509,47,

Out[]=
Instead of using Graph we can just a Graphics plot -- again see the definition in “Setup”. Here is an example:
In[]:=
ChordTrails[509,75,"Color"->Automatic]
Out[]=
Note that the modular inverse is going to produce the same chord trails plot:
In[]:=
Row[{​​ChordTrails[257,3,ImageSize->300],​​ChordTrails[257,ModularInverse[3,257],ImageSize->300]​​}]
Out[]=

Making collections of plots

Here w pick a large enough modulus, we find the primitive roots, and keep only primitive roots that will produce unique chord trail plots:
In[]:=
n=509;​​rs=PrimitiveRootList[n];​​Length[rs]​​urs=Select[rs,#≤ModularInverse[#,n]&];​​urs//Length
Out[]=
252
Out[]=
126
Here is the collection using Graph:
In[]:=
AbsoluteTiming[​​gs1=Association@Map[#->ChordTrailsGraph[n,#,EdgeStyle->{AbsoluteThickness[0.8]},VertexSize->0,VertexStyle->EdgeForm[None],EdgeStyle->
,ImageSize->300]&,urs];​​]
Out[]=
{0.771692,Null}
Here is a sample of plots from the collection:
Here is the collection using Graphics:
Here is a sample of plots from the collection (same indexes as above):
Remark: It looks like that using Graph is faster and produces (admittedly, with tweaking options) better looking plots.
Since we want to make an animation of chord-trail plots, we convert the collection of plots into a collection of images:

Generalization

The function ChordTrails can be generalized to take a (pre-computed) chords argument. Here is an example of chords plot that connects integers that are modular inverses of each other:
More details on using chord plots for modular inverses are given "Number theory neat examples in Raku (Set 2)".

LSAMon application

In order to sort the plots we find dimension reduction basis representation of the corresponding images and sort using that representation. For more details see “Re-exploring the structure of Chinese character images”, [AAn1].
In case you want to see the basis (we show just a sample):

Animate sorted

Here we make the animation of sorted chord trail plots:
Playing the link to an uploaded movie:

Export

Remark: The code below follows "Rorschach mask animations projected over 3D surfaces".
Remark: The animations are exported in the subdirectory “AnimatedGIFs”.

Export to MP4 (white background)

Export to GIF (black background)

Optionally, open the animations directory:

References

Articles, posts

[PK1] Peter Karpov, "Modular Arithmetic Visualizations", (2016), Inversed.ru.

Notebooks

[AAn1] Anton Antonov, “Re-exploring the structure of Chinese character images”, (2022), Wolfram Community.
[AAn2] Anton Antonov, “Rorschach mask animations projected over 3D surfaces”, (2022), Wolfram Community.
[AAn3] Anton Antonov, "Handwritten Arabic characters classifiers comparison", (2022), Wolfram Community.
[AAn4] Anton Antonov, "LSA methods comparison over random mandalas deconstruction -- WL", (2022), Wolfram Community.
[AAn5] Anton Antonov, "LSA methods comparison over random mandalas deconstruction -- Python", (2022), Wolfram Community.

Paclets

[AAp1] Anton Antonov, "MonadicLatentSemanticAnalysis", (2023), Wolfram Language Paclet Repository.

Videos

[AAv1] Anton Antonov, "Number theory neat examples in Raku (Set 1)", (2025), YouTube/@AAA4prediction.
[AAv2] Anton Antonov, "Number theory neat examples in Raku (Set 2)”, (2025), YouTube/@AAA4prediction.
[AAv3] Anton Antonov, "Random Mandalas Deconstruction in R, Python, and Mathematica (Greater Boston useR Meetup, Feb 2022)", (2022), YouTube/@AAA4prediction.

CITE THIS NOTEBOOK

Primitive roots generation trails​
by Anton Antonov​
Wolfram Community, STAFF PICKS, April 9, 2025
​https://community.wolfram.com/groups/-/m/t/3442027