{- | The exposition presented here follows Herman Geuvers, Freek Wiedijk, Jan Zwanenburg paper
- "A Constructive Proof of the Fundamental Theorem of Algebra without using the Rationals"
- see https://www.cs.ru.nl/~herman/PUBS/Types00-GeuversWiedijkZwanenburg.pdf
-}
\import Algebra.Group
\import Algebra.Meta
\import Algebra.Monoid
\import Algebra.Ordered
\import Algebra.Pointed
\import Algebra.Ring
\import Algebra.Ring.Poly
\import Algebra.Semiring
\import Algebra.StrictlyOrdered
\import Arith.Complex
\import Arith.Complex.Norm
\import Arith.Complex.Root
\import Arith.Fin.Order
\import Arith.Nat
\import Arith.Rat
\import Arith.Real
\import Arith.Real.Field
\import Arith.Real.Root
\import Data.Array
\import Data.Fin
\import Function.Meta
\import Logic
\import Logic.Meta
\import Meta
\import Order.Biordered
\import Order.Lattice
\import Order.LinearOrder
\import Order.PartialOrder
\import Order.StrictOrder
\import Paths
\import Paths.Meta
\import Set.Fin
\open Monoid
\open AddMonoid
\open OrderedField
\open OrderedSemiring
\private \meta one/3 => (ratio 1 3 : Real)
\private \lemma one/3>0 : one/3 > {RealField} 0 => linarith
\private \lemma one/3>=0 : one/3 >= {RealField} 0 => <=-less one/3>0
\private \lemma one/3<1 : one/3 < {RealField} 1 => linarith
\private \lemma one/2>=0 : ratio 1 2 >= {RealField} zro => linarith
\private \lemma pow1/3-strict-monotone {i k : Nat} (p : k < i) : pow one/3 i < pow one/3 k
=> OrderedSemiring.pow_<-degree {_} {one/3} one/3>0 one/3<1 p
\private \lemma pow1/3-monotone {i k : Nat} (p : k <= i) : pow one/3 i <= pow one/3 k
=> PosetSemiring.pow_<=-degree one/3>=0 (<=-less one/3<1) p
\lemma MainLemma {n : Nat} (n>0 : n > 0) {eps : Real} (eps>0 : eps > 0)
(a : Nat -> Real)
(a0>eps : a 0 > eps)
(ai>=0 : \Pi (i : Nat) -> a i >= 0)
(an=1 : a n = 1)
: ∃ (r : Real) (r>0 : r > 0) (k : Fin n) (pow r n < a 0)
(pow one/3 (2 * n * n) * a 0 - 2 * eps < a (suc k) * pow r (suc k))
(a (suc k) * pow r (suc k) < a 0)
(BigSum (\new Array Real k (\lam i => a (i Nat.+ 1) * pow r (i + 1))) +
BigSum (\new Array Real (pred n -' k) (\lam i => a (k Nat.+ i + 2) * pow r (k Nat.+ i + 2) ))
< a (suc k) * pow r (suc k) * (Real.fromRat 1 - pow one/3 n) + eps * pow 3 n) \elim n
| 0 => absurd \case n>0
| suc n-1 \as n =>
\let
| (inP (r0, r0>0, kSeq, k_mono, C1, C2)) => KeyLemma n>0 eps>0 a a0>eps ai>=0 an=1
| (inP (j, eq1, eq2)) => three-consecutive-equals {n-1} {2 * n-1 + 1} (usingOnly () linarith) kSeq k_mono
| k : Fin n => kSeq j
| r : Real => r0 * pow one/3 (suc j)
| k0 : Fin n => kSeq 0
| arr-1/3^i+1 => \new Array RealField (n-1 -' k) (\lam i => pow one/3 (i Nat.+ 1))
| arr-1/3^k+i+2 => \new Array RealField (n-1 -' k) (\lam i => pow one/3 (k Nat.+ i + 2))
| arr-1/3^k-i => \new Array RealField k (\lam i => pow one/3 (k -' i))
| arr-3^i+1 => \new Array RealField k (\lam i => pow 3 (suc i))
\in \have
| r>0 : r > 0
=> LinearlyOrderedSemiring.<_*_positive_positive r0>0 (LinearlyOrderedSemiring.pow>0 {_} one/3>0 {j + 1})
| r<=r0 : r <= r0
=> transport (_ <=) ide-right $ PosetSemiring.<=_*_positive-right (<=-less r0>0) $ PosetSemiring.pow<=1 one/3>=0 (usingOnly () linarith) {j + 1}
| a0_minus_eps_>0 : a 0 - eps > 0 => usingOnly a0>eps linarith
| -2eps=-eps-eps x : x - (natCoef 2 * eps) = x - eps - eps => usingOnly () linarith
| three_times_one_third : (3 : Real) * one/3 = 1 => usingOnly () linarith
| third^m*3^m=1 m : pow one/3 m * pow 3 m = 1
=> rewriteI CMonoid.pow_*-comm (rewrite (rewrite *-comm in three_times_one_third) Monoid.pow_ide)
| k+i<n-1 (i : Fin (n-1 -' k)) : k Nat.+ i < n-1
=> <_+-right k (fin_< i) <∘l rewrite (<=_exists (<_suc_<= (fin_< k))) <=-refl
| C2j (i : Fin (n-1 -' k)) : a (k Nat.+ i + 2) * pow r (k Nat.+ i + 2)
< a (suc k) * pow r (suc k) * pow one/3 (suc i) + eps * pow one/3 (k Nat.+ i + 2)
=> run {
rewriteI (RealField.ide-right {r}, three_times_one_third, *-assoc {_} {r}),
rewrite (CMonoid.pow_*-comm {_} {r * natCoef 3} {_} {k Nat.+ i + 2}),
rewrite (CMonoid.pow_*-comm {_} {r * natCoef 3} {_} {suc k}),
rewrite (*-assoc {_} {a (suc k)}),
rewrite (*-assoc {_} {pow (r * natCoef 3) (suc k)}),
rewriteI (CMonoid.pow_+ {_} {one/3} {suc k} {suc i}),
rewriteI (*-assoc {_} {a (suc k)}),
rewriteI (rdistr, *-assoc),
<_*_positive-left {RealField} {_} {_} {pow one/3 (k Nat.+ i + 2)} __ (OrderedSemiring.pow>0 one/3>0 {k Nat.+ i + 2}),
OrderedAddGroup.<-diff-mid,
\have | k+i+2_mod : (k Nat.+ i + 2) Nat.mod suc n = {Nat} k Nat.+ i + 2
=> mod_< (NatOrder.suc<suc (NatOrder.suc<suc (k+i<n-1 i)))
| helper : \Pi (i : Fin (suc n)) -> i > 0 -> a (suc k) * pow (r * 3) (suc k) > a i * pow (r * 3) i - eps
=> rewrite (*-assoc, *-assoc, inv (*-comm {_} {3}), three_times_one_third, ide-right) (C2 j),
rewrite k+i+2_mod in helper ((k Nat.+ i + 2) Nat.mod suc n) (transport (__ > 0) (inv k+i+2_mod) NatOrder.zero<suc)
}
| C2j+2 (i : Fin (suc k)) : a (suc i) * pow r (suc i)
< a (suc k) * pow r (suc k) * pow one/3 (k -' i) + eps * pow 3 (suc i)
=> run {
rewriteI (ide-right {_} {pow r (suc i)}, ide-right {_} {pow one/3 (k -' i)}, third^m*3^m=1 (suc i)),
rewriteI (*-assoc {_} {pow one/3 (k -' i)} {pow one/3 (suc i)}, Monoid.pow_+ {RealField} {one/3} {k -' i} {suc i}),
rewrite (+-comm {_} {k -' i} {i}, <=_exists (<_suc_<= (fin_< i))),
rewriteI (*-assoc {RealField} {a (suc k) * pow r (suc k)} {pow one/3 (suc k)} {pow 3 (suc i)}),
rewriteI (*-assoc, *-assoc, rdistr),
<_*_positive-left __ (OrderedSemiring.pow>0 (usingOnly () linarith) {suc i}),
later $ rewrite (*-assoc, inv (CMonoid.pow_*-comm {RealField} {r} {one/3} {suc i})),
rewrite (*-assoc {RealField} {a (suc k)} {pow r (suc k)} {pow one/3 (suc k)}, inv (CMonoid.pow_*-comm {RealField} {r} {one/3} {suc k})),
OrderedAddGroup.<-diff-mid,
\have | i+1_mod : (suc i) Nat.mod suc n = {Nat} i Nat.+ 1
=> mod_< (NatOrder.suc<suc (fin_< i <∘l suc_<_<= (fin_< k)))
| helper : \Pi (i : Fin (suc n)) -> i > 0 -> a (suc k) * pow (r * one/3) (suc k) > a i * pow (r * one/3) i - eps
=> rewrite (inv eq2, inv eq1, *-assoc, inv (*-assoc {_} {r0}),
inv (*-assoc {_} {r0 * pow one/3 j}), *-assoc {_} {r0}) in C2 (suc (suc j)),
rewrite i+1_mod in helper ((suc i) Nat.mod suc n) (transport (__ > 0) (inv i+1_mod) NatOrder.zero<suc)
}
| small-part-estimate : BigSum arr-1/3^k-i + BigSum arr-1/3^i+1 <= 1 - pow one/3 n
=> \let arr-1/3^i => \new Array RealField (suc k) (\lam i => pow one/3 i)
\in \have
| sum-arr-1/3^i+1-estimate : BigSum arr-1/3^i+1 <= Real.fromRat (ratio 1 2) * (Real.fromRat 1 - pow one/3 n-1) => geom_sum_upper {n-1} {n-1 -' k} -'<=id
| helper : (\lam (i : Fin k) => pow one/3 ((kSeq j -' i) Nat.mod suc (kSeq j))) = (\lam (i : Fin k) => pow {RealField} one/3 (kSeq j -' i))
=> ext (\lam i => rewrite (mod_< {k -' i} {suc k} (<=_<_suc -'<=id)) idp)
| sum-arr-1/3^k-i-computation : BigSum arr-1/3^k-i = BigSum arr-1/3^i - ide
=> usingOnly (rewrite (BigSum_suc, helper, mod_< {k} {suc k} id<suc, -'id)
in AbMonoid.BigSum-reverse arr-1/3^i) linarith
| sum-arr-1/3^k-i-estimate : BigSum arr-1/3^k-i <= Real.fromRat (ratio 3 2) * (Real.fromRat 1 - pow one/3 n) - ide
=> rewrite sum-arr-1/3^k-i-computation (<=_+ (geom_sum_upper' {n} {suc k} (suc_<_<= (fin_< k)) ) (<=-refl {_} {negative ide}))
\in usingOnly (pow1/3-strict-monotone id<suc, sum-arr-1/3^i+1-estimate, sum-arr-1/3^k-i-estimate) linarith
| big-part-estimate : BigSum arr-3^i+1 + BigSum arr-1/3^k+i+2 < pow (natCoef 3) n => run {
\have one-3=-2 : negative (ide - natCoef 3) = {RealField} natCoef 2 => usingOnly () linarith,
\have helper => pmap negative (Ring.geometric-like-partial-sum {RealField} {1} {k} {natCoef 3}),
rewrite (inv PseudoRing.negative_*-left, one-3=-2, AbGroup.negative_+-comm) at helper,
unfold (-) at helper,
rewrite (AddGroup.negative-isInv, ide-left) at helper,
rewriteI {4} zro-right,
rewriteI (negative-left {_} {ide}, +-assoc),
\have sum-arr-1/3^k+i+2<=1 : BigSum arr-1/3^k+i+2 <= ide => run {
\have one/3^k+1<=1 : pow one/3 (suc k) <= ide => <=-less (pow1/3-strict-monotone {_} NatOrder.zero<suc),
\have one-1/3^n>=0 : Real.fromRat 1 - pow one/3 n >= zro =>
rewriteI (negative-right {RealField} {pow one/3 n}) (<=_+ (PosetSemiring.pow<=1 one/3>=0 (usingOnly () linarith) {n}) <=-refl),
<=∘ rewriteI {2} (zro-right {_} {ide}) (<=_+ (<=-refl {_} {Real.fromRat ide}) (PosetAddGroup.negative<=0 (<=-less (OrderedSemiring.pow>0 one/3>0 {n})))),
<=∘ (rewrite *-assoc in (rewrite ide-left in (<=_*_positive-left (rewrite ide-left in <=_*_positive-left {RealField} one/3^k+1<=1 one/2>=0) one-1/3^n>=0) <=∘
(<=_*_positive-left (usingOnly () linarith) one-1/3^n>=0))),
unfold arr-1/3^k+i+2,
\have helper : (\lam (i : Fin (n-1 -' k)) => pow one/3 (suc (suc k) Nat.+ i)) = (\lam i => pow one/3 (suc k) * pow one/3 (suc i)) => ext (\lam _ => rewrite Monoid.pow_+ equation.cMonoid),
rewrite helper,
rewriteI (Semiring.BigSum-ldistr {_} {pow one/3 (suc k)} {\new Array RealField (n-1 -' k) (\lam i => pow one/3 (suc i))}),
<=_*_positive-right {RealField} {pow one/3 (suc k)} (<=-less (OrderedSemiring.pow>0 one/3>0 {suc k})),
geom_sum_upper {n} {n-1 -' k} (-'<=id <=∘ id<=suc)
},
LinearlyOrderedAbMonoid.<=_+-right __ sum-arr-1/3^k+i+2<=1,
LinearlyOrderedSemiring.<_*_positive-cancel-left {RealField} {2} (usingOnly () linarith),
later $ rewrite (helper, ldistr),
rewrite {2} +-comm,
LinearlyOrderedAbMonoid.<=_+-right (usingOnly () linarith),
PosetSemiring.pow_<=-degree_>=1 {RealField} (PosetSemiring.natCoef_<= {_} {1} {3} (usingOnly () linarith)) (<_suc_<= (fin_< (suc k))) <=∘,
rewriteI {1} ide-left,
<=_*_positive-left __ (PosetSemiring.pow>=0 {_} {_} {suc _} (usingOnly () linarith)),
usingOnly () linarith
}
| ai_ri_<_a0 (i : Fin (suc n)) (i>0 : i > {NatSemiring} 0) : a i * pow r i < a 0 => run {
PosetSemiring.<=_*_positive-right (ai>=0 i) (PosetSemiring.pow_<=-monotone {_} {r} {r0} {i} (<=-less r>0) r<=r0) <∘r,
(rewrite (+-assoc, negative-left, zro-right, ide-right) in <_+-left eps (C2 0 i i>0)) <∘l,
rewrite (C1, +-assoc, negative-left, zro-right),
<=-refl
}
\in inP (r, r>0, k,
rewrite (an=1, ide-left) in (rewrite (mod_< id<suc) in ai_ri_<_a0 (finMod n) (rewrite (mod_< id<suc) n>0)),
run {
<∘r (rewriteI eq1 in C2 (suc j) (suc k0) NatOrder.zero<suc),
rewrite -2eps=-eps-eps,
`<=_+ (<=-refl {RealField} {negative eps}),
\have helper : a (suc k0) * pow r (suc k0) >= (a 0 - eps) * pow one/3 (2 * n * n) => run {
rewrite (pmap (a (suc k0) *) (CMonoid.pow_*-comm {RealField} {r0} {pow one/3 (suc j)} {suc k0})),
rewrite (pmap (pow r0 (suc k0) *) (inv (Monoid.pow_* {RealField} {one/3} {suc j} {suc k0}))),
rewrite (inv (*-assoc {RealField}) *> pmap (* pow one/3 ((suc j : Nat) * suc k0)) C1),
<=_*_positive-right (<=-less a0_minus_eps_>0),
PosetSemiring.pow_<=-degree one/3>=0 (usingOnly () linarith) {suc _} {suc (suc _)},
<=_* {suc j} {2 * n} {suc k0} {n} (suc_<_<= (fin_< j NatSemiring.<∘ id<suc)) (suc_<_<= (fin_< k0))
},
<=∘ helper,
later $ rewrite (rdistr, *-comm {_} {_} {a 0}),
<=_+ <=-refl,
rewriteI {1} ide-right,
<=-less,
<_*_negative-right {RealField} {negative eps} (OrderedAddGroup.positive_negative eps>0),
pow1/3-strict-monotone {suc (suc _)} {0} NatOrder.zero<suc
},
ai_ri_<_a0 (suc k) NatOrder.zero<suc,
run {
<=_+ (PosetAddMonoid.BigSum_<= {RealField} {k} {\lam i => a (suc i) * pow r (suc i)}
{\lam i => a (suc k) * pow r (suc k) * pow one/3 (k -' i) + eps * pow 3 (suc i)} (\lam i => <=-less (C2j+2 i)))
(PosetAddMonoid.BigSum_<= {RealField} {n-1 -' k} {\lam i => a (k Nat.+ i + 2) * pow r (k Nat.+ i + 2)}
{\lam i => a (suc k) * pow r (suc k) * pow one/3 (suc i) + eps * pow one/3 (k Nat.+ i + 2) } (\lam i => <=-less (C2j i))) <∘r,
rewrite (AbMonoid.BigSum_+ {RealField} {n-1 -' k} {\new Array RealField (n-1 -' k) (\lam i => a (suc k) * pow r (suc k) * pow one/3 (suc i))} {\new Array RealField (n-1 -' k) (\lam i => eps * pow one/3 (k Nat.+ i + 2))}),
rewrite (AbMonoid.BigSum_+ {RealField} {k} {\new Array RealField k (\lam i => a (suc k) * pow r (suc k) * pow one/3 (k -' i))} {\new Array RealField k (\lam i => eps * pow 3 (suc i))}),
rewriteI (Semiring.BigSum-ldistr {_} {a (suc k) RealField.* (pow {RealField} r (suc k))} {arr-1/3^i+1}),
later $ rewriteI (Semiring.BigSum-ldistr {_} {eps} {arr-1/3^k+i+2}),
rewriteI (Semiring.BigSum-ldistr {_} {a (suc k) RealField.* (pow {RealField} r (suc k))} {arr-1/3^k-i}),
later $ rewriteI (Semiring.BigSum-ldistr {_} {eps} {arr-3^i+1}),
\have helper-reorder :
a (suc k) RealField.* (pow r (k Nat.+ 1)) * BigSum arr-1/3^k-i + eps * BigSum arr-3^i+1 +
(a (suc k) RealField.* (pow r (k Nat.+ 1)) * BigSum arr-1/3^i+1 + eps * BigSum arr-1/3^k+i+2) =
(a (suc k) RealField.* (pow r (k Nat.+ 1))) * (BigSum arr-1/3^k-i + BigSum arr-1/3^i+1) +
eps * (BigSum arr-3^i+1 + BigSum arr-1/3^k+i+2) => equation.ring,
rewrite helper-reorder,
\have ak+1*r^k+1>=0 : a (suc k) * pow {RealField} r (suc k) >= 0 =>
PosetSemiring.<=_*_positive_positive {RealField} (ai>=0 (suc k)) (PosetSemiring.pow>=0 {_} {r} {suc k} (<=-less r>0)),
LinearlyOrderedAbMonoid.<=_+-left (<=_*_positive-right ak+1*r^k+1>=0 small-part-estimate),
<_*_positive-right eps>0,
big-part-estimate
})
\where \private {
\lemma geom_sum_upper {n m : Nat} (m<=n : m <= n)
: BigSum (\lam (i : Fin m) => RealField.pow one/3 (suc i))
<= Real.fromRat (ratio 1 2) * (Real.fromRat 1 - RealField.pow one/3 n)
=> linarith (pow1/3-monotone m<=n, RealField.geometric-like-partial-sum {1} {m} {one/3})
\lemma geom_sum_upper' {n m : Nat} (m<=n : m <= n)
: BigSum (\lam (i : Fin m) => RealField.pow one/3 i)
<= Real.fromRat (ratio 3 2) * (Real.fromRat 1 - RealField.pow one/3 n)
=> linarith (RealField.geometric-partial-sum {m} {one/3}, pow1/3-monotone m<=n)
\lemma KeyLemma {n : Nat} (n>0 : n > 0) {eps : Real} (eps>0 : eps > 0)
(a : Nat -> Real) (a0>eps : a 0 > eps) (ai>=0 : \Pi (i : Nat) -> a i >= 0) (an=1 : a n = 1)
: ∃ (r0 : Real) (r0>0 : r0 > 0) (k : Fin (2 * n + 1) -> Fin n)
(k_monotone : ∀ (j : Fin (2 * n)) (k (suc j) <= {NatSemiring} k j))
(C1 : a (suc (k 0)) * pow r0 (suc (k 0)) = a 0 - eps)
∀ (j : Fin (2 * n + 1)) (i : Fin (n + 1)) (i > 0)
(a (suc (k j)) * pow (r0 * pow one/3 j) (suc (k j)) > a i * pow (r0 * pow one/3 j) i - eps) \elim n
| 0 => absurd \case n>0
| suc n => \case base {eps} {eps>0} {suc n} a a0>eps ai>=0 an=1 (n Nat.mod suc n) \with {
| inP (k0, r0, r0>0, invC1, invC2) =>
\case finiteDependentChoice
{\Sigma (j : Nat) (kj : Fin (n + 1)) (P : ∀ (i : Fin (n + 2)) (i > 0) (a (suc kj) * pow (r0 * pow one/3 j) (suc kj) > a i * pow (r0 * pow one/3 j) i - eps))}
(\lam (j, kj, Pj) (j+1,kj+1,Pj+1) => \Sigma (kj+1 <= {NatSemiring} kj) (j+1 = {NatSemiring} suc j))
(0, k0, \lam l _ => rewrite ide-right (invC2 l (rewrite (mod_< id<suc) natarith)))
(step {eps} {eps>0} {suc n} a ai>=0 r0 r0>0) (2 * n + 4) \with {
| inP (f, p, mon) => inP (r0, r0>0, \lam j => (f j).2, \lam j => (mon j).1, rewrite p invC1,
\lam j => rewrite (sucPreserving-isId {2 * n + 3} (\lam k => (f k).1) (\lam k => (mon k).2) (pmap __.1 p) j) in (f j).3)
}
} \where {
\private \lemma step
{n : Nat} (a : Nat -> Real) (ai>=0 : \Pi (i : Nat) -> a i >= 0) (r0 : Real) (r0>0 : r0 > 0)
: ∀ (tuple_j : triple) ∃ (tuple_j+1 : triple) Given (tuple_j+1.2 NatSemiring.<= tuple_j.2) (tuple_j+1.1 = tuple_j.1 Nat.+ 1)
=> \lam (j, kj, IA) =>
\let
| eps/2 : Real => eps * ratio 1 2
| eps-eps/2 : eps/2 - eps = negative eps/2 => AddGroup.cancel-left eps/2 (rewrite (AddGroup.toZero idp, inv +-assoc, inv Ring.ldistr, RealAbGroup.half+half {1}, ide-right) (AddGroup.toZero idp))
| eps2>0 : eps/2 > 0 => OrderedSemiring.<_*_positive_positive eps>0 (rat_real_<.1 idp)
| -eps2-eps2=-eps : negative eps/2 + negative eps/2 = negative eps => rewrite (inv AbGroup.negative_+-comm, inv ldistr, RealAbGroup.half+half {1}, ide-right) idp
| r0/3^j>0 : r0 * pow one/3 j > {RealField} 0 => OrderedSemiring.<_*_positive_positive r0>0 (OrderedSemiring.pow>0 one/3>0 {j})
| rNext : Real => r0 * pow one/3 (suc j)
| rNext=r/3 : rNext = (r0 * pow one/3 j) * one/3 => inv *-assoc
| kj+1<=n : suc kj <= {NatSemiring} n => suc_<_<= (fin_< kj)
| (inP (k', Pk')) => array-eps-max {suc kj} NatOrder.zero<suc eps2>0 (mkArray (\lam (t : Fin (suc kj)) => a ((suc t : Nat) Nat.mod suc n) * pow rNext (suc t)))
\in inP ((suc j, fin-inc_<= kj+1<=n k', \case \elim __ \with {
| 0 => \case __
| suc i => \lam _ =>
\have
| k<n => (fin_< k') <∘l {NatSemiring} kj+1<=n
| suc_k'_compatibility : suc (fin-inc_<= {suc kj} {n} kj+1<=n k') = {Nat} (suc k' Nat.mod suc n) => run {
rewrite (mod_< {suc k'} {suc n} (NatOrder.suc<suc k<n)),
rewrite (toFin=id {_} {_} {k<n}),
idp
}
| case_i<=kj (i<kj+1 : i < {NatSemiring} suc kj)
: a (suc (fin-inc_<= kj+1<=n k')) * (pow (r0 * (pow one/3 j * one/3)) (fin-inc_<= kj+1<=n k') * (r0 * (pow one/3 j * one/3)))
> a (suc i) * (pow (r0 * (pow one/3 j * one/3)) i * (r0 * (pow one/3 j * one/3))) - eps
=> run {
rewrite (nat_fin_= suc_k'_compatibility),
rewrite (toFin=id {_} {_} {k<n}),
(rewrite (+-assoc, eps-eps/2, mod_< {i} {suc kj} i<kj+1, nat_fin_= (mod_< {suc i} {suc n} linarith))
in <_+-left (negative eps) (Pk' (i Nat.mod suc kj))) <∘,
x-eps<x eps2>0
}
\in \case LinearOrder.trichotomy i kj \with {
| less i<kj => case_i<=kj (i<kj <∘ id<suc)
| equals i=kj => case_i<=kj (rewrite i=kj id<suc)
| greater kj<i => run {
\have i-term-eps<kj-term-eps/2 : a (suc i) * pow rNext (suc i) - eps RealField.< a (suc kj) * pow rNext (suc kj) - eps/2 => run {
rewrite (rNext=r/3, CMonoid.pow_*-comm {_} {_} {one/3} {suc i}, CMonoid.pow_*-comm {_} {_} {one/3} {suc kj}),
unfold (-),
rewriteI (-eps2-eps2=-eps, +-assoc, *-assoc),
<_+-left {_} (negative eps/2),
OrderedAddMonoid.<_+-right _ (OrderedAddGroup.negative_< (<_*_positive-right eps>0 ((PosetSemiring.pow<=id one/3>=0 (usingOnly () linarith) {suc i} (\case __)) <∘r linarith))) <∘l,
(rewrite PseudoRing.rdistr_- in <=_*_positive-left (<=-less (IA (suc i) (usingOnly () linarith))) (PosetSemiring.pow>=0 {_} {one/3} {suc i} one/3>=0)) <=∘,
rewriteI {2} (*-assoc {_} {a (suc kj)}),
<=_*_positive-right (positivity (using (ai>=0 (suc kj), one/3>0))),
PosetSemiring.pow_<=-degree one/3>=0 (usingOnly () linarith) {suc kj} {suc i} (suc<=suc (NatSemiring.<=-less kj<i))
},
i-term-eps<kj-term-eps/2 <∘,
<_+-left (negative eps/2) (rewrite (toFin=id, nat_fin_= (mod_< {suc kj} {suc n} (<=_<_suc kj+1<=n))) in Pk' (toFin kj id<suc)) <∘l,
rewrite (nat_fin_= suc_k'_compatibility, toFin=id, +-assoc, AddGroup.toZero idp, zro-right),
<=-refl
}
}
}), (rewrite toFin=id (suc<=suc.conv (suc_<_<= (fin_< k'))), idp))
\where {
\func triple => Given (j : Nat) (kj : Fin n) ∀ (i : Fin (suc n)) (i > 0) (a (suc kj) * pow (r0 * pow one/3 j) (suc kj) > a i * pow (r0 * pow one/3 j) i - eps)
}
\lemma x-eps<x {x : Real} {eps : Real} (eps>0 : eps > 0) : x - eps < x => linarith
\lemma n_mod_n+1=n {n : Nat} : n Nat.mod (n + 1) = {Nat} n
=> mod_< {n} {n + 1} linarith
\lemma base
{n : Nat} (a : Nat -> Real) (a0>eps : a 0 > eps)
(ai>=0 : \Pi (i : Nat) -> a i >= 0) (an=1 : a n = {Real} 1) (m : Fin n)
: ∃ (k0 : Fin n) (r0 : Real) (r0>0 : r0 > 0) (a (suc k0) * pow r0 (suc k0) = a 0 - eps)
∀ (l : Fin (n + 1)) (l NatSemiring.+ m >= n) (a (suc k0) * pow r0 (suc k0) > a l * pow r0 l - eps)
\elim n, m
| suc n, 0 =>
\have | pow-eq : pow (root (suc n) (a 0 - eps)) (finMod n) * root (suc n) (a 0 - eps) = a 0 - eps
=> pmap (\lam (m : Nat) => pow (root (suc n) (a 0 - eps)) m * root (suc n) (a 0 - eps)) n_mod_n+1=n
*> pow_root {suc n} suc/=0 {a 0 - eps} linarith
| an+1=1 : a (suc (finMod n)) = ide => pmap a (pmap suc (mod_< {n} {suc n} id<suc)) *> an=1
| term=a0-eps : a (suc (finMod n)) * (pow (root (suc n) (a 0 - eps)) (finMod n) * root (suc n) (a 0 - eps)) = a 0 - eps
=> rewrite (an+1=1, pow-eq) ide-left
\in inP (finMod n, root (suc n) (a 0 - eps), root>0 (\lam p => later \case p) linarith, term=a0-eps,
\lam l l>=n+1 =>
\have | l=n+1 : l = {Nat} n + 1 => <=-antisymmetric (<_suc_<= (fin_< l)) l>=n+1
| l-term=a0-eps : a l * pow (root (suc n) (a 0 - eps)) l = a 0 - eps
=> pmap2 (*) (pmap a l=n+1 *> an=1)
(pmap (pow (root (suc n) (a 0 - eps))) l=n+1 *> pow_root {suc n} suc/=0 {a 0 - eps} linarith)
*> ide-left
\in linarith)
| suc n, suc m => \case base {eps} {eps>0} {suc n} a a0>eps ai>=0 an=1 m \with {
| inP (k0, r0, r0>0, k0-term=a0-eps, IA) =>
\let
| i => n -' suc m
| i<n+1 : i < n + 1 => <=_<_suc -'<=id
| i/=0 : suc i /= {Nat} 0 => \lam p => \case p
| a0-eps>0 : a 0 - eps > 0 => linarith
| l=i+1 : \Pi {l : Fin (n + 2)} (l+m=n : l + m = {Nat} n) -> l = {Nat} suc i =>
\lam {_} l+m=n => rewriteI (-'_suc (suc_<_<= (fin_< m)), l+m=n) (inv -'+)
\in
\case real-located {a (suc i Nat.mod suc (suc n)) * pow r0 (suc i)} {a 0 - eps} {a 0} linarith \with {
| byLeft a0-eps<ai*r0^i =>
\have
| r0^i>0 => OrderedSemiring.pow>0 r0>0 {suc i}
| ai>0 : a (suc i Nat.mod suc (suc n)) > 0 => rewrite (*-assoc, OrderedField.pinv-right, ide-right) in
OrderedSemiring.<_*_positive_positive {_} {_} {OrderedField.pinv (pow r0 (suc i)) r0^i>0}
(a0-eps>0 <∘ a0-eps<ai*r0^i) (OrderedField.pinv>0 r0^i>0)
| a0-eps/ai>0 => OrderedSemiring.<_*_positive_positive a0-eps>0 (OrderedField.pinv>0 ai>0)
| r0new<r0 => rewrite (root_pow i/=0 (<=-less r0>0)) in root-monotone i/=0 (<=-less a0-eps/ai>0)
(rewriteI (RealField.ide-right {pow r0 (suc i)}, OrderedField.pinv-right {_} {a (suc i Nat.mod suc (suc n))} ai>0, *-assoc)
(<_*_positive-left (rewrite *-comm a0-eps<ai*r0^i) (OrderedField.pinv>0 ai>0)))
| i-term=a0-eps : a (finMod (suc i)) RealField.* (pow {RealField} _ (suc i)) = {Real} a 0 - {RealField} eps => run {
rewrite (pow_root i/=0 (<=-less a0-eps/ai>0)),
rewrite {1} *-comm,
rewriteI (*-assoc {RealField}),
rewrite OrderedField.pinv-right,
ide-left
}
\in inP (finMod i, root (suc i) ((a 0 - eps) * OrderedField.pinv (a (finMod (suc i))) ai>0), root>0 i/=0 a0-eps/ai>0,
rewrite (mod_suc i i<n+1, mod_< {i} {suc n} linarith, i-term=a0-eps) idp,
\lam l l+m+1>=n+1 =>
\case LinearOrder.Dec.trichotomy (l + {NatSemiring} m) n \with {
| less l+m<n => absurd (suc<=suc.conv l+m+1>=n+1 l+m<n)
| equals l+m=n => run {
rewrite (mod_suc i i<n+1, mod_< {i} {suc n} i<n+1),
rewrite (nat_fin_= {suc (suc n)} {l} {(suc i) Nat.mod suc (suc n)} (rewrite (mod_< {suc i} {suc (suc n)} linarith) (l=i+1 l+m=n))),
rewrite {9} (mod_< {suc i} {suc (suc n)} linarith),
x-eps<x eps>0
}
| greater l+m>n => run {
\have l/=0 : l /= {Nat} 0 => \lam l=0 => suc<=suc.conv l+m+1>=n+1 (rewrite l=0 (fin_< m)),
\have r0new^l<=r0^l => <=-less (LinearlyOrderedSemiring.pow_<-monotone l/=0 (<=-less (root>0 i/=0 a0-eps/ai>0)) r0new<r0),
rewrite {2} (mod_< {i} {suc n} i<n+1),
rewrite (mod_suc {n} i i<n+1),
rewrite i-term=a0-eps,
(<=_+ (<=_*_positive-right (ai>=0 l) r0new^l<=r0^l) (<=-refl {RealField} {negative eps})) <∘r,
(IA l (<_suc_<= linarith)) <∘l,
rewrite k0-term=a0-eps,
<=-refl
}
})
| byRight ai*r0^i<a0 => inP (k0, r0, r0>0, k0-term=a0-eps, \lam l l+m+1>=n+1 =>
\case LinearOrder.Dec.trichotomy (l + {NatSemiring} m) n \with {
| less l+m<n => absurd (suc<=suc.conv l+m+1>=n+1 l+m<n)
| greater l+m>n => IA l (<_suc_<= linarith)
| equals l+m=n =>
run {
rewrite k0-term=a0-eps,
<_+-left (negative eps),
rewrite {2} (l=i+1 l+m=n),
rewrite (nat_fin_= {suc (suc n)} {l} (rewrite (mod_< {suc i} {suc (suc n)} linarith) (l=i+1 l+m=n))),
ai*r0^i<a0
}
})
}
}
}
}
{- | Shifted split of a finite sum: $\sum_{i<m+l} f_i = \sum_{i<m} f_i + \sum_{j<l} f_{m+j}$.
Phrased for a coefficient function on `Nat` rather than an `Array`, which keeps the
index arithmetic free of the `Fin.fromNat` truncation. -}
\private \lemma BigSum-shift {M : AddMonoid} (f : Nat -> M) (m l : Nat)
: BigSum (\new Array M (m Nat.+ l) (f __))
= BigSum (\new Array M m (f __)) + BigSum (\new Array M l (\lam j => f (m Nat.+ j)))
=> BigSum-split {M} {m} {l} {\new Array M (m Nat.+ l) (f __)}
*> pmap2 (+) (BigSum-ext (\lam i => pmap f fin-inc.char))
(BigSum-ext (\lam j => pmap f fin-raise.char))
-- | A polynomial evaluated at $0$ is its constant coefficient.
\private \lemma polySum_zro {R : Ring} (b : Nat -> R) (m : Nat)
: BigSum (\new Array R (suc m) (\lam i => b i * pow 0 i)) = b 0
=> pmap2 (+) ide-right (R.BigSum_zro \lam j => inv *-assoc *> zro_*-right) *> zro-right
-- | The Kneser factor $q = 1 - 3^{ -2n^2 - n}$, explicit as in the statement of Lemma 4.
\func kneser-q (n : Nat) : Real => 1 - pow one/3 (2 * n * n + n)
\lemma kneser-q>0 {n : Nat} (n>0 : 0 < n) : 0 < kneser-q n
=> linarith (pow1/3-strict-monotone {2 * n * n + n} {0} (linarith : 0 < 2 * n * n + n))
\lemma kneser-q<1 {n : Nat} : kneser-q n < 1
=> linarith (OrderedSemiring.pow>0 one/3>0 {2 * n * n + n})
{- | **Lemma 4 (Kneser's Lemma).** With the explicit factor $q = $ `kneser-q n`, which satisfies
$0 < q < 1$ by `kneser-q>0` and `kneser-q<1`: for every monic polynomial
$f(z) = \sum_{i \le n} b_i z^i$ of degree $n > 0$ over $\mathbb{C}$ and every $c$ with
$|f(0)| < c$, there is a $z$ with $|z|^n < c$ and $|f(z)| < qc$.
The proof follows the informal one. Either $|f(0)|$ already lies below $qc$ and $z = 0$
works, or $|f(0)| > 0$; then choose $\varepsilon$ satisfying conditions (1) and (2)
(`exists-eps`), feed the moduli $a_i = |b_i|$ to `MainLemma` to obtain a radius $r$ and an
index $k$, and use `opposite-direction` to place $z$ on the circle of radius $r$ so that
$b_k z^k$ points against $b_0$. The remaining terms are negligible by the third estimate
of `MainLemma`. -}
\lemma KneserLemma {n : Nat} (n>0 : 0 < n) (b : Nat -> Complex) (bn=1 : b n = 1)
{c : Real} (b0<c : cabs (b 0) < c)
: ∃ (z : Complex) (pow (cabs z) n < c)
(cabs (BigSum (\new Array Complex (suc n) (\lam i => b i * pow z i)))
< kneser-q n * c) \elim n
| 0 => absurd \case n>0
| suc m \as n =>
\have | c>0 : (0 : Real) < c => usingOnly (cabs>=0 {b 0}, b0<c) linarith
| q>0 : (0 : Real) < kneser-q n => kneser-q>0 n>0
\in \case real-located {cabs (b 0)} {0} {kneser-q n * c} (<_*_positive_positive q>0 c>0) \with {
| byRight small => inP (0, rewrite (cabs_zro, zro_*-right) c>0, rewrite (polySum_zro b n) small)
| byLeft a0>0 =>
\have
| A>0 : (0 : Real) < pow one/3 (2 * n * n) * cabs (b 0)
=> <_*_positive_positive (OrderedSemiring.pow>0 one/3>0 {2 * n * n}) a0>0
| B>0 : (0 : Real) < kneser-q n * (c - cabs (b 0)) => <_*_positive_positive q>0 (usingOnly b0<c linarith)
| D>0 : (0 : Real) < pow 3 n + 1
=> usingOnly (OrderedSemiring.pow>0 (usingOnly () linarith : (0 : Real) < 3) {n}) linarith
\in \case exists-eps A>0 B>0 D>0 \with {
| inP (eps, eps>0, cond1, cond2) =>
\have
| scale<=1 : pow one/3 (2 * n * n) * cabs (b 0) <= cabs (b 0)
=> gcongr (PosetSemiring.pow<=1 one/3>=0 (<=-less one/3<1) {2 * n * n}) <=∘ =_<= ide-left
| a0>eps : cabs (b 0) > eps => usingOnly (cond1, scale<=1, eps>0) linarith
\in \case MainLemma n>0 eps>0 (\lam i => cabs (b i)) a0>eps
(\lam i => cabs>=0) (pmap cabs bn=1 *> cabs_ide) \with {
| inP (r, r>0, kF, rn<a0, lower, upper, sums) =>
\have
| R>0 : (0 : Real) < pow r (suc kF) => OrderedSemiring.pow>0 r>0 {suc kF}
| prod>0 : (0 : Real) < cabs (b (suc kF)) * pow r (suc kF) => usingOnly (lower, cond1) linarith
| ak>0 : (0 : Real) < cabs (b (suc kF))
=> LinearlyOrderedSemiring.<_*_positive-cancel-right (<=-less R>0)
(transportInv (< cabs (b (suc kF)) * pow r (suc kF)) zro_*-left prod>0)
| kF<=m => <_suc_<= (fin_< kF)
\in \case opposite-direction a0>0 ak>0 {suc kF} NatOrder.zero<suc r>0 upper \with {
| inP (z, cz, key) =>
\have
| sums' : BigSum (\new Array Real kF (\lam i => cabs (b (suc i)) * pow r (suc i)))
+ BigSum (\new Array Real (m -' kF)
(\lam i => cabs (b (suc kF Nat.+ suc i)) * pow r (suc kF Nat.+ suc i)))
< cabs (b (suc kF)) * pow r (suc kF)
- pow one/3 n * (cabs (b (suc kF)) * pow r (suc kF)) + pow 3 n * eps
=> sums <∘l =_<= equation.cRing
| lower-scaled : pow one/3 n * (pow one/3 (2 * n * n) * cabs (b 0) - 2 * eps)
< pow one/3 n * (cabs (b (suc kF)) * pow r (suc kF))
=> <_*_positive-right (OrderedSemiring.pow>0 one/3>0 {n}) lower
| pow-split : pow one/3 n * (pow one/3 (2 * n * n) * cabs (b 0) - 2 * eps)
= pow one/3 (2 * n * n + n) * cabs (b 0) - 2 * (pow one/3 n * eps)
=> rewrite (Monoid.pow_+ {RealField} {one/3} {2 * n * n} {n}) equation.cRing
| eps-third : pow one/3 n * eps <= one/3 * eps
=> gcongr (pow1/3-monotone {n} {1} (suc_<_<= n>0) <=∘ =_<= ide-left)
| cond2' : pow 3 n * eps + eps
< c - cabs (b 0) - pow one/3 (2 * n * n + n) * c
+ pow one/3 (2 * n * n + n) * cabs (b 0)
=> =_<= equation.cRing <∘r (cond2 <∘l =_<= equation.cRing)
| q-eq : c - pow one/3 (2 * n * n + n) * c = kneser-q n * c => equation.cRing
\in inP (z, rewrite cz (usingOnly (rn<a0, b0<c) linarith),
polySum-bound b z r cz kF (m -' kF) (pmap (\lam t => suc (suc t)) (inv (<=_exists kF<=m)))
<∘r (usingOnly (key, sums', cond2', lower-scaled, pow-split, eps-third, eps>0) linarith
<∘l =_<= q-eq))
}
}
}
}
\where {
{- | The two conditions (1) and (2) on $\varepsilon$ in the proof of Lemma 4: given
positive $A$, $B$ and $D$, there is an $\varepsilon > 0$ with $2\varepsilon < A$ and
$D\varepsilon < B$. Each condition is one `split-eps`; their meet satisfies both. -}
\lemma exists-eps {A B D : Real} (A>0 : 0 < A) (B>0 : 0 < B) (D>0 : 0 < D)
: ∃ (eps : Real) (0 < eps) (2 * eps < A) (D * eps < B)
=> \let | (inP (d1, d1>0, d1A)) => split-eps A>0 (linarith : (0 : Real) < 2)
| (inP (d2, d2>0, d2B)) => split-eps B>0 D>0
\in inP (d1 ∧ d2, <_meet-univ d1>0 d2>0,
linarith (RealAbGroup.meet-left {d1} {d2}),
gcongr (RealAbGroup.meet-right {d1} {d2}) <∘r (=_<= *-comm <∘r d2B))
{- | Triangle-inequality bound for a split polynomial value: the modulus is at most the modulus
of the two distinguished terms together, plus the sums of the moduli of the rest. -}
\lemma polySum-bound (b : Nat -> Complex) (z : Complex) (r : Real) (cz : cabs z = r)
{N : Nat} (p q : Nat) (Neq : N = suc p Nat.+ suc q)
: cabs (BigSum (\new Array Complex N (\lam i => b i * pow z i)))
<= cabs (b 0 + b (suc p) * pow z (suc p))
+ (BigSum (\new Array Real p (\lam i => cabs (b (suc i)) * pow r (suc i)))
+ BigSum (\new Array Real q
(\lam i => cabs (b (suc p Nat.+ suc i)) * pow r (suc p Nat.+ suc i))))
=> =_<= (pmap cabs (BigSum-split4 (\lam i => b i * pow z i) ide-right p q Neq))
<=∘ cabs_+3 (b 0 + b (suc p) * pow z (suc p))
(tail-bound b cz p suc) (tail-bound b cz q (suc p Nat.+ suc __))
\where {
\lemma BigSum-split4 {M : AbMonoid} (g : Nat -> M) {c : M} (g0 : g 0 = c)
{N : Nat} (p q : Nat) (Neq : N = suc p Nat.+ suc q)
: BigSum (\new Array M N (g __))
= (c + g (suc p)) + (BigSum (\new Array M p (\lam i => g (suc i)))
+ BigSum (\new Array M q (\lam i => g (suc p Nat.+ suc i)))) \elim Neq
| idp => BigSum-shift g (suc p) (suc q)
*> pmap2 (+) (BigSum-shift g 1 p *> pmap (+ _) (zro-right *> g0))
(BigSum-shift (\lam j => g (suc p Nat.+ j)) 1 q *> pmap (+ _) zro-right)
*> +-exchange
-- | Regroup a sum of two sums: $(x + s) + (y + t) = (x + y) + (s + t)$.
\private \lemma +-exchange {M : AbMonoid} {x y s t : M} : (x + s) + (y + t) = (x + y) + (s + t)
=> equation.abMonoid
-- | Triangle inequality for a tail sum: $|\sum_i b_{f i} z^{f i}| \le \sum_i |b_{f i}| r^{f i}$.
\lemma tail-bound (b : Nat -> Complex) {z : Complex} {r : Real} (cz : cabs z = r)
(m : Nat) (f : Nat -> Nat)
: cabs (BigSum (\new Array Complex m (\lam i => b (f i) * pow z (f i))))
<= BigSum (\new Array Real m (\lam i => cabs (b (f i)) * pow r (f i)))
=> cabs_BigSum <=∘ =_<= (BigSum-ext (\lam i => cabs_*
*> pmap (cabs (b (f i)) *) (cabs_pow *> pmap (\lam t => pow t (f i)) cz)))
{- | $|X + (Y + Z)| \leq |X| + (y + z)$ from bounds $|Y| \leq y$ and $|Z| \leq z$. `X` is
explicit: a concrete `Complex` sum in the goal unfolds into its components, so an
implicit `X` under the `+` would not be inferrable. -}
\lemma cabs_+3 (X : Complex) {Y Z : Complex} {y z : Real} (py : cabs Y <= y) (pz : cabs Z <= z)
: cabs (X + (Y + Z)) <= cabs X + (y + z)
=> cabs_+ {X} {Y + Z} <=∘ <=_+ <=-refl (cabs_+ {Y} {Z} <=∘ <=_+ py pz)
}
{- | Directional core of Kneser Lemma. If $b_0 \neq 0$, $b_k \neq 0$, $k > 0$ and
$|b_k| r^k < |b_0|$, then some $z$ of modulus $r$ makes $b_k z^k$ point exactly opposite
to $b_0$, so that the two terms cancel as much as they can:
$$|b_0 + b_k z^k| = |b_0| - |b_k| r^k.$$
Take $z = rv$, where $v$ is a $k$-th root (`complex-root`) of the unit vector `unit-dir`
supplies. Then $b_k z^k = -\,(|b_k| r^k / |b_0|)\,b_0$ is a negative real multiple of
$b_0$, so the moduli subtract. -}
\private \lemma opposite-direction {b0 bk : Complex} (b0>0 : 0 < cabs b0) (bk>0 : 0 < cabs bk)
{k : Nat} (k>0 : 0 < k) {r : Real} (r>0 : 0 < r)
(small : cabs bk * pow r k < cabs b0)
: ∃ (z : Complex) (cabs z = r) (cabs (b0 + bk * pow z k) = cabs b0 - cabs bk * pow r k) \elim k
| 0 => absurd \case k>0
| suc k-1 \as k =>
\case unit-dir b0>0 bk>0 \with {
| inP (u, cu, bku) => \case complex-root {u} (rewrite cu linarith) k>0 \with {
| inP (v, pv) =>
\have
| cv : cabs v = 1 => run {
rewriteI (root_pow {k} suc/=0 (cabs>=0 {v})),
rewrite (inv (cabs_pow {v} {k}), pv, cu),
root_ide suc/=0
}
| s<1 : cabs bk * pow r k * pinv (cabs b0) b0>0 < 1
=> gcongr <∘l =_<= (pinv-right b0>0)
| bkzk : bk * pow (Complex.fromReal r * v) k
= ComplexField.negative
(Complex.fromReal (cabs bk * pow r k * pinv (cabs b0) b0>0) * b0)
=> pmap (bk *) (CMonoid.pow_*-comm {ComplexField} {Complex.fromReal r} {v} {k}
*> pmap (* pow v k) (inv (Complex.fromReal_pow {r} {k}))
*> pmap (Complex.fromReal (pow r k) *) pv)
*> cring-pull-neg bku
*> neg_fromReal_* equation.cRing
| sum-eq : b0 + bk * pow (Complex.fromReal r * v) k
= Complex.fromReal (1 - cabs bk * pow r k * pinv (cabs b0) b0>0) * b0
=> pmap (b0 +) bkzk *> cring-diff *> pmap (* b0) (inv fromReal_1-)
\in inP (Complex.fromReal r * v,
cabs_* *> pmap2 (*) (cabs_fromReal *> RealField.abs-ofPos (<=-less r>0)) cv *> ide-right,
pmap cabs sum-eq *> cabs_* *>
pmap (* cabs b0) (cabs_fromReal *> RealField.abs-ofPos (linarith : (0 : Real) <= 1 - cabs bk * pow r k * pinv (cabs b0) b0>0))
*> pinv-cancel b0>0)
}
}
\where {
{- | The unit complex number pointing opposite to $b_0/b_k$: there is a $u$ with $|u| = 1$ and
$b_k u = -\,(|b_k|/|b_0|)\,b_0$. Concretely $u = -\,b_0\overline{b_k}/(|b_0|\,|b_k|)$,
which works because $b_k \overline{b_k} = |b_k|^2$ is real. -}
\lemma unit-dir {b0 bk : Complex} (b0>0 : 0 < cabs b0) (bk>0 : 0 < cabs bk)
: ∃ (u : Complex) (cabs u = 1) (bk * u = ComplexField.negative (Complex.fromReal (cabs bk * pinv (cabs b0) b0>0) * b0))
=> inP (ComplexField.negative (Complex.fromReal (pinv (cabs b0) b0>0 * pinv (cabs bk) bk>0) * (b0 * conj bk)),
dir-cabs b0 bk (pinv (cabs b0) b0>0 * pinv (cabs bk) bk>0) positivity
*> pmap (* (cabs b0 * cabs bk)) (pinv_* b0>0 bk>0) *> pinv-left _,
cring-swap (mul-conj *> pmap Complex.fromReal (inv cabs-square))
*> neg_fromReal_* (equation.cRing
*> pmap (cabs bk * pinv (cabs b0) b0>0 *) (pinv-left bk>0) *> ide-right))
\where {
{- | Modulus of the direction vector $-\,t\,b_0\overline{b_k}$ for a nonnegative real scale
$t$. The scale is a parameter rather than an inlined expression: with a concrete
`Complex` product in the goal the implicit arguments of `cabs_*` are not inferrable. -}
\lemma dir-cabs (b0 bk : Complex) (t : Real) (t>=0 : 0 <= t)
: cabs (ComplexField.negative (Complex.fromReal t * (b0 * conj bk)))
= t * (cabs b0 * cabs bk)
=> cabs_negative {Complex.fromReal t * (b0 * conj bk)}
*> cabs_* {Complex.fromReal t} {b0 * conj bk}
*> pmap2 (*) (cabs_fromReal *> RealField.abs-ofPos t>=0)
(cabs_* {b0} {conj bk} *> pmap (cabs b0 *) cabs_conj)
-- | $x \cdot (-(t (y c))) = -((t s) y)$ whenever $x c = s$: moves $x$ next to $c$ and folds.
\lemma cring-swap {C : CRing} {t x y c s : C} (h : x * c = s)
: x * negative (t * (y * c)) = negative (t * s * y)
=> equation.cRing *> pmap (\lam w => negative (t * w * y)) h
}
{- | Half of the cancellation at the heart of the lemma: $x (p z) = -(p e y)$ whenever
$x z = -(e y)$. Stated over an abstract `CRing` because on a concrete `Complex` both
`equation.cRing` and `rewrite` unfold the record into its components and then fail. -}
\lemma cring-pull-neg {C : CRing} {x y z p e : C} (h : x * z = negative (e * y))
: x * (p * z) = negative (p * e * y)
=> equation.cRing *> pmap (p *) h *> equation.cRing
{- | Fold a product of two real scalars under $-(\_ \cdot b_0)$: from a real identity
$xy = z$ conclude $-(x\,y\,b_0) = -(z\,b_0)$ in $\mathbb{C}$. Shared by `bkzk` above
and by `unit-dir`. -}
\lemma neg_fromReal_* {b0 : Complex} {x y z : Real} (h : x * y = z)
: ComplexField.negative (Complex.fromReal x * Complex.fromReal y * b0)
= ComplexField.negative (Complex.fromReal z * b0)
=> pmap (\lam w => ComplexField.negative (w * b0))
(inv Complex.fromReal_* *> pmap Complex.fromReal h)
-- | The other half: $u + -(e u) = (1 - e) u$.
\lemma cring-diff {C : CRing} {e u : C} : u + negative (e * u) = (1 - e) * u => equation.cRing
-- | $\mathrm{fromReal}\,(1 - y) = 1 - \mathrm{fromReal}\ y$.
\lemma fromReal_1- {y : Real} : Complex.fromReal (1 - y) = 1 - Complex.fromReal y
=> Complex.fromReal_+ *> pmap (_ +) Complex.fromReal_negative
-- | $(1 - x A^{-1}) A = A - x$.
\lemma pinv-cancel {E : OrderedField} {A x : E} (A>0 : 0 < A)
: (1 - x * pinv A A>0) * A = A - x
=> equation.cRing *> pmap (\lam u => A - x * u) (pinv-left A>0) *> pmap (A -) ide-right
}
}
\lemma KneserLemma-poly {n : Nat} (n>0 : 0 < n) (f : Poly ComplexField)
(deg : degree< f (suc n)) (monic : polyCoef f n = 1)
{c : Real} (f0<c : cabs (polyEval f 0) < c)
: ∃ (z : Complex) (pow (cabs z) n < c) (cabs (polyEval f z) < kneser-q n * c)
=> \have | ev (z : Complex) : polyEval f z
= BigSum (\new Array Complex (suc n) (\lam i => polyCoef f i * pow z i))
=> polyEval_polyCoef deg
\in \case KneserLemma n>0 (polyCoef f) monic
(rewriteI (ev 0 *> polySum_zro (polyCoef f) n) f0<c) \with {
| inP (z, |z|^n<c, |f|<qc) => inP (z, |z|^n<c, rewrite (ev z) |f|<qc)
}