Notes on binary quadratic forms, revisited

Back in the early 2000s, a student of mine was writing a thesis on binary quadratic forms. To force myself to learn enough about the subject to hold up my end of our thesis discussions, I wrote a set of elementary expository notes in 2004 (originally using SageMath, now updated using pure Python) mapping out the computational dictionary between indefinite integral binary quadratic forms f(x,y) = ax^2 + bxy + cy^2, ideals in real quadratic fields, and simple continued fractions.

Over twenty years later, I have returned to those notes with the help of AI. With the help of ChatGPT (mostly, but also with the help of Gemini and Claude), I’ve completed a corrected, substantially expanded rewrite: Notes on indefinite integral binary quadratic forms with Python. For more details on the AI usage, please see the Acknowledgment section at the end of the paper. The LaTeX source, PDF, and companion standard-library Python scripts are all available on GitHub at wdjoyner/alg-graph-thry/tree/main/quad-forms.

The original goal was to clarify the classical three-way correspondence for positive nonsquare discriminants D \equiv 1 \pmod 4—paying careful attention to leading-coefficient signs, orientation, and the distinction between the ordinary class group C(R) and the narrow class group C^+(R). In this new edition, all computations have been rewritten in pure standard-library Python (no external computer algebra system required), and a fourth, combinatorial viewpoint has been added: finite directed reduction graphs.

For a fixed discriminant D, the primitive reduced forms serve as the vertices of a finite directed graph G_D, with edges f \to \rho(f) given by the right-neighbor reduction step. Every component is a directed cycle corresponding to a narrow ideal class. Using D = 1353 as a running example, exact enumeration produces 64 reduced forms partitioned into four directed cycles of lengths 12, 12, 20, 20, giving C^+(R) \cong C_2 \times C_2 and C(R) \cong C_2. Assigning logarithmic edge weights \log x_{i+1} in the continued-fraction direction \rho^{-1} turns the principal cycle into a metric circle whose circumference recovers the positive-norm regulator \log \epsilon_+.

Exploring G_D through algebraic graph theory led to several new sections and results:

  • Equivariant twisted traces and genus theory: Let P_D be the permutation matrix of the reduction map \rho on G_D. While the ordinary permutation spectrum of P_D only records cycle lengths, twisting P_D by the sign-change involution \tau(a,b,c) = (-a,b,-c) and the reflection r(a,b,c) = (c,b,a) yields exact trace formulas. In particular, \rm{tr}(\Sigma_D P_D) = 2n_2 counts the 2-torsion of C^+(R), while \rm{tr}(\Theta_D \Sigma_D) = 2n_U counts classes squaring to U = [\sqrt{D}R]—which we show equals twice the number of primitive representations D = b^2 + 4a^2 with a,b > 0. For fundamental discriminants, both counts recover classical genus theory.
  • Isospectral reduction graphs: At D = 165 and D = 221, the unweighted directed reduction graphs are isomorphic—both have cycle lengths 2, 2, 4, 4 and characteristic polynomial (t^2 - 1)^2(t^4 - 1)^2—yet their narrow class groups are C_2 \times C_2 and C_4, respectively. The twisted traces immediately tell them apart.
  • Harmonic morphisms between nested orders: The “nested orders” in the title is shorthand for morphisms between the reduction graphs associated with nested orders. Specifically, an order R_m is associated with the finite directed graph G_{m^2 D_0} whose vertices are the primitive reduced quadratic forms of discriminant m^2 D_0—or equivalently, via the form–ideal dictionary, the oriented reduced invertible fractional R_m-ideals—with directed edges f \to \rho(f) given by the right-neighbor reduction step. Its connected components are finite directed cycles in bijection with the narrow class group C^+(R_m).Comparing the order of conductor 3 (D = 153, two 8-cycles) with the maximal order (D = 17, one 6-cycle) shows that ideal extension need not lift to a successor-preserving map of directed reduction graphs, since 6 \nmid 8. However, passing to the weighted metric circles and subdividing yields a degree-2 covering—and hence a harmonic morphism of graphs.
  • Further directions: The paper concludes with open questions on character-twisted dynamical zeta functions Z_D(u, \chi), entropy and moment statistics of logarithmic edge weights, and 4-rank invariants.

Everything can be reproduced out of the box by cloning the repository and running python3 verify_quadratic_forms.py and python3 research_quadratic_forms.py in the quad-forms/scripts directory. Feedback, comments, and pointers to related literature are very welcome!