{-# OPTIONS --safe --cubical-compatible --guardedness --exact-split --double-check #-}
module Real.Interval.Pi.Core where
open import Data.Nat using (ℕ; suc; _∸_) renaming (_≤_ to _≤ℕ_; _+_ to _+ℕ_)
open import Data.Nat.Properties using
(≤-trans; m+[n∸m]≡n; +-suc; m≤m+n; +-identityˡ)
open import Data.Integer using (+_)
open import Data.Rational using (ℚ; 0ℚ; 1ℚ; _≤_; _<_; _+_; _-_; _*_; _/_)
open import Data.Rational.Properties using
(≤-refl; <-≤-trans; +-assoc; +-comm; *-identityˡ; <ᵇ⇒<)
open import Data.Unit using (tt)
open import Data.Product using (proj₁; _,_)
open import Relation.Binary.PropositionalEquality using
(_≡_; refl; sym; trans; cong; subst)
open import Base.RationalRing using (0≤1; lt-shift)
open import Real using (CauchySeq)
open import Real.CauchyToInterval using (M; μ≤M; K; node-go)
open import Real.IntervalEquiv using (l; u)
open import Real.Interval using (RealI; embedℚ; δ)
open import Real.Interval.Arith using (0ℝ)
open import Real.Interval.Atan.Core using
(atanTerm; atanTerm-dec; atanTerm-nonNeg; sign; altSum; shift; altSum-bounds
; atanSeq; atanq)
open import Real.Interval.Continuity using (0<embedℚ)
open import Real.Interval.Equiv using (≈ℝ-sym)
open import Real.Interval.Embed using (embedℚ-*)
open import Real.Interval.Mul using (_*ℝ_)
open import Real.Interval.Mul.Laws using (*ℝ-comm)
open import Real.Interval.Mul.Order using (<-*mono; ≈-<-respʳ)
open import Real.Interval.Node using (u-dyadic-k)
open import Real.Interval.Order using (_<ℝ_)
open import Real.Interval.OrderLaws using (≈-<-resp)
open import Real.Interval.Series.Bounds using (le-self-add)
atanPartial : ℕ → ℚ
atanPartial d = altSum (atanTerm 1ℚ) d
term1 : ℕ → ℚ
term1 n = atanTerm 1ℚ n
one-le-one : 1ℚ ≤ 1ℚ
one-le-one = ≤-refl
atan1 : RealI
atan1 = atanq 1ℚ 0≤1 one-le-one
sample : CauchySeq
sample = atanSeq 1ℚ 0≤1 one-le-one
sign-8 : sign 8 ≡ 1ℚ
sign-8 = refl
tail-dec : (j : ℕ) → term1 (8 +ℕ suc j) ≤ term1 (8 +ℕ j)
tail-dec j =
subst (λ z → term1 z ≤ term1 (8 +ℕ j)) (sym (+-suc 8 j))
(atanTerm-dec 1ℚ 0≤1 one-le-one (8 +ℕ j))
tail-nonNeg : (d : ℕ) → 0ℚ ≤ altSum (λ j → term1 (8 +ℕ j)) d
tail-nonNeg d =
proj₁ (altSum-bounds (λ j → term1 (8 +ℕ j)) tail-dec
(λ j → atanTerm-nonNeg 1ℚ 0≤1 (8 +ℕ j)) d)
shift-8 : (d : ℕ) →
atanPartial (8 +ℕ d) ≡ atanPartial 8 + altSum (λ j → term1 (8 +ℕ j)) d
shift-8 d =
trans (shift (atanTerm 1ℚ) 8 d)
(cong (_+_ (atanPartial 8))
(trans (cong (_* altSum (λ j → term1 (8 +ℕ j)) d) sign-8)
(*-identityˡ (altSum (λ j → term1 (8 +ℕ j)) d))))
partial-mono-from-8 : (d : ℕ) → atanPartial 8 ≤ atanPartial (8 +ℕ d)
partial-mono-from-8 d =
subst (λ z → atanPartial 8 ≤ z) (sym (shift-8 d))
(le-self-add {p = atanPartial 8}
{q = altSum (λ j → term1 (8 +ℕ j)) d} (tail-nonNeg d))
partial-mono : (D' : ℕ) → 8 ≤ℕ D' → atanPartial 8 ≤ atanPartial D'
partial-mono D' 8≤D' =
subst (λ z → atanPartial 8 ≤ atanPartial z)
(m+[n∸m]≡n {8} {D'} 8≤D') (partial-mono-from-8 (D' ∸ 8))
closed-bound : ℚ
closed-bound = (+ 3 / 4) + (δ 9 + δ 9)
closed-bound<8 : closed-bound < atanPartial 8
closed-bound<8 = <ᵇ⇒< tt
K-9 : K 9 ≡ 2048
K-9 = refl
2049≤sample : 2049 ≤ℕ M sample 9
2049≤sample =
subst (λ z → suc z ≤ℕ M sample 9) (sym K-9) (μ≤M sample 9)
8≤sample : 8 ≤ℕ M sample 9
8≤sample = ≤-trans (m≤m+n 8 2041) 2049≤sample
reassoc : closed-bound ≡ δ 9 + (δ 9 + (+ 3 / 4))
reassoc =
trans (+-comm (+ 3 / 4) (δ 9 + δ 9))
(sym (+-assoc (δ 9) (δ 9) (+ 3 / 4)))
rational-bound : δ 9 + (+ 3 / 4) < atanPartial (M sample 9) - δ 9
rational-bound =
let
h : closed-bound < atanPartial (M sample 9)
h = <-≤-trans closed-bound<8 (partial-mono (M sample 9) 8≤sample)
h' : δ 9 + (δ 9 + (+ 3 / 4)) < atanPartial (M sample 9)
h' = subst (λ z → z < atanPartial (M sample 9)) reassoc h
h'' : (δ 9 + (+ 3 / 4)) + δ 9 < atanPartial (M sample 9)
h'' = subst (λ z → z < atanPartial (M sample 9))
(+-comm (δ 9) (δ 9 + (+ 3 / 4))) h'
in lt-shift (δ 9 + (+ 3 / 4)) (atanPartial (M sample 9)) (δ 9) h''
l-atan : (n : ℕ) → l atan1 n ≡ atanPartial (M sample n) - δ n
l-atan n = trans (cong RealI.l (node-go sample n)) refl
l-atan-9 = l-atan 9
u-embed : u (embedℚ (+ 3 / 4)) 9 ≡ δ 9 + (+ 3 / 4)
u-embed =
trans (u-dyadic-k (+ 3 / 4) 0 9)
(cong (λ z → z + (+ 3 / 4)) (cong δ (+-identityˡ 9)))
three-quarters<atan1 : embedℚ (+ 3 / 4) <ℝ atan1
three-quarters<atan1 =
let
h : u (embedℚ (+ 3 / 4)) 9 < l atan1 9
h = subst (λ z → z < l atan1 9) u-embed
(subst (λ z → δ 9 + (+ 3 / 4) < z) (sym l-atan-9)
rational-bound)
in 9 , h
pi : RealI
pi = embedℚ (+ 4 / 1) *ℝ atan1
four-positive : 0ℝ <ℝ embedℚ (+ 4 / 1)
four-positive = 0<embedℚ (+ 4 / 1) (<ᵇ⇒< tt)
scaled : embedℚ (+ 3 / 4) *ℝ embedℚ (+ 4 / 1)
<ℝ atan1 *ℝ embedℚ (+ 4 / 1)
scaled = <-*mono (embedℚ (+ 3 / 4)) atan1 (embedℚ (+ 4 / 1))
three-quarters<atan1 four-positive
three-quarters-times-four : (+ 3 / 4) * (+ 4 / 1) ≡ (+ 3 / 1)
three-quarters-times-four = refl
left-endpoint : embedℚ ((+ 3 / 4) * (+ 4 / 1)) <ℝ atan1 *ℝ embedℚ (+ 4 / 1)
left-endpoint =
≈-<-resp (≈ℝ-sym (embedℚ-* (+ 3 / 4) (+ 4 / 1))) scaled
three-quarters<pi : embedℚ (+ 3 / 1) <ℝ atan1 *ℝ embedℚ (+ 4 / 1)
three-quarters<pi =
subst (λ q → embedℚ q <ℝ atan1 *ℝ embedℚ (+ 4 / 1))
three-quarters-times-four left-endpoint
3ℝ : RealI
3ℝ = embedℚ (+ 3 / 1)
three<pi : 3ℝ <ℝ pi
three<pi = ≈-<-respʳ (*ℝ-comm atan1 (embedℚ (+ 4 / 1))) three-quarters<pi