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