Lean / Reader
Lean reader

WallisTriples.lean

328 lines44 declarationsno sorryimports WallisTypes WallisConvolution Mathlib.Data.Nat.Choose.Sum Mathlib.Data.Finset.Sort Mathlib.Data.Finset.Sum Mathlib.Order.Fin.Basic +1

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

  1. import WallisTypes
  2. import WallisConvolution
  3. import Mathlib.Data.Nat.Choose.Sum
  4. import Mathlib.Data.Finset.Sort
  5. import Mathlib.Data.Finset.Sum
  6. import Mathlib.Order.Fin.Basic
  7. import Mathlib.Tactic
  8. open Finset
  9. namespace WallisGap
  10. abbrev Disc (n m : ℕ) := {t : Triple n m // DisjointTriple t}
  11. private def supp {n m : ℕ} (t : Triple n m) := insert t.2.2 (t.1.val ∪ t.2.1.val)
  12. private theorem supp_card {n m : ℕ} (t : Disc n m) : (supp t.val).card = 2*m+1 := by
  13. obtain ⟨h,h₁,h₂⟩ := t.property
  14. have hm := t.val.1.property
  15. have hn := t.val.2.1.property
  16. have hu : t.val.2.2 ∉ t.val.1.val ∪ t.val.2.1.val := by simp [h₁,h₂]
  17. rw [supp, Finset.card_insert_of_notMem hu, card_union_of_disjoint h, hm, hn]
  18. omega
  19. private theorem fullSupp {m : ℕ} (t : Disc (2*m+1) m) : supp t.val = univ :=
  20. Finset.eq_univ_of_card _ (by simpa using supp_card t)
  21. private def transport {d n m : ℕ} (e : Fin d ↪o Fin n) (t : Triple d m) : Triple n m :=
  22. (⟨t.1.val.map e.toEmbedding, by simpa using t.1.property⟩,
  23. ⟨t.2.1.val.map e.toEmbedding, by simpa using t.2.1.property⟩, e t.2.2)
  24. private theorem transport_disc {d n m : ℕ} (e : Fin d ↪o Fin n) (t : Triple d m) :
  25. DisjointTriple (transport e t) ↔ DisjointTriple t := by
  26. simp [DisjointTriple, transport, Finset.disjoint_map]
  27. private theorem transport_rank {d n m : ℕ} (e : Fin d ↪o Fin n) (t : Triple d m) :
  28. MatchingRank (transport e t) ↔ MatchingRank t := by
  29. simp [MatchingRank, transport, Finset.filter_map, Function.comp_def]
  30. private theorem transport_supp {d n m : ℕ} (e : Fin d ↪o Fin n) (t : Triple d m) :
  31. supp (transport e t) = (supp t).map e.toEmbedding := by
  32. simp [supp, transport, map_union]
  33. private theorem transport_inj {d n m : ℕ} (e : Fin d ↪o Fin n) :
  34. Function.Injective (transport (m := m) e) := by
  35. rintro ⟨⟨a,ha⟩,⟨b,hb⟩,u⟩ ⟨⟨c,hc⟩,⟨d,hd⟩,v⟩ h
  36. simpa [transport, Prod.mk.injEq, Subtype.mk.injEq] using h
  37. private def supportLift {n m : ℕ} (x : SubsetOfSize n (2*m+1) × Disc (2*m+1) m) :
  38. Disc n m :=
  39. ⟨transport (x.1.val.orderEmbOfFin x.1.property) x.2.val,
  40. (transport_disc ..).mpr x.2.property⟩
  41. private theorem recoverSupp {n m : ℕ} (C : SubsetOfSize n (2*m+1)) (t : Disc (2*m+1) m) :
  42. supp (supportLift (C,t)).val = C.val := by
  43. change supp (transport _ _) = _
  44. rw [transport_supp, fullSupp, map_orderEmbOfFin_univ]
  45. private theorem lift_inj (n m : ℕ) : Function.Injective (@supportLift n m) := by
  46. rintro ⟨C,t⟩ ⟨D,s⟩ h
  47. have hCD : C = D := Subtype.ext <|
  48. (recoverSupp C t).symm.trans ((congrArg (fun x => supp (Subtype.val x)) h).trans (recoverSupp D s))
  49. subst D
  50. congr 1
  51. exact Subtype.ext (transport_inj (C.val.orderEmbOfFin C.property) (congrArg Subtype.val h))
  52. private theorem realizes {α β : Type*} [DecidableEq β] [Fintype α]
  53. (e : α ↪ β) (s : Finset β) (h : s ⊆ univ.map e) :
  54. (univ.filter (fun i => e i ∈ s)).map e = s := by
  55. ext x
  56. constructor
  57. · intro hx
  58. obtain ⟨a,ha,rfl⟩ := mem_map.mp hx
  59. exact (mem_filter.mp ha).2
  60. · intro hx
  61. obtain ⟨a,ha,rfl⟩ := mem_map.mp (h hx)
  62. exact mem_map.mpr ⟨a,by simp [hx],rfl⟩
  63. private theorem lift_surj (n m : ℕ) : Function.Surjective (@supportLift n m) := by
  64. rintro ⟨t,ht⟩
  65. let C : SubsetOfSize n (2*m+1) := ⟨supp t,supp_card ⟨t,ht⟩⟩
  66. let e := C.val.orderEmbOfFin C.property
  67. have hall : univ.map e.toEmbedding = supp t := map_orderEmbOfFin_univ ..
  68. have ha : t.1.val ⊆ univ.map e.toEmbedding := by
  69. rw [hall]; exact fun x hx => mem_insert_of_mem (mem_union_left _ hx)
  70. have hb : t.2.1.val ⊆ univ.map e.toEmbedding := by
  71. rw [hall]; exact fun x hx => mem_insert_of_mem (mem_union_right _ hx)
  72. have hu : t.2.2 ∈ univ.map e.toEmbedding := by rw [hall]; exact mem_insert_self ..
  73. obtain ⟨u,_,hu⟩ := mem_map.mp hu
  74. have hA := realizes e.toEmbedding _ ha
  75. have hB := realizes e.toEmbedding _ hb
  76. let s : Triple (2*m+1) m :=
  77. (⟨_,by simpa using (congrArg Finset.card hA).trans t.1.property⟩,
  78. ⟨_,by simpa using (congrArg Finset.card hB).trans t.2.1.property⟩,u)
  79. have h : transport e s = t := by
  80. exact Prod.ext (Subtype.ext hA) (Prod.ext (Subtype.ext hB) hu)
  81. have hs : DisjointTriple s := (transport_disc e s).mp (h ▸ ht)
  82. exact ⟨(C,⟨s,hs⟩),Subtype.ext h⟩
  83. private noncomputable def supportEquiv (n m : ℕ) :
  84. SubsetOfSize n (2*m+1) × Disc (2*m+1) m ≃ Disc n m :=
  85. Equiv.ofBijective supportLift ⟨lift_inj n m, lift_surj n m⟩
  86. private def removeMark {m : ℕ} (w : MarkedWord m) : Triple (2*m+1) m :=
  87. (⟨w.2.val.map w.1.succAboveOrderEmb.toEmbedding, by simpa using w.2.property⟩,
  88. ⟨w.2.valᶜ.map w.1.succAboveOrderEmb.toEmbedding, by
  89. simp [Finset.card_compl, w.2.property]; omega⟩, w.1)
  90. private theorem removeMark_disc {m : ℕ} (w : MarkedWord m) :
  91. DisjointTriple (removeMark w) := by
  92. simp [removeMark, DisjointTriple, disjoint_map, Finset.mem_map, Fin.succAbove_ne, disjoint_compl_right]
  93. private def markedLift {m : ℕ} (w : MarkedWord m) : Disc (2*m+1) m :=
  94. ⟨removeMark w, removeMark_disc w⟩
  95. private theorem markedLift_inj (m : ℕ) : Function.Injective (@markedLift m) := by
  96. rintro ⟨u,a⟩ ⟨v,b⟩ h
  97. have huv : u = v := congrArg (fun x => x.val.2.2) h
  98. subst v
  99. have hAB : a.val.map u.succAboveOrderEmb.toEmbedding = b.val.map _ :=
  100. congrArg (fun x => x.val.1.val) h
  101. exact Prod.ext rfl (Subtype.ext (Finset.map_inj.mp hAB))
  102. private theorem markedLift_surj (m : ℕ) : Function.Surjective (@markedLift m) := by
  103. rintro ⟨⟨A,B,u⟩,hd⟩
  104. obtain ⟨hab,hua,hub⟩ := hd
  105. let e := u.succAboveOrderEmb.toEmbedding
  106. have hImage : univ.map e = ({u} : Finset _)ᶜ := by
  107. ext x
  108. simp [e, mem_map, Fin.exists_succAbove_eq_iff]
  109. have ha : A.val ⊆ univ.map e := by
  110. rw [hImage]; intro x hx; simp only [mem_compl, mem_singleton]; exact fun h => hua (h ▸ hx)
  111. have hA := realizes e _ ha
  112. let w : BalancedWord m := ⟨_,by simpa using (congrArg Finset.card hA).trans A.property⟩
  113. have hB : w.valᶜ.map e = B.val := by
  114. apply Finset.eq_of_subset_of_card_le
  115. · intro x hx
  116. by_contra hn
  117. obtain ⟨i,hi,rfl⟩ := mem_map.mp hx
  118. have hiA : e i ∉ A.val := by rw [← hA]; simpa [w] using hi
  119. have hmem := fullSupp (m := m) ⟨(A,B,u),⟨hab,hua,hub⟩⟩
  120. have hx : e i ∈ supp (A,B,u) := hmem ▸ mem_univ _
  121. have hne : e i ≠ u := Fin.succAbove_ne u i
  122. simp only [supp,mem_insert,mem_union,hne,hiA,hn,or_self] at hx
  123. · simp [card_compl,w.property,B.property]; omega
  124. exact ⟨(u,w),Subtype.ext (Prod.ext (Subtype.ext hA) (Prod.ext (Subtype.ext hB) rfl))⟩
  125. private noncomputable def markedEquiv (m : ℕ) : MarkedWord m ≃ Disc (2*m+1) m :=
  126. Equiv.ofBijective markedLift ⟨markedLift_inj m, markedLift_surj m⟩
  127. private def pre {N : ℕ} (u : Fin (N+1)) (s : Finset (Fin N)) :=
  128. s.filter fun i => i.val < u.val
  129. private theorem matching_mark {m : ℕ} (w : MarkedWord m) :
  130. MatchingRank (removeMark w) ↔ (pre w.1 w.2.val).card = (pre w.1 w.2.valᶜ).card := by
  131. have h (i : Fin (2*m)) : w.1.succAboveOrderEmb i < w.1 ↔ i.val < w.1.val :=
  132. Fin.succAbove_lt_iff_castSucc_lt w.1 i
  133. simp only [MatchingRank, removeMark, pre, Finset.filter_map, Function.comp_def,
  134. RelEmbedding.coe_toEmbedding, h, Finset.card_map]
  135. private theorem all_pre {N : ℕ} (u : Fin (N+1)) : (pre u univ).card = u.val := by
  136. let e := Fin.castLEEmb (show u.val ≤ N by omega)
  137. have h : pre u univ = univ.map e := by
  138. ext i
  139. simp only [pre,mem_filter,mem_univ,true_and,mem_map]
  140. constructor
  141. · intro h; exact ⟨⟨i.val,h⟩,Fin.ext rfl⟩
  142. · rintro ⟨j,rfl⟩; exact j.isLt
  143. simp [h,e]
  144. private theorem pre_add_compl {N : ℕ} (u : Fin (N+1)) (s : Finset (Fin N)) :
  145. (pre u s).card + (pre u sᶜ).card = u.val := by
  146. have e₁ : pre u s = (pre u univ).filter (· ∈ s) := by ext; simp [pre,and_comm]
  147. have e₂ : pre u sᶜ = (pre u univ).filter (· ∉ s) := by ext; simp [pre,and_comm]
  148. rw [e₁,e₂, filter_card_add_filter_neg_card_eq_card, all_pre]
  149. private theorem match_iff {m : ℕ} (w : MarkedWord m) :
  150. MatchingRank (removeMark w) ↔ 2 * (pre w.1 w.2.val).card = w.1.val := by
  151. rw [matching_mark]
  152. have h := pre_add_compl w.1 w.2.val
  153. omega
  154. private def cutEquiv {a b N : ℕ} (h : a+b=N) : Fin a ⊕ Fin b ≃ Fin N :=
  155. finSumFinEquiv.trans (finCongr h)
  156. private def join {a b N : ℕ} (h : a+b=N) (l : Finset (Fin a)) (r : Finset (Fin b)) :
  157. Finset (Fin N) := (l.disjSum r).map (cutEquiv h).toEmbedding
  158. private theorem joinAll {a b N : ℕ} (h : a+b=N) (s : Finset (Fin N)) :
  159. ∃ l r, join h l r = s := by
  160. let e := cutEquiv h
  161. let z := e.finsetCongr.symm s
  162. refine ⟨z.toLeft,z.toRight,?_⟩
  163. rw [join, toLeft_disjSum_toRight]
  164. exact e.finsetCongr.apply_symm_apply s
  165. private theorem join_inj {a b N : ℕ} (h : a+b=N) {l l' : Finset (Fin a)} {r r' : Finset (Fin b)}
  166. (he : join h l r = join h l' r') : l = l' ∧ r = r' :=
  167. disjSum_inj.mp (Finset.map_inj.mp he)
  168. private theorem join_size {a b N : ℕ} (h : a+b=N) (l : Finset (Fin a)) (r : Finset (Fin b)) :
  169. (join h l r).card = l.card + r.card := by simp [join]
  170. private theorem join_pre {a b N : ℕ} (h : a+b=N) (l : Finset (Fin a)) (r : Finset (Fin b)) :
  171. (pre (⟨a,by omega⟩ : Fin (N+1)) (join h l r)).card = l.card := by
  172. let e := cutEquiv h
  173. have H : (l.disjSum r).filter (fun x => (e x).val < a) = l.map Function.Embedding.inl := by
  174. ext x
  175. cases x with
  176. | inl i =>
  177. simp [e,cutEquiv,i.isLt]
  178. | inr j =>
  179. simp [e,cutEquiv]
  180. simp only [pre,join,filter_map,Function.comp_def]
  181. simpa [e] using congrArg Finset.card H
  182. private def pieceWord {m : ℕ} (p : BalancedPieces m) : MarkedWord m :=
  183. (⟨2*p.1.val,by have := p.1.isLt; omega⟩, ⟨join (by have := p.1.isLt; omega) p.2.1.val p.2.2.val,
  184. by rw [join_size,p.2.1.property,p.2.2.property]; have := p.1.isLt; omega⟩)
  185. private theorem piece_good {m : ℕ} (p : BalancedPieces m) :
  186. MatchingRank (removeMark (pieceWord p)) := by
  187. rw [match_iff]
  188. simp [pieceWord,join_pre,p.2.1.property]
  189. private theorem piece_inj {m : ℕ} : Function.Injective (@pieceWord m) := by
  190. rintro ⟨j,L,R⟩ ⟨k,L',R'⟩ he
  191. have hj : 2*j.val = 2*k.val := congrArg (fun z => z.1.val) he
  192. have hjk : j = k := Fin.ext (by omega)
  193. subst k
  194. have he' := congrArg (fun z => z.2.val) he
  195. obtain ⟨hl,hr⟩ := join_inj _ he'
  196. congr 2 <;> apply Subtype.ext <;> assumption
  197. private theorem piece_surj {m : ℕ} (w : MarkedWord m) (hw : MatchingRank (removeMark w)) :
  198. ∃ p : BalancedPieces m, pieceWord p = w := by
  199. obtain ⟨u,W⟩ := w
  200. have h := (match_iff (u,W)).mp hw
  201. let j : Fin (m+1) := ⟨(pre u W.val).card,by
  202. have hc : (pre u W.val).card ≤ W.val.card := Finset.card_le_card (filter_subset ..)
  203. have := W.property; omega⟩
  204. have hu : 2*j.val = u.val := h
  205. have hsum : 2*j.val + 2*(m-j.val) = 2*m := by have := j.isLt; omega
  206. obtain ⟨l,r,H⟩ := joinAll hsum W.val
  207. have hjl : l.card = j.val := by
  208. have hh := join_pre hsum l r
  209. rw [H] at hh
  210. have uc : (⟨2*j.val,by omega⟩ : Fin (2*m+1)) = u := Fin.ext hu
  211. rw [uc] at hh
  212. exact hh.symm
  213. have hr : r.card = m-j.val := by
  214. have hc := join_size hsum l r
  215. rw [H,W.property,hjl] at hc; omega
  216. exact ⟨⟨j,⟨l,hjl⟩,⟨r,hr⟩⟩, Prod.ext (Fin.ext hu) (Subtype.ext H)⟩
  217. private abbrev Good (n m : ℕ) := {t : Triple n m // DisjointTriple t ∧ MatchingRank t}
  218. private def goodLift {n m : ℕ} (x : SubsetOfSize n (2*m+1) × BalancedPieces m) : Good n m :=
  219. let d := supportLift (x.1,markedLift (pieceWord x.2))
  220. ⟨d.val,d.property,(transport_rank ..).mpr (piece_good x.2)⟩
  221. private theorem goodLift_inj (n m : ℕ) : Function.Injective (@goodLift n m) := by
  222. rintro ⟨C,p⟩ ⟨D,q⟩ h
  223. have hd : supportLift (C, markedLift (pieceWord p)) = supportLift (D, markedLift (pieceWord q)) :=
  224. Subtype.ext (congrArg (fun z : Good n m => z.val) h)
  225. have h' := (lift_inj n m) hd
  226. have hC : C = D := congrArg (fun z : SubsetOfSize n (2*m+1) × Disc (2*m+1) m => z.1) h'
  227. have hW : markedLift (pieceWord p) = markedLift (pieceWord q) := congrArg Prod.snd h'
  228. exact Prod.ext hC (piece_inj (markedLift_inj _ hW))
  229. private theorem goodLift_surj (n m : ℕ) : Function.Surjective (@goodLift n m) := by
  230. rintro ⟨t,ht,hr⟩
  231. obtain ⟨⟨C,s⟩,hs⟩ := lift_surj n m ⟨t,ht⟩
  232. obtain ⟨w,hw⟩ := markedLift_surj _ s
  233. subst s
  234. have he : transport (C.val.orderEmbOfFin C.property) (removeMark w) = t := congrArg Subtype.val hs
  235. have hrw : MatchingRank (removeMark w) := (transport_rank ..).mp (he ▸ hr)
  236. obtain ⟨p,hp⟩ := piece_surj w hrw
  237. subst w
  238. exact ⟨(C,p),Subtype.ext he⟩
  239. /-- Among all disjoint triples of m-subsets and a marked position, the relative frequency
  240. of equal ranks is independent of n. The identity is valid even when both counts vanish. -/
  241. theorem disjointTripleRankCount (n m : ℕ) :
  242. (univ.filter fun t : Triple n m => DisjointTriple t ∧ MatchingRank t).card *
  243. ((2*m+1)*Nat.centralBinom m) =
  244. (univ.filter fun t : Triple n m => DisjointTriple t).card * 4^m := by
  245. let goodEquiv := Equiv.ofBijective (@goodLift n m) ⟨goodLift_inj n m,goodLift_surj n m⟩
  246. have hg := (Fintype.card_congr goodEquiv).symm
  247. have hd := (Fintype.card_congr (supportEquiv n m)).symm
  248. have hm := (Fintype.card_congr (markedEquiv m)).symm
  249. have hp : Fintype.card (BalancedPieces m) = 4^m := by
  250. simp only [BalancedPieces, Fintype.card_sigma,Fintype.card_prod,BalancedWord,
  251. Fintype.card_finset_len,Fintype.card_fin,← Nat.centralBinom_eq_two_mul_choose]
  252. rw [Fin.sum_univ_eq_sum_range (fun k => Nat.centralBinom k * Nat.centralBinom (m-k)),
  253. centralBinomialConvolution]
  254. simp only [Fintype.card_prod] at hd hg
  255. simp only [MarkedWord, BalancedWord, Fintype.card_prod, Fintype.card_fin,
  256. Fintype.card_finset_len,← Nat.centralBinom_eq_two_mul_choose] at hm
  257. rw [← Fintype.card_subtype, ← Fintype.card_subtype]
  258. change Fintype.card (Good n m) * _ = Fintype.card (Disc n m) * _
  259. rw [hg, hd, hm, hp]
  260. ring
  261. /-- Odd Wallis integral with the normalization from Problem 1.6. -/
  262. noncomputable def wallisValue (m : ℕ) : ℝ :=
  263. ∫ x in 0..Real.pi / 2, Real.sin x ^ (2*m+1)
  264. theorem wallisValue_bounds (m : ℕ) : 0 ≤ wallisValue m ∧ wallisValue m ≤ 1 := by
  265. rw [wallisValue, oddWallisIntegral]
  266. have hc : (0:ℝ) < Nat.centralBinom m := by exact_mod_cast Nat.centralBinom_pos m
  267. have hD : (0:ℝ) < (2*(m:ℝ)+1) * Nat.centralBinom m := by positivity
  268. constructor
  269. · positivity
  270. · rw [div_le_one hD]
  271. exact_mod_cast Nat.four_pow_le_two_mul_add_one_mul_central_binom m
  272. theorem disjointTripleRankCount_real (n m : ℕ) :
  273. ((univ.filter fun t : Triple n m => DisjointTriple t ∧ MatchingRank t).card : ℝ) =
  274. ((univ.filter fun t : Triple n m => DisjointTriple t).card : ℝ) * wallisValue m := by
  275. rw [wallisValue, oddWallisIntegral, ← mul_div_assoc]
  276. have hc : (0:ℝ) < Nat.centralBinom m := by exact_mod_cast Nat.centralBinom_pos m
  277. have hD : (0:ℝ) < (2*(m:ℝ)+1) * Nat.centralBinom m := by positivity
  278. apply (eq_div_iff hD.ne').mpr
  279. exact_mod_cast disjointTripleRankCount n m
  280. #print axioms disjointTripleRankCount_real
  281. end WallisGap