Download data/lean/OAI/Analysis/BoxTransport/Computation.lean from introvoyz045/math: direct link, hf CLI and curl.
- Browser
- Download file 19.9 kB
-
https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/Analysis/BoxTransport/Computation.lean
- Command line
-
hf download hf://datasets/introvoyz045/math/data/lean/OAI/Analysis/BoxTransport/Computation.lean
-
curl -L -o Computation.lean https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/Analysis/BoxTransport/Computation.lean
19.9 kB
| import Mathlib | |
| import OAI.Analysis.BoxTransport.Definitions | |
| namespace OAI | |
| /-! Computable rational arithmetic and exponential/flat-function approximation. -/ | |
| noncomputable section | |
| open scoped Topology | |
| open scoped BigOperators ContDiff | |
| open scoped BigOperators ContDiff | |
| namespace BoxTransport.Routing.Computation | |
| abbrev QCode := β Γ β Γ β | |
| def Qval (q : QCode) : β := ((q.1 : β) - q.2.1) / (q.2.2 + 1) | |
| def qZero : QCode := (0, 0, 0) | |
| def qOne : QCode := (1, 0, 0) | |
| def qNat (n : β) : QCode := (n, 0, 0) | |
| def qNeg (q : QCode) : QCode := (q.2.1, q.1, q.2.2) | |
| def qAdd (q r : QCode) : QCode := | |
| (q.1 * (r.2.2 + 1) + r.1 * (q.2.2 + 1), | |
| q.2.1 * (r.2.2 + 1) + r.2.1 * (q.2.2 + 1), | |
| (q.2.2 + 1) * (r.2.2 + 1) - 1) | |
| def qMul (q r : QCode) : QCode := | |
| (q.1 * r.1 + q.2.1 * r.2.1, q.1 * r.2.1 + q.2.1 * r.1, | |
| (q.2.2 + 1) * (r.2.2 + 1) - 1) | |
| def qInv (q : QCode) : QCode := | |
| if q.2.1 < q.1 then (q.2.2 + 1, 0, q.1 - q.2.1 - 1) | |
| else if q.1 < q.2.1 then (0, q.2.2 + 1, q.2.1 - q.1 - 1) | |
| else qZero | |
| def qSub (q r : QCode) : QCode := qAdd q (qNeg r) | |
| def qDiv (q r : QCode) : QCode := qMul q (qInv r) | |
| def qPow (q : QCode) (n : β) : QCode := (qMul q)^[n] qOne | |
| def qLe (q r : QCode) : Prop := | |
| q.1 * (r.2.2 + 1) + r.2.1 * (q.2.2 + 1) β€ | |
| r.1 * (q.2.2 + 1) + q.2.1 * (r.2.2 + 1) | |
| instance : DecidableRel qLe := fun _ _ => Nat.decLe _ _ | |
| @[simp] theorem Qval_zero : Qval qZero = 0 := by norm_num [Qval, qZero] | |
| @[simp] theorem Qval_one : Qval qOne = 1 := by norm_num [Qval, qOne] | |
| @[simp] theorem Qval_nat (n : β) : Qval (qNat n) = n := by simp [Qval, qNat] | |
| @[simp] theorem Qval_neg (q : QCode) : Qval (qNeg q) = -Qval q := by | |
| dsimp [Qval, qNeg] | |
| ring | |
| theorem denominator_product (d e : β) : | |
| (((d + 1) * (e + 1) - 1 : β) : β) + 1 = (d + 1) * (e + 1) := by | |
| have h : 1 β€ (d + 1) * (e + 1) := by nlinarith | |
| rw [Nat.cast_sub h] | |
| push_cast | |
| ring | |
| @[simp] theorem Qval_add (q r : QCode) : Qval (qAdd q r) = Qval q + Qval r := by | |
| simp only [Qval, qAdd, Nat.cast_add, Nat.cast_mul, Nat.cast_one, denominator_product] | |
| field_simp | |
| ring | |
| @[simp] theorem Qval_mul (q r : QCode) : Qval (qMul q r) = Qval q * Qval r := by | |
| simp only [Qval, qMul, Nat.cast_add, Nat.cast_mul, denominator_product] | |
| rw [div_mul_div_comm] | |
| congr 1 | |
| ring | |
| @[simp] theorem Qval_inv (q : QCode) : Qval (qInv q) = (Qval q)β»ΒΉ := by | |
| unfold qInv | |
| split_ifs with h h' | |
| Β· have hd : 1 β€ q.1 - q.2.1 := by omega | |
| have he : ((((q.1 - q.2.1) - 1 : β) : β) + 1) = (q.1 : β) - q.2.1 := by | |
| rw [Nat.cast_sub hd, Nat.cast_sub (by omega : q.2.1 β€ q.1)] | |
| norm_num | |
| simp only [Qval, Nat.cast_add, Nat.cast_one, Nat.cast_zero, sub_zero, he, inv_div] | |
| Β· have hd : 1 β€ q.2.1 - q.1 := by omega | |
| have he : ((((q.2.1 - q.1) - 1 : β) : β) + 1) = (q.2.1 : β) - q.1 := by | |
| rw [Nat.cast_sub hd, Nat.cast_sub (by omega : q.1 β€ q.2.1)] | |
| norm_num | |
| simp only [Qval, Nat.cast_add, Nat.cast_one, Nat.cast_zero, zero_sub, he, inv_div] | |
| rw [β neg_sub (q.1 : β) q.2.1, div_neg, neg_div, neg_neg] | |
| Β· have he : q.1 = q.2.1 := by omega | |
| simp [Qval, qZero, he] | |
| @[simp] theorem Qval_sub (q r : QCode) : Qval (qSub q r) = Qval q - Qval r := by | |
| simp [qSub, sub_eq_add_neg] | |
| @[simp] theorem Qval_div (q r : QCode) : Qval (qDiv q r) = Qval q / Qval r := by | |
| simp [qDiv, div_eq_mul_inv] | |
| @[simp] theorem Qval_pow (q : QCode) (n : β) : Qval (qPow q n) = Qval q ^ n := by | |
| induction n with | |
| | zero => simp [qPow] | |
| | succ n ih => | |
| simp only [qPow, Function.iterate_succ_apply', Qval_mul] | |
| rw [β qPow, ih, pow_succ'] | |
| theorem qLe_iff (q r : QCode) : qLe q r β Qval q β€ Qval r := by | |
| rw [Qval, Qval, div_le_div_iffβ (by positivity) (by positivity)] | |
| rw [qLe] | |
| have : | |
| q.1 * (r.2.2 + 1) + r.2.1 * (q.2.2 + 1) β€ | |
| r.1 * (q.2.2 + 1) + q.2.1 * (r.2.2 + 1) β | |
| (q.1 : β) * (r.2.2 + 1) + r.2.1 * (q.2.2 + 1) β€ | |
| r.1 * (q.2.2 + 1) + q.2.1 * (r.2.2 + 1) := by norm_cast | |
| rw [this] | |
| constructor <;> intro h <;> nlinarith | |
| theorem Qval_surjective : Function.Surjective Qval := by | |
| intro q | |
| let p := q.num.toNat | |
| let n := (-q.num).toNat | |
| have hpn : q.num = (p : β€) - n := (Int.toNat_sub_toNat_neg q.num).symm | |
| refine β¨(p, n, q.den - 1), ?_β© | |
| have hd : 1 β€ q.den := q.pos | |
| have he : ((q.den - 1 : β) : β) + 1 = q.den := by | |
| rw [Nat.cast_sub hd] | |
| norm_num | |
| rw [Qval, he, β Int.cast_natCast p, β Int.cast_natCast n, β Int.cast_sub, β hpn] | |
| exact Rat.num_div_den q | |
| private theorem pr_plus {Ξ±} [Primcodable Ξ±] {f g : Ξ± β β} | |
| (hf : Primrec f) (hg : Primrec g) : Primrec fun a => f a + g a := | |
| Primrec.nat_add.comp hf hg | |
| private theorem pr_times {Ξ±} [Primcodable Ξ±] {f g : Ξ± β β} | |
| (hf : Primrec f) (hg : Primrec g) : Primrec fun a => f a * g a := | |
| Primrec.nat_mul.comp hf hg | |
| theorem primrec_qNat : Primrec qNat := (Primrec.id.pair ((Primrec.const 0).pair (.const 0))) | |
| theorem primrec_qNeg : Primrec qNeg := | |
| (Primrec.fst.comp Primrec.snd).pair (Primrec.fst.pair (Primrec.snd.comp Primrec.snd)) | |
| theorem primrec_qAdd : Primrecβ qAdd := by | |
| unfold qAdd | |
| apply Primrec.pair | |
| Β· exact pr_plus | |
| (pr_times (Primrec.fst.comp Primrec.fst) | |
| (Primrec.succ.comp (Primrec.snd.comp (Primrec.snd.comp Primrec.snd)))) | |
| (pr_times (Primrec.fst.comp Primrec.snd) | |
| (Primrec.succ.comp (Primrec.snd.comp (Primrec.snd.comp Primrec.fst)))) | |
| apply Primrec.pair | |
| Β· exact pr_plus | |
| (pr_times (Primrec.fst.comp (Primrec.snd.comp Primrec.fst)) | |
| (Primrec.succ.comp (Primrec.snd.comp (Primrec.snd.comp Primrec.snd)))) | |
| (pr_times (Primrec.fst.comp (Primrec.snd.comp Primrec.snd)) | |
| (Primrec.succ.comp (Primrec.snd.comp (Primrec.snd.comp Primrec.fst)))) | |
| exact Primrec.nat_sub.comp | |
| (pr_times (Primrec.succ.comp (Primrec.snd.comp (Primrec.snd.comp Primrec.fst))) | |
| (Primrec.succ.comp (Primrec.snd.comp (Primrec.snd.comp Primrec.snd)))) (Primrec.const 1) | |
| theorem primrec_qMul : Primrecβ qMul := by | |
| unfold qMul | |
| apply Primrec.pair | |
| Β· exact pr_plus (pr_times (Primrec.fst.comp Primrec.fst) (Primrec.fst.comp Primrec.snd)) | |
| (pr_times (Primrec.fst.comp (Primrec.snd.comp Primrec.fst)) | |
| (Primrec.fst.comp (Primrec.snd.comp Primrec.snd))) | |
| apply Primrec.pair | |
| Β· exact pr_plus | |
| (pr_times (Primrec.fst.comp Primrec.fst) (Primrec.fst.comp (Primrec.snd.comp Primrec.snd))) | |
| (pr_times (Primrec.fst.comp (Primrec.snd.comp Primrec.fst)) (Primrec.fst.comp Primrec.snd)) | |
| exact Primrec.nat_sub.comp | |
| (pr_times (Primrec.succ.comp (Primrec.snd.comp (Primrec.snd.comp Primrec.fst))) | |
| (Primrec.succ.comp (Primrec.snd.comp (Primrec.snd.comp Primrec.snd)))) (Primrec.const 1) | |
| theorem primrec_qInv : Primrec qInv := by | |
| unfold qInv | |
| apply Primrec.ite (Primrec.nat_lt.comp (Primrec.fst.comp Primrec.snd) Primrec.fst) | |
| Β· apply Primrec.pair | |
| Β· exact Primrec.succ.comp (Primrec.snd.comp Primrec.snd) | |
| apply Primrec.pair (Primrec.const 0) | |
| exact Primrec.nat_sub.comp | |
| (Primrec.nat_sub.comp Primrec.fst (Primrec.fst.comp Primrec.snd)) (Primrec.const 1) | |
| apply Primrec.ite (Primrec.nat_lt.comp Primrec.fst (Primrec.fst.comp Primrec.snd)) | |
| Β· apply Primrec.pair (Primrec.const 0) | |
| apply Primrec.pair (Primrec.succ.comp (Primrec.snd.comp Primrec.snd)) | |
| exact Primrec.nat_sub.comp | |
| (Primrec.nat_sub.comp (Primrec.fst.comp Primrec.snd) Primrec.fst) (Primrec.const 1) | |
| exact Primrec.const qZero | |
| theorem primrec_qSub : Primrecβ qSub := | |
| primrec_qAdd.comp Primrec.fst (primrec_qNeg.comp Primrec.snd) | |
| theorem primrec_qDiv : Primrecβ qDiv := | |
| primrec_qMul.comp Primrec.fst (primrec_qInv.comp Primrec.snd) | |
| theorem primrec_qPow : Primrecβ qPow := by | |
| exact Primrec.nat_iterate Primrec.snd (Primrec.const qOne) | |
| (primrec_qMul.comp (Primrec.fst.comp Primrec.fst) Primrec.snd) | |
| theorem primrec_qLe : PrimrecRel qLe := by | |
| exact Primrec.nat_le.comp | |
| (pr_plus | |
| (pr_times (Primrec.fst.comp Primrec.fst) | |
| (Primrec.succ.comp (Primrec.snd.comp (Primrec.snd.comp Primrec.snd)))) | |
| (pr_times (Primrec.fst.comp (Primrec.snd.comp Primrec.snd)) | |
| (Primrec.succ.comp (Primrec.snd.comp (Primrec.snd.comp Primrec.fst))))) | |
| (pr_plus | |
| (pr_times (Primrec.fst.comp Primrec.snd) | |
| (Primrec.succ.comp (Primrec.snd.comp (Primrec.snd.comp Primrec.fst)))) | |
| (pr_times (Primrec.fst.comp (Primrec.snd.comp Primrec.fst)) | |
| (Primrec.succ.comp (Primrec.snd.comp (Primrec.snd.comp Primrec.snd))))) | |
| def qAbs (q : QCode) : QCode := if qLe qZero q then q else qNeg q | |
| @[simp] theorem Qval_abs (q : QCode) : Qval (qAbs q) = |Qval q| := by | |
| unfold qAbs | |
| split_ifs with h | |
| Β· rw [abs_of_nonneg (by simpa only [qLe_iff, Qval_zero] using h)] | |
| Β· rw [Qval_neg, abs_of_neg (lt_of_not_ge (by simpa only [qLe_iff, Qval_zero] using h))] | |
| theorem primrec_qAbs : Primrec qAbs := | |
| Primrec.ite (primrec_qLe.comp (.const qZero) .id) .id primrec_qNeg | |
| theorem primrec_factorial : Primrec Nat.factorial := by | |
| have hp : Primrec (Nat.rec 1 (fun n s => (n + 1) * s)) := | |
| Primrec.nat_recβ 1 (Primrec.nat_mul.comp (Primrec.succ.comp Primrec.fst) Primrec.snd) | |
| apply hp.of_eq | |
| intro n | |
| induction n with | |
| | zero => rfl | |
| | succ n ih => dsimp; rw [ih, Nat.factorial_succ] | |
| def qTaylor (q : QCode) (n : β) : QCode := | |
| ((List.range n).map fun m => qDiv (qPow q m) (qNat m.factorial)).foldr qAdd qZero | |
| @[simp] theorem Qval_taylor (q : QCode) (n : β) : | |
| Qval (qTaylor q n) = β m β Finset.range n, Qval q ^ m / m.factorial := by | |
| have hsum (l : List QCode) : Qval (l.foldr qAdd qZero) = (l.map Qval).sum := by | |
| induction l with | |
| | nil => simp | |
| | cons q l ih => simp [ih] | |
| rw [qTaylor, hsum, List.map_map] | |
| simp only [Function.comp_def, Qval_div, Qval_pow, Qval_nat] | |
| simpa only [List.toFinset_range] using (List.sum_toFinset (fun m => Qval q ^ m / m.factorial) (List.nodup_range (n := n))).symm | |
| theorem primrec_qTaylor : Primrecβ qTaylor := by | |
| unfold Primrecβ qTaylor | |
| apply Primrec.list_foldr (h := fun _ p => qAdd p.1 p.2) | |
| Β· exact Primrec.list_map (Primrec.list_range.comp Primrec.snd) | |
| (primrec_qDiv.comp (primrec_qPow.comp (Primrec.fst.comp Primrec.fst) Primrec.snd) | |
| (primrec_qNat.comp (primrec_factorial.comp Primrec.snd))) | |
| Β· exact Primrec.const qZero | |
| exact primrec_qAdd.comp (Primrec.fst.comp Primrec.snd) (Primrec.snd.comp Primrec.snd) | |
| def expStop (r : QCode Γ β) (m : β) : Prop := | |
| qLe (qDiv (qAbs r.1) (qNat (m + 1))) (qInv (qNat 2)) β§ | |
| qLe (qMul (qDiv (qPow (qAbs r.1) m) (qNat m.factorial)) (qNat 2)) | |
| (qInv (qNat (r.2 + 1))) | |
| instance : DecidableRel expStop := fun _ _ => inferInstanceAs (Decidable (_ β§ _)) | |
| theorem primrec_expStop : PrimrecRel expStop := by | |
| apply PrimrecPred.and | |
| Β· exact primrec_qLe.comp | |
| (primrec_qDiv.comp (primrec_qAbs.comp (Primrec.fst.comp Primrec.fst)) | |
| (primrec_qNat.comp (Primrec.succ.comp Primrec.snd))) (Primrec.const _) | |
| exact primrec_qLe.comp | |
| (primrec_qMul.comp | |
| (primrec_qDiv.comp | |
| (primrec_qPow.comp (primrec_qAbs.comp (Primrec.fst.comp Primrec.fst)) Primrec.snd) | |
| (primrec_qNat.comp (primrec_factorial.comp Primrec.snd))) (Primrec.const _)) | |
| (primrec_qInv.comp (primrec_qNat.comp (Primrec.succ.comp (Primrec.snd.comp Primrec.fst)))) | |
| theorem expStop_iff (q : QCode) (N m : β) : expStop (q, N) m β | |
| |(Qval q : β)| / (m + 1) β€ 1 / 2 β§ | |
| |(Qval q : β)| ^ m / m.factorial * 2 β€ ((N : β) + 1)β»ΒΉ := by | |
| simp only [expStop, qLe_iff, Qval_div, Qval_abs, Qval_nat, Qval_inv, | |
| Qval_mul, Qval_pow] | |
| rw [β Rat.cast_le (K := β), β Rat.cast_le (K := β)] | |
| push_cast | |
| norm_num | |
| theorem exists_expStop (r : QCode Γ β) : β m, expStop r m := by | |
| let x : β := Qval r.1 | |
| have htail : Filter.Tendsto (fun m : β => |x| ^ m / m.factorial * 2) | |
| Filter.atTop (nhds 0) := by | |
| simpa using (Real.summable_pow_div_factorial |x|).tendsto_atTop_zero.mul_const 2 | |
| have hp : (0 : β) < ((r.2 : β) + 1)β»ΒΉ := by positivity | |
| obtain β¨K, hKβ© := Filter.eventually_atTop.1 (htail.eventually_lt_const hp) | |
| obtain β¨M : β, hMβ© := exists_nat_gt (2 * |x|) | |
| refine β¨max K M, (expStop_iff r.1 r.2 _).2 β¨?_, (hK _ (le_max_left ..)).leβ©β© | |
| rw [div_le_iffβ (by positivity)] | |
| have hm : (M : β) β€ (max K M : β) := by exact_mod_cast le_max_right K M | |
| dsimp [x] at hM | |
| linarith | |
| noncomputable def qExp (r : QCode Γ β) : QCode := qTaylor r.1 (Nat.find (exists_expStop r)) | |
| theorem computable_qExp : Computable qExp := | |
| primrec_qTaylor.to_comp.comp Computable.fst (Computable.find primrec_expStop.computablePred exists_expStop) | |
| theorem real_exp_taylor_bound (x : β) (n : β) (h : |x| / (n + 1) β€ 1 / 2) : | |
| |Real.exp x - β m β Finset.range n, x ^ m / m.factorial| β€ | |
| |x| ^ n / n.factorial * 2 := by | |
| have hc := Complex.exp_bound' (x := (x : β)) (n := n) (by simpa [Real.norm_eq_abs] using h) | |
| rw [β Complex.ofReal_exp] at hc | |
| have he : (β m β Finset.range n, (x : β) ^ m / (m.factorial : β)) = | |
| ((β m β Finset.range n, x ^ m / m.factorial : β) : β) := by push_cast; rfl | |
| rw [he, β Complex.ofReal_sub, Complex.norm_real] at hc | |
| simpa [Real.norm_eq_abs] using hc | |
| theorem qExp_error (q : QCode) (N : β) : | |
| |Real.exp (Qval q) - (Qval (qExp (q, N)) : β)| β€ ((N : β) + 1)β»ΒΉ := by | |
| have h := (expStop_iff q N (Nat.find (exists_expStop (q, N)))).1 | |
| (Nat.find_spec (exists_expStop (q, N))) | |
| rw [qExp, Qval_taylor] | |
| push_cast | |
| exact (real_exp_taylor_bound _ _ h.1).trans h.2 | |
| end BoxTransport.Routing.Computation | |
| open scoped BigOperators ContDiff | |
| namespace BoxTransport.Routing.Computation | |
| noncomputable def flatTerm (m : β) (x : β) : β := xβ»ΒΉ ^ m * expNegInvGlue x | |
| @[simp] theorem flatTerm_zero_index : flatTerm 0 = expNegInvGlue := by | |
| funext x | |
| simp [flatTerm] | |
| theorem flatTerm_zero {m : β} {x : β} (hx : x β€ 0) : flatTerm m x = 0 := by | |
| simp [flatTerm, expNegInvGlue.zero_of_nonpos hx] | |
| theorem contDiff_flatTerm (m : β) : ContDiff β β (flatTerm m) := by | |
| convert! (expNegInvGlue.contDiff_polynomial_eval_inv_mul (n := β€) (Polynomial.X ^ m)) using 1 | |
| ext x | |
| simp only [flatTerm, Polynomial.eval_pow, Polynomial.eval_X] | |
| theorem hasDerivAt_flatTerm (m : β) (x : β) : | |
| HasDerivAt (flatTerm m) (flatTerm (m + 2) x - m * flatTerm (m + 1) x) x := by | |
| convert expNegInvGlue.hasDerivAt_polynomial_eval_inv_mul (Polynomial.X ^ m) x using 1 | |
| Β· ext y | |
| simp [flatTerm] | |
| Β· cases m with | |
| | zero => simp [flatTerm] | |
| | succ m => | |
| rw [Polynomial.derivative_X_pow] | |
| simp only [flatTerm, Polynomial.eval_mul, Polynomial.eval_pow, Polynomial.eval_X, | |
| Polynomial.eval_sub, Polynomial.eval_C, Nat.succ_sub_one, Nat.cast_succ] | |
| simp only [pow_succ] | |
| ring | |
| theorem flatTerm_nonneg (m : β) (x : β) : 0 β€ flatTerm m x := by | |
| by_cases hx : x β€ 0 | |
| Β· rw [flatTerm_zero hx] | |
| Β· unfold flatTerm | |
| have : 0 β€ xβ»ΒΉ := inv_nonneg.mpr (le_of_not_ge hx) | |
| exact mul_nonneg (pow_nonneg this _) (expNegInvGlue.nonneg x) | |
| theorem flatTerm_bound (m : β) (x : β) : |flatTerm m x| β€ (m.factorial : β) := by | |
| rw [abs_of_nonneg (flatTerm_nonneg m x)] | |
| by_cases hx : x β€ 0 | |
| Β· rw [flatTerm_zero hx] | |
| positivity | |
| have hv : 0 β€ xβ»ΒΉ := le_of_lt (inv_pos.mpr (lt_of_not_ge hx)) | |
| have hterm : xβ»ΒΉ ^ m / (m.factorial : β) β€ Real.exp xβ»ΒΉ := by | |
| apply le_trans _ (Real.sum_le_exp_of_nonneg hv (m + 1)) | |
| exact Finset.single_le_sum (f := fun i : β => xβ»ΒΉ ^ i / (i.factorial : β)) | |
| (fun i _ => by positivity) (Finset.mem_range.mpr (Nat.lt_succ_self m)) | |
| have hm : (0 : β) < m.factorial := by exact_mod_cast Nat.factorial_pos m | |
| have hmul := mul_le_mul_of_nonneg_right ((div_le_iffβ hm).mp hterm) | |
| (Real.exp_pos (-xβ»ΒΉ)).le | |
| have he : Real.exp xβ»ΒΉ * Real.exp (-xβ»ΒΉ) = 1 := by rw [β Real.exp_add]; simp | |
| unfold flatTerm expNegInvGlue | |
| rw [ite_eq_right hx] | |
| nlinarith [he] | |
| def flatLipschitzBound (m : β) : β := (m + 2).factorial + m * (m + 1).factorial | |
| theorem flatTerm_lipschitz (m : β) : LipschitzWith (flatLipschitzBound m) (flatTerm m) := by | |
| apply lipschitzWith_of_nnnorm_deriv_le ((contDiff_flatTerm m).differentiable (by simp)) | |
| intro x | |
| rw [(hasDerivAt_flatTerm m x).deriv] | |
| apply NNReal.coe_le_coe.mp | |
| simp only [coe_nnnorm, Real.norm_eq_abs, flatLipschitzBound, | |
| Nat.cast_add, Nat.cast_mul] | |
| calc | |
| |flatTerm (m + 2) x - (m : β) * flatTerm (m + 1) x| β€ | |
| |flatTerm (m + 2) x| + |(m : β) * flatTerm (m + 1) x| := abs_sub _ _ | |
| _ β€ (m + 2).factorial + (m : β) * (m + 1).factorial := by | |
| simp only [abs_mul, Nat.abs_cast] | |
| gcongr <;> exact flatTerm_bound _ _ | |
| end BoxTransport.Routing.Computation | |
| open scoped BigOperators ContDiff | |
| namespace BoxTransport.Routing.Computation | |
| theorem computable_ite {Ξ± Ξ²} [Primcodable Ξ±] [Primcodable Ξ²] | |
| {p : Ξ± β Prop} [DecidablePred p] {f g : Ξ± β Ξ²} | |
| (hp : ComputablePred p) (hf : Computable f) (hg : Computable g) : | |
| Computable (fun a => if p a then f a else g a) := by | |
| simpa only [Bool.cond_decide] using Computable.cond hp.decide hf hg | |
| def qBound (q : QCode) : β := q.1 + q.2.1 | |
| theorem Qval_bound (q : QCode) : |Qval q| β€ qBound q := by | |
| rw [Qval, abs_div, abs_of_pos (by positivity : (0 : β) < q.2.2 + 1)] | |
| have h : |(q.1 : β) - q.2.1| β€ (q.1 : β) + q.2.1 := by | |
| simpa only [Nat.abs_cast] using abs_sub (q.1 : β) q.2.1 | |
| apply le_trans (div_le_self (abs_nonneg _) (by norm_cast; omega)) | |
| simpa [qBound] using h | |
| theorem primrec_qBound : Primrec qBound := | |
| Primrec.nat_add.comp Primrec.fst (Primrec.fst.comp Primrec.snd) | |
| noncomputable def qFlat (r : β Γ QCode Γ β) : QCode := | |
| if qLe r.2.1 qZero then qZero else | |
| let w := qPow (qInv r.2.1) r.1 | |
| qMul w (qExp (qNeg (qInv r.2.1), (r.2.2 + 1) * (qBound w + 1))) | |
| theorem computable_qFlat : Computable qFlat := by | |
| unfold qFlat | |
| apply computable_ite (primrec_qLe.comp (Primrec.fst.comp Primrec.snd) (.const qZero)).computablePred | |
| Β· exact .const qZero | |
| have hw : Primrec fun r : β Γ QCode Γ β => qPow (qInv r.2.1) r.1 := | |
| primrec_qPow.comp (primrec_qInv.comp (Primrec.fst.comp Primrec.snd)) Primrec.fst | |
| exact primrec_qMul.to_comp.comp hw.to_comp (computable_qExp.comp | |
| ((primrec_qNeg.comp (primrec_qInv.comp (Primrec.fst.comp Primrec.snd))).pair | |
| (Primrec.nat_mul.comp (Primrec.succ.comp (Primrec.snd.comp Primrec.snd)) | |
| (Primrec.succ.comp (primrec_qBound.comp hw)))).to_comp) | |
| theorem qFlat_error (m : β) (q : QCode) (N : β) : | |
| |flatTerm m (Qval q) - (Qval (qFlat (m, q, N)) : β)| β€ ((N : β) + 1)β»ΒΉ := by | |
| by_cases hq : qLe q qZero | |
| Β· rw [qFlat, ite_eq_left hq, Qval_zero, Rat.cast_zero, sub_zero, | |
| flatTerm_zero (by exact_mod_cast (show Qval q β€ 0 from by simpa using (qLe_iff q qZero).1 hq)), abs_zero] | |
| positivity | |
| have hp : (0 : β) < Qval q := by | |
| exact_mod_cast lt_of_not_ge (by simpa only [qLe_iff, Qval_zero] using hq) | |
| let w := qPow (qInv q) m | |
| let B := qBound w | |
| have hB : |(Qval w : β)| β€ (B : β) := by exact_mod_cast Qval_bound w | |
| have hw : (Qval w : β) = (Qval q : β)β»ΒΉ ^ m := by simp [w] | |
| have hx : (Qval (qNeg (qInv q)) : β) = -(Qval q : β)β»ΒΉ := by simp | |
| have he := qExp_error (qNeg (qInv q)) ((N + 1) * (B + 1)) | |
| rw [hx] at he | |
| rw [qFlat, ite_eq_right hq] | |
| change |flatTerm m (Qval q) - (Qval (qMul w | |
| (qExp (qNeg (qInv q), (N + 1) * (B + 1)))) : β)| β€ _ | |
| rw [Qval_mul, Rat.cast_mul] | |
| have ht : flatTerm m (Qval q) = (Qval w : β) * Real.exp (-(Qval q : β)β»ΒΉ) := by | |
| simp only [flatTerm, expNegInvGlue, not_le.mpr hp, βreduceIte, hw] | |
| rw [ht, β mul_sub, abs_mul] | |
| apply le_trans (mul_le_mul hB he (abs_nonneg _) (Nat.cast_nonneg B)) | |
| push_cast | |
| rw [β div_eq_mul_inv, div_le_iffβ (by positivity)] | |
| have hn : (0 : β) < N + 1 := by positivity | |
| apply (mul_le_mul_iff_rightβ hn).mp | |
| field_simp | |
| nlinarith | |
| end BoxTransport.Routing.Computation | |
| end | |
| end OAI | |