src/HOL/Decision_Procs/Approximation.thy
 author hoelzl Tue Oct 13 13:40:26 2009 +0200 (2009-10-13) changeset 32920 ccfb774af58c parent 32919 37adfa07b54b child 32960 69916a850301 permissions -rw-r--r--
order conjunctions to be printed without parentheses
 hoelzl@30443 ` 1` ```(* Author: Johannes Hoelzl 2008 / 2009 *) ``` wenzelm@30122 ` 2` wenzelm@30886 ` 3` ```header {* Prove Real Valued Inequalities by Computation *} ``` wenzelm@30122 ` 4` hoelzl@29805 ` 5` ```theory Approximation ``` haftmann@29823 ` 6` ```imports Complex_Main Float Reflection Dense_Linear_Order Efficient_Nat ``` hoelzl@29805 ` 7` ```begin ``` hoelzl@29805 ` 8` hoelzl@29805 ` 9` ```section "Horner Scheme" ``` hoelzl@29805 ` 10` hoelzl@29805 ` 11` ```subsection {* Define auxiliary helper @{text horner} function *} ``` hoelzl@29805 ` 12` hoelzl@31098 ` 13` ```primrec horner :: "(nat \ nat) \ (nat \ nat \ nat) \ nat \ nat \ nat \ real \ real" where ``` hoelzl@29805 ` 14` ```"horner F G 0 i k x = 0" | ``` hoelzl@29805 ` 15` ```"horner F G (Suc n) i k x = 1 / real k - x * horner F G n (F i) (G i k) x" ``` hoelzl@29805 ` 16` hoelzl@29805 ` 17` ```lemma horner_schema': fixes x :: real and a :: "nat \ real" ``` hoelzl@29805 ` 18` ``` shows "a 0 - x * (\ i=0.. i=0..i. - (x * ((-1)^i * a (Suc i) * x ^ i)) = (-1)^(Suc i) * a (Suc i) * x ^ (Suc i)" by auto ``` hoelzl@29805 ` 21` ``` show ?thesis unfolding setsum_right_distrib shift_pow real_diff_def setsum_negf[symmetric] setsum_head_upt_Suc[OF zero_less_Suc] ``` hoelzl@29805 ` 22` ``` setsum_reindex[OF inj_Suc, unfolded comp_def, symmetric, of "\ n. (-1)^n *a n * x^n"] by auto ``` hoelzl@29805 ` 23` ```qed ``` hoelzl@29805 ` 24` hoelzl@29805 ` 25` ```lemma horner_schema: fixes f :: "nat \ nat" and G :: "nat \ nat \ nat" and F :: "nat \ nat" ``` haftmann@30971 ` 26` ``` assumes f_Suc: "\n. f (Suc n) = G ((F ^^ n) s) (f n)" ``` haftmann@30971 ` 27` ``` shows "horner F G n ((F ^^ j') s) (f j') x = (\ j = 0..< n. -1 ^ j * (1 / real (f (j' + j))) * x ^ j)" ``` hoelzl@29805 ` 28` ```proof (induct n arbitrary: i k j') ``` hoelzl@29805 ` 29` ``` case (Suc n) ``` hoelzl@29805 ` 30` hoelzl@29805 ` 31` ``` show ?case unfolding horner.simps Suc[where j'="Suc j'", unfolded funpow.simps comp_def f_Suc] ``` hoelzl@29805 ` 32` ``` using horner_schema'[of "\ j. 1 / real (f (j' + j))"] by auto ``` hoelzl@29805 ` 33` ```qed auto ``` hoelzl@29805 ` 34` hoelzl@29805 ` 35` ```lemma horner_bounds': ``` hoelzl@31098 ` 36` ``` assumes "0 \ real x" and f_Suc: "\n. f (Suc n) = G ((F ^^ n) s) (f n)" ``` hoelzl@29805 ` 37` ``` and lb_0: "\ i k x. lb 0 i k x = 0" ``` hoelzl@29805 ` 38` ``` and lb_Suc: "\ n i k x. lb (Suc n) i k x = lapprox_rat prec 1 (int k) - x * (ub n (F i) (G i k) x)" ``` hoelzl@29805 ` 39` ``` and ub_0: "\ i k x. ub 0 i k x = 0" ``` hoelzl@29805 ` 40` ``` and ub_Suc: "\ n i k x. ub (Suc n) i k x = rapprox_rat prec 1 (int k) - x * (lb n (F i) (G i k) x)" ``` hoelzl@31809 ` 41` ``` shows "real (lb n ((F ^^ j') s) (f j') x) \ horner F G n ((F ^^ j') s) (f j') (real x) \ ``` hoelzl@31098 ` 42` ``` horner F G n ((F ^^ j') s) (f j') (real x) \ real (ub n ((F ^^ j') s) (f j') x)" ``` hoelzl@29805 ` 43` ``` (is "?lb n j' \ ?horner n j' \ ?horner n j' \ ?ub n j'") ``` hoelzl@29805 ` 44` ```proof (induct n arbitrary: j') ``` hoelzl@29805 ` 45` ``` case 0 thus ?case unfolding lb_0 ub_0 horner.simps by auto ``` hoelzl@29805 ` 46` ```next ``` hoelzl@29805 ` 47` ``` case (Suc n) ``` hoelzl@31098 ` 48` ``` have "?lb (Suc n) j' \ ?horner (Suc n) j'" unfolding lb_Suc ub_Suc horner.simps real_of_float_sub diff_def ``` hoelzl@29805 ` 49` ``` proof (rule add_mono) ``` hoelzl@31098 ` 50` ``` show "real (lapprox_rat prec 1 (int (f j'))) \ 1 / real (f j')" using lapprox_rat[of prec 1 "int (f j')"] by auto ``` hoelzl@31098 ` 51` ``` from Suc[where j'="Suc j'", unfolded funpow.simps comp_def f_Suc, THEN conjunct2] `0 \ real x` ``` hoelzl@31098 ` 52` ``` show "- real (x * ub n (F ((F ^^ j') s)) (G ((F ^^ j') s) (f j')) x) \ - (real x * horner F G n (F ((F ^^ j') s)) (G ((F ^^ j') s) (f j')) (real x))" ``` hoelzl@31098 ` 53` ``` unfolding real_of_float_mult neg_le_iff_le by (rule mult_left_mono) ``` hoelzl@29805 ` 54` ``` qed ``` hoelzl@31098 ` 55` ``` moreover have "?horner (Suc n) j' \ ?ub (Suc n) j'" unfolding ub_Suc ub_Suc horner.simps real_of_float_sub diff_def ``` hoelzl@29805 ` 56` ``` proof (rule add_mono) ``` hoelzl@31098 ` 57` ``` show "1 / real (f j') \ real (rapprox_rat prec 1 (int (f j')))" using rapprox_rat[of 1 "int (f j')" prec] by auto ``` hoelzl@31098 ` 58` ``` from Suc[where j'="Suc j'", unfolded funpow.simps comp_def f_Suc, THEN conjunct1] `0 \ real x` ``` hoelzl@31809 ` 59` ``` show "- (real x * horner F G n (F ((F ^^ j') s)) (G ((F ^^ j') s) (f j')) (real x)) \ ``` hoelzl@31098 ` 60` ``` - real (x * lb n (F ((F ^^ j') s)) (G ((F ^^ j') s) (f j')) x)" ``` hoelzl@31098 ` 61` ``` unfolding real_of_float_mult neg_le_iff_le by (rule mult_left_mono) ``` hoelzl@29805 ` 62` ``` qed ``` hoelzl@29805 ` 63` ``` ultimately show ?case by blast ``` hoelzl@29805 ` 64` ```qed ``` hoelzl@29805 ` 65` hoelzl@29805 ` 66` ```subsection "Theorems for floating point functions implementing the horner scheme" ``` hoelzl@29805 ` 67` hoelzl@29805 ` 68` ```text {* ``` hoelzl@29805 ` 69` hoelzl@29805 ` 70` ```Here @{term_type "f :: nat \ nat"} is the sequence defining the Taylor series, the coefficients are ``` hoelzl@29805 ` 71` ```all alternating and reciprocs. We use @{term G} and @{term F} to describe the computation of @{term f}. ``` hoelzl@29805 ` 72` hoelzl@29805 ` 73` ```*} ``` hoelzl@29805 ` 74` hoelzl@29805 ` 75` ```lemma horner_bounds: fixes F :: "nat \ nat" and G :: "nat \ nat \ nat" ``` hoelzl@31098 ` 76` ``` assumes "0 \ real x" and f_Suc: "\n. f (Suc n) = G ((F ^^ n) s) (f n)" ``` hoelzl@29805 ` 77` ``` and lb_0: "\ i k x. lb 0 i k x = 0" ``` hoelzl@29805 ` 78` ``` and lb_Suc: "\ n i k x. lb (Suc n) i k x = lapprox_rat prec 1 (int k) - x * (ub n (F i) (G i k) x)" ``` hoelzl@29805 ` 79` ``` and ub_0: "\ i k x. ub 0 i k x = 0" ``` hoelzl@29805 ` 80` ``` and ub_Suc: "\ n i k x. ub (Suc n) i k x = rapprox_rat prec 1 (int k) - x * (lb n (F i) (G i k) x)" ``` hoelzl@31809 ` 81` ``` shows "real (lb n ((F ^^ j') s) (f j') x) \ (\j=0..j=0.. real (ub n ((F ^^ j') s) (f j') x)" (is "?ub") ``` hoelzl@29805 ` 83` ```proof - ``` hoelzl@31809 ` 84` ``` have "?lb \ ?ub" ``` hoelzl@31098 ` 85` ``` using horner_bounds'[where lb=lb, OF `0 \ real x` f_Suc lb_0 lb_Suc ub_0 ub_Suc] ``` hoelzl@29805 ` 86` ``` unfolding horner_schema[where f=f, OF f_Suc] . ``` hoelzl@29805 ` 87` ``` thus "?lb" and "?ub" by auto ``` hoelzl@29805 ` 88` ```qed ``` hoelzl@29805 ` 89` hoelzl@29805 ` 90` ```lemma horner_bounds_nonpos: fixes F :: "nat \ nat" and G :: "nat \ nat \ nat" ``` hoelzl@31098 ` 91` ``` assumes "real x \ 0" and f_Suc: "\n. f (Suc n) = G ((F ^^ n) s) (f n)" ``` hoelzl@29805 ` 92` ``` and lb_0: "\ i k x. lb 0 i k x = 0" ``` hoelzl@29805 ` 93` ``` and lb_Suc: "\ n i k x. lb (Suc n) i k x = lapprox_rat prec 1 (int k) + x * (ub n (F i) (G i k) x)" ``` hoelzl@29805 ` 94` ``` and ub_0: "\ i k x. ub 0 i k x = 0" ``` hoelzl@29805 ` 95` ``` and ub_Suc: "\ n i k x. ub (Suc n) i k x = rapprox_rat prec 1 (int k) + x * (lb n (F i) (G i k) x)" ``` hoelzl@31809 ` 96` ``` shows "real (lb n ((F ^^ j') s) (f j') x) \ (\j=0..j=0.. real (ub n ((F ^^ j') s) (f j') x)" (is "?ub") ``` hoelzl@29805 ` 98` ```proof - ``` hoelzl@29805 ` 99` ``` { fix x y z :: float have "x - y * z = x + - y * z" ``` haftmann@30968 ` 100` ``` by (cases x, cases y, cases z, simp add: plus_float.simps minus_float_def uminus_float.simps times_float.simps algebra_simps) ``` hoelzl@29805 ` 101` ``` } note diff_mult_minus = this ``` hoelzl@29805 ` 102` hoelzl@29805 ` 103` ``` { fix x :: float have "- (- x) = x" by (cases x, auto simp add: uminus_float.simps) } note minus_minus = this ``` hoelzl@29805 ` 104` hoelzl@31098 ` 105` ``` have move_minus: "real (-x) = -1 * real x" by auto ``` hoelzl@29805 ` 106` hoelzl@31809 ` 107` ``` have sum_eq: "(\j=0..j = 0.. {0 ..< n}" ``` hoelzl@31098 ` 111` ``` show "1 / real (f (j' + j)) * real x ^ j = -1 ^ j * (1 / real (f (j' + j))) * real (- x) ^ j" ``` hoelzl@29805 ` 112` ``` unfolding move_minus power_mult_distrib real_mult_assoc[symmetric] ``` haftmann@30952 ` 113` ``` unfolding real_mult_commute unfolding real_mult_assoc[of "-1 ^ j", symmetric] power_mult_distrib[symmetric] ``` hoelzl@29805 ` 114` ``` by auto ``` hoelzl@29805 ` 115` ``` qed ``` hoelzl@29805 ` 116` hoelzl@31098 ` 117` ``` have "0 \ real (-x)" using assms by auto ``` hoelzl@29805 ` 118` ``` from horner_bounds[where G=G and F=F and f=f and s=s and prec=prec ``` hoelzl@29805 ` 119` ``` and lb="\ n i k x. lb n i k (-x)" and ub="\ n i k x. ub n i k (-x)", unfolded lb_Suc ub_Suc diff_mult_minus, ``` hoelzl@29805 ` 120` ``` OF this f_Suc lb_0 refl ub_0 refl] ``` hoelzl@29805 ` 121` ``` show "?lb" and "?ub" unfolding minus_minus sum_eq ``` hoelzl@29805 ` 122` ``` by auto ``` hoelzl@29805 ` 123` ```qed ``` hoelzl@29805 ` 124` hoelzl@29805 ` 125` ```subsection {* Selectors for next even or odd number *} ``` hoelzl@29805 ` 126` hoelzl@29805 ` 127` ```text {* ``` hoelzl@29805 ` 128` hoelzl@29805 ` 129` ```The horner scheme computes alternating series. To get the upper and lower bounds we need to ``` hoelzl@29805 ` 130` ```guarantee to access a even or odd member. To do this we use @{term get_odd} and @{term get_even}. ``` hoelzl@29805 ` 131` hoelzl@29805 ` 132` ```*} ``` hoelzl@29805 ` 133` hoelzl@29805 ` 134` ```definition get_odd :: "nat \ nat" where ``` hoelzl@29805 ` 135` ``` "get_odd n = (if odd n then n else (Suc n))" ``` hoelzl@29805 ` 136` hoelzl@29805 ` 137` ```definition get_even :: "nat \ nat" where ``` hoelzl@29805 ` 138` ``` "get_even n = (if even n then n else (Suc n))" ``` hoelzl@29805 ` 139` hoelzl@29805 ` 140` ```lemma get_odd[simp]: "odd (get_odd n)" unfolding get_odd_def by (cases "odd n", auto) ``` hoelzl@29805 ` 141` ```lemma get_even[simp]: "even (get_even n)" unfolding get_even_def by (cases "even n", auto) ``` hoelzl@29805 ` 142` ```lemma get_odd_ex: "\ k. Suc k = get_odd n \ odd (Suc k)" ``` hoelzl@29805 ` 143` ```proof (cases "odd n") ``` hoelzl@29805 ` 144` ``` case True hence "0 < n" by (rule odd_pos) ``` hoelzl@31467 ` 145` ``` from gr0_implies_Suc[OF this] obtain k where "Suc k = n" by auto ``` hoelzl@29805 ` 146` ``` thus ?thesis unfolding get_odd_def if_P[OF True] using True[unfolded `Suc k = n`[symmetric]] by blast ``` hoelzl@29805 ` 147` ```next ``` hoelzl@29805 ` 148` ``` case False hence "odd (Suc n)" by auto ``` hoelzl@29805 ` 149` ``` thus ?thesis unfolding get_odd_def if_not_P[OF False] by blast ``` hoelzl@29805 ` 150` ```qed ``` hoelzl@29805 ` 151` hoelzl@29805 ` 152` ```lemma get_even_double: "\i. get_even n = 2 * i" using get_even[unfolded even_mult_two_ex] . ``` hoelzl@29805 ` 153` ```lemma get_odd_double: "\i. get_odd n = 2 * i + 1" using get_odd[unfolded odd_Suc_mult_two_ex] by auto ``` hoelzl@29805 ` 154` hoelzl@29805 ` 155` ```section "Power function" ``` hoelzl@29805 ` 156` hoelzl@29805 ` 157` ```definition float_power_bnds :: "nat \ float \ float \ float * float" where ``` hoelzl@29805 ` 158` ```"float_power_bnds n l u = (if odd n \ 0 < l then (l ^ n, u ^ n) ``` hoelzl@29805 ` 159` ``` else if u < 0 then (u ^ n, l ^ n) ``` hoelzl@29805 ` 160` ``` else (0, (max (-l) u) ^ n))" ``` hoelzl@29805 ` 161` hoelzl@31098 ` 162` ```lemma float_power_bnds: assumes "(l1, u1) = float_power_bnds n l u" and "x \ {real l .. real u}" ``` hoelzl@31098 ` 163` ``` shows "x ^ n \ {real l1..real u1}" ``` hoelzl@29805 ` 164` ```proof (cases "even n") ``` hoelzl@31467 ` 165` ``` case True ``` hoelzl@29805 ` 166` ``` show ?thesis ``` hoelzl@29805 ` 167` ``` proof (cases "0 < l") ``` hoelzl@31098 ` 168` ``` case True hence "odd n \ 0 < l" and "0 \ real l" unfolding less_float_def by auto ``` hoelzl@29805 ` 169` ``` have u1: "u1 = u ^ n" and l1: "l1 = l ^ n" using assms unfolding float_power_bnds_def if_P[OF `odd n \ 0 < l`] by auto ``` hoelzl@31098 ` 170` ``` have "real l ^ n \ x ^ n" and "x ^ n \ real u ^ n " using `0 \ real l` and assms unfolding atLeastAtMost_iff using power_mono[of "real l" x] power_mono[of x "real u"] by auto ``` hoelzl@29805 ` 171` ``` thus ?thesis using assms `0 < l` unfolding atLeastAtMost_iff l1 u1 float_power less_float_def by auto ``` hoelzl@29805 ` 172` ``` next ``` hoelzl@29805 ` 173` ``` case False hence P: "\ (odd n \ 0 < l)" using `even n` by auto ``` hoelzl@29805 ` 174` ``` show ?thesis ``` hoelzl@29805 ` 175` ``` proof (cases "u < 0") ``` hoelzl@31098 ` 176` ``` case True hence "0 \ - real u" and "- real u \ - x" and "0 \ - x" and "-x \ - real l" using assms unfolding less_float_def by auto ``` hoelzl@31809 ` 177` ``` hence "real u ^ n \ x ^ n" and "x ^ n \ real l ^ n" using power_mono[of "-x" "-real l" n] power_mono[of "-real u" "-x" n] ``` hoelzl@29805 ` 178` ``` unfolding power_minus_even[OF `even n`] by auto ``` hoelzl@29805 ` 179` ``` moreover have u1: "u1 = l ^ n" and l1: "l1 = u ^ n" using assms unfolding float_power_bnds_def if_not_P[OF P] if_P[OF True] by auto ``` hoelzl@29805 ` 180` ``` ultimately show ?thesis using float_power by auto ``` hoelzl@29805 ` 181` ``` next ``` hoelzl@31467 ` 182` ``` case False ``` hoelzl@31098 ` 183` ``` have "\x\ \ real (max (-l) u)" ``` hoelzl@29805 ` 184` ``` proof (cases "-l \ u") ``` hoelzl@29805 ` 185` ``` case True thus ?thesis unfolding max_def if_P[OF True] using assms unfolding le_float_def by auto ``` hoelzl@29805 ` 186` ``` next ``` hoelzl@29805 ` 187` ``` case False thus ?thesis unfolding max_def if_not_P[OF False] using assms unfolding le_float_def by auto ``` hoelzl@29805 ` 188` ``` qed ``` hoelzl@31098 ` 189` ``` hence x_abs: "\x\ \ \real (max (-l) u)\" by auto ``` hoelzl@29805 ` 190` ``` have u1: "u1 = (max (-l) u) ^ n" and l1: "l1 = 0" using assms unfolding float_power_bnds_def if_not_P[OF P] if_not_P[OF False] by auto ``` hoelzl@29805 ` 191` ``` show ?thesis unfolding atLeastAtMost_iff l1 u1 float_power using zero_le_even_power[OF `even n`] power_mono_even[OF `even n` x_abs] by auto ``` hoelzl@29805 ` 192` ``` qed ``` hoelzl@29805 ` 193` ``` qed ``` hoelzl@29805 ` 194` ```next ``` hoelzl@29805 ` 195` ``` case False hence "odd n \ 0 < l" by auto ``` hoelzl@29805 ` 196` ``` have u1: "u1 = u ^ n" and l1: "l1 = l ^ n" using assms unfolding float_power_bnds_def if_P[OF `odd n \ 0 < l`] by auto ``` hoelzl@31098 ` 197` ``` have "real l ^ n \ x ^ n" and "x ^ n \ real u ^ n " using assms unfolding atLeastAtMost_iff using power_mono_odd[OF False] by auto ``` hoelzl@29805 ` 198` ``` thus ?thesis unfolding atLeastAtMost_iff l1 u1 float_power less_float_def by auto ``` hoelzl@29805 ` 199` ```qed ``` hoelzl@29805 ` 200` hoelzl@31098 ` 201` ```lemma bnds_power: "\ x l u. (l1, u1) = float_power_bnds n l u \ x \ {real l .. real u} \ real l1 \ x ^ n \ x ^ n \ real u1" ``` hoelzl@29805 ` 202` ``` using float_power_bnds by auto ``` hoelzl@29805 ` 203` hoelzl@29805 ` 204` ```section "Square root" ``` hoelzl@29805 ` 205` hoelzl@29805 ` 206` ```text {* ``` hoelzl@29805 ` 207` hoelzl@29805 ` 208` ```The square root computation is implemented as newton iteration. As first first step we use the ``` hoelzl@29805 ` 209` ```nearest power of two greater than the square root. ``` hoelzl@29805 ` 210` hoelzl@29805 ` 211` ```*} ``` hoelzl@29805 ` 212` hoelzl@29805 ` 213` ```fun sqrt_iteration :: "nat \ nat \ float \ float" where ``` hoelzl@29805 ` 214` ```"sqrt_iteration prec 0 (Float m e) = Float 1 ((e + bitlen m) div 2 + 1)" | ``` hoelzl@31467 ` 215` ```"sqrt_iteration prec (Suc m) x = (let y = sqrt_iteration prec m x ``` hoelzl@29805 ` 216` ``` in Float 1 -1 * (y + float_divr prec x y))" ``` hoelzl@29805 ` 217` hoelzl@31467 ` 218` ```function ub_sqrt lb_sqrt :: "nat \ float \ float" where ``` hoelzl@31467 ` 219` ```"ub_sqrt prec x = (if 0 < x then (sqrt_iteration prec prec x) ``` hoelzl@31467 ` 220` ``` else if x < 0 then - lb_sqrt prec (- x) ``` hoelzl@31467 ` 221` ``` else 0)" | ``` hoelzl@31467 ` 222` ```"lb_sqrt prec x = (if 0 < x then (float_divl prec x (sqrt_iteration prec prec x)) ``` hoelzl@31467 ` 223` ``` else if x < 0 then - ub_sqrt prec (- x) ``` hoelzl@31467 ` 224` ``` else 0)" ``` hoelzl@31467 ` 225` ```by pat_completeness auto ``` hoelzl@31467 ` 226` ```termination by (relation "measure (\ v. let (prec, x) = sum_case id id v in (if x < 0 then 1 else 0))", auto simp add: less_float_def) ``` hoelzl@29805 ` 227` hoelzl@31467 ` 228` ```declare lb_sqrt.simps[simp del] ``` hoelzl@31467 ` 229` ```declare ub_sqrt.simps[simp del] ``` hoelzl@29805 ` 230` hoelzl@29805 ` 231` ```lemma sqrt_ub_pos_pos_1: ``` hoelzl@29805 ` 232` ``` assumes "sqrt x < b" and "0 < b" and "0 < x" ``` hoelzl@29805 ` 233` ``` shows "sqrt x < (b + x / b)/2" ``` hoelzl@29805 ` 234` ```proof - ``` hoelzl@29805 ` 235` ``` from assms have "0 < (b - sqrt x) ^ 2 " by simp ``` hoelzl@29805 ` 236` ``` also have "\ = b ^ 2 - 2 * b * sqrt x + (sqrt x) ^ 2" by algebra ``` hoelzl@29805 ` 237` ``` also have "\ = b ^ 2 - 2 * b * sqrt x + x" using assms by (simp add: real_sqrt_pow2) ``` hoelzl@29805 ` 238` ``` finally have "0 < b ^ 2 - 2 * b * sqrt x + x" by assumption ``` hoelzl@29805 ` 239` ``` hence "0 < b / 2 - sqrt x + x / (2 * b)" using assms ``` hoelzl@29805 ` 240` ``` by (simp add: field_simps power2_eq_square) ``` hoelzl@29805 ` 241` ``` thus ?thesis by (simp add: field_simps) ``` hoelzl@29805 ` 242` ```qed ``` hoelzl@29805 ` 243` hoelzl@31098 ` 244` ```lemma sqrt_iteration_bound: assumes "0 < real x" ``` hoelzl@31098 ` 245` ``` shows "sqrt (real x) < real (sqrt_iteration prec n x)" ``` hoelzl@29805 ` 246` ```proof (induct n) ``` hoelzl@29805 ` 247` ``` case 0 ``` hoelzl@29805 ` 248` ``` show ?case ``` hoelzl@29805 ` 249` ``` proof (cases x) ``` hoelzl@29805 ` 250` ``` case (Float m e) ``` hoelzl@29805 ` 251` ``` hence "0 < m" using float_pos_m_pos[unfolded less_float_def] assms by auto ``` hoelzl@29805 ` 252` ``` hence "0 < sqrt (real m)" by auto ``` hoelzl@29805 ` 253` hoelzl@29805 ` 254` ``` have int_nat_bl: "int (nat (bitlen m)) = bitlen m" using bitlen_ge0 by auto ``` hoelzl@29805 ` 255` hoelzl@31098 ` 256` ``` have "real x = (real m / 2^nat (bitlen m)) * pow2 (e + int (nat (bitlen m)))" ``` hoelzl@31098 ` 257` ``` unfolding pow2_add pow2_int Float real_of_float_simp by auto ``` hoelzl@29805 ` 258` ``` also have "\ < 1 * pow2 (e + int (nat (bitlen m)))" ``` hoelzl@29805 ` 259` ``` proof (rule mult_strict_right_mono, auto) ``` hoelzl@31467 ` 260` ``` show "real m < 2^nat (bitlen m)" using bitlen_bounds[OF `0 < m`, THEN conjunct2] ``` hoelzl@29805 ` 261` ``` unfolding real_of_int_less_iff[of m, symmetric] by auto ``` hoelzl@29805 ` 262` ``` qed ``` hoelzl@31098 ` 263` ``` finally have "sqrt (real x) < sqrt (pow2 (e + bitlen m))" unfolding int_nat_bl by auto ``` hoelzl@29805 ` 264` ``` also have "\ \ pow2 ((e + bitlen m) div 2 + 1)" ``` hoelzl@29805 ` 265` ``` proof - ``` hoelzl@29805 ` 266` ``` let ?E = "e + bitlen m" ``` hoelzl@29805 ` 267` ``` have E_mod_pow: "pow2 (?E mod 2) < 4" ``` hoelzl@29805 ` 268` ``` proof (cases "?E mod 2 = 1") ``` hoelzl@29805 ` 269` ``` case True thus ?thesis by auto ``` hoelzl@29805 ` 270` ``` next ``` hoelzl@31467 ` 271` ``` case False ``` hoelzl@31467 ` 272` ``` have "0 \ ?E mod 2" by auto ``` hoelzl@29805 ` 273` ``` have "?E mod 2 < 2" by auto ``` hoelzl@29805 ` 274` ``` from this[THEN zless_imp_add1_zle] ``` hoelzl@29805 ` 275` ``` have "?E mod 2 \ 0" using False by auto ``` hoelzl@29805 ` 276` ``` from xt1(5)[OF `0 \ ?E mod 2` this] ``` hoelzl@29805 ` 277` ``` show ?thesis by auto ``` hoelzl@29805 ` 278` ``` qed ``` hoelzl@29805 ` 279` ``` hence "sqrt (pow2 (?E mod 2)) < sqrt (2 * 2)" by auto ``` hoelzl@29805 ` 280` ``` hence E_mod_pow: "sqrt (pow2 (?E mod 2)) < 2" unfolding real_sqrt_abs2 by auto ``` hoelzl@29805 ` 281` hoelzl@29805 ` 282` ``` have E_eq: "pow2 ?E = pow2 (?E div 2 + ?E div 2 + ?E mod 2)" by auto ``` hoelzl@29805 ` 283` ``` have "sqrt (pow2 ?E) = sqrt (pow2 (?E div 2) * pow2 (?E div 2) * pow2 (?E mod 2))" ``` hoelzl@29805 ` 284` ``` unfolding E_eq unfolding pow2_add .. ``` hoelzl@29805 ` 285` ``` also have "\ = pow2 (?E div 2) * sqrt (pow2 (?E mod 2))" ``` hoelzl@29805 ` 286` ``` unfolding real_sqrt_mult[of _ "pow2 (?E mod 2)"] real_sqrt_abs2 by auto ``` hoelzl@31467 ` 287` ``` also have "\ < pow2 (?E div 2) * 2" ``` hoelzl@29805 ` 288` ``` by (rule mult_strict_left_mono, auto intro: E_mod_pow) ``` hoelzl@29805 ` 289` ``` also have "\ = pow2 (?E div 2 + 1)" unfolding zadd_commute[of _ 1] pow2_add1 by auto ``` hoelzl@29805 ` 290` ``` finally show ?thesis by auto ``` hoelzl@29805 ` 291` ``` qed ``` hoelzl@31467 ` 292` ``` finally show ?thesis ``` hoelzl@31098 ` 293` ``` unfolding Float sqrt_iteration.simps real_of_float_simp by auto ``` hoelzl@29805 ` 294` ``` qed ``` hoelzl@29805 ` 295` ```next ``` hoelzl@29805 ` 296` ``` case (Suc n) ``` hoelzl@29805 ` 297` ``` let ?b = "sqrt_iteration prec n x" ``` hoelzl@31098 ` 298` ``` have "0 < sqrt (real x)" using `0 < real x` by auto ``` hoelzl@31098 ` 299` ``` also have "\ < real ?b" using Suc . ``` hoelzl@31098 ` 300` ``` finally have "sqrt (real x) < (real ?b + real x / real ?b)/2" using sqrt_ub_pos_pos_1[OF Suc _ `0 < real x`] by auto ``` hoelzl@31098 ` 301` ``` also have "\ \ (real ?b + real (float_divr prec x ?b))/2" by (rule divide_right_mono, auto simp add: float_divr) ``` hoelzl@31098 ` 302` ``` also have "\ = real (Float 1 -1) * (real ?b + real (float_divr prec x ?b))" by auto ``` hoelzl@31098 ` 303` ``` finally show ?case unfolding sqrt_iteration.simps Let_def real_of_float_mult real_of_float_add right_distrib . ``` hoelzl@29805 ` 304` ```qed ``` hoelzl@29805 ` 305` hoelzl@31098 ` 306` ```lemma sqrt_iteration_lower_bound: assumes "0 < real x" ``` hoelzl@31098 ` 307` ``` shows "0 < real (sqrt_iteration prec n x)" (is "0 < ?sqrt") ``` hoelzl@29805 ` 308` ```proof - ``` hoelzl@31098 ` 309` ``` have "0 < sqrt (real x)" using assms by auto ``` hoelzl@29805 ` 310` ``` also have "\ < ?sqrt" using sqrt_iteration_bound[OF assms] . ``` hoelzl@29805 ` 311` ``` finally show ?thesis . ``` hoelzl@29805 ` 312` ```qed ``` hoelzl@29805 ` 313` hoelzl@31098 ` 314` ```lemma lb_sqrt_lower_bound: assumes "0 \ real x" ``` hoelzl@31467 ` 315` ``` shows "0 \ real (lb_sqrt prec x)" ``` hoelzl@29805 ` 316` ```proof (cases "0 < x") ``` hoelzl@31098 ` 317` ``` case True hence "0 < real x" and "0 \ x" using `0 \ real x` unfolding less_float_def le_float_def by auto ``` hoelzl@31809 ` 318` ``` hence "0 < sqrt_iteration prec prec x" unfolding less_float_def using sqrt_iteration_lower_bound by auto ``` hoelzl@31098 ` 319` ``` hence "0 \ real (float_divl prec x (sqrt_iteration prec prec x))" using float_divl_lower_bound[OF `0 \ x`] unfolding le_float_def by auto ``` hoelzl@31467 ` 320` ``` thus ?thesis unfolding lb_sqrt.simps using True by auto ``` hoelzl@29805 ` 321` ```next ``` hoelzl@31098 ` 322` ``` case False with `0 \ real x` have "real x = 0" unfolding less_float_def by auto ``` hoelzl@31467 ` 323` ``` thus ?thesis unfolding lb_sqrt.simps less_float_def by auto ``` hoelzl@29805 ` 324` ```qed ``` hoelzl@29805 ` 325` hoelzl@31467 ` 326` ```lemma bnds_sqrt': ``` hoelzl@31467 ` 327` ``` shows "sqrt (real x) \ { real (lb_sqrt prec x) .. real (ub_sqrt prec x) }" ``` hoelzl@31467 ` 328` ```proof - ``` hoelzl@31467 ` 329` ``` { fix x :: float assume "0 < x" ``` hoelzl@31467 ` 330` ``` hence "0 < real x" and "0 \ real x" unfolding less_float_def by auto ``` hoelzl@31467 ` 331` ``` hence sqrt_gt0: "0 < sqrt (real x)" by auto ``` hoelzl@31467 ` 332` ``` hence sqrt_ub: "sqrt (real x) < real (sqrt_iteration prec prec x)" using sqrt_iteration_bound by auto ``` hoelzl@31467 ` 333` hoelzl@31467 ` 334` ``` have "real (float_divl prec x (sqrt_iteration prec prec x)) \ ``` hoelzl@31467 ` 335` ``` real x / real (sqrt_iteration prec prec x)" by (rule float_divl) ``` hoelzl@31467 ` 336` ``` also have "\ < real x / sqrt (real x)" ``` hoelzl@31467 ` 337` ``` by (rule divide_strict_left_mono[OF sqrt_ub `0 < real x` ``` hoelzl@31467 ` 338` ``` mult_pos_pos[OF order_less_trans[OF sqrt_gt0 sqrt_ub] sqrt_gt0]]) ``` hoelzl@31809 ` 339` ``` also have "\ = sqrt (real x)" ``` hoelzl@31467 ` 340` ``` unfolding inverse_eq_iff_eq[of _ "sqrt (real x)", symmetric] ``` hoelzl@31467 ` 341` ``` sqrt_divide_self_eq[OF `0 \ real x`, symmetric] by auto ``` hoelzl@31467 ` 342` ``` finally have "real (lb_sqrt prec x) \ sqrt (real x)" ``` hoelzl@31467 ` 343` ``` unfolding lb_sqrt.simps if_P[OF `0 < x`] by auto } ``` hoelzl@31467 ` 344` ``` note lb = this ``` hoelzl@31467 ` 345` hoelzl@31467 ` 346` ``` { fix x :: float assume "0 < x" ``` hoelzl@31467 ` 347` ``` hence "0 < real x" unfolding less_float_def by auto ``` hoelzl@31467 ` 348` ``` hence "0 < sqrt (real x)" by auto ``` hoelzl@31467 ` 349` ``` hence "sqrt (real x) < real (sqrt_iteration prec prec x)" ``` hoelzl@31467 ` 350` ``` using sqrt_iteration_bound by auto ``` hoelzl@31467 ` 351` ``` hence "sqrt (real x) \ real (ub_sqrt prec x)" ``` hoelzl@31467 ` 352` ``` unfolding ub_sqrt.simps if_P[OF `0 < x`] by auto } ``` hoelzl@31467 ` 353` ``` note ub = this ``` hoelzl@31467 ` 354` hoelzl@31467 ` 355` ``` show ?thesis ``` hoelzl@31467 ` 356` ``` proof (cases "0 < x") ``` hoelzl@31467 ` 357` ``` case True with lb ub show ?thesis by auto ``` hoelzl@31467 ` 358` ``` next case False show ?thesis ``` hoelzl@31467 ` 359` ``` proof (cases "real x = 0") ``` hoelzl@31809 ` 360` ``` case True thus ?thesis ``` hoelzl@31467 ` 361` ``` by (auto simp add: less_float_def lb_sqrt.simps ub_sqrt.simps) ``` hoelzl@31467 ` 362` ``` next ``` hoelzl@31467 ` 363` ``` case False with `\ 0 < x` have "x < 0" and "0 < -x" ``` hoelzl@31467 ` 364` ``` by (auto simp add: less_float_def) ``` hoelzl@31467 ` 365` hoelzl@31467 ` 366` ``` with `\ 0 < x` ``` hoelzl@31467 ` 367` ``` show ?thesis using lb[OF `0 < -x`] ub[OF `0 < -x`] ``` hoelzl@31467 ` 368` ``` by (auto simp add: real_sqrt_minus lb_sqrt.simps ub_sqrt.simps) ``` hoelzl@31467 ` 369` ``` qed qed ``` hoelzl@29805 ` 370` ```qed ``` hoelzl@29805 ` 371` hoelzl@31467 ` 372` ```lemma bnds_sqrt: "\ x lx ux. (l, u) = (lb_sqrt prec lx, ub_sqrt prec ux) \ x \ {real lx .. real ux} \ real l \ sqrt x \ sqrt x \ real u" ``` hoelzl@31467 ` 373` ```proof ((rule allI) +, rule impI, erule conjE, rule conjI) ``` hoelzl@31467 ` 374` ``` fix x lx ux ``` hoelzl@31467 ` 375` ``` assume "(l, u) = (lb_sqrt prec lx, ub_sqrt prec ux)" ``` hoelzl@31467 ` 376` ``` and x: "x \ {real lx .. real ux}" ``` hoelzl@31467 ` 377` ``` hence l: "l = lb_sqrt prec lx " and u: "u = ub_sqrt prec ux" by auto ``` hoelzl@29805 ` 378` hoelzl@31467 ` 379` ``` have "sqrt (real lx) \ sqrt x" using x by auto ``` hoelzl@31467 ` 380` ``` from order_trans[OF _ this] ``` hoelzl@31467 ` 381` ``` show "real l \ sqrt x" unfolding l using bnds_sqrt'[of lx prec] by auto ``` hoelzl@29805 ` 382` hoelzl@31467 ` 383` ``` have "sqrt x \ sqrt (real ux)" using x by auto ``` hoelzl@31467 ` 384` ``` from order_trans[OF this] ``` hoelzl@31467 ` 385` ``` show "sqrt x \ real u" unfolding u using bnds_sqrt'[of ux prec] by auto ``` hoelzl@29805 ` 386` ```qed ``` hoelzl@29805 ` 387` hoelzl@29805 ` 388` ```section "Arcus tangens and \" ``` hoelzl@29805 ` 389` hoelzl@29805 ` 390` ```subsection "Compute arcus tangens series" ``` hoelzl@29805 ` 391` hoelzl@29805 ` 392` ```text {* ``` hoelzl@29805 ` 393` hoelzl@29805 ` 394` ```As first step we implement the computation of the arcus tangens series. This is only valid in the range ``` hoelzl@29805 ` 395` ```@{term "{-1 :: real .. 1}"}. This is used to compute \ and then the entire arcus tangens. ``` hoelzl@29805 ` 396` hoelzl@29805 ` 397` ```*} ``` hoelzl@29805 ` 398` hoelzl@29805 ` 399` ```fun ub_arctan_horner :: "nat \ nat \ nat \ float \ float" ``` hoelzl@29805 ` 400` ```and lb_arctan_horner :: "nat \ nat \ nat \ float \ float" where ``` hoelzl@29805 ` 401` ``` "ub_arctan_horner prec 0 k x = 0" ``` hoelzl@31809 ` 402` ```| "ub_arctan_horner prec (Suc n) k x = ``` hoelzl@29805 ` 403` ``` (rapprox_rat prec 1 (int k)) - x * (lb_arctan_horner prec n (k + 2) x)" ``` hoelzl@29805 ` 404` ```| "lb_arctan_horner prec 0 k x = 0" ``` hoelzl@31809 ` 405` ```| "lb_arctan_horner prec (Suc n) k x = ``` hoelzl@29805 ` 406` ``` (lapprox_rat prec 1 (int k)) - x * (ub_arctan_horner prec n (k + 2) x)" ``` hoelzl@29805 ` 407` hoelzl@31098 ` 408` ```lemma arctan_0_1_bounds': assumes "0 \ real x" "real x \ 1" and "even n" ``` hoelzl@31098 ` 409` ``` shows "arctan (real x) \ {real (x * lb_arctan_horner prec n 1 (x * x)) .. real (x * ub_arctan_horner prec (Suc n) 1 (x * x))}" ``` hoelzl@29805 ` 410` ```proof - ``` hoelzl@31098 ` 411` ``` let "?c i" = "-1^i * (1 / real (i * 2 + 1) * real x ^ (i * 2 + 1))" ``` hoelzl@29805 ` 412` ``` let "?S n" = "\ i=0.. real (x * x)" by auto ``` hoelzl@29805 ` 415` ``` from `even n` obtain m where "2 * m = n" unfolding even_mult_two_ex by auto ``` hoelzl@31809 ` 416` hoelzl@31098 ` 417` ``` have "arctan (real x) \ { ?S n .. ?S (Suc n) }" ``` hoelzl@31098 ` 418` ``` proof (cases "real x = 0") ``` hoelzl@29805 ` 419` ``` case False ``` hoelzl@31098 ` 420` ``` hence "0 < real x" using `0 \ real x` by auto ``` hoelzl@31809 ` 421` ``` hence prem: "0 < 1 / real (0 * 2 + (1::nat)) * real x ^ (0 * 2 + 1)" by auto ``` hoelzl@29805 ` 422` hoelzl@31098 ` 423` ``` have "\ real x \ \ 1" using `0 \ real x` `real x \ 1` by auto ``` hoelzl@29805 ` 424` ``` from mp[OF summable_Leibniz(2)[OF zeroseq_arctan_series[OF this] monoseq_arctan_series[OF this]] prem, THEN spec, of m, unfolded `2 * m = n`] ``` nipkow@31790 ` 425` ``` show ?thesis unfolding arctan_series[OF `\ real x \ \ 1`] Suc_eq_plus1 . ``` hoelzl@29805 ` 426` ``` qed auto ``` hoelzl@29805 ` 427` ``` note arctan_bounds = this[unfolded atLeastAtMost_iff] ``` hoelzl@29805 ` 428` hoelzl@29805 ` 429` ``` have F: "\n. 2 * Suc n + 1 = 2 * n + 1 + 2" by auto ``` hoelzl@29805 ` 430` hoelzl@31809 ` 431` ``` note bounds = horner_bounds[where s=1 and f="\i. 2 * i + 1" and j'=0 ``` hoelzl@29805 ` 432` ``` and lb="\n i k x. lb_arctan_horner prec n k x" ``` hoelzl@31809 ` 433` ``` and ub="\n i k x. ub_arctan_horner prec n k x", ``` hoelzl@31098 ` 434` ``` OF `0 \ real (x*x)` F lb_arctan_horner.simps ub_arctan_horner.simps] ``` hoelzl@29805 ` 435` hoelzl@31098 ` 436` ``` { have "real (x * lb_arctan_horner prec n 1 (x*x)) \ ?S n" ``` hoelzl@31098 ` 437` ``` using bounds(1) `0 \ real x` ``` hoelzl@31098 ` 438` ``` unfolding real_of_float_mult power_add power_one_right real_mult_assoc[symmetric] setsum_left_distrib[symmetric] ``` hoelzl@31098 ` 439` ``` unfolding real_mult_commute mult_commute[of _ "2::nat"] power_mult power2_eq_square[of "real x"] ``` hoelzl@29805 ` 440` ``` by (auto intro!: mult_left_mono) ``` hoelzl@31098 ` 441` ``` also have "\ \ arctan (real x)" using arctan_bounds .. ``` hoelzl@31098 ` 442` ``` finally have "real (x * lb_arctan_horner prec n 1 (x*x)) \ arctan (real x)" . } ``` hoelzl@29805 ` 443` ``` moreover ``` hoelzl@31098 ` 444` ``` { have "arctan (real x) \ ?S (Suc n)" using arctan_bounds .. ``` hoelzl@31098 ` 445` ``` also have "\ \ real (x * ub_arctan_horner prec (Suc n) 1 (x*x))" ``` hoelzl@31098 ` 446` ``` using bounds(2)[of "Suc n"] `0 \ real x` ``` hoelzl@31098 ` 447` ``` unfolding real_of_float_mult power_add power_one_right real_mult_assoc[symmetric] setsum_left_distrib[symmetric] ``` hoelzl@31098 ` 448` ``` unfolding real_mult_commute mult_commute[of _ "2::nat"] power_mult power2_eq_square[of "real x"] ``` hoelzl@29805 ` 449` ``` by (auto intro!: mult_left_mono) ``` hoelzl@31098 ` 450` ``` finally have "arctan (real x) \ real (x * ub_arctan_horner prec (Suc n) 1 (x*x))" . } ``` hoelzl@29805 ` 451` ``` ultimately show ?thesis by auto ``` hoelzl@29805 ` 452` ```qed ``` hoelzl@29805 ` 453` hoelzl@31098 ` 454` ```lemma arctan_0_1_bounds: assumes "0 \ real x" "real x \ 1" ``` hoelzl@31098 ` 455` ``` shows "arctan (real x) \ {real (x * lb_arctan_horner prec (get_even n) 1 (x * x)) .. real (x * ub_arctan_horner prec (get_odd n) 1 (x * x))}" ``` hoelzl@29805 ` 456` ```proof (cases "even n") ``` hoelzl@29805 ` 457` ``` case True ``` hoelzl@29805 ` 458` ``` obtain n' where "Suc n' = get_odd n" and "odd (Suc n')" using get_odd_ex by auto ``` nipkow@31148 ` 459` ``` hence "even n'" unfolding even_Suc by auto ``` hoelzl@31098 ` 460` ``` have "arctan (real x) \ real (x * ub_arctan_horner prec (get_odd n) 1 (x * x))" ``` hoelzl@31098 ` 461` ``` unfolding `Suc n' = get_odd n`[symmetric] using arctan_0_1_bounds'[OF `0 \ real x` `real x \ 1` `even n'`] by auto ``` hoelzl@29805 ` 462` ``` moreover ``` hoelzl@31098 ` 463` ``` have "real (x * lb_arctan_horner prec (get_even n) 1 (x * x)) \ arctan (real x)" ``` hoelzl@31098 ` 464` ``` unfolding get_even_def if_P[OF True] using arctan_0_1_bounds'[OF `0 \ real x` `real x \ 1` `even n`] by auto ``` hoelzl@29805 ` 465` ``` ultimately show ?thesis by auto ``` hoelzl@29805 ` 466` ```next ``` hoelzl@29805 ` 467` ``` case False hence "0 < n" by (rule odd_pos) ``` hoelzl@29805 ` 468` ``` from gr0_implies_Suc[OF this] obtain n' where "n = Suc n'" .. ``` nipkow@31148 ` 469` ``` from False[unfolded this even_Suc] ``` hoelzl@29805 ` 470` ``` have "even n'" and "even (Suc (Suc n'))" by auto ``` hoelzl@29805 ` 471` ``` have "get_odd n = Suc n'" unfolding get_odd_def if_P[OF False] using `n = Suc n'` . ``` hoelzl@29805 ` 472` hoelzl@31098 ` 473` ``` have "arctan (real x) \ real (x * ub_arctan_horner prec (get_odd n) 1 (x * x))" ``` hoelzl@31098 ` 474` ``` unfolding `get_odd n = Suc n'` using arctan_0_1_bounds'[OF `0 \ real x` `real x \ 1` `even n'`] by auto ``` hoelzl@29805 ` 475` ``` moreover ``` hoelzl@31098 ` 476` ``` have "real (x * lb_arctan_horner prec (get_even n) 1 (x * x)) \ arctan (real x)" ``` hoelzl@31098 ` 477` ``` unfolding get_even_def if_not_P[OF False] unfolding `n = Suc n'` using arctan_0_1_bounds'[OF `0 \ real x` `real x \ 1` `even (Suc (Suc n'))`] by auto ``` hoelzl@29805 ` 478` ``` ultimately show ?thesis by auto ``` hoelzl@29805 ` 479` ```qed ``` hoelzl@29805 ` 480` hoelzl@29805 ` 481` ```subsection "Compute \" ``` hoelzl@29805 ` 482` hoelzl@29805 ` 483` ```definition ub_pi :: "nat \ float" where ``` hoelzl@31809 ` 484` ``` "ub_pi prec = (let A = rapprox_rat prec 1 5 ; ``` hoelzl@29805 ` 485` ``` B = lapprox_rat prec 1 239 ``` hoelzl@31809 ` 486` ``` in ((Float 1 2) * ((Float 1 2) * A * (ub_arctan_horner prec (get_odd (prec div 4 + 1)) 1 (A * A)) - ``` hoelzl@29805 ` 487` ``` B * (lb_arctan_horner prec (get_even (prec div 14 + 1)) 1 (B * B)))))" ``` hoelzl@29805 ` 488` hoelzl@29805 ` 489` ```definition lb_pi :: "nat \ float" where ``` hoelzl@31809 ` 490` ``` "lb_pi prec = (let A = lapprox_rat prec 1 5 ; ``` hoelzl@29805 ` 491` ``` B = rapprox_rat prec 1 239 ``` hoelzl@31809 ` 492` ``` in ((Float 1 2) * ((Float 1 2) * A * (lb_arctan_horner prec (get_even (prec div 4 + 1)) 1 (A * A)) - ``` hoelzl@29805 ` 493` ``` B * (ub_arctan_horner prec (get_odd (prec div 14 + 1)) 1 (B * B)))))" ``` hoelzl@29805 ` 494` hoelzl@31098 ` 495` ```lemma pi_boundaries: "pi \ {real (lb_pi n) .. real (ub_pi n)}" ``` hoelzl@29805 ` 496` ```proof - ``` hoelzl@29805 ` 497` ``` have machin_pi: "pi = 4 * (4 * arctan (1 / 5) - arctan (1 / 239))" unfolding machin[symmetric] by auto ``` hoelzl@29805 ` 498` hoelzl@29805 ` 499` ``` { fix prec n :: nat fix k :: int assume "1 < k" hence "0 \ k" and "0 < k" and "1 \ k" by auto ``` hoelzl@29805 ` 500` ``` let ?k = "rapprox_rat prec 1 k" ``` hoelzl@29805 ` 501` ``` have "1 div k = 0" using div_pos_pos_trivial[OF _ `1 < k`] by auto ``` hoelzl@31809 ` 502` hoelzl@31098 ` 503` ``` have "0 \ real ?k" by (rule order_trans[OF _ rapprox_rat], auto simp add: `0 \ k`) ``` hoelzl@31098 ` 504` ``` have "real ?k \ 1" unfolding rapprox_rat.simps(2)[OF zero_le_one `0 < k`] ``` hoelzl@29805 ` 505` ``` by (rule rapprox_posrat_le1, auto simp add: `0 < k` `1 \ k`) ``` hoelzl@29805 ` 506` hoelzl@31098 ` 507` ``` have "1 / real k \ real ?k" using rapprox_rat[where x=1 and y=k] by auto ``` hoelzl@31098 ` 508` ``` hence "arctan (1 / real k) \ arctan (real ?k)" by (rule arctan_monotone') ``` hoelzl@31098 ` 509` ``` also have "\ \ real (?k * ub_arctan_horner prec (get_odd n) 1 (?k * ?k))" ``` hoelzl@31098 ` 510` ``` using arctan_0_1_bounds[OF `0 \ real ?k` `real ?k \ 1`] by auto ``` hoelzl@31098 ` 511` ``` finally have "arctan (1 / (real k)) \ real (?k * ub_arctan_horner prec (get_odd n) 1 (?k * ?k))" . ``` hoelzl@29805 ` 512` ``` } note ub_arctan = this ``` hoelzl@29805 ` 513` hoelzl@29805 ` 514` ``` { fix prec n :: nat fix k :: int assume "1 < k" hence "0 \ k" and "0 < k" by auto ``` hoelzl@29805 ` 515` ``` let ?k = "lapprox_rat prec 1 k" ``` hoelzl@29805 ` 516` ``` have "1 div k = 0" using div_pos_pos_trivial[OF _ `1 < k`] by auto ``` hoelzl@29805 ` 517` ``` have "1 / real k \ 1" using `1 < k` by auto ``` hoelzl@29805 ` 518` hoelzl@31098 ` 519` ``` have "\n. 0 \ real ?k" using lapprox_rat_bottom[where x=1 and y=k, OF zero_le_one `0 < k`] by (auto simp add: `1 div k = 0`) ``` hoelzl@31098 ` 520` ``` have "\n. real ?k \ 1" using lapprox_rat by (rule order_trans, auto simp add: `1 / real k \ 1`) ``` hoelzl@29805 ` 521` hoelzl@31098 ` 522` ``` have "real ?k \ 1 / real k" using lapprox_rat[where x=1 and y=k] by auto ``` hoelzl@29805 ` 523` hoelzl@31098 ` 524` ``` have "real (?k * lb_arctan_horner prec (get_even n) 1 (?k * ?k)) \ arctan (real ?k)" ``` hoelzl@31098 ` 525` ``` using arctan_0_1_bounds[OF `0 \ real ?k` `real ?k \ 1`] by auto ``` hoelzl@31098 ` 526` ``` also have "\ \ arctan (1 / real k)" using `real ?k \ 1 / real k` by (rule arctan_monotone') ``` hoelzl@31098 ` 527` ``` finally have "real (?k * lb_arctan_horner prec (get_even n) 1 (?k * ?k)) \ arctan (1 / (real k))" . ``` hoelzl@29805 ` 528` ``` } note lb_arctan = this ``` hoelzl@29805 ` 529` hoelzl@31098 ` 530` ``` have "pi \ real (ub_pi n)" ``` hoelzl@31098 ` 531` ``` unfolding ub_pi_def machin_pi Let_def real_of_float_mult real_of_float_sub unfolding Float_num ``` hoelzl@29805 ` 532` ``` using lb_arctan[of 239] ub_arctan[of 5] ``` hoelzl@29805 ` 533` ``` by (auto intro!: mult_left_mono add_mono simp add: diff_minus simp del: lapprox_rat.simps rapprox_rat.simps) ``` hoelzl@29805 ` 534` ``` moreover ``` hoelzl@31098 ` 535` ``` have "real (lb_pi n) \ pi" ``` hoelzl@31098 ` 536` ``` unfolding lb_pi_def machin_pi Let_def real_of_float_mult real_of_float_sub Float_num ``` hoelzl@29805 ` 537` ``` using lb_arctan[of 5] ub_arctan[of 239] ``` hoelzl@29805 ` 538` ``` by (auto intro!: mult_left_mono add_mono simp add: diff_minus simp del: lapprox_rat.simps rapprox_rat.simps) ``` hoelzl@29805 ` 539` ``` ultimately show ?thesis by auto ``` hoelzl@29805 ` 540` ```qed ``` hoelzl@29805 ` 541` hoelzl@29805 ` 542` ```subsection "Compute arcus tangens in the entire domain" ``` hoelzl@29805 ` 543` hoelzl@31467 ` 544` ```function lb_arctan :: "nat \ float \ float" and ub_arctan :: "nat \ float \ float" where ``` hoelzl@29805 ` 545` ``` "lb_arctan prec x = (let ub_horner = \ x. x * ub_arctan_horner prec (get_odd (prec div 4 + 1)) 1 (x * x) ; ``` hoelzl@29805 ` 546` ``` lb_horner = \ x. x * lb_arctan_horner prec (get_even (prec div 4 + 1)) 1 (x * x) ``` hoelzl@29805 ` 547` ``` in (if x < 0 then - ub_arctan prec (-x) else ``` hoelzl@29805 ` 548` ``` if x \ Float 1 -1 then lb_horner x else ``` hoelzl@31467 ` 549` ``` if x \ Float 1 1 then Float 1 1 * lb_horner (float_divl prec x (1 + ub_sqrt prec (1 + x * x))) ``` hoelzl@31467 ` 550` ``` else (let inv = float_divr prec 1 x ``` hoelzl@31467 ` 551` ``` in if inv > 1 then 0 ``` hoelzl@29805 ` 552` ``` else lb_pi prec * Float 1 -1 - ub_horner inv)))" ``` hoelzl@29805 ` 553` hoelzl@29805 ` 554` ```| "ub_arctan prec x = (let lb_horner = \ x. x * lb_arctan_horner prec (get_even (prec div 4 + 1)) 1 (x * x) ; ``` hoelzl@29805 ` 555` ``` ub_horner = \ x. x * ub_arctan_horner prec (get_odd (prec div 4 + 1)) 1 (x * x) ``` hoelzl@29805 ` 556` ``` in (if x < 0 then - lb_arctan prec (-x) else ``` hoelzl@29805 ` 557` ``` if x \ Float 1 -1 then ub_horner x else ``` hoelzl@31467 ` 558` ``` if x \ Float 1 1 then let y = float_divr prec x (1 + lb_sqrt prec (1 + x * x)) ``` hoelzl@31467 ` 559` ``` in if y > 1 then ub_pi prec * Float 1 -1 ``` hoelzl@31467 ` 560` ``` else Float 1 1 * ub_horner y ``` hoelzl@29805 ` 561` ``` else ub_pi prec * Float 1 -1 - lb_horner (float_divl prec 1 x)))" ``` hoelzl@29805 ` 562` ```by pat_completeness auto ``` hoelzl@29805 ` 563` ```termination by (relation "measure (\ v. let (prec, x) = sum_case id id v in (if x < 0 then 1 else 0))", auto simp add: less_float_def) ``` hoelzl@29805 ` 564` hoelzl@29805 ` 565` ```declare ub_arctan_horner.simps[simp del] ``` hoelzl@29805 ` 566` ```declare lb_arctan_horner.simps[simp del] ``` hoelzl@29805 ` 567` hoelzl@31098 ` 568` ```lemma lb_arctan_bound': assumes "0 \ real x" ``` hoelzl@31098 ` 569` ``` shows "real (lb_arctan prec x) \ arctan (real x)" ``` hoelzl@29805 ` 570` ```proof - ``` hoelzl@31098 ` 571` ``` have "\ x < 0" and "0 \ x" unfolding less_float_def le_float_def using `0 \ real x` by auto ``` hoelzl@29805 ` 572` ``` let "?ub_horner x" = "x * ub_arctan_horner prec (get_odd (prec div 4 + 1)) 1 (x * x)" ``` hoelzl@29805 ` 573` ``` and "?lb_horner x" = "x * lb_arctan_horner prec (get_even (prec div 4 + 1)) 1 (x * x)" ``` hoelzl@29805 ` 574` hoelzl@29805 ` 575` ``` show ?thesis ``` hoelzl@29805 ` 576` ``` proof (cases "x \ Float 1 -1") ``` hoelzl@31098 ` 577` ``` case True hence "real x \ 1" unfolding le_float_def Float_num by auto ``` hoelzl@29805 ` 578` ``` show ?thesis unfolding lb_arctan.simps Let_def if_not_P[OF `\ x < 0`] if_P[OF True] ``` hoelzl@31098 ` 579` ``` using arctan_0_1_bounds[OF `0 \ real x` `real x \ 1`] by auto ``` hoelzl@29805 ` 580` ``` next ``` hoelzl@31098 ` 581` ``` case False hence "0 < real x" unfolding le_float_def Float_num by auto ``` hoelzl@31098 ` 582` ``` let ?R = "1 + sqrt (1 + real x * real x)" ``` hoelzl@31467 ` 583` ``` let ?fR = "1 + ub_sqrt prec (1 + x * x)" ``` hoelzl@29805 ` 584` ``` let ?DIV = "float_divl prec x ?fR" ``` hoelzl@31467 ` 585` hoelzl@31098 ` 586` ``` have sqr_ge0: "0 \ 1 + real x * real x" using sum_power2_ge_zero[of 1 "real x", unfolded numeral_2_eq_2] by auto ``` hoelzl@29805 ` 587` ``` hence divisor_gt0: "0 < ?R" by (auto intro: add_pos_nonneg) ``` hoelzl@29805 ` 588` hoelzl@31467 ` 589` ``` have "sqrt (real (1 + x * x)) \ real (ub_sqrt prec (1 + x * x))" ``` hoelzl@31467 ` 590` ``` using bnds_sqrt'[of "1 + x * x"] by auto ``` hoelzl@31467 ` 591` hoelzl@31098 ` 592` ``` hence "?R \ real ?fR" by auto ``` hoelzl@31098 ` 593` ``` hence "0 < ?fR" and "0 < real ?fR" unfolding less_float_def using `0 < ?R` by auto ``` hoelzl@29805 ` 594` hoelzl@31098 ` 595` ``` have monotone: "real (float_divl prec x ?fR) \ real x / ?R" ``` hoelzl@29805 ` 596` ``` proof - ``` hoelzl@31098 ` 597` ``` have "real ?DIV \ real x / real ?fR" by (rule float_divl) ``` hoelzl@31098 ` 598` ``` also have "\ \ real x / ?R" by (rule divide_left_mono[OF `?R \ real ?fR` `0 \ real x` mult_pos_pos[OF order_less_le_trans[OF divisor_gt0 `?R \ real ?fR`] divisor_gt0]]) ``` hoelzl@29805 ` 599` ``` finally show ?thesis . ``` hoelzl@29805 ` 600` ``` qed ``` hoelzl@29805 ` 601` hoelzl@29805 ` 602` ``` show ?thesis ``` hoelzl@29805 ` 603` ``` proof (cases "x \ Float 1 1") ``` hoelzl@29805 ` 604` ``` case True ``` hoelzl@31467 ` 605` hoelzl@31098 ` 606` ``` have "real x \ sqrt (real (1 + x * x))" using real_sqrt_sum_squares_ge2[where x=1, unfolded numeral_2_eq_2] by auto ``` hoelzl@31467 ` 607` ``` also have "\ \ real (ub_sqrt prec (1 + x * x))" ``` hoelzl@31467 ` 608` ``` using bnds_sqrt'[of "1 + x * x"] by auto ``` hoelzl@31098 ` 609` ``` finally have "real x \ real ?fR" by auto ``` hoelzl@31098 ` 610` ``` moreover have "real ?DIV \ real x / real ?fR" by (rule float_divl) ``` hoelzl@31098 ` 611` ``` ultimately have "real ?DIV \ 1" unfolding divide_le_eq_1_pos[OF `0 < real ?fR`, symmetric] by auto ``` hoelzl@29805 ` 612` hoelzl@31098 ` 613` ``` have "0 \ real ?DIV" using float_divl_lower_bound[OF `0 \ x` `0 < ?fR`] unfolding le_float_def by auto ``` hoelzl@29805 ` 614` hoelzl@31098 ` 615` ``` have "real (Float 1 1 * ?lb_horner ?DIV) \ 2 * arctan (real (float_divl prec x ?fR))" unfolding real_of_float_mult[of "Float 1 1"] Float_num ``` hoelzl@31098 ` 616` ``` using arctan_0_1_bounds[OF `0 \ real ?DIV` `real ?DIV \ 1`] by auto ``` hoelzl@31098 ` 617` ``` also have "\ \ 2 * arctan (real x / ?R)" ``` hoelzl@29805 ` 618` ``` using arctan_monotone'[OF monotone] by (auto intro!: mult_left_mono) ``` hoelzl@31809 ` 619` ``` also have "2 * arctan (real x / ?R) = arctan (real x)" using arctan_half[symmetric] unfolding numeral_2_eq_2 power_Suc2 power_0 real_mult_1 . ``` hoelzl@29805 ` 620` ``` finally show ?thesis unfolding lb_arctan.simps Let_def if_not_P[OF `\ x < 0`] if_not_P[OF `\ x \ Float 1 -1`] if_P[OF True] . ``` hoelzl@29805 ` 621` ``` next ``` hoelzl@29805 ` 622` ``` case False ``` hoelzl@31098 ` 623` ``` hence "2 < real x" unfolding le_float_def Float_num by auto ``` hoelzl@31098 ` 624` ``` hence "1 \ real x" by auto ``` hoelzl@29805 ` 625` hoelzl@29805 ` 626` ``` let "?invx" = "float_divr prec 1 x" ``` hoelzl@31098 ` 627` ``` have "0 \ arctan (real x)" using arctan_monotone'[OF `0 \ real x`] using arctan_tan[of 0, unfolded tan_zero] by auto ``` hoelzl@29805 ` 628` hoelzl@29805 ` 629` ``` show ?thesis ``` hoelzl@29805 ` 630` ``` proof (cases "1 < ?invx") ``` hoelzl@29805 ` 631` ``` case True ``` hoelzl@31809 ` 632` ``` show ?thesis unfolding lb_arctan.simps Let_def if_not_P[OF `\ x < 0`] if_not_P[OF `\ x \ Float 1 -1`] if_not_P[OF False] if_P[OF True] ``` hoelzl@31098 ` 633` ``` using `0 \ arctan (real x)` by auto ``` hoelzl@29805 ` 634` ``` next ``` hoelzl@29805 ` 635` ``` case False ``` hoelzl@31098 ` 636` ``` hence "real ?invx \ 1" unfolding less_float_def by auto ``` hoelzl@31098 ` 637` ``` have "0 \ real ?invx" by (rule order_trans[OF _ float_divr], auto simp add: `0 \ real x`) ``` hoelzl@29805 ` 638` hoelzl@31098 ` 639` ``` have "1 / real x \ 0" and "0 < 1 / real x" using `0 < real x` by auto ``` hoelzl@31467 ` 640` hoelzl@31098 ` 641` ``` have "arctan (1 / real x) \ arctan (real ?invx)" unfolding real_of_float_1[symmetric] by (rule arctan_monotone', rule float_divr) ``` hoelzl@31098 ` 642` ``` also have "\ \ real (?ub_horner ?invx)" using arctan_0_1_bounds[OF `0 \ real ?invx` `real ?invx \ 1`] by auto ``` hoelzl@31467 ` 643` ``` finally have "pi / 2 - real (?ub_horner ?invx) \ arctan (real x)" ``` hoelzl@31467 ` 644` ``` using `0 \ arctan (real x)` arctan_inverse[OF `1 / real x \ 0`] ``` hoelzl@31098 ` 645` ``` unfolding real_sgn_pos[OF `0 < 1 / real x`] le_diff_eq by auto ``` hoelzl@29805 ` 646` ``` moreover ``` hoelzl@31098 ` 647` ``` have "real (lb_pi prec * Float 1 -1) \ pi / 2" unfolding real_of_float_mult Float_num times_divide_eq_right real_mult_1 using pi_boundaries by auto ``` hoelzl@29805 ` 648` ``` ultimately ``` hoelzl@29805 ` 649` ``` show ?thesis unfolding lb_arctan.simps Let_def if_not_P[OF `\ x < 0`] if_not_P[OF `\ x \ Float 1 -1`] if_not_P[OF `\ x \ Float 1 1`] if_not_P[OF False] ``` hoelzl@29805 ` 650` ``` by auto ``` hoelzl@29805 ` 651` ``` qed ``` hoelzl@29805 ` 652` ``` qed ``` hoelzl@29805 ` 653` ``` qed ``` hoelzl@29805 ` 654` ```qed ``` hoelzl@29805 ` 655` hoelzl@31098 ` 656` ```lemma ub_arctan_bound': assumes "0 \ real x" ``` hoelzl@31098 ` 657` ``` shows "arctan (real x) \ real (ub_arctan prec x)" ``` hoelzl@29805 ` 658` ```proof - ``` hoelzl@31098 ` 659` ``` have "\ x < 0" and "0 \ x" unfolding less_float_def le_float_def using `0 \ real x` by auto ``` hoelzl@29805 ` 660` hoelzl@29805 ` 661` ``` let "?ub_horner x" = "x * ub_arctan_horner prec (get_odd (prec div 4 + 1)) 1 (x * x)" ``` hoelzl@29805 ` 662` ``` and "?lb_horner x" = "x * lb_arctan_horner prec (get_even (prec div 4 + 1)) 1 (x * x)" ``` hoelzl@29805 ` 663` hoelzl@29805 ` 664` ``` show ?thesis ``` hoelzl@29805 ` 665` ``` proof (cases "x \ Float 1 -1") ``` hoelzl@31098 ` 666` ``` case True hence "real x \ 1" unfolding le_float_def Float_num by auto ``` hoelzl@29805 ` 667` ``` show ?thesis unfolding ub_arctan.simps Let_def if_not_P[OF `\ x < 0`] if_P[OF True] ``` hoelzl@31098 ` 668` ``` using arctan_0_1_bounds[OF `0 \ real x` `real x \ 1`] by auto ``` hoelzl@29805 ` 669` ``` next ``` hoelzl@31098 ` 670` ``` case False hence "0 < real x" unfolding le_float_def Float_num by auto ``` hoelzl@31098 ` 671` ``` let ?R = "1 + sqrt (1 + real x * real x)" ``` hoelzl@31467 ` 672` ``` let ?fR = "1 + lb_sqrt prec (1 + x * x)" ``` hoelzl@29805 ` 673` ``` let ?DIV = "float_divr prec x ?fR" ``` hoelzl@31467 ` 674` hoelzl@31098 ` 675` ``` have sqr_ge0: "0 \ 1 + real x * real x" using sum_power2_ge_zero[of 1 "real x", unfolded numeral_2_eq_2] by auto ``` hoelzl@31098 ` 676` ``` hence "0 \ real (1 + x*x)" by auto ``` hoelzl@31467 ` 677` hoelzl@29805 ` 678` ``` hence divisor_gt0: "0 < ?R" by (auto intro: add_pos_nonneg) ``` hoelzl@29805 ` 679` hoelzl@31467 ` 680` ``` have "real (lb_sqrt prec (1 + x * x)) \ sqrt (real (1 + x * x))" ``` hoelzl@31467 ` 681` ``` using bnds_sqrt'[of "1 + x * x"] by auto ``` hoelzl@31098 ` 682` ``` hence "real ?fR \ ?R" by auto ``` hoelzl@31098 ` 683` ``` have "0 < real ?fR" unfolding real_of_float_add real_of_float_1 by (rule order_less_le_trans[OF zero_less_one], auto simp add: lb_sqrt_lower_bound[OF `0 \ real (1 + x*x)`]) ``` hoelzl@29805 ` 684` hoelzl@31098 ` 685` ``` have monotone: "real x / ?R \ real (float_divr prec x ?fR)" ``` hoelzl@29805 ` 686` ``` proof - ``` hoelzl@31098 ` 687` ``` from divide_left_mono[OF `real ?fR \ ?R` `0 \ real x` mult_pos_pos[OF divisor_gt0 `0 < real ?fR`]] ``` hoelzl@31098 ` 688` ``` have "real x / ?R \ real x / real ?fR" . ``` hoelzl@31098 ` 689` ``` also have "\ \ real ?DIV" by (rule float_divr) ``` hoelzl@29805 ` 690` ``` finally show ?thesis . ``` hoelzl@29805 ` 691` ``` qed ``` hoelzl@29805 ` 692` hoelzl@29805 ` 693` ``` show ?thesis ``` hoelzl@29805 ` 694` ``` proof (cases "x \ Float 1 1") ``` hoelzl@29805 ` 695` ``` case True ``` hoelzl@29805 ` 696` ``` show ?thesis ``` hoelzl@29805 ` 697` ``` proof (cases "?DIV > 1") ``` hoelzl@29805 ` 698` ``` case True ``` hoelzl@31098 ` 699` ``` have "pi / 2 \ real (ub_pi prec * Float 1 -1)" unfolding real_of_float_mult Float_num times_divide_eq_right real_mult_1 using pi_boundaries by auto ``` hoelzl@29805 ` 700` ``` from order_less_le_trans[OF arctan_ubound this, THEN less_imp_le] ``` hoelzl@29805 ` 701` ``` show ?thesis unfolding ub_arctan.simps Let_def if_not_P[OF `\ x < 0`] if_not_P[OF `\ x \ Float 1 -1`] if_P[OF `x \ Float 1 1`] if_P[OF True] . ``` hoelzl@29805 ` 702` ``` next ``` hoelzl@29805 ` 703` ``` case False ``` hoelzl@31098 ` 704` ``` hence "real ?DIV \ 1" unfolding less_float_def by auto ``` hoelzl@31467 ` 705` hoelzl@31098 ` 706` ``` have "0 \ real x / ?R" using `0 \ real x` `0 < ?R` unfolding real_0_le_divide_iff by auto ``` hoelzl@31098 ` 707` ``` hence "0 \ real ?DIV" using monotone by (rule order_trans) ``` hoelzl@29805 ` 708` hoelzl@31098 ` 709` ``` have "arctan (real x) = 2 * arctan (real x / ?R)" using arctan_half unfolding numeral_2_eq_2 power_Suc2 power_0 real_mult_1 . ``` hoelzl@31098 ` 710` ``` also have "\ \ 2 * arctan (real ?DIV)" ``` hoelzl@29805 ` 711` ``` using arctan_monotone'[OF monotone] by (auto intro!: mult_left_mono) ``` hoelzl@31098 ` 712` ``` also have "\ \ real (Float 1 1 * ?ub_horner ?DIV)" unfolding real_of_float_mult[of "Float 1 1"] Float_num ``` hoelzl@31098 ` 713` ``` using arctan_0_1_bounds[OF `0 \ real ?DIV` `real ?DIV \ 1`] by auto ``` hoelzl@29805 ` 714` ``` finally show ?thesis unfolding ub_arctan.simps Let_def if_not_P[OF `\ x < 0`] if_not_P[OF `\ x \ Float 1 -1`] if_P[OF `x \ Float 1 1`] if_not_P[OF False] . ``` hoelzl@29805 ` 715` ``` qed ``` hoelzl@29805 ` 716` ``` next ``` hoelzl@29805 ` 717` ``` case False ``` hoelzl@31098 ` 718` ``` hence "2 < real x" unfolding le_float_def Float_num by auto ``` hoelzl@31098 ` 719` ``` hence "1 \ real x" by auto ``` hoelzl@31098 ` 720` ``` hence "0 < real x" by auto ``` hoelzl@29805 ` 721` ``` hence "0 < x" unfolding less_float_def by auto ``` hoelzl@29805 ` 722` hoelzl@29805 ` 723` ``` let "?invx" = "float_divl prec 1 x" ``` hoelzl@31098 ` 724` ``` have "0 \ arctan (real x)" using arctan_monotone'[OF `0 \ real x`] using arctan_tan[of 0, unfolded tan_zero] by auto ``` hoelzl@29805 ` 725` hoelzl@31098 ` 726` ``` have "real ?invx \ 1" unfolding less_float_def by (rule order_trans[OF float_divl], auto simp add: `1 \ real x` divide_le_eq_1_pos[OF `0 < real x`]) ``` hoelzl@31098 ` 727` ``` have "0 \ real ?invx" unfolding real_of_float_0[symmetric] by (rule float_divl_lower_bound[unfolded le_float_def], auto simp add: `0 < x`) ``` hoelzl@31467 ` 728` hoelzl@31098 ` 729` ``` have "1 / real x \ 0" and "0 < 1 / real x" using `0 < real x` by auto ``` hoelzl@31467 ` 730` hoelzl@31098 ` 731` ``` have "real (?lb_horner ?invx) \ arctan (real ?invx)" using arctan_0_1_bounds[OF `0 \ real ?invx` `real ?invx \ 1`] by auto ``` hoelzl@31098 ` 732` ``` also have "\ \ arctan (1 / real x)" unfolding real_of_float_1[symmetric] by (rule arctan_monotone', rule float_divl) ``` hoelzl@31098 ` 733` ``` finally have "arctan (real x) \ pi / 2 - real (?lb_horner ?invx)" ``` hoelzl@31809 ` 734` ``` using `0 \ arctan (real x)` arctan_inverse[OF `1 / real x \ 0`] ``` hoelzl@31098 ` 735` ``` unfolding real_sgn_pos[OF `0 < 1 / real x`] le_diff_eq by auto ``` hoelzl@29805 ` 736` ``` moreover ``` hoelzl@31098 ` 737` ``` have "pi / 2 \ real (ub_pi prec * Float 1 -1)" unfolding real_of_float_mult Float_num times_divide_eq_right mult_1_right using pi_boundaries by auto ``` hoelzl@29805 ` 738` ``` ultimately ``` hoelzl@29805 ` 739` ``` show ?thesis unfolding ub_arctan.simps Let_def if_not_P[OF `\ x < 0`] if_not_P[OF `\ x \ Float 1 -1`] if_not_P[OF `\ x \ Float 1 1`] if_not_P[OF False] ``` hoelzl@29805 ` 740` ``` by auto ``` hoelzl@29805 ` 741` ``` qed ``` hoelzl@29805 ` 742` ``` qed ``` hoelzl@29805 ` 743` ```qed ``` hoelzl@29805 ` 744` hoelzl@29805 ` 745` ```lemma arctan_boundaries: ``` hoelzl@31098 ` 746` ``` "arctan (real x) \ {real (lb_arctan prec x) .. real (ub_arctan prec x)}" ``` hoelzl@29805 ` 747` ```proof (cases "0 \ x") ``` hoelzl@31098 ` 748` ``` case True hence "0 \ real x" unfolding le_float_def by auto ``` hoelzl@31098 ` 749` ``` show ?thesis using ub_arctan_bound'[OF `0 \ real x`] lb_arctan_bound'[OF `0 \ real x`] unfolding atLeastAtMost_iff by auto ``` hoelzl@29805 ` 750` ```next ``` hoelzl@29805 ` 751` ``` let ?mx = "-x" ``` hoelzl@31098 ` 752` ``` case False hence "x < 0" and "0 \ real ?mx" unfolding le_float_def less_float_def by auto ``` hoelzl@31098 ` 753` ``` hence bounds: "real (lb_arctan prec ?mx) \ arctan (real ?mx) \ arctan (real ?mx) \ real (ub_arctan prec ?mx)" ``` hoelzl@31098 ` 754` ``` using ub_arctan_bound'[OF `0 \ real ?mx`] lb_arctan_bound'[OF `0 \ real ?mx`] by auto ``` hoelzl@31098 ` 755` ``` show ?thesis unfolding real_of_float_minus arctan_minus lb_arctan.simps[where x=x] ub_arctan.simps[where x=x] Let_def if_P[OF `x < 0`] ``` hoelzl@31098 ` 756` ``` unfolding atLeastAtMost_iff using bounds[unfolded real_of_float_minus arctan_minus] by auto ``` hoelzl@29805 ` 757` ```qed ``` hoelzl@29805 ` 758` hoelzl@31098 ` 759` ```lemma bnds_arctan: "\ x lx ux. (l, u) = (lb_arctan prec lx, ub_arctan prec ux) \ x \ {real lx .. real ux} \ real l \ arctan x \ arctan x \ real u" ``` hoelzl@29805 ` 760` ```proof (rule allI, rule allI, rule allI, rule impI) ``` hoelzl@29805 ` 761` ``` fix x lx ux ``` hoelzl@31098 ` 762` ``` assume "(l, u) = (lb_arctan prec lx, ub_arctan prec ux) \ x \ {real lx .. real ux}" ``` hoelzl@31098 ` 763` ``` hence l: "lb_arctan prec lx = l " and u: "ub_arctan prec ux = u" and x: "x \ {real lx .. real ux}" by auto ``` hoelzl@29805 ` 764` hoelzl@29805 ` 765` ``` { from arctan_boundaries[of lx prec, unfolded l] ``` hoelzl@31098 ` 766` ``` have "real l \ arctan (real lx)" by (auto simp del: lb_arctan.simps) ``` hoelzl@29805 ` 767` ``` also have "\ \ arctan x" using x by (auto intro: arctan_monotone') ``` hoelzl@31098 ` 768` ``` finally have "real l \ arctan x" . ``` hoelzl@29805 ` 769` ``` } moreover ``` hoelzl@31098 ` 770` ``` { have "arctan x \ arctan (real ux)" using x by (auto intro: arctan_monotone') ``` hoelzl@31098 ` 771` ``` also have "\ \ real u" using arctan_boundaries[of ux prec, unfolded u] by (auto simp del: ub_arctan.simps) ``` hoelzl@31098 ` 772` ``` finally have "arctan x \ real u" . ``` hoelzl@31098 ` 773` ``` } ultimately show "real l \ arctan x \ arctan x \ real u" .. ``` hoelzl@29805 ` 774` ```qed ``` hoelzl@29805 ` 775` hoelzl@29805 ` 776` ```section "Sinus and Cosinus" ``` hoelzl@29805 ` 777` hoelzl@29805 ` 778` ```subsection "Compute the cosinus and sinus series" ``` hoelzl@29805 ` 779` hoelzl@29805 ` 780` ```fun ub_sin_cos_aux :: "nat \ nat \ nat \ nat \ float \ float" ``` hoelzl@29805 ` 781` ```and lb_sin_cos_aux :: "nat \ nat \ nat \ nat \ float \ float" where ``` hoelzl@29805 ` 782` ``` "ub_sin_cos_aux prec 0 i k x = 0" ``` hoelzl@31809 ` 783` ```| "ub_sin_cos_aux prec (Suc n) i k x = ``` hoelzl@29805 ` 784` ``` (rapprox_rat prec 1 (int k)) - x * (lb_sin_cos_aux prec n (i + 2) (k * i * (i + 1)) x)" ``` hoelzl@29805 ` 785` ```| "lb_sin_cos_aux prec 0 i k x = 0" ``` hoelzl@31809 ` 786` ```| "lb_sin_cos_aux prec (Suc n) i k x = ``` hoelzl@29805 ` 787` ``` (lapprox_rat prec 1 (int k)) - x * (ub_sin_cos_aux prec n (i + 2) (k * i * (i + 1)) x)" ``` hoelzl@29805 ` 788` hoelzl@29805 ` 789` ```lemma cos_aux: ``` hoelzl@31098 ` 790` ``` shows "real (lb_sin_cos_aux prec n 1 1 (x * x)) \ (\ i=0.. i=0.. real (ub_sin_cos_aux prec n 1 1 (x * x))" (is "?ub") ``` hoelzl@29805 ` 792` ```proof - ``` hoelzl@31098 ` 793` ``` have "0 \ real (x * x)" unfolding real_of_float_mult by auto ``` hoelzl@29805 ` 794` ``` let "?f n" = "fact (2 * n)" ``` hoelzl@29805 ` 795` hoelzl@31809 ` 796` ``` { fix n ``` haftmann@30971 ` 797` ``` have F: "\m. ((\i. i + 2) ^^ n) m = m + 2 * n" by (induct n arbitrary: m, auto) ``` haftmann@30971 ` 798` ``` have "?f (Suc n) = ?f n * ((\i. i + 2) ^^ n) 1 * (((\i. i + 2) ^^ n) 1 + 1)" ``` hoelzl@29805 ` 799` ``` unfolding F by auto } note f_eq = this ``` hoelzl@31809 ` 800` hoelzl@31809 ` 801` ``` from horner_bounds[where lb="lb_sin_cos_aux prec" and ub="ub_sin_cos_aux prec" and j'=0, ``` hoelzl@31098 ` 802` ``` OF `0 \ real (x * x)` f_eq lb_sin_cos_aux.simps ub_sin_cos_aux.simps] ``` hoelzl@31098 ` 803` ``` show "?lb" and "?ub" by (auto simp add: power_mult power2_eq_square[of "real x"]) ``` hoelzl@29805 ` 804` ```qed ``` hoelzl@29805 ` 805` hoelzl@31098 ` 806` ```lemma cos_boundaries: assumes "0 \ real x" and "real x \ pi / 2" ``` hoelzl@31098 ` 807` ``` shows "cos (real x) \ {real (lb_sin_cos_aux prec (get_even n) 1 1 (x * x)) .. real (ub_sin_cos_aux prec (get_odd n) 1 1 (x * x))}" ``` hoelzl@31098 ` 808` ```proof (cases "real x = 0") ``` hoelzl@31098 ` 809` ``` case False hence "real x \ 0" by auto ``` hoelzl@31098 ` 810` ``` hence "0 < x" and "0 < real x" using `0 \ real x` unfolding less_float_def by auto ``` hoelzl@31098 ` 811` ``` have "0 < x * x" using `0 < x` unfolding less_float_def real_of_float_mult real_of_float_0 ``` hoelzl@31098 ` 812` ``` using mult_pos_pos[where a="real x" and b="real x"] by auto ``` hoelzl@29805 ` 813` haftmann@30952 ` 814` ``` { fix x n have "(\ i=0.. i = 0 ..< 2 * n. (if even(i) then (-1 ^ (i div 2))/(real (fact i)) else 0) * x ^ i)" (is "?sum = ?ifsum") ``` hoelzl@29805 ` 816` ``` proof - ``` hoelzl@29805 ` 817` ``` have "?sum = ?sum + (\ j = 0 ..< n. 0)" by auto ``` hoelzl@31809 ` 818` ``` also have "\ = ``` hoelzl@29805 ` 819` ``` (\ j = 0 ..< n. -1 ^ ((2 * j) div 2) / (real (fact (2 * j))) * x ^(2 * j)) + (\ j = 0 ..< n. 0)" by auto ``` hoelzl@29805 ` 820` ``` also have "\ = (\ i = 0 ..< 2 * n. if even i then -1 ^ (i div 2) / (real (fact i)) * x ^ i else 0)" ``` hoelzl@29805 ` 821` ``` unfolding sum_split_even_odd .. ``` hoelzl@29805 ` 822` ``` also have "\ = (\ i = 0 ..< 2 * n. (if even i then -1 ^ (i div 2) / (real (fact i)) else 0) * x ^ i)" ``` hoelzl@29805 ` 823` ``` by (rule setsum_cong2) auto ``` hoelzl@29805 ` 824` ``` finally show ?thesis by assumption ``` hoelzl@29805 ` 825` ``` qed } note morph_to_if_power = this ``` hoelzl@29805 ` 826` hoelzl@29805 ` 827` hoelzl@29805 ` 828` ``` { fix n :: nat assume "0 < n" ``` hoelzl@29805 ` 829` ``` hence "0 < 2 * n" by auto ``` hoelzl@31098 ` 830` ``` obtain t where "0 < t" and "t < real x" and ``` hoelzl@31809 ` 831` ``` cos_eq: "cos (real x) = (\ i = 0 ..< 2 * n. (if even(i) then (-1 ^ (i div 2))/(real (fact i)) else 0) * (real x) ^ i) ``` hoelzl@31809 ` 832` ``` + (cos (t + 1/2 * real (2 * n) * pi) / real (fact (2*n))) * (real x)^(2*n)" ``` hoelzl@29805 ` 833` ``` (is "_ = ?SUM + ?rest / ?fact * ?pow") ``` hoelzl@31098 ` 834` ``` using Maclaurin_cos_expansion2[OF `0 < real x` `0 < 2 * n`] by auto ``` hoelzl@29805 ` 835` hoelzl@29805 ` 836` ``` have "cos t * -1^n = cos t * cos (real n * pi) + sin t * sin (real n * pi)" by auto ``` hoelzl@29805 ` 837` ``` also have "\ = cos (t + real n * pi)" using cos_add by auto ``` hoelzl@29805 ` 838` ``` also have "\ = ?rest" by auto ``` hoelzl@29805 ` 839` ``` finally have "cos t * -1^n = ?rest" . ``` hoelzl@29805 ` 840` ``` moreover ``` hoelzl@31098 ` 841` ``` have "t \ pi / 2" using `t < real x` and `real x \ pi / 2` by auto ``` hoelzl@29805 ` 842` ``` hence "0 \ cos t" using `0 < t` and cos_ge_zero by auto ``` hoelzl@29805 ` 843` ``` ultimately have even: "even n \ 0 \ ?rest" and odd: "odd n \ 0 \ - ?rest " by auto ``` hoelzl@29805 ` 844` hoelzl@29805 ` 845` ``` have "0 < ?fact" by auto ``` hoelzl@31098 ` 846` ``` have "0 < ?pow" using `0 < real x` by auto ``` hoelzl@29805 ` 847` hoelzl@29805 ` 848` ``` { ``` hoelzl@29805 ` 849` ``` assume "even n" ``` hoelzl@31098 ` 850` ``` have "real (lb_sin_cos_aux prec n 1 1 (x * x)) \ ?SUM" ``` hoelzl@31809 ` 851` ``` unfolding morph_to_if_power[symmetric] using cos_aux by auto ``` hoelzl@31098 ` 852` ``` also have "\ \ cos (real x)" ``` hoelzl@29805 ` 853` ``` proof - ``` hoelzl@29805 ` 854` ``` from even[OF `even n`] `0 < ?fact` `0 < ?pow` ``` hoelzl@29805 ` 855` ``` have "0 \ (?rest / ?fact) * ?pow" by (metis mult_nonneg_nonneg divide_nonneg_pos less_imp_le) ``` hoelzl@29805 ` 856` ``` thus ?thesis unfolding cos_eq by auto ``` hoelzl@29805 ` 857` ``` qed ``` hoelzl@31098 ` 858` ``` finally have "real (lb_sin_cos_aux prec n 1 1 (x * x)) \ cos (real x)" . ``` hoelzl@29805 ` 859` ``` } note lb = this ``` hoelzl@29805 ` 860` hoelzl@29805 ` 861` ``` { ``` hoelzl@29805 ` 862` ``` assume "odd n" ``` hoelzl@31098 ` 863` ``` have "cos (real x) \ ?SUM" ``` hoelzl@29805 ` 864` ``` proof - ``` hoelzl@29805 ` 865` ``` from `0 < ?fact` and `0 < ?pow` and odd[OF `odd n`] ``` hoelzl@29805 ` 866` ``` have "0 \ (- ?rest) / ?fact * ?pow" ``` hoelzl@29805 ` 867` ``` by (metis mult_nonneg_nonneg divide_nonneg_pos less_imp_le) ``` hoelzl@29805 ` 868` ``` thus ?thesis unfolding cos_eq by auto ``` hoelzl@29805 ` 869` ``` qed ``` hoelzl@31098 ` 870` ``` also have "\ \ real (ub_sin_cos_aux prec n 1 1 (x * x))" ``` hoelzl@29805 ` 871` ``` unfolding morph_to_if_power[symmetric] using cos_aux by auto ``` hoelzl@31098 ` 872` ``` finally have "cos (real x) \ real (ub_sin_cos_aux prec n 1 1 (x * x))" . ``` hoelzl@29805 ` 873` ``` } note ub = this and lb ``` hoelzl@29805 ` 874` ``` } note ub = this(1) and lb = this(2) ``` hoelzl@29805 ` 875` hoelzl@31098 ` 876` ``` have "cos (real x) \ real (ub_sin_cos_aux prec (get_odd n) 1 1 (x * x))" using ub[OF odd_pos[OF get_odd] get_odd] . ``` hoelzl@31809 ` 877` ``` moreover have "real (lb_sin_cos_aux prec (get_even n) 1 1 (x * x)) \ cos (real x)" ``` hoelzl@29805 ` 878` ``` proof (cases "0 < get_even n") ``` hoelzl@29805 ` 879` ``` case True show ?thesis using lb[OF True get_even] . ``` hoelzl@29805 ` 880` ``` next ``` hoelzl@29805 ` 881` ``` case False ``` hoelzl@29805 ` 882` ``` hence "get_even n = 0" by auto ``` hoelzl@31098 ` 883` ``` have "- (pi / 2) \ real x" by (rule order_trans[OF _ `0 < real x`[THEN less_imp_le]], auto) ``` hoelzl@31098 ` 884` ``` with `real x \ pi / 2` ``` hoelzl@31098 ` 885` ``` show ?thesis unfolding `get_even n = 0` lb_sin_cos_aux.simps real_of_float_minus real_of_float_0 using cos_ge_zero by auto ``` hoelzl@29805 ` 886` ``` qed ``` hoelzl@29805 ` 887` ``` ultimately show ?thesis by auto ``` hoelzl@29805 ` 888` ```next ``` hoelzl@29805 ` 889` ``` case True ``` hoelzl@29805 ` 890` ``` show ?thesis ``` hoelzl@29805 ` 891` ``` proof (cases "n = 0") ``` hoelzl@31809 ` 892` ``` case True ``` hoelzl@31098 ` 893` ``` thus ?thesis unfolding `n = 0` get_even_def get_odd_def using `real x = 0` lapprox_rat[where x="-1" and y=1] by auto ``` hoelzl@29805 ` 894` ``` next ``` hoelzl@29805 ` 895` ``` case False with not0_implies_Suc obtain m where "n = Suc m" by blast ``` hoelzl@31098 ` 896` ``` thus ?thesis unfolding `n = Suc m` get_even_def get_odd_def using `real x = 0` rapprox_rat[where x=1 and y=1] lapprox_rat[where x=1 and y=1] by (cases "even (Suc m)", auto) ``` hoelzl@29805 ` 897` ``` qed ``` hoelzl@29805 ` 898` ```qed ``` hoelzl@29805 ` 899` hoelzl@31098 ` 900` ```lemma sin_aux: assumes "0 \ real x" ``` hoelzl@31098 ` 901` ``` shows "real (x * lb_sin_cos_aux prec n 2 1 (x * x)) \ (\ i=0.. i=0.. real (x * ub_sin_cos_aux prec n 2 1 (x * x))" (is "?ub") ``` hoelzl@29805 ` 903` ```proof - ``` hoelzl@31098 ` 904` ``` have "0 \ real (x * x)" unfolding real_of_float_mult by auto ``` hoelzl@29805 ` 905` ``` let "?f n" = "fact (2 * n + 1)" ``` hoelzl@29805 ` 906` hoelzl@31809 ` 907` ``` { fix n ``` haftmann@30971 ` 908` ``` have F: "\m. ((\i. i + 2) ^^ n) m = m + 2 * n" by (induct n arbitrary: m, auto) ``` haftmann@30971 ` 909` ``` have "?f (Suc n) = ?f n * ((\i. i + 2) ^^ n) 2 * (((\i. i + 2) ^^ n) 2 + 1)" ``` hoelzl@29805 ` 910` ``` unfolding F by auto } note f_eq = this ``` hoelzl@31809 ` 911` hoelzl@29805 ` 912` ``` from horner_bounds[where lb="lb_sin_cos_aux prec" and ub="ub_sin_cos_aux prec" and j'=0, ``` hoelzl@31098 ` 913` ``` OF `0 \ real (x * x)` f_eq lb_sin_cos_aux.simps ub_sin_cos_aux.simps] ``` hoelzl@31098 ` 914` ``` show "?lb" and "?ub" using `0 \ real x` unfolding real_of_float_mult ``` hoelzl@29805 ` 915` ``` unfolding power_add power_one_right real_mult_assoc[symmetric] setsum_left_distrib[symmetric] ``` hoelzl@29805 ` 916` ``` unfolding real_mult_commute ``` hoelzl@31098 ` 917` ``` by (auto intro!: mult_left_mono simp add: power_mult power2_eq_square[of "real x"]) ``` hoelzl@29805 ` 918` ```qed ``` hoelzl@29805 ` 919` hoelzl@31098 ` 920` ```lemma sin_boundaries: assumes "0 \ real x" and "real x \ pi / 2" ``` hoelzl@31098 ` 921` ``` shows "sin (real x) \ {real (x * lb_sin_cos_aux prec (get_even n) 2 1 (x * x)) .. real (x * ub_sin_cos_aux prec (get_odd n) 2 1 (x * x))}" ``` hoelzl@31098 ` 922` ```proof (cases "real x = 0") ``` hoelzl@31098 ` 923` ``` case False hence "real x \ 0" by auto ``` hoelzl@31098 ` 924` ``` hence "0 < x" and "0 < real x" using `0 \ real x` unfolding less_float_def by auto ``` hoelzl@31098 ` 925` ``` have "0 < x * x" using `0 < x` unfolding less_float_def real_of_float_mult real_of_float_0 ``` hoelzl@31098 ` 926` ``` using mult_pos_pos[where a="real x" and b="real x"] by auto ``` hoelzl@29805 ` 927` hoelzl@29805 ` 928` ``` { fix x n have "(\ j = 0 ..< n. -1 ^ (((2 * j + 1) - Suc 0) div 2) / (real (fact (2 * j + 1))) * x ^(2 * j + 1)) ``` hoelzl@29805 ` 929` ``` = (\ i = 0 ..< 2 * n. (if even(i) then 0 else (-1 ^ ((i - Suc 0) div 2))/(real (fact i))) * x ^ i)" (is "?SUM = _") ``` hoelzl@29805 ` 930` ``` proof - ``` hoelzl@29805 ` 931` ``` have pow: "!!i. x ^ (2 * i + 1) = x * x ^ (2 * i)" by auto ``` hoelzl@29805 ` 932` ``` have "?SUM = (\ j = 0 ..< n. 0) + ?SUM" by auto ``` hoelzl@29805 ` 933` ``` also have "\ = (\ i = 0 ..< 2 * n. if even i then 0 else -1 ^ ((i - Suc 0) div 2) / (real (fact i)) * x ^ i)" ``` hoelzl@29805 ` 934` ``` unfolding sum_split_even_odd .. ``` hoelzl@29805 ` 935` ``` also have "\ = (\ i = 0 ..< 2 * n. (if even i then 0 else -1 ^ ((i - Suc 0) div 2) / (real (fact i))) * x ^ i)" ``` hoelzl@29805 ` 936` ``` by (rule setsum_cong2) auto ``` hoelzl@29805 ` 937` ``` finally show ?thesis by assumption ``` hoelzl@29805 ` 938` ``` qed } note setsum_morph = this ``` hoelzl@29805 ` 939` hoelzl@29805 ` 940` ``` { fix n :: nat assume "0 < n" ``` hoelzl@29805 ` 941` ``` hence "0 < 2 * n + 1" by auto ``` hoelzl@31098 ` 942` ``` obtain t where "0 < t" and "t < real x" and ``` hoelzl@31809 ` 943` ``` sin_eq: "sin (real x) = (\ i = 0 ..< 2 * n + 1. (if even(i) then 0 else (-1 ^ ((i - Suc 0) div 2))/(real (fact i))) * (real x) ^ i) ``` hoelzl@31809 ` 944` ``` + (sin (t + 1/2 * real (2 * n + 1) * pi) / real (fact (2*n + 1))) * (real x)^(2*n + 1)" ``` hoelzl@29805 ` 945` ``` (is "_ = ?SUM + ?rest / ?fact * ?pow") ``` hoelzl@31098 ` 946` ``` using Maclaurin_sin_expansion3[OF `0 < 2 * n + 1` `0 < real x`] by auto ``` hoelzl@29805 ` 947` hoelzl@29805 ` 948` ``` have "?rest = cos t * -1^n" unfolding sin_add cos_add real_of_nat_add left_distrib right_distrib by auto ``` hoelzl@29805 ` 949` ``` moreover ``` hoelzl@31098 ` 950` ``` have "t \ pi / 2" using `t < real x` and `real x \ pi / 2` by auto ``` hoelzl@29805 ` 951` ``` hence "0 \ cos t" using `0 < t` and cos_ge_zero by auto ``` hoelzl@29805 ` 952` ``` ultimately have even: "even n \ 0 \ ?rest" and odd: "odd n \ 0 \ - ?rest " by auto ``` hoelzl@29805 ` 953` hoelzl@29805 ` 954` ``` have "0 < ?fact" by (rule real_of_nat_fact_gt_zero) ``` hoelzl@31098 ` 955` ``` have "0 < ?pow" using `0 < real x` by (rule zero_less_power) ``` hoelzl@29805 ` 956` hoelzl@29805 ` 957` ``` { ``` hoelzl@29805 ` 958` ``` assume "even n" ``` hoelzl@31809 ` 959` ``` have "real (x * lb_sin_cos_aux prec n 2 1 (x * x)) \ ``` hoelzl@31098 ` 960` ``` (\ i = 0 ..< 2 * n. (if even(i) then 0 else (-1 ^ ((i - Suc 0) div 2))/(real (fact i))) * (real x) ^ i)" ``` hoelzl@31098 ` 961` ``` using sin_aux[OF `0 \ real x`] unfolding setsum_morph[symmetric] by auto ``` hoelzl@29805 ` 962` ``` also have "\ \ ?SUM" by auto ``` hoelzl@31098 ` 963` ``` also have "\ \ sin (real x)" ``` hoelzl@29805 ` 964` ``` proof - ``` hoelzl@29805 ` 965` ``` from even[OF `even n`] `0 < ?fact` `0 < ?pow` ``` hoelzl@29805 ` 966` ``` have "0 \ (?rest / ?fact) * ?pow" by (metis mult_nonneg_nonneg divide_nonneg_pos less_imp_le) ``` hoelzl@29805 ` 967` ``` thus ?thesis unfolding sin_eq by auto ``` hoelzl@29805 ` 968` ``` qed ``` hoelzl@31098 ` 969` ``` finally have "real (x * lb_sin_cos_aux prec n 2 1 (x * x)) \ sin (real x)" . ``` hoelzl@29805 ` 970` ``` } note lb = this ``` hoelzl@29805 ` 971` hoelzl@29805 ` 972` ``` { ``` hoelzl@29805 ` 973` ``` assume "odd n" ``` hoelzl@31098 ` 974` ``` have "sin (real x) \ ?SUM" ``` hoelzl@29805 ` 975` ``` proof - ``` hoelzl@29805 ` 976` ``` from `0 < ?fact` and `0 < ?pow` and odd[OF `odd n`] ``` hoelzl@29805 ` 977` ``` have "0 \ (- ?rest) / ?fact * ?pow" ``` hoelzl@29805 ` 978` ``` by (metis mult_nonneg_nonneg divide_nonneg_pos less_imp_le) ``` hoelzl@29805 ` 979` ``` thus ?thesis unfolding sin_eq by auto ``` hoelzl@29805 ` 980` ``` qed ``` hoelzl@31098 ` 981` ``` also have "\ \ (\ i = 0 ..< 2 * n. (if even(i) then 0 else (-1 ^ ((i - Suc 0) div 2))/(real (fact i))) * (real x) ^ i)" ``` hoelzl@29805 ` 982` ``` by auto ``` hoelzl@31809 ` 983` ``` also have "\ \ real (x * ub_sin_cos_aux prec n 2 1 (x * x))" ``` hoelzl@31098 ` 984` ``` using sin_aux[OF `0 \ real x`] unfolding setsum_morph[symmetric] by auto ``` hoelzl@31098 ` 985` ``` finally have "sin (real x) \ real (x * ub_sin_cos_aux prec n 2 1 (x * x))" . ``` hoelzl@29805 ` 986` ``` } note ub = this and lb ``` hoelzl@29805 ` 987` ``` } note ub = this(1) and lb = this(2) ``` hoelzl@29805 ` 988` hoelzl@31098 ` 989` ``` have "sin (real x) \ real (x * ub_sin_cos_aux prec (get_odd n) 2 1 (x * x))" using ub[OF odd_pos[OF get_odd] get_odd] . ``` hoelzl@31809 ` 990` ``` moreover have "real (x * lb_sin_cos_aux prec (get_even n) 2 1 (x * x)) \ sin (real x)" ``` hoelzl@29805 ` 991` ``` proof (cases "0 < get_even n") ``` hoelzl@29805 ` 992` ``` case True show ?thesis using lb[OF True get_even] . ``` hoelzl@29805 ` 993` ``` next ``` hoelzl@29805 ` 994` ``` case False ``` hoelzl@29805 ` 995` ``` hence "get_even n = 0" by auto ``` hoelzl@31098 ` 996` ``` with `real x \ pi / 2` `0 \ real x` ``` hoelzl@31098 ` 997` ``` show ?thesis unfolding `get_even n = 0` ub_sin_cos_aux.simps real_of_float_minus real_of_float_0 using sin_ge_zero by auto ``` hoelzl@29805 ` 998` ``` qed ``` hoelzl@29805 ` 999` ``` ultimately show ?thesis by auto ``` hoelzl@29805 ` 1000` ```next ``` hoelzl@29805 ` 1001` ``` case True ``` hoelzl@29805 ` 1002` ``` show ?thesis ``` hoelzl@29805 ` 1003` ``` proof (cases "n = 0") ``` hoelzl@31809 ` 1004` ``` case True ``` hoelzl@31098 ` 1005` ``` thus ?thesis unfolding `n = 0` get_even_def get_odd_def using `real x = 0` lapprox_rat[where x="-1" and y=1] by auto ``` hoelzl@29805 ` 1006` ``` next ``` hoelzl@29805 ` 1007` ``` case False with not0_implies_Suc obtain m where "n = Suc m" by blast ``` hoelzl@31098 ` 1008` ``` thus ?thesis unfolding `n = Suc m` get_even_def get_odd_def using `real x = 0` rapprox_rat[where x=1 and y=1] lapprox_rat[where x=1 and y=1] by (cases "even (Suc m)", auto) ``` hoelzl@29805 ` 1009` ``` qed ``` hoelzl@29805 ` 1010` ```qed ``` hoelzl@29805 ` 1011` hoelzl@29805 ` 1012` ```subsection "Compute the cosinus in the entire domain" ``` hoelzl@29805 ` 1013` hoelzl@29805 ` 1014` ```definition lb_cos :: "nat \ float \ float" where ``` hoelzl@29805 ` 1015` ```"lb_cos prec x = (let ``` hoelzl@29805 ` 1016` ``` horner = \ x. lb_sin_cos_aux prec (get_even (prec div 4 + 1)) 1 1 (x * x) ; ``` hoelzl@29805 ` 1017` ``` half = \ x. if x < 0 then - 1 else Float 1 1 * x * x - 1 ``` hoelzl@29805 ` 1018` ``` in if x < Float 1 -1 then horner x ``` hoelzl@29805 ` 1019` ```else if x < 1 then half (horner (x * Float 1 -1)) ``` hoelzl@29805 ` 1020` ``` else half (half (horner (x * Float 1 -2))))" ``` hoelzl@29805 ` 1021` hoelzl@29805 ` 1022` ```definition ub_cos :: "nat \ float \ float" where ``` hoelzl@29805 ` 1023` ```"ub_cos prec x = (let ``` hoelzl@29805 ` 1024` ``` horner = \ x. ub_sin_cos_aux prec (get_odd (prec div 4 + 1)) 1 1 (x * x) ; ``` hoelzl@29805 ` 1025` ``` half = \ x. Float 1 1 * x * x - 1 ``` hoelzl@29805 ` 1026` ``` in if x < Float 1 -1 then horner x ``` hoelzl@29805 ` 1027` ```else if x < 1 then half (horner (x * Float 1 -1)) ``` hoelzl@29805 ` 1028` ``` else half (half (horner (x * Float 1 -2))))" ``` hoelzl@29805 ` 1029` hoelzl@31467 ` 1030` ```lemma lb_cos: assumes "0 \ real x" and "real x \ pi" ``` hoelzl@31098 ` 1031` ``` shows "cos (real x) \ {real (lb_cos prec x) .. real (ub_cos prec x)}" (is "?cos x \ { real (?lb x) .. real (?ub x) }") ``` hoelzl@29805 ` 1032` ```proof - ``` hoelzl@29805 ` 1033` ``` { fix x :: real ``` hoelzl@29805 ` 1034` ``` have "cos x = cos (x / 2 + x / 2)" by auto ``` hoelzl@29805 ` 1035` ``` also have "\ = cos (x / 2) * cos (x / 2) + sin (x / 2) * sin (x / 2) - sin (x / 2) * sin (x / 2) + cos (x / 2) * cos (x / 2) - 1" ``` hoelzl@29805 ` 1036` ``` unfolding cos_add by auto ``` hoelzl@29805 ` 1037` ``` also have "\ = 2 * cos (x / 2) * cos (x / 2) - 1" by algebra ``` hoelzl@29805 ` 1038` ``` finally have "cos x = 2 * cos (x / 2) * cos (x / 2) - 1" . ``` hoelzl@29805 ` 1039` ``` } note x_half = this[symmetric] ``` hoelzl@29805 ` 1040` hoelzl@31098 ` 1041` ``` have "\ x < 0" using `0 \ real x` unfolding less_float_def by auto ``` hoelzl@29805 ` 1042` ``` let "?ub_horner x" = "ub_sin_cos_aux prec (get_odd (prec div 4 + 1)) 1 1 (x * x)" ``` hoelzl@29805 ` 1043` ``` let "?lb_horner x" = "lb_sin_cos_aux prec (get_even (prec div 4 + 1)) 1 1 (x * x)" ``` hoelzl@29805 ` 1044` ``` let "?ub_half x" = "Float 1 1 * x * x - 1" ``` hoelzl@29805 ` 1045` ``` let "?lb_half x" = "if x < 0 then - 1 else Float 1 1 * x * x - 1" ``` hoelzl@29805 ` 1046` hoelzl@29805 ` 1047` ``` show ?thesis ``` hoelzl@29805 ` 1048` ``` proof (cases "x < Float 1 -1") ``` hoelzl@31098 ` 1049` ``` case True hence "real x \ pi / 2" unfolding less_float_def using pi_ge_two by auto ``` hoelzl@29805 ` 1050` ``` show ?thesis unfolding lb_cos_def[where x=x] ub_cos_def[where x=x] if_not_P[OF `\ x < 0`] if_P[OF `x < Float 1 -1`] Let_def ``` hoelzl@31098 ` 1051` ``` using cos_boundaries[OF `0 \ real x` `real x \ pi / 2`] . ``` hoelzl@29805 ` 1052` ``` next ``` hoelzl@29805 ` 1053` ``` case False ``` hoelzl@31098 ` 1054` ``` { fix y x :: float let ?x2 = "real (x * Float 1 -1)" ``` hoelzl@31098 ` 1055` ``` assume "real y \ cos ?x2" and "-pi \ real x" and "real x \ pi" ``` hoelzl@31098 ` 1056` ``` hence "- (pi / 2) \ ?x2" and "?x2 \ pi / 2" using pi_ge_two unfolding real_of_float_mult Float_num by auto ``` hoelzl@29805 ` 1057` ``` hence "0 \ cos ?x2" by (rule cos_ge_zero) ``` hoelzl@31467 ` 1058` hoelzl@31098 ` 1059` ``` have "real (?lb_half y) \ cos (real x)" ``` hoelzl@29805 ` 1060` ``` proof (cases "y < 0") ``` hoelzl@29805 ` 1061` ``` case True show ?thesis using cos_ge_minus_one unfolding if_P[OF True] by auto ``` hoelzl@29805 ` 1062` ``` next ``` hoelzl@29805 ` 1063` ``` case False ``` hoelzl@31098 ` 1064` ``` hence "0 \ real y" unfolding less_float_def by auto ``` hoelzl@31098 ` 1065` ``` from mult_mono[OF `real y \ cos ?x2` `real y \ cos ?x2` `0 \ cos ?x2` this] ``` hoelzl@31098 ` 1066` ``` have "real y * real y \ cos ?x2 * cos ?x2" . ``` hoelzl@31098 ` 1067` ``` hence "2 * real y * real y \ 2 * cos ?x2 * cos ?x2" by auto ``` hoelzl@31098 ` 1068` ``` hence "2 * real y * real y - 1 \ 2 * cos (real x / 2) * cos (real x / 2) - 1" unfolding Float_num real_of_float_mult by auto ``` hoelzl@31098 ` 1069` ``` thus ?thesis unfolding if_not_P[OF False] x_half Float_num real_of_float_mult real_of_float_sub by auto ``` hoelzl@29805 ` 1070` ``` qed ``` hoelzl@29805 ` 1071` ``` } note lb_half = this ``` hoelzl@31467 ` 1072` hoelzl@31098 ` 1073` ``` { fix y x :: float let ?x2 = "real (x * Float 1 -1)" ``` hoelzl@31098 ` 1074` ``` assume ub: "cos ?x2 \ real y" and "- pi \ real x" and "real x \ pi" ``` hoelzl@31098 ` 1075` ``` hence "- (pi / 2) \ ?x2" and "?x2 \ pi / 2" using pi_ge_two unfolding real_of_float_mult Float_num by auto ``` hoelzl@29805 ` 1076` ``` hence "0 \ cos ?x2" by (rule cos_ge_zero) ``` hoelzl@31467 ` 1077` hoelzl@31098 ` 1078` ``` have "cos (real x) \ real (?ub_half y)" ``` hoelzl@29805 ` 1079` ``` proof - ``` hoelzl@31098 ` 1080` ``` have "0 \ real y" using `0 \ cos ?x2` ub by (rule order_trans) ``` hoelzl@29805 ` 1081` ``` from mult_mono[OF ub ub this `0 \ cos ?x2`] ``` hoelzl@31098 ` 1082` ``` have "cos ?x2 * cos ?x2 \ real y * real y" . ``` hoelzl@31098 ` 1083` ``` hence "2 * cos ?x2 * cos ?x2 \ 2 * real y * real y" by auto ``` hoelzl@31098 ` 1084` ``` hence "2 * cos (real x / 2) * cos (real x / 2) - 1 \ 2 * real y * real y - 1" unfolding Float_num real_of_float_mult by auto ``` hoelzl@31098 ` 1085` ``` thus ?thesis unfolding x_half real_of_float_mult Float_num real_of_float_sub by auto ``` hoelzl@29805 ` 1086` ``` qed ``` hoelzl@29805 ` 1087` ``` } note ub_half = this ``` hoelzl@31467 ` 1088` hoelzl@29805 ` 1089` ``` let ?x2 = "x * Float 1 -1" ``` hoelzl@29805 ` 1090` ``` let ?x4 = "x * Float 1 -1 * Float 1 -1" ``` hoelzl@31467 ` 1091` hoelzl@31098 ` 1092` ``` have "-pi \ real x" using pi_ge_zero[THEN le_imp_neg_le, unfolded minus_zero] `0 \ real x` by (rule order_trans) ``` hoelzl@31467 ` 1093` hoelzl@29805 ` 1094` ``` show ?thesis ``` hoelzl@29805 ` 1095` ``` proof (cases "x < 1") ``` hoelzl@31098 ` 1096` ``` case True hence "real x \ 1" unfolding less_float_def by auto ``` hoelzl@31098 ` 1097` ``` have "0 \ real ?x2" and "real ?x2 \ pi / 2" using pi_ge_two `0 \ real x` unfolding real_of_float_mult Float_num using assms by auto ``` hoelzl@29805 ` 1098` ``` from cos_boundaries[OF this] ``` hoelzl@31098 ` 1099` ``` have lb: "real (?lb_horner ?x2) \ ?cos ?x2" and ub: "?cos ?x2 \ real (?ub_horner ?x2)" by auto ``` hoelzl@31467 ` 1100` hoelzl@31098 ` 1101` ``` have "real (?lb x) \ ?cos x" ``` hoelzl@29805 ` 1102` ``` proof - ``` hoelzl@31098 ` 1103` ``` from lb_half[OF lb `-pi \ real x` `real x \ pi`] ``` hoelzl@29805 ` 1104` ``` show ?thesis unfolding lb_cos_def[where x=x] Let_def using `\ x < 0` `\ x < Float 1 -1` `x < 1` by auto ``` hoelzl@29805 ` 1105` ``` qed ``` hoelzl@31098 ` 1106` ``` moreover have "?cos x \ real (?ub x)" ``` hoelzl@29805 ` 1107` ``` proof - ``` hoelzl@31098 ` 1108` ``` from ub_half[OF ub `-pi \ real x` `real x \ pi`] ``` hoelzl@31809 ` 1109` ``` show ?thesis unfolding ub_cos_def[where x=x] Let_def using `\ x < 0` `\ x < Float 1 -1` `x < 1` by auto ``` hoelzl@29805 ` 1110` ``` qed ``` hoelzl@29805 ` 1111` ``` ultimately show ?thesis by auto ``` hoelzl@29805 ` 1112` ``` next ``` hoelzl@29805 ` 1113` ``` case False ``` hoelzl@31098 ` 1114` ``` have "0 \ real ?x4" and "real ?x4 \ pi / 2" using pi_ge_two `0 \ real x` `real x \ pi` unfolding real_of_float_mult Float_num by auto ``` hoelzl@29805 ` 1115` ``` from cos_boundaries[OF this] ``` hoelzl@31098 ` 1116` ``` have lb: "real (?lb_horner ?x4) \ ?cos ?x4" and ub: "?cos ?x4 \ real (?ub_horner ?x4)" by auto ``` hoelzl@31467 ` 1117` hoelzl@29805 ` 1118` ``` have eq_4: "?x2 * Float 1 -1 = x * Float 1 -2" by (cases x, auto simp add: times_float.simps) ``` hoelzl@31467 ` 1119` hoelzl@31098 ` 1120` ``` have "real (?lb x) \ ?cos x" ``` hoelzl@29805 ` 1121` ``` proof - ``` hoelzl@31098 ` 1122` ``` have "-pi \ real ?x2" and "real ?x2 \ pi" unfolding real_of_float_mult Float_num using pi_ge_two `0 \ real x` `real x \ pi` by auto ``` hoelzl@31098 ` 1123` ``` from lb_half[OF lb_half[OF lb this] `-pi \ real x` `real x \ pi`, unfolded eq_4] ``` hoelzl@29805 ` 1124` ``` show ?thesis unfolding lb_cos_def[where x=x] if_not_P[OF `\ x < 0`] if_not_P[OF `\ x < Float 1 -1`] if_not_P[OF `\ x < 1`] Let_def . ``` hoelzl@29805 ` 1125` ``` qed ``` hoelzl@31098 ` 1126` ``` moreover have "?cos x \ real (?ub x)" ``` hoelzl@29805 ` 1127` ``` proof - ``` hoelzl@31098 ` 1128` ``` have "-pi \ real ?x2" and "real ?x2 \ pi" unfolding real_of_float_mult Float_num using pi_ge_two `0 \ real x` `real x \ pi` by auto ``` hoelzl@31098 ` 1129` ``` from ub_half[OF ub_half[OF ub this] `-pi \ real x` `real x \ pi`, unfolded eq_4] ``` hoelzl@29805 ` 1130` ``` show ?thesis unfolding ub_cos_def[where x=x] if_not_P[OF `\ x < 0`] if_not_P[OF `\ x < Float 1 -1`] if_not_P[OF `\ x < 1`] Let_def . ``` hoelzl@29805 ` 1131` ``` qed ``` hoelzl@29805 ` 1132` ``` ultimately show ?thesis by auto ``` hoelzl@29805 ` 1133` ``` qed ``` hoelzl@29805 ` 1134` ``` qed ``` hoelzl@29805 ` 1135` ```qed ``` hoelzl@29805 ` 1136` hoelzl@31467 ` 1137` ```lemma lb_cos_minus: assumes "-pi \ real x" and "real x \ 0" ``` hoelzl@31098 ` 1138` ``` shows "cos (real (-x)) \ {real (lb_cos prec (-x)) .. real (ub_cos prec (-x))}" ``` hoelzl@29805 ` 1139` ```proof - ``` hoelzl@31098 ` 1140` ``` have "0 \ real (-x)" and "real (-x) \ pi" using `-pi \ real x` `real x \ 0` by auto ``` hoelzl@29805 ` 1141` ``` from lb_cos[OF this] show ?thesis . ``` hoelzl@29805 ` 1142` ```qed ``` hoelzl@29805 ` 1143` hoelzl@31467 ` 1144` ```definition bnds_cos :: "nat \ float \ float \ float * float" where ``` hoelzl@31467 ` 1145` ```"bnds_cos prec lx ux = (let ``` hoelzl@31467 ` 1146` ``` lpi = round_down prec (lb_pi prec) ; ``` hoelzl@31467 ` 1147` ``` upi = round_up prec (ub_pi prec) ; ``` hoelzl@31467 ` 1148` ``` k = floor_fl (float_divr prec (lx + lpi) (2 * lpi)) ; ``` hoelzl@31467 ` 1149` ``` lx = lx - k * 2 * (if k < 0 then lpi else upi) ; ``` hoelzl@31467 ` 1150` ``` ux = ux - k * 2 * (if k < 0 then upi else lpi) ``` hoelzl@31467 ` 1151` ``` in if - lpi \ lx \ ux \ 0 then (lb_cos prec (-lx), ub_cos prec (-ux)) ``` hoelzl@31467 ` 1152` ``` else if 0 \ lx \ ux \ lpi then (lb_cos prec ux, ub_cos prec lx) ``` hoelzl@31467 ` 1153` ``` else if - lpi \ lx \ ux \ lpi then (min (lb_cos prec (-lx)) (lb_cos prec ux), Float 1 0) ``` hoelzl@31467 ` 1154` ``` else if 0 \ lx \ ux \ 2 * lpi then (Float -1 0, max (ub_cos prec lx) (ub_cos prec (- (ux - 2 * lpi)))) ``` hoelzl@31508 ` 1155` ``` else if -2 * lpi \ lx \ ux \ 0 then (Float -1 0, max (ub_cos prec (lx + 2 * lpi)) (ub_cos prec (-ux))) ``` hoelzl@31467 ` 1156` ``` else (Float -1 0, Float 1 0))" ``` hoelzl@29805 ` 1157` hoelzl@31467 ` 1158` ```lemma floor_int: ``` hoelzl@31467 ` 1159` ``` obtains k :: int where "real k = real (floor_fl f)" ``` hoelzl@31467 ` 1160` ```proof - ``` hoelzl@31467 ` 1161` ``` assume *: "\ k :: int. real k = real (floor_fl f) \ thesis" ``` hoelzl@31467 ` 1162` ``` obtain m e where fl: "Float m e = floor_fl f" by (cases "floor_fl f", auto) ``` hoelzl@31467 ` 1163` ``` from floor_pos_exp[OF this] ``` hoelzl@31467 ` 1164` ``` have "real (m* 2^(nat e)) = real (floor_fl f)" ``` hoelzl@31467 ` 1165` ``` by (auto simp add: fl[symmetric] real_of_float_def pow2_def) ``` hoelzl@31467 ` 1166` ``` from *[OF this] show thesis by blast ``` hoelzl@31467 ` 1167` ```qed ``` hoelzl@29805 ` 1168` hoelzl@31467 ` 1169` ```lemma float_remove_real_numeral[simp]: "real (number_of k :: float) = number_of k" ``` hoelzl@31467 ` 1170` ```proof - ``` hoelzl@31467 ` 1171` ``` have "real (number_of k :: float) = real k" ``` hoelzl@31467 ` 1172` ``` unfolding number_of_float_def real_of_float_def pow2_def by auto ``` hoelzl@31467 ` 1173` ``` also have "\ = real (number_of k :: int)" ``` hoelzl@31467 ` 1174` ``` by (simp add: number_of_is_id) ``` hoelzl@31467 ` 1175` ``` finally show ?thesis by auto ``` hoelzl@31467 ` 1176` ```qed ``` hoelzl@29805 ` 1177` hoelzl@31467 ` 1178` ```lemma cos_periodic_nat[simp]: fixes n :: nat shows "cos (x + real n * 2 * pi) = cos x" ``` hoelzl@31467 ` 1179` ```proof (induct n arbitrary: x) ``` hoelzl@31467 ` 1180` ``` case (Suc n) ``` hoelzl@31467 ` 1181` ``` have split_pi_off: "x + real (Suc n) * 2 * pi = (x + real n * 2 * pi) + 2 * pi" ``` nipkow@31790 ` 1182` ``` unfolding Suc_eq_plus1 real_of_nat_add real_of_one real_add_mult_distrib by auto ``` hoelzl@31467 ` 1183` ``` show ?case unfolding split_pi_off using Suc by auto ``` hoelzl@31467 ` 1184` ```qed auto ``` hoelzl@31467 ` 1185` hoelzl@31467 ` 1186` ```lemma cos_periodic_int[simp]: fixes i :: int shows "cos (x + real i * 2 * pi) = cos x" ``` hoelzl@31467 ` 1187` ```proof (cases "0 \ i") ``` hoelzl@31467 ` 1188` ``` case True hence i_nat: "real i = real (nat i)" by auto ``` hoelzl@31467 ` 1189` ``` show ?thesis unfolding i_nat by auto ``` hoelzl@31467 ` 1190` ```next ``` hoelzl@31467 ` 1191` ``` case False hence i_nat: "real i = - real (nat (-i))" by auto ``` hoelzl@31467 ` 1192` ``` have "cos x = cos (x + real i * 2 * pi - real i * 2 * pi)" by auto ``` hoelzl@31467 ` 1193` ``` also have "\ = cos (x + real i * 2 * pi)" ``` hoelzl@31467 ` 1194` ``` unfolding i_nat mult_minus_left diff_minus_eq_add by (rule cos_periodic_nat) ``` hoelzl@31467 ` 1195` ``` finally show ?thesis by auto ``` hoelzl@29805 ` 1196` ```qed ``` hoelzl@29805 ` 1197` hoelzl@31467 ` 1198` ```lemma bnds_cos: "\ x lx ux. (l, u) = bnds_cos prec lx ux \ x \ {real lx .. real ux} \ real l \ cos x \ cos x \ real u" ``` hoelzl@31467 ` 1199` ```proof ((rule allI | rule impI | erule conjE) +) ``` hoelzl@31467 ` 1200` ``` fix x lx ux ``` hoelzl@31467 ` 1201` ``` assume bnds: "(l, u) = bnds_cos prec lx ux" and x: "x \ {real lx .. real ux}" ``` hoelzl@31467 ` 1202` hoelzl@31467 ` 1203` ``` let ?lpi = "round_down prec (lb_pi prec)" ``` hoelzl@31467 ` 1204` ``` let ?upi = "round_up prec (ub_pi prec)" ``` hoelzl@31467 ` 1205` ``` let ?k = "floor_fl (float_divr prec (lx + ?lpi) (2 * ?lpi))" ``` hoelzl@31467 ` 1206` ``` let ?lx = "lx - ?k * 2 * (if ?k < 0 then ?lpi else ?upi)" ``` hoelzl@31467 ` 1207` ``` let ?ux = "ux - ?k * 2 * (if ?k < 0 then ?upi else ?lpi)" ``` hoelzl@31467 ` 1208` hoelzl@31467 ` 1209` ``` obtain k :: int where k: "real k = real ?k" using floor_int . ``` hoelzl@31467 ` 1210` hoelzl@31467 ` 1211` ``` have upi: "pi \ real ?upi" and lpi: "real ?lpi \ pi" ``` hoelzl@31467 ` 1212` ``` using round_up[of "ub_pi prec" prec] pi_boundaries[of prec] ``` hoelzl@31467 ` 1213` ``` round_down[of prec "lb_pi prec"] by auto ``` hoelzl@31467 ` 1214` ``` hence "real ?lx \ x - real k * 2 * pi \ x - real k * 2 * pi \ real ?ux" ``` hoelzl@31467 ` 1215` ``` using x by (cases "k = 0") (auto intro!: add_mono ``` hoelzl@31467 ` 1216` ``` simp add: real_diff_def k[symmetric] less_float_def) ``` hoelzl@31467 ` 1217` ``` note lx = this[THEN conjunct1] and ux = this[THEN conjunct2] ``` hoelzl@31467 ` 1218` ``` hence lx_less_ux: "real ?lx \ real ?ux" by (rule order_trans) ``` hoelzl@31467 ` 1219` hoelzl@31467 ` 1220` ``` { assume "- ?lpi \ ?lx" and x_le_0: "x - real k * 2 * pi \ 0" ``` hoelzl@31467 ` 1221` ``` with lpi[THEN le_imp_neg_le] lx ``` hoelzl@31467 ` 1222` ``` have pi_lx: "- pi \ real ?lx" and lx_0: "real ?lx \ 0" ``` hoelzl@31467 ` 1223` ``` by (simp_all add: le_float_def) ``` hoelzl@29805 ` 1224` hoelzl@31467 ` 1225` ``` have "real (lb_cos prec (- ?lx)) \ cos (real (- ?lx))" ``` hoelzl@31467 ` 1226` ``` using lb_cos_minus[OF pi_lx lx_0] by simp ``` hoelzl@31467 ` 1227` ``` also have "\ \ cos (x + real (-k) * 2 * pi)" ``` hoelzl@31467 ` 1228` ``` using cos_monotone_minus_pi_0'[OF pi_lx lx x_le_0] ``` hoelzl@31467 ` 1229` ``` by (simp only: real_of_float_minus real_of_int_minus ``` hoelzl@31467 ` 1230` ``` cos_minus real_diff_def mult_minus_left) ``` hoelzl@31467 ` 1231` ``` finally have "real (lb_cos prec (- ?lx)) \ cos x" ``` hoelzl@31467 ` 1232` ``` unfolding cos_periodic_int . } ``` hoelzl@31467 ` 1233` ``` note negative_lx = this ``` hoelzl@31467 ` 1234` hoelzl@31467 ` 1235` ``` { assume "0 \ ?lx" and pi_x: "x - real k * 2 * pi \ pi" ``` hoelzl@31467 ` 1236` ``` with lx ``` hoelzl@31467 ` 1237` ``` have pi_lx: "real ?lx \ pi" and lx_0: "0 \ real ?lx" ``` hoelzl@31467 ` 1238` ``` by (auto simp add: le_float_def) ``` hoelzl@29805 ` 1239` hoelzl@31467 ` 1240` ``` have "cos (x + real (-k) * 2 * pi) \ cos (real ?lx)" ``` hoelzl@31467 ` 1241` ``` using cos_monotone_0_pi'[OF lx_0 lx pi_x] ``` hoelzl@31467 ` 1242` ``` by (simp only: real_of_float_minus real_of_int_minus ``` hoelzl@31467 ` 1243` ``` cos_minus real_diff_def mult_minus_left) ``` hoelzl@31467 ` 1244` ``` also have "\ \ real (ub_cos prec ?lx)" ``` hoelzl@31467 ` 1245` ``` using lb_cos[OF lx_0 pi_lx] by simp ``` hoelzl@31467 ` 1246` ``` finally have "cos x \ real (ub_cos prec ?lx)" ``` hoelzl@31467 ` 1247` ``` unfolding cos_periodic_int . } ``` hoelzl@31467 ` 1248` ``` note positive_lx = this ``` hoelzl@31467 ` 1249` hoelzl@31467 ` 1250` ``` { assume pi_x: "- pi \ x - real k * 2 * pi" and "?ux \ 0" ``` hoelzl@31467 ` 1251` ``` with ux ``` hoelzl@31467 ` 1252` ``` have pi_ux: "- pi \ real ?ux" and ux_0: "real ?ux \ 0" ``` hoelzl@31467 ` 1253` ``` by (simp_all add: le_float_def) ``` hoelzl@29805 ` 1254` hoelzl@31467 ` 1255` ``` have "cos (x + real (-k) * 2 * pi) \ cos (real (- ?ux))" ``` hoelzl@31467 ` 1256` ``` using cos_monotone_minus_pi_0'[OF pi_x ux ux_0] ``` hoelzl@31467 ` 1257` ``` by (simp only: real_of_float_minus real_of_int_minus ``` hoelzl@31467 ` 1258` ``` cos_minus real_diff_def mult_minus_left) ``` hoelzl@31467 ` 1259` ``` also have "\ \ real (ub_cos prec (- ?ux))" ``` hoelzl@31467 ` 1260` ``` using lb_cos_minus[OF pi_ux ux_0, of prec] by simp ``` hoelzl@31467 ` 1261` ``` finally have "cos x \ real (ub_cos prec (- ?ux))" ``` hoelzl@31467 ` 1262` ``` unfolding cos_periodic_int . } ``` hoelzl@31467 ` 1263` ``` note negative_ux = this ``` hoelzl@31467 ` 1264` hoelzl@31467 ` 1265` ``` { assume "?ux \ ?lpi" and x_ge_0: "0 \ x - real k * 2 * pi" ``` hoelzl@31467 ` 1266` ``` with lpi ux ``` hoelzl@31467 ` 1267` ``` have pi_ux: "real ?ux \ pi" and ux_0: "0 \ real ?ux" ``` hoelzl@31467 ` 1268` ``` by (simp_all add: le_float_def) ``` hoelzl@31467 ` 1269` hoelzl@31467 ` 1270` ``` have "real (lb_cos prec ?ux) \ cos (real ?ux)" ``` hoelzl@31467 ` 1271` ``` using lb_cos[OF ux_0 pi_ux] by simp ``` hoelzl@31467 ` 1272` ``` also have "\ \ cos (x + real (-k) * 2 * pi)" ``` hoelzl@31467 ` 1273` ``` using cos_monotone_0_pi'[OF x_ge_0 ux pi_ux] ``` hoelzl@31467 ` 1274` ``` by (simp only: real_of_float_minus real_of_int_minus ``` hoelzl@31467 ` 1275` ``` cos_minus real_diff_def mult_minus_left) ``` hoelzl@31467 ` 1276` ``` finally have "real (lb_cos prec ?ux) \ cos x" ``` hoelzl@31467 ` 1277` ``` unfolding cos_periodic_int . } ``` hoelzl@31467 ` 1278` ``` note positive_ux = this ``` hoelzl@31467 ` 1279` hoelzl@31467 ` 1280` ``` show "real l \ cos x \ cos x \ real u" ``` hoelzl@31467 ` 1281` ``` proof (cases "- ?lpi \ ?lx \ ?ux \ 0") ``` hoelzl@31467 ` 1282` ``` case True with bnds ``` hoelzl@31467 ` 1283` ``` have l: "l = lb_cos prec (-?lx)" ``` hoelzl@31467 ` 1284` ``` and u: "u = ub_cos prec (-?ux)" ``` hoelzl@31467 ` 1285` ``` by (auto simp add: bnds_cos_def Let_def) ``` hoelzl@29805 ` 1286` hoelzl@31467 ` 1287` ``` from True lpi[THEN le_imp_neg_le] lx ux ``` hoelzl@31467 ` 1288` ``` have "- pi \ x - real k * 2 * pi" ``` hoelzl@31467 ` 1289` ``` and "x - real k * 2 * pi \ 0" ``` hoelzl@31467 ` 1290` ``` by (auto simp add: le_float_def) ``` hoelzl@31467 ` 1291` ``` with True negative_ux negative_lx ``` hoelzl@31467 ` 1292` ``` show ?thesis unfolding l u by simp ``` hoelzl@31467 ` 1293` ``` next case False note 1 = this show ?thesis ``` hoelzl@31467 ` 1294` ``` proof (cases "0 \ ?lx \ ?ux \ ?lpi") ``` hoelzl@31467 ` 1295` ``` case True with bnds 1 ``` hoelzl@31467 ` 1296` ``` have l: "l = lb_cos prec ?ux" ``` hoelzl@31467 ` 1297` ``` and u: "u = ub_cos prec ?lx" ``` hoelzl@31467 ` 1298` ``` by (auto simp add: bnds_cos_def Let_def) ``` hoelzl@29805 ` 1299` hoelzl@31467 ` 1300` ``` from True lpi lx ux ``` hoelzl@31467 ` 1301` ``` have "0 \ x - real k * 2 * pi" ``` hoelzl@31467 ` 1302` ``` and "x - real k * 2 * pi \ pi" ``` hoelzl@31467 ` 1303` ``` by (auto simp add: le_float_def) ``` hoelzl@31467 ` 1304` ``` with True positive_ux positive_lx ``` hoelzl@31467 ` 1305` ``` show ?thesis unfolding l u by simp ``` hoelzl@31467 ` 1306` ``` next case False note 2 = this show ?thesis ``` hoelzl@31467 ` 1307` ``` proof (cases "- ?lpi \ ?lx \ ?ux \ ?lpi") ``` hoelzl@31467 ` 1308` ``` case True note Cond = this with bnds 1 2 ``` hoelzl@31467 ` 1309` ``` have l: "l = min (lb_cos prec (-?lx)) (lb_cos prec ?ux)" ``` hoelzl@31467 ` 1310` ``` and u: "u = Float 1 0" ``` hoelzl@31467 ` 1311` ``` by (auto simp add: bnds_cos_def Let_def) ``` hoelzl@29805 ` 1312` hoelzl@31467 ` 1313` ``` show ?thesis unfolding u l using negative_lx positive_ux Cond ``` hoelzl@31467 ` 1314` ``` by (cases "x - real k * 2 * pi < 0", simp_all add: real_of_float_min) ``` hoelzl@31467 ` 1315` ``` next case False note 3 = this show ?thesis ``` hoelzl@31467 ` 1316` ``` proof (cases "0 \ ?lx \ ?ux \ 2 * ?lpi") ``` hoelzl@31467 ` 1317` ``` case True note Cond = this with bnds 1 2 3 ``` hoelzl@31467 ` 1318` ``` have l: "l = Float -1 0" ``` hoelzl@31467 ` 1319` ``` and u: "u = max (ub_cos prec ?lx) (ub_cos prec (- (?ux - 2 * ?lpi)))" ``` hoelzl@31467 ` 1320` ``` by (auto simp add: bnds_cos_def Let_def) ``` hoelzl@31467 ` 1321` hoelzl@31467 ` 1322` ``` have "cos x \ real u" ``` hoelzl@31467 ` 1323` ``` proof (cases "x - real k * 2 * pi < pi") ``` hoelzl@31467 ` 1324` ``` case True hence "x - real k * 2 * pi \ pi" by simp ``` hoelzl@31467 ` 1325` ``` from positive_lx[OF Cond[THEN conjunct1] this] ``` hoelzl@31467 ` 1326` ``` show ?thesis unfolding u by (simp add: real_of_float_max) ``` hoelzl@29805 ` 1327` ``` next ``` hoelzl@31467 ` 1328` ``` case False hence "pi \ x - real k * 2 * pi" by simp ``` hoelzl@31467 ` 1329` ``` hence pi_x: "- pi \ x - real k * 2 * pi - 2 * pi" by simp ``` hoelzl@31467 ` 1330` hoelzl@31467 ` 1331` ``` have "real ?ux \ 2 * pi" using Cond lpi by (auto simp add: le_float_def) ``` hoelzl@31467 ` 1332` ``` hence "x - real k * 2 * pi - 2 * pi \ 0" using ux by simp ``` hoelzl@31467 ` 1333` hoelzl@31467 ` 1334` ``` have ux_0: "real (?ux - 2 * ?lpi) \ 0" ``` hoelzl@31467 ` 1335` ``` using Cond by (auto simp add: le_float_def) ``` hoelzl@31467 ` 1336` hoelzl@31467 ` 1337` ``` from 2 and Cond have "\ ?ux \ ?lpi" by auto ``` hoelzl@31467 ` 1338` ``` hence "- ?lpi \ ?ux - 2 * ?lpi" by (auto simp add: le_float_def) ``` hoelzl@31467 ` 1339` ``` hence pi_ux: "- pi \ real (?ux - 2 * ?lpi)" ``` hoelzl@31467 ` 1340` ``` using lpi[THEN le_imp_neg_le] by (auto simp add: le_float_def) ``` hoelzl@31467 ` 1341` hoelzl@31467 ` 1342` ``` have x_le_ux: "x - real k * 2 * pi - 2 * pi \ real (?ux - 2 * ?lpi)" ``` hoelzl@31467 ` 1343` ``` using ux lpi by auto ``` hoelzl@31467 ` 1344` hoelzl@31467 ` 1345` ``` have "cos x = cos (x + real (-k) * 2 * pi + real (-1 :: int) * 2 * pi)" ``` hoelzl@31467 ` 1346` ``` unfolding cos_periodic_int .. ``` hoelzl@31467 ` 1347` ``` also have "\ \ cos (real (?ux - 2 * ?lpi))" ``` hoelzl@31467 ` 1348` ``` using cos_monotone_minus_pi_0'[OF pi_x x_le_ux ux_0] ``` hoelzl@31467 ` 1349` ``` by (simp only: real_of_float_minus real_of_int_minus real_of_one ``` hoelzl@31467 ` 1350` ``` number_of_Min real_diff_def mult_minus_left real_mult_1) ``` hoelzl@31467 ` 1351` ``` also have "\ = cos (real (- (?ux - 2 * ?lpi)))" ``` hoelzl@31467 ` 1352` ``` unfolding real_of_float_minus cos_minus .. ``` hoelzl@31467 ` 1353` ``` also have "\ \ real (ub_cos prec (- (?ux - 2 * ?lpi)))" ``` hoelzl@31467 ` 1354` ``` using lb_cos_minus[OF pi_ux ux_0] by simp ``` hoelzl@31467 ` 1355` ``` finally show ?thesis unfolding u by (simp add: real_of_float_max) ``` hoelzl@29805 ` 1356` ``` qed ``` hoelzl@31467 ` 1357` ``` thus ?thesis unfolding l by auto ``` hoelzl@31508 ` 1358` ``` next case False note 4 = this show ?thesis ``` hoelzl@31508 ` 1359` ``` proof (cases "-2 * ?lpi \ ?lx \ ?ux \ 0") ``` hoelzl@31508 ` 1360` ``` case True note Cond = this with bnds 1 2 3 4 ``` hoelzl@31508 ` 1361` ``` have l: "l = Float -1 0" ``` hoelzl@31508 ` 1362` ``` and u: "u = max (ub_cos prec (?lx + 2 * ?lpi)) (ub_cos prec (-?ux))" ``` hoelzl@31508 ` 1363` ``` by (auto simp add: bnds_cos_def Let_def) ``` hoelzl@31508 ` 1364` hoelzl@31508 ` 1365` ``` have "cos x \ real u" ``` hoelzl@31508 ` 1366` ``` proof (cases "-pi < x - real k * 2 * pi") ``` hoelzl@31508 ` 1367` ``` case True hence "-pi \ x - real k * 2 * pi" by simp ``` hoelzl@31508 ` 1368` ``` from negative_ux[OF this Cond[THEN conjunct2]] ``` hoelzl@31508 ` 1369` ``` show ?thesis unfolding u by (simp add: real_of_float_max) ``` hoelzl@31508 ` 1370` ``` next ``` hoelzl@31508 ` 1371` ``` case False hence "x - real k * 2 * pi \ -pi" by simp ``` hoelzl@31508 ` 1372` ``` hence pi_x: "x - real k * 2 * pi + 2 * pi \ pi" by simp ``` hoelzl@31508 ` 1373` hoelzl@31508 ` 1374` ``` have "-2 * pi \ real ?lx" using Cond lpi by (auto simp add: le_float_def) ``` hoelzl@31508 ` 1375` hoelzl@31508 ` 1376` ``` hence "0 \ x - real k * 2 * pi + 2 * pi" using lx by simp ``` hoelzl@31508 ` 1377` hoelzl@31508 ` 1378` ``` have lx_0: "0 \ real (?lx + 2 * ?lpi)" ``` hoelzl@31508 ` 1379` ``` using Cond lpi by (auto simp add: le_float_def) ``` hoelzl@31508 ` 1380` hoelzl@31508 ` 1381` ``` from 1 and Cond have "\ -?lpi \ ?lx" by auto ``` hoelzl@31508 ` 1382` ``` hence "?lx + 2 * ?lpi \ ?lpi" by (auto simp add: le_float_def) ``` hoelzl@31508 ` 1383` ``` hence pi_lx: "real (?lx + 2 * ?lpi) \ pi" ``` hoelzl@31508 ` 1384` ``` using lpi[THEN le_imp_neg_le] by (auto simp add: le_float_def) ``` hoelzl@31508 ` 1385` hoelzl@31508 ` 1386` ``` have lx_le_x: "real (?lx + 2 * ?lpi) \ x - real k * 2 * pi + 2 * pi" ``` hoelzl@31508 ` 1387` ``` using lx lpi by auto ``` hoelzl@31508 ` 1388` hoelzl@31508 ` 1389` ``` have "cos x = cos (x + real (-k) * 2 * pi + real (1 :: int) * 2 * pi)" ``` hoelzl@31508 ` 1390` ``` unfolding cos_periodic_int .. ``` hoelzl@31508 ` 1391` ``` also have "\ \ cos (real (?lx + 2 * ?lpi))" ``` hoelzl@31508 ` 1392` ``` using cos_monotone_0_pi'[OF lx_0 lx_le_x pi_x] ``` hoelzl@31508 ` 1393` ``` by (simp only: real_of_float_minus real_of_int_minus real_of_one ``` hoelzl@31508 ` 1394` ``` number_of_Min real_diff_def mult_minus_left real_mult_1) ``` hoelzl@31508 ` 1395` ``` also have "\ \ real (ub_cos prec (?lx + 2 * ?lpi))" ``` hoelzl@31508 ` 1396` ``` using lb_cos[OF lx_0 pi_lx] by simp ``` hoelzl@31508 ` 1397` ``` finally show ?thesis unfolding u by (simp add: real_of_float_max) ``` hoelzl@31508 ` 1398` ``` qed ``` hoelzl@31508 ` 1399` ``` thus ?thesis unfolding l by auto ``` hoelzl@29805 ` 1400` ``` next ``` hoelzl@31508 ` 1401` ``` case False with bnds 1 2 3 4 show ?thesis by (auto simp add: bnds_cos_def Let_def) ``` hoelzl@31508 ` 1402` ``` qed qed qed qed qed ``` hoelzl@29805 ` 1403` ```qed ``` hoelzl@29805 ` 1404` hoelzl@29805 ` 1405` ```section "Exponential function" ``` hoelzl@29805 ` 1406` hoelzl@29805 ` 1407` ```subsection "Compute the series of the exponential function" ``` hoelzl@29805 ` 1408` hoelzl@29805 ` 1409` ```fun ub_exp_horner :: "nat \ nat \ nat \ nat \ float \ float" and lb_exp_horner :: "nat \ nat \ nat \ nat \ float \ float" where ``` hoelzl@29805 ` 1410` ```"ub_exp_horner prec 0 i k x = 0" | ``` hoelzl@29805 ` 1411` ```"ub_exp_horner prec (Suc n) i k x = rapprox_rat prec 1 (int k) + x * lb_exp_horner prec n (i + 1) (k * i) x" | ``` hoelzl@29805 ` 1412` ```"lb_exp_horner prec 0 i k x = 0" | ``` hoelzl@29805 ` 1413` ```"lb_exp_horner prec (Suc n) i k x = lapprox_rat prec 1 (int k) + x * ub_exp_horner prec n (i + 1) (k * i) x" ``` hoelzl@29805 ` 1414` hoelzl@31098 ` 1415` ```lemma bnds_exp_horner: assumes "real x \ 0" ``` hoelzl@31098 ` 1416` ``` shows "exp (real x) \ { real (lb_exp_horner prec (get_even n) 1 1 x) .. real (ub_exp_horner prec (get_odd n) 1 1 x) }" ``` hoelzl@29805 ` 1417` ```proof - ``` hoelzl@29805 ` 1418` ``` { fix n ``` haftmann@30971 ` 1419` ``` have F: "\ m. ((\i. i + 1) ^^ n) m = n + m" by (induct n, auto) ``` haftmann@30971 ` 1420` ``` have "fact (Suc n) = fact n * ((\i. i + 1) ^^ n) 1" unfolding F by auto } note f_eq = this ``` hoelzl@31467 ` 1421` hoelzl@29805 ` 1422` ``` note bounds = horner_bounds_nonpos[where f="fact" and lb="lb_exp_horner prec" and ub="ub_exp_horner prec" and j'=0 and s=1, ``` hoelzl@29805 ` 1423` ``` OF assms f_eq lb_exp_horner.simps ub_exp_horner.simps] ``` hoelzl@29805 ` 1424` hoelzl@31098 ` 1425` ``` { have "real (lb_exp_horner prec (get_even n) 1 1 x) \ (\j = 0.. \ exp (real x)" ``` hoelzl@29805 ` 1428` ``` proof - ``` hoelzl@31098 ` 1429` ``` obtain t where "\t\ \ \real x\" and "exp (real x) = (\m = 0.. exp t / real (fact (get_even n)) * (real x) ^ (get_even n)" ``` hoelzl@29805 ` 1432` ``` by (auto intro!: mult_nonneg_nonneg divide_nonneg_pos simp add: get_even zero_le_even_power exp_gt_zero) ``` hoelzl@29805 ` 1433` ``` ultimately show ?thesis ``` hoelzl@29805 ` 1434` ``` using get_odd exp_gt_zero by (auto intro!: pordered_cancel_semiring_class.mult_nonneg_nonneg) ``` hoelzl@29805 ` 1435` ``` qed ``` hoelzl@31098 ` 1436` ``` finally have "real (lb_exp_horner prec (get_even n) 1 1 x) \ exp (real x)" . ``` hoelzl@29805 ` 1437` ``` } moreover ``` hoelzl@31809 ` 1438` ``` { ``` hoelzl@31098 ` 1439` ``` have x_less_zero: "real x ^ get_odd n \ 0" ``` hoelzl@31098 ` 1440` ``` proof (cases "real x = 0") ``` hoelzl@29805 ` 1441` ``` case True ``` hoelzl@29805 ` 1442` ``` have "(get_odd n) \ 0" using get_odd[THEN odd_pos] by auto ``` hoelzl@29805 ` 1443` ``` thus ?thesis unfolding True power_0_left by auto ``` hoelzl@29805 ` 1444` ``` next ``` hoelzl@31098 ` 1445` ``` case False hence "real x < 0" using `real x \ 0` by auto ``` hoelzl@31098 ` 1446` ``` show ?thesis by (rule less_imp_le, auto simp add: power_less_zero_eq get_odd `real x < 0`) ``` hoelzl@29805 ` 1447` ``` qed ``` hoelzl@29805 ` 1448` hoelzl@31098 ` 1449` ``` obtain t where "\t\ \ \real x\" and "exp (real x) = (\m = 0.. 0" ``` hoelzl@29805 ` 1452` ``` by (auto intro!: mult_nonneg_nonpos divide_nonpos_pos simp add: x_less_zero exp_gt_zero) ``` hoelzl@31098 ` 1453` ``` ultimately have "exp (real x) \ (\j = 0.. \ real (ub_exp_horner prec (get_odd n) 1 1 x)" ``` hoelzl@29805 ` 1456` ``` using bounds(2) by auto ``` hoelzl@31098 ` 1457` ``` finally have "exp (real x) \ real (ub_exp_horner prec (get_odd n) 1 1 x)" . ``` hoelzl@29805 ` 1458` ``` } ultimately show ?thesis by auto ``` hoelzl@29805 ` 1459` ```qed ``` hoelzl@29805 ` 1460` hoelzl@29805 ` 1461` ```subsection "Compute the exponential function on the entire domain" ``` hoelzl@29805 ` 1462` hoelzl@29805 ` 1463` ```function ub_exp :: "nat \ float \ float" and lb_exp :: "nat \ float \ float" where ``` hoelzl@29805 ` 1464` ```"lb_exp prec x = (if 0 < x then float_divl prec 1 (ub_exp prec (-x)) ``` hoelzl@31809 ` 1465` ``` else let ``` hoelzl@29805 ` 1466` ``` horner = (\ x. let y = lb_exp_horner prec (get_even (prec + 2)) 1 1 x in if y \ 0 then Float 1 -2 else y) ``` hoelzl@29805 ` 1467` ``` in if x < - 1 then (case floor_fl x of (Float m e) \ (horner (float_divl prec x (- Float m e))) ^ (nat (-m) * 2 ^ nat e)) ``` hoelzl@29805 ` 1468` ``` else horner x)" | ``` hoelzl@29805 ` 1469` ```"ub_exp prec x = (if 0 < x then float_divr prec 1 (lb_exp prec (-x)) ``` hoelzl@31809 ` 1470` ``` else if x < - 1 then (case floor_fl x of (Float m e) \ ``` hoelzl@29805 ` 1471` ``` (ub_exp_horner prec (get_odd (prec + 2)) 1 1 (float_divr prec x (- Float m e))) ^ (nat (-m) * 2 ^ nat e)) ``` hoelzl@29805 ` 1472` ``` else ub_exp_horner prec (get_odd (prec + 2)) 1 1 x)" ``` hoelzl@29805 ` 1473` ```by pat_completeness auto ``` hoelzl@29805 ` 1474` ```termination by (relation "measure (\ v. let (prec, x) = sum_case id id v in (if 0 < x then 1 else 0))", auto simp add: less_float_def) ``` hoelzl@29805 ` 1475` hoelzl@29805 ` 1476` ```lemma exp_m1_ge_quarter: "(1 / 4 :: real) \ exp (- 1)" ``` hoelzl@29805 ` 1477` ```proof - ``` hoelzl@29805 ` 1478` ``` have eq4: "4 = Suc (Suc (Suc (Suc 0)))" by auto ``` hoelzl@29805 ` 1479` hoelzl@31098 ` 1480` ``` have "1 / 4 = real (Float 1 -2)" unfolding Float_num by auto ``` hoelzl@31098 ` 1481` ``` also have "\ \ real (lb_exp_horner 1 (get_even 4) 1 1 (- 1))" ``` hoelzl@31809 ` 1482` ``` unfolding get_even_def eq4 ``` hoelzl@29805 ` 1483` ``` by (auto simp add: lapprox_posrat_def rapprox_posrat_def normfloat.simps) ``` hoelzl@31098 ` 1484` ``` also have "\ \ exp (real (- 1 :: float))" using bnds_exp_horner[where x="- 1"] by auto ``` hoelzl@31809 ` 1485` ``` finally show ?thesis unfolding real_of_float_minus real_of_float_1 . ``` hoelzl@29805 ` 1486` ```qed ``` hoelzl@29805 ` 1487` hoelzl@29805 ` 1488` ```lemma lb_exp_pos: assumes "\ 0 < x" shows "0 < lb_exp prec x" ``` hoelzl@29805 ` 1489` ```proof - ``` hoelzl@29805 ` 1490` ``` let "?lb_horner x" = "lb_exp_horner prec (get_even (prec + 2)) 1 1 x" ``` hoelzl@29805 ` 1491` ``` let "?horner x" = "let y = ?lb_horner x in if y \ 0 then Float 1 -2 else y" ``` hoelzl@29805 ` 1492` ``` have pos_horner: "\ x. 0 < ?horner x" unfolding Let_def by (cases "?lb_horner x \ 0", auto simp add: le_float_def less_float_def) ``` hoelzl@29805 ` 1493` ``` moreover { fix x :: float fix num :: nat ``` hoelzl@31098 ` 1494` ``` have "0 < real (?horner x) ^ num" using `0 < ?horner x`[unfolded less_float_def real_of_float_0] by (rule zero_less_power) ``` hoelzl@31098 ` 1495` ``` also have "\ = real ((?horner x) ^ num)" using float_power by auto ``` hoelzl@31098 ` 1496` ``` finally have "0 < real ((?horner x) ^ num)" . ``` hoelzl@29805 ` 1497` ``` } ``` hoelzl@29805 ` 1498` ``` ultimately show ?thesis ``` haftmann@30968 ` 1499` ``` unfolding lb_exp.simps if_not_P[OF `\ 0 < x`] Let_def ``` haftmann@30968 ` 1500` ``` by (cases "floor_fl x", cases "x < - 1", auto simp add: float_power le_float_def less_float_def) ``` hoelzl@29805 ` 1501` ```qed ``` hoelzl@29805 ` 1502` hoelzl@29805 ` 1503` ```lemma exp_boundaries': assumes "x \ 0" ``` hoelzl@31098 ` 1504` ``` shows "exp (real x) \ { real (lb_exp prec x) .. real (ub_exp prec x)}" ``` hoelzl@29805 ` 1505` ```proof - ``` hoelzl@29805 ` 1506` ``` let "?lb_exp_horner x" = "lb_exp_horner prec (get_even (prec + 2)) 1 1 x" ``` hoelzl@29805 ` 1507` ``` let "?ub_exp_horner x" = "ub_exp_horner prec (get_odd (prec + 2)) 1 1 x" ``` hoelzl@29805 ` 1508` hoelzl@31098 ` 1509` ``` have "real x \ 0" and "\ x > 0" using `x \ 0` unfolding le_float_def less_float_def by auto ``` hoelzl@29805 ` 1510` ``` show ?thesis ``` hoelzl@29805 ` 1511` ``` proof (cases "x < - 1") ``` hoelzl@31098 ` 1512` ``` case False hence "- 1 \ real x" unfolding less_float_def by auto ``` hoelzl@29805 ` 1513` ``` show ?thesis ``` hoelzl@29805 ` 1514` ``` proof (cases "?lb_exp_horner x \ 0") ``` hoelzl@31098 ` 1515` ``` from `\ x < - 1` have "- 1 \ real x" unfolding less_float_def by auto ``` hoelzl@31098 ` 1516` ``` hence "exp (- 1) \ exp (real x)" unfolding exp_le_cancel_iff . ``` hoelzl@29805 ` 1517` ``` from order_trans[OF exp_m1_ge_quarter this] ``` hoelzl@31098 ` 1518` ``` have "real (Float 1 -2) \ exp (real x)" unfolding Float_num . ``` hoelzl@29805 ` 1519` ``` moreover case True ``` hoelzl@31098 ` 1520` ``` ultimately show ?thesis using bnds_exp_horner `real x \ 0` `\ x > 0` `\ x < - 1` by auto ``` hoelzl@29805 ` 1521` ``` next ``` hoelzl@31098 ` 1522` ``` case False thus ?thesis using bnds_exp_horner `real x \ 0` `\ x > 0` `\ x < - 1` by (auto simp add: Let_def) ``` hoelzl@29805 ` 1523` ``` qed ``` hoelzl@29805 ` 1524` ``` next ``` hoelzl@29805 ` 1525` ``` case True ``` hoelzl@31809 ` 1526` hoelzl@29805 ` 1527` ``` obtain m e where Float_floor: "floor_fl x = Float m e" by (cases "floor_fl x", auto) ``` hoelzl@29805 ` 1528` ``` let ?num = "nat (- m) * 2 ^ nat e" ``` hoelzl@31809 ` 1529` hoelzl@31098 ` 1530` ``` have "real (floor_fl x) < - 1" using floor_fl `x < - 1` unfolding le_float_def less_float_def real_of_float_minus real_of_float_1 by (rule order_le_less_trans) ``` hoelzl@31098 ` 1531` ``` hence "real (floor_fl x) < 0" unfolding Float_floor real_of_float_simp using zero_less_pow2[of xe] by auto ``` hoelzl@29805 ` 1532` ``` hence "m < 0" ``` hoelzl@31098 ` 1533` ``` unfolding less_float_def real_of_float_0 Float_floor real_of_float_simp ``` hoelzl@29805 ` 1534` ``` unfolding pos_prod_lt[OF zero_less_pow2[of e], unfolded real_mult_commute] by auto ``` hoelzl@29805 ` 1535` ``` hence "1 \ - m" by auto ``` hoelzl@29805 ` 1536` ``` hence "0 < nat (- m)" by auto ``` hoelzl@29805 ` 1537` ``` moreover ``` hoelzl@29805 ` 1538` ``` have "0 \ e" using floor_pos_exp Float_floor[symmetric] by auto ``` hoelzl@29805 ` 1539` ``` hence "(0::nat) < 2 ^ nat e" by auto ``` hoelzl@29805 ` 1540` ``` ultimately have "0 < ?num" by auto ``` hoelzl@29805 ` 1541` ``` hence "real ?num \ 0" by auto ``` hoelzl@29805 ` 1542` ``` have e_nat: "int (nat e) = e" using `0 \ e` by auto ``` hoelzl@31098 ` 1543` ``` have num_eq: "real ?num = real (- floor_fl x)" using `0 < nat (- m)` ``` hoelzl@31098 ` 1544` ``` unfolding Float_floor real_of_float_minus real_of_float_simp real_of_nat_mult pow2_int[of "nat e", unfolded e_nat] realpow_real_of_nat[symmetric] by auto ``` hoelzl@31098 ` 1545` ``` have "0 < - floor_fl x" using `0 < ?num`[unfolded real_of_nat_less_iff[symmetric]] unfolding less_float_def num_eq[symmetric] real_of_float_0 real_of_nat_zero . ``` hoelzl@31098 ` 1546` ``` hence "real (floor_fl x) < 0" unfolding less_float_def by auto ``` hoelzl@31809 ` 1547` hoelzl@31098 ` 1548` ``` have "exp (real x) \ real (ub_exp prec x)" ``` hoelzl@29805 ` 1549` ``` proof - ``` hoelzl@31809 ` 1550` ``` have div_less_zero: "real (float_divr prec x (- floor_fl x)) \ 0" ``` hoelzl@31098 ` 1551` ``` using float_divr_nonpos_pos_upper_bound[OF `x \ 0` `0 < - floor_fl x`] unfolding le_float_def real_of_float_0 . ``` hoelzl@31809 ` 1552` hoelzl@31098 ` 1553` ``` have "exp (real x) = exp (real ?num * (real x / real ?num))" using `real ?num \ 0` by auto ``` hoelzl@31098 ` 1554` ``` also have "\ = exp (real x / real ?num) ^ ?num" unfolding exp_real_of_nat_mult .. ``` hoelzl@31098 ` 1555` ``` also have "\ \ exp (real (float_divr prec x (- floor_fl x))) ^ ?num" unfolding num_eq ``` hoelzl@29805 ` 1556` ``` by (rule power_mono, rule exp_le_cancel_iff[THEN iffD2], rule float_divr) auto ``` hoelzl@31098 ` 1557` ``` also have "\ \ real ((?ub_exp_horner (float_divr prec x (- floor_fl x))) ^ ?num)" unfolding float_power ``` hoelzl@29805 ` 1558` ``` by (rule power_mono, rule bnds_exp_horner[OF div_less_zero, unfolded atLeastAtMost_iff, THEN conjunct2], auto) ``` hoelzl@29805 ` 1559` ``` finally show ?thesis unfolding ub_exp.simps if_not_P[OF `\ 0 < x`] if_P[OF `x < - 1`] float.cases Float_floor Let_def . ``` hoelzl@29805 ` 1560` ``` qed ``` hoelzl@31809 ` 1561` ``` moreover ``` hoelzl@31098 ` 1562` ``` have "real (lb_exp prec x) \ exp (real x)" ``` hoelzl@29805 ` 1563` ``` proof - ``` hoelzl@29805 ` 1564` ``` let ?divl = "float_divl prec x (- Float m e)" ``` hoelzl@29805 ` 1565` ``` let ?horner = "?lb_exp_horner ?divl" ``` hoelzl@31809 ` 1566` hoelzl@29805 ` 1567` ``` show ?thesis ``` hoelzl@29805 ` 1568` ``` proof (cases "?horner \ 0") ``` hoelzl@31098 ` 1569` ``` case False hence "0 \ real ?horner" unfolding le_float_def by auto ``` hoelzl@31809 ` 1570` hoelzl@31098 ` 1571` ``` have div_less_zero: "real (float_divl prec x (- floor_fl x)) \ 0" ``` hoelzl@31098 ` 1572` ``` using `real (floor_fl x) < 0` `real x \ 0` by (auto intro!: order_trans[OF float_divl] divide_nonpos_neg) ``` hoelzl@31809 ` 1573` hoelzl@31809 ` 1574` ``` have "real ((?lb_exp_horner (float_divl prec x (- floor_fl x))) ^ ?num) \ ``` hoelzl@31809 ` 1575` ``` exp (real (float_divl prec x (- floor_fl x))) ^ ?num" unfolding float_power ``` hoelzl@31098 ` 1576` ``` using `0 \ real ?horner`[unfolded Float_floor[symmetric]] bnds_exp_horner[OF div_less_zero, unfolded atLeastAtMost_iff, THEN conjunct1] by (auto intro!: power_mono) ``` hoelzl@31098 ` 1577` ``` also have "\ \ exp (real x / real ?num) ^ ?num" unfolding num_eq ``` hoelzl@31098 ` 1578` ``` using float_divl by (auto intro!: power_mono simp del: real_of_float_minus) ``` hoelzl@31098 ` 1579` ``` also have "\ = exp (real ?num * (real x / real ?num))" unfolding exp_real_of_nat_mult .. ``` hoelzl@31098 ` 1580` ``` also have "\ = exp (real x)" using `real ?num \ 0` by auto ``` hoelzl@29805 ` 1581` ``` finally show ?thesis ``` hoelzl@29805 ` 1582` ``` unfolding lb_exp.simps if_not_P[OF `\ 0 < x`] if_P[OF `x < - 1`] float.cases Float_floor Let_def if_not_P[OF False] by auto ``` hoelzl@29805 ` 1583` ``` next ``` hoelzl@29805 ` 1584` ``` case True ``` hoelzl@31098 ` 1585` ``` have "real (floor_fl x) \ 0" and "real (floor_fl x) \ 0" using `real (floor_fl x) < 0` by auto ``` hoelzl@31098 ` 1586` ``` from divide_right_mono_neg[OF floor_fl[of x] `real (floor_fl x) \ 0`, unfolded divide_self[OF `real (floor_fl x) \ 0`]] ``` hoelzl@31098 ` 1587` ``` have "- 1 \ real x / real (- floor_fl x)" unfolding real_of_float_minus by auto ``` hoelzl@29805 ` 1588` ``` from order_trans[OF exp_m1_ge_quarter this[unfolded exp_le_cancel_iff[where x="- 1", symmetric]]] ``` hoelzl@31098 ` 1589` ``` have "real (Float 1 -2) \ exp (real x / real (- floor_fl x))" unfolding Float_num . ``` hoelzl@31098 ` 1590` ``` hence "real (Float 1 -2) ^ ?num \ exp (real x / real (- floor_fl x)) ^ ?num" ``` hoelzl@29805 ` 1591` ``` by (auto intro!: power_mono simp add: Float_num) ``` hoelzl@31098 ` 1592` ``` also have "\ = exp (real x)" unfolding num_eq exp_real_of_nat_mult[symmetric] using `real (floor_fl x) \ 0` by auto ``` hoelzl@29805 ` 1593` ``` finally show ?thesis ``` hoelzl@29805 ` 1594` ``` unfolding lb_exp.simps if_not_P[OF `\ 0 < x`] if_P[OF `x < - 1`] float.cases Float_floor Let_def if_P[OF True] float_power . ``` hoelzl@29805 ` 1595` ``` qed ``` hoelzl@29805 ` 1596` ``` qed ``` hoelzl@29805 ` 1597` ``` ultimately show ?thesis by auto ``` hoelzl@29805 ` 1598` ``` qed ``` hoelzl@29805 ` 1599` ```qed ``` hoelzl@29805 ` 1600` hoelzl@31098 ` 1601` ```lemma exp_boundaries: "exp (real x) \ { real (lb_exp prec x) .. real (ub_exp prec x)}" ``` hoelzl@29805 ` 1602` ```proof - ``` hoelzl@29805 ` 1603` ``` show ?thesis ``` hoelzl@29805 ` 1604` ``` proof (cases "0 < x") ``` hoelzl@31809 ` 1605` ``` case False hence "x \ 0" unfolding less_float_def le_float_def by auto ``` hoelzl@29805 ` 1606` ``` from exp_boundaries'[OF this] show ?thesis . ``` hoelzl@29805 ` 1607` ``` next ``` hoelzl@29805 ` 1608` ``` case True hence "-x \ 0" unfolding less_float_def le_float_def by auto ``` hoelzl@31809 ` 1609` hoelzl@31098 ` 1610` ``` have "real (lb_exp prec x) \ exp (real x)" ``` hoelzl@29805 ` 1611` ``` proof - ``` hoelzl@29805 ` 1612` ``` from exp_boundaries'[OF `-x \ 0`] ``` hoelzl@31098 ` 1613` ``` have ub_exp: "exp (- real x) \ real (ub_exp prec (-x))" unfolding atLeastAtMost_iff real_of_float_minus by auto ``` hoelzl@31809 ` 1614` hoelzl@31098 ` 1615` ``` have "real (float_divl prec 1 (ub_exp prec (-x))) \ 1 / real (ub_exp prec (-x))" using float_divl[where x=1] by auto ``` hoelzl@31098 ` 1616` ``` also have "\ \ exp (real x)" ``` hoelzl@29805 ` 1617` ``` using ub_exp[unfolded inverse_le_iff_le[OF order_less_le_trans[OF exp_gt_zero ub_exp] exp_gt_zero, symmetric]] ``` hoelzl@29805 ` 1618` ``` unfolding exp_minus nonzero_inverse_inverse_eq[OF exp_not_eq_zero] inverse_eq_divide by auto ``` hoelzl@29805 ` 1619` ``` finally show ?thesis unfolding lb_exp.simps if_P[OF True] . ``` hoelzl@29805 ` 1620` ``` qed ``` hoelzl@29805 ` 1621` ``` moreover ``` hoelzl@31098 ` 1622` ``` have "exp (real x) \ real (ub_exp prec x)" ``` hoelzl@29805 ` 1623` ``` proof - ``` hoelzl@29805 ` 1624` ``` have "\ 0 < -x" using `0 < x` unfolding less_float_def by auto ``` hoelzl@31809 ` 1625` hoelzl@29805 ` 1626` ``` from exp_boundaries'[OF `-x \ 0`] ``` hoelzl@31098 ` 1627` ``` have lb_exp: "real (lb_exp prec (-x)) \ exp (- real x)" unfolding atLeastAtMost_iff real_of_float_minus by auto ``` hoelzl@31809 ` 1628` hoelzl@31098 ` 1629` ``` have "exp (real x) \ real (1 :: float) / real (lb_exp prec (-x))" ``` hoelzl@31809 ` 1630` ``` using lb_exp[unfolded inverse_le_iff_le[OF exp_gt_zero lb_exp_pos[OF `\ 0 < -x`, unfolded less_float_def real_of_float_0], ``` hoelzl@31098 ` 1631` ``` symmetric]] ``` hoelzl@31098 ` 1632` ``` unfolding exp_minus nonzero_inverse_inverse_eq[OF exp_not_eq_zero] inverse_eq_divide real_of_float_1 by auto ``` hoelzl@31098 ` 1633` ``` also have "\ \ real (float_divr prec 1 (lb_exp prec (-x)))" using float_divr . ``` hoelzl@29805 ` 1634` ``` finally show ?thesis unfolding ub_exp.simps if_P[OF True] . ``` hoelzl@29805 ` 1635` ``` qed ``` hoelzl@29805 ` 1636` ``` ultimately show ?thesis by auto ``` hoelzl@29805 ` 1637` ``` qed ``` hoelzl@29805 ` 1638` ```qed ``` hoelzl@29805 ` 1639` hoelzl@31098 ` 1640` ```lemma bnds_exp: "\ x lx ux. (l, u) = (lb_exp prec lx, ub_exp prec ux) \ x \ {real lx .. real ux} \ real l \ exp x \ exp x \ real u" ``` hoelzl@29805 ` 1641` ```proof (rule allI, rule allI, rule allI, rule impI) ``` hoelzl@29805 ` 1642` ``` fix x lx ux ``` hoelzl@31098 ` 1643` ``` assume "(l, u) = (lb_exp prec lx, ub_exp prec ux) \ x \ {real lx .. real ux}" ``` hoelzl@31098 ` 1644` ``` hence l: "lb_exp prec lx = l " and u: "ub_exp prec ux = u" and x: "x \ {real lx .. real ux}" by auto ``` hoelzl@29805 ` 1645` hoelzl@29805 ` 1646` ``` { from exp_boundaries[of lx prec, unfolded l] ``` hoelzl@31098 ` 1647` ``` have "real l \ exp (real lx)" by (auto simp del: lb_exp.simps) ``` hoelzl@29805 ` 1648` ``` also have "\ \ exp x" using x by auto ``` hoelzl@31098 ` 1649` ``` finally have "real l \ exp x" . ``` hoelzl@29805 ` 1650` ``` } moreover ``` hoelzl@31098 ` 1651` ``` { have "exp x \ exp (real ux)" using x by auto ``` hoelzl@31098 ` 1652` ``` also have "\ \ real u" using exp_boundaries[of ux prec, unfolded u] by (auto simp del: ub_exp.simps) ``` hoelzl@31098 ` 1653` ``` finally have "exp x \ real u" . ``` hoelzl@31098 ` 1654` ``` } ultimately show "real l \ exp x \ exp x \ real u" .. ``` hoelzl@29805 ` 1655` ```qed ``` hoelzl@29805 ` 1656` hoelzl@29805 ` 1657` ```section "Logarithm" ``` hoelzl@29805 ` 1658` hoelzl@29805 ` 1659` ```subsection "Compute the logarithm series" ``` hoelzl@29805 ` 1660` hoelzl@31809 ` 1661` ```fun ub_ln_horner :: "nat \ nat \ nat \ float \ float" ``` hoelzl@29805 ` 1662` ```and lb_ln_horner :: "nat \ nat \ nat \ float \ float" where ``` hoelzl@29805 ` 1663` ```"ub_ln_horner prec 0 i x = 0" | ``` hoelzl@29805 ` 1664` ```"ub_ln_horner prec (Suc n) i x = rapprox_rat prec 1 (int i) - x * lb_ln_horner prec n (Suc i) x" | ``` hoelzl@29805 ` 1665` ```"lb_ln_horner prec 0 i x = 0" | ``` hoelzl@29805 ` 1666` ```"lb_ln_horner prec (Suc n) i x = lapprox_rat prec 1 (int i) - x * ub_ln_horner prec n (Suc i) x" ``` hoelzl@29805 ` 1667` hoelzl@29805 ` 1668` ```lemma ln_bounds: ``` hoelzl@29805 ` 1669` ``` assumes "0 \ x" and "x < 1" ``` haftmann@30952 ` 1670` ``` shows "(\i=0..<2*n. -1^i * (1 / real (i + 1)) * x ^ (Suc i)) \ ln (x + 1)" (is "?lb") ``` haftmann@30952 ` 1671` ``` and "ln (x + 1) \ (\i=0..<2*n + 1. -1^i * (1 / real (i + 1)) * x ^ (Suc i))" (is "?ub") ``` hoelzl@29805 ` 1672` ```proof - ``` haftmann@30952 ` 1673` ``` let "?a n" = "(1/real (n +1)) * x ^ (Suc n)" ``` hoelzl@29805 ` 1674` hoelzl@29805 ` 1675` ``` have ln_eq: "(\ i. -1^i * ?a i) = ln (x + 1)" ``` hoelzl@29805 ` 1676` ``` using ln_series[of "x + 1"] `0 \ x` `x < 1` by auto ``` hoelzl@29805 ` 1677` hoelzl@29805 ` 1678` ``` have "norm x < 1" using assms by auto ``` hoelzl@31809 ` 1679` ``` have "?a ----> 0" unfolding Suc_eq_plus1[symmetric] inverse_eq_divide[symmetric] ``` hoelzl@29805 ` 1680` ``` using LIMSEQ_mult[OF LIMSEQ_inverse_real_of_nat LIMSEQ_Suc[OF LIMSEQ_power_zero[OF `norm x < 1`]]] by auto ``` hoelzl@29805 ` 1681` ``` { fix n have "0 \ ?a n" by (rule mult_nonneg_nonneg, auto intro!: mult_nonneg_nonneg simp add: `0 \ x`) } ``` hoelzl@29805 ` 1682` ``` { fix n have "?a (Suc n) \ ?a n" unfolding inverse_eq_divide[symmetric] ``` hoelzl@29805 ` 1683` ``` proof (rule mult_mono) ``` hoelzl@29805 ` 1684` ``` show "0 \ x ^ Suc (Suc n)" by (auto intro!: mult_nonneg_nonneg simp add: `0 \ x`) ``` hoelzl@31809 ` 1685` ``` have "x ^ Suc (Suc n) \ x ^ Suc n * 1" unfolding power_Suc2 real_mult_assoc[symmetric] ``` hoelzl@29805 ` 1686` ``` by (rule mult_left_mono, fact less_imp_le[OF `x < 1`], auto intro!: mult_nonneg_nonneg simp add: `0 \ x`) ``` hoelzl@29805 ` 1687` ``` thus "x ^ Suc (Suc n) \ x ^ Suc n" by auto ``` hoelzl@29805 ` 1688` ``` qed auto } ``` hoelzl@29805 ` 1689` ``` from summable_Leibniz'(2,4)[OF `?a ----> 0` `\n. 0 \ ?a n`, OF `\n. ?a (Suc n) \ ?a n`, unfolded ln_eq] ``` hoelzl@29805 ` 1690` ``` show "?lb" and "?ub" by auto ``` hoelzl@29805 ` 1691` ```qed ``` hoelzl@29805 ` 1692` hoelzl@31809 ` 1693` ```lemma ln_float_bounds: ``` hoelzl@31098 ` 1694` ``` assumes "0 \ real x" and "real x < 1" ``` hoelzl@31098 ` 1695` ``` shows "real (x * lb_ln_horner prec (get_even n) 1 x) \ ln (real x + 1)" (is "?lb \ ?ln") ``` hoelzl@31098 ` 1696` ``` and "ln (real x + 1) \ real (x * ub_ln_horner prec (get_odd n) 1 x)" (is "?ln \ ?ub") ``` hoelzl@29805 ` 1697` ```proof - ``` hoelzl@29805 ` 1698` ``` obtain ev where ev: "get_even n = 2 * ev" using get_even_double .. ``` hoelzl@29805 ` 1699` ``` obtain od where od: "get_odd n = 2 * od + 1" using get_odd_double .. ``` hoelzl@29805 ` 1700` hoelzl@31098 ` 1701` ``` let "?s n" = "-1^n * (1 / real (1 + n)) * (real x)^(Suc n)" ``` hoelzl@29805 ` 1702` hoelzl@31098 ` 1703` ``` have "?lb \ setsum ?s {0 ..< 2 * ev}" unfolding power_Suc2 real_mult_assoc[symmetric] real_of_float_mult setsum_left_distrib[symmetric] unfolding real_mult_commute[of "real x"] ev ``` hoelzl@29805 ` 1704` ``` using horner_bounds(1)[where G="\ i k. Suc k" and F="\x. x" and f="\x. x" and lb="\n i k x. lb_ln_horner prec n k x" and ub="\n i k x. ub_ln_horner prec n k x" and j'=1 and n="2*ev", ``` hoelzl@31098 ` 1705` ``` OF `0 \ real x` refl lb_ln_horner.simps ub_ln_horner.simps] `0 \ real x` ``` hoelzl@29805 ` 1706` ``` by (rule mult_right_mono) ``` hoelzl@31098 ` 1707` ``` also have "\ \ ?ln" using ln_bounds(1)[OF `0 \ real x` `real x < 1`] by auto ``` hoelzl@31809 ` 1708` ``` finally show "?lb \ ?ln" . ``` hoelzl@29805 ` 1709` hoelzl@31098 ` 1710` ``` have "?ln \ setsum ?s {0 ..< 2 * od + 1}" using ln_bounds(2)[OF `0 \ real x` `real x < 1`] by auto ``` hoelzl@31098 ` 1711` ``` also have "\ \ ?ub" unfolding power_Suc2 real_mult_assoc[symmetric] real_of_float_mult setsum_left_distrib[symmetric] unfolding real_mult_commute[of "real x"] od ``` hoelzl@29805 ` 1712` ``` using horner_bounds(2)[where G="\ i k. Suc k" and F="\x. x" and f="\x. x" and lb="\n i k x. lb_ln_horner prec n k x" and ub="\n i k x. ub_ln_horner prec n k x" and j'=1 and n="2*od+1", ``` hoelzl@31098 ` 1713` ``` OF `0 \ real x` refl lb_ln_horner.simps ub_ln_horner.simps] `0 \ real x` ``` hoelzl@29805 ` 1714` ``` by (rule mult_right_mono) ``` hoelzl@31809 ` 1715` ``` finally show "?ln \ ?ub" . ``` hoelzl@29805 ` 1716` ```qed ``` hoelzl@29805 ` 1717` hoelzl@29805 ` 1718` ```lemma ln_add: assumes "0 < x" and "0 < y" shows "ln (x + y) = ln x + ln (1 + y / x)" ``` hoelzl@29805 ` 1719` ```proof - ``` hoelzl@29805 ` 1720` ``` have "x \ 0" using assms by auto ``` hoelzl@29805 ` 1721` ``` have "x + y = x * (1 + y / x)" unfolding right_distrib times_divide_eq_right nonzero_mult_divide_cancel_left[OF `x \ 0`] by auto ``` hoelzl@31809 ` 1722` ``` moreover ``` hoelzl@29805 ` 1723` ``` have "0 < y / x" using assms divide_pos_pos by auto ``` hoelzl@29805 ` 1724` ``` hence "0 < 1 + y / x" by auto ``` hoelzl@29805 ` 1725` ``` ultimately show ?thesis using ln_mult assms by auto ``` hoelzl@29805 ` 1726` ```qed ``` hoelzl@29805 ` 1727` hoelzl@29805 ` 1728` ```subsection "Compute the logarithm of 2" ``` hoelzl@29805 ` 1729` hoelzl@31809 ` 1730` ```definition ub_ln2 where "ub_ln2 prec = (let third = rapprox_rat (max prec 1) 1 3 ``` hoelzl@31809 ` 1731` ``` in (Float 1 -1 * ub_ln_horner prec (get_odd prec) 1 (Float 1 -1)) + ``` hoelzl@29805 ` 1732` ``` (third * ub_ln_horner prec (get_odd prec) 1 third))" ``` hoelzl@31809 ` 1733` ```definition lb_ln2 where "lb_ln2 prec = (let third = lapprox_rat prec 1 3 ``` hoelzl@31809 ` 1734` ``` in (Float 1 -1 * lb_ln_horner prec (get_even prec) 1 (Float 1 -1)) + ``` hoelzl@29805 ` 1735` ``` (third * lb_ln_horner prec (get_even prec) 1 third))" ``` hoelzl@29805 ` 1736` hoelzl@31098 ` 1737` ```lemma ub_ln2: "ln 2 \ real (ub_ln2 prec)" (is "?ub_ln2") ``` hoelzl@31098 ` 1738` ``` and lb_ln2: "real (lb_ln2 prec) \ ln 2" (is "?lb_ln2") ``` hoelzl@29805 ` 1739` ```proof - ``` hoelzl@29805 ` 1740` ``` let ?uthird = "rapprox_rat (max prec 1) 1 3" ``` hoelzl@29805 ` 1741` ``` let ?lthird = "lapprox_rat prec 1 3" ``` hoelzl@29805 ` 1742` hoelzl@29805 ` 1743` ``` have ln2_sum: "ln 2 = ln (1/2 + 1) + ln (1 / 3 + 1)" ``` hoelzl@29805 ` 1744` ``` using ln_add[of "3 / 2" "1 / 2"] by auto ``` hoelzl@31098 ` 1745` ``` have lb3: "real ?lthird \ 1 / 3" using lapprox_rat[of prec 1 3] by auto ``` hoelzl@31098 ` 1746` ``` hence lb3_ub: "real ?lthird < 1" by auto ``` hoelzl@31098 ` 1747` ``` have lb3_lb: "0 \ real ?lthird" using lapprox_rat_bottom[of 1 3] by auto ``` hoelzl@31098 ` 1748` ``` have ub3: "1 / 3 \ real ?uthird" using rapprox_rat[of 1 3] by auto ``` hoelzl@31098 ` 1749` ``` hence ub3_lb: "0 \ real ?uthird" by auto ``` hoelzl@29805 ` 1750` hoelzl@31098 ` 1751` ``` have lb2: "0 \ real (Float 1 -1)" and ub2: "real (Float 1 -1) < 1" unfolding Float_num by auto ``` hoelzl@29805 ` 1752` hoelzl@29805 ` 1753` ``` have "0 \ (1::int)" and "0 < (3::int)" by auto ``` hoelzl@31098 ` 1754` ``` have ub3_ub: "real ?uthird < 1" unfolding rapprox_rat.simps(2)[OF `0 \ 1` `0 < 3`] ``` hoelzl@29805 ` 1755` ``` by (rule rapprox_posrat_less1, auto) ``` hoelzl@29805 ` 1756` hoelzl@29805 ` 1757` ``` have third_gt0: "(0 :: real) < 1 / 3 + 1" by auto ``` hoelzl@31098 ` 1758` ``` have uthird_gt0: "0 < real ?uthird + 1" using ub3_lb by auto ``` hoelzl@31098 ` 1759` ``` have lthird_gt0: "0 < real ?lthird + 1" using lb3_lb by auto ``` hoelzl@29805 ` 1760` hoelzl@31098 ` 1761` ``` show ?ub_ln2 unfolding ub_ln2_def Let_def real_of_float_add ln2_sum Float_num(4)[symmetric] ``` hoelzl@29805 ` 1762` ``` proof (rule add_mono, fact ln_float_bounds(2)[OF lb2 ub2]) ``` hoelzl@31098 ` 1763` ``` have "ln (1 / 3 + 1) \ ln (real ?uthird + 1)" unfolding ln_le_cancel_iff[OF third_gt0 uthird_gt0] using ub3 by auto ``` hoelzl@31098 ` 1764` ``` also have "\ \ real (?uthird * ub_ln_horner prec (get_odd prec) 1 ?uthird)" ``` hoelzl@29805 ` 1765` ``` using ln_float_bounds(2)[OF ub3_lb ub3_ub] . ``` hoelzl@31098 ` 1766` ``` finally show "ln (1 / 3 + 1) \ real (?uthird * ub_ln_horner prec (get_odd prec) 1 ?uthird)" . ``` hoelzl@29805 ` 1767` ``` qed ``` hoelzl@31098 ` 1768` ``` show ?lb_ln2 unfolding lb_ln2_def Let_def real_of_float_add ln2_sum Float_num(4)[symmetric] ``` hoelzl@29805 ` 1769` ``` proof (rule add_mono, fact ln_float_bounds(1)[OF lb2 ub2]) ``` hoelzl@31098 ` 1770` ``` have "real (?lthird * lb_ln_horner prec (get_even prec) 1 ?lthird) \ ln (real ?lthird + 1)" ``` hoelzl@29805 ` 1771` ``` using ln_float_bounds(1)[OF lb3_lb lb3_ub] . ``` hoelzl@29805 ` 1772` ``` also have "\ \ ln (1 / 3 + 1)" unfolding ln_le_cancel_iff[OF lthird_gt0 third_gt0] using lb3 by auto ``` hoelzl@31098 ` 1773` ``` finally show "real (?lthird * lb_ln_horner prec (get_even prec) 1 ?lthird) \ ln (1 / 3 + 1)" . ``` hoelzl@29805 ` 1774` ``` qed ``` hoelzl@29805 ` 1775` ```qed ``` hoelzl@29805 ` 1776` hoelzl@29805 ` 1777` ```subsection "Compute the logarithm in the entire domain" ``` hoelzl@29805 ` 1778` hoelzl@29805 ` 1779` ```function ub_ln :: "nat \ float \ float option" and lb_ln :: "nat \ float \ float option" where ``` hoelzl@31468 ` 1780` ```"ub_ln prec x = (if x \ 0 then None ``` hoelzl@31468 ` 1781` ``` else if x < 1 then Some (- the (lb_ln prec (float_divl (max prec 1) 1 x))) ``` hoelzl@31468 ` 1782` ``` else let horner = \x. x * ub_ln_horner prec (get_odd prec) 1 x in ``` hoelzl@31468 ` 1783` ``` if x \ Float 3 -1 then Some (horner (x - 1)) ``` hoelzl@31468 ` 1784` ``` else if x < Float 1 1 then Some (horner (Float 1 -1) + horner (x * rapprox_rat prec 2 3 - 1)) ``` hoelzl@31468 ` 1785` ``` else let l = bitlen (mantissa x) - 1 in ``` hoelzl@31468 ` 1786` ``` Some (ub_ln2 prec * (Float (scale x + l) 0) + horner (Float (mantissa x) (- l) - 1)))" | ``` hoelzl@31468 ` 1787` ```"lb_ln prec x = (if x \ 0 then None ``` hoelzl@31468 ` 1788` ``` else if x < 1 then Some (- the (ub_ln prec (float_divr prec 1 x))) ``` hoelzl@31468 ` 1789` ``` else let horner = \x. x * lb_ln_horner prec (get_even prec) 1 x in ``` hoelzl@31468 ` 1790` ``` if x \ Float 3 -1 then Some (horner (x - 1)) ``` hoelzl@31468 ` 1791` ``` else if x < Float 1 1 then Some (horner (Float 1 -1) + ``` hoelzl@31468 ` 1792` ``` horner (max (x * lapprox_rat prec 2 3 - 1) 0)) ``` hoelzl@31468 ` 1793` ``` else let l = bitlen (mantissa x) - 1 in ``` hoelzl@31468 ` 1794` ``` Some (lb_ln2 prec * (Float (scale x + l) 0) + horner (Float (mantissa x) (- l) - 1)))" ``` hoelzl@29805 ` 1795` ```by pat_completeness auto ``` hoelzl@29805 ` 1796` hoelzl@29805 ` 1797` ```termination proof (relation "measure (\ v. let (prec, x) = sum_case id id v in (if x < 1 then 1 else 0))", auto) ``` hoelzl@29805 ` 1798` ``` fix prec x assume "\ x \ 0" and "x < 1" and "float_divl (max prec (Suc 0)) 1 x < 1" ``` hoelzl@29805 ` 1799` ``` hence "0 < x" and "0 < max prec (Suc 0)" unfolding less_float_def le_float_def by auto ``` hoelzl@29805 ` 1800` ``` from float_divl_pos_less1_bound[OF `0 < x` `x < 1` `0 < max prec (Suc 0)`] ``` hoelzl@29805 ` 1801` ``` show False using `float_divl (max prec (Suc 0)) 1 x < 1` unfolding less_float_def le_float_def by auto ``` hoelzl@29805 ` 1802` ```next ``` hoelzl@29805 ` 1803` ``` fix prec x assume "\ x \ 0" and "x < 1" and "float_divr prec 1 x < 1" ``` hoelzl@29805 ` 1804` ``` hence "0 < x" unfolding less_float_def le_float_def by auto ``` hoelzl@29805 ` 1805` ``` from float_divr_pos_less1_lower_bound[OF `0 < x` `x < 1`, of prec] ``` hoelzl@29805 ` 1806` ``` show False using `float_divr prec 1 x < 1` unfolding less_float_def le_float_def by auto ``` hoelzl@29805 ` 1807` ```qed ``` hoelzl@29805 ` 1808` hoelzl@31098 ` 1809` ```lemma ln_shifted_float: assumes "0 < m" shows "ln (real (Float m e)) = ln 2 * real (e + (bitlen m - 1)) + ln (real (Float m (- (bitlen m - 1))))" ``` hoelzl@29805 ` 1810` ```proof - ``` hoelzl@29805 ` 1811` ``` let ?B = "2^nat (bitlen m - 1)" ``` hoelzl@29805 ` 1812` ``` have "0 < real m" and "\X. (0 :: real) < 2^X" and "0 < (2 :: real)" and "m \ 0" using assms by auto ``` hoelzl@29805 ` 1813` ``` hence "0 \ bitlen m - 1" using bitlen_ge1[OF `m \ 0`] by auto ``` hoelzl@31468 ` 1814` ``` show ?thesis ``` hoelzl@29805 ` 1815` ``` proof (cases "0 \ e") ``` hoelzl@29805 ` 1816` ``` case True ``` hoelzl@29805 ` 1817` ``` show ?thesis unfolding normalized_float[OF `m \ 0`] ``` hoelzl@31468 ` 1818` ``` unfolding ln_div[OF `0 < real m` `0 < ?B`] real_of_int_add ln_realpow[OF `0 < 2`] ``` hoelzl@31468 ` 1819` ``` unfolding real_of_float_ge0_exp[OF True] ln_mult[OF `0 < real m` `0 < 2^nat e`] ``` hoelzl@29805 ` 1820` ``` ln_realpow[OF `0 < 2`] algebra_simps using `0 \ bitlen m - 1` True by auto ``` hoelzl@29805 ` 1821` ``` next ``` hoelzl@29805 ` 1822` ``` case False hence "0 < -e" by auto ``` hoelzl@29805 ` 1823` ``` hence pow_gt0: "(0::real) < 2^nat (-e)" by auto ``` hoelzl@29805 ` 1824` ``` hence inv_gt0: "(0::real) < inverse (2^nat (-e))" by auto ``` hoelzl@29805 ` 1825` ``` show ?thesis unfolding normalized_float[OF `m \ 0`] ``` hoelzl@31468 ` 1826` ``` unfolding ln_div[OF `0 < real m` `0 < ?B`] real_of_int_add ln_realpow[OF `0 < 2`] ``` hoelzl@31098 ` 1827` ``` unfolding real_of_float_nge0_exp[OF False] ln_mult[OF `0 < real m` inv_gt0] ln_inverse[OF pow_gt0] ``` hoelzl@29805 ` 1828` ``` ln_realpow[OF `0 < 2`] algebra_simps using `0 \ bitlen m - 1` False by auto ``` hoelzl@29805 ` 1829` ``` qed ``` hoelzl@29805 ` 1830` ```qed ``` hoelzl@29805 ` 1831` hoelzl@29805 ` 1832` ```lemma ub_ln_lb_ln_bounds': assumes "1 \ x" ``` hoelzl@31098 ` 1833` ``` shows "real (the (lb_ln prec x)) \ ln (real x) \ ln (real x) \ real (the (ub_ln prec x))" ``` hoelzl@29805 ` 1834` ``` (is "?lb \ ?ln \ ?ln \ ?ub") ``` hoelzl@29805 ` 1835` ```proof (cases "x < Float 1 1") ``` hoelzl@31468 ` 1836` ``` case True ``` hoelzl@31468 ` 1837` ``` hence "real (x - 1) < 1" and "real x < 2" unfolding less_float_def Float_num by auto ``` hoelzl@29805 ` 1838` ``` have "\ x \ 0" and "\ x < 1" using `1 \ x` unfolding less_float_def le_float_def by auto ``` hoelzl@31098 ` 1839` ``` hence "0 \ real (x - 1)" using `1 \ x` unfolding less_float_def Float_num by auto ``` hoelzl@31468 ` 1840` hoelzl@31468 ` 1841` ``` have [simp]: "real (Float 3 -1) = 3 / 2" by (simp add: real_of_float_def pow2_def) ``` hoelzl@31468 ` 1842` hoelzl@31468 ` 1843` ``` show ?thesis ``` hoelzl@31468 ` 1844` ``` proof (cases "x \ Float 3 -1") ``` hoelzl@31468 ` 1845` ``` case True ``` hoelzl@31468 ` 1846` ``` show ?thesis unfolding lb_ln.simps unfolding ub_ln.simps Let_def ``` hoelzl@31468 ` 1847` ``` using ln_float_bounds[OF `0 \ real (x - 1)` `real (x - 1) < 1`, of prec] `\ x \ 0` `\ x < 1` True ``` hoelzl@31468 ` 1848` ``` by auto ``` hoelzl@31468 ` 1849` ``` next ``` hoelzl@31468 ` 1850` ``` case False hence *: "3 / 2 < real x" by (auto simp add: le_float_def) ``` hoelzl@31468 ` 1851` hoelzl@31468 ` 1852` ``` with ln_add[of "3 / 2" "real x - 3 / 2"] ``` hoelzl@31468 ` 1853` ``` have add: "ln (real x) = ln (3 / 2) + ln (real x * 2 / 3)" ``` hoelzl@31468 ` 1854` ``` by (auto simp add: algebra_simps diff_divide_distrib) ``` hoelzl@31468 ` 1855` hoelzl@31468 ` 1856` ``` let "?ub_horner x" = "x * ub_ln_horner prec (get_odd prec) 1 x" ``` hoelzl@31468 ` 1857` ``` let "?lb_horner x" = "x * lb_ln_horner prec (get_even prec) 1 x" ``` hoelzl@31468 ` 1858` hoelzl@31468 ` 1859` ``` { have up: "real (rapprox_rat prec 2 3) \ 1" ``` hoelzl@31468 ` 1860` ``` by (rule rapprox_rat_le1) simp_all ``` hoelzl@31468 ` 1861` ``` have low: "2 / 3 \ real (rapprox_rat prec 2 3)" ``` hoelzl@31468 ` 1862` ``` by (rule order_trans[OF _ rapprox_rat]) simp ``` hoelzl@31468 ` 1863` ``` from mult_less_le_imp_less[OF * low] * ``` hoelzl@31468 ` 1864` ``` have pos: "0 < real (x * rapprox_rat prec 2 3 - 1)" by auto ``` hoelzl@31468 ` 1865` hoelzl@31468 ` 1866` ``` have "ln (real x * 2/3) ``` hoelzl@31468 ` 1867` ``` \ ln (real (x * rapprox_rat prec 2 3 - 1) + 1)" ``` hoelzl@31468 ` 1868` ``` proof (rule ln_le_cancel_iff[symmetric, THEN iffD1]) ``` hoelzl@31468 ` 1869` ``` show "real x * 2 / 3 \ real (x * rapprox_rat prec 2 3 - 1) + 1" ``` hoelzl@31468 ` 1870` ``` using * low by auto ``` hoelzl@31468 ` 1871` ``` show "0 < real x * 2 / 3" using * by simp ``` hoelzl@31468 ` 1872` ``` show "0 < real (x * rapprox_rat prec 2 3 - 1) + 1" using pos by auto ``` hoelzl@31468 ` 1873` ``` qed ``` hoelzl@31468 ` 1874` ``` also have "\ \ real (?ub_horner (x * rapprox_rat prec 2 3 - 1))" ``` hoelzl@31468 ` 1875` ``` proof (rule ln_float_bounds(2)) ``` hoelzl@31468 ` 1876` ``` from mult_less_le_imp_less[OF `real x < 2` up] low * ``` hoelzl@31468 ` 1877` ``` show "real (x * rapprox_rat prec 2 3 - 1) < 1" by auto ``` hoelzl@31468 ` 1878` ``` show "0 \ real (x * rapprox_rat prec 2 3 - 1)" using pos by auto ``` hoelzl@31468 ` 1879` ``` qed ``` hoelzl@31468 ` 1880` ``` finally have "ln (real x) ``` hoelzl@31468 ` 1881` ``` \ real (?ub_horner (Float 1 -1)) ``` hoelzl@31468 ` 1882` ``` + real (?ub_horner (x * rapprox_rat prec 2 3 - 1))" ``` hoelzl@31468 ` 1883` ``` using ln_float_bounds(2)[of "Float 1 -1" prec prec] add by auto } ``` hoelzl@31468 ` 1884` ``` moreover ``` hoelzl@31468 ` 1885` ``` { let ?max = "max (x * lapprox_rat prec 2 3 - 1) 0" ``` hoelzl@31468 ` 1886` hoelzl@31468 ` 1887` ``` have up: "real (lapprox_rat prec 2 3) \ 2/3" ``` hoelzl@31468 ` 1888` ``` by (rule order_trans[OF lapprox_rat], simp) ``` hoelzl@31468 ` 1889` hoelzl@31468 ` 1890` ``` have low: "0 \ real (lapprox_rat prec 2 3)" ``` hoelzl@31468 ` 1891` ``` using lapprox_rat_bottom[of 2 3 prec] by simp ``` hoelzl@31468 ` 1892` hoelzl@31468 ` 1893` ``` have "real (?lb_horner ?max) ``` hoelzl@31468 ` 1894` ``` \ ln (real ?max + 1)" ``` hoelzl@31468 ` 1895` ``` proof (rule ln_float_bounds(1)) ``` hoelzl@31468 ` 1896` ``` from mult_less_le_imp_less[OF `real x < 2` up] * low ``` hoelzl@31468 ` 1897` ``` show "real ?max < 1" by (cases "real (lapprox_rat prec 2 3) = 0", ``` hoelzl@31468 ` 1898` ``` auto simp add: real_of_float_max) ``` hoelzl@31468 ` 1899` ``` show "0 \ real ?max" by (auto simp add: real_of_float_max) ``` hoelzl@31468 ` 1900` ``` qed ``` hoelzl@31468 ` 1901` ``` also have "\ \ ln (real x * 2/3)" ``` hoelzl@31468 ` 1902` ``` proof (rule ln_le_cancel_iff[symmetric, THEN iffD1]) ``` hoelzl@31468 ` 1903` ``` show "0 < real ?max + 1" by (auto simp add: real_of_float_max) ``` hoelzl@31468 ` 1904` ``` show "0 < real x * 2/3" using * by auto ``` hoelzl@31468 ` 1905` ``` show "real ?max + 1 \ real x * 2/3" using * up ``` hoelzl@31468 ` 1906` ``` by (cases "0 < real x * real (lapprox_posrat prec 2 3) - 1", ``` haftmann@32642 ` 1907` ``` auto simp add: real_of_float_max min_max.sup_absorb1) ``` hoelzl@31468 ` 1908` ``` qed ``` hoelzl@31468 ` 1909` ``` finally have "real (?lb_horner (Float 1 -1)) + real (?lb_horner ?max) ``` hoelzl@31468 ` 1910` ``` \ ln (real x)" ``` hoelzl@31468 ` 1911` ``` using ln_float_bounds(1)[of "Float 1 -1" prec prec] add by auto } ``` hoelzl@31468 ` 1912` ``` ultimately ``` hoelzl@31468 ` 1913` ``` show ?thesis unfolding lb_ln.simps unfolding ub_ln.simps Let_def ``` hoelzl@31468 ` 1914` ``` using `\ x \ 0` `\ x < 1` True False by auto ``` hoelzl@31468 ` 1915` ``` qed ``` hoelzl@29805 ` 1916` ```next ``` hoelzl@29805 ` 1917` ``` case False ``` hoelzl@31468 ` 1918` ``` hence "\ x \ 0" and "\ x < 1" "0 < x" "\ x \ Float 3 -1" ``` hoelzl@31468 ` 1919` ``` using `1 \ x` unfolding less_float_def le_float_def real_of_float_simp pow2_def ``` hoelzl@31468 ` 1920` ``` by auto ``` hoelzl@29805 ` 1921` ``` show ?thesis ``` hoelzl@29805 ` 1922` ``` proof (cases x) ``` hoelzl@29805 ` 1923` ``` case (Float m e) ``` hoelzl@29805 ` 1924` ``` let ?s = "Float (e + (bitlen m - 1)) 0" ``` hoelzl@29805 ` 1925` ``` let ?x = "Float m (- (bitlen m - 1))" ``` hoelzl@29805 ` 1926` hoelzl@29805 ` 1927` ``` have "0 < m" and "m \ 0" using float_pos_m_pos `0 < x` Float by auto ``` hoelzl@29805 ` 1928` hoelzl@29805 ` 1929` ``` { ``` hoelzl@31098 ` 1930` ``` have "real (lb_ln2 prec * ?s) \ ln 2 * real (e + (bitlen m - 1))" (is "?lb2 \ _") ``` hoelzl@31098 ` 1931` ``` unfolding real_of_float_mult real_of_float_ge0_exp[OF order_refl] nat_0 power_0 mult_1_right ``` hoelzl@29805 ` 1932` ``` using lb_ln2[of prec] ``` hoelzl@29805 ` 1933` ``` proof (rule mult_right_mono) ``` hoelzl@29805 ` 1934` ``` have "1 \ Float m e" using `1 \ x` Float unfolding le_float_def by auto ``` hoelzl@29805 ` 1935` ``` from float_gt1_scale[OF this] ``` hoelzl@29805 ` 1936` ``` show "0 \ real (e + (bitlen m - 1))" by auto ``` hoelzl@29805 ` 1937` ``` qed ``` hoelzl@29805 ` 1938` ``` moreover ``` hoelzl@29805 ` 1939` ``` from bitlen_div[OF `0 < m`, unfolded normalized_float[OF `m \ 0`, symmetric]] ``` hoelzl@31098 ` 1940` ``` have "0 \ real (?x - 1)" and "real (?x - 1) < 1" by auto ``` hoelzl@29805 ` 1941` ``` from ln_float_bounds(1)[OF this] ``` hoelzl@31098 ` 1942` ``` have "real ((?x - 1) * lb_ln_horner prec (get_even prec) 1 (?x - 1)) \ ln (real ?x)" (is "?lb_horner \ _") by auto ``` hoelzl@31098 ` 1943` ``` ultimately have "?lb2 + ?lb_horner \ ln (real x)" ``` hoelzl@29805 ` 1944` ``` unfolding Float ln_shifted_float[OF `0 < m`, of e] by auto ``` hoelzl@31468 ` 1945` ``` } ``` hoelzl@29805 ` 1946` ``` moreover ``` hoelzl@29805 ` 1947` ``` { ``` hoelzl@29805 ` 1948` ``` from bitlen_div[OF `0 < m`, unfolded normalized_float[OF `m \ 0`, symmetric]] ``` hoelzl@31098 ` 1949` ``` have "0 \ real (?x - 1)" and "real (?x - 1) < 1" by auto ``` hoelzl@29805 ` 1950` ``` from ln_float_bounds(2)[OF this] ``` hoelzl@31098 ` 1951` ``` have "ln (real ?x) \ real ((?x - 1) * ub_ln_horner prec (get_odd prec) 1 (?x - 1))" (is "_ \ ?ub_horner") by auto ``` hoelzl@29805 ` 1952` ``` moreover ``` hoelzl@31098 ` 1953` ``` have "ln 2 * real (e + (bitlen m - 1)) \ real (ub_ln2 prec * ?s)" (is "_ \ ?ub2") ``` hoelzl@31098 ` 1954` ``` unfolding real_of_float_mult real_of_float_ge0_exp[OF order_refl] nat_0 power_0 mult_1_right ``` hoelzl@31468 ` 1955` ``` using ub_ln2[of prec] ``` hoelzl@29805 ` 1956` ``` proof (rule mult_right_mono) ``` hoelzl@29805 ` 1957` ``` have "1 \ Float m e" using `1 \ x` Float unfolding le_float_def by auto ``` hoelzl@29805 ` 1958` ``` from float_gt1_scale[OF this] ``` hoelzl@29805 ` 1959` ``` show "0 \ real (e + (bitlen m - 1))" by auto ``` hoelzl@29805 ` 1960` ``` qed ``` hoelzl@31098 ` 1961` ``` ultimately have "ln (real x) \ ?ub2 + ?ub_horner" ``` hoelzl@29805 ` 1962` ``` unfolding Float ln_shifted_float[OF `0 < m`, of e] by auto ``` hoelzl@29805 ` 1963` ``` } ``` hoelzl@29805 ` 1964` ``` ultimately show ?thesis unfolding lb_ln.simps unfolding ub_ln.simps ``` hoelzl@31468 ` 1965` ``` unfolding if_not_P[OF `\ x \ 0`] if_not_P[OF `\ x < 1`] if_not_P[OF False] if_not_P[OF `\ x \ Float 3 -1`] Let_def ``` hoelzl@31468 ` 1966` ``` unfolding scale.simps[of m e, unfolded Float[symmetric]] mantissa.simps[of m e, unfolded Float[symmetric]] real_of_float_add ``` hoelzl@31468 ` 1967` ``` by auto ``` hoelzl@29805 ` 1968` ``` qed ``` hoelzl@29805 ` 1969` ```qed ``` hoelzl@29805 ` 1970` hoelzl@29805 ` 1971` ```lemma ub_ln_lb_ln_bounds: assumes "0 < x" ``` hoelzl@31098 ` 1972` ``` shows "real (the (lb_ln prec x)) \ ln (real x) \ ln (real x) \ real (the (ub_ln prec x))" ``` hoelzl@29805 ` 1973` ``` (is "?lb \ ?ln \ ?ln \ ?ub") ``` hoelzl@29805 ` 1974` ```proof (cases "x < 1") ``` hoelzl@29805 ` 1975` ``` case False hence "1 \ x" unfolding less_float_def le_float_def by auto ``` hoelzl@29805 ` 1976` ``` show ?thesis using ub_ln_lb_ln_bounds'[OF `1 \ x`] . ``` hoelzl@29805 ` 1977` ```next ``` hoelzl@29805 ` 1978` ``` case True have "\ x \ 0" using `0 < x` unfolding less_float_def le_float_def by auto ``` hoelzl@29805 ` 1979` hoelzl@31098 ` 1980` ``` have "0 < real x" and "real x \ 0" using `0 < x` unfolding less_float_def by auto ``` hoelzl@31098 ` 1981` ``` hence A: "0 < 1 / real x" by auto ``` hoelzl@29805 ` 1982` hoelzl@29805 ` 1983` ``` { ``` hoelzl@29805 ` 1984` ``` let ?divl = "float_divl (max prec 1) 1 x" ``` hoelzl@29805 ` 1985` ``` have A': "1 \ ?divl" using float_divl_pos_less1_bound[OF `0 < x` `x < 1`] unfolding le_float_def less_float_def by auto ``` hoelzl@31098 ` 1986` ``` hence B: "0 < real ?divl" unfolding le_float_def by auto ``` hoelzl@31468 ` 1987` hoelzl@31098 ` 1988` ``` have "ln (real ?divl) \ ln (1 / real x)" unfolding ln_le_cancel_iff[OF B A] using float_divl[of _ 1 x] by auto ``` hoelzl@31098 ` 1989` ``` hence "ln (real x) \ - ln (real ?divl)" unfolding nonzero_inverse_eq_divide[OF `real x \ 0`, symmetric] ln_inverse[OF `0 < real x`] by auto ``` hoelzl@31468 ` 1990` ``` from this ub_ln_lb_ln_bounds'[OF A', THEN conjunct1, THEN le_imp_neg_le] ``` hoelzl@31098 ` 1991` ``` have "?ln \ real (- the (lb_ln prec ?divl))" unfolding real_of_float_minus by (rule order_trans) ``` hoelzl@29805 ` 1992` ``` } moreover ``` hoelzl@29805 ` 1993` ``` { ``` hoelzl@29805 ` 1994` ``` let ?divr = "float_divr prec 1 x" ``` hoelzl@29805 ` 1995` ``` have A': "1 \ ?divr" using float_divr_pos_less1_lower_bound[OF `0 < x` `x < 1`] unfolding le_float_def less_float_def by auto ``` hoelzl@31098 ` 1996` ``` hence B: "0 < real ?divr" unfolding le_float_def by auto ``` hoelzl@31468 ` 1997` hoelzl@31098 ` 1998` ``` have "ln (1 / real x) \ ln (real ?divr)" unfolding ln_le_cancel_iff[OF A B] using float_divr[of 1 x] by auto ``` hoelzl@31098 ` 1999` ``` hence "- ln (real ?divr) \ ln (real x)" unfolding nonzero_inverse_eq_divide[OF `real x \ 0`, symmetric] ln_inverse[OF `0 < real x`] by auto ``` hoelzl@29805 ` 2000` ``` from ub_ln_lb_ln_bounds'[OF A', THEN conjunct2, THEN le_imp_neg_le] this ``` hoelzl@31098 ` 2001` ``` have "real (- the (ub_ln prec ?divr)) \ ?ln" unfolding real_of_float_minus by (rule order_trans) ``` hoelzl@29805 ` 2002` ``` } ``` hoelzl@29805 ` 2003` ``` ultimately show ?thesis unfolding lb_ln.simps[where x=x] ub_ln.simps[where x=x] ``` hoelzl@29805 ` 2004` ``` unfolding if_not_P[OF `\ x \ 0`] if_P[OF True] by auto ``` hoelzl@29805 ` 2005` ```qed ``` hoelzl@29805 ` 2006` hoelzl@29805 ` 2007` ```lemma lb_ln: assumes "Some y = lb_ln prec x" ``` hoelzl@31098 ` 2008` ``` shows "real y \ ln (real x)" and "0 < real x" ``` hoelzl@29805 ` 2009` ```proof - ``` hoelzl@29805 ` 2010` ``` have "0 < x" ``` hoelzl@29805 ` 2011` ``` proof (rule ccontr) ``` hoelzl@29805 ` 2012` ``` assume "\ 0 < x" hence "x \ 0" unfolding le_float_def less_float_def by auto ``` hoelzl@29805 ` 2013` ``` thus False using assms by auto ``` hoelzl@29805 ` 2014` ``` qed ``` hoelzl@31098 ` 2015` ``` thus "0 < real x" unfolding less_float_def by auto ``` hoelzl@31098 ` 2016` ``` have "real (the (lb_ln prec x)) \ ln (real x)" using ub_ln_lb_ln_bounds[OF `0 < x`] .. ``` hoelzl@31098 ` 2017` ``` thus "real y \ ln (real x)" unfolding assms[symmetric] by auto ``` hoelzl@29805 ` 2018` ```qed ``` hoelzl@29805 ` 2019` hoelzl@29805 ` 2020` ```lemma ub_ln: assumes "Some y = ub_ln prec x" ``` hoelzl@31098 ` 2021` ``` shows "ln (real x) \ real y" and "0 < real x" ``` hoelzl@29805 ` 2022` ```proof - ``` hoelzl@29805 ` 2023` ``` have "0 < x" ``` hoelzl@29805 ` 2024` ``` proof (rule ccontr) ``` hoelzl@29805 ` 2025` ``` assume "\ 0 < x" hence "x \ 0" unfolding le_float_def less_float_def by auto ``` hoelzl@29805 ` 2026` ``` thus False using assms by auto ``` hoelzl@29805 ` 2027` ``` qed ``` hoelzl@31098 ` 2028` ``` thus "0 < real x" unfolding less_float_def by auto ``` hoelzl@31098 ` 2029` ``` have "ln (real x) \ real (the (ub_ln prec x))" using ub_ln_lb_ln_bounds[OF `0 < x`] .. ``` hoelzl@31098 ` 2030` ``` thus "ln (real x) \ real y" unfolding assms[symmetric] by auto ``` hoelzl@29805 ` 2031` ```qed ``` hoelzl@29805 ` 2032` hoelzl@31098 ` 2033` ```lemma bnds_ln: "\ x lx ux. (Some l, Some u) = (lb_ln prec lx, ub_ln prec ux) \ x \ {real lx .. real ux} \ real l \ ln x \ ln x \ real u" ``` hoelzl@29805 ` 2034` ```proof (rule allI, rule allI, rule allI, rule impI) ``` hoelzl@29805 ` 2035` ``` fix x lx ux ``` hoelzl@31098 ` 2036` ``` assume "(Some l, Some u) = (lb_ln prec lx, ub_ln prec ux) \ x \ {real lx .. real ux}" ``` hoelzl@31098 ` 2037` ``` hence l: "Some l = lb_ln prec lx " and u: "Some u = ub_ln prec ux" and x: "x \ {real lx .. real ux}" by auto ``` hoelzl@29805 ` 2038` hoelzl@31098 ` 2039` ``` have "ln (real ux) \ real u" and "0 < real ux" using ub_ln u by auto ``` hoelzl@31098 ` 2040` ``` have "real l \ ln (real lx)" and "0 < real lx" and "0 < x" using lb_ln[OF l] x by auto ``` hoelzl@29805 ` 2041` hoelzl@31467 ` 2042` ``` from ln_le_cancel_iff[OF `0 < real lx` `0 < x`] `real l \ ln (real lx)` ``` hoelzl@31098 ` 2043` ``` have "real l \ ln x" using x unfolding atLeastAtMost_iff by auto ``` hoelzl@29805 ` 2044` ``` moreover ``` hoelzl@31467 ` 2045` ``` from ln_le_cancel_iff[OF `0 < x` `0 < real ux`] `ln (real ux) \ real u` ``` hoelzl@31098 ` 2046` ``` have "ln x \ real u" using x unfolding atLeastAtMost_iff by auto ``` hoelzl@31098 ` 2047` ``` ultimately show "real l \ ln x \ ln x \ real u" .. ``` hoelzl@29805 ` 2048` ```qed ``` hoelzl@29805 ` 2049` hoelzl@29805 ` 2050` ```section "Implement floatarith" ``` hoelzl@29805 ` 2051` hoelzl@29805 ` 2052` ```subsection "Define syntax and semantics" ``` hoelzl@29805 ` 2053` hoelzl@29805 ` 2054` ```datatype floatarith ``` hoelzl@29805 ` 2055` ``` = Add floatarith floatarith ``` hoelzl@29805 ` 2056` ``` | Minus floatarith ``` hoelzl@29805 ` 2057` ``` | Mult floatarith floatarith ``` hoelzl@29805 ` 2058` ``` | Inverse floatarith ``` hoelzl@29805 ` 2059` ``` | Cos floatarith ``` hoelzl@29805 ` 2060` ``` | Arctan floatarith ``` hoelzl@29805 ` 2061` ``` | Abs floatarith ``` hoelzl@29805 ` 2062` ``` | Max floatarith floatarith ``` hoelzl@29805 ` 2063` ``` | Min floatarith floatarith ``` hoelzl@29805 ` 2064` ``` | Pi ``` hoelzl@29805 ` 2065` ``` | Sqrt floatarith ``` hoelzl@29805 ` 2066` ``` | Exp floatarith ``` hoelzl@29805 ` 2067` ``` | Ln floatarith ``` hoelzl@29805 ` 2068` ``` | Power floatarith nat ``` hoelzl@32919 ` 2069` ``` | Var nat ``` hoelzl@29805 ` 2070` ``` | Num float ``` hoelzl@29805 ` 2071` hoelzl@31863 ` 2072` ```fun interpret_floatarith :: "floatarith \ real list \ real" where ``` hoelzl@31098 ` 2073` ```"interpret_floatarith (Add a b) vs = (interpret_floatarith a vs) + (interpret_floatarith b vs)" | ``` hoelzl@31098 ` 2074` ```"interpret_floatarith (Minus a) vs = - (interpret_floatarith a vs)" | ``` hoelzl@31098 ` 2075` ```"interpret_floatarith (Mult a b) vs = (interpret_floatarith a vs) * (interpret_floatarith b vs)" | ``` hoelzl@31098 ` 2076` ```"interpret_floatarith (Inverse a) vs = inverse (interpret_floatarith a vs)" | ``` hoelzl@31098 ` 2077` ```"interpret_floatarith (Cos a) vs = cos (interpret_floatarith a vs)" | ``` hoelzl@31098 ` 2078` ```"interpret_floatarith (Arctan a) vs = arctan (interpret_floatarith a vs)" | ``` hoelzl@31098 ` 2079` ```"interpret_floatarith (Min a b) vs = min (interpret_floatarith a vs) (interpret_floatarith b vs)" | ``` hoelzl@31098 ` 2080` ```"interpret_floatarith (Max a b) vs = max (interpret_floatarith a vs) (interpret_floatarith b vs)" | ``` hoelzl@31098 ` 2081` ```"interpret_floatarith (Abs a) vs = abs (interpret_floatarith a vs)" | ``` hoelzl@31098 ` 2082` ```"interpret_floatarith Pi vs = pi" | ``` hoelzl@31098 ` 2083` ```"interpret_floatarith (Sqrt a) vs = sqrt (interpret_floatarith a vs)" | ``` hoelzl@31098 ` 2084` ```"interpret_floatarith (Exp a) vs = exp (interpret_floatarith a vs)" | ``` hoelzl@31098 ` 2085` ```"interpret_floatarith (Ln a) vs = ln (interpret_floatarith a vs)" | ``` hoelzl@31098 ` 2086` ```"interpret_floatarith (Power a n) vs = (interpret_floatarith a vs)^n" | ``` hoelzl@31098 ` 2087` ```"interpret_floatarith (Num f) vs = real f" | ``` hoelzl@32919 ` 2088` ```"interpret_floatarith (Var n) vs = vs ! n" ``` hoelzl@29805 ` 2089` hoelzl@31811 ` 2090` ```lemma interpret_floatarith_divide: "interpret_floatarith (Mult a (Inverse b)) vs = (interpret_floatarith a vs) / (interpret_floatarith b vs)" ``` hoelzl@31811 ` 2091` ``` unfolding real_divide_def interpret_floatarith.simps .. ``` hoelzl@31811 ` 2092` hoelzl@31811 ` 2093` ```lemma interpret_floatarith_diff: "interpret_floatarith (Add a (Minus b)) vs = (interpret_floatarith a vs) - (interpret_floatarith b vs)" ``` hoelzl@31811 ` 2094` ``` unfolding real_diff_def interpret_floatarith.simps .. ``` hoelzl@31811 ` 2095` hoelzl@31811 ` 2096` ```lemma interpret_floatarith_sin: "interpret_floatarith (Cos (Add (Mult Pi (Num (Float 1 -1))) (Minus a))) vs = ``` hoelzl@31811 ` 2097` ``` sin (interpret_floatarith a vs)" ``` hoelzl@31811 ` 2098` ``` unfolding sin_cos_eq interpret_floatarith.simps ``` hoelzl@31811 ` 2099` ``` interpret_floatarith_divide interpret_floatarith_diff real_diff_def ``` hoelzl@31811 ` 2100` ``` by auto ``` hoelzl@31811 ` 2101` hoelzl@31811 ` 2102` ```lemma interpret_floatarith_tan: ``` hoelzl@31811 ` 2103` ``` "interpret_floatarith (Mult (Cos (Add (Mult Pi (Num (Float 1 -1))) (Minus a))) (Inverse (Cos a))) vs = ``` hoelzl@31811 ` 2104` ``` tan (interpret_floatarith a vs)" ``` hoelzl@31811 ` 2105` ``` unfolding interpret_floatarith.simps(3,4) interpret_floatarith_sin tan_def real_divide_def ``` hoelzl@31811 ` 2106` ``` by auto ``` hoelzl@31811 ` 2107` hoelzl@31811 ` 2108` ```lemma interpret_floatarith_powr: "interpret_floatarith (Exp (Mult b (Ln a))) vs = (interpret_floatarith a vs) powr (interpret_floatarith b vs)" ``` hoelzl@31811 ` 2109` ``` unfolding powr_def interpret_floatarith.simps .. ``` hoelzl@31811 ` 2110` hoelzl@31811 ` 2111` ```lemma interpret_floatarith_log: "interpret_floatarith ((Mult (Ln x) (Inverse (Ln b)))) vs = log (interpret_floatarith b vs) (interpret_floatarith x vs)" ``` hoelzl@31811 ` 2112` ``` unfolding log_def interpret_floatarith.simps real_divide_def .. ``` hoelzl@31811 ` 2113` hoelzl@31811 ` 2114` ```lemma interpret_floatarith_num: ``` hoelzl@31811 ` 2115` ``` shows "interpret_floatarith (Num (Float 0 0)) vs = 0" ``` hoelzl@31811 ` 2116` ``` and "interpret_floatarith (Num (Float 1 0)) vs = 1" ``` hoelzl@31811 ` 2117` ``` and "interpret_floatarith (Num (Float (number_of a) 0)) vs = number_of a" by auto ``` hoelzl@31811 ` 2118` hoelzl@29805 ` 2119` ```subsection "Implement approximation function" ``` hoelzl@29805 ` 2120` hoelzl@29805 ` 2121` ```fun lift_bin' :: "(float * float) option \ (float * float) option \ (float \ float \ float \ float \ (float * float)) \ (float * float) option" where ``` hoelzl@29805 ` 2122` ```"lift_bin' (Some (l1, u1)) (Some (l2, u2)) f = Some (f l1 u1 l2 u2)" | ``` hoelzl@29805 ` 2123` ```"lift_bin' a b f = None" ``` hoelzl@29805 ` 2124` hoelzl@29805 ` 2125` ```fun lift_un :: "(float * float) option \ (float \ float \ ((float option) * (float option))) \ (float * float) option" where ``` hoelzl@29805 ` 2126` ```"lift_un (Some (l1, u1)) f = (case (f l1 u1) of (Some l, Some u) \ Some (l, u) ``` hoelzl@29805 ` 2127` ``` | t \ None)" | ``` hoelzl@29805 ` 2128` ```"lift_un b f = None" ``` hoelzl@29805 ` 2129` hoelzl@29805 ` 2130` ```fun lift_un' :: "(float * float) option \ (float \ float \ (float * float)) \ (float * float) option" where ``` hoelzl@29805 ` 2131` ```"lift_un' (Some (l1, u1)) f = Some (f l1 u1)" | ``` hoelzl@29805 ` 2132` ```"lift_un' b f = None" ``` hoelzl@29805 ` 2133` hoelzl@31811 ` 2134` ```definition ``` hoelzl@31811 ` 2135` ```"bounded_by xs vs \ ``` hoelzl@31811 ` 2136` ``` (\ i < length vs. case vs ! i of None \ True ``` hoelzl@31811 ` 2137` ``` | Some (l, u) \ xs ! i \ { real l .. real u })" ``` hoelzl@31811 ` 2138` hoelzl@31811 ` 2139` ```lemma bounded_byE: ``` hoelzl@31811 ` 2140` ``` assumes "bounded_by xs vs" ``` hoelzl@31811 ` 2141` ``` shows "\ i. i < length vs \ case vs ! i of None \ True ``` hoelzl@31811 ` 2142` ``` | Some (l, u) \ xs ! i \ { real l .. real u }" ``` hoelzl@31811 ` 2143` ``` using assms bounded_by_def by blast ``` hoelzl@31811 ` 2144` hoelzl@31811 ` 2145` ```lemma bounded_by_update: ``` hoelzl@31811 ` 2146` ``` assumes "bounded_by xs vs" ``` hoelzl@31811 ` 2147` ``` and bnd: "xs ! i \ { real l .. real u }" ``` hoelzl@31811 ` 2148` ``` shows "bounded_by xs (vs[i := Some (l,u)])" ``` hoelzl@31811 ` 2149` ```proof - ``` hoelzl@31811 ` 2150` ```{ fix j ``` hoelzl@31811 ` 2151` ``` let ?vs = "vs[i := Some (l,u)]" ``` hoelzl@31811 ` 2152` ``` assume "j < length ?vs" hence [simp]: "j < length vs" by simp ``` hoelzl@31811 ` 2153` ``` have "case ?vs ! j of None \ True | Some (l, u) \ xs ! j \ { real l .. real u }" ``` hoelzl@31811 ` 2154` ``` proof (cases "?vs ! j") ``` hoelzl@31811 ` 2155` ``` case (Some b) ``` hoelzl@31811 ` 2156` ``` thus ?thesis ``` hoelzl@31811 ` 2157` ``` proof (cases "i = j") ``` hoelzl@31811 ` 2158` ``` case True ``` hoelzl@31811 ` 2159` ``` thus ?thesis using `?vs ! j = Some b` and bnd by auto ``` hoelzl@31811 ` 2160` ``` next ``` hoelzl@31811 ` 2161` ``` case False ``` hoelzl@31811 ` 2162` ``` thus ?thesis using `bounded_by xs vs` unfolding bounded_by_def by auto ``` hoelzl@31811 ` 2163` ``` qed ``` hoelzl@31811 ` 2164` ``` qed auto } ``` hoelzl@31811 ` 2165` ``` thus ?thesis unfolding bounded_by_def by auto ``` hoelzl@31811 ` 2166` ```qed ``` hoelzl@31811 ` 2167` hoelzl@31811 ` 2168` ```lemma bounded_by_None: ``` hoelzl@31811 ` 2169` ``` shows "bounded_by xs (replicate (length xs) None)" ``` hoelzl@31811 ` 2170` ``` unfolding bounded_by_def by auto ``` hoelzl@31811 ` 2171` hoelzl@31811 ` 2172` ```fun approx approx' :: "nat \ floatarith \ (float * float) option list \ (float * float) option" where ``` hoelzl@29805 ` 2173` ```"approx' prec a bs = (case (approx prec a bs) of Some (l, u) \ Some (round_down prec l, round_up prec u) | None \ None)" | ``` hoelzl@31811 ` 2174` ```"approx prec (Add a b) bs = lift_bin' (approx' prec a bs) (approx' prec b bs) (\ l1 u1 l2 u2. (l1 + l2, u1 + u2))" | ``` hoelzl@29805 ` 2175` ```"approx prec (Minus a) bs = lift_un' (approx' prec a bs) (\ l u. (-u, -l))" | ``` hoelzl@29805 ` 2176` ```"approx prec (Mult a b) bs = lift_bin' (approx' prec a bs) (approx' prec b bs) ``` hoelzl@31809 ` 2177` ``` (\ a1 a2 b1 b2. (float_nprt a1 * float_pprt b2 + float_nprt a2 * float_nprt b2 + float_pprt a1 * float_pprt b1 + float_pprt a2 * float_nprt b1, ``` hoelzl@29805 ` 2178` ``` float_pprt a2 * float_pprt b2 + float_pprt a1 * float_nprt b2 + float_nprt a2 * float_pprt b1 + float_nprt a1 * float_nprt b1))" | ``` hoelzl@29805 ` 2179` ```"approx prec (Inverse a) bs = lift_un (approx' prec a bs) (\ l u. if (0 < l \ u < 0) then (Some (float_divl prec 1 u), Some (float_divr prec 1 l)) else (None, None))" | ``` hoelzl@29805 ` 2180` ```"approx prec (Cos a) bs = lift_un' (approx' prec a bs) (bnds_cos prec)" | ``` hoelzl@29805 ` 2181` ```"approx prec Pi bs = Some (lb_pi prec, ub_pi prec)" | ``` hoelzl@29805 ` 2182` ```"approx prec (Min a b) bs = lift_bin' (approx' prec a bs) (approx' prec b bs) (\ l1 u1 l2 u2. (min l1 l2, min u1 u2))" | ``` hoelzl@29805 ` 2183` ```"approx prec (Max a b) bs = lift_bin' (approx' prec a bs) (approx' prec b bs) (\ l1 u1 l2 u2. (max l1 l2, max u1 u2))" | ``` hoelzl@29805 ` 2184` ```"approx prec (Abs a) bs = lift_un' (approx' prec a bs) (\l u. (if l < 0 \ 0 < u then 0 else min \l\ \u\, max \l\ \u\))" | ``` hoelzl@29805 ` 2185` ```"approx prec (Arctan a) bs = lift_un' (approx' prec a bs) (\ l u. (lb_arctan prec l, ub_arctan prec u))" | ``` hoelzl@31467 ` 2186` ```"approx prec (Sqrt a) bs = lift_un' (approx' prec a bs) (\ l u. (lb_sqrt prec l, ub_sqrt prec u))" | ``` hoelzl@29805 ` 2187` ```"approx prec (Exp a) bs = lift_un' (approx' prec a bs) (\ l u. (lb_exp prec l, ub_exp prec u))" | ``` hoelzl@29805 ` 2188` ```"approx prec (Ln a) bs = lift_un (approx' prec a bs) (\ l u. (lb_ln prec l, ub_ln prec u))" | ``` hoelzl@29805 ` 2189` ```"approx prec (Power a n) bs = lift_un' (approx' prec a bs) (float_power_bnds n)" | ``` hoelzl@29805 ` 2190` ```"approx prec (Num f) bs = Some (f, f)" | ``` hoelzl@32919 ` 2191` ```"approx prec (Var i) bs = (if i < length bs then bs ! i else None)" ``` hoelzl@29805 ` 2192` hoelzl@29805 ` 2193` ```lemma lift_bin'_ex: ``` hoelzl@29805 ` 2194` ``` assumes lift_bin'_Some: "Some (l, u) = lift_bin' a b f" ``` hoelzl@29805 ` 2195` ``` shows "\ l1 u1 l2 u2. Some (l1, u1) = a \ Some (l2, u2) = b" ``` hoelzl@29805 ` 2196` ```proof (cases a) ``` hoelzl@29805 ` 2197` ``` case None hence "None = lift_bin' a b f" unfolding None lift_bin'.simps .. ```