| author | huffman | 
| Sat, 21 Apr 2012 06:49:04 +0200 | |
| changeset 47640 | 62bfba15b212 | 
| parent 47621 | 4cf6011fb884 | 
| child 49351 | 0dd3449640b4 | 
| permissions | -rw-r--r-- | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1 | (* Author: Johannes Hoelzl, TU Muenchen | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2 | Coercions removed by Dmitriy Traytel *) | 
| 30122 | 3 | |
| 30886 
dda08b76fa99
updated official title of contribution by Johannes Hoelzl;
 wenzelm parents: 
30549diff
changeset | 4 | header {* Prove Real Valued Inequalities by Computation *}
 | 
| 30122 | 5 | |
| 40892 | 6 | theory Approximation | 
| 41413 
64cd30d6b0b8
explicit file specifications -- avoid secondary load path;
 wenzelm parents: 
41024diff
changeset | 7 | imports | 
| 
64cd30d6b0b8
explicit file specifications -- avoid secondary load path;
 wenzelm parents: 
41024diff
changeset | 8 | Complex_Main | 
| 
64cd30d6b0b8
explicit file specifications -- avoid secondary load path;
 wenzelm parents: 
41024diff
changeset | 9 | "~~/src/HOL/Library/Float" | 
| 
64cd30d6b0b8
explicit file specifications -- avoid secondary load path;
 wenzelm parents: 
41024diff
changeset | 10 | "~~/src/HOL/Library/Reflection" | 
| 
64cd30d6b0b8
explicit file specifications -- avoid secondary load path;
 wenzelm parents: 
41024diff
changeset | 11 | "~~/src/HOL/Decision_Procs/Dense_Linear_Order" | 
| 
64cd30d6b0b8
explicit file specifications -- avoid secondary load path;
 wenzelm parents: 
41024diff
changeset | 12 | "~~/src/HOL/Library/Efficient_Nat" | 
| 29805 | 13 | begin | 
| 14 | ||
| 47600 | 15 | declare powr_numeral[simp] | 
| 16 | declare powr_neg_numeral[simp] | |
| 17 | ||
| 29805 | 18 | section "Horner Scheme" | 
| 19 | ||
| 20 | subsection {* Define auxiliary helper @{text horner} function *}
 | |
| 21 | ||
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 22 | primrec horner :: "(nat \<Rightarrow> nat) \<Rightarrow> (nat \<Rightarrow> nat \<Rightarrow> nat) \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> real \<Rightarrow> real" where | 
| 29805 | 23 | "horner F G 0 i k x = 0" | | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 24 | "horner F G (Suc n) i k x = 1 / k - x * horner F G n (F i) (G i k) x" | 
| 29805 | 25 | |
| 26 | lemma horner_schema': fixes x :: real and a :: "nat \<Rightarrow> real" | |
| 27 | shows "a 0 - x * (\<Sum> i=0..<n. (-1)^i * a (Suc i) * x^i) = (\<Sum> i=0..<Suc n. (-1)^i * a i * x^i)" | |
| 28 | proof - | |
| 29 | have shift_pow: "\<And>i. - (x * ((-1)^i * a (Suc i) * x ^ i)) = (-1)^(Suc i) * a (Suc i) * x ^ (Suc i)" by auto | |
| 37887 | 30 | show ?thesis unfolding setsum_right_distrib shift_pow diff_minus setsum_negf[symmetric] setsum_head_upt_Suc[OF zero_less_Suc] | 
| 29805 | 31 | setsum_reindex[OF inj_Suc, unfolded comp_def, symmetric, of "\<lambda> n. (-1)^n *a n * x^n"] by auto | 
| 32 | qed | |
| 33 | ||
| 34 | lemma horner_schema: fixes f :: "nat \<Rightarrow> nat" and G :: "nat \<Rightarrow> nat \<Rightarrow> nat" and F :: "nat \<Rightarrow> nat" | |
| 30971 | 35 | assumes f_Suc: "\<And>n. f (Suc n) = G ((F ^^ n) s) (f n)" | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 36 | shows "horner F G n ((F ^^ j') s) (f j') x = (\<Sum> j = 0..< n. -1 ^ j * (1 / (f (j' + j))) * x ^ j)" | 
| 45129 
1fce03e3e8ad
tuned proofs -- eliminated vacuous "induct arbitrary: ..." situations;
 wenzelm parents: 
44821diff
changeset | 37 | proof (induct n arbitrary: j') | 
| 
1fce03e3e8ad
tuned proofs -- eliminated vacuous "induct arbitrary: ..." situations;
 wenzelm parents: 
44821diff
changeset | 38 | case 0 | 
| 
1fce03e3e8ad
tuned proofs -- eliminated vacuous "induct arbitrary: ..." situations;
 wenzelm parents: 
44821diff
changeset | 39 | then show ?case by auto | 
| 
1fce03e3e8ad
tuned proofs -- eliminated vacuous "induct arbitrary: ..." situations;
 wenzelm parents: 
44821diff
changeset | 40 | next | 
| 29805 | 41 | case (Suc n) | 
| 42 | show ?case unfolding horner.simps Suc[where j'="Suc j'", unfolded funpow.simps comp_def f_Suc] | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 43 | using horner_schema'[of "\<lambda> j. 1 / (f (j' + j))"] by auto | 
| 45129 
1fce03e3e8ad
tuned proofs -- eliminated vacuous "induct arbitrary: ..." situations;
 wenzelm parents: 
44821diff
changeset | 44 | qed | 
| 29805 | 45 | |
| 46 | lemma horner_bounds': | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 47 | fixes lb :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> float \<Rightarrow> float" and ub :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> float \<Rightarrow> float" | 
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 48 | assumes "0 \<le> real x" and f_Suc: "\<And>n. f (Suc n) = G ((F ^^ n) s) (f n)" | 
| 29805 | 49 | and lb_0: "\<And> i k x. lb 0 i k x = 0" | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 50 | and lb_Suc: "\<And> n i k x. lb (Suc n) i k x = lapprox_rat prec 1 k - x * (ub n (F i) (G i k) x)" | 
| 29805 | 51 | and ub_0: "\<And> i k x. ub 0 i k x = 0" | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 52 | and ub_Suc: "\<And> n i k x. ub (Suc n) i k x = rapprox_rat prec 1 k - x * (lb n (F i) (G i k) x)" | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 53 | shows "(lb n ((F ^^ j') s) (f j') x) \<le> horner F G n ((F ^^ j') s) (f j') x \<and> | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 54 | horner F G n ((F ^^ j') s) (f j') x \<le> (ub n ((F ^^ j') s) (f j') x)" | 
| 29805 | 55 | (is "?lb n j' \<le> ?horner n j' \<and> ?horner n j' \<le> ?ub n j'") | 
| 56 | proof (induct n arbitrary: j') | |
| 57 | case 0 thus ?case unfolding lb_0 ub_0 horner.simps by auto | |
| 58 | next | |
| 59 | case (Suc n) | |
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 60 | thus ?case using lapprox_rat[of prec 1 "f j'"] using rapprox_rat[of 1 "f j'" prec] | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 61 | Suc[where j'="Suc j'"] `0 \<le> real x` | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 62 | by (auto intro!: add_mono mult_left_mono simp add: lb_Suc ub_Suc field_simps f_Suc) | 
| 29805 | 63 | qed | 
| 64 | ||
| 65 | subsection "Theorems for floating point functions implementing the horner scheme" | |
| 66 | ||
| 67 | text {*
 | |
| 68 | ||
| 69 | Here @{term_type "f :: nat \<Rightarrow> nat"} is the sequence defining the Taylor series, the coefficients are
 | |
| 70 | all alternating and reciprocs. We use @{term G} and @{term F} to describe the computation of @{term f}.
 | |
| 71 | ||
| 72 | *} | |
| 73 | ||
| 74 | lemma horner_bounds: fixes F :: "nat \<Rightarrow> nat" and G :: "nat \<Rightarrow> nat \<Rightarrow> nat" | |
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 75 | assumes "0 \<le> real x" and f_Suc: "\<And>n. f (Suc n) = G ((F ^^ n) s) (f n)" | 
| 29805 | 76 | and lb_0: "\<And> i k x. lb 0 i k x = 0" | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 77 | and lb_Suc: "\<And> n i k x. lb (Suc n) i k x = lapprox_rat prec 1 k - x * (ub n (F i) (G i k) x)" | 
| 29805 | 78 | and ub_0: "\<And> i k x. ub 0 i k x = 0" | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 79 | and ub_Suc: "\<And> n i k x. ub (Suc n) i k x = rapprox_rat prec 1 k - x * (lb n (F i) (G i k) x)" | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 80 | shows "(lb n ((F ^^ j') s) (f j') x) \<le> (\<Sum>j=0..<n. -1 ^ j * (1 / (f (j' + j))) * (x ^ j))" (is "?lb") and | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 81 | "(\<Sum>j=0..<n. -1 ^ j * (1 / (f (j' + j))) * (x ^ j)) \<le> (ub n ((F ^^ j') s) (f j') x)" (is "?ub") | 
| 29805 | 82 | proof - | 
| 31809 | 83 | have "?lb \<and> ?ub" | 
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 84 | using horner_bounds'[where lb=lb, OF `0 \<le> real x` f_Suc lb_0 lb_Suc ub_0 ub_Suc] | 
| 29805 | 85 | unfolding horner_schema[where f=f, OF f_Suc] . | 
| 86 | thus "?lb" and "?ub" by auto | |
| 87 | qed | |
| 88 | ||
| 89 | lemma horner_bounds_nonpos: fixes F :: "nat \<Rightarrow> nat" and G :: "nat \<Rightarrow> nat \<Rightarrow> nat" | |
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 90 | assumes "real x \<le> 0" and f_Suc: "\<And>n. f (Suc n) = G ((F ^^ n) s) (f n)" | 
| 29805 | 91 | and lb_0: "\<And> i k x. lb 0 i k x = 0" | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 92 | and lb_Suc: "\<And> n i k x. lb (Suc n) i k x = lapprox_rat prec 1 k + x * (ub n (F i) (G i k) x)" | 
| 29805 | 93 | and ub_0: "\<And> i k x. ub 0 i k x = 0" | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 94 | and ub_Suc: "\<And> n i k x. ub (Suc n) i k x = rapprox_rat prec 1 k + x * (lb n (F i) (G i k) x)" | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 95 | shows "(lb n ((F ^^ j') s) (f j') x) \<le> (\<Sum>j=0..<n. (1 / (f (j' + j))) * real x ^ j)" (is "?lb") and | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 96 | "(\<Sum>j=0..<n. (1 / (f (j' + j))) * real x ^ j) \<le> (ub n ((F ^^ j') s) (f j') x)" (is "?ub") | 
| 29805 | 97 | proof - | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 98 |   { fix x y z :: float have "x - y * z = x + - y * z" by simp } note diff_mult_minus = this
 | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 99 | have sum_eq: "(\<Sum>j=0..<n. (1 / (f (j' + j))) * real x ^ j) = | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 100 | (\<Sum>j = 0..<n. -1 ^ j * (1 / (f (j' + j))) * real (- x) ^ j)" | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 101 | by (auto simp add: field_simps power_mult_distrib[symmetric]) | 
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 102 | have "0 \<le> real (-x)" using assms by auto | 
| 29805 | 103 | from horner_bounds[where G=G and F=F and f=f and s=s and prec=prec | 
| 104 | and lb="\<lambda> n i k x. lb n i k (-x)" and ub="\<lambda> n i k x. ub n i k (-x)", unfolded lb_Suc ub_Suc diff_mult_minus, | |
| 105 | OF this f_Suc lb_0 refl ub_0 refl] | |
| 106 | show "?lb" and "?ub" unfolding minus_minus sum_eq | |
| 107 | by auto | |
| 108 | qed | |
| 109 | ||
| 110 | subsection {* Selectors for next even or odd number *}
 | |
| 111 | ||
| 112 | text {*
 | |
| 113 | ||
| 114 | The horner scheme computes alternating series. To get the upper and lower bounds we need to | |
| 115 | guarantee to access a even or odd member. To do this we use @{term get_odd} and @{term get_even}.
 | |
| 116 | ||
| 117 | *} | |
| 118 | ||
| 119 | definition get_odd :: "nat \<Rightarrow> nat" where | |
| 120 | "get_odd n = (if odd n then n else (Suc n))" | |
| 121 | ||
| 122 | definition get_even :: "nat \<Rightarrow> nat" where | |
| 123 | "get_even n = (if even n then n else (Suc n))" | |
| 124 | ||
| 125 | lemma get_odd[simp]: "odd (get_odd n)" unfolding get_odd_def by (cases "odd n", auto) | |
| 126 | lemma get_even[simp]: "even (get_even n)" unfolding get_even_def by (cases "even n", auto) | |
| 127 | lemma get_odd_ex: "\<exists> k. Suc k = get_odd n \<and> odd (Suc k)" | |
| 128 | proof (cases "odd n") | |
| 129 | case True hence "0 < n" by (rule odd_pos) | |
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 130 | from gr0_implies_Suc[OF this] obtain k where "Suc k = n" by auto | 
| 29805 | 131 | thus ?thesis unfolding get_odd_def if_P[OF True] using True[unfolded `Suc k = n`[symmetric]] by blast | 
| 132 | next | |
| 133 | case False hence "odd (Suc n)" by auto | |
| 134 | thus ?thesis unfolding get_odd_def if_not_P[OF False] by blast | |
| 135 | qed | |
| 136 | ||
| 137 | lemma get_even_double: "\<exists>i. get_even n = 2 * i" using get_even[unfolded even_mult_two_ex] . | |
| 138 | lemma get_odd_double: "\<exists>i. get_odd n = 2 * i + 1" using get_odd[unfolded odd_Suc_mult_two_ex] by auto | |
| 139 | ||
| 140 | section "Power function" | |
| 141 | ||
| 142 | definition float_power_bnds :: "nat \<Rightarrow> float \<Rightarrow> float \<Rightarrow> float * float" where | |
| 143 | "float_power_bnds n l u = (if odd n \<or> 0 < l then (l ^ n, u ^ n) | |
| 144 | else if u < 0 then (u ^ n, l ^ n) | |
| 145 | else (0, (max (-l) u) ^ n))" | |
| 146 | ||
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 147 | lemma float_power_bnds: fixes x :: real | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 148 |   assumes "(l1, u1) = float_power_bnds n l u" and "x \<in> {l .. u}"
 | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 149 |   shows "x ^ n \<in> {l1..u1}"
 | 
| 29805 | 150 | proof (cases "even n") | 
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 151 | case True | 
| 29805 | 152 | show ?thesis | 
| 153 | proof (cases "0 < l") | |
| 47600 | 154 | case True hence "odd n \<or> 0 < l" and "0 \<le> real l" by auto | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 155 | have u1: "u1 = u ^ n" and l1: "l1 = l ^ n" using assms | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 156 | unfolding float_power_bnds_def if_P[OF `odd n \<or> 0 < l`] by auto | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 157 | have "real l ^ n \<le> x ^ n" and "x ^ n \<le> real u ^ n " using `0 \<le> real l` assms | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 158 | by (auto simp: power_mono) | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 159 | thus ?thesis using assms `0 < l` unfolding l1 u1 by auto | 
| 29805 | 160 | next | 
| 161 | case False hence P: "\<not> (odd n \<or> 0 < l)" using `even n` by auto | |
| 162 | show ?thesis | |
| 163 | proof (cases "u < 0") | |
| 47600 | 164 | case True hence "0 \<le> - real u" and "- real u \<le> - x" and "0 \<le> - x" and "-x \<le> - real l" using assms by auto | 
| 31809 | 165 | hence "real u ^ n \<le> x ^ n" and "x ^ n \<le> real l ^ n" using power_mono[of "-x" "-real l" n] power_mono[of "-real u" "-x" n] | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 166 | unfolding power_minus_even[OF `even n`] by auto | 
| 29805 | 167 | 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 | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 168 | ultimately show ?thesis by auto | 
| 29805 | 169 | next | 
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 170 | case False | 
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 171 | have "\<bar>x\<bar> \<le> real (max (-l) u)" | 
| 29805 | 172 | proof (cases "-l \<le> u") | 
| 47600 | 173 | case True thus ?thesis unfolding max_def if_P[OF True] using assms by auto | 
| 29805 | 174 | next | 
| 47600 | 175 | case False thus ?thesis unfolding max_def if_not_P[OF False] using assms by auto | 
| 29805 | 176 | qed | 
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 177 | hence x_abs: "\<bar>x\<bar> \<le> \<bar>real (max (-l) u)\<bar>" by auto | 
| 29805 | 178 | 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 | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 179 | show ?thesis unfolding atLeastAtMost_iff l1 u1 using zero_le_even_power[OF `even n`] power_mono_even[OF `even n` x_abs] by auto | 
| 29805 | 180 | qed | 
| 181 | qed | |
| 182 | next | |
| 183 | case False hence "odd n \<or> 0 < l" by auto | |
| 184 | have u1: "u1 = u ^ n" and l1: "l1 = l ^ n" using assms unfolding float_power_bnds_def if_P[OF `odd n \<or> 0 < l`] by auto | |
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 185 | have "real l ^ n \<le> x ^ n" and "x ^ n \<le> real u ^ n " using assms unfolding atLeastAtMost_iff using power_mono_odd[OF False] by auto | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 186 | thus ?thesis unfolding atLeastAtMost_iff l1 u1 less_float_def by auto | 
| 29805 | 187 | qed | 
| 188 | ||
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 189 | lemma bnds_power: "\<forall> (x::real) l u. (l1, u1) = float_power_bnds n l u \<and> x \<in> {l .. u} \<longrightarrow> l1 \<le> x ^ n \<and> x ^ n \<le> u1"
 | 
| 29805 | 190 | using float_power_bnds by auto | 
| 191 | ||
| 192 | section "Square root" | |
| 193 | ||
| 194 | text {*
 | |
| 195 | ||
| 196 | The square root computation is implemented as newton iteration. As first first step we use the | |
| 197 | nearest power of two greater than the square root. | |
| 198 | ||
| 199 | *} | |
| 200 | ||
| 201 | fun sqrt_iteration :: "nat \<Rightarrow> nat \<Rightarrow> float \<Rightarrow> float" where | |
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 202 | "sqrt_iteration prec 0 x = Float 1 ((bitlen \<bar>mantissa x\<bar> + exponent x) div 2 + 1)" | | 
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 203 | "sqrt_iteration prec (Suc m) x = (let y = sqrt_iteration prec m x | 
| 29805 | 204 | in Float 1 -1 * (y + float_divr prec x y))" | 
| 205 | ||
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 206 | lemma compute_sqrt_iteration_base[code]: | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 207 | shows "sqrt_iteration prec n (Float m e) = | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 208 | (if n = 0 then Float 1 ((if m = 0 then 0 else bitlen \<bar>m\<bar> + e) div 2 + 1) | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 209 | else (let y = sqrt_iteration prec (n - 1) (Float m e) in | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 210 | Float 1 -1 * (y + float_divr prec (Float m e) y)))" | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 211 | using bitlen_Float by (cases n) simp_all | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 212 | |
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 213 | function ub_sqrt lb_sqrt :: "nat \<Rightarrow> float \<Rightarrow> float" where | 
| 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 214 | "ub_sqrt prec x = (if 0 < x then (sqrt_iteration prec prec x) | 
| 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 215 | else if x < 0 then - lb_sqrt prec (- x) | 
| 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 216 | else 0)" | | 
| 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 217 | "lb_sqrt prec x = (if 0 < x then (float_divl prec x (sqrt_iteration prec prec x)) | 
| 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 218 | else if x < 0 then - ub_sqrt prec (- x) | 
| 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 219 | else 0)" | 
| 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 220 | by pat_completeness auto | 
| 47600 | 221 | termination by (relation "measure (\<lambda> v. let (prec, x) = sum_case id id v in (if x < 0 then 1 else 0))", auto) | 
| 29805 | 222 | |
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 223 | declare lb_sqrt.simps[simp del] | 
| 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 224 | declare ub_sqrt.simps[simp del] | 
| 29805 | 225 | |
| 226 | lemma sqrt_ub_pos_pos_1: | |
| 227 | assumes "sqrt x < b" and "0 < b" and "0 < x" | |
| 228 | shows "sqrt x < (b + x / b)/2" | |
| 229 | proof - | |
| 230 | from assms have "0 < (b - sqrt x) ^ 2 " by simp | |
| 231 | also have "\<dots> = b ^ 2 - 2 * b * sqrt x + (sqrt x) ^ 2" by algebra | |
| 46545 | 232 | also have "\<dots> = b ^ 2 - 2 * b * sqrt x + x" using assms by simp | 
| 29805 | 233 | finally have "0 < b ^ 2 - 2 * b * sqrt x + x" by assumption | 
| 234 | hence "0 < b / 2 - sqrt x + x / (2 * b)" using assms | |
| 235 | by (simp add: field_simps power2_eq_square) | |
| 236 | thus ?thesis by (simp add: field_simps) | |
| 237 | qed | |
| 238 | ||
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 239 | lemma sqrt_iteration_bound: assumes "0 < real x" | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 240 | shows "sqrt x < (sqrt_iteration prec n x)" | 
| 29805 | 241 | proof (induct n) | 
| 242 | case 0 | |
| 243 | show ?case | |
| 244 | proof (cases x) | |
| 245 | case (Float m e) | |
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 246 | hence "0 < m" using assms powr_gt_zero[of 2 e] by (auto simp: sign_simps) | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 247 | hence "0 < sqrt m" by auto | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 248 | |
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 249 | have int_nat_bl: "(nat (bitlen m)) = bitlen m" using bitlen_nonneg by auto | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 250 | |
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 251 | have "x = (m / 2^nat (bitlen m)) * 2 powr (e + (nat (bitlen m)))" | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 252 | unfolding Float by (auto simp: powr_realpow[symmetric] field_simps powr_add) | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 253 | also have "\<dots> < 1 * 2 powr (e + nat (bitlen m))" | 
| 29805 | 254 | proof (rule mult_strict_right_mono, auto) | 
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 255 | show "real m < 2^nat (bitlen m)" using bitlen_bounds[OF `0 < m`, THEN conjunct2] | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 256 | unfolding real_of_int_less_iff[of m, symmetric] by auto | 
| 29805 | 257 | qed | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 258 | finally have "sqrt x < sqrt (2 powr (e + bitlen m))" unfolding int_nat_bl by auto | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 259 | also have "\<dots> \<le> 2 powr ((e + bitlen m) div 2 + 1)" | 
| 29805 | 260 | proof - | 
| 261 | let ?E = "e + bitlen m" | |
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 262 | have E_mod_pow: "2 powr (?E mod 2) < 4" | 
| 29805 | 263 | proof (cases "?E mod 2 = 1") | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 264 | case True thus ?thesis by auto | 
| 29805 | 265 | next | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 266 | case False | 
| 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 267 | have "0 \<le> ?E mod 2" by auto | 
| 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 268 | have "?E mod 2 < 2" by auto | 
| 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 269 | from this[THEN zless_imp_add1_zle] | 
| 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 270 | have "?E mod 2 \<le> 0" using False by auto | 
| 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 271 | from xt1(5)[OF `0 \<le> ?E mod 2` this] | 
| 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 272 | show ?thesis by auto | 
| 29805 | 273 | qed | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 274 | hence "sqrt (2 powr (?E mod 2)) < sqrt (2 * 2)" by auto | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 275 | hence E_mod_pow: "sqrt (2 powr (?E mod 2)) < 2" unfolding real_sqrt_abs2 by auto | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 276 | |
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 277 | have E_eq: "2 powr ?E = 2 powr (?E div 2 + ?E div 2 + ?E mod 2)" by auto | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 278 | have "sqrt (2 powr ?E) = sqrt (2 powr (?E div 2) * 2 powr (?E div 2) * 2 powr (?E mod 2))" | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 279 | unfolding E_eq unfolding powr_add[symmetric] by (simp add: int_of_reals del: real_of_ints) | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 280 | also have "\<dots> = 2 powr (?E div 2) * sqrt (2 powr (?E mod 2))" | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 281 | unfolding real_sqrt_mult[of _ "2 powr (?E mod 2)"] real_sqrt_abs2 by auto | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 282 | also have "\<dots> < 2 powr (?E div 2) * 2 powr 1" | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 283 | by (rule mult_strict_left_mono, auto intro: E_mod_pow) | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 284 | also have "\<dots> = 2 powr (?E div 2 + 1)" unfolding add_commute[of _ 1] powr_add[symmetric] | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 285 | by simp | 
| 29805 | 286 | finally show ?thesis by auto | 
| 287 | qed | |
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 288 | finally show ?thesis using `0 < m` | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 289 | unfolding Float | 
| 47600 | 290 | by (subst compute_sqrt_iteration_base) (simp add: ac_simps) | 
| 29805 | 291 | qed | 
| 292 | next | |
| 293 | case (Suc n) | |
| 294 | let ?b = "sqrt_iteration prec n x" | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 295 | have "0 < sqrt x" using `0 < real x` by auto | 
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 296 | also have "\<dots> < real ?b" using Suc . | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 297 | finally have "sqrt x < (?b + x / ?b)/2" using sqrt_ub_pos_pos_1[OF Suc _ `0 < real x`] by auto | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 298 | also have "\<dots> \<le> (?b + (float_divr prec x ?b))/2" by (rule divide_right_mono, auto simp add: float_divr) | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 299 | also have "\<dots> = (Float 1 -1) * (?b + (float_divr prec x ?b))" by simp | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 300 | finally show ?case unfolding sqrt_iteration.simps Let_def right_distrib . | 
| 29805 | 301 | qed | 
| 302 | ||
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 303 | lemma sqrt_iteration_lower_bound: assumes "0 < real x" | 
| 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 304 | shows "0 < real (sqrt_iteration prec n x)" (is "0 < ?sqrt") | 
| 29805 | 305 | proof - | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 306 | have "0 < sqrt x" using assms by auto | 
| 29805 | 307 | also have "\<dots> < ?sqrt" using sqrt_iteration_bound[OF assms] . | 
| 308 | finally show ?thesis . | |
| 309 | qed | |
| 310 | ||
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 311 | lemma lb_sqrt_lower_bound: assumes "0 \<le> real x" | 
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 312 | shows "0 \<le> real (lb_sqrt prec x)" | 
| 29805 | 313 | proof (cases "0 < x") | 
| 47600 | 314 | case True hence "0 < real x" and "0 \<le> x" using `0 \<le> real x` by auto | 
| 315 | hence "0 < sqrt_iteration prec prec x" using sqrt_iteration_lower_bound by auto | |
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 316 | hence "0 \<le> real (float_divl prec x (sqrt_iteration prec prec x))" using float_divl_lower_bound[OF `0 \<le> x`] unfolding less_eq_float_def by auto | 
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 317 | thus ?thesis unfolding lb_sqrt.simps using True by auto | 
| 29805 | 318 | next | 
| 47600 | 319 | case False with `0 \<le> real x` have "real x = 0" by auto | 
| 320 | thus ?thesis unfolding lb_sqrt.simps by auto | |
| 29805 | 321 | qed | 
| 322 | ||
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 323 | lemma bnds_sqrt': | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 324 |   shows "sqrt x \<in> {(lb_sqrt prec x) .. (ub_sqrt prec x) }"
 | 
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 325 | proof - | 
| 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 326 |   { fix x :: float assume "0 < x"
 | 
| 47600 | 327 | hence "0 < real x" and "0 \<le> real x" by auto | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 328 | hence sqrt_gt0: "0 < sqrt x" by auto | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 329 | hence sqrt_ub: "sqrt x < sqrt_iteration prec prec x" using sqrt_iteration_bound by auto | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 330 | |
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 331 | have "(float_divl prec x (sqrt_iteration prec prec x)) \<le> | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 332 | x / (sqrt_iteration prec prec x)" by (rule float_divl) | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 333 | also have "\<dots> < x / sqrt x" | 
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 334 | by (rule divide_strict_left_mono[OF sqrt_ub `0 < real x` | 
| 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 335 | mult_pos_pos[OF order_less_trans[OF sqrt_gt0 sqrt_ub] sqrt_gt0]]) | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 336 | also have "\<dots> = sqrt x" | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 337 | unfolding inverse_eq_iff_eq[of _ "sqrt x", symmetric] | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 338 | sqrt_divide_self_eq[OF `0 \<le> real x`, symmetric] by auto | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 339 | finally have "lb_sqrt prec x \<le> sqrt x" | 
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 340 | unfolding lb_sqrt.simps if_P[OF `0 < x`] by auto } | 
| 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 341 | note lb = this | 
| 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 342 | |
| 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 343 |   { fix x :: float assume "0 < x"
 | 
| 47600 | 344 | hence "0 < real x" by auto | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 345 | hence "0 < sqrt x" by auto | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 346 | hence "sqrt x < sqrt_iteration prec prec x" | 
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 347 | using sqrt_iteration_bound by auto | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 348 | hence "sqrt x \<le> ub_sqrt prec x" | 
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 349 | unfolding ub_sqrt.simps if_P[OF `0 < x`] by auto } | 
| 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 350 | note ub = this | 
| 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 351 | |
| 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 352 | show ?thesis | 
| 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 353 | proof (cases "0 < x") | 
| 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 354 | case True with lb ub show ?thesis by auto | 
| 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 355 | next case False show ?thesis | 
| 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 356 | proof (cases "real x = 0") | 
| 31809 | 357 | case True thus ?thesis | 
| 47600 | 358 | by (auto simp add: lb_sqrt.simps ub_sqrt.simps) | 
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 359 | next | 
| 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 360 | case False with `\<not> 0 < x` have "x < 0" and "0 < -x" | 
| 47600 | 361 | by auto | 
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 362 | |
| 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 363 | with `\<not> 0 < x` | 
| 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 364 | show ?thesis using lb[OF `0 < -x`] ub[OF `0 < -x`] | 
| 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 365 | by (auto simp add: real_sqrt_minus lb_sqrt.simps ub_sqrt.simps) | 
| 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 366 | qed qed | 
| 29805 | 367 | qed | 
| 368 | ||
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 369 | lemma bnds_sqrt: "\<forall> (x::real) lx ux. (l, u) = (lb_sqrt prec lx, ub_sqrt prec ux) \<and> x \<in> {lx .. ux} \<longrightarrow> l \<le> sqrt x \<and> sqrt x \<le> u"
 | 
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 370 | proof ((rule allI) +, rule impI, erule conjE, rule conjI) | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 371 | fix x :: real fix lx ux | 
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 372 | assume "(l, u) = (lb_sqrt prec lx, ub_sqrt prec ux)" | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 373 |     and x: "x \<in> {lx .. ux}"
 | 
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 374 | hence l: "l = lb_sqrt prec lx " and u: "u = ub_sqrt prec ux" by auto | 
| 29805 | 375 | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 376 | have "sqrt lx \<le> sqrt x" using x by auto | 
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 377 | from order_trans[OF _ this] | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 378 | show "l \<le> sqrt x" unfolding l using bnds_sqrt'[of lx prec] by auto | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 379 | |
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 380 | have "sqrt x \<le> sqrt ux" using x by auto | 
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 381 | from order_trans[OF this] | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 382 | show "sqrt x \<le> u" unfolding u using bnds_sqrt'[of ux prec] by auto | 
| 29805 | 383 | qed | 
| 384 | ||
| 385 | section "Arcus tangens and \<pi>" | |
| 386 | ||
| 387 | subsection "Compute arcus tangens series" | |
| 388 | ||
| 389 | text {*
 | |
| 390 | ||
| 391 | As first step we implement the computation of the arcus tangens series. This is only valid in the range | |
| 392 | @{term "{-1 :: real .. 1}"}. This is used to compute \<pi> and then the entire arcus tangens.
 | |
| 393 | ||
| 394 | *} | |
| 395 | ||
| 396 | fun ub_arctan_horner :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> float \<Rightarrow> float" | |
| 397 | and lb_arctan_horner :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> float \<Rightarrow> float" where | |
| 398 | "ub_arctan_horner prec 0 k x = 0" | |
| 31809 | 399 | | "ub_arctan_horner prec (Suc n) k x = | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 400 | (rapprox_rat prec 1 k) - x * (lb_arctan_horner prec n (k + 2) x)" | 
| 29805 | 401 | | "lb_arctan_horner prec 0 k x = 0" | 
| 31809 | 402 | | "lb_arctan_horner prec (Suc n) k x = | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 403 | (lapprox_rat prec 1 k) - x * (ub_arctan_horner prec n (k + 2) x)" | 
| 29805 | 404 | |
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 405 | lemma arctan_0_1_bounds': assumes "0 \<le> real x" "real x \<le> 1" and "even n" | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 406 |   shows "arctan x \<in> {(x * lb_arctan_horner prec n 1 (x * x)) .. (x * ub_arctan_horner prec (Suc n) 1 (x * x))}"
 | 
| 29805 | 407 | proof - | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 408 | let "?c i" = "-1^i * (1 / (i * 2 + (1::nat)) * real x ^ (i * 2 + 1))" | 
| 29805 | 409 | let "?S n" = "\<Sum> i=0..<n. ?c i" | 
| 410 | ||
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 411 | have "0 \<le> real (x * x)" by auto | 
| 29805 | 412 | from `even n` obtain m where "2 * m = n" unfolding even_mult_two_ex by auto | 
| 31809 | 413 | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 414 |   have "arctan x \<in> { ?S n .. ?S (Suc n) }"
 | 
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 415 | proof (cases "real x = 0") | 
| 29805 | 416 | case False | 
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 417 | hence "0 < real x" using `0 \<le> real x` by auto | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 418 | hence prem: "0 < 1 / (0 * 2 + (1::nat)) * real x ^ (0 * 2 + 1)" by auto | 
| 29805 | 419 | |
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 420 | have "\<bar> real x \<bar> \<le> 1" using `0 \<le> real x` `real x \<le> 1` by auto | 
| 29805 | 421 | 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`] | 
| 31790 | 422 | show ?thesis unfolding arctan_series[OF `\<bar> real x \<bar> \<le> 1`] Suc_eq_plus1 . | 
| 29805 | 423 | qed auto | 
| 424 | note arctan_bounds = this[unfolded atLeastAtMost_iff] | |
| 425 | ||
| 426 | have F: "\<And>n. 2 * Suc n + 1 = 2 * n + 1 + 2" by auto | |
| 427 | ||
| 31809 | 428 | note bounds = horner_bounds[where s=1 and f="\<lambda>i. 2 * i + 1" and j'=0 | 
| 29805 | 429 | and lb="\<lambda>n i k x. lb_arctan_horner prec n k x" | 
| 31809 | 430 | and ub="\<lambda>n i k x. ub_arctan_horner prec n k x", | 
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 431 | OF `0 \<le> real (x*x)` F lb_arctan_horner.simps ub_arctan_horner.simps] | 
| 29805 | 432 | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 433 |   { have "(x * lb_arctan_horner prec n 1 (x*x)) \<le> ?S n"
 | 
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 434 | using bounds(1) `0 \<le> real x` | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 435 | unfolding power_add power_one_right mult_assoc[symmetric] setsum_left_distrib[symmetric] | 
| 36778 
739a9379e29b
avoid using real-specific versions of generic lemmas
 huffman parents: 
36531diff
changeset | 436 | unfolding mult_commute[where 'a=real] mult_commute[of _ "2::nat"] power_mult power2_eq_square[of "real x"] | 
| 29805 | 437 | by (auto intro!: mult_left_mono) | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 438 | also have "\<dots> \<le> arctan x" using arctan_bounds .. | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 439 | finally have "(x * lb_arctan_horner prec n 1 (x*x)) \<le> arctan x" . } | 
| 29805 | 440 | moreover | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 441 |   { have "arctan x \<le> ?S (Suc n)" using arctan_bounds ..
 | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 442 | also have "\<dots> \<le> (x * ub_arctan_horner prec (Suc n) 1 (x*x))" | 
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 443 | using bounds(2)[of "Suc n"] `0 \<le> real x` | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 444 | unfolding power_add power_one_right mult_assoc[symmetric] setsum_left_distrib[symmetric] | 
| 36778 
739a9379e29b
avoid using real-specific versions of generic lemmas
 huffman parents: 
36531diff
changeset | 445 | unfolding mult_commute[where 'a=real] mult_commute[of _ "2::nat"] power_mult power2_eq_square[of "real x"] | 
| 29805 | 446 | by (auto intro!: mult_left_mono) | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 447 | finally have "arctan x \<le> (x * ub_arctan_horner prec (Suc n) 1 (x*x))" . } | 
| 29805 | 448 | ultimately show ?thesis by auto | 
| 449 | qed | |
| 450 | ||
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 451 | lemma arctan_0_1_bounds: assumes "0 \<le> real x" "real x \<le> 1" | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 452 |   shows "arctan x \<in> {(x * lb_arctan_horner prec (get_even n) 1 (x * x)) .. (x * ub_arctan_horner prec (get_odd n) 1 (x * x))}"
 | 
| 29805 | 453 | proof (cases "even n") | 
| 454 | case True | |
| 455 | obtain n' where "Suc n' = get_odd n" and "odd (Suc n')" using get_odd_ex by auto | |
| 31148 | 456 | hence "even n'" unfolding even_Suc by auto | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 457 | have "arctan x \<le> x * ub_arctan_horner prec (get_odd n) 1 (x * x)" | 
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 458 | unfolding `Suc n' = get_odd n`[symmetric] using arctan_0_1_bounds'[OF `0 \<le> real x` `real x \<le> 1` `even n'`] by auto | 
| 29805 | 459 | moreover | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 460 | have "x * lb_arctan_horner prec (get_even n) 1 (x * x) \<le> arctan x" | 
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 461 | unfolding get_even_def if_P[OF True] using arctan_0_1_bounds'[OF `0 \<le> real x` `real x \<le> 1` `even n`] by auto | 
| 29805 | 462 | ultimately show ?thesis by auto | 
| 463 | next | |
| 464 | case False hence "0 < n" by (rule odd_pos) | |
| 465 | from gr0_implies_Suc[OF this] obtain n' where "n = Suc n'" .. | |
| 31148 | 466 | from False[unfolded this even_Suc] | 
| 29805 | 467 | have "even n'" and "even (Suc (Suc n'))" by auto | 
| 468 | have "get_odd n = Suc n'" unfolding get_odd_def if_P[OF False] using `n = Suc n'` . | |
| 469 | ||
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 470 | have "arctan x \<le> x * ub_arctan_horner prec (get_odd n) 1 (x * x)" | 
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 471 | unfolding `get_odd n = Suc n'` using arctan_0_1_bounds'[OF `0 \<le> real x` `real x \<le> 1` `even n'`] by auto | 
| 29805 | 472 | moreover | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 473 | have "(x * lb_arctan_horner prec (get_even n) 1 (x * x)) \<le> arctan x" | 
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 474 | unfolding get_even_def if_not_P[OF False] unfolding `n = Suc n'` using arctan_0_1_bounds'[OF `0 \<le> real x` `real x \<le> 1` `even (Suc (Suc n'))`] by auto | 
| 29805 | 475 | ultimately show ?thesis by auto | 
| 476 | qed | |
| 477 | ||
| 478 | subsection "Compute \<pi>" | |
| 479 | ||
| 480 | definition ub_pi :: "nat \<Rightarrow> float" where | |
| 31809 | 481 | "ub_pi prec = (let A = rapprox_rat prec 1 5 ; | 
| 29805 | 482 | B = lapprox_rat prec 1 239 | 
| 31809 | 483 | in ((Float 1 2) * ((Float 1 2) * A * (ub_arctan_horner prec (get_odd (prec div 4 + 1)) 1 (A * A)) - | 
| 29805 | 484 | B * (lb_arctan_horner prec (get_even (prec div 14 + 1)) 1 (B * B)))))" | 
| 485 | ||
| 486 | definition lb_pi :: "nat \<Rightarrow> float" where | |
| 31809 | 487 | "lb_pi prec = (let A = lapprox_rat prec 1 5 ; | 
| 29805 | 488 | B = rapprox_rat prec 1 239 | 
| 31809 | 489 | in ((Float 1 2) * ((Float 1 2) * A * (lb_arctan_horner prec (get_even (prec div 4 + 1)) 1 (A * A)) - | 
| 29805 | 490 | B * (ub_arctan_horner prec (get_odd (prec div 14 + 1)) 1 (B * B)))))" | 
| 491 | ||
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 492 | lemma pi_boundaries: "pi \<in> {(lb_pi n) .. (ub_pi n)}"
 | 
| 29805 | 493 | proof - | 
| 494 | have machin_pi: "pi = 4 * (4 * arctan (1 / 5) - arctan (1 / 239))" unfolding machin[symmetric] by auto | |
| 495 | ||
| 496 |   { fix prec n :: nat fix k :: int assume "1 < k" hence "0 \<le> k" and "0 < k" and "1 \<le> k" by auto
 | |
| 497 | let ?k = "rapprox_rat prec 1 k" | |
| 498 | have "1 div k = 0" using div_pos_pos_trivial[OF _ `1 < k`] by auto | |
| 31809 | 499 | |
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 500 | have "0 \<le> real ?k" by (rule order_trans[OF _ rapprox_rat], auto simp add: `0 \<le> k`) | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 501 | have "real ?k \<le> 1" | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 502 | by (rule rapprox_rat_le1, auto simp add: `0 < k` `1 \<le> k`) | 
| 29805 | 503 | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 504 | have "1 / k \<le> ?k" using rapprox_rat[where x=1 and y=k] by auto | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 505 | hence "arctan (1 / k) \<le> arctan ?k" by (rule arctan_monotone') | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 506 | also have "\<dots> \<le> (?k * ub_arctan_horner prec (get_odd n) 1 (?k * ?k))" | 
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 507 | using arctan_0_1_bounds[OF `0 \<le> real ?k` `real ?k \<le> 1`] by auto | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 508 | finally have "arctan (1 / k) \<le> ?k * ub_arctan_horner prec (get_odd n) 1 (?k * ?k)" . | 
| 29805 | 509 | } note ub_arctan = this | 
| 510 | ||
| 511 |   { fix prec n :: nat fix k :: int assume "1 < k" hence "0 \<le> k" and "0 < k" by auto
 | |
| 512 | let ?k = "lapprox_rat prec 1 k" | |
| 513 | have "1 div k = 0" using div_pos_pos_trivial[OF _ `1 < k`] by auto | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 514 | have "1 / k \<le> 1" using `1 < k` by auto | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 515 | have "\<And>n. 0 \<le> real ?k" using lapprox_rat_nonneg[where x=1 and y=k, OF zero_le_one `0 < k`] by (auto simp add: `1 div k = 0`) | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 516 | have "\<And>n. real ?k \<le> 1" using lapprox_rat by (rule order_trans, auto simp add: `1 / k \<le> 1`) | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 517 | |
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 518 | have "?k \<le> 1 / k" using lapprox_rat[where x=1 and y=k] by auto | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 519 | |
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 520 | have "?k * lb_arctan_horner prec (get_even n) 1 (?k * ?k) \<le> arctan ?k" | 
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 521 | using arctan_0_1_bounds[OF `0 \<le> real ?k` `real ?k \<le> 1`] by auto | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 522 | also have "\<dots> \<le> arctan (1 / k)" using `?k \<le> 1 / k` by (rule arctan_monotone') | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 523 | finally have "?k * lb_arctan_horner prec (get_even n) 1 (?k * ?k) \<le> arctan (1 / k)" . | 
| 29805 | 524 | } note lb_arctan = this | 
| 525 | ||
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 526 | have "pi \<le> ub_pi n" | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 527 | unfolding ub_pi_def machin_pi Let_def unfolding Float_num | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 528 | using lb_arctan[of 239] ub_arctan[of 5] powr_realpow[of 2 2] | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 529 | by (auto intro!: mult_left_mono add_mono simp add: diff_minus) | 
| 29805 | 530 | moreover | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 531 | have "lb_pi n \<le> pi" | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 532 | unfolding lb_pi_def machin_pi Let_def Float_num | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 533 | using lb_arctan[of 5] ub_arctan[of 239] powr_realpow[of 2 2] | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 534 | by (auto intro!: mult_left_mono add_mono simp add: diff_minus) | 
| 29805 | 535 | ultimately show ?thesis by auto | 
| 536 | qed | |
| 537 | ||
| 538 | subsection "Compute arcus tangens in the entire domain" | |
| 539 | ||
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 540 | function lb_arctan :: "nat \<Rightarrow> float \<Rightarrow> float" and ub_arctan :: "nat \<Rightarrow> float \<Rightarrow> float" where | 
| 29805 | 541 | "lb_arctan prec x = (let ub_horner = \<lambda> x. x * ub_arctan_horner prec (get_odd (prec div 4 + 1)) 1 (x * x) ; | 
| 542 | lb_horner = \<lambda> x. x * lb_arctan_horner prec (get_even (prec div 4 + 1)) 1 (x * x) | |
| 543 | in (if x < 0 then - ub_arctan prec (-x) else | |
| 544 | if x \<le> Float 1 -1 then lb_horner x else | |
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 545 | if x \<le> Float 1 1 then Float 1 1 * lb_horner (float_divl prec x (1 + ub_sqrt prec (1 + x * x))) | 
| 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 546 | else (let inv = float_divr prec 1 x | 
| 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 547 | in if inv > 1 then 0 | 
| 29805 | 548 | else lb_pi prec * Float 1 -1 - ub_horner inv)))" | 
| 549 | ||
| 550 | | "ub_arctan prec x = (let lb_horner = \<lambda> x. x * lb_arctan_horner prec (get_even (prec div 4 + 1)) 1 (x * x) ; | |
| 551 | ub_horner = \<lambda> x. x * ub_arctan_horner prec (get_odd (prec div 4 + 1)) 1 (x * x) | |
| 552 | in (if x < 0 then - lb_arctan prec (-x) else | |
| 553 | if x \<le> Float 1 -1 then ub_horner x else | |
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 554 | if x \<le> Float 1 1 then let y = float_divr prec x (1 + lb_sqrt prec (1 + x * x)) | 
| 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 555 | in if y > 1 then ub_pi prec * Float 1 -1 | 
| 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 556 | else Float 1 1 * ub_horner y | 
| 29805 | 557 | else ub_pi prec * Float 1 -1 - lb_horner (float_divl prec 1 x)))" | 
| 558 | by pat_completeness auto | |
| 47600 | 559 | termination by (relation "measure (\<lambda> v. let (prec, x) = sum_case id id v in (if x < 0 then 1 else 0))", auto) | 
| 29805 | 560 | |
| 561 | declare ub_arctan_horner.simps[simp del] | |
| 562 | declare lb_arctan_horner.simps[simp del] | |
| 563 | ||
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 564 | lemma lb_arctan_bound': assumes "0 \<le> real x" | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 565 | shows "lb_arctan prec x \<le> arctan x" | 
| 29805 | 566 | proof - | 
| 47600 | 567 | have "\<not> x < 0" and "0 \<le> x" using `0 \<le> real x` by auto | 
| 29805 | 568 | let "?ub_horner x" = "x * ub_arctan_horner prec (get_odd (prec div 4 + 1)) 1 (x * x)" | 
| 569 | and "?lb_horner x" = "x * lb_arctan_horner prec (get_even (prec div 4 + 1)) 1 (x * x)" | |
| 570 | ||
| 571 | show ?thesis | |
| 572 | proof (cases "x \<le> Float 1 -1") | |
| 47600 | 573 | case True hence "real x \<le> 1" by auto | 
| 29805 | 574 | show ?thesis unfolding lb_arctan.simps Let_def if_not_P[OF `\<not> x < 0`] if_P[OF True] | 
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 575 | using arctan_0_1_bounds[OF `0 \<le> real x` `real x \<le> 1`] by auto | 
| 29805 | 576 | next | 
| 47600 | 577 | case False hence "0 < real x" by auto | 
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 578 | let ?R = "1 + sqrt (1 + real x * real x)" | 
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 579 | let ?fR = "1 + ub_sqrt prec (1 + x * x)" | 
| 29805 | 580 | let ?DIV = "float_divl prec x ?fR" | 
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 581 | |
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 582 | have sqr_ge0: "0 \<le> 1 + real x * real x" using sum_power2_ge_zero[of 1 "real x", unfolded numeral_2_eq_2] by auto | 
| 29805 | 583 | hence divisor_gt0: "0 < ?R" by (auto intro: add_pos_nonneg) | 
| 584 | ||
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 585 | have "sqrt (1 + x * x) \<le> ub_sqrt prec (1 + x * x)" | 
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 586 | using bnds_sqrt'[of "1 + x * x"] by auto | 
| 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 587 | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 588 | hence "?R \<le> ?fR" by auto | 
| 47600 | 589 | hence "0 < ?fR" and "0 < real ?fR" using `0 < ?R` by auto | 
| 29805 | 590 | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 591 | have monotone: "(float_divl prec x ?fR) \<le> x / ?R" | 
| 29805 | 592 | proof - | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 593 | have "?DIV \<le> real x / ?fR" by (rule float_divl) | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 594 | also have "\<dots> \<le> x / ?R" by (rule divide_left_mono[OF `?R \<le> ?fR` `0 \<le> real x` mult_pos_pos[OF order_less_le_trans[OF divisor_gt0 `?R \<le> real ?fR`] divisor_gt0]]) | 
| 29805 | 595 | finally show ?thesis . | 
| 596 | qed | |
| 597 | ||
| 598 | show ?thesis | |
| 599 | proof (cases "x \<le> Float 1 1") | |
| 600 | case True | |
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 601 | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 602 | have "x \<le> sqrt (1 + x * x)" using real_sqrt_sum_squares_ge2[where x=1, unfolded numeral_2_eq_2] by auto | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 603 | also have "\<dots> \<le> (ub_sqrt prec (1 + x * x))" | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 604 | using bnds_sqrt'[of "1 + x * x"] by auto | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 605 | finally have "real x \<le> ?fR" by auto | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 606 | moreover have "?DIV \<le> real x / ?fR" by (rule float_divl) | 
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 607 | ultimately have "real ?DIV \<le> 1" unfolding divide_le_eq_1_pos[OF `0 < real ?fR`, symmetric] by auto | 
| 29805 | 608 | |
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 609 | have "0 \<le> real ?DIV" using float_divl_lower_bound[OF `0 \<le> x` `0 < ?fR`] unfolding less_eq_float_def by auto | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 610 | |
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 611 | have "(Float 1 1 * ?lb_horner ?DIV) \<le> 2 * arctan (float_divl prec x ?fR)" | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 612 | using arctan_0_1_bounds[OF `0 \<le> real ?DIV` `real ?DIV \<le> 1`] by auto | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 613 | also have "\<dots> \<le> 2 * arctan (x / ?R)" | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 614 | using arctan_monotone'[OF monotone] by (auto intro!: mult_left_mono) | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 615 | also have "2 * arctan (x / ?R) = arctan x" using arctan_half[symmetric] unfolding numeral_2_eq_2 power_Suc2 power_0 mult_1_left . | 
| 29805 | 616 | finally show ?thesis unfolding lb_arctan.simps Let_def if_not_P[OF `\<not> x < 0`] if_not_P[OF `\<not> x \<le> Float 1 -1`] if_P[OF True] . | 
| 617 | next | |
| 618 | case False | |
| 47600 | 619 | hence "2 < real x" by auto | 
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 620 | hence "1 \<le> real x" by auto | 
| 29805 | 621 | |
| 622 | let "?invx" = "float_divr prec 1 x" | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 623 | have "0 \<le> arctan x" using arctan_monotone'[OF `0 \<le> real x`] using arctan_tan[of 0, unfolded tan_zero] by auto | 
| 29805 | 624 | |
| 625 | show ?thesis | |
| 626 | proof (cases "1 < ?invx") | |
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 627 | case True | 
| 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 628 | show ?thesis unfolding lb_arctan.simps Let_def if_not_P[OF `\<not> x < 0`] if_not_P[OF `\<not> x \<le> Float 1 -1`] if_not_P[OF False] if_P[OF True] | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 629 | using `0 \<le> arctan x` by auto | 
| 29805 | 630 | next | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 631 | case False | 
| 47600 | 632 | hence "real ?invx \<le> 1" by auto | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 633 | have "0 \<le> real ?invx" by (rule order_trans[OF _ float_divr], auto simp add: `0 \<le> real x`) | 
| 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 634 | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 635 | have "1 / x \<noteq> 0" and "0 < 1 / x" using `0 < real x` by auto | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 636 | |
| 47601 
050718fe6eee
use real :: float => real as lifting-morphism so we can directlry use the rep_eq theorems
 hoelzl parents: 
47600diff
changeset | 637 | have "arctan (1 / x) \<le> arctan ?invx" unfolding one_float.rep_eq[symmetric] by (rule arctan_monotone', rule float_divr) | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 638 | also have "\<dots> \<le> (?ub_horner ?invx)" using arctan_0_1_bounds[OF `0 \<le> real ?invx` `real ?invx \<le> 1`] by auto | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 639 | finally have "pi / 2 - (?ub_horner ?invx) \<le> arctan x" | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 640 | using `0 \<le> arctan x` arctan_inverse[OF `1 / x \<noteq> 0`] | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 641 | unfolding real_sgn_pos[OF `0 < 1 / real x`] le_diff_eq by auto | 
| 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 642 | moreover | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 643 | have "lb_pi prec * Float 1 -1 \<le> pi / 2" | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 644 | unfolding Float_num times_divide_eq_right mult_1_left using pi_boundaries by simp | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 645 | ultimately | 
| 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 646 | show ?thesis unfolding lb_arctan.simps Let_def if_not_P[OF `\<not> x < 0`] if_not_P[OF `\<not> x \<le> Float 1 -1`] if_not_P[OF `\<not> x \<le> Float 1 1`] if_not_P[OF False] | 
| 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 647 | by auto | 
| 29805 | 648 | qed | 
| 649 | qed | |
| 650 | qed | |
| 651 | qed | |
| 652 | ||
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 653 | lemma ub_arctan_bound': assumes "0 \<le> real x" | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 654 | shows "arctan x \<le> ub_arctan prec x" | 
| 29805 | 655 | proof - | 
| 47600 | 656 | have "\<not> x < 0" and "0 \<le> x" using `0 \<le> real x` by auto | 
| 29805 | 657 | |
| 658 | let "?ub_horner x" = "x * ub_arctan_horner prec (get_odd (prec div 4 + 1)) 1 (x * x)" | |
| 659 | and "?lb_horner x" = "x * lb_arctan_horner prec (get_even (prec div 4 + 1)) 1 (x * x)" | |
| 660 | ||
| 661 | show ?thesis | |
| 662 | proof (cases "x \<le> Float 1 -1") | |
| 47600 | 663 | case True hence "real x \<le> 1" by auto | 
| 29805 | 664 | show ?thesis unfolding ub_arctan.simps Let_def if_not_P[OF `\<not> x < 0`] if_P[OF True] | 
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 665 | using arctan_0_1_bounds[OF `0 \<le> real x` `real x \<le> 1`] by auto | 
| 29805 | 666 | next | 
| 47600 | 667 | case False hence "0 < real x" by auto | 
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 668 | let ?R = "1 + sqrt (1 + real x * real x)" | 
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 669 | let ?fR = "1 + lb_sqrt prec (1 + x * x)" | 
| 29805 | 670 | let ?DIV = "float_divr prec x ?fR" | 
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 671 | |
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 672 | have sqr_ge0: "0 \<le> 1 + real x * real x" using sum_power2_ge_zero[of 1 "real x", unfolded numeral_2_eq_2] by auto | 
| 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 673 | hence "0 \<le> real (1 + x*x)" by auto | 
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 674 | |
| 29805 | 675 | hence divisor_gt0: "0 < ?R" by (auto intro: add_pos_nonneg) | 
| 676 | ||
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 677 | have "lb_sqrt prec (1 + x * x) \<le> sqrt (1 + x * x)" | 
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 678 | using bnds_sqrt'[of "1 + x * x"] by auto | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 679 | hence "?fR \<le> ?R" by auto | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 680 | have "0 < real ?fR" by (rule order_less_le_trans[OF zero_less_one], auto simp add: lb_sqrt_lower_bound[OF `0 \<le> real (1 + x*x)`]) | 
| 29805 | 681 | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 682 | have monotone: "x / ?R \<le> (float_divr prec x ?fR)" | 
| 29805 | 683 | proof - | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 684 | from divide_left_mono[OF `?fR \<le> ?R` `0 \<le> real x` mult_pos_pos[OF divisor_gt0 `0 < real ?fR`]] | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 685 | have "x / ?R \<le> x / ?fR" . | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 686 | also have "\<dots> \<le> ?DIV" by (rule float_divr) | 
| 29805 | 687 | finally show ?thesis . | 
| 688 | qed | |
| 689 | ||
| 690 | show ?thesis | |
| 691 | proof (cases "x \<le> Float 1 1") | |
| 692 | case True | |
| 693 | show ?thesis | |
| 694 | proof (cases "?DIV > 1") | |
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 695 | case True | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 696 | have "pi / 2 \<le> ub_pi prec * Float 1 -1" unfolding Float_num times_divide_eq_right mult_1_left using pi_boundaries by auto | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 697 | from order_less_le_trans[OF arctan_ubound this, THEN less_imp_le] | 
| 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 698 | show ?thesis unfolding ub_arctan.simps Let_def if_not_P[OF `\<not> x < 0`] if_not_P[OF `\<not> x \<le> Float 1 -1`] if_P[OF `x \<le> Float 1 1`] if_P[OF True] . | 
| 29805 | 699 | next | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 700 | case False | 
| 47600 | 701 | hence "real ?DIV \<le> 1" by auto | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 702 | |
| 44349 
f057535311c5
remove redundant lemma real_0_le_divide_iff in favor or zero_le_divide_iff
 huffman parents: 
44306diff
changeset | 703 | have "0 \<le> x / ?R" using `0 \<le> real x` `0 < ?R` unfolding zero_le_divide_iff by auto | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 704 | hence "0 \<le> real ?DIV" using monotone by (rule order_trans) | 
| 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 705 | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 706 | have "arctan x = 2 * arctan (x / ?R)" using arctan_half unfolding numeral_2_eq_2 power_Suc2 power_0 mult_1_left . | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 707 | also have "\<dots> \<le> 2 * arctan (?DIV)" | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 708 | using arctan_monotone'[OF monotone] by (auto intro!: mult_left_mono) | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 709 | also have "\<dots> \<le> (Float 1 1 * ?ub_horner ?DIV)" unfolding Float_num | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 710 | using arctan_0_1_bounds[OF `0 \<le> real ?DIV` `real ?DIV \<le> 1`] by auto | 
| 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 711 | finally show ?thesis unfolding ub_arctan.simps Let_def if_not_P[OF `\<not> x < 0`] if_not_P[OF `\<not> x \<le> Float 1 -1`] if_P[OF `x \<le> Float 1 1`] if_not_P[OF False] . | 
| 29805 | 712 | qed | 
| 713 | next | |
| 714 | case False | |
| 47600 | 715 | hence "2 < real x" by auto | 
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 716 | hence "1 \<le> real x" by auto | 
| 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 717 | hence "0 < real x" by auto | 
| 47600 | 718 | hence "0 < x" by auto | 
| 29805 | 719 | |
| 720 | let "?invx" = "float_divl prec 1 x" | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 721 | have "0 \<le> arctan x" using arctan_monotone'[OF `0 \<le> real x`] using arctan_tan[of 0, unfolded tan_zero] by auto | 
| 29805 | 722 | |
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 723 | have "real ?invx \<le> 1" unfolding less_float_def by (rule order_trans[OF float_divl], auto simp add: `1 \<le> real x` divide_le_eq_1_pos[OF `0 < real x`]) | 
| 47600 | 724 | have "0 \<le> real ?invx" using `0 < x` by (intro float_divl_lower_bound) auto | 
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 725 | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 726 | have "1 / x \<noteq> 0" and "0 < 1 / x" using `0 < real x` by auto | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 727 | |
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 728 | have "(?lb_horner ?invx) \<le> arctan (?invx)" using arctan_0_1_bounds[OF `0 \<le> real ?invx` `real ?invx \<le> 1`] by auto | 
| 47601 
050718fe6eee
use real :: float => real as lifting-morphism so we can directlry use the rep_eq theorems
 hoelzl parents: 
47600diff
changeset | 729 | also have "\<dots> \<le> arctan (1 / x)" unfolding one_float.rep_eq[symmetric] by (rule arctan_monotone', rule float_divl) | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 730 | finally have "arctan x \<le> pi / 2 - (?lb_horner ?invx)" | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 731 | using `0 \<le> arctan x` arctan_inverse[OF `1 / x \<noteq> 0`] | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 732 | unfolding real_sgn_pos[OF `0 < 1 / x`] le_diff_eq by auto | 
| 29805 | 733 | moreover | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 734 | have "pi / 2 \<le> ub_pi prec * Float 1 -1" unfolding Float_num times_divide_eq_right mult_1_right using pi_boundaries by auto | 
| 29805 | 735 | ultimately | 
| 46545 | 736 | show ?thesis unfolding ub_arctan.simps Let_def if_not_P[OF `\<not> x < 0`]if_not_P[OF `\<not> x \<le> Float 1 -1`] if_not_P[OF False] | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 737 | by auto | 
| 29805 | 738 | qed | 
| 739 | qed | |
| 740 | qed | |
| 741 | ||
| 742 | lemma arctan_boundaries: | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 743 |   "arctan x \<in> {(lb_arctan prec x) .. (ub_arctan prec x)}"
 | 
| 29805 | 744 | proof (cases "0 \<le> x") | 
| 47600 | 745 | case True hence "0 \<le> real x" by auto | 
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 746 | show ?thesis using ub_arctan_bound'[OF `0 \<le> real x`] lb_arctan_bound'[OF `0 \<le> real x`] unfolding atLeastAtMost_iff by auto | 
| 29805 | 747 | next | 
| 748 | let ?mx = "-x" | |
| 47600 | 749 | case False hence "x < 0" and "0 \<le> real ?mx" by auto | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 750 | hence bounds: "lb_arctan prec ?mx \<le> arctan ?mx \<and> arctan ?mx \<le> ub_arctan prec ?mx" | 
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 751 | using ub_arctan_bound'[OF `0 \<le> real ?mx`] lb_arctan_bound'[OF `0 \<le> real ?mx`] by auto | 
| 47601 
050718fe6eee
use real :: float => real as lifting-morphism so we can directlry use the rep_eq theorems
 hoelzl parents: 
47600diff
changeset | 752 | show ?thesis unfolding minus_float.rep_eq arctan_minus lb_arctan.simps[where x=x] ub_arctan.simps[where x=x] Let_def if_P[OF `x < 0`] | 
| 
050718fe6eee
use real :: float => real as lifting-morphism so we can directlry use the rep_eq theorems
 hoelzl parents: 
47600diff
changeset | 753 | unfolding atLeastAtMost_iff using bounds[unfolded minus_float.rep_eq arctan_minus] | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 754 | by (simp add: arctan_minus) | 
| 29805 | 755 | qed | 
| 756 | ||
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 757 | lemma bnds_arctan: "\<forall> (x::real) lx ux. (l, u) = (lb_arctan prec lx, ub_arctan prec ux) \<and> x \<in> {lx .. ux} \<longrightarrow> l \<le> arctan x \<and> arctan x \<le> u"
 | 
| 29805 | 758 | proof (rule allI, rule allI, rule allI, rule impI) | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 759 | fix x :: real fix lx ux | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 760 |   assume "(l, u) = (lb_arctan prec lx, ub_arctan prec ux) \<and> x \<in> {lx .. ux}"
 | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 761 |   hence l: "lb_arctan prec lx = l " and u: "ub_arctan prec ux = u" and x: "x \<in> {lx .. ux}" by auto
 | 
| 29805 | 762 | |
| 763 |   { from arctan_boundaries[of lx prec, unfolded l]
 | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 764 | have "l \<le> arctan lx" by (auto simp del: lb_arctan.simps) | 
| 29805 | 765 | also have "\<dots> \<le> arctan x" using x by (auto intro: arctan_monotone') | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 766 | finally have "l \<le> arctan x" . | 
| 29805 | 767 | } moreover | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 768 |   { have "arctan x \<le> arctan ux" using x by (auto intro: arctan_monotone')
 | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 769 | also have "\<dots> \<le> u" using arctan_boundaries[of ux prec, unfolded u] by (auto simp del: ub_arctan.simps) | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 770 | finally have "arctan x \<le> u" . | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 771 | } ultimately show "l \<le> arctan x \<and> arctan x \<le> u" .. | 
| 29805 | 772 | qed | 
| 773 | ||
| 774 | section "Sinus and Cosinus" | |
| 775 | ||
| 776 | subsection "Compute the cosinus and sinus series" | |
| 777 | ||
| 778 | fun ub_sin_cos_aux :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> float \<Rightarrow> float" | |
| 779 | and lb_sin_cos_aux :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> float \<Rightarrow> float" where | |
| 780 | "ub_sin_cos_aux prec 0 i k x = 0" | |
| 31809 | 781 | | "ub_sin_cos_aux prec (Suc n) i k x = | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 782 | (rapprox_rat prec 1 k) - x * (lb_sin_cos_aux prec n (i + 2) (k * i * (i + 1)) x)" | 
| 29805 | 783 | | "lb_sin_cos_aux prec 0 i k x = 0" | 
| 31809 | 784 | | "lb_sin_cos_aux prec (Suc n) i k x = | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 785 | (lapprox_rat prec 1 k) - x * (ub_sin_cos_aux prec n (i + 2) (k * i * (i + 1)) x)" | 
| 47601 
050718fe6eee
use real :: float => real as lifting-morphism so we can directlry use the rep_eq theorems
 hoelzl parents: 
47600diff
changeset | 786 | |
| 29805 | 787 | lemma cos_aux: | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 788 | shows "(lb_sin_cos_aux prec n 1 1 (x * x)) \<le> (\<Sum> i=0..<n. -1^i * (1/real (fact (2 * i))) * x ^(2 * i))" (is "?lb") | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 789 | and "(\<Sum> i=0..<n. -1^i * (1/real (fact (2 * i))) * x^(2 * i)) \<le> (ub_sin_cos_aux prec n 1 1 (x * x))" (is "?ub") | 
| 29805 | 790 | proof - | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 791 | have "0 \<le> real (x * x)" by auto | 
| 29805 | 792 | let "?f n" = "fact (2 * n)" | 
| 793 | ||
| 31809 | 794 |   { fix n
 | 
| 45129 
1fce03e3e8ad
tuned proofs -- eliminated vacuous "induct arbitrary: ..." situations;
 wenzelm parents: 
44821diff
changeset | 795 | have F: "\<And>m. ((\<lambda>i. i + 2) ^^ n) m = m + 2 * n" by (induct n) auto | 
| 30971 | 796 | have "?f (Suc n) = ?f n * ((\<lambda>i. i + 2) ^^ n) 1 * (((\<lambda>i. i + 2) ^^ n) 1 + 1)" | 
| 29805 | 797 | unfolding F by auto } note f_eq = this | 
| 31809 | 798 | |
| 799 | from horner_bounds[where lb="lb_sin_cos_aux prec" and ub="ub_sin_cos_aux prec" and j'=0, | |
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 800 | OF `0 \<le> real (x * x)` f_eq lb_sin_cos_aux.simps ub_sin_cos_aux.simps] | 
| 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 801 | show "?lb" and "?ub" by (auto simp add: power_mult power2_eq_square[of "real x"]) | 
| 29805 | 802 | qed | 
| 803 | ||
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 804 | lemma cos_boundaries: assumes "0 \<le> real x" and "x \<le> pi / 2" | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 805 |   shows "cos x \<in> {(lb_sin_cos_aux prec (get_even n) 1 1 (x * x)) .. (ub_sin_cos_aux prec (get_odd n) 1 1 (x * x))}"
 | 
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 806 | proof (cases "real x = 0") | 
| 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 807 | case False hence "real x \<noteq> 0" by auto | 
| 47600 | 808 | hence "0 < x" and "0 < real x" using `0 \<le> real x` by auto | 
| 809 | have "0 < x * x" using `0 < x` | |
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 810 | using mult_pos_pos[where a="real x" and b="real x"] by auto | 
| 29805 | 811 | |
| 30952 
7ab2716dd93b
power operation on functions with syntax o^; power operation on relations with syntax ^^
 haftmann parents: 
30886diff
changeset | 812 |   { fix x n have "(\<Sum> i=0..<n. -1^i * (1/real (fact (2 * i))) * x ^ (2 * i))
 | 
| 29805 | 813 | = (\<Sum> i = 0 ..< 2 * n. (if even(i) then (-1 ^ (i div 2))/(real (fact i)) else 0) * x ^ i)" (is "?sum = ?ifsum") | 
| 814 | proof - | |
| 815 | have "?sum = ?sum + (\<Sum> j = 0 ..< n. 0)" by auto | |
| 31809 | 816 | also have "\<dots> = | 
| 29805 | 817 | (\<Sum> j = 0 ..< n. -1 ^ ((2 * j) div 2) / (real (fact (2 * j))) * x ^(2 * j)) + (\<Sum> j = 0 ..< n. 0)" by auto | 
| 818 | also have "\<dots> = (\<Sum> i = 0 ..< 2 * n. if even i then -1 ^ (i div 2) / (real (fact i)) * x ^ i else 0)" | |
| 819 | unfolding sum_split_even_odd .. | |
| 820 | also have "\<dots> = (\<Sum> i = 0 ..< 2 * n. (if even i then -1 ^ (i div 2) / (real (fact i)) else 0) * x ^ i)" | |
| 821 | by (rule setsum_cong2) auto | |
| 822 | finally show ?thesis by assumption | |
| 823 | qed } note morph_to_if_power = this | |
| 824 | ||
| 825 | ||
| 826 |   { fix n :: nat assume "0 < n"
 | |
| 827 | hence "0 < 2 * n" by auto | |
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 828 | obtain t where "0 < t" and "t < real x" and | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 829 | cos_eq: "cos x = (\<Sum> i = 0 ..< 2 * n. (if even(i) then (-1 ^ (i div 2))/(real (fact i)) else 0) * (real x) ^ i) | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 830 | + (cos (t + 1/2 * (2 * n) * pi) / real (fact (2*n))) * (real x)^(2*n)" | 
| 29805 | 831 | (is "_ = ?SUM + ?rest / ?fact * ?pow") | 
| 44306 
33572a766836
fold definitions of sin_coeff and cos_coeff in Maclaurin lemmas
 huffman parents: 
44305diff
changeset | 832 | using Maclaurin_cos_expansion2[OF `0 < real x` `0 < 2 * n`] | 
| 
33572a766836
fold definitions of sin_coeff and cos_coeff in Maclaurin lemmas
 huffman parents: 
44305diff
changeset | 833 | unfolding cos_coeff_def by auto | 
| 29805 | 834 | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 835 | have "cos t * -1^n = cos t * cos (n * pi) + sin t * sin (n * pi)" by auto | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 836 | also have "\<dots> = cos (t + n * pi)" using cos_add by auto | 
| 29805 | 837 | also have "\<dots> = ?rest" by auto | 
| 838 | finally have "cos t * -1^n = ?rest" . | |
| 839 | moreover | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 840 | have "t \<le> pi / 2" using `t < real x` and `x \<le> pi / 2` by auto | 
| 29805 | 841 | hence "0 \<le> cos t" using `0 < t` and cos_ge_zero by auto | 
| 842 | ultimately have even: "even n \<Longrightarrow> 0 \<le> ?rest" and odd: "odd n \<Longrightarrow> 0 \<le> - ?rest " by auto | |
| 843 | ||
| 844 | have "0 < ?fact" by auto | |
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 845 | have "0 < ?pow" using `0 < real x` by auto | 
| 29805 | 846 | |
| 847 |     {
 | |
| 848 | assume "even n" | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 849 | have "(lb_sin_cos_aux prec n 1 1 (x * x)) \<le> ?SUM" | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 850 | unfolding morph_to_if_power[symmetric] using cos_aux by auto | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 851 | also have "\<dots> \<le> cos x" | 
| 29805 | 852 | proof - | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 853 | from even[OF `even n`] `0 < ?fact` `0 < ?pow` | 
| 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 854 | have "0 \<le> (?rest / ?fact) * ?pow" by (metis mult_nonneg_nonneg divide_nonneg_pos less_imp_le) | 
| 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 855 | thus ?thesis unfolding cos_eq by auto | 
| 29805 | 856 | qed | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 857 | finally have "(lb_sin_cos_aux prec n 1 1 (x * x)) \<le> cos x" . | 
| 29805 | 858 | } note lb = this | 
| 859 | ||
| 860 |     {
 | |
| 861 | assume "odd n" | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 862 | have "cos x \<le> ?SUM" | 
| 29805 | 863 | proof - | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 864 | from `0 < ?fact` and `0 < ?pow` and odd[OF `odd n`] | 
| 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 865 | have "0 \<le> (- ?rest) / ?fact * ?pow" | 
| 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 866 | by (metis mult_nonneg_nonneg divide_nonneg_pos less_imp_le) | 
| 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 867 | thus ?thesis unfolding cos_eq by auto | 
| 29805 | 868 | qed | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 869 | also have "\<dots> \<le> (ub_sin_cos_aux prec n 1 1 (x * x))" | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 870 | unfolding morph_to_if_power[symmetric] using cos_aux by auto | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 871 | finally have "cos x \<le> (ub_sin_cos_aux prec n 1 1 (x * x))" . | 
| 29805 | 872 | } note ub = this and lb | 
| 873 | } note ub = this(1) and lb = this(2) | |
| 874 | ||
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 875 | have "cos x \<le> (ub_sin_cos_aux prec (get_odd n) 1 1 (x * x))" using ub[OF odd_pos[OF get_odd] get_odd] . | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 876 | moreover have "(lb_sin_cos_aux prec (get_even n) 1 1 (x * x)) \<le> cos x" | 
| 29805 | 877 | proof (cases "0 < get_even n") | 
| 878 | case True show ?thesis using lb[OF True get_even] . | |
| 879 | next | |
| 880 | case False | |
| 881 | hence "get_even n = 0" by auto | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 882 | have "- (pi / 2) \<le> x" by (rule order_trans[OF _ `0 < real x`[THEN less_imp_le]], auto) | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 883 | with `x \<le> pi / 2` | 
| 47601 
050718fe6eee
use real :: float => real as lifting-morphism so we can directlry use the rep_eq theorems
 hoelzl parents: 
47600diff
changeset | 884 | show ?thesis unfolding `get_even n = 0` lb_sin_cos_aux.simps minus_float.rep_eq zero_float.rep_eq using cos_ge_zero by auto | 
| 29805 | 885 | qed | 
| 886 | ultimately show ?thesis by auto | |
| 887 | next | |
| 888 | case True | |
| 889 | show ?thesis | |
| 890 | proof (cases "n = 0") | |
| 31809 | 891 | case True | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 892 | thus ?thesis unfolding `n = 0` get_even_def get_odd_def | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 893 | using `real x = 0` lapprox_rat[where x="-1" and y=1] | 
| 47621 
4cf6011fb884
hide code generation facts in the Float theory, they are only exported for Approximation
 hoelzl parents: 
47601diff
changeset | 894 | by (auto simp: Float.compute_lapprox_rat Float.compute_rapprox_rat) | 
| 29805 | 895 | next | 
| 896 | case False with not0_implies_Suc obtain m where "n = Suc m" by blast | |
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 897 | 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) | 
| 29805 | 898 | qed | 
| 899 | qed | |
| 900 | ||
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 901 | lemma sin_aux: assumes "0 \<le> real x" | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 902 | shows "(x * lb_sin_cos_aux prec n 2 1 (x * x)) \<le> (\<Sum> i=0..<n. -1^i * (1/real (fact (2 * i + 1))) * x^(2 * i + 1))" (is "?lb") | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 903 | and "(\<Sum> i=0..<n. -1^i * (1/real (fact (2 * i + 1))) * x^(2 * i + 1)) \<le> (x * ub_sin_cos_aux prec n 2 1 (x * x))" (is "?ub") | 
| 29805 | 904 | proof - | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 905 | have "0 \<le> real (x * x)" by auto | 
| 29805 | 906 | let "?f n" = "fact (2 * n + 1)" | 
| 907 | ||
| 31809 | 908 |   { fix n
 | 
| 45129 
1fce03e3e8ad
tuned proofs -- eliminated vacuous "induct arbitrary: ..." situations;
 wenzelm parents: 
44821diff
changeset | 909 | have F: "\<And>m. ((\<lambda>i. i + 2) ^^ n) m = m + 2 * n" by (induct n) auto | 
| 30971 | 910 | have "?f (Suc n) = ?f n * ((\<lambda>i. i + 2) ^^ n) 2 * (((\<lambda>i. i + 2) ^^ n) 2 + 1)" | 
| 29805 | 911 | unfolding F by auto } note f_eq = this | 
| 31809 | 912 | |
| 29805 | 913 | from horner_bounds[where lb="lb_sin_cos_aux prec" and ub="ub_sin_cos_aux prec" and j'=0, | 
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 914 | OF `0 \<le> real (x * x)` f_eq lb_sin_cos_aux.simps ub_sin_cos_aux.simps] | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 915 | show "?lb" and "?ub" using `0 \<le> real x` | 
| 36778 
739a9379e29b
avoid using real-specific versions of generic lemmas
 huffman parents: 
36531diff
changeset | 916 | unfolding power_add power_one_right mult_assoc[symmetric] setsum_left_distrib[symmetric] | 
| 
739a9379e29b
avoid using real-specific versions of generic lemmas
 huffman parents: 
36531diff
changeset | 917 | unfolding mult_commute[where 'a=real] | 
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 918 | by (auto intro!: mult_left_mono simp add: power_mult power2_eq_square[of "real x"]) | 
| 29805 | 919 | qed | 
| 920 | ||
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 921 | lemma sin_boundaries: assumes "0 \<le> real x" and "x \<le> pi / 2" | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 922 |   shows "sin x \<in> {(x * lb_sin_cos_aux prec (get_even n) 2 1 (x * x)) .. (x * ub_sin_cos_aux prec (get_odd n) 2 1 (x * x))}"
 | 
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 923 | proof (cases "real x = 0") | 
| 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 924 | case False hence "real x \<noteq> 0" by auto | 
| 47600 | 925 | hence "0 < x" and "0 < real x" using `0 \<le> real x` by auto | 
| 926 | have "0 < x * x" using `0 < x` | |
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 927 | using mult_pos_pos[where a="real x" and b="real x"] by auto | 
| 29805 | 928 | |
| 929 |   { fix x n have "(\<Sum> j = 0 ..< n. -1 ^ (((2 * j + 1) - Suc 0) div 2) / (real (fact (2 * j + 1))) * x ^(2 * j + 1))
 | |
| 930 | = (\<Sum> i = 0 ..< 2 * n. (if even(i) then 0 else (-1 ^ ((i - Suc 0) div 2))/(real (fact i))) * x ^ i)" (is "?SUM = _") | |
| 931 | proof - | |
| 932 | have pow: "!!i. x ^ (2 * i + 1) = x * x ^ (2 * i)" by auto | |
| 933 | have "?SUM = (\<Sum> j = 0 ..< n. 0) + ?SUM" by auto | |
| 934 | also have "\<dots> = (\<Sum> i = 0 ..< 2 * n. if even i then 0 else -1 ^ ((i - Suc 0) div 2) / (real (fact i)) * x ^ i)" | |
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 935 | unfolding sum_split_even_odd .. | 
| 29805 | 936 | also have "\<dots> = (\<Sum> i = 0 ..< 2 * n. (if even i then 0 else -1 ^ ((i - Suc 0) div 2) / (real (fact i))) * x ^ i)" | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 937 | by (rule setsum_cong2) auto | 
| 29805 | 938 | finally show ?thesis by assumption | 
| 939 | qed } note setsum_morph = this | |
| 940 | ||
| 941 |   { fix n :: nat assume "0 < n"
 | |
| 942 | hence "0 < 2 * n + 1" by auto | |
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 943 | obtain t where "0 < t" and "t < real x" and | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 944 | sin_eq: "sin x = (\<Sum> i = 0 ..< 2 * n + 1. (if even(i) then 0 else (-1 ^ ((i - Suc 0) div 2))/(real (fact i))) * (real x) ^ i) | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 945 | + (sin (t + 1/2 * (2 * n + 1) * pi) / real (fact (2*n + 1))) * (real x)^(2*n + 1)" | 
| 29805 | 946 | (is "_ = ?SUM + ?rest / ?fact * ?pow") | 
| 44306 
33572a766836
fold definitions of sin_coeff and cos_coeff in Maclaurin lemmas
 huffman parents: 
44305diff
changeset | 947 | using Maclaurin_sin_expansion3[OF `0 < 2 * n + 1` `0 < real x`] | 
| 
33572a766836
fold definitions of sin_coeff and cos_coeff in Maclaurin lemmas
 huffman parents: 
44305diff
changeset | 948 | unfolding sin_coeff_def by auto | 
| 29805 | 949 | |
| 950 | have "?rest = cos t * -1^n" unfolding sin_add cos_add real_of_nat_add left_distrib right_distrib by auto | |
| 951 | moreover | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 952 | have "t \<le> pi / 2" using `t < real x` and `x \<le> pi / 2` by auto | 
| 29805 | 953 | hence "0 \<le> cos t" using `0 < t` and cos_ge_zero by auto | 
| 954 | ultimately have even: "even n \<Longrightarrow> 0 \<le> ?rest" and odd: "odd n \<Longrightarrow> 0 \<le> - ?rest " by auto | |
| 955 | ||
| 44305 | 956 | have "0 < ?fact" by (simp del: fact_Suc) | 
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 957 | have "0 < ?pow" using `0 < real x` by (rule zero_less_power) | 
| 29805 | 958 | |
| 959 |     {
 | |
| 960 | assume "even n" | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 961 | have "(x * lb_sin_cos_aux prec n 2 1 (x * x)) \<le> | 
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 962 | (\<Sum> i = 0 ..< 2 * n. (if even(i) then 0 else (-1 ^ ((i - Suc 0) div 2))/(real (fact i))) * (real x) ^ i)" | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 963 | using sin_aux[OF `0 \<le> real x`] unfolding setsum_morph[symmetric] by auto | 
| 29805 | 964 | also have "\<dots> \<le> ?SUM" by auto | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 965 | also have "\<dots> \<le> sin x" | 
| 29805 | 966 | proof - | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 967 | from even[OF `even n`] `0 < ?fact` `0 < ?pow` | 
| 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 968 | have "0 \<le> (?rest / ?fact) * ?pow" by (metis mult_nonneg_nonneg divide_nonneg_pos less_imp_le) | 
| 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 969 | thus ?thesis unfolding sin_eq by auto | 
| 29805 | 970 | qed | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 971 | finally have "(x * lb_sin_cos_aux prec n 2 1 (x * x)) \<le> sin x" . | 
| 29805 | 972 | } note lb = this | 
| 973 | ||
| 974 |     {
 | |
| 975 | assume "odd n" | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 976 | have "sin x \<le> ?SUM" | 
| 29805 | 977 | proof - | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 978 | from `0 < ?fact` and `0 < ?pow` and odd[OF `odd n`] | 
| 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 979 | have "0 \<le> (- ?rest) / ?fact * ?pow" | 
| 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 980 | by (metis mult_nonneg_nonneg divide_nonneg_pos less_imp_le) | 
| 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 981 | thus ?thesis unfolding sin_eq by auto | 
| 29805 | 982 | qed | 
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 983 | also have "\<dots> \<le> (\<Sum> i = 0 ..< 2 * n. (if even(i) then 0 else (-1 ^ ((i - Suc 0) div 2))/(real (fact i))) * (real x) ^ i)" | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 984 | by auto | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 985 | also have "\<dots> \<le> (x * ub_sin_cos_aux prec n 2 1 (x * x))" | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 986 | using sin_aux[OF `0 \<le> real x`] unfolding setsum_morph[symmetric] by auto | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 987 | finally have "sin x \<le> (x * ub_sin_cos_aux prec n 2 1 (x * x))" . | 
| 29805 | 988 | } note ub = this and lb | 
| 989 | } note ub = this(1) and lb = this(2) | |
| 990 | ||
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 991 | have "sin x \<le> (x * ub_sin_cos_aux prec (get_odd n) 2 1 (x * x))" using ub[OF odd_pos[OF get_odd] get_odd] . | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 992 | moreover have "(x * lb_sin_cos_aux prec (get_even n) 2 1 (x * x)) \<le> sin x" | 
| 29805 | 993 | proof (cases "0 < get_even n") | 
| 994 | case True show ?thesis using lb[OF True get_even] . | |
| 995 | next | |
| 996 | case False | |
| 997 | hence "get_even n = 0" by auto | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 998 | with `x \<le> pi / 2` `0 \<le> real x` | 
| 47601 
050718fe6eee
use real :: float => real as lifting-morphism so we can directlry use the rep_eq theorems
 hoelzl parents: 
47600diff
changeset | 999 | show ?thesis unfolding `get_even n = 0` ub_sin_cos_aux.simps minus_float.rep_eq using sin_ge_zero by auto | 
| 29805 | 1000 | qed | 
| 1001 | ultimately show ?thesis by auto | |
| 1002 | next | |
| 1003 | case True | |
| 1004 | show ?thesis | |
| 1005 | proof (cases "n = 0") | |
| 31809 | 1006 | case True | 
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 1007 | 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 | 
| 29805 | 1008 | next | 
| 1009 | case False with not0_implies_Suc obtain m where "n = Suc m" by blast | |
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 1010 | 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) | 
| 29805 | 1011 | qed | 
| 1012 | qed | |
| 1013 | ||
| 1014 | subsection "Compute the cosinus in the entire domain" | |
| 1015 | ||
| 1016 | definition lb_cos :: "nat \<Rightarrow> float \<Rightarrow> float" where | |
| 1017 | "lb_cos prec x = (let | |
| 1018 | horner = \<lambda> x. lb_sin_cos_aux prec (get_even (prec div 4 + 1)) 1 1 (x * x) ; | |
| 1019 | half = \<lambda> x. if x < 0 then - 1 else Float 1 1 * x * x - 1 | |
| 1020 | in if x < Float 1 -1 then horner x | |
| 1021 | else if x < 1 then half (horner (x * Float 1 -1)) | |
| 1022 | else half (half (horner (x * Float 1 -2))))" | |
| 1023 | ||
| 1024 | definition ub_cos :: "nat \<Rightarrow> float \<Rightarrow> float" where | |
| 1025 | "ub_cos prec x = (let | |
| 1026 | horner = \<lambda> x. ub_sin_cos_aux prec (get_odd (prec div 4 + 1)) 1 1 (x * x) ; | |
| 1027 | half = \<lambda> x. Float 1 1 * x * x - 1 | |
| 1028 | in if x < Float 1 -1 then horner x | |
| 1029 | else if x < 1 then half (horner (x * Float 1 -1)) | |
| 1030 | else half (half (horner (x * Float 1 -2))))" | |
| 1031 | ||
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1032 | lemma lb_cos: assumes "0 \<le> real x" and "x \<le> pi" | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1033 |   shows "cos x \<in> {(lb_cos prec x) .. (ub_cos prec x)}" (is "?cos x \<in> {(?lb x) .. (?ub x) }")
 | 
| 29805 | 1034 | proof - | 
| 1035 |   { fix x :: real
 | |
| 1036 | have "cos x = cos (x / 2 + x / 2)" by auto | |
| 1037 | also have "\<dots> = 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" | |
| 1038 | unfolding cos_add by auto | |
| 1039 | also have "\<dots> = 2 * cos (x / 2) * cos (x / 2) - 1" by algebra | |
| 1040 | finally have "cos x = 2 * cos (x / 2) * cos (x / 2) - 1" . | |
| 1041 | } note x_half = this[symmetric] | |
| 1042 | ||
| 47600 | 1043 | have "\<not> x < 0" using `0 \<le> real x` by auto | 
| 29805 | 1044 | let "?ub_horner x" = "ub_sin_cos_aux prec (get_odd (prec div 4 + 1)) 1 1 (x * x)" | 
| 1045 | let "?lb_horner x" = "lb_sin_cos_aux prec (get_even (prec div 4 + 1)) 1 1 (x * x)" | |
| 1046 | let "?ub_half x" = "Float 1 1 * x * x - 1" | |
| 1047 | let "?lb_half x" = "if x < 0 then - 1 else Float 1 1 * x * x - 1" | |
| 1048 | ||
| 1049 | show ?thesis | |
| 1050 | proof (cases "x < Float 1 -1") | |
| 47600 | 1051 | case True hence "x \<le> pi / 2" using pi_ge_two by auto | 
| 29805 | 1052 | show ?thesis unfolding lb_cos_def[where x=x] ub_cos_def[where x=x] if_not_P[OF `\<not> x < 0`] if_P[OF `x < Float 1 -1`] Let_def | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1053 | using cos_boundaries[OF `0 \<le> real x` `x \<le> pi / 2`] . | 
| 29805 | 1054 | next | 
| 1055 | case False | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1056 |     { fix y x :: float let ?x2 = "(x * Float 1 -1)"
 | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1057 | assume "y \<le> cos ?x2" and "-pi \<le> x" and "x \<le> pi" | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1058 | hence "- (pi / 2) \<le> ?x2" and "?x2 \<le> pi / 2" using pi_ge_two unfolding Float_num by auto | 
| 29805 | 1059 | hence "0 \<le> cos ?x2" by (rule cos_ge_zero) | 
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1060 | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1061 | have "(?lb_half y) \<le> cos x" | 
| 29805 | 1062 | proof (cases "y < 0") | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 1063 | case True show ?thesis using cos_ge_minus_one unfolding if_P[OF True] by auto | 
| 29805 | 1064 | next | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 1065 | case False | 
| 47600 | 1066 | hence "0 \<le> real y" by auto | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1067 | from mult_mono[OF `y \<le> cos ?x2` `y \<le> cos ?x2` `0 \<le> cos ?x2` this] | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 1068 | have "real y * real y \<le> cos ?x2 * cos ?x2" . | 
| 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 1069 | hence "2 * real y * real y \<le> 2 * cos ?x2 * cos ?x2" by auto | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1070 | hence "2 * real y * real y - 1 \<le> 2 * cos (x / 2) * cos (x / 2) - 1" unfolding Float_num by auto | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1071 | thus ?thesis unfolding if_not_P[OF False] x_half Float_num by auto | 
| 29805 | 1072 | qed | 
| 1073 | } note lb_half = this | |
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1074 | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1075 |     { fix y x :: float let ?x2 = "(x * Float 1 -1)"
 | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1076 | assume ub: "cos ?x2 \<le> y" and "- pi \<le> x" and "x \<le> pi" | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1077 | hence "- (pi / 2) \<le> ?x2" and "?x2 \<le> pi / 2" using pi_ge_two unfolding Float_num by auto | 
| 29805 | 1078 | hence "0 \<le> cos ?x2" by (rule cos_ge_zero) | 
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1079 | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1080 | have "cos x \<le> (?ub_half y)" | 
| 29805 | 1081 | proof - | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 1082 | have "0 \<le> real y" using `0 \<le> cos ?x2` ub by (rule order_trans) | 
| 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 1083 | from mult_mono[OF ub ub this `0 \<le> cos ?x2`] | 
| 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 1084 | have "cos ?x2 * cos ?x2 \<le> real y * real y" . | 
| 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 1085 | hence "2 * cos ?x2 * cos ?x2 \<le> 2 * real y * real y" by auto | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1086 | hence "2 * cos (x / 2) * cos (x / 2) - 1 \<le> 2 * real y * real y - 1" unfolding Float_num by auto | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1087 | thus ?thesis unfolding x_half Float_num by auto | 
| 29805 | 1088 | qed | 
| 1089 | } note ub_half = this | |
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1090 | |
| 29805 | 1091 | let ?x2 = "x * Float 1 -1" | 
| 1092 | let ?x4 = "x * Float 1 -1 * Float 1 -1" | |
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1093 | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1094 | have "-pi \<le> x" using pi_ge_zero[THEN le_imp_neg_le, unfolded minus_zero] `0 \<le> real x` by (rule order_trans) | 
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1095 | |
| 29805 | 1096 | show ?thesis | 
| 1097 | proof (cases "x < 1") | |
| 47600 | 1098 | case True hence "real x \<le> 1" by auto | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1099 | have "0 \<le> real ?x2" and "?x2 \<le> pi / 2" using pi_ge_two `0 \<le> real x` using assms by auto | 
| 29805 | 1100 | from cos_boundaries[OF this] | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1101 | have lb: "(?lb_horner ?x2) \<le> ?cos ?x2" and ub: "?cos ?x2 \<le> (?ub_horner ?x2)" by auto | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1102 | |
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1103 | have "(?lb x) \<le> ?cos x" | 
| 29805 | 1104 | proof - | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1105 | from lb_half[OF lb `-pi \<le> x` `x \<le> pi`] | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 1106 | show ?thesis unfolding lb_cos_def[where x=x] Let_def using `\<not> x < 0` `\<not> x < Float 1 -1` `x < 1` by auto | 
| 29805 | 1107 | qed | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1108 | moreover have "?cos x \<le> (?ub x)" | 
| 29805 | 1109 | proof - | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1110 | from ub_half[OF ub `-pi \<le> x` `x \<le> pi`] | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 1111 | show ?thesis unfolding ub_cos_def[where x=x] Let_def using `\<not> x < 0` `\<not> x < Float 1 -1` `x < 1` by auto | 
| 29805 | 1112 | qed | 
| 1113 | ultimately show ?thesis by auto | |
| 1114 | next | |
| 1115 | case False | |
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1116 | have "0 \<le> real ?x4" and "?x4 \<le> pi / 2" using pi_ge_two `0 \<le> real x` `x \<le> pi` unfolding Float_num by auto | 
| 29805 | 1117 | from cos_boundaries[OF this] | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1118 | have lb: "(?lb_horner ?x4) \<le> ?cos ?x4" and ub: "?cos ?x4 \<le> (?ub_horner ?x4)" by auto | 
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1119 | |
| 47600 | 1120 | have eq_4: "?x2 * Float 1 -1 = x * Float 1 -2" by transfer simp | 
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1121 | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1122 | have "(?lb x) \<le> ?cos x" | 
| 29805 | 1123 | proof - | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1124 | have "-pi \<le> ?x2" and "?x2 \<le> pi" using pi_ge_two `0 \<le> real x` `x \<le> pi` by auto | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1125 | from lb_half[OF lb_half[OF lb this] `-pi \<le> x` `x \<le> pi`, unfolded eq_4] | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 1126 | show ?thesis unfolding lb_cos_def[where x=x] if_not_P[OF `\<not> x < 0`] if_not_P[OF `\<not> x < Float 1 -1`] if_not_P[OF `\<not> x < 1`] Let_def . | 
| 29805 | 1127 | qed | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1128 | moreover have "?cos x \<le> (?ub x)" | 
| 29805 | 1129 | proof - | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1130 | have "-pi \<le> ?x2" and "?x2 \<le> pi" using pi_ge_two `0 \<le> real x` ` x \<le> pi` by auto | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1131 | from ub_half[OF ub_half[OF ub this] `-pi \<le> x` `x \<le> pi`, unfolded eq_4] | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 1132 | show ?thesis unfolding ub_cos_def[where x=x] if_not_P[OF `\<not> x < 0`] if_not_P[OF `\<not> x < Float 1 -1`] if_not_P[OF `\<not> x < 1`] Let_def . | 
| 29805 | 1133 | qed | 
| 1134 | ultimately show ?thesis by auto | |
| 1135 | qed | |
| 1136 | qed | |
| 1137 | qed | |
| 1138 | ||
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1139 | lemma lb_cos_minus: assumes "-pi \<le> x" and "real x \<le> 0" | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1140 |   shows "cos (real(-x)) \<in> {(lb_cos prec (-x)) .. (ub_cos prec (-x))}"
 | 
| 29805 | 1141 | proof - | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1142 | have "0 \<le> real (-x)" and "(-x) \<le> pi" using `-pi \<le> x` `real x \<le> 0` by auto | 
| 29805 | 1143 | from lb_cos[OF this] show ?thesis . | 
| 1144 | qed | |
| 1145 | ||
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1146 | definition bnds_cos :: "nat \<Rightarrow> float \<Rightarrow> float \<Rightarrow> float * float" where | 
| 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1147 | "bnds_cos prec lx ux = (let | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1148 | lpi = float_round_down prec (lb_pi prec) ; | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1149 | upi = float_round_up prec (ub_pi prec) ; | 
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1150 | k = floor_fl (float_divr prec (lx + lpi) (2 * lpi)) ; | 
| 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1151 | lx = lx - k * 2 * (if k < 0 then lpi else upi) ; | 
| 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1152 | ux = ux - k * 2 * (if k < 0 then upi else lpi) | 
| 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1153 | in if - lpi \<le> lx \<and> ux \<le> 0 then (lb_cos prec (-lx), ub_cos prec (-ux)) | 
| 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1154 | else if 0 \<le> lx \<and> ux \<le> lpi then (lb_cos prec ux, ub_cos prec lx) | 
| 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1155 | else if - lpi \<le> lx \<and> ux \<le> lpi then (min (lb_cos prec (-lx)) (lb_cos prec ux), Float 1 0) | 
| 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1156 | else if 0 \<le> lx \<and> ux \<le> 2 * lpi then (Float -1 0, max (ub_cos prec lx) (ub_cos prec (- (ux - 2 * lpi)))) | 
| 31508 | 1157 | else if -2 * lpi \<le> lx \<and> ux \<le> 0 then (Float -1 0, max (ub_cos prec (lx + 2 * lpi)) (ub_cos prec (-ux))) | 
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1158 | else (Float -1 0, Float 1 0))" | 
| 29805 | 1159 | |
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1160 | lemma floor_int: | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1161 | obtains k :: int where "real k = (floor_fl f)" | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1162 | by (simp add: floor_fl_def) | 
| 29805 | 1163 | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1164 | lemma cos_periodic_nat[simp]: fixes n :: nat shows "cos (x + n * (2 * pi)) = cos x" | 
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1165 | proof (induct n arbitrary: x) | 
| 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1166 | case (Suc n) | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1167 | have split_pi_off: "x + (Suc n) * (2 * pi) = (x + n * (2 * pi)) + 2 * pi" | 
| 36778 
739a9379e29b
avoid using real-specific versions of generic lemmas
 huffman parents: 
36531diff
changeset | 1168 | unfolding Suc_eq_plus1 real_of_nat_add real_of_one left_distrib by auto | 
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1169 | show ?case unfolding split_pi_off using Suc by auto | 
| 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1170 | qed auto | 
| 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1171 | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1172 | lemma cos_periodic_int[simp]: fixes i :: int shows "cos (x + i * (2 * pi)) = cos x" | 
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1173 | proof (cases "0 \<le> i") | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1174 | case True hence i_nat: "real i = nat i" by auto | 
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1175 | show ?thesis unfolding i_nat by auto | 
| 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1176 | next | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1177 | case False hence i_nat: "i = - real (nat (-i))" by auto | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1178 | have "cos x = cos (x + i * (2 * pi) - i * (2 * pi))" by auto | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1179 | also have "\<dots> = cos (x + i * (2 * pi))" | 
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1180 | unfolding i_nat mult_minus_left diff_minus_eq_add by (rule cos_periodic_nat) | 
| 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1181 | finally show ?thesis by auto | 
| 29805 | 1182 | qed | 
| 1183 | ||
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1184 | lemma bnds_cos: "\<forall> (x::real) lx ux. (l, u) = bnds_cos prec lx ux \<and> x \<in> {lx .. ux} \<longrightarrow> l \<le> cos x \<and> cos x \<le> u"
 | 
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1185 | proof ((rule allI | rule impI | erule conjE) +) | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1186 | fix x :: real fix lx ux | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1187 |   assume bnds: "(l, u) = bnds_cos prec lx ux" and x: "x \<in> {lx .. ux}"
 | 
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1188 | |
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1189 | let ?lpi = "float_round_down prec (lb_pi prec)" | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1190 | let ?upi = "float_round_up prec (ub_pi prec)" | 
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1191 | let ?k = "floor_fl (float_divr prec (lx + ?lpi) (2 * ?lpi))" | 
| 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1192 | let ?lx = "lx - ?k * 2 * (if ?k < 0 then ?lpi else ?upi)" | 
| 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1193 | let ?ux = "ux - ?k * 2 * (if ?k < 0 then ?upi else ?lpi)" | 
| 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1194 | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1195 | obtain k :: int where k: "k = real ?k" using floor_int . | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1196 | |
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1197 | have upi: "pi \<le> ?upi" and lpi: "?lpi \<le> pi" | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1198 | using float_round_up[of "ub_pi prec" prec] pi_boundaries[of prec] | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1199 | float_round_down[of prec "lb_pi prec"] by auto | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1200 | hence "?lx \<le> x - k * (2 * pi) \<and> x - k * (2 * pi) \<le> ?ux" | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1201 | using x unfolding k[symmetric] | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1202 | by (cases "k = 0") | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1203 | (auto intro!: add_mono | 
| 47600 | 1204 | simp add: diff_minus k[symmetric] | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1205 | simp del: float_of_numeral) | 
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1206 | note lx = this[THEN conjunct1] and ux = this[THEN conjunct2] | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1207 | hence lx_less_ux: "?lx \<le> real ?ux" by (rule order_trans) | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1208 | |
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1209 |   { assume "- ?lpi \<le> ?lx" and x_le_0: "x - k * (2 * pi) \<le> 0"
 | 
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1210 | with lpi[THEN le_imp_neg_le] lx | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1211 | have pi_lx: "- pi \<le> ?lx" and lx_0: "real ?lx \<le> 0" | 
| 47600 | 1212 | by simp_all | 
| 29805 | 1213 | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1214 | have "(lb_cos prec (- ?lx)) \<le> cos (real (- ?lx))" | 
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1215 | using lb_cos_minus[OF pi_lx lx_0] by simp | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1216 | also have "\<dots> \<le> cos (x + (-k) * (2 * pi))" | 
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1217 | using cos_monotone_minus_pi_0'[OF pi_lx lx x_le_0] | 
| 47601 
050718fe6eee
use real :: float => real as lifting-morphism so we can directlry use the rep_eq theorems
 hoelzl parents: 
47600diff
changeset | 1218 | by (simp only: uminus_float.rep_eq real_of_int_minus | 
| 37887 | 1219 | cos_minus diff_minus mult_minus_left) | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1220 | finally have "(lb_cos prec (- ?lx)) \<le> cos x" | 
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1221 | unfolding cos_periodic_int . } | 
| 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1222 | note negative_lx = this | 
| 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1223 | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1224 |   { assume "0 \<le> ?lx" and pi_x: "x - k * (2 * pi) \<le> pi"
 | 
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1225 | with lx | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1226 | have pi_lx: "?lx \<le> pi" and lx_0: "0 \<le> real ?lx" | 
| 47600 | 1227 | by auto | 
| 29805 | 1228 | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1229 | have "cos (x + (-k) * (2 * pi)) \<le> cos ?lx" | 
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1230 | using cos_monotone_0_pi'[OF lx_0 lx pi_x] | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1231 | by (simp only: real_of_int_minus | 
| 37887 | 1232 | cos_minus diff_minus mult_minus_left) | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1233 | also have "\<dots> \<le> (ub_cos prec ?lx)" | 
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1234 | using lb_cos[OF lx_0 pi_lx] by simp | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1235 | finally have "cos x \<le> (ub_cos prec ?lx)" | 
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1236 | unfolding cos_periodic_int . } | 
| 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1237 | note positive_lx = this | 
| 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1238 | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1239 |   { assume pi_x: "- pi \<le> x - k * (2 * pi)" and "?ux \<le> 0"
 | 
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1240 | with ux | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1241 | have pi_ux: "- pi \<le> ?ux" and ux_0: "real ?ux \<le> 0" | 
| 47600 | 1242 | by simp_all | 
| 29805 | 1243 | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1244 | have "cos (x + (-k) * (2 * pi)) \<le> cos (real (- ?ux))" | 
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1245 | using cos_monotone_minus_pi_0'[OF pi_x ux ux_0] | 
| 47601 
050718fe6eee
use real :: float => real as lifting-morphism so we can directlry use the rep_eq theorems
 hoelzl parents: 
47600diff
changeset | 1246 | by (simp only: uminus_float.rep_eq real_of_int_minus | 
| 37887 | 1247 | cos_minus diff_minus mult_minus_left) | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1248 | also have "\<dots> \<le> (ub_cos prec (- ?ux))" | 
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1249 | using lb_cos_minus[OF pi_ux ux_0, of prec] by simp | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1250 | finally have "cos x \<le> (ub_cos prec (- ?ux))" | 
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1251 | unfolding cos_periodic_int . } | 
| 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1252 | note negative_ux = this | 
| 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1253 | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1254 |   { assume "?ux \<le> ?lpi" and x_ge_0: "0 \<le> x - k * (2 * pi)"
 | 
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1255 | with lpi ux | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1256 | have pi_ux: "?ux \<le> pi" and ux_0: "0 \<le> real ?ux" | 
| 47600 | 1257 | by simp_all | 
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1258 | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1259 | have "(lb_cos prec ?ux) \<le> cos ?ux" | 
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1260 | using lb_cos[OF ux_0 pi_ux] by simp | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1261 | also have "\<dots> \<le> cos (x + (-k) * (2 * pi))" | 
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1262 | using cos_monotone_0_pi'[OF x_ge_0 ux pi_ux] | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1263 | by (simp only: real_of_int_minus | 
| 37887 | 1264 | cos_minus diff_minus mult_minus_left) | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1265 | finally have "(lb_cos prec ?ux) \<le> cos x" | 
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1266 | unfolding cos_periodic_int . } | 
| 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1267 | note positive_ux = this | 
| 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1268 | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1269 | show "l \<le> cos x \<and> cos x \<le> u" | 
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1270 | proof (cases "- ?lpi \<le> ?lx \<and> ?ux \<le> 0") | 
| 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1271 | case True with bnds | 
| 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1272 | have l: "l = lb_cos prec (-?lx)" | 
| 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1273 | and u: "u = ub_cos prec (-?ux)" | 
| 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1274 | by (auto simp add: bnds_cos_def Let_def) | 
| 29805 | 1275 | |
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1276 | from True lpi[THEN le_imp_neg_le] lx ux | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1277 | have "- pi \<le> x - k * (2 * pi)" | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1278 | and "x - k * (2 * pi) \<le> 0" | 
| 47600 | 1279 | by auto | 
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1280 | with True negative_ux negative_lx | 
| 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1281 | show ?thesis unfolding l u by simp | 
| 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1282 | next case False note 1 = this show ?thesis | 
| 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1283 | proof (cases "0 \<le> ?lx \<and> ?ux \<le> ?lpi") | 
| 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1284 | case True with bnds 1 | 
| 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1285 | have l: "l = lb_cos prec ?ux" | 
| 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1286 | and u: "u = ub_cos prec ?lx" | 
| 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1287 | by (auto simp add: bnds_cos_def Let_def) | 
| 29805 | 1288 | |
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1289 | from True lpi lx ux | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1290 | have "0 \<le> x - k * (2 * pi)" | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1291 | and "x - k * (2 * pi) \<le> pi" | 
| 47600 | 1292 | by auto | 
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1293 | with True positive_ux positive_lx | 
| 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1294 | show ?thesis unfolding l u by simp | 
| 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1295 | next case False note 2 = this show ?thesis | 
| 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1296 | proof (cases "- ?lpi \<le> ?lx \<and> ?ux \<le> ?lpi") | 
| 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1297 | case True note Cond = this with bnds 1 2 | 
| 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1298 | have l: "l = min (lb_cos prec (-?lx)) (lb_cos prec ?ux)" | 
| 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1299 | and u: "u = Float 1 0" | 
| 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1300 | by (auto simp add: bnds_cos_def Let_def) | 
| 29805 | 1301 | |
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1302 | show ?thesis unfolding u l using negative_lx positive_ux Cond | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1303 | by (cases "x - k * (2 * pi) < 0") (auto simp add: real_of_float_min) | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1304 | |
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1305 | next case False note 3 = this show ?thesis | 
| 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1306 | proof (cases "0 \<le> ?lx \<and> ?ux \<le> 2 * ?lpi") | 
| 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1307 | case True note Cond = this with bnds 1 2 3 | 
| 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1308 | have l: "l = Float -1 0" | 
| 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1309 | and u: "u = max (ub_cos prec ?lx) (ub_cos prec (- (?ux - 2 * ?lpi)))" | 
| 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1310 | by (auto simp add: bnds_cos_def Let_def) | 
| 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1311 | |
| 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1312 | have "cos x \<le> real u" | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1313 | proof (cases "x - k * (2 * pi) < pi") | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1314 | case True hence "x - k * (2 * pi) \<le> pi" by simp | 
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1315 | from positive_lx[OF Cond[THEN conjunct1] this] | 
| 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1316 | show ?thesis unfolding u by (simp add: real_of_float_max) | 
| 29805 | 1317 | next | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1318 | case False hence "pi \<le> x - k * (2 * pi)" by simp | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1319 | hence pi_x: "- pi \<le> x - k * (2 * pi) - 2 * pi" by simp | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1320 | |
| 47600 | 1321 | have "?ux \<le> 2 * pi" using Cond lpi by auto | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1322 | hence "x - k * (2 * pi) - 2 * pi \<le> 0" using ux by simp | 
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1323 | |
| 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1324 | have ux_0: "real (?ux - 2 * ?lpi) \<le> 0" | 
| 47600 | 1325 | using Cond by auto | 
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1326 | |
| 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1327 | from 2 and Cond have "\<not> ?ux \<le> ?lpi" by auto | 
| 47600 | 1328 | hence "- ?lpi \<le> ?ux - 2 * ?lpi" by auto | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1329 | hence pi_ux: "- pi \<le> (?ux - 2 * ?lpi)" | 
| 47600 | 1330 | using lpi[THEN le_imp_neg_le] by auto | 
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1331 | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1332 | have x_le_ux: "x - k * (2 * pi) - 2 * pi \<le> (?ux - 2 * ?lpi)" | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 1333 | using ux lpi by auto | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1334 | have "cos x = cos (x + (-k) * (2 * pi) + (-1::int) * (2 * pi))" | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 1335 | unfolding cos_periodic_int .. | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1336 | also have "\<dots> \<le> cos ((?ux - 2 * ?lpi))" | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 1337 | using cos_monotone_minus_pi_0'[OF pi_x x_le_ux ux_0] | 
| 47601 
050718fe6eee
use real :: float => real as lifting-morphism so we can directlry use the rep_eq theorems
 hoelzl parents: 
47600diff
changeset | 1338 | by (simp only: minus_float.rep_eq real_of_int_minus real_of_one minus_one[symmetric] | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1339 | diff_minus mult_minus_left mult_1_left) | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1340 | also have "\<dots> = cos ((- (?ux - 2 * ?lpi)))" | 
| 47601 
050718fe6eee
use real :: float => real as lifting-morphism so we can directlry use the rep_eq theorems
 hoelzl parents: 
47600diff
changeset | 1341 | unfolding uminus_float.rep_eq cos_minus .. | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1342 | also have "\<dots> \<le> (ub_cos prec (- (?ux - 2 * ?lpi)))" | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 1343 | using lb_cos_minus[OF pi_ux ux_0] by simp | 
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1344 | finally show ?thesis unfolding u by (simp add: real_of_float_max) | 
| 29805 | 1345 | qed | 
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1346 | thus ?thesis unfolding l by auto | 
| 31508 | 1347 | next case False note 4 = this show ?thesis | 
| 1348 | proof (cases "-2 * ?lpi \<le> ?lx \<and> ?ux \<le> 0") | |
| 1349 | case True note Cond = this with bnds 1 2 3 4 | |
| 1350 | have l: "l = Float -1 0" | |
| 1351 | and u: "u = max (ub_cos prec (?lx + 2 * ?lpi)) (ub_cos prec (-?ux))" | |
| 47600 | 1352 | by (auto simp add: bnds_cos_def Let_def) | 
| 31508 | 1353 | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1354 | have "cos x \<le> u" | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1355 | proof (cases "-pi < x - k * (2 * pi)") | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1356 | case True hence "-pi \<le> x - k * (2 * pi)" by simp | 
| 31508 | 1357 | from negative_ux[OF this Cond[THEN conjunct2]] | 
| 1358 | show ?thesis unfolding u by (simp add: real_of_float_max) | |
| 1359 | next | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1360 | case False hence "x - k * (2 * pi) \<le> -pi" by simp | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1361 | hence pi_x: "x - k * (2 * pi) + 2 * pi \<le> pi" by simp | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1362 | |
| 47600 | 1363 | have "-2 * pi \<le> ?lx" using Cond lpi by auto | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1364 | |
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1365 | hence "0 \<le> x - k * (2 * pi) + 2 * pi" using lx by simp | 
| 31508 | 1366 | |
| 1367 | have lx_0: "0 \<le> real (?lx + 2 * ?lpi)" | |
| 47600 | 1368 | using Cond lpi by auto | 
| 31508 | 1369 | |
| 1370 | from 1 and Cond have "\<not> -?lpi \<le> ?lx" by auto | |
| 47600 | 1371 | hence "?lx + 2 * ?lpi \<le> ?lpi" by auto | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1372 | hence pi_lx: "(?lx + 2 * ?lpi) \<le> pi" | 
| 47600 | 1373 | using lpi[THEN le_imp_neg_le] by auto | 
| 31508 | 1374 | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1375 | have lx_le_x: "(?lx + 2 * ?lpi) \<le> x - k * (2 * pi) + 2 * pi" | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 1376 | using lx lpi by auto | 
| 31508 | 1377 | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1378 | have "cos x = cos (x + (-k) * (2 * pi) + (1 :: int) * (2 * pi))" | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 1379 | unfolding cos_periodic_int .. | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1380 | also have "\<dots> \<le> cos ((?lx + 2 * ?lpi))" | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 1381 | using cos_monotone_0_pi'[OF lx_0 lx_le_x pi_x] | 
| 47601 
050718fe6eee
use real :: float => real as lifting-morphism so we can directlry use the rep_eq theorems
 hoelzl parents: 
47600diff
changeset | 1382 | by (simp only: minus_float.rep_eq real_of_int_minus real_of_one | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1383 | minus_one[symmetric] diff_minus mult_minus_left mult_1_left) | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1384 | also have "\<dots> \<le> (ub_cos prec (?lx + 2 * ?lpi))" | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 1385 | using lb_cos[OF lx_0 pi_lx] by simp | 
| 31508 | 1386 | finally show ?thesis unfolding u by (simp add: real_of_float_max) | 
| 1387 | qed | |
| 1388 | thus ?thesis unfolding l by auto | |
| 29805 | 1389 | next | 
| 31508 | 1390 | case False with bnds 1 2 3 4 show ?thesis by (auto simp add: bnds_cos_def Let_def) | 
| 1391 | qed qed qed qed qed | |
| 29805 | 1392 | qed | 
| 1393 | ||
| 1394 | section "Exponential function" | |
| 1395 | ||
| 1396 | subsection "Compute the series of the exponential function" | |
| 1397 | ||
| 1398 | fun ub_exp_horner :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> float \<Rightarrow> float" and lb_exp_horner :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> float \<Rightarrow> float" where | |
| 1399 | "ub_exp_horner prec 0 i k x = 0" | | |
| 1400 | "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" | | |
| 1401 | "lb_exp_horner prec 0 i k x = 0" | | |
| 1402 | "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" | |
| 1403 | ||
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 1404 | lemma bnds_exp_horner: assumes "real x \<le> 0" | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1405 |   shows "exp x \<in> { lb_exp_horner prec (get_even n) 1 1 x .. ub_exp_horner prec (get_odd n) 1 1 x }"
 | 
| 29805 | 1406 | proof - | 
| 1407 |   { fix n
 | |
| 30971 | 1408 | have F: "\<And> m. ((\<lambda>i. i + 1) ^^ n) m = n + m" by (induct n, auto) | 
| 1409 | have "fact (Suc n) = fact n * ((\<lambda>i. i + 1) ^^ n) 1" unfolding F by auto } note f_eq = this | |
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 1410 | |
| 29805 | 1411 | 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, | 
| 1412 | OF assms f_eq lb_exp_horner.simps ub_exp_horner.simps] | |
| 1413 | ||
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1414 |   { have "lb_exp_horner prec (get_even n) 1 1 x \<le> (\<Sum>j = 0..<get_even n. 1 / real (fact j) * real x ^ j)"
 | 
| 29805 | 1415 | using bounds(1) by auto | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1416 | also have "\<dots> \<le> exp x" | 
| 29805 | 1417 | proof - | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1418 | obtain t where "\<bar>t\<bar> \<le> \<bar>real x\<bar>" and "exp x = (\<Sum>m = 0..<get_even n. real x ^ m / real (fact m)) + exp t / real (fact (get_even n)) * (real x) ^ (get_even n)" | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 1419 | using Maclaurin_exp_le by blast | 
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 1420 | moreover have "0 \<le> exp t / real (fact (get_even n)) * (real x) ^ (get_even n)" | 
| 46545 | 1421 | by (auto intro!: mult_nonneg_nonneg divide_nonneg_pos simp add: zero_le_even_power) | 
| 29805 | 1422 | ultimately show ?thesis | 
| 35028 
108662d50512
more consistent naming of type classes involving orderings (and lattices) -- c.f. NEWS
 haftmann parents: 
33030diff
changeset | 1423 | using get_odd exp_gt_zero by (auto intro!: mult_nonneg_nonneg) | 
| 29805 | 1424 | qed | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1425 | finally have "lb_exp_horner prec (get_even n) 1 1 x \<le> exp x" . | 
| 29805 | 1426 | } moreover | 
| 31809 | 1427 |   {
 | 
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 1428 | have x_less_zero: "real x ^ get_odd n \<le> 0" | 
| 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 1429 | proof (cases "real x = 0") | 
| 29805 | 1430 | case True | 
| 1431 | have "(get_odd n) \<noteq> 0" using get_odd[THEN odd_pos] by auto | |
| 1432 | thus ?thesis unfolding True power_0_left by auto | |
| 1433 | next | |
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 1434 | case False hence "real x < 0" using `real x \<le> 0` by auto | 
| 46545 | 1435 | show ?thesis by (rule less_imp_le, auto simp add: power_less_zero_eq `real x < 0`) | 
| 29805 | 1436 | qed | 
| 1437 | ||
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1438 | obtain t where "\<bar>t\<bar> \<le> \<bar>real x\<bar>" and "exp x = (\<Sum>m = 0..<get_odd n. (real x) ^ m / real (fact m)) + exp t / real (fact (get_odd n)) * (real x) ^ (get_odd n)" | 
| 29805 | 1439 | using Maclaurin_exp_le by blast | 
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 1440 | moreover have "exp t / real (fact (get_odd n)) * (real x) ^ (get_odd n) \<le> 0" | 
| 46545 | 1441 | by (auto intro!: mult_nonneg_nonpos divide_nonpos_pos simp add: x_less_zero) | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1442 | ultimately have "exp x \<le> (\<Sum>j = 0..<get_odd n. 1 / real (fact j) * real x ^ j)" | 
| 35028 
108662d50512
more consistent naming of type classes involving orderings (and lattices) -- c.f. NEWS
 haftmann parents: 
33030diff
changeset | 1443 | using get_odd exp_gt_zero by (auto intro!: mult_nonneg_nonneg) | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1444 | also have "\<dots> \<le> ub_exp_horner prec (get_odd n) 1 1 x" | 
| 29805 | 1445 | using bounds(2) by auto | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1446 | finally have "exp x \<le> ub_exp_horner prec (get_odd n) 1 1 x" . | 
| 29805 | 1447 | } ultimately show ?thesis by auto | 
| 1448 | qed | |
| 1449 | ||
| 1450 | subsection "Compute the exponential function on the entire domain" | |
| 1451 | ||
| 1452 | function ub_exp :: "nat \<Rightarrow> float \<Rightarrow> float" and lb_exp :: "nat \<Rightarrow> float \<Rightarrow> float" where | |
| 1453 | "lb_exp prec x = (if 0 < x then float_divl prec 1 (ub_exp prec (-x)) | |
| 31809 | 1454 | else let | 
| 29805 | 1455 | horner = (\<lambda> x. let y = lb_exp_horner prec (get_even (prec + 2)) 1 1 x in if y \<le> 0 then Float 1 -2 else y) | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1456 | in if x < - 1 then (horner (float_divl prec x (- floor_fl x))) ^ nat (- int_floor_fl x) | 
| 29805 | 1457 | else horner x)" | | 
| 1458 | "ub_exp prec x = (if 0 < x then float_divr prec 1 (lb_exp prec (-x)) | |
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1459 | else if x < - 1 then ub_exp_horner prec (get_odd (prec + 2)) 1 1 (float_divr prec x (- floor_fl x)) ^ (nat (- int_floor_fl x)) | 
| 29805 | 1460 | else ub_exp_horner prec (get_odd (prec + 2)) 1 1 x)" | 
| 1461 | by pat_completeness auto | |
| 47600 | 1462 | termination by (relation "measure (\<lambda> v. let (prec, x) = sum_case id id v in (if 0 < x then 1 else 0))", auto) | 
| 29805 | 1463 | |
| 1464 | lemma exp_m1_ge_quarter: "(1 / 4 :: real) \<le> exp (- 1)" | |
| 1465 | proof - | |
| 1466 | have eq4: "4 = Suc (Suc (Suc (Suc 0)))" by auto | |
| 1467 | ||
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1468 | have "1 / 4 = (Float 1 -2)" unfolding Float_num by auto | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1469 | also have "\<dots> \<le> lb_exp_horner 1 (get_even 4) 1 1 (- 1)" | 
| 31809 | 1470 | unfolding get_even_def eq4 | 
| 47621 
4cf6011fb884
hide code generation facts in the Float theory, they are only exported for Approximation
 hoelzl parents: 
47601diff
changeset | 1471 | by (auto simp add: Float.compute_lapprox_rat Float.compute_rapprox_rat | 
| 
4cf6011fb884
hide code generation facts in the Float theory, they are only exported for Approximation
 hoelzl parents: 
47601diff
changeset | 1472 | Float.compute_lapprox_posrat Float.compute_rapprox_posrat rat_precision_def Float.compute_bitlen) | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1473 | also have "\<dots> \<le> exp (- 1 :: float)" using bnds_exp_horner[where x="- 1"] by auto | 
| 47600 | 1474 | finally show ?thesis by simp | 
| 29805 | 1475 | qed | 
| 1476 | ||
| 1477 | lemma lb_exp_pos: assumes "\<not> 0 < x" shows "0 < lb_exp prec x" | |
| 1478 | proof - | |
| 1479 | let "?lb_horner x" = "lb_exp_horner prec (get_even (prec + 2)) 1 1 x" | |
| 1480 | let "?horner x" = "let y = ?lb_horner x in if y \<le> 0 then Float 1 -2 else y" | |
| 47600 | 1481 | have pos_horner: "\<And> x. 0 < ?horner x" unfolding Let_def by (cases "?lb_horner x \<le> 0", auto) | 
| 29805 | 1482 |   moreover { fix x :: float fix num :: nat
 | 
| 47600 | 1483 | have "0 < real (?horner x) ^ num" using `0 < ?horner x` by simp | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1484 | also have "\<dots> = (?horner x) ^ num" by auto | 
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 1485 | finally have "0 < real ((?horner x) ^ num)" . | 
| 29805 | 1486 | } | 
| 1487 | ultimately show ?thesis | |
| 30968 
10fef94f40fc
adaptions due to rearrangment of power operation
 haftmann parents: 
30952diff
changeset | 1488 | unfolding lb_exp.simps if_not_P[OF `\<not> 0 < x`] Let_def | 
| 47600 | 1489 | by (cases "floor_fl x", cases "x < - 1", auto) | 
| 29805 | 1490 | qed | 
| 1491 | ||
| 1492 | lemma exp_boundaries': assumes "x \<le> 0" | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1493 |   shows "exp x \<in> { (lb_exp prec x) .. (ub_exp prec x)}"
 | 
| 29805 | 1494 | proof - | 
| 1495 | let "?lb_exp_horner x" = "lb_exp_horner prec (get_even (prec + 2)) 1 1 x" | |
| 1496 | let "?ub_exp_horner x" = "ub_exp_horner prec (get_odd (prec + 2)) 1 1 x" | |
| 1497 | ||
| 47600 | 1498 | have "real x \<le> 0" and "\<not> x > 0" using `x \<le> 0` by auto | 
| 29805 | 1499 | show ?thesis | 
| 1500 | proof (cases "x < - 1") | |
| 47600 | 1501 | case False hence "- 1 \<le> real x" by auto | 
| 29805 | 1502 | show ?thesis | 
| 1503 | proof (cases "?lb_exp_horner x \<le> 0") | |
| 47600 | 1504 | from `\<not> x < - 1` have "- 1 \<le> real x" by auto | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1505 | hence "exp (- 1) \<le> exp x" unfolding exp_le_cancel_iff . | 
| 29805 | 1506 | from order_trans[OF exp_m1_ge_quarter this] | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1507 | have "Float 1 -2 \<le> exp x" unfolding Float_num . | 
| 29805 | 1508 | moreover case True | 
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 1509 | ultimately show ?thesis using bnds_exp_horner `real x \<le> 0` `\<not> x > 0` `\<not> x < - 1` by auto | 
| 29805 | 1510 | next | 
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 1511 | case False thus ?thesis using bnds_exp_horner `real x \<le> 0` `\<not> x > 0` `\<not> x < - 1` by (auto simp add: Let_def) | 
| 29805 | 1512 | qed | 
| 1513 | next | |
| 1514 | case True | |
| 31809 | 1515 | |
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1516 | let ?num = "nat (- int_floor_fl x)" | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1517 | |
| 47600 | 1518 | have "real (int_floor_fl x) < - 1" using int_floor_fl[of x] `x < - 1` | 
| 1519 | by simp | |
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1520 | hence "real (int_floor_fl x) < 0" by simp | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1521 | hence "int_floor_fl x < 0" by auto | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1522 | hence "1 \<le> - int_floor_fl x" by auto | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1523 | hence "0 < nat (- int_floor_fl x)" by auto | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1524 | hence "0 < ?num" by auto | 
| 29805 | 1525 | hence "real ?num \<noteq> 0" by auto | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1526 | have num_eq: "real ?num = - int_floor_fl x" using `0 < nat (- int_floor_fl x)` by auto | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1527 | have "0 < - int_floor_fl x" using `0 < ?num`[unfolded real_of_nat_less_iff[symmetric]] by simp | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1528 | hence "real (int_floor_fl x) < 0" unfolding less_float_def by auto | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1529 | have fl_eq: "real (- int_floor_fl x) = real (- floor_fl x)" | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1530 | by (simp add: floor_fl_def int_floor_fl_def) | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1531 | from `0 < - int_floor_fl x` have "0 < real (- floor_fl x)" | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1532 | by (simp add: floor_fl_def int_floor_fl_def) | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1533 | from `real (int_floor_fl x) < 0` have "real (floor_fl x) < 0" | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1534 | by (simp add: floor_fl_def int_floor_fl_def) | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1535 | have "exp x \<le> ub_exp prec x" | 
| 29805 | 1536 | proof - | 
| 31809 | 1537 | have div_less_zero: "real (float_divr prec x (- floor_fl x)) \<le> 0" | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1538 | using float_divr_nonpos_pos_upper_bound[OF `real x \<le> 0` `0 < real (- floor_fl x)`] | 
| 47601 
050718fe6eee
use real :: float => real as lifting-morphism so we can directlry use the rep_eq theorems
 hoelzl parents: 
47600diff
changeset | 1539 | unfolding less_eq_float_def zero_float.rep_eq . | 
| 31809 | 1540 | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1541 | have "exp x = exp (?num * (x / ?num))" using `real ?num \<noteq> 0` by auto | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1542 | also have "\<dots> = exp (x / ?num) ^ ?num" unfolding exp_real_of_nat_mult .. | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1543 | also have "\<dots> \<le> exp (float_divr prec x (- floor_fl x)) ^ ?num" unfolding num_eq fl_eq | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 1544 | by (rule power_mono, rule exp_le_cancel_iff[THEN iffD2], rule float_divr) auto | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1545 | also have "\<dots> \<le> (?ub_exp_horner (float_divr prec x (- floor_fl x))) ^ ?num" | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1546 | unfolding real_of_float_power | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 1547 | by (rule power_mono, rule bnds_exp_horner[OF div_less_zero, unfolded atLeastAtMost_iff, THEN conjunct2], auto) | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1548 | finally show ?thesis unfolding ub_exp.simps if_not_P[OF `\<not> 0 < x`] if_P[OF `x < - 1`] floor_fl_def Let_def . | 
| 29805 | 1549 | qed | 
| 31809 | 1550 | moreover | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1551 | have "lb_exp prec x \<le> exp x" | 
| 29805 | 1552 | proof - | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1553 | let ?divl = "float_divl prec x (- floor_fl x)" | 
| 29805 | 1554 | let ?horner = "?lb_exp_horner ?divl" | 
| 31809 | 1555 | |
| 29805 | 1556 | show ?thesis | 
| 1557 | proof (cases "?horner \<le> 0") | |
| 47600 | 1558 | case False hence "0 \<le> real ?horner" by auto | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 1559 | |
| 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 1560 | have div_less_zero: "real (float_divl prec x (- floor_fl x)) \<le> 0" | 
| 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 1561 | using `real (floor_fl x) < 0` `real x \<le> 0` by (auto intro!: order_trans[OF float_divl] divide_nonpos_neg) | 
| 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 1562 | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1563 | have "(?lb_exp_horner (float_divl prec x (- floor_fl x))) ^ ?num \<le> | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1564 | exp (float_divl prec x (- floor_fl x)) ^ ?num" | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1565 | using `0 \<le> real ?horner`[unfolded floor_fl_def[symmetric]] bnds_exp_horner[OF div_less_zero, unfolded atLeastAtMost_iff, THEN conjunct1] by (auto intro!: power_mono) | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1566 | also have "\<dots> \<le> exp (x / ?num) ^ ?num" unfolding num_eq fl_eq | 
| 47601 
050718fe6eee
use real :: float => real as lifting-morphism so we can directlry use the rep_eq theorems
 hoelzl parents: 
47600diff
changeset | 1567 | using float_divl by (auto intro!: power_mono simp del: uminus_float.rep_eq) | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1568 | also have "\<dots> = exp (?num * (x / ?num))" unfolding exp_real_of_nat_mult .. | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1569 | also have "\<dots> = exp x" using `real ?num \<noteq> 0` by auto | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 1570 | finally show ?thesis | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1571 | unfolding lb_exp.simps if_not_P[OF `\<not> 0 < x`] if_P[OF `x < - 1`] int_floor_fl_def Let_def if_not_P[OF False] by auto | 
| 29805 | 1572 | next | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 1573 | case True | 
| 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 1574 | have "real (floor_fl x) \<noteq> 0" and "real (floor_fl x) \<le> 0" using `real (floor_fl x) < 0` by auto | 
| 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 1575 | from divide_right_mono_neg[OF floor_fl[of x] `real (floor_fl x) \<le> 0`, unfolded divide_self[OF `real (floor_fl x) \<noteq> 0`]] | 
| 47601 
050718fe6eee
use real :: float => real as lifting-morphism so we can directlry use the rep_eq theorems
 hoelzl parents: 
47600diff
changeset | 1576 | have "- 1 \<le> x / (- floor_fl x)" unfolding minus_float.rep_eq by auto | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 1577 | from order_trans[OF exp_m1_ge_quarter this[unfolded exp_le_cancel_iff[where x="- 1", symmetric]]] | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1578 | have "Float 1 -2 \<le> exp (x / (- floor_fl x))" unfolding Float_num . | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1579 | hence "real (Float 1 -2) ^ ?num \<le> exp (x / (- floor_fl x)) ^ ?num" | 
| 46545 | 1580 | by (auto intro!: power_mono) | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1581 | also have "\<dots> = exp x" unfolding num_eq fl_eq exp_real_of_nat_mult[symmetric] using `real (floor_fl x) \<noteq> 0` by auto | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 1582 | finally show ?thesis | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1583 | unfolding lb_exp.simps if_not_P[OF `\<not> 0 < x`] if_P[OF `x < - 1`] int_floor_fl_def Let_def if_P[OF True] real_of_float_power . | 
| 29805 | 1584 | qed | 
| 1585 | qed | |
| 1586 | ultimately show ?thesis by auto | |
| 1587 | qed | |
| 1588 | qed | |
| 1589 | ||
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1590 | lemma exp_boundaries: "exp x \<in> { lb_exp prec x .. ub_exp prec x }"
 | 
| 29805 | 1591 | proof - | 
| 1592 | show ?thesis | |
| 1593 | proof (cases "0 < x") | |
| 47600 | 1594 | case False hence "x \<le> 0" by auto | 
| 29805 | 1595 | from exp_boundaries'[OF this] show ?thesis . | 
| 1596 | next | |
| 47600 | 1597 | case True hence "-x \<le> 0" by auto | 
| 31809 | 1598 | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1599 | have "lb_exp prec x \<le> exp x" | 
| 29805 | 1600 | proof - | 
| 1601 | from exp_boundaries'[OF `-x \<le> 0`] | |
| 47601 
050718fe6eee
use real :: float => real as lifting-morphism so we can directlry use the rep_eq theorems
 hoelzl parents: 
47600diff
changeset | 1602 | have ub_exp: "exp (- real x) \<le> ub_exp prec (-x)" unfolding atLeastAtMost_iff minus_float.rep_eq by auto | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1603 | |
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1604 | have "float_divl prec 1 (ub_exp prec (-x)) \<le> 1 / ub_exp prec (-x)" using float_divl[where x=1] by auto | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1605 | also have "\<dots> \<le> exp x" | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 1606 | using ub_exp[unfolded inverse_le_iff_le[OF order_less_le_trans[OF exp_gt_zero ub_exp] exp_gt_zero, symmetric]] | 
| 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 1607 | unfolding exp_minus nonzero_inverse_inverse_eq[OF exp_not_eq_zero] inverse_eq_divide by auto | 
| 29805 | 1608 | finally show ?thesis unfolding lb_exp.simps if_P[OF True] . | 
| 1609 | qed | |
| 1610 | moreover | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1611 | have "exp x \<le> ub_exp prec x" | 
| 29805 | 1612 | proof - | 
| 47600 | 1613 | have "\<not> 0 < -x" using `0 < x` by auto | 
| 31809 | 1614 | |
| 29805 | 1615 | from exp_boundaries'[OF `-x \<le> 0`] | 
| 47601 
050718fe6eee
use real :: float => real as lifting-morphism so we can directlry use the rep_eq theorems
 hoelzl parents: 
47600diff
changeset | 1616 | have lb_exp: "lb_exp prec (-x) \<le> exp (- real x)" unfolding atLeastAtMost_iff minus_float.rep_eq by auto | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1617 | |
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1618 | have "exp x \<le> (1 :: float) / lb_exp prec (-x)" | 
| 47600 | 1619 | using lb_exp lb_exp_pos[OF `\<not> 0 < -x`, of prec] | 
| 1620 | by (simp del: lb_exp.simps add: exp_minus inverse_eq_divide field_simps) | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1621 | also have "\<dots> \<le> float_divr prec 1 (lb_exp prec (-x))" using float_divr . | 
| 29805 | 1622 | finally show ?thesis unfolding ub_exp.simps if_P[OF True] . | 
| 1623 | qed | |
| 1624 | ultimately show ?thesis by auto | |
| 1625 | qed | |
| 1626 | qed | |
| 1627 | ||
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1628 | lemma bnds_exp: "\<forall> (x::real) lx ux. (l, u) = (lb_exp prec lx, ub_exp prec ux) \<and> x \<in> {lx .. ux} \<longrightarrow> l \<le> exp x \<and> exp x \<le> u"
 | 
| 29805 | 1629 | proof (rule allI, rule allI, rule allI, rule impI) | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1630 | fix x::real and lx ux | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1631 |   assume "(l, u) = (lb_exp prec lx, ub_exp prec ux) \<and> x \<in> {lx .. ux}"
 | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1632 |   hence l: "lb_exp prec lx = l " and u: "ub_exp prec ux = u" and x: "x \<in> {lx .. ux}" by auto
 | 
| 29805 | 1633 | |
| 1634 |   { from exp_boundaries[of lx prec, unfolded l]
 | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1635 | have "l \<le> exp lx" by (auto simp del: lb_exp.simps) | 
| 29805 | 1636 | also have "\<dots> \<le> exp x" using x by auto | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1637 | finally have "l \<le> exp x" . | 
| 29805 | 1638 | } moreover | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1639 |   { have "exp x \<le> exp ux" using x by auto
 | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1640 | also have "\<dots> \<le> u" using exp_boundaries[of ux prec, unfolded u] by (auto simp del: ub_exp.simps) | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1641 | finally have "exp x \<le> u" . | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1642 | } ultimately show "l \<le> exp x \<and> exp x \<le> u" .. | 
| 29805 | 1643 | qed | 
| 1644 | ||
| 1645 | section "Logarithm" | |
| 1646 | ||
| 1647 | subsection "Compute the logarithm series" | |
| 1648 | ||
| 31809 | 1649 | fun ub_ln_horner :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> float \<Rightarrow> float" | 
| 29805 | 1650 | and lb_ln_horner :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> float \<Rightarrow> float" where | 
| 1651 | "ub_ln_horner prec 0 i x = 0" | | |
| 1652 | "ub_ln_horner prec (Suc n) i x = rapprox_rat prec 1 (int i) - x * lb_ln_horner prec n (Suc i) x" | | |
| 1653 | "lb_ln_horner prec 0 i x = 0" | | |
| 1654 | "lb_ln_horner prec (Suc n) i x = lapprox_rat prec 1 (int i) - x * ub_ln_horner prec n (Suc i) x" | |
| 1655 | ||
| 1656 | lemma ln_bounds: | |
| 1657 | assumes "0 \<le> x" and "x < 1" | |
| 30952 
7ab2716dd93b
power operation on functions with syntax o^; power operation on relations with syntax ^^
 haftmann parents: 
30886diff
changeset | 1658 | shows "(\<Sum>i=0..<2*n. -1^i * (1 / real (i + 1)) * x ^ (Suc i)) \<le> ln (x + 1)" (is "?lb") | 
| 
7ab2716dd93b
power operation on functions with syntax o^; power operation on relations with syntax ^^
 haftmann parents: 
30886diff
changeset | 1659 | and "ln (x + 1) \<le> (\<Sum>i=0..<2*n + 1. -1^i * (1 / real (i + 1)) * x ^ (Suc i))" (is "?ub") | 
| 29805 | 1660 | proof - | 
| 30952 
7ab2716dd93b
power operation on functions with syntax o^; power operation on relations with syntax ^^
 haftmann parents: 
30886diff
changeset | 1661 | let "?a n" = "(1/real (n +1)) * x ^ (Suc n)" | 
| 29805 | 1662 | |
| 1663 | have ln_eq: "(\<Sum> i. -1^i * ?a i) = ln (x + 1)" | |
| 1664 | using ln_series[of "x + 1"] `0 \<le> x` `x < 1` by auto | |
| 1665 | ||
| 1666 | have "norm x < 1" using assms by auto | |
| 31809 | 1667 | have "?a ----> 0" unfolding Suc_eq_plus1[symmetric] inverse_eq_divide[symmetric] | 
| 44568 
e6f291cb5810
discontinue many legacy theorems about LIM and LIMSEQ, in favor of tendsto theorems
 huffman parents: 
44349diff
changeset | 1668 | using tendsto_mult[OF LIMSEQ_inverse_real_of_nat LIMSEQ_Suc[OF LIMSEQ_power_zero[OF `norm x < 1`]]] by auto | 
| 29805 | 1669 |   { fix n have "0 \<le> ?a n" by (rule mult_nonneg_nonneg, auto intro!: mult_nonneg_nonneg simp add: `0 \<le> x`) }
 | 
| 1670 |   { fix n have "?a (Suc n) \<le> ?a n" unfolding inverse_eq_divide[symmetric]
 | |
| 1671 | proof (rule mult_mono) | |
| 1672 | show "0 \<le> x ^ Suc (Suc n)" by (auto intro!: mult_nonneg_nonneg simp add: `0 \<le> x`) | |
| 36778 
739a9379e29b
avoid using real-specific versions of generic lemmas
 huffman parents: 
36531diff
changeset | 1673 | have "x ^ Suc (Suc n) \<le> x ^ Suc n * 1" unfolding power_Suc2 mult_assoc[symmetric] | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 1674 | by (rule mult_left_mono, fact less_imp_le[OF `x < 1`], auto intro!: mult_nonneg_nonneg simp add: `0 \<le> x`) | 
| 29805 | 1675 | thus "x ^ Suc (Suc n) \<le> x ^ Suc n" by auto | 
| 1676 | qed auto } | |
| 1677 | from summable_Leibniz'(2,4)[OF `?a ----> 0` `\<And>n. 0 \<le> ?a n`, OF `\<And>n. ?a (Suc n) \<le> ?a n`, unfolded ln_eq] | |
| 1678 | show "?lb" and "?ub" by auto | |
| 1679 | qed | |
| 1680 | ||
| 31809 | 1681 | lemma ln_float_bounds: | 
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 1682 | assumes "0 \<le> real x" and "real x < 1" | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1683 | shows "x * lb_ln_horner prec (get_even n) 1 x \<le> ln (x + 1)" (is "?lb \<le> ?ln") | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1684 | and "ln (x + 1) \<le> x * ub_ln_horner prec (get_odd n) 1 x" (is "?ln \<le> ?ub") | 
| 29805 | 1685 | proof - | 
| 1686 | obtain ev where ev: "get_even n = 2 * ev" using get_even_double .. | |
| 1687 | obtain od where od: "get_odd n = 2 * od + 1" using get_odd_double .. | |
| 1688 | ||
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 1689 | let "?s n" = "-1^n * (1 / real (1 + n)) * (real x)^(Suc n)" | 
| 29805 | 1690 | |
| 47601 
050718fe6eee
use real :: float => real as lifting-morphism so we can directlry use the rep_eq theorems
 hoelzl parents: 
47600diff
changeset | 1691 |   have "?lb \<le> setsum ?s {0 ..< 2 * ev}" unfolding power_Suc2 mult_assoc[symmetric] times_float.rep_eq setsum_left_distrib[symmetric] unfolding mult_commute[of "real x"] ev
 | 
| 29805 | 1692 | using horner_bounds(1)[where G="\<lambda> i k. Suc k" and F="\<lambda>x. x" and f="\<lambda>x. x" and lb="\<lambda>n i k x. lb_ln_horner prec n k x" and ub="\<lambda>n i k x. ub_ln_horner prec n k x" and j'=1 and n="2*ev", | 
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 1693 | OF `0 \<le> real x` refl lb_ln_horner.simps ub_ln_horner.simps] `0 \<le> real x` | 
| 29805 | 1694 | by (rule mult_right_mono) | 
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 1695 | also have "\<dots> \<le> ?ln" using ln_bounds(1)[OF `0 \<le> real x` `real x < 1`] by auto | 
| 31809 | 1696 | finally show "?lb \<le> ?ln" . | 
| 29805 | 1697 | |
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 1698 |   have "?ln \<le> setsum ?s {0 ..< 2 * od + 1}" using ln_bounds(2)[OF `0 \<le> real x` `real x < 1`] by auto
 | 
| 47601 
050718fe6eee
use real :: float => real as lifting-morphism so we can directlry use the rep_eq theorems
 hoelzl parents: 
47600diff
changeset | 1699 | also have "\<dots> \<le> ?ub" unfolding power_Suc2 mult_assoc[symmetric] times_float.rep_eq setsum_left_distrib[symmetric] unfolding mult_commute[of "real x"] od | 
| 29805 | 1700 | using horner_bounds(2)[where G="\<lambda> i k. Suc k" and F="\<lambda>x. x" and f="\<lambda>x. x" and lb="\<lambda>n i k x. lb_ln_horner prec n k x" and ub="\<lambda>n i k x. ub_ln_horner prec n k x" and j'=1 and n="2*od+1", | 
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 1701 | OF `0 \<le> real x` refl lb_ln_horner.simps ub_ln_horner.simps] `0 \<le> real x` | 
| 29805 | 1702 | by (rule mult_right_mono) | 
| 31809 | 1703 | finally show "?ln \<le> ?ub" . | 
| 29805 | 1704 | qed | 
| 1705 | ||
| 1706 | lemma ln_add: assumes "0 < x" and "0 < y" shows "ln (x + y) = ln x + ln (1 + y / x)" | |
| 1707 | proof - | |
| 1708 | have "x \<noteq> 0" using assms by auto | |
| 1709 | have "x + y = x * (1 + y / x)" unfolding right_distrib times_divide_eq_right nonzero_mult_divide_cancel_left[OF `x \<noteq> 0`] by auto | |
| 31809 | 1710 | moreover | 
| 29805 | 1711 | have "0 < y / x" using assms divide_pos_pos by auto | 
| 1712 | hence "0 < 1 + y / x" by auto | |
| 1713 | ultimately show ?thesis using ln_mult assms by auto | |
| 1714 | qed | |
| 1715 | ||
| 1716 | subsection "Compute the logarithm of 2" | |
| 1717 | ||
| 31809 | 1718 | definition ub_ln2 where "ub_ln2 prec = (let third = rapprox_rat (max prec 1) 1 3 | 
| 1719 | in (Float 1 -1 * ub_ln_horner prec (get_odd prec) 1 (Float 1 -1)) + | |
| 29805 | 1720 | (third * ub_ln_horner prec (get_odd prec) 1 third))" | 
| 31809 | 1721 | definition lb_ln2 where "lb_ln2 prec = (let third = lapprox_rat prec 1 3 | 
| 1722 | in (Float 1 -1 * lb_ln_horner prec (get_even prec) 1 (Float 1 -1)) + | |
| 29805 | 1723 | (third * lb_ln_horner prec (get_even prec) 1 third))" | 
| 1724 | ||
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1725 | lemma ub_ln2: "ln 2 \<le> ub_ln2 prec" (is "?ub_ln2") | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1726 | and lb_ln2: "lb_ln2 prec \<le> ln 2" (is "?lb_ln2") | 
| 29805 | 1727 | proof - | 
| 1728 | let ?uthird = "rapprox_rat (max prec 1) 1 3" | |
| 1729 | let ?lthird = "lapprox_rat prec 1 3" | |
| 1730 | ||
| 1731 | have ln2_sum: "ln 2 = ln (1/2 + 1) + ln (1 / 3 + 1)" | |
| 1732 | using ln_add[of "3 / 2" "1 / 2"] by auto | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1733 | have lb3: "?lthird \<le> 1 / 3" using lapprox_rat[of prec 1 3] by auto | 
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 1734 | hence lb3_ub: "real ?lthird < 1" by auto | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1735 | have lb3_lb: "0 \<le> real ?lthird" using lapprox_rat_nonneg[of 1 3] by auto | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1736 | have ub3: "1 / 3 \<le> ?uthird" using rapprox_rat[of 1 3] by auto | 
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 1737 | hence ub3_lb: "0 \<le> real ?uthird" by auto | 
| 29805 | 1738 | |
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 1739 | have lb2: "0 \<le> real (Float 1 -1)" and ub2: "real (Float 1 -1) < 1" unfolding Float_num by auto | 
| 29805 | 1740 | |
| 1741 | have "0 \<le> (1::int)" and "0 < (3::int)" by auto | |
| 47621 
4cf6011fb884
hide code generation facts in the Float theory, they are only exported for Approximation
 hoelzl parents: 
47601diff
changeset | 1742 | have ub3_ub: "real ?uthird < 1" by (simp add: Float.compute_rapprox_rat rapprox_posrat_less1) | 
| 29805 | 1743 | |
| 1744 | have third_gt0: "(0 :: real) < 1 / 3 + 1" by auto | |
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 1745 | have uthird_gt0: "0 < real ?uthird + 1" using ub3_lb by auto | 
| 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 1746 | have lthird_gt0: "0 < real ?lthird + 1" using lb3_lb by auto | 
| 29805 | 1747 | |
| 47601 
050718fe6eee
use real :: float => real as lifting-morphism so we can directlry use the rep_eq theorems
 hoelzl parents: 
47600diff
changeset | 1748 | show ?ub_ln2 unfolding ub_ln2_def Let_def plus_float.rep_eq ln2_sum Float_num(4)[symmetric] | 
| 29805 | 1749 | proof (rule add_mono, fact ln_float_bounds(2)[OF lb2 ub2]) | 
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 1750 | have "ln (1 / 3 + 1) \<le> ln (real ?uthird + 1)" unfolding ln_le_cancel_iff[OF third_gt0 uthird_gt0] using ub3 by auto | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1751 | also have "\<dots> \<le> ?uthird * ub_ln_horner prec (get_odd prec) 1 ?uthird" | 
| 29805 | 1752 | using ln_float_bounds(2)[OF ub3_lb ub3_ub] . | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1753 | finally show "ln (1 / 3 + 1) \<le> ?uthird * ub_ln_horner prec (get_odd prec) 1 ?uthird" . | 
| 29805 | 1754 | qed | 
| 47601 
050718fe6eee
use real :: float => real as lifting-morphism so we can directlry use the rep_eq theorems
 hoelzl parents: 
47600diff
changeset | 1755 | show ?lb_ln2 unfolding lb_ln2_def Let_def plus_float.rep_eq ln2_sum Float_num(4)[symmetric] | 
| 29805 | 1756 | proof (rule add_mono, fact ln_float_bounds(1)[OF lb2 ub2]) | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1757 | have "?lthird * lb_ln_horner prec (get_even prec) 1 ?lthird \<le> ln (real ?lthird + 1)" | 
| 29805 | 1758 | using ln_float_bounds(1)[OF lb3_lb lb3_ub] . | 
| 1759 | also have "\<dots> \<le> ln (1 / 3 + 1)" unfolding ln_le_cancel_iff[OF lthird_gt0 third_gt0] using lb3 by auto | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1760 | finally show "?lthird * lb_ln_horner prec (get_even prec) 1 ?lthird \<le> ln (1 / 3 + 1)" . | 
| 29805 | 1761 | qed | 
| 1762 | qed | |
| 1763 | ||
| 1764 | subsection "Compute the logarithm in the entire domain" | |
| 1765 | ||
| 1766 | function ub_ln :: "nat \<Rightarrow> float \<Rightarrow> float option" and lb_ln :: "nat \<Rightarrow> float \<Rightarrow> float option" where | |
| 31468 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 hoelzl parents: 
31467diff
changeset | 1767 | "ub_ln prec x = (if x \<le> 0 then None | 
| 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 hoelzl parents: 
31467diff
changeset | 1768 | else if x < 1 then Some (- the (lb_ln prec (float_divl (max prec 1) 1 x))) | 
| 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 hoelzl parents: 
31467diff
changeset | 1769 | else let horner = \<lambda>x. x * ub_ln_horner prec (get_odd prec) 1 x in | 
| 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 hoelzl parents: 
31467diff
changeset | 1770 | if x \<le> Float 3 -1 then Some (horner (x - 1)) | 
| 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 hoelzl parents: 
31467diff
changeset | 1771 | else if x < Float 1 1 then Some (horner (Float 1 -1) + horner (x * rapprox_rat prec 2 3 - 1)) | 
| 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 hoelzl parents: 
31467diff
changeset | 1772 | else let l = bitlen (mantissa x) - 1 in | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1773 | Some (ub_ln2 prec * (Float (exponent x + l) 0) + horner (Float (mantissa x) (- l) - 1)))" | | 
| 31468 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 hoelzl parents: 
31467diff
changeset | 1774 | "lb_ln prec x = (if x \<le> 0 then None | 
| 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 hoelzl parents: 
31467diff
changeset | 1775 | else if x < 1 then Some (- the (ub_ln prec (float_divr prec 1 x))) | 
| 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 hoelzl parents: 
31467diff
changeset | 1776 | else let horner = \<lambda>x. x * lb_ln_horner prec (get_even prec) 1 x in | 
| 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 hoelzl parents: 
31467diff
changeset | 1777 | if x \<le> Float 3 -1 then Some (horner (x - 1)) | 
| 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 hoelzl parents: 
31467diff
changeset | 1778 | else if x < Float 1 1 then Some (horner (Float 1 -1) + | 
| 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 hoelzl parents: 
31467diff
changeset | 1779 | horner (max (x * lapprox_rat prec 2 3 - 1) 0)) | 
| 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 hoelzl parents: 
31467diff
changeset | 1780 | else let l = bitlen (mantissa x) - 1 in | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1781 | Some (lb_ln2 prec * (Float (exponent x + l) 0) + horner (Float (mantissa x) (- l) - 1)))" | 
| 29805 | 1782 | by pat_completeness auto | 
| 1783 | ||
| 1784 | termination proof (relation "measure (\<lambda> v. let (prec, x) = sum_case id id v in (if x < 1 then 1 else 0))", auto) | |
| 47600 | 1785 | fix prec and x :: float assume "\<not> real x \<le> 0" and "real x < 1" and "real (float_divl (max prec (Suc 0)) 1 x) < 1" | 
| 1786 | hence "0 < real x" "1 \<le> max prec (Suc 0)" "real x < 1" by auto | |
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1787 | from float_divl_pos_less1_bound[OF `0 < real x` `real x < 1` `1 \<le> max prec (Suc 0)`] | 
| 47600 | 1788 | show False using `real (float_divl (max prec (Suc 0)) 1 x) < 1` by auto | 
| 29805 | 1789 | next | 
| 47600 | 1790 | fix prec x assume "\<not> real x \<le> 0" and "real x < 1" and "real (float_divr prec 1 x) < 1" | 
| 1791 | hence "0 < x" by auto | |
| 1792 | from float_divr_pos_less1_lower_bound[OF `0 < x`, of prec] `real x < 1` | |
| 1793 | show False using `real (float_divr prec 1 x) < 1` by auto | |
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1794 | qed | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1795 | |
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1796 | lemma float_pos_eq_mantissa_pos: "x > 0 \<longleftrightarrow> mantissa x > 0" | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1797 | apply (subst Float_mantissa_exponent[of x, symmetric]) | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1798 | apply (auto simp add: zero_less_mult_iff zero_float_def powr_gt_zero[of 2 "exponent x"] dest: less_zeroE) | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1799 | using powr_gt_zero[of 2 "exponent x"] | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1800 | apply simp | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1801 | done | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1802 | |
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1803 | lemma Float_pos_eq_mantissa_pos: "Float m e > 0 \<longleftrightarrow> m > 0" | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1804 | apply (auto simp add: zero_less_mult_iff zero_float_def powr_gt_zero[of 2 "exponent x"] dest: less_zeroE) | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1805 | using powr_gt_zero[of 2 "e"] | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1806 | apply simp | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1807 | done | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1808 | |
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1809 | lemma Float_representation_aux: | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1810 | fixes m e | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1811 | defines "x \<equiv> Float m e" | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1812 | assumes "x > 0" | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1813 | shows "Float (exponent x + (bitlen (mantissa x) - 1)) 0 = Float (e + (bitlen m - 1)) 0" (is ?th1) | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1814 | and "Float (mantissa x) (- (bitlen (mantissa x) - 1)) = Float m ( - (bitlen m - 1))" (is ?th2) | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1815 | proof - | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1816 | from assms have mantissa_pos: "m > 0" "mantissa x > 0" | 
| 47600 | 1817 | using Float_pos_eq_mantissa_pos[of m e] float_pos_eq_mantissa_pos[of x] by simp_all | 
| 1818 | thus ?th1 using bitlen_Float[of m e] assms by (auto simp add: zero_less_mult_iff intro!: arg_cong2[where f=Float]) | |
| 1819 | have "x \<noteq> float_of 0" | |
| 1820 | unfolding zero_float_def[symmetric] using `0 < x` by auto | |
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1821 | from denormalize_shift[OF assms(1) this] guess i . note i = this | 
| 47600 | 1822 | |
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1823 | have "2 powr (1 - (real (bitlen (mantissa x)) + real i)) = | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1824 | 2 powr (1 - (real (bitlen (mantissa x)))) * inverse (2 powr (real i))" | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1825 | by (simp add: powr_minus[symmetric] powr_add[symmetric] field_simps) | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1826 | hence "real (mantissa x) * 2 powr (1 - real (bitlen (mantissa x))) = | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1827 | (real (mantissa x) * 2 ^ i) * 2 powr (1 - real (bitlen (mantissa x * 2 ^ i)))" | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1828 | using `mantissa x > 0` by (simp add: powr_realpow) | 
| 47600 | 1829 | then show ?th2 | 
| 1830 | unfolding i by transfer auto | |
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1831 | qed | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1832 | |
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1833 | lemma compute_ln[code]: | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1834 | fixes m e | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1835 | defines "x \<equiv> Float m e" | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1836 | shows "ub_ln prec x = (if x \<le> 0 then None | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1837 | else if x < 1 then Some (- the (lb_ln prec (float_divl (max prec 1) 1 x))) | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1838 | else let horner = \<lambda>x. x * ub_ln_horner prec (get_odd prec) 1 x in | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1839 | if x \<le> Float 3 -1 then Some (horner (x - 1)) | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1840 | else if x < Float 1 1 then Some (horner (Float 1 -1) + horner (x * rapprox_rat prec 2 3 - 1)) | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1841 | else let l = bitlen m - 1 in | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1842 | Some (ub_ln2 prec * (Float (e + l) 0) + horner (Float m (- l) - 1)))" | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1843 | (is ?th1) | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1844 | and "lb_ln prec x = (if x \<le> 0 then None | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1845 | else if x < 1 then Some (- the (ub_ln prec (float_divr prec 1 x))) | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1846 | else let horner = \<lambda>x. x * lb_ln_horner prec (get_even prec) 1 x in | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1847 | if x \<le> Float 3 -1 then Some (horner (x - 1)) | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1848 | else if x < Float 1 1 then Some (horner (Float 1 -1) + | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1849 | horner (max (x * lapprox_rat prec 2 3 - 1) 0)) | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1850 | else let l = bitlen m - 1 in | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1851 | Some (lb_ln2 prec * (Float (e + l) 0) + horner (Float m (- l) - 1)))" | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1852 | (is ?th2) | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1853 | proof - | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1854 | from assms Float_pos_eq_mantissa_pos have "x > 0 \<Longrightarrow> m > 0" by simp | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1855 | thus ?th1 ?th2 using Float_representation_aux[of m e] unfolding x_def[symmetric] | 
| 47600 | 1856 | by (auto dest: not_leE) | 
| 29805 | 1857 | qed | 
| 1858 | ||
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1859 | lemma ln_shifted_float: assumes "0 < m" shows "ln (Float m e) = ln 2 * (e + (bitlen m - 1)) + ln (Float m (- (bitlen m - 1)))" | 
| 29805 | 1860 | proof - | 
| 1861 | let ?B = "2^nat (bitlen m - 1)" | |
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1862 | def bl \<equiv> "bitlen m - 1" | 
| 29805 | 1863 | have "0 < real m" and "\<And>X. (0 :: real) < 2^X" and "0 < (2 :: real)" and "m \<noteq> 0" using assms by auto | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1864 | hence "0 \<le> bl" by (simp add: bitlen_def bl_def) | 
| 31468 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 hoelzl parents: 
31467diff
changeset | 1865 | show ?thesis | 
| 29805 | 1866 | proof (cases "0 \<le> e") | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1867 | case True | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1868 | thus ?thesis | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1869 | unfolding bl_def[symmetric] using `0 < real m` `0 \<le> bl` | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1870 | apply (simp add: ln_mult) | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1871 | apply (cases "e=0") | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1872 | apply (cases "bl = 0", simp_all add: powr_minus ln_inverse ln_powr) | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1873 | apply (cases "bl = 0", simp_all add: powr_minus ln_inverse ln_powr field_simps) | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1874 | done | 
| 29805 | 1875 | next | 
| 1876 | case False hence "0 < -e" by auto | |
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1877 | have lne: "ln (2 powr real e) = ln (inverse (2 powr - e))" by (simp add: powr_minus) | 
| 29805 | 1878 | hence pow_gt0: "(0::real) < 2^nat (-e)" by auto | 
| 1879 | hence inv_gt0: "(0::real) < inverse (2^nat (-e))" by auto | |
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1880 | show ?thesis using False unfolding bl_def[symmetric] using `0 < real m` `0 \<le> bl` | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1881 | apply (simp add: ln_mult lne) | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1882 | apply (cases "e=0") | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1883 | apply (cases "bl = 0", simp_all add: powr_minus ln_inverse ln_powr) | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1884 | apply (simp add: ln_inverse lne) | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1885 | apply (cases "bl = 0", simp_all add: ln_inverse ln_powr field_simps) | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1886 | done | 
| 29805 | 1887 | qed | 
| 1888 | qed | |
| 1889 | ||
| 1890 | lemma ub_ln_lb_ln_bounds': assumes "1 \<le> x" | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1891 | shows "the (lb_ln prec x) \<le> ln x \<and> ln x \<le> the (ub_ln prec x)" | 
| 29805 | 1892 | (is "?lb \<le> ?ln \<and> ?ln \<le> ?ub") | 
| 1893 | proof (cases "x < Float 1 1") | |
| 31468 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 hoelzl parents: 
31467diff
changeset | 1894 | case True | 
| 47600 | 1895 | hence "real (x - 1) < 1" and "real x < 2" by auto | 
| 1896 | have "\<not> x \<le> 0" and "\<not> x < 1" using `1 \<le> x` by auto | |
| 1897 | hence "0 \<le> real (x - 1)" using `1 \<le> x` by auto | |
| 31468 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 hoelzl parents: 
31467diff
changeset | 1898 | |
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1899 | have [simp]: "(Float 3 -1) = 3 / 2" by simp | 
| 31468 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 hoelzl parents: 
31467diff
changeset | 1900 | |
| 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 hoelzl parents: 
31467diff
changeset | 1901 | show ?thesis | 
| 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 hoelzl parents: 
31467diff
changeset | 1902 | proof (cases "x \<le> Float 3 -1") | 
| 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 hoelzl parents: 
31467diff
changeset | 1903 | case True | 
| 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 hoelzl parents: 
31467diff
changeset | 1904 | show ?thesis unfolding lb_ln.simps unfolding ub_ln.simps Let_def | 
| 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 hoelzl parents: 
31467diff
changeset | 1905 | using ln_float_bounds[OF `0 \<le> real (x - 1)` `real (x - 1) < 1`, of prec] `\<not> x \<le> 0` `\<not> x < 1` True | 
| 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 hoelzl parents: 
31467diff
changeset | 1906 | by auto | 
| 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 hoelzl parents: 
31467diff
changeset | 1907 | next | 
| 47600 | 1908 | case False hence *: "3 / 2 < x" by auto | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1909 | |
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1910 | with ln_add[of "3 / 2" "x - 3 / 2"] | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1911 | have add: "ln x = ln (3 / 2) + ln (real x * 2 / 3)" | 
| 31468 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 hoelzl parents: 
31467diff
changeset | 1912 | by (auto simp add: algebra_simps diff_divide_distrib) | 
| 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 hoelzl parents: 
31467diff
changeset | 1913 | |
| 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 hoelzl parents: 
31467diff
changeset | 1914 | let "?ub_horner x" = "x * ub_ln_horner prec (get_odd prec) 1 x" | 
| 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 hoelzl parents: 
31467diff
changeset | 1915 | let "?lb_horner x" = "x * lb_ln_horner prec (get_even prec) 1 x" | 
| 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 hoelzl parents: 
31467diff
changeset | 1916 | |
| 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 hoelzl parents: 
31467diff
changeset | 1917 |     { have up: "real (rapprox_rat prec 2 3) \<le> 1"
 | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 1918 | by (rule rapprox_rat_le1) simp_all | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1919 | have low: "2 / 3 \<le> rapprox_rat prec 2 3" | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 1920 | by (rule order_trans[OF _ rapprox_rat]) simp | 
| 31468 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 hoelzl parents: 
31467diff
changeset | 1921 | from mult_less_le_imp_less[OF * low] * | 
| 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 hoelzl parents: 
31467diff
changeset | 1922 | have pos: "0 < real (x * rapprox_rat prec 2 3 - 1)" by auto | 
| 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 hoelzl parents: 
31467diff
changeset | 1923 | |
| 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 hoelzl parents: 
31467diff
changeset | 1924 | have "ln (real x * 2/3) | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 1925 | \<le> ln (real (x * rapprox_rat prec 2 3 - 1) + 1)" | 
| 31468 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 hoelzl parents: 
31467diff
changeset | 1926 | proof (rule ln_le_cancel_iff[symmetric, THEN iffD1]) | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 1927 | show "real x * 2 / 3 \<le> real (x * rapprox_rat prec 2 3 - 1) + 1" | 
| 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 1928 | using * low by auto | 
| 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 1929 | show "0 < real x * 2 / 3" using * by simp | 
| 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 1930 | show "0 < real (x * rapprox_rat prec 2 3 - 1) + 1" using pos by auto | 
| 31468 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 hoelzl parents: 
31467diff
changeset | 1931 | qed | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1932 | also have "\<dots> \<le> ?ub_horner (x * rapprox_rat prec 2 3 - 1)" | 
| 31468 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 hoelzl parents: 
31467diff
changeset | 1933 | proof (rule ln_float_bounds(2)) | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 1934 | from mult_less_le_imp_less[OF `real x < 2` up] low * | 
| 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 1935 | show "real (x * rapprox_rat prec 2 3 - 1) < 1" by auto | 
| 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 1936 | show "0 \<le> real (x * rapprox_rat prec 2 3 - 1)" using pos by auto | 
| 31468 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 hoelzl parents: 
31467diff
changeset | 1937 | qed | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1938 | finally have "ln x | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1939 | \<le> ?ub_horner (Float 1 -1) | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1940 | + ?ub_horner (x * rapprox_rat prec 2 3 - 1)" | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 1941 | using ln_float_bounds(2)[of "Float 1 -1" prec prec] add by auto } | 
| 31468 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 hoelzl parents: 
31467diff
changeset | 1942 | moreover | 
| 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 hoelzl parents: 
31467diff
changeset | 1943 |     { let ?max = "max (x * lapprox_rat prec 2 3 - 1) 0"
 | 
| 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 hoelzl parents: 
31467diff
changeset | 1944 | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1945 | have up: "lapprox_rat prec 2 3 \<le> 2/3" | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 1946 | by (rule order_trans[OF lapprox_rat], simp) | 
| 31468 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 hoelzl parents: 
31467diff
changeset | 1947 | |
| 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 hoelzl parents: 
31467diff
changeset | 1948 | have low: "0 \<le> real (lapprox_rat prec 2 3)" | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1949 | using lapprox_rat_nonneg[of 2 3 prec] by simp | 
| 31468 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 hoelzl parents: 
31467diff
changeset | 1950 | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1951 | have "?lb_horner ?max | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 1952 | \<le> ln (real ?max + 1)" | 
| 31468 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 hoelzl parents: 
31467diff
changeset | 1953 | proof (rule ln_float_bounds(1)) | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 1954 | from mult_less_le_imp_less[OF `real x < 2` up] * low | 
| 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 1955 | show "real ?max < 1" by (cases "real (lapprox_rat prec 2 3) = 0", | 
| 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 1956 | auto simp add: real_of_float_max) | 
| 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 1957 | show "0 \<le> real ?max" by (auto simp add: real_of_float_max) | 
| 31468 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 hoelzl parents: 
31467diff
changeset | 1958 | qed | 
| 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 hoelzl parents: 
31467diff
changeset | 1959 | also have "\<dots> \<le> ln (real x * 2/3)" | 
| 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 hoelzl parents: 
31467diff
changeset | 1960 | proof (rule ln_le_cancel_iff[symmetric, THEN iffD1]) | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 1961 | show "0 < real ?max + 1" by (auto simp add: real_of_float_max) | 
| 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 1962 | show "0 < real x * 2/3" using * by auto | 
| 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 1963 | show "real ?max + 1 \<le> real x * 2/3" using * up | 
| 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 1964 | by (cases "0 < real x * real (lapprox_posrat prec 2 3) - 1", | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1965 | auto simp add: max_def) | 
| 31468 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 hoelzl parents: 
31467diff
changeset | 1966 | qed | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1967 | finally have "?lb_horner (Float 1 -1) + ?lb_horner ?max | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1968 | \<le> ln x" | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 1969 | using ln_float_bounds(1)[of "Float 1 -1" prec prec] add by auto } | 
| 31468 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 hoelzl parents: 
31467diff
changeset | 1970 | ultimately | 
| 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 hoelzl parents: 
31467diff
changeset | 1971 | show ?thesis unfolding lb_ln.simps unfolding ub_ln.simps Let_def | 
| 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 hoelzl parents: 
31467diff
changeset | 1972 | using `\<not> x \<le> 0` `\<not> x < 1` True False by auto | 
| 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 hoelzl parents: 
31467diff
changeset | 1973 | qed | 
| 29805 | 1974 | next | 
| 1975 | case False | |
| 31468 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 hoelzl parents: 
31467diff
changeset | 1976 | hence "\<not> x \<le> 0" and "\<not> x < 1" "0 < x" "\<not> x \<le> Float 3 -1" | 
| 47600 | 1977 | using `1 \<le> x` by auto | 
| 29805 | 1978 | show ?thesis | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1979 | proof - | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1980 | def m \<equiv> "mantissa x" | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1981 | def e \<equiv> "exponent x" | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1982 | from Float_mantissa_exponent[of x] have Float: "x = Float m e" by (simp add: m_def e_def) | 
| 29805 | 1983 | let ?s = "Float (e + (bitlen m - 1)) 0" | 
| 1984 | let ?x = "Float m (- (bitlen m - 1))" | |
| 1985 | ||
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1986 | have "0 < m" and "m \<noteq> 0" using `0 < x` Float powr_gt_zero[of 2 e] | 
| 47600 | 1987 | by (auto simp: zero_less_mult_iff) | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1988 | def bl \<equiv> "bitlen m - 1" hence "bl \<ge> 0" using `m > 0` by (simp add: bitlen_def) | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1989 | have "1 \<le> Float m e" using `1 \<le> x` Float unfolding less_eq_float_def by auto | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1990 | from bitlen_div[OF `0 < m`] float_gt1_scale[OF `1 \<le> Float m e`] `bl \<ge> 0` | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1991 | have x_bnds: "0 \<le> real (?x - 1)" "real (?x - 1) < 1" | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1992 | unfolding bl_def[symmetric] | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1993 | by (auto simp: powr_realpow[symmetric] field_simps inverse_eq_divide) | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 1994 | (auto simp : powr_minus field_simps inverse_eq_divide) | 
| 29805 | 1995 | |
| 1996 |     {
 | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 1997 | have "lb_ln2 prec * ?s \<le> ln 2 * (e + (bitlen m - 1))" (is "?lb2 \<le> _") | 
| 47601 
050718fe6eee
use real :: float => real as lifting-morphism so we can directlry use the rep_eq theorems
 hoelzl parents: 
47600diff
changeset | 1998 | unfolding nat_0 power_0 mult_1_right times_float.rep_eq | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 1999 | using lb_ln2[of prec] | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 2000 | proof (rule mult_mono) | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 2001 | from float_gt1_scale[OF `1 \<le> Float m e`] | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 2002 | show "0 \<le> real (Float (e + (bitlen m - 1)) 0)" by simp | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 2003 | qed auto | 
| 29805 | 2004 | moreover | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 2005 | from ln_float_bounds(1)[OF x_bnds] | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2006 | have "(?x - 1) * lb_ln_horner prec (get_even prec) 1 (?x - 1) \<le> ln ?x" (is "?lb_horner \<le> _") by auto | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2007 | ultimately have "?lb2 + ?lb_horner \<le> ln x" | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 2008 | unfolding Float ln_shifted_float[OF `0 < m`, of e] by auto | 
| 31468 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 hoelzl parents: 
31467diff
changeset | 2009 | } | 
| 29805 | 2010 | moreover | 
| 2011 |     {
 | |
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 2012 | from ln_float_bounds(2)[OF x_bnds] | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2013 | have "ln ?x \<le> (?x - 1) * ub_ln_horner prec (get_odd prec) 1 (?x - 1)" (is "_ \<le> ?ub_horner") by auto | 
| 29805 | 2014 | moreover | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2015 | have "ln 2 * (e + (bitlen m - 1)) \<le> ub_ln2 prec * ?s" (is "_ \<le> ?ub2") | 
| 47601 
050718fe6eee
use real :: float => real as lifting-morphism so we can directlry use the rep_eq theorems
 hoelzl parents: 
47600diff
changeset | 2016 | unfolding nat_0 power_0 mult_1_right times_float.rep_eq | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 2017 | using ub_ln2[of prec] | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 2018 | proof (rule mult_mono) | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 2019 | from float_gt1_scale[OF `1 \<le> Float m e`] | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 2020 | show "0 \<le> real (e + (bitlen m - 1))" by auto | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 2021 | next | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 2022 | have "0 \<le> ln 2" by simp | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 2023 | thus "0 \<le> real (ub_ln2 prec)" using ub_ln2[of prec] by arith | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 2024 | qed auto | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2025 | ultimately have "ln x \<le> ?ub2 + ?ub_horner" | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 2026 | unfolding Float ln_shifted_float[OF `0 < m`, of e] by auto | 
| 29805 | 2027 | } | 
| 2028 | ultimately show ?thesis unfolding lb_ln.simps unfolding ub_ln.simps | |
| 31468 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 hoelzl parents: 
31467diff
changeset | 2029 | unfolding if_not_P[OF `\<not> x \<le> 0`] if_not_P[OF `\<not> x < 1`] if_not_P[OF False] if_not_P[OF `\<not> x \<le> Float 3 -1`] Let_def | 
| 47601 
050718fe6eee
use real :: float => real as lifting-morphism so we can directlry use the rep_eq theorems
 hoelzl parents: 
47600diff
changeset | 2030 | unfolding plus_float.rep_eq e_def[symmetric] m_def[symmetric] by simp | 
| 29805 | 2031 | qed | 
| 2032 | qed | |
| 2033 | ||
| 2034 | lemma ub_ln_lb_ln_bounds: assumes "0 < x" | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2035 | shows "the (lb_ln prec x) \<le> ln x \<and> ln x \<le> the (ub_ln prec x)" | 
| 29805 | 2036 | (is "?lb \<le> ?ln \<and> ?ln \<le> ?ub") | 
| 2037 | proof (cases "x < 1") | |
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 2038 | case False hence "1 \<le> x" unfolding less_float_def less_eq_float_def by auto | 
| 29805 | 2039 | show ?thesis using ub_ln_lb_ln_bounds'[OF `1 \<le> x`] . | 
| 2040 | next | |
| 47600 | 2041 | case True have "\<not> x \<le> 0" using `0 < x` by auto | 
| 2042 | from True have "real x < 1" by simp | |
| 2043 | have "0 < real x" and "real x \<noteq> 0" using `0 < x` by auto | |
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 2044 | hence A: "0 < 1 / real x" by auto | 
| 29805 | 2045 | |
| 2046 |   {
 | |
| 2047 | let ?divl = "float_divl (max prec 1) 1 x" | |
| 47600 | 2048 | have A': "1 \<le> ?divl" using float_divl_pos_less1_bound[OF `0 < real x` `real x < 1`] by auto | 
| 2049 | hence B: "0 < real ?divl" by auto | |
| 31468 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 hoelzl parents: 
31467diff
changeset | 2050 | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2051 | have "ln ?divl \<le> ln (1 / x)" unfolding ln_le_cancel_iff[OF B A] using float_divl[of _ 1 x] by auto | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2052 | hence "ln x \<le> - ln ?divl" unfolding nonzero_inverse_eq_divide[OF `real x \<noteq> 0`, symmetric] ln_inverse[OF `0 < real x`] by auto | 
| 31468 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 hoelzl parents: 
31467diff
changeset | 2053 | from this ub_ln_lb_ln_bounds'[OF A', THEN conjunct1, THEN le_imp_neg_le] | 
| 47601 
050718fe6eee
use real :: float => real as lifting-morphism so we can directlry use the rep_eq theorems
 hoelzl parents: 
47600diff
changeset | 2054 | have "?ln \<le> - the (lb_ln prec ?divl)" unfolding uminus_float.rep_eq by (rule order_trans) | 
| 29805 | 2055 | } moreover | 
| 2056 |   {
 | |
| 2057 | let ?divr = "float_divr prec 1 x" | |
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 2058 | have A': "1 \<le> ?divr" using float_divr_pos_less1_lower_bound[OF `0 < x` `x < 1`] unfolding less_eq_float_def less_float_def by auto | 
| 47600 | 2059 | hence B: "0 < real ?divr" by auto | 
| 31468 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 hoelzl parents: 
31467diff
changeset | 2060 | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2061 | have "ln (1 / x) \<le> ln ?divr" unfolding ln_le_cancel_iff[OF A B] using float_divr[of 1 x] by auto | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2062 | hence "- ln ?divr \<le> ln x" unfolding nonzero_inverse_eq_divide[OF `real x \<noteq> 0`, symmetric] ln_inverse[OF `0 < real x`] by auto | 
| 29805 | 2063 | from ub_ln_lb_ln_bounds'[OF A', THEN conjunct2, THEN le_imp_neg_le] this | 
| 47601 
050718fe6eee
use real :: float => real as lifting-morphism so we can directlry use the rep_eq theorems
 hoelzl parents: 
47600diff
changeset | 2064 | have "- the (ub_ln prec ?divr) \<le> ?ln" unfolding uminus_float.rep_eq by (rule order_trans) | 
| 29805 | 2065 | } | 
| 2066 | ultimately show ?thesis unfolding lb_ln.simps[where x=x] ub_ln.simps[where x=x] | |
| 2067 | unfolding if_not_P[OF `\<not> x \<le> 0`] if_P[OF True] by auto | |
| 2068 | qed | |
| 2069 | ||
| 2070 | lemma lb_ln: assumes "Some y = lb_ln prec x" | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2071 | shows "y \<le> ln x" and "0 < real x" | 
| 29805 | 2072 | proof - | 
| 2073 | have "0 < x" | |
| 2074 | proof (rule ccontr) | |
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 2075 | assume "\<not> 0 < x" hence "x \<le> 0" unfolding less_eq_float_def less_float_def by auto | 
| 29805 | 2076 | thus False using assms by auto | 
| 2077 | qed | |
| 47600 | 2078 | thus "0 < real x" by auto | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2079 | have "the (lb_ln prec x) \<le> ln x" using ub_ln_lb_ln_bounds[OF `0 < x`] .. | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2080 | thus "y \<le> ln x" unfolding assms[symmetric] by auto | 
| 29805 | 2081 | qed | 
| 2082 | ||
| 2083 | lemma ub_ln: assumes "Some y = ub_ln prec x" | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2084 | shows "ln x \<le> y" and "0 < real x" | 
| 29805 | 2085 | proof - | 
| 2086 | have "0 < x" | |
| 2087 | proof (rule ccontr) | |
| 47600 | 2088 | assume "\<not> 0 < x" hence "x \<le> 0" by auto | 
| 29805 | 2089 | thus False using assms by auto | 
| 2090 | qed | |
| 47600 | 2091 | thus "0 < real x" by auto | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2092 | have "ln x \<le> the (ub_ln prec x)" using ub_ln_lb_ln_bounds[OF `0 < x`] .. | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2093 | thus "ln x \<le> y" unfolding assms[symmetric] by auto | 
| 29805 | 2094 | qed | 
| 2095 | ||
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2096 | lemma bnds_ln: "\<forall> (x::real) lx ux. (Some l, Some u) = (lb_ln prec lx, ub_ln prec ux) \<and> x \<in> {lx .. ux} \<longrightarrow> l \<le> ln x \<and> ln x \<le> u"
 | 
| 29805 | 2097 | proof (rule allI, rule allI, rule allI, rule impI) | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2098 | fix x::real and lx ux | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2099 |   assume "(Some l, Some u) = (lb_ln prec lx, ub_ln prec ux) \<and> x \<in> {lx .. ux}"
 | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2100 |   hence l: "Some l = lb_ln prec lx " and u: "Some u = ub_ln prec ux" and x: "x \<in> {lx .. ux}" by auto
 | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2101 | |
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2102 | have "ln ux \<le> u" and "0 < real ux" using ub_ln u by auto | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2103 | have "l \<le> ln lx" and "0 < real lx" and "0 < x" using lb_ln[OF l] x by auto | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2104 | |
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2105 | from ln_le_cancel_iff[OF `0 < real lx` `0 < x`] `l \<le> ln lx` | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2106 | have "l \<le> ln x" using x unfolding atLeastAtMost_iff by auto | 
| 29805 | 2107 | moreover | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2108 | from ln_le_cancel_iff[OF `0 < x` `0 < real ux`] `ln ux \<le> real u` | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2109 | have "ln x \<le> u" using x unfolding atLeastAtMost_iff by auto | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2110 | ultimately show "l \<le> ln x \<and> ln x \<le> u" .. | 
| 29805 | 2111 | qed | 
| 2112 | ||
| 2113 | section "Implement floatarith" | |
| 2114 | ||
| 2115 | subsection "Define syntax and semantics" | |
| 2116 | ||
| 2117 | datatype floatarith | |
| 2118 | = Add floatarith floatarith | |
| 2119 | | Minus floatarith | |
| 2120 | | Mult floatarith floatarith | |
| 2121 | | Inverse floatarith | |
| 2122 | | Cos floatarith | |
| 2123 | | Arctan floatarith | |
| 2124 | | Abs floatarith | |
| 2125 | | Max floatarith floatarith | |
| 2126 | | Min floatarith floatarith | |
| 2127 | | Pi | |
| 2128 | | Sqrt floatarith | |
| 2129 | | Exp floatarith | |
| 2130 | | Ln floatarith | |
| 2131 | | Power floatarith nat | |
| 32919 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 2132 | | Var nat | 
| 29805 | 2133 | | Num float | 
| 2134 | ||
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2135 | fun interpret_floatarith :: "floatarith \<Rightarrow> real list \<Rightarrow> real" where | 
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 2136 | "interpret_floatarith (Add a b) vs = (interpret_floatarith a vs) + (interpret_floatarith b vs)" | | 
| 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 2137 | "interpret_floatarith (Minus a) vs = - (interpret_floatarith a vs)" | | 
| 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 2138 | "interpret_floatarith (Mult a b) vs = (interpret_floatarith a vs) * (interpret_floatarith b vs)" | | 
| 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 2139 | "interpret_floatarith (Inverse a) vs = inverse (interpret_floatarith a vs)" | | 
| 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 2140 | "interpret_floatarith (Cos a) vs = cos (interpret_floatarith a vs)" | | 
| 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 2141 | "interpret_floatarith (Arctan a) vs = arctan (interpret_floatarith a vs)" | | 
| 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 2142 | "interpret_floatarith (Min a b) vs = min (interpret_floatarith a vs) (interpret_floatarith b vs)" | | 
| 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 2143 | "interpret_floatarith (Max a b) vs = max (interpret_floatarith a vs) (interpret_floatarith b vs)" | | 
| 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 2144 | "interpret_floatarith (Abs a) vs = abs (interpret_floatarith a vs)" | | 
| 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 2145 | "interpret_floatarith Pi vs = pi" | | 
| 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 2146 | "interpret_floatarith (Sqrt a) vs = sqrt (interpret_floatarith a vs)" | | 
| 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 2147 | "interpret_floatarith (Exp a) vs = exp (interpret_floatarith a vs)" | | 
| 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 2148 | "interpret_floatarith (Ln a) vs = ln (interpret_floatarith a vs)" | | 
| 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 2149 | "interpret_floatarith (Power a n) vs = (interpret_floatarith a vs)^n" | | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2150 | "interpret_floatarith (Num f) vs = f" | | 
| 32919 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 2151 | "interpret_floatarith (Var n) vs = vs ! n" | 
| 29805 | 2152 | |
| 31811 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2153 | lemma interpret_floatarith_divide: "interpret_floatarith (Mult a (Inverse b)) vs = (interpret_floatarith a vs) / (interpret_floatarith b vs)" | 
| 36778 
739a9379e29b
avoid using real-specific versions of generic lemmas
 huffman parents: 
36531diff
changeset | 2154 | unfolding divide_inverse interpret_floatarith.simps .. | 
| 31811 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2155 | |
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2156 | lemma interpret_floatarith_diff: "interpret_floatarith (Add a (Minus b)) vs = (interpret_floatarith a vs) - (interpret_floatarith b vs)" | 
| 37887 | 2157 | unfolding diff_minus interpret_floatarith.simps .. | 
| 31811 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2158 | |
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2159 | lemma interpret_floatarith_sin: "interpret_floatarith (Cos (Add (Mult Pi (Num (Float 1 -1))) (Minus a))) vs = | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2160 | sin (interpret_floatarith a vs)" | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2161 | unfolding sin_cos_eq interpret_floatarith.simps | 
| 37887 | 2162 | interpret_floatarith_divide interpret_floatarith_diff diff_minus | 
| 31811 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2163 | by auto | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2164 | |
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2165 | lemma interpret_floatarith_tan: | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2166 | "interpret_floatarith (Mult (Cos (Add (Mult Pi (Num (Float 1 -1))) (Minus a))) (Inverse (Cos a))) vs = | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2167 | tan (interpret_floatarith a vs)" | 
| 36778 
739a9379e29b
avoid using real-specific versions of generic lemmas
 huffman parents: 
36531diff
changeset | 2168 | unfolding interpret_floatarith.simps(3,4) interpret_floatarith_sin tan_def divide_inverse | 
| 31811 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2169 | by auto | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2170 | |
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2171 | lemma interpret_floatarith_powr: "interpret_floatarith (Exp (Mult b (Ln a))) vs = (interpret_floatarith a vs) powr (interpret_floatarith b vs)" | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2172 | unfolding powr_def interpret_floatarith.simps .. | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2173 | |
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2174 | lemma interpret_floatarith_log: "interpret_floatarith ((Mult (Ln x) (Inverse (Ln b)))) vs = log (interpret_floatarith b vs) (interpret_floatarith x vs)" | 
| 36778 
739a9379e29b
avoid using real-specific versions of generic lemmas
 huffman parents: 
36531diff
changeset | 2175 | unfolding log_def interpret_floatarith.simps divide_inverse .. | 
| 31811 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2176 | |
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2177 | lemma interpret_floatarith_num: | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2178 | shows "interpret_floatarith (Num (Float 0 0)) vs = 0" | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2179 | and "interpret_floatarith (Num (Float 1 0)) vs = 1" | 
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46545diff
changeset | 2180 | and "interpret_floatarith (Num (Float (numeral a) 0)) vs = numeral a" | 
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46545diff
changeset | 2181 | and "interpret_floatarith (Num (Float (neg_numeral a) 0)) vs = neg_numeral a" by auto | 
| 31811 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2182 | |
| 29805 | 2183 | subsection "Implement approximation function" | 
| 2184 | ||
| 2185 | fun lift_bin' :: "(float * float) option \<Rightarrow> (float * float) option \<Rightarrow> (float \<Rightarrow> float \<Rightarrow> float \<Rightarrow> float \<Rightarrow> (float * float)) \<Rightarrow> (float * float) option" where | |
| 2186 | "lift_bin' (Some (l1, u1)) (Some (l2, u2)) f = Some (f l1 u1 l2 u2)" | | |
| 2187 | "lift_bin' a b f = None" | |
| 2188 | ||
| 2189 | fun lift_un :: "(float * float) option \<Rightarrow> (float \<Rightarrow> float \<Rightarrow> ((float option) * (float option))) \<Rightarrow> (float * float) option" where | |
| 2190 | "lift_un (Some (l1, u1)) f = (case (f l1 u1) of (Some l, Some u) \<Rightarrow> Some (l, u) | |
| 2191 | | t \<Rightarrow> None)" | | |
| 2192 | "lift_un b f = None" | |
| 2193 | ||
| 2194 | fun lift_un' :: "(float * float) option \<Rightarrow> (float \<Rightarrow> float \<Rightarrow> (float * float)) \<Rightarrow> (float * float) option" where | |
| 2195 | "lift_un' (Some (l1, u1)) f = Some (f l1 u1)" | | |
| 2196 | "lift_un' b f = None" | |
| 2197 | ||
| 31811 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2198 | definition | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2199 | "bounded_by xs vs \<longleftrightarrow> | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2200 | (\<forall> i < length vs. case vs ! i of None \<Rightarrow> True | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2201 |          | Some (l, u) \<Rightarrow> xs ! i \<in> { real l .. real u })"
 | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2202 | |
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2203 | lemma bounded_byE: | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2204 | assumes "bounded_by xs vs" | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2205 | shows "\<And> i. i < length vs \<Longrightarrow> case vs ! i of None \<Rightarrow> True | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2206 |          | Some (l, u) \<Rightarrow> xs ! i \<in> { real l .. real u }"
 | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2207 | using assms bounded_by_def by blast | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2208 | |
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2209 | lemma bounded_by_update: | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2210 | assumes "bounded_by xs vs" | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2211 |   and bnd: "xs ! i \<in> { real l .. real u }"
 | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2212 | shows "bounded_by xs (vs[i := Some (l,u)])" | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2213 | proof - | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2214 | { fix j
 | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2215 | let ?vs = "vs[i := Some (l,u)]" | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2216 | assume "j < length ?vs" hence [simp]: "j < length vs" by simp | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2217 |   have "case ?vs ! j of None \<Rightarrow> True | Some (l, u) \<Rightarrow> xs ! j \<in> { real l .. real u }"
 | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2218 | proof (cases "?vs ! j") | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2219 | case (Some b) | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2220 | thus ?thesis | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2221 | proof (cases "i = j") | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2222 | case True | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2223 | thus ?thesis using `?vs ! j = Some b` and bnd by auto | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2224 | next | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2225 | case False | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2226 | thus ?thesis using `bounded_by xs vs` unfolding bounded_by_def by auto | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2227 | qed | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2228 | qed auto } | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2229 | thus ?thesis unfolding bounded_by_def by auto | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2230 | qed | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2231 | |
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2232 | lemma bounded_by_None: | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2233 | shows "bounded_by xs (replicate (length xs) None)" | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2234 | unfolding bounded_by_def by auto | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2235 | |
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2236 | fun approx approx' :: "nat \<Rightarrow> floatarith \<Rightarrow> (float * float) option list \<Rightarrow> (float * float) option" where | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 2237 | "approx' prec a bs = (case (approx prec a bs) of Some (l, u) \<Rightarrow> Some (float_round_down prec l, float_round_up prec u) | None \<Rightarrow> None)" | | 
| 31811 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2238 | "approx prec (Add a b) bs = lift_bin' (approx' prec a bs) (approx' prec b bs) (\<lambda> l1 u1 l2 u2. (l1 + l2, u1 + u2))" | | 
| 29805 | 2239 | "approx prec (Minus a) bs = lift_un' (approx' prec a bs) (\<lambda> l u. (-u, -l))" | | 
| 2240 | "approx prec (Mult a b) bs = lift_bin' (approx' prec a bs) (approx' prec b bs) | |
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 2241 | (\<lambda> a1 a2 b1 b2. (nprt a1 * pprt b2 + nprt a2 * nprt b2 + pprt a1 * pprt b1 + pprt a2 * nprt b1, | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 2242 | pprt a2 * pprt b2 + pprt a1 * nprt b2 + nprt a2 * pprt b1 + nprt a1 * nprt b1))" | | 
| 29805 | 2243 | "approx prec (Inverse a) bs = lift_un (approx' prec a bs) (\<lambda> l u. if (0 < l \<or> u < 0) then (Some (float_divl prec 1 u), Some (float_divr prec 1 l)) else (None, None))" | | 
| 2244 | "approx prec (Cos a) bs = lift_un' (approx' prec a bs) (bnds_cos prec)" | | |
| 2245 | "approx prec Pi bs = Some (lb_pi prec, ub_pi prec)" | | |
| 2246 | "approx prec (Min a b) bs = lift_bin' (approx' prec a bs) (approx' prec b bs) (\<lambda> l1 u1 l2 u2. (min l1 l2, min u1 u2))" | | |
| 2247 | "approx prec (Max a b) bs = lift_bin' (approx' prec a bs) (approx' prec b bs) (\<lambda> l1 u1 l2 u2. (max l1 l2, max u1 u2))" | | |
| 2248 | "approx prec (Abs a) bs = lift_un' (approx' prec a bs) (\<lambda>l u. (if l < 0 \<and> 0 < u then 0 else min \<bar>l\<bar> \<bar>u\<bar>, max \<bar>l\<bar> \<bar>u\<bar>))" | | |
| 2249 | "approx prec (Arctan a) bs = lift_un' (approx' prec a bs) (\<lambda> l u. (lb_arctan prec l, ub_arctan prec u))" | | |
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 2250 | "approx prec (Sqrt a) bs = lift_un' (approx' prec a bs) (\<lambda> l u. (lb_sqrt prec l, ub_sqrt prec u))" | | 
| 29805 | 2251 | "approx prec (Exp a) bs = lift_un' (approx' prec a bs) (\<lambda> l u. (lb_exp prec l, ub_exp prec u))" | | 
| 2252 | "approx prec (Ln a) bs = lift_un (approx' prec a bs) (\<lambda> l u. (lb_ln prec l, ub_ln prec u))" | | |
| 2253 | "approx prec (Power a n) bs = lift_un' (approx' prec a bs) (float_power_bnds n)" | | |
| 2254 | "approx prec (Num f) bs = Some (f, f)" | | |
| 32919 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 2255 | "approx prec (Var i) bs = (if i < length bs then bs ! i else None)" | 
| 29805 | 2256 | |
| 2257 | lemma lift_bin'_ex: | |
| 2258 | assumes lift_bin'_Some: "Some (l, u) = lift_bin' a b f" | |
| 2259 | shows "\<exists> l1 u1 l2 u2. Some (l1, u1) = a \<and> Some (l2, u2) = b" | |
| 2260 | proof (cases a) | |
| 2261 | case None hence "None = lift_bin' a b f" unfolding None lift_bin'.simps .. | |
| 2262 | thus ?thesis using lift_bin'_Some by auto | |
| 2263 | next | |
| 2264 | case (Some a') | |
| 2265 | show ?thesis | |
| 2266 | proof (cases b) | |
| 2267 | case None hence "None = lift_bin' a b f" unfolding None lift_bin'.simps .. | |
| 2268 | thus ?thesis using lift_bin'_Some by auto | |
| 2269 | next | |
| 2270 | case (Some b') | |
| 2271 | obtain la ua where a': "a' = (la, ua)" by (cases a', auto) | |
| 2272 | obtain lb ub where b': "b' = (lb, ub)" by (cases b', auto) | |
| 2273 | thus ?thesis unfolding `a = Some a'` `b = Some b'` a' b' by auto | |
| 2274 | qed | |
| 2275 | qed | |
| 2276 | ||
| 2277 | lemma lift_bin'_f: | |
| 2278 | assumes lift_bin'_Some: "Some (l, u) = lift_bin' (g a) (g b) f" | |
| 2279 | and Pa: "\<And>l u. Some (l, u) = g a \<Longrightarrow> P l u a" and Pb: "\<And>l u. Some (l, u) = g b \<Longrightarrow> P l u b" | |
| 2280 | shows "\<exists> l1 u1 l2 u2. P l1 u1 a \<and> P l2 u2 b \<and> l = fst (f l1 u1 l2 u2) \<and> u = snd (f l1 u1 l2 u2)" | |
| 2281 | proof - | |
| 2282 | obtain l1 u1 l2 u2 | |
| 2283 | where Sa: "Some (l1, u1) = g a" and Sb: "Some (l2, u2) = g b" using lift_bin'_ex[OF assms(1)] by auto | |
| 31809 | 2284 | have lu: "(l, u) = f l1 u1 l2 u2" using lift_bin'_Some[unfolded Sa[symmetric] Sb[symmetric] lift_bin'.simps] by auto | 
| 29805 | 2285 | have "l = fst (f l1 u1 l2 u2)" and "u = snd (f l1 u1 l2 u2)" unfolding lu[symmetric] by auto | 
| 31809 | 2286 | thus ?thesis using Pa[OF Sa] Pb[OF Sb] by auto | 
| 29805 | 2287 | qed | 
| 2288 | ||
| 2289 | lemma approx_approx': | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2290 | assumes Pa: "\<And>l u. Some (l, u) = approx prec a vs \<Longrightarrow> l \<le> interpret_floatarith a xs \<and> interpret_floatarith a xs \<le> u" | 
| 29805 | 2291 | and approx': "Some (l, u) = approx' prec a vs" | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2292 | shows "l \<le> interpret_floatarith a xs \<and> interpret_floatarith a xs \<le> u" | 
| 29805 | 2293 | proof - | 
| 2294 | obtain l' u' where S: "Some (l', u') = approx prec a vs" | |
| 2295 | using approx' unfolding approx'.simps by (cases "approx prec a vs", auto) | |
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 2296 | have l': "l = float_round_down prec l'" and u': "u = float_round_up prec u'" | 
| 29805 | 2297 | using approx' unfolding approx'.simps S[symmetric] by auto | 
| 31809 | 2298 | show ?thesis unfolding l' u' | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 2299 | using order_trans[OF Pa[OF S, THEN conjunct2] float_round_up[of u']] | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 2300 | using order_trans[OF float_round_down[of _ l'] Pa[OF S, THEN conjunct1]] by auto | 
| 29805 | 2301 | qed | 
| 2302 | ||
| 2303 | lemma lift_bin': | |
| 2304 | assumes lift_bin'_Some: "Some (l, u) = lift_bin' (approx' prec a bs) (approx' prec b bs) f" | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2305 | and Pa: "\<And>l u. Some (l, u) = approx prec a bs \<Longrightarrow> l \<le> interpret_floatarith a xs \<and> interpret_floatarith a xs \<le> u" (is "\<And>l u. _ = ?g a \<Longrightarrow> ?P l u a") | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2306 | and Pb: "\<And>l u. Some (l, u) = approx prec b bs \<Longrightarrow> l \<le> interpret_floatarith b xs \<and> interpret_floatarith b xs \<le> u" | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2307 | shows "\<exists> l1 u1 l2 u2. (l1 \<le> interpret_floatarith a xs \<and> interpret_floatarith a xs \<le> u1) \<and> | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2308 | (l2 \<le> interpret_floatarith b xs \<and> interpret_floatarith b xs \<le> u2) \<and> | 
| 29805 | 2309 | l = fst (f l1 u1 l2 u2) \<and> u = snd (f l1 u1 l2 u2)" | 
| 2310 | proof - | |
| 2311 |   { fix l u assume "Some (l, u) = approx' prec a bs"
 | |
| 2312 | with approx_approx'[of prec a bs, OF _ this] Pa | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2313 | have "l \<le> interpret_floatarith a xs \<and> interpret_floatarith a xs \<le> u" by auto } note Pa = this | 
| 29805 | 2314 |   { fix l u assume "Some (l, u) = approx' prec b bs"
 | 
| 2315 | with approx_approx'[of prec b bs, OF _ this] Pb | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2316 | have "l \<le> interpret_floatarith b xs \<and> interpret_floatarith b xs \<le> u" by auto } note Pb = this | 
| 29805 | 2317 | |
| 2318 | from lift_bin'_f[where g="\<lambda>a. approx' prec a bs" and P = ?P, OF lift_bin'_Some, OF Pa Pb] | |
| 2319 | show ?thesis by auto | |
| 2320 | qed | |
| 2321 | ||
| 2322 | lemma lift_un'_ex: | |
| 2323 | assumes lift_un'_Some: "Some (l, u) = lift_un' a f" | |
| 2324 | shows "\<exists> l u. Some (l, u) = a" | |
| 2325 | proof (cases a) | |
| 2326 | case None hence "None = lift_un' a f" unfolding None lift_un'.simps .. | |
| 2327 | thus ?thesis using lift_un'_Some by auto | |
| 2328 | next | |
| 2329 | case (Some a') | |
| 2330 | obtain la ua where a': "a' = (la, ua)" by (cases a', auto) | |
| 2331 | thus ?thesis unfolding `a = Some a'` a' by auto | |
| 2332 | qed | |
| 2333 | ||
| 2334 | lemma lift_un'_f: | |
| 2335 | assumes lift_un'_Some: "Some (l, u) = lift_un' (g a) f" | |
| 2336 | and Pa: "\<And>l u. Some (l, u) = g a \<Longrightarrow> P l u a" | |
| 2337 | shows "\<exists> l1 u1. P l1 u1 a \<and> l = fst (f l1 u1) \<and> u = snd (f l1 u1)" | |
| 2338 | proof - | |
| 2339 | obtain l1 u1 where Sa: "Some (l1, u1) = g a" using lift_un'_ex[OF assms(1)] by auto | |
| 2340 | have lu: "(l, u) = f l1 u1" using lift_un'_Some[unfolded Sa[symmetric] lift_un'.simps] by auto | |
| 2341 | have "l = fst (f l1 u1)" and "u = snd (f l1 u1)" unfolding lu[symmetric] by auto | |
| 2342 | thus ?thesis using Pa[OF Sa] by auto | |
| 2343 | qed | |
| 2344 | ||
| 2345 | lemma lift_un': | |
| 2346 | assumes lift_un'_Some: "Some (l, u) = lift_un' (approx' prec a bs) f" | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2347 | and Pa: "\<And>l u. Some (l, u) = approx prec a bs \<Longrightarrow> l \<le> interpret_floatarith a xs \<and> interpret_floatarith a xs \<le> u" (is "\<And>l u. _ = ?g a \<Longrightarrow> ?P l u a") | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2348 | shows "\<exists> l1 u1. (l1 \<le> interpret_floatarith a xs \<and> interpret_floatarith a xs \<le> u1) \<and> | 
| 29805 | 2349 | l = fst (f l1 u1) \<and> u = snd (f l1 u1)" | 
| 2350 | proof - | |
| 2351 |   { fix l u assume "Some (l, u) = approx' prec a bs"
 | |
| 2352 | with approx_approx'[of prec a bs, OF _ this] Pa | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2353 | have "l \<le> interpret_floatarith a xs \<and> interpret_floatarith a xs \<le> u" by auto } note Pa = this | 
| 29805 | 2354 | from lift_un'_f[where g="\<lambda>a. approx' prec a bs" and P = ?P, OF lift_un'_Some, OF Pa] | 
| 2355 | show ?thesis by auto | |
| 2356 | qed | |
| 2357 | ||
| 2358 | lemma lift_un'_bnds: | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2359 |   assumes bnds: "\<forall> (x::real) lx ux. (l, u) = f lx ux \<and> x \<in> { lx .. ux } \<longrightarrow> l \<le> f' x \<and> f' x \<le> u"
 | 
| 29805 | 2360 | and lift_un'_Some: "Some (l, u) = lift_un' (approx' prec a bs) f" | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2361 | and Pa: "\<And>l u. Some (l, u) = approx prec a bs \<Longrightarrow> l \<le> interpret_floatarith a xs \<and> interpret_floatarith a xs \<le> u" | 
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 2362 | shows "real l \<le> f' (interpret_floatarith a xs) \<and> f' (interpret_floatarith a xs) \<le> real u" | 
| 29805 | 2363 | proof - | 
| 2364 | from lift_un'[OF lift_un'_Some Pa] | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2365 | obtain l1 u1 where "l1 \<le> interpret_floatarith a xs" and "interpret_floatarith a xs \<le> u1" and "l = fst (f l1 u1)" and "u = snd (f l1 u1)" by blast | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2366 |   hence "(l, u) = f l1 u1" and "interpret_floatarith a xs \<in> {l1 .. u1}" by auto
 | 
| 29805 | 2367 | thus ?thesis using bnds by auto | 
| 2368 | qed | |
| 2369 | ||
| 2370 | lemma lift_un_ex: | |
| 2371 | assumes lift_un_Some: "Some (l, u) = lift_un a f" | |
| 2372 | shows "\<exists> l u. Some (l, u) = a" | |
| 2373 | proof (cases a) | |
| 2374 | case None hence "None = lift_un a f" unfolding None lift_un.simps .. | |
| 2375 | thus ?thesis using lift_un_Some by auto | |
| 2376 | next | |
| 2377 | case (Some a') | |
| 2378 | obtain la ua where a': "a' = (la, ua)" by (cases a', auto) | |
| 2379 | thus ?thesis unfolding `a = Some a'` a' by auto | |
| 2380 | qed | |
| 2381 | ||
| 2382 | lemma lift_un_f: | |
| 2383 | assumes lift_un_Some: "Some (l, u) = lift_un (g a) f" | |
| 2384 | and Pa: "\<And>l u. Some (l, u) = g a \<Longrightarrow> P l u a" | |
| 2385 | shows "\<exists> l1 u1. P l1 u1 a \<and> Some l = fst (f l1 u1) \<and> Some u = snd (f l1 u1)" | |
| 2386 | proof - | |
| 2387 | obtain l1 u1 where Sa: "Some (l1, u1) = g a" using lift_un_ex[OF assms(1)] by auto | |
| 2388 | have "fst (f l1 u1) \<noteq> None \<and> snd (f l1 u1) \<noteq> None" | |
| 2389 | proof (rule ccontr) | |
| 2390 | assume "\<not> (fst (f l1 u1) \<noteq> None \<and> snd (f l1 u1) \<noteq> None)" | |
| 2391 | hence or: "fst (f l1 u1) = None \<or> snd (f l1 u1) = None" by auto | |
| 31809 | 2392 | hence "lift_un (g a) f = None" | 
| 29805 | 2393 | proof (cases "fst (f l1 u1) = None") | 
| 2394 | case True | |
| 2395 | then obtain b where b: "f l1 u1 = (None, b)" by (cases "f l1 u1", auto) | |
| 2396 | thus ?thesis unfolding Sa[symmetric] lift_un.simps b by auto | |
| 2397 | next | |
| 2398 | case False hence "snd (f l1 u1) = None" using or by auto | |
| 2399 | with False obtain b where b: "f l1 u1 = (Some b, None)" by (cases "f l1 u1", auto) | |
| 2400 | thus ?thesis unfolding Sa[symmetric] lift_un.simps b by auto | |
| 2401 | qed | |
| 2402 | thus False using lift_un_Some by auto | |
| 2403 | qed | |
| 2404 | then obtain a' b' where f: "f l1 u1 = (Some a', Some b')" by (cases "f l1 u1", auto) | |
| 2405 | from lift_un_Some[unfolded Sa[symmetric] lift_un.simps f] | |
| 2406 | have "Some l = fst (f l1 u1)" and "Some u = snd (f l1 u1)" unfolding f by auto | |
| 2407 | thus ?thesis unfolding Sa[symmetric] lift_un.simps using Pa[OF Sa] by auto | |
| 2408 | qed | |
| 2409 | ||
| 2410 | lemma lift_un: | |
| 2411 | assumes lift_un_Some: "Some (l, u) = lift_un (approx' prec a bs) f" | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2412 | and Pa: "\<And>l u. Some (l, u) = approx prec a bs \<Longrightarrow> l \<le> interpret_floatarith a xs \<and> interpret_floatarith a xs \<le> u" (is "\<And>l u. _ = ?g a \<Longrightarrow> ?P l u a") | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2413 | shows "\<exists> l1 u1. (l1 \<le> interpret_floatarith a xs \<and> interpret_floatarith a xs \<le> u1) \<and> | 
| 29805 | 2414 | Some l = fst (f l1 u1) \<and> Some u = snd (f l1 u1)" | 
| 2415 | proof - | |
| 2416 |   { fix l u assume "Some (l, u) = approx' prec a bs"
 | |
| 2417 | with approx_approx'[of prec a bs, OF _ this] Pa | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2418 | have "l \<le> interpret_floatarith a xs \<and> interpret_floatarith a xs \<le> u" by auto } note Pa = this | 
| 29805 | 2419 | from lift_un_f[where g="\<lambda>a. approx' prec a bs" and P = ?P, OF lift_un_Some, OF Pa] | 
| 2420 | show ?thesis by auto | |
| 2421 | qed | |
| 2422 | ||
| 2423 | lemma lift_un_bnds: | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2424 |   assumes bnds: "\<forall> (x::real) lx ux. (Some l, Some u) = f lx ux \<and> x \<in> { lx .. ux } \<longrightarrow> l \<le> f' x \<and> f' x \<le> u"
 | 
| 29805 | 2425 | and lift_un_Some: "Some (l, u) = lift_un (approx' prec a bs) f" | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2426 | and Pa: "\<And>l u. Some (l, u) = approx prec a bs \<Longrightarrow> l \<le> interpret_floatarith a xs \<and> interpret_floatarith a xs \<le> u" | 
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 2427 | shows "real l \<le> f' (interpret_floatarith a xs) \<and> f' (interpret_floatarith a xs) \<le> real u" | 
| 29805 | 2428 | proof - | 
| 2429 | from lift_un[OF lift_un_Some Pa] | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2430 | obtain l1 u1 where "l1 \<le> interpret_floatarith a xs" and "interpret_floatarith a xs \<le> u1" and "Some l = fst (f l1 u1)" and "Some u = snd (f l1 u1)" by blast | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2431 |   hence "(Some l, Some u) = f l1 u1" and "interpret_floatarith a xs \<in> {l1 .. u1}" by auto
 | 
| 29805 | 2432 | thus ?thesis using bnds by auto | 
| 2433 | qed | |
| 2434 | ||
| 2435 | lemma approx: | |
| 2436 | assumes "bounded_by xs vs" | |
| 2437 | and "Some (l, u) = approx prec arith vs" (is "_ = ?g arith") | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2438 | shows "l \<le> interpret_floatarith arith xs \<and> interpret_floatarith arith xs \<le> u" (is "?P l u arith") | 
| 31809 | 2439 | using `Some (l, u) = approx prec arith vs` | 
| 45129 
1fce03e3e8ad
tuned proofs -- eliminated vacuous "induct arbitrary: ..." situations;
 wenzelm parents: 
44821diff
changeset | 2440 | proof (induct arith arbitrary: l u) | 
| 29805 | 2441 | case (Add a b) | 
| 2442 | from lift_bin'[OF Add.prems[unfolded approx.simps]] Add.hyps | |
| 2443 | obtain l1 u1 l2 u2 where "l = l1 + l2" and "u = u1 + u2" | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2444 | "l1 \<le> interpret_floatarith a xs" and "interpret_floatarith a xs \<le> u1" | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2445 | "l2 \<le> interpret_floatarith b xs" and "interpret_floatarith b xs \<le> u2" unfolding fst_conv snd_conv by blast | 
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 2446 | thus ?case unfolding interpret_floatarith.simps by auto | 
| 29805 | 2447 | next | 
| 2448 | case (Minus a) | |
| 2449 | from lift_un'[OF Minus.prems[unfolded approx.simps]] Minus.hyps | |
| 2450 | obtain l1 u1 where "l = -u1" and "u = -l1" | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2451 | "l1 \<le> interpret_floatarith a xs" and "interpret_floatarith a xs \<le> u1" unfolding fst_conv snd_conv by blast | 
| 47601 
050718fe6eee
use real :: float => real as lifting-morphism so we can directlry use the rep_eq theorems
 hoelzl parents: 
47600diff
changeset | 2452 | thus ?case unfolding interpret_floatarith.simps using minus_float.rep_eq by auto | 
| 29805 | 2453 | next | 
| 2454 | case (Mult a b) | |
| 2455 | from lift_bin'[OF Mult.prems[unfolded approx.simps]] Mult.hyps | |
| 31809 | 2456 | obtain l1 u1 l2 u2 | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 2457 | where l: "l = nprt l1 * pprt u2 + nprt u1 * nprt u2 + pprt l1 * pprt l2 + pprt u1 * nprt l2" | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 2458 | and u: "u = pprt u1 * pprt u2 + pprt l1 * nprt u2 + nprt u1 * pprt l2 + nprt l1 * nprt l2" | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2459 | and "l1 \<le> interpret_floatarith a xs" and "interpret_floatarith a xs \<le> u1" | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2460 | and "l2 \<le> interpret_floatarith b xs" and "interpret_floatarith b xs \<le> u2" unfolding fst_conv snd_conv by blast | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 2461 | thus ?case unfolding interpret_floatarith.simps l u | 
| 29805 | 2462 | using mult_le_prts mult_ge_prts by auto | 
| 2463 | next | |
| 2464 | case (Inverse a) | |
| 2465 | from lift_un[OF Inverse.prems[unfolded approx.simps], unfolded if_distrib[of fst] if_distrib[of snd] fst_conv snd_conv] Inverse.hyps | |
| 31809 | 2466 | obtain l1 u1 where l': "Some l = (if 0 < l1 \<or> u1 < 0 then Some (float_divl prec 1 u1) else None)" | 
| 29805 | 2467 | and u': "Some u = (if 0 < l1 \<or> u1 < 0 then Some (float_divr prec 1 l1) else None)" | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2468 | and l1: "l1 \<le> interpret_floatarith a xs" and u1: "interpret_floatarith a xs \<le> u1" by blast | 
| 29805 | 2469 | have either: "0 < l1 \<or> u1 < 0" proof (rule ccontr) assume P: "\<not> (0 < l1 \<or> u1 < 0)" show False using l' unfolding if_not_P[OF P] by auto qed | 
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 2470 | moreover have l1_le_u1: "real l1 \<le> real u1" using l1 u1 by auto | 
| 47600 | 2471 | ultimately have "real l1 \<noteq> 0" and "real u1 \<noteq> 0" by auto | 
| 29805 | 2472 | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2473 | have inv: "inverse u1 \<le> inverse (interpret_floatarith a xs) | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2474 | \<and> inverse (interpret_floatarith a xs) \<le> inverse l1" | 
| 29805 | 2475 | proof (cases "0 < l1") | 
| 31809 | 2476 | case True hence "0 < real u1" and "0 < real l1" "0 < interpret_floatarith a xs" | 
| 47600 | 2477 | using l1_le_u1 l1 by auto | 
| 29805 | 2478 | show ?thesis | 
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 2479 | unfolding inverse_le_iff_le[OF `0 < real u1` `0 < interpret_floatarith a xs`] | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 2480 | inverse_le_iff_le[OF `0 < interpret_floatarith a xs` `0 < real l1`] | 
| 29805 | 2481 | using l1 u1 by auto | 
| 2482 | next | |
| 2483 | case False hence "u1 < 0" using either by blast | |
| 31809 | 2484 | hence "real u1 < 0" and "real l1 < 0" "interpret_floatarith a xs < 0" | 
| 47600 | 2485 | using l1_le_u1 u1 by auto | 
| 29805 | 2486 | show ?thesis | 
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 2487 | unfolding inverse_le_iff_le_neg[OF `real u1 < 0` `interpret_floatarith a xs < 0`] | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 2488 | inverse_le_iff_le_neg[OF `interpret_floatarith a xs < 0` `real l1 < 0`] | 
| 29805 | 2489 | using l1 u1 by auto | 
| 2490 | qed | |
| 31468 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 hoelzl parents: 
31467diff
changeset | 2491 | |
| 29805 | 2492 | from l' have "l = float_divl prec 1 u1" by (cases "0 < l1 \<or> u1 < 0", auto) | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2493 | hence "l \<le> inverse u1" unfolding nonzero_inverse_eq_divide[OF `real u1 \<noteq> 0`] using float_divl[of prec 1 u1] by auto | 
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 2494 | also have "\<dots> \<le> inverse (interpret_floatarith a xs)" using inv by auto | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2495 | finally have "l \<le> inverse (interpret_floatarith a xs)" . | 
| 29805 | 2496 | moreover | 
| 2497 | from u' have "u = float_divr prec 1 l1" by (cases "0 < l1 \<or> u1 < 0", auto) | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2498 | hence "inverse l1 \<le> u" unfolding nonzero_inverse_eq_divide[OF `real l1 \<noteq> 0`] using float_divr[of 1 l1 prec] by auto | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2499 | hence "inverse (interpret_floatarith a xs) \<le> u" by (rule order_trans[OF inv[THEN conjunct2]]) | 
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 2500 | ultimately show ?case unfolding interpret_floatarith.simps using l1 u1 by auto | 
| 29805 | 2501 | next | 
| 2502 | case (Abs x) | |
| 2503 | from lift_un'[OF Abs.prems[unfolded approx.simps], unfolded fst_conv snd_conv] Abs.hyps | |
| 2504 | obtain l1 u1 where l': "l = (if l1 < 0 \<and> 0 < u1 then 0 else min \<bar>l1\<bar> \<bar>u1\<bar>)" and u': "u = max \<bar>l1\<bar> \<bar>u1\<bar>" | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2505 | and l1: "l1 \<le> interpret_floatarith x xs" and u1: "interpret_floatarith x xs \<le> u1" by blast | 
| 47600 | 2506 | thus ?case unfolding l' u' by (cases "l1 < 0 \<and> 0 < u1", auto simp add: real_of_float_min real_of_float_max) | 
| 29805 | 2507 | next | 
| 2508 | case (Min a b) | |
| 2509 | from lift_bin'[OF Min.prems[unfolded approx.simps], unfolded fst_conv snd_conv] Min.hyps | |
| 2510 | obtain l1 u1 l2 u2 where l': "l = min l1 l2" and u': "u = min u1 u2" | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2511 | and l1: "l1 \<le> interpret_floatarith a xs" and u1: "interpret_floatarith a xs \<le> u1" | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2512 | and l1: "l2 \<le> interpret_floatarith b xs" and u1: "interpret_floatarith b xs \<le> u2" by blast | 
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 2513 | thus ?case unfolding l' u' by (auto simp add: real_of_float_min) | 
| 29805 | 2514 | next | 
| 2515 | case (Max a b) | |
| 2516 | from lift_bin'[OF Max.prems[unfolded approx.simps], unfolded fst_conv snd_conv] Max.hyps | |
| 2517 | obtain l1 u1 l2 u2 where l': "l = max l1 l2" and u': "u = max u1 u2" | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2518 | and l1: "l1 \<le> interpret_floatarith a xs" and u1: "interpret_floatarith a xs \<le> u1" | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2519 | and l1: "l2 \<le> interpret_floatarith b xs" and u1: "interpret_floatarith b xs \<le> u2" by blast | 
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 2520 | thus ?case unfolding l' u' by (auto simp add: real_of_float_max) | 
| 29805 | 2521 | next case (Cos a) with lift_un'_bnds[OF bnds_cos] show ?case by auto | 
| 2522 | next case (Arctan a) with lift_un'_bnds[OF bnds_arctan] show ?case by auto | |
| 2523 | next case Pi with pi_boundaries show ?case by auto | |
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 2524 | next case (Sqrt a) with lift_un'_bnds[OF bnds_sqrt] show ?case by auto | 
| 29805 | 2525 | next case (Exp a) with lift_un'_bnds[OF bnds_exp] show ?case by auto | 
| 2526 | next case (Ln a) with lift_un_bnds[OF bnds_ln] show ?case by auto | |
| 2527 | next case (Power a n) with lift_un'_bnds[OF bnds_power] show ?case by auto | |
| 2528 | next case (Num f) thus ?case by auto | |
| 2529 | next | |
| 32919 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 2530 | case (Var n) | 
| 31811 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2531 | from this[symmetric] `bounded_by xs vs`[THEN bounded_byE, of n] | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2532 | show ?case by (cases "n < length vs", auto) | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2533 | qed | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2534 | |
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2535 | datatype form = Bound floatarith floatarith floatarith form | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2536 | | Assign floatarith floatarith form | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2537 | | Less floatarith floatarith | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2538 | | LessEqual floatarith floatarith | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2539 | | AtLeastAtMost floatarith floatarith floatarith | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2540 | |
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2541 | fun interpret_form :: "form \<Rightarrow> real list \<Rightarrow> bool" where | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2542 | "interpret_form (Bound x a b f) vs = (interpret_floatarith x vs \<in> { interpret_floatarith a vs .. interpret_floatarith b vs } \<longrightarrow> interpret_form f vs)" |
 | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2543 | "interpret_form (Assign x a f) vs = (interpret_floatarith x vs = interpret_floatarith a vs \<longrightarrow> interpret_form f vs)" | | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2544 | "interpret_form (Less a b) vs = (interpret_floatarith a vs < interpret_floatarith b vs)" | | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2545 | "interpret_form (LessEqual a b) vs = (interpret_floatarith a vs \<le> interpret_floatarith b vs)" | | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2546 | "interpret_form (AtLeastAtMost x a b) vs = (interpret_floatarith x vs \<in> { interpret_floatarith a vs .. interpret_floatarith b vs })"
 | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2547 | |
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2548 | fun approx_form' and approx_form :: "nat \<Rightarrow> form \<Rightarrow> (float * float) option list \<Rightarrow> nat list \<Rightarrow> bool" where | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2549 | "approx_form' prec f 0 n l u bs ss = approx_form prec f (bs[n := Some (l, u)]) ss" | | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2550 | "approx_form' prec f (Suc s) n l u bs ss = | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2551 | (let m = (l + u) * Float 1 -1 | 
| 32919 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 2552 | in (if approx_form' prec f s n l m bs ss then approx_form' prec f s n m u bs ss else False))" | | 
| 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 2553 | "approx_form prec (Bound (Var n) a b f) bs ss = | 
| 31811 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2554 | (case (approx prec a bs, approx prec b bs) | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2555 | of (Some (l, _), Some (_, u)) \<Rightarrow> approx_form' prec f (ss ! n) n l u bs ss | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2556 | | _ \<Rightarrow> False)" | | 
| 32919 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 2557 | "approx_form prec (Assign (Var n) a f) bs ss = | 
| 31811 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2558 | (case (approx prec a bs) | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2559 | of (Some (l, u)) \<Rightarrow> approx_form' prec f (ss ! n) n l u bs ss | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2560 | | _ \<Rightarrow> False)" | | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2561 | "approx_form prec (Less a b) bs ss = | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2562 | (case (approx prec a bs, approx prec b bs) | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2563 | of (Some (l, u), Some (l', u')) \<Rightarrow> u < l' | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2564 | | _ \<Rightarrow> False)" | | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2565 | "approx_form prec (LessEqual a b) bs ss = | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2566 | (case (approx prec a bs, approx prec b bs) | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2567 | of (Some (l, u), Some (l', u')) \<Rightarrow> u \<le> l' | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2568 | | _ \<Rightarrow> False)" | | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2569 | "approx_form prec (AtLeastAtMost x a b) bs ss = | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2570 | (case (approx prec x bs, approx prec a bs, approx prec b bs) | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2571 | of (Some (lx, ux), Some (l, u), Some (l', u')) \<Rightarrow> u \<le> lx \<and> ux \<le> l' | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2572 | | _ \<Rightarrow> False)" | | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2573 | "approx_form _ _ _ _ = False" | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2574 | |
| 32919 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 2575 | lemma lazy_conj: "(if A then B else False) = (A \<and> B)" by simp | 
| 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 2576 | |
| 31811 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2577 | lemma approx_form_approx_form': | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2578 |   assumes "approx_form' prec f s n l u bs ss" and "(x::real) \<in> { l .. u }"
 | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2579 |   obtains l' u' where "x \<in> { l' .. u' }"
 | 
| 31811 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2580 | and "approx_form prec f (bs[n := Some (l', u')]) ss" | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2581 | using assms proof (induct s arbitrary: l u) | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2582 | case 0 | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2583 | from this(1)[of l u] this(2,3) | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2584 | show thesis by auto | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2585 | next | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2586 | case (Suc s) | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2587 | |
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2588 | let ?m = "(l + u) * Float 1 -1" | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2589 | have "real l \<le> ?m" and "?m \<le> real u" | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 2590 | unfolding less_eq_float_def using Suc.prems by auto | 
| 31811 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2591 | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2592 |   with `x \<in> { l .. u }`
 | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2593 |   have "x \<in> { l .. ?m} \<or> x \<in> { ?m .. u }" by auto
 | 
| 31811 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2594 | thus thesis | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2595 | proof (rule disjE) | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2596 |     assume *: "x \<in> { l .. ?m }"
 | 
| 31811 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2597 | with Suc.hyps[OF _ _ *] Suc.prems | 
| 32919 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 2598 | show thesis by (simp add: Let_def lazy_conj) | 
| 29805 | 2599 | next | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2600 |     assume *: "x \<in> { ?m .. u }"
 | 
| 31811 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2601 | with Suc.hyps[OF _ _ *] Suc.prems | 
| 32919 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 2602 | show thesis by (simp add: Let_def lazy_conj) | 
| 29805 | 2603 | qed | 
| 2604 | qed | |
| 2605 | ||
| 31811 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2606 | lemma approx_form_aux: | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2607 | assumes "approx_form prec f vs ss" | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2608 | and "bounded_by xs vs" | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2609 | shows "interpret_form f xs" | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2610 | using assms proof (induct f arbitrary: vs) | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2611 | case (Bound x a b f) | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2612 | then obtain n | 
| 32919 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 2613 | where x_eq: "x = Var n" by (cases x) auto | 
| 31811 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2614 | |
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2615 | with Bound.prems obtain l u' l' u | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2616 | where l_eq: "Some (l, u') = approx prec a vs" | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2617 | and u_eq: "Some (l', u) = approx prec b vs" | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2618 | and approx_form': "approx_form' prec f (ss ! n) n l u vs ss" | 
| 37411 
c88c44156083
removed simplifier congruence rule of "prod_case"
 haftmann parents: 
37391diff
changeset | 2619 | by (cases "approx prec a vs", simp) (cases "approx prec b vs", auto) | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2620 | |
| 31811 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2621 |   { assume "xs ! n \<in> { interpret_floatarith a xs .. interpret_floatarith b xs }"
 | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2622 | with approx[OF Bound.prems(2) l_eq] and approx[OF Bound.prems(2) u_eq] | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2623 |     have "xs ! n \<in> { l .. u}" by auto
 | 
| 31811 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2624 | |
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2625 | from approx_form_approx_form'[OF approx_form' this] | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2626 |     obtain lx ux where bnds: "xs ! n \<in> { lx .. ux }"
 | 
| 31811 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2627 | and approx_form: "approx_form prec f (vs[n := Some (lx, ux)]) ss" . | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2628 | |
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2629 | from `bounded_by xs vs` bnds | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2630 | have "bounded_by xs (vs[n := Some (lx, ux)])" by (rule bounded_by_update) | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2631 | with Bound.hyps[OF approx_form] | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2632 | have "interpret_form f xs" by blast } | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2633 | thus ?case using interpret_form.simps x_eq and interpret_floatarith.simps by simp | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2634 | next | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2635 | case (Assign x a f) | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2636 | then obtain n | 
| 32919 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 2637 | where x_eq: "x = Var n" by (cases x) auto | 
| 31811 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2638 | |
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 2639 | with Assign.prems obtain l u | 
| 31811 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2640 | where bnd_eq: "Some (l, u) = approx prec a vs" | 
| 32919 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 2641 | and x_eq: "x = Var n" | 
| 31811 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2642 | and approx_form': "approx_form' prec f (ss ! n) n l u vs ss" | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2643 | by (cases "approx prec a vs") auto | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2644 | |
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2645 |   { assume bnds: "xs ! n = interpret_floatarith a xs"
 | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2646 | with approx[OF Assign.prems(2) bnd_eq] | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2647 |     have "xs ! n \<in> { l .. u}" by auto
 | 
| 31811 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2648 | from approx_form_approx_form'[OF approx_form' this] | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2649 |     obtain lx ux where bnds: "xs ! n \<in> { lx .. ux }"
 | 
| 31811 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2650 | and approx_form: "approx_form prec f (vs[n := Some (lx, ux)]) ss" . | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2651 | |
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2652 | from `bounded_by xs vs` bnds | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2653 | have "bounded_by xs (vs[n := Some (lx, ux)])" by (rule bounded_by_update) | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2654 | with Assign.hyps[OF approx_form] | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2655 | have "interpret_form f xs" by blast } | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2656 | thus ?case using interpret_form.simps x_eq and interpret_floatarith.simps by simp | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2657 | next | 
| 29805 | 2658 | case (Less a b) | 
| 31811 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2659 | then obtain l u l' u' | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2660 | where l_eq: "Some (l, u) = approx prec a vs" | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2661 | and u_eq: "Some (l', u') = approx prec b vs" | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2662 | and inequality: "u < l'" | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2663 | by (cases "approx prec a vs", auto, | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2664 | cases "approx prec b vs", auto) | 
| 47600 | 2665 | from inequality approx[OF Less.prems(2) l_eq] approx[OF Less.prems(2) u_eq] | 
| 31811 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2666 | show ?case by auto | 
| 29805 | 2667 | next | 
| 2668 | case (LessEqual a b) | |
| 31811 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2669 | then obtain l u l' u' | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2670 | where l_eq: "Some (l, u) = approx prec a vs" | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2671 | and u_eq: "Some (l', u') = approx prec b vs" | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2672 | and inequality: "u \<le> l'" | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2673 | by (cases "approx prec a vs", auto, | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2674 | cases "approx prec b vs", auto) | 
| 47600 | 2675 | from inequality approx[OF LessEqual.prems(2) l_eq] approx[OF LessEqual.prems(2) u_eq] | 
| 31811 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2676 | show ?case by auto | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2677 | next | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2678 | case (AtLeastAtMost x a b) | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2679 | then obtain lx ux l u l' u' | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2680 | where x_eq: "Some (lx, ux) = approx prec x vs" | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2681 | and l_eq: "Some (l, u) = approx prec a vs" | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2682 | and u_eq: "Some (l', u') = approx prec b vs" | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2683 | and inequality: "u \<le> lx \<and> ux \<le> l'" | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2684 | by (cases "approx prec x vs", auto, | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2685 | cases "approx prec a vs", auto, | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2686 | cases "approx prec b vs", auto, blast) | 
| 47600 | 2687 | from inequality approx[OF AtLeastAtMost.prems(2) l_eq] approx[OF AtLeastAtMost.prems(2) u_eq] approx[OF AtLeastAtMost.prems(2) x_eq] | 
| 31811 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2688 | show ?case by auto | 
| 29805 | 2689 | qed | 
| 2690 | ||
| 31811 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2691 | lemma approx_form: | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2692 | assumes "n = length xs" | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2693 | assumes "approx_form prec f (replicate n None) ss" | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2694 | shows "interpret_form f xs" | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 2695 | using approx_form_aux[OF _ bounded_by_None] assms by auto | 
| 29805 | 2696 | |
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2697 | subsection {* Implementing Taylor series expansion *}
 | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2698 | |
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2699 | fun isDERIV :: "nat \<Rightarrow> floatarith \<Rightarrow> real list \<Rightarrow> bool" where | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2700 | "isDERIV x (Add a b) vs = (isDERIV x a vs \<and> isDERIV x b vs)" | | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2701 | "isDERIV x (Mult a b) vs = (isDERIV x a vs \<and> isDERIV x b vs)" | | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2702 | "isDERIV x (Minus a) vs = isDERIV x a vs" | | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2703 | "isDERIV x (Inverse a) vs = (isDERIV x a vs \<and> interpret_floatarith a vs \<noteq> 0)" | | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2704 | "isDERIV x (Cos a) vs = isDERIV x a vs" | | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2705 | "isDERIV x (Arctan a) vs = isDERIV x a vs" | | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2706 | "isDERIV x (Min a b) vs = False" | | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2707 | "isDERIV x (Max a b) vs = False" | | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2708 | "isDERIV x (Abs a) vs = False" | | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2709 | "isDERIV x Pi vs = True" | | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2710 | "isDERIV x (Sqrt a) vs = (isDERIV x a vs \<and> interpret_floatarith a vs > 0)" | | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2711 | "isDERIV x (Exp a) vs = isDERIV x a vs" | | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2712 | "isDERIV x (Ln a) vs = (isDERIV x a vs \<and> interpret_floatarith a vs > 0)" | | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2713 | "isDERIV x (Power a 0) vs = True" | | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2714 | "isDERIV x (Power a (Suc n)) vs = isDERIV x a vs" | | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2715 | "isDERIV x (Num f) vs = True" | | 
| 32919 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 2716 | "isDERIV x (Var n) vs = True" | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2717 | |
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2718 | fun DERIV_floatarith :: "nat \<Rightarrow> floatarith \<Rightarrow> floatarith" where | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2719 | "DERIV_floatarith x (Add a b) = Add (DERIV_floatarith x a) (DERIV_floatarith x b)" | | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2720 | "DERIV_floatarith x (Mult a b) = Add (Mult a (DERIV_floatarith x b)) (Mult (DERIV_floatarith x a) b)" | | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2721 | "DERIV_floatarith x (Minus a) = Minus (DERIV_floatarith x a)" | | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2722 | "DERIV_floatarith x (Inverse a) = Minus (Mult (DERIV_floatarith x a) (Inverse (Power a 2)))" | | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2723 | "DERIV_floatarith x (Cos a) = Minus (Mult (Cos (Add (Mult Pi (Num (Float 1 -1))) (Minus a))) (DERIV_floatarith x a))" | | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2724 | "DERIV_floatarith x (Arctan a) = Mult (Inverse (Add (Num 1) (Power a 2))) (DERIV_floatarith x a)" | | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2725 | "DERIV_floatarith x (Min a b) = Num 0" | | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2726 | "DERIV_floatarith x (Max a b) = Num 0" | | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2727 | "DERIV_floatarith x (Abs a) = Num 0" | | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2728 | "DERIV_floatarith x Pi = Num 0" | | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2729 | "DERIV_floatarith x (Sqrt a) = (Mult (Inverse (Mult (Sqrt a) (Num 2))) (DERIV_floatarith x a))" | | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2730 | "DERIV_floatarith x (Exp a) = Mult (Exp a) (DERIV_floatarith x a)" | | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2731 | "DERIV_floatarith x (Ln a) = Mult (Inverse a) (DERIV_floatarith x a)" | | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2732 | "DERIV_floatarith x (Power a 0) = Num 0" | | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2733 | "DERIV_floatarith x (Power a (Suc n)) = Mult (Num (Float (int (Suc n)) 0)) (Mult (Power a n) (DERIV_floatarith x a))" | | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2734 | "DERIV_floatarith x (Num f) = Num 0" | | 
| 32919 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 2735 | "DERIV_floatarith x (Var n) = (if x = n then Num 1 else Num 0)" | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2736 | |
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2737 | lemma DERIV_floatarith: | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2738 | assumes "n < length vs" | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2739 | assumes isDERIV: "isDERIV n f (vs[n := x])" | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2740 | shows "DERIV (\<lambda> x'. interpret_floatarith f (vs[n := x'])) x :> | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2741 | interpret_floatarith (DERIV_floatarith n f) (vs[n := x])" | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2742 | (is "DERIV (?i f) x :> _") | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2743 | using isDERIV proof (induct f arbitrary: x) | 
| 31881 | 2744 | case (Inverse a) thus ?case | 
| 2745 | by (auto intro!: DERIV_intros | |
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2746 | simp add: algebra_simps power2_eq_square) | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2747 | next case (Cos a) thus ?case | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 2748 | apply (auto intro!: DERIV_intros | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2749 | simp del: interpret_floatarith.simps(5) | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2750 | simp add: interpret_floatarith_sin interpret_floatarith.simps(5)[of a]) | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 2751 | done | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2752 | next case (Power a n) thus ?case | 
| 31881 | 2753 | by (cases n, auto intro!: DERIV_intros | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 2754 | simp del: power_Suc) | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2755 | next case (Ln a) thus ?case | 
| 31881 | 2756 | by (auto intro!: DERIV_intros simp add: divide_inverse) | 
| 32919 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 2757 | next case (Var i) thus ?case using `n < length vs` by auto | 
| 31881 | 2758 | qed (auto intro!: DERIV_intros) | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2759 | |
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2760 | declare approx.simps[simp del] | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2761 | |
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2762 | fun isDERIV_approx :: "nat \<Rightarrow> nat \<Rightarrow> floatarith \<Rightarrow> (float * float) option list \<Rightarrow> bool" where | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2763 | "isDERIV_approx prec x (Add a b) vs = (isDERIV_approx prec x a vs \<and> isDERIV_approx prec x b vs)" | | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2764 | "isDERIV_approx prec x (Mult a b) vs = (isDERIV_approx prec x a vs \<and> isDERIV_approx prec x b vs)" | | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2765 | "isDERIV_approx prec x (Minus a) vs = isDERIV_approx prec x a vs" | | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2766 | "isDERIV_approx prec x (Inverse a) vs = | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2767 | (isDERIV_approx prec x a vs \<and> (case approx prec a vs of Some (l, u) \<Rightarrow> 0 < l \<or> u < 0 | None \<Rightarrow> False))" | | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2768 | "isDERIV_approx prec x (Cos a) vs = isDERIV_approx prec x a vs" | | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2769 | "isDERIV_approx prec x (Arctan a) vs = isDERIV_approx prec x a vs" | | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2770 | "isDERIV_approx prec x (Min a b) vs = False" | | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2771 | "isDERIV_approx prec x (Max a b) vs = False" | | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2772 | "isDERIV_approx prec x (Abs a) vs = False" | | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2773 | "isDERIV_approx prec x Pi vs = True" | | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2774 | "isDERIV_approx prec x (Sqrt a) vs = | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2775 | (isDERIV_approx prec x a vs \<and> (case approx prec a vs of Some (l, u) \<Rightarrow> 0 < l | None \<Rightarrow> False))" | | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2776 | "isDERIV_approx prec x (Exp a) vs = isDERIV_approx prec x a vs" | | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2777 | "isDERIV_approx prec x (Ln a) vs = | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2778 | (isDERIV_approx prec x a vs \<and> (case approx prec a vs of Some (l, u) \<Rightarrow> 0 < l | None \<Rightarrow> False))" | | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2779 | "isDERIV_approx prec x (Power a 0) vs = True" | | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2780 | "isDERIV_approx prec x (Power a (Suc n)) vs = isDERIV_approx prec x a vs" | | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2781 | "isDERIV_approx prec x (Num f) vs = True" | | 
| 32919 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 2782 | "isDERIV_approx prec x (Var n) vs = True" | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2783 | |
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2784 | lemma isDERIV_approx: | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2785 | assumes "bounded_by xs vs" | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2786 | and isDERIV_approx: "isDERIV_approx prec x f vs" | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2787 | shows "isDERIV x f xs" | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2788 | using isDERIV_approx proof (induct f) | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2789 | case (Inverse a) | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2790 | then obtain l u where approx_Some: "Some (l, u) = approx prec a vs" | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2791 | and *: "0 < l \<or> u < 0" | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2792 | by (cases "approx prec a vs", auto) | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2793 | with approx[OF `bounded_by xs vs` approx_Some] | 
| 47600 | 2794 | have "interpret_floatarith a xs \<noteq> 0" by auto | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2795 | thus ?case using Inverse by auto | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2796 | next | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2797 | case (Ln a) | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2798 | then obtain l u where approx_Some: "Some (l, u) = approx prec a vs" | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2799 | and *: "0 < l" | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2800 | by (cases "approx prec a vs", auto) | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2801 | with approx[OF `bounded_by xs vs` approx_Some] | 
| 47600 | 2802 | have "0 < interpret_floatarith a xs" by auto | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2803 | thus ?case using Ln by auto | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2804 | next | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2805 | case (Sqrt a) | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2806 | then obtain l u where approx_Some: "Some (l, u) = approx prec a vs" | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2807 | and *: "0 < l" | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2808 | by (cases "approx prec a vs", auto) | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2809 | with approx[OF `bounded_by xs vs` approx_Some] | 
| 47600 | 2810 | have "0 < interpret_floatarith a xs" by auto | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2811 | thus ?case using Sqrt by auto | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2812 | next | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2813 | case (Power a n) thus ?case by (cases n, auto) | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2814 | qed auto | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2815 | |
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2816 | lemma bounded_by_update_var: | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2817 | assumes "bounded_by xs vs" and "vs ! i = Some (l, u)" | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2818 |   and bnd: "x \<in> { real l .. real u }"
 | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2819 | shows "bounded_by (xs[i := x]) vs" | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2820 | proof (cases "i < length xs") | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2821 | case False thus ?thesis using `bounded_by xs vs` by auto | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2822 | next | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2823 | let ?xs = "xs[i := x]" | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2824 | case True hence "i < length ?xs" by auto | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2825 | { fix j
 | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2826 | assume "j < length vs" | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2827 |   have "case vs ! j of None \<Rightarrow> True | Some (l, u) \<Rightarrow> ?xs ! j \<in> { real l .. real u }"
 | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2828 | proof (cases "vs ! j") | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2829 | case (Some b) | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2830 | thus ?thesis | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2831 | proof (cases "i = j") | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2832 | case True | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2833 | thus ?thesis using `vs ! i = Some (l, u)` Some and bnd `i < length ?xs` | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 2834 | by auto | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2835 | next | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2836 | case False | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2837 | thus ?thesis using `bounded_by xs vs`[THEN bounded_byE, OF `j < length vs`] Some | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 2838 | by auto | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2839 | qed | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2840 | qed auto } | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2841 | thus ?thesis unfolding bounded_by_def by auto | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2842 | qed | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2843 | |
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2844 | lemma isDERIV_approx': | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2845 | assumes "bounded_by xs vs" | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2846 |   and vs_x: "vs ! x = Some (l, u)" and X_in: "X \<in> { real l .. real u }"
 | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2847 | and approx: "isDERIV_approx prec x f vs" | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2848 | shows "isDERIV x f (xs[x := X])" | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2849 | proof - | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2850 | note bounded_by_update_var[OF `bounded_by xs vs` vs_x X_in] approx | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2851 | thus ?thesis by (rule isDERIV_approx) | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2852 | qed | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2853 | |
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2854 | lemma DERIV_approx: | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2855 | assumes "n < length xs" and bnd: "bounded_by xs vs" | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2856 | and isD: "isDERIV_approx prec n f vs" | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2857 | and app: "Some (l, u) = approx prec (DERIV_floatarith n f) vs" (is "_ = approx _ ?D _") | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2858 | shows "\<exists>(x::real). l \<le> x \<and> x \<le> u \<and> | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2859 | DERIV (\<lambda> x. interpret_floatarith f (xs[n := x])) (xs!n) :> x" | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2860 | (is "\<exists> x. _ \<and> _ \<and> DERIV (?i f) _ :> _") | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2861 | proof (rule exI[of _ "?i ?D (xs!n)"], rule conjI[OF _ conjI]) | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2862 | let "?i f x" = "interpret_floatarith f (xs[n := x])" | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2863 | from approx[OF bnd app] | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2864 | show "l \<le> ?i ?D (xs!n)" and "?i ?D (xs!n) \<le> u" | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2865 | using `n < length xs` by auto | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2866 | from DERIV_floatarith[OF `n < length xs`, of f "xs!n"] isDERIV_approx[OF bnd isD] | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2867 | show "DERIV (?i f) (xs!n) :> (?i ?D (xs!n))" by simp | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2868 | qed | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2869 | |
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2870 | fun lift_bin :: "(float * float) option \<Rightarrow> (float * float) option \<Rightarrow> (float \<Rightarrow> float \<Rightarrow> float \<Rightarrow> float \<Rightarrow> (float * float) option) \<Rightarrow> (float * float) option" where | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2871 | "lift_bin (Some (l1, u1)) (Some (l2, u2)) f = f l1 u1 l2 u2" | | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2872 | "lift_bin a b f = None" | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2873 | |
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2874 | lemma lift_bin: | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2875 | assumes lift_bin_Some: "Some (l, u) = lift_bin a b f" | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2876 | obtains l1 u1 l2 u2 | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2877 | where "a = Some (l1, u1)" | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2878 | and "b = Some (l2, u2)" | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2879 | and "f l1 u1 l2 u2 = Some (l, u)" | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2880 | using assms by (cases a, simp, cases b, simp, auto) | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2881 | |
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2882 | fun approx_tse where | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2883 | "approx_tse prec n 0 c k f bs = approx prec f bs" | | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2884 | "approx_tse prec n (Suc s) c k f bs = | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2885 | (if isDERIV_approx prec n f bs then | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2886 | lift_bin (approx prec f (bs[n := Some (c,c)])) | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2887 | (approx_tse prec n s c (Suc k) (DERIV_floatarith n f) bs) | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2888 | (\<lambda> l1 u1 l2 u2. approx prec | 
| 32919 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 2889 | (Add (Var 0) | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2890 | (Mult (Inverse (Num (Float (int k) 0))) | 
| 32919 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 2891 | (Mult (Add (Var (Suc (Suc 0))) (Minus (Num c))) | 
| 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 2892 | (Var (Suc 0))))) [Some (l1, u1), Some (l2, u2), bs!n]) | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2893 | else approx prec f bs)" | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2894 | |
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2895 | lemma bounded_by_Cons: | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2896 | assumes bnd: "bounded_by xs vs" | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2897 |   and x: "x \<in> { real l .. real u }"
 | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2898 | shows "bounded_by (x#xs) ((Some (l, u))#vs)" | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2899 | proof - | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2900 |   { fix i assume *: "i < length ((Some (l, u))#vs)"
 | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2901 |     have "case ((Some (l,u))#vs) ! i of Some (l, u) \<Rightarrow> (x#xs)!i \<in> { real l .. real u } | None \<Rightarrow> True"
 | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2902 | proof (cases i) | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2903 | case 0 with x show ?thesis by auto | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2904 | next | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2905 | case (Suc i) with * have "i < length vs" by auto | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2906 | from bnd[THEN bounded_byE, OF this] | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2907 | show ?thesis unfolding Suc nth_Cons_Suc . | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2908 | qed } | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2909 | thus ?thesis by (auto simp add: bounded_by_def) | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2910 | qed | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2911 | |
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2912 | lemma approx_tse_generic: | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2913 | assumes "bounded_by xs vs" | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2914 | and bnd_c: "bounded_by (xs[x := c]) vs" and "x < length vs" and "x < length xs" | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2915 | and bnd_x: "vs ! x = Some (lx, ux)" | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2916 | and ate: "Some (l, u) = approx_tse prec x s c k f vs" | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2917 |   shows "\<exists> n. (\<forall> m < n. \<forall> (z::real) \<in> {lx .. ux}.
 | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2918 | DERIV (\<lambda> y. interpret_floatarith ((DERIV_floatarith x ^^ m) f) (xs[x := y])) z :> | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2919 | (interpret_floatarith ((DERIV_floatarith x ^^ (Suc m)) f) (xs[x := z]))) | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2920 |    \<and> (\<forall> (t::real) \<in> {lx .. ux}.  (\<Sum> i = 0..<n. inverse (real (\<Prod> j \<in> {k..<k+i}. j)) *
 | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2921 | interpret_floatarith ((DERIV_floatarith x ^^ i) f) (xs[x := c]) * | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2922 | (xs!x - c)^i) + | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2923 |       inverse (real (\<Prod> j \<in> {k..<k+n}. j)) *
 | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2924 | interpret_floatarith ((DERIV_floatarith x ^^ n) f) (xs[x := t]) * | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2925 |       (xs!x - c)^n \<in> {l .. u})" (is "\<exists> n. ?taylor f k l u n")
 | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2926 | using ate proof (induct s arbitrary: k f l u) | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2927 | case 0 | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2928 |   { fix t::real assume "t \<in> {lx .. ux}"
 | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2929 | note bounded_by_update_var[OF `bounded_by xs vs` bnd_x this] | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2930 | from approx[OF this 0[unfolded approx_tse.simps]] | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2931 |     have "(interpret_floatarith f (xs[x := t])) \<in> {l .. u}"
 | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2932 | by (auto simp add: algebra_simps) | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2933 | } thus ?case by (auto intro!: exI[of _ 0]) | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2934 | next | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2935 | case (Suc s) | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2936 | show ?case | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2937 | proof (cases "isDERIV_approx prec x f vs") | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2938 | case False | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2939 | note ap = Suc.prems[unfolded approx_tse.simps if_not_P[OF False]] | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2940 | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2941 |     { fix t::real assume "t \<in> {lx .. ux}"
 | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2942 | note bounded_by_update_var[OF `bounded_by xs vs` bnd_x this] | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2943 | from approx[OF this ap] | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2944 |       have "(interpret_floatarith f (xs[x := t])) \<in> {l .. u}"
 | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 2945 | by (auto simp add: algebra_simps) | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2946 | } thus ?thesis by (auto intro!: exI[of _ 0]) | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2947 | next | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2948 | case True | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2949 | with Suc.prems | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2950 | obtain l1 u1 l2 u2 | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2951 | where a: "Some (l1, u1) = approx prec f (vs[x := Some (c,c)])" | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2952 | and ate: "Some (l2, u2) = approx_tse prec x s c (Suc k) (DERIV_floatarith x f) vs" | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2953 | and final: "Some (l, u) = approx prec | 
| 32919 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 2954 | (Add (Var 0) | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2955 | (Mult (Inverse (Num (Float (int k) 0))) | 
| 32919 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 2956 | (Mult (Add (Var (Suc (Suc 0))) (Minus (Num c))) | 
| 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 2957 | (Var (Suc 0))))) [Some (l1, u1), Some (l2, u2), vs!x]" | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2958 | by (auto elim!: lift_bin) blast | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2959 | |
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2960 | from bnd_c `x < length xs` | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2961 | have bnd: "bounded_by (xs[x:=c]) (vs[x:= Some (c,c)])" | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2962 | by (auto intro!: bounded_by_update) | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2963 | |
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2964 | from approx[OF this a] | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2965 |     have f_c: "interpret_floatarith ((DERIV_floatarith x ^^ 0) f) (xs[x := c]) \<in> { l1 .. u1 }"
 | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2966 | (is "?f 0 (real c) \<in> _") | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2967 | by auto | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2968 | |
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2969 |     { fix f :: "'a \<Rightarrow> 'a" fix n :: nat fix x :: 'a
 | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2970 | have "(f ^^ Suc n) x = (f ^^ n) (f x)" | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 2971 | by (induct n, auto) } | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2972 | note funpow_Suc = this[symmetric] | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2973 | from Suc.hyps[OF ate, unfolded this] | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2974 | obtain n | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2975 |       where DERIV_hyp: "\<And> m z. \<lbrakk> m < n ; (z::real) \<in> { lx .. ux } \<rbrakk> \<Longrightarrow> DERIV (?f (Suc m)) z :> ?f (Suc (Suc m)) z"
 | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2976 |       and hyp: "\<forall> t \<in> {real lx .. real ux}. (\<Sum> i = 0..<n. inverse (real (\<Prod> j \<in> {Suc k..<Suc k + i}. j)) * ?f (Suc i) c * (xs!x - c)^i) +
 | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2977 |            inverse (real (\<Prod> j \<in> {Suc k..<Suc k + n}. j)) * ?f (Suc n) t * (xs!x - c)^n \<in> {l2 .. u2}"
 | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2978 | (is "\<forall> t \<in> _. ?X (Suc k) f n t \<in> _") | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2979 | by blast | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2980 | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2981 |     { fix m and z::real
 | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 2982 |       assume "m < Suc n" and bnd_z: "z \<in> { lx .. ux }"
 | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2983 | have "DERIV (?f m) z :> ?f (Suc m) z" | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2984 | proof (cases m) | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 2985 | case 0 | 
| 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 2986 | with DERIV_floatarith[OF `x < length xs` isDERIV_approx'[OF `bounded_by xs vs` bnd_x bnd_z True]] | 
| 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 2987 | show ?thesis by simp | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2988 | next | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 2989 | case (Suc m') | 
| 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 2990 | hence "m' < n" using `m < Suc n` by auto | 
| 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 2991 | from DERIV_hyp[OF this bnd_z] | 
| 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 2992 | show ?thesis using Suc by simp | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2993 | qed } note DERIV = this | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2994 | |
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2995 |     have "\<And> k i. k < i \<Longrightarrow> {k ..< i} = insert k {Suc k ..< i}" by auto
 | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2996 |     hence setprod_head_Suc: "\<And> k i. \<Prod> {k ..< k + Suc i} = k * \<Prod> {Suc k ..< Suc k + i}" by auto
 | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2997 |     have setsum_move0: "\<And> k F. setsum F {0..<Suc k} = F 0 + setsum (\<lambda> k. F (Suc k)) {0..<k}"
 | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2998 | unfolding setsum_shift_bounds_Suc_ivl[symmetric] | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 2999 | unfolding setsum_head_upt_Suc[OF zero_less_Suc] .. | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 3000 | def C \<equiv> "xs!x - c" | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 3001 | |
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 3002 |     { fix t::real assume t: "t \<in> {lx .. ux}"
 | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3003 | hence "bounded_by [xs!x] [vs!x]" | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 3004 | using `bounded_by xs vs`[THEN bounded_byE, OF `x < length vs`] | 
| 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 3005 | by (cases "vs!x", auto simp add: bounded_by_def) | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3006 | |
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3007 | with hyp[THEN bspec, OF t] f_c | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 3008 | have "bounded_by [?f 0 c, ?X (Suc k) f n t, xs!x] [Some (l1, u1), Some (l2, u2), vs!x]" | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 3009 | by (auto intro!: bounded_by_Cons) | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3010 | from approx[OF this final, unfolded atLeastAtMost_iff[symmetric]] | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 3011 |       have "?X (Suc k) f n t * (xs!x - real c) * inverse k + ?f 0 c \<in> {l .. u}"
 | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 3012 | by (auto simp add: algebra_simps) | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 3013 | also have "?X (Suc k) f n t * (xs!x - real c) * inverse (real k) + ?f 0 c = | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 3014 |                (\<Sum> i = 0..<Suc n. inverse (real (\<Prod> j \<in> {k..<k+i}. j)) * ?f i c * (xs!x - c)^i) +
 | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 3015 |                inverse (real (\<Prod> j \<in> {k..<k+Suc n}. j)) * ?f (Suc n) t * (xs!x - c)^Suc n" (is "_ = ?T")
 | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 3016 | unfolding funpow_Suc C_def[symmetric] setsum_move0 setprod_head_Suc | 
| 35082 | 3017 | by (auto simp add: algebra_simps) | 
| 3018 | (simp only: mult_left_commute [of _ "inverse (real k)"] setsum_right_distrib [symmetric]) | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 3019 |       finally have "?T \<in> {l .. u}" . }
 | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3020 | thus ?thesis using DERIV by blast | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3021 | qed | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3022 | qed | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3023 | |
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3024 | lemma setprod_fact: "\<Prod> {1..<1 + k} = fact (k :: nat)"
 | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3025 | proof (induct k) | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3026 | case (Suc k) | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3027 |   have "{ 1 ..< Suc (Suc k) } = insert (Suc k) { 1 ..< Suc k }" by auto
 | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3028 |   hence "\<Prod> { 1 ..< Suc (Suc k) } = (Suc k) * \<Prod> { 1 ..< Suc k }" by auto
 | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3029 | thus ?case using Suc by auto | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3030 | qed simp | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3031 | |
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3032 | lemma approx_tse: | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3033 | assumes "bounded_by xs vs" | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 3034 |   and bnd_x: "vs ! x = Some (lx, ux)" and bnd_c: "real c \<in> {lx .. ux}"
 | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3035 | and "x < length vs" and "x < length xs" | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3036 | and ate: "Some (l, u) = approx_tse prec x s c 1 f vs" | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 3037 |   shows "interpret_floatarith f xs \<in> { l .. u }"
 | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3038 | proof - | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3039 | def F \<equiv> "\<lambda> n z. interpret_floatarith ((DERIV_floatarith x ^^ n) f) (xs[x := z])" | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3040 | hence F0: "F 0 = (\<lambda> z. interpret_floatarith f (xs[x := z]))" by auto | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3041 | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 3042 | hence "bounded_by (xs[x := c]) vs" and "x < length vs" "x < length xs" | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3043 | using `bounded_by xs vs` bnd_x bnd_c `x < length vs` `x < length xs` | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3044 | by (auto intro!: bounded_by_update_var) | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3045 | |
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3046 | from approx_tse_generic[OF `bounded_by xs vs` this bnd_x ate] | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3047 | obtain n | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3048 | where DERIV: "\<forall> m z. m < n \<and> real lx \<le> z \<and> z \<le> real ux \<longrightarrow> DERIV (F m) z :> F (Suc m) z" | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 3049 |     and hyp: "\<And> (t::real). t \<in> {lx .. ux} \<Longrightarrow>
 | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 3050 | (\<Sum> j = 0..<n. inverse (real (fact j)) * F j c * (xs!x - c)^j) + | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 3051 | inverse (real (fact n)) * F n t * (xs!x - c)^n | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 3052 |              \<in> {l .. u}" (is "\<And> t. _ \<Longrightarrow> ?taylor t \<in> _")
 | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3053 | unfolding F_def atLeastAtMost_iff[symmetric] setprod_fact by blast | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3054 | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 3055 |   have bnd_xs: "xs ! x \<in> { lx .. ux }"
 | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3056 | using `bounded_by xs vs`[THEN bounded_byE, OF `x < length vs`] bnd_x by auto | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3057 | |
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3058 | show ?thesis | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3059 | proof (cases n) | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3060 | case 0 thus ?thesis using hyp[OF bnd_xs] unfolding F_def by auto | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3061 | next | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3062 | case (Suc n') | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3063 | show ?thesis | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 3064 | proof (cases "xs ! x = c") | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3065 | case True | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3066 | from True[symmetric] hyp[OF bnd_xs] Suc show ?thesis | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 3067 | unfolding F_def Suc setsum_head_upt_Suc[OF zero_less_Suc] setsum_shift_bounds_Suc_ivl by auto | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3068 | next | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3069 | case False | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3070 | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 3071 | have "lx \<le> real c" "real c \<le> ux" "lx \<le> xs!x" "xs!x \<le> ux" | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 3072 | using Suc bnd_c `bounded_by xs vs`[THEN bounded_byE, OF `x < length vs`] bnd_x by auto | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3073 | from Taylor.taylor[OF zero_less_Suc, of F, OF F0 DERIV[unfolded Suc] this False] | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 3074 | obtain t::real where t_bnd: "if xs ! x < c then xs ! x < t \<and> t < c else c < t \<and> t < xs ! x" | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 3075 | and fl_eq: "interpret_floatarith f (xs[x := xs ! x]) = | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 3076 | (\<Sum>m = 0..<Suc n'. F m c / real (fact m) * (xs ! x - c) ^ m) + | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 3077 | F (Suc n') t / real (fact (Suc n')) * (xs ! x - c) ^ Suc n'" | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 3078 | by blast | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3079 | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 3080 |       from t_bnd bnd_xs bnd_c have *: "t \<in> {lx .. ux}"
 | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 3081 | by (cases "xs ! x < c", auto) | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3082 | |
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3083 | have "interpret_floatarith f (xs[x := xs ! x]) = ?taylor t" | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 3084 | unfolding fl_eq Suc by (auto simp add: algebra_simps divide_inverse) | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 3085 |       also have "\<dots> \<in> {l .. u}" using * by (rule hyp)
 | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3086 | finally show ?thesis by simp | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3087 | qed | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3088 | qed | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3089 | qed | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3090 | |
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3091 | fun approx_tse_form' where | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3092 | "approx_tse_form' prec t f 0 l u cmp = | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3093 | (case approx_tse prec 0 t ((l + u) * Float 1 -1) 1 f [Some (l, u)] | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3094 | of Some (l, u) \<Rightarrow> cmp l u | None \<Rightarrow> False)" | | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3095 | "approx_tse_form' prec t f (Suc s) l u cmp = | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3096 | (let m = (l + u) * Float 1 -1 | 
| 32919 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 3097 | in (if approx_tse_form' prec t f s l m cmp then | 
| 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 3098 | approx_tse_form' prec t f s m u cmp else False))" | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3099 | |
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3100 | lemma approx_tse_form': | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 3101 | fixes x :: real | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 3102 |   assumes "approx_tse_form' prec t f s l u cmp" and "x \<in> {l .. u}"
 | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 3103 |   shows "\<exists> l' u' ly uy. x \<in> { l' .. u' } \<and> real l \<le> l' \<and> u' \<le> real u \<and> cmp ly uy \<and>
 | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3104 | approx_tse prec 0 t ((l' + u') * Float 1 -1) 1 f [Some (l', u')] = Some (ly, uy)" | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3105 | using assms proof (induct s arbitrary: l u) | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3106 | case 0 | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3107 | then obtain ly uy | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3108 | where *: "approx_tse prec 0 t ((l + u) * Float 1 -1) 1 f [Some (l, u)] = Some (ly, uy)" | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3109 | and **: "cmp ly uy" by (auto elim!: option_caseE) | 
| 46545 | 3110 | with 0 show ?case by auto | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3111 | next | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3112 | case (Suc s) | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3113 | let ?m = "(l + u) * Float 1 -1" | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3114 | from Suc.prems | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3115 | have l: "approx_tse_form' prec t f s l ?m cmp" | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3116 | and u: "approx_tse_form' prec t f s ?m u cmp" | 
| 32919 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 3117 | by (auto simp add: Let_def lazy_conj) | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3118 | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 3119 | have m_l: "real l \<le> ?m" and m_u: "?m \<le> real u" | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47108diff
changeset | 3120 | unfolding less_eq_float_def using Suc.prems by auto | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3121 | |
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 3122 |   with `x \<in> { l .. u }`
 | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 3123 |   have "x \<in> { l .. ?m} \<or> x \<in> { ?m .. u }" by auto
 | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3124 | thus ?case | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3125 | proof (rule disjE) | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 3126 |     assume "x \<in> { l .. ?m}"
 | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3127 | from Suc.hyps[OF l this] | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3128 | obtain l' u' ly uy | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 3129 |       where "x \<in> { l' .. u' } \<and> real l \<le> l' \<and> real u' \<le> ?m \<and> cmp ly uy \<and>
 | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3130 | approx_tse prec 0 t ((l' + u') * Float 1 -1) 1 f [Some (l', u')] = Some (ly, uy)" by blast | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3131 | with m_u show ?thesis by (auto intro!: exI) | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3132 | next | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 3133 |     assume "x \<in> { ?m .. u }"
 | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3134 | from Suc.hyps[OF u this] | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3135 | obtain l' u' ly uy | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 3136 |       where "x \<in> { l' .. u' } \<and> ?m \<le> real l' \<and> u' \<le> real u \<and> cmp ly uy \<and>
 | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3137 | approx_tse prec 0 t ((l' + u') * Float 1 -1) 1 f [Some (l', u')] = Some (ly, uy)" by blast | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3138 | with m_u show ?thesis by (auto intro!: exI) | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3139 | qed | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3140 | qed | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3141 | |
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3142 | lemma approx_tse_form'_less: | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 3143 | fixes x :: real | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3144 | assumes tse: "approx_tse_form' prec t (Add a (Minus b)) s l u (\<lambda> l u. 0 < l)" | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 3145 |   and x: "x \<in> {l .. u}"
 | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3146 | shows "interpret_floatarith b [x] < interpret_floatarith a [x]" | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3147 | proof - | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3148 | from approx_tse_form'[OF tse x] | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3149 | obtain l' u' ly uy | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 3150 |     where x': "x \<in> { l' .. u' }" and "l \<le> real l'"
 | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 3151 | and "real u' \<le> u" and "0 < ly" | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3152 | and tse: "approx_tse prec 0 t ((l' + u') * Float 1 -1) 1 (Add a (Minus b)) [Some (l', u')] = Some (ly, uy)" | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3153 | by blast | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3154 | |
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3155 | hence "bounded_by [x] [Some (l', u')]" by (auto simp add: bounded_by_def) | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3156 | |
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3157 | from approx_tse[OF this _ _ _ _ tse[symmetric], of l' u'] x' | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 3158 | have "ly \<le> interpret_floatarith a [x] - interpret_floatarith b [x]" | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3159 | by (auto simp add: diff_minus) | 
| 47600 | 3160 | from order_less_le_trans[OF _ this, of 0] `0 < ly` | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3161 | show ?thesis by auto | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3162 | qed | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3163 | |
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3164 | lemma approx_tse_form'_le: | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 3165 | fixes x :: real | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3166 | assumes tse: "approx_tse_form' prec t (Add a (Minus b)) s l u (\<lambda> l u. 0 \<le> l)" | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 3167 |   and x: "x \<in> {l .. u}"
 | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3168 | shows "interpret_floatarith b [x] \<le> interpret_floatarith a [x]" | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3169 | proof - | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3170 | from approx_tse_form'[OF tse x] | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3171 | obtain l' u' ly uy | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 3172 |     where x': "x \<in> { l' .. u' }" and "l \<le> real l'"
 | 
| 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 3173 | and "real u' \<le> u" and "0 \<le> ly" | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3174 | and tse: "approx_tse prec 0 t ((l' + u') * Float 1 -1) 1 (Add a (Minus b)) [Some (l', u')] = Some (ly, uy)" | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3175 | by blast | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3176 | |
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3177 | hence "bounded_by [x] [Some (l', u')]" by (auto simp add: bounded_by_def) | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3178 | |
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3179 | from approx_tse[OF this _ _ _ _ tse[symmetric], of l' u'] x' | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 3180 | have "ly \<le> interpret_floatarith a [x] - interpret_floatarith b [x]" | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3181 | by (auto simp add: diff_minus) | 
| 47600 | 3182 | from order_trans[OF _ this, of 0] `0 \<le> ly` | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3183 | show ?thesis by auto | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3184 | qed | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3185 | |
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3186 | definition | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3187 | "approx_tse_form prec t s f = | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3188 | (case f | 
| 32919 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 3189 | of (Bound x a b f) \<Rightarrow> x = Var 0 \<and> | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3190 | (case (approx prec a [None], approx prec b [None]) | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3191 | of (Some (l, u), Some (l', u')) \<Rightarrow> | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3192 | (case f | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3193 | of Less lf rt \<Rightarrow> approx_tse_form' prec t (Add rt (Minus lf)) s l u' (\<lambda> l u. 0 < l) | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3194 | | LessEqual lf rt \<Rightarrow> approx_tse_form' prec t (Add rt (Minus lf)) s l u' (\<lambda> l u. 0 \<le> l) | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3195 | | AtLeastAtMost x lf rt \<Rightarrow> | 
| 32919 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 3196 | (if approx_tse_form' prec t (Add x (Minus lf)) s l u' (\<lambda> l u. 0 \<le> l) then | 
| 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 3197 | approx_tse_form' prec t (Add rt (Minus x)) s l u' (\<lambda> l u. 0 \<le> l) else False) | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3198 | | _ \<Rightarrow> False) | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3199 | | _ \<Rightarrow> False) | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3200 | | _ \<Rightarrow> False)" | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3201 | |
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3202 | lemma approx_tse_form: | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3203 | assumes "approx_tse_form prec t s f" | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3204 | shows "interpret_form f [x]" | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3205 | proof (cases f) | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3206 | case (Bound i a b f') note f_def = this | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3207 | with assms obtain l u l' u' | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3208 | where a: "approx prec a [None] = Some (l, u)" | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3209 | and b: "approx prec b [None] = Some (l', u')" | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3210 | unfolding approx_tse_form_def by (auto elim!: option_caseE) | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3211 | |
| 32919 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 3212 | from Bound assms have "i = Var 0" unfolding approx_tse_form_def by auto | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3213 | hence i: "interpret_floatarith i [x] = x" by auto | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3214 | |
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3215 |   { let "?f z" = "interpret_floatarith z [x]"
 | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3216 |     assume "?f i \<in> { ?f a .. ?f b }"
 | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3217 | with approx[OF _ a[symmetric], of "[x]"] approx[OF _ b[symmetric], of "[x]"] | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 3218 |     have bnd: "x \<in> { l .. u'}" unfolding bounded_by_def i by auto
 | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3219 | |
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3220 | have "interpret_form f' [x]" | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3221 | proof (cases f') | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3222 | case (Less lf rt) | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3223 | with Bound a b assms | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3224 | have "approx_tse_form' prec t (Add rt (Minus lf)) s l u' (\<lambda> l u. 0 < l)" | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 3225 | unfolding approx_tse_form_def by auto | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3226 | from approx_tse_form'_less[OF this bnd] | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3227 | show ?thesis using Less by auto | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3228 | next | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3229 | case (LessEqual lf rt) | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3230 | with Bound a b assms | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3231 | have "approx_tse_form' prec t (Add rt (Minus lf)) s l u' (\<lambda> l u. 0 \<le> l)" | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 3232 | unfolding approx_tse_form_def by auto | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3233 | from approx_tse_form'_le[OF this bnd] | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3234 | show ?thesis using LessEqual by auto | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3235 | next | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3236 | case (AtLeastAtMost x lf rt) | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3237 | with Bound a b assms | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3238 | have "approx_tse_form' prec t (Add rt (Minus x)) s l u' (\<lambda> l u. 0 \<le> l)" | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 3239 | and "approx_tse_form' prec t (Add x (Minus lf)) s l u' (\<lambda> l u. 0 \<le> l)" | 
| 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32920diff
changeset | 3240 | unfolding approx_tse_form_def lazy_conj by auto | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3241 | from approx_tse_form'_le[OF this(1) bnd] approx_tse_form'_le[OF this(2) bnd] | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3242 | show ?thesis using AtLeastAtMost by auto | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3243 | next | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3244 | case (Bound x a b f') with assms | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3245 | show ?thesis by (auto elim!: option_caseE simp add: f_def approx_tse_form_def) | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3246 | next | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3247 | case (Assign x a f') with assms | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3248 | show ?thesis by (auto elim!: option_caseE simp add: f_def approx_tse_form_def) | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3249 | qed } thus ?thesis unfolding f_def by auto | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3250 | next case Assign with assms show ?thesis by (auto simp add: approx_tse_form_def) | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3251 | next case LessEqual with assms show ?thesis by (auto simp add: approx_tse_form_def) | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3252 | next case Less with assms show ?thesis by (auto simp add: approx_tse_form_def) | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3253 | next case AtLeastAtMost with assms show ?thesis by (auto simp add: approx_tse_form_def) | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3254 | qed | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3255 | |
| 32919 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 3256 | text {* @{term approx_form_eval} is only used for the {\tt value}-command. *}
 | 
| 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 3257 | |
| 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 3258 | fun approx_form_eval :: "nat \<Rightarrow> form \<Rightarrow> (float * float) option list \<Rightarrow> (float * float) option list" where | 
| 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 3259 | "approx_form_eval prec (Bound (Var n) a b f) bs = | 
| 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 3260 | (case (approx prec a bs, approx prec b bs) | 
| 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 3261 | of (Some (l, _), Some (_, u)) \<Rightarrow> approx_form_eval prec f (bs[n := Some (l, u)]) | 
| 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 3262 | | _ \<Rightarrow> bs)" | | 
| 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 3263 | "approx_form_eval prec (Assign (Var n) a f) bs = | 
| 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 3264 | (case (approx prec a bs) | 
| 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 3265 | of (Some (l, u)) \<Rightarrow> approx_form_eval prec f (bs[n := Some (l, u)]) | 
| 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 3266 | | _ \<Rightarrow> bs)" | | 
| 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 3267 | "approx_form_eval prec (Less a b) bs = bs @ [approx prec a bs, approx prec b bs]" | | 
| 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 3268 | "approx_form_eval prec (LessEqual a b) bs = bs @ [approx prec a bs, approx prec b bs]" | | 
| 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 3269 | "approx_form_eval prec (AtLeastAtMost x a b) bs = | 
| 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 3270 | bs @ [approx prec x bs, approx prec a bs, approx prec b bs]" | | 
| 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 3271 | "approx_form_eval _ _ bs = bs" | 
| 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 3272 | |
| 29805 | 3273 | subsection {* Implement proof method \texttt{approximation} *}
 | 
| 3274 | ||
| 31811 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 3275 | lemmas interpret_form_equations = interpret_form.simps interpret_floatarith.simps interpret_floatarith_num | 
| 31098 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 hoelzl parents: 
30971diff
changeset | 3276 | interpret_floatarith_divide interpret_floatarith_diff interpret_floatarith_tan interpret_floatarith_powr interpret_floatarith_log | 
| 31467 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 hoelzl parents: 
31148diff
changeset | 3277 | interpret_floatarith_sin | 
| 29805 | 3278 | |
| 36985 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3279 | oracle approximation_oracle = {* fn (thy, t) =>
 | 
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3280 | let | 
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3281 | |
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3282 |   fun bad t = error ("Bad term: " ^ Syntax.string_of_term_global thy t);
 | 
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3283 | |
| 38716 
3c3b4ad683d5
approximation_oracle: actually match true/false in ML, not arbitrary values;
 wenzelm parents: 
38558diff
changeset | 3284 |   fun term_of_bool true = @{term True}
 | 
| 
3c3b4ad683d5
approximation_oracle: actually match true/false in ML, not arbitrary values;
 wenzelm parents: 
38558diff
changeset | 3285 |     | term_of_bool false = @{term False};
 | 
| 36985 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3286 | |
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3287 |   fun term_of_float (@{code Float} (k, l)) =
 | 
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3288 |     @{term Float} $ HOLogic.mk_number @{typ int} k $ HOLogic.mk_number @{typ int} l;
 | 
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3289 | |
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3290 |   fun term_of_float_float_option NONE = @{term "None :: (float \<times> float) option"}
 | 
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3291 |     | term_of_float_float_option (SOME ff) = @{term "Some :: float \<times> float \<Rightarrow> _"}
 | 
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3292 | $ HOLogic.mk_prod (pairself term_of_float ff); | 
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3293 | |
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3294 | val term_of_float_float_option_list = | 
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3295 |     HOLogic.mk_list @{typ "(float \<times> float) option"} o map term_of_float_float_option;
 | 
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3296 | |
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3297 | fun nat_of_term t = HOLogic.dest_nat t handle TERM _ => snd (HOLogic.dest_number t); | 
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3298 | |
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3299 |   fun float_of_term (@{term Float} $ k $ l) =
 | 
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3300 |         @{code Float} (snd (HOLogic.dest_number k), snd (HOLogic.dest_number l))
 | 
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3301 | | float_of_term t = bad t; | 
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3302 | |
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3303 |   fun floatarith_of_term (@{term Add} $ a $ b) = @{code Add} (floatarith_of_term a, floatarith_of_term b)
 | 
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3304 |     | floatarith_of_term (@{term Minus} $ a) = @{code Minus} (floatarith_of_term a)
 | 
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3305 |     | floatarith_of_term (@{term Mult} $ a $ b) = @{code Mult} (floatarith_of_term a, floatarith_of_term b)
 | 
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3306 |     | floatarith_of_term (@{term Inverse} $ a) = @{code Inverse} (floatarith_of_term a)
 | 
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3307 |     | floatarith_of_term (@{term Cos} $ a) = @{code Cos} (floatarith_of_term a)
 | 
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3308 |     | floatarith_of_term (@{term Arctan} $ a) = @{code Arctan} (floatarith_of_term a)
 | 
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3309 |     | floatarith_of_term (@{term Abs} $ a) = @{code Abs} (floatarith_of_term a)
 | 
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3310 |     | floatarith_of_term (@{term Max} $ a $ b) = @{code Max} (floatarith_of_term a, floatarith_of_term b)
 | 
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3311 |     | floatarith_of_term (@{term Min} $ a $ b) = @{code Min} (floatarith_of_term a, floatarith_of_term b)
 | 
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3312 |     | floatarith_of_term @{term Pi} = @{code Pi}
 | 
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3313 |     | floatarith_of_term (@{term Sqrt} $ a) = @{code Sqrt} (floatarith_of_term a)
 | 
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3314 |     | floatarith_of_term (@{term Exp} $ a) = @{code Exp} (floatarith_of_term a)
 | 
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3315 |     | floatarith_of_term (@{term Ln} $ a) = @{code Ln} (floatarith_of_term a)
 | 
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3316 |     | floatarith_of_term (@{term Power} $ a $ n) =
 | 
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3317 |         @{code Power} (floatarith_of_term a, nat_of_term n)
 | 
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3318 |     | floatarith_of_term (@{term Var} $ n) = @{code Var} (nat_of_term n)
 | 
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3319 |     | floatarith_of_term (@{term Num} $ m) = @{code Num} (float_of_term m)
 | 
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3320 | | floatarith_of_term t = bad t; | 
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3321 | |
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3322 |   fun form_of_term (@{term Bound} $ a $ b $ c $ p) = @{code Bound}
 | 
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3323 | (floatarith_of_term a, floatarith_of_term b, floatarith_of_term c, form_of_term p) | 
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3324 |     | form_of_term (@{term Assign} $ a $ b $ p) = @{code Assign}
 | 
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3325 | (floatarith_of_term a, floatarith_of_term b, form_of_term p) | 
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3326 |     | form_of_term (@{term Less} $ a $ b) = @{code Less}
 | 
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3327 | (floatarith_of_term a, floatarith_of_term b) | 
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3328 |     | form_of_term (@{term LessEqual} $ a $ b) = @{code LessEqual}
 | 
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3329 | (floatarith_of_term a, floatarith_of_term b) | 
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3330 |     | form_of_term (@{term AtLeastAtMost} $ a $ b $ c) = @{code AtLeastAtMost}
 | 
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3331 | (floatarith_of_term a, floatarith_of_term b, floatarith_of_term c) | 
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3332 | | form_of_term t = bad t; | 
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3333 | |
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3334 |   fun float_float_option_of_term @{term "None :: (float \<times> float) option"} = NONE
 | 
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3335 |     | float_float_option_of_term (@{term "Some :: float \<times> float \<Rightarrow> _"} $ ff) =
 | 
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3336 | SOME (pairself float_of_term (HOLogic.dest_prod ff)) | 
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3337 |     | float_float_option_of_term (@{term approx'} $ n $ a $ ffs) = @{code approx'}
 | 
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3338 | (nat_of_term n) (floatarith_of_term a) (float_float_option_list_of_term ffs) | 
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3339 | | float_float_option_of_term t = bad t | 
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3340 | and float_float_option_list_of_term | 
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3341 |         (@{term "replicate :: _ \<Rightarrow> (float \<times> float) option \<Rightarrow> _"} $ n $ @{term "None :: (float \<times> float) option"}) =
 | 
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3342 |           @{code replicate} (nat_of_term n) NONE
 | 
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3343 |     | float_float_option_list_of_term (@{term approx_form_eval} $ n $ p $ ffs) =
 | 
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3344 |         @{code approx_form_eval} (nat_of_term n) (form_of_term p) (float_float_option_list_of_term ffs)
 | 
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3345 | | float_float_option_list_of_term t = map float_float_option_of_term | 
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3346 | (HOLogic.dest_list t); | 
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3347 | |
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3348 | val nat_list_of_term = map nat_of_term o HOLogic.dest_list ; | 
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3349 | |
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3350 |   fun bool_of_term (@{term approx_form} $ n $ p $ ffs $ ms) = @{code approx_form}
 | 
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3351 | (nat_of_term n) (form_of_term p) (float_float_option_list_of_term ffs) (nat_list_of_term ms) | 
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3352 |     | bool_of_term (@{term approx_tse_form} $ m $ n $ q $ p) =
 | 
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3353 |         @{code approx_tse_form} (nat_of_term m) (nat_of_term n) (nat_of_term q) (form_of_term p)
 | 
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3354 | | bool_of_term t = bad t; | 
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3355 | |
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3356 | fun eval t = case fastype_of t | 
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3357 |    of @{typ bool} =>
 | 
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3358 | (term_of_bool o bool_of_term) t | 
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3359 |     | @{typ "(float \<times> float) option"} =>
 | 
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3360 | (term_of_float_float_option o float_float_option_of_term) t | 
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3361 |     | @{typ "(float \<times> float) option list"} =>
 | 
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3362 | (term_of_float_float_option_list o float_float_option_list_of_term) t | 
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3363 | | _ => bad t; | 
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3364 | |
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3365 | val normalize = eval o Envir.beta_norm o Pattern.eta_long []; | 
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3366 | |
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3367 | in Thm.cterm_of thy (Logic.mk_equals (t, normalize t)) end | 
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3368 | *} | 
| 31099 
03314c427b34
optimized Approximation by precompiling approx_inequality
 hoelzl parents: 
31098diff
changeset | 3369 | |
| 
03314c427b34
optimized Approximation by precompiling approx_inequality
 hoelzl parents: 
31098diff
changeset | 3370 | ML {*
 | 
| 32212 | 3371 | fun reorder_bounds_tac prems i = | 
| 29805 | 3372 | let | 
| 38558 | 3373 |       fun variable_of_bound (Const (@{const_name Trueprop}, _) $
 | 
| 37677 | 3374 |                              (Const (@{const_name Set.member}, _) $
 | 
| 31811 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 3375 | Free (name, _) $ _)) = name | 
| 38558 | 3376 |         | variable_of_bound (Const (@{const_name Trueprop}, _) $
 | 
| 38864 
4abe644fcea5
formerly unnamed infix equality now named HOL.eq
 haftmann parents: 
38786diff
changeset | 3377 |                              (Const (@{const_name HOL.eq}, _) $
 | 
| 31811 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 3378 | Free (name, _) $ _)) = name | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 3379 |         | variable_of_bound t = raise TERM ("variable_of_bound", [t])
 | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 3380 | |
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 3381 | val variable_bounds | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 3382 | = map (` (variable_of_bound o prop_of)) prems | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 3383 | |
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 3384 | fun add_deps (name, bnds) | 
| 32650 | 3385 | = Graph.add_deps_acyclic (name, | 
| 3386 | remove (op =) name (Term.add_free_names (prop_of bnds) [])) | |
| 3387 | ||
| 31811 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 3388 | val order = Graph.empty | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 3389 | |> fold Graph.new_node variable_bounds | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 3390 | |> fold add_deps variable_bounds | 
| 32650 | 3391 | |> Graph.strong_conn |> map the_single |> rev | 
| 31811 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 3392 | |> map_filter (AList.lookup (op =) variable_bounds) | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 3393 | |
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 3394 | fun prepend_prem th tac | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 3395 |         = tac THEN rtac (th RSN (2, @{thm mp})) i
 | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 3396 | in | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 3397 | fold prepend_prem order all_tac | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 3398 | end | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 3399 | |
| 36985 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3400 | fun approximation_conv ctxt ct = | 
| 42361 | 3401 | approximation_oracle (Proof_Context.theory_of ctxt, Thm.term_of ct |> tap (tracing o Syntax.string_of_term ctxt)); | 
| 36985 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3402 | |
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3403 | fun approximate ctxt t = | 
| 42361 | 3404 | approximation_oracle (Proof_Context.theory_of ctxt, t) | 
| 36985 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3405 | |> Thm.prop_of |> Logic.dest_equals |> snd; | 
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3406 | |
| 31811 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 3407 | (* Should be in HOL.thy ? *) | 
| 36985 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3408 | fun gen_eval_tac conv ctxt = CONVERSION | 
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3409 | (Object_Logic.judgment_conv (Conv.params_conv (~1) (K (Conv.concl_conv (~1) conv)) ctxt)) | 
| 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3410 | THEN' rtac TrueI | 
| 29805 | 3411 | |
| 39556 | 3412 |   val form_equations = @{thms interpret_form_equations};
 | 
| 31811 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 3413 | |
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3414 | fun rewrite_interpret_form_tac ctxt prec splitting taylor i st = let | 
| 46545 | 3415 | fun lookup_splitting (Free (name, _)) | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3416 | = case AList.lookup (op =) splitting name | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3417 |           of SOME s => HOLogic.mk_number @{typ nat} s
 | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3418 |            | NONE => @{term "0 :: nat"}
 | 
| 31811 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 3419 | val vs = nth (prems_of st) (i - 1) | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 3420 | |> Logic.strip_imp_concl | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 3421 | |> HOLogic.dest_Trueprop | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 3422 | |> Term.strip_comb |> snd |> List.last | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 3423 | |> HOLogic.dest_list | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 3424 | val p = prec | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 3425 |               |> HOLogic.mk_number @{typ nat}
 | 
| 42361 | 3426 | |> Thm.cterm_of (Proof_Context.theory_of ctxt) | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3427 | in case taylor | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3428 | of NONE => let | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3429 | val n = vs |> length | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3430 |                  |> HOLogic.mk_number @{typ nat}
 | 
| 42361 | 3431 | |> Thm.cterm_of (Proof_Context.theory_of ctxt) | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3432 | val s = vs | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3433 | |> map lookup_splitting | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3434 |                  |> HOLogic.mk_list @{typ nat}
 | 
| 42361 | 3435 | |> Thm.cterm_of (Proof_Context.theory_of ctxt) | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3436 | in | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3437 |          (rtac (Thm.instantiate ([], [(@{cpat "?n::nat"}, n),
 | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3438 |                                      (@{cpat "?prec::nat"}, p),
 | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3439 |                                      (@{cpat "?ss::nat list"}, s)])
 | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3440 |               @{thm "approx_form"}) i
 | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3441 |           THEN simp_tac @{simpset} i) st
 | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3442 | end | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3443 | |
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3444 |      | SOME t => if length vs <> 1 then raise (TERM ("More than one variable used for taylor series expansion", [prop_of st]))
 | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3445 | else let | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3446 | val t = t | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3447 |               |> HOLogic.mk_number @{typ nat}
 | 
| 42361 | 3448 | |> Thm.cterm_of (Proof_Context.theory_of ctxt) | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3449 | val s = vs |> map lookup_splitting |> hd | 
| 42361 | 3450 | |> Thm.cterm_of (Proof_Context.theory_of ctxt) | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3451 | in | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3452 |          rtac (Thm.instantiate ([], [(@{cpat "?s::nat"}, s),
 | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3453 |                                      (@{cpat "?t::nat"}, t),
 | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3454 |                                      (@{cpat "?prec::nat"}, p)])
 | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3455 |               @{thm "approx_tse_form"}) i st
 | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3456 | end | 
| 31811 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 3457 | end | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 3458 | |
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 3459 | (* copied from Tools/induct.ML should probably in args.ML *) | 
| 46545 | 3460 | val free = Args.context -- Args.term >> (fn (_, Free (n, _)) => n | (ctxt, t) => | 
| 31811 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 3461 |     error ("Bad free variable: " ^ Syntax.string_of_term ctxt t));
 | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 3462 | |
| 29805 | 3463 | *} | 
| 3464 | ||
| 31811 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 3465 | lemma intervalE: "a \<le> x \<and> x \<le> b \<Longrightarrow> \<lbrakk> x \<in> { a .. b } \<Longrightarrow> P\<rbrakk> \<Longrightarrow> P"
 | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 3466 | by auto | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 3467 | |
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 3468 | lemma meta_eqE: "x \<equiv> a \<Longrightarrow> \<lbrakk> x = a \<Longrightarrow> P\<rbrakk> \<Longrightarrow> P" | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 3469 | by auto | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 3470 | |
| 30549 | 3471 | method_setup approximation = {*
 | 
| 36960 
01594f816e3a
prefer structure Keyword, Parse, Parse_Spec, Outer_Syntax;
 wenzelm parents: 
36778diff
changeset | 3472 | Scan.lift Parse.nat | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3473 | -- | 
| 31811 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 3474 | Scan.optional (Scan.lift (Args.$$$ "splitting" |-- Args.colon) | 
| 36960 
01594f816e3a
prefer structure Keyword, Parse, Parse_Spec, Outer_Syntax;
 wenzelm parents: 
36778diff
changeset | 3475 | |-- Parse.and_list' (free --| Scan.lift (Args.$$$ "=") -- Scan.lift Parse.nat)) [] | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3476 | -- | 
| 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3477 | Scan.option (Scan.lift (Args.$$$ "taylor" |-- Args.colon) | 
| 36960 
01594f816e3a
prefer structure Keyword, Parse, Parse_Spec, Outer_Syntax;
 wenzelm parents: 
36778diff
changeset | 3478 | |-- (free |-- Scan.lift (Args.$$$ "=") |-- Scan.lift Parse.nat)) | 
| 31811 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 3479 | >> | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3480 | (fn ((prec, splitting), taylor) => fn ctxt => | 
| 30549 | 3481 | SIMPLE_METHOD' (fn i => | 
| 31811 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 3482 |       REPEAT (FIRST' [etac @{thm intervalE},
 | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 3483 |                       etac @{thm meta_eqE},
 | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 3484 |                       rtac @{thm impI}] i)
 | 
| 32283 | 3485 |       THEN Subgoal.FOCUS (fn {prems, ...} => reorder_bounds_tac prems i) @{context} i
 | 
| 32650 | 3486 | THEN DETERM (TRY (filter_prems_tac (K false) i)) | 
| 31811 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 3487 | THEN DETERM (Reflection.genreify_tac ctxt form_equations NONE i) | 
| 31863 
e391eee8bf14
Implemented taylor series expansion for approximation
 hoelzl parents: 
31811diff
changeset | 3488 | THEN rewrite_interpret_form_tac ctxt prec splitting taylor i | 
| 36985 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3489 | THEN gen_eval_tac (approximation_conv ctxt) ctxt i)) | 
| 31811 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 3490 | *} "real number approximation" | 
| 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 3491 | |
| 31810 | 3492 | ML {*
 | 
| 32919 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 3493 |   fun calculated_subterms (@{const Trueprop} $ t) = calculated_subterms t
 | 
| 38786 
e46e7a9cb622
formerly unnamed infix impliciation now named HOL.implies
 haftmann parents: 
38716diff
changeset | 3494 |     | calculated_subterms (@{const HOL.implies} $ _ $ t) = calculated_subterms t
 | 
| 32919 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 3495 |     | calculated_subterms (@{term "op <= :: real \<Rightarrow> real \<Rightarrow> bool"} $ t1 $ t2) = [t1, t2]
 | 
| 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 3496 |     | calculated_subterms (@{term "op < :: real \<Rightarrow> real \<Rightarrow> bool"} $ t1 $ t2) = [t1, t2]
 | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 3497 |     | calculated_subterms (@{term "op : :: real \<Rightarrow> real set \<Rightarrow> bool"} $ t1 $
 | 
| 32919 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 3498 |                            (@{term "atLeastAtMost :: real \<Rightarrow> real \<Rightarrow> real set"} $ t2 $ t3)) = [t1, t2, t3]
 | 
| 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 3499 |     | calculated_subterms t = raise TERM ("calculated_subterms", [t])
 | 
| 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 3500 | |
| 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 3501 |   fun dest_interpret_form (@{const "interpret_form"} $ b $ xs) = (b, xs)
 | 
| 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 3502 |     | dest_interpret_form t = raise TERM ("dest_interpret_form", [t])
 | 
| 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 3503 | |
| 31810 | 3504 |   fun dest_interpret (@{const "interpret_floatarith"} $ b $ xs) = (b, xs)
 | 
| 32919 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 3505 |     | dest_interpret t = raise TERM ("dest_interpret", [t])
 | 
| 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 3506 | |
| 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 3507 | |
| 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 3508 |   fun dest_float (@{const "Float"} $ m $ e) = (snd (HOLogic.dest_number m), snd (HOLogic.dest_number e))
 | 
| 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 3509 |   fun dest_ivl (Const (@{const_name "Some"}, _) $
 | 
| 37391 | 3510 |                 (Const (@{const_name Pair}, _) $ u $ l)) = SOME (dest_float u, dest_float l)
 | 
| 32919 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 3511 |     | dest_ivl (Const (@{const_name "None"}, _)) = NONE
 | 
| 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 3512 |     | dest_ivl t = raise TERM ("dest_result", [t])
 | 
| 31810 | 3513 | |
| 3514 |   fun mk_approx' prec t = (@{const "approx'"}
 | |
| 3515 |                          $ HOLogic.mk_number @{typ nat} prec
 | |
| 32650 | 3516 |                          $ t $ @{term "[] :: (float * float) option list"})
 | 
| 31810 | 3517 | |
| 32919 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 3518 |   fun mk_approx_form_eval prec t xs = (@{const "approx_form_eval"}
 | 
| 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 3519 |                          $ HOLogic.mk_number @{typ nat} prec
 | 
| 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 3520 | $ t $ xs) | 
| 31810 | 3521 | |
| 3522 | fun float2_float10 prec round_down (m, e) = ( | |
| 3523 | let | |
| 3524 | val (m, e) = (if e < 0 then (m,e) else (m * Integer.pow e 2, 0)) | |
| 3525 | ||
| 3526 | fun frac c p 0 digits cnt = (digits, cnt, 0) | |
| 3527 | | frac c 0 r digits cnt = (digits, cnt, r) | |
| 3528 | | frac c p r digits cnt = (let | |
| 3529 | val (d, r) = Integer.div_mod (r * 10) (Integer.pow (~e) 2) | |
| 3530 | in frac (c orelse d <> 0) (if d <> 0 orelse c then p - 1 else p) r | |
| 3531 | (digits * 10 + d) (cnt + 1) | |
| 3532 | end) | |
| 3533 | ||
| 3534 | val sgn = Int.sign m | |
| 3535 | val m = abs m | |
| 3536 | ||
| 3537 | val round_down = (sgn = 1 andalso round_down) orelse | |
| 3538 | (sgn = ~1 andalso not round_down) | |
| 3539 | ||
| 3540 | val (x, r) = Integer.div_mod m (Integer.pow (~e) 2) | |
| 3541 | ||
| 3542 | val p = ((if x = 0 then prec else prec - (IntInf.log2 x + 1)) * 3) div 10 + 1 | |
| 3543 | ||
| 3544 | val (digits, e10, r) = if p > 0 then frac (x <> 0) p r 0 0 else (0,0,0) | |
| 3545 | ||
| 3546 | val digits = if round_down orelse r = 0 then digits else digits + 1 | |
| 3547 | ||
| 3548 | in (sgn * (digits + x * (Integer.pow e10 10)), ~e10) | |
| 3549 | end) | |
| 3550 | ||
| 3551 | fun mk_result prec (SOME (l, u)) = (let | |
| 3552 | fun mk_float10 rnd x = (let val (m, e) = float2_float10 prec rnd x | |
| 3553 |                          in if e = 0 then HOLogic.mk_number @{typ real} m
 | |
| 3554 |                        else if e = 1 then @{term "divide :: real \<Rightarrow> real \<Rightarrow> real"} $
 | |
| 3555 |                                           HOLogic.mk_number @{typ real} m $
 | |
| 3556 |                                           @{term "10"}
 | |
| 3557 |                                      else @{term "divide :: real \<Rightarrow> real \<Rightarrow> real"} $
 | |
| 3558 |                                           HOLogic.mk_number @{typ real} m $
 | |
| 3559 |                                           (@{term "power 10 :: nat \<Rightarrow> real"} $
 | |
| 3560 |                                            HOLogic.mk_number @{typ nat} (~e)) end)
 | |
| 32919 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 3561 |       in @{term "atLeastAtMost :: real \<Rightarrow> real \<Rightarrow> real set"} $ mk_float10 true l $ mk_float10 false u end)
 | 
| 46545 | 3562 |     | mk_result _ NONE = @{term "UNIV :: real set"}
 | 
| 31810 | 3563 | |
| 3564 | fun realify t = let | |
| 35845 
e5980f0ad025
renamed varify/unvarify operations to varify_global/unvarify_global to emphasize that these only work in a global situation;
 wenzelm parents: 
35346diff
changeset | 3565 | val t = Logic.varify_global t | 
| 46545 | 3566 |       val m = map (fn (name, _) => (name, @{typ real})) (Term.add_tvars t [])
 | 
| 31810 | 3567 | val t = Term.subst_TVars m t | 
| 3568 | in t end | |
| 3569 | ||
| 32919 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 3570 | fun converted_result t = | 
| 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 3571 | prop_of t | 
| 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 3572 | |> HOLogic.dest_Trueprop | 
| 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 3573 | |> HOLogic.dest_eq |> snd | 
| 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 3574 | |
| 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 3575 | fun apply_tactic context term tactic = cterm_of context term | 
| 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 3576 | |> Goal.init | 
| 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 3577 | |> SINGLE tactic | 
| 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 3578 | |> the |> prems_of |> hd | 
| 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 3579 | |
| 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 3580 | fun prepare_form context term = apply_tactic context term ( | 
| 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 3581 |       REPEAT (FIRST' [etac @{thm intervalE}, etac @{thm meta_eqE}, rtac @{thm impI}] 1)
 | 
| 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 3582 |       THEN Subgoal.FOCUS (fn {prems, ...} => reorder_bounds_tac prems 1) @{context} 1
 | 
| 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 3583 | THEN DETERM (TRY (filter_prems_tac (K false) 1))) | 
| 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 3584 | |
| 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 3585 | fun reify_form context term = apply_tactic context term | 
| 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 3586 |      (Reflection.genreify_tac @{context} form_equations NONE 1)
 | 
| 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 3587 | |
| 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 3588 | fun approx_form prec ctxt t = | 
| 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 3589 | realify t | 
| 42361 | 3590 | |> prepare_form (Proof_Context.theory_of ctxt) | 
| 32919 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 3591 | |> (fn arith_term => | 
| 42361 | 3592 | reify_form (Proof_Context.theory_of ctxt) arith_term | 
| 32919 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 3593 | |> HOLogic.dest_Trueprop |> dest_interpret_form | 
| 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 3594 | |> (fn (data, xs) => | 
| 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 3595 |           mk_approx_form_eval prec data (HOLogic.mk_list @{typ "(float * float) option"}
 | 
| 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 3596 |             (map (fn _ => @{term "None :: (float * float) option"}) (HOLogic.dest_list xs)))
 | 
| 36985 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3597 | |> approximate ctxt | 
| 32919 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 3598 | |> HOLogic.dest_list | 
| 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 3599 | |> curry ListPair.zip (HOLogic.dest_list xs @ calculated_subterms arith_term) | 
| 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 3600 |        |> map (fn (elem, s) => @{term "op : :: real \<Rightarrow> real set \<Rightarrow> bool"} $ elem $ mk_result prec (dest_ivl s))
 | 
| 32920 
ccfb774af58c
order conjunctions to be printed without parentheses
 hoelzl parents: 
32919diff
changeset | 3601 | |> foldr1 HOLogic.mk_conj)) | 
| 32919 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 3602 | |
| 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 3603 | fun approx_arith prec ctxt t = realify t | 
| 31811 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 hoelzl parents: 
31810diff
changeset | 3604 | |> Reflection.genreif ctxt form_equations | 
| 31810 | 3605 | |> prop_of | 
| 3606 | |> HOLogic.dest_Trueprop | |
| 3607 | |> HOLogic.dest_eq |> snd | |
| 3608 | |> dest_interpret |> fst | |
| 3609 | |> mk_approx' prec | |
| 36985 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 haftmann parents: 
36960diff
changeset | 3610 | |> approximate ctxt | 
| 32919 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 3611 | |> dest_ivl | 
| 31810 | 3612 | |> mk_result prec | 
| 32919 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 3613 | |
| 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 3614 |    fun approx prec ctxt t = if type_of t = @{typ prop} then approx_form prec ctxt t
 | 
| 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 3615 |      else if type_of t = @{typ bool} then approx_form prec ctxt (@{const Trueprop} $ t)
 | 
| 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 hoelzl parents: 
32650diff
changeset | 3616 | else approx_arith prec ctxt t | 
| 31810 | 3617 | *} | 
| 3618 | ||
| 3619 | setup {*
 | |
| 3620 |   Value.add_evaluator ("approximate", approx 30)
 | |
| 3621 | *} | |
| 3622 | ||
| 29805 | 3623 | end | 
| 40881 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 hoelzl parents: 
39556diff
changeset | 3624 |