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