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