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