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