| author | wenzelm | 
| Tue, 10 Feb 2015 14:48:26 +0100 | |
| changeset 59498 | 50b60f501b05 | 
| parent 59058 | a78612c67ec0 | 
| child 59580 | cbc38731d42f | 
| permissions | -rw-r--r-- | 
| 58834 | 1  | 
(* Author: Johannes Hoelzl, TU Muenchen  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
2  | 
Coercions removed by Dmitriy Traytel *)  | 
| 30122 | 3  | 
|
| 58889 | 4  | 
section {* Prove Real Valued Inequalities by Computation *}
 | 
| 30122 | 5  | 
|
| 40892 | 6  | 
theory Approximation  | 
| 
41413
 
64cd30d6b0b8
explicit file specifications -- avoid secondary load path;
 
wenzelm 
parents: 
41024 
diff
changeset
 | 
7  | 
imports  | 
| 
 
64cd30d6b0b8
explicit file specifications -- avoid secondary load path;
 
wenzelm 
parents: 
41024 
diff
changeset
 | 
8  | 
Complex_Main  | 
| 
 
64cd30d6b0b8
explicit file specifications -- avoid secondary load path;
 
wenzelm 
parents: 
41024 
diff
changeset
 | 
9  | 
"~~/src/HOL/Library/Float"  | 
| 51544 | 10  | 
Dense_Linear_Order  | 
| 
51143
 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 
haftmann 
parents: 
49962 
diff
changeset
 | 
11  | 
"~~/src/HOL/Library/Code_Target_Numeral"  | 
| 56923 | 12  | 
keywords "approximate" :: diag  | 
| 29805 | 13  | 
begin  | 
14  | 
||
| 
54489
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54269 
diff
changeset
 | 
15  | 
declare powr_numeral [simp]  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54269 
diff
changeset
 | 
16  | 
declare powr_neg_one [simp]  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54269 
diff
changeset
 | 
17  | 
declare powr_neg_numeral [simp]  | 
| 47600 | 18  | 
|
| 29805 | 19  | 
section "Horner Scheme"  | 
20  | 
||
21  | 
subsection {* Define auxiliary helper @{text horner} function *}
 | 
|
22  | 
||
| 
31098
 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 
hoelzl 
parents: 
30971 
diff
changeset
 | 
23  | 
primrec horner :: "(nat \<Rightarrow> nat) \<Rightarrow> (nat \<Rightarrow> nat \<Rightarrow> nat) \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> real \<Rightarrow> real" where  | 
| 29805 | 24  | 
"horner F G 0 i k x = 0" |  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
25  | 
"horner F G (Suc n) i k x = 1 / k - x * horner F G n (F i) (G i k) x"  | 
| 29805 | 26  | 
|
| 49351 | 27  | 
lemma horner_schema':  | 
28  | 
fixes x :: real and a :: "nat \<Rightarrow> real"  | 
|
| 29805 | 29  | 
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)"  | 
30  | 
proof -  | 
|
| 49351 | 31  | 
have shift_pow: "\<And>i. - (x * ((-1)^i * a (Suc i) * x ^ i)) = (-1)^(Suc i) * a (Suc i) * x ^ (Suc i)"  | 
32  | 
by auto  | 
|
33  | 
show ?thesis  | 
|
| 
54230
 
b1d955791529
more simplification rules on unary and binary minus
 
haftmann 
parents: 
53077 
diff
changeset
 | 
34  | 
unfolding setsum_right_distrib shift_pow uminus_add_conv_diff [symmetric] setsum_negf[symmetric]  | 
| 49351 | 35  | 
setsum_head_upt_Suc[OF zero_less_Suc]  | 
| 57418 | 36  | 
setsum.reindex[OF inj_Suc, unfolded comp_def, symmetric, of "\<lambda> n. (-1)^n *a n * x^n"] by auto  | 
| 29805 | 37  | 
qed  | 
38  | 
||
| 49351 | 39  | 
lemma horner_schema:  | 
40  | 
fixes f :: "nat \<Rightarrow> nat" and G :: "nat \<Rightarrow> nat \<Rightarrow> nat" and F :: "nat \<Rightarrow> nat"  | 
|
| 30971 | 41  | 
assumes f_Suc: "\<And>n. f (Suc n) = G ((F ^^ n) s) (f n)"  | 
| 
58410
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
42  | 
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: 
44821 
diff
changeset
 | 
43  | 
proof (induct n arbitrary: j')  | 
| 
 
1fce03e3e8ad
tuned proofs -- eliminated vacuous "induct arbitrary: ..." situations;
 
wenzelm 
parents: 
44821 
diff
changeset
 | 
44  | 
case 0  | 
| 
 
1fce03e3e8ad
tuned proofs -- eliminated vacuous "induct arbitrary: ..." situations;
 
wenzelm 
parents: 
44821 
diff
changeset
 | 
45  | 
then show ?case by auto  | 
| 
 
1fce03e3e8ad
tuned proofs -- eliminated vacuous "induct arbitrary: ..." situations;
 
wenzelm 
parents: 
44821 
diff
changeset
 | 
46  | 
next  | 
| 29805 | 47  | 
case (Suc n)  | 
48  | 
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: 
39556 
diff
changeset
 | 
49  | 
using horner_schema'[of "\<lambda> j. 1 / (f (j' + j))"] by auto  | 
| 
45129
 
1fce03e3e8ad
tuned proofs -- eliminated vacuous "induct arbitrary: ..." situations;
 
wenzelm 
parents: 
44821 
diff
changeset
 | 
50  | 
qed  | 
| 29805 | 51  | 
|
52  | 
lemma horner_bounds':  | 
|
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
53  | 
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: 
30971 
diff
changeset
 | 
54  | 
assumes "0 \<le> real x" and f_Suc: "\<And>n. f (Suc n) = G ((F ^^ n) s) (f n)"  | 
| 49351 | 55  | 
and lb_0: "\<And> i k x. lb 0 i k x = 0"  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
56  | 
and lb_Suc: "\<And> n i k x. lb (Suc n) i k x = float_plus_down prec  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
57  | 
(lapprox_rat prec 1 k)  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
58  | 
(- float_round_up prec (x * (ub n (F i) (G i k) x)))"  | 
| 49351 | 59  | 
and ub_0: "\<And> i k x. ub 0 i k x = 0"  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
60  | 
and ub_Suc: "\<And> n i k x. ub (Suc n) i k x = float_plus_up prec  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
61  | 
(rapprox_rat prec 1 k)  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
62  | 
(- float_round_down prec (x * (lb n (F i) (G i k) x)))"  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
63  | 
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: 
39556 
diff
changeset
 | 
64  | 
horner F G n ((F ^^ j') s) (f j') x \<le> (ub n ((F ^^ j') s) (f j') x)"  | 
| 29805 | 65  | 
(is "?lb n j' \<le> ?horner n j' \<and> ?horner n j' \<le> ?ub n j'")  | 
66  | 
proof (induct n arbitrary: j')  | 
|
| 49351 | 67  | 
case 0  | 
68  | 
thus ?case unfolding lb_0 ub_0 horner.simps by auto  | 
|
| 29805 | 69  | 
next  | 
70  | 
case (Suc n)  | 
|
| 
47599
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
71  | 
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: 
47108 
diff
changeset
 | 
72  | 
Suc[where j'="Suc j'"] `0 \<le> real x`  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
73  | 
by (auto intro!: add_mono mult_left_mono float_round_down_le float_round_up_le  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
74  | 
order_trans[OF add_mono[OF _ float_plus_down_le]]  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
75  | 
order_trans[OF _ add_mono[OF _ float_plus_up_le]]  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
76  | 
simp add: lb_Suc ub_Suc field_simps f_Suc)  | 
| 29805 | 77  | 
qed  | 
78  | 
||
79  | 
subsection "Theorems for floating point functions implementing the horner scheme"  | 
|
80  | 
||
81  | 
text {*
 | 
|
82  | 
||
83  | 
Here @{term_type "f :: nat \<Rightarrow> nat"} is the sequence defining the Taylor series, the coefficients are
 | 
|
84  | 
all alternating and reciprocs. We use @{term G} and @{term F} to describe the computation of @{term f}.
 | 
|
85  | 
||
86  | 
*}  | 
|
87  | 
||
| 49351 | 88  | 
lemma horner_bounds:  | 
89  | 
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: 
30971 
diff
changeset
 | 
90  | 
assumes "0 \<le> real x" and f_Suc: "\<And>n. f (Suc n) = G ((F ^^ n) s) (f n)"  | 
| 49351 | 91  | 
and lb_0: "\<And> i k x. lb 0 i k x = 0"  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
92  | 
and lb_Suc: "\<And> n i k x. lb (Suc n) i k x = float_plus_down prec  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
93  | 
(lapprox_rat prec 1 k)  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
94  | 
(- float_round_up prec (x * (ub n (F i) (G i k) x)))"  | 
| 49351 | 95  | 
and ub_0: "\<And> i k x. ub 0 i k x = 0"  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
96  | 
and ub_Suc: "\<And> n i k x. ub (Suc n) i k x = float_plus_up prec  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
97  | 
(rapprox_rat prec 1 k)  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
98  | 
(- float_round_down prec (x * (lb n (F i) (G i k) x)))"  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
99  | 
shows "(lb n ((F ^^ j') s) (f j') x) \<le> (\<Sum>j=0..<n. (- 1) ^ j * (1 / (f (j' + j))) * (x ^ j))"  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
100  | 
(is "?lb")  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
101  | 
and "(\<Sum>j=0..<n. (- 1) ^ j * (1 / (f (j' + j))) * (x ^ j)) \<le> (ub n ((F ^^ j') s) (f j') x)"  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
102  | 
(is "?ub")  | 
| 29805 | 103  | 
proof -  | 
| 31809 | 104  | 
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: 
30971 
diff
changeset
 | 
105  | 
using horner_bounds'[where lb=lb, OF `0 \<le> real x` f_Suc lb_0 lb_Suc ub_0 ub_Suc]  | 
| 29805 | 106  | 
unfolding horner_schema[where f=f, OF f_Suc] .  | 
107  | 
thus "?lb" and "?ub" by auto  | 
|
108  | 
qed  | 
|
109  | 
||
| 49351 | 110  | 
lemma horner_bounds_nonpos:  | 
111  | 
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: 
30971 
diff
changeset
 | 
112  | 
assumes "real x \<le> 0" and f_Suc: "\<And>n. f (Suc n) = G ((F ^^ n) s) (f n)"  | 
| 49351 | 113  | 
and lb_0: "\<And> i k x. lb 0 i k x = 0"  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
114  | 
and lb_Suc: "\<And> n i k x. lb (Suc n) i k x = float_plus_down prec  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
115  | 
(lapprox_rat prec 1 k)  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
116  | 
(float_round_down prec (x * (ub n (F i) (G i k) x)))"  | 
| 49351 | 117  | 
and ub_0: "\<And> i k x. ub 0 i k x = 0"  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
118  | 
and ub_Suc: "\<And> n i k x. ub (Suc n) i k x = float_plus_up prec  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
119  | 
(rapprox_rat prec 1 k)  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
120  | 
(float_round_up prec (x * (lb n (F i) (G i k) x)))"  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
121  | 
shows "(lb n ((F ^^ j') s) (f j') x) \<le> (\<Sum>j=0..<n. (1 / (f (j' + j))) * real x ^ j)" (is "?lb")  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
122  | 
and "(\<Sum>j=0..<n. (1 / (f (j' + j))) * real x ^ j) \<le> (ub n ((F ^^ j') s) (f j') x)" (is "?ub")  | 
| 29805 | 123  | 
proof -  | 
| 
47599
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
124  | 
  { 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: 
39556 
diff
changeset
 | 
125  | 
have sum_eq: "(\<Sum>j=0..<n. (1 / (f (j' + j))) * real x ^ j) =  | 
| 
58410
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
126  | 
(\<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: 
47108 
diff
changeset
 | 
127  | 
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: 
30971 
diff
changeset
 | 
128  | 
have "0 \<le> real (-x)" using assms by auto  | 
| 29805 | 129  | 
from horner_bounds[where G=G and F=F and f=f and s=s and prec=prec  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
130  | 
and lb="\<lambda> n i k x. lb n i k (-x)" and ub="\<lambda> n i k x. ub n i k (-x)",  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
131  | 
unfolded lb_Suc ub_Suc diff_mult_minus,  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
132  | 
OF this f_Suc lb_0 _ ub_0 _]  | 
| 29805 | 133  | 
show "?lb" and "?ub" unfolding minus_minus sum_eq  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
134  | 
by (auto simp: minus_float_round_up_eq minus_float_round_down_eq)  | 
| 29805 | 135  | 
qed  | 
136  | 
||
137  | 
subsection {* Selectors for next even or odd number *}
 | 
|
138  | 
||
139  | 
text {*
 | 
|
140  | 
||
141  | 
The horner scheme computes alternating series. To get the upper and lower bounds we need to  | 
|
142  | 
guarantee to access a even or odd member. To do this we use @{term get_odd} and @{term get_even}.
 | 
|
143  | 
||
144  | 
*}  | 
|
145  | 
||
146  | 
definition get_odd :: "nat \<Rightarrow> nat" where  | 
|
147  | 
"get_odd n = (if odd n then n else (Suc n))"  | 
|
148  | 
||
149  | 
definition get_even :: "nat \<Rightarrow> nat" where  | 
|
150  | 
"get_even n = (if even n then n else (Suc n))"  | 
|
151  | 
||
152  | 
lemma get_odd[simp]: "odd (get_odd n)" unfolding get_odd_def by (cases "odd n", auto)  | 
|
153  | 
lemma get_even[simp]: "even (get_even n)" unfolding get_even_def by (cases "even n", auto)  | 
|
154  | 
lemma get_odd_ex: "\<exists> k. Suc k = get_odd n \<and> odd (Suc k)"  | 
|
| 54269 | 155  | 
by (auto simp: get_odd_def odd_pos intro!: exI[of _ "n - 1"])  | 
| 29805 | 156  | 
|
| 
58709
 
efdc6c533bd3
prefer generic elimination rules for even/odd over specialized unfold rules for nat
 
haftmann 
parents: 
58410 
diff
changeset
 | 
157  | 
lemma get_even_double:  | 
| 
 
efdc6c533bd3
prefer generic elimination rules for even/odd over specialized unfold rules for nat
 
haftmann 
parents: 
58410 
diff
changeset
 | 
158  | 
"\<exists>i. get_even n = 2 * i" using get_even by (blast elim: evenE)  | 
| 
 
efdc6c533bd3
prefer generic elimination rules for even/odd over specialized unfold rules for nat
 
haftmann 
parents: 
58410 
diff
changeset
 | 
159  | 
|
| 
 
efdc6c533bd3
prefer generic elimination rules for even/odd over specialized unfold rules for nat
 
haftmann 
parents: 
58410 
diff
changeset
 | 
160  | 
lemma get_odd_double:  | 
| 
 
efdc6c533bd3
prefer generic elimination rules for even/odd over specialized unfold rules for nat
 
haftmann 
parents: 
58410 
diff
changeset
 | 
161  | 
"\<exists>i. get_odd n = 2 * i + 1" using get_odd by (blast elim: oddE)  | 
| 29805 | 162  | 
|
163  | 
section "Power function"  | 
|
164  | 
||
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
165  | 
definition float_power_bnds :: "nat \<Rightarrow> nat \<Rightarrow> float \<Rightarrow> float \<Rightarrow> float * float" where  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
166  | 
"float_power_bnds prec n l u =  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
167  | 
(if 0 < l then (power_down_fl prec l n, power_up_fl prec u n)  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
168  | 
else if odd n then  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
169  | 
(- power_up_fl prec (abs l) n,  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
170  | 
if u < 0 then - power_down_fl prec (abs u) n else power_up_fl prec u n)  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
171  | 
else if u < 0 then (power_down_fl prec (abs u) n, power_up_fl prec (abs l) n)  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
172  | 
else (0, power_up_fl prec (max (abs l) (abs u)) n))"  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
173  | 
|
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
174  | 
lemma le_minus_power_downI: "0 \<le> x \<Longrightarrow> x ^ n \<le> - a \<Longrightarrow> a \<le> - power_down prec x n"  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
175  | 
by (subst le_minus_iff) (auto intro: power_down_le power_mono_odd)  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
176  | 
|
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
177  | 
lemma float_power_bnds:  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
178  | 
  "(l1, u1) = float_power_bnds prec n l u \<Longrightarrow> x \<in> {l .. u} \<Longrightarrow> (x::real) ^ n \<in> {l1..u1}"
 | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
179  | 
by (auto  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
180  | 
simp: float_power_bnds_def max_def real_power_up_fl real_power_down_fl minus_le_iff  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
181  | 
split: split_if_asm  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
182  | 
intro!: power_up_le power_down_le le_minus_power_downI  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
183  | 
intro: power_mono_odd power_mono power_mono_even zero_le_even_power)  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
184  | 
|
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
185  | 
lemma bnds_power:  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
186  | 
  "\<forall> (x::real) l u. (l1, u1) = float_power_bnds prec n l u \<and> x \<in> {l .. u} \<longrightarrow>
 | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
187  | 
l1 \<le> x ^ n \<and> x ^ n \<le> u1"  | 
| 29805 | 188  | 
using float_power_bnds by auto  | 
189  | 
||
190  | 
section "Square root"  | 
|
191  | 
||
192  | 
text {*
 | 
|
193  | 
||
194  | 
The square root computation is implemented as newton iteration. As first first step we use the  | 
|
195  | 
nearest power of two greater than the square root.  | 
|
196  | 
||
197  | 
*}  | 
|
198  | 
||
199  | 
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: 
47108 
diff
changeset
 | 
200  | 
"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: 
31148 
diff
changeset
 | 
201  | 
"sqrt_iteration prec (Suc m) x = (let y = sqrt_iteration prec m x  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
202  | 
in Float 1 (- 1) * float_plus_up prec y (float_divr prec x y))"  | 
| 29805 | 203  | 
|
| 
47599
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
204  | 
lemma compute_sqrt_iteration_base[code]:  | 
| 
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
205  | 
shows "sqrt_iteration prec n (Float m e) =  | 
| 
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
206  | 
(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: 
47108 
diff
changeset
 | 
207  | 
else (let y = sqrt_iteration prec (n - 1) (Float m e) in  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
208  | 
Float 1 (- 1) * float_plus_up prec y (float_divr prec (Float m e) y)))"  | 
| 
47599
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
209  | 
using bitlen_Float by (cases n) simp_all  | 
| 
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
210  | 
|
| 
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: 
31148 
diff
changeset
 | 
211  | 
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: 
31148 
diff
changeset
 | 
212  | 
"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: 
31148 
diff
changeset
 | 
213  | 
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: 
31148 
diff
changeset
 | 
214  | 
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: 
31148 
diff
changeset
 | 
215  | 
"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: 
31148 
diff
changeset
 | 
216  | 
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: 
31148 
diff
changeset
 | 
217  | 
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: 
31148 
diff
changeset
 | 
218  | 
by pat_completeness auto  | 
| 
55414
 
eab03e9cee8a
renamed '{prod,sum,bool,unit}_case' to 'case_...'
 
blanchet 
parents: 
55413 
diff
changeset
 | 
219  | 
termination by (relation "measure (\<lambda> v. let (prec, x) = case_sum id id v in (if x < 0 then 1 else 0))", auto)  | 
| 29805 | 220  | 
|
| 
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: 
31148 
diff
changeset
 | 
221  | 
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: 
31148 
diff
changeset
 | 
222  | 
declare ub_sqrt.simps[simp del]  | 
| 29805 | 223  | 
|
224  | 
lemma sqrt_ub_pos_pos_1:  | 
|
225  | 
assumes "sqrt x < b" and "0 < b" and "0 < x"  | 
|
226  | 
shows "sqrt x < (b + x / b)/2"  | 
|
227  | 
proof -  | 
|
| 53077 | 228  | 
from assms have "0 < (b - sqrt x)\<^sup>2 " by simp  | 
229  | 
also have "\<dots> = b\<^sup>2 - 2 * b * sqrt x + (sqrt x)\<^sup>2" by algebra  | 
|
230  | 
also have "\<dots> = b\<^sup>2 - 2 * b * sqrt x + x" using assms by simp  | 
|
231  | 
finally have "0 < b\<^sup>2 - 2 * b * sqrt x + x" .  | 
|
| 29805 | 232  | 
hence "0 < b / 2 - sqrt x + x / (2 * b)" using assms  | 
233  | 
by (simp add: field_simps power2_eq_square)  | 
|
234  | 
thus ?thesis by (simp add: field_simps)  | 
|
235  | 
qed  | 
|
236  | 
||
| 
31098
 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 
hoelzl 
parents: 
30971 
diff
changeset
 | 
237  | 
lemma sqrt_iteration_bound: assumes "0 < real x"  | 
| 54269 | 238  | 
shows "sqrt x < sqrt_iteration prec n x"  | 
| 29805 | 239  | 
proof (induct n)  | 
240  | 
case 0  | 
|
241  | 
show ?case  | 
|
242  | 
proof (cases x)  | 
|
243  | 
case (Float m e)  | 
|
| 
47599
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
244  | 
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: 
39556 
diff
changeset
 | 
245  | 
hence "0 < sqrt m" by auto  | 
| 
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
246  | 
|
| 
47599
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
247  | 
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: 
47108 
diff
changeset
 | 
248  | 
|
| 
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
249  | 
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: 
47108 
diff
changeset
 | 
250  | 
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: 
47108 
diff
changeset
 | 
251  | 
also have "\<dots> < 1 * 2 powr (e + nat (bitlen m))"  | 
| 29805 | 252  | 
proof (rule mult_strict_right_mono, auto)  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
253  | 
show "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: 
32920 
diff
changeset
 | 
254  | 
unfolding real_of_int_less_iff[of m, symmetric] by auto  | 
| 29805 | 255  | 
qed  | 
| 
47599
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
256  | 
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: 
47108 
diff
changeset
 | 
257  | 
also have "\<dots> \<le> 2 powr ((e + bitlen m) div 2 + 1)"  | 
| 29805 | 258  | 
proof -  | 
259  | 
let ?E = "e + bitlen m"  | 
|
| 
47599
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
260  | 
have E_mod_pow: "2 powr (?E mod 2) < 4"  | 
| 29805 | 261  | 
proof (cases "?E mod 2 = 1")  | 
| 
32960
 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 
wenzelm 
parents: 
32920 
diff
changeset
 | 
262  | 
case True thus ?thesis by auto  | 
| 29805 | 263  | 
next  | 
| 
32960
 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 
wenzelm 
parents: 
32920 
diff
changeset
 | 
264  | 
case False  | 
| 
 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 
wenzelm 
parents: 
32920 
diff
changeset
 | 
265  | 
have "0 \<le> ?E mod 2" by auto  | 
| 
 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 
wenzelm 
parents: 
32920 
diff
changeset
 | 
266  | 
have "?E mod 2 < 2" by auto  | 
| 
 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 
wenzelm 
parents: 
32920 
diff
changeset
 | 
267  | 
from this[THEN zless_imp_add1_zle]  | 
| 
 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 
wenzelm 
parents: 
32920 
diff
changeset
 | 
268  | 
have "?E mod 2 \<le> 0" using False by auto  | 
| 
 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 
wenzelm 
parents: 
32920 
diff
changeset
 | 
269  | 
from xt1(5)[OF `0 \<le> ?E mod 2` this]  | 
| 
 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 
wenzelm 
parents: 
32920 
diff
changeset
 | 
270  | 
show ?thesis by auto  | 
| 29805 | 271  | 
qed  | 
| 
56889
 
48a745e1bde7
avoid the Complex constructor, use the more natural Re/Im view; moved csqrt to Complex.
 
hoelzl 
parents: 
56813 
diff
changeset
 | 
272  | 
hence "sqrt (2 powr (?E mod 2)) < sqrt (2 * 2)"  | 
| 
 
48a745e1bde7
avoid the Complex constructor, use the more natural Re/Im view; moved csqrt to Complex.
 
hoelzl 
parents: 
56813 
diff
changeset
 | 
273  | 
by (auto simp del: real_sqrt_four)  | 
| 
 
48a745e1bde7
avoid the Complex constructor, use the more natural Re/Im view; moved csqrt to Complex.
 
hoelzl 
parents: 
56813 
diff
changeset
 | 
274  | 
hence E_mod_pow: "sqrt (2 powr (?E mod 2)) < 2" by auto  | 
| 
47599
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
275  | 
|
| 
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
276  | 
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: 
47108 
diff
changeset
 | 
277  | 
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: 
47108 
diff
changeset
 | 
278  | 
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: 
47108 
diff
changeset
 | 
279  | 
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: 
47108 
diff
changeset
 | 
280  | 
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: 
47108 
diff
changeset
 | 
281  | 
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: 
32920 
diff
changeset
 | 
282  | 
by (rule mult_strict_left_mono, auto intro: E_mod_pow)  | 
| 
57512
 
cc97b347b301
reduced name variants for assoc and commute on plus and mult
 
haftmann 
parents: 
57418 
diff
changeset
 | 
283  | 
also have "\<dots> = 2 powr (?E div 2 + 1)" unfolding add.commute[of _ 1] powr_add[symmetric]  | 
| 
47599
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
284  | 
by simp  | 
| 29805 | 285  | 
finally show ?thesis by auto  | 
286  | 
qed  | 
|
| 
47599
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
287  | 
finally show ?thesis using `0 < m`  | 
| 
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
288  | 
unfolding Float  | 
| 47600 | 289  | 
by (subst compute_sqrt_iteration_base) (simp add: ac_simps)  | 
| 29805 | 290  | 
qed  | 
291  | 
next  | 
|
292  | 
case (Suc n)  | 
|
293  | 
let ?b = "sqrt_iteration prec n x"  | 
|
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
294  | 
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: 
30971 
diff
changeset
 | 
295  | 
also have "\<dots> < real ?b" using Suc .  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
296  | 
finally have "sqrt x < (?b + x / ?b)/2" using sqrt_ub_pos_pos_1[OF Suc _ `0 < real x`] by auto  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
297  | 
also have "\<dots> \<le> (?b + (float_divr prec x ?b))/2"  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
298  | 
by (rule divide_right_mono, auto simp add: float_divr)  | 
| 
58410
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
299  | 
also have "\<dots> = (Float 1 (- 1)) * (?b + (float_divr prec x ?b))" by simp  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
300  | 
also have "\<dots> \<le> (Float 1 (- 1)) * (float_plus_up prec ?b (float_divr prec x ?b))"  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
301  | 
by (auto simp add: algebra_simps float_plus_up_le)  | 
| 
49962
 
a8cc904a6820
Renamed {left,right}_distrib to distrib_{right,left}.
 
webertj 
parents: 
49351 
diff
changeset
 | 
302  | 
finally show ?case unfolding sqrt_iteration.simps Let_def distrib_left .  | 
| 29805 | 303  | 
qed  | 
304  | 
||
| 
31098
 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 
hoelzl 
parents: 
30971 
diff
changeset
 | 
305  | 
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: 
30971 
diff
changeset
 | 
306  | 
shows "0 < real (sqrt_iteration prec n x)" (is "0 < ?sqrt")  | 
| 29805 | 307  | 
proof -  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
308  | 
have "0 < sqrt x" using assms by auto  | 
| 29805 | 309  | 
also have "\<dots> < ?sqrt" using sqrt_iteration_bound[OF assms] .  | 
310  | 
finally show ?thesis .  | 
|
311  | 
qed  | 
|
312  | 
||
| 
31098
 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 
hoelzl 
parents: 
30971 
diff
changeset
 | 
313  | 
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: 
31148 
diff
changeset
 | 
314  | 
shows "0 \<le> real (lb_sqrt prec x)"  | 
| 29805 | 315  | 
proof (cases "0 < x")  | 
| 47600 | 316  | 
case True hence "0 < real x" and "0 \<le> x" using `0 \<le> real x` by auto  | 
317  | 
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: 
47108 
diff
changeset
 | 
318  | 
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: 
31148 
diff
changeset
 | 
319  | 
thus ?thesis unfolding lb_sqrt.simps using True by auto  | 
| 29805 | 320  | 
next  | 
| 47600 | 321  | 
case False with `0 \<le> real x` have "real x = 0" by auto  | 
322  | 
thus ?thesis unfolding lb_sqrt.simps by auto  | 
|
| 29805 | 323  | 
qed  | 
324  | 
||
| 49351 | 325  | 
lemma bnds_sqrt': "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: 
31148 
diff
changeset
 | 
326  | 
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: 
31148 
diff
changeset
 | 
327  | 
  { fix x :: float assume "0 < x"
 | 
| 47600 | 328  | 
hence "0 < real x" and "0 \<le> real x" by auto  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
329  | 
hence sqrt_gt0: "0 < sqrt x" by auto  | 
| 
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
330  | 
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: 
39556 
diff
changeset
 | 
331  | 
|
| 
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
332  | 
have "(float_divl prec x (sqrt_iteration prec prec x)) \<le>  | 
| 
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
333  | 
x / (sqrt_iteration prec prec x)" by (rule float_divl)  | 
| 
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
334  | 
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: 
31148 
diff
changeset
 | 
335  | 
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: 
31148 
diff
changeset
 | 
336  | 
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: 
39556 
diff
changeset
 | 
337  | 
also have "\<dots> = sqrt x"  | 
| 
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
338  | 
unfolding inverse_eq_iff_eq[of _ "sqrt x", symmetric]  | 
| 
32960
 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 
wenzelm 
parents: 
32920 
diff
changeset
 | 
339  | 
sqrt_divide_self_eq[OF `0 \<le> real x`, symmetric] by auto  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
340  | 
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: 
31148 
diff
changeset
 | 
341  | 
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: 
31148 
diff
changeset
 | 
342  | 
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: 
31148 
diff
changeset
 | 
343  | 
|
| 
 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 
hoelzl 
parents: 
31148 
diff
changeset
 | 
344  | 
  { fix x :: float assume "0 < x"
 | 
| 47600 | 345  | 
hence "0 < real x" by auto  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
346  | 
hence "0 < sqrt x" by auto  | 
| 
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
347  | 
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: 
31148 
diff
changeset
 | 
348  | 
using sqrt_iteration_bound by auto  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
349  | 
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: 
31148 
diff
changeset
 | 
350  | 
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: 
31148 
diff
changeset
 | 
351  | 
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: 
31148 
diff
changeset
 | 
352  | 
|
| 
 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 
hoelzl 
parents: 
31148 
diff
changeset
 | 
353  | 
show ?thesis  | 
| 54269 | 354  | 
using lb[of "-x"] ub[of "-x"] lb[of x] ub[of x]  | 
355  | 
by (auto simp add: lb_sqrt.simps ub_sqrt.simps real_sqrt_minus)  | 
|
| 29805 | 356  | 
qed  | 
357  | 
||
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
358  | 
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: 
31148 
diff
changeset
 | 
359  | 
proof ((rule allI) +, rule impI, erule conjE, rule conjI)  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
360  | 
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: 
31148 
diff
changeset
 | 
361  | 
assume "(l, u) = (lb_sqrt prec lx, ub_sqrt prec ux)"  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
362  | 
    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: 
31148 
diff
changeset
 | 
363  | 
hence l: "l = lb_sqrt prec lx " and u: "u = ub_sqrt prec ux" by auto  | 
| 29805 | 364  | 
|
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
365  | 
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: 
31148 
diff
changeset
 | 
366  | 
from order_trans[OF _ this]  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
367  | 
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: 
39556 
diff
changeset
 | 
368  | 
|
| 
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
369  | 
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: 
31148 
diff
changeset
 | 
370  | 
from order_trans[OF this]  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
371  | 
show "sqrt x \<le> u" unfolding u using bnds_sqrt'[of ux prec] by auto  | 
| 29805 | 372  | 
qed  | 
373  | 
||
374  | 
section "Arcus tangens and \<pi>"  | 
|
375  | 
||
376  | 
subsection "Compute arcus tangens series"  | 
|
377  | 
||
378  | 
text {*
 | 
|
379  | 
||
380  | 
As first step we implement the computation of the arcus tangens series. This is only valid in the range  | 
|
381  | 
@{term "{-1 :: real .. 1}"}. This is used to compute \<pi> and then the entire arcus tangens.
 | 
|
382  | 
||
383  | 
*}  | 
|
384  | 
||
385  | 
fun ub_arctan_horner :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> float \<Rightarrow> float"  | 
|
386  | 
and lb_arctan_horner :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> float \<Rightarrow> float" where  | 
|
387  | 
"ub_arctan_horner prec 0 k x = 0"  | 
|
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
388  | 
| "ub_arctan_horner prec (Suc n) k x = float_plus_up prec  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
389  | 
(rapprox_rat prec 1 k) (- float_round_down prec (x * (lb_arctan_horner prec n (k + 2) x)))"  | 
| 29805 | 390  | 
| "lb_arctan_horner prec 0 k x = 0"  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
391  | 
| "lb_arctan_horner prec (Suc n) k x = float_plus_down prec  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
392  | 
(lapprox_rat prec 1 k) (- float_round_up prec (x * (ub_arctan_horner prec n (k + 2) x)))"  | 
| 29805 | 393  | 
|
| 49351 | 394  | 
lemma arctan_0_1_bounds':  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
395  | 
assumes "0 \<le> real y" "real y \<le> 1" and "even n"  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
396  | 
shows "arctan (sqrt y) \<in>  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
397  | 
      {(sqrt y * lb_arctan_horner prec n 1 y) .. (sqrt y * ub_arctan_horner prec (Suc n) 1 y)}"
 | 
| 29805 | 398  | 
proof -  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
399  | 
let ?c = "\<lambda>i. (- 1) ^ i * (1 / (i * 2 + (1::nat)) * sqrt y ^ (i * 2 + 1))"  | 
| 54269 | 400  | 
let ?S = "\<lambda>n. \<Sum> i=0..<n. ?c i"  | 
| 29805 | 401  | 
|
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
402  | 
have "0 \<le> sqrt y" using assms by auto  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
403  | 
have "sqrt y \<le> 1" using assms by auto  | 
| 
58709
 
efdc6c533bd3
prefer generic elimination rules for even/odd over specialized unfold rules for nat
 
haftmann 
parents: 
58410 
diff
changeset
 | 
404  | 
from `even n` obtain m where "2 * m = n" by (blast elim: evenE)  | 
| 31809 | 405  | 
|
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
406  | 
  have "arctan (sqrt y) \<in> { ?S n .. ?S (Suc n) }"
 | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
407  | 
proof (cases "sqrt y = 0")  | 
| 29805 | 408  | 
case False  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
409  | 
hence "0 < sqrt y" using `0 \<le> sqrt y` by auto  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
410  | 
hence prem: "0 < 1 / (0 * 2 + (1::nat)) * sqrt y ^ (0 * 2 + 1)" by auto  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
411  | 
|
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
412  | 
have "\<bar> sqrt y \<bar> \<le> 1" using `0 \<le> sqrt y` `sqrt y \<le> 1` by auto  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
413  | 
from mp[OF summable_Leibniz(2)[OF zeroseq_arctan_series[OF this]  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
414  | 
monoseq_arctan_series[OF this]] prem, THEN spec, of m, unfolded `2 * m = n`]  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
415  | 
show ?thesis unfolding arctan_series[OF `\<bar> sqrt y \<bar> \<le> 1`] Suc_eq_plus1 atLeast0LessThan .  | 
| 29805 | 416  | 
qed auto  | 
417  | 
note arctan_bounds = this[unfolded atLeastAtMost_iff]  | 
|
418  | 
||
419  | 
have F: "\<And>n. 2 * Suc n + 1 = 2 * n + 1 + 2" by auto  | 
|
420  | 
||
| 31809 | 421  | 
note bounds = horner_bounds[where s=1 and f="\<lambda>i. 2 * i + 1" and j'=0  | 
| 29805 | 422  | 
and lb="\<lambda>n i k x. lb_arctan_horner prec n k x"  | 
| 31809 | 423  | 
and ub="\<lambda>n i k x. ub_arctan_horner prec n k x",  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
424  | 
OF `0 \<le> real y` F lb_arctan_horner.simps ub_arctan_horner.simps]  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
425  | 
|
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
426  | 
  { have "(sqrt y * lb_arctan_horner prec n 1 y) \<le> ?S n"
 | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
427  | 
using bounds(1) `0 \<le> sqrt y`  | 
| 
57512
 
cc97b347b301
reduced name variants for assoc and commute on plus and mult
 
haftmann 
parents: 
57418 
diff
changeset
 | 
428  | 
unfolding power_add power_one_right mult.assoc[symmetric] setsum_left_distrib[symmetric]  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
429  | 
unfolding mult.commute[where 'a=real] mult.commute[of _ "2::nat"] power_mult  | 
| 29805 | 430  | 
by (auto intro!: mult_left_mono)  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
431  | 
also have "\<dots> \<le> arctan (sqrt y)" using arctan_bounds ..  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
432  | 
finally have "(sqrt y * lb_arctan_horner prec n 1 y) \<le> arctan (sqrt y)" . }  | 
| 29805 | 433  | 
moreover  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
434  | 
  { have "arctan (sqrt y) \<le> ?S (Suc n)" using arctan_bounds ..
 | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
435  | 
also have "\<dots> \<le> (sqrt y * ub_arctan_horner prec (Suc n) 1 y)"  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
436  | 
using bounds(2)[of "Suc n"] `0 \<le> sqrt y`  | 
| 
57512
 
cc97b347b301
reduced name variants for assoc and commute on plus and mult
 
haftmann 
parents: 
57418 
diff
changeset
 | 
437  | 
unfolding power_add power_one_right mult.assoc[symmetric] setsum_left_distrib[symmetric]  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
438  | 
unfolding mult.commute[where 'a=real] mult.commute[of _ "2::nat"] power_mult  | 
| 29805 | 439  | 
by (auto intro!: mult_left_mono)  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
440  | 
finally have "arctan (sqrt y) \<le> (sqrt y * ub_arctan_horner prec (Suc n) 1 y)" . }  | 
| 29805 | 441  | 
ultimately show ?thesis by auto  | 
442  | 
qed  | 
|
443  | 
||
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
444  | 
lemma arctan_0_1_bounds: assumes "0 \<le> real y" "real y \<le> 1"  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
445  | 
shows "arctan (sqrt y) \<in>  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
446  | 
    {(sqrt y * lb_arctan_horner prec (get_even n) 1 y) ..
 | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
447  | 
(sqrt y * ub_arctan_horner prec (get_odd n) 1 y)}"  | 
| 54269 | 448  | 
using  | 
449  | 
arctan_0_1_bounds'[OF assms, of n prec]  | 
|
450  | 
arctan_0_1_bounds'[OF assms, of "n + 1" prec]  | 
|
451  | 
arctan_0_1_bounds'[OF assms, of "n - 1" prec]  | 
|
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
452  | 
by (auto simp: get_even_def get_odd_def odd_pos simp del: ub_arctan_horner.simps  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
453  | 
lb_arctan_horner.simps)  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
454  | 
|
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
455  | 
lemma arctan_lower_bound:  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
456  | 
assumes "0 \<le> x"  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
457  | 
shows "x / (1 + x\<^sup>2) \<le> arctan x" (is "?l x \<le> _")  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
458  | 
proof -  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
459  | 
have "?l x - arctan x \<le> ?l 0 - arctan 0"  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
460  | 
using assms  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
461  | 
by (intro DERIV_nonpos_imp_nonincreasing[where f="\<lambda>x. ?l x - arctan x"])  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
462  | 
(auto intro!: derivative_eq_intros simp: add_nonneg_eq_0_iff field_simps)  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
463  | 
thus ?thesis by simp  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
464  | 
qed  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
465  | 
|
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
466  | 
lemma arctan_divide_mono: "0 < x \<Longrightarrow> x \<le> y \<Longrightarrow> arctan y / y \<le> arctan x / x"  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
467  | 
by (rule DERIV_nonpos_imp_nonincreasing[where f="\<lambda>x. arctan x / x"])  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
468  | 
(auto intro!: derivative_eq_intros divide_nonpos_nonneg  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
469  | 
simp: inverse_eq_divide arctan_lower_bound)  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
470  | 
|
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
471  | 
lemma arctan_mult_mono: "0 \<le> x \<Longrightarrow> x \<le> y \<Longrightarrow> x * arctan y \<le> y * arctan x"  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
472  | 
using arctan_divide_mono[of x y] by (cases "x = 0") (simp_all add: field_simps)  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
473  | 
|
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
474  | 
lemma arctan_mult_le:  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
475  | 
assumes "0 \<le> x" "x \<le> y" "y * z \<le> arctan y"  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
476  | 
shows "x * z \<le> arctan x"  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
477  | 
proof cases  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
478  | 
assume "x \<noteq> 0"  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
479  | 
with assms have "z \<le> arctan y / y" by (simp add: field_simps)  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
480  | 
also have "\<dots> \<le> arctan x / x" using assms `x \<noteq> 0` by (auto intro!: arctan_divide_mono)  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
481  | 
finally show ?thesis using assms `x \<noteq> 0` by (simp add: field_simps)  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
482  | 
qed simp  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
483  | 
|
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
484  | 
lemma arctan_le_mult:  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
485  | 
assumes "0 < x" "x \<le> y" "arctan x \<le> x * z"  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
486  | 
shows "arctan y \<le> y * z"  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
487  | 
proof -  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
488  | 
from assms have "arctan y / y \<le> arctan x / x" by (auto intro!: arctan_divide_mono)  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
489  | 
also have "\<dots> \<le> z" using assms by (auto simp: field_simps)  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
490  | 
finally show ?thesis using assms by (simp add: field_simps)  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
491  | 
qed  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
492  | 
|
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
493  | 
lemma arctan_0_1_bounds_le:  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
494  | 
assumes "0 \<le> x" "x \<le> 1" "0 < real xl" "real xl \<le> x * x" "x * x \<le> real xu" "real xu \<le> 1"  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
495  | 
shows "arctan x \<in>  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
496  | 
      {x * lb_arctan_horner p1 (get_even n) 1 xu .. x * ub_arctan_horner p2 (get_odd n) 1 xl}"
 | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
497  | 
proof -  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
498  | 
from assms have "real xl \<le> 1" "sqrt (real xl) \<le> x" "x \<le> sqrt (real xu)" "0 \<le> real xu"  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
499  | 
"0 \<le> real xl" "0 < sqrt (real xl)"  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
500  | 
by (auto intro!: real_le_rsqrt real_le_lsqrt simp: power2_eq_square)  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
501  | 
from arctan_0_1_bounds[OF `0 \<le> real xu` `real xu \<le> 1`]  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
502  | 
have "sqrt (real xu) * real (lb_arctan_horner p1 (get_even n) 1 xu) \<le> arctan (sqrt (real xu))"  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
503  | 
by simp  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
504  | 
from arctan_mult_le[OF `0 \<le> x` `x \<le> sqrt _` this]  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
505  | 
have "x * real (lb_arctan_horner p1 (get_even n) 1 xu) \<le> arctan x" .  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
506  | 
moreover  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
507  | 
from arctan_0_1_bounds[OF `0 \<le> real xl` `real xl \<le> 1`]  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
508  | 
have "arctan (sqrt (real xl)) \<le> sqrt (real xl) * real (ub_arctan_horner p2 (get_odd n) 1 xl)"  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
509  | 
by simp  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
510  | 
from arctan_le_mult[OF `0 < sqrt xl` `sqrt xl \<le> x` this]  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
511  | 
have "arctan x \<le> x * real (ub_arctan_horner p2 (get_odd n) 1 xl)" .  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
512  | 
ultimately show ?thesis by simp  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
513  | 
qed  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
514  | 
|
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
515  | 
lemma mult_nonneg_le_one: fixes a::real assumes "0 \<le> a" "0 \<le> b" "a \<le> 1" "b \<le> 1" shows "a * b \<le> 1"  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
516  | 
proof -  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
517  | 
have "a * b \<le> 1 * 1"  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
518  | 
by (intro mult_mono assms) simp_all  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
519  | 
thus ?thesis by simp  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
520  | 
qed  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
521  | 
|
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
522  | 
lemma arctan_0_1_bounds_round:  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
523  | 
assumes "0 \<le> real x" "real x \<le> 1"  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
524  | 
shows "arctan x \<in>  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
525  | 
      {real x * lb_arctan_horner p1 (get_even n) 1 (float_round_up (Suc p2) (x * x)) ..
 | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
526  | 
real x * ub_arctan_horner p3 (get_odd n) 1 (float_round_down (Suc p4) (x * x))}"  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
527  | 
using assms  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
528  | 
apply (cases "x > 0")  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
529  | 
apply (intro arctan_0_1_bounds_le)  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
530  | 
apply (auto simp: float_round_down.rep_eq float_round_up.rep_eq  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
531  | 
intro!: truncate_up_le1 mult_nonneg_le_one truncate_down_le truncate_up_le truncate_down_pos  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
532  | 
mult_pos_pos)  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
533  | 
done  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
534  | 
|
| 29805 | 535  | 
|
536  | 
subsection "Compute \<pi>"  | 
|
537  | 
||
538  | 
definition ub_pi :: "nat \<Rightarrow> float" where  | 
|
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
539  | 
"ub_pi prec =  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
540  | 
(let  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
541  | 
A = rapprox_rat prec 1 5 ;  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
542  | 
B = lapprox_rat prec 1 239  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
543  | 
in ((Float 1 2) * float_plus_up prec  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
544  | 
((Float 1 2) * float_round_up prec (A * (ub_arctan_horner prec (get_odd (prec div 4 + 1)) 1  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
545  | 
(float_round_down (Suc prec) (A * A)))))  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
546  | 
(- float_round_down prec (B * (lb_arctan_horner prec (get_even (prec div 14 + 1)) 1  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
547  | 
(float_round_up (Suc prec) (B * B)))))))"  | 
| 29805 | 548  | 
|
549  | 
definition lb_pi :: "nat \<Rightarrow> float" where  | 
|
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
550  | 
"lb_pi prec =  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
551  | 
(let  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
552  | 
A = lapprox_rat prec 1 5 ;  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
553  | 
B = rapprox_rat prec 1 239  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
554  | 
in ((Float 1 2) * float_plus_down prec  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
555  | 
((Float 1 2) * float_round_down prec (A * (lb_arctan_horner prec (get_even (prec div 4 + 1)) 1  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
556  | 
(float_round_up (Suc prec) (A * A)))))  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
557  | 
(- float_round_up prec (B * (ub_arctan_horner prec (get_odd (prec div 14 + 1)) 1  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
558  | 
(float_round_down (Suc prec) (B * B)))))))"  | 
| 29805 | 559  | 
|
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
560  | 
lemma pi_boundaries: "pi \<in> {(lb_pi n) .. (ub_pi n)}"
 | 
| 29805 | 561  | 
proof -  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
562  | 
have machin_pi: "pi = 4 * (4 * arctan (1 / 5) - arctan (1 / 239))"  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
563  | 
unfolding machin[symmetric] by auto  | 
| 29805 | 564  | 
|
565  | 
  { fix prec n :: nat fix k :: int assume "1 < k" hence "0 \<le> k" and "0 < k" and "1 \<le> k" by auto
 | 
|
566  | 
let ?k = "rapprox_rat prec 1 k"  | 
|
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
567  | 
let ?kl = "float_round_down (Suc prec) (?k * ?k)"  | 
| 29805 | 568  | 
have "1 div k = 0" using div_pos_pos_trivial[OF _ `1 < k`] by auto  | 
| 31809 | 569  | 
|
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
570  | 
have "0 \<le> real ?k" by (rule order_trans[OF _ rapprox_rat]) (auto simp add: `0 \<le> k`)  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
571  | 
have "real ?k \<le> 1"  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
572  | 
by (auto simp add: `0 < k` `1 \<le> k` less_imp_le  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
573  | 
intro!: mult_nonneg_le_one order_trans[OF _ rapprox_rat] rapprox_rat_le1)  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
574  | 
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: 
39556 
diff
changeset
 | 
575  | 
hence "arctan (1 / k) \<le> arctan ?k" by (rule arctan_monotone')  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
576  | 
also have "\<dots> \<le> (?k * ub_arctan_horner prec (get_odd n) 1 ?kl)"  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
577  | 
using arctan_0_1_bounds_round[OF `0 \<le> real ?k` `real ?k \<le> 1`]  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
578  | 
by auto  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
579  | 
finally have "arctan (1 / k) \<le> ?k * ub_arctan_horner prec (get_odd n) 1 ?kl" .  | 
| 29805 | 580  | 
} note ub_arctan = this  | 
581  | 
||
582  | 
  { fix prec n :: nat fix k :: int assume "1 < k" hence "0 \<le> k" and "0 < k" by auto
 | 
|
583  | 
let ?k = "lapprox_rat prec 1 k"  | 
|
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
584  | 
let ?ku = "float_round_up (Suc prec) (?k * ?k)"  | 
| 29805 | 585  | 
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: 
39556 
diff
changeset
 | 
586  | 
have "1 / k \<le> 1" using `1 < k` by auto  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
587  | 
have "0 \<le> real ?k" using lapprox_rat_nonneg[where x=1 and y=k, OF zero_le_one `0 \<le> k`]  | 
| 
58982
 
27e7e3f9e665
simplified computations based on round_up by reducing to round_down;
 
immler 
parents: 
58889 
diff
changeset
 | 
588  | 
by (auto simp add: `1 div k = 0`)  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
589  | 
have "0 \<le> real (?k * ?k)" by simp  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
590  | 
have "real ?k \<le> 1" using lapprox_rat by (rule order_trans, auto simp add: `1 / k \<le> 1`)  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
591  | 
hence "real (?k * ?k) \<le> 1" using `0 \<le> real ?k` by (auto intro!: mult_nonneg_le_one)  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
592  | 
|
| 
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
593  | 
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: 
39556 
diff
changeset
 | 
594  | 
|
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
595  | 
have "?k * lb_arctan_horner prec (get_even n) 1 ?ku \<le> arctan ?k"  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
596  | 
using arctan_0_1_bounds_round[OF `0 \<le> real ?k` `real ?k \<le> 1`]  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
597  | 
by auto  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
598  | 
also have "\<dots> \<le> arctan (1 / k)" using `?k \<le> 1 / k` by (rule arctan_monotone')  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
599  | 
finally have "?k * lb_arctan_horner prec (get_even n) 1 ?ku \<le> arctan (1 / k)" .  | 
| 29805 | 600  | 
} note lb_arctan = this  | 
601  | 
||
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
602  | 
have "pi \<le> ub_pi n "  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
603  | 
unfolding ub_pi_def machin_pi Let_def times_float.rep_eq Float_num  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
604  | 
using lb_arctan[of 239] ub_arctan[of 5] powr_realpow[of 2 2]  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
605  | 
by (intro mult_left_mono float_plus_up_le float_plus_down_le)  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
606  | 
(auto intro!: mult_left_mono float_round_down_le float_round_up_le diff_mono)  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
607  | 
moreover have "lb_pi n \<le> pi"  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
608  | 
unfolding lb_pi_def machin_pi Let_def times_float.rep_eq Float_num  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
609  | 
using lb_arctan[of 5] ub_arctan[of 239]  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
610  | 
by (intro mult_left_mono float_plus_up_le float_plus_down_le)  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
611  | 
(auto intro!: mult_left_mono float_round_down_le float_round_up_le diff_mono)  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
612  | 
ultimately show ?thesis by auto  | 
| 29805 | 613  | 
qed  | 
614  | 
||
615  | 
subsection "Compute arcus tangens in the entire domain"  | 
|
616  | 
||
| 
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: 
31148 
diff
changeset
 | 
617  | 
function lb_arctan :: "nat \<Rightarrow> float \<Rightarrow> float" and ub_arctan :: "nat \<Rightarrow> float \<Rightarrow> float" where  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
618  | 
"lb_arctan prec x =  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
619  | 
(let  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
620  | 
ub_horner = \<lambda> x. float_round_up prec  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
621  | 
(x *  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
622  | 
ub_arctan_horner prec (get_odd (prec div 4 + 1)) 1 (float_round_down (Suc prec) (x * x)));  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
623  | 
lb_horner = \<lambda> x. float_round_down prec  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
624  | 
(x *  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
625  | 
lb_arctan_horner prec (get_even (prec div 4 + 1)) 1 (float_round_up (Suc prec) (x * x)))  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
626  | 
in  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
627  | 
if x < 0 then - ub_arctan prec (-x)  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
628  | 
else if x \<le> Float 1 (- 1) then lb_horner x  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
629  | 
else if x \<le> Float 1 1 then  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
630  | 
Float 1 1 *  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
631  | 
lb_horner  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
632  | 
(float_divl prec x  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
633  | 
(float_plus_up prec 1  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
634  | 
(ub_sqrt prec (float_plus_up prec 1 (float_round_up prec (x * x))))))  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
635  | 
else let inv = float_divr prec 1 x in  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
636  | 
if inv > 1 then 0  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
637  | 
else float_plus_down prec (lb_pi prec * Float 1 (- 1)) ( - ub_horner inv))"  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
638  | 
|
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
639  | 
| "ub_arctan prec x =  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
640  | 
(let  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
641  | 
lb_horner = \<lambda> x. float_round_down prec  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
642  | 
(x *  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
643  | 
lb_arctan_horner prec (get_even (prec div 4 + 1)) 1 (float_round_up (Suc prec) (x * x))) ;  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
644  | 
ub_horner = \<lambda> x. float_round_up prec  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
645  | 
(x *  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
646  | 
ub_arctan_horner prec (get_odd (prec div 4 + 1)) 1 (float_round_down (Suc prec) (x * x)))  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
647  | 
in if x < 0 then - lb_arctan prec (-x)  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
648  | 
else if x \<le> Float 1 (- 1) then ub_horner x  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
649  | 
else if x \<le> Float 1 1 then  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
650  | 
let y = float_divr prec x  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
651  | 
(float_plus_down  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
652  | 
(Suc prec) 1 (lb_sqrt prec (float_plus_down prec 1 (float_round_down prec (x * x)))))  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
653  | 
in if y > 1 then ub_pi prec * Float 1 (- 1) else Float 1 1 * ub_horner y  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
654  | 
else float_plus_up prec (ub_pi prec * Float 1 (- 1)) ( - lb_horner (float_divl prec 1 x)))"  | 
| 29805 | 655  | 
by pat_completeness auto  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
656  | 
termination  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
657  | 
by (relation "measure (\<lambda> v. let (prec, x) = case_sum id id v in (if x < 0 then 1 else 0))", auto)  | 
| 29805 | 658  | 
|
659  | 
declare ub_arctan_horner.simps[simp del]  | 
|
660  | 
declare lb_arctan_horner.simps[simp del]  | 
|
661  | 
||
| 
31098
 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 
hoelzl 
parents: 
30971 
diff
changeset
 | 
662  | 
lemma lb_arctan_bound': assumes "0 \<le> real x"  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
663  | 
shows "lb_arctan prec x \<le> arctan x"  | 
| 29805 | 664  | 
proof -  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
665  | 
have "\<not> x < 0" and "0 \<le> x"  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
666  | 
using `0 \<le> real x` by (auto intro!: truncate_up_le )  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
667  | 
|
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
668  | 
let "?ub_horner x" =  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
669  | 
"x * ub_arctan_horner prec (get_odd (prec div 4 + 1)) 1 (float_round_down (Suc prec) (x * x))"  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
670  | 
and "?lb_horner x" =  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
671  | 
"x * lb_arctan_horner prec (get_even (prec div 4 + 1)) 1 (float_round_up (Suc prec) (x * x))"  | 
| 29805 | 672  | 
|
673  | 
show ?thesis  | 
|
| 
58410
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
674  | 
proof (cases "x \<le> Float 1 (- 1)")  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
675  | 
case True hence "real x \<le> 1" by simp  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
676  | 
from arctan_0_1_bounds_round[OF `0 \<le> real x` `real x \<le> 1`]  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
677  | 
show ?thesis  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
678  | 
unfolding lb_arctan.simps Let_def if_not_P[OF `\<not> x < 0`] if_P[OF True] using `0 \<le> x`  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
679  | 
by (auto intro!: float_round_down_le)  | 
| 29805 | 680  | 
next  | 
| 47600 | 681  | 
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: 
30971 
diff
changeset
 | 
682  | 
let ?R = "1 + sqrt (1 + real x * real x)"  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
683  | 
let ?sxx = "float_plus_up prec 1 (float_round_up prec (x * x))"  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
684  | 
let ?fR = "float_plus_up prec 1 (ub_sqrt prec ?sxx)"  | 
| 29805 | 685  | 
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: 
31148 
diff
changeset
 | 
686  | 
|
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
687  | 
have divisor_gt0: "0 < ?R" by (auto intro: add_pos_nonneg)  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
688  | 
|
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
689  | 
have "sqrt (1 + x*x) \<le> sqrt ?sxx"  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
690  | 
by (auto simp: float_plus_up.rep_eq plus_up_def float_round_up.rep_eq intro!: truncate_up_le)  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
691  | 
also have "\<dots> \<le> ub_sqrt prec ?sxx"  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
692  | 
using bnds_sqrt'[of ?sxx prec] by auto  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
693  | 
finally  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
694  | 
have "sqrt (1 + x*x) \<le> ub_sqrt prec ?sxx" .  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
695  | 
hence "?R \<le> ?fR" by (auto simp: float_plus_up.rep_eq plus_up_def intro!: truncate_up_le)  | 
| 47600 | 696  | 
hence "0 < ?fR" and "0 < real ?fR" using `0 < ?R` by auto  | 
| 29805 | 697  | 
|
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
698  | 
have monotone: "?DIV \<le> x / ?R"  | 
| 29805 | 699  | 
proof -  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
700  | 
have "?DIV \<le> real x / ?fR" by (rule float_divl)  | 
| 
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
701  | 
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 | 702  | 
finally show ?thesis .  | 
703  | 
qed  | 
|
704  | 
||
705  | 
show ?thesis  | 
|
706  | 
proof (cases "x \<le> Float 1 1")  | 
|
707  | 
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: 
31148 
diff
changeset
 | 
708  | 
|
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
709  | 
have "x \<le> sqrt (1 + x * x)" using real_sqrt_sum_squares_ge2[where x=1, unfolded numeral_2_eq_2] by auto  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
710  | 
also note `\<dots> \<le> (ub_sqrt prec ?sxx)`  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
711  | 
finally have "real x \<le> ?fR" by (auto simp: float_plus_up.rep_eq plus_up_def intro!: truncate_up_le)  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
712  | 
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: 
30971 
diff
changeset
 | 
713  | 
ultimately have "real ?DIV \<le> 1" unfolding divide_le_eq_1_pos[OF `0 < real ?fR`, symmetric] by auto  | 
| 29805 | 714  | 
|
| 54782 | 715  | 
have "0 \<le> real ?DIV" using float_divl_lower_bound[OF `0 \<le> x`] `0 < ?fR` unfolding less_eq_float_def by auto  | 
| 
47599
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
716  | 
|
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
717  | 
from arctan_0_1_bounds_round[OF `0 \<le> real (?DIV)` `real (?DIV) \<le> 1`]  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
718  | 
have "Float 1 1 * ?lb_horner ?DIV \<le> 2 * arctan ?DIV" by simp  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
719  | 
also have "\<dots> \<le> 2 * arctan (x / ?R)"  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
720  | 
using arctan_monotone'[OF monotone] by (auto intro!: mult_left_mono arctan_monotone')  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
721  | 
also have "2 * arctan (x / ?R) = arctan x" using arctan_half[symmetric] unfolding numeral_2_eq_2 power_Suc2 power_0 mult_1_left .  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
722  | 
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]  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
723  | 
by (auto simp: float_round_down.rep_eq intro!: order_trans[OF mult_left_mono[OF truncate_down]])  | 
| 29805 | 724  | 
next  | 
725  | 
case False  | 
|
| 47600 | 726  | 
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: 
30971 
diff
changeset
 | 
727  | 
hence "1 \<le> real x" by auto  | 
| 29805 | 728  | 
|
729  | 
let "?invx" = "float_divr prec 1 x"  | 
|
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
730  | 
have "0 \<le> arctan x" using arctan_monotone'[OF `0 \<le> real x`] using arctan_tan[of 0, unfolded tan_zero] by auto  | 
| 29805 | 731  | 
|
732  | 
show ?thesis  | 
|
733  | 
proof (cases "1 < ?invx")  | 
|
| 
32960
 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 
wenzelm 
parents: 
32920 
diff
changeset
 | 
734  | 
case True  | 
| 
58410
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
735  | 
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: 
39556 
diff
changeset
 | 
736  | 
using `0 \<le> arctan x` by auto  | 
| 29805 | 737  | 
next  | 
| 
32960
 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 
wenzelm 
parents: 
32920 
diff
changeset
 | 
738  | 
case False  | 
| 47600 | 739  | 
hence "real ?invx \<le> 1" by auto  | 
| 
32960
 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 
wenzelm 
parents: 
32920 
diff
changeset
 | 
740  | 
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: 
32920 
diff
changeset
 | 
741  | 
|
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
742  | 
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: 
39556 
diff
changeset
 | 
743  | 
|
| 
47601
 
050718fe6eee
use real :: float => real as lifting-morphism so we can directlry use the rep_eq theorems
 
hoelzl 
parents: 
47600 
diff
changeset
 | 
744  | 
have "arctan (1 / x) \<le> arctan ?invx" unfolding one_float.rep_eq[symmetric] by (rule arctan_monotone', rule float_divr)  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
745  | 
also have "\<dots> \<le> ?ub_horner ?invx" using arctan_0_1_bounds_round[OF `0 \<le> real ?invx` `real ?invx \<le> 1`]  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
746  | 
by (auto intro!: float_round_up_le)  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
747  | 
also note float_round_up  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
748  | 
finally have "pi / 2 - float_round_up prec (?ub_horner ?invx) \<le> arctan x"  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
749  | 
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: 
32920 
diff
changeset
 | 
750  | 
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: 
32920 
diff
changeset
 | 
751  | 
moreover  | 
| 
58410
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
752  | 
have "lb_pi prec * Float 1 (- 1) \<le> pi / 2"  | 
| 
47599
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
753  | 
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: 
32920 
diff
changeset
 | 
754  | 
ultimately  | 
| 
58410
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
755  | 
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]  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
756  | 
by (auto intro!: float_plus_down_le)  | 
| 29805 | 757  | 
qed  | 
758  | 
qed  | 
|
759  | 
qed  | 
|
760  | 
qed  | 
|
761  | 
||
| 
31098
 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 
hoelzl 
parents: 
30971 
diff
changeset
 | 
762  | 
lemma ub_arctan_bound': assumes "0 \<le> real x"  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
763  | 
shows "arctan x \<le> ub_arctan prec x"  | 
| 29805 | 764  | 
proof -  | 
| 47600 | 765  | 
have "\<not> x < 0" and "0 \<le> x" using `0 \<le> real x` by auto  | 
| 29805 | 766  | 
|
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
767  | 
let "?ub_horner x" = "float_round_up prec (x * ub_arctan_horner prec (get_odd (prec div 4 + 1)) 1 (float_round_down (Suc prec) (x * x)))"  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
768  | 
and "?lb_horner x" = "float_round_down prec (x * lb_arctan_horner prec (get_even (prec div 4 + 1)) 1 (float_round_up (Suc prec) (x * x)))"  | 
| 29805 | 769  | 
|
770  | 
show ?thesis  | 
|
| 
58410
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
771  | 
proof (cases "x \<le> Float 1 (- 1)")  | 
| 47600 | 772  | 
case True hence "real x \<le> 1" by auto  | 
| 29805 | 773  | 
show ?thesis unfolding ub_arctan.simps Let_def if_not_P[OF `\<not> x < 0`] if_P[OF True]  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
774  | 
using arctan_0_1_bounds_round[OF `0 \<le> real x` `real x \<le> 1`]  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
775  | 
by (auto intro!: float_round_up_le)  | 
| 29805 | 776  | 
next  | 
| 47600 | 777  | 
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: 
30971 
diff
changeset
 | 
778  | 
let ?R = "1 + sqrt (1 + real x * real x)"  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
779  | 
let ?sxx = "float_plus_down prec 1 (float_round_down prec (x * x))"  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
780  | 
let ?fR = "float_plus_down (Suc prec) 1 (lb_sqrt prec ?sxx)"  | 
| 29805 | 781  | 
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: 
31148 
diff
changeset
 | 
782  | 
|
| 
31098
 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 
hoelzl 
parents: 
30971 
diff
changeset
 | 
783  | 
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: 
30971 
diff
changeset
 | 
784  | 
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: 
31148 
diff
changeset
 | 
785  | 
|
| 29805 | 786  | 
hence divisor_gt0: "0 < ?R" by (auto intro: add_pos_nonneg)  | 
787  | 
||
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
788  | 
have "lb_sqrt prec ?sxx \<le> sqrt ?sxx"  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
789  | 
using bnds_sqrt'[of ?sxx] by auto  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
790  | 
also have "\<dots> \<le> sqrt (1 + x*x)"  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
791  | 
by (auto simp: float_plus_down.rep_eq plus_down_def float_round_down.rep_eq truncate_down_le)  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
792  | 
finally have "lb_sqrt prec ?sxx \<le> sqrt (1 + x*x)" .  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
793  | 
hence "?fR \<le> ?R" by (auto simp: float_plus_down.rep_eq plus_down_def truncate_down_le)  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
794  | 
have "0 < real ?fR"  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
795  | 
by (auto simp: float_plus_down.rep_eq plus_down_def float_round_down.rep_eq  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
796  | 
intro!: truncate_down_ge1 lb_sqrt_lower_bound order_less_le_trans[OF zero_less_one]  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
797  | 
truncate_down_nonneg add_nonneg_nonneg)  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
798  | 
have monotone: "x / ?R \<le> (float_divr prec x ?fR)"  | 
| 29805 | 799  | 
proof -  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
800  | 
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: 
39556 
diff
changeset
 | 
801  | 
have "x / ?R \<le> x / ?fR" .  | 
| 
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
802  | 
also have "\<dots> \<le> ?DIV" by (rule float_divr)  | 
| 29805 | 803  | 
finally show ?thesis .  | 
804  | 
qed  | 
|
805  | 
||
806  | 
show ?thesis  | 
|
807  | 
proof (cases "x \<le> Float 1 1")  | 
|
808  | 
case True  | 
|
809  | 
show ?thesis  | 
|
810  | 
proof (cases "?DIV > 1")  | 
|
| 
32960
 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 
wenzelm 
parents: 
32920 
diff
changeset
 | 
811  | 
case True  | 
| 
58410
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
812  | 
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: 
32920 
diff
changeset
 | 
813  | 
from order_less_le_trans[OF arctan_ubound this, THEN less_imp_le]  | 
| 
58410
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
814  | 
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 | 815  | 
next  | 
| 
32960
 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 
wenzelm 
parents: 
32920 
diff
changeset
 | 
816  | 
case False  | 
| 47600 | 817  | 
hence "real ?DIV \<le> 1" by auto  | 
| 
32960
 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 
wenzelm 
parents: 
32920 
diff
changeset
 | 
818  | 
|
| 
44349
 
f057535311c5
remove redundant lemma real_0_le_divide_iff in favor or zero_le_divide_iff
 
huffman 
parents: 
44306 
diff
changeset
 | 
819  | 
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: 
32920 
diff
changeset
 | 
820  | 
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: 
32920 
diff
changeset
 | 
821  | 
|
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
822  | 
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: 
39556 
diff
changeset
 | 
823  | 
also have "\<dots> \<le> 2 * arctan (?DIV)"  | 
| 
32960
 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 
wenzelm 
parents: 
32920 
diff
changeset
 | 
824  | 
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: 
47108 
diff
changeset
 | 
825  | 
also have "\<dots> \<le> (Float 1 1 * ?ub_horner ?DIV)" unfolding Float_num  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
826  | 
using arctan_0_1_bounds_round[OF `0 \<le> real ?DIV` `real ?DIV \<le> 1`]  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
827  | 
by (auto intro!: float_round_up_le)  | 
| 
58410
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
828  | 
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 | 829  | 
qed  | 
830  | 
next  | 
|
831  | 
case False  | 
|
| 47600 | 832  | 
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: 
30971 
diff
changeset
 | 
833  | 
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: 
30971 
diff
changeset
 | 
834  | 
hence "0 < real x" by auto  | 
| 47600 | 835  | 
hence "0 < x" by auto  | 
| 29805 | 836  | 
|
837  | 
let "?invx" = "float_divl prec 1 x"  | 
|
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
838  | 
have "0 \<le> arctan x" using arctan_monotone'[OF `0 \<le> real x`] using arctan_tan[of 0, unfolded tan_zero] by auto  | 
| 29805 | 839  | 
|
| 
31098
 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 
hoelzl 
parents: 
30971 
diff
changeset
 | 
840  | 
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 | 841  | 
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: 
31148 
diff
changeset
 | 
842  | 
|
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
843  | 
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: 
39556 
diff
changeset
 | 
844  | 
|
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
845  | 
have "(?lb_horner ?invx) \<le> arctan (?invx)" using arctan_0_1_bounds_round[OF `0 \<le> real ?invx` `real ?invx \<le> 1`]  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
846  | 
by (auto intro!: float_round_down_le)  | 
| 
47601
 
050718fe6eee
use real :: float => real as lifting-morphism so we can directlry use the rep_eq theorems
 
hoelzl 
parents: 
47600 
diff
changeset
 | 
847  | 
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: 
39556 
diff
changeset
 | 
848  | 
finally have "arctan x \<le> pi / 2 - (?lb_horner ?invx)"  | 
| 
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
849  | 
using `0 \<le> arctan x` arctan_inverse[OF `1 / x \<noteq> 0`]  | 
| 
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
850  | 
unfolding real_sgn_pos[OF `0 < 1 / x`] le_diff_eq by auto  | 
| 29805 | 851  | 
moreover  | 
| 
58410
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
852  | 
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 | 853  | 
ultimately  | 
| 
58410
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
854  | 
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]  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
855  | 
by (auto intro!: float_round_up_le float_plus_up_le)  | 
| 29805 | 856  | 
qed  | 
857  | 
qed  | 
|
858  | 
qed  | 
|
859  | 
||
860  | 
lemma arctan_boundaries:  | 
|
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
861  | 
  "arctan x \<in> {(lb_arctan prec x) .. (ub_arctan prec x)}"
 | 
| 29805 | 862  | 
proof (cases "0 \<le> x")  | 
| 47600 | 863  | 
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: 
30971 
diff
changeset
 | 
864  | 
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 | 865  | 
next  | 
866  | 
let ?mx = "-x"  | 
|
| 47600 | 867  | 
case False hence "x < 0" and "0 \<le> real ?mx" by auto  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
868  | 
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: 
30971 
diff
changeset
 | 
869  | 
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: 
47600 
diff
changeset
 | 
870  | 
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: 
47600 
diff
changeset
 | 
871  | 
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: 
47108 
diff
changeset
 | 
872  | 
by (simp add: arctan_minus)  | 
| 29805 | 873  | 
qed  | 
874  | 
||
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
875  | 
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 | 876  | 
proof (rule allI, rule allI, rule allI, rule impI)  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
877  | 
fix x :: real fix lx ux  | 
| 
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
878  | 
  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: 
39556 
diff
changeset
 | 
879  | 
  hence l: "lb_arctan prec lx = l " and u: "ub_arctan prec ux = u" and x: "x \<in> {lx .. ux}" by auto
 | 
| 29805 | 880  | 
|
881  | 
  { from arctan_boundaries[of lx prec, unfolded l]
 | 
|
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
882  | 
have "l \<le> arctan lx" by (auto simp del: lb_arctan.simps)  | 
| 29805 | 883  | 
also have "\<dots> \<le> arctan x" using x by (auto intro: arctan_monotone')  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
884  | 
finally have "l \<le> arctan x" .  | 
| 29805 | 885  | 
} moreover  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
886  | 
  { have "arctan x \<le> arctan ux" using x by (auto intro: arctan_monotone')
 | 
| 
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
887  | 
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: 
39556 
diff
changeset
 | 
888  | 
finally have "arctan x \<le> u" .  | 
| 
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
889  | 
} ultimately show "l \<le> arctan x \<and> arctan x \<le> u" ..  | 
| 29805 | 890  | 
qed  | 
891  | 
||
892  | 
section "Sinus and Cosinus"  | 
|
893  | 
||
894  | 
subsection "Compute the cosinus and sinus series"  | 
|
895  | 
||
896  | 
fun ub_sin_cos_aux :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> float \<Rightarrow> float"  | 
|
897  | 
and lb_sin_cos_aux :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> float \<Rightarrow> float" where  | 
|
898  | 
"ub_sin_cos_aux prec 0 i k x = 0"  | 
|
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
899  | 
| "ub_sin_cos_aux prec (Suc n) i k x = float_plus_up prec  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
900  | 
(rapprox_rat prec 1 k) (-  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
901  | 
float_round_down prec (x * (lb_sin_cos_aux prec n (i + 2) (k * i * (i + 1)) x)))"  | 
| 29805 | 902  | 
| "lb_sin_cos_aux prec 0 i k x = 0"  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
903  | 
| "lb_sin_cos_aux prec (Suc n) i k x = float_plus_down prec  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
904  | 
(lapprox_rat prec 1 k) (-  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
905  | 
float_round_up prec (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: 
47600 
diff
changeset
 | 
906  | 
|
| 29805 | 907  | 
lemma cos_aux:  | 
| 
58410
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
908  | 
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")  | 
| 
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
909  | 
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 | 910  | 
proof -  | 
| 
47599
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
911  | 
have "0 \<le> real (x * x)" by auto  | 
| 29805 | 912  | 
let "?f n" = "fact (2 * n)"  | 
913  | 
||
| 31809 | 914  | 
  { fix n
 | 
| 
45129
 
1fce03e3e8ad
tuned proofs -- eliminated vacuous "induct arbitrary: ..." situations;
 
wenzelm 
parents: 
44821 
diff
changeset
 | 
915  | 
have F: "\<And>m. ((\<lambda>i. i + 2) ^^ n) m = m + 2 * n" by (induct n) auto  | 
| 30971 | 916  | 
have "?f (Suc n) = ?f n * ((\<lambda>i. i + 2) ^^ n) 1 * (((\<lambda>i. i + 2) ^^ n) 1 + 1)"  | 
| 29805 | 917  | 
unfolding F by auto } note f_eq = this  | 
| 31809 | 918  | 
|
919  | 
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: 
30971 
diff
changeset
 | 
920  | 
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: 
30971 
diff
changeset
 | 
921  | 
show "?lb" and "?ub" by (auto simp add: power_mult power2_eq_square[of "real x"])  | 
| 29805 | 922  | 
qed  | 
923  | 
||
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
924  | 
lemma lb_sin_cos_aux_zero_le_one: "lb_sin_cos_aux prec n i j 0 \<le> 1"  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
925  | 
by (cases j n rule: nat.exhaust[case_product nat.exhaust])  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
926  | 
(auto intro!: float_plus_down_le order_trans[OF lapprox_rat])  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
927  | 
|
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
928  | 
lemma one_le_ub_sin_cos_aux: "odd n \<Longrightarrow> 1 \<le> ub_sin_cos_aux prec n i (Suc 0) 0"  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
929  | 
by (cases n) (auto intro!: float_plus_up_le order_trans[OF _ rapprox_rat])  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
930  | 
|
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
931  | 
lemma cos_boundaries: assumes "0 \<le> real x" and "x \<le> pi / 2"  | 
| 
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
932  | 
  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: 
30971 
diff
changeset
 | 
933  | 
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: 
30971 
diff
changeset
 | 
934  | 
case False hence "real x \<noteq> 0" by auto  | 
| 47600 | 935  | 
hence "0 < x" and "0 < real x" using `0 \<le> real x` by auto  | 
| 56544 | 936  | 
have "0 < x * x" using `0 < x` by simp  | 
| 29805 | 937  | 
|
| 
58410
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
938  | 
  { fix x n have "(\<Sum> i=0..<n. (- 1) ^ i * (1/real (fact (2 * i))) * x ^ (2 * i))
 | 
| 
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
939  | 
= (\<Sum> i = 0 ..< 2 * n. (if even(i) then ((- 1) ^ (i div 2))/(real (fact i)) else 0) * x ^ i)" (is "?sum = ?ifsum")  | 
| 29805 | 940  | 
proof -  | 
941  | 
have "?sum = ?sum + (\<Sum> j = 0 ..< n. 0)" by auto  | 
|
| 31809 | 942  | 
also have "\<dots> =  | 
| 
58410
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
943  | 
(\<Sum> j = 0 ..< n. (- 1) ^ ((2 * j) div 2) / (real (fact (2 * j))) * x ^(2 * j)) + (\<Sum> j = 0 ..< n. 0)" by auto  | 
| 
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
944  | 
also have "\<dots> = (\<Sum> i = 0 ..< 2 * n. if even i then (- 1) ^ (i div 2) / (real (fact i)) * x ^ i else 0)"  | 
| 56195 | 945  | 
unfolding sum_split_even_odd atLeast0LessThan ..  | 
| 
58410
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
946  | 
also have "\<dots> = (\<Sum> i = 0 ..< 2 * n. (if even i then (- 1) ^ (i div 2) / (real (fact i)) else 0) * x ^ i)"  | 
| 57418 | 947  | 
by (rule setsum.cong) auto  | 
| 29805 | 948  | 
finally show ?thesis by assumption  | 
949  | 
qed } note morph_to_if_power = this  | 
|
950  | 
||
951  | 
||
952  | 
  { fix n :: nat assume "0 < n"
 | 
|
953  | 
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: 
30971 
diff
changeset
 | 
954  | 
obtain t where "0 < t" and "t < real x" and  | 
| 
58410
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
955  | 
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)  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
956  | 
+ (cos (t + 1/2 * (2 * n) * pi) / real (fact (2*n))) * (real x)^(2*n)"  | 
| 29805 | 957  | 
(is "_ = ?SUM + ?rest / ?fact * ?pow")  | 
| 
44306
 
33572a766836
fold definitions of sin_coeff and cos_coeff in Maclaurin lemmas
 
huffman 
parents: 
44305 
diff
changeset
 | 
958  | 
using Maclaurin_cos_expansion2[OF `0 < real x` `0 < 2 * n`]  | 
| 56195 | 959  | 
unfolding cos_coeff_def atLeast0LessThan by auto  | 
| 29805 | 960  | 
|
| 
58410
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
961  | 
have "cos t * (- 1) ^ n = cos t * cos (n * pi) + sin t * sin (n * pi)" by auto  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
962  | 
also have "\<dots> = cos (t + n * pi)" using cos_add by auto  | 
| 29805 | 963  | 
also have "\<dots> = ?rest" by auto  | 
| 
58410
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
964  | 
finally have "cos t * (- 1) ^ n = ?rest" .  | 
| 29805 | 965  | 
moreover  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
966  | 
have "t \<le> pi / 2" using `t < real x` and `x \<le> pi / 2` by auto  | 
| 29805 | 967  | 
hence "0 \<le> cos t" using `0 < t` and cos_ge_zero by auto  | 
968  | 
ultimately have even: "even n \<Longrightarrow> 0 \<le> ?rest" and odd: "odd n \<Longrightarrow> 0 \<le> - ?rest " by auto  | 
|
969  | 
||
970  | 
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: 
30971 
diff
changeset
 | 
971  | 
have "0 < ?pow" using `0 < real x` by auto  | 
| 29805 | 972  | 
|
973  | 
    {
 | 
|
974  | 
assume "even n"  | 
|
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
975  | 
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: 
32920 
diff
changeset
 | 
976  | 
unfolding morph_to_if_power[symmetric] using cos_aux by auto  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
977  | 
also have "\<dots> \<le> cos x"  | 
| 29805 | 978  | 
proof -  | 
| 
32960
 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 
wenzelm 
parents: 
32920 
diff
changeset
 | 
979  | 
from even[OF `even n`] `0 < ?fact` `0 < ?pow`  | 
| 
56571
 
f4635657d66f
added divide_nonneg_nonneg and co; made it a simp rule
 
hoelzl 
parents: 
56544 
diff
changeset
 | 
980  | 
have "0 \<le> (?rest / ?fact) * ?pow" by simp  | 
| 
32960
 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 
wenzelm 
parents: 
32920 
diff
changeset
 | 
981  | 
thus ?thesis unfolding cos_eq by auto  | 
| 29805 | 982  | 
qed  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
983  | 
finally have "(lb_sin_cos_aux prec n 1 1 (x * x)) \<le> cos x" .  | 
| 29805 | 984  | 
} note lb = this  | 
985  | 
||
986  | 
    {
 | 
|
987  | 
assume "odd n"  | 
|
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
988  | 
have "cos x \<le> ?SUM"  | 
| 29805 | 989  | 
proof -  | 
| 
32960
 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 
wenzelm 
parents: 
32920 
diff
changeset
 | 
990  | 
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: 
32920 
diff
changeset
 | 
991  | 
have "0 \<le> (- ?rest) / ?fact * ?pow"  | 
| 
 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 
wenzelm 
parents: 
32920 
diff
changeset
 | 
992  | 
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: 
32920 
diff
changeset
 | 
993  | 
thus ?thesis unfolding cos_eq by auto  | 
| 29805 | 994  | 
qed  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
995  | 
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: 
32920 
diff
changeset
 | 
996  | 
unfolding morph_to_if_power[symmetric] using cos_aux by auto  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
997  | 
finally have "cos x \<le> (ub_sin_cos_aux prec n 1 1 (x * x))" .  | 
| 29805 | 998  | 
} note ub = this and lb  | 
999  | 
} note ub = this(1) and lb = this(2)  | 
|
1000  | 
||
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1001  | 
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: 
39556 
diff
changeset
 | 
1002  | 
moreover have "(lb_sin_cos_aux prec (get_even n) 1 1 (x * x)) \<le> cos x"  | 
| 29805 | 1003  | 
proof (cases "0 < get_even n")  | 
1004  | 
case True show ?thesis using lb[OF True get_even] .  | 
|
1005  | 
next  | 
|
1006  | 
case False  | 
|
1007  | 
hence "get_even n = 0" by auto  | 
|
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1008  | 
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: 
39556 
diff
changeset
 | 
1009  | 
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: 
47600 
diff
changeset
 | 
1010  | 
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 | 1011  | 
qed  | 
1012  | 
ultimately show ?thesis by auto  | 
|
1013  | 
next  | 
|
1014  | 
case True  | 
|
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1015  | 
hence "x = 0"  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1016  | 
by transfer  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1017  | 
thus ?thesis  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1018  | 
using lb_sin_cos_aux_zero_le_one one_le_ub_sin_cos_aux  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1019  | 
by simp  | 
| 29805 | 1020  | 
qed  | 
1021  | 
||
| 
31098
 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 
hoelzl 
parents: 
30971 
diff
changeset
 | 
1022  | 
lemma sin_aux: assumes "0 \<le> real x"  | 
| 
58410
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
1023  | 
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")  | 
| 
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
1024  | 
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 | 1025  | 
proof -  | 
| 
47599
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
1026  | 
have "0 \<le> real (x * x)" by auto  | 
| 29805 | 1027  | 
let "?f n" = "fact (2 * n + 1)"  | 
1028  | 
||
| 31809 | 1029  | 
  { fix n
 | 
| 
45129
 
1fce03e3e8ad
tuned proofs -- eliminated vacuous "induct arbitrary: ..." situations;
 
wenzelm 
parents: 
44821 
diff
changeset
 | 
1030  | 
have F: "\<And>m. ((\<lambda>i. i + 2) ^^ n) m = m + 2 * n" by (induct n) auto  | 
| 30971 | 1031  | 
have "?f (Suc n) = ?f n * ((\<lambda>i. i + 2) ^^ n) 2 * (((\<lambda>i. i + 2) ^^ n) 2 + 1)"  | 
| 29805 | 1032  | 
unfolding F by auto } note f_eq = this  | 
| 31809 | 1033  | 
|
| 29805 | 1034  | 
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: 
30971 
diff
changeset
 | 
1035  | 
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: 
47108 
diff
changeset
 | 
1036  | 
show "?lb" and "?ub" using `0 \<le> real x`  | 
| 
57512
 
cc97b347b301
reduced name variants for assoc and commute on plus and mult
 
haftmann 
parents: 
57418 
diff
changeset
 | 
1037  | 
unfolding power_add power_one_right mult.assoc[symmetric] setsum_left_distrib[symmetric]  | 
| 
 
cc97b347b301
reduced name variants for assoc and commute on plus and mult
 
haftmann 
parents: 
57418 
diff
changeset
 | 
1038  | 
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: 
30971 
diff
changeset
 | 
1039  | 
by (auto intro!: mult_left_mono simp add: power_mult power2_eq_square[of "real x"])  | 
| 29805 | 1040  | 
qed  | 
1041  | 
||
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1042  | 
lemma sin_boundaries: assumes "0 \<le> real x" and "x \<le> pi / 2"  | 
| 
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1043  | 
  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: 
30971 
diff
changeset
 | 
1044  | 
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: 
30971 
diff
changeset
 | 
1045  | 
case False hence "real x \<noteq> 0" by auto  | 
| 47600 | 1046  | 
hence "0 < x" and "0 < real x" using `0 \<le> real x` by auto  | 
| 56544 | 1047  | 
have "0 < x * x" using `0 < x` by simp  | 
| 29805 | 1048  | 
|
| 
58410
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
1049  | 
  { 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))
 | 
| 
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
1050  | 
= (\<Sum> i = 0 ..< 2 * n. (if even(i) then 0 else ((- 1) ^ ((i - Suc 0) div 2))/(real (fact i))) * x ^ i)" (is "?SUM = _")  | 
| 29805 | 1051  | 
proof -  | 
1052  | 
have pow: "!!i. x ^ (2 * i + 1) = x * x ^ (2 * i)" by auto  | 
|
1053  | 
have "?SUM = (\<Sum> j = 0 ..< n. 0) + ?SUM" by auto  | 
|
| 
58410
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
1054  | 
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)"  | 
| 56195 | 1055  | 
unfolding sum_split_even_odd atLeast0LessThan ..  | 
| 
58410
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
1056  | 
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)"  | 
| 57418 | 1057  | 
by (rule setsum.cong) auto  | 
| 29805 | 1058  | 
finally show ?thesis by assumption  | 
1059  | 
qed } note setsum_morph = this  | 
|
1060  | 
||
1061  | 
  { fix n :: nat assume "0 < n"
 | 
|
1062  | 
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: 
30971 
diff
changeset
 | 
1063  | 
obtain t where "0 < t" and "t < real x" and  | 
| 
58410
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
1064  | 
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)  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1065  | 
+ (sin (t + 1/2 * (2 * n + 1) * pi) / real (fact (2*n + 1))) * (real x)^(2*n + 1)"  | 
| 29805 | 1066  | 
(is "_ = ?SUM + ?rest / ?fact * ?pow")  | 
| 
44306
 
33572a766836
fold definitions of sin_coeff and cos_coeff in Maclaurin lemmas
 
huffman 
parents: 
44305 
diff
changeset
 | 
1067  | 
using Maclaurin_sin_expansion3[OF `0 < 2 * n + 1` `0 < real x`]  | 
| 56195 | 1068  | 
unfolding sin_coeff_def atLeast0LessThan by auto  | 
| 29805 | 1069  | 
|
| 
58410
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
1070  | 
have "?rest = cos t * (- 1) ^ n" unfolding sin_add cos_add real_of_nat_add distrib_right distrib_left by auto  | 
| 29805 | 1071  | 
moreover  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1072  | 
have "t \<le> pi / 2" using `t < real x` and `x \<le> pi / 2` by auto  | 
| 29805 | 1073  | 
hence "0 \<le> cos t" using `0 < t` and cos_ge_zero by auto  | 
1074  | 
ultimately have even: "even n \<Longrightarrow> 0 \<le> ?rest" and odd: "odd n \<Longrightarrow> 0 \<le> - ?rest " by auto  | 
|
1075  | 
||
| 44305 | 1076  | 
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: 
30971 
diff
changeset
 | 
1077  | 
have "0 < ?pow" using `0 < real x` by (rule zero_less_power)  | 
| 29805 | 1078  | 
|
1079  | 
    {
 | 
|
1080  | 
assume "even n"  | 
|
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1081  | 
have "(x * lb_sin_cos_aux prec n 2 1 (x * x)) \<le>  | 
| 
58410
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
1082  | 
(\<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: 
32920 
diff
changeset
 | 
1083  | 
using sin_aux[OF `0 \<le> real x`] unfolding setsum_morph[symmetric] by auto  | 
| 29805 | 1084  | 
also have "\<dots> \<le> ?SUM" by auto  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1085  | 
also have "\<dots> \<le> sin x"  | 
| 29805 | 1086  | 
proof -  | 
| 
32960
 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 
wenzelm 
parents: 
32920 
diff
changeset
 | 
1087  | 
from even[OF `even n`] `0 < ?fact` `0 < ?pow`  | 
| 
56571
 
f4635657d66f
added divide_nonneg_nonneg and co; made it a simp rule
 
hoelzl 
parents: 
56544 
diff
changeset
 | 
1088  | 
have "0 \<le> (?rest / ?fact) * ?pow" by simp  | 
| 
32960
 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 
wenzelm 
parents: 
32920 
diff
changeset
 | 
1089  | 
thus ?thesis unfolding sin_eq by auto  | 
| 29805 | 1090  | 
qed  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1091  | 
finally have "(x * lb_sin_cos_aux prec n 2 1 (x * x)) \<le> sin x" .  | 
| 29805 | 1092  | 
} note lb = this  | 
1093  | 
||
1094  | 
    {
 | 
|
1095  | 
assume "odd n"  | 
|
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1096  | 
have "sin x \<le> ?SUM"  | 
| 29805 | 1097  | 
proof -  | 
| 
32960
 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 
wenzelm 
parents: 
32920 
diff
changeset
 | 
1098  | 
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: 
32920 
diff
changeset
 | 
1099  | 
have "0 \<le> (- ?rest) / ?fact * ?pow"  | 
| 
 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 
wenzelm 
parents: 
32920 
diff
changeset
 | 
1100  | 
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: 
32920 
diff
changeset
 | 
1101  | 
thus ?thesis unfolding sin_eq by auto  | 
| 29805 | 1102  | 
qed  | 
| 
58410
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
1103  | 
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: 
32920 
diff
changeset
 | 
1104  | 
by auto  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1105  | 
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: 
32920 
diff
changeset
 | 
1106  | 
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: 
39556 
diff
changeset
 | 
1107  | 
finally have "sin x \<le> (x * ub_sin_cos_aux prec n 2 1 (x * x))" .  | 
| 29805 | 1108  | 
} note ub = this and lb  | 
1109  | 
} note ub = this(1) and lb = this(2)  | 
|
1110  | 
||
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1111  | 
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: 
39556 
diff
changeset
 | 
1112  | 
moreover have "(x * lb_sin_cos_aux prec (get_even n) 2 1 (x * x)) \<le> sin x"  | 
| 29805 | 1113  | 
proof (cases "0 < get_even n")  | 
1114  | 
case True show ?thesis using lb[OF True get_even] .  | 
|
1115  | 
next  | 
|
1116  | 
case False  | 
|
1117  | 
hence "get_even n = 0" by auto  | 
|
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1118  | 
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: 
47600 
diff
changeset
 | 
1119  | 
show ?thesis unfolding `get_even n = 0` ub_sin_cos_aux.simps minus_float.rep_eq using sin_ge_zero by auto  | 
| 29805 | 1120  | 
qed  | 
1121  | 
ultimately show ?thesis by auto  | 
|
1122  | 
next  | 
|
1123  | 
case True  | 
|
1124  | 
show ?thesis  | 
|
1125  | 
proof (cases "n = 0")  | 
|
| 31809 | 1126  | 
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: 
30971 
diff
changeset
 | 
1127  | 
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 | 1128  | 
next  | 
1129  | 
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: 
30971 
diff
changeset
 | 
1130  | 
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 | 1131  | 
qed  | 
1132  | 
qed  | 
|
1133  | 
||
1134  | 
subsection "Compute the cosinus in the entire domain"  | 
|
1135  | 
||
1136  | 
definition lb_cos :: "nat \<Rightarrow> float \<Rightarrow> float" where  | 
|
1137  | 
"lb_cos prec x = (let  | 
|
1138  | 
horner = \<lambda> x. lb_sin_cos_aux prec (get_even (prec div 4 + 1)) 1 1 (x * x) ;  | 
|
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1139  | 
half = \<lambda> x. if x < 0 then - 1 else float_plus_down prec (Float 1 1 * x * x) (- 1)  | 
| 
58410
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
1140  | 
in if x < Float 1 (- 1) then horner x  | 
| 
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
1141  | 
else if x < 1 then half (horner (x * Float 1 (- 1)))  | 
| 
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
1142  | 
else half (half (horner (x * Float 1 (- 2)))))"  | 
| 29805 | 1143  | 
|
1144  | 
definition ub_cos :: "nat \<Rightarrow> float \<Rightarrow> float" where  | 
|
1145  | 
"ub_cos prec x = (let  | 
|
1146  | 
horner = \<lambda> x. ub_sin_cos_aux prec (get_odd (prec div 4 + 1)) 1 1 (x * x) ;  | 
|
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1147  | 
half = \<lambda> x. float_plus_up prec (Float 1 1 * x * x) (- 1)  | 
| 
58410
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
1148  | 
in if x < Float 1 (- 1) then horner x  | 
| 
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
1149  | 
else if x < 1 then half (horner (x * Float 1 (- 1)))  | 
| 
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
1150  | 
else half (half (horner (x * Float 1 (- 2)))))"  | 
| 29805 | 1151  | 
|
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1152  | 
lemma lb_cos: assumes "0 \<le> real x" and "x \<le> pi"  | 
| 
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1153  | 
  shows "cos x \<in> {(lb_cos prec x) .. (ub_cos prec x)}" (is "?cos x \<in> {(?lb x) .. (?ub x) }")
 | 
| 29805 | 1154  | 
proof -  | 
1155  | 
  { fix x :: real
 | 
|
1156  | 
have "cos x = cos (x / 2 + x / 2)" by auto  | 
|
1157  | 
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"  | 
|
1158  | 
unfolding cos_add by auto  | 
|
1159  | 
also have "\<dots> = 2 * cos (x / 2) * cos (x / 2) - 1" by algebra  | 
|
1160  | 
finally have "cos x = 2 * cos (x / 2) * cos (x / 2) - 1" .  | 
|
1161  | 
} note x_half = this[symmetric]  | 
|
1162  | 
||
| 47600 | 1163  | 
have "\<not> x < 0" using `0 \<le> real x` by auto  | 
| 29805 | 1164  | 
let "?ub_horner x" = "ub_sin_cos_aux prec (get_odd (prec div 4 + 1)) 1 1 (x * x)"  | 
1165  | 
let "?lb_horner x" = "lb_sin_cos_aux prec (get_even (prec div 4 + 1)) 1 1 (x * x)"  | 
|
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1166  | 
let "?ub_half x" = "float_plus_up prec (Float 1 1 * x * x) (- 1)"  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1167  | 
let "?lb_half x" = "if x < 0 then - 1 else float_plus_down prec (Float 1 1 * x * x) (- 1)"  | 
| 29805 | 1168  | 
|
1169  | 
show ?thesis  | 
|
| 
58410
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
1170  | 
proof (cases "x < Float 1 (- 1)")  | 
| 47600 | 1171  | 
case True hence "x \<le> pi / 2" using pi_ge_two by auto  | 
| 
58410
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
1172  | 
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: 
39556 
diff
changeset
 | 
1173  | 
using cos_boundaries[OF `0 \<le> real x` `x \<le> pi / 2`] .  | 
| 29805 | 1174  | 
next  | 
1175  | 
case False  | 
|
| 
58410
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
1176  | 
    { fix y x :: float let ?x2 = "(x * Float 1 (- 1))"
 | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1177  | 
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: 
47108 
diff
changeset
 | 
1178  | 
hence "- (pi / 2) \<le> ?x2" and "?x2 \<le> pi / 2" using pi_ge_two unfolding Float_num by auto  | 
| 29805 | 1179  | 
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: 
31148 
diff
changeset
 | 
1180  | 
|
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1181  | 
have "(?lb_half y) \<le> cos x"  | 
| 29805 | 1182  | 
proof (cases "y < 0")  | 
| 
32960
 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 
wenzelm 
parents: 
32920 
diff
changeset
 | 
1183  | 
case True show ?thesis using cos_ge_minus_one unfolding if_P[OF True] by auto  | 
| 29805 | 1184  | 
next  | 
| 
32960
 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 
wenzelm 
parents: 
32920 
diff
changeset
 | 
1185  | 
case False  | 
| 47600 | 1186  | 
hence "0 \<le> real y" by auto  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1187  | 
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: 
32920 
diff
changeset
 | 
1188  | 
have "real y * real y \<le> cos ?x2 * cos ?x2" .  | 
| 
 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 
wenzelm 
parents: 
32920 
diff
changeset
 | 
1189  | 
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: 
47108 
diff
changeset
 | 
1190  | 
hence "2 * real y * real y - 1 \<le> 2 * cos (x / 2) * cos (x / 2) - 1" unfolding Float_num by auto  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1191  | 
thus ?thesis unfolding if_not_P[OF False] x_half Float_num  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1192  | 
by (auto intro!: float_plus_down_le)  | 
| 29805 | 1193  | 
qed  | 
1194  | 
} 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: 
31148 
diff
changeset
 | 
1195  | 
|
| 
58410
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
1196  | 
    { fix y x :: float let ?x2 = "(x * Float 1 (- 1))"
 | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1197  | 
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: 
47108 
diff
changeset
 | 
1198  | 
hence "- (pi / 2) \<le> ?x2" and "?x2 \<le> pi / 2" using pi_ge_two unfolding Float_num by auto  | 
| 29805 | 1199  | 
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: 
31148 
diff
changeset
 | 
1200  | 
|
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1201  | 
have "cos x \<le> (?ub_half y)"  | 
| 29805 | 1202  | 
proof -  | 
| 
32960
 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 
wenzelm 
parents: 
32920 
diff
changeset
 | 
1203  | 
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: 
32920 
diff
changeset
 | 
1204  | 
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: 
32920 
diff
changeset
 | 
1205  | 
have "cos ?x2 * cos ?x2 \<le> real y * real y" .  | 
| 
 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 
wenzelm 
parents: 
32920 
diff
changeset
 | 
1206  | 
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: 
47108 
diff
changeset
 | 
1207  | 
hence "2 * cos (x / 2) * cos (x / 2) - 1 \<le> 2 * real y * real y - 1" unfolding Float_num by auto  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1208  | 
thus ?thesis unfolding x_half Float_num  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1209  | 
by (auto intro!: float_plus_up_le)  | 
| 29805 | 1210  | 
qed  | 
1211  | 
} 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: 
31148 
diff
changeset
 | 
1212  | 
|
| 
58410
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
1213  | 
let ?x2 = "x * Float 1 (- 1)"  | 
| 
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
1214  | 
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: 
31148 
diff
changeset
 | 
1215  | 
|
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1216  | 
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: 
31148 
diff
changeset
 | 
1217  | 
|
| 29805 | 1218  | 
show ?thesis  | 
1219  | 
proof (cases "x < 1")  | 
|
| 47600 | 1220  | 
case True hence "real x \<le> 1" by auto  | 
| 
47599
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
1221  | 
have "0 \<le> real ?x2" and "?x2 \<le> pi / 2" using pi_ge_two `0 \<le> real x` using assms by auto  | 
| 29805 | 1222  | 
from cos_boundaries[OF this]  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1223  | 
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: 
39556 
diff
changeset
 | 
1224  | 
|
| 
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1225  | 
have "(?lb x) \<le> ?cos x"  | 
| 29805 | 1226  | 
proof -  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1227  | 
from lb_half[OF lb `-pi \<le> x` `x \<le> pi`]  | 
| 
58410
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
1228  | 
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 | 1229  | 
qed  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1230  | 
moreover have "?cos x \<le> (?ub x)"  | 
| 29805 | 1231  | 
proof -  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1232  | 
from ub_half[OF ub `-pi \<le> x` `x \<le> pi`]  | 
| 
58410
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
1233  | 
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 | 1234  | 
qed  | 
1235  | 
ultimately show ?thesis by auto  | 
|
1236  | 
next  | 
|
1237  | 
case False  | 
|
| 
47599
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
1238  | 
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 | 1239  | 
from cos_boundaries[OF this]  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1240  | 
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: 
31148 
diff
changeset
 | 
1241  | 
|
| 
58410
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
1242  | 
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: 
31148 
diff
changeset
 | 
1243  | 
|
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1244  | 
have "(?lb x) \<le> ?cos x"  | 
| 29805 | 1245  | 
proof -  | 
| 
47599
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
1246  | 
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: 
39556 
diff
changeset
 | 
1247  | 
from lb_half[OF lb_half[OF lb this] `-pi \<le> x` `x \<le> pi`, unfolded eq_4]  | 
| 
58410
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
1248  | 
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 | 1249  | 
qed  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1250  | 
moreover have "?cos x \<le> (?ub x)"  | 
| 29805 | 1251  | 
proof -  | 
| 
47599
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
1252  | 
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: 
39556 
diff
changeset
 | 
1253  | 
from ub_half[OF ub_half[OF ub this] `-pi \<le> x` `x \<le> pi`, unfolded eq_4]  | 
| 
58410
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
1254  | 
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 | 1255  | 
qed  | 
1256  | 
ultimately show ?thesis by auto  | 
|
1257  | 
qed  | 
|
1258  | 
qed  | 
|
1259  | 
qed  | 
|
1260  | 
||
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1261  | 
lemma lb_cos_minus: assumes "-pi \<le> x" and "real x \<le> 0"  | 
| 
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1262  | 
  shows "cos (real(-x)) \<in> {(lb_cos prec (-x)) .. (ub_cos prec (-x))}"
 | 
| 29805 | 1263  | 
proof -  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1264  | 
have "0 \<le> real (-x)" and "(-x) \<le> pi" using `-pi \<le> x` `real x \<le> 0` by auto  | 
| 29805 | 1265  | 
from lb_cos[OF this] show ?thesis .  | 
1266  | 
qed  | 
|
1267  | 
||
| 
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: 
31148 
diff
changeset
 | 
1268  | 
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: 
31148 
diff
changeset
 | 
1269  | 
"bnds_cos prec lx ux = (let  | 
| 
47599
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
1270  | 
lpi = float_round_down prec (lb_pi prec) ;  | 
| 
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
1271  | 
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: 
31148 
diff
changeset
 | 
1272  | 
k = floor_fl (float_divr prec (lx + lpi) (2 * lpi)) ;  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1273  | 
lx = float_plus_down prec lx (- k * 2 * (if k < 0 then lpi else upi)) ;  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1274  | 
ux = float_plus_up prec ux (- k * 2 * (if k < 0 then upi else lpi))  | 
| 
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: 
31148 
diff
changeset
 | 
1275  | 
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: 
31148 
diff
changeset
 | 
1276  | 
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: 
31148 
diff
changeset
 | 
1277  | 
else if - lpi \<le> lx \<and> ux \<le> lpi then (min (lb_cos prec (-lx)) (lb_cos prec ux), Float 1 0)  | 
| 
58410
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
1278  | 
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))))  | 
| 
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
1279  | 
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)))  | 
| 
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
1280  | 
else (Float (- 1) 0, Float 1 0))"  | 
| 29805 | 1281  | 
|
| 
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: 
31148 
diff
changeset
 | 
1282  | 
lemma floor_int:  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1283  | 
obtains k :: int where "real k = (floor_fl f)"  | 
| 
47599
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
1284  | 
by (simp add: floor_fl_def)  | 
| 29805 | 1285  | 
|
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1286  | 
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: 
31148 
diff
changeset
 | 
1287  | 
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: 
31148 
diff
changeset
 | 
1288  | 
case (Suc n)  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1289  | 
have split_pi_off: "x + (Suc n) * (2 * pi) = (x + n * (2 * pi)) + 2 * pi"  | 
| 
49962
 
a8cc904a6820
Renamed {left,right}_distrib to distrib_{right,left}.
 
webertj 
parents: 
49351 
diff
changeset
 | 
1290  | 
unfolding Suc_eq_plus1 real_of_nat_add real_of_one distrib_right 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: 
31148 
diff
changeset
 | 
1291  | 
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: 
31148 
diff
changeset
 | 
1292  | 
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: 
31148 
diff
changeset
 | 
1293  | 
|
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1294  | 
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: 
31148 
diff
changeset
 | 
1295  | 
proof (cases "0 \<le> i")  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1296  | 
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: 
31148 
diff
changeset
 | 
1297  | 
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: 
31148 
diff
changeset
 | 
1298  | 
next  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1299  | 
case False hence i_nat: "i = - real (nat (-i))" by auto  | 
| 
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1300  | 
have "cos x = cos (x + i * (2 * pi) - i * (2 * pi))" by auto  | 
| 
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1301  | 
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: 
31148 
diff
changeset
 | 
1302  | 
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: 
31148 
diff
changeset
 | 
1303  | 
finally show ?thesis by auto  | 
| 29805 | 1304  | 
qed  | 
1305  | 
||
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1306  | 
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: 
31148 
diff
changeset
 | 
1307  | 
proof ((rule allI | rule impI | erule conjE) +)  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1308  | 
fix x :: real fix lx ux  | 
| 
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1309  | 
  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: 
31148 
diff
changeset
 | 
1310  | 
|
| 
47599
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
1311  | 
let ?lpi = "float_round_down prec (lb_pi prec)"  | 
| 
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
1312  | 
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: 
31148 
diff
changeset
 | 
1313  | 
let ?k = "floor_fl (float_divr prec (lx + ?lpi) (2 * ?lpi))"  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1314  | 
let ?lx2 = "(- ?k * 2 * (if ?k < 0 then ?lpi else ?upi))"  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1315  | 
let ?ux2 = "(- ?k * 2 * (if ?k < 0 then ?upi else ?lpi))"  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1316  | 
let ?lx = "float_plus_down prec lx ?lx2"  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1317  | 
let ?ux = "float_plus_up prec ux ?ux2"  | 
| 
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: 
31148 
diff
changeset
 | 
1318  | 
|
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1319  | 
obtain k :: int where k: "k = real ?k" using floor_int .  | 
| 
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1320  | 
|
| 
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1321  | 
have upi: "pi \<le> ?upi" and lpi: "?lpi \<le> pi"  | 
| 
47599
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
1322  | 
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: 
47108 
diff
changeset
 | 
1323  | 
float_round_down[of prec "lb_pi prec"] by auto  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1324  | 
hence "lx + ?lx2 \<le> x - k * (2 * pi) \<and> x - k * (2 * pi) \<le> ux + ?ux2"  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1325  | 
using x  | 
| 
47599
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
1326  | 
by (cases "k = 0")  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1327  | 
(auto intro!: add_mono  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1328  | 
simp add: k [symmetric] uminus_add_conv_diff [symmetric]  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1329  | 
simp del: float_of_numeral uminus_add_conv_diff)  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1330  | 
hence "?lx \<le> x - k * (2 * pi) \<and> x - k * (2 * pi) \<le> ?ux"  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1331  | 
by (auto intro!: float_plus_down_le float_plus_up_le)  | 
| 
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: 
31148 
diff
changeset
 | 
1332  | 
note lx = this[THEN conjunct1] and ux = this[THEN conjunct2]  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1333  | 
hence lx_less_ux: "?lx \<le> real ?ux" by (rule order_trans)  | 
| 
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1334  | 
|
| 
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1335  | 
  { 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: 
31148 
diff
changeset
 | 
1336  | 
with lpi[THEN le_imp_neg_le] lx  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1337  | 
have pi_lx: "- pi \<le> ?lx" and lx_0: "real ?lx \<le> 0"  | 
| 47600 | 1338  | 
by simp_all  | 
| 29805 | 1339  | 
|
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1340  | 
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: 
31148 
diff
changeset
 | 
1341  | 
using lb_cos_minus[OF pi_lx lx_0] by simp  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1342  | 
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: 
31148 
diff
changeset
 | 
1343  | 
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: 
47600 
diff
changeset
 | 
1344  | 
by (simp only: uminus_float.rep_eq real_of_int_minus  | 
| 
54230
 
b1d955791529
more simplification rules on unary and binary minus
 
haftmann 
parents: 
53077 
diff
changeset
 | 
1345  | 
cos_minus mult_minus_left) simp  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1346  | 
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: 
31148 
diff
changeset
 | 
1347  | 
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: 
31148 
diff
changeset
 | 
1348  | 
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: 
31148 
diff
changeset
 | 
1349  | 
|
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1350  | 
  { 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: 
31148 
diff
changeset
 | 
1351  | 
with lx  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1352  | 
have pi_lx: "?lx \<le> pi" and lx_0: "0 \<le> real ?lx"  | 
| 47600 | 1353  | 
by auto  | 
| 29805 | 1354  | 
|
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1355  | 
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: 
31148 
diff
changeset
 | 
1356  | 
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: 
47108 
diff
changeset
 | 
1357  | 
by (simp only: real_of_int_minus  | 
| 
54230
 
b1d955791529
more simplification rules on unary and binary minus
 
haftmann 
parents: 
53077 
diff
changeset
 | 
1358  | 
cos_minus mult_minus_left) simp  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1359  | 
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: 
31148 
diff
changeset
 | 
1360  | 
using lb_cos[OF lx_0 pi_lx] by simp  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1361  | 
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: 
31148 
diff
changeset
 | 
1362  | 
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: 
31148 
diff
changeset
 | 
1363  | 
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: 
31148 
diff
changeset
 | 
1364  | 
|
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1365  | 
  { 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: 
31148 
diff
changeset
 | 
1366  | 
with ux  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1367  | 
have pi_ux: "- pi \<le> ?ux" and ux_0: "real ?ux \<le> 0"  | 
| 47600 | 1368  | 
by simp_all  | 
| 29805 | 1369  | 
|
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1370  | 
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: 
31148 
diff
changeset
 | 
1371  | 
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: 
47600 
diff
changeset
 | 
1372  | 
by (simp only: uminus_float.rep_eq real_of_int_minus  | 
| 
54230
 
b1d955791529
more simplification rules on unary and binary minus
 
haftmann 
parents: 
53077 
diff
changeset
 | 
1373  | 
cos_minus mult_minus_left) simp  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1374  | 
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: 
31148 
diff
changeset
 | 
1375  | 
using lb_cos_minus[OF pi_ux ux_0, of prec] by simp  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1376  | 
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: 
31148 
diff
changeset
 | 
1377  | 
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: 
31148 
diff
changeset
 | 
1378  | 
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: 
31148 
diff
changeset
 | 
1379  | 
|
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1380  | 
  { 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: 
31148 
diff
changeset
 | 
1381  | 
with lpi ux  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1382  | 
have pi_ux: "?ux \<le> pi" and ux_0: "0 \<le> real ?ux"  | 
| 47600 | 1383  | 
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: 
31148 
diff
changeset
 | 
1384  | 
|
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1385  | 
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: 
31148 
diff
changeset
 | 
1386  | 
using lb_cos[OF ux_0 pi_ux] by simp  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1387  | 
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: 
31148 
diff
changeset
 | 
1388  | 
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: 
47108 
diff
changeset
 | 
1389  | 
by (simp only: real_of_int_minus  | 
| 
54230
 
b1d955791529
more simplification rules on unary and binary minus
 
haftmann 
parents: 
53077 
diff
changeset
 | 
1390  | 
cos_minus mult_minus_left) simp  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1391  | 
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: 
31148 
diff
changeset
 | 
1392  | 
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: 
31148 
diff
changeset
 | 
1393  | 
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: 
31148 
diff
changeset
 | 
1394  | 
|
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1395  | 
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: 
31148 
diff
changeset
 | 
1396  | 
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: 
31148 
diff
changeset
 | 
1397  | 
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: 
31148 
diff
changeset
 | 
1398  | 
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: 
31148 
diff
changeset
 | 
1399  | 
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: 
31148 
diff
changeset
 | 
1400  | 
by (auto simp add: bnds_cos_def Let_def)  | 
| 29805 | 1401  | 
|
| 
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: 
31148 
diff
changeset
 | 
1402  | 
from True lpi[THEN le_imp_neg_le] lx ux  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1403  | 
have "- pi \<le> x - k * (2 * pi)"  | 
| 
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1404  | 
and "x - k * (2 * pi) \<le> 0"  | 
| 47600 | 1405  | 
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: 
31148 
diff
changeset
 | 
1406  | 
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: 
31148 
diff
changeset
 | 
1407  | 
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: 
31148 
diff
changeset
 | 
1408  | 
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: 
31148 
diff
changeset
 | 
1409  | 
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: 
31148 
diff
changeset
 | 
1410  | 
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: 
31148 
diff
changeset
 | 
1411  | 
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: 
31148 
diff
changeset
 | 
1412  | 
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: 
31148 
diff
changeset
 | 
1413  | 
by (auto simp add: bnds_cos_def Let_def)  | 
| 29805 | 1414  | 
|
| 
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: 
31148 
diff
changeset
 | 
1415  | 
from True lpi lx ux  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1416  | 
have "0 \<le> x - k * (2 * pi)"  | 
| 
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1417  | 
and "x - k * (2 * pi) \<le> pi"  | 
| 47600 | 1418  | 
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: 
31148 
diff
changeset
 | 
1419  | 
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: 
31148 
diff
changeset
 | 
1420  | 
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: 
31148 
diff
changeset
 | 
1421  | 
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: 
31148 
diff
changeset
 | 
1422  | 
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: 
31148 
diff
changeset
 | 
1423  | 
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: 
31148 
diff
changeset
 | 
1424  | 
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: 
31148 
diff
changeset
 | 
1425  | 
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: 
31148 
diff
changeset
 | 
1426  | 
by (auto simp add: bnds_cos_def Let_def)  | 
| 29805 | 1427  | 
|
| 
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: 
31148 
diff
changeset
 | 
1428  | 
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: 
47108 
diff
changeset
 | 
1429  | 
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: 
47108 
diff
changeset
 | 
1430  | 
|
| 
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: 
31148 
diff
changeset
 | 
1431  | 
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: 
31148 
diff
changeset
 | 
1432  | 
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: 
31148 
diff
changeset
 | 
1433  | 
case True note Cond = this with bnds 1 2 3  | 
| 
58410
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
1434  | 
have l: "l = Float (- 1) 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: 
31148 
diff
changeset
 | 
1435  | 
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: 
31148 
diff
changeset
 | 
1436  | 
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: 
31148 
diff
changeset
 | 
1437  | 
|
| 
 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 
hoelzl 
parents: 
31148 
diff
changeset
 | 
1438  | 
have "cos x \<le> real u"  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1439  | 
proof (cases "x - k * (2 * pi) < pi")  | 
| 
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1440  | 
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: 
31148 
diff
changeset
 | 
1441  | 
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: 
31148 
diff
changeset
 | 
1442  | 
show ?thesis unfolding u by (simp add: real_of_float_max)  | 
| 29805 | 1443  | 
next  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1444  | 
case False hence "pi \<le> x - k * (2 * pi)" by simp  | 
| 
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1445  | 
hence pi_x: "- pi \<le> x - k * (2 * pi) - 2 * pi" by simp  | 
| 
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1446  | 
|
| 47600 | 1447  | 
have "?ux \<le> 2 * pi" using Cond lpi by auto  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1448  | 
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: 
31148 
diff
changeset
 | 
1449  | 
|
| 
 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 
hoelzl 
parents: 
31148 
diff
changeset
 | 
1450  | 
have ux_0: "real (?ux - 2 * ?lpi) \<le> 0"  | 
| 47600 | 1451  | 
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: 
31148 
diff
changeset
 | 
1452  | 
|
| 
 
f7d2aa438bee
Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
 
hoelzl 
parents: 
31148 
diff
changeset
 | 
1453  | 
from 2 and Cond have "\<not> ?ux \<le> ?lpi" by auto  | 
| 47600 | 1454  | 
hence "- ?lpi \<le> ?ux - 2 * ?lpi" by auto  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1455  | 
hence pi_ux: "- pi \<le> (?ux - 2 * ?lpi)"  | 
| 47600 | 1456  | 
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: 
31148 
diff
changeset
 | 
1457  | 
|
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1458  | 
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: 
32920 
diff
changeset
 | 
1459  | 
using ux lpi by auto  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1460  | 
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: 
32920 
diff
changeset
 | 
1461  | 
unfolding cos_periodic_int ..  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1462  | 
also have "\<dots> \<le> cos ((?ux - 2 * ?lpi))"  | 
| 
32960
 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 
wenzelm 
parents: 
32920 
diff
changeset
 | 
1463  | 
using cos_monotone_minus_pi_0'[OF pi_x x_le_ux ux_0]  | 
| 
54489
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54269 
diff
changeset
 | 
1464  | 
by (simp only: minus_float.rep_eq real_of_int_minus real_of_one  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54269 
diff
changeset
 | 
1465  | 
mult_minus_left mult_1_left) simp  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1466  | 
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: 
47600 
diff
changeset
 | 
1467  | 
unfolding uminus_float.rep_eq cos_minus ..  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1468  | 
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: 
32920 
diff
changeset
 | 
1469  | 
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: 
31148 
diff
changeset
 | 
1470  | 
finally show ?thesis unfolding u by (simp add: real_of_float_max)  | 
| 29805 | 1471  | 
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: 
31148 
diff
changeset
 | 
1472  | 
thus ?thesis unfolding l by auto  | 
| 31508 | 1473  | 
next case False note 4 = this show ?thesis  | 
1474  | 
proof (cases "-2 * ?lpi \<le> ?lx \<and> ?ux \<le> 0")  | 
|
1475  | 
case True note Cond = this with bnds 1 2 3 4  | 
|
| 
58410
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
1476  | 
have l: "l = Float (- 1) 0"  | 
| 31508 | 1477  | 
and u: "u = max (ub_cos prec (?lx + 2 * ?lpi)) (ub_cos prec (-?ux))"  | 
| 47600 | 1478  | 
by (auto simp add: bnds_cos_def Let_def)  | 
| 31508 | 1479  | 
|
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1480  | 
have "cos x \<le> u"  | 
| 
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1481  | 
proof (cases "-pi < x - k * (2 * pi)")  | 
| 
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1482  | 
case True hence "-pi \<le> x - k * (2 * pi)" by simp  | 
| 31508 | 1483  | 
from negative_ux[OF this Cond[THEN conjunct2]]  | 
1484  | 
show ?thesis unfolding u by (simp add: real_of_float_max)  | 
|
1485  | 
next  | 
|
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1486  | 
case False hence "x - k * (2 * pi) \<le> -pi" by simp  | 
| 
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1487  | 
hence pi_x: "x - k * (2 * pi) + 2 * pi \<le> pi" by simp  | 
| 
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1488  | 
|
| 47600 | 1489  | 
have "-2 * pi \<le> ?lx" using Cond lpi by auto  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1490  | 
|
| 
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1491  | 
hence "0 \<le> x - k * (2 * pi) + 2 * pi" using lx by simp  | 
| 31508 | 1492  | 
|
1493  | 
have lx_0: "0 \<le> real (?lx + 2 * ?lpi)"  | 
|
| 47600 | 1494  | 
using Cond lpi by auto  | 
| 31508 | 1495  | 
|
1496  | 
from 1 and Cond have "\<not> -?lpi \<le> ?lx" by auto  | 
|
| 47600 | 1497  | 
hence "?lx + 2 * ?lpi \<le> ?lpi" by auto  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1498  | 
hence pi_lx: "(?lx + 2 * ?lpi) \<le> pi"  | 
| 47600 | 1499  | 
using lpi[THEN le_imp_neg_le] by auto  | 
| 31508 | 1500  | 
|
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1501  | 
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: 
32920 
diff
changeset
 | 
1502  | 
using lx lpi by auto  | 
| 31508 | 1503  | 
|
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1504  | 
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: 
32920 
diff
changeset
 | 
1505  | 
unfolding cos_periodic_int ..  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1506  | 
also have "\<dots> \<le> cos ((?lx + 2 * ?lpi))"  | 
| 
32960
 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 
wenzelm 
parents: 
32920 
diff
changeset
 | 
1507  | 
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: 
47600 
diff
changeset
 | 
1508  | 
by (simp only: minus_float.rep_eq real_of_int_minus real_of_one  | 
| 
54489
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54269 
diff
changeset
 | 
1509  | 
mult_minus_left mult_1_left) simp  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1510  | 
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: 
32920 
diff
changeset
 | 
1511  | 
using lb_cos[OF lx_0 pi_lx] by simp  | 
| 31508 | 1512  | 
finally show ?thesis unfolding u by (simp add: real_of_float_max)  | 
1513  | 
qed  | 
|
1514  | 
thus ?thesis unfolding l by auto  | 
|
| 29805 | 1515  | 
next  | 
| 31508 | 1516  | 
case False with bnds 1 2 3 4 show ?thesis by (auto simp add: bnds_cos_def Let_def)  | 
1517  | 
qed qed qed qed qed  | 
|
| 29805 | 1518  | 
qed  | 
1519  | 
||
1520  | 
section "Exponential function"  | 
|
1521  | 
||
1522  | 
subsection "Compute the series of the exponential function"  | 
|
1523  | 
||
1524  | 
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  | 
|
1525  | 
"ub_exp_horner prec 0 i k x = 0" |  | 
|
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1526  | 
"ub_exp_horner prec (Suc n) i k x = float_plus_up prec  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1527  | 
(rapprox_rat prec 1 (int k)) (float_round_up prec (x * lb_exp_horner prec n (i + 1) (k * i) x))" |  | 
| 29805 | 1528  | 
"lb_exp_horner prec 0 i k x = 0" |  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1529  | 
"lb_exp_horner prec (Suc n) i k x = float_plus_down prec  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1530  | 
(lapprox_rat prec 1 (int k)) (float_round_down prec (x * ub_exp_horner prec n (i + 1) (k * i) x))"  | 
| 29805 | 1531  | 
|
| 
31098
 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 
hoelzl 
parents: 
30971 
diff
changeset
 | 
1532  | 
lemma bnds_exp_horner: assumes "real x \<le> 0"  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1533  | 
  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 | 1534  | 
proof -  | 
1535  | 
  { fix n
 | 
|
| 30971 | 1536  | 
have F: "\<And> m. ((\<lambda>i. i + 1) ^^ n) m = n + m" by (induct n, auto)  | 
1537  | 
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: 
31148 
diff
changeset
 | 
1538  | 
|
| 29805 | 1539  | 
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,  | 
1540  | 
OF assms f_eq lb_exp_horner.simps ub_exp_horner.simps]  | 
|
1541  | 
||
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1542  | 
  { 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 | 1543  | 
using bounds(1) by auto  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1544  | 
also have "\<dots> \<le> exp x"  | 
| 29805 | 1545  | 
proof -  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1546  | 
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)"  | 
| 56195 | 1547  | 
using Maclaurin_exp_le unfolding atLeast0LessThan 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: 
30971 
diff
changeset
 | 
1548  | 
moreover have "0 \<le> exp t / real (fact (get_even n)) * (real x) ^ (get_even n)"  | 
| 
56571
 
f4635657d66f
added divide_nonneg_nonneg and co; made it a simp rule
 
hoelzl 
parents: 
56544 
diff
changeset
 | 
1549  | 
by (auto simp: zero_le_even_power)  | 
| 56536 | 1550  | 
ultimately show ?thesis using get_odd exp_gt_zero by auto  | 
| 29805 | 1551  | 
qed  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1552  | 
finally have "lb_exp_horner prec (get_even n) 1 1 x \<le> exp x" .  | 
| 29805 | 1553  | 
} moreover  | 
| 31809 | 1554  | 
  {
 | 
| 
31098
 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 
hoelzl 
parents: 
30971 
diff
changeset
 | 
1555  | 
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: 
30971 
diff
changeset
 | 
1556  | 
proof (cases "real x = 0")  | 
| 29805 | 1557  | 
case True  | 
1558  | 
have "(get_odd n) \<noteq> 0" using get_odd[THEN odd_pos] by auto  | 
|
1559  | 
thus ?thesis unfolding True power_0_left by auto  | 
|
1560  | 
next  | 
|
| 
31098
 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 
hoelzl 
parents: 
30971 
diff
changeset
 | 
1561  | 
case False hence "real x < 0" using `real x \<le> 0` by auto  | 
| 46545 | 1562  | 
show ?thesis by (rule less_imp_le, auto simp add: power_less_zero_eq `real x < 0`)  | 
| 29805 | 1563  | 
qed  | 
1564  | 
||
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1565  | 
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)"  | 
| 56195 | 1566  | 
using Maclaurin_exp_le unfolding atLeast0LessThan 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: 
30971 
diff
changeset
 | 
1567  | 
moreover have "exp t / real (fact (get_odd n)) * (real x) ^ (get_odd n) \<le> 0"  | 
| 46545 | 1568  | 
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: 
39556 
diff
changeset
 | 
1569  | 
ultimately have "exp x \<le> (\<Sum>j = 0..<get_odd n. 1 / real (fact j) * real x ^ j)"  | 
| 56536 | 1570  | 
using get_odd exp_gt_zero by auto  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1571  | 
also have "\<dots> \<le> ub_exp_horner prec (get_odd n) 1 1 x"  | 
| 29805 | 1572  | 
using bounds(2) by auto  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1573  | 
finally have "exp x \<le> ub_exp_horner prec (get_odd n) 1 1 x" .  | 
| 29805 | 1574  | 
} ultimately show ?thesis by auto  | 
1575  | 
qed  | 
|
1576  | 
||
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1577  | 
lemma ub_exp_horner_nonneg: "real x \<le> 0 \<Longrightarrow> 0 \<le> real (ub_exp_horner prec (get_odd n) (Suc 0) (Suc 0) x)"  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1578  | 
using bnds_exp_horner[of x prec n]  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1579  | 
by (intro order_trans[OF exp_ge_zero]) auto  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1580  | 
|
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1581  | 
|
| 29805 | 1582  | 
subsection "Compute the exponential function on the entire domain"  | 
1583  | 
||
1584  | 
function ub_exp :: "nat \<Rightarrow> float \<Rightarrow> float" and lb_exp :: "nat \<Rightarrow> float \<Rightarrow> float" where  | 
|
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1585  | 
"lb_exp prec x =  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1586  | 
(if 0 < x then float_divl prec 1 (ub_exp prec (-x))  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1587  | 
else  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1588  | 
let  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1589  | 
horner = (\<lambda> x. let y = lb_exp_horner prec (get_even (prec + 2)) 1 1 x in  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1590  | 
if y \<le> 0 then Float 1 (- 2) else y)  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1591  | 
in  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1592  | 
if x < - 1 then  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1593  | 
power_down_fl prec (horner (float_divl prec x (- floor_fl x))) (nat (- int_floor_fl x))  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1594  | 
else horner x)" |  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1595  | 
"ub_exp prec x =  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1596  | 
(if 0 < x then float_divr prec 1 (lb_exp prec (-x))  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1597  | 
else if x < - 1 then  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1598  | 
power_up_fl prec  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1599  | 
(ub_exp_horner prec (get_odd (prec + 2)) 1 1  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1600  | 
(float_divr prec x (- floor_fl x))) (nat (- int_floor_fl x))  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1601  | 
else ub_exp_horner prec (get_odd (prec + 2)) 1 1 x)"  | 
| 29805 | 1602  | 
by pat_completeness auto  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1603  | 
termination  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1604  | 
by (relation "measure (\<lambda> v. let (prec, x) = case_sum id id v in (if 0 < x then 1 else 0))", auto)  | 
| 29805 | 1605  | 
|
1606  | 
lemma exp_m1_ge_quarter: "(1 / 4 :: real) \<le> exp (- 1)"  | 
|
1607  | 
proof -  | 
|
1608  | 
have eq4: "4 = Suc (Suc (Suc (Suc 0)))" by auto  | 
|
1609  | 
||
| 
58410
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
1610  | 
have "1 / 4 = (Float 1 (- 2))" unfolding Float_num by auto  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1611  | 
also have "\<dots> \<le> lb_exp_horner 3 (get_even 3) 1 1 (- 1)"  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1612  | 
by code_simp  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1613  | 
also have "\<dots> \<le> exp (- 1 :: float)" using bnds_exp_horner[where x="- 1"] by auto  | 
| 47600 | 1614  | 
finally show ?thesis by simp  | 
| 29805 | 1615  | 
qed  | 
1616  | 
||
1617  | 
lemma lb_exp_pos: assumes "\<not> 0 < x" shows "0 < lb_exp prec x"  | 
|
1618  | 
proof -  | 
|
1619  | 
let "?lb_horner x" = "lb_exp_horner prec (get_even (prec + 2)) 1 1 x"  | 
|
| 
58410
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
1620  | 
let "?horner x" = "let y = ?lb_horner x in if y \<le> 0 then Float 1 (- 2) else y"  | 
| 47600 | 1621  | 
have pos_horner: "\<And> x. 0 < ?horner x" unfolding Let_def by (cases "?lb_horner x \<le> 0", auto)  | 
| 29805 | 1622  | 
  moreover { fix x :: float fix num :: nat
 | 
| 47600 | 1623  | 
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: 
47108 
diff
changeset
 | 
1624  | 
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: 
30971 
diff
changeset
 | 
1625  | 
finally have "0 < real ((?horner x) ^ num)" .  | 
| 29805 | 1626  | 
}  | 
1627  | 
ultimately show ?thesis  | 
|
| 
30968
 
10fef94f40fc
adaptions due to rearrangment of power operation
 
haftmann 
parents: 
30952 
diff
changeset
 | 
1628  | 
unfolding lb_exp.simps if_not_P[OF `\<not> 0 < x`] Let_def  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1629  | 
by (cases "floor_fl x", cases "x < - 1", auto simp: real_power_up_fl real_power_down_fl  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1630  | 
intro!: power_up_less power_down_pos)  | 
| 29805 | 1631  | 
qed  | 
1632  | 
||
1633  | 
lemma exp_boundaries': assumes "x \<le> 0"  | 
|
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1634  | 
  shows "exp x \<in> { (lb_exp prec x) .. (ub_exp prec x)}"
 | 
| 29805 | 1635  | 
proof -  | 
1636  | 
let "?lb_exp_horner x" = "lb_exp_horner prec (get_even (prec + 2)) 1 1 x"  | 
|
1637  | 
let "?ub_exp_horner x" = "ub_exp_horner prec (get_odd (prec + 2)) 1 1 x"  | 
|
1638  | 
||
| 47600 | 1639  | 
have "real x \<le> 0" and "\<not> x > 0" using `x \<le> 0` by auto  | 
| 29805 | 1640  | 
show ?thesis  | 
1641  | 
proof (cases "x < - 1")  | 
|
| 47600 | 1642  | 
case False hence "- 1 \<le> real x" by auto  | 
| 29805 | 1643  | 
show ?thesis  | 
1644  | 
proof (cases "?lb_exp_horner x \<le> 0")  | 
|
| 47600 | 1645  | 
from `\<not> x < - 1` have "- 1 \<le> real x" by auto  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1646  | 
hence "exp (- 1) \<le> exp x" unfolding exp_le_cancel_iff .  | 
| 29805 | 1647  | 
from order_trans[OF exp_m1_ge_quarter this]  | 
| 
58410
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
1648  | 
have "Float 1 (- 2) \<le> exp x" unfolding Float_num .  | 
| 29805 | 1649  | 
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: 
30971 
diff
changeset
 | 
1650  | 
ultimately show ?thesis using bnds_exp_horner `real x \<le> 0` `\<not> x > 0` `\<not> x < - 1` by auto  | 
| 29805 | 1651  | 
next  | 
| 
31098
 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 
hoelzl 
parents: 
30971 
diff
changeset
 | 
1652  | 
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 | 1653  | 
qed  | 
1654  | 
next  | 
|
1655  | 
case True  | 
|
| 31809 | 1656  | 
|
| 
47599
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
1657  | 
let ?num = "nat (- int_floor_fl x)"  | 
| 
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
1658  | 
|
| 47600 | 1659  | 
have "real (int_floor_fl x) < - 1" using int_floor_fl[of x] `x < - 1`  | 
1660  | 
by simp  | 
|
| 
47599
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
1661  | 
hence "real (int_floor_fl x) < 0" by simp  | 
| 
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
1662  | 
hence "int_floor_fl x < 0" by auto  | 
| 
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
1663  | 
hence "1 \<le> - int_floor_fl x" by auto  | 
| 
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
1664  | 
hence "0 < nat (- int_floor_fl x)" by auto  | 
| 
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
1665  | 
hence "0 < ?num" by auto  | 
| 29805 | 1666  | 
hence "real ?num \<noteq> 0" by auto  | 
| 
47599
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
1667  | 
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: 
47108 
diff
changeset
 | 
1668  | 
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: 
47108 
diff
changeset
 | 
1669  | 
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: 
47108 
diff
changeset
 | 
1670  | 
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: 
47108 
diff
changeset
 | 
1671  | 
by (simp add: floor_fl_def int_floor_fl_def)  | 
| 
58982
 
27e7e3f9e665
simplified computations based on round_up by reducing to round_down;
 
immler 
parents: 
58889 
diff
changeset
 | 
1672  | 
from `0 < - int_floor_fl x` have "0 \<le> real (- floor_fl x)"  | 
| 
47599
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
1673  | 
by (simp add: floor_fl_def int_floor_fl_def)  | 
| 
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
1674  | 
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: 
47108 
diff
changeset
 | 
1675  | 
by (simp add: floor_fl_def int_floor_fl_def)  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1676  | 
have "exp x \<le> ub_exp prec x"  | 
| 29805 | 1677  | 
proof -  | 
| 31809 | 1678  | 
have div_less_zero: "real (float_divr prec x (- floor_fl x)) \<le> 0"  | 
| 
58982
 
27e7e3f9e665
simplified computations based on round_up by reducing to round_down;
 
immler 
parents: 
58889 
diff
changeset
 | 
1679  | 
using float_divr_nonpos_pos_upper_bound[OF `real x \<le> 0` `0 \<le> real (- floor_fl x)`]  | 
| 
47601
 
050718fe6eee
use real :: float => real as lifting-morphism so we can directlry use the rep_eq theorems
 
hoelzl 
parents: 
47600 
diff
changeset
 | 
1680  | 
unfolding less_eq_float_def zero_float.rep_eq .  | 
| 31809 | 1681  | 
|
| 
56479
 
91958d4b30f7
revert c1bbd3e22226, a14831ac3023, and 36489d77c484: divide_minus_left/right are again simp rules
 
hoelzl 
parents: 
56410 
diff
changeset
 | 
1682  | 
have "exp x = exp (?num * (x / ?num))" using `real ?num \<noteq> 0` by auto  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1683  | 
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: 
47108 
diff
changeset
 | 
1684  | 
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: 
32920 
diff
changeset
 | 
1685  | 
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: 
47108 
diff
changeset
 | 
1686  | 
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: 
47108 
diff
changeset
 | 
1687  | 
unfolding real_of_float_power  | 
| 
32960
 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 
wenzelm 
parents: 
32920 
diff
changeset
 | 
1688  | 
by (rule power_mono, rule bnds_exp_horner[OF div_less_zero, unfolded atLeastAtMost_iff, THEN conjunct2], auto)  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1689  | 
also have "\<dots> \<le> real (power_up_fl prec (?ub_exp_horner (float_divr prec x (- floor_fl x))) ?num)"  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1690  | 
by (auto simp add: real_power_up_fl intro!: power_up ub_exp_horner_nonneg div_less_zero)  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1691  | 
finally show ?thesis unfolding ub_exp.simps if_not_P[OF `\<not> 0 < x`] if_P[OF `x < - 1`] floor_fl_def Let_def  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1692  | 
.  | 
| 29805 | 1693  | 
qed  | 
| 31809 | 1694  | 
moreover  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1695  | 
have "lb_exp prec x \<le> exp x"  | 
| 29805 | 1696  | 
proof -  | 
| 
47599
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
1697  | 
let ?divl = "float_divl prec x (- floor_fl x)"  | 
| 29805 | 1698  | 
let ?horner = "?lb_exp_horner ?divl"  | 
| 31809 | 1699  | 
|
| 29805 | 1700  | 
show ?thesis  | 
1701  | 
proof (cases "?horner \<le> 0")  | 
|
| 47600 | 1702  | 
case False hence "0 \<le> real ?horner" by auto  | 
| 
32960
 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 
wenzelm 
parents: 
32920 
diff
changeset
 | 
1703  | 
|
| 
 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 
wenzelm 
parents: 
32920 
diff
changeset
 | 
1704  | 
have div_less_zero: "real (float_divl prec x (- floor_fl x)) \<le> 0"  | 
| 
56479
 
91958d4b30f7
revert c1bbd3e22226, a14831ac3023, and 36489d77c484: divide_minus_left/right are again simp rules
 
hoelzl 
parents: 
56410 
diff
changeset
 | 
1705  | 
using `real (floor_fl x) < 0` `real x \<le> 0` by (auto intro!: order_trans[OF float_divl] divide_nonpos_neg)  | 
| 
 
91958d4b30f7
revert c1bbd3e22226, a14831ac3023, and 36489d77c484: divide_minus_left/right are again simp rules
 
hoelzl 
parents: 
56410 
diff
changeset
 | 
1706  | 
|
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1707  | 
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: 
47108 
diff
changeset
 | 
1708  | 
exp (float_divl prec x (- floor_fl x)) ^ ?num"  | 
| 
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
1709  | 
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: 
47108 
diff
changeset
 | 
1710  | 
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: 
47600 
diff
changeset
 | 
1711  | 
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: 
39556 
diff
changeset
 | 
1712  | 
also have "\<dots> = exp (?num * (x / ?num))" unfolding exp_real_of_nat_mult ..  | 
| 
56479
 
91958d4b30f7
revert c1bbd3e22226, a14831ac3023, and 36489d77c484: divide_minus_left/right are again simp rules
 
hoelzl 
parents: 
56410 
diff
changeset
 | 
1713  | 
also have "\<dots> = exp x" using `real ?num \<noteq> 0` by auto  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1714  | 
finally show ?thesis using False  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1715  | 
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]  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1716  | 
by (auto simp: real_power_down_fl intro!: power_down_le)  | 
| 29805 | 1717  | 
next  | 
| 
32960
 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 
wenzelm 
parents: 
32920 
diff
changeset
 | 
1718  | 
case True  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1719  | 
have "power_down_fl prec (Float 1 (- 2)) ?num \<le> real (Float 1 (- 2)) ^ ?num"  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1720  | 
by (auto simp: real_power_down_fl power_down)  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1721  | 
also  | 
| 
32960
 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 
wenzelm 
parents: 
32920 
diff
changeset
 | 
1722  | 
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: 
32920 
diff
changeset
 | 
1723  | 
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: 
47600 
diff
changeset
 | 
1724  | 
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: 
32920 
diff
changeset
 | 
1725  | 
from order_trans[OF exp_m1_ge_quarter this[unfolded exp_le_cancel_iff[where x="- 1", symmetric]]]  | 
| 
58410
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
1726  | 
have "Float 1 (- 2) \<le> exp (x / (- floor_fl x))" unfolding Float_num .  | 
| 
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
1727  | 
hence "real (Float 1 (- 2)) ^ ?num \<le> exp (x / (- floor_fl x)) ^ ?num"  | 
| 46545 | 1728  | 
by (auto intro!: power_mono)  | 
| 
56479
 
91958d4b30f7
revert c1bbd3e22226, a14831ac3023, and 36489d77c484: divide_minus_left/right are again simp rules
 
hoelzl 
parents: 
56410 
diff
changeset
 | 
1729  | 
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: 
32920 
diff
changeset
 | 
1730  | 
finally show ?thesis  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1731  | 
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  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1732  | 
.  | 
| 29805 | 1733  | 
qed  | 
1734  | 
qed  | 
|
1735  | 
ultimately show ?thesis by auto  | 
|
1736  | 
qed  | 
|
1737  | 
qed  | 
|
1738  | 
||
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1739  | 
lemma exp_boundaries: "exp x \<in> { lb_exp prec x .. ub_exp prec x }"
 | 
| 29805 | 1740  | 
proof -  | 
1741  | 
show ?thesis  | 
|
1742  | 
proof (cases "0 < x")  | 
|
| 47600 | 1743  | 
case False hence "x \<le> 0" by auto  | 
| 29805 | 1744  | 
from exp_boundaries'[OF this] show ?thesis .  | 
1745  | 
next  | 
|
| 47600 | 1746  | 
case True hence "-x \<le> 0" by auto  | 
| 31809 | 1747  | 
|
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1748  | 
have "lb_exp prec x \<le> exp x"  | 
| 29805 | 1749  | 
proof -  | 
1750  | 
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: 
47600 
diff
changeset
 | 
1751  | 
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: 
39556 
diff
changeset
 | 
1752  | 
|
| 
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1753  | 
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: 
39556 
diff
changeset
 | 
1754  | 
also have "\<dots> \<le> exp x"  | 
| 
32960
 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 
wenzelm 
parents: 
32920 
diff
changeset
 | 
1755  | 
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: 
32920 
diff
changeset
 | 
1756  | 
unfolding exp_minus nonzero_inverse_inverse_eq[OF exp_not_eq_zero] inverse_eq_divide by auto  | 
| 29805 | 1757  | 
finally show ?thesis unfolding lb_exp.simps if_P[OF True] .  | 
1758  | 
qed  | 
|
1759  | 
moreover  | 
|
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1760  | 
have "exp x \<le> ub_exp prec x"  | 
| 29805 | 1761  | 
proof -  | 
| 47600 | 1762  | 
have "\<not> 0 < -x" using `0 < x` by auto  | 
| 31809 | 1763  | 
|
| 29805 | 1764  | 
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: 
47600 
diff
changeset
 | 
1765  | 
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: 
39556 
diff
changeset
 | 
1766  | 
|
| 
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1767  | 
have "exp x \<le> (1 :: float) / lb_exp prec (-x)"  | 
| 47600 | 1768  | 
using lb_exp lb_exp_pos[OF `\<not> 0 < -x`, of prec]  | 
1769  | 
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: 
39556 
diff
changeset
 | 
1770  | 
also have "\<dots> \<le> float_divr prec 1 (lb_exp prec (-x))" using float_divr .  | 
| 29805 | 1771  | 
finally show ?thesis unfolding ub_exp.simps if_P[OF True] .  | 
1772  | 
qed  | 
|
1773  | 
ultimately show ?thesis by auto  | 
|
1774  | 
qed  | 
|
1775  | 
qed  | 
|
1776  | 
||
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1777  | 
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 | 1778  | 
proof (rule allI, rule allI, rule allI, rule impI)  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1779  | 
fix x::real and lx ux  | 
| 
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1780  | 
  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: 
39556 
diff
changeset
 | 
1781  | 
  hence l: "lb_exp prec lx = l " and u: "ub_exp prec ux = u" and x: "x \<in> {lx .. ux}" by auto
 | 
| 29805 | 1782  | 
|
1783  | 
  { from exp_boundaries[of lx prec, unfolded l]
 | 
|
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1784  | 
have "l \<le> exp lx" by (auto simp del: lb_exp.simps)  | 
| 29805 | 1785  | 
also have "\<dots> \<le> exp x" using x by auto  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1786  | 
finally have "l \<le> exp x" .  | 
| 29805 | 1787  | 
} moreover  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1788  | 
  { have "exp x \<le> exp ux" using x by auto
 | 
| 
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1789  | 
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: 
39556 
diff
changeset
 | 
1790  | 
finally have "exp x \<le> u" .  | 
| 
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1791  | 
} ultimately show "l \<le> exp x \<and> exp x \<le> u" ..  | 
| 29805 | 1792  | 
qed  | 
1793  | 
||
1794  | 
section "Logarithm"  | 
|
1795  | 
||
1796  | 
subsection "Compute the logarithm series"  | 
|
1797  | 
||
| 31809 | 1798  | 
fun ub_ln_horner :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> float \<Rightarrow> float"  | 
| 29805 | 1799  | 
and lb_ln_horner :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> float \<Rightarrow> float" where  | 
1800  | 
"ub_ln_horner prec 0 i x = 0" |  | 
|
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1801  | 
"ub_ln_horner prec (Suc n) i x = float_plus_up prec  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1802  | 
(rapprox_rat prec 1 (int i)) (- float_round_down prec (x * lb_ln_horner prec n (Suc i) x))" |  | 
| 29805 | 1803  | 
"lb_ln_horner prec 0 i x = 0" |  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1804  | 
"lb_ln_horner prec (Suc n) i x = float_plus_down prec  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1805  | 
(lapprox_rat prec 1 (int i)) (- float_round_up prec (x * ub_ln_horner prec n (Suc i) x))"  | 
| 29805 | 1806  | 
|
1807  | 
lemma ln_bounds:  | 
|
1808  | 
assumes "0 \<le> x" and "x < 1"  | 
|
| 
58410
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
1809  | 
shows "(\<Sum>i=0..<2*n. (- 1) ^ i * (1 / real (i + 1)) * x ^ (Suc i)) \<le> ln (x + 1)" (is "?lb")  | 
| 
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
1810  | 
and "ln (x + 1) \<le> (\<Sum>i=0..<2*n + 1. (- 1) ^ i * (1 / real (i + 1)) * x ^ (Suc i))" (is "?ub")  | 
| 29805 | 1811  | 
proof -  | 
| 
30952
 
7ab2716dd93b
power operation on functions with syntax o^; power operation on relations with syntax ^^
 
haftmann 
parents: 
30886 
diff
changeset
 | 
1812  | 
let "?a n" = "(1/real (n +1)) * x ^ (Suc n)"  | 
| 29805 | 1813  | 
|
| 
58410
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
1814  | 
have ln_eq: "(\<Sum> i. (- 1) ^ i * ?a i) = ln (x + 1)"  | 
| 29805 | 1815  | 
using ln_series[of "x + 1"] `0 \<le> x` `x < 1` by auto  | 
1816  | 
||
1817  | 
have "norm x < 1" using assms by auto  | 
|
| 31809 | 1818  | 
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: 
44349 
diff
changeset
 | 
1819  | 
using tendsto_mult[OF LIMSEQ_inverse_real_of_nat LIMSEQ_Suc[OF LIMSEQ_power_zero[OF `norm x < 1`]]] by auto  | 
| 56536 | 1820  | 
  { fix n have "0 \<le> ?a n" by (rule mult_nonneg_nonneg, auto simp: `0 \<le> x`) }
 | 
| 29805 | 1821  | 
  { fix n have "?a (Suc n) \<le> ?a n" unfolding inverse_eq_divide[symmetric]
 | 
1822  | 
proof (rule mult_mono)  | 
|
| 56536 | 1823  | 
show "0 \<le> x ^ Suc (Suc n)" by (auto simp add: `0 \<le> x`)  | 
| 
57512
 
cc97b347b301
reduced name variants for assoc and commute on plus and mult
 
haftmann 
parents: 
57418 
diff
changeset
 | 
1824  | 
have "x ^ Suc (Suc n) \<le> x ^ Suc n * 1" unfolding power_Suc2 mult.assoc[symmetric]  | 
| 56536 | 1825  | 
by (rule mult_left_mono, fact less_imp_le[OF `x < 1`], auto simp: `0 \<le> x`)  | 
| 29805 | 1826  | 
thus "x ^ Suc (Suc n) \<le> x ^ Suc n" by auto  | 
1827  | 
qed auto }  | 
|
1828  | 
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]  | 
|
| 56195 | 1829  | 
show "?lb" and "?ub" unfolding atLeast0LessThan by auto  | 
| 29805 | 1830  | 
qed  | 
1831  | 
||
| 31809 | 1832  | 
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: 
30971 
diff
changeset
 | 
1833  | 
assumes "0 \<le> real x" and "real x < 1"  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1834  | 
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: 
39556 
diff
changeset
 | 
1835  | 
and "ln (x + 1) \<le> x * ub_ln_horner prec (get_odd n) 1 x" (is "?ln \<le> ?ub")  | 
| 29805 | 1836  | 
proof -  | 
1837  | 
obtain ev where ev: "get_even n = 2 * ev" using get_even_double ..  | 
|
1838  | 
obtain od where od: "get_odd n = 2 * od + 1" using get_odd_double ..  | 
|
1839  | 
||
| 
58410
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
1840  | 
let "?s n" = "(- 1) ^ n * (1 / real (1 + n)) * (real x)^(Suc n)"  | 
| 29805 | 1841  | 
|
| 
57512
 
cc97b347b301
reduced name variants for assoc and commute on plus and mult
 
haftmann 
parents: 
57418 
diff
changeset
 | 
1842  | 
  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 | 1843  | 
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: 
30971 
diff
changeset
 | 
1844  | 
OF `0 \<le> real x` refl lb_ln_horner.simps ub_ln_horner.simps] `0 \<le> real x`  | 
| 29805 | 1845  | 
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: 
30971 
diff
changeset
 | 
1846  | 
also have "\<dots> \<le> ?ln" using ln_bounds(1)[OF `0 \<le> real x` `real x < 1`] by auto  | 
| 31809 | 1847  | 
finally show "?lb \<le> ?ln" .  | 
| 29805 | 1848  | 
|
| 
31098
 
73dd67adf90a
replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
 
hoelzl 
parents: 
30971 
diff
changeset
 | 
1849  | 
  have "?ln \<le> setsum ?s {0 ..< 2 * od + 1}" using ln_bounds(2)[OF `0 \<le> real x` `real x < 1`] by auto
 | 
| 
57512
 
cc97b347b301
reduced name variants for assoc and commute on plus and mult
 
haftmann 
parents: 
57418 
diff
changeset
 | 
1850  | 
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 | 1851  | 
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: 
30971 
diff
changeset
 | 
1852  | 
OF `0 \<le> real x` refl lb_ln_horner.simps ub_ln_horner.simps] `0 \<le> real x`  | 
| 29805 | 1853  | 
by (rule mult_right_mono)  | 
| 31809 | 1854  | 
finally show "?ln \<le> ?ub" .  | 
| 29805 | 1855  | 
qed  | 
1856  | 
||
1857  | 
lemma ln_add: assumes "0 < x" and "0 < y" shows "ln (x + y) = ln x + ln (1 + y / x)"  | 
|
1858  | 
proof -  | 
|
1859  | 
have "x \<noteq> 0" using assms by auto  | 
|
| 
49962
 
a8cc904a6820
Renamed {left,right}_distrib to distrib_{right,left}.
 
webertj 
parents: 
49351 
diff
changeset
 | 
1860  | 
have "x + y = x * (1 + y / x)" unfolding distrib_left times_divide_eq_right nonzero_mult_divide_cancel_left[OF `x \<noteq> 0`] by auto  | 
| 31809 | 1861  | 
moreover  | 
| 56541 | 1862  | 
have "0 < y / x" using assms by auto  | 
| 29805 | 1863  | 
hence "0 < 1 + y / x" by auto  | 
1864  | 
ultimately show ?thesis using ln_mult assms by auto  | 
|
1865  | 
qed  | 
|
1866  | 
||
1867  | 
subsection "Compute the logarithm of 2"  | 
|
1868  | 
||
| 31809 | 1869  | 
definition ub_ln2 where "ub_ln2 prec = (let third = rapprox_rat (max prec 1) 1 3  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1870  | 
in float_plus_up prec  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1871  | 
((Float 1 (- 1) * ub_ln_horner prec (get_odd prec) 1 (Float 1 (- 1))))  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1872  | 
(float_round_up prec (third * ub_ln_horner prec (get_odd prec) 1 third)))"  | 
| 31809 | 1873  | 
definition lb_ln2 where "lb_ln2 prec = (let third = lapprox_rat prec 1 3  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1874  | 
in float_plus_down prec  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1875  | 
((Float 1 (- 1) * lb_ln_horner prec (get_even prec) 1 (Float 1 (- 1))))  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1876  | 
(float_round_down prec (third * lb_ln_horner prec (get_even prec) 1 third)))"  | 
| 29805 | 1877  | 
|
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1878  | 
lemma ub_ln2: "ln 2 \<le> ub_ln2 prec" (is "?ub_ln2")  | 
| 
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1879  | 
and lb_ln2: "lb_ln2 prec \<le> ln 2" (is "?lb_ln2")  | 
| 29805 | 1880  | 
proof -  | 
1881  | 
let ?uthird = "rapprox_rat (max prec 1) 1 3"  | 
|
1882  | 
let ?lthird = "lapprox_rat prec 1 3"  | 
|
1883  | 
||
1884  | 
have ln2_sum: "ln 2 = ln (1/2 + 1) + ln (1 / 3 + 1)"  | 
|
1885  | 
using ln_add[of "3 / 2" "1 / 2"] by auto  | 
|
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1886  | 
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: 
30971 
diff
changeset
 | 
1887  | 
hence lb3_ub: "real ?lthird < 1" by auto  | 
| 
47599
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
1888  | 
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: 
39556 
diff
changeset
 | 
1889  | 
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: 
30971 
diff
changeset
 | 
1890  | 
hence ub3_lb: "0 \<le> real ?uthird" by auto  | 
| 29805 | 1891  | 
|
| 
58410
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
1892  | 
have lb2: "0 \<le> real (Float 1 (- 1))" and ub2: "real (Float 1 (- 1)) < 1" unfolding Float_num by auto  | 
| 29805 | 1893  | 
|
1894  | 
have "0 \<le> (1::int)" and "0 < (3::int)" by auto  | 
|
| 
58982
 
27e7e3f9e665
simplified computations based on round_up by reducing to round_down;
 
immler 
parents: 
58889 
diff
changeset
 | 
1895  | 
have ub3_ub: "real ?uthird < 1"  | 
| 
 
27e7e3f9e665
simplified computations based on round_up by reducing to round_down;
 
immler 
parents: 
58889 
diff
changeset
 | 
1896  | 
by (simp add: Float.compute_rapprox_rat Float.compute_lapprox_rat rapprox_posrat_less1)  | 
| 29805 | 1897  | 
|
1898  | 
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: 
30971 
diff
changeset
 | 
1899  | 
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: 
30971 
diff
changeset
 | 
1900  | 
have lthird_gt0: "0 < real ?lthird + 1" using lb3_lb by auto  | 
| 29805 | 1901  | 
|
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1902  | 
show ?ub_ln2 unfolding ub_ln2_def Let_def ln2_sum Float_num(4)[symmetric]  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1903  | 
proof (rule float_plus_up_le, 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: 
30971 
diff
changeset
 | 
1904  | 
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: 
39556 
diff
changeset
 | 
1905  | 
also have "\<dots> \<le> ?uthird * ub_ln_horner prec (get_odd prec) 1 ?uthird"  | 
| 29805 | 1906  | 
using ln_float_bounds(2)[OF ub3_lb ub3_ub] .  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1907  | 
also note float_round_up  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1908  | 
finally show "ln (1 / 3 + 1) \<le> float_round_up prec (?uthird * ub_ln_horner prec (get_odd prec) 1 ?uthird)" .  | 
| 29805 | 1909  | 
qed  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1910  | 
show ?lb_ln2 unfolding lb_ln2_def Let_def ln2_sum Float_num(4)[symmetric]  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1911  | 
proof (rule float_plus_down_le, rule add_mono, fact ln_float_bounds(1)[OF lb2 ub2])  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
1912  | 
have "?lthird * lb_ln_horner prec (get_even prec) 1 ?lthird \<le> ln (real ?lthird + 1)"  | 
| 29805 | 1913  | 
using ln_float_bounds(1)[OF lb3_lb lb3_ub] .  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1914  | 
note float_round_down_le[OF this]  | 
| 29805 | 1915  | 
also have "\<dots> \<le> ln (1 / 3 + 1)" unfolding ln_le_cancel_iff[OF lthird_gt0 third_gt0] using lb3 by auto  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1916  | 
finally show "float_round_down prec (?lthird * lb_ln_horner prec (get_even prec) 1 ?lthird) \<le> ln (1 / 3 + 1)" .  | 
| 29805 | 1917  | 
qed  | 
1918  | 
qed  | 
|
1919  | 
||
1920  | 
subsection "Compute the logarithm in the entire domain"  | 
|
1921  | 
||
1922  | 
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: 
31467 
diff
changeset
 | 
1923  | 
"ub_ln prec x = (if x \<le> 0 then None  | 
| 
 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 
hoelzl 
parents: 
31467 
diff
changeset
 | 
1924  | 
else if x < 1 then Some (- the (lb_ln prec (float_divl (max prec 1) 1 x)))  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1925  | 
else let horner = \<lambda>x. float_round_up prec (x * ub_ln_horner prec (get_odd prec) 1 x) in  | 
| 
58410
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
1926  | 
if x \<le> Float 3 (- 1) then Some (horner (x - 1))  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1927  | 
else if x < Float 1 1 then Some (float_round_up prec (horner (Float 1 (- 1)) + horner (x * rapprox_rat prec 2 3 - 1)))  | 
| 
31468
 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 
hoelzl 
parents: 
31467 
diff
changeset
 | 
1928  | 
else let l = bitlen (mantissa x) - 1 in  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1929  | 
Some (float_plus_up prec (float_round_up prec (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: 
31467 
diff
changeset
 | 
1930  | 
"lb_ln prec x = (if x \<le> 0 then None  | 
| 
 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 
hoelzl 
parents: 
31467 
diff
changeset
 | 
1931  | 
else if x < 1 then Some (- the (ub_ln prec (float_divr prec 1 x)))  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1932  | 
else let horner = \<lambda>x. float_round_down prec (x * lb_ln_horner prec (get_even prec) 1 x) in  | 
| 
58410
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
1933  | 
if x \<le> Float 3 (- 1) then Some (horner (x - 1))  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1934  | 
else if x < Float 1 1 then Some (float_round_down prec (horner (Float 1 (- 1)) +  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1935  | 
horner (max (x * lapprox_rat prec 2 3 - 1) 0)))  | 
| 
31468
 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 
hoelzl 
parents: 
31467 
diff
changeset
 | 
1936  | 
else let l = bitlen (mantissa x) - 1 in  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1937  | 
Some (float_plus_down prec (float_round_down prec (lb_ln2 prec * (Float (exponent x + l) 0))) (horner (Float (mantissa x) (- l) - 1))))"  | 
| 29805 | 1938  | 
by pat_completeness auto  | 
1939  | 
||
| 
55414
 
eab03e9cee8a
renamed '{prod,sum,bool,unit}_case' to 'case_...'
 
blanchet 
parents: 
55413 
diff
changeset
 | 
1940  | 
termination proof (relation "measure (\<lambda> v. let (prec, x) = case_sum id id v in (if x < 1 then 1 else 0))", auto)  | 
| 47600 | 1941  | 
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"  | 
1942  | 
hence "0 < real x" "1 \<le> max prec (Suc 0)" "real x < 1" by auto  | 
|
| 
58982
 
27e7e3f9e665
simplified computations based on round_up by reducing to round_down;
 
immler 
parents: 
58889 
diff
changeset
 | 
1943  | 
from float_divl_pos_less1_bound[OF `0 < real x` `real x < 1`[THEN less_imp_le] `1 \<le> max prec (Suc 0)`]  | 
| 47600 | 1944  | 
show False using `real (float_divl (max prec (Suc 0)) 1 x) < 1` by auto  | 
| 29805 | 1945  | 
next  | 
| 47600 | 1946  | 
fix prec x assume "\<not> real x \<le> 0" and "real x < 1" and "real (float_divr prec 1 x) < 1"  | 
1947  | 
hence "0 < x" by auto  | 
|
1948  | 
from float_divr_pos_less1_lower_bound[OF `0 < x`, of prec] `real x < 1`  | 
|
1949  | 
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: 
47108 
diff
changeset
 | 
1950  | 
qed  | 
| 
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
1951  | 
|
| 
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
1952  | 
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: 
47108 
diff
changeset
 | 
1953  | 
apply (subst Float_mantissa_exponent[of x, symmetric])  | 
| 
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
1954  | 
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: 
47108 
diff
changeset
 | 
1955  | 
using powr_gt_zero[of 2 "exponent x"]  | 
| 
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
1956  | 
apply simp  | 
| 
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
1957  | 
done  | 
| 
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
1958  | 
|
| 
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
1959  | 
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: 
47108 
diff
changeset
 | 
1960  | 
using powr_gt_zero[of 2 "e"]  | 
| 54269 | 1961  | 
by (auto simp add: zero_less_mult_iff zero_float_def simp del: powr_gt_zero dest: less_zeroE)  | 
| 
47599
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
1962  | 
|
| 
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
1963  | 
lemma Float_representation_aux:  | 
| 
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
1964  | 
fixes m e  | 
| 
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
1965  | 
defines "x \<equiv> Float m e"  | 
| 
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
1966  | 
assumes "x > 0"  | 
| 
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
1967  | 
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: 
47108 
diff
changeset
 | 
1968  | 
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: 
47108 
diff
changeset
 | 
1969  | 
proof -  | 
| 
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
1970  | 
from assms have mantissa_pos: "m > 0" "mantissa x > 0"  | 
| 47600 | 1971  | 
using Float_pos_eq_mantissa_pos[of m e] float_pos_eq_mantissa_pos[of x] by simp_all  | 
1972  | 
thus ?th1 using bitlen_Float[of m e] assms by (auto simp add: zero_less_mult_iff intro!: arg_cong2[where f=Float])  | 
|
1973  | 
have "x \<noteq> float_of 0"  | 
|
1974  | 
unfolding zero_float_def[symmetric] using `0 < x` by auto  | 
|
| 
47599
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
1975  | 
from denormalize_shift[OF assms(1) this] guess i . note i = this  | 
| 47600 | 1976  | 
|
| 
47599
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
1977  | 
have "2 powr (1 - (real (bitlen (mantissa x)) + real i)) =  | 
| 
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
1978  | 
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: 
47108 
diff
changeset
 | 
1979  | 
by (simp add: powr_minus[symmetric] powr_add[symmetric] field_simps)  | 
| 
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
1980  | 
hence "real (mantissa x) * 2 powr (1 - real (bitlen (mantissa x))) =  | 
| 
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
1981  | 
(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: 
47108 
diff
changeset
 | 
1982  | 
using `mantissa x > 0` by (simp add: powr_realpow)  | 
| 47600 | 1983  | 
then show ?th2  | 
1984  | 
unfolding i by transfer auto  | 
|
| 
47599
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
1985  | 
qed  | 
| 
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
1986  | 
|
| 
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
1987  | 
lemma compute_ln[code]:  | 
| 
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
1988  | 
fixes m e  | 
| 
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
1989  | 
defines "x \<equiv> Float m e"  | 
| 
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
1990  | 
shows "ub_ln prec x = (if x \<le> 0 then None  | 
| 
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
1991  | 
else if x < 1 then Some (- the (lb_ln prec (float_divl (max prec 1) 1 x)))  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1992  | 
else let horner = \<lambda>x. float_round_up prec (x * ub_ln_horner prec (get_odd prec) 1 x) in  | 
| 
58410
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
1993  | 
if x \<le> Float 3 (- 1) then Some (horner (x - 1))  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1994  | 
else if x < Float 1 1 then Some (float_round_up prec (horner (Float 1 (- 1)) + horner (x * rapprox_rat prec 2 3 - 1)))  | 
| 
47599
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
1995  | 
else let l = bitlen m - 1 in  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
1996  | 
Some (float_plus_up prec (float_round_up prec (ub_ln2 prec * (Float (e + l) 0))) (horner (Float m (- l) - 1))))"  | 
| 
47599
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
1997  | 
(is ?th1)  | 
| 
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
1998  | 
and "lb_ln prec x = (if x \<le> 0 then None  | 
| 
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
1999  | 
else if x < 1 then Some (- the (ub_ln prec (float_divr prec 1 x)))  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
2000  | 
else let horner = \<lambda>x. float_round_down prec (x * lb_ln_horner prec (get_even prec) 1 x) in  | 
| 
58410
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
2001  | 
if x \<le> Float 3 (- 1) then Some (horner (x - 1))  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
2002  | 
else if x < Float 1 1 then Some (float_round_down prec (horner (Float 1 (- 1)) +  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
2003  | 
horner (max (x * lapprox_rat prec 2 3 - 1) 0)))  | 
| 
47599
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
2004  | 
else let l = bitlen m - 1 in  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
2005  | 
Some (float_plus_down prec (float_round_down prec (lb_ln2 prec * (Float (e + l) 0))) (horner (Float m (- l) - 1))))"  | 
| 
47599
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
2006  | 
(is ?th2)  | 
| 
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
2007  | 
proof -  | 
| 
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
2008  | 
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: 
47108 
diff
changeset
 | 
2009  | 
thus ?th1 ?th2 using Float_representation_aux[of m e] unfolding x_def[symmetric]  | 
| 47600 | 2010  | 
by (auto dest: not_leE)  | 
| 29805 | 2011  | 
qed  | 
2012  | 
||
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
2013  | 
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 | 2014  | 
proof -  | 
2015  | 
let ?B = "2^nat (bitlen m - 1)"  | 
|
| 
47599
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
2016  | 
def bl \<equiv> "bitlen m - 1"  | 
| 29805 | 2017  | 
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: 
47108 
diff
changeset
 | 
2018  | 
hence "0 \<le> bl" by (simp add: bitlen_def bl_def)  | 
| 
31468
 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 
hoelzl 
parents: 
31467 
diff
changeset
 | 
2019  | 
show ?thesis  | 
| 29805 | 2020  | 
proof (cases "0 \<le> e")  | 
| 
47599
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
2021  | 
case True  | 
| 
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
2022  | 
thus ?thesis  | 
| 
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
2023  | 
unfolding bl_def[symmetric] using `0 < real m` `0 \<le> bl`  | 
| 
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
2024  | 
apply (simp add: ln_mult)  | 
| 
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
2025  | 
apply (cases "e=0")  | 
| 
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
2026  | 
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: 
47108 
diff
changeset
 | 
2027  | 
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: 
47108 
diff
changeset
 | 
2028  | 
done  | 
| 29805 | 2029  | 
next  | 
2030  | 
case False hence "0 < -e" by auto  | 
|
| 
47599
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
2031  | 
have lne: "ln (2 powr real e) = ln (inverse (2 powr - e))" by (simp add: powr_minus)  | 
| 29805 | 2032  | 
hence pow_gt0: "(0::real) < 2^nat (-e)" by auto  | 
2033  | 
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: 
47108 
diff
changeset
 | 
2034  | 
show ?thesis using False unfolding bl_def[symmetric] using `0 < real m` `0 \<le> bl`  | 
| 56483 | 2035  | 
by (auto simp add: lne ln_mult ln_powr ln_div field_simps)  | 
| 29805 | 2036  | 
qed  | 
2037  | 
qed  | 
|
2038  | 
||
2039  | 
lemma ub_ln_lb_ln_bounds': assumes "1 \<le> x"  | 
|
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
2040  | 
shows "the (lb_ln prec x) \<le> ln x \<and> ln x \<le> the (ub_ln prec x)"  | 
| 29805 | 2041  | 
(is "?lb \<le> ?ln \<and> ?ln \<le> ?ub")  | 
2042  | 
proof (cases "x < Float 1 1")  | 
|
| 
31468
 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 
hoelzl 
parents: 
31467 
diff
changeset
 | 
2043  | 
case True  | 
| 47600 | 2044  | 
hence "real (x - 1) < 1" and "real x < 2" by auto  | 
2045  | 
have "\<not> x \<le> 0" and "\<not> x < 1" using `1 \<le> x` by auto  | 
|
2046  | 
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: 
31467 
diff
changeset
 | 
2047  | 
|
| 
58410
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
2048  | 
have [simp]: "(Float 3 (- 1)) = 3 / 2" by simp  | 
| 
31468
 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 
hoelzl 
parents: 
31467 
diff
changeset
 | 
2049  | 
|
| 
 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 
hoelzl 
parents: 
31467 
diff
changeset
 | 
2050  | 
show ?thesis  | 
| 
58410
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
2051  | 
proof (cases "x \<le> Float 3 (- 1)")  | 
| 
31468
 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 
hoelzl 
parents: 
31467 
diff
changeset
 | 
2052  | 
case True  | 
| 
 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 
hoelzl 
parents: 
31467 
diff
changeset
 | 
2053  | 
show ?thesis unfolding lb_ln.simps unfolding ub_ln.simps Let_def  | 
| 
 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 
hoelzl 
parents: 
31467 
diff
changeset
 | 
2054  | 
using ln_float_bounds[OF `0 \<le> real (x - 1)` `real (x - 1) < 1`, of prec] `\<not> x \<le> 0` `\<not> x < 1` True  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
2055  | 
by (auto intro!: float_round_down_le float_round_up_le)  | 
| 
31468
 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 
hoelzl 
parents: 
31467 
diff
changeset
 | 
2056  | 
next  | 
| 47600 | 2057  | 
case False hence *: "3 / 2 < x" by auto  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
2058  | 
|
| 
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
2059  | 
with ln_add[of "3 / 2" "x - 3 / 2"]  | 
| 
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
2060  | 
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: 
31467 
diff
changeset
 | 
2061  | 
by (auto simp add: algebra_simps diff_divide_distrib)  | 
| 
 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 
hoelzl 
parents: 
31467 
diff
changeset
 | 
2062  | 
|
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
2063  | 
let "?ub_horner x" = "float_round_up prec (x * ub_ln_horner prec (get_odd prec) 1 x)"  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
2064  | 
let "?lb_horner x" = "float_round_down prec (x * lb_ln_horner prec (get_even prec) 1 x)"  | 
| 
31468
 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 
hoelzl 
parents: 
31467 
diff
changeset
 | 
2065  | 
|
| 
 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 
hoelzl 
parents: 
31467 
diff
changeset
 | 
2066  | 
    { 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: 
32920 
diff
changeset
 | 
2067  | 
by (rule rapprox_rat_le1) simp_all  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
2068  | 
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: 
32920 
diff
changeset
 | 
2069  | 
by (rule order_trans[OF _ rapprox_rat]) simp  | 
| 
31468
 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 
hoelzl 
parents: 
31467 
diff
changeset
 | 
2070  | 
from mult_less_le_imp_less[OF * low] *  | 
| 
 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 
hoelzl 
parents: 
31467 
diff
changeset
 | 
2071  | 
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: 
31467 
diff
changeset
 | 
2072  | 
|
| 
 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 
hoelzl 
parents: 
31467 
diff
changeset
 | 
2073  | 
have "ln (real x * 2/3)  | 
| 
32960
 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 
wenzelm 
parents: 
32920 
diff
changeset
 | 
2074  | 
\<le> ln (real (x * rapprox_rat prec 2 3 - 1) + 1)"  | 
| 
31468
 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 
hoelzl 
parents: 
31467 
diff
changeset
 | 
2075  | 
proof (rule ln_le_cancel_iff[symmetric, THEN iffD1])  | 
| 
32960
 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 
wenzelm 
parents: 
32920 
diff
changeset
 | 
2076  | 
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: 
32920 
diff
changeset
 | 
2077  | 
using * low by auto  | 
| 
 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 
wenzelm 
parents: 
32920 
diff
changeset
 | 
2078  | 
show "0 < real x * 2 / 3" using * by simp  | 
| 
 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 
wenzelm 
parents: 
32920 
diff
changeset
 | 
2079  | 
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: 
31467 
diff
changeset
 | 
2080  | 
qed  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
2081  | 
also have "\<dots> \<le> ?ub_horner (x * rapprox_rat prec 2 3 - 1)"  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
2082  | 
proof (rule float_round_up_le, rule ln_float_bounds(2))  | 
| 
32960
 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 
wenzelm 
parents: 
32920 
diff
changeset
 | 
2083  | 
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: 
32920 
diff
changeset
 | 
2084  | 
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: 
32920 
diff
changeset
 | 
2085  | 
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: 
31467 
diff
changeset
 | 
2086  | 
qed  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
2087  | 
finally have "ln x \<le> ?ub_horner (Float 1 (-1))  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
2088  | 
+ ?ub_horner ((x * rapprox_rat prec 2 3 - 1))"  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
2089  | 
using ln_float_bounds(2)[of "Float 1 (- 1)" prec prec] add  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
2090  | 
by (auto intro!: add_mono float_round_up_le)  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
2091  | 
note float_round_up_le[OF this, of prec]  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
2092  | 
}  | 
| 
31468
 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 
hoelzl 
parents: 
31467 
diff
changeset
 | 
2093  | 
moreover  | 
| 
 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 
hoelzl 
parents: 
31467 
diff
changeset
 | 
2094  | 
    { let ?max = "max (x * lapprox_rat prec 2 3 - 1) 0"
 | 
| 
 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 
hoelzl 
parents: 
31467 
diff
changeset
 | 
2095  | 
|
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
2096  | 
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: 
32920 
diff
changeset
 | 
2097  | 
by (rule order_trans[OF lapprox_rat], simp)  | 
| 
31468
 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 
hoelzl 
parents: 
31467 
diff
changeset
 | 
2098  | 
|
| 
 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 
hoelzl 
parents: 
31467 
diff
changeset
 | 
2099  | 
have low: "0 \<le> real (lapprox_rat prec 2 3)"  | 
| 
47599
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
2100  | 
using lapprox_rat_nonneg[of 2 3 prec] by simp  | 
| 
31468
 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 
hoelzl 
parents: 
31467 
diff
changeset
 | 
2101  | 
|
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
2102  | 
have "?lb_horner ?max  | 
| 
32960
 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 
wenzelm 
parents: 
32920 
diff
changeset
 | 
2103  | 
\<le> ln (real ?max + 1)"  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
2104  | 
proof (rule float_round_down_le, rule ln_float_bounds(1))  | 
| 
32960
 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 
wenzelm 
parents: 
32920 
diff
changeset
 | 
2105  | 
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: 
32920 
diff
changeset
 | 
2106  | 
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: 
32920 
diff
changeset
 | 
2107  | 
auto simp add: real_of_float_max)  | 
| 
 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 
wenzelm 
parents: 
32920 
diff
changeset
 | 
2108  | 
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: 
31467 
diff
changeset
 | 
2109  | 
qed  | 
| 
 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 
hoelzl 
parents: 
31467 
diff
changeset
 | 
2110  | 
also have "\<dots> \<le> ln (real x * 2/3)"  | 
| 
 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 
hoelzl 
parents: 
31467 
diff
changeset
 | 
2111  | 
proof (rule ln_le_cancel_iff[symmetric, THEN iffD1])  | 
| 
32960
 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 
wenzelm 
parents: 
32920 
diff
changeset
 | 
2112  | 
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: 
32920 
diff
changeset
 | 
2113  | 
show "0 < real x * 2/3" using * by auto  | 
| 
 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 
wenzelm 
parents: 
32920 
diff
changeset
 | 
2114  | 
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: 
32920 
diff
changeset
 | 
2115  | 
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: 
47108 
diff
changeset
 | 
2116  | 
auto simp add: max_def)  | 
| 
31468
 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 
hoelzl 
parents: 
31467 
diff
changeset
 | 
2117  | 
qed  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
2118  | 
finally have "?lb_horner (Float 1 (- 1)) + ?lb_horner ?max \<le> ln x"  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
2119  | 
using ln_float_bounds(1)[of "Float 1 (- 1)" prec prec] add  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
2120  | 
by (auto intro!: add_mono float_round_down_le)  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
2121  | 
note float_round_down_le[OF this, of prec]  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
2122  | 
}  | 
| 
31468
 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 
hoelzl 
parents: 
31467 
diff
changeset
 | 
2123  | 
ultimately  | 
| 
 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 
hoelzl 
parents: 
31467 
diff
changeset
 | 
2124  | 
show ?thesis unfolding lb_ln.simps unfolding ub_ln.simps Let_def  | 
| 
 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 
hoelzl 
parents: 
31467 
diff
changeset
 | 
2125  | 
using `\<not> x \<le> 0` `\<not> x < 1` True False by auto  | 
| 
 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 
hoelzl 
parents: 
31467 
diff
changeset
 | 
2126  | 
qed  | 
| 29805 | 2127  | 
next  | 
2128  | 
case False  | 
|
| 
58410
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
2129  | 
hence "\<not> x \<le> 0" and "\<not> x < 1" "0 < x" "\<not> x \<le> Float 3 (- 1)"  | 
| 47600 | 2130  | 
using `1 \<le> x` by auto  | 
| 29805 | 2131  | 
show ?thesis  | 
| 
47599
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
2132  | 
proof -  | 
| 
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
2133  | 
def m \<equiv> "mantissa x"  | 
| 
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
2134  | 
def e \<equiv> "exponent x"  | 
| 
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
2135  | 
from Float_mantissa_exponent[of x] have Float: "x = Float m e" by (simp add: m_def e_def)  | 
| 29805 | 2136  | 
let ?s = "Float (e + (bitlen m - 1)) 0"  | 
2137  | 
let ?x = "Float m (- (bitlen m - 1))"  | 
|
2138  | 
||
| 
47599
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
2139  | 
have "0 < m" and "m \<noteq> 0" using `0 < x` Float powr_gt_zero[of 2 e]  | 
| 47600 | 2140  | 
by (auto simp: zero_less_mult_iff)  | 
| 
47599
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
2141  | 
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: 
47108 
diff
changeset
 | 
2142  | 
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: 
47108 
diff
changeset
 | 
2143  | 
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: 
47108 
diff
changeset
 | 
2144  | 
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: 
47108 
diff
changeset
 | 
2145  | 
unfolding bl_def[symmetric]  | 
| 
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
2146  | 
by (auto simp: powr_realpow[symmetric] field_simps inverse_eq_divide)  | 
| 
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
2147  | 
(auto simp : powr_minus field_simps inverse_eq_divide)  | 
| 29805 | 2148  | 
|
2149  | 
    {
 | 
|
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
2150  | 
have "float_round_down prec (lb_ln2 prec * ?s) \<le> ln 2 * (e + (bitlen m - 1))" (is "real ?lb2 \<le> _")  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
2151  | 
apply (rule float_round_down_le)  | 
| 
47601
 
050718fe6eee
use real :: float => real as lifting-morphism so we can directlry use the rep_eq theorems
 
hoelzl 
parents: 
47600 
diff
changeset
 | 
2152  | 
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: 
32920 
diff
changeset
 | 
2153  | 
using lb_ln2[of prec]  | 
| 
47599
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
2154  | 
proof (rule mult_mono)  | 
| 
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
2155  | 
from float_gt1_scale[OF `1 \<le> Float m e`]  | 
| 
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
2156  | 
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: 
47108 
diff
changeset
 | 
2157  | 
qed auto  | 
| 29805 | 2158  | 
moreover  | 
| 
47599
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
2159  | 
from ln_float_bounds(1)[OF x_bnds]  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
2160  | 
have "float_round_down prec ((?x - 1) * lb_ln_horner prec (get_even prec) 1 (?x - 1)) \<le> ln ?x" (is "real ?lb_horner \<le> _")  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
2161  | 
by (auto intro!: float_round_down_le)  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
2162  | 
ultimately have "float_plus_down prec ?lb2 ?lb_horner \<le> ln x"  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
2163  | 
unfolding Float ln_shifted_float[OF `0 < m`, of e] by (auto intro!: float_plus_down_le)  | 
| 
31468
 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 
hoelzl 
parents: 
31467 
diff
changeset
 | 
2164  | 
}  | 
| 29805 | 2165  | 
moreover  | 
2166  | 
    {
 | 
|
| 
47599
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
2167  | 
from ln_float_bounds(2)[OF x_bnds]  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
2168  | 
have "ln ?x \<le> float_round_up prec ((?x - 1) * ub_ln_horner prec (get_odd prec) 1 (?x - 1))" (is "_ \<le> real ?ub_horner")  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
2169  | 
by (auto intro!: float_round_up_le)  | 
| 29805 | 2170  | 
moreover  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
2171  | 
have "ln 2 * (e + (bitlen m - 1)) \<le> float_round_up prec (ub_ln2 prec * ?s)" (is "_ \<le> real ?ub2")  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
2172  | 
apply (rule float_round_up_le)  | 
| 
47601
 
050718fe6eee
use real :: float => real as lifting-morphism so we can directlry use the rep_eq theorems
 
hoelzl 
parents: 
47600 
diff
changeset
 | 
2173  | 
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: 
32920 
diff
changeset
 | 
2174  | 
using ub_ln2[of prec]  | 
| 
47599
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
2175  | 
proof (rule mult_mono)  | 
| 
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
2176  | 
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: 
32920 
diff
changeset
 | 
2177  | 
show "0 \<le> real (e + (bitlen m - 1))" by auto  | 
| 
47599
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
2178  | 
next  | 
| 
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
2179  | 
have "0 \<le> ln 2" by simp  | 
| 
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
2180  | 
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: 
47108 
diff
changeset
 | 
2181  | 
qed auto  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
2182  | 
ultimately have "ln x \<le> float_plus_up prec ?ub2 ?ub_horner"  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
2183  | 
unfolding Float ln_shifted_float[OF `0 < m`, of e]  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
2184  | 
by (auto intro!: float_plus_up_le)  | 
| 29805 | 2185  | 
}  | 
2186  | 
ultimately show ?thesis unfolding lb_ln.simps unfolding ub_ln.simps  | 
|
| 
58410
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
2187  | 
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: 
47600 
diff
changeset
 | 
2188  | 
unfolding plus_float.rep_eq e_def[symmetric] m_def[symmetric] by simp  | 
| 29805 | 2189  | 
qed  | 
2190  | 
qed  | 
|
2191  | 
||
| 49351 | 2192  | 
lemma ub_ln_lb_ln_bounds:  | 
2193  | 
assumes "0 < x"  | 
|
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
2194  | 
shows "the (lb_ln prec x) \<le> ln x \<and> ln x \<le> the (ub_ln prec x)"  | 
| 29805 | 2195  | 
(is "?lb \<le> ?ln \<and> ?ln \<le> ?ub")  | 
2196  | 
proof (cases "x < 1")  | 
|
| 
47599
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
2197  | 
case False hence "1 \<le> x" unfolding less_float_def less_eq_float_def by auto  | 
| 29805 | 2198  | 
show ?thesis using ub_ln_lb_ln_bounds'[OF `1 \<le> x`] .  | 
2199  | 
next  | 
|
| 47600 | 2200  | 
case True have "\<not> x \<le> 0" using `0 < x` by auto  | 
| 
58982
 
27e7e3f9e665
simplified computations based on round_up by reducing to round_down;
 
immler 
parents: 
58889 
diff
changeset
 | 
2201  | 
from True have "real x \<le> 1" "x \<le> 1" by simp_all  | 
| 47600 | 2202  | 
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: 
30971 
diff
changeset
 | 
2203  | 
hence A: "0 < 1 / real x" by auto  | 
| 29805 | 2204  | 
|
2205  | 
  {
 | 
|
2206  | 
let ?divl = "float_divl (max prec 1) 1 x"  | 
|
| 
58982
 
27e7e3f9e665
simplified computations based on round_up by reducing to round_down;
 
immler 
parents: 
58889 
diff
changeset
 | 
2207  | 
have A': "1 \<le> ?divl" using float_divl_pos_less1_bound[OF `0 < real x` `real x \<le> 1`] by auto  | 
| 47600 | 2208  | 
hence B: "0 < real ?divl" by auto  | 
| 
31468
 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 
hoelzl 
parents: 
31467 
diff
changeset
 | 
2209  | 
|
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
2210  | 
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: 
39556 
diff
changeset
 | 
2211  | 
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: 
31467 
diff
changeset
 | 
2212  | 
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: 
47600 
diff
changeset
 | 
2213  | 
have "?ln \<le> - the (lb_ln prec ?divl)" unfolding uminus_float.rep_eq by (rule order_trans)  | 
| 29805 | 2214  | 
} moreover  | 
2215  | 
  {
 | 
|
2216  | 
let ?divr = "float_divr prec 1 x"  | 
|
| 
58982
 
27e7e3f9e665
simplified computations based on round_up by reducing to round_down;
 
immler 
parents: 
58889 
diff
changeset
 | 
2217  | 
have A': "1 \<le> ?divr" using float_divr_pos_less1_lower_bound[OF `0 < x` `x \<le> 1`] unfolding less_eq_float_def less_float_def by auto  | 
| 47600 | 2218  | 
hence B: "0 < real ?divr" by auto  | 
| 
31468
 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 
hoelzl 
parents: 
31467 
diff
changeset
 | 
2219  | 
|
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
2220  | 
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: 
39556 
diff
changeset
 | 
2221  | 
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 | 2222  | 
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: 
47600 
diff
changeset
 | 
2223  | 
have "- the (ub_ln prec ?divr) \<le> ?ln" unfolding uminus_float.rep_eq by (rule order_trans)  | 
| 29805 | 2224  | 
}  | 
2225  | 
ultimately show ?thesis unfolding lb_ln.simps[where x=x] ub_ln.simps[where x=x]  | 
|
2226  | 
unfolding if_not_P[OF `\<not> x \<le> 0`] if_P[OF True] by auto  | 
|
2227  | 
qed  | 
|
2228  | 
||
| 49351 | 2229  | 
lemma lb_ln:  | 
2230  | 
assumes "Some y = lb_ln prec x"  | 
|
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
2231  | 
shows "y \<le> ln x" and "0 < real x"  | 
| 29805 | 2232  | 
proof -  | 
2233  | 
have "0 < x"  | 
|
2234  | 
proof (rule ccontr)  | 
|
| 
47599
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
2235  | 
assume "\<not> 0 < x" hence "x \<le> 0" unfolding less_eq_float_def less_float_def by auto  | 
| 29805 | 2236  | 
thus False using assms by auto  | 
2237  | 
qed  | 
|
| 47600 | 2238  | 
thus "0 < real x" by auto  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
2239  | 
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: 
39556 
diff
changeset
 | 
2240  | 
thus "y \<le> ln x" unfolding assms[symmetric] by auto  | 
| 29805 | 2241  | 
qed  | 
2242  | 
||
| 49351 | 2243  | 
lemma ub_ln:  | 
2244  | 
assumes "Some y = ub_ln prec x"  | 
|
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
2245  | 
shows "ln x \<le> y" and "0 < real x"  | 
| 29805 | 2246  | 
proof -  | 
2247  | 
have "0 < x"  | 
|
2248  | 
proof (rule ccontr)  | 
|
| 47600 | 2249  | 
assume "\<not> 0 < x" hence "x \<le> 0" by auto  | 
| 29805 | 2250  | 
thus False using assms by auto  | 
2251  | 
qed  | 
|
| 47600 | 2252  | 
thus "0 < real x" by auto  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
2253  | 
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: 
39556 
diff
changeset
 | 
2254  | 
thus "ln x \<le> y" unfolding assms[symmetric] by auto  | 
| 29805 | 2255  | 
qed  | 
2256  | 
||
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
2257  | 
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 | 2258  | 
proof (rule allI, rule allI, rule allI, rule impI)  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
2259  | 
fix x::real and lx ux  | 
| 
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
2260  | 
  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: 
39556 
diff
changeset
 | 
2261  | 
  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: 
39556 
diff
changeset
 | 
2262  | 
|
| 
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
2263  | 
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: 
39556 
diff
changeset
 | 
2264  | 
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: 
39556 
diff
changeset
 | 
2265  | 
|
| 
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
2266  | 
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: 
39556 
diff
changeset
 | 
2267  | 
have "l \<le> ln x" using x unfolding atLeastAtMost_iff by auto  | 
| 29805 | 2268  | 
moreover  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
2269  | 
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: 
39556 
diff
changeset
 | 
2270  | 
have "ln x \<le> u" using x unfolding atLeastAtMost_iff by auto  | 
| 
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
2271  | 
ultimately show "l \<le> ln x \<and> ln x \<le> u" ..  | 
| 29805 | 2272  | 
qed  | 
2273  | 
||
2274  | 
section "Implement floatarith"  | 
|
2275  | 
||
2276  | 
subsection "Define syntax and semantics"  | 
|
2277  | 
||
| 58310 | 2278  | 
datatype floatarith  | 
| 29805 | 2279  | 
= Add floatarith floatarith  | 
2280  | 
| Minus floatarith  | 
|
2281  | 
| Mult floatarith floatarith  | 
|
2282  | 
| Inverse floatarith  | 
|
2283  | 
| Cos floatarith  | 
|
2284  | 
| Arctan floatarith  | 
|
2285  | 
| Abs floatarith  | 
|
2286  | 
| Max floatarith floatarith  | 
|
2287  | 
| Min floatarith floatarith  | 
|
2288  | 
| Pi  | 
|
2289  | 
| Sqrt floatarith  | 
|
2290  | 
| Exp floatarith  | 
|
2291  | 
| Ln floatarith  | 
|
2292  | 
| 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: 
32650 
diff
changeset
 | 
2293  | 
| Var nat  | 
| 29805 | 2294  | 
| Num float  | 
2295  | 
||
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
2296  | 
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: 
30971 
diff
changeset
 | 
2297  | 
"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: 
30971 
diff
changeset
 | 
2298  | 
"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: 
30971 
diff
changeset
 | 
2299  | 
"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: 
30971 
diff
changeset
 | 
2300  | 
"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: 
30971 
diff
changeset
 | 
2301  | 
"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: 
30971 
diff
changeset
 | 
2302  | 
"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: 
30971 
diff
changeset
 | 
2303  | 
"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: 
30971 
diff
changeset
 | 
2304  | 
"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: 
30971 
diff
changeset
 | 
2305  | 
"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: 
30971 
diff
changeset
 | 
2306  | 
"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: 
30971 
diff
changeset
 | 
2307  | 
"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: 
30971 
diff
changeset
 | 
2308  | 
"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: 
30971 
diff
changeset
 | 
2309  | 
"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: 
30971 
diff
changeset
 | 
2310  | 
"interpret_floatarith (Power a n) vs = (interpret_floatarith a vs)^n" |  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
2311  | 
"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: 
32650 
diff
changeset
 | 
2312  | 
"interpret_floatarith (Var n) vs = vs ! n"  | 
| 29805 | 2313  | 
|
| 
31811
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2314  | 
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: 
36531 
diff
changeset
 | 
2315  | 
unfolding divide_inverse interpret_floatarith.simps ..  | 
| 
31811
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2316  | 
|
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2317  | 
lemma interpret_floatarith_diff: "interpret_floatarith (Add a (Minus b)) vs = (interpret_floatarith a vs) - (interpret_floatarith b vs)"  | 
| 
54230
 
b1d955791529
more simplification rules on unary and binary minus
 
haftmann 
parents: 
53077 
diff
changeset
 | 
2318  | 
unfolding interpret_floatarith.simps by simp  | 
| 
31811
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2319  | 
|
| 
58410
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
2320  | 
lemma interpret_floatarith_sin: "interpret_floatarith (Cos (Add (Mult Pi (Num (Float 1 (- 1)))) (Minus a))) vs =  | 
| 
31811
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2321  | 
sin (interpret_floatarith a vs)"  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2322  | 
unfolding sin_cos_eq interpret_floatarith.simps  | 
| 
54230
 
b1d955791529
more simplification rules on unary and binary minus
 
haftmann 
parents: 
53077 
diff
changeset
 | 
2323  | 
interpret_floatarith_divide interpret_floatarith_diff  | 
| 
31811
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2324  | 
by auto  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2325  | 
|
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2326  | 
lemma interpret_floatarith_tan:  | 
| 
58410
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
2327  | 
"interpret_floatarith (Mult (Cos (Add (Mult Pi (Num (Float 1 (- 1)))) (Minus a))) (Inverse (Cos a))) vs =  | 
| 
31811
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2328  | 
tan (interpret_floatarith a vs)"  | 
| 
36778
 
739a9379e29b
avoid using real-specific versions of generic lemmas
 
huffman 
parents: 
36531 
diff
changeset
 | 
2329  | 
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: 
31810 
diff
changeset
 | 
2330  | 
by auto  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2331  | 
|
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2332  | 
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: 
31810 
diff
changeset
 | 
2333  | 
unfolding powr_def interpret_floatarith.simps ..  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2334  | 
|
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2335  | 
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: 
36531 
diff
changeset
 | 
2336  | 
unfolding log_def interpret_floatarith.simps divide_inverse ..  | 
| 
31811
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2337  | 
|
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2338  | 
lemma interpret_floatarith_num:  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2339  | 
shows "interpret_floatarith (Num (Float 0 0)) vs = 0"  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2340  | 
and "interpret_floatarith (Num (Float 1 0)) vs = 1"  | 
| 
54489
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54269 
diff
changeset
 | 
2341  | 
and "interpret_floatarith (Num (Float (- 1) 0)) vs = - 1"  | 
| 
47108
 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 
huffman 
parents: 
46545 
diff
changeset
 | 
2342  | 
and "interpret_floatarith (Num (Float (numeral a) 0)) vs = numeral a"  | 
| 
54489
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54269 
diff
changeset
 | 
2343  | 
and "interpret_floatarith (Num (Float (- numeral a) 0)) vs = - numeral a" by auto  | 
| 
31811
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2344  | 
|
| 29805 | 2345  | 
subsection "Implement approximation function"  | 
2346  | 
||
2347  | 
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  | 
|
2348  | 
"lift_bin' (Some (l1, u1)) (Some (l2, u2)) f = Some (f l1 u1 l2 u2)" |  | 
|
2349  | 
"lift_bin' a b f = None"  | 
|
2350  | 
||
2351  | 
fun lift_un :: "(float * float) option \<Rightarrow> (float \<Rightarrow> float \<Rightarrow> ((float option) * (float option))) \<Rightarrow> (float * float) option" where  | 
|
2352  | 
"lift_un (Some (l1, u1)) f = (case (f l1 u1) of (Some l, Some u) \<Rightarrow> Some (l, u)  | 
|
2353  | 
| t \<Rightarrow> None)" |  | 
|
2354  | 
"lift_un b f = None"  | 
|
2355  | 
||
2356  | 
fun lift_un' :: "(float * float) option \<Rightarrow> (float \<Rightarrow> float \<Rightarrow> (float * float)) \<Rightarrow> (float * float) option" where  | 
|
2357  | 
"lift_un' (Some (l1, u1)) f = Some (f l1 u1)" |  | 
|
2358  | 
"lift_un' b f = None"  | 
|
2359  | 
||
| 
31811
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2360  | 
definition  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2361  | 
"bounded_by xs vs \<longleftrightarrow>  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2362  | 
(\<forall> i < length vs. case vs ! i of None \<Rightarrow> True  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2363  | 
         | Some (l, u) \<Rightarrow> xs ! i \<in> { real l .. real u })"
 | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2364  | 
|
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2365  | 
lemma bounded_byE:  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2366  | 
assumes "bounded_by xs vs"  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2367  | 
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: 
31810 
diff
changeset
 | 
2368  | 
         | Some (l, u) \<Rightarrow> xs ! i \<in> { real l .. real u }"
 | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2369  | 
using assms bounded_by_def by blast  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2370  | 
|
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2371  | 
lemma bounded_by_update:  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2372  | 
assumes "bounded_by xs vs"  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2373  | 
  and bnd: "xs ! i \<in> { real l .. real u }"
 | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2374  | 
shows "bounded_by xs (vs[i := Some (l,u)])"  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2375  | 
proof -  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2376  | 
{ fix j
 | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2377  | 
let ?vs = "vs[i := Some (l,u)]"  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2378  | 
assume "j < length ?vs" hence [simp]: "j < length vs" by simp  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2379  | 
  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: 
31810 
diff
changeset
 | 
2380  | 
proof (cases "?vs ! j")  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2381  | 
case (Some b)  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2382  | 
thus ?thesis  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2383  | 
proof (cases "i = j")  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2384  | 
case True  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2385  | 
thus ?thesis using `?vs ! j = Some b` and bnd by auto  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2386  | 
next  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2387  | 
case False  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2388  | 
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: 
31810 
diff
changeset
 | 
2389  | 
qed  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2390  | 
qed auto }  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2391  | 
thus ?thesis unfolding bounded_by_def by auto  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2392  | 
qed  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2393  | 
|
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2394  | 
lemma bounded_by_None:  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2395  | 
shows "bounded_by xs (replicate (length xs) None)"  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2396  | 
unfolding bounded_by_def by auto  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2397  | 
|
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2398  | 
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: 
47108 
diff
changeset
 | 
2399  | 
"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)" |  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
2400  | 
"approx prec (Add a b) bs =  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
2401  | 
lift_bin' (approx' prec a bs) (approx' prec b bs)  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
2402  | 
(\<lambda> l1 u1 l2 u2. (float_plus_down prec l1 l2, float_plus_up prec u1 u2))" |  | 
| 29805 | 2403  | 
"approx prec (Minus a) bs = lift_un' (approx' prec a bs) (\<lambda> l u. (-u, -l))" |  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
2404  | 
"approx prec (Mult a b) bs =  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
2405  | 
lift_bin' (approx' prec a bs) (approx' prec b bs)  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
2406  | 
(\<lambda> a1 a2 b1 b2.  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
2407  | 
(float_plus_down prec (nprt a1 * pprt b2)  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
2408  | 
(float_plus_down prec (nprt a2 * nprt b2)  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
2409  | 
(float_plus_down prec (pprt a1 * pprt b1) (pprt a2 * nprt b1))),  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
2410  | 
float_plus_up prec (pprt a2 * pprt b2)  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
2411  | 
(float_plus_up prec (pprt a1 * nprt b2)  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
2412  | 
(float_plus_up prec (nprt a2 * pprt b1) (nprt a1 * nprt b1)))))" |  | 
| 29805 | 2413  | 
"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))" |  | 
2414  | 
"approx prec (Cos a) bs = lift_un' (approx' prec a bs) (bnds_cos prec)" |  | 
|
2415  | 
"approx prec Pi bs = Some (lb_pi prec, ub_pi prec)" |  | 
|
2416  | 
"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))" |  | 
|
2417  | 
"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))" |  | 
|
2418  | 
"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>))" |  | 
|
2419  | 
"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: 
31148 
diff
changeset
 | 
2420  | 
"approx prec (Sqrt a) bs = lift_un' (approx' prec a bs) (\<lambda> l u. (lb_sqrt prec l, ub_sqrt prec u))" |  | 
| 29805 | 2421  | 
"approx prec (Exp a) bs = lift_un' (approx' prec a bs) (\<lambda> l u. (lb_exp prec l, ub_exp prec u))" |  | 
2422  | 
"approx prec (Ln a) bs = lift_un (approx' prec a bs) (\<lambda> l u. (lb_ln prec l, ub_ln prec u))" |  | 
|
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
2423  | 
"approx prec (Power a n) bs = lift_un' (approx' prec a bs) (float_power_bnds prec n)" |  | 
| 29805 | 2424  | 
"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: 
32650 
diff
changeset
 | 
2425  | 
"approx prec (Var i) bs = (if i < length bs then bs ! i else None)"  | 
| 29805 | 2426  | 
|
2427  | 
lemma lift_bin'_ex:  | 
|
2428  | 
assumes lift_bin'_Some: "Some (l, u) = lift_bin' a b f"  | 
|
2429  | 
shows "\<exists> l1 u1 l2 u2. Some (l1, u1) = a \<and> Some (l2, u2) = b"  | 
|
2430  | 
proof (cases a)  | 
|
2431  | 
case None hence "None = lift_bin' a b f" unfolding None lift_bin'.simps ..  | 
|
2432  | 
thus ?thesis using lift_bin'_Some by auto  | 
|
2433  | 
next  | 
|
2434  | 
case (Some a')  | 
|
2435  | 
show ?thesis  | 
|
2436  | 
proof (cases b)  | 
|
2437  | 
case None hence "None = lift_bin' a b f" unfolding None lift_bin'.simps ..  | 
|
2438  | 
thus ?thesis using lift_bin'_Some by auto  | 
|
2439  | 
next  | 
|
2440  | 
case (Some b')  | 
|
2441  | 
obtain la ua where a': "a' = (la, ua)" by (cases a', auto)  | 
|
2442  | 
obtain lb ub where b': "b' = (lb, ub)" by (cases b', auto)  | 
|
2443  | 
thus ?thesis unfolding `a = Some a'` `b = Some b'` a' b' by auto  | 
|
2444  | 
qed  | 
|
2445  | 
qed  | 
|
2446  | 
||
2447  | 
lemma lift_bin'_f:  | 
|
2448  | 
assumes lift_bin'_Some: "Some (l, u) = lift_bin' (g a) (g b) f"  | 
|
2449  | 
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"  | 
|
2450  | 
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)"  | 
|
2451  | 
proof -  | 
|
2452  | 
obtain l1 u1 l2 u2  | 
|
2453  | 
where Sa: "Some (l1, u1) = g a" and Sb: "Some (l2, u2) = g b" using lift_bin'_ex[OF assms(1)] by auto  | 
|
| 31809 | 2454  | 
have lu: "(l, u) = f l1 u1 l2 u2" using lift_bin'_Some[unfolded Sa[symmetric] Sb[symmetric] lift_bin'.simps] by auto  | 
| 29805 | 2455  | 
have "l = fst (f l1 u1 l2 u2)" and "u = snd (f l1 u1 l2 u2)" unfolding lu[symmetric] by auto  | 
| 31809 | 2456  | 
thus ?thesis using Pa[OF Sa] Pb[OF Sb] by auto  | 
| 29805 | 2457  | 
qed  | 
2458  | 
||
2459  | 
lemma approx_approx':  | 
|
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
2460  | 
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 | 2461  | 
and approx': "Some (l, u) = approx' prec a vs"  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
2462  | 
shows "l \<le> interpret_floatarith a xs \<and> interpret_floatarith a xs \<le> u"  | 
| 29805 | 2463  | 
proof -  | 
2464  | 
obtain l' u' where S: "Some (l', u') = approx prec a vs"  | 
|
2465  | 
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: 
47108 
diff
changeset
 | 
2466  | 
have l': "l = float_round_down prec l'" and u': "u = float_round_up prec u'"  | 
| 29805 | 2467  | 
using approx' unfolding approx'.simps S[symmetric] by auto  | 
| 31809 | 2468  | 
show ?thesis unfolding l' u'  | 
| 
47599
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
2469  | 
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: 
47108 
diff
changeset
 | 
2470  | 
using order_trans[OF float_round_down[of _ l'] Pa[OF S, THEN conjunct1]] by auto  | 
| 29805 | 2471  | 
qed  | 
2472  | 
||
2473  | 
lemma lift_bin':  | 
|
2474  | 
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: 
39556 
diff
changeset
 | 
2475  | 
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: 
39556 
diff
changeset
 | 
2476  | 
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: 
39556 
diff
changeset
 | 
2477  | 
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: 
39556 
diff
changeset
 | 
2478  | 
(l2 \<le> interpret_floatarith b xs \<and> interpret_floatarith b xs \<le> u2) \<and>  | 
| 29805 | 2479  | 
l = fst (f l1 u1 l2 u2) \<and> u = snd (f l1 u1 l2 u2)"  | 
2480  | 
proof -  | 
|
2481  | 
  { fix l u assume "Some (l, u) = approx' prec a bs"
 | 
|
2482  | 
with approx_approx'[of prec a bs, OF _ this] Pa  | 
|
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
2483  | 
have "l \<le> interpret_floatarith a xs \<and> interpret_floatarith a xs \<le> u" by auto } note Pa = this  | 
| 29805 | 2484  | 
  { fix l u assume "Some (l, u) = approx' prec b bs"
 | 
2485  | 
with approx_approx'[of prec b bs, OF _ this] Pb  | 
|
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
2486  | 
have "l \<le> interpret_floatarith b xs \<and> interpret_floatarith b xs \<le> u" by auto } note Pb = this  | 
| 29805 | 2487  | 
|
2488  | 
from lift_bin'_f[where g="\<lambda>a. approx' prec a bs" and P = ?P, OF lift_bin'_Some, OF Pa Pb]  | 
|
2489  | 
show ?thesis by auto  | 
|
2490  | 
qed  | 
|
2491  | 
||
2492  | 
lemma lift_un'_ex:  | 
|
2493  | 
assumes lift_un'_Some: "Some (l, u) = lift_un' a f"  | 
|
2494  | 
shows "\<exists> l u. Some (l, u) = a"  | 
|
2495  | 
proof (cases a)  | 
|
2496  | 
case None hence "None = lift_un' a f" unfolding None lift_un'.simps ..  | 
|
2497  | 
thus ?thesis using lift_un'_Some by auto  | 
|
2498  | 
next  | 
|
2499  | 
case (Some a')  | 
|
2500  | 
obtain la ua where a': "a' = (la, ua)" by (cases a', auto)  | 
|
2501  | 
thus ?thesis unfolding `a = Some a'` a' by auto  | 
|
2502  | 
qed  | 
|
2503  | 
||
2504  | 
lemma lift_un'_f:  | 
|
2505  | 
assumes lift_un'_Some: "Some (l, u) = lift_un' (g a) f"  | 
|
2506  | 
and Pa: "\<And>l u. Some (l, u) = g a \<Longrightarrow> P l u a"  | 
|
2507  | 
shows "\<exists> l1 u1. P l1 u1 a \<and> l = fst (f l1 u1) \<and> u = snd (f l1 u1)"  | 
|
2508  | 
proof -  | 
|
2509  | 
obtain l1 u1 where Sa: "Some (l1, u1) = g a" using lift_un'_ex[OF assms(1)] by auto  | 
|
2510  | 
have lu: "(l, u) = f l1 u1" using lift_un'_Some[unfolded Sa[symmetric] lift_un'.simps] by auto  | 
|
2511  | 
have "l = fst (f l1 u1)" and "u = snd (f l1 u1)" unfolding lu[symmetric] by auto  | 
|
2512  | 
thus ?thesis using Pa[OF Sa] by auto  | 
|
2513  | 
qed  | 
|
2514  | 
||
2515  | 
lemma lift_un':  | 
|
2516  | 
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: 
39556 
diff
changeset
 | 
2517  | 
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: 
39556 
diff
changeset
 | 
2518  | 
shows "\<exists> l1 u1. (l1 \<le> interpret_floatarith a xs \<and> interpret_floatarith a xs \<le> u1) \<and>  | 
| 29805 | 2519  | 
l = fst (f l1 u1) \<and> u = snd (f l1 u1)"  | 
2520  | 
proof -  | 
|
2521  | 
  { fix l u assume "Some (l, u) = approx' prec a bs"
 | 
|
2522  | 
with approx_approx'[of prec a bs, OF _ this] Pa  | 
|
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
2523  | 
have "l \<le> interpret_floatarith a xs \<and> interpret_floatarith a xs \<le> u" by auto } note Pa = this  | 
| 29805 | 2524  | 
from lift_un'_f[where g="\<lambda>a. approx' prec a bs" and P = ?P, OF lift_un'_Some, OF Pa]  | 
2525  | 
show ?thesis by auto  | 
|
2526  | 
qed  | 
|
2527  | 
||
2528  | 
lemma lift_un'_bnds:  | 
|
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
2529  | 
  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 | 2530  | 
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: 
39556 
diff
changeset
 | 
2531  | 
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: 
30971 
diff
changeset
 | 
2532  | 
shows "real l \<le> f' (interpret_floatarith a xs) \<and> f' (interpret_floatarith a xs) \<le> real u"  | 
| 29805 | 2533  | 
proof -  | 
2534  | 
from lift_un'[OF lift_un'_Some Pa]  | 
|
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
2535  | 
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: 
39556 
diff
changeset
 | 
2536  | 
  hence "(l, u) = f l1 u1" and "interpret_floatarith a xs \<in> {l1 .. u1}" by auto
 | 
| 29805 | 2537  | 
thus ?thesis using bnds by auto  | 
2538  | 
qed  | 
|
2539  | 
||
2540  | 
lemma lift_un_ex:  | 
|
2541  | 
assumes lift_un_Some: "Some (l, u) = lift_un a f"  | 
|
2542  | 
shows "\<exists> l u. Some (l, u) = a"  | 
|
2543  | 
proof (cases a)  | 
|
2544  | 
case None hence "None = lift_un a f" unfolding None lift_un.simps ..  | 
|
2545  | 
thus ?thesis using lift_un_Some by auto  | 
|
2546  | 
next  | 
|
2547  | 
case (Some a')  | 
|
2548  | 
obtain la ua where a': "a' = (la, ua)" by (cases a', auto)  | 
|
2549  | 
thus ?thesis unfolding `a = Some a'` a' by auto  | 
|
2550  | 
qed  | 
|
2551  | 
||
2552  | 
lemma lift_un_f:  | 
|
2553  | 
assumes lift_un_Some: "Some (l, u) = lift_un (g a) f"  | 
|
2554  | 
and Pa: "\<And>l u. Some (l, u) = g a \<Longrightarrow> P l u a"  | 
|
2555  | 
shows "\<exists> l1 u1. P l1 u1 a \<and> Some l = fst (f l1 u1) \<and> Some u = snd (f l1 u1)"  | 
|
2556  | 
proof -  | 
|
2557  | 
obtain l1 u1 where Sa: "Some (l1, u1) = g a" using lift_un_ex[OF assms(1)] by auto  | 
|
2558  | 
have "fst (f l1 u1) \<noteq> None \<and> snd (f l1 u1) \<noteq> None"  | 
|
2559  | 
proof (rule ccontr)  | 
|
2560  | 
assume "\<not> (fst (f l1 u1) \<noteq> None \<and> snd (f l1 u1) \<noteq> None)"  | 
|
2561  | 
hence or: "fst (f l1 u1) = None \<or> snd (f l1 u1) = None" by auto  | 
|
| 31809 | 2562  | 
hence "lift_un (g a) f = None"  | 
| 29805 | 2563  | 
proof (cases "fst (f l1 u1) = None")  | 
2564  | 
case True  | 
|
2565  | 
then obtain b where b: "f l1 u1 = (None, b)" by (cases "f l1 u1", auto)  | 
|
2566  | 
thus ?thesis unfolding Sa[symmetric] lift_un.simps b by auto  | 
|
2567  | 
next  | 
|
2568  | 
case False hence "snd (f l1 u1) = None" using or by auto  | 
|
2569  | 
with False obtain b where b: "f l1 u1 = (Some b, None)" by (cases "f l1 u1", auto)  | 
|
2570  | 
thus ?thesis unfolding Sa[symmetric] lift_un.simps b by auto  | 
|
2571  | 
qed  | 
|
2572  | 
thus False using lift_un_Some by auto  | 
|
2573  | 
qed  | 
|
2574  | 
then obtain a' b' where f: "f l1 u1 = (Some a', Some b')" by (cases "f l1 u1", auto)  | 
|
2575  | 
from lift_un_Some[unfolded Sa[symmetric] lift_un.simps f]  | 
|
2576  | 
have "Some l = fst (f l1 u1)" and "Some u = snd (f l1 u1)" unfolding f by auto  | 
|
2577  | 
thus ?thesis unfolding Sa[symmetric] lift_un.simps using Pa[OF Sa] by auto  | 
|
2578  | 
qed  | 
|
2579  | 
||
2580  | 
lemma lift_un:  | 
|
2581  | 
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: 
39556 
diff
changeset
 | 
2582  | 
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: 
39556 
diff
changeset
 | 
2583  | 
shows "\<exists> l1 u1. (l1 \<le> interpret_floatarith a xs \<and> interpret_floatarith a xs \<le> u1) \<and>  | 
| 29805 | 2584  | 
Some l = fst (f l1 u1) \<and> Some u = snd (f l1 u1)"  | 
2585  | 
proof -  | 
|
2586  | 
  { fix l u assume "Some (l, u) = approx' prec a bs"
 | 
|
2587  | 
with approx_approx'[of prec a bs, OF _ this] Pa  | 
|
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
2588  | 
have "l \<le> interpret_floatarith a xs \<and> interpret_floatarith a xs \<le> u" by auto } note Pa = this  | 
| 29805 | 2589  | 
from lift_un_f[where g="\<lambda>a. approx' prec a bs" and P = ?P, OF lift_un_Some, OF Pa]  | 
2590  | 
show ?thesis by auto  | 
|
2591  | 
qed  | 
|
2592  | 
||
2593  | 
lemma lift_un_bnds:  | 
|
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
2594  | 
  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 | 2595  | 
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: 
39556 
diff
changeset
 | 
2596  | 
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: 
30971 
diff
changeset
 | 
2597  | 
shows "real l \<le> f' (interpret_floatarith a xs) \<and> f' (interpret_floatarith a xs) \<le> real u"  | 
| 29805 | 2598  | 
proof -  | 
2599  | 
from lift_un[OF lift_un_Some Pa]  | 
|
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
2600  | 
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: 
39556 
diff
changeset
 | 
2601  | 
  hence "(Some l, Some u) = f l1 u1" and "interpret_floatarith a xs \<in> {l1 .. u1}" by auto
 | 
| 29805 | 2602  | 
thus ?thesis using bnds by auto  | 
2603  | 
qed  | 
|
2604  | 
||
2605  | 
lemma approx:  | 
|
2606  | 
assumes "bounded_by xs vs"  | 
|
2607  | 
and "Some (l, u) = approx prec arith vs" (is "_ = ?g arith")  | 
|
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
2608  | 
shows "l \<le> interpret_floatarith arith xs \<and> interpret_floatarith arith xs \<le> u" (is "?P l u arith")  | 
| 31809 | 2609  | 
using `Some (l, u) = approx prec arith vs`  | 
| 
45129
 
1fce03e3e8ad
tuned proofs -- eliminated vacuous "induct arbitrary: ..." situations;
 
wenzelm 
parents: 
44821 
diff
changeset
 | 
2610  | 
proof (induct arith arbitrary: l u)  | 
| 29805 | 2611  | 
case (Add a b)  | 
2612  | 
from lift_bin'[OF Add.prems[unfolded approx.simps]] Add.hyps  | 
|
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
2613  | 
obtain l1 u1 l2 u2 where "l = float_plus_down prec l1 l2" and "u = float_plus_up prec u1 u2"  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
2614  | 
"l1 \<le> interpret_floatarith a xs" and "interpret_floatarith a xs \<le> u1"  | 
| 
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
2615  | 
"l2 \<le> interpret_floatarith b xs" and "interpret_floatarith b xs \<le> u2" unfolding fst_conv snd_conv by blast  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
2616  | 
thus ?case unfolding interpret_floatarith.simps by (auto intro!: float_plus_up_le float_plus_down_le)  | 
| 29805 | 2617  | 
next  | 
2618  | 
case (Minus a)  | 
|
2619  | 
from lift_un'[OF Minus.prems[unfolded approx.simps]] Minus.hyps  | 
|
2620  | 
obtain l1 u1 where "l = -u1" and "u = -l1"  | 
|
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
2621  | 
"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: 
47600 
diff
changeset
 | 
2622  | 
thus ?case unfolding interpret_floatarith.simps using minus_float.rep_eq by auto  | 
| 29805 | 2623  | 
next  | 
2624  | 
case (Mult a b)  | 
|
2625  | 
from lift_bin'[OF Mult.prems[unfolded approx.simps]] Mult.hyps  | 
|
| 31809 | 2626  | 
obtain l1 u1 l2 u2  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
2627  | 
where l: "l = float_plus_down prec (nprt l1 * pprt u2) (float_plus_down prec (nprt u1 * nprt u2) (float_plus_down prec (pprt l1 * pprt l2) (pprt u1 * nprt l2)))"  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
2628  | 
and u: "u = float_plus_up prec (pprt u1 * pprt u2) (float_plus_up prec (pprt l1 * nprt u2) (float_plus_up prec (nprt u1 * pprt l2) (nprt l1 * nprt l2)))"  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
2629  | 
and "l1 \<le> interpret_floatarith a xs" and "interpret_floatarith a xs \<le> u1"  | 
| 
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
2630  | 
and "l2 \<le> interpret_floatarith b xs" and "interpret_floatarith b xs \<le> u2" unfolding fst_conv snd_conv by blast  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
2631  | 
hence bnds:  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
2632  | 
"nprt l1 * pprt u2 + nprt u1 * nprt u2 + pprt l1 * pprt l2 + pprt u1 * nprt l2 \<le> interpret_floatarith (Mult a b) xs" (is "?l \<le> _")  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
2633  | 
"interpret_floatarith (Mult a b) xs \<le> pprt u1 * pprt u2 + pprt l1 * nprt u2 + nprt u1 * pprt l2 + nprt l1 * nprt l2" (is "_ \<le> ?u")  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
2634  | 
unfolding interpret_floatarith.simps l u  | 
| 29805 | 2635  | 
using mult_le_prts mult_ge_prts by auto  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
2636  | 
from l u have "l \<le> ?l" "?u \<le> u"  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
2637  | 
by (auto intro!: float_plus_up_le float_plus_down_le)  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
2638  | 
thus ?case using bnds by simp  | 
| 29805 | 2639  | 
next  | 
2640  | 
case (Inverse a)  | 
|
2641  | 
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 | 2642  | 
obtain l1 u1 where l': "Some l = (if 0 < l1 \<or> u1 < 0 then Some (float_divl prec 1 u1) else None)"  | 
| 29805 | 2643  | 
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: 
39556 
diff
changeset
 | 
2644  | 
and l1: "l1 \<le> interpret_floatarith a xs" and u1: "interpret_floatarith a xs \<le> u1" by blast  | 
| 29805 | 2645  | 
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: 
30971 
diff
changeset
 | 
2646  | 
moreover have l1_le_u1: "real l1 \<le> real u1" using l1 u1 by auto  | 
| 47600 | 2647  | 
ultimately have "real l1 \<noteq> 0" and "real u1 \<noteq> 0" by auto  | 
| 29805 | 2648  | 
|
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
2649  | 
have inv: "inverse u1 \<le> inverse (interpret_floatarith a xs)  | 
| 
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
2650  | 
\<and> inverse (interpret_floatarith a xs) \<le> inverse l1"  | 
| 29805 | 2651  | 
proof (cases "0 < l1")  | 
| 31809 | 2652  | 
case True hence "0 < real u1" and "0 < real l1" "0 < interpret_floatarith a xs"  | 
| 47600 | 2653  | 
using l1_le_u1 l1 by auto  | 
| 29805 | 2654  | 
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: 
30971 
diff
changeset
 | 
2655  | 
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: 
32920 
diff
changeset
 | 
2656  | 
inverse_le_iff_le[OF `0 < interpret_floatarith a xs` `0 < real l1`]  | 
| 29805 | 2657  | 
using l1 u1 by auto  | 
2658  | 
next  | 
|
2659  | 
case False hence "u1 < 0" using either by blast  | 
|
| 31809 | 2660  | 
hence "real u1 < 0" and "real l1 < 0" "interpret_floatarith a xs < 0"  | 
| 47600 | 2661  | 
using l1_le_u1 u1 by auto  | 
| 29805 | 2662  | 
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: 
30971 
diff
changeset
 | 
2663  | 
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: 
32920 
diff
changeset
 | 
2664  | 
inverse_le_iff_le_neg[OF `interpret_floatarith a xs < 0` `real l1 < 0`]  | 
| 29805 | 2665  | 
using l1 u1 by auto  | 
2666  | 
qed  | 
|
| 
31468
 
b8267feaf342
Approximation: Corrected precision of ln on all real values
 
hoelzl 
parents: 
31467 
diff
changeset
 | 
2667  | 
|
| 29805 | 2668  | 
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: 
39556 
diff
changeset
 | 
2669  | 
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: 
30971 
diff
changeset
 | 
2670  | 
also have "\<dots> \<le> inverse (interpret_floatarith a xs)" using inv by auto  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
2671  | 
finally have "l \<le> inverse (interpret_floatarith a xs)" .  | 
| 29805 | 2672  | 
moreover  | 
2673  | 
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: 
39556 
diff
changeset
 | 
2674  | 
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: 
39556 
diff
changeset
 | 
2675  | 
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: 
30971 
diff
changeset
 | 
2676  | 
ultimately show ?case unfolding interpret_floatarith.simps using l1 u1 by auto  | 
| 29805 | 2677  | 
next  | 
2678  | 
case (Abs x)  | 
|
2679  | 
from lift_un'[OF Abs.prems[unfolded approx.simps], unfolded fst_conv snd_conv] Abs.hyps  | 
|
2680  | 
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: 
39556 
diff
changeset
 | 
2681  | 
and l1: "l1 \<le> interpret_floatarith x xs" and u1: "interpret_floatarith x xs \<le> u1" by blast  | 
| 47600 | 2682  | 
thus ?case unfolding l' u' by (cases "l1 < 0 \<and> 0 < u1", auto simp add: real_of_float_min real_of_float_max)  | 
| 29805 | 2683  | 
next  | 
2684  | 
case (Min a b)  | 
|
2685  | 
from lift_bin'[OF Min.prems[unfolded approx.simps], unfolded fst_conv snd_conv] Min.hyps  | 
|
2686  | 
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: 
39556 
diff
changeset
 | 
2687  | 
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: 
39556 
diff
changeset
 | 
2688  | 
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: 
30971 
diff
changeset
 | 
2689  | 
thus ?case unfolding l' u' by (auto simp add: real_of_float_min)  | 
| 29805 | 2690  | 
next  | 
2691  | 
case (Max a b)  | 
|
2692  | 
from lift_bin'[OF Max.prems[unfolded approx.simps], unfolded fst_conv snd_conv] Max.hyps  | 
|
2693  | 
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: 
39556 
diff
changeset
 | 
2694  | 
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: 
39556 
diff
changeset
 | 
2695  | 
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: 
30971 
diff
changeset
 | 
2696  | 
thus ?case unfolding l' u' by (auto simp add: real_of_float_max)  | 
| 29805 | 2697  | 
next case (Cos a) with lift_un'_bnds[OF bnds_cos] show ?case by auto  | 
2698  | 
next case (Arctan a) with lift_un'_bnds[OF bnds_arctan] show ?case by auto  | 
|
2699  | 
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: 
31148 
diff
changeset
 | 
2700  | 
next case (Sqrt a) with lift_un'_bnds[OF bnds_sqrt] show ?case by auto  | 
| 29805 | 2701  | 
next case (Exp a) with lift_un'_bnds[OF bnds_exp] show ?case by auto  | 
2702  | 
next case (Ln a) with lift_un_bnds[OF bnds_ln] show ?case by auto  | 
|
2703  | 
next case (Power a n) with lift_un'_bnds[OF bnds_power] show ?case by auto  | 
|
2704  | 
next case (Num f) thus ?case by auto  | 
|
2705  | 
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: 
32650 
diff
changeset
 | 
2706  | 
case (Var n)  | 
| 
31811
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2707  | 
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: 
31810 
diff
changeset
 | 
2708  | 
show ?case by (cases "n < length vs", auto)  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2709  | 
qed  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2710  | 
|
| 58310 | 2711  | 
datatype form = Bound floatarith floatarith floatarith form  | 
| 
31811
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2712  | 
| Assign floatarith floatarith form  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2713  | 
| Less floatarith floatarith  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2714  | 
| LessEqual floatarith floatarith  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2715  | 
| AtLeastAtMost floatarith floatarith floatarith  | 
| 58986 | 2716  | 
| Conj form form  | 
2717  | 
| Disj form form  | 
|
| 
31811
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2718  | 
|
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2719  | 
fun interpret_form :: "form \<Rightarrow> real list \<Rightarrow> bool" where  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2720  | 
"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: 
31810 
diff
changeset
 | 
2721  | 
"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: 
31810 
diff
changeset
 | 
2722  | 
"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: 
31810 
diff
changeset
 | 
2723  | 
"interpret_form (LessEqual a b) vs = (interpret_floatarith a vs \<le> interpret_floatarith b vs)" |  | 
| 58986 | 2724  | 
"interpret_form (AtLeastAtMost x a b) vs = (interpret_floatarith x vs \<in> { interpret_floatarith a vs .. interpret_floatarith b vs })" |
 | 
2725  | 
"interpret_form (Conj f g) vs \<longleftrightarrow> interpret_form f vs \<and> interpret_form g vs" |  | 
|
2726  | 
"interpret_form (Disj f g) vs \<longleftrightarrow> interpret_form f vs \<or> interpret_form g vs"  | 
|
| 
31811
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2727  | 
|
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2728  | 
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: 
31810 
diff
changeset
 | 
2729  | 
"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: 
31810 
diff
changeset
 | 
2730  | 
"approx_form' prec f (Suc s) n l u bs ss =  | 
| 
58410
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
2731  | 
(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: 
32650 
diff
changeset
 | 
2732  | 
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: 
32650 
diff
changeset
 | 
2733  | 
"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: 
31810 
diff
changeset
 | 
2734  | 
(case (approx prec a bs, approx prec b bs)  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2735  | 
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: 
31810 
diff
changeset
 | 
2736  | 
| _ \<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: 
32650 
diff
changeset
 | 
2737  | 
"approx_form prec (Assign (Var n) a f) bs ss =  | 
| 
31811
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2738  | 
(case (approx prec a bs)  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2739  | 
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: 
31810 
diff
changeset
 | 
2740  | 
| _ \<Rightarrow> False)" |  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2741  | 
"approx_form prec (Less a b) bs ss =  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2742  | 
(case (approx prec a bs, approx prec b bs)  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
2743  | 
of (Some (l, u), Some (l', u')) \<Rightarrow> float_plus_up prec u (-l') < 0  | 
| 
31811
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2744  | 
| _ \<Rightarrow> False)" |  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2745  | 
"approx_form prec (LessEqual a b) bs ss =  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2746  | 
(case (approx prec a bs, approx prec b bs)  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
2747  | 
of (Some (l, u), Some (l', u')) \<Rightarrow> float_plus_up prec u (-l') \<le> 0  | 
| 
31811
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2748  | 
| _ \<Rightarrow> False)" |  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2749  | 
"approx_form prec (AtLeastAtMost x a b) bs ss =  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2750  | 
(case (approx prec x bs, approx prec a bs, approx prec b bs)  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
2751  | 
of (Some (lx, ux), Some (l, u), Some (l', u')) \<Rightarrow> float_plus_up prec u (-lx) \<le> 0 \<and> float_plus_up prec ux (-l') \<le> 0  | 
| 
31811
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2752  | 
| _ \<Rightarrow> False)" |  | 
| 58986 | 2753  | 
"approx_form prec (Conj a b) bs ss \<longleftrightarrow> approx_form prec a bs ss \<and> approx_form prec b bs ss" |  | 
2754  | 
"approx_form prec (Disj a b) bs ss \<longleftrightarrow> approx_form prec a bs ss \<or> approx_form prec b bs ss" |  | 
|
| 
31811
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2755  | 
"approx_form _ _ _ _ = False"  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2756  | 
|
| 
32919
 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 
hoelzl 
parents: 
32650 
diff
changeset
 | 
2757  | 
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: 
32650 
diff
changeset
 | 
2758  | 
|
| 
31811
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2759  | 
lemma approx_form_approx_form':  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
2760  | 
  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: 
39556 
diff
changeset
 | 
2761  | 
  obtains l' u' where "x \<in> { l' .. u' }"
 | 
| 49351 | 2762  | 
and "approx_form prec f (bs[n := Some (l', u')]) ss"  | 
| 
31811
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2763  | 
using assms proof (induct s arbitrary: l u)  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2764  | 
case 0  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2765  | 
from this(1)[of l u] this(2,3)  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2766  | 
show thesis by auto  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2767  | 
next  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2768  | 
case (Suc s)  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2769  | 
|
| 
58410
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
2770  | 
let ?m = "(l + u) * Float 1 (- 1)"  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
2771  | 
have "real l \<le> ?m" and "?m \<le> real u"  | 
| 
47599
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
2772  | 
unfolding less_eq_float_def using Suc.prems by auto  | 
| 
31811
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2773  | 
|
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
2774  | 
  with `x \<in> { l .. u }`
 | 
| 
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
2775  | 
  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: 
31810 
diff
changeset
 | 
2776  | 
thus thesis  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2777  | 
proof (rule disjE)  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
2778  | 
    assume *: "x \<in> { l .. ?m }"
 | 
| 
31811
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2779  | 
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: 
32650 
diff
changeset
 | 
2780  | 
show thesis by (simp add: Let_def lazy_conj)  | 
| 29805 | 2781  | 
next  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
2782  | 
    assume *: "x \<in> { ?m .. u }"
 | 
| 
31811
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2783  | 
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: 
32650 
diff
changeset
 | 
2784  | 
show thesis by (simp add: Let_def lazy_conj)  | 
| 29805 | 2785  | 
qed  | 
2786  | 
qed  | 
|
2787  | 
||
| 
31811
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2788  | 
lemma approx_form_aux:  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2789  | 
assumes "approx_form prec f vs ss"  | 
| 49351 | 2790  | 
and "bounded_by xs vs"  | 
| 
31811
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2791  | 
shows "interpret_form f xs"  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2792  | 
using assms proof (induct f arbitrary: vs)  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2793  | 
case (Bound x a b f)  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2794  | 
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: 
32650 
diff
changeset
 | 
2795  | 
where x_eq: "x = Var n" by (cases x) auto  | 
| 
31811
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2796  | 
|
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2797  | 
with Bound.prems obtain l u' l' u  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2798  | 
where l_eq: "Some (l, u') = approx prec a vs"  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2799  | 
and u_eq: "Some (l', u) = approx prec b vs"  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2800  | 
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: 
37391 
diff
changeset
 | 
2801  | 
by (cases "approx prec a vs", simp) (cases "approx prec b vs", auto)  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
2802  | 
|
| 
31811
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2803  | 
  { 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: 
31810 
diff
changeset
 | 
2804  | 
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: 
39556 
diff
changeset
 | 
2805  | 
    have "xs ! n \<in> { l .. u}" by auto
 | 
| 
31811
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2806  | 
|
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2807  | 
from approx_form_approx_form'[OF approx_form' this]  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
2808  | 
    obtain lx ux where bnds: "xs ! n \<in> { lx .. ux }"
 | 
| 
31811
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2809  | 
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: 
31810 
diff
changeset
 | 
2810  | 
|
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2811  | 
from `bounded_by xs vs` bnds  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2812  | 
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: 
31810 
diff
changeset
 | 
2813  | 
with Bound.hyps[OF approx_form]  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2814  | 
have "interpret_form f xs" by blast }  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2815  | 
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: 
31810 
diff
changeset
 | 
2816  | 
next  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2817  | 
case (Assign x a f)  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2818  | 
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: 
32650 
diff
changeset
 | 
2819  | 
where x_eq: "x = Var n" by (cases x) auto  | 
| 
31811
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2820  | 
|
| 
47599
 
400b158f1589
replace the float datatype by a type with unique representation
 
hoelzl 
parents: 
47108 
diff
changeset
 | 
2821  | 
with Assign.prems obtain l u  | 
| 
31811
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2822  | 
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: 
32650 
diff
changeset
 | 
2823  | 
and x_eq: "x = Var n"  | 
| 
31811
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2824  | 
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: 
31810 
diff
changeset
 | 
2825  | 
by (cases "approx prec a vs") auto  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2826  | 
|
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2827  | 
  { assume bnds: "xs ! n = interpret_floatarith a xs"
 | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2828  | 
with approx[OF Assign.prems(2) bnd_eq]  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
2829  | 
    have "xs ! n \<in> { l .. u}" by auto
 | 
| 
31811
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2830  | 
from approx_form_approx_form'[OF approx_form' this]  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
2831  | 
    obtain lx ux where bnds: "xs ! n \<in> { lx .. ux }"
 | 
| 
31811
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2832  | 
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: 
31810 
diff
changeset
 | 
2833  | 
|
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2834  | 
from `bounded_by xs vs` bnds  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2835  | 
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: 
31810 
diff
changeset
 | 
2836  | 
with Assign.hyps[OF approx_form]  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2837  | 
have "interpret_form f xs" by blast }  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2838  | 
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: 
31810 
diff
changeset
 | 
2839  | 
next  | 
| 29805 | 2840  | 
case (Less a b)  | 
| 
31811
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2841  | 
then obtain l u l' u'  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2842  | 
where l_eq: "Some (l, u) = approx prec a vs"  | 
| 49351 | 2843  | 
and u_eq: "Some (l', u') = approx prec b vs"  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
2844  | 
and inequality: "real (float_plus_up prec u (-l')) < 0"  | 
| 
31811
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2845  | 
by (cases "approx prec a vs", auto,  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2846  | 
cases "approx prec b vs", auto)  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
2847  | 
from le_less_trans[OF float_plus_up inequality]  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
2848  | 
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: 
31810 
diff
changeset
 | 
2849  | 
show ?case by auto  | 
| 29805 | 2850  | 
next  | 
2851  | 
case (LessEqual a b)  | 
|
| 
31811
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2852  | 
then obtain l u l' u'  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2853  | 
where l_eq: "Some (l, u) = approx prec a vs"  | 
| 49351 | 2854  | 
and u_eq: "Some (l', u') = approx prec b vs"  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
2855  | 
and inequality: "real (float_plus_up prec u (-l')) \<le> 0"  | 
| 
31811
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2856  | 
by (cases "approx prec a vs", auto,  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2857  | 
cases "approx prec b vs", auto)  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
2858  | 
from order_trans[OF float_plus_up inequality]  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
2859  | 
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: 
31810 
diff
changeset
 | 
2860  | 
show ?case by auto  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2861  | 
next  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2862  | 
case (AtLeastAtMost x a b)  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2863  | 
then obtain lx ux l u l' u'  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2864  | 
where x_eq: "Some (lx, ux) = approx prec x vs"  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2865  | 
and l_eq: "Some (l, u) = approx prec a vs"  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2866  | 
and u_eq: "Some (l', u') = approx prec b vs"  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
2867  | 
and inequality: "real (float_plus_up prec u (-lx)) \<le> 0" "real (float_plus_up prec ux (-l')) \<le> 0"  | 
| 
31811
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2868  | 
by (cases "approx prec x vs", auto,  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2869  | 
cases "approx prec a vs", auto,  | 
| 
56073
 
29e308b56d23
enhanced simplifier solver for preconditions of rewrite rule, can now deal with conjunctions
 
nipkow 
parents: 
55506 
diff
changeset
 | 
2870  | 
cases "approx prec b vs", auto)  | 
| 
58985
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
2871  | 
from order_trans[OF float_plus_up inequality(1)] order_trans[OF float_plus_up inequality(2)]  | 
| 
 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 
immler 
parents: 
58982 
diff
changeset
 | 
2872  | 
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: 
31810 
diff
changeset
 | 
2873  | 
show ?case by auto  | 
| 58986 | 2874  | 
qed auto  | 
| 29805 | 2875  | 
|
| 
31811
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2876  | 
lemma approx_form:  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2877  | 
assumes "n = length xs"  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2878  | 
assumes "approx_form prec f (replicate n None) ss"  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2879  | 
shows "interpret_form f xs"  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
2880  | 
using approx_form_aux[OF _ bounded_by_None] assms by auto  | 
| 29805 | 2881  | 
|
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
2882  | 
subsection {* Implementing Taylor series expansion *}
 | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
2883  | 
|
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
2884  | 
fun isDERIV :: "nat \<Rightarrow> floatarith \<Rightarrow> real list \<Rightarrow> bool" where  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
2885  | 
"isDERIV x (Add a b) vs = (isDERIV x a vs \<and> isDERIV x b vs)" |  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
2886  | 
"isDERIV x (Mult a b) vs = (isDERIV x a vs \<and> isDERIV x b vs)" |  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
2887  | 
"isDERIV x (Minus a) vs = isDERIV x a vs" |  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
2888  | 
"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: 
31811 
diff
changeset
 | 
2889  | 
"isDERIV x (Cos a) vs = isDERIV x a vs" |  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
2890  | 
"isDERIV x (Arctan a) vs = isDERIV x a vs" |  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
2891  | 
"isDERIV x (Min a b) vs = False" |  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
2892  | 
"isDERIV x (Max a b) vs = False" |  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
2893  | 
"isDERIV x (Abs a) vs = False" |  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
2894  | 
"isDERIV x Pi vs = True" |  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
2895  | 
"isDERIV x (Sqrt a) vs = (isDERIV x a vs \<and> interpret_floatarith a vs > 0)" |  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
2896  | 
"isDERIV x (Exp a) vs = isDERIV x a vs" |  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
2897  | 
"isDERIV x (Ln a) vs = (isDERIV x a vs \<and> interpret_floatarith a vs > 0)" |  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
2898  | 
"isDERIV x (Power a 0) vs = True" |  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
2899  | 
"isDERIV x (Power a (Suc n)) vs = isDERIV x a vs" |  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
2900  | 
"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: 
32650 
diff
changeset
 | 
2901  | 
"isDERIV x (Var n) vs = True"  | 
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
2902  | 
|
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
2903  | 
fun DERIV_floatarith :: "nat \<Rightarrow> floatarith \<Rightarrow> floatarith" where  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
2904  | 
"DERIV_floatarith x (Add a b) = Add (DERIV_floatarith x a) (DERIV_floatarith x b)" |  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
2905  | 
"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: 
31811 
diff
changeset
 | 
2906  | 
"DERIV_floatarith x (Minus a) = Minus (DERIV_floatarith x a)" |  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
2907  | 
"DERIV_floatarith x (Inverse a) = Minus (Mult (DERIV_floatarith x a) (Inverse (Power a 2)))" |  | 
| 
58410
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
2908  | 
"DERIV_floatarith x (Cos a) = Minus (Mult (Cos (Add (Mult Pi (Num (Float 1 (- 1)))) (Minus a))) (DERIV_floatarith x a))" |  | 
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
2909  | 
"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: 
31811 
diff
changeset
 | 
2910  | 
"DERIV_floatarith x (Min a b) = Num 0" |  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
2911  | 
"DERIV_floatarith x (Max a b) = Num 0" |  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
2912  | 
"DERIV_floatarith x (Abs a) = Num 0" |  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
2913  | 
"DERIV_floatarith x Pi = Num 0" |  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
2914  | 
"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: 
31811 
diff
changeset
 | 
2915  | 
"DERIV_floatarith x (Exp a) = Mult (Exp a) (DERIV_floatarith x a)" |  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
2916  | 
"DERIV_floatarith x (Ln a) = Mult (Inverse a) (DERIV_floatarith x a)" |  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
2917  | 
"DERIV_floatarith x (Power a 0) = Num 0" |  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
2918  | 
"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: 
31811 
diff
changeset
 | 
2919  | 
"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: 
32650 
diff
changeset
 | 
2920  | 
"DERIV_floatarith x (Var n) = (if x = n then Num 1 else Num 0)"  | 
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
2921  | 
|
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
2922  | 
lemma DERIV_floatarith:  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
2923  | 
assumes "n < length vs"  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
2924  | 
assumes isDERIV: "isDERIV n f (vs[n := x])"  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
2925  | 
shows "DERIV (\<lambda> x'. interpret_floatarith f (vs[n := x'])) x :>  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
2926  | 
interpret_floatarith (DERIV_floatarith n f) (vs[n := x])"  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
2927  | 
(is "DERIV (?i f) x :> _")  | 
| 49351 | 2928  | 
using isDERIV  | 
2929  | 
proof (induct f arbitrary: x)  | 
|
2930  | 
case (Inverse a)  | 
|
2931  | 
thus ?case  | 
|
| 
56381
 
0556204bc230
merged DERIV_intros, has_derivative_intros into derivative_intros
 
hoelzl 
parents: 
56195 
diff
changeset
 | 
2932  | 
by (auto intro!: derivative_eq_intros simp add: algebra_simps power2_eq_square)  | 
| 49351 | 2933  | 
next  | 
2934  | 
case (Cos a)  | 
|
2935  | 
thus ?case  | 
|
| 56382 | 2936  | 
by (auto intro!: derivative_eq_intros  | 
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
2937  | 
simp del: interpret_floatarith.simps(5)  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
2938  | 
simp add: interpret_floatarith_sin interpret_floatarith.simps(5)[of a])  | 
| 49351 | 2939  | 
next  | 
2940  | 
case (Power a n)  | 
|
2941  | 
thus ?case  | 
|
| 56382 | 2942  | 
by (cases n) (auto intro!: derivative_eq_intros simp del: power_Suc simp add: real_of_nat_def)  | 
| 49351 | 2943  | 
next  | 
2944  | 
case (Ln a)  | 
|
| 56382 | 2945  | 
thus ?case by (auto intro!: derivative_eq_intros simp add: divide_inverse)  | 
| 49351 | 2946  | 
next  | 
2947  | 
case (Var i)  | 
|
2948  | 
thus ?case using `n < length vs` by auto  | 
|
| 
56381
 
0556204bc230
merged DERIV_intros, has_derivative_intros into derivative_intros
 
hoelzl 
parents: 
56195 
diff
changeset
 | 
2949  | 
qed (auto intro!: derivative_eq_intros)  | 
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
2950  | 
|
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
2951  | 
declare approx.simps[simp del]  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
2952  | 
|
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
2953  | 
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: 
31811 
diff
changeset
 | 
2954  | 
"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: 
31811 
diff
changeset
 | 
2955  | 
"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: 
31811 
diff
changeset
 | 
2956  | 
"isDERIV_approx prec x (Minus a) vs = isDERIV_approx prec x a vs" |  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
2957  | 
"isDERIV_approx prec x (Inverse a) vs =  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
2958  | 
(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: 
31811 
diff
changeset
 | 
2959  | 
"isDERIV_approx prec x (Cos a) vs = isDERIV_approx prec x a vs" |  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
2960  | 
"isDERIV_approx prec x (Arctan a) vs = isDERIV_approx prec x a vs" |  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
2961  | 
"isDERIV_approx prec x (Min a b) vs = False" |  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
2962  | 
"isDERIV_approx prec x (Max a b) vs = False" |  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
2963  | 
"isDERIV_approx prec x (Abs a) vs = False" |  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
2964  | 
"isDERIV_approx prec x Pi vs = True" |  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
2965  | 
"isDERIV_approx prec x (Sqrt a) vs =  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
2966  | 
(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: 
31811 
diff
changeset
 | 
2967  | 
"isDERIV_approx prec x (Exp a) vs = isDERIV_approx prec x a vs" |  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
2968  | 
"isDERIV_approx prec x (Ln a) vs =  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
2969  | 
(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: 
31811 
diff
changeset
 | 
2970  | 
"isDERIV_approx prec x (Power a 0) vs = True" |  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
2971  | 
"isDERIV_approx prec x (Power a (Suc n)) vs = isDERIV_approx prec x a vs" |  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
2972  | 
"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: 
32650 
diff
changeset
 | 
2973  | 
"isDERIV_approx prec x (Var n) vs = True"  | 
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
2974  | 
|
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
2975  | 
lemma isDERIV_approx:  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
2976  | 
assumes "bounded_by xs vs"  | 
| 49351 | 2977  | 
and isDERIV_approx: "isDERIV_approx prec x f vs"  | 
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
2978  | 
shows "isDERIV x f xs"  | 
| 49351 | 2979  | 
using isDERIV_approx  | 
2980  | 
proof (induct f)  | 
|
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
2981  | 
case (Inverse a)  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
2982  | 
then obtain l u where approx_Some: "Some (l, u) = approx prec a vs"  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
2983  | 
and *: "0 < l \<or> u < 0"  | 
| 49351 | 2984  | 
by (cases "approx prec a vs") auto  | 
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
2985  | 
with approx[OF `bounded_by xs vs` approx_Some]  | 
| 47600 | 2986  | 
have "interpret_floatarith a xs \<noteq> 0" by auto  | 
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
2987  | 
thus ?case using Inverse by auto  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
2988  | 
next  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
2989  | 
case (Ln a)  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
2990  | 
then obtain l u where approx_Some: "Some (l, u) = approx prec a vs"  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
2991  | 
and *: "0 < l"  | 
| 49351 | 2992  | 
by (cases "approx prec a vs") auto  | 
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
2993  | 
with approx[OF `bounded_by xs vs` approx_Some]  | 
| 47600 | 2994  | 
have "0 < interpret_floatarith a xs" by auto  | 
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
2995  | 
thus ?case using Ln by auto  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
2996  | 
next  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
2997  | 
case (Sqrt a)  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
2998  | 
then obtain l u where approx_Some: "Some (l, u) = approx prec a vs"  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
2999  | 
and *: "0 < l"  | 
| 49351 | 3000  | 
by (cases "approx prec a vs") auto  | 
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3001  | 
with approx[OF `bounded_by xs vs` approx_Some]  | 
| 47600 | 3002  | 
have "0 < interpret_floatarith a xs" by auto  | 
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3003  | 
thus ?case using Sqrt by auto  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3004  | 
next  | 
| 49351 | 3005  | 
case (Power a n) thus ?case by (cases n) auto  | 
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3006  | 
qed auto  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3007  | 
|
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3008  | 
lemma bounded_by_update_var:  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3009  | 
assumes "bounded_by xs vs" and "vs ! i = Some (l, u)"  | 
| 49351 | 3010  | 
    and bnd: "x \<in> { real l .. real u }"
 | 
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3011  | 
shows "bounded_by (xs[i := x]) vs"  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3012  | 
proof (cases "i < length xs")  | 
| 49351 | 3013  | 
case False  | 
3014  | 
thus ?thesis using `bounded_by xs vs` by auto  | 
|
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3015  | 
next  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3016  | 
let ?xs = "xs[i := x]"  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3017  | 
case True hence "i < length ?xs" by auto  | 
| 49351 | 3018  | 
  {
 | 
3019  | 
fix j  | 
|
3020  | 
assume "j < length vs"  | 
|
3021  | 
    have "case vs ! j of None \<Rightarrow> True | Some (l, u) \<Rightarrow> ?xs ! j \<in> { real l .. real u }"
 | 
|
3022  | 
proof (cases "vs ! j")  | 
|
3023  | 
case (Some b)  | 
|
3024  | 
thus ?thesis  | 
|
3025  | 
proof (cases "i = j")  | 
|
3026  | 
case True  | 
|
3027  | 
thus ?thesis using `vs ! i = Some (l, u)` Some and bnd `i < length ?xs`  | 
|
3028  | 
by auto  | 
|
3029  | 
next  | 
|
3030  | 
case False  | 
|
3031  | 
thus ?thesis  | 
|
3032  | 
using `bounded_by xs vs`[THEN bounded_byE, OF `j < length vs`] Some by auto  | 
|
3033  | 
qed  | 
|
3034  | 
qed auto  | 
|
3035  | 
}  | 
|
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3036  | 
thus ?thesis unfolding bounded_by_def by auto  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3037  | 
qed  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3038  | 
|
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3039  | 
lemma isDERIV_approx':  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3040  | 
assumes "bounded_by xs vs"  | 
| 49351 | 3041  | 
    and vs_x: "vs ! x = Some (l, u)" and X_in: "X \<in> { real l .. real u }"
 | 
3042  | 
and approx: "isDERIV_approx prec x f vs"  | 
|
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3043  | 
shows "isDERIV x f (xs[x := X])"  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3044  | 
proof -  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3045  | 
note bounded_by_update_var[OF `bounded_by xs vs` vs_x X_in] approx  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3046  | 
thus ?thesis by (rule isDERIV_approx)  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3047  | 
qed  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3048  | 
|
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3049  | 
lemma DERIV_approx:  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3050  | 
assumes "n < length xs" and bnd: "bounded_by xs vs"  | 
| 49351 | 3051  | 
and isD: "isDERIV_approx prec n f vs"  | 
3052  | 
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: 
39556 
diff
changeset
 | 
3053  | 
shows "\<exists>(x::real). l \<le> x \<and> x \<le> u \<and>  | 
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3054  | 
DERIV (\<lambda> x. interpret_floatarith f (xs[n := x])) (xs!n) :> x"  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3055  | 
(is "\<exists> x. _ \<and> _ \<and> DERIV (?i f) _ :> _")  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3056  | 
proof (rule exI[of _ "?i ?D (xs!n)"], rule conjI[OF _ conjI])  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3057  | 
let "?i f x" = "interpret_floatarith f (xs[n := x])"  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3058  | 
from approx[OF bnd app]  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
3059  | 
show "l \<le> ?i ?D (xs!n)" and "?i ?D (xs!n) \<le> u"  | 
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3060  | 
using `n < length xs` by auto  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3061  | 
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: 
31811 
diff
changeset
 | 
3062  | 
show "DERIV (?i f) (xs!n) :> (?i ?D (xs!n))" by simp  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3063  | 
qed  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3064  | 
|
| 49351 | 3065  | 
fun lift_bin :: "(float * float) option \<Rightarrow>  | 
3066  | 
(float * float) option \<Rightarrow> (float \<Rightarrow> float \<Rightarrow> float \<Rightarrow> float \<Rightarrow> (float * float) option) \<Rightarrow>  | 
|
3067  | 
(float * float) option" where  | 
|
3068  | 
"lift_bin (Some (l1, u1)) (Some (l2, u2)) f = f l1 u1 l2 u2"  | 
|
3069  | 
| "lift_bin a b f = None"  | 
|
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3070  | 
|
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3071  | 
lemma lift_bin:  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3072  | 
assumes lift_bin_Some: "Some (l, u) = lift_bin a b f"  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3073  | 
obtains l1 u1 l2 u2  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3074  | 
where "a = Some (l1, u1)"  | 
| 49351 | 3075  | 
and "b = Some (l2, u2)"  | 
3076  | 
and "f l1 u1 l2 u2 = Some (l, u)"  | 
|
3077  | 
using assms by (cases a, simp, cases b, simp, auto)  | 
|
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3078  | 
|
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3079  | 
fun approx_tse where  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3080  | 
"approx_tse prec n 0 c k f bs = approx prec f bs" |  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3081  | 
"approx_tse prec n (Suc s) c k f bs =  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3082  | 
(if isDERIV_approx prec n f bs then  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3083  | 
lift_bin (approx prec f (bs[n := Some (c,c)]))  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3084  | 
(approx_tse prec n s c (Suc k) (DERIV_floatarith n f) bs)  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3085  | 
(\<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: 
32650 
diff
changeset
 | 
3086  | 
(Add (Var 0)  | 
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3087  | 
(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: 
32650 
diff
changeset
 | 
3088  | 
(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: 
32650 
diff
changeset
 | 
3089  | 
(Var (Suc 0))))) [Some (l1, u1), Some (l2, u2), bs!n])  | 
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3090  | 
else approx prec f bs)"  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3091  | 
|
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3092  | 
lemma bounded_by_Cons:  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3093  | 
assumes bnd: "bounded_by xs vs"  | 
| 49351 | 3094  | 
    and x: "x \<in> { real l .. real u }"
 | 
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3095  | 
shows "bounded_by (x#xs) ((Some (l, u))#vs)"  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3096  | 
proof -  | 
| 49351 | 3097  | 
  {
 | 
3098  | 
fix i assume *: "i < length ((Some (l, u))#vs)"  | 
|
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3099  | 
    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: 
31811 
diff
changeset
 | 
3100  | 
proof (cases i)  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3101  | 
case 0 with x show ?thesis by auto  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3102  | 
next  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3103  | 
case (Suc i) with * have "i < length vs" by auto  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3104  | 
from bnd[THEN bounded_byE, OF this]  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3105  | 
show ?thesis unfolding Suc nth_Cons_Suc .  | 
| 49351 | 3106  | 
qed  | 
3107  | 
}  | 
|
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3108  | 
thus ?thesis by (auto simp add: bounded_by_def)  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3109  | 
qed  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3110  | 
|
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3111  | 
lemma approx_tse_generic:  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3112  | 
assumes "bounded_by xs vs"  | 
| 49351 | 3113  | 
and bnd_c: "bounded_by (xs[x := c]) vs" and "x < length vs" and "x < length xs"  | 
3114  | 
and bnd_x: "vs ! x = Some (lx, ux)"  | 
|
3115  | 
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: 
39556 
diff
changeset
 | 
3116  | 
  shows "\<exists> n. (\<forall> m < n. \<forall> (z::real) \<in> {lx .. ux}.
 | 
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3117  | 
DERIV (\<lambda> y. interpret_floatarith ((DERIV_floatarith x ^^ m) f) (xs[x := y])) z :>  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3118  | 
(interpret_floatarith ((DERIV_floatarith x ^^ (Suc m)) f) (xs[x := z])))  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
3119  | 
   \<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: 
39556 
diff
changeset
 | 
3120  | 
interpret_floatarith ((DERIV_floatarith x ^^ i) f) (xs[x := c]) *  | 
| 
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
3121  | 
(xs!x - c)^i) +  | 
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3122  | 
      inverse (real (\<Prod> j \<in> {k..<k+n}. j)) *
 | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3123  | 
interpret_floatarith ((DERIV_floatarith x ^^ n) f) (xs[x := t]) *  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
3124  | 
      (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: 
31811 
diff
changeset
 | 
3125  | 
using ate proof (induct s arbitrary: k f l u)  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3126  | 
case 0  | 
| 49351 | 3127  | 
  {
 | 
3128  | 
    fix t::real assume "t \<in> {lx .. ux}"
 | 
|
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3129  | 
note bounded_by_update_var[OF `bounded_by xs vs` bnd_x this]  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3130  | 
from approx[OF this 0[unfolded approx_tse.simps]]  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
3131  | 
    have "(interpret_floatarith f (xs[x := t])) \<in> {l .. u}"
 | 
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3132  | 
by (auto simp add: algebra_simps)  | 
| 49351 | 3133  | 
}  | 
3134  | 
thus ?case by (auto intro!: exI[of _ 0])  | 
|
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3135  | 
next  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3136  | 
case (Suc s)  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3137  | 
show ?case  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3138  | 
proof (cases "isDERIV_approx prec x f vs")  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3139  | 
case False  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3140  | 
note ap = Suc.prems[unfolded approx_tse.simps if_not_P[OF False]]  | 
| 49351 | 3141  | 
    {
 | 
3142  | 
      fix t::real assume "t \<in> {lx .. ux}"
 | 
|
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3143  | 
note bounded_by_update_var[OF `bounded_by xs vs` bnd_x this]  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3144  | 
from approx[OF this ap]  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
3145  | 
      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: 
32920 
diff
changeset
 | 
3146  | 
by (auto simp add: algebra_simps)  | 
| 49351 | 3147  | 
}  | 
3148  | 
thus ?thesis by (auto intro!: exI[of _ 0])  | 
|
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3149  | 
next  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3150  | 
case True  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3151  | 
with Suc.prems  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3152  | 
obtain l1 u1 l2 u2  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3153  | 
where a: "Some (l1, u1) = approx prec f (vs[x := Some (c,c)])"  | 
| 49351 | 3154  | 
and ate: "Some (l2, u2) = approx_tse prec x s c (Suc k) (DERIV_floatarith x f) vs"  | 
3155  | 
and final: "Some (l, u) = approx prec  | 
|
3156  | 
(Add (Var 0)  | 
|
3157  | 
(Mult (Inverse (Num (Float (int k) 0)))  | 
|
3158  | 
(Mult (Add (Var (Suc (Suc 0))) (Minus (Num c)))  | 
|
3159  | 
(Var (Suc 0))))) [Some (l1, u1), Some (l2, u2), vs!x]"  | 
|
| 
56073
 
29e308b56d23
enhanced simplifier solver for preconditions of rewrite rule, can now deal with conjunctions
 
nipkow 
parents: 
55506 
diff
changeset
 | 
3160  | 
by (auto elim!: lift_bin)  | 
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3161  | 
|
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3162  | 
from bnd_c `x < length xs`  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
3163  | 
have bnd: "bounded_by (xs[x:=c]) (vs[x:= Some (c,c)])"  | 
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3164  | 
by (auto intro!: bounded_by_update)  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3165  | 
|
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3166  | 
from approx[OF this a]  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
3167  | 
    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: 
31811 
diff
changeset
 | 
3168  | 
(is "?f 0 (real c) \<in> _")  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3169  | 
by auto  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3170  | 
|
| 49351 | 3171  | 
    {
 | 
3172  | 
fix f :: "'a \<Rightarrow> 'a" fix n :: nat fix x :: 'a  | 
|
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3173  | 
have "(f ^^ Suc n) x = (f ^^ n) (f x)"  | 
| 49351 | 3174  | 
by (induct n) auto  | 
3175  | 
}  | 
|
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3176  | 
note funpow_Suc = this[symmetric]  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3177  | 
from Suc.hyps[OF ate, unfolded this]  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3178  | 
obtain n  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
3179  | 
      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: 
39556 
diff
changeset
 | 
3180  | 
      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: 
39556 
diff
changeset
 | 
3181  | 
           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: 
31811 
diff
changeset
 | 
3182  | 
(is "\<forall> t \<in> _. ?X (Suc k) f n t \<in> _")  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3183  | 
by blast  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3184  | 
|
| 49351 | 3185  | 
    {
 | 
3186  | 
fix m and z::real  | 
|
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
3187  | 
      assume "m < Suc n" and bnd_z: "z \<in> { lx .. ux }"
 | 
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3188  | 
have "DERIV (?f m) z :> ?f (Suc m) z"  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3189  | 
proof (cases m)  | 
| 
32960
 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 
wenzelm 
parents: 
32920 
diff
changeset
 | 
3190  | 
case 0  | 
| 
 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 
wenzelm 
parents: 
32920 
diff
changeset
 | 
3191  | 
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: 
32920 
diff
changeset
 | 
3192  | 
show ?thesis by simp  | 
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3193  | 
next  | 
| 
32960
 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 
wenzelm 
parents: 
32920 
diff
changeset
 | 
3194  | 
case (Suc m')  | 
| 
 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 
wenzelm 
parents: 
32920 
diff
changeset
 | 
3195  | 
hence "m' < n" using `m < Suc n` by auto  | 
| 
 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 
wenzelm 
parents: 
32920 
diff
changeset
 | 
3196  | 
from DERIV_hyp[OF this bnd_z]  | 
| 
 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 
wenzelm 
parents: 
32920 
diff
changeset
 | 
3197  | 
show ?thesis using Suc by simp  | 
| 49351 | 3198  | 
qed  | 
3199  | 
} note DERIV = this  | 
|
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3200  | 
|
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3201  | 
    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: 
31811 
diff
changeset
 | 
3202  | 
    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: 
31811 
diff
changeset
 | 
3203  | 
    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: 
31811 
diff
changeset
 | 
3204  | 
unfolding setsum_shift_bounds_Suc_ivl[symmetric]  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3205  | 
unfolding setsum_head_upt_Suc[OF zero_less_Suc] ..  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
3206  | 
def C \<equiv> "xs!x - c"  | 
| 
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
3207  | 
|
| 49351 | 3208  | 
    {
 | 
3209  | 
      fix t::real assume t: "t \<in> {lx .. ux}"
 | 
|
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3210  | 
hence "bounded_by [xs!x] [vs!x]"  | 
| 
32960
 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 
wenzelm 
parents: 
32920 
diff
changeset
 | 
3211  | 
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: 
32920 
diff
changeset
 | 
3212  | 
by (cases "vs!x", auto simp add: bounded_by_def)  | 
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3213  | 
|
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3214  | 
with hyp[THEN bspec, OF t] f_c  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
3215  | 
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: 
32920 
diff
changeset
 | 
3216  | 
by (auto intro!: bounded_by_Cons)  | 
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3217  | 
from approx[OF this final, unfolded atLeastAtMost_iff[symmetric]]  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
3218  | 
      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: 
32920 
diff
changeset
 | 
3219  | 
by (auto simp add: algebra_simps)  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
3220  | 
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: 
39556 
diff
changeset
 | 
3221  | 
               (\<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: 
39556 
diff
changeset
 | 
3222  | 
               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: 
32920 
diff
changeset
 | 
3223  | 
unfolding funpow_Suc C_def[symmetric] setsum_move0 setprod_head_Suc  | 
| 35082 | 3224  | 
by (auto simp add: algebra_simps)  | 
| 
57512
 
cc97b347b301
reduced name variants for assoc and commute on plus and mult
 
haftmann 
parents: 
57418 
diff
changeset
 | 
3225  | 
(simp only: mult.left_commute [of _ "inverse (real k)"] setsum_right_distrib [symmetric])  | 
| 49351 | 3226  | 
      finally have "?T \<in> {l .. u}" .
 | 
3227  | 
}  | 
|
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3228  | 
thus ?thesis using DERIV by blast  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3229  | 
qed  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3230  | 
qed  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3231  | 
|
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3232  | 
lemma setprod_fact: "\<Prod> {1..<1 + k} = fact (k :: nat)"
 | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3233  | 
proof (induct k)  | 
| 49351 | 3234  | 
case 0  | 
3235  | 
show ?case by simp  | 
|
3236  | 
next  | 
|
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3237  | 
case (Suc k)  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3238  | 
  have "{ 1 ..< Suc (Suc k) } = insert (Suc k) { 1 ..< Suc k }" by auto
 | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3239  | 
  hence "\<Prod> { 1 ..< Suc (Suc k) } = (Suc k) * \<Prod> { 1 ..< Suc k }" by auto
 | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3240  | 
thus ?case using Suc by auto  | 
| 49351 | 3241  | 
qed  | 
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3242  | 
|
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3243  | 
lemma approx_tse:  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3244  | 
assumes "bounded_by xs vs"  | 
| 49351 | 3245  | 
    and bnd_x: "vs ! x = Some (lx, ux)" and bnd_c: "real c \<in> {lx .. ux}"
 | 
3246  | 
and "x < length vs" and "x < length xs"  | 
|
3247  | 
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: 
39556 
diff
changeset
 | 
3248  | 
  shows "interpret_floatarith f xs \<in> { l .. u }"
 | 
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3249  | 
proof -  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3250  | 
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: 
31811 
diff
changeset
 | 
3251  | 
hence F0: "F 0 = (\<lambda> z. interpret_floatarith f (xs[x := z]))" by auto  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3252  | 
|
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
3253  | 
hence "bounded_by (xs[x := c]) vs" and "x < length vs" "x < length xs"  | 
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3254  | 
using `bounded_by xs vs` bnd_x bnd_c `x < length vs` `x < length xs`  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3255  | 
by (auto intro!: bounded_by_update_var)  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3256  | 
|
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3257  | 
from approx_tse_generic[OF `bounded_by xs vs` this bnd_x ate]  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3258  | 
obtain n  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3259  | 
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: 
39556 
diff
changeset
 | 
3260  | 
    and hyp: "\<And> (t::real). t \<in> {lx .. ux} \<Longrightarrow>
 | 
| 
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
3261  | 
(\<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: 
39556 
diff
changeset
 | 
3262  | 
inverse (real (fact n)) * F n t * (xs!x - c)^n  | 
| 
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
3263  | 
             \<in> {l .. u}" (is "\<And> t. _ \<Longrightarrow> ?taylor t \<in> _")
 | 
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3264  | 
unfolding F_def atLeastAtMost_iff[symmetric] setprod_fact by blast  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3265  | 
|
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
3266  | 
  have bnd_xs: "xs ! x \<in> { lx .. ux }"
 | 
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3267  | 
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: 
31811 
diff
changeset
 | 
3268  | 
|
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3269  | 
show ?thesis  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3270  | 
proof (cases n)  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3271  | 
case 0 thus ?thesis using hyp[OF bnd_xs] unfolding F_def by auto  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3272  | 
next  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3273  | 
case (Suc n')  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3274  | 
show ?thesis  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
3275  | 
proof (cases "xs ! x = c")  | 
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3276  | 
case True  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3277  | 
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: 
32920 
diff
changeset
 | 
3278  | 
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: 
31811 
diff
changeset
 | 
3279  | 
next  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3280  | 
case False  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3281  | 
|
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
3282  | 
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: 
32920 
diff
changeset
 | 
3283  | 
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: 
31811 
diff
changeset
 | 
3284  | 
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: 
39556 
diff
changeset
 | 
3285  | 
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: 
32920 
diff
changeset
 | 
3286  | 
and fl_eq: "interpret_floatarith f (xs[x := xs ! x]) =  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
3287  | 
(\<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: 
39556 
diff
changeset
 | 
3288  | 
F (Suc n') t / real (fact (Suc n')) * (xs ! x - c) ^ Suc n'"  | 
| 56195 | 3289  | 
unfolding atLeast0LessThan by blast  | 
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3290  | 
|
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
3291  | 
      from t_bnd bnd_xs bnd_c have *: "t \<in> {lx .. ux}"
 | 
| 
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
3292  | 
by (cases "xs ! x < c", auto)  | 
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3293  | 
|
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3294  | 
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: 
32920 
diff
changeset
 | 
3295  | 
unfolding fl_eq Suc by (auto simp add: algebra_simps divide_inverse)  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
3296  | 
      also have "\<dots> \<in> {l .. u}" using * by (rule hyp)
 | 
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3297  | 
finally show ?thesis by simp  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3298  | 
qed  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3299  | 
qed  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3300  | 
qed  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3301  | 
|
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3302  | 
fun approx_tse_form' where  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3303  | 
"approx_tse_form' prec t f 0 l u cmp =  | 
| 
58410
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
3304  | 
(case approx_tse prec 0 t ((l + u) * Float 1 (- 1)) 1 f [Some (l, u)]  | 
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3305  | 
of Some (l, u) \<Rightarrow> cmp l u | None \<Rightarrow> False)" |  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3306  | 
"approx_tse_form' prec t f (Suc s) l u cmp =  | 
| 
58410
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
3307  | 
(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: 
32650 
diff
changeset
 | 
3308  | 
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: 
32650 
diff
changeset
 | 
3309  | 
approx_tse_form' prec t f s m u cmp else False))"  | 
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3310  | 
|
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3311  | 
lemma approx_tse_form':  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
3312  | 
fixes x :: real  | 
| 
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
3313  | 
  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: 
39556 
diff
changeset
 | 
3314  | 
  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>
 | 
| 
58410
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
3315  | 
approx_tse prec 0 t ((l' + u') * Float 1 (- 1)) 1 f [Some (l', u')] = Some (ly, uy)"  | 
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3316  | 
using assms proof (induct s arbitrary: l u)  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3317  | 
case 0  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3318  | 
then obtain ly uy  | 
| 
58410
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
3319  | 
where *: "approx_tse prec 0 t ((l + u) * Float 1 (- 1)) 1 f [Some (l, u)] = Some (ly, uy)"  | 
| 
55413
 
a8e96847523c
adapted theories to '{case,rec}_{list,option}' names
 
blanchet 
parents: 
54782 
diff
changeset
 | 
3320  | 
and **: "cmp ly uy" by (auto elim!: case_optionE)  | 
| 46545 | 3321  | 
with 0 show ?case by auto  | 
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3322  | 
next  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3323  | 
case (Suc s)  | 
| 
58410
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
3324  | 
let ?m = "(l + u) * Float 1 (- 1)"  | 
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3325  | 
from Suc.prems  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3326  | 
have l: "approx_tse_form' prec t f s l ?m cmp"  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3327  | 
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: 
32650 
diff
changeset
 | 
3328  | 
by (auto simp add: Let_def lazy_conj)  | 
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3329  | 
|
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
3330  | 
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: 
47108 
diff
changeset
 | 
3331  | 
unfolding less_eq_float_def using Suc.prems by auto  | 
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3332  | 
|
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
3333  | 
  with `x \<in> { l .. u }`
 | 
| 
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
3334  | 
  have "x \<in> { l .. ?m} \<or> x \<in> { ?m .. u }" by auto
 | 
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3335  | 
thus ?case  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3336  | 
proof (rule disjE)  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
3337  | 
    assume "x \<in> { l .. ?m}"
 | 
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3338  | 
from Suc.hyps[OF l this]  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3339  | 
obtain l' u' ly uy  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
3340  | 
      where "x \<in> { l' .. u' } \<and> real l \<le> l' \<and> real u' \<le> ?m \<and> cmp ly uy \<and>
 | 
| 
58410
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
3341  | 
approx_tse prec 0 t ((l' + u') * Float 1 (- 1)) 1 f [Some (l', u')] = Some (ly, uy)" by blast  | 
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3342  | 
with m_u show ?thesis by (auto intro!: exI)  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3343  | 
next  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
3344  | 
    assume "x \<in> { ?m .. u }"
 | 
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3345  | 
from Suc.hyps[OF u this]  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3346  | 
obtain l' u' ly uy  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
3347  | 
      where "x \<in> { l' .. u' } \<and> ?m \<le> real l' \<and> u' \<le> real u \<and> cmp ly uy \<and>
 | 
| 
58410
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
3348  | 
approx_tse prec 0 t ((l' + u') * Float 1 (- 1)) 1 f [Some (l', u')] = Some (ly, uy)" by blast  | 
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3349  | 
with m_u show ?thesis by (auto intro!: exI)  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3350  | 
qed  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3351  | 
qed  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3352  | 
|
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3353  | 
lemma approx_tse_form'_less:  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
3354  | 
fixes x :: real  | 
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3355  | 
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: 
39556 
diff
changeset
 | 
3356  | 
  and x: "x \<in> {l .. u}"
 | 
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3357  | 
shows "interpret_floatarith b [x] < interpret_floatarith a [x]"  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3358  | 
proof -  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3359  | 
from approx_tse_form'[OF tse x]  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3360  | 
obtain l' u' ly uy  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
3361  | 
    where x': "x \<in> { l' .. u' }" and "l \<le> real l'"
 | 
| 
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
3362  | 
and "real u' \<le> u" and "0 < ly"  | 
| 
58410
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
3363  | 
and tse: "approx_tse prec 0 t ((l' + u') * Float 1 (- 1)) 1 (Add a (Minus b)) [Some (l', u')] = Some (ly, uy)"  | 
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3364  | 
by blast  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3365  | 
|
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3366  | 
hence "bounded_by [x] [Some (l', u')]" by (auto simp add: bounded_by_def)  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3367  | 
|
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3368  | 
from approx_tse[OF this _ _ _ _ tse[symmetric], of l' u'] x'  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
3369  | 
have "ly \<le> interpret_floatarith a [x] - interpret_floatarith b [x]"  | 
| 
54230
 
b1d955791529
more simplification rules on unary and binary minus
 
haftmann 
parents: 
53077 
diff
changeset
 | 
3370  | 
by auto  | 
| 47600 | 3371  | 
from order_less_le_trans[OF _ this, of 0] `0 < ly`  | 
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3372  | 
show ?thesis by auto  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3373  | 
qed  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3374  | 
|
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3375  | 
lemma approx_tse_form'_le:  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
3376  | 
fixes x :: real  | 
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3377  | 
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: 
39556 
diff
changeset
 | 
3378  | 
  and x: "x \<in> {l .. u}"
 | 
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3379  | 
shows "interpret_floatarith b [x] \<le> interpret_floatarith a [x]"  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3380  | 
proof -  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3381  | 
from approx_tse_form'[OF tse x]  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3382  | 
obtain l' u' ly uy  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
3383  | 
    where x': "x \<in> { l' .. u' }" and "l \<le> real l'"
 | 
| 
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
3384  | 
and "real u' \<le> u" and "0 \<le> ly"  | 
| 
58410
 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 
haftmann 
parents: 
58310 
diff
changeset
 | 
3385  | 
and tse: "approx_tse prec 0 t ((l' + u') * Float 1 (- 1)) 1 (Add a (Minus b)) [Some (l', u')] = Some (ly, uy)"  | 
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3386  | 
by blast  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3387  | 
|
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3388  | 
hence "bounded_by [x] [Some (l', u')]" by (auto simp add: bounded_by_def)  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3389  | 
|
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3390  | 
from approx_tse[OF this _ _ _ _ tse[symmetric], of l' u'] x'  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
3391  | 
have "ly \<le> interpret_floatarith a [x] - interpret_floatarith b [x]"  | 
| 
54230
 
b1d955791529
more simplification rules on unary and binary minus
 
haftmann 
parents: 
53077 
diff
changeset
 | 
3392  | 
by auto  | 
| 47600 | 3393  | 
from order_trans[OF _ this, of 0] `0 \<le> ly`  | 
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3394  | 
show ?thesis by auto  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3395  | 
qed  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3396  | 
|
| 58986 | 3397  | 
fun approx_tse_concl where  | 
3398  | 
"approx_tse_concl prec t (Less lf rt) s l u l' u' \<longleftrightarrow>  | 
|
3399  | 
approx_tse_form' prec t (Add rt (Minus lf)) s l u' (\<lambda> l u. 0 < l)" |  | 
|
3400  | 
"approx_tse_concl prec t (LessEqual lf rt) s l u l' u' \<longleftrightarrow>  | 
|
3401  | 
approx_tse_form' prec t (Add rt (Minus lf)) s l u' (\<lambda> l u. 0 \<le> l)" |  | 
|
3402  | 
"approx_tse_concl prec t (AtLeastAtMost x lf rt) s l u l' u' \<longleftrightarrow>  | 
|
3403  | 
(if approx_tse_form' prec t (Add x (Minus lf)) s l u' (\<lambda> l u. 0 \<le> l) then  | 
|
3404  | 
approx_tse_form' prec t (Add rt (Minus x)) s l u' (\<lambda> l u. 0 \<le> l) else False)" |  | 
|
3405  | 
"approx_tse_concl prec t (Conj f g) s l u l' u' \<longleftrightarrow>  | 
|
3406  | 
approx_tse_concl prec t f s l u l' u' \<and> approx_tse_concl prec t g s l u l' u'" |  | 
|
3407  | 
"approx_tse_concl prec t (Disj f g) s l u l' u' \<longleftrightarrow>  | 
|
3408  | 
approx_tse_concl prec t f s l u l' u' \<or> approx_tse_concl prec t g s l u l' u'" |  | 
|
3409  | 
"approx_tse_concl _ _ _ _ _ _ _ _ \<longleftrightarrow> False"  | 
|
3410  | 
||
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3411  | 
definition  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3412  | 
"approx_tse_form prec t s f =  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3413  | 
(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: 
32650 
diff
changeset
 | 
3414  | 
of (Bound x a b f) \<Rightarrow> x = Var 0 \<and>  | 
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3415  | 
(case (approx prec a [None], approx prec b [None])  | 
| 58986 | 3416  | 
of (Some (l, u), Some (l', u')) \<Rightarrow> approx_tse_concl prec t f s l u l' u'  | 
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3417  | 
| _ \<Rightarrow> False)  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3418  | 
| _ \<Rightarrow> False)"  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3419  | 
|
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3420  | 
lemma approx_tse_form:  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3421  | 
assumes "approx_tse_form prec t s f"  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3422  | 
shows "interpret_form f [x]"  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3423  | 
proof (cases f)  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3424  | 
case (Bound i a b f') note f_def = this  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3425  | 
with assms obtain l u l' u'  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3426  | 
where a: "approx prec a [None] = Some (l, u)"  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3427  | 
and b: "approx prec b [None] = Some (l', u')"  | 
| 
55413
 
a8e96847523c
adapted theories to '{case,rec}_{list,option}' names
 
blanchet 
parents: 
54782 
diff
changeset
 | 
3428  | 
unfolding approx_tse_form_def by (auto elim!: case_optionE)  | 
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3429  | 
|
| 
32919
 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 
hoelzl 
parents: 
32650 
diff
changeset
 | 
3430  | 
from Bound assms have "i = Var 0" unfolding approx_tse_form_def by auto  | 
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3431  | 
hence i: "interpret_floatarith i [x] = x" by auto  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3432  | 
|
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3433  | 
  { let "?f z" = "interpret_floatarith z [x]"
 | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3434  | 
    assume "?f i \<in> { ?f a .. ?f b }"
 | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3435  | 
with approx[OF _ a[symmetric], of "[x]"] approx[OF _ b[symmetric], of "[x]"]  | 
| 
40881
 
e84f82418e09
Use coercions in Approximation (by Dmitriy Traytel).
 
hoelzl 
parents: 
39556 
diff
changeset
 | 
3436  | 
    have bnd: "x \<in> { l .. u'}" unfolding bounded_by_def i by auto
 | 
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3437  | 
|
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3438  | 
have "interpret_form f' [x]"  | 
| 58986 | 3439  | 
using assms[unfolded Bound]  | 
3440  | 
proof (induct f')  | 
|
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3441  | 
case (Less lf rt)  | 
| 58986 | 3442  | 
with a b  | 
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3443  | 
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: 
32920 
diff
changeset
 | 
3444  | 
unfolding approx_tse_form_def by auto  | 
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3445  | 
from approx_tse_form'_less[OF this bnd]  | 
| 58986 | 3446  | 
show ?case using Less by auto  | 
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3447  | 
next  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3448  | 
case (LessEqual lf rt)  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3449  | 
with Bound a b assms  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3450  | 
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: 
32920 
diff
changeset
 | 
3451  | 
unfolding approx_tse_form_def by auto  | 
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3452  | 
from approx_tse_form'_le[OF this bnd]  | 
| 58986 | 3453  | 
show ?case using LessEqual by auto  | 
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3454  | 
next  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3455  | 
case (AtLeastAtMost x lf rt)  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3456  | 
with Bound a b assms  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3457  | 
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: 
32920 
diff
changeset
 | 
3458  | 
and "approx_tse_form' prec t (Add x (Minus lf)) s l u' (\<lambda> l u. 0 \<le> l)"  | 
| 58986 | 3459  | 
unfolding approx_tse_form_def lazy_conj by (auto split: split_if_asm)  | 
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3460  | 
from approx_tse_form'_le[OF this(1) bnd] approx_tse_form'_le[OF this(2) bnd]  | 
| 58986 | 3461  | 
show ?case using AtLeastAtMost by auto  | 
3462  | 
qed (auto simp: f_def approx_tse_form_def elim!: case_optionE)  | 
|
3463  | 
} thus ?thesis unfolding f_def by auto  | 
|
3464  | 
qed (insert assms, auto simp add: approx_tse_form_def)  | 
|
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3465  | 
|
| 
32919
 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 
hoelzl 
parents: 
32650 
diff
changeset
 | 
3466  | 
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: 
32650 
diff
changeset
 | 
3467  | 
|
| 
 
37adfa07b54b
approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
 
hoelzl 
parents: 
32650 
diff
changeset
 | 
3468  | 
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: 
32650 
diff
changeset
 | 
3469  | 
"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: 
32650 
diff
changeset
 | 
3470  | 
(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: 
32650 
diff
changeset
 | 
3471  | 
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: 
32650 
diff
changeset
 | 
3472  | 
| _ \<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: 
32650 
diff
changeset
 | 
3473  | 
"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: 
32650 
diff
changeset
 | 
3474  | 
(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: 
32650 
diff
changeset
 | 
3475  | 
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: 
32650 
diff
changeset
 | 
3476  | 
| _ \<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: 
32650 
diff
changeset
 | 
3477  | 
"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: 
32650 
diff
changeset
 | 
3478  | 
"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: 
32650 
diff
changeset
 | 
3479  | 
"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: 
32650 
diff
changeset
 | 
3480  | 
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: 
32650 
diff
changeset
 | 
3481  | 
"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: 
32650 
diff
changeset
 | 
3482  | 
|
| 29805 | 3483  | 
subsection {* Implement proof method \texttt{approximation} *}
 | 
3484  | 
||
| 
31811
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
3485  | 
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: 
30971 
diff
changeset
 | 
3486  | 
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: 
31148 
diff
changeset
 | 
3487  | 
interpret_floatarith_sin  | 
| 29805 | 3488  | 
|
| 
36985
 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 
haftmann 
parents: 
36960 
diff
changeset
 | 
3489  | 
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: 
36960 
diff
changeset
 | 
3490  | 
let  | 
| 
 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 
haftmann 
parents: 
36960 
diff
changeset
 | 
3491  | 
  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: 
36960 
diff
changeset
 | 
3492  | 
|
| 
38716
 
3c3b4ad683d5
approximation_oracle: actually match true/false in ML, not arbitrary values;
 
wenzelm 
parents: 
38558 
diff
changeset
 | 
3493  | 
  fun term_of_bool true = @{term True}
 | 
| 
 
3c3b4ad683d5
approximation_oracle: actually match true/false in ML, not arbitrary values;
 
wenzelm 
parents: 
38558 
diff
changeset
 | 
3494  | 
    | 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: 
36960 
diff
changeset
 | 
3495  | 
|
| 
51143
 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 
haftmann 
parents: 
49962 
diff
changeset
 | 
3496  | 
  val mk_int = HOLogic.mk_number @{typ int} o @{code integer_of_int};
 | 
| 58988 | 3497  | 
  fun dest_int (@{term int_of_integer} $ j) = @{code int_of_integer} (snd (HOLogic.dest_number j))
 | 
3498  | 
    | dest_int i = @{code int_of_integer} (snd (HOLogic.dest_number i));
 | 
|
| 
51143
 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 
haftmann 
parents: 
49962 
diff
changeset
 | 
3499  | 
|
| 
36985
 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 
haftmann 
parents: 
36960 
diff
changeset
 | 
3500  | 
  fun term_of_float (@{code Float} (k, l)) =
 | 
| 
51143
 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 
haftmann 
parents: 
49962 
diff
changeset
 | 
3501  | 
    @{term Float} $ mk_int k $ mk_int l;
 | 
| 
36985
 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 
haftmann 
parents: 
36960 
diff
changeset
 | 
3502  | 
|
| 
 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 
haftmann 
parents: 
36960 
diff
changeset
 | 
3503  | 
  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: 
36960 
diff
changeset
 | 
3504  | 
    | term_of_float_float_option (SOME ff) = @{term "Some :: float \<times> float \<Rightarrow> _"}
 | 
| 
59058
 
a78612c67ec0
renamed "pairself" to "apply2", in accordance to @{apply 2};
 
wenzelm 
parents: 
58988 
diff
changeset
 | 
3505  | 
$ HOLogic.mk_prod (apply2 term_of_float ff);  | 
| 
36985
 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 
haftmann 
parents: 
36960 
diff
changeset
 | 
3506  | 
|
| 
 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 
haftmann 
parents: 
36960 
diff
changeset
 | 
3507  | 
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: 
36960 
diff
changeset
 | 
3508  | 
    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: 
36960 
diff
changeset
 | 
3509  | 
|
| 
51143
 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 
haftmann 
parents: 
49962 
diff
changeset
 | 
3510  | 
  fun nat_of_term t = @{code nat_of_integer}
 | 
| 
 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 
haftmann 
parents: 
49962 
diff
changeset
 | 
3511  | 
(HOLogic.dest_nat t handle TERM _ => snd (HOLogic.dest_number t));  | 
| 
36985
 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 
haftmann 
parents: 
36960 
diff
changeset
 | 
3512  | 
|
| 
 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 
haftmann 
parents: 
36960 
diff
changeset
 | 
3513  | 
  fun float_of_term (@{term Float} $ k $ l) =
 | 
| 
51143
 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 
haftmann 
parents: 
49962 
diff
changeset
 | 
3514  | 
        @{code Float} (dest_int k, dest_int l)
 | 
| 
36985
 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 
haftmann 
parents: 
36960 
diff
changeset
 | 
3515  | 
| 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: 
36960 
diff
changeset
 | 
3516  | 
|
| 
 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 
haftmann 
parents: 
36960 
diff
changeset
 | 
3517  | 
  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: 
36960 
diff
changeset
 | 
3518  | 
    | 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: 
36960 
diff
changeset
 | 
3519  | 
    | 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: 
36960 
diff
changeset
 | 
3520  | 
    | 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: 
36960 
diff
changeset
 | 
3521  | 
    | 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: 
36960 
diff
changeset
 | 
3522  | 
    | 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: 
36960 
diff
changeset
 | 
3523  | 
    | 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: 
36960 
diff
changeset
 | 
3524  | 
    | 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: 
36960 
diff
changeset
 | 
3525  | 
    | 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: 
36960 
diff
changeset
 | 
3526  | 
    | 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: 
36960 
diff
changeset
 | 
3527  | 
    | 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: 
36960 
diff
changeset
 | 
3528  | 
    | 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: 
36960 
diff
changeset
 | 
3529  | 
    | 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: 
36960 
diff
changeset
 | 
3530  | 
    | 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: 
36960 
diff
changeset
 | 
3531  | 
        @{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: 
36960 
diff
changeset
 | 
3532  | 
    | 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: 
36960 
diff
changeset
 | 
3533  | 
    | 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: 
36960 
diff
changeset
 | 
3534  | 
| 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: 
36960 
diff
changeset
 | 
3535  | 
|
| 
 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 
haftmann 
parents: 
36960 
diff
changeset
 | 
3536  | 
  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: 
36960 
diff
changeset
 | 
3537  | 
(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: 
36960 
diff
changeset
 | 
3538  | 
    | 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: 
36960 
diff
changeset
 | 
3539  | 
(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: 
36960 
diff
changeset
 | 
3540  | 
    | 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: 
36960 
diff
changeset
 | 
3541  | 
(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: 
36960 
diff
changeset
 | 
3542  | 
    | 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: 
36960 
diff
changeset
 | 
3543  | 
(floatarith_of_term a, floatarith_of_term b)  | 
| 58986 | 3544  | 
    | form_of_term (@{term Conj} $ a $ b) = @{code Conj}
 | 
3545  | 
(form_of_term a, form_of_term b)  | 
|
3546  | 
    | form_of_term (@{term Disj} $ a $ b) = @{code Disj}
 | 
|
3547  | 
(form_of_term a, form_of_term b)  | 
|
| 
36985
 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 
haftmann 
parents: 
36960 
diff
changeset
 | 
3548  | 
    | 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: 
36960 
diff
changeset
 | 
3549  | 
(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: 
36960 
diff
changeset
 | 
3550  | 
| 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: 
36960 
diff
changeset
 | 
3551  | 
|
| 
 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 
haftmann 
parents: 
36960 
diff
changeset
 | 
3552  | 
  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: 
36960 
diff
changeset
 | 
3553  | 
    | float_float_option_of_term (@{term "Some :: float \<times> float \<Rightarrow> _"} $ ff) =
 | 
| 
59058
 
a78612c67ec0
renamed "pairself" to "apply2", in accordance to @{apply 2};
 
wenzelm 
parents: 
58988 
diff
changeset
 | 
3554  | 
SOME (apply2 float_of_term (HOLogic.dest_prod ff))  | 
| 
36985
 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 
haftmann 
parents: 
36960 
diff
changeset
 | 
3555  | 
    | 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: 
36960 
diff
changeset
 | 
3556  | 
(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: 
36960 
diff
changeset
 | 
3557  | 
| 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: 
36960 
diff
changeset
 | 
3558  | 
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: 
36960 
diff
changeset
 | 
3559  | 
        (@{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: 
36960 
diff
changeset
 | 
3560  | 
          @{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: 
36960 
diff
changeset
 | 
3561  | 
    | 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: 
36960 
diff
changeset
 | 
3562  | 
        @{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: 
36960 
diff
changeset
 | 
3563  | 
| 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: 
36960 
diff
changeset
 | 
3564  | 
(HOLogic.dest_list t);  | 
| 
 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 
haftmann 
parents: 
36960 
diff
changeset
 | 
3565  | 
|
| 
 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 
haftmann 
parents: 
36960 
diff
changeset
 | 
3566  | 
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: 
36960 
diff
changeset
 | 
3567  | 
|
| 
 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 
haftmann 
parents: 
36960 
diff
changeset
 | 
3568  | 
  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: 
36960 
diff
changeset
 | 
3569  | 
(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: 
36960 
diff
changeset
 | 
3570  | 
    | 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: 
36960 
diff
changeset
 | 
3571  | 
        @{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: 
36960 
diff
changeset
 | 
3572  | 
| 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: 
36960 
diff
changeset
 | 
3573  | 
|
| 
 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 
haftmann 
parents: 
36960 
diff
changeset
 | 
3574  | 
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: 
36960 
diff
changeset
 | 
3575  | 
   of @{typ bool} =>
 | 
| 
 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 
haftmann 
parents: 
36960 
diff
changeset
 | 
3576  | 
(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: 
36960 
diff
changeset
 | 
3577  | 
    | @{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: 
36960 
diff
changeset
 | 
3578  | 
(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: 
36960 
diff
changeset
 | 
3579  | 
    | @{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: 
36960 
diff
changeset
 | 
3580  | 
(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: 
36960 
diff
changeset
 | 
3581  | 
| _ => bad t;  | 
| 
 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 
haftmann 
parents: 
36960 
diff
changeset
 | 
3582  | 
|
| 52131 | 3583  | 
val normalize = eval o Envir.beta_norm o Envir.eta_long [];  | 
| 
36985
 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 
haftmann 
parents: 
36960 
diff
changeset
 | 
3584  | 
|
| 
 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 
haftmann 
parents: 
36960 
diff
changeset
 | 
3585  | 
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: 
36960 
diff
changeset
 | 
3586  | 
*}  | 
| 
31099
 
03314c427b34
optimized Approximation by precompiling approx_inequality
 
hoelzl 
parents: 
31098 
diff
changeset
 | 
3587  | 
|
| 
 
03314c427b34
optimized Approximation by precompiling approx_inequality
 
hoelzl 
parents: 
31098 
diff
changeset
 | 
3588  | 
ML {*
 | 
| 32212 | 3589  | 
fun reorder_bounds_tac prems i =  | 
| 29805 | 3590  | 
let  | 
| 38558 | 3591  | 
      fun variable_of_bound (Const (@{const_name Trueprop}, _) $
 | 
| 37677 | 3592  | 
                             (Const (@{const_name Set.member}, _) $
 | 
| 
31811
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
3593  | 
Free (name, _) $ _)) = name  | 
| 38558 | 3594  | 
        | variable_of_bound (Const (@{const_name Trueprop}, _) $
 | 
| 
38864
 
4abe644fcea5
formerly unnamed infix equality now named HOL.eq
 
haftmann 
parents: 
38786 
diff
changeset
 | 
3595  | 
                             (Const (@{const_name HOL.eq}, _) $
 | 
| 
31811
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
3596  | 
Free (name, _) $ _)) = name  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
3597  | 
        | variable_of_bound t = raise TERM ("variable_of_bound", [t])
 | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
3598  | 
|
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
3599  | 
val variable_bounds  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
3600  | 
= map (` (variable_of_bound o prop_of)) prems  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
3601  | 
|
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
3602  | 
fun add_deps (name, bnds)  | 
| 32650 | 3603  | 
= Graph.add_deps_acyclic (name,  | 
3604  | 
remove (op =) name (Term.add_free_names (prop_of bnds) []))  | 
|
3605  | 
||
| 
31811
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
3606  | 
val order = Graph.empty  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
3607  | 
|> fold Graph.new_node variable_bounds  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
3608  | 
|> fold add_deps variable_bounds  | 
| 32650 | 3609  | 
|> Graph.strong_conn |> map the_single |> rev  | 
| 
31811
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
3610  | 
|> map_filter (AList.lookup (op =) variable_bounds)  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
3611  | 
|
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
3612  | 
fun prepend_prem th tac  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
3613  | 
        = tac THEN rtac (th RSN (2, @{thm mp})) i
 | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
3614  | 
in  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
3615  | 
fold prepend_prem order all_tac  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
3616  | 
end  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
3617  | 
|
| 
36985
 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 
haftmann 
parents: 
36960 
diff
changeset
 | 
3618  | 
fun approximation_conv ctxt ct =  | 
| 42361 | 3619  | 
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: 
36960 
diff
changeset
 | 
3620  | 
|
| 
 
41c5d4002f60
spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
 
haftmann 
parents: 
36960 
diff
changeset
 | 
3621  | 
fun approximate ctxt t =  | 
| 42361 | 3622  | 
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: 
36960 
diff
changeset
 | 
3623  | 
|> 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: 
36960 
diff
changeset
 | 
3624  | 
|
| 
31811
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
3625  | 
(* 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: 
36960 
diff
changeset
 | 
3626  | 
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: 
36960 
diff
changeset
 | 
3627  | 
(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: 
36960 
diff
changeset
 | 
3628  | 
THEN' rtac TrueI  | 
| 29805 | 3629  | 
|
| 39556 | 3630  | 
  val form_equations = @{thms interpret_form_equations};
 | 
| 
31811
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
3631  | 
|
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3632  | 
fun rewrite_interpret_form_tac ctxt prec splitting taylor i st = let  | 
| 46545 | 3633  | 
fun lookup_splitting (Free (name, _))  | 
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3634  | 
= case AList.lookup (op =) splitting name  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3635  | 
          of SOME s => HOLogic.mk_number @{typ nat} s
 | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3636  | 
           | NONE => @{term "0 :: nat"}
 | 
| 
31811
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
3637  | 
val vs = nth (prems_of st) (i - 1)  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
3638  | 
|> Logic.strip_imp_concl  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
3639  | 
|> HOLogic.dest_Trueprop  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
3640  | 
|> Term.strip_comb |> snd |> List.last  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
3641  | 
|> HOLogic.dest_list  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
3642  | 
val p = prec  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
3643  | 
              |> HOLogic.mk_number @{typ nat}
 | 
| 42361 | 3644  | 
|> Thm.cterm_of (Proof_Context.theory_of ctxt)  | 
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3645  | 
in case taylor  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3646  | 
of NONE => let  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3647  | 
val n = vs |> length  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3648  | 
                 |> HOLogic.mk_number @{typ nat}
 | 
| 42361 | 3649  | 
|> Thm.cterm_of (Proof_Context.theory_of ctxt)  | 
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3650  | 
val s = vs  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3651  | 
|> map lookup_splitting  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3652  | 
                 |> HOLogic.mk_list @{typ nat}
 | 
| 42361 | 3653  | 
|> Thm.cterm_of (Proof_Context.theory_of ctxt)  | 
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3654  | 
in  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3655  | 
         (rtac (Thm.instantiate ([], [(@{cpat "?n::nat"}, n),
 | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3656  | 
                                     (@{cpat "?prec::nat"}, p),
 | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3657  | 
                                     (@{cpat "?ss::nat list"}, s)])
 | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3658  | 
              @{thm "approx_form"}) i
 | 
| 52090 | 3659  | 
          THEN simp_tac (put_simpset (simpset_of @{context}) ctxt) i) st
 | 
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3660  | 
end  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3661  | 
|
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3662  | 
     | 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: 
31811 
diff
changeset
 | 
3663  | 
else let  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3664  | 
val t = t  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3665  | 
              |> HOLogic.mk_number @{typ nat}
 | 
| 42361 | 3666  | 
|> Thm.cterm_of (Proof_Context.theory_of ctxt)  | 
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3667  | 
val s = vs |> map lookup_splitting |> hd  | 
| 42361 | 3668  | 
|> Thm.cterm_of (Proof_Context.theory_of ctxt)  | 
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3669  | 
in  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3670  | 
         rtac (Thm.instantiate ([], [(@{cpat "?s::nat"}, s),
 | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3671  | 
                                     (@{cpat "?t::nat"}, t),
 | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3672  | 
                                     (@{cpat "?prec::nat"}, p)])
 | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3673  | 
              @{thm "approx_tse_form"}) i st
 | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3674  | 
end  | 
| 
31811
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
3675  | 
end  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
3676  | 
|
| 46545 | 3677  | 
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: 
31810 
diff
changeset
 | 
3678  | 
    error ("Bad free variable: " ^ Syntax.string_of_term ctxt t));
 | 
| 29805 | 3679  | 
*}  | 
3680  | 
||
| 
31811
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
3681  | 
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: 
31810 
diff
changeset
 | 
3682  | 
by auto  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
3683  | 
|
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
3684  | 
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: 
31810 
diff
changeset
 | 
3685  | 
by auto  | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
3686  | 
|
| 30549 | 3687  | 
method_setup approximation = {*
 | 
| 
36960
 
01594f816e3a
prefer structure Keyword, Parse, Parse_Spec, Outer_Syntax;
 
wenzelm 
parents: 
36778 
diff
changeset
 | 
3688  | 
Scan.lift Parse.nat  | 
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3689  | 
--  | 
| 
31811
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
3690  | 
Scan.optional (Scan.lift (Args.$$$ "splitting" |-- Args.colon)  | 
| 
36960
 
01594f816e3a
prefer structure Keyword, Parse, Parse_Spec, Outer_Syntax;
 
wenzelm 
parents: 
36778 
diff
changeset
 | 
3691  | 
|-- Parse.and_list' (free --| Scan.lift (Args.$$$ "=") -- Scan.lift Parse.nat)) []  | 
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3692  | 
--  | 
| 
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3693  | 
Scan.option (Scan.lift (Args.$$$ "taylor" |-- Args.colon)  | 
| 
36960
 
01594f816e3a
prefer structure Keyword, Parse, Parse_Spec, Outer_Syntax;
 
wenzelm 
parents: 
36778 
diff
changeset
 | 
3694  | 
|-- (free |-- Scan.lift (Args.$$$ "=") |-- Scan.lift Parse.nat))  | 
| 
31811
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
3695  | 
>>  | 
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3696  | 
(fn ((prec, splitting), taylor) => fn ctxt =>  | 
| 30549 | 3697  | 
SIMPLE_METHOD' (fn i =>  | 
| 
31811
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
3698  | 
      REPEAT (FIRST' [etac @{thm intervalE},
 | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
3699  | 
                      etac @{thm meta_eqE},
 | 
| 
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
3700  | 
                      rtac @{thm impI}] i)
 | 
| 52090 | 3701  | 
      THEN Subgoal.FOCUS (fn {prems, ...} => reorder_bounds_tac prems i) ctxt i
 | 
| 
59498
 
50b60f501b05
proper context for resolve_tac, eresolve_tac, dresolve_tac, forward_tac etc.;
 
wenzelm 
parents: 
59058 
diff
changeset
 | 
3702  | 
THEN DETERM (TRY (filter_prems_tac ctxt (K false) i))  | 
| 52286 | 3703  | 
THEN DETERM (Reification.tac ctxt form_equations NONE i)  | 
| 
31863
 
e391eee8bf14
Implemented taylor series expansion for approximation
 
hoelzl 
parents: 
31811 
diff
changeset
 | 
3704  | 
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: 
36960 
diff
changeset
 | 
3705  | 
THEN gen_eval_tac (approximation_conv ctxt) ctxt i))  | 
| 55506 | 3706  | 
*} "real number approximation"  | 
| 
31811
 
64dea9a15031
Improved computation of bounds and implemented interval splitting for 'approximation'.
 
hoelzl 
parents: 
31810 
diff
changeset
 | 
3707  | 
|
| 56813 | 3708  | 
ML_file "approximation.ML"  | 
3709  | 
||
| 58988 | 3710  | 
|
3711  | 
section "Quickcheck Generator"  | 
|
3712  | 
||
3713  | 
ML_file "approximation_generator.ML"  | 
|
3714  | 
||
3715  | 
setup "Approximation_Generator.setup"  | 
|
3716  | 
||
| 29805 | 3717  | 
end  |