Grace/Cumulus/NumberTheory/LatticeCount.g17
Imports
Grace.Cumulus.NumberTheory.GaussParity · Grace.Cumulus.Cardinal
Declarations
- lowerLattice def
- upperLattice def
- lowerLattice.mem_iff theorem derivation
- upperLattice.mem_iff theorem derivation
- lowerLattice.subset theorem derivation
- upperLattice.subset theorem derivation
- lattice.off_line theorem derivation
- lattice.partition theorem derivation
- floorWeights.le_half theorem derivation
- latticeShift defgraph
- latticeShift.pair theorem derivation
- latticeShift.bijective theorem derivation
- lowerLattice.count theorem derivation
- upperLattice.count theorem derivation
- lattice.count theorem derivation
Source
1-- Eisenstein's lattice count: for distinct odd primes p = 2hp + 1 and2-- q = 2hq + 1, the floor sums Σ ⌊q k / p⌋ (k = 1..hp) and Σ ⌊p j / q⌋3-- (j = 1..hq) count the lattice points of the hp × hq rectangle below and4-- above the line p j = q k, which has no lattice point on it; together they5-- count hp · hq.6-- Part of Cumulus, the Grace17 mathematics library.7-- Copyright (c) 2026 Grant Passmore.8-- Released under the Apache License 2.0. See LICENSE in the repository root.9-- Authors: Grant Passmore1011module Grace.Cumulus.NumberTheory.LatticeCount12import Grace.Cumulus.NumberTheory.GaussParity13import Grace.Cumulus.Cardinal1415-- The lattice points below and above the line1617-- The points (k, j) of 1..hp × 1..hq with p j < q k, and those with18-- q k < p j.19def lowerLattice (p q hp hq : Nat) : Set :=20 { r ∈ halfRange(hp) × halfRange(hq) | mul(p, snd(r)) < mul(q, fst(r)) }2122def upperLattice (p q hp hq : Nat) : Set :=23 { r ∈ halfRange(hp) × halfRange(hq) | mul(q, fst(r)) < mul(p, snd(r)) }2425theorem lowerLattice.mem_iff (p q hp hq k j : Nat)26 shows (k, j) ∈ lowerLattice(p, q, hp, hq) ↔27 k ∈ halfRange(hp) ∧ j ∈ halfRange(hq) ∧ p * j < q * k28proof29 constructor30 . intro member31 obtain ⟨inProduct, below⟩ := lowerLattice.membership.forward with member32 have components := (product.pair_membership at k, j, halfRange(hp),33 halfRange(hq)).forward with inProduct34 rw [fst.pair, snd.pair] at below35 exact ⟨components.left, components.right, below⟩36 . intro all37 obtain ⟨hk, hj, below⟩ := all38 have inProduct := product.pair with hk, hj39 have shaped : mul(p, snd((k, j))) < mul(q, fst((k, j)))40 rw [fst.pair, snd.pair]41 exact below42 exact lowerLattice.membership.backward with ⟨inProduct, shaped⟩4344theorem upperLattice.mem_iff (p q hp hq k j : Nat)45 shows (k, j) ∈ upperLattice(p, q, hp, hq) ↔46 k ∈ halfRange(hp) ∧ j ∈ halfRange(hq) ∧ q * k < p * j47proof48 constructor49 . intro member50 obtain ⟨inProduct, below⟩ := upperLattice.membership.forward with member51 have components := (product.pair_membership at k, j, halfRange(hp),52 halfRange(hq)).forward with inProduct53 rw [fst.pair, snd.pair] at below54 exact ⟨components.left, components.right, below⟩55 . intro all56 obtain ⟨hk, hj, below⟩ := all57 have inProduct := product.pair with hk, hj58 have shaped : mul(q, fst((k, j))) < mul(p, snd((k, j)))59 rw [fst.pair, snd.pair]60 exact below61 exact upperLattice.membership.backward with ⟨inProduct, shaped⟩6263theorem lowerLattice.subset (p q hp hq : Nat)64 shows lowerLattice(p, q, hp, hq) ⊆ halfRange(hp) × halfRange(hq)65proof66 intros r member67 have parts := lowerLattice.membership.forward with member68 exact parts.left6970theorem upperLattice.subset (p q hp hq : Nat)71 shows upperLattice(p, q, hp, hq) ⊆ halfRange(hp) × halfRange(hq)72proof73 intros r member74 have parts := upperLattice.membership.forward with member75 exact parts.left7677-- The line misses the lattice, and the two sides partition it7879-- No lattice point of the rectangle lies on the line: p j = q k gives80-- p ∣ k with 0 < k ≤ hp < p.81theorem lattice.off_line (p q hp hq k j : Nat)82 requires primeP : Prime(p)83 requires primeQ : Prime(q)84 requires distinct : p ≠ q85 requires oddP : p = 2 * hp + 186 requires oddQ : q = 2 * hq + 187 requires kIn : k ∈ halfRange(hp)88 requires jIn : j ∈ halfRange(hq)89 shows p * j ≠ q * k90proof91 intro equal92 obtain ⟨kPositive, kBound⟩ := (halfRange.mem_iff at hp, k).forward with kIn93 have divides : p ∣ q * k94 rw ← equal95 exact divides.mul_left_self at p, j96 have split := Prime.divides_mul with primeP, divides97 cases split with dividesQ | dividesK98 . have same := Prime.divides_prime with primeP, primeQ, dividesQ99 exact distinct with same100 . have kNonzero : k ≠ 0 by arith101 have le := divides.le with dividesK, kNonzero102 have impossible : ¬p ≤ k by arith103 exact impossible with le104105theorem lattice.partition (p q hp hq : Nat)106 requires primeP : Prime(p)107 requires primeQ : Prime(q)108 requires distinct : p ≠ q109 requires oddP : p = 2 * hp + 1110 requires oddQ : q = 2 * hq + 1111 shows lowerLattice(p, q, hp, hq) ∪ upperLattice(p, q, hp, hq) =112 halfRange(hp) × halfRange(hq) ∧113 Disjoint(lowerLattice(p, q, hp, hq), upperLattice(p, q, hp, hq))114proof115 constructor116 . ext r117 constructor118 . intro member119 have split := union2.membership.forward with member120 cases split with inLower | inUpper121 . exact lowerLattice.subset at p, q, hp, hq with r, inLower122 . exact upperLattice.subset at p, q, hp, hq with r, inUpper123 . intro member124 obtain ⟨bound, k, hk, j, hj, shape⟩ := product.membership.forward125 with member126 have kNat := halfRange.member_nat with hk127 have jNat := halfRange.member_nat with hj128 have offLine := lattice.off_line with primeP, primeQ, distinct, oddP,129 oddQ, hk, hj130 have trichotomy := Less.trichotomy at p * j, q * k with *131 apply union2.membership.backward132 rw shape133 rcases trichotomy with below | same | above134 . left135 exact (lowerLattice.mem_iff at p, q, hp, hq, k, j).backward136 with ⟨hk, hj, below⟩137 . exfalso138 exact offLine with same139 . right140 exact (upperLattice.mem_iff at p, q, hp, hq, k, j).backward141 with ⟨hk, hj, above⟩142 . apply (Disjoint.iff at lowerLattice(p, q, hp, hq),143 upperLattice(p, q, hp, hq)).backward144 intros r inLower inUpper145 have inProduct := lowerLattice.subset at p, q, hp, hq with r, inLower146 obtain ⟨bound, k, hk, j, hj, shape⟩ := product.membership.forward147 with inProduct148 have kNat := halfRange.member_nat with hk149 have jNat := halfRange.member_nat with hj150 rw shape at inLower151 rw shape at inUpper152 have lowerAll := (lowerLattice.mem_iff at p, q, hp, hq, k, j).forward153 with inLower154 have upperAll := (upperLattice.mem_iff at p, q, hp, hq, k, j).forward155 with inUpper156 have below := lowerAll.right.right157 have above := upperAll.right.right158 have impossible : ¬q * k < p * j by arith159 exact impossible with above160161-- Floors162163-- For 0 < k ≤ hp the floor ⌊q k / p⌋ is at most hq: q k / p < q / 2.164theorem floorWeights.le_half (p q hp hq k : Nat)165 requires primeP : Prime(p)166 requires oddP : p = 2 * hp + 1167 requires oddQ : q = 2 * hq + 1168 requires kIn : k ∈ halfRange(hp)169 shows quotientNat(q * k, p) ≤ hq170proof171 have nonzero := Prime.nonzero with primeP172 obtain ⟨kPositive, kBound⟩ := (halfRange.mem_iff at hp, k).forward with kIn173 have cases := Less.or_LessEqual at hq, quotientNat(q * k, p)174 cases cases with above | small175 . exfalso176 have step : hq + 1 ≤ quotientNat(q * k, p) by arith177 have scaled := (quotientNat.le_iff at p, q * k, hq + 1 with nonzero).forward178 with step179 have bounded := LessEqual.mul_left at k, hp, q with kBound180 have expandP : p * (hq + 1) = 2 * (hp * hq) + 2 * hp + hq + 1181 rw oddP182 ring183 have expandQ : q * hp = 2 * (hp * hq) + hp184 rw oddQ185 ring186 rw expandP at scaled187 rw expandQ at bounded188 arith189 . exact small190191-- The floor sum counts the lower points192193-- Shift the weighted pairs (k, i), i < ⌊q k / p⌋, to the lattice points194-- (k, i + 1).195defgraph latticeShift (p q hp hq : Nat)196 (r ∈ weightedPairs(halfRange(hp), floorWeights(q, p, hp))) :197 lowerLattice(p, q, hp, hq) := (fst(r), succ(snd(r)))198requires primeP : Prime(p)199requires primeQ : Prime(q)200requires distinct : p ≠ q201requires oddP : p = 2 * hp + 1202requires oddQ : q = 2 * hq + 1203proof204 have parts := weightedPairs.membership.forward with member205 obtain ⟨k, hk, i, hi, shape⟩ := parts.right206 rw [shape, fst.pair, snd.pair]207 have kNat := halfRange.member_nat with hk208 rw floorWeights.value with hk at hi209 have iNat := natural.member at quotientNat(q * k, p) with hi210 have iBelow := Less.of_membership at i, quotientNat(q * k, p) with hi211 have step : succ(i) ≤ quotientNat(q * k, p) by arith212 have nonzero := Prime.nonzero with primeP213 have weak := (quotientNat.le_iff at p, q * k, succ(i) with nonzero).forward214 with step215 have half := floorWeights.le_half with primeP, oddP, oddQ, hk216 have jPositive : 0 < succ(i) by arith217 have jBound : succ(i) ≤ hq by arith218 have hj := (halfRange.mem_iff at hq, succ(i)).backward219 with ⟨jPositive, jBound⟩220 have offLine := lattice.off_line with primeP, primeQ, distinct, oddP, oddQ,221 hk, hj222 have strict : p * succ(i) < q * k by arith223 exact (lowerLattice.mem_iff at p, q, hp, hq, k, succ(i)).backward224 with ⟨hk, hj, strict⟩225226theorem latticeShift.pair (p q hp hq k i : Nat)227 requires primeP : Prime(p)228 requires primeQ : Prime(q)229 requires distinct : p ≠ q230 requires oddP : p = 2 * hp + 1231 requires oddQ : q = 2 * hq + 1232 requires member :233 (k, i) ∈ weightedPairs(halfRange(hp), floorWeights(q, p, hp))234 shows latticeShift(p, q, hp, hq)((k, i)) = (k, succ(i))235proof236 have value := latticeShift.value with primeP, primeQ, distinct, oddP, oddQ,237 member238 rw [value, fst.pair, snd.pair]239 rfl240241theorem latticeShift.bijective (p q hp hq : Nat)242 requires primeP : Prime(p)243 requires primeQ : Prime(q)244 requires distinct : p ≠ q245 requires oddP : p = 2 * hp + 1246 requires oddQ : q = 2 * hq + 1247 shows BijectiveBetween(latticeShift(p, q, hp, hq),248 weightedPairs(halfRange(hp), floorWeights(q, p, hp)),249 lowerLattice(p, q, hp, hq))250proof251 have nonzero := Prime.nonzero with primeP252 have function := latticeShift.function with primeP, primeQ, distinct, oddP,253 oddQ254 constructor255 . constructor256 . exact function257 . intros x hx y hy z hz hxz hyz258 have xValue := FunctionBetween.apply_value with function, hx, hxz259 have yValue := FunctionBetween.apply_value with function, hy, hyz260 have xParts := weightedPairs.membership.forward with hx261 obtain ⟨k, hk, i, hi, xShape⟩ := xParts.right262 have yParts := weightedPairs.membership.forward with hy263 obtain ⟨l, hl, m, hm, yShape⟩ := yParts.right264 have kNat := halfRange.member_nat with hk265 have lNat := halfRange.member_nat with hl266 rw floorWeights.value with hk at hi267 rw floorWeights.value with hl at hm268 have iNat := natural.member at quotientNat(q * k, p) with hi269 have mNat := natural.member at quotientNat(q * l, p) with hm270 rw xShape at hx271 rw yShape at hy272 have xPair := latticeShift.pair with primeP, primeQ, distinct, oddP,273 oddQ, hx274 have yPair := latticeShift.pair with primeP, primeQ, distinct, oddP,275 oddQ, hy276 rw [xShape, xPair] at xValue277 rw [yShape, yPair] at yValue278 have same : (k, succ(i)) = (l, succ(m))279 rw [xValue, yValue]280 rfl281 have coordinates := orderedPair.injective with same282 have seconds := Naturals.injective.succ with coordinates.right283 rw [xShape, yShape, coordinates.left, seconds]284 rfl285 . constructor286 . exact function287 . intros z hz288 have inProduct := lowerLattice.subset at p, q, hp, hq with z, hz289 obtain ⟨bound, k, hk, j, hj, shape⟩ := product.membership.forward290 with inProduct291 rw shape at hz292 have kNat := halfRange.member_nat with hk293 have jNat := halfRange.member_nat with hj294 obtain ⟨hkAgain, hjAgain, below⟩ :=295 (lowerLattice.mem_iff at p, q, hp, hq, k, j).forward with hz296 obtain ⟨jPositive, jBound⟩ := (halfRange.mem_iff at hq, j).forward with hj297 have form := natural.zero_or_successor at j298 cases form with zero | successor299 . exfalso300 rw zero at jPositive301 exact Less.irreflexive at 0 with *, jPositive302 . obtain ⟨i, iNat, jForm⟩ := successor303 have weak : p * succ(i) ≤ q * k304 rw ← jForm305 arith306 have characterization := quotientNat.le_iff at p, q * k, succ(i)307 with nonzero308 have step := characterization.backward with weak309 have iBelow : i < quotientNat(q * k, p) by arith310 have iIn := membership.of_Less at i, quotientNat(q * k, p) with *, *,311 iBelow312 have weight : i ∈ floorWeights(q, p, hp)(k)313 rw floorWeights.value with hk314 exact iIn315 have iNatural : i ∈ ℕ by exact nat.omega.forward with iNat316 have pairIn := product.pair with hk, iNatural317 have same : (k, i) = (k, i) by rfl318 have memberX := weightedPairs.membership.backward319 with ⟨pairIn, k, hk, i, weight, same⟩320 have edge := FunctionBetween.relates_apply with function, memberX321 have value := latticeShift.pair with primeP, primeQ, distinct, oddP,322 oddQ, memberX323 rw [value, ← jForm] at edge324 refine ⟨(k, i), memberX, _⟩325 rw shape326 exact edge327328-- j ≤ ⌊q k / p⌋ exactly when p j ≤ q k, and off the line p j < q k; so329-- the weighted pairs of the floor sum are the lower lattice points,330-- after shifting i < ⌊q k / p⌋ to j = i + 1.331theorem lowerLattice.count (p q hp hq : Nat)332 requires primeP : Prime(p)333 requires primeQ : Prime(q)334 requires distinct : p ≠ q335 requires oddP : p = 2 * hp + 1336 requires oddQ : q = 2 * hq + 1337 shows natSum(halfRange(hp), floorWeights(q, p, hp)) =338 cardinality(lowerLattice(p, q, hp, hq))339proof340 have finite := weightedPairs.finite with (halfRange.finite at hp),341 (halfRange.cardinality at hp), (floorWeights.natural at q, p, hp)342 have bijective := latticeShift.bijective with primeP, primeQ, distinct,343 oddP, oddQ344 have same := cardinality.bijective with finite, bijective345 unfold natSum346 exact same347348-- The counts349350-- The upper points are the lower points of the roles exchanged, under the351-- swap (k, j) ↦ (j, k).352theorem upperLattice.count (p q hp hq : Nat)353 requires primeP : Prime(p)354 requires primeQ : Prime(q)355 requires distinct : p ≠ q356 requires oddP : p = 2 * hp + 1357 requires oddQ : q = 2 * hq + 1358 shows natSum(halfRange(hq), floorWeights(p, q, hq)) =359 cardinality(upperLattice(p, q, hp, hq))360proof361 have distinctQ : q ≠ p362 intro same363 apply distinct364 symm365 exact same366 have lower := lowerLattice.count at q, p, hq, hp with primeQ, primeP,367 distinctQ, oddQ, oddP368 rw lower369 have swapBijective := productSwap.bijective at halfRange(hq), halfRange(hp)370 have injective := swapBijective.left371 have subset := lowerLattice.subset at q, p, hq, hp372 have finiteProduct := Finite.product with (halfRange.finite at hq),373 (halfRange.finite at hp)374 have finite := Finite.subset with finiteProduct, subset375 have count := cardinality.injective_image with injective, subset, finite376 have image : functionImage(productSwap(halfRange(hq), halfRange(hp)),377 lowerLattice(q, p, hq, hp), halfRange(hp) × halfRange(hq)) =378 upperLattice(p, q, hp, hq)379 ext z380 constructor381 . intro member382 obtain ⟨inProduct, r, hr, value⟩ := functionImage.membership.forward383 with member384 have rProduct := subset with r, hr385 obtain ⟨bound, j, hj, k, hk, shape⟩ := product.membership.forward386 with rProduct387 rw shape at hr388 have jNat := halfRange.member_nat with hj389 have kNat := halfRange.member_nat with hk390 obtain ⟨hjAgain, hkAgain, below⟩ :=391 (lowerLattice.mem_iff at q, p, hq, hp, j, k).forward with hr392 have swapped := productSwap.pair with hj, hk393 rw [shape, swapped] at value394 rw value395 exact (upperLattice.mem_iff at p, q, hp, hq, k, j).backward396 with ⟨hk, hj, below⟩397 . intro member398 have inProduct := upperLattice.subset at p, q, hp, hq with z, member399 obtain ⟨bound, k, hk, j, hj, shape⟩ := product.membership.forward400 with inProduct401 rw shape at member402 have kNat := halfRange.member_nat with hk403 have jNat := halfRange.member_nat with hj404 obtain ⟨hkAgain, hjAgain, above⟩ :=405 (upperLattice.mem_iff at p, q, hp, hq, k, j).forward with member406 have inLower := (lowerLattice.mem_iff at q, p, hq, hp, j, k).backward407 with ⟨hj, hk, above⟩408 have swapped := productSwap.pair with hj, hk409 have equal : (k, j) = productSwap(halfRange(hq), halfRange(hp))((j, k))410 rw swapped411 rfl412 have pairIn := product.pair with hk, hj413 have both : (k, j) ∈ halfRange(hp) × halfRange(hq) ∧414 (∃ x ∈ lowerLattice(q, p, hq, hp),415 (k, j) = productSwap(halfRange(hq), halfRange(hp))(x))416 constructor417 . exact pairIn418 . refine ⟨(j, k), inLower, equal⟩419 rw shape420 exact functionImage.membership.backward with both421 rw image at count422 symm423 exact count424425theorem lattice.count (p q hp hq : Nat)426 requires primeP : Prime(p)427 requires primeQ : Prime(q)428 requires distinct : p ≠ q429 requires oddP : p = 2 * hp + 1430 requires oddQ : q = 2 * hq + 1431 shows add(natSum(halfRange(hp), floorWeights(q, p, hp)),432 natSum(halfRange(hq), floorWeights(p, q, hq))) = hp * hq433proof434 have lower := lowerLattice.count with primeP, primeQ, distinct, oddP, oddQ435 have upper := upperLattice.count with primeP, primeQ, distinct, oddP, oddQ436 rw [lower, upper]437 obtain ⟨cover, disjoint⟩ :=438 lattice.partition with primeP, primeQ, distinct, oddP, oddQ439 have finiteProduct := Finite.product with (halfRange.finite at hp),440 (halfRange.finite at hq)441 have lowerFinite := Finite.subset with finiteProduct,442 (lowerLattice.subset at p, q, hp, hq)443 have upperFinite := Finite.subset with finiteProduct,444 (upperLattice.subset at p, q, hp, hq)445 have union := cardinality.union_disjoint with lowerFinite, upperFinite,446 disjoint447 rw cover at union448 rw ← union449 have countP := cardinality.correct with (halfRange.finite at hp)450 have countQ := cardinality.correct with (halfRange.finite at hq)451 have product := HasCardinality.product with countP, countQ452 have value := cardinality.eq_of_has with product453 rw [value, halfRange.cardinality, halfRange.cardinality]454 rfl455