From ab519640927ed7cf5686f7dc7e0680273139adce Mon Sep 17 00:00:00 2001 From: Vasily Ilin Date: Sat, 28 Nov 2026 22:58:52 +0000 Subject: [PATCH] Speed up MovingSofa certificate replay with bounded Taylor steps --- .../Foundations/AreaCertificateReplay.lean | 92 +++++ .../Foundations/CapAreaCertificate.lean | 8 +- .../Foundations/NicheAreaCertificate.lean | 8 +- .../PartE/Certificates/Bundle001.lean | 4 +- .../PartE/Certificates/Bundle002.lean | 4 +- .../PartE/Certificates/Bundle003.lean | 4 +- .../PartE/Certificates/Bundle004.lean | 4 +- .../PartE/Certificates/Bundle005.lean | 4 +- .../PartE/Certificates/Bundle006.lean | 4 +- .../PartE/Certificates/Bundle007.lean | 4 +- .../PartE/Certificates/Bundle008.lean | 4 +- .../PartE/Certificates/Bundle009.lean | 4 +- .../PartE/Certificates/Bundle010.lean | 4 +- .../PartE/Certificates/Bundle011.lean | 4 +- .../PartE/Certificates/Bundle012.lean | 4 +- .../PartE/Certificates/Bundle013.lean | 4 +- .../PartE/Certificates/Bundle014.lean | 4 +- .../PartE/Certificates/Bundle015.lean | 4 +- .../PartE/Certificates/Bundle016.lean | 4 +- .../PartE/Certificates/Bundle017.lean | 4 +- .../PartE/Certificates/Bundle018.lean | 4 +- .../PartE/Certificates/Bundle019.lean | 4 +- .../PartE/Certificates/Bundle020.lean | 4 +- .../PartE/Certificates/Bundle021.lean | 4 +- .../PartE/Certificates/Bundle022.lean | 4 +- .../PartE/Certificates/Bundle023.lean | 4 +- .../PartE/Certificates/Bundle024.lean | 4 +- .../PartE/Certificates/Bundle025.lean | 4 +- .../PartE/Certificates/Bundle026.lean | 4 +- .../PartE/Certificates/Bundle027.lean | 4 +- .../PartE/Certificates/Bundle028.lean | 4 +- .../PartE/Certificates/Bundle029.lean | 4 +- .../PartE/Certificates/Bundle030.lean | 4 +- .../KernelOnly/PartE/FixedPointResiduals.lean | 368 ++++++++++++++++++ 34 files changed, 534 insertions(+), 62 deletions(-) diff --git a/LeanPool/MovingSofa/Development/Geometry/Foundations/AreaCertificateReplay.lean b/LeanPool/MovingSofa/Development/Geometry/Foundations/AreaCertificateReplay.lean index e17ed3c2e2..caebb58009 100644 --- a/LeanPool/MovingSofa/Development/Geometry/Foundations/AreaCertificateReplay.lean +++ b/LeanPool/MovingSofa/Development/Geometry/Foundations/AreaCertificateReplay.lean @@ -253,4 +253,96 @@ def rectangle (row column : ℕ) : ℤ := theorem rectangle_eq (row column : ℕ) : rectangle row column = rectAreaZ row column := by simp only [rectangle, contact_eq, eval_eq, rectAreaZ, rectLoZ, rectHiZ, rectHZ] +/-- Match the sign constructor before passing an integer to the next calculation. -/ +def withInt (x : ℤ) (use : ℤ → α) : α := + Int.casesOn x (fun n => use (Int.ofNat n)) (fun n => use (Int.negSucc n)) + +private theorem withInt_eq (x : ℤ) (use : ℤ → α) : withInt x use = use x := by + cases x <;> rfl + +/-- Pass the four evaluated endpoints to the next step of an area sum. -/ +def withContact (contact : SI × SI) (use : (SI × SI) → α) : α := + withInt contact.1.lo fun xLo => + withInt contact.1.hi fun xHi => + withInt contact.2.lo fun yLo => + withInt contact.2.hi fun yHi => + use (⟨xLo, xHi⟩, ⟨yLo, yHi⟩) + +private theorem withContact_eq (contact : SI × SI) (use : (SI × SI) → α) : + withContact contact use = use contact := by + rcases contact with ⟨⟨xLo, xHi⟩, ⟨yLo, yHi⟩⟩ + simp only [withContact, withInt_eq] + +/-- Traverse adjacent pairs, reusing the right contact as the next left contact. -/ +def sumAdjacent (sample : ℕ → SI × SI) (area : ℕ → (SI × SI) → (SI × SI) → ℤ) : + (start : ℕ) → (left : SI × SI) → (length : ℕ) → ℤ + | _, _, 0 => 0 + | start, left, length + 1 => + withContact (sample (start + 1)) fun right => + area start left right + sumAdjacent sample area (start + 1) right length + +/-- The traversal adds the same terms as the independent evaluations. -/ +theorem sumAdjacent_eq (sample : ℕ → SI × SI) + (area : ℕ → (SI × SI) → (SI × SI) → ℤ) (start length : ℕ) : + sumAdjacent sample area start (sample start) length = + ((List.range' start length).map fun i => area i (sample i) (sample (i + 1))).sum := by + induction length generalizing start with + | zero => rfl + | succ length ih => + simp only [sumAdjacent, withContact_eq, ih, List.range'_succ, List.map_cons, List.sum_cons] + +/-- Compute a fan determinant using the contact points supplied by the traversal. -/ +def crossFromContacts (center left right : SI × SI) : SI := + sub (mul (sub left.1 center.1) (sub right.2 center.2)) + (mul (sub left.2 center.2) (sub right.1 center.1)) + +/-- Sum the lower endpoints of a block of fan determinants. -/ +def capBlock (start length : ℕ) : ℤ := + withContact anchor fun center => + withContact (fan start) fun left => + sumAdjacent fan (fun _ left right => (crossFromContacts center left right).lo) + start left length + +/-- The block sums the original lower endpoints. -/ +theorem capBlock_eq (start length : ℕ) : + capBlock start length = ((List.range' start length).map fun i => (crossZ i).lo).sum := by + simp only [capBlock, withContact_eq, sumAdjacent_eq] + have terms : (fun i => (crossFromContacts anchor (fan i) (fan (i + 1))).lo) = + (fun i => (crossZ i).lo) := by + funext i + rw [← cross_eq] + rfl + rw [terms] + +/-- Compute the area of a niche rectangle using its two supplied contacts. -/ +def rectangleFromContacts (row index : ℕ) (left right : SI × SI) : ℤ := + let lo := if rowFwd row then left.1.lo else right.1.lo + let hi := if rowFwd row then right.1.hi else left.1.hi + let height := imax (eval (stageOf (index + 1)) ⟨(ttZ index).lo, (ttZ (index + 1)).hi⟩ + (rowKind row)).2.hi left.2.hi + imax 0 (hi - lo) * imax 0 height + +/-- Sum an adjacent block of niche rectangles. -/ +def nicheBlock (row start length : ℕ) : ℤ := + let sample := fun column => contact (rowBase row column) (rowKind row) + withContact (sample start) fun left => + sumAdjacent sample (fun column => rectangleFromContacts row (rowBase row column)) + start left length + +/-- The block sums the original covering rectangles. -/ +theorem nicheBlock_eq (row start length : ℕ) : + nicheBlock row start length = ((List.range' start length).map (rectAreaZ row)).sum := by + simp only [nicheBlock, withContact_eq, sumAdjacent_eq] + have terms : (fun i => rectangleFromContacts row (rowBase row i) + (contact (rowBase row i) (rowKind row)) (contact (rowBase row (i + 1)) (rowKind row))) = + rectAreaZ row := by + funext i + rw [← rectangle_eq] + have next : rowBase row (i + 1) = rowBase row i + 1 := by + unfold rowBase + rw [Nat.add_assoc] + rw [next] + rfl + rw [terms] + end MovingSofa.GerverAreaCert.AreaReplay diff --git a/LeanPool/MovingSofa/Development/Geometry/Foundations/CapAreaCertificate.lean b/LeanPool/MovingSofa/Development/Geometry/Foundations/CapAreaCertificate.lean index 687a92b720..278fc48182 100644 --- a/LeanPool/MovingSofa/Development/Geometry/Foundations/CapAreaCertificate.lean +++ b/LeanPool/MovingSofa/Development/Geometry/Foundations/CapAreaCertificate.lean @@ -79,7 +79,13 @@ private theorem replayLower_eq : replayLower = capLower := by Decidable (blockSum capLower start length = expected) := decidable_of_iff (blockSum replayLower start length = expected) (by rw [replayLower_eq]) -attribute [local instance 1100] decideBlock +@[instance_reducible] private def decideAdjacentBlock (start length : ℕ) (expected : ℤ) : + Decidable (blockSum capLower start length = expected) := + decidable_of_iff (AreaReplay.capBlock start length = expected) (by + rw [AreaReplay.capBlock_eq] + rfl) + +attribute [local instance 1100] decideAdjacentBlock private theorem capBlock0 : blockSum capLower 0 64 = -333692128541 := by decide +kernel diff --git a/LeanPool/MovingSofa/Development/Geometry/Foundations/NicheAreaCertificate.lean b/LeanPool/MovingSofa/Development/Geometry/Foundations/NicheAreaCertificate.lean index 21cc8e5999..6d6a9f4027 100644 --- a/LeanPool/MovingSofa/Development/Geometry/Foundations/NicheAreaCertificate.lean +++ b/LeanPool/MovingSofa/Development/Geometry/Foundations/NicheAreaCertificate.lean @@ -55,7 +55,13 @@ private theorem rectangleBlockReplay_eq (row start length : ℕ) : decidable_of_iff (rectangleBlockReplay row start length = expected) (by rw [rectangleBlockReplay_eq]) -attribute [local instance 1100] decideRectangleBlock +@[instance_reducible] private def decideAdjacentBlock (row start length : ℕ) (expected : ℤ) : + Decidable (rectangleBlock row start length = expected) := + decidable_of_iff (AreaReplay.nicheBlock row start length = expected) (by + rw [AreaReplay.nicheBlock_eq] + rfl) + +attribute [local instance 1100] decideAdjacentBlock private theorem nicheRow0First : rectangleBlock 0 0 32 = 8845072297334670692780361894254852519218486529319646026 := by gerver_kernel_decide diff --git a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle001.lean b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle001.lean index d2266c860d..46d919fc90 100644 --- a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle001.lean +++ b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle001.lean @@ -18,9 +18,9 @@ public import LeanPool.MovingSofa.GerverSofa.KernelOnly.Core.Bundle005 * `GerverSofa.KernelOnly.PartE.Certificates.Batch004`. -/ --- Replay closed rejections with the exact shift-based binary-grid evaluator. +-- Try the bounded Taylor evaluator before full interval replay. attribute [local instance 1100] - GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShiftedGrid.decidableRejected + GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShortTaylor.decidableRejected @[expose] public section diff --git a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle002.lean b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle002.lean index 1a941b4c07..7cd13b33c1 100644 --- a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle002.lean +++ b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle002.lean @@ -16,9 +16,9 @@ public import LeanPool.MovingSofa.GerverSofa.KernelOnly.Core.Bundle005 * `GerverSofa.KernelOnly.PartE.Certificates.Batch014`. -/ --- Replay closed rejections with the exact shift-based binary-grid evaluator. +-- Try the bounded Taylor evaluator before full interval replay. attribute [local instance 1100] - GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShiftedGrid.decidableRejected + GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShortTaylor.decidableRejected @[expose] public section diff --git a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle003.lean b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle003.lean index bfb8d352a3..d6fff1b59f 100644 --- a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle003.lean +++ b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle003.lean @@ -16,9 +16,9 @@ public import LeanPool.MovingSofa.GerverSofa.KernelOnly.Core.Bundle005 * `GerverSofa.KernelOnly.PartE.Certificates.Batch012`. -/ --- Replay closed rejections with the exact shift-based binary-grid evaluator. +-- Try the bounded Taylor evaluator before full interval replay. attribute [local instance 1100] - GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShiftedGrid.decidableRejected + GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShortTaylor.decidableRejected @[expose] public section diff --git a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle004.lean b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle004.lean index 84a4a70935..53aca9b279 100644 --- a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle004.lean +++ b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle004.lean @@ -16,9 +16,9 @@ public import LeanPool.MovingSofa.GerverSofa.KernelOnly.Core.Bundle005 * `GerverSofa.KernelOnly.PartE.Certificates.Batch027`. -/ --- Replay closed rejections with the exact shift-based binary-grid evaluator. +-- Try the bounded Taylor evaluator before full interval replay. attribute [local instance 1100] - GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShiftedGrid.decidableRejected + GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShortTaylor.decidableRejected @[expose] public section diff --git a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle005.lean b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle005.lean index 39f21350d5..7567496d13 100644 --- a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle005.lean +++ b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle005.lean @@ -16,9 +16,9 @@ public import LeanPool.MovingSofa.GerverSofa.KernelOnly.Core.Bundle005 * `GerverSofa.KernelOnly.PartE.Certificates.Batch028`. -/ --- Replay closed rejections with the exact shift-based binary-grid evaluator. +-- Try the bounded Taylor evaluator before full interval replay. attribute [local instance 1100] - GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShiftedGrid.decidableRejected + GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShortTaylor.decidableRejected @[expose] public section diff --git a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle006.lean b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle006.lean index 4461ca223d..99e8cd34f8 100644 --- a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle006.lean +++ b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle006.lean @@ -16,9 +16,9 @@ public import LeanPool.MovingSofa.GerverSofa.KernelOnly.Core.Bundle005 * `GerverSofa.KernelOnly.PartE.Certificates.Batch023`. -/ --- Replay closed rejections with the exact shift-based binary-grid evaluator. +-- Try the bounded Taylor evaluator before full interval replay. attribute [local instance 1100] - GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShiftedGrid.decidableRejected + GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShortTaylor.decidableRejected @[expose] public section diff --git a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle007.lean b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle007.lean index 9e0f3c04dc..ccab90087e 100644 --- a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle007.lean +++ b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle007.lean @@ -16,9 +16,9 @@ public import LeanPool.MovingSofa.GerverSofa.KernelOnly.Core.Bundle005 * `GerverSofa.KernelOnly.PartE.Certificates.Batch026`. -/ --- Replay closed rejections with the exact shift-based binary-grid evaluator. +-- Try the bounded Taylor evaluator before full interval replay. attribute [local instance 1100] - GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShiftedGrid.decidableRejected + GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShortTaylor.decidableRejected @[expose] public section diff --git a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle008.lean b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle008.lean index 2f4215a105..3ac6308557 100644 --- a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle008.lean +++ b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle008.lean @@ -16,9 +16,9 @@ public import LeanPool.MovingSofa.GerverSofa.KernelOnly.Core.Bundle005 * `GerverSofa.KernelOnly.PartE.Certificates.Batch024`. -/ --- Replay closed rejections with the exact shift-based binary-grid evaluator. +-- Try the bounded Taylor evaluator before full interval replay. attribute [local instance 1100] - GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShiftedGrid.decidableRejected + GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShortTaylor.decidableRejected @[expose] public section diff --git a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle009.lean b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle009.lean index 84bc29f3b2..369370ef75 100644 --- a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle009.lean +++ b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle009.lean @@ -16,9 +16,9 @@ public import LeanPool.MovingSofa.GerverSofa.KernelOnly.Core.Bundle005 * `GerverSofa.KernelOnly.PartE.Certificates.Batch029`. -/ --- Replay closed rejections with the exact shift-based binary-grid evaluator. +-- Try the bounded Taylor evaluator before full interval replay. attribute [local instance 1100] - GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShiftedGrid.decidableRejected + GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShortTaylor.decidableRejected @[expose] public section diff --git a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle010.lean b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle010.lean index f577723c09..73304a5d6f 100644 --- a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle010.lean +++ b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle010.lean @@ -16,9 +16,9 @@ public import LeanPool.MovingSofa.GerverSofa.KernelOnly.Core.Bundle005 * `GerverSofa.KernelOnly.PartE.Certificates.Batch013`. -/ --- Replay closed rejections with the exact shift-based binary-grid evaluator. +-- Try the bounded Taylor evaluator before full interval replay. attribute [local instance 1100] - GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShiftedGrid.decidableRejected + GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShortTaylor.decidableRejected @[expose] public section diff --git a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle011.lean b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle011.lean index 3ea26b607c..a2b4afa1fb 100644 --- a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle011.lean +++ b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle011.lean @@ -16,9 +16,9 @@ public import LeanPool.MovingSofa.GerverSofa.KernelOnly.Core.Bundle005 * `GerverSofa.KernelOnly.PartE.Certificates.Batch019`. -/ --- Replay closed rejections with the exact shift-based binary-grid evaluator. +-- Try the bounded Taylor evaluator before full interval replay. attribute [local instance 1100] - GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShiftedGrid.decidableRejected + GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShortTaylor.decidableRejected @[expose] public section diff --git a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle012.lean b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle012.lean index b4163b630a..68e24238c1 100644 --- a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle012.lean +++ b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle012.lean @@ -16,9 +16,9 @@ public import LeanPool.MovingSofa.GerverSofa.KernelOnly.Core.Bundle005 * `GerverSofa.KernelOnly.PartE.Certificates.Batch021`. -/ --- Replay closed rejections with the exact shift-based binary-grid evaluator. +-- Try the bounded Taylor evaluator before full interval replay. attribute [local instance 1100] - GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShiftedGrid.decidableRejected + GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShortTaylor.decidableRejected @[expose] public section diff --git a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle013.lean b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle013.lean index c5629a1ae0..8af8c6a3a8 100644 --- a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle013.lean +++ b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle013.lean @@ -17,9 +17,9 @@ public import LeanPool.MovingSofa.GerverSofa.KernelOnly.Core.Bundle005 * `GerverSofa.KernelOnly.PartE.Certificates.Batch044`. -/ --- Replay closed rejections with the exact shift-based binary-grid evaluator. +-- Try the bounded Taylor evaluator before full interval replay. attribute [local instance 1100] - GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShiftedGrid.decidableRejected + GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShortTaylor.decidableRejected @[expose] public section diff --git a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle014.lean b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle014.lean index 85ab480ad6..48a55bc466 100644 --- a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle014.lean +++ b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle014.lean @@ -15,9 +15,9 @@ public import LeanPool.MovingSofa.GerverSofa.KernelOnly.Core.Bundle005 * `GerverSofa.KernelOnly.PartE.Certificates.Batch020`. -/ --- Replay closed rejections with the exact shift-based binary-grid evaluator. +-- Try the bounded Taylor evaluator before full interval replay. attribute [local instance 1100] - GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShiftedGrid.decidableRejected + GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShortTaylor.decidableRejected @[expose] public section diff --git a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle015.lean b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle015.lean index 1f9dc5500d..1a18dd7d23 100644 --- a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle015.lean +++ b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle015.lean @@ -15,9 +15,9 @@ public import LeanPool.MovingSofa.GerverSofa.KernelOnly.Core.Bundle005 * `GerverSofa.KernelOnly.PartE.Certificates.Batch022`. -/ --- Replay closed rejections with the exact shift-based binary-grid evaluator. +-- Try the bounded Taylor evaluator before full interval replay. attribute [local instance 1100] - GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShiftedGrid.decidableRejected + GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShortTaylor.decidableRejected @[expose] public section diff --git a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle016.lean b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle016.lean index 11d8723411..8943a74e17 100644 --- a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle016.lean +++ b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle016.lean @@ -15,9 +15,9 @@ public import LeanPool.MovingSofa.GerverSofa.KernelOnly.Core.Bundle005 * `GerverSofa.KernelOnly.PartE.Certificates.Batch025`. -/ --- Replay closed rejections with the exact shift-based binary-grid evaluator. +-- Try the bounded Taylor evaluator before full interval replay. attribute [local instance 1100] - GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShiftedGrid.decidableRejected + GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShortTaylor.decidableRejected @[expose] public section diff --git a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle017.lean b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle017.lean index ae8442e7c9..c1a587a2e8 100644 --- a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle017.lean +++ b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle017.lean @@ -15,9 +15,9 @@ public import LeanPool.MovingSofa.GerverSofa.KernelOnly.Core.Bundle005 * `GerverSofa.KernelOnly.PartE.Certificates.Batch030`. -/ --- Replay closed rejections with the exact shift-based binary-grid evaluator. +-- Try the bounded Taylor evaluator before full interval replay. attribute [local instance 1100] - GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShiftedGrid.decidableRejected + GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShortTaylor.decidableRejected @[expose] public section diff --git a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle018.lean b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle018.lean index 0c91041d6e..ac53a33a0f 100644 --- a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle018.lean +++ b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle018.lean @@ -15,9 +15,9 @@ public import LeanPool.MovingSofa.GerverSofa.KernelOnly.Core.Bundle005 * `GerverSofa.KernelOnly.PartE.Certificates.Batch031`. -/ --- Replay closed rejections with the exact shift-based binary-grid evaluator. +-- Try the bounded Taylor evaluator before full interval replay. attribute [local instance 1100] - GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShiftedGrid.decidableRejected + GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShortTaylor.decidableRejected @[expose] public section diff --git a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle019.lean b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle019.lean index c32dc61651..bb6640f194 100644 --- a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle019.lean +++ b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle019.lean @@ -15,9 +15,9 @@ public import LeanPool.MovingSofa.GerverSofa.KernelOnly.Core.Bundle005 * `GerverSofa.KernelOnly.PartE.Certificates.Batch032`. -/ --- Replay closed rejections with the exact shift-based binary-grid evaluator. +-- Try the bounded Taylor evaluator before full interval replay. attribute [local instance 1100] - GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShiftedGrid.decidableRejected + GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShortTaylor.decidableRejected @[expose] public section diff --git a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle020.lean b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle020.lean index f701eedf08..d39c695bf5 100644 --- a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle020.lean +++ b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle020.lean @@ -18,9 +18,9 @@ public import LeanPool.MovingSofa.GerverSofa.KernelOnly.Core.Bundle005 * `GerverSofa.KernelOnly.PartE.Certificates.Batch043`. -/ --- Replay closed rejections with the exact shift-based binary-grid evaluator. +-- Try the bounded Taylor evaluator before full interval replay. attribute [local instance 1100] - GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShiftedGrid.decidableRejected + GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShortTaylor.decidableRejected @[expose] public section diff --git a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle021.lean b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle021.lean index 170e56fdc9..a8905b8901 100644 --- a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle021.lean +++ b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle021.lean @@ -18,9 +18,9 @@ public import LeanPool.MovingSofa.GerverSofa.KernelOnly.Core.Bundle005 * `GerverSofa.KernelOnly.PartE.Certificates.Batch034`. -/ --- Replay closed rejections with the exact shift-based binary-grid evaluator. +-- Try the bounded Taylor evaluator before full interval replay. attribute [local instance 1100] - GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShiftedGrid.decidableRejected + GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShortTaylor.decidableRejected @[expose] public section diff --git a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle022.lean b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle022.lean index a471c47e8e..201e20b99c 100644 --- a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle022.lean +++ b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle022.lean @@ -16,9 +16,9 @@ public import LeanPool.MovingSofa.GerverSofa.KernelOnly.Core.Bundle005 * `GerverSofa.KernelOnly.PartE.Certificates.Batch035`. -/ --- Replay closed rejections with the exact shift-based binary-grid evaluator. +-- Try the bounded Taylor evaluator before full interval replay. attribute [local instance 1100] - GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShiftedGrid.decidableRejected + GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShortTaylor.decidableRejected @[expose] public section diff --git a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle023.lean b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle023.lean index 32a08cb157..b30bce5741 100644 --- a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle023.lean +++ b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle023.lean @@ -16,9 +16,9 @@ public import LeanPool.MovingSofa.GerverSofa.KernelOnly.Core.Bundle005 * `GerverSofa.KernelOnly.PartE.Certificates.Batch036`. -/ --- Replay closed rejections with the exact shift-based binary-grid evaluator. +-- Try the bounded Taylor evaluator before full interval replay. attribute [local instance 1100] - GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShiftedGrid.decidableRejected + GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShortTaylor.decidableRejected @[expose] public section diff --git a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle024.lean b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle024.lean index 386eca4a34..1fbe0903f3 100644 --- a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle024.lean +++ b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle024.lean @@ -16,9 +16,9 @@ public import LeanPool.MovingSofa.GerverSofa.KernelOnly.Core.Bundle005 * `GerverSofa.KernelOnly.PartE.Certificates.Batch037`. -/ --- Replay closed rejections with the exact shift-based binary-grid evaluator. +-- Try the bounded Taylor evaluator before full interval replay. attribute [local instance 1100] - GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShiftedGrid.decidableRejected + GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShortTaylor.decidableRejected @[expose] public section diff --git a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle025.lean b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle025.lean index ee0f82ad28..4dfd0a2f0c 100644 --- a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle025.lean +++ b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle025.lean @@ -16,9 +16,9 @@ public import LeanPool.MovingSofa.GerverSofa.KernelOnly.Core.Bundle005 * `GerverSofa.KernelOnly.PartE.Certificates.Batch038`. -/ --- Replay closed rejections with the exact shift-based binary-grid evaluator. +-- Try the bounded Taylor evaluator before full interval replay. attribute [local instance 1100] - GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShiftedGrid.decidableRejected + GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShortTaylor.decidableRejected @[expose] public section diff --git a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle026.lean b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle026.lean index 49080339d4..120075e6c9 100644 --- a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle026.lean +++ b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle026.lean @@ -16,9 +16,9 @@ public import LeanPool.MovingSofa.GerverSofa.KernelOnly.Core.Bundle005 * `GerverSofa.KernelOnly.PartE.Certificates.Batch039`. -/ --- Replay closed rejections with the exact shift-based binary-grid evaluator. +-- Try the bounded Taylor evaluator before full interval replay. attribute [local instance 1100] - GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShiftedGrid.decidableRejected + GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShortTaylor.decidableRejected @[expose] public section diff --git a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle027.lean b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle027.lean index 10394f7e08..9554402022 100644 --- a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle027.lean +++ b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle027.lean @@ -16,9 +16,9 @@ public import LeanPool.MovingSofa.GerverSofa.KernelOnly.Core.Bundle005 * `GerverSofa.KernelOnly.PartE.Certificates.Batch040`. -/ --- Replay closed rejections with the exact shift-based binary-grid evaluator. +-- Try the bounded Taylor evaluator before full interval replay. attribute [local instance 1100] - GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShiftedGrid.decidableRejected + GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShortTaylor.decidableRejected @[expose] public section diff --git a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle028.lean b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle028.lean index dc5afe6b07..2452675adc 100644 --- a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle028.lean +++ b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle028.lean @@ -17,9 +17,9 @@ public import LeanPool.MovingSofa.GerverSofa.KernelOnly.Core.Bundle007 * `GerverSofa.KernelOnly.PartE.Certificates.Batch041`. -/ --- Replay closed rejections with the exact shift-based binary-grid evaluator. +-- Try the bounded Taylor evaluator before full interval replay. attribute [local instance 1100] - GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShiftedGrid.decidableRejected + GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShortTaylor.decidableRejected @[expose] public section diff --git a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle029.lean b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle029.lean index 85aaac2b71..010b68138b 100644 --- a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle029.lean +++ b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle029.lean @@ -16,9 +16,9 @@ public import LeanPool.MovingSofa.GerverSofa.KernelOnly.Core.Bundle005 * `GerverSofa.KernelOnly.PartE.Certificates.Batch042`. -/ --- Replay closed rejections with the exact shift-based binary-grid evaluator. +-- Try the bounded Taylor evaluator before full interval replay. attribute [local instance 1100] - GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShiftedGrid.decidableRejected + GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShortTaylor.decidableRejected @[expose] public section diff --git a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle030.lean b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle030.lean index 186a3d48bd..d222e8cb75 100644 --- a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle030.lean +++ b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/Certificates/Bundle030.lean @@ -60,9 +60,9 @@ public import LeanPool.MovingSofa.GerverSofa.KernelOnly.Core.Bundle007 * `GerverSofa.KernelOnly.PartE.Certificates.Reconstruction`. -/ --- Replay closed rejections with the exact shift-based binary-grid evaluator. +-- Try the bounded Taylor evaluator before full interval replay. attribute [local instance 1100] - GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShiftedGrid.decidableRejected + GerverSofa.PartE.FixedPointResiduals.BinaryGrid.ShortTaylor.decidableRejected @[expose] public section diff --git a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/FixedPointResiduals.lean b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/FixedPointResiduals.lean index cff878b885..f42c30b305 100644 --- a/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/FixedPointResiduals.lean +++ b/LeanPool/MovingSofa/GerverSofa/KernelOnly/PartE/FixedPointResiduals.lean @@ -1259,6 +1259,374 @@ private theorem rejects_eq (cell : AngleCell) : rejects cell = BinaryGrid.reject end ShiftedGrid + +namespace ShortTaylor + +/-! +For angles in `[0, 1]`, the squared argument also lies in `[0, 1]`. Evaluate the +last three cosine coefficients and the last two sine coefficients on that whole +interval once. The resulting bounds serve as the initial accumulators for the +three Horner steps that still depend on the cell. The outward rounding of the +Taylor remainder at one radian is also a valid bound for every smaller angle. +-/ + +private theorem covers_add {outerLeft outerRight innerLeft innerRight : IntervalRat} + (left : Covers outerLeft innerLeft) (right : Covers outerRight innerRight) : + Covers (outerLeft.add outerRight) (innerLeft.add innerRight) := + ⟨add_le_add left.lower right.lower, add_le_add left.upper right.upper⟩ + +private theorem covers_taylor (coefficients : List ℚ) {outer inner : IntervalRat} + (covered : Covers outer inner) : + Covers (evalTaylorSeries coefficients outer) (evalTaylorSeries coefficients inner) := by + induction coefficients with + | nil => exact Covers.refl _ + | cons coefficient rest ih => + exact covers_add (Covers.refl _) (ih.mul covered) + +private def unitInterval : IntervalRat := ⟨0, 1, by decide⟩ + +private theorem unitInterval_covers_square {input : IntervalRat} (nonnegative : 0 ≤ input.lo) + (atMostOne : input.hi ≤ 1) : Covers unitInterval (input.mul input) := by + have upperNonnegative := nonnegative.trans input.le + have lowerAtMostOne := input.le.trans atMostOne + have product_le_one {a b : ℚ} (ha : a ≤ 1) (hb : b ≤ 1) (hb0 : 0 ≤ b) : + a * b ≤ 1 := (mul_le_mul_of_nonneg_right ha hb0).trans (by simpa only [one_mul] using hb) + constructor + · simp only [unitInterval, IntervalRat.mul, IntervalRat.min4, le_min_iff] + exact ⟨⟨mul_nonneg nonnegative nonnegative, mul_nonneg nonnegative upperNonnegative⟩, + ⟨mul_nonneg upperNonnegative nonnegative, mul_nonneg upperNonnegative upperNonnegative⟩⟩ + · simp only [unitInterval, IntervalRat.mul, IntervalRat.max4, max_le_iff] + exact ⟨⟨product_le_one lowerAtMostOne lowerAtMostOne nonnegative, + product_le_one lowerAtMostOne atMostOne upperNonnegative⟩, + ⟨product_le_one atMostOne lowerAtMostOne nonnegative, + product_le_one atMostOne atMostOne upperNonnegative⟩⟩ + +/-- Horner iteration starting from an enclosure of the higher-degree terms. -/ +def horner (leading : List SI) (tailBounds square : SI) : SI := + leading.foldr (fun coefficient accumulator => + SI.add coefficient (ShiftedGrid.mulRight accumulator square)) + tailBounds + +private theorem horner_encloses (leading tail : List ℚ) {square tailBounds : SI} + {inner : IntervalRat} (squareEnclosed : Encloses square inner) + (nonnegative : 0 ≤ square.lo) + (tailEnclosed : Encloses tailBounds (evalTaylorSeries tail inner)) : + Encloses (horner (leading.map point) tailBounds square) + (evalTaylorSeries (leading ++ tail) inner) := by + induction leading with + | nil => exact tailEnclosed + | cons coefficient rest ih => + change Encloses (SI.add (point coefficient) (ShiftedGrid.mulRight _ _)) _ + rw [ShiftedGrid.mulRight_eq_mul _ _ nonnegative] + exact (point_encloses coefficient).add (ih.mul squareEnclosed) + +/-- Product for a nonpositive left interval and nonnegative right interval. -/ +def mulNonpositive (left right : SI) : SI := + ShiftedGrid.round (left.lo * right.hi) (left.hi * right.lo) + +/-- Product of two nonnegative intervals. -/ +def mulNonnegative (left right : SI) : SI := + ShiftedGrid.round (left.lo * right.lo) (left.hi * right.hi) + +private theorem mulNonpositive_eq (left right : SI) (lo : left.lo < 0) (hi : left.hi < 0) : + mulNonpositive left right = ShiftedGrid.mulRight left right := by + simp only [mulNonpositive, ShiftedGrid.mulRight, Bool.cond_eq_ite, ShiftedGrid.nonnegative_spec, + ite_eq_right (not_le.mpr lo), ite_eq_right (not_le.mpr hi)] + +private theorem mulNonnegative_eq (left right : SI) (lo : 0 ≤ left.lo) (hi : 0 ≤ left.hi) : + mulNonnegative left right = ShiftedGrid.mulRight left right := by + simp only [mulNonnegative, ShiftedGrid.mulRight, Bool.cond_eq_ite, ShiftedGrid.nonnegative_spec, + ite_eq_left lo, ite_eq_left hi] + +private theorem lower_round (bound lo hi : ℤ) (h : bound * denominator ≤ lo) : + bound ≤ (ShiftedGrid.round lo hi).lo := by + change bound ≤ lo >>> (30 : Nat) + rw [ShiftedGrid.shift_eq] + have positive : 0 < denominator := by decide + simpa only [Int.mul_ediv_cancel _ positive.ne'] using Int.ediv_le_ediv positive h + +private theorem upper_round (bound lo hi : ℤ) (h : hi ≤ bound * denominator) : + (ShiftedGrid.round lo hi).hi ≤ bound := by + change -((-hi) >>> (30 : Nat)) ≤ bound + rw [ShiftedGrid.shift_eq, neg_le] + have positive : 0 < denominator := by decide + simpa only [← neg_mul, Int.mul_ediv_cancel _ positive.ne'] using + Int.ediv_le_ediv positive (neg_le_neg h) + +/-- Three Horner steps for the successive negative, positive and negative accumulators. -/ +def hornerThree (constantTerm linear quadratic tailBounds square : SI) : SI := + let high := SI.add quadratic (mulNonpositive tailBounds square) + let middle := SI.add linear (mulNonnegative high square) + SI.add constantTerm (mulNonpositive middle square) + +private theorem hornerThree_eq (constantTerm linear quadratic tailBounds square : SI) + (squareNonnegative : 0 ≤ square.lo) (squareOrdered : square.lo ≤ square.hi) + (squareAtMostOne : square.hi ≤ denominator) + (tailLo : tailBounds.lo < 0) (tailHi : tailBounds.hi < 0) + (positiveLo : 0 ≤ quadratic.lo + tailBounds.lo) + (positiveHi : 0 ≤ quadratic.hi + tailBounds.hi) + (quadraticOrdered : quadratic.lo ≤ quadratic.hi) (quadraticHi : 0 ≤ quadratic.hi) + (negativeLo : linear.lo + quadratic.hi < 0) + (negativeHi : linear.hi + quadratic.hi < 0) : + hornerThree constantTerm linear quadratic tailBounds square = + horner [constantTerm, linear, quadratic] tailBounds square := by + let high := SI.add quadratic (mulNonpositive tailBounds square) + let middle := SI.add linear (mulNonnegative high square) + have highLo : 0 ≤ high.lo := positiveLo.trans (add_le_add_right (lower_round _ _ _ + (mul_le_mul_of_nonpos_left squareAtMostOne tailLo.le)) _) + have highHi : 0 ≤ high.hi := by + apply positiveHi.trans + apply add_le_add_right + change tailBounds.hi ≤ -((- (tailBounds.hi * square.lo)) >>> (30 : Nat)) + rw [ShiftedGrid.shift_eq, le_neg] + have positive : 0 < denominator := by decide + have product : -(tailBounds.hi * square.lo) ≤ (-tailBounds.hi) * denominator := by + rw [neg_mul] + exact neg_le_neg (mul_le_mul_of_nonpos_left (squareOrdered.trans squareAtMostOne) tailHi.le) + have h := Int.ediv_le_ediv positive product + rwa [Int.mul_ediv_cancel _ positive.ne'] at h + have highBound : high.hi ≤ quadratic.hi := by + change quadratic.hi + (ShiftedGrid.round _ _).hi ≤ quadratic.hi + exact add_le_of_nonpos_right (upper_round 0 _ _ (by + rw [zero_mul] + exact mul_nonpos_of_nonpos_of_nonneg tailHi.le squareNonnegative)) + have squareHiNonnegative := squareNonnegative.trans squareOrdered + have middleHi : middle.hi < 0 := by + apply lt_of_le_of_lt _ negativeHi + apply add_le_add_right + exact upper_round _ _ _ (mul_le_mul highBound squareAtMostOne squareHiNonnegative quadraticHi) + have highLoBound : high.lo ≤ quadratic.hi := by + change quadratic.lo + (ShiftedGrid.round _ _).lo ≤ quadratic.hi + have h : (ShiftedGrid.round (tailBounds.lo * square.hi) 0).lo ≤ 0 := by + change (tailBounds.lo * square.hi) >>> (30 : Nat) ≤ 0 + rw [ShiftedGrid.shift_eq] + have positive : 0 < denominator := by decide + simpa only [Int.zero_ediv] using Int.ediv_le_ediv positive + (mul_nonpos_of_nonpos_of_nonneg tailLo.le squareHiNonnegative) + exact (add_le_of_nonpos_right h).trans quadraticOrdered + have middleLo : middle.lo < 0 := by + apply lt_of_le_of_lt _ negativeLo + apply add_le_add_right + change (ShiftedGrid.round _ _).lo ≤ quadratic.hi + have positive : 0 < denominator := by decide + change (high.lo * square.lo) >>> (30 : Nat) ≤ quadratic.hi + rw [ShiftedGrid.shift_eq] + have h := Int.ediv_le_ediv positive + (mul_le_mul highLoBound (squareOrdered.trans squareAtMostOne) + squareNonnegative quadraticHi) + rwa [Int.mul_ediv_cancel _ positive.ne'] at h + change SI.add constantTerm (mulNonpositive middle square) = _ + rw [mulNonpositive_eq _ _ middleLo middleHi] + change constantTerm.add + (ShiftedGrid.mulRight (linear.add (mulNonnegative high square)) square) = _ + rw [mulNonnegative_eq _ _ highLo highHi] + change constantTerm.add (ShiftedGrid.mulRight (linear.add (ShiftedGrid.mulRight + (quadratic.add (mulNonpositive tailBounds square)) square)) square) = _ + rw [mulNonpositive_eq _ _ tailLo tailHi] + rfl + +private theorem cosineTail : + Encloses ⟨-1491309, -1464677⟩ (evalTaylorSeries [-1 / 720, 1 / 40320, -1 / 3628800] + unitInterval) := by + unfold Encloses + decide +kernel + +private theorem sineTail : + Encloses ⟨-213045, -210084⟩ (evalTaylorSeries [-1 / 5040, 1 / 362880] unitInterval) := by + unfold Encloses + decide +kernel + +/-- Bound the trigonometric intervals using three cell-dependent Horner steps. -/ +def trig (input : SI) : SI × SI := + let square := ShiftedGrid.mulRight input input + let error : SI := ⟨-27, 27⟩ + let cosine := hornerThree ⟨1073741824, 1073741825⟩ ⟨-536870912, -536870911⟩ + ⟨44739242, 44739243⟩ ⟨-1491309, -1464677⟩ square + let sine := hornerThree ⟨1073741824, 1073741825⟩ ⟨-178956971, -178956970⟩ + ⟨8947848, 8947849⟩ ⟨-213045, -210084⟩ square + (SI.add cosine error, SI.add (ShiftedGrid.mulRight sine input) error) + +private theorem trig_encloses {input : SI} {interval : IntervalRat} + (enclosed : Encloses input interval) (positive : 0 < input.lo) + (atMostOne : input.hi ≤ denominator) : + Encloses (trig input).1 (cosComputable interval 10) ∧ + Encloses (trig input).2 (sinComputable interval 10) := by + have rationalPositive : 0 < interval.lo := + (div_pos (by exact_mod_cast positive) denominator_pos).trans_le enclosed.1 + have rationalAtMostOne : interval.hi ≤ 1 := enclosed.2.trans + ((div_le_one denominator_pos).2 (by exact_mod_cast atMostOne)) + have squareEnclosed : Encloses (ShiftedGrid.mulRight input input) (interval.mul interval) := by + rw [ShiftedGrid.mulRight_eq_mul _ _ positive.le] + exact enclosed.mul enclosed + have squareNonnegative := ShiftedGrid.mulRight_lower_nonnegative positive.le positive.le + have squareCovered := unitInterval_covers_square rationalPositive.le rationalAtMostOne + have cosine := horner_encloses [1, -1 / 2, 1 / 24] [-1 / 720, 1 / 40320, -1 / 3628800] + squareEnclosed squareNonnegative (cosineTail.trans (covers_taylor _ squareCovered)) + have sine := horner_encloses [1, -1 / 6, 1 / 120] [-1 / 5040, 1 / 362880] + squareEnclosed squareNonnegative (sineTail.trans (covers_taylor _ squareCovered)) + have cosineLeading : ([1, -1 / 2, 1 / 24] : List ℚ).map point = + [⟨1073741824, 1073741825⟩, ⟨-536870912, -536870911⟩, ⟨44739242, 44739243⟩] := by + decide +kernel + have sineLeading : ([1, -1 / 6, 1 / 120] : List ℚ).map point = + [⟨1073741824, 1073741825⟩, ⟨-178956971, -178956970⟩, ⟨8947848, 8947849⟩] := by + decide +kernel + rw [cosineLeading] at cosine + rw [sineLeading] at sine + have unitEnclosed : Encloses ⟨0, denominator⟩ interval := by + constructor + · simpa only [Int.cast_zero, zero_div] using rationalPositive.le + · simpa only [div_self denominator_pos.ne'] using rationalAtMostOne + have errorBounds : remainder denominator = ⟨-27, 27⟩ := by decide +kernel + have remainder := remainder_encloses unitEnclosed rationalPositive.le + rw [errorBounds] at remainder + rw [← FastIntervals.cosineSparse_eq, ← FastIntervals.sineSparse_eq] + simp only [FastIntervals.cosineSparse, FastIntervals.sineSparse, ite_eq_left rationalPositive, + FastIntervals.multiplyForTaylor_eq_mul, FastIntervals.evalTaylor_eq_evalTaylorSeries] + change Encloses _ (FastIntervals.clip _) ∧ Encloses _ (FastIntervals.clip _) + have squareAtMostOne : (ShiftedGrid.mulRight input input).hi ≤ denominator := by + have hiNonnegative := positive.le.trans enclosed.ordered + simp only [ShiftedGrid.mulRight, Bool.cond_eq_ite, ShiftedGrid.nonnegative_spec, + ite_eq_left hiNonnegative] + exact upper_round _ _ _ (mul_le_mul atMostOne atMostOne hiNonnegative (by decide)) + have cosineHorner := hornerThree_eq ⟨1073741824, 1073741825⟩ ⟨-536870912, -536870911⟩ + ⟨44739242, 44739243⟩ ⟨-1491309, -1464677⟩ _ squareNonnegative squareEnclosed.ordered + squareAtMostOne (by decide) (by decide) (by decide) (by decide) (by decide) + (by decide) (by decide) (by decide) + have sineHorner := hornerThree_eq ⟨1073741824, 1073741825⟩ ⟨-178956971, -178956970⟩ + ⟨8947848, 8947849⟩ ⟨-213045, -210084⟩ _ squareNonnegative squareEnclosed.ordered + squareAtMostOne (by decide) (by decide) (by decide) (by decide) (by decide) + (by decide) (by decide) (by decide) + simp only [trig] + rw [cosineHorner, sineHorner] + simp only [ShiftedGrid.mulRight_eq_mul _ _ positive.le] + simp only [ShiftedGrid.mulRight_eq_mul _ _ positive.le] at cosine sine + exact ⟨(cosine.add remainder).trans (covers_clip _), + ((sine.mul enclosed).add remainder).trans (covers_clip _)⟩ + +/-- Divide the grid endpoints by two with outward rounding. -/ +def half (x : SI) : SI := ⟨x.lo >>> (1 : Nat), -((-x.hi) >>> (1 : Nat))⟩ + +/-- Divide the grid endpoints by four with outward rounding. -/ +def quarter (x : SI) : SI := ⟨x.lo >>> (2 : Nat), -((-x.hi) >>> (2 : Nat))⟩ + +private theorem half_eq (x : SI) : half x = ShiftedGrid.scale (1 / 2) x := by + have numerator : ((1 / 2 : ℚ)).num = 1 := by decide +kernel + have divisor : ((1 / 2 : ℚ)).den = 2 := by decide +kernel + simp only [half, ShiftedGrid.scale, numerator, divisor, one_mul] + +private theorem quarter_eq (x : SI) : quarter x = ShiftedGrid.scale (1 / 4) x := by + have numerator : ((1 / 4 : ℚ)).num = 1 := by decide +kernel + have divisor : ((1 / 4 : ℚ)).den = 4 := by decide +kernel + simp only [quarter, ShiftedGrid.scale, numerator, divisor, one_mul] + +private theorem scaleInteger_eq (n : ℕ) (x : SI) : + ShiftedGrid.scale (n : ℚ) x = ⟨n * x.lo, n * x.hi⟩ := rfl + +/-- Evaluate the scaled residuals with their fixed binary-grid constants +and power-of-two divisors. -/ +def residuals (phi theta cosinePhi sinePhi cosineTheta sineTheta : SI) : SI × SI := + let multiply := ShiftedGrid.mul + let subtract := fun a b => SI.add a (SI.neg b) + let twice := fun (x : SI) => (⟨2 * x.lo, 2 * x.hi⟩ : SI) + let triple := fun (x : SI) => (⟨3 * x.lo, 3 * x.hi⟩ : SI) + let one : SI := ⟨1073741824, 1073741825⟩ + let oneHalf : SI := ⟨536870912, 536870913⟩ + let halfPi : SI := ⟨1686629713, 1686629714⟩ + let delta := subtract theta phi + let halfDelta := half delta + let base := SI.add (SI.add (subtract (subtract halfPi phi) theta) halfDelta) + (quarter (multiply delta delta)) + let onePlusHalfDelta := SI.add one halfDelta + let denominator := subtract cosinePhi (multiply onePlusHalfDelta sinePhi) + let numerator := SI.add (subtract (SI.add sinePhi oneHalf) (half cosinePhi)) + (multiply base sinePhi) + let twiceNumeratorB := twice (SI.add (multiply numerator onePlusHalfDelta) + (multiply base denominator)) + let deltaMinusOne := subtract delta one + let cosinePhiProduct := multiply cosinePhi denominator + let sinePhiProduct := multiply sinePhi denominator + let first := SI.add + (SI.add (SI.add (SI.add + (subtract (multiply numerator (subtract cosineTheta cosinePhi)) + (multiply twiceNumeratorB sinePhi)) + (multiply (multiply deltaMinusOne cosineTheta) denominator)) + (SI.neg (multiply sineTheta denominator))) + cosinePhiProduct) sinePhiProduct + let second := SI.add + (SI.add (SI.add (SI.add + (subtract (multiply numerator (SI.add (triple sineTheta) sinePhi)) + (multiply twiceNumeratorB cosinePhi)) + (multiply (multiply (triple deltaMinusOne) sineTheta) denominator)) + (multiply (triple cosineTheta) denominator)) + (SI.neg sinePhiProduct)) cosinePhiProduct + (first, second) + +private theorem residuals_eq (phi theta cosinePhi sinePhi cosineTheta sineTheta : SI) : + residuals phi theta cosinePhi sinePhi cosineTheta sineTheta = + residualsFromGrid point (ofRat kernelPiInterval) ShiftedGrid.scale ShiftedGrid.mul + phi theta cosinePhi sinePhi cosineTheta sineTheta := by + have onePoint : point 1 = ⟨1073741824, 1073741825⟩ := by decide +kernel + have halfPoint : point (1 / 2) = ⟨536870912, 536870913⟩ := by decide +kernel + have halfPi : ShiftedGrid.scale (1 / 2) (ofRat kernelPiInterval) = + ⟨1686629713, 1686629714⟩ := by decide +kernel + have double (x : SI) : ShiftedGrid.scale 2 x = ⟨2 * x.lo, 2 * x.hi⟩ := + scaleInteger_eq 2 x + have triple (x : SI) : ShiftedGrid.scale 3 x = ⟨3 * x.lo, 3 * x.hi⟩ := + scaleInteger_eq 3 x + simp only [residuals, residualsFromGrid, half_eq, quarter_eq, onePoint, halfPoint, halfPi, + double, triple] + +/-- Try bounded Taylor replay before falling back to full-degree evaluation. -/ +def rejects (cell : AngleCell) : Bool := + let phi := ofRat cell.phiI + let theta := ofRat cell.thetaI + bif ShiftedGrid.positive phi.lo && ShiftedGrid.positive theta.lo && + ShiftedGrid.endpointLE phi.hi denominator && ShiftedGrid.endpointLE theta.hi denominator then + let phiTrig := trig phi + let thetaTrig := trig theta + let bounds := residuals phi theta phiTrig.1 phiTrig.2 thetaTrig.1 thetaTrig.2 + ShiftedGrid.separated bounds.1 || ShiftedGrid.separated bounds.2 + else false + +private theorem rejected_of_check (cell : AngleCell) (checked : rejects cell = true) : + cell.rejected = true := by + unfold rejects at checked + simp only [Bool.cond_eq_ite, Bool.and_eq_true, ShiftedGrid.positive_spec, + ShiftedGrid.endpointLE_spec] at checked + split at checked + next angles => + let phi := ofRat_encloses cell.phiI + let theta := ofRat_encloses cell.thetaI + have phiTrig := trig_encloses phi angles.1.1.1 angles.1.2 + have thetaTrig := trig_encloses theta angles.1.1.2 angles.2 + have scaleFunction : ShiftedGrid.scale = scaleNonnegative := + funext fun value => funext (ShiftedGrid.scale_eq value) + have mulFunction : ShiftedGrid.mul = BinaryGrid.mul := + funext fun left => funext (ShiftedGrid.mul_eq left) + simp only [residuals_eq, scaleFunction, mulFunction, ShiftedGrid.separated_eq, + Bool.or_eq_true] at checked + have covered := residualsFromGrid_encloses point (ofRat kernelPiInterval) + scaleNonnegative mul point_encloses (ofRat_encloses _) Encloses.scaleNonnegative + Encloses.add Encloses.neg + Encloses.mul phi theta phiTrig.1 phiTrig.2 thetaTrig.1 thetaTrig.2 + have original : FastIntervals.rejectWith IntervalRat.mul cell + (cosComputable cell.phiI 10) (sinComputable cell.phiI 10) + (cosComputable cell.thetaI 10) (sinComputable cell.thetaI 10) = + cell.rejected := rfl + rw [← original] + simp only [FastIntervals.rejectWith, Bool.or_eq_true] + exact checked.imp covered.1.excludesZero covered.2.excludesZero + next => cases checked + +/-- Decide a rejection first with the bounded Taylor intervals and then with the full evaluator. -/ +@[instance_reducible] def decidableRejected (cell : AngleCell) : + Decidable (cell.rejected = true) := + if checked : rejects cell = true then + isTrue (by exact rejected_of_check cell checked) + else ShiftedGrid.decidableRejected cell + +end ShortTaylor + end BinaryGrid end GerverSofa.PartE.FixedPointResiduals