Lenstra-Lenstra-Lovász lattice basis reduction of a Gram matrix (Scoccola, Gakhar, Bush, Schonsheck, Rask, Zhou, Perea, "Toroidal Coordinates: Decorrelating Circular Coordinates With Lattice Reduction", arXiv:2212.07201, Algorithm 4). Given the Gram matrix G of k independent vectors, factor G = C Cᵀ (Cholesky), run LLL on the rows of C, and return the unimodular integer change of basis: the shortest, most nearly orthogonal basis of the same lattice. CircularCoordinates.computeToroidal uses it to choose among the equally valid bases of a group of H¹ classes.
The Gram-Schmidt step projects onto the orthogonalized vectors, as in the textbook algorithm. DREiMac's _gram_schmidt projects onto the original ones, which agrees for k = 2 but not for k >= 3, so results can differ from DREiMac's there.
case class Result(basisChange: Array[Array[Int]], reducedGram: Array[Array[Double]])
Value parameters
basisChange
U, k x k, integer, unimodular (|det U| = 1, checked internally -- a failure here indicates a bug in reduce itself, not a caller error). Column c gives the coefficients of the c-th REDUCED generator as a combination of the ORIGINAL generators: reduced_c = sum_r U(r)(c) * original_r. Apply the SAME U to any other linear stand-in for the original generators -- the harmonic cocycles themselves, or the circular coordinates they produce -- to get the reduced version of that thing (both are linear in the choice of generator).
reducedGram
U^T G U: the reduced generators' own Gram matrix. Diagonal entries are the reduced generators' own squared norms, off-diagonal their pairwise correlation under the inner product G was built from -- smaller/more-diagonal than G is the whole point of reduction, and is what a caller checks to confirm it actually helped on real data.
def reduce(gram: Array[Array[Double]], delta: Double = ...): Result
Value parameters
delta
the Lovász condition parameter, required in (1/4, 1] for LLL's own termination guarantee. 3/4 is the universal textbook default (also DREiMac's own default; the paper itself does not pin a specific value) and this library's own default; smaller trades reduction quality for speed, 1.0 is the strongest (slowest) guarantee.
gram
k x k, symmetric (checked up to a loose relative tolerance, then symmetrized by averaging -- absorbs ordinary floating-point noise from however a caller accumulated it, e.g. summing many small terms in two different orders for (i,j) vs (j,i)) and positive DEFINITE. Positive-semi-definite (linearly DEPENDENT generators -- e.g. the same cohomology class selected twice) is a caller error, reported as an IllegalArgumentException naming the failing pivot rather than a cryptic NaN from a failed Cholesky step.