Lean / Reader
Lean reader

FiveGrid.lean

116 lines17 declarationsno sorryimports Mathlib.Data.Fintype.BigOperators Mathlib.Algebra.BigOperators.Fin Mathlib.Algebra.BigOperators.Ring.Finset Mathlib.Tactic.NormNum Mathlib.Tactic.Ring Mathlib.Data.Rat.Cast.Order
  1. import Mathlib.Data.Fintype.BigOperators
  2. import Mathlib.Algebra.BigOperators.Fin
  3. import Mathlib.Algebra.BigOperators.Ring.Finset
  4. import Mathlib.Tactic.NormNum
  5. import Mathlib.Tactic.Ring
  6. import Mathlib.Data.Rat.Cast.Order
  7. namespace FiveGrid
  8. /-- The one-coordinate third difference on the four signed numerators. -/
  9. def four (f : ℤ → ℚ) : ℚ := -f (-3) + 3 * f (-1) - 3 * f 1 + f 3
  10. /-- Accumulate the first two elementary symmetric functions of the scaled
  11. coordinate factors. Each point coordinate is its integer numerator over four. -/
  12. def quadratic : ℕ → ℤ → ℤ → ℚ
  13. | 0, _, e => 256 / (e : ℚ)
  14. | n+1, s, e => four fun x => four fun y =>
  15. let t := 16 - x*y
  16. quadratic n (s+t) (e+s*t)
  17. def six (f : ℤ → ℚ) : ℚ :=
  18. 18*(f 1 - f (-1)) - 12*(f 3 - f (-3)) + 2*(f 9 - f (-9))
  19. theorem compress (f : ℤ → ℚ) :
  20. (four fun x => four fun y => f (x*y)) = six f := by
  21. norm_num [four, six]
  22. ring
  23. def value : ℕ → ℤ → ℤ → ℚ
  24. | 0, _, e => 256 / (e : ℚ)
  25. | n+1, s, e => six fun p =>
  26. let t := 16-p
  27. value n (s+t) (e+s*t)
  28. theorem quadratic_eq (n : ℕ) (s e : ℤ) : quadratic n s e = value n s e := by
  29. induction n generalizing s e with
  30. | zero => rfl
  31. | succ n ih =>
  32. simp only [quadratic, value, ih]
  33. exact compress (fun p => value n (s+(16-p)) (e+s*(16-p)))
  34. set_option maxRecDepth 100000 in
  35. set_option maxHeartbeats 8000000 in
  36. theorem negative : quadratic 5 0 0 < -(1/51000 : ℚ) := by
  37. rw [quadratic_eq]
  38. decide +kernel
  39. @[reducible] def Grid : ℕ → Type
  40. | 0 => Unit
  41. | n+1 => Fin 4 × Grid n
  42. @[instance_reducible] instance gridFinite (n : ℕ) : Fintype (Grid n) := by
  43. induction n with
  44. | zero => exact inferInstanceAs (Fintype Unit)
  45. | succ n ih => exact @instFintypeProd (Fin 4) (Grid n) inferInstance ih
  46. def digit (i : Fin 4) : ℤ := 2 * (i.val : ℤ) - 3
  47. def weight (i : Fin 4) : ℚ := ![-1,3,-3,1] i
  48. def tensor : (n : ℕ) → Grid n → ℚ
  49. | 0, _ => 1
  50. | n+1, (i,x) => weight i * tensor n x
  51. def entry : (n : ℕ) → ℤ → ℤ → Grid n → Grid n → ℚ
  52. | 0, _, e, _, _ => 256 / (e : ℚ)
  53. | n+1, s, e, (i,x), (j,y) =>
  54. let t := 16 - digit i * digit j
  55. entry n (s+t) (e+s*t) x y
  56. lemma four_sum (f : ℤ → ℚ) : (∑ i, weight i * f (digit i)) = four f := by
  57. rw [Fin.sum_univ_four]
  58. change (-1)*f (-3) + 3*f (-1) + (-3)*f 1 + 1*f 3 = _
  59. unfold four
  60. ring
  61. lemma split (n : ℕ) (f : Grid (n+1) → ℚ) :
  62. ∑ x, f x = ∑ i : Fin 4, ∑ x : Grid n, f (i,x) := by
  63. change (∑ x : Fin 4 × Grid n, f x) = _
  64. rw [Fintype.sum_prod_type]
  65. theorem grid_form (n : ℕ) (s e : ℤ) :
  66. (∑ x : Grid n, ∑ y : Grid n, tensor n x * tensor n y * entry n s e x y) =
  67. quadratic n s e := by
  68. induction n generalizing s e with
  69. | zero => simp [Grid, tensor, entry, quadratic]
  70. | succ n ih =>
  71. rw [split]
  72. apply Eq.trans _ (show (∑ i, weight i * (∑ j, weight j *
  73. quadratic n (s+(16-digit i*digit j)) (e+s*(16-digit i*digit j)))) = _ from by
  74. let g (a b : ℤ) := quadratic n (s+(16-a*b)) (e+s*(16-a*b))
  75. change (∑ i, weight i * (∑ j, weight j * g (digit i) (digit j))) = _
  76. simp only [four_sum (g _)]
  77. exact four_sum (fun a => four (g a)))
  78. apply Finset.sum_congr rfl
  79. intro i _
  80. simp_rw [split n]
  81. rw [Finset.sum_comm, Finset.mul_sum]
  82. apply Finset.sum_congr rfl
  83. intro j _
  84. rw [← ih]
  85. simp only [Finset.mul_sum, tensor, entry]
  86. apply Finset.sum_congr rfl
  87. intro x _
  88. apply Finset.sum_congr rfl
  89. intro y _
  90. ring
  91. theorem grid_negative :
  92. (∑ x : Grid 5, ∑ y : Grid 5, tensor 5 x * tensor 5 y * entry 5 0 0 x y)
  93. < -(1/51000 : ℚ) := by
  94. rw [grid_form]
  95. exact negative
  96. end FiveGrid