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