A matrix is a word
The peel is the subtractive Euclidean algorithm, so it never has to search.
The modular group PSL(2,Z) is the group of 2×2 integer matrices of determinant 1, taken up to overall sign. It acts on the upper half-plane by Möbius transformations z↦cz+daz+b, and the quotient is the modular surface — the object that continued fractions, binary quadratic forms, the Farey tree and the j-invariant are all secretly about.
What makes it a good fit here is that it is a free product, Z/2∗Z/3. A free product has no relations to speak of, so every element is a word, and questions about the group become questions about words — which is combinatorics this catalogue already knows how to count.
The payoff at the end of the page: a conjugacy class in this group is a closed orbit of a flow, that orbit is a knot in the complement of a trefoil, and the knot's linking number with that trefoil is something you can read off the word by counting letters.
The group is generated by
S=(01−10),T=(1011),with S the inversion z↦−1/z and T the translation z↦z+1. In PSL, S has order 2 and U=ST has order 3; those two are the free factors.
But there are three useful alphabets, and they name the same elements:
| alphabet | letters | what a word is |
|---|---|---|
| S, T | the geometric generators | an alternating product Te0STe1S⋯ — the exponents are a continued fraction |
| L, R | (1101), (1011) | a positive word, unique for any matrix with non-negative entries — and it is a path down the Stern–Brocot tree |
| necklace | L,R up to rotation | a conjugacy class — i.e. a closed geodesic |
The peel is the subtractive Euclidean algorithm, so it never has to search.
Why the peel never needs a search is worth a sentence: R−1M subtracts row 2 from row 1 and L−1M subtracts row 1 from row 2, so R comes off when a≥c and b≥d, and L when c≥a and d≥b. Both conditions at once would force a=c and b=d, hence determinant 0 — impossible. There is never a choice to make.
The trace sorts elements into three kinds, and the trichotomy is the whole geometry: elliptic elements (∣tr∣<2) are rotations of finite order, parabolic ones (=2) fix a single point on the boundary, and hyperbolic ones (>2) translate along a geodesic. Only the last kind has a closed geodesic, and a positive LR word is hyperbolic exactly when it uses both letters.
All-L or all-R is parabolic: a word has to turn both ways to close up.
The L/R word of a positive rational is its path down the Stern–Brocot tree, and the runs of that path are its continued fraction — with the last run one short, because the final step is the arrival rather than a turn. Three objects, one description.
π's famous convergent: three rights, seven lefts, fifteen rights.
The Fibonacci fractions alternate perfectly — RLRLR… — which is the sense in which the golden ratio is the "most irrational" number: its path never commits to a direction, so no convergent is ever unusually good.
Consecutive Farey fractions satisfy ps − qr = ±1 — a determinant, i.e. a group element.
Here is the step that turns geometry into counting. Conjugating a positive word by its own first letter rotates it: x−1(xw)x=wx. So a conjugacy class of positive words is precisely a word up to cyclic rotation — a binary necklace.
And a hyperbolic conjugacy class is a closed geodesic on the modular surface. So:
closed geodesics of symbolic length n = binary necklaces of length n, less the two constant ones (which are parabolic, not hyperbolic).
That is a closed form, n1∑d∣nφ(d)2n/d−2, and the primitive geodesics — those not a repeat of a shorter one — are counted by the aperiodic necklaces, the Lyndon words: n1∑d∣nμ(d)2n/d.
Named by the least rotation, which is the canonical name of a necklace.
The word length is the geodesic's symbolic period — how many times it crosses the fundamental domain. Its geometric length is ℓ=2arccosh(∣tr∣/2), so the trace is the real weight, and the two orderings do not agree: LLLR and LRLR have the same word length but traces 5 and 7. The shortest closed geodesic on the modular surface is LR, with trace 3; its fixed point is the golden ratio.
Gauss got to these cycles from a completely different direction, and it is worth seeing that they are the same ones.
A binary quadratic form is ax2+bxy+cy2, carried by its triple (a,b,c), with discriminant D=b2−4ac. The modular group acts on forms by substituting (x,y)↦(px+qy,rx+sy), the discriminant is invariant, and the orbits are the classes — the objects class numbers count.
When D>0 and is not a perfect square the form is indefinite, and the thing that makes it belong here happens: a class does not contain one distinguished reduced form but a whole cycle of them. Step round that cycle with ρ and you are running the continued fraction of the form's root; it closes because that expansion is periodic. So
a class of indefinite forms = a cycle of reduced forms = a periodic continued fraction = a closed geodesic.
The cycle length is always even — ρ flips the sign of the leading coefficient.
The stabiliser of a form is generated by its automorph, built from the fundamental solution of Pell's equation t2−Du2=4:
((t−bu)/2au−cu(t+bu)/2),whose determinant is (t2−Du2)/4=1 and whose trace is t. Since t>2 that matrix is hyperbolic — so the form's class really is one of the closed geodesics above, and it has an LR word like any other.
The automorph fixes the form, and its word is the geodesic the class names.
That the ρ-cycles really are the equivalence classes is the load-bearing claim, and it is checked against an oracle that never mentions ρ: given two forms, a transforming matrix must have a first column (p,r) with f(p,r)=a′, and along the resulting family the middle coefficient moves in steps of exactly 2a′ — so the matrix is solved for rather than searched, and the check is exact.
Now the geometry. The modular flow is the geodesic flow on the unit tangent bundle of the modular surface, and its closed orbits are exactly the hyperbolic conjugacy classes above. The striking fact is what that unit tangent bundle is:
SL(2,Z)\SL(2,R)≅S3∖trefoil.The space the flow lives in is the complement of a trefoil knot in the 3-sphere. So every closed orbit is a closed curve in that complement — a modular knot. Étienne Ghys proved that these are precisely the Lorenz knots, the periodic orbits of the Lorenz attractor, and that their linking number with the missing trefoil has a completely elementary description:
lk(kγ,trefoil)=Ψ(γ)=#R−#L.Count the letters. That is the linking number.
A geodesic that turns equally both ways has linking number zero.
Counting letters is one description of that linking number. The other comes from number theory and looks nothing like it. Rademacher's function is
Φ(acbd)=ca+d−12sign(c)s(d,∣c∣),c=0,where s(h,k)=∑j=1k−1((kj))((khj)) is a Dedekind sum — the same sum that appears in the transformation law of the Dedekind eta function and in the exact formula for the partition numbers. The Rademacher symbol corrects Φ into a class function:
Ψ(M)=Φ(M)−3sign(c(a+d)).Nothing in that formula mentions words. That Ψ equals #R−#L on every hyperbolic element is the theorem, and it is what the package's tests check — one side by brute-force Dedekind sums over the matrix, the other by counting letters in a string.
Φ alone is not conjugation-invariant; only the corrected Ψ is.
The Dedekind sums are computed straight from the definition and then checked against reciprocity,
s(h,k)+s(k,h)=−41+121(kh+hk+hk1),which is an independent enough statement to catch any slip in the sawtooth.
FormClassNumber counts them.