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