Lean / Reader
Lean reader

GridExamples.lean

91 lines6 declarationsno sorryimports GridLift

Lean 4 Web has Mathlib, so it can check files that import only Mathlib. This one also imports GridLift, which it does not have: you can read it there, but it will not check. Build the project itself to check it.

  1. import GridLift
  2. namespace ReciprocalGrid
  3. open Finset
  4. theorem scale_harmonic (p : ℕ) (hp : 0 < p) (A : Finset ℕ) :
  5. harmonic (A.image (p * ·)) = harmonic A / (p : ℚ) := by
  6. rw [harmonic, sum_image (fun _ _ _ _ h => Nat.eq_of_mul_eq_mul_left hp h), harmonic, Finset.sum_div]
  7. apply Finset.sum_congr rfl
  8. intro n _
  9. simp [mul_comm, div_eq_mul_inv]
  10. theorem disjoint_prime_copy {D p : ℕ} (h : ¬p ∣ D) {U V : Finset ℕ}
  11. (hu : ∀ n ∈ U, n ∣ D) : Disjoint U (V.image (p * ·)) := by
  12. apply Finset.disjoint_left.mpr
  13. intro n hn hv
  14. obtain ⟨v, _, rfl⟩ := Finset.mem_image.mp hv
  15. exact h (dvd_trans (dvd_mul_right p v) (hu _ hn))
  16. theorem disjoint_prime_copies {D p q : ℕ} (hp : Nat.Prime p) (hq : Nat.Prime q)
  17. (hne : p ≠ q) (h : ¬p ∣ D) {U V : Finset ℕ} (hv : ∀ n ∈ V, n ∣ D) :
  18. Disjoint (U.image (p * ·)) (V.image (q * ·)) := by
  19. apply Finset.disjoint_left.mpr
  20. intro n hn hn'
  21. obtain ⟨u, _, rfl⟩ := Finset.mem_image.mp hn
  22. obtain ⟨v, hV, he⟩ := Finset.mem_image.mp hn'
  23. have hd : p ∣ q*v := he ▸ dvd_mul_right p u
  24. rcases hp.dvd_mul.mp hd with h₁ | h₂
  25. · exact hne ((Nat.prime_dvd_prime_iff_eq hp hq).mp h₁)
  26. · exact h (h₂.trans (hv v hV))
  27. theorem glue_harmonic {D : ℕ} (S A₀ : Finset ℕ) (A : ℕ → Finset ℕ)
  28. (hs : ∀ p ∈ S, Nat.Prime p) (hnew : ∀ p ∈ S, ¬p ∣ D)
  29. (h₀ : ∀ n ∈ A₀, n ∣ D) (hA : ∀ p ∈ S, ∀ n ∈ A p, n ∣ D) :
  30. harmonic (A₀ ∪ S.biUnion (fun p => (A p).image (p * ·))) =
  31. harmonic A₀ + ∑ p ∈ S, harmonic (A p) / (p : ℚ) := by
  32. have hd : Disjoint A₀ (S.biUnion (fun p => (A p).image (p * ·))) :=
  33. (Finset.disjoint_biUnion_right ..).mpr fun p hp => disjoint_prime_copy (hnew p hp) h₀
  34. rw [append_mass hd]
  35. congr 1
  36. have hh : (↑S : Set ℕ).PairwiseDisjoint (fun p => (A p).image (p * ·)) := by
  37. intro p hp q hq hne
  38. exact disjoint_prime_copies (hs p hp) (hs q hq) hne (hnew p hp) (hA q hq)
  39. unfold harmonic
  40. rw [Finset.sum_biUnion hh]
  41. exact Finset.sum_congr rfl fun p hp => scale_harmonic p (hs p hp).pos (A p)
  42. theorem grid_value {D a b₀ : ℕ} (S : Finset ℕ) (b : ℕ → ℕ) (hd : 0 < D)
  43. (hs : ∀ p ∈ S, Nat.Prime p)
  44. (he : b₀*modulus S + ∑ p ∈ S, cofactor S p*b p = a) :
  45. (b₀:ℚ)/D + ∑ p ∈ S, ((b p : ℚ)/D)/(p:ℚ) = (a:ℚ)/(D*(modulus S:ℚ)) := by
  46. have hP : 0 < modulus S := Finset.prod_pos fun p hp => (hs p hp).pos
  47. have hD' : (D : ℚ) ≠ 0 := by exact_mod_cast hd.ne'
  48. have hP' : (modulus S : ℚ) ≠ 0 := by exact_mod_cast hP.ne'
  49. apply (eq_div_iff (mul_ne_zero hD' hP')).mpr
  50. rw [add_mul, Finset.sum_mul]
  51. have hw : ∀ p ∈ S, (b p : ℚ)/D/p*(D*(modulus S:ℚ)) = (cofactor S p:ℚ)*b p := by
  52. intro p hp
  53. have hp' : (p : ℚ) ≠ 0 := by exact_mod_cast (hs p hp).ne_zero
  54. rw [← cofactor_mul hp, Nat.cast_mul]
  55. field_simp
  56. rw [Finset.sum_congr rfl hw]
  57. convert (show (b₀ : ℚ)*(modulus S:ℚ) + ∑ p ∈ S, (cofactor S p:ℚ)*(b p:ℚ) = a by exact_mod_cast he) using 1
  58. field_simp
  59. theorem lift_representation {D m a : ℕ} {S U : Finset ℕ} (hd : 0 < D)
  60. (hu : ∀ n ∈ U, n ∣ D) (hs : ∀ p ∈ S, Nat.Prime p) (hnew : ∀ p ∈ S, ¬p ∣ D)
  61. (hw : ∀ p ∈ S, 4*p ≤ D) (hm : 2*m ≤ D)
  62. (old : ∀ n, m ≤ n → n ≤ D → ∃ A ⊆ U, harmonic A = (n:ℚ)/D)
  63. (hlo : m*modulus S + ∑ p ∈ S, cofactor S p*D ≤ a) (hhi : a ≤ D*modulus S) :
  64. ∃ V ⊆ U ∪ S.biUnion (fun p => U.image (p * ·)), harmonic V = (a:ℚ)/(D*(modulus S:ℚ)) := by
  65. classical
  66. obtain ⟨b₀,b,h₁,h₂,hb,he⟩ := bounded_grid_digits hs hw hm hlo hhi
  67. obtain ⟨A₀,hA₀,hv₀⟩ := old b₀ h₁ h₂
  68. have hex : ∀ p, ∃ A ⊆ U, p ∈ S → harmonic A = (b p:ℚ)/D := by
  69. intro p
  70. by_cases h : p ∈ S
  71. · obtain ⟨A,hA,hv⟩ := old _ (hb p h).1 (hb p h).2
  72. exact ⟨A,hA,fun _ => hv⟩
  73. · exact ⟨∅,Finset.empty_subset _, fun hp => False.elim (h hp)⟩
  74. choose A hA hv using hex
  75. refine ⟨A₀ ∪ S.biUnion (fun p => (A p).image (p * ·)), Finset.union_subset_union hA₀ ?_, ?_⟩
  76. · apply Finset.biUnion_subset.mpr
  77. intro p hp
  78. exact (Finset.image_subset_image (hA p)).trans (Finset.subset_biUnion_of_mem (fun p => U.image (p * ·)) hp)
  79. · rw [glue_harmonic S A₀ A hs hnew (fun n hn => hu n (hA₀ hn)) (fun p _ n hn => hu n (hA p hn)), hv₀]
  80. rw [Finset.sum_congr rfl (fun (p : ℕ) (hp : p ∈ S) => congrArg (fun z : ℚ => z / (p:ℚ)) (hv p hp))]
  81. exact grid_value S b hd hs he
  82. end ReciprocalGrid