src/HOL/Decision_Procs/Approximation.thy
author wenzelm
Mon, 24 Apr 2017 11:52:51 +0200
changeset 65573 0f3fdf689bf9
parent 65109 a79c1080f1e9
child 65578 e4997c181cce
permissions -rw-r--r--
clarified parent session images, to avoid duplicate loading of theories;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
58834
773b378d9313 more simp rules concerning dvd and even/odd
haftmann
parents: 58709
diff changeset
     1
 (* Author:     Johannes Hoelzl, TU Muenchen
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
     2
   Coercions removed by Dmitriy Traytel *)
30122
1c912a9d8200 standard headers;
wenzelm
parents: 29823
diff changeset
     3
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
     4
section \<open>Prove Real Valued Inequalities by Computation\<close>
30122
1c912a9d8200 standard headers;
wenzelm
parents: 29823
diff changeset
     5
40892
6f7292b94652 proper theory name (cf. e84f82418e09);
wenzelm
parents: 40881
diff changeset
     6
theory Approximation
41413
64cd30d6b0b8 explicit file specifications -- avoid secondary load path;
wenzelm
parents: 41024
diff changeset
     7
imports
64cd30d6b0b8 explicit file specifications -- avoid secondary load path;
wenzelm
parents: 41024
diff changeset
     8
  Complex_Main
64cd30d6b0b8 explicit file specifications -- avoid secondary load path;
wenzelm
parents: 41024
diff changeset
     9
  "~~/src/HOL/Library/Float"
51544
8c58fbbc1d5a tuned session specification;
wenzelm
parents: 51143
diff changeset
    10
  Dense_Linear_Order
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
    11
  "~~/src/HOL/Library/Code_Target_Numeral"
56923
c062543d380e prefer separate command for approximation
haftmann
parents: 56889
diff changeset
    12
keywords "approximate" :: diag
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
    13
begin
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
    14
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54269
diff changeset
    15
declare powr_neg_one [simp]
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54269
diff changeset
    16
declare powr_neg_numeral [simp]
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
    17
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
    18
section "Horner Scheme"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
    19
61586
5197a2ecb658 isabelle update_cartouches -c -t;
wenzelm
parents: 60680
diff changeset
    20
subsection \<open>Define auxiliary helper \<open>horner\<close> function\<close>
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
    21
31098
73dd67adf90a replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
hoelzl
parents: 30971
diff changeset
    22
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
    23
"horner F G 0 i k x       = 0" |
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
    24
"horner F G (Suc n) i k x = 1 / k - x * horner F G n (F i) (G i k) x"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
    25
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
    26
lemma horner_schema':
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
    27
  fixes x :: real and a :: "nat \<Rightarrow> real"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
    28
  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
    29
proof -
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
    30
  have shift_pow: "\<And>i. - (x * ((-1)^i * a (Suc i) * x ^ i)) = (-1)^(Suc i) * a (Suc i) * x ^ (Suc i)"
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
    31
    by auto
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
    32
  show ?thesis
64267
b9a1486e79be setsum -> sum
nipkow
parents: 64246
diff changeset
    33
    unfolding sum_distrib_left shift_pow uminus_add_conv_diff [symmetric] sum_negf[symmetric]
b9a1486e79be setsum -> sum
nipkow
parents: 64246
diff changeset
    34
    sum_head_upt_Suc[OF zero_less_Suc]
b9a1486e79be setsum -> sum
nipkow
parents: 64246
diff changeset
    35
    sum.reindex[OF inj_Suc, unfolded comp_def, symmetric, of "\<lambda> n. (-1)^n  *a n * x^n"] by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
    36
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
    37
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
    38
lemma horner_schema:
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
    39
  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
    40
  assumes f_Suc: "\<And>n. f (Suc n) = G ((F ^^ n) s) (f n)"
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
    41
  shows "horner F G n ((F ^^ j') s) (f j') x = (\<Sum> j = 0..< n. (- 1) ^ j * (1 / (f (j' + j))) * x ^ j)"
45129
1fce03e3e8ad tuned proofs -- eliminated vacuous "induct arbitrary: ..." situations;
wenzelm
parents: 44821
diff changeset
    42
proof (induct n arbitrary: j')
1fce03e3e8ad tuned proofs -- eliminated vacuous "induct arbitrary: ..." situations;
wenzelm
parents: 44821
diff changeset
    43
  case 0
1fce03e3e8ad tuned proofs -- eliminated vacuous "induct arbitrary: ..." situations;
wenzelm
parents: 44821
diff changeset
    44
  then show ?case by auto
1fce03e3e8ad tuned proofs -- eliminated vacuous "induct arbitrary: ..." situations;
wenzelm
parents: 44821
diff changeset
    45
next
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
    46
  case (Suc n)
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
    47
  show ?case unfolding horner.simps Suc[where j'="Suc j'", unfolded funpow.simps comp_def f_Suc]
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
    48
    using horner_schema'[of "\<lambda> j. 1 / (f (j' + j))"] by auto
45129
1fce03e3e8ad tuned proofs -- eliminated vacuous "induct arbitrary: ..." situations;
wenzelm
parents: 44821
diff changeset
    49
qed
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
    50
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
    51
lemma horner_bounds':
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
    52
  fixes lb :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> float \<Rightarrow> float" and ub :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> float \<Rightarrow> float"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
    53
  assumes "0 \<le> real_of_float x" and f_Suc: "\<And>n. f (Suc n) = G ((F ^^ n) s) (f n)"
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
    54
    and lb_0: "\<And> i k x. lb 0 i k x = 0"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
    55
    and lb_Suc: "\<And> n i k x. lb (Suc n) i k x = float_plus_down prec
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
    56
        (lapprox_rat prec 1 k)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
    57
        (- float_round_up prec (x * (ub n (F i) (G i k) x)))"
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
    58
    and ub_0: "\<And> i k x. ub 0 i k x = 0"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
    59
    and ub_Suc: "\<And> n i k x. ub (Suc n) i k x = float_plus_up prec
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
    60
        (rapprox_rat prec 1 k)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
    61
        (- float_round_down prec (x * (lb n (F i) (G i k) x)))"
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
    62
  shows "(lb n ((F ^^ j') s) (f j') x) \<le> horner F G n ((F ^^ j') s) (f j') x \<and>
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
    63
         horner F G n ((F ^^ j') s) (f j') x \<le> (ub n ((F ^^ j') s) (f j') x)"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
    64
  (is "?lb n j' \<le> ?horner n j' \<and> ?horner n j' \<le> ?ub n j'")
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
    65
proof (induct n arbitrary: j')
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
    66
  case 0
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
    67
  thus ?case unfolding lb_0 ub_0 horner.simps by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
    68
next
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
    69
  case (Suc n)
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
    70
  thus ?case using lapprox_rat[of prec 1 "f j'"] using rapprox_rat[of 1 "f j'" prec]
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
    71
    Suc[where j'="Suc j'"] \<open>0 \<le> real_of_float x\<close>
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
    72
    by (auto intro!: add_mono mult_left_mono float_round_down_le float_round_up_le
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
    73
      order_trans[OF add_mono[OF _ float_plus_down_le]]
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
    74
      order_trans[OF _ add_mono[OF _ float_plus_up_le]]
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
    75
      simp add: lb_Suc ub_Suc field_simps f_Suc)
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
    76
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
    77
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
    78
subsection "Theorems for floating point functions implementing the horner scheme"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
    79
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
    80
text \<open>
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
    81
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
    82
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
    83
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
    84
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
    85
\<close>
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
    86
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
    87
lemma horner_bounds:
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
    88
  fixes F :: "nat \<Rightarrow> nat" and G :: "nat \<Rightarrow> nat \<Rightarrow> nat"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
    89
  assumes "0 \<le> real_of_float x" and f_Suc: "\<And>n. f (Suc n) = G ((F ^^ n) s) (f n)"
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
    90
    and lb_0: "\<And> i k x. lb 0 i k x = 0"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
    91
    and lb_Suc: "\<And> n i k x. lb (Suc n) i k x = float_plus_down prec
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
    92
        (lapprox_rat prec 1 k)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
    93
        (- float_round_up prec (x * (ub n (F i) (G i k) x)))"
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
    94
    and ub_0: "\<And> i k x. ub 0 i k x = 0"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
    95
    and ub_Suc: "\<And> n i k x. ub (Suc n) i k x = float_plus_up prec
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
    96
        (rapprox_rat prec 1 k)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
    97
        (- float_round_down prec (x * (lb n (F i) (G i k) x)))"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
    98
  shows "(lb n ((F ^^ j') s) (f j') x) \<le> (\<Sum>j=0..<n. (- 1) ^ j * (1 / (f (j' + j))) * (x ^ j))"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
    99
      (is "?lb")
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   100
    and "(\<Sum>j=0..<n. (- 1) ^ j * (1 / (f (j' + j))) * (x ^ j)) \<le> (ub n ((F ^^ j') s) (f j') x)"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   101
      (is "?ub")
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   102
proof -
31809
hoelzl
parents: 31790
diff changeset
   103
  have "?lb  \<and> ?ub"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   104
    using horner_bounds'[where lb=lb, OF \<open>0 \<le> real_of_float x\<close> f_Suc lb_0 lb_Suc ub_0 ub_Suc]
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   105
    unfolding horner_schema[where f=f, OF f_Suc] by simp
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   106
  thus "?lb" and "?ub" by auto
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   107
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   108
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
   109
lemma horner_bounds_nonpos:
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
   110
  fixes F :: "nat \<Rightarrow> nat" and G :: "nat \<Rightarrow> nat \<Rightarrow> nat"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   111
  assumes "real_of_float x \<le> 0" and f_Suc: "\<And>n. f (Suc n) = G ((F ^^ n) s) (f n)"
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
   112
    and lb_0: "\<And> i k x. lb 0 i k x = 0"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   113
    and lb_Suc: "\<And> n i k x. lb (Suc n) i k x = float_plus_down prec
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   114
        (lapprox_rat prec 1 k)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   115
        (float_round_down prec (x * (ub n (F i) (G i k) x)))"
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
   116
    and ub_0: "\<And> i k x. ub 0 i k x = 0"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   117
    and ub_Suc: "\<And> n i k x. ub (Suc n) i k x = float_plus_up prec
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   118
        (rapprox_rat prec 1 k)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   119
        (float_round_up prec (x * (lb n (F i) (G i k) x)))"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   120
  shows "(lb n ((F ^^ j') s) (f j') x) \<le> (\<Sum>j=0..<n. (1 / (f (j' + j))) * real_of_float x ^ j)" (is "?lb")
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   121
    and "(\<Sum>j=0..<n. (1 / (f (j' + j))) * real_of_float x ^ j) \<le> (ub n ((F ^^ j') s) (f j') x)" (is "?ub")
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   122
proof -
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   123
  have diff_mult_minus: "x - y * z = x + - y * z" for x y z :: float by simp
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   124
  have sum_eq: "(\<Sum>j=0..<n. (1 / (f (j' + j))) * real_of_float x ^ j) =
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   125
    (\<Sum>j = 0..<n. (- 1) ^ j * (1 / (f (j' + j))) * real_of_float (- x) ^ j)"
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
   126
    by (auto simp add: field_simps power_mult_distrib[symmetric])
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   127
  have "0 \<le> real_of_float (-x)" using assms by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   128
  from horner_bounds[where G=G and F=F and f=f and s=s and prec=prec
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   129
    and lb="\<lambda> n i k x. lb n i k (-x)" and ub="\<lambda> n i k x. ub n i k (-x)",
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   130
    unfolded lb_Suc ub_Suc diff_mult_minus,
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   131
    OF this f_Suc lb_0 _ ub_0 _]
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   132
  show "?lb" and "?ub" unfolding minus_minus sum_eq
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   133
    by (auto simp: minus_float_round_up_eq minus_float_round_down_eq)
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   134
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   135
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   136
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
   137
subsection \<open>Selectors for next even or odd number\<close>
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
   138
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
   139
text \<open>
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   140
The horner scheme computes alternating series. To get the upper and lower bounds we need to
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   141
guarantee to access a even or odd member. To do this we use @{term get_odd} and @{term get_even}.
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
   142
\<close>
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   143
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   144
definition get_odd :: "nat \<Rightarrow> nat" where
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   145
  "get_odd n = (if odd n then n else (Suc n))"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   146
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   147
definition get_even :: "nat \<Rightarrow> nat" where
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   148
  "get_even n = (if even n then n else (Suc n))"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   149
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   150
lemma get_odd[simp]: "odd (get_odd n)"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   151
  unfolding get_odd_def by (cases "odd n") auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   152
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   153
lemma get_even[simp]: "even (get_even n)"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   154
  unfolding get_even_def by (cases "even n") auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   155
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   156
lemma get_odd_ex: "\<exists> k. Suc k = get_odd n \<and> odd (Suc k)"
54269
dcdfec41a325 tuned proofs in Approximation
hoelzl
parents: 54230
diff changeset
   157
  by (auto simp: get_odd_def odd_pos intro!: exI[of _ "n - 1"])
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   158
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   159
lemma get_even_double: "\<exists>i. get_even n = 2 * i"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   160
  using get_even by (blast elim: evenE)
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   161
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   162
lemma get_odd_double: "\<exists>i. get_odd n = 2 * i + 1"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   163
  using get_odd by (blast elim: oddE)
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   164
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   165
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   166
section "Power function"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   167
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   168
definition float_power_bnds :: "nat \<Rightarrow> nat \<Rightarrow> float \<Rightarrow> float \<Rightarrow> float * float" where
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   169
"float_power_bnds prec n l u =
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   170
  (if 0 < l then (power_down_fl prec l n, power_up_fl prec u n)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   171
  else if odd n then
61945
1135b8de26c3 more symbols;
wenzelm
parents: 61824
diff changeset
   172
    (- power_up_fl prec \<bar>l\<bar> n,
1135b8de26c3 more symbols;
wenzelm
parents: 61824
diff changeset
   173
      if u < 0 then - power_down_fl prec \<bar>u\<bar> n else power_up_fl prec u n)
1135b8de26c3 more symbols;
wenzelm
parents: 61824
diff changeset
   174
  else if u < 0 then (power_down_fl prec \<bar>u\<bar> n, power_up_fl prec \<bar>l\<bar> n)
1135b8de26c3 more symbols;
wenzelm
parents: 61824
diff changeset
   175
  else (0, power_up_fl prec (max \<bar>l\<bar> \<bar>u\<bar>) n))"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   176
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   177
lemma le_minus_power_downI: "0 \<le> x \<Longrightarrow> x ^ n \<le> - a \<Longrightarrow> a \<le> - power_down prec x n"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   178
  by (subst le_minus_iff) (auto intro: power_down_le power_mono_odd)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   179
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   180
lemma float_power_bnds:
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   181
  "(l1, u1) = float_power_bnds prec n l u \<Longrightarrow> x \<in> {l .. u} \<Longrightarrow> (x::real) ^ n \<in> {l1..u1}"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   182
  by (auto
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   183
    simp: float_power_bnds_def max_def real_power_up_fl real_power_down_fl minus_le_iff
62390
842917225d56 more canonical names
nipkow
parents: 62200
diff changeset
   184
    split: if_split_asm
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   185
    intro!: power_up_le power_down_le le_minus_power_downI
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   186
    intro: power_mono_odd power_mono power_mono_even zero_le_even_power)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   187
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   188
lemma bnds_power:
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   189
  "\<forall>(x::real) l u. (l1, u1) = float_power_bnds prec n l u \<and> x \<in> {l .. u} \<longrightarrow>
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   190
    l1 \<le> x ^ n \<and> x ^ n \<le> u1"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   191
  using float_power_bnds by auto
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   192
62200
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
   193
section \<open>Approximation utility functions\<close>
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
   194
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
   195
definition bnds_mult :: "nat \<Rightarrow> float \<Rightarrow> float \<Rightarrow> float \<Rightarrow> float \<Rightarrow> float \<times> float" where
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
   196
  "bnds_mult prec a1 a2 b1 b2 =
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
   197
      (float_plus_down prec (nprt a1 * pprt b2)
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
   198
          (float_plus_down prec (nprt a2 * nprt b2)
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
   199
            (float_plus_down prec (pprt a1 * pprt b1) (pprt a2 * nprt b1))),
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
   200
        float_plus_up prec (pprt a2 * pprt b2)
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
   201
            (float_plus_up prec (pprt a1 * nprt b2)
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
   202
              (float_plus_up prec (nprt a2 * pprt b1) (nprt a1 * nprt b1))))"
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
   203
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
   204
lemma bnds_mult:
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
   205
  fixes prec :: nat and a1 aa2 b1 b2 :: float
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
   206
  assumes "(l, u) = bnds_mult prec a1 a2 b1 b2"
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
   207
  assumes "a \<in> {real_of_float a1..real_of_float a2}"
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
   208
  assumes "b \<in> {real_of_float b1..real_of_float b2}"
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
   209
  shows   "a * b \<in> {real_of_float l..real_of_float u}"
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
   210
proof -
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
   211
  from assms have "real_of_float l \<le> a * b" 
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
   212
    by (intro order.trans[OF _ mult_ge_prts[of a1 a a2 b1 b b2]])
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
   213
       (auto simp: bnds_mult_def intro!: float_plus_down_le)
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
   214
  moreover from assms have "real_of_float u \<ge> a * b" 
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
   215
    by (intro order.trans[OF mult_le_prts[of a1 a a2 b1 b b2]])
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
   216
       (auto simp: bnds_mult_def intro!: float_plus_up_le)
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
   217
  ultimately show ?thesis by simp
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
   218
qed
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
   219
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
   220
definition map_bnds :: "(nat \<Rightarrow> float \<Rightarrow> float) \<Rightarrow> (nat \<Rightarrow> float \<Rightarrow> float) \<Rightarrow>
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
   221
                           nat \<Rightarrow> (float \<times> float) \<Rightarrow> (float \<times> float)" where
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
   222
  "map_bnds lb ub prec = (\<lambda>(l,u). (lb prec l, ub prec u))"
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
   223
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
   224
lemma map_bnds:
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
   225
  assumes "(lf, uf) = map_bnds lb ub prec (l, u)"
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
   226
  assumes "mono f"
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
   227
  assumes "x \<in> {real_of_float l..real_of_float u}"
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
   228
  assumes "real_of_float (lb prec l) \<le> f (real_of_float l)"
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
   229
  assumes "real_of_float (ub prec u) \<ge> f (real_of_float u)"
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
   230
  shows   "f x \<in> {real_of_float lf..real_of_float uf}"
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
   231
proof -
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
   232
  from assms have "real_of_float lf = real_of_float (lb prec l)"
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
   233
    by (simp add: map_bnds_def)
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
   234
  also have "real_of_float (lb prec l) \<le> f (real_of_float l)"  by fact
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
   235
  also from assms have "\<dots> \<le> f x"
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
   236
    by (intro monoD[OF \<open>mono f\<close>]) auto
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
   237
  finally have lf: "real_of_float lf \<le> f x" .
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
   238
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
   239
  from assms have "f x \<le> f (real_of_float u)"
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
   240
    by (intro monoD[OF \<open>mono f\<close>]) auto
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
   241
  also have "\<dots> \<le> real_of_float (ub prec u)" by fact
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
   242
  also from assms have "\<dots> = real_of_float uf"
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
   243
    by (simp add: map_bnds_def)
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
   244
  finally have uf: "f x \<le> real_of_float uf" .
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
   245
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
   246
  from lf uf show ?thesis by simp
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
   247
qed
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
   248
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   249
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   250
section "Square root"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   251
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
   252
text \<open>
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   253
The square root computation is implemented as newton iteration. As first first step we use the
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   254
nearest power of two greater than the square root.
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
   255
\<close>
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   256
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   257
fun sqrt_iteration :: "nat \<Rightarrow> nat \<Rightarrow> float \<Rightarrow> float" where
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
   258
"sqrt_iteration prec 0 x = Float 1 ((bitlen \<bar>mantissa x\<bar> + exponent x) div 2 + 1)" |
31467
f7d2aa438bee Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
hoelzl
parents: 31148
diff changeset
   259
"sqrt_iteration prec (Suc m) x = (let y = sqrt_iteration prec m x
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   260
                                  in Float 1 (- 1) * float_plus_up prec y (float_divr prec x y))"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   261
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
   262
lemma compute_sqrt_iteration_base[code]:
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
   263
  shows "sqrt_iteration prec n (Float m e) =
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
   264
    (if n = 0 then Float 1 ((if m = 0 then 0 else bitlen \<bar>m\<bar> + e) div 2 + 1)
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
   265
    else (let y = sqrt_iteration prec (n - 1) (Float m e) in
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   266
      Float 1 (- 1) * float_plus_up prec y (float_divr prec (Float m e) y)))"
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
   267
  using bitlen_Float by (cases n) simp_all
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
   268
31467
f7d2aa438bee Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
hoelzl
parents: 31148
diff changeset
   269
function ub_sqrt lb_sqrt :: "nat \<Rightarrow> float \<Rightarrow> float" where
f7d2aa438bee Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
hoelzl
parents: 31148
diff changeset
   270
"ub_sqrt prec x = (if 0 < x then (sqrt_iteration prec prec x)
f7d2aa438bee Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
hoelzl
parents: 31148
diff changeset
   271
              else if x < 0 then - lb_sqrt prec (- x)
f7d2aa438bee Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
hoelzl
parents: 31148
diff changeset
   272
                            else 0)" |
f7d2aa438bee Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
hoelzl
parents: 31148
diff changeset
   273
"lb_sqrt prec x = (if 0 < x then (float_divl prec x (sqrt_iteration prec prec x))
f7d2aa438bee Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
hoelzl
parents: 31148
diff changeset
   274
              else if x < 0 then - ub_sqrt prec (- x)
f7d2aa438bee Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
hoelzl
parents: 31148
diff changeset
   275
                            else 0)"
f7d2aa438bee Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
hoelzl
parents: 31148
diff changeset
   276
by pat_completeness auto
55414
eab03e9cee8a renamed '{prod,sum,bool,unit}_case' to 'case_...'
blanchet
parents: 55413
diff changeset
   277
termination by (relation "measure (\<lambda> v. let (prec, x) = case_sum id id v in (if x < 0 then 1 else 0))", auto)
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   278
31467
f7d2aa438bee Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
hoelzl
parents: 31148
diff changeset
   279
declare lb_sqrt.simps[simp del]
f7d2aa438bee Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
hoelzl
parents: 31148
diff changeset
   280
declare ub_sqrt.simps[simp del]
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   281
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   282
lemma sqrt_ub_pos_pos_1:
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   283
  assumes "sqrt x < b" and "0 < b" and "0 < x"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   284
  shows "sqrt x < (b + x / b)/2"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   285
proof -
53077
a1b3784f8129 more symbols;
wenzelm
parents: 52286
diff changeset
   286
  from assms have "0 < (b - sqrt x)\<^sup>2 " by simp
a1b3784f8129 more symbols;
wenzelm
parents: 52286
diff changeset
   287
  also have "\<dots> = b\<^sup>2 - 2 * b * sqrt x + (sqrt x)\<^sup>2" by algebra
a1b3784f8129 more symbols;
wenzelm
parents: 52286
diff changeset
   288
  also have "\<dots> = b\<^sup>2 - 2 * b * sqrt x + x" using assms by simp
a1b3784f8129 more symbols;
wenzelm
parents: 52286
diff changeset
   289
  finally have "0 < b\<^sup>2 - 2 * b * sqrt x + x" .
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   290
  hence "0 < b / 2 - sqrt x + x / (2 * b)" using assms
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   291
    by (simp add: field_simps power2_eq_square)
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   292
  thus ?thesis by (simp add: field_simps)
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   293
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   294
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   295
lemma sqrt_iteration_bound:
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   296
  assumes "0 < real_of_float x"
54269
dcdfec41a325 tuned proofs in Approximation
hoelzl
parents: 54230
diff changeset
   297
  shows "sqrt x < sqrt_iteration prec n x"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   298
proof (induct n)
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   299
  case 0
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   300
  show ?case
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   301
  proof (cases x)
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   302
    case (Float m e)
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   303
    hence "0 < m"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   304
      using assms
60017
b785d6d06430 Overloading of ln and powr, but "approximation" no longer works for powr. Code generation also fails due to type ambiguity in scala.
paulson <lp15@cam.ac.uk>
parents: 59850
diff changeset
   305
      apply (auto simp: sign_simps)
b785d6d06430 Overloading of ln and powr, but "approximation" no longer works for powr. Code generation also fails due to type ambiguity in scala.
paulson <lp15@cam.ac.uk>
parents: 59850
diff changeset
   306
      by (meson not_less powr_ge_pzero)
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   307
    hence "0 < sqrt m" by auto
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   308
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   309
    have int_nat_bl: "(nat (bitlen m)) = bitlen m"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   310
      using bitlen_nonneg by auto
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
   311
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
   312
    have "x = (m / 2^nat (bitlen m)) * 2 powr (e + (nat (bitlen m)))"
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
   313
      unfolding Float by (auto simp: powr_realpow[symmetric] field_simps powr_add)
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
   314
    also have "\<dots> < 1 * 2 powr (e + nat (bitlen m))"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   315
    proof (rule mult_strict_right_mono, auto)
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   316
      show "m < 2^nat (bitlen m)"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   317
        using bitlen_bounds[OF \<open>0 < m\<close>, THEN conjunct2]
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   318
        unfolding of_int_less_iff[of m, symmetric] by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   319
    qed
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   320
    finally have "sqrt x < sqrt (2 powr (e + bitlen m))"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   321
      unfolding int_nat_bl by auto
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
   322
    also have "\<dots> \<le> 2 powr ((e + bitlen m) div 2 + 1)"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   323
    proof -
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   324
      let ?E = "e + bitlen m"
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
   325
      have E_mod_pow: "2 powr (?E mod 2) < 4"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   326
      proof (cases "?E mod 2 = 1")
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   327
        case True
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   328
        thus ?thesis by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   329
      next
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
   330
        case False
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
   331
        have "0 \<le> ?E mod 2" by auto
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
   332
        have "?E mod 2 < 2" by auto
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
   333
        from this[THEN zless_imp_add1_zle]
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
   334
        have "?E mod 2 \<le> 0" using False by auto
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
   335
        from xt1(5)[OF \<open>0 \<le> ?E mod 2\<close> this]
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
   336
        show ?thesis by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   337
      qed
56889
48a745e1bde7 avoid the Complex constructor, use the more natural Re/Im view; moved csqrt to Complex.
hoelzl
parents: 56813
diff changeset
   338
      hence "sqrt (2 powr (?E mod 2)) < sqrt (2 * 2)"
48a745e1bde7 avoid the Complex constructor, use the more natural Re/Im view; moved csqrt to Complex.
hoelzl
parents: 56813
diff changeset
   339
        by (auto simp del: real_sqrt_four)
48a745e1bde7 avoid the Complex constructor, use the more natural Re/Im view; moved csqrt to Complex.
hoelzl
parents: 56813
diff changeset
   340
      hence E_mod_pow: "sqrt (2 powr (?E mod 2)) < 2" by auto
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
   341
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   342
      have E_eq: "2 powr ?E = 2 powr (?E div 2 + ?E div 2 + ?E mod 2)"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   343
        by auto
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
   344
      have "sqrt (2 powr ?E) = sqrt (2 powr (?E div 2) * 2 powr (?E div 2) * 2 powr (?E mod 2))"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   345
        unfolding E_eq unfolding powr_add[symmetric] by (metis of_int_add)
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
   346
      also have "\<dots> = 2 powr (?E div 2) * sqrt (2 powr (?E mod 2))"
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
   347
        unfolding real_sqrt_mult[of _ "2 powr (?E mod 2)"] real_sqrt_abs2 by auto
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
   348
      also have "\<dots> < 2 powr (?E div 2) * 2 powr 1"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   349
        by (rule mult_strict_left_mono) (auto intro: E_mod_pow)
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   350
      also have "\<dots> = 2 powr (?E div 2 + 1)"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   351
        unfolding add.commute[of _ 1] powr_add[symmetric] by simp
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   352
      finally show ?thesis by auto
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   353
    qed
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
   354
    finally show ?thesis using \<open>0 < m\<close>
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
   355
      unfolding Float
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   356
      by (subst compute_sqrt_iteration_base) (simp add: ac_simps)
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   357
  qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   358
next
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   359
  case (Suc n)
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   360
  let ?b = "sqrt_iteration prec n x"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   361
  have "0 < sqrt x"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   362
    using \<open>0 < real_of_float x\<close> by auto
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   363
  also have "\<dots> < real_of_float ?b"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   364
    using Suc .
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   365
  finally have "sqrt x < (?b + x / ?b)/2"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   366
    using sqrt_ub_pos_pos_1[OF Suc _ \<open>0 < real_of_float x\<close>] by auto
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   367
  also have "\<dots> \<le> (?b + (float_divr prec x ?b))/2"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   368
    by (rule divide_right_mono, auto simp add: float_divr)
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   369
  also have "\<dots> = (Float 1 (- 1)) * (?b + (float_divr prec x ?b))"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   370
    by simp
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   371
  also have "\<dots> \<le> (Float 1 (- 1)) * (float_plus_up prec ?b (float_divr prec x ?b))"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   372
    by (auto simp add: algebra_simps float_plus_up_le)
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   373
  finally show ?case
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   374
    unfolding sqrt_iteration.simps Let_def distrib_left .
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   375
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   376
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   377
lemma sqrt_iteration_lower_bound:
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   378
  assumes "0 < real_of_float x"
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   379
  shows "0 < real_of_float (sqrt_iteration prec n x)" (is "0 < ?sqrt")
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   380
proof -
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   381
  have "0 < sqrt x" using assms by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   382
  also have "\<dots> < ?sqrt" using sqrt_iteration_bound[OF assms] .
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   383
  finally show ?thesis .
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   384
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   385
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   386
lemma lb_sqrt_lower_bound:
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   387
  assumes "0 \<le> real_of_float x"
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   388
  shows "0 \<le> real_of_float (lb_sqrt prec x)"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   389
proof (cases "0 < x")
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   390
  case True
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   391
  hence "0 < real_of_float x" and "0 \<le> x"
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   392
    using \<open>0 \<le> real_of_float x\<close> by auto
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   393
  hence "0 < sqrt_iteration prec prec x"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   394
    using sqrt_iteration_lower_bound by auto
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   395
  hence "0 \<le> real_of_float (float_divl prec x (sqrt_iteration prec prec x))"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   396
    using float_divl_lower_bound[OF \<open>0 \<le> x\<close>] unfolding less_eq_float_def by auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   397
  thus ?thesis
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   398
    unfolding lb_sqrt.simps using True by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   399
next
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   400
  case False
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   401
  with \<open>0 \<le> real_of_float x\<close> have "real_of_float x = 0" by auto
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   402
  thus ?thesis
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   403
    unfolding lb_sqrt.simps by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   404
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   405
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
   406
lemma bnds_sqrt': "sqrt x \<in> {(lb_sqrt prec x) .. (ub_sqrt prec x)}"
31467
f7d2aa438bee Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
hoelzl
parents: 31148
diff changeset
   407
proof -
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   408
  have lb: "lb_sqrt prec x \<le> sqrt x" if "0 < x" for x :: float
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   409
  proof -
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   410
    from that have "0 < real_of_float x" and "0 \<le> real_of_float x" by auto
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   411
    hence sqrt_gt0: "0 < sqrt x" by auto
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   412
    hence sqrt_ub: "sqrt x < sqrt_iteration prec prec x"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   413
      using sqrt_iteration_bound by auto
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   414
    have "(float_divl prec x (sqrt_iteration prec prec x)) \<le>
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   415
          x / (sqrt_iteration prec prec x)" by (rule float_divl)
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   416
    also have "\<dots> < x / sqrt x"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   417
      by (rule divide_strict_left_mono[OF sqrt_ub \<open>0 < real_of_float x\<close>
31467
f7d2aa438bee Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
hoelzl
parents: 31148
diff changeset
   418
               mult_pos_pos[OF order_less_trans[OF sqrt_gt0 sqrt_ub] sqrt_gt0]])
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   419
    also have "\<dots> = sqrt x"
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   420
      unfolding inverse_eq_iff_eq[of _ "sqrt x", symmetric]
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   421
                sqrt_divide_self_eq[OF \<open>0 \<le> real_of_float x\<close>, symmetric] by auto
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   422
    finally show ?thesis
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   423
      unfolding lb_sqrt.simps if_P[OF \<open>0 < x\<close>] by auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   424
  qed
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   425
  have ub: "sqrt x \<le> ub_sqrt prec x" if "0 < x" for x :: float
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   426
  proof -
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   427
    from that have "0 < real_of_float x" by auto
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   428
    hence "0 < sqrt x" by auto
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   429
    hence "sqrt x < sqrt_iteration prec prec x"
31467
f7d2aa438bee Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
hoelzl
parents: 31148
diff changeset
   430
      using sqrt_iteration_bound by auto
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   431
    then show ?thesis
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   432
      unfolding ub_sqrt.simps if_P[OF \<open>0 < x\<close>] by auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   433
  qed
31467
f7d2aa438bee Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
hoelzl
parents: 31148
diff changeset
   434
  show ?thesis
54269
dcdfec41a325 tuned proofs in Approximation
hoelzl
parents: 54230
diff changeset
   435
    using lb[of "-x"] ub[of "-x"] lb[of x] ub[of x]
dcdfec41a325 tuned proofs in Approximation
hoelzl
parents: 54230
diff changeset
   436
    by (auto simp add: lb_sqrt.simps ub_sqrt.simps real_sqrt_minus)
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   437
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   438
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   439
lemma bnds_sqrt: "\<forall>(x::real) lx ux.
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   440
  (l, u) = (lb_sqrt prec lx, ub_sqrt prec ux) \<and> x \<in> {lx .. ux} \<longrightarrow> l \<le> sqrt x \<and> sqrt x \<le> u"
31467
f7d2aa438bee Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
hoelzl
parents: 31148
diff changeset
   441
proof ((rule allI) +, rule impI, erule conjE, rule conjI)
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   442
  fix x :: real
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   443
  fix lx ux
31467
f7d2aa438bee Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
hoelzl
parents: 31148
diff changeset
   444
  assume "(l, u) = (lb_sqrt prec lx, ub_sqrt prec ux)"
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   445
    and x: "x \<in> {lx .. ux}"
31467
f7d2aa438bee Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
hoelzl
parents: 31148
diff changeset
   446
  hence l: "l = lb_sqrt prec lx " and u: "u = ub_sqrt prec ux" by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   447
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   448
  have "sqrt lx \<le> sqrt x" using x by auto
31467
f7d2aa438bee Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
hoelzl
parents: 31148
diff changeset
   449
  from order_trans[OF _ this]
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   450
  show "l \<le> sqrt x" unfolding l using bnds_sqrt'[of lx prec] by auto
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   451
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   452
  have "sqrt x \<le> sqrt ux" using x by auto
31467
f7d2aa438bee Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
hoelzl
parents: 31148
diff changeset
   453
  from order_trans[OF this]
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   454
  show "sqrt x \<le> u" unfolding u using bnds_sqrt'[of ux prec] by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   455
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   456
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   457
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   458
section "Arcus tangens and \<pi>"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   459
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   460
subsection "Compute arcus tangens series"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   461
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
   462
text \<open>
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   463
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
   464
@{term "{-1 :: real .. 1}"}. This is used to compute \<pi> and then the entire arcus tangens.
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
   465
\<close>
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   466
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   467
fun ub_arctan_horner :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> float \<Rightarrow> float"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   468
and lb_arctan_horner :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> float \<Rightarrow> float" where
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   469
  "ub_arctan_horner prec 0 k x = 0"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   470
| "ub_arctan_horner prec (Suc n) k x = float_plus_up prec
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   471
      (rapprox_rat prec 1 k) (- float_round_down prec (x * (lb_arctan_horner prec n (k + 2) x)))"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   472
| "lb_arctan_horner prec 0 k x = 0"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   473
| "lb_arctan_horner prec (Suc n) k x = float_plus_down prec
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   474
      (lapprox_rat prec 1 k) (- float_round_up prec (x * (ub_arctan_horner prec n (k + 2) x)))"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   475
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
   476
lemma arctan_0_1_bounds':
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   477
  assumes "0 \<le> real_of_float y" "real_of_float y \<le> 1"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   478
    and "even n"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   479
  shows "arctan (sqrt y) \<in>
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   480
      {(sqrt y * lb_arctan_horner prec n 1 y) .. (sqrt y * ub_arctan_horner prec (Suc n) 1 y)}"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   481
proof -
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   482
  let ?c = "\<lambda>i. (- 1) ^ i * (1 / (i * 2 + (1::nat)) * sqrt y ^ (i * 2 + 1))"
54269
dcdfec41a325 tuned proofs in Approximation
hoelzl
parents: 54230
diff changeset
   483
  let ?S = "\<lambda>n. \<Sum> i=0..<n. ?c i"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   484
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   485
  have "0 \<le> sqrt y" using assms by auto
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   486
  have "sqrt y \<le> 1" using assms by auto
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
   487
  from \<open>even n\<close> obtain m where "2 * m = n" by (blast elim: evenE)
31809
hoelzl
parents: 31790
diff changeset
   488
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   489
  have "arctan (sqrt y) \<in> { ?S n .. ?S (Suc n) }"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   490
  proof (cases "sqrt y = 0")
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   491
    case True
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   492
    then show ?thesis by simp
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   493
  next
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   494
    case False
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
   495
    hence "0 < sqrt y" using \<open>0 \<le> sqrt y\<close> by auto
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   496
    hence prem: "0 < 1 / (0 * 2 + (1::nat)) * sqrt y ^ (0 * 2 + 1)" by auto
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   497
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
   498
    have "\<bar> sqrt y \<bar> \<le> 1"  using \<open>0 \<le> sqrt y\<close> \<open>sqrt y \<le> 1\<close> by auto
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   499
    from mp[OF summable_Leibniz(2)[OF zeroseq_arctan_series[OF this]
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
   500
      monoseq_arctan_series[OF this]] prem, THEN spec, of m, unfolded \<open>2 * m = n\<close>]
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
   501
    show ?thesis unfolding arctan_series[OF \<open>\<bar> sqrt y \<bar> \<le> 1\<close>] Suc_eq_plus1 atLeast0LessThan .
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   502
  qed
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   503
  note arctan_bounds = this[unfolded atLeastAtMost_iff]
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   504
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   505
  have F: "\<And>n. 2 * Suc n + 1 = 2 * n + 1 + 2" by auto
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   506
31809
hoelzl
parents: 31790
diff changeset
   507
  note bounds = horner_bounds[where s=1 and f="\<lambda>i. 2 * i + 1" and j'=0
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   508
    and lb="\<lambda>n i k x. lb_arctan_horner prec n k x"
31809
hoelzl
parents: 31790
diff changeset
   509
    and ub="\<lambda>n i k x. ub_arctan_horner prec n k x",
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   510
    OF \<open>0 \<le> real_of_float y\<close> F lb_arctan_horner.simps ub_arctan_horner.simps]
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   511
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   512
  have "(sqrt y * lb_arctan_horner prec n 1 y) \<le> arctan (sqrt y)"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   513
  proof -
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   514
    have "(sqrt y * lb_arctan_horner prec n 1 y) \<le> ?S n"
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
   515
      using bounds(1) \<open>0 \<le> sqrt y\<close>
64267
b9a1486e79be setsum -> sum
nipkow
parents: 64246
diff changeset
   516
      apply (simp only: power_add power_one_right mult.assoc[symmetric] sum_distrib_right[symmetric])
63170
eae6549dbea2 tuned proofs, to allow unfold_abs_def;
wenzelm
parents: 63040
diff changeset
   517
      apply (simp only: mult.commute[where 'a=real] mult.commute[of _ "2::nat"] power_mult)
eae6549dbea2 tuned proofs, to allow unfold_abs_def;
wenzelm
parents: 63040
diff changeset
   518
      apply (auto intro!: mult_left_mono)
eae6549dbea2 tuned proofs, to allow unfold_abs_def;
wenzelm
parents: 63040
diff changeset
   519
      done
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   520
    also have "\<dots> \<le> arctan (sqrt y)" using arctan_bounds ..
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   521
    finally show ?thesis .
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   522
  qed
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   523
  moreover
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   524
  have "arctan (sqrt y) \<le> (sqrt y * ub_arctan_horner prec (Suc n) 1 y)"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   525
  proof -
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   526
    have "arctan (sqrt y) \<le> ?S (Suc n)" using arctan_bounds ..
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   527
    also have "\<dots> \<le> (sqrt y * ub_arctan_horner prec (Suc n) 1 y)"
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
   528
      using bounds(2)[of "Suc n"] \<open>0 \<le> sqrt y\<close>
64267
b9a1486e79be setsum -> sum
nipkow
parents: 64246
diff changeset
   529
      apply (simp only: power_add power_one_right mult.assoc[symmetric] sum_distrib_right[symmetric])
63170
eae6549dbea2 tuned proofs, to allow unfold_abs_def;
wenzelm
parents: 63040
diff changeset
   530
      apply (simp only: mult.commute[where 'a=real] mult.commute[of _ "2::nat"] power_mult)
eae6549dbea2 tuned proofs, to allow unfold_abs_def;
wenzelm
parents: 63040
diff changeset
   531
      apply (auto intro!: mult_left_mono)
eae6549dbea2 tuned proofs, to allow unfold_abs_def;
wenzelm
parents: 63040
diff changeset
   532
      done
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   533
    finally show ?thesis .
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   534
  qed
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   535
  ultimately show ?thesis by auto
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   536
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   537
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   538
lemma arctan_0_1_bounds:
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   539
  assumes "0 \<le> real_of_float y" "real_of_float y \<le> 1"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   540
  shows "arctan (sqrt y) \<in>
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   541
    {(sqrt y * lb_arctan_horner prec (get_even n) 1 y) ..
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   542
      (sqrt y * ub_arctan_horner prec (get_odd n) 1 y)}"
54269
dcdfec41a325 tuned proofs in Approximation
hoelzl
parents: 54230
diff changeset
   543
  using
dcdfec41a325 tuned proofs in Approximation
hoelzl
parents: 54230
diff changeset
   544
    arctan_0_1_bounds'[OF assms, of n prec]
dcdfec41a325 tuned proofs in Approximation
hoelzl
parents: 54230
diff changeset
   545
    arctan_0_1_bounds'[OF assms, of "n + 1" prec]
dcdfec41a325 tuned proofs in Approximation
hoelzl
parents: 54230
diff changeset
   546
    arctan_0_1_bounds'[OF assms, of "n - 1" prec]
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   547
  by (auto simp: get_even_def get_odd_def odd_pos
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   548
    simp del: ub_arctan_horner.simps lb_arctan_horner.simps)
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   549
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   550
lemma arctan_lower_bound:
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   551
  assumes "0 \<le> x"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   552
  shows "x / (1 + x\<^sup>2) \<le> arctan x" (is "?l x \<le> _")
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   553
proof -
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   554
  have "?l x - arctan x \<le> ?l 0 - arctan 0"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   555
    using assms
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   556
    by (intro DERIV_nonpos_imp_nonincreasing[where f="\<lambda>x. ?l x - arctan x"])
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   557
      (auto intro!: derivative_eq_intros simp: add_nonneg_eq_0_iff field_simps)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   558
  thus ?thesis by simp
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   559
qed
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   560
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   561
lemma arctan_divide_mono: "0 < x \<Longrightarrow> x \<le> y \<Longrightarrow> arctan y / y \<le> arctan x / x"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   562
  by (rule DERIV_nonpos_imp_nonincreasing[where f="\<lambda>x. arctan x / x"])
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   563
    (auto intro!: derivative_eq_intros divide_nonpos_nonneg
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   564
      simp: inverse_eq_divide arctan_lower_bound)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   565
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   566
lemma arctan_mult_mono: "0 \<le> x \<Longrightarrow> x \<le> y \<Longrightarrow> x * arctan y \<le> y * arctan x"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   567
  using arctan_divide_mono[of x y] by (cases "x = 0") (simp_all add: field_simps)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   568
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   569
lemma arctan_mult_le:
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   570
  assumes "0 \<le> x" "x \<le> y" "y * z \<le> arctan y"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   571
  shows "x * z \<le> arctan x"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   572
proof (cases "x = 0")
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   573
  case True
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   574
  then show ?thesis by simp
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   575
next
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   576
  case False
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   577
  with assms have "z \<le> arctan y / y" by (simp add: field_simps)
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
   578
  also have "\<dots> \<le> arctan x / x" using assms \<open>x \<noteq> 0\<close> by (auto intro!: arctan_divide_mono)
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
   579
  finally show ?thesis using assms \<open>x \<noteq> 0\<close> by (simp add: field_simps)
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   580
qed
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   581
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   582
lemma arctan_le_mult:
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   583
  assumes "0 < x" "x \<le> y" "arctan x \<le> x * z"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   584
  shows "arctan y \<le> y * z"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   585
proof -
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   586
  from assms have "arctan y / y \<le> arctan x / x" by (auto intro!: arctan_divide_mono)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   587
  also have "\<dots> \<le> z" using assms by (auto simp: field_simps)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   588
  finally show ?thesis using assms by (simp add: field_simps)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   589
qed
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   590
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   591
lemma arctan_0_1_bounds_le:
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   592
  assumes "0 \<le> x" "x \<le> 1" "0 < real_of_float xl" "real_of_float xl \<le> x * x" "x * x \<le> real_of_float xu" "real_of_float xu \<le> 1"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   593
  shows "arctan x \<in>
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   594
      {x * lb_arctan_horner p1 (get_even n) 1 xu .. x * ub_arctan_horner p2 (get_odd n) 1 xl}"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   595
proof -
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   596
  from assms have "real_of_float xl \<le> 1" "sqrt (real_of_float xl) \<le> x" "x \<le> sqrt (real_of_float xu)" "0 \<le> real_of_float xu"
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   597
    "0 \<le> real_of_float xl" "0 < sqrt (real_of_float xl)"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   598
    by (auto intro!: real_le_rsqrt real_le_lsqrt simp: power2_eq_square)
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   599
  from arctan_0_1_bounds[OF \<open>0 \<le> real_of_float xu\<close>  \<open>real_of_float xu \<le> 1\<close>]
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   600
  have "sqrt (real_of_float xu) * real_of_float (lb_arctan_horner p1 (get_even n) 1 xu) \<le> arctan (sqrt (real_of_float xu))"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   601
    by simp
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
   602
  from arctan_mult_le[OF \<open>0 \<le> x\<close> \<open>x \<le> sqrt _\<close>  this]
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   603
  have "x * real_of_float (lb_arctan_horner p1 (get_even n) 1 xu) \<le> arctan x" .
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   604
  moreover
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   605
  from arctan_0_1_bounds[OF \<open>0 \<le> real_of_float xl\<close>  \<open>real_of_float xl \<le> 1\<close>]
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   606
  have "arctan (sqrt (real_of_float xl)) \<le> sqrt (real_of_float xl) * real_of_float (ub_arctan_horner p2 (get_odd n) 1 xl)"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   607
    by simp
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
   608
  from arctan_le_mult[OF \<open>0 < sqrt xl\<close> \<open>sqrt xl \<le> x\<close> this]
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   609
  have "arctan x \<le> x * real_of_float (ub_arctan_horner p2 (get_odd n) 1 xl)" .
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   610
  ultimately show ?thesis by simp
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   611
qed
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   612
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   613
lemma arctan_0_1_bounds_round:
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   614
  assumes "0 \<le> real_of_float x" "real_of_float x \<le> 1"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   615
  shows "arctan x \<in>
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   616
      {real_of_float x * lb_arctan_horner p1 (get_even n) 1 (float_round_up (Suc p2) (x * x)) ..
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   617
        real_of_float x * ub_arctan_horner p3 (get_odd n) 1 (float_round_down (Suc p4) (x * x))}"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   618
  using assms
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   619
  apply (cases "x > 0")
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   620
   apply (intro arctan_0_1_bounds_le)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   621
   apply (auto simp: float_round_down.rep_eq float_round_up.rep_eq
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   622
    intro!: truncate_up_le1 mult_le_one truncate_down_le truncate_up_le truncate_down_pos
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   623
      mult_pos_pos)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   624
  done
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   625
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   626
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   627
subsection "Compute \<pi>"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   628
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   629
definition ub_pi :: "nat \<Rightarrow> float" where
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   630
  "ub_pi prec =
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   631
    (let
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   632
      A = rapprox_rat prec 1 5 ;
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   633
      B = lapprox_rat prec 1 239
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   634
    in ((Float 1 2) * float_plus_up prec
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   635
      ((Float 1 2) * float_round_up prec (A * (ub_arctan_horner prec (get_odd (prec div 4 + 1)) 1
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   636
        (float_round_down (Suc prec) (A * A)))))
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   637
      (- float_round_down prec (B * (lb_arctan_horner prec (get_even (prec div 14 + 1)) 1
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   638
        (float_round_up (Suc prec) (B * B)))))))"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   639
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   640
definition lb_pi :: "nat \<Rightarrow> float" where
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   641
  "lb_pi prec =
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   642
    (let
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   643
      A = lapprox_rat prec 1 5 ;
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   644
      B = rapprox_rat prec 1 239
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   645
    in ((Float 1 2) * float_plus_down prec
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   646
      ((Float 1 2) * float_round_down prec (A * (lb_arctan_horner prec (get_even (prec div 4 + 1)) 1
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   647
        (float_round_up (Suc prec) (A * A)))))
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   648
      (- float_round_up prec (B * (ub_arctan_horner prec (get_odd (prec div 14 + 1)) 1
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   649
        (float_round_down (Suc prec) (B * B)))))))"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   650
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   651
lemma pi_boundaries: "pi \<in> {(lb_pi n) .. (ub_pi n)}"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   652
proof -
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   653
  have machin_pi: "pi = 4 * (4 * arctan (1 / 5) - arctan (1 / 239))"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   654
    unfolding machin[symmetric] by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   655
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   656
  {
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   657
    fix prec n :: nat
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   658
    fix k :: int
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   659
    assume "1 < k" hence "0 \<le> k" and "0 < k" and "1 \<le> k" by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   660
    let ?k = "rapprox_rat prec 1 k"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   661
    let ?kl = "float_round_down (Suc prec) (?k * ?k)"
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
   662
    have "1 div k = 0" using div_pos_pos_trivial[OF _ \<open>1 < k\<close>] by auto
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
   663
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   664
    have "0 \<le> real_of_float ?k" by (rule order_trans[OF _ rapprox_rat]) (auto simp add: \<open>0 \<le> k\<close>)
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   665
    have "real_of_float ?k \<le> 1"
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
   666
      by (auto simp add: \<open>0 < k\<close> \<open>1 \<le> k\<close> less_imp_le
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   667
        intro!: mult_le_one order_trans[OF _ rapprox_rat] rapprox_rat_le1)
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   668
    have "1 / k \<le> ?k" using rapprox_rat[where x=1 and y=k] by auto
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   669
    hence "arctan (1 / k) \<le> arctan ?k" by (rule arctan_monotone')
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   670
    also have "\<dots> \<le> (?k * ub_arctan_horner prec (get_odd n) 1 ?kl)"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   671
      using arctan_0_1_bounds_round[OF \<open>0 \<le> real_of_float ?k\<close> \<open>real_of_float ?k \<le> 1\<close>]
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   672
      by auto
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   673
    finally have "arctan (1 / k) \<le> ?k * ub_arctan_horner prec (get_odd n) 1 ?kl" .
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   674
  } note ub_arctan = this
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   675
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   676
  {
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   677
    fix prec n :: nat
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   678
    fix k :: int
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   679
    assume "1 < k" hence "0 \<le> k" and "0 < k" by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   680
    let ?k = "lapprox_rat prec 1 k"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   681
    let ?ku = "float_round_up (Suc prec) (?k * ?k)"
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
   682
    have "1 div k = 0" using div_pos_pos_trivial[OF _ \<open>1 < k\<close>] by auto
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
   683
    have "1 / k \<le> 1" using \<open>1 < k\<close> by auto
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   684
    have "0 \<le> real_of_float ?k" using lapprox_rat_nonneg[where x=1 and y=k, OF zero_le_one \<open>0 \<le> k\<close>]
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
   685
      by (auto simp add: \<open>1 div k = 0\<close>)
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   686
    have "0 \<le> real_of_float (?k * ?k)" by simp
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   687
    have "real_of_float ?k \<le> 1" using lapprox_rat by (rule order_trans, auto simp add: \<open>1 / k \<le> 1\<close>)
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   688
    hence "real_of_float (?k * ?k) \<le> 1" using \<open>0 \<le> real_of_float ?k\<close> by (auto intro!: mult_le_one)
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   689
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   690
    have "?k \<le> 1 / k" using lapprox_rat[where x=1 and y=k] by auto
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   691
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   692
    have "?k * lb_arctan_horner prec (get_even n) 1 ?ku \<le> arctan ?k"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   693
      using arctan_0_1_bounds_round[OF \<open>0 \<le> real_of_float ?k\<close> \<open>real_of_float ?k \<le> 1\<close>]
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   694
      by auto
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
   695
    also have "\<dots> \<le> arctan (1 / k)" using \<open>?k \<le> 1 / k\<close> by (rule arctan_monotone')
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   696
    finally have "?k * lb_arctan_horner prec (get_even n) 1 ?ku \<le> arctan (1 / k)" .
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   697
  } note lb_arctan = this
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   698
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   699
  have "pi \<le> ub_pi n "
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   700
    unfolding ub_pi_def machin_pi Let_def times_float.rep_eq Float_num
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   701
    using lb_arctan[of 239] ub_arctan[of 5] powr_realpow[of 2 2]
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   702
    by (intro mult_left_mono float_plus_up_le float_plus_down_le)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   703
      (auto intro!: mult_left_mono float_round_down_le float_round_up_le diff_mono)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   704
  moreover have "lb_pi n \<le> pi"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   705
    unfolding lb_pi_def machin_pi Let_def times_float.rep_eq Float_num
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   706
    using lb_arctan[of 5] ub_arctan[of 239]
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   707
    by (intro mult_left_mono float_plus_up_le float_plus_down_le)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   708
      (auto intro!: mult_left_mono float_round_down_le float_round_up_le diff_mono)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   709
  ultimately show ?thesis by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   710
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   711
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   712
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   713
subsection "Compute arcus tangens in the entire domain"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   714
31467
f7d2aa438bee Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
hoelzl
parents: 31148
diff changeset
   715
function lb_arctan :: "nat \<Rightarrow> float \<Rightarrow> float" and ub_arctan :: "nat \<Rightarrow> float \<Rightarrow> float" where
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   716
  "lb_arctan prec x =
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   717
    (let
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   718
      ub_horner = \<lambda> x. float_round_up prec
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   719
        (x *
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   720
          ub_arctan_horner prec (get_odd (prec div 4 + 1)) 1 (float_round_down (Suc prec) (x * x)));
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   721
      lb_horner = \<lambda> x. float_round_down prec
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   722
        (x *
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   723
          lb_arctan_horner prec (get_even (prec div 4 + 1)) 1 (float_round_up (Suc prec) (x * x)))
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   724
    in
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   725
      if x < 0 then - ub_arctan prec (-x)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   726
      else if x \<le> Float 1 (- 1) then lb_horner x
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   727
      else if x \<le> Float 1 1 then
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   728
        Float 1 1 *
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   729
        lb_horner
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   730
          (float_divl prec x
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   731
            (float_plus_up prec 1
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   732
              (ub_sqrt prec (float_plus_up prec 1 (float_round_up prec (x * x))))))
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   733
      else let inv = float_divr prec 1 x in
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   734
        if inv > 1 then 0
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   735
        else float_plus_down prec (lb_pi prec * Float 1 (- 1)) ( - ub_horner inv))"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   736
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   737
| "ub_arctan prec x =
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   738
    (let
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   739
      lb_horner = \<lambda> x. float_round_down prec
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   740
        (x *
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   741
          lb_arctan_horner prec (get_even (prec div 4 + 1)) 1 (float_round_up (Suc prec) (x * x))) ;
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   742
      ub_horner = \<lambda> x. float_round_up prec
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   743
        (x *
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   744
          ub_arctan_horner prec (get_odd (prec div 4 + 1)) 1 (float_round_down (Suc prec) (x * x)))
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   745
    in if x < 0 then - lb_arctan prec (-x)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   746
    else if x \<le> Float 1 (- 1) then ub_horner x
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   747
    else if x \<le> Float 1 1 then
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   748
      let y = float_divr prec x
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   749
        (float_plus_down
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   750
          (Suc prec) 1 (lb_sqrt prec (float_plus_down prec 1 (float_round_down prec (x * x)))))
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   751
      in if y > 1 then ub_pi prec * Float 1 (- 1) else Float 1 1 * ub_horner y
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   752
    else float_plus_up prec (ub_pi prec * Float 1 (- 1)) ( - lb_horner (float_divl prec 1 x)))"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   753
by pat_completeness auto
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   754
termination
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   755
by (relation "measure (\<lambda> v. let (prec, x) = case_sum id id v in (if x < 0 then 1 else 0))", auto)
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   756
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   757
declare ub_arctan_horner.simps[simp del]
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   758
declare lb_arctan_horner.simps[simp del]
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   759
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   760
lemma lb_arctan_bound':
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   761
  assumes "0 \<le> real_of_float x"
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   762
  shows "lb_arctan prec x \<le> arctan x"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   763
proof -
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   764
  have "\<not> x < 0" and "0 \<le> x"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   765
    using \<open>0 \<le> real_of_float x\<close> by (auto intro!: truncate_up_le )
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   766
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   767
  let "?ub_horner x" =
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   768
      "x * ub_arctan_horner prec (get_odd (prec div 4 + 1)) 1 (float_round_down (Suc prec) (x * x))"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   769
    and "?lb_horner x" =
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   770
      "x * lb_arctan_horner prec (get_even (prec div 4 + 1)) 1 (float_round_up (Suc prec) (x * x))"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   771
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   772
  show ?thesis
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
   773
  proof (cases "x \<le> Float 1 (- 1)")
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   774
    case True
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   775
    hence "real_of_float x \<le> 1" by simp
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   776
    from arctan_0_1_bounds_round[OF \<open>0 \<le> real_of_float x\<close> \<open>real_of_float x \<le> 1\<close>]
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   777
    show ?thesis
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
   778
      unfolding lb_arctan.simps Let_def if_not_P[OF \<open>\<not> x < 0\<close>] if_P[OF True] using \<open>0 \<le> x\<close>
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   779
      by (auto intro!: float_round_down_le)
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   780
  next
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   781
    case False
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   782
    hence "0 < real_of_float x" by auto
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   783
    let ?R = "1 + sqrt (1 + real_of_float x * real_of_float x)"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   784
    let ?sxx = "float_plus_up prec 1 (float_round_up prec (x * x))"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   785
    let ?fR = "float_plus_up prec 1 (ub_sqrt prec ?sxx)"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   786
    let ?DIV = "float_divl prec x ?fR"
31467
f7d2aa438bee Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
hoelzl
parents: 31148
diff changeset
   787
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   788
    have divisor_gt0: "0 < ?R" by (auto intro: add_pos_nonneg)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   789
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   790
    have "sqrt (1 + x*x) \<le> sqrt ?sxx"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   791
      by (auto simp: float_plus_up.rep_eq plus_up_def float_round_up.rep_eq intro!: truncate_up_le)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   792
    also have "\<dots> \<le> ub_sqrt prec ?sxx"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   793
      using bnds_sqrt'[of ?sxx prec] by auto
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   794
    finally
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   795
    have "sqrt (1 + x*x) \<le> ub_sqrt prec ?sxx" .
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   796
    hence "?R \<le> ?fR" by (auto simp: float_plus_up.rep_eq plus_up_def intro!: truncate_up_le)
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   797
    hence "0 < ?fR" and "0 < real_of_float ?fR" using \<open>0 < ?R\<close> by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   798
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   799
    have monotone: "?DIV \<le> x / ?R"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   800
    proof -
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   801
      have "?DIV \<le> real_of_float x / ?fR" by (rule float_divl)
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   802
      also have "\<dots> \<le> x / ?R" by (rule divide_left_mono[OF \<open>?R \<le> ?fR\<close> \<open>0 \<le> real_of_float x\<close> mult_pos_pos[OF order_less_le_trans[OF divisor_gt0 \<open>?R \<le> real_of_float ?fR\<close>] divisor_gt0]])
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   803
      finally show ?thesis .
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   804
    qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   806
    show ?thesis
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   807
    proof (cases "x \<le> Float 1 1")
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   808
      case True
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   809
      have "x \<le> sqrt (1 + x * x)"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   810
        using real_sqrt_sum_squares_ge2[where x=1, unfolded numeral_2_eq_2] by auto
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
   811
      also note \<open>\<dots> \<le> (ub_sqrt prec ?sxx)\<close>
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   812
      finally have "real_of_float x \<le> ?fR"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   813
        by (auto simp: float_plus_up.rep_eq plus_up_def intro!: truncate_up_le)
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   814
      moreover have "?DIV \<le> real_of_float x / ?fR"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   815
        by (rule float_divl)
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   816
      ultimately have "real_of_float ?DIV \<le> 1"
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   817
        unfolding divide_le_eq_1_pos[OF \<open>0 < real_of_float ?fR\<close>, symmetric] by auto
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   818
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   819
      have "0 \<le> real_of_float ?DIV"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   820
        using float_divl_lower_bound[OF \<open>0 \<le> x\<close>] \<open>0 < ?fR\<close>
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   821
        unfolding less_eq_float_def by auto
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
   822
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   823
      from arctan_0_1_bounds_round[OF \<open>0 \<le> real_of_float (?DIV)\<close> \<open>real_of_float (?DIV) \<le> 1\<close>]
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   824
      have "Float 1 1 * ?lb_horner ?DIV \<le> 2 * arctan ?DIV"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   825
        by simp
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   826
      also have "\<dots> \<le> 2 * arctan (x / ?R)"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   827
        using arctan_monotone'[OF monotone] by (auto intro!: mult_left_mono arctan_monotone')
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   828
      also have "2 * arctan (x / ?R) = arctan x"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   829
        using arctan_half[symmetric] unfolding numeral_2_eq_2 power_Suc2 power_0 mult_1_left .
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   830
      finally show ?thesis
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   831
        unfolding lb_arctan.simps Let_def if_not_P[OF \<open>\<not> x < 0\<close>]
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   832
          if_not_P[OF \<open>\<not> x \<le> Float 1 (- 1)\<close>] if_P[OF True]
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   833
        by (auto simp: float_round_down.rep_eq
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   834
          intro!: order_trans[OF mult_left_mono[OF truncate_down]])
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   835
    next
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   836
      case False
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   837
      hence "2 < real_of_float x" by auto
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   838
      hence "1 \<le> real_of_float x" by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   839
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   840
      let "?invx" = "float_divr prec 1 x"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   841
      have "0 \<le> arctan x" using arctan_monotone'[OF \<open>0 \<le> real_of_float x\<close>]
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   842
        using arctan_tan[of 0, unfolded tan_zero] by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   843
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   844
      show ?thesis
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   845
      proof (cases "1 < ?invx")
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
   846
        case True
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   847
        show ?thesis
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   848
          unfolding lb_arctan.simps Let_def if_not_P[OF \<open>\<not> x < 0\<close>]
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   849
            if_not_P[OF \<open>\<not> x \<le> Float 1 (- 1)\<close>] if_not_P[OF False] if_P[OF True]
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
   850
          using \<open>0 \<le> arctan x\<close> by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   851
      next
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
   852
        case False
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   853
        hence "real_of_float ?invx \<le> 1" by auto
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   854
        have "0 \<le> real_of_float ?invx"
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   855
          by (rule order_trans[OF _ float_divr]) (auto simp add: \<open>0 \<le> real_of_float x\<close>)
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   856
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   857
        have "1 / x \<noteq> 0" and "0 < 1 / x"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   858
          using \<open>0 < real_of_float x\<close> by auto
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   859
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   860
        have "arctan (1 / x) \<le> arctan ?invx"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   861
          unfolding one_float.rep_eq[symmetric] by (rule arctan_monotone', rule float_divr)
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   862
        also have "\<dots> \<le> ?ub_horner ?invx"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   863
          using arctan_0_1_bounds_round[OF \<open>0 \<le> real_of_float ?invx\<close> \<open>real_of_float ?invx \<le> 1\<close>]
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   864
          by (auto intro!: float_round_up_le)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   865
        also note float_round_up
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   866
        finally have "pi / 2 - float_round_up prec (?ub_horner ?invx) \<le> arctan x"
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
   867
          using \<open>0 \<le> arctan x\<close> arctan_inverse[OF \<open>1 / x \<noteq> 0\<close>]
61649
268d88ec9087 Tweaks for "real": Removal of [iff] status for some lemmas, adding [simp] for others. Plus fixes.
paulson <lp15@cam.ac.uk>
parents: 61610
diff changeset
   868
          unfolding sgn_pos[OF \<open>0 < 1 / real_of_float x\<close>] le_diff_eq by auto
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
   869
        moreover
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
   870
        have "lb_pi prec * Float 1 (- 1) \<le> pi / 2"
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
   871
          unfolding Float_num times_divide_eq_right mult_1_left using pi_boundaries by simp
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
   872
        ultimately
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   873
        show ?thesis
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   874
          unfolding lb_arctan.simps Let_def if_not_P[OF \<open>\<not> x < 0\<close>]
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   875
            if_not_P[OF \<open>\<not> x \<le> Float 1 (- 1)\<close>] if_not_P[OF \<open>\<not> x \<le> Float 1 1\<close>] if_not_P[OF False]
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   876
          by (auto intro!: float_plus_down_le)
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   877
      qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   878
    qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   879
  qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   880
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   881
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   882
lemma ub_arctan_bound':
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   883
  assumes "0 \<le> real_of_float x"
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   884
  shows "arctan x \<le> ub_arctan prec x"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   885
proof -
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   886
  have "\<not> x < 0" and "0 \<le> x"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   887
    using \<open>0 \<le> real_of_float x\<close> by auto
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   888
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   889
  let "?ub_horner x" =
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   890
    "float_round_up prec (x * ub_arctan_horner prec (get_odd (prec div 4 + 1)) 1 (float_round_down (Suc prec) (x * x)))"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   891
  let "?lb_horner x" =
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   892
    "float_round_down prec (x * lb_arctan_horner prec (get_even (prec div 4 + 1)) 1 (float_round_up (Suc prec) (x * x)))"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   893
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   894
  show ?thesis
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
   895
  proof (cases "x \<le> Float 1 (- 1)")
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   896
    case True
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   897
    hence "real_of_float x \<le> 1" by auto
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   898
    show ?thesis
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   899
      unfolding ub_arctan.simps Let_def if_not_P[OF \<open>\<not> x < 0\<close>] if_P[OF True]
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   900
      using arctan_0_1_bounds_round[OF \<open>0 \<le> real_of_float x\<close> \<open>real_of_float x \<le> 1\<close>]
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   901
      by (auto intro!: float_round_up_le)
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   902
  next
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   903
    case False
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   904
    hence "0 < real_of_float x" by auto
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   905
    let ?R = "1 + sqrt (1 + real_of_float x * real_of_float x)"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   906
    let ?sxx = "float_plus_down prec 1 (float_round_down prec (x * x))"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   907
    let ?fR = "float_plus_down (Suc prec) 1 (lb_sqrt prec ?sxx)"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   908
    let ?DIV = "float_divr prec x ?fR"
31467
f7d2aa438bee Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
hoelzl
parents: 31148
diff changeset
   909
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   910
    have sqr_ge0: "0 \<le> 1 + real_of_float x * real_of_float x"
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   911
      using sum_power2_ge_zero[of 1 "real_of_float x", unfolded numeral_2_eq_2] by auto
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   912
    hence "0 \<le> real_of_float (1 + x*x)" by auto
31467
f7d2aa438bee Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
hoelzl
parents: 31148
diff changeset
   913
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   914
    hence divisor_gt0: "0 < ?R" by (auto intro: add_pos_nonneg)
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   915
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   916
    have "lb_sqrt prec ?sxx \<le> sqrt ?sxx"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   917
      using bnds_sqrt'[of ?sxx] by auto
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   918
    also have "\<dots> \<le> sqrt (1 + x*x)"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   919
      by (auto simp: float_plus_down.rep_eq plus_down_def float_round_down.rep_eq truncate_down_le)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   920
    finally have "lb_sqrt prec ?sxx \<le> sqrt (1 + x*x)" .
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   921
    hence "?fR \<le> ?R"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   922
      by (auto simp: float_plus_down.rep_eq plus_down_def truncate_down_le)
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   923
    have "0 < real_of_float ?fR"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   924
      by (auto simp: float_plus_down.rep_eq plus_down_def float_round_down.rep_eq
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   925
        intro!: truncate_down_ge1 lb_sqrt_lower_bound order_less_le_trans[OF zero_less_one]
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   926
        truncate_down_nonneg add_nonneg_nonneg)
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   927
    have monotone: "x / ?R \<le> (float_divr prec x ?fR)"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   928
    proof -
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   929
      from divide_left_mono[OF \<open>?fR \<le> ?R\<close> \<open>0 \<le> real_of_float x\<close> mult_pos_pos[OF divisor_gt0 \<open>0 < real_of_float ?fR\<close>]]
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   930
      have "x / ?R \<le> x / ?fR" .
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   931
      also have "\<dots> \<le> ?DIV" by (rule float_divr)
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   932
      finally show ?thesis .
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   933
    qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   934
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   935
    show ?thesis
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   936
    proof (cases "x \<le> Float 1 1")
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   937
      case True
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   938
      show ?thesis
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   939
      proof (cases "?DIV > 1")
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
   940
        case True
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   941
        have "pi / 2 \<le> ub_pi prec * Float 1 (- 1)"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   942
          unfolding Float_num times_divide_eq_right mult_1_left using pi_boundaries by auto
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
   943
        from order_less_le_trans[OF arctan_ubound this, THEN less_imp_le]
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   944
        show ?thesis
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   945
          unfolding ub_arctan.simps Let_def if_not_P[OF \<open>\<not> x < 0\<close>]
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   946
            if_not_P[OF \<open>\<not> x \<le> Float 1 (- 1)\<close>] if_P[OF \<open>x \<le> Float 1 1\<close>] if_P[OF True] .
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   947
      next
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
   948
        case False
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   949
        hence "real_of_float ?DIV \<le> 1" by auto
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
   950
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   951
        have "0 \<le> x / ?R"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   952
          using \<open>0 \<le> real_of_float x\<close> \<open>0 < ?R\<close> unfolding zero_le_divide_iff by auto
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   953
        hence "0 \<le> real_of_float ?DIV"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   954
          using monotone by (rule order_trans)
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   955
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   956
        have "arctan x = 2 * arctan (x / ?R)"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   957
          using arctan_half unfolding numeral_2_eq_2 power_Suc2 power_0 mult_1_left .
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   958
        also have "\<dots> \<le> 2 * arctan (?DIV)"
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
   959
          using arctan_monotone'[OF monotone] by (auto intro!: mult_left_mono)
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
   960
        also have "\<dots> \<le> (Float 1 1 * ?ub_horner ?DIV)" unfolding Float_num
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   961
          using arctan_0_1_bounds_round[OF \<open>0 \<le> real_of_float ?DIV\<close> \<open>real_of_float ?DIV \<le> 1\<close>]
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   962
          by (auto intro!: float_round_up_le)
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   963
        finally show ?thesis
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   964
          unfolding ub_arctan.simps Let_def if_not_P[OF \<open>\<not> x < 0\<close>]
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   965
            if_not_P[OF \<open>\<not> x \<le> Float 1 (- 1)\<close>] if_P[OF \<open>x \<le> Float 1 1\<close>] if_not_P[OF False] .
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   966
      qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   967
    next
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   968
      case False
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   969
      hence "2 < real_of_float x" by auto
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   970
      hence "1 \<le> real_of_float x" by auto
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   971
      hence "0 < real_of_float x" by auto
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   972
      hence "0 < x" by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   973
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   974
      let "?invx" = "float_divl prec 1 x"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   975
      have "0 \<le> arctan x"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   976
        using arctan_monotone'[OF \<open>0 \<le> real_of_float x\<close>] and arctan_tan[of 0, unfolded tan_zero] by auto
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   977
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   978
      have "real_of_float ?invx \<le> 1"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   979
        unfolding less_float_def
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   980
        by (rule order_trans[OF float_divl])
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   981
          (auto simp add: \<open>1 \<le> real_of_float x\<close> divide_le_eq_1_pos[OF \<open>0 < real_of_float x\<close>])
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   982
      have "0 \<le> real_of_float ?invx"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   983
        using \<open>0 < x\<close> by (intro float_divl_lower_bound) auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   984
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   985
      have "1 / x \<noteq> 0" and "0 < 1 / x"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   986
        using \<open>0 < real_of_float x\<close> by auto
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   987
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   988
      have "(?lb_horner ?invx) \<le> arctan (?invx)"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
   989
        using arctan_0_1_bounds_round[OF \<open>0 \<le> real_of_float ?invx\<close> \<open>real_of_float ?invx \<le> 1\<close>]
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   990
        by (auto intro!: float_round_down_le)
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   991
      also have "\<dots> \<le> arctan (1 / x)"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   992
        unfolding one_float.rep_eq[symmetric] by (rule arctan_monotone') (rule float_divl)
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   993
      finally have "arctan x \<le> pi / 2 - (?lb_horner ?invx)"
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
   994
        using \<open>0 \<le> arctan x\<close> arctan_inverse[OF \<open>1 / x \<noteq> 0\<close>]
61649
268d88ec9087 Tweaks for "real": Removal of [iff] status for some lemmas, adding [simp] for others. Plus fixes.
paulson <lp15@cam.ac.uk>
parents: 61610
diff changeset
   995
        unfolding sgn_pos[OF \<open>0 < 1 / x\<close>] le_diff_eq by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   996
      moreover
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   997
      have "pi / 2 \<le> ub_pi prec * Float 1 (- 1)"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   998
        unfolding Float_num times_divide_eq_right mult_1_right
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
   999
        using pi_boundaries by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1000
      ultimately
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1001
      show ?thesis
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1002
        unfolding ub_arctan.simps Let_def if_not_P[OF \<open>\<not> x < 0\<close>]
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1003
          if_not_P[OF \<open>\<not> x \<le> Float 1 (- 1)\<close>] if_not_P[OF False]
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1004
        by (auto intro!: float_round_up_le float_plus_up_le)
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1005
    qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1006
  qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1007
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1008
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1009
lemma arctan_boundaries: "arctan x \<in> {(lb_arctan prec x) .. (ub_arctan prec x)}"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1010
proof (cases "0 \<le> x")
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1011
  case True
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1012
  hence "0 \<le> real_of_float x" by auto
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1013
  show ?thesis
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1014
    using ub_arctan_bound'[OF \<open>0 \<le> real_of_float x\<close>] lb_arctan_bound'[OF \<open>0 \<le> real_of_float x\<close>]
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1015
    unfolding atLeastAtMost_iff by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1016
next
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1017
  case False
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1018
  let ?mx = "-x"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1019
  from False have "x < 0" and "0 \<le> real_of_float ?mx"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1020
    by auto
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1021
  hence bounds: "lb_arctan prec ?mx \<le> arctan ?mx \<and> arctan ?mx \<le> ub_arctan prec ?mx"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1022
    using ub_arctan_bound'[OF \<open>0 \<le> real_of_float ?mx\<close>] lb_arctan_bound'[OF \<open>0 \<le> real_of_float ?mx\<close>] by auto
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1023
  show ?thesis
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1024
    unfolding minus_float.rep_eq arctan_minus lb_arctan.simps[where x=x]
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1025
      ub_arctan.simps[where x=x] Let_def if_P[OF \<open>x < 0\<close>]
47601
050718fe6eee use real :: float => real as lifting-morphism so we can directlry use the rep_eq theorems
hoelzl
parents: 47600
diff changeset
  1026
    unfolding atLeastAtMost_iff using bounds[unfolded minus_float.rep_eq arctan_minus]
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1027
    by (simp add: arctan_minus)
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1028
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1029
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1030
lemma bnds_arctan: "\<forall> (x::real) lx ux. (l, u) = (lb_arctan prec lx, ub_arctan prec ux) \<and> x \<in> {lx .. ux} \<longrightarrow> l \<le> arctan x \<and> arctan x \<le> u"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1031
proof (rule allI, rule allI, rule allI, rule impI)
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1032
  fix x :: real
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1033
  fix lx ux
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1034
  assume "(l, u) = (lb_arctan prec lx, ub_arctan prec ux) \<and> x \<in> {lx .. ux}"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1035
  hence l: "lb_arctan prec lx = l "
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1036
    and u: "ub_arctan prec ux = u"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1037
    and x: "x \<in> {lx .. ux}"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1038
    by auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1039
  show "l \<le> arctan x \<and> arctan x \<le> u"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1040
  proof
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1041
    show "l \<le> arctan x"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1042
    proof -
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1043
      from arctan_boundaries[of lx prec, unfolded l]
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1044
      have "l \<le> arctan lx" by (auto simp del: lb_arctan.simps)
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1045
      also have "\<dots> \<le> arctan x" using x by (auto intro: arctan_monotone')
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1046
      finally show ?thesis .
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1047
    qed
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1048
    show "arctan x \<le> u"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1049
    proof -
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1050
      have "arctan x \<le> arctan ux" using x by (auto intro: arctan_monotone')
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1051
      also have "\<dots> \<le> u" using arctan_boundaries[of ux prec, unfolded u] by (auto simp del: ub_arctan.simps)
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1052
      finally show ?thesis .
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1053
    qed
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1054
  qed
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1055
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1056
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1057
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1058
section "Sinus and Cosinus"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1059
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1060
subsection "Compute the cosinus and sinus series"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1061
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1062
fun ub_sin_cos_aux :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> float \<Rightarrow> float"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1063
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
  1064
  "ub_sin_cos_aux prec 0 i k x = 0"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1065
| "ub_sin_cos_aux prec (Suc n) i k x = float_plus_up prec
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1066
    (rapprox_rat prec 1 k) (-
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1067
      float_round_down prec (x * (lb_sin_cos_aux prec n (i + 2) (k * i * (i + 1)) x)))"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1068
| "lb_sin_cos_aux prec 0 i k x = 0"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1069
| "lb_sin_cos_aux prec (Suc n) i k x = float_plus_down prec
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1070
    (lapprox_rat prec 1 k) (-
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1071
      float_round_up prec (x * (ub_sin_cos_aux prec n (i + 2) (k * i * (i + 1)) x)))"
47601
050718fe6eee use real :: float => real as lifting-morphism so we can directlry use the rep_eq theorems
hoelzl
parents: 47600
diff changeset
  1072
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1073
lemma cos_aux:
59730
b7c394c7a619 The factorial function, "fact", now has type "nat => 'a"
paulson <lp15@cam.ac.uk>
parents: 59658
diff changeset
  1074
  shows "(lb_sin_cos_aux prec n 1 1 (x * x)) \<le> (\<Sum> i=0..<n. (- 1) ^ i * (1/(fact (2 * i))) * x ^(2 * i))" (is "?lb")
b7c394c7a619 The factorial function, "fact", now has type "nat => 'a"
paulson <lp15@cam.ac.uk>
parents: 59658
diff changeset
  1075
  and "(\<Sum> i=0..<n. (- 1) ^ i * (1/(fact (2 * i))) * x^(2 * i)) \<le> (ub_sin_cos_aux prec n 1 1 (x * x))" (is "?ub")
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1076
proof -
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1077
  have "0 \<le> real_of_float (x * x)" by auto
59730
b7c394c7a619 The factorial function, "fact", now has type "nat => 'a"
paulson <lp15@cam.ac.uk>
parents: 59658
diff changeset
  1078
  let "?f n" = "fact (2 * n) :: nat"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1079
  have f_eq: "?f (Suc n) = ?f n * ((\<lambda>i. i + 2) ^^ n) 1 * (((\<lambda>i. i + 2) ^^ n) 1 + 1)" for n
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1080
  proof -
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1081
    have "\<And>m. ((\<lambda>i. i + 2) ^^ n) m = m + 2 * n" by (induct n) auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1082
    then show ?thesis by auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1083
  qed
31809
hoelzl
parents: 31790
diff changeset
  1084
  from horner_bounds[where lb="lb_sin_cos_aux prec" and ub="ub_sin_cos_aux prec" and j'=0,
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1085
    OF \<open>0 \<le> real_of_float (x * x)\<close> f_eq lb_sin_cos_aux.simps ub_sin_cos_aux.simps]
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1086
  show ?lb and ?ub
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1087
    by (auto simp add: power_mult power2_eq_square[of "real_of_float x"])
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1088
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1089
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1090
lemma lb_sin_cos_aux_zero_le_one: "lb_sin_cos_aux prec n i j 0 \<le> 1"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1091
  by (cases j n rule: nat.exhaust[case_product nat.exhaust])
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1092
    (auto intro!: float_plus_down_le order_trans[OF lapprox_rat])
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1093
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1094
lemma one_le_ub_sin_cos_aux: "odd n \<Longrightarrow> 1 \<le> ub_sin_cos_aux prec n i (Suc 0) 0"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1095
  by (cases n) (auto intro!: float_plus_up_le order_trans[OF _ rapprox_rat])
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1096
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1097
lemma cos_boundaries:
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1098
  assumes "0 \<le> real_of_float x" and "x \<le> pi / 2"
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1099
  shows "cos x \<in> {(lb_sin_cos_aux prec (get_even n) 1 1 (x * x)) .. (ub_sin_cos_aux prec (get_odd n) 1 1 (x * x))}"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1100
proof (cases "real_of_float x = 0")
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1101
  case False
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1102
  hence "real_of_float x \<noteq> 0" by auto
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1103
  hence "0 < x" and "0 < real_of_float x"
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1104
    using \<open>0 \<le> real_of_float x\<close> by auto
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1105
  have "0 < x * x"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1106
    using \<open>0 < x\<close> by simp
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1107
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1108
  have morph_to_if_power: "(\<Sum> i=0..<n. (-1::real) ^ i * (1/(fact (2 * i))) * x ^ (2 * i)) =
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1109
    (\<Sum> i = 0 ..< 2 * n. (if even(i) then ((- 1) ^ (i div 2))/((fact i)) else 0) * x ^ i)"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1110
    (is "?sum = ?ifsum") for x n
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1111
  proof -
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1112
    have "?sum = ?sum + (\<Sum> j = 0 ..< n. 0)" by auto
31809
hoelzl
parents: 31790
diff changeset
  1113
    also have "\<dots> =
59730
b7c394c7a619 The factorial function, "fact", now has type "nat => 'a"
paulson <lp15@cam.ac.uk>
parents: 59658
diff changeset
  1114
      (\<Sum> j = 0 ..< n. (- 1) ^ ((2 * j) div 2) / ((fact (2 * j))) * x ^(2 * j)) + (\<Sum> j = 0 ..< n. 0)" by auto
b7c394c7a619 The factorial function, "fact", now has type "nat => 'a"
paulson <lp15@cam.ac.uk>
parents: 59658
diff changeset
  1115
    also have "\<dots> = (\<Sum> i = 0 ..< 2 * n. if even i then (- 1) ^ (i div 2) / ((fact i)) * x ^ i else 0)"
56195
c7dfd924a165 adapt to Isabelle/c726ecfb22b6
huffman
parents: 56073
diff changeset
  1116
      unfolding sum_split_even_odd atLeast0LessThan ..
59730
b7c394c7a619 The factorial function, "fact", now has type "nat => 'a"
paulson <lp15@cam.ac.uk>
parents: 59658
diff changeset
  1117
    also have "\<dots> = (\<Sum> i = 0 ..< 2 * n. (if even i then (- 1) ^ (i div 2) / ((fact i)) else 0) * x ^ i)"
64267
b9a1486e79be setsum -> sum
nipkow
parents: 64246
diff changeset
  1118
      by (rule sum.cong) auto
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1119
    finally show ?thesis .
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1120
  qed
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1121
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1122
  { fix n :: nat assume "0 < n"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1123
    hence "0 < 2 * n" by auto
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1124
    obtain t where "0 < t" and "t < real_of_float x" and
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1125
      cos_eq: "cos x = (\<Sum> i = 0 ..< 2 * n. (if even(i) then ((- 1) ^ (i div 2))/((fact i)) else 0) * (real_of_float x) ^ i)
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1126
      + (cos (t + 1/2 * (2 * n) * pi) / (fact (2*n))) * (real_of_float x)^(2*n)"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1127
      (is "_ = ?SUM + ?rest / ?fact * ?pow")
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1128
      using Maclaurin_cos_expansion2[OF \<open>0 < real_of_float x\<close> \<open>0 < 2 * n\<close>]
56195
c7dfd924a165 adapt to Isabelle/c726ecfb22b6
huffman
parents: 56073
diff changeset
  1129
      unfolding cos_coeff_def atLeast0LessThan by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1130
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
  1131
    have "cos t * (- 1) ^ n = cos t * cos (n * pi) + sin t * sin (n * pi)" by auto
59751
916c0f6c83e3 New material for complex sin, cos, tan, Ln, also some reorganisation
paulson <lp15@cam.ac.uk>
parents: 59741
diff changeset
  1132
    also have "\<dots> = cos (t + n * pi)" by (simp add: cos_add)
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1133
    also have "\<dots> = ?rest" by auto
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
  1134
    finally have "cos t * (- 1) ^ n = ?rest" .
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1135
    moreover
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1136
    have "t \<le> pi / 2" using \<open>t < real_of_float x\<close> and \<open>x \<le> pi / 2\<close> by auto
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
  1137
    hence "0 \<le> cos t" using \<open>0 < t\<close> and cos_ge_zero by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1138
    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
  1139
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1140
    have "0 < ?fact" by auto
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1141
    have "0 < ?pow" using \<open>0 < real_of_float x\<close> by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1142
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1143
    {
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1144
      assume "even n"
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1145
      have "(lb_sin_cos_aux prec n 1 1 (x * x)) \<le> ?SUM"
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  1146
        unfolding morph_to_if_power[symmetric] using cos_aux by auto
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1147
      also have "\<dots> \<le> cos x"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1148
      proof -
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
  1149
        from even[OF \<open>even n\<close>] \<open>0 < ?fact\<close> \<open>0 < ?pow\<close>
56571
f4635657d66f added divide_nonneg_nonneg and co; made it a simp rule
hoelzl
parents: 56544
diff changeset
  1150
        have "0 \<le> (?rest / ?fact) * ?pow" by simp
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  1151
        thus ?thesis unfolding cos_eq by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1152
      qed
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1153
      finally have "(lb_sin_cos_aux prec n 1 1 (x * x)) \<le> cos x" .
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1154
    } note lb = this
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1155
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1156
    {
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1157
      assume "odd n"
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1158
      have "cos x \<le> ?SUM"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1159
      proof -
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
  1160
        from \<open>0 < ?fact\<close> and \<open>0 < ?pow\<close> and odd[OF \<open>odd n\<close>]
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  1161
        have "0 \<le> (- ?rest) / ?fact * ?pow"
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  1162
          by (metis mult_nonneg_nonneg divide_nonneg_pos less_imp_le)
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  1163
        thus ?thesis unfolding cos_eq by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1164
      qed
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1165
      also have "\<dots> \<le> (ub_sin_cos_aux prec n 1 1 (x * x))"
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  1166
        unfolding morph_to_if_power[symmetric] using cos_aux by auto
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1167
      finally have "cos x \<le> (ub_sin_cos_aux prec n 1 1 (x * x))" .
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1168
    } note ub = this and lb
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1169
  } note ub = this(1) and lb = this(2)
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1170
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1171
  have "cos x \<le> (ub_sin_cos_aux prec (get_odd n) 1 1 (x * x))"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1172
    using ub[OF odd_pos[OF get_odd] get_odd] .
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1173
  moreover have "(lb_sin_cos_aux prec (get_even n) 1 1 (x * x)) \<le> cos x"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1174
  proof (cases "0 < get_even n")
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1175
    case True
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1176
    show ?thesis using lb[OF True get_even] .
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1177
  next
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1178
    case False
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1179
    hence "get_even n = 0" by auto
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1180
    have "- (pi / 2) \<le> x"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1181
      by (rule order_trans[OF _ \<open>0 < real_of_float x\<close>[THEN less_imp_le]]) auto
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1182
    with \<open>x \<le> pi / 2\<close> show ?thesis
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1183
      unfolding \<open>get_even n = 0\<close> lb_sin_cos_aux.simps minus_float.rep_eq zero_float.rep_eq
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1184
      using cos_ge_zero by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1185
  qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1186
  ultimately show ?thesis by auto
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1187
next
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1188
  case True
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1189
  hence "x = 0"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1190
    by transfer
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1191
  thus ?thesis
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1192
    using lb_sin_cos_aux_zero_le_one one_le_ub_sin_cos_aux
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1193
    by simp
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1194
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1195
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1196
lemma sin_aux:
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1197
  assumes "0 \<le> real_of_float x"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1198
  shows "(x * lb_sin_cos_aux prec n 2 1 (x * x)) \<le>
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1199
      (\<Sum> i=0..<n. (- 1) ^ i * (1/(fact (2 * i + 1))) * x^(2 * i + 1))" (is "?lb")
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1200
    and "(\<Sum> i=0..<n. (- 1) ^ i * (1/(fact (2 * i + 1))) * x^(2 * i + 1)) \<le>
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1201
      (x * ub_sin_cos_aux prec n 2 1 (x * x))" (is "?ub")
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1202
proof -
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1203
  have "0 \<le> real_of_float (x * x)" by auto
59730
b7c394c7a619 The factorial function, "fact", now has type "nat => 'a"
paulson <lp15@cam.ac.uk>
parents: 59658
diff changeset
  1204
  let "?f n" = "fact (2 * n + 1) :: nat"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1205
  have f_eq: "?f (Suc n) = ?f n * ((\<lambda>i. i + 2) ^^ n) 2 * (((\<lambda>i. i + 2) ^^ n) 2 + 1)" for n
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1206
  proof -
45129
1fce03e3e8ad tuned proofs -- eliminated vacuous "induct arbitrary: ..." situations;
wenzelm
parents: 44821
diff changeset
  1207
    have F: "\<And>m. ((\<lambda>i. i + 2) ^^ n) m = m + 2 * n" by (induct n) auto
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1208
    show ?thesis
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1209
      unfolding F by auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1210
  qed
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1211
  from horner_bounds[where lb="lb_sin_cos_aux prec" and ub="ub_sin_cos_aux prec" and j'=0,
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1212
    OF \<open>0 \<le> real_of_float (x * x)\<close> f_eq lb_sin_cos_aux.simps ub_sin_cos_aux.simps]
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1213
  show "?lb" and "?ub" using \<open>0 \<le> real_of_float x\<close>
64267
b9a1486e79be setsum -> sum
nipkow
parents: 64246
diff changeset
  1214
    apply (simp_all only: power_add power_one_right mult.assoc[symmetric] sum_distrib_right[symmetric])
63170
eae6549dbea2 tuned proofs, to allow unfold_abs_def;
wenzelm
parents: 63040
diff changeset
  1215
    apply (simp_all only: mult.commute[where 'a=real] of_nat_fact)
eae6549dbea2 tuned proofs, to allow unfold_abs_def;
wenzelm
parents: 63040
diff changeset
  1216
    apply (auto intro!: mult_left_mono simp add: power_mult power2_eq_square[of "real_of_float x"])
eae6549dbea2 tuned proofs, to allow unfold_abs_def;
wenzelm
parents: 63040
diff changeset
  1217
    done
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1218
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1219
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1220
lemma sin_boundaries:
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1221
  assumes "0 \<le> real_of_float x"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1222
    and "x \<le> pi / 2"
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1223
  shows "sin x \<in> {(x * lb_sin_cos_aux prec (get_even n) 2 1 (x * x)) .. (x * ub_sin_cos_aux prec (get_odd n) 2 1 (x * x))}"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1224
proof (cases "real_of_float x = 0")
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1225
  case False
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1226
  hence "real_of_float x \<noteq> 0" by auto
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1227
  hence "0 < x" and "0 < real_of_float x"
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1228
    using \<open>0 \<le> real_of_float x\<close> by auto
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1229
  have "0 < x * x"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1230
    using \<open>0 < x\<close> by simp
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1231
64267
b9a1486e79be setsum -> sum
nipkow
parents: 64246
diff changeset
  1232
  have sum_morph: "(\<Sum>j = 0 ..< n. (- 1) ^ (((2 * j + 1) - Suc 0) div 2) / ((fact (2 * j + 1))) * x ^(2 * j + 1)) =
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1233
    (\<Sum> i = 0 ..< 2 * n. (if even(i) then 0 else ((- 1) ^ ((i - Suc 0) div 2))/((fact i))) * x ^ i)"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1234
    (is "?SUM = _") for x :: real and n
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1235
  proof -
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1236
    have pow: "!!i. x ^ (2 * i + 1) = x * x ^ (2 * i)"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1237
      by auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1238
    have "?SUM = (\<Sum> j = 0 ..< n. 0) + ?SUM"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1239
      by auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1240
    also have "\<dots> = (\<Sum> i = 0 ..< 2 * n. if even i then 0 else (- 1) ^ ((i - Suc 0) div 2) / ((fact i)) * x ^ i)"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1241
      unfolding sum_split_even_odd atLeast0LessThan ..
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1242
    also have "\<dots> = (\<Sum> i = 0 ..< 2 * n. (if even i then 0 else (- 1) ^ ((i - Suc 0) div 2) / ((fact i))) * x ^ i)"
64267
b9a1486e79be setsum -> sum
nipkow
parents: 64246
diff changeset
  1243
      by (rule sum.cong) auto
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1244
    finally show ?thesis .
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1245
  qed
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1246
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1247
  { fix n :: nat assume "0 < n"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1248
    hence "0 < 2 * n + 1" by auto
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1249
    obtain t where "0 < t" and "t < real_of_float x" and
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1250
      sin_eq: "sin x = (\<Sum> i = 0 ..< 2 * n + 1. (if even(i) then 0 else ((- 1) ^ ((i - Suc 0) div 2))/((fact i))) * (real_of_float x) ^ i)
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1251
      + (sin (t + 1/2 * (2 * n + 1) * pi) / (fact (2*n + 1))) * (real_of_float x)^(2*n + 1)"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1252
      (is "_ = ?SUM + ?rest / ?fact * ?pow")
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1253
      using Maclaurin_sin_expansion3[OF \<open>0 < 2 * n + 1\<close> \<open>0 < real_of_float x\<close>]
56195
c7dfd924a165 adapt to Isabelle/c726ecfb22b6
huffman
parents: 56073
diff changeset
  1254
      unfolding sin_coeff_def atLeast0LessThan by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1255
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1256
    have "?rest = cos t * (- 1) ^ n"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1257
      unfolding sin_add cos_add of_nat_add distrib_right distrib_left by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1258
    moreover
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1259
    have "t \<le> pi / 2"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1260
      using \<open>t < real_of_float x\<close> and \<open>x \<le> pi / 2\<close> by auto
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1261
    hence "0 \<le> cos t"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1262
      using \<open>0 < t\<close> and cos_ge_zero by auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1263
    ultimately have even: "even n \<Longrightarrow> 0 \<le> ?rest" and odd: "odd n \<Longrightarrow> 0 \<le> - ?rest"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1264
      by auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1265
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1266
    have "0 < ?fact"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1267
      by (simp del: fact_Suc)
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1268
    have "0 < ?pow"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1269
      using \<open>0 < real_of_float x\<close> by (rule zero_less_power)
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1270
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1271
    {
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1272
      assume "even n"
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1273
      have "(x * lb_sin_cos_aux prec n 2 1 (x * x)) \<le>
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1274
            (\<Sum> i = 0 ..< 2 * n. (if even(i) then 0 else ((- 1) ^ ((i - Suc 0) div 2))/((fact i))) * (real_of_float x) ^ i)"
64267
b9a1486e79be setsum -> sum
nipkow
parents: 64246
diff changeset
  1275
        using sin_aux[OF \<open>0 \<le> real_of_float x\<close>] unfolding sum_morph[symmetric] by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1276
      also have "\<dots> \<le> ?SUM" by auto
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1277
      also have "\<dots> \<le> sin x"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1278
      proof -
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
  1279
        from even[OF \<open>even n\<close>] \<open>0 < ?fact\<close> \<open>0 < ?pow\<close>
56571
f4635657d66f added divide_nonneg_nonneg and co; made it a simp rule
hoelzl
parents: 56544
diff changeset
  1280
        have "0 \<le> (?rest / ?fact) * ?pow" by simp
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  1281
        thus ?thesis unfolding sin_eq by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1282
      qed
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1283
      finally have "(x * lb_sin_cos_aux prec n 2 1 (x * x)) \<le> sin x" .
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1284
    } note lb = this
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1285
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1286
    {
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1287
      assume "odd n"
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1288
      have "sin x \<le> ?SUM"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1289
      proof -
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
  1290
        from \<open>0 < ?fact\<close> and \<open>0 < ?pow\<close> and odd[OF \<open>odd n\<close>]
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  1291
        have "0 \<le> (- ?rest) / ?fact * ?pow"
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  1292
          by (metis mult_nonneg_nonneg divide_nonneg_pos less_imp_le)
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  1293
        thus ?thesis unfolding sin_eq by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1294
      qed
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1295
      also have "\<dots> \<le> (\<Sum> i = 0 ..< 2 * n. (if even(i) then 0 else ((- 1) ^ ((i - Suc 0) div 2))/((fact i))) * (real_of_float x) ^ i)"
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  1296
         by auto
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1297
      also have "\<dots> \<le> (x * ub_sin_cos_aux prec n 2 1 (x * x))"
64267
b9a1486e79be setsum -> sum
nipkow
parents: 64246
diff changeset
  1298
        using sin_aux[OF \<open>0 \<le> real_of_float x\<close>] unfolding sum_morph[symmetric] by auto
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1299
      finally have "sin x \<le> (x * ub_sin_cos_aux prec n 2 1 (x * x))" .
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1300
    } note ub = this and lb
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1301
  } note ub = this(1) and lb = this(2)
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1302
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1303
  have "sin x \<le> (x * ub_sin_cos_aux prec (get_odd n) 2 1 (x * x))"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1304
    using ub[OF odd_pos[OF get_odd] get_odd] .
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1305
  moreover have "(x * lb_sin_cos_aux prec (get_even n) 2 1 (x * x)) \<le> sin x"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1306
  proof (cases "0 < get_even n")
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1307
    case True
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1308
    show ?thesis
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1309
      using lb[OF True get_even] .
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1310
  next
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1311
    case False
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1312
    hence "get_even n = 0" by auto
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1313
    with \<open>x \<le> pi / 2\<close> \<open>0 \<le> real_of_float x\<close>
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1314
    show ?thesis
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1315
      unfolding \<open>get_even n = 0\<close> ub_sin_cos_aux.simps minus_float.rep_eq
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1316
      using sin_ge_zero by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1317
  qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1318
  ultimately show ?thesis by auto
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1319
next
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1320
  case True
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1321
  show ?thesis
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1322
  proof (cases "n = 0")
31809
hoelzl
parents: 31790
diff changeset
  1323
    case True
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1324
    thus ?thesis
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1325
      unfolding \<open>n = 0\<close> get_even_def get_odd_def
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1326
      using \<open>real_of_float x = 0\<close> lapprox_rat[where x="-1" and y=1] by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1327
  next
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1328
    case False
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1329
    with not0_implies_Suc obtain m where "n = Suc m" by blast
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1330
    thus ?thesis
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1331
      unfolding \<open>n = Suc m\<close> get_even_def get_odd_def
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1332
      using \<open>real_of_float x = 0\<close> rapprox_rat[where x=1 and y=1] lapprox_rat[where x=1 and y=1]
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1333
      by (cases "even (Suc m)") auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1334
  qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1335
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1336
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1337
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1338
subsection "Compute the cosinus in the entire domain"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1339
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1340
definition lb_cos :: "nat \<Rightarrow> float \<Rightarrow> float" where
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1341
"lb_cos prec x = (let
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1342
    horner = \<lambda> x. lb_sin_cos_aux prec (get_even (prec div 4 + 1)) 1 1 (x * x) ;
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1343
    half = \<lambda> x. if x < 0 then - 1 else float_plus_down prec (Float 1 1 * x * x) (- 1)
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
  1344
  in if x < Float 1 (- 1) then horner x
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
  1345
else if x < 1          then half (horner (x * Float 1 (- 1)))
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
  1346
                       else half (half (horner (x * Float 1 (- 2)))))"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1347
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1348
definition ub_cos :: "nat \<Rightarrow> float \<Rightarrow> float" where
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1349
"ub_cos prec x = (let
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1350
    horner = \<lambda> x. ub_sin_cos_aux prec (get_odd (prec div 4 + 1)) 1 1 (x * x) ;
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1351
    half = \<lambda> x. float_plus_up prec (Float 1 1 * x * x) (- 1)
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
  1352
  in if x < Float 1 (- 1) then horner x
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
  1353
else if x < 1          then half (horner (x * Float 1 (- 1)))
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
  1354
                       else half (half (horner (x * Float 1 (- 2)))))"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1355
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1356
lemma lb_cos:
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1357
  assumes "0 \<le> real_of_float x" and "x \<le> pi"
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1358
  shows "cos x \<in> {(lb_cos prec x) .. (ub_cos prec x)}" (is "?cos x \<in> {(?lb x) .. (?ub x) }")
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1359
proof -
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1360
  have x_half[symmetric]: "cos x = 2 * cos (x / 2) * cos (x / 2) - 1" for x :: real
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1361
  proof -
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1362
    have "cos x = cos (x / 2 + x / 2)"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1363
      by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1364
    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
  1365
      unfolding cos_add by auto
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1366
    also have "\<dots> = 2 * cos (x / 2) * cos (x / 2) - 1"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1367
      by algebra
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1368
    finally show ?thesis .
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1369
  qed
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1370
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1371
  have "\<not> x < 0" using \<open>0 \<le> real_of_float x\<close> by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1372
  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
  1373
  let "?lb_horner x" = "lb_sin_cos_aux prec (get_even (prec div 4 + 1)) 1 1 (x * x)"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1374
  let "?ub_half x" = "float_plus_up prec (Float 1 1 * x * x) (- 1)"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1375
  let "?lb_half x" = "if x < 0 then - 1 else float_plus_down prec (Float 1 1 * x * x) (- 1)"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1376
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1377
  show ?thesis
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
  1378
  proof (cases "x < Float 1 (- 1)")
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1379
    case True
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1380
    hence "x \<le> pi / 2"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1381
      using pi_ge_two by auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1382
    show ?thesis
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1383
      unfolding lb_cos_def[where x=x] ub_cos_def[where x=x]
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1384
        if_not_P[OF \<open>\<not> x < 0\<close>] if_P[OF \<open>x < Float 1 (- 1)\<close>] Let_def
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1385
      using cos_boundaries[OF \<open>0 \<le> real_of_float x\<close> \<open>x \<le> pi / 2\<close>] .
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1386
  next
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1387
    case False
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
  1388
    { fix y x :: float let ?x2 = "(x * Float 1 (- 1))"
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1389
      assume "y \<le> cos ?x2" and "-pi \<le> x" and "x \<le> pi"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1390
      hence "- (pi / 2) \<le> ?x2" and "?x2 \<le> pi / 2"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1391
        using pi_ge_two unfolding Float_num by auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1392
      hence "0 \<le> cos ?x2"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1393
        by (rule cos_ge_zero)
31467
f7d2aa438bee Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
hoelzl
parents: 31148
diff changeset
  1394
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1395
      have "(?lb_half y) \<le> cos x"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1396
      proof (cases "y < 0")
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1397
        case True
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1398
        show ?thesis
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1399
          using cos_ge_minus_one unfolding if_P[OF True] by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1400
      next
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  1401
        case False
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1402
        hence "0 \<le> real_of_float y" by auto
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
  1403
        from mult_mono[OF \<open>y \<le> cos ?x2\<close> \<open>y \<le> cos ?x2\<close> \<open>0 \<le> cos ?x2\<close> this]
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1404
        have "real_of_float y * real_of_float y \<le> cos ?x2 * cos ?x2" .
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1405
        hence "2 * real_of_float y * real_of_float y \<le> 2 * cos ?x2 * cos ?x2"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1406
          by auto
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1407
        hence "2 * real_of_float y * real_of_float y - 1 \<le> 2 * cos (x / 2) * cos (x / 2) - 1"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1408
          unfolding Float_num by auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1409
        thus ?thesis
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1410
          unfolding if_not_P[OF False] x_half Float_num
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1411
          by (auto intro!: float_plus_down_le)
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1412
      qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1413
    } note lb_half = this
31467
f7d2aa438bee Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
hoelzl
parents: 31148
diff changeset
  1414
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
  1415
    { fix y x :: float let ?x2 = "(x * Float 1 (- 1))"
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1416
      assume ub: "cos ?x2 \<le> y" and "- pi \<le> x" and "x \<le> pi"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1417
      hence "- (pi / 2) \<le> ?x2" and "?x2 \<le> pi / 2"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1418
        using pi_ge_two unfolding Float_num by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1419
      hence "0 \<le> cos ?x2" by (rule cos_ge_zero)
31467
f7d2aa438bee Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
hoelzl
parents: 31148
diff changeset
  1420
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1421
      have "cos x \<le> (?ub_half y)"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1422
      proof -
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1423
        have "0 \<le> real_of_float y"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1424
          using \<open>0 \<le> cos ?x2\<close> ub by (rule order_trans)
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
  1425
        from mult_mono[OF ub ub this \<open>0 \<le> cos ?x2\<close>]
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1426
        have "cos ?x2 * cos ?x2 \<le> real_of_float y * real_of_float y" .
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1427
        hence "2 * cos ?x2 * cos ?x2 \<le> 2 * real_of_float y * real_of_float y"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1428
          by auto
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1429
        hence "2 * cos (x / 2) * cos (x / 2) - 1 \<le> 2 * real_of_float y * real_of_float y - 1"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1430
          unfolding Float_num by auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1431
        thus ?thesis
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1432
          unfolding x_half Float_num
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1433
          by (auto intro!: float_plus_up_le)
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1434
      qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1435
    } note ub_half = this
31467
f7d2aa438bee Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
hoelzl
parents: 31148
diff changeset
  1436
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
  1437
    let ?x2 = "x * Float 1 (- 1)"
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
  1438
    let ?x4 = "x * Float 1 (- 1) * Float 1 (- 1)"
31467
f7d2aa438bee Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
hoelzl
parents: 31148
diff changeset
  1439
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1440
    have "-pi \<le> x"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1441
      using pi_ge_zero[THEN le_imp_neg_le, unfolded minus_zero] \<open>0 \<le> real_of_float x\<close>
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1442
      by (rule order_trans)
31467
f7d2aa438bee Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
hoelzl
parents: 31148
diff changeset
  1443
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1444
    show ?thesis
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1445
    proof (cases "x < 1")
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1446
      case True
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1447
      hence "real_of_float x \<le> 1" by auto
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1448
      have "0 \<le> real_of_float ?x2" and "?x2 \<le> pi / 2"
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1449
        using pi_ge_two \<open>0 \<le> real_of_float x\<close> using assms by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1450
      from cos_boundaries[OF this]
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1451
      have lb: "(?lb_horner ?x2) \<le> ?cos ?x2" and ub: "?cos ?x2 \<le> (?ub_horner ?x2)"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1452
        by auto
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1453
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1454
      have "(?lb x) \<le> ?cos x"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1455
      proof -
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
  1456
        from lb_half[OF lb \<open>-pi \<le> x\<close> \<open>x \<le> pi\<close>]
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1457
        show ?thesis
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1458
          unfolding lb_cos_def[where x=x] Let_def
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1459
          using \<open>\<not> x < 0\<close> \<open>\<not> x < Float 1 (- 1)\<close> \<open>x < 1\<close> by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1460
      qed
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1461
      moreover have "?cos x \<le> (?ub x)"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1462
      proof -
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
  1463
        from ub_half[OF ub \<open>-pi \<le> x\<close> \<open>x \<le> pi\<close>]
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1464
        show ?thesis
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1465
          unfolding ub_cos_def[where x=x] Let_def
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1466
          using \<open>\<not> x < 0\<close> \<open>\<not> x < Float 1 (- 1)\<close> \<open>x < 1\<close> by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1467
      qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1468
      ultimately show ?thesis by auto
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1469
    next
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1470
      case False
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1471
      have "0 \<le> real_of_float ?x4" and "?x4 \<le> pi / 2"
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1472
        using pi_ge_two \<open>0 \<le> real_of_float x\<close> \<open>x \<le> pi\<close> unfolding Float_num by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1473
      from cos_boundaries[OF this]
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1474
      have lb: "(?lb_horner ?x4) \<le> ?cos ?x4" and ub: "?cos ?x4 \<le> (?ub_horner ?x4)"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1475
        by auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1476
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1477
      have eq_4: "?x2 * Float 1 (- 1) = x * Float 1 (- 2)"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1478
        by transfer simp
31467
f7d2aa438bee Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
hoelzl
parents: 31148
diff changeset
  1479
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1480
      have "(?lb x) \<le> ?cos x"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1481
      proof -
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1482
        have "-pi \<le> ?x2" and "?x2 \<le> pi"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1483
          using pi_ge_two \<open>0 \<le> real_of_float x\<close> \<open>x \<le> pi\<close> by auto
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
  1484
        from lb_half[OF lb_half[OF lb this] \<open>-pi \<le> x\<close> \<open>x \<le> pi\<close>, unfolded eq_4]
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1485
        show ?thesis
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1486
          unfolding lb_cos_def[where x=x] if_not_P[OF \<open>\<not> x < 0\<close>]
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1487
            if_not_P[OF \<open>\<not> x < Float 1 (- 1)\<close>] if_not_P[OF \<open>\<not> x < 1\<close>] Let_def .
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1488
      qed
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1489
      moreover have "?cos x \<le> (?ub x)"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1490
      proof -
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1491
        have "-pi \<le> ?x2" and "?x2 \<le> pi"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1492
          using pi_ge_two \<open>0 \<le> real_of_float x\<close> \<open> x \<le> pi\<close> by auto
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
  1493
        from ub_half[OF ub_half[OF ub this] \<open>-pi \<le> x\<close> \<open>x \<le> pi\<close>, unfolded eq_4]
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1494
        show ?thesis
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1495
          unfolding ub_cos_def[where x=x] if_not_P[OF \<open>\<not> x < 0\<close>]
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1496
            if_not_P[OF \<open>\<not> x < Float 1 (- 1)\<close>] if_not_P[OF \<open>\<not> x < 1\<close>] Let_def .
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1497
      qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1498
      ultimately show ?thesis by auto
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1499
    qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1500
  qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1501
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1502
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1503
lemma lb_cos_minus:
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1504
  assumes "-pi \<le> x"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1505
    and "real_of_float x \<le> 0"
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1506
  shows "cos (real_of_float(-x)) \<in> {(lb_cos prec (-x)) .. (ub_cos prec (-x))}"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1507
proof -
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1508
  have "0 \<le> real_of_float (-x)" and "(-x) \<le> pi"
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1509
    using \<open>-pi \<le> x\<close> \<open>real_of_float x \<le> 0\<close> by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1510
  from lb_cos[OF this] show ?thesis .
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1511
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1512
31467
f7d2aa438bee Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
hoelzl
parents: 31148
diff changeset
  1513
definition bnds_cos :: "nat \<Rightarrow> float \<Rightarrow> float \<Rightarrow> float * float" where
f7d2aa438bee Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
hoelzl
parents: 31148
diff changeset
  1514
"bnds_cos prec lx ux = (let
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1515
    lpi = float_round_down prec (lb_pi prec) ;
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1516
    upi = float_round_up prec (ub_pi prec) ;
31467
f7d2aa438bee Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
hoelzl
parents: 31148
diff changeset
  1517
    k = floor_fl (float_divr prec (lx + lpi) (2 * lpi)) ;
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1518
    lx = float_plus_down prec lx (- k * 2 * (if k < 0 then lpi else upi)) ;
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1519
    ux = float_plus_up prec ux (- k * 2 * (if k < 0 then upi else lpi))
31467
f7d2aa438bee Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
hoelzl
parents: 31148
diff changeset
  1520
  in   if - lpi \<le> lx \<and> ux \<le> 0    then (lb_cos prec (-lx), ub_cos prec (-ux))
f7d2aa438bee Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
hoelzl
parents: 31148
diff changeset
  1521
  else if 0 \<le> lx \<and> ux \<le> lpi      then (lb_cos prec ux, ub_cos prec lx)
f7d2aa438bee Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
hoelzl
parents: 31148
diff changeset
  1522
  else if - lpi \<le> lx \<and> ux \<le> lpi  then (min (lb_cos prec (-lx)) (lb_cos prec ux), Float 1 0)
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
  1523
  else if 0 \<le> lx \<and> ux \<le> 2 * lpi  then (Float (- 1) 0, max (ub_cos prec lx) (ub_cos prec (- (ux - 2 * lpi))))
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
  1524
  else if -2 * lpi \<le> lx \<and> ux \<le> 0 then (Float (- 1) 0, max (ub_cos prec (lx + 2 * lpi)) (ub_cos prec (-ux)))
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
  1525
                                 else (Float (- 1) 0, Float 1 0))"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1526
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1527
lemma floor_int: obtains k :: int where "real_of_int k = (floor_fl f)"
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1528
  by (simp add: floor_fl_def)
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1529
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1530
lemma cos_periodic_nat[simp]:
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1531
  fixes n :: nat
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1532
  shows "cos (x + n * (2 * pi)) = cos x"
31467
f7d2aa438bee Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
hoelzl
parents: 31148
diff changeset
  1533
proof (induct n arbitrary: x)
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1534
  case 0
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1535
  then show ?case by simp
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1536
next
31467
f7d2aa438bee Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
hoelzl
parents: 31148
diff changeset
  1537
  case (Suc n)
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1538
  have split_pi_off: "x + (Suc n) * (2 * pi) = (x + n * (2 * pi)) + 2 * pi"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1539
    unfolding Suc_eq_plus1 of_nat_add of_int_1 distrib_right by auto
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1540
  show ?case
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1541
    unfolding split_pi_off using Suc by auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1542
qed
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1543
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1544
lemma cos_periodic_int[simp]:
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1545
  fixes i :: int
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1546
  shows "cos (x + i * (2 * pi)) = cos x"
31467
f7d2aa438bee Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
hoelzl
parents: 31148
diff changeset
  1547
proof (cases "0 \<le> i")
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1548
  case True
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1549
  hence i_nat: "real_of_int i = nat i" by auto
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1550
  show ?thesis
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1551
    unfolding i_nat by auto
31467
f7d2aa438bee Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
hoelzl
parents: 31148
diff changeset
  1552
next
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1553
  case False
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1554
    hence i_nat: "i = - real (nat (-i))" by auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1555
  have "cos x = cos (x + i * (2 * pi) - i * (2 * pi))"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1556
    by auto
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1557
  also have "\<dots> = cos (x + i * (2 * pi))"
31467
f7d2aa438bee Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
hoelzl
parents: 31148
diff changeset
  1558
    unfolding i_nat mult_minus_left diff_minus_eq_add by (rule cos_periodic_nat)
f7d2aa438bee Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
hoelzl
parents: 31148
diff changeset
  1559
  finally show ?thesis by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1560
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1561
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1562
lemma bnds_cos: "\<forall>(x::real) lx ux. (l, u) =
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1563
  bnds_cos prec lx ux \<and> x \<in> {lx .. ux} \<longrightarrow> l \<le> cos x \<and> cos x \<le> u"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1564
proof (rule allI | rule impI | erule conjE)+
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1565
  fix x :: real
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1566
  fix lx ux
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1567
  assume bnds: "(l, u) = bnds_cos prec lx ux" and x: "x \<in> {lx .. ux}"
31467
f7d2aa438bee Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
hoelzl
parents: 31148
diff changeset
  1568
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1569
  let ?lpi = "float_round_down prec (lb_pi prec)"
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1570
  let ?upi = "float_round_up prec (ub_pi prec)"
31467
f7d2aa438bee Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
hoelzl
parents: 31148
diff changeset
  1571
  let ?k = "floor_fl (float_divr prec (lx + ?lpi) (2 * ?lpi))"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1572
  let ?lx2 = "(- ?k * 2 * (if ?k < 0 then ?lpi else ?upi))"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1573
  let ?ux2 = "(- ?k * 2 * (if ?k < 0 then ?upi else ?lpi))"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1574
  let ?lx = "float_plus_down prec lx ?lx2"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1575
  let ?ux = "float_plus_up prec ux ?ux2"
31467
f7d2aa438bee Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
hoelzl
parents: 31148
diff changeset
  1576
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1577
  obtain k :: int where k: "k = real_of_float ?k"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1578
    by (rule floor_int)
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1579
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1580
  have upi: "pi \<le> ?upi" and lpi: "?lpi \<le> pi"
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1581
    using float_round_up[of "ub_pi prec" prec] pi_boundaries[of prec]
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1582
      float_round_down[of prec "lb_pi prec"]
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1583
    by auto
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1584
  hence "lx + ?lx2 \<le> x - k * (2 * pi) \<and> x - k * (2 * pi) \<le> ux + ?ux2"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1585
    using x
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1586
    by (cases "k = 0")
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1587
      (auto intro!: add_mono
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1588
        simp add: k [symmetric] uminus_add_conv_diff [symmetric]
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1589
        simp del: float_of_numeral uminus_add_conv_diff)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1590
  hence "?lx \<le> x - k * (2 * pi) \<and> x - k * (2 * pi) \<le> ?ux"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1591
    by (auto intro!: float_plus_down_le float_plus_up_le)
31467
f7d2aa438bee Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
hoelzl
parents: 31148
diff changeset
  1592
  note lx = this[THEN conjunct1] and ux = this[THEN conjunct2]
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1593
  hence lx_less_ux: "?lx \<le> real_of_float ?ux" by (rule order_trans)
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1594
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1595
  { assume "- ?lpi \<le> ?lx" and x_le_0: "x - k * (2 * pi) \<le> 0"
31467
f7d2aa438bee Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
hoelzl
parents: 31148
diff changeset
  1596
    with lpi[THEN le_imp_neg_le] lx
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1597
    have pi_lx: "- pi \<le> ?lx" and lx_0: "real_of_float ?lx \<le> 0"
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  1598
      by simp_all
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1599
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1600
    have "(lb_cos prec (- ?lx)) \<le> cos (real_of_float (- ?lx))"
31467
f7d2aa438bee Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
hoelzl
parents: 31148
diff changeset
  1601
      using lb_cos_minus[OF pi_lx lx_0] by simp
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1602
    also have "\<dots> \<le> cos (x + (-k) * (2 * pi))"
31467
f7d2aa438bee Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
hoelzl
parents: 31148
diff changeset
  1603
      using cos_monotone_minus_pi_0'[OF pi_lx lx x_le_0]
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1604
      by (simp only: uminus_float.rep_eq of_int_minus
54230
b1d955791529 more simplification rules on unary and binary minus
haftmann
parents: 53077
diff changeset
  1605
        cos_minus mult_minus_left) simp
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1606
    finally have "(lb_cos prec (- ?lx)) \<le> cos x"
31467
f7d2aa438bee Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
hoelzl
parents: 31148
diff changeset
  1607
      unfolding cos_periodic_int . }
f7d2aa438bee Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
hoelzl
parents: 31148
diff changeset
  1608
  note negative_lx = this
f7d2aa438bee Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
hoelzl
parents: 31148
diff changeset
  1609
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1610
  { assume "0 \<le> ?lx" and pi_x: "x - k * (2 * pi) \<le> pi"
31467
f7d2aa438bee Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
hoelzl
parents: 31148
diff changeset
  1611
    with lx
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1612
    have pi_lx: "?lx \<le> pi" and lx_0: "0 \<le> real_of_float ?lx"
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  1613
      by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1614
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1615
    have "cos (x + (-k) * (2 * pi)) \<le> cos ?lx"
59751
916c0f6c83e3 New material for complex sin, cos, tan, Ln, also some reorganisation
paulson <lp15@cam.ac.uk>
parents: 59741
diff changeset
  1616
      using cos_monotone_0_pi_le[OF lx_0 lx pi_x]
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1617
      by (simp only: of_int_minus
54230
b1d955791529 more simplification rules on unary and binary minus
haftmann
parents: 53077
diff changeset
  1618
        cos_minus mult_minus_left) simp
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1619
    also have "\<dots> \<le> (ub_cos prec ?lx)"
31467
f7d2aa438bee Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
hoelzl
parents: 31148
diff changeset
  1620
      using lb_cos[OF lx_0 pi_lx] by simp
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1621
    finally have "cos x \<le> (ub_cos prec ?lx)"
31467
f7d2aa438bee Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
hoelzl
parents: 31148
diff changeset
  1622
      unfolding cos_periodic_int . }
f7d2aa438bee Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
hoelzl
parents: 31148
diff changeset
  1623
  note positive_lx = this
f7d2aa438bee Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
hoelzl
parents: 31148
diff changeset
  1624
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1625
  { assume pi_x: "- pi \<le> x - k * (2 * pi)" and "?ux \<le> 0"
31467
f7d2aa438bee Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
hoelzl
parents: 31148
diff changeset
  1626
    with ux
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1627
    have pi_ux: "- pi \<le> ?ux" and ux_0: "real_of_float ?ux \<le> 0"
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  1628
      by simp_all
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1629
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1630
    have "cos (x + (-k) * (2 * pi)) \<le> cos (real_of_float (- ?ux))"
31467
f7d2aa438bee Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
hoelzl
parents: 31148
diff changeset
  1631
      using cos_monotone_minus_pi_0'[OF pi_x ux ux_0]
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1632
      by (simp only: uminus_float.rep_eq of_int_minus
54230
b1d955791529 more simplification rules on unary and binary minus
haftmann
parents: 53077
diff changeset
  1633
          cos_minus mult_minus_left) simp
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1634
    also have "\<dots> \<le> (ub_cos prec (- ?ux))"
31467
f7d2aa438bee Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
hoelzl
parents: 31148
diff changeset
  1635
      using lb_cos_minus[OF pi_ux ux_0, of prec] by simp
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1636
    finally have "cos x \<le> (ub_cos prec (- ?ux))"
31467
f7d2aa438bee Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
hoelzl
parents: 31148
diff changeset
  1637
      unfolding cos_periodic_int . }
f7d2aa438bee Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
hoelzl
parents: 31148
diff changeset
  1638
  note negative_ux = this
f7d2aa438bee Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
hoelzl
parents: 31148
diff changeset
  1639
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1640
  { assume "?ux \<le> ?lpi" and x_ge_0: "0 \<le> x - k * (2 * pi)"
31467
f7d2aa438bee Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
hoelzl
parents: 31148
diff changeset
  1641
    with lpi ux
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1642
    have pi_ux: "?ux \<le> pi" and ux_0: "0 \<le> real_of_float ?ux"
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  1643
      by simp_all
31467
f7d2aa438bee Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
hoelzl
parents: 31148
diff changeset
  1644
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1645
    have "(lb_cos prec ?ux) \<le> cos ?ux"
31467
f7d2aa438bee Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
hoelzl
parents: 31148
diff changeset
  1646
      using lb_cos[OF ux_0 pi_ux] by simp
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1647
    also have "\<dots> \<le> cos (x + (-k) * (2 * pi))"
59751
916c0f6c83e3 New material for complex sin, cos, tan, Ln, also some reorganisation
paulson <lp15@cam.ac.uk>
parents: 59741
diff changeset
  1648
      using cos_monotone_0_pi_le[OF x_ge_0 ux pi_ux]
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1649
      by (simp only: of_int_minus
54230
b1d955791529 more simplification rules on unary and binary minus
haftmann
parents: 53077
diff changeset
  1650
        cos_minus mult_minus_left) simp
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1651
    finally have "(lb_cos prec ?ux) \<le> cos x"
31467
f7d2aa438bee Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
hoelzl
parents: 31148
diff changeset
  1652
      unfolding cos_periodic_int . }
f7d2aa438bee Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
hoelzl
parents: 31148
diff changeset
  1653
  note positive_ux = this
f7d2aa438bee Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
hoelzl
parents: 31148
diff changeset
  1654
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1655
  show "l \<le> cos x \<and> cos x \<le> u"
31467
f7d2aa438bee Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
hoelzl
parents: 31148
diff changeset
  1656
  proof (cases "- ?lpi \<le> ?lx \<and> ?ux \<le> 0")
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1657
    case True
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1658
    with bnds have l: "l = lb_cos prec (-?lx)" and u: "u = ub_cos prec (-?ux)"
31467
f7d2aa438bee Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
hoelzl
parents: 31148
diff changeset
  1659
      by (auto simp add: bnds_cos_def Let_def)
f7d2aa438bee Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
hoelzl
parents: 31148
diff changeset
  1660
    from True lpi[THEN le_imp_neg_le] lx ux
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1661
    have "- pi \<le> x - k * (2 * pi)" and "x - k * (2 * pi) \<le> 0"
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  1662
      by auto
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1663
    with True negative_ux negative_lx show ?thesis
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1664
      unfolding l u by simp
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1665
  next
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1666
    case 1: False
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1667
    show ?thesis
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1668
    proof (cases "0 \<le> ?lx \<and> ?ux \<le> ?lpi")
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1669
      case True with bnds 1
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1670
      have l: "l = lb_cos prec ?ux"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1671
        and u: "u = ub_cos prec ?lx"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1672
        by (auto simp add: bnds_cos_def Let_def)
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1673
      from True lpi lx ux
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1674
      have "0 \<le> x - k * (2 * pi)" and "x - k * (2 * pi) \<le> pi"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1675
        by auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1676
      with True positive_ux positive_lx show ?thesis
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1677
        unfolding l u by simp
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1678
    next
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1679
      case 2: False
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1680
      show ?thesis
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1681
      proof (cases "- ?lpi \<le> ?lx \<and> ?ux \<le> ?lpi")
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1682
        case Cond: True
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1683
        with bnds 1 2 have l: "l = min (lb_cos prec (-?lx)) (lb_cos prec ?ux)"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1684
          and u: "u = Float 1 0"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1685
          by (auto simp add: bnds_cos_def Let_def)
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1686
        show ?thesis
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1687
          unfolding u l using negative_lx positive_ux Cond
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1688
          by (cases "x - k * (2 * pi) < 0") (auto simp add: real_of_float_min)
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1689
      next
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1690
        case 3: False
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1691
        show ?thesis
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1692
        proof (cases "0 \<le> ?lx \<and> ?ux \<le> 2 * ?lpi")
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1693
          case Cond: True
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1694
          with bnds 1 2 3
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1695
          have l: "l = Float (- 1) 0"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1696
            and u: "u = max (ub_cos prec ?lx) (ub_cos prec (- (?ux - 2 * ?lpi)))"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1697
            by (auto simp add: bnds_cos_def Let_def)
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1698
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1699
          have "cos x \<le> real_of_float u"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1700
          proof (cases "x - k * (2 * pi) < pi")
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1701
            case True
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1702
            hence "x - k * (2 * pi) \<le> pi" by simp
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1703
            from positive_lx[OF Cond[THEN conjunct1] this] show ?thesis
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1704
              unfolding u by (simp add: real_of_float_max)
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1705
          next
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1706
            case False
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1707
            hence "pi \<le> x - k * (2 * pi)" by simp
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1708
            hence pi_x: "- pi \<le> x - k * (2 * pi) - 2 * pi" by simp
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1709
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1710
            have "?ux \<le> 2 * pi"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1711
              using Cond lpi by auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1712
            hence "x - k * (2 * pi) - 2 * pi \<le> 0"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1713
              using ux by simp
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1714
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1715
            have ux_0: "real_of_float (?ux - 2 * ?lpi) \<le> 0"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1716
              using Cond by auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1717
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1718
            from 2 and Cond have "\<not> ?ux \<le> ?lpi" by auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1719
            hence "- ?lpi \<le> ?ux - 2 * ?lpi" by auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1720
            hence pi_ux: "- pi \<le> (?ux - 2 * ?lpi)"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1721
              using lpi[THEN le_imp_neg_le] by auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1722
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1723
            have x_le_ux: "x - k * (2 * pi) - 2 * pi \<le> (?ux - 2 * ?lpi)"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1724
              using ux lpi by auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1725
            have "cos x = cos (x + (-k) * (2 * pi) + (-1::int) * (2 * pi))"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1726
              unfolding cos_periodic_int ..
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1727
            also have "\<dots> \<le> cos ((?ux - 2 * ?lpi))"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1728
              using cos_monotone_minus_pi_0'[OF pi_x x_le_ux ux_0]
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1729
              by (simp only: minus_float.rep_eq of_int_minus of_int_1
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1730
                mult_minus_left mult_1_left) simp
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1731
            also have "\<dots> = cos ((- (?ux - 2 * ?lpi)))"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1732
              unfolding uminus_float.rep_eq cos_minus ..
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1733
            also have "\<dots> \<le> (ub_cos prec (- (?ux - 2 * ?lpi)))"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1734
              using lb_cos_minus[OF pi_ux ux_0] by simp
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1735
            finally show ?thesis unfolding u by (simp add: real_of_float_max)
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1736
          qed
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1737
          thus ?thesis unfolding l by auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1738
        next
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1739
          case 4: False
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1740
          show ?thesis
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1741
          proof (cases "-2 * ?lpi \<le> ?lx \<and> ?ux \<le> 0")
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1742
            case Cond: True
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1743
            with bnds 1 2 3 4 have l: "l = Float (- 1) 0"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1744
              and u: "u = max (ub_cos prec (?lx + 2 * ?lpi)) (ub_cos prec (-?ux))"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1745
              by (auto simp add: bnds_cos_def Let_def)
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1746
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1747
            have "cos x \<le> u"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1748
            proof (cases "-pi < x - k * (2 * pi)")
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1749
              case True
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1750
              hence "-pi \<le> x - k * (2 * pi)" by simp
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1751
              from negative_ux[OF this Cond[THEN conjunct2]] show ?thesis
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1752
                unfolding u by (simp add: real_of_float_max)
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1753
            next
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1754
              case False
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1755
              hence "x - k * (2 * pi) \<le> -pi" by simp
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1756
              hence pi_x: "x - k * (2 * pi) + 2 * pi \<le> pi" by simp
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1757
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1758
              have "-2 * pi \<le> ?lx" using Cond lpi by auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1759
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1760
              hence "0 \<le> x - k * (2 * pi) + 2 * pi" using lx by simp
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1761
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1762
              have lx_0: "0 \<le> real_of_float (?lx + 2 * ?lpi)"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1763
                using Cond lpi by auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1764
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1765
              from 1 and Cond have "\<not> -?lpi \<le> ?lx" by auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1766
              hence "?lx + 2 * ?lpi \<le> ?lpi" by auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1767
              hence pi_lx: "(?lx + 2 * ?lpi) \<le> pi"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1768
                using lpi[THEN le_imp_neg_le] by auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1769
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1770
              have lx_le_x: "(?lx + 2 * ?lpi) \<le> x - k * (2 * pi) + 2 * pi"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1771
                using lx lpi by auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1772
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1773
              have "cos x = cos (x + (-k) * (2 * pi) + (1 :: int) * (2 * pi))"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1774
                unfolding cos_periodic_int ..
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1775
              also have "\<dots> \<le> cos ((?lx + 2 * ?lpi))"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1776
                using cos_monotone_0_pi_le[OF lx_0 lx_le_x pi_x]
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1777
                by (simp only: minus_float.rep_eq of_int_minus of_int_1
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1778
                  mult_minus_left mult_1_left) simp
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1779
              also have "\<dots> \<le> (ub_cos prec (?lx + 2 * ?lpi))"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1780
                using lb_cos[OF lx_0 pi_lx] by simp
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1781
              finally show ?thesis unfolding u by (simp add: real_of_float_max)
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1782
            qed
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1783
            thus ?thesis unfolding l by auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1784
          next
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1785
            case False
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1786
            with bnds 1 2 3 4 show ?thesis
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1787
              by (auto simp add: bnds_cos_def Let_def)
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1788
          qed
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1789
        qed
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1790
      qed
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1791
    qed
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1792
  qed
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1793
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1794
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1795
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1796
section "Exponential function"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1797
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1798
subsection "Compute the series of the exponential function"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1799
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1800
fun ub_exp_horner :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> float \<Rightarrow> float"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1801
  and lb_exp_horner :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> float \<Rightarrow> float"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1802
where
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1803
"ub_exp_horner prec 0 i k x       = 0" |
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1804
"ub_exp_horner prec (Suc n) i k x = float_plus_up prec
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1805
    (rapprox_rat prec 1 (int k)) (float_round_up prec (x * lb_exp_horner prec n (i + 1) (k * i) x))" |
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1806
"lb_exp_horner prec 0 i k x       = 0" |
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1807
"lb_exp_horner prec (Suc n) i k x = float_plus_down prec
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1808
    (lapprox_rat prec 1 (int k)) (float_round_down prec (x * ub_exp_horner prec n (i + 1) (k * i) x))"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1809
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1810
lemma bnds_exp_horner:
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1811
  assumes "real_of_float x \<le> 0"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1812
  shows "exp x \<in> {lb_exp_horner prec (get_even n) 1 1 x .. ub_exp_horner prec (get_odd n) 1 1 x}"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1813
proof -
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1814
  have f_eq: "fact (Suc n) = fact n * ((\<lambda>i::nat. i + 1) ^^ n) 1" for n
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1815
  proof -
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1816
    have F: "\<And> m. ((\<lambda>i. i + 1) ^^ n) m = n + m"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1817
      by (induct n) auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1818
    show ?thesis
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1819
      unfolding F by auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1820
  qed
31467
f7d2aa438bee Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
hoelzl
parents: 31148
diff changeset
  1821
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1822
  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
  1823
    OF assms f_eq lb_exp_horner.simps ub_exp_horner.simps]
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1824
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1825
  have "lb_exp_horner prec (get_even n) 1 1 x \<le> exp x"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1826
  proof -
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1827
    have "lb_exp_horner prec (get_even n) 1 1 x \<le> (\<Sum>j = 0..<get_even n. 1 / (fact j) * real_of_float x ^ j)"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1828
      using bounds(1) by auto
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1829
    also have "\<dots> \<le> exp x"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1830
    proof -
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1831
      obtain t where "\<bar>t\<bar> \<le> \<bar>real_of_float x\<bar>" and "exp x = (\<Sum>m = 0..<get_even n. real_of_float x ^ m / (fact m)) + exp t / (fact (get_even n)) * (real_of_float x) ^ (get_even n)"
56195
c7dfd924a165 adapt to Isabelle/c726ecfb22b6
huffman
parents: 56073
diff changeset
  1832
        using Maclaurin_exp_le unfolding atLeast0LessThan by blast
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1833
      moreover have "0 \<le> exp t / (fact (get_even n)) * (real_of_float x) ^ (get_even n)"
56571
f4635657d66f added divide_nonneg_nonneg and co; made it a simp rule
hoelzl
parents: 56544
diff changeset
  1834
        by (auto simp: zero_le_even_power)
56536
aefb4a8da31f made mult_nonneg_nonneg a simp rule
nipkow
parents: 56483
diff changeset
  1835
      ultimately show ?thesis using get_odd exp_gt_zero by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1836
    qed
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1837
    finally show ?thesis .
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1838
  qed
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1839
  moreover
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1840
  have "exp x \<le> ub_exp_horner prec (get_odd n) 1 1 x"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1841
  proof -
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1842
    have x_less_zero: "real_of_float x ^ get_odd n \<le> 0"
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1843
    proof (cases "real_of_float x = 0")
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1844
      case True
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1845
      have "(get_odd n) \<noteq> 0" using get_odd[THEN odd_pos] by auto
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1846
      thus ?thesis unfolding True power_0_left by auto
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1847
    next
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1848
      case False hence "real_of_float x < 0" using \<open>real_of_float x \<le> 0\<close> by auto
63263
c6c95d64607a approximation, derivative, and continuity of floor and ceiling
immler
parents: 63248
diff changeset
  1849
      show ?thesis by (rule less_imp_le, auto simp add: \<open>real_of_float x < 0\<close>)
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1850
    qed
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1851
    obtain t where "\<bar>t\<bar> \<le> \<bar>real_of_float x\<bar>"
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1852
      and "exp x = (\<Sum>m = 0..<get_odd n. (real_of_float x) ^ m / (fact m)) + exp t / (fact (get_odd n)) * (real_of_float x) ^ (get_odd n)"
56195
c7dfd924a165 adapt to Isabelle/c726ecfb22b6
huffman
parents: 56073
diff changeset
  1853
      using Maclaurin_exp_le unfolding atLeast0LessThan by blast
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1854
    moreover have "exp t / (fact (get_odd n)) * (real_of_float x) ^ (get_odd n) \<le> 0"
46545
haftmann
parents: 45481
diff changeset
  1855
      by (auto intro!: mult_nonneg_nonpos divide_nonpos_pos simp add: x_less_zero)
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1856
    ultimately have "exp x \<le> (\<Sum>j = 0..<get_odd n. 1 / (fact j) * real_of_float x ^ j)"
56536
aefb4a8da31f made mult_nonneg_nonneg a simp rule
nipkow
parents: 56483
diff changeset
  1857
      using get_odd exp_gt_zero by auto
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1858
    also have "\<dots> \<le> ub_exp_horner prec (get_odd n) 1 1 x"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1859
      using bounds(2) by auto
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1860
    finally show ?thesis .
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1861
  qed
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1862
  ultimately show ?thesis by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1863
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1864
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1865
lemma ub_exp_horner_nonneg: "real_of_float x \<le> 0 \<Longrightarrow>
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1866
  0 \<le> real_of_float (ub_exp_horner prec (get_odd n) (Suc 0) (Suc 0) x)"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1867
  using bnds_exp_horner[of x prec n]
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1868
  by (intro order_trans[OF exp_ge_zero]) auto
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1869
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1870
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1871
subsection "Compute the exponential function on the entire domain"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1872
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1873
function ub_exp :: "nat \<Rightarrow> float \<Rightarrow> float" and lb_exp :: "nat \<Rightarrow> float \<Rightarrow> float" where
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1874
"lb_exp prec x =
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1875
  (if 0 < x then float_divl prec 1 (ub_exp prec (-x))
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1876
  else
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1877
    let
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1878
      horner = (\<lambda> x. let  y = lb_exp_horner prec (get_even (prec + 2)) 1 1 x in
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1879
        if y \<le> 0 then Float 1 (- 2) else y)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1880
    in
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1881
      if x < - 1 then
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1882
        power_down_fl prec (horner (float_divl prec x (- floor_fl x))) (nat (- int_floor_fl x))
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1883
      else horner x)" |
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1884
"ub_exp prec x =
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1885
  (if 0 < x then float_divr prec 1 (lb_exp prec (-x))
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1886
  else if x < - 1 then
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1887
    power_up_fl prec
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1888
      (ub_exp_horner prec (get_odd (prec + 2)) 1 1
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1889
        (float_divr prec x (- floor_fl x))) (nat (- int_floor_fl x))
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1890
  else ub_exp_horner prec (get_odd (prec + 2)) 1 1 x)"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1891
  by pat_completeness auto
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1892
termination
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1893
  by (relation "measure (\<lambda> v. let (prec, x) = case_sum id id v in (if 0 < x then 1 else 0))") auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1894
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1895
lemma exp_m1_ge_quarter: "(1 / 4 :: real) \<le> exp (- 1)"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1896
proof -
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1897
  have eq4: "4 = Suc (Suc (Suc (Suc 0)))" by auto
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1898
  have "1 / 4 = (Float 1 (- 2))"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1899
    unfolding Float_num by auto
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1900
  also have "\<dots> \<le> lb_exp_horner 3 (get_even 3) 1 1 (- 1)"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1901
    by (subst less_eq_float.rep_eq [symmetric]) code_simp
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1902
  also have "\<dots> \<le> exp (- 1 :: float)"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1903
    using bnds_exp_horner[where x="- 1"] by auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1904
  finally show ?thesis
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1905
    by simp
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1906
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1907
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1908
lemma lb_exp_pos:
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1909
  assumes "\<not> 0 < x"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1910
  shows "0 < lb_exp prec x"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1911
proof -
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1912
  let "?lb_horner x" = "lb_exp_horner prec (get_even (prec + 2)) 1 1 x"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1913
  let "?horner x" = "let y = ?lb_horner x in if y \<le> 0 then Float 1 (- 2) else y"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1914
  have pos_horner: "0 < ?horner x" for x
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1915
    unfolding Let_def by (cases "?lb_horner x \<le> 0") auto
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1916
  moreover have "0 < real_of_float ((?horner x) ^ num)" for x :: float and num :: nat
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1917
  proof -
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1918
    have "0 < real_of_float (?horner x) ^ num" using \<open>0 < ?horner x\<close> by simp
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1919
    also have "\<dots> = (?horner x) ^ num" by auto
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1920
    finally show ?thesis .
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1921
  qed
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1922
  ultimately show ?thesis
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
  1923
    unfolding lb_exp.simps if_not_P[OF \<open>\<not> 0 < x\<close>] Let_def
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1924
    by (cases "floor_fl x", cases "x < - 1")
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1925
      (auto simp: real_power_up_fl real_power_down_fl intro!: power_up_less power_down_pos)
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1926
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1927
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1928
lemma exp_boundaries':
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1929
  assumes "x \<le> 0"
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1930
  shows "exp x \<in> { (lb_exp prec x) .. (ub_exp prec x)}"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1931
proof -
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1932
  let "?lb_exp_horner x" = "lb_exp_horner prec (get_even (prec + 2)) 1 1 x"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1933
  let "?ub_exp_horner x" = "ub_exp_horner prec (get_odd (prec + 2)) 1 1 x"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1934
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1935
  have "real_of_float x \<le> 0" and "\<not> x > 0"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1936
    using \<open>x \<le> 0\<close> by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1937
  show ?thesis
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1938
  proof (cases "x < - 1")
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1939
    case False
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1940
    hence "- 1 \<le> real_of_float x" by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1941
    show ?thesis
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1942
    proof (cases "?lb_exp_horner x \<le> 0")
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1943
      case True
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1944
      from \<open>\<not> x < - 1\<close>
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1945
      have "- 1 \<le> real_of_float x" by auto
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1946
      hence "exp (- 1) \<le> exp x"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1947
        unfolding exp_le_cancel_iff .
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1948
      from order_trans[OF exp_m1_ge_quarter this] have "Float 1 (- 2) \<le> exp x"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1949
        unfolding Float_num .
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1950
      with True show ?thesis
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1951
        using bnds_exp_horner \<open>real_of_float x \<le> 0\<close> \<open>\<not> x > 0\<close> \<open>\<not> x < - 1\<close> by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1952
    next
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1953
      case False
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1954
      thus ?thesis
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1955
        using bnds_exp_horner \<open>real_of_float x \<le> 0\<close> \<open>\<not> x > 0\<close> \<open>\<not> x < - 1\<close> by (auto simp add: Let_def)
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1956
    qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1957
  next
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1958
    case True
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1959
    let ?num = "nat (- int_floor_fl x)"
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1960
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1961
    have "real_of_int (int_floor_fl x) < - 1"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1962
      using int_floor_fl[of x] \<open>x < - 1\<close> by simp
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1963
    hence "real_of_int (int_floor_fl x) < 0" by simp
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1964
    hence "int_floor_fl x < 0" by auto
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1965
    hence "1 \<le> - int_floor_fl x" by auto
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1966
    hence "0 < nat (- int_floor_fl x)" by auto
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1967
    hence "0 < ?num"  by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1968
    hence "real ?num \<noteq> 0" by auto
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1969
    have num_eq: "real ?num = - int_floor_fl x"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1970
      using \<open>0 < nat (- int_floor_fl x)\<close> by auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1971
    have "0 < - int_floor_fl x"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1972
      using \<open>0 < ?num\<close>[unfolded of_nat_less_iff[symmetric]] by simp
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1973
    hence "real_of_int (int_floor_fl x) < 0"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1974
      unfolding less_float_def by auto
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1975
    have fl_eq: "real_of_int (- int_floor_fl x) = real_of_float (- floor_fl x)"
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1976
      by (simp add: floor_fl_def int_floor_fl_def)
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1977
    from \<open>0 < - int_floor_fl x\<close> have "0 \<le> real_of_float (- floor_fl x)"
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1978
      by (simp add: floor_fl_def int_floor_fl_def)
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1979
    from \<open>real_of_int (int_floor_fl x) < 0\<close> have "real_of_float (floor_fl x) < 0"
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1980
      by (simp add: floor_fl_def int_floor_fl_def)
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1981
    have "exp x \<le> ub_exp prec x"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1982
    proof -
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1983
      have div_less_zero: "real_of_float (float_divr prec x (- floor_fl x)) \<le> 0"
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1984
        using float_divr_nonpos_pos_upper_bound[OF \<open>real_of_float x \<le> 0\<close> \<open>0 \<le> real_of_float (- floor_fl x)\<close>]
47601
050718fe6eee use real :: float => real as lifting-morphism so we can directlry use the rep_eq theorems
hoelzl
parents: 47600
diff changeset
  1985
        unfolding less_eq_float_def zero_float.rep_eq .
31809
hoelzl
parents: 31790
diff changeset
  1986
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1987
      have "exp x = exp (?num * (x / ?num))"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1988
        using \<open>real ?num \<noteq> 0\<close> by auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1989
      also have "\<dots> = exp (x / ?num) ^ ?num"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1990
        unfolding exp_real_of_nat_mult ..
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1991
      also have "\<dots> \<le> exp (float_divr prec x (- floor_fl x)) ^ ?num"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1992
        unfolding num_eq fl_eq
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  1993
        by (rule power_mono, rule exp_le_cancel_iff[THEN iffD2], rule float_divr) auto
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1994
      also have "\<dots> \<le> (?ub_exp_horner (float_divr prec x (- floor_fl x))) ^ ?num"
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1995
        unfolding real_of_float_power
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  1996
        by (rule power_mono, rule bnds_exp_horner[OF div_less_zero, unfolded atLeastAtMost_iff, THEN conjunct2], auto)
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  1997
      also have "\<dots> \<le> real_of_float (power_up_fl prec (?ub_exp_horner (float_divr prec x (- floor_fl x))) ?num)"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1998
        by (auto simp add: real_power_up_fl intro!: power_up ub_exp_horner_nonneg div_less_zero)
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  1999
      finally show ?thesis
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2000
        unfolding ub_exp.simps if_not_P[OF \<open>\<not> 0 < x\<close>] if_P[OF \<open>x < - 1\<close>] floor_fl_def Let_def .
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2001
    qed
31809
hoelzl
parents: 31790
diff changeset
  2002
    moreover
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2003
    have "lb_exp prec x \<le> exp x"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2004
    proof -
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2005
      let ?divl = "float_divl prec x (- floor_fl x)"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2006
      let ?horner = "?lb_exp_horner ?divl"
31809
hoelzl
parents: 31790
diff changeset
  2007
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2008
      show ?thesis
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2009
      proof (cases "?horner \<le> 0")
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2010
        case False
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2011
        hence "0 \<le> real_of_float ?horner" by auto
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2012
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2013
        have div_less_zero: "real_of_float (float_divl prec x (- floor_fl x)) \<le> 0"
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2014
          using \<open>real_of_float (floor_fl x) < 0\<close> \<open>real_of_float x \<le> 0\<close>
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2015
          by (auto intro!: order_trans[OF float_divl] divide_nonpos_neg)
56479
91958d4b30f7 revert c1bbd3e22226, a14831ac3023, and 36489d77c484: divide_minus_left/right are again simp rules
hoelzl
parents: 56410
diff changeset
  2016
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2017
        have "(?lb_exp_horner (float_divl prec x (- floor_fl x))) ^ ?num \<le>
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2018
          exp (float_divl prec x (- floor_fl x)) ^ ?num"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2019
          using \<open>0 \<le> real_of_float ?horner\<close>[unfolded floor_fl_def[symmetric]]
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2020
            bnds_exp_horner[OF div_less_zero, unfolded atLeastAtMost_iff, THEN conjunct1]
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2021
          by (auto intro!: power_mono)
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2022
        also have "\<dots> \<le> exp (x / ?num) ^ ?num"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2023
          unfolding num_eq fl_eq
47601
050718fe6eee use real :: float => real as lifting-morphism so we can directlry use the rep_eq theorems
hoelzl
parents: 47600
diff changeset
  2024
          using float_divl by (auto intro!: power_mono simp del: uminus_float.rep_eq)
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2025
        also have "\<dots> = exp (?num * (x / ?num))"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2026
          unfolding exp_real_of_nat_mult ..
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2027
        also have "\<dots> = exp x"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2028
          using \<open>real ?num \<noteq> 0\<close> by auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2029
        finally show ?thesis
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2030
          using False
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2031
          unfolding lb_exp.simps if_not_P[OF \<open>\<not> 0 < x\<close>] if_P[OF \<open>x < - 1\<close>]
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2032
            int_floor_fl_def Let_def if_not_P[OF False]
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2033
          by (auto simp: real_power_down_fl intro!: power_down_le)
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2034
      next
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  2035
        case True
59741
5b762cd73a8e Lots of new material on complex-valued functions. Modified simplification of (x/n)^k
paulson <lp15@cam.ac.uk>
parents: 59730
diff changeset
  2036
        have "power_down_fl prec (Float 1 (- 2))  ?num \<le> (Float 1 (- 2)) ^ ?num"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2037
          by (metis Float_le_zero_iff less_imp_le linorder_not_less
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2038
            not_numeral_le_zero numeral_One power_down_fl)
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2039
        then have "power_down_fl prec (Float 1 (- 2))  ?num \<le> real_of_float (Float 1 (- 2)) ^ ?num"
59741
5b762cd73a8e Lots of new material on complex-valued functions. Modified simplification of (x/n)^k
paulson <lp15@cam.ac.uk>
parents: 59730
diff changeset
  2040
          by simp
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2041
        also
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2042
        have "real_of_float (floor_fl x) \<noteq> 0" and "real_of_float (floor_fl x) \<le> 0"
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2043
          using \<open>real_of_float (floor_fl x) < 0\<close> by auto
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2044
        from divide_right_mono_neg[OF floor_fl[of x] \<open>real_of_float (floor_fl x) \<le> 0\<close>, unfolded divide_self[OF \<open>real_of_float (floor_fl x) \<noteq> 0\<close>]]
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2045
        have "- 1 \<le> x / (- floor_fl x)"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2046
          unfolding minus_float.rep_eq by auto
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  2047
        from order_trans[OF exp_m1_ge_quarter this[unfolded exp_le_cancel_iff[where x="- 1", symmetric]]]
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2048
        have "Float 1 (- 2) \<le> exp (x / (- floor_fl x))"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2049
          unfolding Float_num .
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2050
        hence "real_of_float (Float 1 (- 2)) ^ ?num \<le> exp (x / (- floor_fl x)) ^ ?num"
59741
5b762cd73a8e Lots of new material on complex-valued functions. Modified simplification of (x/n)^k
paulson <lp15@cam.ac.uk>
parents: 59730
diff changeset
  2051
          by (metis Float_num(5) power_mono zero_le_divide_1_iff zero_le_numeral)
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2052
        also have "\<dots> = exp x"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2053
          unfolding num_eq fl_eq exp_real_of_nat_mult[symmetric]
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2054
          using \<open>real_of_float (floor_fl x) \<noteq> 0\<close> by auto
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  2055
        finally show ?thesis
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2056
          unfolding lb_exp.simps if_not_P[OF \<open>\<not> 0 < x\<close>] if_P[OF \<open>x < - 1\<close>]
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2057
            int_floor_fl_def Let_def if_P[OF True] real_of_float_power .
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2058
      qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2059
    qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2060
    ultimately show ?thesis 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
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2064
lemma exp_boundaries: "exp x \<in> { lb_exp prec x .. ub_exp prec x }"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2065
proof -
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2066
  show ?thesis
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2067
  proof (cases "0 < x")
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2068
    case False
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2069
    hence "x \<le> 0" by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2070
    from exp_boundaries'[OF this] show ?thesis .
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2071
  next
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2072
    case True
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2073
    hence "-x \<le> 0" by auto
31809
hoelzl
parents: 31790
diff changeset
  2074
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2075
    have "lb_exp prec x \<le> exp x"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2076
    proof -
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
  2077
      from exp_boundaries'[OF \<open>-x \<le> 0\<close>]
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2078
      have ub_exp: "exp (- real_of_float x) \<le> ub_exp prec (-x)"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2079
        unfolding atLeastAtMost_iff minus_float.rep_eq by auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2080
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2081
      have "float_divl prec 1 (ub_exp prec (-x)) \<le> 1 / ub_exp prec (-x)"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2082
        using float_divl[where x=1] by auto
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2083
      also have "\<dots> \<le> exp x"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2084
        using ub_exp[unfolded inverse_le_iff_le[OF order_less_le_trans[OF exp_gt_zero ub_exp]
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2085
          exp_gt_zero, symmetric]]
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2086
        unfolding exp_minus nonzero_inverse_inverse_eq[OF exp_not_eq_zero] inverse_eq_divide
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2087
        by auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2088
      finally show ?thesis
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2089
        unfolding lb_exp.simps if_P[OF True] .
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2090
    qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2091
    moreover
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2092
    have "exp x \<le> ub_exp prec x"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2093
    proof -
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
  2094
      have "\<not> 0 < -x" using \<open>0 < x\<close> by auto
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
  2095
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
  2096
      from exp_boundaries'[OF \<open>-x \<le> 0\<close>]
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2097
      have lb_exp: "lb_exp prec (-x) \<le> exp (- real_of_float x)"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2098
        unfolding atLeastAtMost_iff minus_float.rep_eq by auto
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2099
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2100
      have "exp x \<le> (1 :: float) / lb_exp prec (-x)"
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
  2101
        using lb_exp lb_exp_pos[OF \<open>\<not> 0 < -x\<close>, of prec]
63263
c6c95d64607a approximation, derivative, and continuity of floor and ceiling
immler
parents: 63248
diff changeset
  2102
        by (simp del: lb_exp.simps add: exp_minus field_simps)
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2103
      also have "\<dots> \<le> float_divr prec 1 (lb_exp prec (-x))"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2104
        using float_divr .
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2105
      finally show ?thesis
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2106
        unfolding ub_exp.simps if_P[OF True] .
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2107
    qed
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2108
    ultimately show ?thesis
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2109
      by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2110
  qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2111
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2112
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2113
lemma bnds_exp: "\<forall>(x::real) lx ux. (l, u) =
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2114
  (lb_exp prec lx, ub_exp prec ux) \<and> x \<in> {lx .. ux} \<longrightarrow> l \<le> exp x \<and> exp x \<le> u"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2115
proof (rule allI, rule allI, rule allI, rule impI)
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2116
  fix x :: real and lx ux
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2117
  assume "(l, u) = (lb_exp prec lx, ub_exp prec ux) \<and> x \<in> {lx .. ux}"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2118
  hence l: "lb_exp prec lx = l " and u: "ub_exp prec ux = u" and x: "x \<in> {lx .. ux}"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2119
    by auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2120
  show "l \<le> exp x \<and> exp x \<le> u"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2121
  proof
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2122
    show "l \<le> exp x"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2123
    proof -
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2124
      from exp_boundaries[of lx prec, unfolded l]
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2125
      have "l \<le> exp lx" by (auto simp del: lb_exp.simps)
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2126
      also have "\<dots> \<le> exp x" using x by auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2127
      finally show ?thesis .
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2128
    qed
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2129
    show "exp x \<le> u"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2130
    proof -
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2131
      have "exp x \<le> exp ux" using x by auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2132
      also have "\<dots> \<le> u" using exp_boundaries[of ux prec, unfolded u] by (auto simp del: ub_exp.simps)
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2133
      finally show ?thesis .
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2134
    qed
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2135
  qed
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2136
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2137
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2138
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2139
section "Logarithm"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2140
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2141
subsection "Compute the logarithm series"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2142
31809
hoelzl
parents: 31790
diff changeset
  2143
fun ub_ln_horner :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> float \<Rightarrow> float"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2144
and lb_ln_horner :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> float \<Rightarrow> float" where
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2145
"ub_ln_horner prec 0 i x       = 0" |
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2146
"ub_ln_horner prec (Suc n) i x = float_plus_up prec
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2147
    (rapprox_rat prec 1 (int i)) (- float_round_down prec (x * lb_ln_horner prec n (Suc i) x))" |
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2148
"lb_ln_horner prec 0 i x       = 0" |
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2149
"lb_ln_horner prec (Suc n) i x = float_plus_down prec
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2150
    (lapprox_rat prec 1 (int i)) (- float_round_up prec (x * ub_ln_horner prec n (Suc i) x))"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2151
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2152
lemma ln_bounds:
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2153
  assumes "0 \<le> x"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2154
    and "x < 1"
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
  2155
  shows "(\<Sum>i=0..<2*n. (- 1) ^ i * (1 / real (i + 1)) * x ^ (Suc i)) \<le> ln (x + 1)" (is "?lb")
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
  2156
  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
  2157
proof -
30952
7ab2716dd93b power operation on functions with syntax o^; power operation on relations with syntax ^^
haftmann
parents: 30886
diff changeset
  2158
  let "?a n" = "(1/real (n +1)) * x ^ (Suc n)"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2159
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
  2160
  have ln_eq: "(\<Sum> i. (- 1) ^ i * ?a i) = ln (x + 1)"
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
  2161
    using ln_series[of "x + 1"] \<open>0 \<le> x\<close> \<open>x < 1\<close> by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2162
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2163
  have "norm x < 1" using assms by auto
61969
e01015e49041 more symbols;
wenzelm
parents: 61945
diff changeset
  2164
  have "?a \<longlonglongrightarrow> 0" unfolding Suc_eq_plus1[symmetric] inverse_eq_divide[symmetric]
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
  2165
    using tendsto_mult[OF LIMSEQ_inverse_real_of_nat LIMSEQ_Suc[OF LIMSEQ_power_zero[OF \<open>norm x < 1\<close>]]] by auto
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2166
  have "0 \<le> ?a n" for n
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2167
    by (rule mult_nonneg_nonneg) (auto simp: \<open>0 \<le> x\<close>)
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2168
  have "?a (Suc n) \<le> ?a n" for n
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2169
    unfolding inverse_eq_divide[symmetric]
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2170
  proof (rule mult_mono)
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2171
    show "0 \<le> x ^ Suc (Suc n)"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2172
      by (auto simp add: \<open>0 \<le> x\<close>)
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2173
    have "x ^ Suc (Suc n) \<le> x ^ Suc n * 1"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2174
      unfolding power_Suc2 mult.assoc[symmetric]
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2175
      by (rule mult_left_mono, fact less_imp_le[OF \<open>x < 1\<close>]) (auto simp: \<open>0 \<le> x\<close>)
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2176
    thus "x ^ Suc (Suc n) \<le> x ^ Suc n" by auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2177
  qed auto
61969
e01015e49041 more symbols;
wenzelm
parents: 61945
diff changeset
  2178
  from summable_Leibniz'(2,4)[OF \<open>?a \<longlonglongrightarrow> 0\<close> \<open>\<And>n. 0 \<le> ?a n\<close>, OF \<open>\<And>n. ?a (Suc n) \<le> ?a n\<close>, unfolded ln_eq]
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2179
  show ?lb and ?ub
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2180
    unfolding atLeast0LessThan by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2181
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2182
31809
hoelzl
parents: 31790
diff changeset
  2183
lemma ln_float_bounds:
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2184
  assumes "0 \<le> real_of_float x"
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2185
    and "real_of_float x < 1"
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2186
  shows "x * lb_ln_horner prec (get_even n) 1 x \<le> ln (x + 1)" (is "?lb \<le> ?ln")
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2187
    and "ln (x + 1) \<le> x * ub_ln_horner prec (get_odd n) 1 x" (is "?ln \<le> ?ub")
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2188
proof -
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2189
  obtain ev where ev: "get_even n = 2 * ev" using get_even_double ..
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2190
  obtain od where od: "get_odd n = 2 * od + 1" using get_odd_double ..
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2191
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2192
  let "?s n" = "(- 1) ^ n * (1 / real (1 + n)) * (real_of_float x)^(Suc n)"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2193
64267
b9a1486e79be setsum -> sum
nipkow
parents: 64246
diff changeset
  2194
  have "?lb \<le> sum ?s {0 ..< 2 * ev}"
b9a1486e79be setsum -> sum
nipkow
parents: 64246
diff changeset
  2195
    unfolding power_Suc2 mult.assoc[symmetric] times_float.rep_eq sum_distrib_right[symmetric]
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2196
    unfolding mult.commute[of "real_of_float x"] ev 
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2197
    using horner_bounds(1)[where G="\<lambda> i k. Suc k" and F="\<lambda>x. x" and f="\<lambda>x. x" 
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2198
                    and lb="\<lambda>n i k x. lb_ln_horner prec n k x" 
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2199
                    and ub="\<lambda>n i k x. ub_ln_horner prec n k x" and j'=1 and n="2*ev",
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2200
      OF \<open>0 \<le> real_of_float x\<close> refl lb_ln_horner.simps ub_ln_horner.simps] \<open>0 \<le> real_of_float x\<close>
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2201
    unfolding real_of_float_power
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2202
    by (rule mult_right_mono)
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2203
  also have "\<dots> \<le> ?ln"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2204
    using ln_bounds(1)[OF \<open>0 \<le> real_of_float x\<close> \<open>real_of_float x < 1\<close>] by auto
31809
hoelzl
parents: 31790
diff changeset
  2205
  finally show "?lb \<le> ?ln" .
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2206
64267
b9a1486e79be setsum -> sum
nipkow
parents: 64246
diff changeset
  2207
  have "?ln \<le> sum ?s {0 ..< 2 * od + 1}"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2208
    using ln_bounds(2)[OF \<open>0 \<le> real_of_float x\<close> \<open>real_of_float x < 1\<close>] by auto
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2209
  also have "\<dots> \<le> ?ub"
64267
b9a1486e79be setsum -> sum
nipkow
parents: 64246
diff changeset
  2210
    unfolding power_Suc2 mult.assoc[symmetric] times_float.rep_eq sum_distrib_right[symmetric]
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2211
    unfolding mult.commute[of "real_of_float x"] od
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2212
    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",
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2213
      OF \<open>0 \<le> real_of_float x\<close> refl lb_ln_horner.simps ub_ln_horner.simps] \<open>0 \<le> real_of_float x\<close>
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2214
    unfolding real_of_float_power
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2215
    by (rule mult_right_mono)
31809
hoelzl
parents: 31790
diff changeset
  2216
  finally show "?ln \<le> ?ub" .
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2217
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2218
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2219
lemma ln_add:
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2220
  fixes x :: real
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2221
  assumes "0 < x" and "0 < y"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2222
  shows "ln (x + y) = ln x + ln (1 + y / x)"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2223
proof -
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2224
  have "x \<noteq> 0" using assms by auto
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2225
  have "x + y = x * (1 + y / x)"
64240
eabf80376aab more standardized names
haftmann
parents: 63931
diff changeset
  2226
    unfolding distrib_left times_divide_eq_right nonzero_mult_div_cancel_left[OF \<open>x \<noteq> 0\<close>]
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2227
    by auto
31809
hoelzl
parents: 31790
diff changeset
  2228
  moreover
56541
0e3abadbef39 made divide_pos_pos a simp rule
nipkow
parents: 56536
diff changeset
  2229
  have "0 < y / x" using assms by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2230
  hence "0 < 1 + y / x" by auto
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2231
  ultimately show ?thesis
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2232
    using ln_mult assms by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2233
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2234
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2235
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2236
subsection "Compute the logarithm of 2"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2237
31809
hoelzl
parents: 31790
diff changeset
  2238
definition ub_ln2 where "ub_ln2 prec = (let third = rapprox_rat (max prec 1) 1 3
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2239
                                        in float_plus_up prec
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2240
                                          ((Float 1 (- 1) * ub_ln_horner prec (get_odd prec) 1 (Float 1 (- 1))))
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2241
                                           (float_round_up prec (third * ub_ln_horner prec (get_odd prec) 1 third)))"
31809
hoelzl
parents: 31790
diff changeset
  2242
definition lb_ln2 where "lb_ln2 prec = (let third = lapprox_rat prec 1 3
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2243
                                        in float_plus_down prec
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2244
                                          ((Float 1 (- 1) * lb_ln_horner prec (get_even prec) 1 (Float 1 (- 1))))
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2245
                                           (float_round_down prec (third * lb_ln_horner prec (get_even prec) 1 third)))"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2246
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2247
lemma ub_ln2: "ln 2 \<le> ub_ln2 prec" (is "?ub_ln2")
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2248
  and lb_ln2: "lb_ln2 prec \<le> ln 2" (is "?lb_ln2")
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2249
proof -
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2250
  let ?uthird = "rapprox_rat (max prec 1) 1 3"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2251
  let ?lthird = "lapprox_rat prec 1 3"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2252
60017
b785d6d06430 Overloading of ln and powr, but "approximation" no longer works for powr. Code generation also fails due to type ambiguity in scala.
paulson <lp15@cam.ac.uk>
parents: 59850
diff changeset
  2253
  have ln2_sum: "ln 2 = ln (1/2 + 1) + ln (1 / 3 + 1::real)"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2254
    using ln_add[of "3 / 2" "1 / 2"] by auto
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2255
  have lb3: "?lthird \<le> 1 / 3" using lapprox_rat[of prec 1 3] by auto
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2256
  hence lb3_ub: "real_of_float ?lthird < 1" by auto
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2257
  have lb3_lb: "0 \<le> real_of_float ?lthird" using lapprox_rat_nonneg[of 1 3] by auto
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2258
  have ub3: "1 / 3 \<le> ?uthird" using rapprox_rat[of 1 3] by auto
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2259
  hence ub3_lb: "0 \<le> real_of_float ?uthird" by auto
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2260
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2261
  have lb2: "0 \<le> real_of_float (Float 1 (- 1))" and ub2: "real_of_float (Float 1 (- 1)) < 1"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2262
    unfolding Float_num by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2263
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2264
  have "0 \<le> (1::int)" and "0 < (3::int)" by auto
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2265
  have ub3_ub: "real_of_float ?uthird < 1"
58982
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58889
diff changeset
  2266
    by (simp add: Float.compute_rapprox_rat Float.compute_lapprox_rat rapprox_posrat_less1)
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2267
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2268
  have third_gt0: "(0 :: real) < 1 / 3 + 1" by auto
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2269
  have uthird_gt0: "0 < real_of_float ?uthird + 1" using ub3_lb by auto
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2270
  have lthird_gt0: "0 < real_of_float ?lthird + 1" using lb3_lb by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2271
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2272
  show ?ub_ln2
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2273
    unfolding ub_ln2_def Let_def ln2_sum Float_num(4)[symmetric]
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2274
  proof (rule float_plus_up_le, rule add_mono, fact ln_float_bounds(2)[OF lb2 ub2])
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2275
    have "ln (1 / 3 + 1) \<le> ln (real_of_float ?uthird + 1)"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2276
      unfolding ln_le_cancel_iff[OF third_gt0 uthird_gt0] using ub3 by auto
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2277
    also have "\<dots> \<le> ?uthird * ub_ln_horner prec (get_odd prec) 1 ?uthird"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2278
      using ln_float_bounds(2)[OF ub3_lb ub3_ub] .
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2279
    also note float_round_up
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2280
    finally show "ln (1 / 3 + 1) \<le> float_round_up prec (?uthird * ub_ln_horner prec (get_odd prec) 1 ?uthird)" .
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2281
  qed
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2282
  show ?lb_ln2
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2283
    unfolding lb_ln2_def Let_def ln2_sum Float_num(4)[symmetric]
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2284
  proof (rule float_plus_down_le, rule add_mono, fact ln_float_bounds(1)[OF lb2 ub2])
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2285
    have "?lthird * lb_ln_horner prec (get_even prec) 1 ?lthird \<le> ln (real_of_float ?lthird + 1)"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2286
      using ln_float_bounds(1)[OF lb3_lb lb3_ub] .
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2287
    note float_round_down_le[OF this]
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2288
    also have "\<dots> \<le> ln (1 / 3 + 1)"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2289
      unfolding ln_le_cancel_iff[OF lthird_gt0 third_gt0]
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2290
      using lb3 by auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2291
    finally show "float_round_down prec (?lthird * lb_ln_horner prec (get_even prec) 1 ?lthird) \<le>
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2292
      ln (1 / 3 + 1)" .
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2293
  qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2294
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2295
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2296
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2297
subsection "Compute the logarithm in the entire domain"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2298
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2299
function ub_ln :: "nat \<Rightarrow> float \<Rightarrow> float option" and lb_ln :: "nat \<Rightarrow> float \<Rightarrow> float option" where
31468
b8267feaf342 Approximation: Corrected precision of ln on all real values
hoelzl
parents: 31467
diff changeset
  2300
"ub_ln prec x = (if x \<le> 0          then None
b8267feaf342 Approximation: Corrected precision of ln on all real values
hoelzl
parents: 31467
diff changeset
  2301
            else if x < 1          then Some (- the (lb_ln prec (float_divl (max prec 1) 1 x)))
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2302
            else let horner = \<lambda>x. float_round_up prec (x * ub_ln_horner prec (get_odd prec) 1 x) in
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
  2303
                 if x \<le> Float 3 (- 1) then Some (horner (x - 1))
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2304
            else if x < Float 1 1  then Some (float_round_up prec (horner (Float 1 (- 1)) + horner (x * rapprox_rat prec 2 3 - 1)))
31468
b8267feaf342 Approximation: Corrected precision of ln on all real values
hoelzl
parents: 31467
diff changeset
  2305
                                   else let l = bitlen (mantissa x) - 1 in
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2306
                                        Some (float_plus_up prec (float_round_up prec (ub_ln2 prec * (Float (exponent x + l) 0))) (horner (Float (mantissa x) (- l) - 1))))" |
31468
b8267feaf342 Approximation: Corrected precision of ln on all real values
hoelzl
parents: 31467
diff changeset
  2307
"lb_ln prec x = (if x \<le> 0          then None
b8267feaf342 Approximation: Corrected precision of ln on all real values
hoelzl
parents: 31467
diff changeset
  2308
            else if x < 1          then Some (- the (ub_ln prec (float_divr prec 1 x)))
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2309
            else let horner = \<lambda>x. float_round_down prec (x * lb_ln_horner prec (get_even prec) 1 x) in
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
  2310
                 if x \<le> Float 3 (- 1) then Some (horner (x - 1))
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2311
            else if x < Float 1 1  then Some (float_round_down prec (horner (Float 1 (- 1)) +
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2312
                                              horner (max (x * lapprox_rat prec 2 3 - 1) 0)))
31468
b8267feaf342 Approximation: Corrected precision of ln on all real values
hoelzl
parents: 31467
diff changeset
  2313
                                   else let l = bitlen (mantissa x) - 1 in
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2314
                                        Some (float_plus_down prec (float_round_down prec (lb_ln2 prec * (Float (exponent x + l) 0))) (horner (Float (mantissa x) (- l) - 1))))"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2315
  by pat_completeness auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2316
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2317
termination
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2318
proof (relation "measure (\<lambda> v. let (prec, x) = case_sum id id v in (if x < 1 then 1 else 0))", auto)
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2319
  fix prec and x :: float
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2320
  assume "\<not> real_of_float x \<le> 0" and "real_of_float x < 1" and "real_of_float (float_divl (max prec (Suc 0)) 1 x) < 1"
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2321
  hence "0 < real_of_float x" "1 \<le> max prec (Suc 0)" "real_of_float x < 1"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2322
    by auto
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2323
  from float_divl_pos_less1_bound[OF \<open>0 < real_of_float x\<close> \<open>real_of_float x < 1\<close>[THEN less_imp_le] \<open>1 \<le> max prec (Suc 0)\<close>]
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2324
  show False
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2325
    using \<open>real_of_float (float_divl (max prec (Suc 0)) 1 x) < 1\<close> by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2326
next
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2327
  fix prec x
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2328
  assume "\<not> real_of_float x \<le> 0" and "real_of_float x < 1" and "real_of_float (float_divr prec 1 x) < 1"
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  2329
  hence "0 < x" by auto
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2330
  from float_divr_pos_less1_lower_bound[OF \<open>0 < x\<close>, of prec] \<open>real_of_float x < 1\<close> show False
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2331
    using \<open>real_of_float (float_divr prec 1 x) < 1\<close> by auto
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2332
qed
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2333
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2334
lemma float_pos_eq_mantissa_pos: "x > 0 \<longleftrightarrow> mantissa x > 0"
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2335
  apply (subst Float_mantissa_exponent[of x, symmetric])
60017
b785d6d06430 Overloading of ln and powr, but "approximation" no longer works for powr. Code generation also fails due to type ambiguity in scala.
paulson <lp15@cam.ac.uk>
parents: 59850
diff changeset
  2336
  apply (auto simp add: zero_less_mult_iff zero_float_def  dest: less_zeroE)
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2337
  apply (metis not_le powr_ge_pzero)
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2338
  done
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2339
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2340
lemma Float_pos_eq_mantissa_pos: "Float m e > 0 \<longleftrightarrow> m > 0"
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2341
  using powr_gt_zero[of 2 "e"]
54269
dcdfec41a325 tuned proofs in Approximation
hoelzl
parents: 54230
diff changeset
  2342
  by (auto simp add: zero_less_mult_iff zero_float_def simp del: powr_gt_zero dest: less_zeroE)
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2343
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2344
lemma Float_representation_aux:
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2345
  fixes m e
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2346
  defines "x \<equiv> Float m e"
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2347
  assumes "x > 0"
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2348
  shows "Float (exponent x + (bitlen (mantissa x) - 1)) 0 = Float (e + (bitlen m - 1)) 0" (is ?th1)
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2349
    and "Float (mantissa x) (- (bitlen (mantissa x) - 1)) = Float m ( - (bitlen m - 1))"  (is ?th2)
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2350
proof -
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2351
  from assms have mantissa_pos: "m > 0" "mantissa x > 0"
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  2352
    using Float_pos_eq_mantissa_pos[of m e] float_pos_eq_mantissa_pos[of x] by simp_all
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2353
  thus ?th1
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2354
    using bitlen_Float[of m e] assms
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2355
    by (auto simp add: zero_less_mult_iff intro!: arg_cong2[where f=Float])
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  2356
  have "x \<noteq> float_of 0"
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
  2357
    unfolding zero_float_def[symmetric] using \<open>0 < x\<close> by auto
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2358
  from denormalize_shift[OF assms(1) this] guess i . note i = this
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  2359
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2360
  have "2 powr (1 - (real_of_int (bitlen (mantissa x)) + real_of_int i)) =
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2361
    2 powr (1 - (real_of_int (bitlen (mantissa x)))) * inverse (2 powr (real i))"
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2362
    by (simp add: powr_minus[symmetric] powr_add[symmetric] field_simps)
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2363
  hence "real_of_int (mantissa x) * 2 powr (1 - real_of_int (bitlen (mantissa x))) =
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2364
    (real_of_int (mantissa x) * 2 ^ i) * 2 powr (1 - real_of_int (bitlen (mantissa x * 2 ^ i)))"
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
  2365
    using \<open>mantissa x > 0\<close> by (simp add: powr_realpow)
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  2366
  then show ?th2
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  2367
    unfolding i by transfer auto
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2368
qed
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2369
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2370
lemma compute_ln[code]:
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2371
  fixes m e
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2372
  defines "x \<equiv> Float m e"
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2373
  shows "ub_ln prec x = (if x \<le> 0          then None
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2374
              else if x < 1          then Some (- the (lb_ln prec (float_divl (max prec 1) 1 x)))
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2375
            else let horner = \<lambda>x. float_round_up prec (x * ub_ln_horner prec (get_odd prec) 1 x) in
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
  2376
                 if x \<le> Float 3 (- 1) then Some (horner (x - 1))
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2377
            else if x < Float 1 1  then Some (float_round_up prec (horner (Float 1 (- 1)) + horner (x * rapprox_rat prec 2 3 - 1)))
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2378
                                   else let l = bitlen m - 1 in
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2379
                                        Some (float_plus_up prec (float_round_up prec (ub_ln2 prec * (Float (e + l) 0))) (horner (Float m (- l) - 1))))"
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2380
    (is ?th1)
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2381
  and "lb_ln prec x = (if x \<le> 0          then None
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2382
            else if x < 1          then Some (- the (ub_ln prec (float_divr prec 1 x)))
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2383
            else let horner = \<lambda>x. float_round_down prec (x * lb_ln_horner prec (get_even prec) 1 x) in
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
  2384
                 if x \<le> Float 3 (- 1) then Some (horner (x - 1))
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2385
            else if x < Float 1 1  then Some (float_round_down prec (horner (Float 1 (- 1)) +
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2386
                                              horner (max (x * lapprox_rat prec 2 3 - 1) 0)))
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2387
                                   else let l = bitlen m - 1 in
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2388
                                        Some (float_plus_down prec (float_round_down prec (lb_ln2 prec * (Float (e + l) 0))) (horner (Float m (- l) - 1))))"
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2389
    (is ?th2)
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2390
proof -
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2391
  from assms Float_pos_eq_mantissa_pos have "x > 0 \<Longrightarrow> m > 0"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2392
    by simp
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2393
  thus ?th1 ?th2
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2394
    using Float_representation_aux[of m e]
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2395
    unfolding x_def[symmetric]
61824
dcbe9f756ae0 not_leE -> not_le_imp_less and other tidying
paulson <lp15@cam.ac.uk>
parents: 61649
diff changeset
  2396
    by (auto dest: not_le_imp_less)
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2397
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2398
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2399
lemma ln_shifted_float:
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2400
  assumes "0 < m"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2401
  shows "ln (Float m e) = ln 2 * (e + (bitlen m - 1)) + ln (Float m (- (bitlen m - 1)))"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2402
proof -
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2403
  let ?B = "2^nat (bitlen m - 1)"
63040
eb4ddd18d635 eliminated old 'def';
wenzelm
parents: 62390
diff changeset
  2404
  define bl where "bl = bitlen m - 1"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2405
  have "0 < real_of_int m" and "\<And>X. (0 :: real) < 2^X" and "0 < (2 :: real)" and "m \<noteq> 0"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2406
    using assms by auto
63248
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63170
diff changeset
  2407
  hence "0 \<le> bl" by (simp add: bitlen_alt_def bl_def)
31468
b8267feaf342 Approximation: Corrected precision of ln on all real values
hoelzl
parents: 31467
diff changeset
  2408
  show ?thesis
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2409
  proof (cases "0 \<le> e")
59751
916c0f6c83e3 New material for complex sin, cos, tan, Ln, also some reorganisation
paulson <lp15@cam.ac.uk>
parents: 59741
diff changeset
  2410
    case True
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2411
    thus ?thesis
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2412
      unfolding bl_def[symmetric] using \<open>0 < real_of_int m\<close> \<open>0 \<le> bl\<close>
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2413
      apply (simp add: ln_mult)
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2414
      apply (cases "e=0")
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2415
        apply (cases "bl = 0", simp_all add: powr_minus ln_inverse ln_powr)
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2416
        apply (cases "bl = 0", simp_all add: powr_minus ln_inverse ln_powr field_simps)
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2417
      done
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2418
  next
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2419
    case False
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2420
    hence "0 < -e" by auto
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2421
    have lne: "ln (2 powr real_of_int e) = ln (inverse (2 powr - e))"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2422
      by (simp add: powr_minus)
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2423
    hence pow_gt0: "(0::real) < 2^nat (-e)"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2424
      by auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2425
    hence inv_gt0: "(0::real) < inverse (2^nat (-e))"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2426
      by auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2427
    show ?thesis
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2428
      using False unfolding bl_def[symmetric]
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2429
      using \<open>0 < real_of_int m\<close> \<open>0 \<le> bl\<close>
56483
5b82c58b665c generalize ln/log_powr; add log_base_powr/pow
hoelzl
parents: 56479
diff changeset
  2430
      by (auto simp add: lne ln_mult ln_powr ln_div field_simps)
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2431
  qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2432
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2433
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2434
lemma ub_ln_lb_ln_bounds':
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2435
  assumes "1 \<le> x"
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2436
  shows "the (lb_ln prec x) \<le> ln x \<and> ln x \<le> the (ub_ln prec x)"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2437
    (is "?lb \<le> ?ln \<and> ?ln \<le> ?ub")
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2438
proof (cases "x < Float 1 1")
31468
b8267feaf342 Approximation: Corrected precision of ln on all real values
hoelzl
parents: 31467
diff changeset
  2439
  case True
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2440
  hence "real_of_float (x - 1) < 1" and "real_of_float x < 2" by auto
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
  2441
  have "\<not> x \<le> 0" and "\<not> x < 1" using \<open>1 \<le> x\<close> by auto
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2442
  hence "0 \<le> real_of_float (x - 1)" using \<open>1 \<le> x\<close> by auto
31468
b8267feaf342 Approximation: Corrected precision of ln on all real values
hoelzl
parents: 31467
diff changeset
  2443
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
  2444
  have [simp]: "(Float 3 (- 1)) = 3 / 2" by simp
31468
b8267feaf342 Approximation: Corrected precision of ln on all real values
hoelzl
parents: 31467
diff changeset
  2445
b8267feaf342 Approximation: Corrected precision of ln on all real values
hoelzl
parents: 31467
diff changeset
  2446
  show ?thesis
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
  2447
  proof (cases "x \<le> Float 3 (- 1)")
31468
b8267feaf342 Approximation: Corrected precision of ln on all real values
hoelzl
parents: 31467
diff changeset
  2448
    case True
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2449
    show ?thesis
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2450
      unfolding lb_ln.simps
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2451
      unfolding ub_ln.simps Let_def
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2452
      using ln_float_bounds[OF \<open>0 \<le> real_of_float (x - 1)\<close> \<open>real_of_float (x - 1) < 1\<close>, of prec]
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2453
        \<open>\<not> x \<le> 0\<close> \<open>\<not> x < 1\<close> True
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2454
      by (auto intro!: float_round_down_le float_round_up_le)
31468
b8267feaf342 Approximation: Corrected precision of ln on all real values
hoelzl
parents: 31467
diff changeset
  2455
  next
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2456
    case False
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2457
    hence *: "3 / 2 < x" by auto
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2458
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2459
    with ln_add[of "3 / 2" "x - 3 / 2"]
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2460
    have add: "ln x = ln (3 / 2) + ln (real_of_float x * 2 / 3)"
31468
b8267feaf342 Approximation: Corrected precision of ln on all real values
hoelzl
parents: 31467
diff changeset
  2461
      by (auto simp add: algebra_simps diff_divide_distrib)
b8267feaf342 Approximation: Corrected precision of ln on all real values
hoelzl
parents: 31467
diff changeset
  2462
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2463
    let "?ub_horner x" = "float_round_up prec (x * ub_ln_horner prec (get_odd prec) 1 x)"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2464
    let "?lb_horner x" = "float_round_down prec (x * lb_ln_horner prec (get_even prec) 1 x)"
31468
b8267feaf342 Approximation: Corrected precision of ln on all real values
hoelzl
parents: 31467
diff changeset
  2465
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2466
    { have up: "real_of_float (rapprox_rat prec 2 3) \<le> 1"
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  2467
        by (rule rapprox_rat_le1) simp_all
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2468
      have low: "2 / 3 \<le> rapprox_rat prec 2 3"
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  2469
        by (rule order_trans[OF _ rapprox_rat]) simp
31468
b8267feaf342 Approximation: Corrected precision of ln on all real values
hoelzl
parents: 31467
diff changeset
  2470
      from mult_less_le_imp_less[OF * low] *
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2471
      have pos: "0 < real_of_float (x * rapprox_rat prec 2 3 - 1)" by auto
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2472
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2473
      have "ln (real_of_float x * 2/3)
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2474
        \<le> ln (real_of_float (x * rapprox_rat prec 2 3 - 1) + 1)"
31468
b8267feaf342 Approximation: Corrected precision of ln on all real values
hoelzl
parents: 31467
diff changeset
  2475
      proof (rule ln_le_cancel_iff[symmetric, THEN iffD1])
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2476
        show "real_of_float x * 2 / 3 \<le> real_of_float (x * rapprox_rat prec 2 3 - 1) + 1"
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  2477
          using * low by auto
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2478
        show "0 < real_of_float x * 2 / 3" using * by simp
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2479
        show "0 < real_of_float (x * rapprox_rat prec 2 3 - 1) + 1" using pos by auto
31468
b8267feaf342 Approximation: Corrected precision of ln on all real values
hoelzl
parents: 31467
diff changeset
  2480
      qed
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2481
      also have "\<dots> \<le> ?ub_horner (x * rapprox_rat prec 2 3 - 1)"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2482
      proof (rule float_round_up_le, rule ln_float_bounds(2))
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2483
        from mult_less_le_imp_less[OF \<open>real_of_float x < 2\<close> up] low *
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2484
        show "real_of_float (x * rapprox_rat prec 2 3 - 1) < 1" by auto
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2485
        show "0 \<le> real_of_float (x * rapprox_rat prec 2 3 - 1)" using pos by auto
31468
b8267feaf342 Approximation: Corrected precision of ln on all real values
hoelzl
parents: 31467
diff changeset
  2486
      qed
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2487
     finally have "ln x \<le> ?ub_horner (Float 1 (-1))
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2488
          + ?ub_horner ((x * rapprox_rat prec 2 3 - 1))"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2489
        using ln_float_bounds(2)[of "Float 1 (- 1)" prec prec] add
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2490
        by (auto intro!: add_mono float_round_up_le)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2491
      note float_round_up_le[OF this, of prec]
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2492
    }
31468
b8267feaf342 Approximation: Corrected precision of ln on all real values
hoelzl
parents: 31467
diff changeset
  2493
    moreover
b8267feaf342 Approximation: Corrected precision of ln on all real values
hoelzl
parents: 31467
diff changeset
  2494
    { let ?max = "max (x * lapprox_rat prec 2 3 - 1) 0"
b8267feaf342 Approximation: Corrected precision of ln on all real values
hoelzl
parents: 31467
diff changeset
  2495
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2496
      have up: "lapprox_rat prec 2 3 \<le> 2/3"
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  2497
        by (rule order_trans[OF lapprox_rat], simp)
31468
b8267feaf342 Approximation: Corrected precision of ln on all real values
hoelzl
parents: 31467
diff changeset
  2498
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2499
      have low: "0 \<le> real_of_float (lapprox_rat prec 2 3)"
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2500
        using lapprox_rat_nonneg[of 2 3 prec] by simp
31468
b8267feaf342 Approximation: Corrected precision of ln on all real values
hoelzl
parents: 31467
diff changeset
  2501
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2502
      have "?lb_horner ?max
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2503
        \<le> ln (real_of_float ?max + 1)"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2504
      proof (rule float_round_down_le, rule ln_float_bounds(1))
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2505
        from mult_less_le_imp_less[OF \<open>real_of_float x < 2\<close> up] * low
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2506
        show "real_of_float ?max < 1" by (cases "real_of_float (lapprox_rat prec 2 3) = 0",
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  2507
          auto simp add: real_of_float_max)
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2508
        show "0 \<le> real_of_float ?max" by (auto simp add: real_of_float_max)
31468
b8267feaf342 Approximation: Corrected precision of ln on all real values
hoelzl
parents: 31467
diff changeset
  2509
      qed
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2510
      also have "\<dots> \<le> ln (real_of_float x * 2/3)"
31468
b8267feaf342 Approximation: Corrected precision of ln on all real values
hoelzl
parents: 31467
diff changeset
  2511
      proof (rule ln_le_cancel_iff[symmetric, THEN iffD1])
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2512
        show "0 < real_of_float ?max + 1" by (auto simp add: real_of_float_max)
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2513
        show "0 < real_of_float x * 2/3" using * by auto
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2514
        show "real_of_float ?max + 1 \<le> real_of_float x * 2/3" using * up
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2515
          by (cases "0 < real_of_float x * real_of_float (lapprox_posrat prec 2 3) - 1",
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2516
              auto simp add: max_def)
31468
b8267feaf342 Approximation: Corrected precision of ln on all real values
hoelzl
parents: 31467
diff changeset
  2517
      qed
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2518
      finally have "?lb_horner (Float 1 (- 1)) + ?lb_horner ?max \<le> ln x"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2519
        using ln_float_bounds(1)[of "Float 1 (- 1)" prec prec] add
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2520
        by (auto intro!: add_mono float_round_down_le)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2521
      note float_round_down_le[OF this, of prec]
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2522
    }
31468
b8267feaf342 Approximation: Corrected precision of ln on all real values
hoelzl
parents: 31467
diff changeset
  2523
    ultimately
b8267feaf342 Approximation: Corrected precision of ln on all real values
hoelzl
parents: 31467
diff changeset
  2524
    show ?thesis unfolding lb_ln.simps unfolding ub_ln.simps Let_def
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
  2525
      using \<open>\<not> x \<le> 0\<close> \<open>\<not> x < 1\<close> True False by auto
31468
b8267feaf342 Approximation: Corrected precision of ln on all real values
hoelzl
parents: 31467
diff changeset
  2526
  qed
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2527
next
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2528
  case False
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
  2529
  hence "\<not> x \<le> 0" and "\<not> x < 1" "0 < x" "\<not> x \<le> Float 3 (- 1)"
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
  2530
    using \<open>1 \<le> x\<close> by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2531
  show ?thesis
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2532
  proof -
63040
eb4ddd18d635 eliminated old 'def';
wenzelm
parents: 62390
diff changeset
  2533
    define m where "m = mantissa x"
eb4ddd18d635 eliminated old 'def';
wenzelm
parents: 62390
diff changeset
  2534
    define e where "e = exponent x"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2535
    from Float_mantissa_exponent[of x] have Float: "x = Float m e"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2536
      by (simp add: m_def e_def)
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2537
    let ?s = "Float (e + (bitlen m - 1)) 0"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2538
    let ?x = "Float m (- (bitlen m - 1))"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2539
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2540
    have "0 < m" and "m \<noteq> 0" using \<open>0 < x\<close> Float powr_gt_zero[of 2 e]
60017
b785d6d06430 Overloading of ln and powr, but "approximation" no longer works for powr. Code generation also fails due to type ambiguity in scala.
paulson <lp15@cam.ac.uk>
parents: 59850
diff changeset
  2541
      apply (auto simp add: zero_less_mult_iff)
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2542
      using not_le powr_ge_pzero apply blast
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2543
      done
63040
eb4ddd18d635 eliminated old 'def';
wenzelm
parents: 62390
diff changeset
  2544
    define bl where "bl = bitlen m - 1"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2545
    hence "bl \<ge> 0"
63248
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63170
diff changeset
  2546
      using \<open>m > 0\<close> by (simp add: bitlen_alt_def)
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2547
    have "1 \<le> Float m e"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2548
      using \<open>1 \<le> x\<close> Float unfolding less_eq_float_def by auto
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
  2549
    from bitlen_div[OF \<open>0 < m\<close>] float_gt1_scale[OF \<open>1 \<le> Float m e\<close>] \<open>bl \<ge> 0\<close>
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2550
    have x_bnds: "0 \<le> real_of_float (?x - 1)" "real_of_float (?x - 1) < 1"
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2551
      unfolding bl_def[symmetric]
63263
c6c95d64607a approximation, derivative, and continuity of floor and ceiling
immler
parents: 63248
diff changeset
  2552
      by (auto simp: powr_realpow[symmetric] field_simps)
c6c95d64607a approximation, derivative, and continuity of floor and ceiling
immler
parents: 63248
diff changeset
  2553
         (auto simp : powr_minus field_simps)
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2554
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2555
    {
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2556
      have "float_round_down prec (lb_ln2 prec * ?s) \<le> ln 2 * (e + (bitlen m - 1))"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2557
          (is "real_of_float ?lb2 \<le> _")
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2558
        apply (rule float_round_down_le)
47601
050718fe6eee use real :: float => real as lifting-morphism so we can directlry use the rep_eq theorems
hoelzl
parents: 47600
diff changeset
  2559
        unfolding nat_0 power_0 mult_1_right times_float.rep_eq
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  2560
        using lb_ln2[of prec]
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2561
      proof (rule mult_mono)
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
  2562
        from float_gt1_scale[OF \<open>1 \<le> Float m e\<close>]
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2563
        show "0 \<le> real_of_float (Float (e + (bitlen m - 1)) 0)" by simp
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2564
      qed auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2565
      moreover
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2566
      from ln_float_bounds(1)[OF x_bnds]
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2567
      have "float_round_down prec ((?x - 1) * lb_ln_horner prec (get_even prec) 1 (?x - 1)) \<le> ln ?x" (is "real_of_float ?lb_horner \<le> _")
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2568
        by (auto intro!: float_round_down_le)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2569
      ultimately have "float_plus_down prec ?lb2 ?lb_horner \<le> ln x"
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
  2570
        unfolding Float ln_shifted_float[OF \<open>0 < m\<close>, of e] by (auto intro!: float_plus_down_le)
31468
b8267feaf342 Approximation: Corrected precision of ln on all real values
hoelzl
parents: 31467
diff changeset
  2571
    }
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2572
    moreover
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2573
    {
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2574
      from ln_float_bounds(2)[OF x_bnds]
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2575
      have "ln ?x \<le> float_round_up prec ((?x - 1) * ub_ln_horner prec (get_odd prec) 1 (?x - 1))"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2576
          (is "_ \<le> real_of_float ?ub_horner")
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2577
        by (auto intro!: float_round_up_le)
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2578
      moreover
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2579
      have "ln 2 * (e + (bitlen m - 1)) \<le> float_round_up prec (ub_ln2 prec * ?s)"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2580
          (is "_ \<le> real_of_float ?ub2")
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2581
        apply (rule float_round_up_le)
47601
050718fe6eee use real :: float => real as lifting-morphism so we can directlry use the rep_eq theorems
hoelzl
parents: 47600
diff changeset
  2582
        unfolding nat_0 power_0 mult_1_right times_float.rep_eq
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  2583
        using ub_ln2[of prec]
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2584
      proof (rule mult_mono)
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
  2585
        from float_gt1_scale[OF \<open>1 \<le> Float m e\<close>]
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2586
        show "0 \<le> real_of_int (e + (bitlen m - 1))" by auto
60017
b785d6d06430 Overloading of ln and powr, but "approximation" no longer works for powr. Code generation also fails due to type ambiguity in scala.
paulson <lp15@cam.ac.uk>
parents: 59850
diff changeset
  2587
        have "0 \<le> ln (2 :: real)" by simp
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2588
        thus "0 \<le> real_of_float (ub_ln2 prec)" using ub_ln2[of prec] by arith
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2589
      qed auto
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2590
      ultimately have "ln x \<le> float_plus_up prec ?ub2 ?ub_horner"
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
  2591
        unfolding Float ln_shifted_float[OF \<open>0 < m\<close>, of e]
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2592
        by (auto intro!: float_plus_up_le)
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2593
    }
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2594
    ultimately show ?thesis
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2595
      unfolding lb_ln.simps
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2596
      unfolding ub_ln.simps
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2597
      unfolding if_not_P[OF \<open>\<not> x \<le> 0\<close>] if_not_P[OF \<open>\<not> x < 1\<close>]
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2598
        if_not_P[OF False] if_not_P[OF \<open>\<not> x \<le> Float 3 (- 1)\<close>] Let_def
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2599
      unfolding plus_float.rep_eq e_def[symmetric] m_def[symmetric]
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2600
      by simp
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2601
  qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2602
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2603
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  2604
lemma ub_ln_lb_ln_bounds:
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  2605
  assumes "0 < x"
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2606
  shows "the (lb_ln prec x) \<le> ln x \<and> ln x \<le> the (ub_ln prec x)"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2607
    (is "?lb \<le> ?ln \<and> ?ln \<le> ?ub")
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2608
proof (cases "x < 1")
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2609
  case False
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2610
  hence "1 \<le> x"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2611
    unfolding less_float_def less_eq_float_def by auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2612
  show ?thesis
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2613
    using ub_ln_lb_ln_bounds'[OF \<open>1 \<le> x\<close>] .
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2614
next
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2615
  case True
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2616
  have "\<not> x \<le> 0" using \<open>0 < x\<close> by auto
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2617
  from True have "real_of_float x \<le> 1" "x \<le> 1"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2618
    by simp_all
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2619
  have "0 < real_of_float x" and "real_of_float x \<noteq> 0"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2620
    using \<open>0 < x\<close> by auto
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2621
  hence A: "0 < 1 / real_of_float x" by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2622
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2623
  {
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2624
    let ?divl = "float_divl (max prec 1) 1 x"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2625
    have A': "1 \<le> ?divl" using float_divl_pos_less1_bound[OF \<open>0 < real_of_float x\<close> \<open>real_of_float x \<le> 1\<close>] by auto
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2626
    hence B: "0 < real_of_float ?divl" by auto
31468
b8267feaf342 Approximation: Corrected precision of ln on all real values
hoelzl
parents: 31467
diff changeset
  2627
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2628
    have "ln ?divl \<le> ln (1 / x)" unfolding ln_le_cancel_iff[OF B A] using float_divl[of _ 1 x] by auto
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2629
    hence "ln x \<le> - ln ?divl" unfolding nonzero_inverse_eq_divide[OF \<open>real_of_float x \<noteq> 0\<close>, symmetric] ln_inverse[OF \<open>0 < real_of_float x\<close>] by auto
31468
b8267feaf342 Approximation: Corrected precision of ln on all real values
hoelzl
parents: 31467
diff changeset
  2630
    from this ub_ln_lb_ln_bounds'[OF A', THEN conjunct1, THEN le_imp_neg_le]
47601
050718fe6eee use real :: float => real as lifting-morphism so we can directlry use the rep_eq theorems
hoelzl
parents: 47600
diff changeset
  2631
    have "?ln \<le> - the (lb_ln prec ?divl)" unfolding uminus_float.rep_eq by (rule order_trans)
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2632
  } moreover
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2633
  {
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2634
    let ?divr = "float_divr prec 1 x"
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
  2635
    have A': "1 \<le> ?divr" using float_divr_pos_less1_lower_bound[OF \<open>0 < x\<close> \<open>x \<le> 1\<close>] unfolding less_eq_float_def less_float_def by auto
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2636
    hence B: "0 < real_of_float ?divr" by auto
31468
b8267feaf342 Approximation: Corrected precision of ln on all real values
hoelzl
parents: 31467
diff changeset
  2637
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2638
    have "ln (1 / x) \<le> ln ?divr" unfolding ln_le_cancel_iff[OF A B] using float_divr[of 1 x] by auto
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2639
    hence "- ln ?divr \<le> ln x" unfolding nonzero_inverse_eq_divide[OF \<open>real_of_float x \<noteq> 0\<close>, symmetric] ln_inverse[OF \<open>0 < real_of_float x\<close>] by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2640
    from ub_ln_lb_ln_bounds'[OF A', THEN conjunct2, THEN le_imp_neg_le] this
47601
050718fe6eee use real :: float => real as lifting-morphism so we can directlry use the rep_eq theorems
hoelzl
parents: 47600
diff changeset
  2641
    have "- the (ub_ln prec ?divr) \<le> ?ln" unfolding uminus_float.rep_eq by (rule order_trans)
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2642
  }
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2643
  ultimately show ?thesis unfolding lb_ln.simps[where x=x]  ub_ln.simps[where x=x]
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
  2644
    unfolding if_not_P[OF \<open>\<not> x \<le> 0\<close>] if_P[OF True] by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2645
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2646
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  2647
lemma lb_ln:
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  2648
  assumes "Some y = lb_ln prec x"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2649
  shows "y \<le> ln x" and "0 < real_of_float x"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2650
proof -
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2651
  have "0 < x"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2652
  proof (rule ccontr)
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2653
    assume "\<not> 0 < x"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2654
    hence "x \<le> 0"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2655
      unfolding less_eq_float_def less_float_def by auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2656
    thus False
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2657
      using assms by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2658
  qed
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2659
  thus "0 < real_of_float x" by auto
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2660
  have "the (lb_ln prec x) \<le> ln x"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2661
    using ub_ln_lb_ln_bounds[OF \<open>0 < x\<close>] ..
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2662
  thus "y \<le> ln x"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2663
    unfolding assms[symmetric] by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2664
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2665
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  2666
lemma ub_ln:
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  2667
  assumes "Some y = ub_ln prec x"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2668
  shows "ln x \<le> y" and "0 < real_of_float x"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2669
proof -
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2670
  have "0 < x"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2671
  proof (rule ccontr)
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2672
    assume "\<not> 0 < x"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2673
    hence "x \<le> 0" by auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2674
    thus False
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2675
      using assms by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2676
  qed
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2677
  thus "0 < real_of_float x" by auto
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2678
  have "ln x \<le> the (ub_ln prec x)"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2679
    using ub_ln_lb_ln_bounds[OF \<open>0 < x\<close>] ..
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2680
  thus "ln x \<le> y"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2681
    unfolding assms[symmetric] by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2682
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2683
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2684
lemma bnds_ln: "\<forall>(x::real) lx ux. (Some l, Some u) =
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2685
  (lb_ln prec lx, ub_ln prec ux) \<and> x \<in> {lx .. ux} \<longrightarrow> l \<le> ln x \<and> ln x \<le> u"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2686
proof (rule allI, rule allI, rule allI, rule impI)
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2687
  fix x :: real
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2688
  fix lx ux
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2689
  assume "(Some l, Some u) = (lb_ln prec lx, ub_ln prec ux) \<and> x \<in> {lx .. ux}"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2690
  hence l: "Some l = lb_ln prec lx " and u: "Some u = ub_ln prec ux" and x: "x \<in> {lx .. ux}"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2691
    by auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2692
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2693
  have "ln ux \<le> u" and "0 < real_of_float ux"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2694
    using ub_ln u by auto
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2695
  have "l \<le> ln lx" and "0 < real_of_float lx" and "0 < x"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2696
    using lb_ln[OF l] x by auto
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2697
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2698
  from ln_le_cancel_iff[OF \<open>0 < real_of_float lx\<close> \<open>0 < x\<close>] \<open>l \<le> ln lx\<close>
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2699
  have "l \<le> ln x"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2700
    using x unfolding atLeastAtMost_iff by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2701
  moreover
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2702
  from ln_le_cancel_iff[OF \<open>0 < x\<close> \<open>0 < real_of_float ux\<close>] \<open>ln ux \<le> real_of_float u\<close>
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2703
  have "ln x \<le> u"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2704
    using x unfolding atLeastAtMost_iff by auto
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2705
  ultimately show "l \<le> ln x \<and> ln x \<le> u" ..
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2706
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2707
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2708
62200
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2709
section \<open>Real power function\<close>
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2710
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2711
definition bnds_powr :: "nat \<Rightarrow> float \<Rightarrow> float \<Rightarrow> float \<Rightarrow> float \<Rightarrow> (float \<times> float) option" where
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2712
  "bnds_powr prec l1 u1 l2 u2 = (
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2713
     if l1 = 0 \<and> u1 = 0 then
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2714
       Some (0, 0)
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2715
     else if l1 = 0 \<and> l2 \<ge> 1 then
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2716
       let uln = the (ub_ln prec u1)
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2717
       in  Some (0, ub_exp prec (float_round_up prec (uln * (if uln \<ge> 0 then u2 else l2))))
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2718
     else if l1 \<le> 0 then
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2719
       None
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2720
     else
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2721
       Some (map_bnds lb_exp ub_exp prec 
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2722
               (bnds_mult prec (the (lb_ln prec l1)) (the (ub_ln prec u1)) l2 u2)))"
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2723
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2724
lemmas [simp del] = lb_exp.simps ub_exp.simps
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2725
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2726
lemma mono_exp_real: "mono (exp :: real \<Rightarrow> real)"
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2727
  by (auto simp: mono_def)
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2728
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2729
lemma ub_exp_nonneg: "real_of_float (ub_exp prec x) \<ge> 0"
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2730
proof -
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2731
  have "0 \<le> exp (real_of_float x)" by simp
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2732
  also from exp_boundaries[of x prec] 
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2733
    have "\<dots> \<le> real_of_float (ub_exp prec x)" by simp
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2734
  finally show ?thesis .
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2735
qed
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2736
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2737
lemma bnds_powr:
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2738
  assumes lu: "Some (l, u) = bnds_powr prec l1 u1 l2 u2"
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2739
  assumes x: "x \<in> {real_of_float l1..real_of_float u1}"
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2740
  assumes y: "y \<in> {real_of_float l2..real_of_float u2}"
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2741
  shows   "x powr y \<in> {real_of_float l..real_of_float u}"
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2742
proof -
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2743
  consider "l1 = 0" "u1 = 0" | "l1 = 0" "u1 \<noteq> 0" "l2 \<ge> 1" | 
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2744
           "l1 \<le> 0" "\<not>(l1 = 0 \<and> (u1 = 0 \<or> l2 \<ge> 1))" | "l1 > 0" by force
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2745
  thus ?thesis
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2746
  proof cases
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2747
    assume "l1 = 0" "u1 = 0"
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2748
    with x lu show ?thesis by (auto simp: bnds_powr_def)
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2749
  next
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2750
    assume A: "l1 = 0" "u1 \<noteq> 0" "l2 \<ge> 1"
63040
eb4ddd18d635 eliminated old 'def';
wenzelm
parents: 62390
diff changeset
  2751
    define uln where "uln = the (ub_ln prec u1)"
62200
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2752
    show ?thesis
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2753
    proof (cases "x = 0")
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2754
      case False
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2755
      with A x y have "x powr y = exp (ln x * y)" by (simp add: powr_def)
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2756
      also {
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2757
        from A x False have "ln x \<le> ln (real_of_float u1)" by simp
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2758
        also from ub_ln_lb_ln_bounds[of u1 prec] A y x False
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2759
          have "ln (real_of_float u1) \<le> real_of_float uln" by (simp add: uln_def del: lb_ln.simps)
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2760
        also from A x y have "\<dots> * y \<le> real_of_float uln * (if uln \<ge> 0 then u2 else l2)"
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2761
          by (auto intro: mult_left_mono mult_left_mono_neg)
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2762
        also have "\<dots> \<le> real_of_float (float_round_up prec (uln * (if uln \<ge> 0 then u2 else l2)))"
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2763
          by (simp add: float_round_up_le)
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2764
        finally have "ln x * y \<le> \<dots>" using A y by - simp
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2765
      }
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2766
      also have "exp (real_of_float (float_round_up prec (uln * (if uln \<ge> 0 then u2 else l2)))) \<le>
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2767
                   real_of_float (ub_exp prec (float_round_up prec
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2768
                       (uln * (if uln \<ge> 0 then u2 else l2))))"
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2769
        using exp_boundaries by simp
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2770
      finally show ?thesis using A x y lu 
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2771
        by (simp add: bnds_powr_def uln_def Let_def del: lb_ln.simps ub_ln.simps)
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2772
    qed (insert x y lu A, simp_all add: bnds_powr_def Let_def ub_exp_nonneg
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2773
                                   del: lb_ln.simps ub_ln.simps)
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2774
  next
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2775
    assume "l1 \<le> 0" "\<not>(l1 = 0 \<and> (u1 = 0 \<or> l2 \<ge> 1))"
62390
842917225d56 more canonical names
nipkow
parents: 62200
diff changeset
  2776
    with lu show ?thesis by (simp add: bnds_powr_def split: if_split_asm)
62200
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2777
  next
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2778
    assume l1: "l1 > 0"
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2779
    obtain lm um where lmum:
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2780
      "(lm, um) = bnds_mult prec (the (lb_ln prec l1)) (the (ub_ln prec u1)) l2 u2"
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2781
      by (cases "bnds_mult prec (the (lb_ln prec l1)) (the (ub_ln prec u1)) l2 u2") simp
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2782
    with l1 have "(l, u) = map_bnds lb_exp ub_exp prec (lm, um)"
62390
842917225d56 more canonical names
nipkow
parents: 62200
diff changeset
  2783
      using lu by (simp add: bnds_powr_def del: lb_ln.simps ub_ln.simps split: if_split_asm)
62200
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2784
    hence "exp (ln x * y) \<in> {real_of_float l..real_of_float u}"
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2785
    proof (rule map_bnds[OF _ mono_exp_real], goal_cases)
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2786
      case 1
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2787
      let ?lln = "the (lb_ln prec l1)" and ?uln = "the (ub_ln prec u1)"
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2788
      from ub_ln_lb_ln_bounds[of l1 prec] ub_ln_lb_ln_bounds[of u1 prec] x l1
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2789
        have "real_of_float ?lln \<le> ln (real_of_float l1) \<and> 
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2790
              ln (real_of_float u1) \<le> real_of_float ?uln"
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2791
        by (auto simp del: lb_ln.simps ub_ln.simps)
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2792
      moreover from l1 x have "ln (real_of_float l1) \<le> ln x \<and> ln x \<le> ln (real_of_float u1)"
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2793
        by auto
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2794
      ultimately have ln: "real_of_float ?lln \<le> ln x \<and> ln x \<le> real_of_float ?uln" by simp
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2795
      from lmum show ?case
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2796
        by (rule bnds_mult) (insert y ln, simp_all)
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2797
    qed (insert exp_boundaries[of lm prec] exp_boundaries[of um prec], simp_all)
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2798
    with x l1 show ?thesis
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2799
      by (simp add: powr_def mult_ac)
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2800
  qed
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2801
qed
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2802
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2803
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2804
section "Implement floatarith"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2806
subsection "Define syntax and semantics"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2807
58310
91ea607a34d8 updated news
blanchet
parents: 58249
diff changeset
  2808
datatype floatarith
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2809
  = Add floatarith floatarith
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2810
  | Minus floatarith
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2811
  | Mult floatarith floatarith
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2812
  | Inverse floatarith
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2813
  | Cos floatarith
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2814
  | Arctan floatarith
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2815
  | Abs floatarith
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2816
  | Max floatarith floatarith
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2817
  | Min floatarith floatarith
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2818
  | Pi
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2819
  | Sqrt floatarith
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2820
  | Exp floatarith
62200
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2821
  | Powr floatarith floatarith
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2822
  | Ln floatarith
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2823
  | Power floatarith nat
63263
c6c95d64607a approximation, derivative, and continuity of floor and ceiling
immler
parents: 63248
diff changeset
  2824
  | Floor floatarith
32919
37adfa07b54b approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
hoelzl
parents: 32650
diff changeset
  2825
  | Var nat
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2826
  | Num float
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2827
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  2828
fun interpret_floatarith :: "floatarith \<Rightarrow> real list \<Rightarrow> real" 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
  2829
"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
  2830
"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
  2831
"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
  2832
"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
  2833
"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
  2834
"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
  2835
"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
  2836
"interpret_floatarith (Max a b) vs    = max (interpret_floatarith a vs) (interpret_floatarith b vs)" |
61945
1135b8de26c3 more symbols;
wenzelm
parents: 61824
diff changeset
  2837
"interpret_floatarith (Abs a) vs      = \<bar>interpret_floatarith a vs\<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
  2838
"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
  2839
"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
  2840
"interpret_floatarith (Exp a) vs      = exp (interpret_floatarith a vs)" |
62200
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2841
"interpret_floatarith (Powr a b) vs   = interpret_floatarith a vs powr interpret_floatarith b 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
  2842
"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
  2843
"interpret_floatarith (Power a n) vs  = (interpret_floatarith a vs)^n" |
63263
c6c95d64607a approximation, derivative, and continuity of floor and ceiling
immler
parents: 63248
diff changeset
  2844
"interpret_floatarith (Floor a) vs      = floor (interpret_floatarith a vs)" |
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2845
"interpret_floatarith (Num f) vs      = f" |
32919
37adfa07b54b approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
hoelzl
parents: 32650
diff changeset
  2846
"interpret_floatarith (Var n) vs     = vs ! n"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2847
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2848
lemma interpret_floatarith_divide:
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2849
  "interpret_floatarith (Mult a (Inverse b)) vs =
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2850
    (interpret_floatarith a vs) / (interpret_floatarith b vs)"
36778
739a9379e29b avoid using real-specific versions of generic lemmas
huffman
parents: 36531
diff changeset
  2851
  unfolding divide_inverse interpret_floatarith.simps ..
31811
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2852
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2853
lemma interpret_floatarith_diff:
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2854
  "interpret_floatarith (Add a (Minus b)) vs =
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2855
    (interpret_floatarith a vs) - (interpret_floatarith b vs)"
54230
b1d955791529 more simplification rules on unary and binary minus
haftmann
parents: 53077
diff changeset
  2856
  unfolding interpret_floatarith.simps by simp
31811
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2857
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2858
lemma interpret_floatarith_sin:
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2859
  "interpret_floatarith (Cos (Add (Mult Pi (Num (Float 1 (- 1)))) (Minus a))) vs =
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2860
    sin (interpret_floatarith a vs)"
31811
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2861
  unfolding sin_cos_eq interpret_floatarith.simps
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2862
    interpret_floatarith_divide interpret_floatarith_diff
31811
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2863
  by auto
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2864
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2865
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2866
subsection "Implement approximation function"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2867
62200
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2868
fun lift_bin :: "(float * float) option \<Rightarrow> (float * float) option \<Rightarrow> (float \<Rightarrow> float \<Rightarrow> float \<Rightarrow> float \<Rightarrow> (float * float) option) \<Rightarrow> (float * float) option" where
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2869
"lift_bin (Some (l1, u1)) (Some (l2, u2)) f = f l1 u1 l2 u2" |
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2870
"lift_bin a b f = None"
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2871
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2872
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
  2873
"lift_bin' (Some (l1, u1)) (Some (l2, u2)) f = Some (f l1 u1 l2 u2)" |
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2874
"lift_bin' a b f = None"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2875
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2876
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
  2877
"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
  2878
                                             | t \<Rightarrow> None)" |
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2879
"lift_un b f = None"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2880
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2881
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
  2882
"lift_un' (Some (l1, u1)) f = Some (f l1 u1)" |
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2883
"lift_un' b f = None"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2884
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2885
definition bounded_by :: "real list \<Rightarrow> (float \<times> float) option list \<Rightarrow> bool" where 
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2886
  "bounded_by xs vs \<longleftrightarrow>
31811
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2887
  (\<forall> i < length vs. case vs ! i of None \<Rightarrow> True
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2888
         | Some (l, u) \<Rightarrow> xs ! i \<in> { real_of_float l .. real_of_float u })"
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2889
                                                                     
31811
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2890
lemma bounded_byE:
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2891
  assumes "bounded_by xs vs"
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2892
  shows "\<And> i. i < length vs \<Longrightarrow> case vs ! i of None \<Rightarrow> True
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2893
         | Some (l, u) \<Rightarrow> xs ! i \<in> { real_of_float l .. real_of_float u }"
31811
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2894
  using assms bounded_by_def by blast
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2895
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2896
lemma bounded_by_update:
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2897
  assumes "bounded_by xs vs"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2898
    and bnd: "xs ! i \<in> { real_of_float l .. real_of_float u }"
31811
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2899
  shows "bounded_by xs (vs[i := Some (l,u)])"
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2900
proof -
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2901
  {
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2902
    fix j
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2903
    let ?vs = "vs[i := Some (l,u)]"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2904
    assume "j < length ?vs"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2905
    hence [simp]: "j < length vs" by simp
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  2906
    have "case ?vs ! j of None \<Rightarrow> True | Some (l, u) \<Rightarrow> xs ! j \<in> { real_of_float l .. real_of_float u }"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2907
    proof (cases "?vs ! j")
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2908
      case (Some b)
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2909
      thus ?thesis
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2910
      proof (cases "i = j")
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2911
        case True
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2912
        thus ?thesis using \<open>?vs ! j = Some b\<close> and bnd by auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2913
      next
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2914
        case False
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2915
        thus ?thesis using \<open>bounded_by xs vs\<close> unfolding bounded_by_def by auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2916
      qed
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2917
    qed auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2918
  }
31811
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2919
  thus ?thesis unfolding bounded_by_def by auto
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2920
qed
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2921
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  2922
lemma bounded_by_None: "bounded_by xs (replicate (length xs) None)"
31811
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2923
  unfolding bounded_by_def by auto
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2924
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2925
fun approx approx' :: "nat \<Rightarrow> floatarith \<Rightarrow> (float * float) option list \<Rightarrow> (float * float) option" where
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2926
"approx' prec a bs          = (case (approx prec a bs) of Some (l, u) \<Rightarrow> Some (float_round_down prec l, float_round_up prec u) | None \<Rightarrow> None)" |
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2927
"approx prec (Add a b) bs   =
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2928
  lift_bin' (approx' prec a bs) (approx' prec b bs)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2929
    (\<lambda> l1 u1 l2 u2. (float_plus_down prec l1 l2, float_plus_up prec u1 u2))" |
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2930
"approx prec (Minus a) bs   = lift_un' (approx' prec a bs) (\<lambda> l u. (-u, -l))" |
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2931
"approx prec (Mult a b) bs  =
62200
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2932
  lift_bin' (approx' prec a bs) (approx' prec b bs) (bnds_mult prec)" |
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2933
"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
  2934
"approx prec (Cos a) bs     = lift_un' (approx' prec a bs) (bnds_cos prec)" |
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2935
"approx prec Pi bs          = Some (lb_pi prec, ub_pi prec)" |
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2936
"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
  2937
"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
  2938
"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
  2939
"approx prec (Arctan a) bs  = lift_un' (approx' prec a bs) (\<lambda> l u. (lb_arctan prec l, ub_arctan prec u))" |
31467
f7d2aa438bee Approximation: Implemented argument reduction for cosine. Sinus is now implemented in terms of cosine. Sqrt computes on the entire real numbers
hoelzl
parents: 31148
diff changeset
  2940
"approx prec (Sqrt a) bs    = lift_un' (approx' prec a bs) (\<lambda> l u. (lb_sqrt prec l, ub_sqrt prec u))" |
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2941
"approx prec (Exp a) bs     = lift_un' (approx' prec a bs) (\<lambda> l u. (lb_exp prec l, ub_exp prec u))" |
62200
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2942
"approx prec (Powr a b) bs  = lift_bin (approx' prec a bs) (approx' prec b bs) (bnds_powr prec)" |
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2943
"approx prec (Ln a) bs      = lift_un (approx' prec a bs) (\<lambda> l u. (lb_ln prec l, ub_ln prec u))" |
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2944
"approx prec (Power a n) bs = lift_un' (approx' prec a bs) (float_power_bnds prec n)" |
63263
c6c95d64607a approximation, derivative, and continuity of floor and ceiling
immler
parents: 63248
diff changeset
  2945
"approx prec (Floor a) bs = lift_un' (approx' prec a bs) (\<lambda> l u. (floor_fl l, floor_fl u))" |
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2946
"approx prec (Num f) bs     = Some (f, f)" |
32919
37adfa07b54b approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
hoelzl
parents: 32650
diff changeset
  2947
"approx prec (Var i) bs    = (if i < length bs then bs ! i else None)"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2948
62200
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2949
lemma approx_approx':
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2950
  assumes Pa: "\<And>l u. Some (l, u) = approx prec a vs \<Longrightarrow>
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2951
      l \<le> interpret_floatarith a xs \<and> interpret_floatarith a xs \<le> u"
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2952
    and approx': "Some (l, u) = approx' prec a vs"
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2953
  shows "l \<le> interpret_floatarith a xs \<and> interpret_floatarith a xs \<le> u"
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2954
proof -
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2955
  obtain l' u' where S: "Some (l', u') = approx prec a vs"
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2956
    using approx' unfolding approx'.simps by (cases "approx prec a vs") auto
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2957
  have l': "l = float_round_down prec l'" and u': "u = float_round_up prec u'"
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2958
    using approx' unfolding approx'.simps S[symmetric] by auto
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2959
  show ?thesis unfolding l' u'
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2960
    using order_trans[OF Pa[OF S, THEN conjunct2] float_round_up[of u']]
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2961
    using order_trans[OF float_round_down[of _ l'] Pa[OF S, THEN conjunct1]] by auto
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2962
qed
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2963
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2964
lemma lift_bin_ex:
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2965
  assumes lift_bin_Some: "Some (l, u) = lift_bin a b f"
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2966
  shows "\<exists> l1 u1 l2 u2. Some (l1, u1) = a \<and> Some (l2, u2) = b"
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2967
proof (cases a)
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2968
  case None
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2969
  hence "None = lift_bin a b f"
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2970
    unfolding None lift_bin.simps ..
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2971
  thus ?thesis
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2972
    using lift_bin_Some by auto
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2973
next
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2974
  case (Some a')
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2975
  show ?thesis
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2976
  proof (cases b)
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2977
    case None
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2978
    hence "None = lift_bin a b f"
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2979
      unfolding None lift_bin.simps ..
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2980
    thus ?thesis using lift_bin_Some by auto
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2981
  next
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2982
    case (Some b')
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2983
    obtain la ua where a': "a' = (la, ua)"
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2984
      by (cases a') auto
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2985
    obtain lb ub where b': "b' = (lb, ub)"
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2986
      by (cases b') auto
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2987
    thus ?thesis
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2988
      unfolding \<open>a = Some a'\<close> \<open>b = Some b'\<close> a' b' by auto
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2989
  qed
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2990
qed
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2991
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2992
lemma lift_bin_f:
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2993
  assumes lift_bin_Some: "Some (l, u) = lift_bin (g a) (g b) f"
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2994
    and Pa: "\<And>l u. Some (l, u) = g a \<Longrightarrow> P l u a"
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2995
    and Pb: "\<And>l u. Some (l, u) = g b \<Longrightarrow> P l u b"
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2996
  shows "\<exists> l1 u1 l2 u2. P l1 u1 a \<and> P l2 u2 b \<and> Some (l, u) = f l1 u1 l2 u2"
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2997
proof -
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2998
  obtain l1 u1 l2 u2
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  2999
    where Sa: "Some (l1, u1) = g a"
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3000
      and Sb: "Some (l2, u2) = g b"
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3001
    using lift_bin_ex[OF assms(1)] by auto
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3002
  have lu: "Some (l, u) = f l1 u1 l2 u2"
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3003
    using lift_bin_Some[unfolded Sa[symmetric] Sb[symmetric] lift_bin.simps] by auto
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3004
  thus ?thesis
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3005
    using Pa[OF Sa] Pb[OF Sb] by auto
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3006
qed
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3007
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3008
lemma lift_bin:
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3009
  assumes lift_bin_Some: "Some (l, u) = lift_bin (approx' prec a bs) (approx' prec b bs) f"
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3010
    and Pa: "\<And>l u. Some (l, u) = approx prec a bs \<Longrightarrow>
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3011
      real_of_float l \<le> interpret_floatarith a xs \<and> interpret_floatarith a xs \<le> real_of_float u" (is "\<And>l u. _ = ?g a \<Longrightarrow> ?P l u a")
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3012
    and Pb: "\<And>l u. Some (l, u) = approx prec b bs \<Longrightarrow>
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3013
      real_of_float l \<le> interpret_floatarith b xs \<and> interpret_floatarith b xs \<le> real_of_float u"
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3014
  shows "\<exists>l1 u1 l2 u2. (real_of_float l1 \<le> interpret_floatarith a xs \<and> interpret_floatarith a xs \<le> real_of_float u1) \<and>
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3015
                       (real_of_float l2 \<le> interpret_floatarith b xs \<and> interpret_floatarith b xs \<le> real_of_float u2) \<and>
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3016
                       Some (l, u) = (f l1 u1 l2 u2)"
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3017
proof -
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3018
  { fix l u assume "Some (l, u) = approx' prec a bs"
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3019
    with approx_approx'[of prec a bs, OF _ this] Pa
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3020
    have "l \<le> interpret_floatarith a xs \<and> interpret_floatarith a xs \<le> u" by auto } note Pa = this
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3021
  { fix l u assume "Some (l, u) = approx' prec b bs"
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3022
    with approx_approx'[of prec b bs, OF _ this] Pb
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3023
    have "l \<le> interpret_floatarith b xs \<and> interpret_floatarith b xs \<le> u" by auto } note Pb = this
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3024
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3025
  from lift_bin_f[where g="\<lambda>a. approx' prec a bs" and P = ?P, OF lift_bin_Some, OF Pa Pb]
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3026
  show ?thesis by auto
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3027
qed
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3028
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3029
lemma lift_bin'_ex:
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3030
  assumes lift_bin'_Some: "Some (l, u) = lift_bin' a b f"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3031
  shows "\<exists> l1 u1 l2 u2. Some (l1, u1) = a \<and> Some (l2, u2) = b"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3032
proof (cases a)
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3033
  case None
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3034
  hence "None = lift_bin' a b f"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3035
    unfolding None lift_bin'.simps ..
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3036
  thus ?thesis
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3037
    using lift_bin'_Some by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3038
next
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3039
  case (Some a')
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3040
  show ?thesis
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3041
  proof (cases b)
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3042
    case None
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3043
    hence "None = lift_bin' a b f"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3044
      unfolding None lift_bin'.simps ..
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3045
    thus ?thesis using lift_bin'_Some by auto
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3046
  next
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3047
    case (Some b')
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3048
    obtain la ua where a': "a' = (la, ua)"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3049
      by (cases a') auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3050
    obtain lb ub where b': "b' = (lb, ub)"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3051
      by (cases b') auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3052
    thus ?thesis
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3053
      unfolding \<open>a = Some a'\<close> \<open>b = Some b'\<close> a' b' by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3054
  qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3055
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3056
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3057
lemma lift_bin'_f:
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3058
  assumes lift_bin'_Some: "Some (l, u) = lift_bin' (g a) (g b) f"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3059
    and Pa: "\<And>l u. Some (l, u) = g a \<Longrightarrow> P l u a"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3060
    and Pb: "\<And>l u. Some (l, u) = g b \<Longrightarrow> P l u b"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3061
  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
  3062
proof -
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3063
  obtain l1 u1 l2 u2
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3064
    where Sa: "Some (l1, u1) = g a"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3065
      and Sb: "Some (l2, u2) = g b"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3066
    using lift_bin'_ex[OF assms(1)] by auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3067
  have lu: "(l, u) = f l1 u1 l2 u2"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3068
    using lift_bin'_Some[unfolded Sa[symmetric] Sb[symmetric] lift_bin'.simps] by auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3069
  have "l = fst (f l1 u1 l2 u2)" and "u = snd (f l1 u1 l2 u2)"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3070
    unfolding lu[symmetric] by auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3071
  thus ?thesis
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3072
    using Pa[OF Sa] Pb[OF Sb] by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3073
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3074
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3075
lemma lift_bin':
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3076
  assumes lift_bin'_Some: "Some (l, u) = lift_bin' (approx' prec a bs) (approx' prec b bs) f"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3077
    and Pa: "\<And>l u. Some (l, u) = approx prec a bs \<Longrightarrow>
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3078
      l \<le> interpret_floatarith a xs \<and> interpret_floatarith a xs \<le> u" (is "\<And>l u. _ = ?g a \<Longrightarrow> ?P l u a")
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3079
    and Pb: "\<And>l u. Some (l, u) = approx prec b bs \<Longrightarrow>
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3080
      l \<le> interpret_floatarith b xs \<and> interpret_floatarith b xs \<le> u"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3081
  shows "\<exists>l1 u1 l2 u2. (l1 \<le> interpret_floatarith a xs \<and> interpret_floatarith a xs \<le> u1) \<and>
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3082
                       (l2 \<le> interpret_floatarith b xs \<and> interpret_floatarith b xs \<le> u2) \<and>
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3083
                       l = fst (f l1 u1 l2 u2) \<and> u = snd (f l1 u1 l2 u2)"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3084
proof -
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3085
  { fix l u assume "Some (l, u) = approx' prec a bs"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3086
    with approx_approx'[of prec a bs, OF _ this] Pa
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  3087
    have "l \<le> interpret_floatarith a xs \<and> interpret_floatarith a xs \<le> u" by auto } note Pa = this
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3088
  { fix l u assume "Some (l, u) = approx' prec b bs"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3089
    with approx_approx'[of prec b bs, OF _ this] Pb
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  3090
    have "l \<le> interpret_floatarith b xs \<and> interpret_floatarith b xs \<le> u" by auto } note Pb = this
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3091
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3092
  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
  3093
  show ?thesis by auto
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3094
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3095
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3096
lemma lift_un'_ex:
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3097
  assumes lift_un'_Some: "Some (l, u) = lift_un' a f"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3098
  shows "\<exists> l u. Some (l, u) = a"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3099
proof (cases a)
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3100
  case None
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3101
  hence "None = lift_un' a f"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3102
    unfolding None lift_un'.simps ..
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3103
  thus ?thesis
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3104
    using lift_un'_Some by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3105
next
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3106
  case (Some a')
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3107
  obtain la ua where a': "a' = (la, ua)"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3108
    by (cases a') auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3109
  thus ?thesis
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3110
    unfolding \<open>a = Some a'\<close> a' by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3111
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3112
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3113
lemma lift_un'_f:
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3114
  assumes lift_un'_Some: "Some (l, u) = lift_un' (g a) f"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3115
    and Pa: "\<And>l u. Some (l, u) = g a \<Longrightarrow> P l u a"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3116
  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
  3117
proof -
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3118
  obtain l1 u1 where Sa: "Some (l1, u1) = g a"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3119
    using lift_un'_ex[OF assms(1)] by auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3120
  have lu: "(l, u) = f l1 u1"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3121
    using lift_un'_Some[unfolded Sa[symmetric] lift_un'.simps] by auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3122
  have "l = fst (f l1 u1)" and "u = snd (f l1 u1)"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3123
    unfolding lu[symmetric] by auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3124
  thus ?thesis
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3125
    using Pa[OF Sa] by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3126
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3127
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3128
lemma lift_un':
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3129
  assumes lift_un'_Some: "Some (l, u) = lift_un' (approx' prec a bs) f"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3130
    and Pa: "\<And>l u. Some (l, u) = approx prec a bs \<Longrightarrow>
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3131
      l \<le> interpret_floatarith a xs \<and> interpret_floatarith a xs \<le> u"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3132
      (is "\<And>l u. _ = ?g a \<Longrightarrow> ?P l u a")
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3133
  shows "\<exists>l1 u1. (l1 \<le> interpret_floatarith a xs \<and> interpret_floatarith a xs \<le> u1) \<and>
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3134
    l = fst (f l1 u1) \<and> u = snd (f l1 u1)"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3135
proof -
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3136
  have Pa: "l \<le> interpret_floatarith a xs \<and> interpret_floatarith a xs \<le> u"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3137
    if "Some (l, u) = approx' prec a bs" for l u
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3138
    using approx_approx'[of prec a bs, OF _ that] Pa
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3139
     by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3140
  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
  3141
  show ?thesis by auto
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3142
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3143
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3144
lemma lift_un'_bnds:
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  3145
  assumes bnds: "\<forall> (x::real) lx ux. (l, u) = f lx ux \<and> x \<in> { lx .. ux } \<longrightarrow> l \<le> f' x \<and> f' x \<le> u"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3146
    and lift_un'_Some: "Some (l, u) = lift_un' (approx' prec a bs) f"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3147
    and Pa: "\<And>l u. Some (l, u) = approx prec a bs \<Longrightarrow>
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3148
      l \<le> interpret_floatarith a xs \<and> interpret_floatarith a xs \<le> u"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  3149
  shows "real_of_float l \<le> f' (interpret_floatarith a xs) \<and> f' (interpret_floatarith a xs) \<le> real_of_float u"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3150
proof -
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3151
  from lift_un'[OF lift_un'_Some Pa]
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3152
  obtain l1 u1 where "l1 \<le> interpret_floatarith a xs"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3153
    and "interpret_floatarith a xs \<le> u1"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3154
    and "l = fst (f l1 u1)"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3155
    and "u = snd (f l1 u1)"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3156
    by blast
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3157
  hence "(l, u) = f l1 u1" and "interpret_floatarith a xs \<in> {l1 .. u1}"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3158
    by auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3159
  thus ?thesis
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3160
    using bnds by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3161
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3162
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3163
lemma lift_un_ex:
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3164
  assumes lift_un_Some: "Some (l, u) = lift_un a f"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3165
  shows "\<exists>l u. Some (l, u) = a"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3166
proof (cases a)
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3167
  case None
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3168
  hence "None = lift_un a f"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3169
    unfolding None lift_un.simps ..
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3170
  thus ?thesis
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3171
    using lift_un_Some by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3172
next
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3173
  case (Some a')
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3174
  obtain la ua where a': "a' = (la, ua)"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3175
    by (cases a') auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3176
  thus ?thesis
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3177
    unfolding \<open>a = Some a'\<close> a' by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3178
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3179
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3180
lemma lift_un_f:
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3181
  assumes lift_un_Some: "Some (l, u) = lift_un (g a) f"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3182
    and Pa: "\<And>l u. Some (l, u) = g a \<Longrightarrow> P l u a"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3183
  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
  3184
proof -
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3185
  obtain l1 u1 where Sa: "Some (l1, u1) = g a"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3186
    using lift_un_ex[OF assms(1)] by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3187
  have "fst (f l1 u1) \<noteq> None \<and> snd (f l1 u1) \<noteq> None"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3188
  proof (rule ccontr)
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3189
    assume "\<not> (fst (f l1 u1) \<noteq> None \<and> snd (f l1 u1) \<noteq> None)"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3190
    hence or: "fst (f l1 u1) = None \<or> snd (f l1 u1) = None" by auto
31809
hoelzl
parents: 31790
diff changeset
  3191
    hence "lift_un (g a) f = None"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3192
    proof (cases "fst (f l1 u1) = None")
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3193
      case True
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3194
      then obtain b where b: "f l1 u1 = (None, b)"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3195
        by (cases "f l1 u1") auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3196
      thus ?thesis
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3197
        unfolding Sa[symmetric] lift_un.simps b by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3198
    next
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3199
      case False
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3200
      hence "snd (f l1 u1) = None"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3201
        using or by auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3202
      with False obtain b where b: "f l1 u1 = (Some b, None)"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3203
        by (cases "f l1 u1") auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3204
      thus ?thesis
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3205
        unfolding Sa[symmetric] lift_un.simps b by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3206
    qed
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3207
    thus False
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3208
      using lift_un_Some by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3209
  qed
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3210
  then obtain a' b' where f: "f l1 u1 = (Some a', Some b')"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3211
    by (cases "f l1 u1") auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3212
  from lift_un_Some[unfolded Sa[symmetric] lift_un.simps f]
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3213
  have "Some l = fst (f l1 u1)" and "Some u = snd (f l1 u1)"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3214
    unfolding f by auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3215
  thus ?thesis
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3216
    unfolding Sa[symmetric] lift_un.simps using Pa[OF Sa] by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3217
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3218
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3219
lemma lift_un:
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3220
  assumes lift_un_Some: "Some (l, u) = lift_un (approx' prec a bs) f"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3221
    and Pa: "\<And>l u. Some (l, u) = approx prec a bs \<Longrightarrow>
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3222
        l \<le> interpret_floatarith a xs \<and> interpret_floatarith a xs \<le> u"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3223
      (is "\<And>l u. _ = ?g a \<Longrightarrow> ?P l u a")
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3224
  shows "\<exists>l1 u1. (l1 \<le> interpret_floatarith a xs \<and> interpret_floatarith a xs \<le> u1) \<and>
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3225
                  Some l = fst (f l1 u1) \<and> Some u = snd (f l1 u1)"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3226
proof -
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3227
  have Pa: "l \<le> interpret_floatarith a xs \<and> interpret_floatarith a xs \<le> u"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3228
    if "Some (l, u) = approx' prec a bs" for l u
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3229
    using approx_approx'[of prec a bs, OF _ that] Pa by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3230
  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
  3231
  show ?thesis by auto
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3232
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3233
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3234
lemma lift_un_bnds:
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3235
  assumes bnds: "\<forall>(x::real) lx ux. (Some l, Some u) = f lx ux \<and> x \<in> { lx .. ux } \<longrightarrow> l \<le> f' x \<and> f' x \<le> u"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3236
    and lift_un_Some: "Some (l, u) = lift_un (approx' prec a bs) f"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3237
    and Pa: "\<And>l u. Some (l, u) = approx prec a bs \<Longrightarrow>
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3238
      l \<le> interpret_floatarith a xs \<and> interpret_floatarith a xs \<le> u"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  3239
  shows "real_of_float l \<le> f' (interpret_floatarith a xs) \<and> f' (interpret_floatarith a xs) \<le> real_of_float u"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3240
proof -
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3241
  from lift_un[OF lift_un_Some Pa]
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3242
  obtain l1 u1 where "l1 \<le> interpret_floatarith a xs"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3243
    and "interpret_floatarith a xs \<le> u1"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3244
    and "Some l = fst (f l1 u1)"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3245
    and "Some u = snd (f l1 u1)"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3246
    by blast
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3247
  hence "(Some l, Some u) = f l1 u1" and "interpret_floatarith a xs \<in> {l1 .. u1}"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3248
    by auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3249
  thus ?thesis
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3250
    using bnds by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3251
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3252
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3253
lemma approx:
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3254
  assumes "bounded_by xs vs"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3255
    and "Some (l, u) = approx prec arith vs" (is "_ = ?g arith")
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  3256
  shows "l \<le> interpret_floatarith arith xs \<and> interpret_floatarith arith xs \<le> u" (is "?P l u arith")
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
  3257
  using \<open>Some (l, u) = approx prec arith vs\<close>
45129
1fce03e3e8ad tuned proofs -- eliminated vacuous "induct arbitrary: ..." situations;
wenzelm
parents: 44821
diff changeset
  3258
proof (induct arith arbitrary: l u)
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3259
  case (Add a b)
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3260
  from lift_bin'[OF Add.prems[unfolded approx.simps]] Add.hyps
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3261
  obtain l1 u1 l2 u2 where "l = float_plus_down prec l1 l2"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3262
    and "u = float_plus_up prec u1 u2" "l1 \<le> interpret_floatarith a xs"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3263
    and "interpret_floatarith a xs \<le> u1" "l2 \<le> interpret_floatarith b xs"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3264
    and "interpret_floatarith b xs \<le> u2"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3265
    unfolding fst_conv snd_conv by blast
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3266
  thus ?case
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3267
    unfolding interpret_floatarith.simps by (auto intro!: float_plus_up_le float_plus_down_le)
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3268
next
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3269
  case (Minus a)
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3270
  from lift_un'[OF Minus.prems[unfolded approx.simps]] Minus.hyps
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3271
  obtain l1 u1 where "l = -u1" "u = -l1"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3272
    and "l1 \<le> interpret_floatarith a xs" "interpret_floatarith a xs \<le> u1"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3273
    unfolding fst_conv snd_conv by blast
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3274
  thus ?case
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3275
    unfolding interpret_floatarith.simps using minus_float.rep_eq by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3276
next
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3277
  case (Mult a b)
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3278
  from lift_bin'[OF Mult.prems[unfolded approx.simps]] Mult.hyps
31809
hoelzl
parents: 31790
diff changeset
  3279
  obtain l1 u1 l2 u2
62200
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3280
    where l: "l = fst (bnds_mult prec l1 u1 l2 u2)"
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3281
    and u: "u = snd (bnds_mult prec l1 u1 l2 u2)"
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3282
    and a: "l1 \<le> interpret_floatarith a xs" "interpret_floatarith a xs \<le> u1"
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3283
    and b: "l2 \<le> interpret_floatarith b xs" "interpret_floatarith b xs \<le> u2" unfolding fst_conv snd_conv by blast
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3284
  from l u have lu: "(l, u) = bnds_mult prec l1 u1 l2 u2" by simp
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3285
  from bnds_mult[OF lu] a b show ?case by simp
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3286
next
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3287
  case (Inverse a)
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3288
  from lift_un[OF Inverse.prems[unfolded approx.simps], unfolded if_distrib[of fst] if_distrib[of snd] fst_conv snd_conv] Inverse.hyps
31809
hoelzl
parents: 31790
diff changeset
  3289
  obtain l1 u1 where l': "Some l = (if 0 < l1 \<or> u1 < 0 then Some (float_divl prec 1 u1) else None)"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3290
    and u': "Some u = (if 0 < l1 \<or> u1 < 0 then Some (float_divr prec 1 l1) else None)"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3291
    and l1: "l1 \<le> interpret_floatarith a xs"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3292
    and u1: "interpret_floatarith a xs \<le> u1"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3293
    by blast
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3294
  have either: "0 < l1 \<or> u1 < 0"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3295
  proof (rule ccontr)
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3296
    assume P: "\<not> (0 < l1 \<or> u1 < 0)"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3297
    show False
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3298
      using l' unfolding if_not_P[OF P] by auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3299
  qed
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  3300
  moreover have l1_le_u1: "real_of_float l1 \<le> real_of_float u1"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3301
    using l1 u1 by auto
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  3302
  ultimately have "real_of_float l1 \<noteq> 0" and "real_of_float u1 \<noteq> 0"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3303
    by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3304
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  3305
  have inv: "inverse u1 \<le> inverse (interpret_floatarith a xs)
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  3306
           \<and> inverse (interpret_floatarith a xs) \<le> inverse l1"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3307
  proof (cases "0 < l1")
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3308
    case True
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  3309
    hence "0 < real_of_float u1" and "0 < real_of_float l1" "0 < interpret_floatarith a xs"
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  3310
      using l1_le_u1 l1 by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3311
    show ?thesis
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  3312
      unfolding inverse_le_iff_le[OF \<open>0 < real_of_float u1\<close> \<open>0 < interpret_floatarith a xs\<close>]
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  3313
        inverse_le_iff_le[OF \<open>0 < interpret_floatarith a xs\<close> \<open>0 < real_of_float l1\<close>]
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3314
      using l1 u1 by auto
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3315
  next
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3316
    case False
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3317
    hence "u1 < 0"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3318
      using either by blast
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  3319
    hence "real_of_float u1 < 0" and "real_of_float l1 < 0" "interpret_floatarith a xs < 0"
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  3320
      using l1_le_u1 u1 by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3321
    show ?thesis
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  3322
      unfolding inverse_le_iff_le_neg[OF \<open>real_of_float u1 < 0\<close> \<open>interpret_floatarith a xs < 0\<close>]
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  3323
        inverse_le_iff_le_neg[OF \<open>interpret_floatarith a xs < 0\<close> \<open>real_of_float l1 < 0\<close>]
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3324
      using l1 u1 by auto
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3325
  qed
31468
b8267feaf342 Approximation: Corrected precision of ln on all real values
hoelzl
parents: 31467
diff changeset
  3326
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3327
  from l' have "l = float_divl prec 1 u1"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3328
    by (cases "0 < l1 \<or> u1 < 0") auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3329
  hence "l \<le> inverse u1"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  3330
    unfolding nonzero_inverse_eq_divide[OF \<open>real_of_float u1 \<noteq> 0\<close>]
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3331
    using float_divl[of prec 1 u1] by auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3332
  also have "\<dots> \<le> inverse (interpret_floatarith a xs)"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3333
    using inv by auto
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  3334
  finally have "l \<le> inverse (interpret_floatarith a xs)" .
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3335
  moreover
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3336
  from u' have "u = float_divr prec 1 l1"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3337
    by (cases "0 < l1 \<or> u1 < 0") auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3338
  hence "inverse l1 \<le> u"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  3339
    unfolding nonzero_inverse_eq_divide[OF \<open>real_of_float l1 \<noteq> 0\<close>]
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3340
    using float_divr[of 1 l1 prec] by auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3341
  hence "inverse (interpret_floatarith a xs) \<le> u"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3342
    by (rule order_trans[OF inv[THEN conjunct2]])
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3343
  ultimately show ?case
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3344
    unfolding interpret_floatarith.simps using l1 u1 by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3345
next
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3346
  case (Abs x)
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3347
  from lift_un'[OF Abs.prems[unfolded approx.simps], unfolded fst_conv snd_conv] Abs.hyps
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3348
  obtain l1 u1 where l': "l = (if l1 < 0 \<and> 0 < u1 then 0 else min \<bar>l1\<bar> \<bar>u1\<bar>)"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3349
    and u': "u = max \<bar>l1\<bar> \<bar>u1\<bar>"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3350
    and l1: "l1 \<le> interpret_floatarith x xs"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3351
    and u1: "interpret_floatarith x xs \<le> u1"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3352
    by blast
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3353
  thus ?case
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3354
    unfolding l' u'
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3355
    by (cases "l1 < 0 \<and> 0 < u1") (auto simp add: real_of_float_min real_of_float_max)
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3356
next
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3357
  case (Min a b)
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3358
  from lift_bin'[OF Min.prems[unfolded approx.simps], unfolded fst_conv snd_conv] Min.hyps
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3359
  obtain l1 u1 l2 u2 where l': "l = min l1 l2" and u': "u = min u1 u2"
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  3360
    and l1: "l1 \<le> interpret_floatarith a xs" and u1: "interpret_floatarith a xs \<le> u1"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3361
    and l1: "l2 \<le> interpret_floatarith b xs" and u1: "interpret_floatarith b xs \<le> u2"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3362
    by blast
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3363
  thus ?case
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3364
    unfolding l' u' by (auto simp add: real_of_float_min)
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3365
next
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3366
  case (Max a b)
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3367
  from lift_bin'[OF Max.prems[unfolded approx.simps], unfolded fst_conv snd_conv] Max.hyps
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3368
  obtain l1 u1 l2 u2 where l': "l = max l1 l2" and u': "u = max u1 u2"
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  3369
    and l1: "l1 \<le> interpret_floatarith a xs" and u1: "interpret_floatarith a xs \<le> u1"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3370
    and l1: "l2 \<le> interpret_floatarith b xs" and u1: "interpret_floatarith b xs \<le> u2"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3371
    by blast
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3372
  thus ?case
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3373
    unfolding l' u' by (auto simp add: real_of_float_max)
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3374
next
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3375
  case (Cos a)
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3376
  with lift_un'_bnds[OF bnds_cos] show ?case by auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3377
next
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3378
  case (Arctan a)
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3379
  with lift_un'_bnds[OF bnds_arctan] show ?case by auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3380
next
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3381
  case Pi
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3382
  with pi_boundaries show ?case by auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3383
next
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3384
  case (Sqrt a)
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3385
  with lift_un'_bnds[OF bnds_sqrt] show ?case by auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3386
next
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3387
  case (Exp a)
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3388
  with lift_un'_bnds[OF bnds_exp] show ?case by auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3389
next
62200
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3390
  case (Powr a b)
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3391
  from lift_bin[OF Powr.prems[unfolded approx.simps]] Powr.hyps
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3392
    obtain l1 u1 l2 u2 where lu: "Some (l, u) = bnds_powr prec l1 u1 l2 u2"
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3393
      and l1: "l1 \<le> interpret_floatarith a xs" and u1: "interpret_floatarith a xs \<le> u1"
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3394
      and l2: "l2 \<le> interpret_floatarith b xs" and u2: "interpret_floatarith b xs \<le> u2"
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3395
      by blast
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3396
  from bnds_powr[OF lu] l1 u1 l2 u2
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3397
    show ?case by simp
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3398
next
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3399
  case (Ln a)
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3400
  with lift_un_bnds[OF bnds_ln] show ?case by auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3401
next
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3402
  case (Power a n)
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3403
  with lift_un'_bnds[OF bnds_power] show ?case by auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3404
next
63263
c6c95d64607a approximation, derivative, and continuity of floor and ceiling
immler
parents: 63248
diff changeset
  3405
  case (Floor a)
c6c95d64607a approximation, derivative, and continuity of floor and ceiling
immler
parents: 63248
diff changeset
  3406
  from lift_un'[OF Floor.prems[unfolded approx.simps] Floor.hyps]
c6c95d64607a approximation, derivative, and continuity of floor and ceiling
immler
parents: 63248
diff changeset
  3407
  show ?case by (auto simp: floor_fl.rep_eq floor_mono)
c6c95d64607a approximation, derivative, and continuity of floor and ceiling
immler
parents: 63248
diff changeset
  3408
next
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3409
  case (Num f)
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3410
  thus ?case by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3411
next
32919
37adfa07b54b approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
hoelzl
parents: 32650
diff changeset
  3412
  case (Var n)
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
  3413
  from this[symmetric] \<open>bounded_by xs vs\<close>[THEN bounded_byE, of n]
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3414
  show ?case by (cases "n < length vs") auto
31811
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3415
qed
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3416
58310
91ea607a34d8 updated news
blanchet
parents: 58249
diff changeset
  3417
datatype form = Bound floatarith floatarith floatarith form
31811
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3418
              | Assign floatarith floatarith form
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3419
              | Less floatarith floatarith
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3420
              | LessEqual floatarith floatarith
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3421
              | AtLeastAtMost floatarith floatarith floatarith
58986
ec7373051a6c disjunction and conjunction for forms
immler
parents: 58985
diff changeset
  3422
              | Conj form form
ec7373051a6c disjunction and conjunction for forms
immler
parents: 58985
diff changeset
  3423
              | Disj form form
31811
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3424
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3425
fun interpret_form :: "form \<Rightarrow> real list \<Rightarrow> bool" where
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3426
"interpret_form (Bound x a b f) vs = (interpret_floatarith x vs \<in> { interpret_floatarith a vs .. interpret_floatarith b vs } \<longrightarrow> interpret_form f vs)" |
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3427
"interpret_form (Assign x a f) vs  = (interpret_floatarith x vs = interpret_floatarith a vs \<longrightarrow> interpret_form f vs)" |
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3428
"interpret_form (Less a b) vs      = (interpret_floatarith a vs < interpret_floatarith b vs)" |
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3429
"interpret_form (LessEqual a b) vs = (interpret_floatarith a vs \<le> interpret_floatarith b vs)" |
58986
ec7373051a6c disjunction and conjunction for forms
immler
parents: 58985
diff changeset
  3430
"interpret_form (AtLeastAtMost x a b) vs = (interpret_floatarith x vs \<in> { interpret_floatarith a vs .. interpret_floatarith b vs })" |
ec7373051a6c disjunction and conjunction for forms
immler
parents: 58985
diff changeset
  3431
"interpret_form (Conj f g) vs \<longleftrightarrow> interpret_form f vs \<and> interpret_form g vs" |
ec7373051a6c disjunction and conjunction for forms
immler
parents: 58985
diff changeset
  3432
"interpret_form (Disj f g) vs \<longleftrightarrow> interpret_form f vs \<or> interpret_form g vs"
31811
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3433
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3434
fun approx_form' and approx_form :: "nat \<Rightarrow> form \<Rightarrow> (float * float) option list \<Rightarrow> nat list \<Rightarrow> bool" where
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3435
"approx_form' prec f 0 n l u bs ss = approx_form prec f (bs[n := Some (l, u)]) ss" |
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3436
"approx_form' prec f (Suc s) n l u bs ss =
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
  3437
  (let m = (l + u) * Float 1 (- 1)
32919
37adfa07b54b approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
hoelzl
parents: 32650
diff changeset
  3438
   in (if approx_form' prec f s n l m bs ss then approx_form' prec f s n m u bs ss else False))" |
37adfa07b54b approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
hoelzl
parents: 32650
diff changeset
  3439
"approx_form prec (Bound (Var n) a b f) bs ss =
31811
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3440
   (case (approx prec a bs, approx prec b bs)
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3441
   of (Some (l, _), Some (_, u)) \<Rightarrow> approx_form' prec f (ss ! n) n l u bs ss
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3442
    | _ \<Rightarrow> False)" |
32919
37adfa07b54b approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
hoelzl
parents: 32650
diff changeset
  3443
"approx_form prec (Assign (Var n) a f) bs ss =
31811
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3444
   (case (approx prec a bs)
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3445
   of (Some (l, u)) \<Rightarrow> approx_form' prec f (ss ! n) n l u bs ss
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3446
    | _ \<Rightarrow> False)" |
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3447
"approx_form prec (Less a b) bs ss =
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3448
   (case (approx prec a bs, approx prec b bs)
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  3449
   of (Some (l, u), Some (l', u')) \<Rightarrow> float_plus_up prec u (-l') < 0
31811
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3450
    | _ \<Rightarrow> False)" |
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3451
"approx_form prec (LessEqual a b) bs ss =
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3452
   (case (approx prec a bs, approx prec b bs)
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  3453
   of (Some (l, u), Some (l', u')) \<Rightarrow> float_plus_up prec u (-l') \<le> 0
31811
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3454
    | _ \<Rightarrow> False)" |
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3455
"approx_form prec (AtLeastAtMost x a b) bs ss =
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3456
   (case (approx prec x bs, approx prec a bs, approx prec b bs)
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  3457
   of (Some (lx, ux), Some (l, u), Some (l', u')) \<Rightarrow> float_plus_up prec u (-lx) \<le> 0 \<and> float_plus_up prec ux (-l') \<le> 0
31811
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3458
    | _ \<Rightarrow> False)" |
58986
ec7373051a6c disjunction and conjunction for forms
immler
parents: 58985
diff changeset
  3459
"approx_form prec (Conj a b) bs ss \<longleftrightarrow> approx_form prec a bs ss \<and> approx_form prec b bs ss" |
ec7373051a6c disjunction and conjunction for forms
immler
parents: 58985
diff changeset
  3460
"approx_form prec (Disj a b) bs ss \<longleftrightarrow> approx_form prec a bs ss \<or> approx_form prec b bs ss" |
31811
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3461
"approx_form _ _ _ _ = False"
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3462
32919
37adfa07b54b approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
hoelzl
parents: 32650
diff changeset
  3463
lemma lazy_conj: "(if A then B else False) = (A \<and> B)" by simp
37adfa07b54b approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
hoelzl
parents: 32650
diff changeset
  3464
31811
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3465
lemma approx_form_approx_form':
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3466
  assumes "approx_form' prec f s n l u bs ss"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3467
    and "(x::real) \<in> { l .. u }"
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  3468
  obtains l' u' where "x \<in> { l' .. u' }"
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3469
    and "approx_form prec f (bs[n := Some (l', u')]) ss"
31811
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3470
using assms proof (induct s arbitrary: l u)
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3471
  case 0
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3472
  from this(1)[of l u] this(2,3)
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3473
  show thesis by auto
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3474
next
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3475
  case (Suc s)
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3476
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
  3477
  let ?m = "(l + u) * Float 1 (- 1)"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  3478
  have "real_of_float l \<le> ?m" and "?m \<le> real_of_float u"
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  3479
    unfolding less_eq_float_def using Suc.prems by auto
31811
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3480
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
  3481
  with \<open>x \<in> { l .. u }\<close>
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  3482
  have "x \<in> { l .. ?m} \<or> x \<in> { ?m .. u }" by auto
31811
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3483
  thus thesis
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3484
  proof (rule disjE)
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  3485
    assume *: "x \<in> { l .. ?m }"
31811
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3486
    with Suc.hyps[OF _ _ *] Suc.prems
32919
37adfa07b54b approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
hoelzl
parents: 32650
diff changeset
  3487
    show thesis by (simp add: Let_def lazy_conj)
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3488
  next
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  3489
    assume *: "x \<in> { ?m .. u }"
31811
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3490
    with Suc.hyps[OF _ _ *] Suc.prems
32919
37adfa07b54b approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
hoelzl
parents: 32650
diff changeset
  3491
    show thesis by (simp add: Let_def lazy_conj)
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3492
  qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3493
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3494
31811
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3495
lemma approx_form_aux:
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3496
  assumes "approx_form prec f vs ss"
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3497
    and "bounded_by xs vs"
31811
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3498
  shows "interpret_form f xs"
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3499
using assms proof (induct f arbitrary: vs)
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3500
  case (Bound x a b f)
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3501
  then obtain n
32919
37adfa07b54b approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
hoelzl
parents: 32650
diff changeset
  3502
    where x_eq: "x = Var n" by (cases x) auto
31811
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3503
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3504
  with Bound.prems obtain l u' l' u
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3505
    where l_eq: "Some (l, u') = approx prec a vs"
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3506
    and u_eq: "Some (l', u) = approx prec b vs"
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3507
    and approx_form': "approx_form' prec f (ss ! n) n l u vs ss"
37411
c88c44156083 removed simplifier congruence rule of "prod_case"
haftmann
parents: 37391
diff changeset
  3508
    by (cases "approx prec a vs", simp) (cases "approx prec b vs", auto)
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  3509
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3510
  have "interpret_form f xs"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3511
    if "xs ! n \<in> { interpret_floatarith a xs .. interpret_floatarith b xs }"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3512
  proof -
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3513
    from approx[OF Bound.prems(2) l_eq] and approx[OF Bound.prems(2) u_eq] that
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  3514
    have "xs ! n \<in> { l .. u}" by auto
31811
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3515
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3516
    from approx_form_approx_form'[OF approx_form' this]
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  3517
    obtain lx ux where bnds: "xs ! n \<in> { lx .. ux }"
31811
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3518
      and approx_form: "approx_form prec f (vs[n := Some (lx, ux)]) ss" .
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3519
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3520
    from \<open>bounded_by xs vs\<close> bnds have "bounded_by xs (vs[n := Some (lx, ux)])"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3521
      by (rule bounded_by_update)
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3522
    with Bound.hyps[OF approx_form] show ?thesis
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3523
      by blast
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3524
  qed
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3525
  thus ?case
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3526
    using interpret_form.simps x_eq and interpret_floatarith.simps by simp
31811
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3527
next
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3528
  case (Assign x a f)
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3529
  then obtain n where x_eq: "x = Var n"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3530
    by (cases x) auto
31811
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3531
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  3532
  with Assign.prems obtain l u
31811
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3533
    where bnd_eq: "Some (l, u) = approx prec a vs"
32919
37adfa07b54b approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
hoelzl
parents: 32650
diff changeset
  3534
    and x_eq: "x = Var n"
31811
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3535
    and approx_form': "approx_form' prec f (ss ! n) n l u vs ss"
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3536
    by (cases "approx prec a vs") auto
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3537
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3538
  have "interpret_form f xs"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3539
    if bnds: "xs ! n = interpret_floatarith a xs"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3540
  proof -
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3541
    from approx[OF Assign.prems(2) bnd_eq] bnds
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  3542
    have "xs ! n \<in> { l .. u}" by auto
31811
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3543
    from approx_form_approx_form'[OF approx_form' this]
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  3544
    obtain lx ux where bnds: "xs ! n \<in> { lx .. ux }"
31811
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3545
      and approx_form: "approx_form prec f (vs[n := Some (lx, ux)]) ss" .
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3546
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3547
    from \<open>bounded_by xs vs\<close> bnds have "bounded_by xs (vs[n := Some (lx, ux)])"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3548
      by (rule bounded_by_update)
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3549
    with Assign.hyps[OF approx_form] show ?thesis
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3550
      by blast
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3551
  qed
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3552
  thus ?case
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3553
    using interpret_form.simps x_eq and interpret_floatarith.simps by simp
31811
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3554
next
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3555
  case (Less a b)
31811
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3556
  then obtain l u l' u'
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3557
    where l_eq: "Some (l, u) = approx prec a vs"
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3558
      and u_eq: "Some (l', u') = approx prec b vs"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  3559
      and inequality: "real_of_float (float_plus_up prec u (-l')) < 0"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3560
    by (cases "approx prec a vs", auto, cases "approx prec b vs", auto)
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  3561
  from le_less_trans[OF float_plus_up inequality]
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  3562
    approx[OF Less.prems(2) l_eq] approx[OF Less.prems(2) u_eq]
31811
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3563
  show ?case by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3564
next
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3565
  case (LessEqual a b)
31811
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3566
  then obtain l u l' u'
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3567
    where l_eq: "Some (l, u) = approx prec a vs"
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3568
      and u_eq: "Some (l', u') = approx prec b vs"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  3569
      and inequality: "real_of_float (float_plus_up prec u (-l')) \<le> 0"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3570
    by (cases "approx prec a vs", auto, cases "approx prec b vs", auto)
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  3571
  from order_trans[OF float_plus_up inequality]
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  3572
    approx[OF LessEqual.prems(2) l_eq] approx[OF LessEqual.prems(2) u_eq]
31811
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3573
  show ?case by auto
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3574
next
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3575
  case (AtLeastAtMost x a b)
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3576
  then obtain lx ux l u l' u'
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3577
    where x_eq: "Some (lx, ux) = approx prec x vs"
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3578
    and l_eq: "Some (l, u) = approx prec a vs"
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3579
    and u_eq: "Some (l', u') = approx prec b vs"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  3580
    and inequality: "real_of_float (float_plus_up prec u (-lx)) \<le> 0" "real_of_float (float_plus_up prec ux (-l')) \<le> 0"
31811
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3581
    by (cases "approx prec x vs", auto,
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3582
      cases "approx prec a vs", auto,
56073
29e308b56d23 enhanced simplifier solver for preconditions of rewrite rule, can now deal with conjunctions
nipkow
parents: 55506
diff changeset
  3583
      cases "approx prec b vs", auto)
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  3584
  from order_trans[OF float_plus_up inequality(1)] order_trans[OF float_plus_up inequality(2)]
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  3585
    approx[OF AtLeastAtMost.prems(2) l_eq] approx[OF AtLeastAtMost.prems(2) u_eq] approx[OF AtLeastAtMost.prems(2) x_eq]
31811
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3586
  show ?case by auto
58986
ec7373051a6c disjunction and conjunction for forms
immler
parents: 58985
diff changeset
  3587
qed auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3588
31811
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3589
lemma approx_form:
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3590
  assumes "n = length xs"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3591
    and "approx_form prec f (replicate n None) ss"
31811
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3592
  shows "interpret_form f xs"
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3593
  using approx_form_aux[OF _ bounded_by_None] assms by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3594
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3595
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
  3596
subsection \<open>Implementing Taylor series expansion\<close>
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3597
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3598
fun isDERIV :: "nat \<Rightarrow> floatarith \<Rightarrow> real list \<Rightarrow> bool" where
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3599
"isDERIV x (Add a b) vs         = (isDERIV x a vs \<and> isDERIV x b vs)" |
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3600
"isDERIV x (Mult a b) vs        = (isDERIV x a vs \<and> isDERIV x b vs)" |
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3601
"isDERIV x (Minus a) vs         = isDERIV x a vs" |
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3602
"isDERIV x (Inverse a) vs       = (isDERIV x a vs \<and> interpret_floatarith a vs \<noteq> 0)" |
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3603
"isDERIV x (Cos a) vs           = isDERIV x a vs" |
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3604
"isDERIV x (Arctan a) vs        = isDERIV x a vs" |
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3605
"isDERIV x (Min a b) vs         = False" |
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3606
"isDERIV x (Max a b) vs         = False" |
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3607
"isDERIV x (Abs a) vs           = False" |
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3608
"isDERIV x Pi vs                = True" |
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3609
"isDERIV x (Sqrt a) vs          = (isDERIV x a vs \<and> interpret_floatarith a vs > 0)" |
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3610
"isDERIV x (Exp a) vs           = isDERIV x a vs" |
63263
c6c95d64607a approximation, derivative, and continuity of floor and ceiling
immler
parents: 63248
diff changeset
  3611
"isDERIV x (Powr a b) vs        =
62200
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3612
    (isDERIV x a vs \<and> isDERIV x b vs \<and> interpret_floatarith a vs > 0)" |
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3613
"isDERIV x (Ln a) vs            = (isDERIV x a vs \<and> interpret_floatarith a vs > 0)" |
63263
c6c95d64607a approximation, derivative, and continuity of floor and ceiling
immler
parents: 63248
diff changeset
  3614
"isDERIV x (Floor a) vs         = (isDERIV x a vs \<and> interpret_floatarith a vs \<notin> \<int>)" |
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3615
"isDERIV x (Power a 0) vs       = True" |
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3616
"isDERIV x (Power a (Suc n)) vs = isDERIV x a vs" |
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3617
"isDERIV x (Num f) vs           = True" |
32919
37adfa07b54b approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
hoelzl
parents: 32650
diff changeset
  3618
"isDERIV x (Var n) vs          = True"
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3619
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3620
fun DERIV_floatarith :: "nat \<Rightarrow> floatarith \<Rightarrow> floatarith" where
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3621
"DERIV_floatarith x (Add a b)         = Add (DERIV_floatarith x a) (DERIV_floatarith x b)" |
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3622
"DERIV_floatarith x (Mult a b)        = Add (Mult a (DERIV_floatarith x b)) (Mult (DERIV_floatarith x a) b)" |
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3623
"DERIV_floatarith x (Minus a)         = Minus (DERIV_floatarith x a)" |
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3624
"DERIV_floatarith x (Inverse a)       = Minus (Mult (DERIV_floatarith x a) (Inverse (Power a 2)))" |
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
  3625
"DERIV_floatarith x (Cos a)           = Minus (Mult (Cos (Add (Mult Pi (Num (Float 1 (- 1)))) (Minus a))) (DERIV_floatarith x a))" |
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3626
"DERIV_floatarith x (Arctan a)        = Mult (Inverse (Add (Num 1) (Power a 2))) (DERIV_floatarith x a)" |
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3627
"DERIV_floatarith x (Min a b)         = Num 0" |
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3628
"DERIV_floatarith x (Max a b)         = Num 0" |
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3629
"DERIV_floatarith x (Abs a)           = Num 0" |
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3630
"DERIV_floatarith x Pi                = Num 0" |
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3631
"DERIV_floatarith x (Sqrt a)          = (Mult (Inverse (Mult (Sqrt a) (Num 2))) (DERIV_floatarith x a))" |
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3632
"DERIV_floatarith x (Exp a)           = Mult (Exp a) (DERIV_floatarith x a)" |
62200
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3633
"DERIV_floatarith x (Powr a b)        =
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3634
   Mult (Powr a b) (Add (Mult (DERIV_floatarith x b) (Ln a)) (Mult (Mult (DERIV_floatarith x a) b) (Inverse a)))" |
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3635
"DERIV_floatarith x (Ln a)            = Mult (Inverse a) (DERIV_floatarith x a)" |
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3636
"DERIV_floatarith x (Power a 0)       = Num 0" |
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3637
"DERIV_floatarith x (Power a (Suc n)) = Mult (Num (Float (int (Suc n)) 0)) (Mult (Power a n) (DERIV_floatarith x a))" |
63263
c6c95d64607a approximation, derivative, and continuity of floor and ceiling
immler
parents: 63248
diff changeset
  3638
"DERIV_floatarith x (Floor a)         = Num 0" |
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3639
"DERIV_floatarith x (Num f)           = Num 0" |
32919
37adfa07b54b approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
hoelzl
parents: 32650
diff changeset
  3640
"DERIV_floatarith x (Var n)          = (if x = n then Num 1 else Num 0)"
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3641
62200
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3642
lemma has_real_derivative_powr':
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3643
  fixes f g :: "real \<Rightarrow> real"
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3644
  assumes "(f has_real_derivative f') (at x)"
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3645
  assumes "(g has_real_derivative g') (at x)"
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3646
  assumes "f x > 0"
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3647
  defines "h \<equiv> \<lambda>x. f x powr g x * (g' * ln (f x) + f' * g x / f x)"
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3648
  shows   "((\<lambda>x. f x powr g x) has_real_derivative h x) (at x)"
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3649
proof (subst DERIV_cong_ev[OF refl _ refl])
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3650
  from assms have "isCont f x"
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3651
    by (simp add: DERIV_continuous)
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3652
  hence "f \<midarrow>x\<rightarrow> f x" by (simp add: continuous_at)
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3653
  with \<open>f x > 0\<close> have "eventually (\<lambda>x. f x > 0) (nhds x)"
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3654
    by (auto simp: tendsto_at_iff_tendsto_nhds dest: order_tendstoD)
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3655
  thus "eventually (\<lambda>x. f x powr g x = exp (g x * ln (f x))) (nhds x)"
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3656
    by eventually_elim (simp add: powr_def)
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3657
next
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3658
  from assms show "((\<lambda>x. exp (g x * ln (f x))) has_real_derivative h x) (at x)"
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3659
    by (auto intro!: derivative_eq_intros simp: h_def powr_def)
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3660
qed
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3661
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3662
lemma DERIV_floatarith:
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3663
  assumes "n < length vs"
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3664
  assumes isDERIV: "isDERIV n f (vs[n := x])"
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3665
  shows "DERIV (\<lambda> x'. interpret_floatarith f (vs[n := x'])) x :>
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3666
               interpret_floatarith (DERIV_floatarith n f) (vs[n := x])"
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3667
   (is "DERIV (?i f) x :> _")
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3668
using isDERIV
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3669
proof (induct f arbitrary: x)
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3670
  case (Inverse a)
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3671
  thus ?case
56381
0556204bc230 merged DERIV_intros, has_derivative_intros into derivative_intros
hoelzl
parents: 56195
diff changeset
  3672
    by (auto intro!: derivative_eq_intros simp add: algebra_simps power2_eq_square)
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3673
next
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3674
  case (Cos a)
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3675
  thus ?case
56382
5a50109d51ab fix #0556204bc230
hoelzl
parents: 56381
diff changeset
  3676
    by (auto intro!: derivative_eq_intros
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3677
           simp del: interpret_floatarith.simps(5)
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3678
           simp add: interpret_floatarith_sin interpret_floatarith.simps(5)[of a])
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3679
next
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3680
  case (Power a n)
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3681
  thus ?case
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  3682
    by (cases n) (auto intro!: derivative_eq_intros simp del: power_Suc)
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3683
next
63263
c6c95d64607a approximation, derivative, and continuity of floor and ceiling
immler
parents: 63248
diff changeset
  3684
  case (Floor a)
c6c95d64607a approximation, derivative, and continuity of floor and ceiling
immler
parents: 63248
diff changeset
  3685
  thus ?case
c6c95d64607a approximation, derivative, and continuity of floor and ceiling
immler
parents: 63248
diff changeset
  3686
    by (auto intro!: derivative_eq_intros DERIV_isCont floor_has_real_derivative)
c6c95d64607a approximation, derivative, and continuity of floor and ceiling
immler
parents: 63248
diff changeset
  3687
next
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3688
  case (Ln a)
56382
5a50109d51ab fix #0556204bc230
hoelzl
parents: 56381
diff changeset
  3689
  thus ?case by (auto intro!: derivative_eq_intros simp add: divide_inverse)
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3690
next
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3691
  case (Var i)
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
  3692
  thus ?case using \<open>n < length vs\<close> by auto
62200
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3693
next
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3694
  case (Powr a b)
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3695
  note [derivative_intros] = has_real_derivative_powr'
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3696
  from Powr show ?case
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3697
    by (auto intro!: derivative_eq_intros simp: field_simps)
56381
0556204bc230 merged DERIV_intros, has_derivative_intros into derivative_intros
hoelzl
parents: 56195
diff changeset
  3698
qed (auto intro!: derivative_eq_intros)
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3699
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3700
declare approx.simps[simp del]
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3701
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3702
fun isDERIV_approx :: "nat \<Rightarrow> nat \<Rightarrow> floatarith \<Rightarrow> (float * float) option list \<Rightarrow> bool" where
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3703
"isDERIV_approx prec x (Add a b) vs         = (isDERIV_approx prec x a vs \<and> isDERIV_approx prec x b vs)" |
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3704
"isDERIV_approx prec x (Mult a b) vs        = (isDERIV_approx prec x a vs \<and> isDERIV_approx prec x b vs)" |
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3705
"isDERIV_approx prec x (Minus a) vs         = isDERIV_approx prec x a vs" |
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3706
"isDERIV_approx prec x (Inverse a) vs       =
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3707
  (isDERIV_approx prec x a vs \<and> (case approx prec a vs of Some (l, u) \<Rightarrow> 0 < l \<or> u < 0 | None \<Rightarrow> False))" |
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3708
"isDERIV_approx prec x (Cos a) vs           = isDERIV_approx prec x a vs" |
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3709
"isDERIV_approx prec x (Arctan a) vs        = isDERIV_approx prec x a vs" |
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3710
"isDERIV_approx prec x (Min a b) vs         = False" |
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3711
"isDERIV_approx prec x (Max a b) vs         = False" |
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3712
"isDERIV_approx prec x (Abs a) vs           = False" |
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3713
"isDERIV_approx prec x Pi vs                = True" |
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3714
"isDERIV_approx prec x (Sqrt a) vs          =
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3715
  (isDERIV_approx prec x a vs \<and> (case approx prec a vs of Some (l, u) \<Rightarrow> 0 < l | None \<Rightarrow> False))" |
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3716
"isDERIV_approx prec x (Exp a) vs           = isDERIV_approx prec x a vs" |
62200
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3717
"isDERIV_approx prec x (Powr a b) vs        =
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3718
  (isDERIV_approx prec x a vs \<and> isDERIV_approx prec x b vs \<and> (case approx prec a vs of Some (l, u) \<Rightarrow> 0 < l | None \<Rightarrow> False))" |
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3719
"isDERIV_approx prec x (Ln a) vs            =
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3720
  (isDERIV_approx prec x a vs \<and> (case approx prec a vs of Some (l, u) \<Rightarrow> 0 < l | None \<Rightarrow> False))" |
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3721
"isDERIV_approx prec x (Power a 0) vs       = True" |
63263
c6c95d64607a approximation, derivative, and continuity of floor and ceiling
immler
parents: 63248
diff changeset
  3722
"isDERIV_approx prec x (Floor a) vs         =
c6c95d64607a approximation, derivative, and continuity of floor and ceiling
immler
parents: 63248
diff changeset
  3723
  (isDERIV_approx prec x a vs \<and> (case approx prec a vs of Some (l, u) \<Rightarrow> l > floor u \<and> u < ceiling l | None \<Rightarrow> False))" |
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3724
"isDERIV_approx prec x (Power a (Suc n)) vs = isDERIV_approx prec x a vs" |
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3725
"isDERIV_approx prec x (Num f) vs           = True" |
62200
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3726
"isDERIV_approx prec x (Var n) vs           = True"
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3727
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3728
lemma isDERIV_approx:
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3729
  assumes "bounded_by xs vs"
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3730
    and isDERIV_approx: "isDERIV_approx prec x f vs"
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3731
  shows "isDERIV x f xs"
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3732
  using isDERIV_approx
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3733
proof (induct f)
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3734
  case (Inverse a)
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3735
  then obtain l u where approx_Some: "Some (l, u) = approx prec a vs"
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3736
    and *: "0 < l \<or> u < 0"
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3737
    by (cases "approx prec a vs") auto
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
  3738
  with approx[OF \<open>bounded_by xs vs\<close> approx_Some]
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  3739
  have "interpret_floatarith a xs \<noteq> 0" by auto
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3740
  thus ?case using Inverse by auto
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3741
next
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3742
  case (Ln a)
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3743
  then obtain l u where approx_Some: "Some (l, u) = approx prec a vs"
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3744
    and *: "0 < l"
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3745
    by (cases "approx prec a vs") auto
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
  3746
  with approx[OF \<open>bounded_by xs vs\<close> approx_Some]
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  3747
  have "0 < interpret_floatarith a xs" by auto
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3748
  thus ?case using Ln by auto
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3749
next
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3750
  case (Sqrt a)
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3751
  then obtain l u where approx_Some: "Some (l, u) = approx prec a vs"
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3752
    and *: "0 < l"
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3753
    by (cases "approx prec a vs") auto
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
  3754
  with approx[OF \<open>bounded_by xs vs\<close> approx_Some]
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  3755
  have "0 < interpret_floatarith a xs" by auto
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3756
  thus ?case using Sqrt by auto
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3757
next
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3758
  case (Power a n)
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3759
  thus ?case by (cases n) auto
62200
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3760
next
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3761
  case (Powr a b)
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3762
  from Powr obtain l1 u1 where a: "Some (l1, u1) = approx prec a vs" and pos: "0 < l1"
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3763
    by (cases "approx prec a vs") auto
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3764
  with approx[OF \<open>bounded_by xs vs\<close> a]
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3765
    have "0 < interpret_floatarith a xs" by auto
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3766
  with Powr show ?case by auto
63263
c6c95d64607a approximation, derivative, and continuity of floor and ceiling
immler
parents: 63248
diff changeset
  3767
next
c6c95d64607a approximation, derivative, and continuity of floor and ceiling
immler
parents: 63248
diff changeset
  3768
  case (Floor a)
c6c95d64607a approximation, derivative, and continuity of floor and ceiling
immler
parents: 63248
diff changeset
  3769
  then obtain l u where approx_Some: "Some (l, u) = approx prec a vs"
c6c95d64607a approximation, derivative, and continuity of floor and ceiling
immler
parents: 63248
diff changeset
  3770
    and "real_of_int \<lfloor>real_of_float u\<rfloor> < real_of_float l" "real_of_float u < real_of_int \<lceil>real_of_float l\<rceil>"
c6c95d64607a approximation, derivative, and continuity of floor and ceiling
immler
parents: 63248
diff changeset
  3771
    and "isDERIV x a xs"
c6c95d64607a approximation, derivative, and continuity of floor and ceiling
immler
parents: 63248
diff changeset
  3772
    by (cases "approx prec a vs") auto
c6c95d64607a approximation, derivative, and continuity of floor and ceiling
immler
parents: 63248
diff changeset
  3773
  with approx[OF \<open>bounded_by xs vs\<close> approx_Some] le_floor_iff
c6c95d64607a approximation, derivative, and continuity of floor and ceiling
immler
parents: 63248
diff changeset
  3774
  show ?case
c6c95d64607a approximation, derivative, and continuity of floor and ceiling
immler
parents: 63248
diff changeset
  3775
    by (force elim!: Ints_cases)
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3776
qed auto
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3777
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3778
lemma bounded_by_update_var:
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3779
  assumes "bounded_by xs vs"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3780
    and "vs ! i = Some (l, u)"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  3781
    and bnd: "x \<in> { real_of_float l .. real_of_float u }"
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3782
  shows "bounded_by (xs[i := x]) vs"
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3783
proof (cases "i < length xs")
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3784
  case False
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3785
  thus ?thesis
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3786
    using \<open>bounded_by xs vs\<close> by auto
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3787
next
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3788
  case True
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3789
  let ?xs = "xs[i := x]"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3790
  from True have "i < length ?xs" by auto
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  3791
  have "case vs ! j of None \<Rightarrow> True | Some (l, u) \<Rightarrow> ?xs ! j \<in> {real_of_float l .. real_of_float u}"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3792
    if "j < length vs" for j
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3793
  proof (cases "vs ! j")
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3794
    case None
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3795
    then show ?thesis by simp
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3796
  next
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3797
    case (Some b)
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3798
    thus ?thesis
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3799
    proof (cases "i = j")
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3800
      case True
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3801
      thus ?thesis using \<open>vs ! i = Some (l, u)\<close> Some and bnd \<open>i < length ?xs\<close>
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3802
        by auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3803
    next
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3804
      case False
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3805
      thus ?thesis
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3806
        using \<open>bounded_by xs vs\<close>[THEN bounded_byE, OF \<open>j < length vs\<close>] Some by auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3807
    qed
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3808
  qed
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3809
  thus ?thesis
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3810
    unfolding bounded_by_def by auto
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3811
qed
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3812
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3813
lemma isDERIV_approx':
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3814
  assumes "bounded_by xs vs"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3815
    and vs_x: "vs ! x = Some (l, u)"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  3816
    and X_in: "X \<in> {real_of_float l .. real_of_float u}"
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3817
    and approx: "isDERIV_approx prec x f vs"
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3818
  shows "isDERIV x f (xs[x := X])"
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3819
proof -
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3820
  from bounded_by_update_var[OF \<open>bounded_by xs vs\<close> vs_x X_in] approx
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3821
  show ?thesis by (rule isDERIV_approx)
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3822
qed
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3823
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3824
lemma DERIV_approx:
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3825
  assumes "n < length xs"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3826
    and bnd: "bounded_by xs vs"
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3827
    and isD: "isDERIV_approx prec n f vs"
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3828
    and app: "Some (l, u) = approx prec (DERIV_floatarith n f) vs" (is "_ = approx _ ?D _")
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  3829
  shows "\<exists>(x::real). l \<le> x \<and> x \<le> u \<and>
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3830
             DERIV (\<lambda> x. interpret_floatarith f (xs[n := x])) (xs!n) :> x"
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3831
         (is "\<exists> x. _ \<and> _ \<and> DERIV (?i f) _ :> _")
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3832
proof (rule exI[of _ "?i ?D (xs!n)"], rule conjI[OF _ conjI])
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3833
  let "?i f" = "\<lambda>x. interpret_floatarith f (xs[n := x])"
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3834
  from approx[OF bnd app]
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  3835
  show "l \<le> ?i ?D (xs!n)" and "?i ?D (xs!n) \<le> u"
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
  3836
    using \<open>n < length xs\<close> by auto
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
  3837
  from DERIV_floatarith[OF \<open>n < length xs\<close>, of f "xs!n"] isDERIV_approx[OF bnd isD]
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3838
  show "DERIV (?i f) (xs!n) :> (?i ?D (xs!n))"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3839
    by simp
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3840
qed
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3841
62200
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3842
lemma lift_bin_aux:
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3843
  assumes lift_bin_Some: "Some (l, u) = lift_bin a b f"
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3844
  obtains l1 u1 l2 u2
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3845
  where "a = Some (l1, u1)"
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3846
    and "b = Some (l2, u2)"
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3847
    and "f l1 u1 l2 u2 = Some (l, u)"
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3848
  using assms by (cases a, simp, cases b, simp, auto)
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3849
62200
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3850
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3851
fun approx_tse where
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3852
"approx_tse prec n 0 c k f bs = approx prec f bs" |
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3853
"approx_tse prec n (Suc s) c k f bs =
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3854
  (if isDERIV_approx prec n f bs then
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3855
    lift_bin (approx prec f (bs[n := Some (c,c)]))
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3856
             (approx_tse prec n s c (Suc k) (DERIV_floatarith n f) bs)
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3857
             (\<lambda> l1 u1 l2 u2. approx prec
32919
37adfa07b54b approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
hoelzl
parents: 32650
diff changeset
  3858
                 (Add (Var 0)
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3859
                      (Mult (Inverse (Num (Float (int k) 0)))
32919
37adfa07b54b approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
hoelzl
parents: 32650
diff changeset
  3860
                                 (Mult (Add (Var (Suc (Suc 0))) (Minus (Num c)))
37adfa07b54b approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
hoelzl
parents: 32650
diff changeset
  3861
                                       (Var (Suc 0))))) [Some (l1, u1), Some (l2, u2), bs!n])
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3862
  else approx prec f bs)"
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3864
lemma bounded_by_Cons:
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3865
  assumes bnd: "bounded_by xs vs"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  3866
    and x: "x \<in> { real_of_float l .. real_of_float u }"
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3867
  shows "bounded_by (x#xs) ((Some (l, u))#vs)"
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3868
proof -
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  3869
  have "case ((Some (l,u))#vs) ! i of Some (l, u) \<Rightarrow> (x#xs)!i \<in> { real_of_float l .. real_of_float u } | None \<Rightarrow> True"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3870
    if *: "i < length ((Some (l, u))#vs)" for i
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3871
  proof (cases i)
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3872
    case 0
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3873
    with x show ?thesis by auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3874
  next
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3875
    case (Suc i)
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3876
    with * have "i < length vs" by auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3877
    from bnd[THEN bounded_byE, OF this]
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3878
    show ?thesis unfolding Suc nth_Cons_Suc .
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3879
  qed
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3880
  thus ?thesis
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3881
    by (auto simp add: bounded_by_def)
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3882
qed
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3883
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3884
lemma approx_tse_generic:
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3885
  assumes "bounded_by xs vs"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3886
    and bnd_c: "bounded_by (xs[x := c]) vs"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3887
    and "x < length vs" and "x < length xs"
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3888
    and bnd_x: "vs ! x = Some (lx, ux)"
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3889
    and ate: "Some (l, u) = approx_tse prec x s c k f vs"
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  3890
  shows "\<exists> n. (\<forall> m < n. \<forall> (z::real) \<in> {lx .. ux}.
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3891
      DERIV (\<lambda> y. interpret_floatarith ((DERIV_floatarith x ^^ m) f) (xs[x := y])) z :>
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3892
            (interpret_floatarith ((DERIV_floatarith x ^^ (Suc m)) f) (xs[x := z])))
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  3893
   \<and> (\<forall> (t::real) \<in> {lx .. ux}.  (\<Sum> i = 0..<n. inverse (real (\<Prod> j \<in> {k..<k+i}. j)) *
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  3894
                  interpret_floatarith ((DERIV_floatarith x ^^ i) f) (xs[x := c]) *
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  3895
                  (xs!x - c)^i) +
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3896
      inverse (real (\<Prod> j \<in> {k..<k+n}. j)) *
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3897
      interpret_floatarith ((DERIV_floatarith x ^^ n) f) (xs[x := t]) *
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  3898
      (xs!x - c)^n \<in> {l .. u})" (is "\<exists> n. ?taylor f k l u n")
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3899
  using ate
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3900
proof (induct s arbitrary: k f l u)
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3901
  case 0
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3902
  {
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3903
    fix t::real assume "t \<in> {lx .. ux}"
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
  3904
    note bounded_by_update_var[OF \<open>bounded_by xs vs\<close> bnd_x this]
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3905
    from approx[OF this 0[unfolded approx_tse.simps]]
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  3906
    have "(interpret_floatarith f (xs[x := t])) \<in> {l .. u}"
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3907
      by (auto simp add: algebra_simps)
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3908
  }
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3909
  thus ?case by (auto intro!: exI[of _ 0])
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3910
next
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3911
  case (Suc s)
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3912
  show ?case
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3913
  proof (cases "isDERIV_approx prec x f vs")
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3914
    case False
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3915
    note ap = Suc.prems[unfolded approx_tse.simps if_not_P[OF False]]
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3916
    {
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3917
      fix t::real assume "t \<in> {lx .. ux}"
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
  3918
      note bounded_by_update_var[OF \<open>bounded_by xs vs\<close> bnd_x this]
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3919
      from approx[OF this ap]
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  3920
      have "(interpret_floatarith f (xs[x := t])) \<in> {l .. u}"
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  3921
        by (auto simp add: algebra_simps)
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3922
    }
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3923
    thus ?thesis by (auto intro!: exI[of _ 0])
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3924
  next
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3925
    case True
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3926
    with Suc.prems
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3927
    obtain l1 u1 l2 u2
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3928
      where a: "Some (l1, u1) = approx prec f (vs[x := Some (c,c)])"
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3929
        and ate: "Some (l2, u2) = approx_tse prec x s c (Suc k) (DERIV_floatarith x f) vs"
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3930
        and final: "Some (l, u) = approx prec
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3931
          (Add (Var 0)
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3932
               (Mult (Inverse (Num (Float (int k) 0)))
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3933
                     (Mult (Add (Var (Suc (Suc 0))) (Minus (Num c)))
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3934
                           (Var (Suc 0))))) [Some (l1, u1), Some (l2, u2), vs!x]"
62200
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  3935
      by (auto elim!: lift_bin_aux)
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3936
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
  3937
    from bnd_c \<open>x < length xs\<close>
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  3938
    have bnd: "bounded_by (xs[x:=c]) (vs[x:= Some (c,c)])"
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3939
      by (auto intro!: bounded_by_update)
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3940
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3941
    from approx[OF this a]
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  3942
    have f_c: "interpret_floatarith ((DERIV_floatarith x ^^ 0) f) (xs[x := c]) \<in> { l1 .. u1 }"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  3943
              (is "?f 0 (real_of_float c) \<in> _")
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3944
      by auto
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3945
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3946
    have funpow_Suc[symmetric]: "(f ^^ Suc n) x = (f ^^ n) (f x)"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3947
      for f :: "'a \<Rightarrow> 'a" and n :: nat and x :: 'a
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3948
      by (induct n) auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3949
    from Suc.hyps[OF ate, unfolded this] obtain n
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3950
      where DERIV_hyp: "\<And>m z. \<lbrakk> m < n ; (z::real) \<in> { lx .. ux } \<rbrakk> \<Longrightarrow>
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3951
        DERIV (?f (Suc m)) z :> ?f (Suc (Suc m)) z"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  3952
      and hyp: "\<forall>t \<in> {real_of_float lx .. real_of_float ux}.
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3953
        (\<Sum> i = 0..<n. inverse (real (\<Prod> j \<in> {Suc k..<Suc k + i}. j)) * ?f (Suc i) c * (xs!x - c)^i) +
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3954
          inverse (real (\<Prod> j \<in> {Suc k..<Suc k + n}. j)) * ?f (Suc n) t * (xs!x - c)^n \<in> {l2 .. u2}"
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3955
          (is "\<forall> t \<in> _. ?X (Suc k) f n t \<in> _")
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3956
      by blast
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3957
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3958
    have DERIV: "DERIV (?f m) z :> ?f (Suc m) z"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3959
      if "m < Suc n" and bnd_z: "z \<in> { lx .. ux }" for m and z::real
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3960
    proof (cases m)
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3961
      case 0
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3962
      with DERIV_floatarith[OF \<open>x < length xs\<close>
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3963
        isDERIV_approx'[OF \<open>bounded_by xs vs\<close> bnd_x bnd_z True]]
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3964
      show ?thesis by simp
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3965
    next
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3966
      case (Suc m')
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3967
      hence "m' < n"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3968
        using \<open>m < Suc n\<close> by auto
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3969
      from DERIV_hyp[OF this bnd_z] show ?thesis
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3970
        using Suc by simp
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3971
    qed
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3972
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3973
    have "\<And>k i. k < i \<Longrightarrow> {k ..< i} = insert k {Suc k ..< i}" by auto
64272
f76b6dda2e56 setprod -> prod
nipkow
parents: 64267
diff changeset
  3974
    hence prod_head_Suc: "\<And>k i. \<Prod>{k ..< k + Suc i} = k * \<Prod>{Suc k ..< Suc k + i}"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  3975
      by auto
64267
b9a1486e79be setsum -> sum
nipkow
parents: 64246
diff changeset
  3976
    have sum_move0: "\<And>k F. sum F {0..<Suc k} = F 0 + sum (\<lambda> k. F (Suc k)) {0..<k}"
b9a1486e79be setsum -> sum
nipkow
parents: 64246
diff changeset
  3977
      unfolding sum_shift_bounds_Suc_ivl[symmetric]
b9a1486e79be setsum -> sum
nipkow
parents: 64246
diff changeset
  3978
      unfolding sum_head_upt_Suc[OF zero_less_Suc] ..
63040
eb4ddd18d635 eliminated old 'def';
wenzelm
parents: 62390
diff changeset
  3979
    define C where "C = xs!x - c"
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  3980
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3981
    {
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3982
      fix t::real assume t: "t \<in> {lx .. ux}"
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3983
      hence "bounded_by [xs!x] [vs!x]"
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
  3984
        using \<open>bounded_by xs vs\<close>[THEN bounded_byE, OF \<open>x < length vs\<close>]
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  3985
        by (cases "vs!x", auto simp add: bounded_by_def)
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3986
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3987
      with hyp[THEN bspec, OF t] f_c
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  3988
      have "bounded_by [?f 0 c, ?X (Suc k) f n t, xs!x] [Some (l1, u1), Some (l2, u2), vs!x]"
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  3989
        by (auto intro!: bounded_by_Cons)
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3990
      from approx[OF this final, unfolded atLeastAtMost_iff[symmetric]]
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  3991
      have "?X (Suc k) f n t * (xs!x - real_of_float c) * inverse k + ?f 0 c \<in> {l .. u}"
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  3992
        by (auto simp add: algebra_simps)
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  3993
      also have "?X (Suc k) f n t * (xs!x - real_of_float c) * inverse (real k) + ?f 0 c =
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  3994
               (\<Sum> i = 0..<Suc n. inverse (real (\<Prod> j \<in> {k..<k+i}. j)) * ?f i c * (xs!x - c)^i) +
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  3995
               inverse (real (\<Prod> j \<in> {k..<k+Suc n}. j)) * ?f (Suc n) t * (xs!x - c)^Suc n" (is "_ = ?T")
64272
f76b6dda2e56 setprod -> prod
nipkow
parents: 64267
diff changeset
  3996
        unfolding funpow_Suc C_def[symmetric] sum_move0 prod_head_Suc
35082
96a21dd3b349 rely less on ordered rewriting
haftmann
parents: 35028
diff changeset
  3997
        by (auto simp add: algebra_simps)
64267
b9a1486e79be setsum -> sum
nipkow
parents: 64246
diff changeset
  3998
          (simp only: mult.left_commute [of _ "inverse (real k)"] sum_distrib_left [symmetric])
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3999
      finally have "?T \<in> {l .. u}" .
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  4000
    }
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4001
    thus ?thesis using DERIV by blast
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4002
  qed
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4003
qed
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4004
64272
f76b6dda2e56 setprod -> prod
nipkow
parents: 64267
diff changeset
  4005
lemma prod_fact: "real (\<Prod> {1..<1 + k}) = fact (k :: nat)"
f76b6dda2e56 setprod -> prod
nipkow
parents: 64267
diff changeset
  4006
  by (simp add: fact_prod atLeastLessThanSuc_atLeastAtMost)
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4007
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4008
lemma approx_tse:
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4009
  assumes "bounded_by xs vs"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  4010
    and bnd_x: "vs ! x = Some (lx, ux)"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  4011
    and bnd_c: "real_of_float c \<in> {lx .. ux}"
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  4012
    and "x < length vs" and "x < length xs"
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  4013
    and ate: "Some (l, u) = approx_tse prec x s c 1 f vs"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  4014
  shows "interpret_floatarith f xs \<in> {l .. u}"
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4015
proof -
63040
eb4ddd18d635 eliminated old 'def';
wenzelm
parents: 62390
diff changeset
  4016
  define F where [abs_def]: "F n z =
eb4ddd18d635 eliminated old 'def';
wenzelm
parents: 62390
diff changeset
  4017
    interpret_floatarith ((DERIV_floatarith x ^^ n) f) (xs[x := z])" for n z
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4018
  hence F0: "F 0 = (\<lambda> z. interpret_floatarith f (xs[x := z]))" by auto
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4019
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  4020
  hence "bounded_by (xs[x := c]) vs" and "x < length vs" "x < length xs"
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
  4021
    using \<open>bounded_by xs vs\<close> bnd_x bnd_c \<open>x < length vs\<close> \<open>x < length xs\<close>
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4022
    by (auto intro!: bounded_by_update_var)
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4023
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
  4024
  from approx_tse_generic[OF \<open>bounded_by xs vs\<close> this bnd_x ate]
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4025
  obtain n
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  4026
    where DERIV: "\<forall> m z. m < n \<and> real_of_float lx \<le> z \<and> z \<le> real_of_float ux \<longrightarrow> DERIV (F m) z :> F (Suc m) z"
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  4027
    and hyp: "\<And> (t::real). t \<in> {lx .. ux} \<Longrightarrow>
59730
b7c394c7a619 The factorial function, "fact", now has type "nat => 'a"
paulson <lp15@cam.ac.uk>
parents: 59658
diff changeset
  4028
           (\<Sum> j = 0..<n. inverse(fact j) * F j c * (xs!x - c)^j) +
b7c394c7a619 The factorial function, "fact", now has type "nat => 'a"
paulson <lp15@cam.ac.uk>
parents: 59658
diff changeset
  4029
             inverse ((fact n)) * F n t * (xs!x - c)^n
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  4030
             \<in> {l .. u}" (is "\<And> t. _ \<Longrightarrow> ?taylor t \<in> _")
64272
f76b6dda2e56 setprod -> prod
nipkow
parents: 64267
diff changeset
  4031
    unfolding F_def atLeastAtMost_iff[symmetric] prod_fact
59730
b7c394c7a619 The factorial function, "fact", now has type "nat => 'a"
paulson <lp15@cam.ac.uk>
parents: 59658
diff changeset
  4032
    by blast
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4033
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  4034
  have bnd_xs: "xs ! x \<in> { lx .. ux }"
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
  4035
    using \<open>bounded_by xs vs\<close>[THEN bounded_byE, OF \<open>x < length vs\<close>] bnd_x by auto
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4036
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4037
  show ?thesis
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4038
  proof (cases n)
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  4039
    case 0
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  4040
    thus ?thesis
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  4041
      using hyp[OF bnd_xs] unfolding F_def by auto
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4042
  next
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4043
    case (Suc n')
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4044
    show ?thesis
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  4045
    proof (cases "xs ! x = c")
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4046
      case True
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4047
      from True[symmetric] hyp[OF bnd_xs] Suc show ?thesis
64267
b9a1486e79be setsum -> sum
nipkow
parents: 64246
diff changeset
  4048
        unfolding F_def Suc sum_head_upt_Suc[OF zero_less_Suc] sum_shift_bounds_Suc_ivl
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  4049
        by auto
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4050
    next
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4051
      case False
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  4052
      have "lx \<le> real_of_float c" "real_of_float c \<le> ux" "lx \<le> xs!x" "xs!x \<le> ux"
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
  4053
        using Suc bnd_c \<open>bounded_by xs vs\<close>[THEN bounded_byE, OF \<open>x < length vs\<close>] bnd_x by auto
63570
1826a90b9cbc simplified theory structure;
wenzelm
parents: 63417
diff changeset
  4054
      from taylor[OF zero_less_Suc, of F, OF F0 DERIV[unfolded Suc] this False]
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  4055
      obtain t::real where t_bnd: "if xs ! x < c then xs ! x < t \<and> t < c else c < t \<and> t < xs ! x"
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  4056
        and fl_eq: "interpret_floatarith f (xs[x := xs ! x]) =
59730
b7c394c7a619 The factorial function, "fact", now has type "nat => 'a"
paulson <lp15@cam.ac.uk>
parents: 59658
diff changeset
  4057
           (\<Sum>m = 0..<Suc n'. F m c / (fact m) * (xs ! x - c) ^ m) +
b7c394c7a619 The factorial function, "fact", now has type "nat => 'a"
paulson <lp15@cam.ac.uk>
parents: 59658
diff changeset
  4058
           F (Suc n') t / (fact (Suc n')) * (xs ! x - c) ^ Suc n'"
56195
c7dfd924a165 adapt to Isabelle/c726ecfb22b6
huffman
parents: 56073
diff changeset
  4059
        unfolding atLeast0LessThan by blast
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4060
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  4061
      from t_bnd bnd_xs bnd_c have *: "t \<in> {lx .. ux}"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  4062
        by (cases "xs ! x < c") auto
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4063
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4064
      have "interpret_floatarith f (xs[x := xs ! x]) = ?taylor t"
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  4065
        unfolding fl_eq Suc by (auto simp add: algebra_simps divide_inverse)
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  4066
      also have "\<dots> \<in> {l .. u}"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  4067
        using * by (rule hyp)
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  4068
      finally show ?thesis
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  4069
        by simp
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4070
    qed
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4071
  qed
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4072
qed
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4073
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4074
fun approx_tse_form' where
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4075
"approx_tse_form' prec t f 0 l u cmp =
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
  4076
  (case approx_tse prec 0 t ((l + u) * Float 1 (- 1)) 1 f [Some (l, u)]
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4077
     of Some (l, u) \<Rightarrow> cmp l u | None \<Rightarrow> False)" |
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4078
"approx_tse_form' prec t f (Suc s) l u cmp =
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
  4079
  (let m = (l + u) * Float 1 (- 1)
32919
37adfa07b54b approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
hoelzl
parents: 32650
diff changeset
  4080
   in (if approx_tse_form' prec t f s l m cmp then
37adfa07b54b approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
hoelzl
parents: 32650
diff changeset
  4081
      approx_tse_form' prec t f s m u cmp else False))"
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4082
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4083
lemma approx_tse_form':
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  4084
  fixes x :: real
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  4085
  assumes "approx_tse_form' prec t f s l u cmp"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  4086
    and "x \<in> {l .. u}"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  4087
  shows "\<exists>l' u' ly uy. x \<in> {l' .. u'} \<and> real_of_float l \<le> l' \<and> u' \<le> real_of_float u \<and> cmp ly uy \<and>
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  4088
    approx_tse prec 0 t ((l' + u') * Float 1 (- 1)) 1 f [Some (l', u')] = Some (ly, uy)"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  4089
  using assms
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  4090
proof (induct s arbitrary: l u)
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4091
  case 0
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4092
  then obtain ly uy
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
  4093
    where *: "approx_tse prec 0 t ((l + u) * Float 1 (- 1)) 1 f [Some (l, u)] = Some (ly, uy)"
55413
a8e96847523c adapted theories to '{case,rec}_{list,option}' names
blanchet
parents: 54782
diff changeset
  4094
    and **: "cmp ly uy" by (auto elim!: case_optionE)
46545
haftmann
parents: 45481
diff changeset
  4095
  with 0 show ?case by auto
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4096
next
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4097
  case (Suc s)
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
  4098
  let ?m = "(l + u) * Float 1 (- 1)"
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4099
  from Suc.prems
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4100
  have l: "approx_tse_form' prec t f s l ?m cmp"
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4101
    and u: "approx_tse_form' prec t f s ?m u cmp"
32919
37adfa07b54b approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
hoelzl
parents: 32650
diff changeset
  4102
    by (auto simp add: Let_def lazy_conj)
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4103
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  4104
  have m_l: "real_of_float l \<le> ?m" and m_u: "?m \<le> real_of_float u"
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  4105
    unfolding less_eq_float_def using Suc.prems by auto
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  4106
  with \<open>x \<in> { l .. u }\<close> consider "x \<in> { l .. ?m}" | "x \<in> {?m .. u}"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  4107
    by atomize_elim auto
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4108
  thus ?case
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  4109
  proof cases
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  4110
    case 1
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4111
    from Suc.hyps[OF l this]
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  4112
    obtain l' u' ly uy where
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  4113
      "x \<in> {l' .. u'} \<and> real_of_float l \<le> l' \<and> real_of_float u' \<le> ?m \<and> cmp ly uy \<and>
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  4114
        approx_tse prec 0 t ((l' + u') * Float 1 (- 1)) 1 f [Some (l', u')] = Some (ly, uy)"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  4115
      by blast
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  4116
    with m_u show ?thesis
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  4117
      by (auto intro!: exI)
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4118
  next
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  4119
    case 2
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4120
    from Suc.hyps[OF u this]
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  4121
    obtain l' u' ly uy where
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  4122
      "x \<in> { l' .. u' } \<and> ?m \<le> real_of_float l' \<and> u' \<le> real_of_float u \<and> cmp ly uy \<and>
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  4123
        approx_tse prec 0 t ((l' + u') * Float 1 (- 1)) 1 f [Some (l', u')] = Some (ly, uy)"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  4124
      by blast
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  4125
    with m_u show ?thesis
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  4126
      by (auto intro!: exI)
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4127
  qed
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4128
qed
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4129
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4130
lemma approx_tse_form'_less:
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  4131
  fixes x :: real
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4132
  assumes tse: "approx_tse_form' prec t (Add a (Minus b)) s l u (\<lambda> l u. 0 < l)"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  4133
    and x: "x \<in> {l .. u}"
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4134
  shows "interpret_floatarith b [x] < interpret_floatarith a [x]"
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4135
proof -
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4136
  from approx_tse_form'[OF tse x]
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4137
  obtain l' u' ly uy
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  4138
    where x': "x \<in> {l' .. u'}"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  4139
    and "real_of_float l \<le> real_of_float l'"
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  4140
    and "real_of_float u' \<le> real_of_float u" and "0 < ly"
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
  4141
    and tse: "approx_tse prec 0 t ((l' + u') * Float 1 (- 1)) 1 (Add a (Minus b)) [Some (l', u')] = Some (ly, uy)"
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4142
    by blast
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4143
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  4144
  hence "bounded_by [x] [Some (l', u')]"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  4145
    by (auto simp add: bounded_by_def)
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4146
  from approx_tse[OF this _ _ _ _ tse[symmetric], of l' u'] x'
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  4147
  have "ly \<le> interpret_floatarith a [x] - interpret_floatarith b [x]"
54230
b1d955791529 more simplification rules on unary and binary minus
haftmann
parents: 53077
diff changeset
  4148
    by auto
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  4149
  from order_less_le_trans[OF _ this, of 0] \<open>0 < ly\<close> show ?thesis
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  4150
    by auto
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4151
qed
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4152
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4153
lemma approx_tse_form'_le:
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  4154
  fixes x :: real
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4155
  assumes tse: "approx_tse_form' prec t (Add a (Minus b)) s l u (\<lambda> l u. 0 \<le> l)"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  4156
    and x: "x \<in> {l .. u}"
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4157
  shows "interpret_floatarith b [x] \<le> interpret_floatarith a [x]"
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4158
proof -
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4159
  from approx_tse_form'[OF tse x]
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4160
  obtain l' u' ly uy
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  4161
    where x': "x \<in> {l' .. u'}"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  4162
    and "l \<le> real_of_float l'"
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60680
diff changeset
  4163
    and "real_of_float u' \<le> u" and "0 \<le> ly"
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
  4164
    and tse: "approx_tse prec 0 t ((l' + u') * Float 1 (- 1)) 1 (Add a (Minus b)) [Some (l', u')] = Some (ly, uy)"
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4165
    by blast
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4166
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4167
  hence "bounded_by [x] [Some (l', u')]" by (auto simp add: bounded_by_def)
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4168
  from approx_tse[OF this _ _ _ _ tse[symmetric], of l' u'] x'
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  4169
  have "ly \<le> interpret_floatarith a [x] - interpret_floatarith b [x]"
54230
b1d955791529 more simplification rules on unary and binary minus
haftmann
parents: 53077
diff changeset
  4170
    by auto
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  4171
  from order_trans[OF _ this, of 0] \<open>0 \<le> ly\<close> show ?thesis
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  4172
    by auto
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4173
qed
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4174
58986
ec7373051a6c disjunction and conjunction for forms
immler
parents: 58985
diff changeset
  4175
fun approx_tse_concl where
ec7373051a6c disjunction and conjunction for forms
immler
parents: 58985
diff changeset
  4176
"approx_tse_concl prec t (Less lf rt) s l u l' u' \<longleftrightarrow>
ec7373051a6c disjunction and conjunction for forms
immler
parents: 58985
diff changeset
  4177
    approx_tse_form' prec t (Add rt (Minus lf)) s l u' (\<lambda> l u. 0 < l)" |
ec7373051a6c disjunction and conjunction for forms
immler
parents: 58985
diff changeset
  4178
"approx_tse_concl prec t (LessEqual lf rt) s l u l' u' \<longleftrightarrow>
ec7373051a6c disjunction and conjunction for forms
immler
parents: 58985
diff changeset
  4179
    approx_tse_form' prec t (Add rt (Minus lf)) s l u' (\<lambda> l u. 0 \<le> l)" |
ec7373051a6c disjunction and conjunction for forms
immler
parents: 58985
diff changeset
  4180
"approx_tse_concl prec t (AtLeastAtMost x lf rt) s l u l' u' \<longleftrightarrow>
ec7373051a6c disjunction and conjunction for forms
immler
parents: 58985
diff changeset
  4181
    (if approx_tse_form' prec t (Add x (Minus lf)) s l u' (\<lambda> l u. 0 \<le> l) then
ec7373051a6c disjunction and conjunction for forms
immler
parents: 58985
diff changeset
  4182
      approx_tse_form' prec t (Add rt (Minus x)) s l u' (\<lambda> l u. 0 \<le> l) else False)" |
ec7373051a6c disjunction and conjunction for forms
immler
parents: 58985
diff changeset
  4183
"approx_tse_concl prec t (Conj f g) s l u l' u' \<longleftrightarrow>
ec7373051a6c disjunction and conjunction for forms
immler
parents: 58985
diff changeset
  4184
    approx_tse_concl prec t f s l u l' u' \<and> approx_tse_concl prec t g s l u l' u'" |
ec7373051a6c disjunction and conjunction for forms
immler
parents: 58985
diff changeset
  4185
"approx_tse_concl prec t (Disj f g) s l u l' u' \<longleftrightarrow>
ec7373051a6c disjunction and conjunction for forms
immler
parents: 58985
diff changeset
  4186
    approx_tse_concl prec t f s l u l' u' \<or> approx_tse_concl prec t g s l u l' u'" |
ec7373051a6c disjunction and conjunction for forms
immler
parents: 58985
diff changeset
  4187
"approx_tse_concl _ _ _ _ _ _ _ _ \<longleftrightarrow> False"
ec7373051a6c disjunction and conjunction for forms
immler
parents: 58985
diff changeset
  4188
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4189
definition
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  4190
  "approx_tse_form prec t s f =
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  4191
    (case f of
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  4192
      Bound x a b f \<Rightarrow>
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  4193
        x = Var 0 \<and>
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  4194
        (case (approx prec a [None], approx prec b [None]) of
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  4195
          (Some (l, u), Some (l', u')) \<Rightarrow> approx_tse_concl prec t f s l u l' u'
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  4196
        | _ \<Rightarrow> False)
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  4197
    | _ \<Rightarrow> False)"
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4198
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4199
lemma approx_tse_form:
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4200
  assumes "approx_tse_form prec t s f"
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4201
  shows "interpret_form f [x]"
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4202
proof (cases f)
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  4203
  case f_def: (Bound i a b f')
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4204
  with assms obtain l u l' u'
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4205
    where a: "approx prec a [None] = Some (l, u)"
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4206
    and b: "approx prec b [None] = Some (l', u')"
55413
a8e96847523c adapted theories to '{case,rec}_{list,option}' names
blanchet
parents: 54782
diff changeset
  4207
    unfolding approx_tse_form_def by (auto elim!: case_optionE)
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4208
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  4209
  from f_def assms have "i = Var 0"
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  4210
    unfolding approx_tse_form_def by auto
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4211
  hence i: "interpret_floatarith i [x] = x" by auto
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4212
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  4213
  {
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  4214
    let ?f = "\<lambda>z. interpret_floatarith z [x]"
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4215
    assume "?f i \<in> { ?f a .. ?f b }"
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4216
    with approx[OF _ a[symmetric], of "[x]"] approx[OF _ b[symmetric], of "[x]"]
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  4217
    have bnd: "x \<in> { l .. u'}" unfolding bounded_by_def i by auto
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4218
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4219
    have "interpret_form f' [x]"
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  4220
      using assms[unfolded f_def]
58986
ec7373051a6c disjunction and conjunction for forms
immler
parents: 58985
diff changeset
  4221
    proof (induct f')
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4222
      case (Less lf rt)
58986
ec7373051a6c disjunction and conjunction for forms
immler
parents: 58985
diff changeset
  4223
      with a b
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4224
      have "approx_tse_form' prec t (Add rt (Minus lf)) s l u' (\<lambda> l u. 0 < l)"
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  4225
        unfolding approx_tse_form_def by auto
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4226
      from approx_tse_form'_less[OF this bnd]
58986
ec7373051a6c disjunction and conjunction for forms
immler
parents: 58985
diff changeset
  4227
      show ?case using Less by auto
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4228
    next
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4229
      case (LessEqual lf rt)
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  4230
      with f_def a b assms
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4231
      have "approx_tse_form' prec t (Add rt (Minus lf)) s l u' (\<lambda> l u. 0 \<le> l)"
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  4232
        unfolding approx_tse_form_def by auto
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4233
      from approx_tse_form'_le[OF this bnd]
58986
ec7373051a6c disjunction and conjunction for forms
immler
parents: 58985
diff changeset
  4234
      show ?case using LessEqual by auto
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4235
    next
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4236
      case (AtLeastAtMost x lf rt)
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  4237
      with f_def a b assms
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4238
      have "approx_tse_form' prec t (Add rt (Minus x)) s l u' (\<lambda> l u. 0 \<le> l)"
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  4239
        and "approx_tse_form' prec t (Add x (Minus lf)) s l u' (\<lambda> l u. 0 \<le> l)"
62390
842917225d56 more canonical names
nipkow
parents: 62200
diff changeset
  4240
        unfolding approx_tse_form_def lazy_conj by (auto split: if_split_asm)
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4241
      from approx_tse_form'_le[OF this(1) bnd] approx_tse_form'_le[OF this(2) bnd]
58986
ec7373051a6c disjunction and conjunction for forms
immler
parents: 58985
diff changeset
  4242
      show ?case using AtLeastAtMost by auto
ec7373051a6c disjunction and conjunction for forms
immler
parents: 58985
diff changeset
  4243
    qed (auto simp: f_def approx_tse_form_def elim!: case_optionE)
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  4244
  }
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  4245
  thus ?thesis unfolding f_def by auto
58986
ec7373051a6c disjunction and conjunction for forms
immler
parents: 58985
diff changeset
  4246
qed (insert assms, auto simp add: approx_tse_form_def)
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  4247
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
  4248
text \<open>@{term approx_form_eval} is only used for the {\tt value}-command.\<close>
32919
37adfa07b54b approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
hoelzl
parents: 32650
diff changeset
  4249
37adfa07b54b approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
hoelzl
parents: 32650
diff changeset
  4250
fun approx_form_eval :: "nat \<Rightarrow> form \<Rightarrow> (float * float) option list \<Rightarrow> (float * float) option list" where
37adfa07b54b approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
hoelzl
parents: 32650
diff changeset
  4251
"approx_form_eval prec (Bound (Var n) a b f) bs =
37adfa07b54b approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
hoelzl
parents: 32650
diff changeset
  4252
   (case (approx prec a bs, approx prec b bs)
37adfa07b54b approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
hoelzl
parents: 32650
diff changeset
  4253
   of (Some (l, _), Some (_, u)) \<Rightarrow> approx_form_eval prec f (bs[n := Some (l, u)])
37adfa07b54b approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
hoelzl
parents: 32650
diff changeset
  4254
    | _ \<Rightarrow> bs)" |
37adfa07b54b approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
hoelzl
parents: 32650
diff changeset
  4255
"approx_form_eval prec (Assign (Var n) a f) bs =
37adfa07b54b approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
hoelzl
parents: 32650
diff changeset
  4256
   (case (approx prec a bs)
37adfa07b54b approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
hoelzl
parents: 32650
diff changeset
  4257
   of (Some (l, u)) \<Rightarrow> approx_form_eval prec f (bs[n := Some (l, u)])
37adfa07b54b approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
hoelzl
parents: 32650
diff changeset
  4258
    | _ \<Rightarrow> bs)" |
37adfa07b54b approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
hoelzl
parents: 32650
diff changeset
  4259
"approx_form_eval prec (Less a b) bs = bs @ [approx prec a bs, approx prec b bs]" |
37adfa07b54b approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
hoelzl
parents: 32650
diff changeset
  4260
"approx_form_eval prec (LessEqual a b) bs = bs @ [approx prec a bs, approx prec b bs]" |
37adfa07b54b approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
hoelzl
parents: 32650
diff changeset
  4261
"approx_form_eval prec (AtLeastAtMost x a b) bs =
37adfa07b54b approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
hoelzl
parents: 32650
diff changeset
  4262
   bs @ [approx prec x bs, approx prec a bs, approx prec b bs]" |
37adfa07b54b approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
hoelzl
parents: 32650
diff changeset
  4263
"approx_form_eval _ _ bs = bs"
37adfa07b54b approximation now fails earlier when using interval splitting; value [approximate] now supports bounded variables; renamed Var -> Atom for better readability
hoelzl
parents: 32650
diff changeset
  4264
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  4265
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
  4266
subsection \<open>Implement proof method \texttt{approximation}\<close>
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  4267
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
  4268
oracle approximation_oracle = \<open>fn (thy, t) =>
36985
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  4269
let
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  4270
  fun bad t = error ("Bad term: " ^ Syntax.string_of_term_global thy t);
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  4271
38716
3c3b4ad683d5 approximation_oracle: actually match true/false in ML, not arbitrary values;
wenzelm
parents: 38558
diff changeset
  4272
  fun term_of_bool true = @{term True}
3c3b4ad683d5 approximation_oracle: actually match true/false in ML, not arbitrary values;
wenzelm
parents: 38558
diff changeset
  4273
    | term_of_bool false = @{term False};
36985
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  4274
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
  4275
  val mk_int = HOLogic.mk_number @{typ int} o @{code integer_of_int};
58988
6ebf918128b9 added quickcheck[approximation]
immler
parents: 58986
diff changeset
  4276
  fun dest_int (@{term int_of_integer} $ j) = @{code int_of_integer} (snd (HOLogic.dest_number j))
6ebf918128b9 added quickcheck[approximation]
immler
parents: 58986
diff changeset
  4277
    | dest_int i = @{code int_of_integer} (snd (HOLogic.dest_number i));
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
  4278
36985
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  4279
  fun term_of_float (@{code Float} (k, l)) =
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
  4280
    @{term Float} $ mk_int k $ mk_int l;
36985
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  4281
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  4282
  fun term_of_float_float_option NONE = @{term "None :: (float \<times> float) option"}
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  4283
    | term_of_float_float_option (SOME ff) = @{term "Some :: float \<times> float \<Rightarrow> _"}
59058
a78612c67ec0 renamed "pairself" to "apply2", in accordance to @{apply 2};
wenzelm
parents: 58988
diff changeset
  4284
        $ HOLogic.mk_prod (apply2 term_of_float ff);
36985
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  4285
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  4286
  val term_of_float_float_option_list =
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  4287
    HOLogic.mk_list @{typ "(float \<times> float) option"} o map term_of_float_float_option;
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  4288
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
  4289
  fun nat_of_term t = @{code nat_of_integer}
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
  4290
    (HOLogic.dest_nat t handle TERM _ => snd (HOLogic.dest_number t));
36985
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  4291
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  4292
  fun float_of_term (@{term Float} $ k $ l) =
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
  4293
        @{code Float} (dest_int k, dest_int l)
36985
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  4294
    | float_of_term t = bad t;
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  4295
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  4296
  fun floatarith_of_term (@{term Add} $ a $ b) = @{code Add} (floatarith_of_term a, floatarith_of_term b)
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  4297
    | floatarith_of_term (@{term Minus} $ a) = @{code Minus} (floatarith_of_term a)
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  4298
    | floatarith_of_term (@{term Mult} $ a $ b) = @{code Mult} (floatarith_of_term a, floatarith_of_term b)
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  4299
    | floatarith_of_term (@{term Inverse} $ a) = @{code Inverse} (floatarith_of_term a)
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  4300
    | floatarith_of_term (@{term Cos} $ a) = @{code Cos} (floatarith_of_term a)
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  4301
    | floatarith_of_term (@{term Arctan} $ a) = @{code Arctan} (floatarith_of_term a)
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  4302
    | floatarith_of_term (@{term Abs} $ a) = @{code Abs} (floatarith_of_term a)
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  4303
    | floatarith_of_term (@{term Max} $ a $ b) = @{code Max} (floatarith_of_term a, floatarith_of_term b)
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  4304
    | floatarith_of_term (@{term Min} $ a $ b) = @{code Min} (floatarith_of_term a, floatarith_of_term b)
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  4305
    | floatarith_of_term @{term Pi} = @{code Pi}
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  4306
    | floatarith_of_term (@{term Sqrt} $ a) = @{code Sqrt} (floatarith_of_term a)
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  4307
    | floatarith_of_term (@{term Exp} $ a) = @{code Exp} (floatarith_of_term a)
62200
67792e4a5486 Made Approximation work for powr again
Manuel Eberl <eberlm@in.tum.de>
parents: 61969
diff changeset
  4308
    | floatarith_of_term (@{term Powr} $ a $ b) = @{code Powr} (floatarith_of_term a, floatarith_of_term b)
36985
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  4309
    | floatarith_of_term (@{term Ln} $ a) = @{code Ln} (floatarith_of_term a)
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  4310
    | floatarith_of_term (@{term Power} $ a $ n) =
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  4311
        @{code Power} (floatarith_of_term a, nat_of_term n)
63263
c6c95d64607a approximation, derivative, and continuity of floor and ceiling
immler
parents: 63248
diff changeset
  4312
    | floatarith_of_term (@{term Floor} $ a) = @{code Floor} (floatarith_of_term a)
36985
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  4313
    | floatarith_of_term (@{term Var} $ n) = @{code Var} (nat_of_term n)
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  4314
    | floatarith_of_term (@{term Num} $ m) = @{code Num} (float_of_term m)
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  4315
    | floatarith_of_term t = bad t;
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  4316
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  4317
  fun form_of_term (@{term Bound} $ a $ b $ c $ p) = @{code Bound}
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  4318
        (floatarith_of_term a, floatarith_of_term b, floatarith_of_term c, form_of_term p)
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  4319
    | form_of_term (@{term Assign} $ a $ b $ p) = @{code Assign}
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  4320
        (floatarith_of_term a, floatarith_of_term b, form_of_term p)
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  4321
    | form_of_term (@{term Less} $ a $ b) = @{code Less}
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  4322
        (floatarith_of_term a, floatarith_of_term b)
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  4323
    | form_of_term (@{term LessEqual} $ a $ b) = @{code LessEqual}
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  4324
        (floatarith_of_term a, floatarith_of_term b)
58986
ec7373051a6c disjunction and conjunction for forms
immler
parents: 58985
diff changeset
  4325
    | form_of_term (@{term Conj} $ a $ b) = @{code Conj}
ec7373051a6c disjunction and conjunction for forms
immler
parents: 58985
diff changeset
  4326
        (form_of_term a, form_of_term b)
ec7373051a6c disjunction and conjunction for forms
immler
parents: 58985
diff changeset
  4327
    | form_of_term (@{term Disj} $ a $ b) = @{code Disj}
ec7373051a6c disjunction and conjunction for forms
immler
parents: 58985
diff changeset
  4328
        (form_of_term a, form_of_term b)
36985
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  4329
    | form_of_term (@{term AtLeastAtMost} $ a $ b $ c) = @{code AtLeastAtMost}
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  4330
        (floatarith_of_term a, floatarith_of_term b, floatarith_of_term c)
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  4331
    | form_of_term t = bad t;
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  4332
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  4333
  fun float_float_option_of_term @{term "None :: (float \<times> float) option"} = NONE
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  4334
    | float_float_option_of_term (@{term "Some :: float \<times> float \<Rightarrow> _"} $ ff) =
59058
a78612c67ec0 renamed "pairself" to "apply2", in accordance to @{apply 2};
wenzelm
parents: 58988
diff changeset
  4335
        SOME (apply2 float_of_term (HOLogic.dest_prod ff))
36985
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  4336
    | float_float_option_of_term (@{term approx'} $ n $ a $ ffs) = @{code approx'}
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  4337
        (nat_of_term n) (floatarith_of_term a) (float_float_option_list_of_term ffs)
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  4338
    | float_float_option_of_term t = bad t
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  4339
  and float_float_option_list_of_term
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  4340
        (@{term "replicate :: _ \<Rightarrow> (float \<times> float) option \<Rightarrow> _"} $ n $ @{term "None :: (float \<times> float) option"}) =
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  4341
          @{code replicate} (nat_of_term n) NONE
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  4342
    | float_float_option_list_of_term (@{term approx_form_eval} $ n $ p $ ffs) =
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  4343
        @{code approx_form_eval} (nat_of_term n) (form_of_term p) (float_float_option_list_of_term ffs)
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  4344
    | float_float_option_list_of_term t = map float_float_option_of_term
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  4345
        (HOLogic.dest_list t);
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  4346
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  4347
  val nat_list_of_term = map nat_of_term o HOLogic.dest_list ;
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  4348
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  4349
  fun bool_of_term (@{term approx_form} $ n $ p $ ffs $ ms) = @{code approx_form}
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  4350
        (nat_of_term n) (form_of_term p) (float_float_option_list_of_term ffs) (nat_list_of_term ms)
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  4351
    | bool_of_term (@{term approx_tse_form} $ m $ n $ q $ p) =
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  4352
        @{code approx_tse_form} (nat_of_term m) (nat_of_term n) (nat_of_term q) (form_of_term p)
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  4353
    | bool_of_term t = bad t;
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  4354
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  4355
  fun eval t = case fastype_of t
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  4356
   of @{typ bool} =>
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  4357
        (term_of_bool o bool_of_term) t
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  4358
    | @{typ "(float \<times> float) option"} =>
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  4359
        (term_of_float_float_option o float_float_option_of_term) t
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  4360
    | @{typ "(float \<times> float) option list"} =>
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  4361
        (term_of_float_float_option_list o float_float_option_list_of_term) t
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  4362
    | _ => bad t;
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  4363
52131
366fa32ee2a3 tuned signature;
wenzelm
parents: 52090
diff changeset
  4364
  val normalize = eval o Envir.beta_norm o Envir.eta_long [];
36985
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  4365
59621
291934bac95e Thm.cterm_of and Thm.ctyp_of operate on local context;
wenzelm
parents: 59582
diff changeset
  4366
in Thm.global_cterm_of thy (Logic.mk_equals (t, normalize t)) end
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
  4367
\<close>
31099
03314c427b34 optimized Approximation by precompiling approx_inequality
hoelzl
parents: 31098
diff changeset
  4368
31811
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  4369
lemma intervalE: "a \<le> x \<and> x \<le> b \<Longrightarrow> \<lbrakk> x \<in> { a .. b } \<Longrightarrow> P\<rbrakk> \<Longrightarrow> P"
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  4370
  by auto
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  4371
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  4372
lemma meta_eqE: "x \<equiv> a \<Longrightarrow> \<lbrakk> x = a \<Longrightarrow> P\<rbrakk> \<Longrightarrow> P"
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  4373
  by auto
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  4374
63929
b673e7221b16 approximation: rewrite for reduction to base expressions
immler
parents: 63918
diff changeset
  4375
named_theorems approximation_preproc
b673e7221b16 approximation: rewrite for reduction to base expressions
immler
parents: 63918
diff changeset
  4376
b673e7221b16 approximation: rewrite for reduction to base expressions
immler
parents: 63918
diff changeset
  4377
lemma approximation_preproc_floatarith[approximation_preproc]:
b673e7221b16 approximation: rewrite for reduction to base expressions
immler
parents: 63918
diff changeset
  4378
  "0 = real_of_float 0"
b673e7221b16 approximation: rewrite for reduction to base expressions
immler
parents: 63918
diff changeset
  4379
  "1 = real_of_float 1"
b673e7221b16 approximation: rewrite for reduction to base expressions
immler
parents: 63918
diff changeset
  4380
  "0 = Float 0 0"
b673e7221b16 approximation: rewrite for reduction to base expressions
immler
parents: 63918
diff changeset
  4381
  "1 = Float 1 0"
b673e7221b16 approximation: rewrite for reduction to base expressions
immler
parents: 63918
diff changeset
  4382
  "numeral a = Float (numeral a) 0"
b673e7221b16 approximation: rewrite for reduction to base expressions
immler
parents: 63918
diff changeset
  4383
  "numeral a = real_of_float (numeral a)"
b673e7221b16 approximation: rewrite for reduction to base expressions
immler
parents: 63918
diff changeset
  4384
  "x - y = x + - y"
b673e7221b16 approximation: rewrite for reduction to base expressions
immler
parents: 63918
diff changeset
  4385
  "x / y = x * inverse y"
b673e7221b16 approximation: rewrite for reduction to base expressions
immler
parents: 63918
diff changeset
  4386
  "ceiling x = - floor (- x)"
b673e7221b16 approximation: rewrite for reduction to base expressions
immler
parents: 63918
diff changeset
  4387
  "log x y = ln y * inverse (ln x)"
b673e7221b16 approximation: rewrite for reduction to base expressions
immler
parents: 63918
diff changeset
  4388
  "sin x = cos (pi / 2 - x)"
b673e7221b16 approximation: rewrite for reduction to base expressions
immler
parents: 63918
diff changeset
  4389
  "tan x = sin x / cos x"
63931
f17a1c60ac39 approximation: preprocessing for nat/int expressions
immler
parents: 63929
diff changeset
  4390
  by (simp_all add: inverse_eq_divide ceiling_def log_def sin_cos_eq tan_def real_of_float_eq)
f17a1c60ac39 approximation: preprocessing for nat/int expressions
immler
parents: 63929
diff changeset
  4391
f17a1c60ac39 approximation: preprocessing for nat/int expressions
immler
parents: 63929
diff changeset
  4392
lemma approximation_preproc_int[approximation_preproc]:
f17a1c60ac39 approximation: preprocessing for nat/int expressions
immler
parents: 63929
diff changeset
  4393
  "real_of_int 0 = real_of_float 0"
f17a1c60ac39 approximation: preprocessing for nat/int expressions
immler
parents: 63929
diff changeset
  4394
  "real_of_int 1 = real_of_float 1"
f17a1c60ac39 approximation: preprocessing for nat/int expressions
immler
parents: 63929
diff changeset
  4395
  "real_of_int (i + j) = real_of_int i + real_of_int j"
63929
b673e7221b16 approximation: rewrite for reduction to base expressions
immler
parents: 63918
diff changeset
  4396
  "real_of_int (- i) = - real_of_int i"
63931
f17a1c60ac39 approximation: preprocessing for nat/int expressions
immler
parents: 63929
diff changeset
  4397
  "real_of_int (i - j) = real_of_int i - real_of_int j"
f17a1c60ac39 approximation: preprocessing for nat/int expressions
immler
parents: 63929
diff changeset
  4398
  "real_of_int (i * j) = real_of_int i * real_of_int j"
f17a1c60ac39 approximation: preprocessing for nat/int expressions
immler
parents: 63929
diff changeset
  4399
  "real_of_int (i div j) = real_of_int (floor (real_of_int i / real_of_int j))"
f17a1c60ac39 approximation: preprocessing for nat/int expressions
immler
parents: 63929
diff changeset
  4400
  "real_of_int (min i j) = min (real_of_int i) (real_of_int j)"
f17a1c60ac39 approximation: preprocessing for nat/int expressions
immler
parents: 63929
diff changeset
  4401
  "real_of_int (max i j) = max (real_of_int i) (real_of_int j)"
f17a1c60ac39 approximation: preprocessing for nat/int expressions
immler
parents: 63929
diff changeset
  4402
  "real_of_int (abs i) = abs (real_of_int i)"
f17a1c60ac39 approximation: preprocessing for nat/int expressions
immler
parents: 63929
diff changeset
  4403
  "real_of_int (i ^ n) = (real_of_int i) ^ n"
f17a1c60ac39 approximation: preprocessing for nat/int expressions
immler
parents: 63929
diff changeset
  4404
  "real_of_int (numeral a) = real_of_float (numeral a)"
f17a1c60ac39 approximation: preprocessing for nat/int expressions
immler
parents: 63929
diff changeset
  4405
  "i mod j = i - i div j * j"
f17a1c60ac39 approximation: preprocessing for nat/int expressions
immler
parents: 63929
diff changeset
  4406
  "i = j \<longleftrightarrow> real_of_int i = real_of_int j"
f17a1c60ac39 approximation: preprocessing for nat/int expressions
immler
parents: 63929
diff changeset
  4407
  "i \<le> j \<longleftrightarrow> real_of_int i \<le> real_of_int j"
f17a1c60ac39 approximation: preprocessing for nat/int expressions
immler
parents: 63929
diff changeset
  4408
  "i < j \<longleftrightarrow> real_of_int i < real_of_int j"
f17a1c60ac39 approximation: preprocessing for nat/int expressions
immler
parents: 63929
diff changeset
  4409
  "i \<in> {j .. k} \<longleftrightarrow> real_of_int i \<in> {real_of_int j .. real_of_int k}"
64246
15d1ee6e847b eliminated irregular aliasses
haftmann
parents: 64243
diff changeset
  4410
  by (simp_all add: floor_divide_of_int_eq minus_div_mult_eq_mod [symmetric])
63931
f17a1c60ac39 approximation: preprocessing for nat/int expressions
immler
parents: 63929
diff changeset
  4411
f17a1c60ac39 approximation: preprocessing for nat/int expressions
immler
parents: 63929
diff changeset
  4412
lemma approximation_preproc_nat[approximation_preproc]:
f17a1c60ac39 approximation: preprocessing for nat/int expressions
immler
parents: 63929
diff changeset
  4413
  "real 0 = real_of_float 0"
f17a1c60ac39 approximation: preprocessing for nat/int expressions
immler
parents: 63929
diff changeset
  4414
  "real 1 = real_of_float 1"
f17a1c60ac39 approximation: preprocessing for nat/int expressions
immler
parents: 63929
diff changeset
  4415
  "real (i + j) = real i + real j"
f17a1c60ac39 approximation: preprocessing for nat/int expressions
immler
parents: 63929
diff changeset
  4416
  "real (i - j) = max (real i - real j) 0"
f17a1c60ac39 approximation: preprocessing for nat/int expressions
immler
parents: 63929
diff changeset
  4417
  "real (i * j) = real i * real j"
f17a1c60ac39 approximation: preprocessing for nat/int expressions
immler
parents: 63929
diff changeset
  4418
  "real (i div j) = real_of_int (floor (real i / real j))"
f17a1c60ac39 approximation: preprocessing for nat/int expressions
immler
parents: 63929
diff changeset
  4419
  "real (min i j) = min (real i) (real j)"
f17a1c60ac39 approximation: preprocessing for nat/int expressions
immler
parents: 63929
diff changeset
  4420
  "real (max i j) = max (real i) (real j)"
f17a1c60ac39 approximation: preprocessing for nat/int expressions
immler
parents: 63929
diff changeset
  4421
  "real (i ^ n) = (real i) ^ n"
f17a1c60ac39 approximation: preprocessing for nat/int expressions
immler
parents: 63929
diff changeset
  4422
  "real (numeral a) = real_of_float (numeral a)"
f17a1c60ac39 approximation: preprocessing for nat/int expressions
immler
parents: 63929
diff changeset
  4423
  "i mod j = i - i div j * j"
f17a1c60ac39 approximation: preprocessing for nat/int expressions
immler
parents: 63929
diff changeset
  4424
  "n = m \<longleftrightarrow> real n = real m"
f17a1c60ac39 approximation: preprocessing for nat/int expressions
immler
parents: 63929
diff changeset
  4425
  "n \<le> m \<longleftrightarrow> real n \<le> real m"
f17a1c60ac39 approximation: preprocessing for nat/int expressions
immler
parents: 63929
diff changeset
  4426
  "n < m \<longleftrightarrow> real n < real m"
f17a1c60ac39 approximation: preprocessing for nat/int expressions
immler
parents: 63929
diff changeset
  4427
  "n \<in> {m .. l} \<longleftrightarrow> real n \<in> {real m .. real l}"
64243
aee949f6642d eliminated irregular aliasses
haftmann
parents: 64242
diff changeset
  4428
  by (simp_all add: real_div_nat_eq_floor_of_divide minus_div_mult_eq_mod [symmetric])
63929
b673e7221b16 approximation: rewrite for reduction to base expressions
immler
parents: 63918
diff changeset
  4429
59850
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59751
diff changeset
  4430
ML_file "approximation.ML"
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59751
diff changeset
  4431
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
  4432
method_setup approximation = \<open>
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  4433
  let
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  4434
    val free =
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  4435
      Args.context -- Args.term >> (fn (_, Free (n, _)) => n | (ctxt, t) =>
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  4436
        error ("Bad free variable: " ^ Syntax.string_of_term ctxt t));
59850
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59751
diff changeset
  4437
  in
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  4438
    Scan.lift Parse.nat --
59850
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59751
diff changeset
  4439
    Scan.optional (Scan.lift (Args.$$$ "splitting" |-- Args.colon)
60680
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  4440
      |-- Parse.and_list' (free --| Scan.lift (Args.$$$ "=") -- Scan.lift Parse.nat)) [] --
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  4441
    Scan.option (Scan.lift (Args.$$$ "taylor" |-- Args.colon) |--
589ed01b94fe tuned proofs;
wenzelm
parents: 60533
diff changeset
  4442
    (free |-- Scan.lift (Args.$$$ "=") |-- Scan.lift Parse.nat)) >>
59850
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59751
diff changeset
  4443
    (fn ((prec, splitting), taylor) => fn ctxt =>
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59751
diff changeset
  4444
      SIMPLE_METHOD' (Approximation.approximation_tac prec splitting taylor ctxt))
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59751
diff changeset
  4445
  end
60533
1e7ccd864b62 isabelle update_cartouches;
wenzelm
parents: 60017
diff changeset
  4446
\<close> "real number approximation"
31811
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  4447
58988
6ebf918128b9 added quickcheck[approximation]
immler
parents: 58986
diff changeset
  4448
6ebf918128b9 added quickcheck[approximation]
immler
parents: 58986
diff changeset
  4449
section "Quickcheck Generator"
6ebf918128b9 added quickcheck[approximation]
immler
parents: 58986
diff changeset
  4450
63929
b673e7221b16 approximation: rewrite for reduction to base expressions
immler
parents: 63918
diff changeset
  4451
lemma approximation_preproc_push_neg[approximation_preproc]:
b673e7221b16 approximation: rewrite for reduction to base expressions
immler
parents: 63918
diff changeset
  4452
  fixes a b::real
b673e7221b16 approximation: rewrite for reduction to base expressions
immler
parents: 63918
diff changeset
  4453
  shows
b673e7221b16 approximation: rewrite for reduction to base expressions
immler
parents: 63918
diff changeset
  4454
    "\<not> (a < b) \<longleftrightarrow> b \<le> a"
b673e7221b16 approximation: rewrite for reduction to base expressions
immler
parents: 63918
diff changeset
  4455
    "\<not> (a \<le> b) \<longleftrightarrow> b < a"
b673e7221b16 approximation: rewrite for reduction to base expressions
immler
parents: 63918
diff changeset
  4456
    "\<not> (a = b) \<longleftrightarrow> b < a \<or> a < b"
b673e7221b16 approximation: rewrite for reduction to base expressions
immler
parents: 63918
diff changeset
  4457
    "\<not> (p \<and> q) \<longleftrightarrow> \<not> p \<or> \<not> q"
b673e7221b16 approximation: rewrite for reduction to base expressions
immler
parents: 63918
diff changeset
  4458
    "\<not> (p \<or> q) \<longleftrightarrow> \<not> p \<and> \<not> q"
b673e7221b16 approximation: rewrite for reduction to base expressions
immler
parents: 63918
diff changeset
  4459
    "\<not> \<not> q \<longleftrightarrow> q"
b673e7221b16 approximation: rewrite for reduction to base expressions
immler
parents: 63918
diff changeset
  4460
  by auto
b673e7221b16 approximation: rewrite for reduction to base expressions
immler
parents: 63918
diff changeset
  4461
58988
6ebf918128b9 added quickcheck[approximation]
immler
parents: 58986
diff changeset
  4462
ML_file "approximation_generator.ML"
6ebf918128b9 added quickcheck[approximation]
immler
parents: 58986
diff changeset
  4463
setup "Approximation_Generator.setup"
6ebf918128b9 added quickcheck[approximation]
immler
parents: 58986
diff changeset
  4464
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  4465
end