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