Binary quadratic forms

Binary quadratic forms

ax² + bxy + cy², with whole coefficients. Gauss sorted them into classes under changes of variable, found that the classes of one discriminant form a group, and so opened the road to class groups of number fields.

Forms, discriminants and equivalence

The form f=(a,b,c)f = (a, b, c) has discriminant D=b2−4acD = b^2 - 4ac. A matrix M=(pqrs)M = \begin{pmatrix} p & q\\ r & s\end{pmatrix} of determinant 1 changes variables, f∘M(x,y)=f(px+qy, rx+sy)f \circ M (x, y) = f(px + qy,\ rx + sy), keeping DD and the set of values. Forms related that way are properly equivalent, and represent the same numbers.

import { form, equivalent, reducedForms, classNumber, classGroup, compose, represent, representations } from "@ratmath/numtheory";

const f = form(10, 34, 29);
f.discriminant;                // -4n
const { form: g, matrix } = f.reduce();
String(g);                     // "x² + y²"
matrix;                        // [[-2n, 5n], [1n, -3n]]: f ∘ matrix = g, checked exactly
equivalent([1, 0, 5], [2, 2, 3]);
// { equivalent: false, reason: "they reduce to different forms, x² + 5y² and 2x² + 2xy + 3y²" }

Reduction

A positive definite form (D<0D < 0, a>0a > 0) is reduced when ∣b∣≤a≤c|b| \le a \le c, with b≥0b \ge 0 if either inequality is an equality. Every class contains exactly one reduced form (Lagrange, Gauss), found by alternately swapping a,ca, c and shifting bb by multiples of 2a2a. Since ∣b∣≤a≤c|b| \le a \le c forces 3a2≤∣D∣3a^2 \le |D|, there are finitely many, and counting them gives the class number h(D)h(D).

reducedForms(-23).map(String);   // ["x² + xy + 6y²", "2x² − xy + 3y²", "2x² + xy + 3y²"]
classNumber(-23);                // 3n
classNumber(-163);               // 1n: the largest of Heegner's nine

For D>0D > 0 the form is indefinite and a class contains several reduced forms, arranged in a cycle by Gauss's ρ\rho operator (a,b,c)↦(c,−b+2ck,… )(a, b, c) \mapsto (c, -b + 2ck, \dots); the class is identified by its cycle. Then hh is the narrow class number, which counts ff and −f-f separately when no unit of norm −1-1 links them.

form(1, 0, -14).cycle().map(String);   // ["x² + 6xy − 5y²", "−5x² + 4xy + 2y²", "2x² + 4xy − 5y²", "−5x² + 6xy + y²"]
classNumber(12);                       // 2n: x² − 3y² and its negative are different classes

The class group

Gauss defined a composition of primitive forms of the same discriminant, under which the classes form a finite abelian group: the identity is the class of the principal form (1, D mod 2, ((D mod 2)−D)/4)\bigl(1,\ D \bmod 2,\ ((D \bmod 2) - D)/4\bigr), the inverse of (a,b,c)(a, b, c) is (a,−b,c)(a, -b, c). Composition here follows Shanks's formulas (Cohen's Algorithm 5.4.7) and reduces the result. classGroup also finds the structure as a product of cyclic groups.

const G = classGroup(-84);
G.h;                         // 4n
G.forms.map(String);         // ["x² + 21y²", "2x² + 2xy + 11y²", "3x² + 7y²", "5x² + 4xy + 5y²"]
G.structure;                 // [2n, 2n]: every class has order 2, Euler's idoneal numbers
classGroup(-56).structure;   // [4n]: cyclic
classGroup(-3299).structure; // [3n, 9n]
String(compose([2, 1, 3], [2, 1, 3]));   // "2x² − xy + 3y²": in h(−23) = 3, the square is the inverse

The class numbers are checked in the tests against Dirichlet's class number formula h(D)=w2∣D∣∑a=1∣D∣−(Da)ah(D) = \frac{w}{2|D|}\sum_{a=1}^{|D|} -\left(\frac Da\right) a for every fundamental DD down to −1500.

Representations

A positive definite form takes each value finitely often: from 4af=(2ax+by)2+∣D∣y24af = (2ax + by)^2 + |D|y^2, ∣y∣≤4an/∣D∣|y| \le \sqrt{4an/|D|}. For any DD, ff properly represents nn exactly when ff is equivalent to some (n,b,⋅)(n, b, \cdot) with b2≡D(mod4n)b^2 \equiv D \pmod{4n}, and the first column of the equivalence matrix is then the representation.

representations([1, 1, 6], 8);   // [[-2n, 1n], [-1n, -1n], [1n, 1n], [2n, -1n]]
represent([1, 0, -2], 7);        // [-3n, 1n]: 9 − 2 = 7
represent([1, 0, 5], 9);         // [2n, -1n]: 4 + 5 = 9
Try itEdit the code and run it with @ratmath/numtheory and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.
Ctrl/⌘ + Enter runs