src/HOL/Decision_Procs/Approximation.thy
author wenzelm
Sat, 23 May 2015 17:19:37 +0200
changeset 60299 5ae2a2e74c93
parent 60017 b785d6d06430
child 60533 1e7ccd864b62
permissions -rw-r--r--
clarified NEWS: document_files are officially required since Isabelle2014, but the absence was tolerated as legacy feature;
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
58889
5b7a9633cfa8 modernized header uniformly as section;
wenzelm
parents: 58834
diff changeset
     4
section {* Prove Real Valued Inequalities by Computation *}
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_numeral [simp]
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54269
diff changeset
    16
declare powr_neg_one [simp]
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54269
diff changeset
    17
declare powr_neg_numeral [simp]
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
    18
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
    19
section "Horner Scheme"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
    20
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
    21
subsection {* Define auxiliary helper @{text horner} function *}
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
    22
31098
73dd67adf90a replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
hoelzl
parents: 30971
diff changeset
    23
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
    24
"horner F G 0 i k x       = 0" |
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
    25
"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
    26
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
    27
lemma horner_schema':
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
    28
  fixes x :: real and a :: "nat \<Rightarrow> real"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
    29
  shows "a 0 - x * (\<Sum> i=0..<n. (-1)^i * a (Suc i) * x^i) = (\<Sum> i=0..<Suc n. (-1)^i * a i * x^i)"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
    30
proof -
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
    31
  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
    32
    by auto
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
    33
  show ?thesis
54230
b1d955791529 more simplification rules on unary and binary minus
haftmann
parents: 53077
diff changeset
    34
    unfolding setsum_right_distrib shift_pow uminus_add_conv_diff [symmetric] setsum_negf[symmetric]
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
    35
    setsum_head_upt_Suc[OF zero_less_Suc]
57418
6ab1c7cb0b8d fact consolidation
haftmann
parents: 56923
diff changeset
    36
    setsum.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
    37
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
    38
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
    39
lemma horner_schema:
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
    40
  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
    41
  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
    42
  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
    43
proof (induct n arbitrary: j')
1fce03e3e8ad tuned proofs -- eliminated vacuous "induct arbitrary: ..." situations;
wenzelm
parents: 44821
diff changeset
    44
  case 0
1fce03e3e8ad tuned proofs -- eliminated vacuous "induct arbitrary: ..." situations;
wenzelm
parents: 44821
diff changeset
    45
  then show ?case by auto
1fce03e3e8ad tuned proofs -- eliminated vacuous "induct arbitrary: ..." situations;
wenzelm
parents: 44821
diff changeset
    46
next
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
    47
  case (Suc n)
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
    48
  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
    49
    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
    50
qed
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
    51
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
    52
lemma horner_bounds':
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
    53
  fixes lb :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> float \<Rightarrow> float" and ub :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> float \<Rightarrow> float"
31098
73dd67adf90a replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
hoelzl
parents: 30971
diff changeset
    54
  assumes "0 \<le> real x" and f_Suc: "\<And>n. f (Suc n) = G ((F ^^ n) s) (f n)"
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
    55
    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
    56
    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
    57
        (lapprox_rat prec 1 k)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
    58
        (- float_round_up prec (x * (ub n (F i) (G i k) x)))"
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
    59
    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
    60
    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
    61
        (rapprox_rat prec 1 k)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
    62
        (- 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
    63
  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
    64
         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
    65
  (is "?lb n j' \<le> ?horner n j' \<and> ?horner n j' \<le> ?ub n j'")
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
    66
proof (induct n arbitrary: j')
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
    67
  case 0
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
    68
  thus ?case unfolding lb_0 ub_0 horner.simps by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
    69
next
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
    70
  case (Suc n)
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
    71
  thus ?case using lapprox_rat[of prec 1 "f j'"] using rapprox_rat[of 1 "f j'" prec]
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
    72
    Suc[where j'="Suc j'"] `0 \<le> real x`
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
    73
    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
    74
      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
    75
      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
    76
      simp add: lb_Suc ub_Suc field_simps f_Suc)
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
    77
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
    78
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
    79
subsection "Theorems for floating point functions implementing the horner scheme"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
    80
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
    81
text {*
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
    82
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
    83
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
    84
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
    85
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
    86
*}
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
    87
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
    88
lemma horner_bounds:
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
    89
  fixes F :: "nat \<Rightarrow> nat" and G :: "nat \<Rightarrow> nat \<Rightarrow> nat"
31098
73dd67adf90a replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
hoelzl
parents: 30971
diff changeset
    90
  assumes "0 \<le> real x" and f_Suc: "\<And>n. f (Suc n) = G ((F ^^ n) s) (f n)"
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
    91
    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
    92
    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
    93
        (lapprox_rat prec 1 k)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
    94
        (- float_round_up prec (x * (ub n (F i) (G i k) x)))"
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
    95
    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
    96
    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
    97
        (rapprox_rat prec 1 k)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
    98
        (- 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
    99
  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
   100
      (is "?lb")
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   101
    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
   102
      (is "?ub")
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   103
proof -
31809
hoelzl
parents: 31790
diff changeset
   104
  have "?lb  \<and> ?ub"
31098
73dd67adf90a replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
hoelzl
parents: 30971
diff changeset
   105
    using horner_bounds'[where lb=lb, OF `0 \<le> real x` f_Suc lb_0 lb_Suc ub_0 ub_Suc]
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   106
    unfolding horner_schema[where f=f, OF f_Suc] .
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   107
  thus "?lb" and "?ub" by auto
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   108
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   109
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
   110
lemma horner_bounds_nonpos:
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
   111
  fixes F :: "nat \<Rightarrow> nat" and G :: "nat \<Rightarrow> nat \<Rightarrow> nat"
31098
73dd67adf90a replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
hoelzl
parents: 30971
diff changeset
   112
  assumes "real 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
   113
    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
   114
    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
   115
        (lapprox_rat prec 1 k)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   116
        (float_round_down prec (x * (ub n (F i) (G i k) x)))"
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
   117
    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
   118
    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
   119
        (rapprox_rat prec 1 k)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   120
        (float_round_up 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
   121
  shows "(lb n ((F ^^ j') s) (f j') x) \<le> (\<Sum>j=0..<n. (1 / (f (j' + j))) * real x ^ j)" (is "?lb")
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   122
    and "(\<Sum>j=0..<n. (1 / (f (j' + j))) * real x ^ j) \<le> (ub n ((F ^^ j') s) (f j') x)" (is "?ub")
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   123
proof -
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
   124
  { fix x y z :: float have "x - y * z = x + - y * z" by simp } note diff_mult_minus = this
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   125
  have sum_eq: "(\<Sum>j=0..<n. (1 / (f (j' + j))) * real x ^ j) =
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
   126
    (\<Sum>j = 0..<n. (- 1) ^ j * (1 / (f (j' + j))) * real (- x) ^ j)"
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
   127
    by (auto simp add: field_simps power_mult_distrib[symmetric])
31098
73dd67adf90a replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
hoelzl
parents: 30971
diff changeset
   128
  have "0 \<le> real (-x)" using assms by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   129
  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
   130
    and lb="\<lambda> n i k x. lb n i k (-x)" and ub="\<lambda> n i k x. ub n i k (-x)",
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   131
    unfolded lb_Suc ub_Suc diff_mult_minus,
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   132
    OF this f_Suc lb_0 _ ub_0 _]
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   133
  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
   134
    by (auto simp: minus_float_round_up_eq minus_float_round_down_eq)
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   135
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   136
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   137
subsection {* Selectors for next even or odd number *}
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   138
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   139
text {*
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   140
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   141
The horner scheme computes alternating series. To get the upper and lower bounds we need to
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   142
guarantee to access a even or odd member. To do this we use @{term get_odd} and @{term get_even}.
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   143
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   144
*}
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   145
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   146
definition get_odd :: "nat \<Rightarrow> nat" where
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   147
  "get_odd n = (if odd n then n else (Suc n))"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   148
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   149
definition get_even :: "nat \<Rightarrow> nat" where
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   150
  "get_even n = (if even n then n else (Suc n))"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   151
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   152
lemma get_odd[simp]: "odd (get_odd n)" unfolding get_odd_def by (cases "odd n", auto)
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   153
lemma get_even[simp]: "even (get_even n)" unfolding get_even_def by (cases "even n", auto)
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   154
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
   155
  by (auto simp: get_odd_def odd_pos intro!: exI[of _ "n - 1"])
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   156
58709
efdc6c533bd3 prefer generic elimination rules for even/odd over specialized unfold rules for nat
haftmann
parents: 58410
diff changeset
   157
lemma get_even_double:
efdc6c533bd3 prefer generic elimination rules for even/odd over specialized unfold rules for nat
haftmann
parents: 58410
diff changeset
   158
  "\<exists>i. get_even n = 2 * i" using get_even by (blast elim: evenE)
efdc6c533bd3 prefer generic elimination rules for even/odd over specialized unfold rules for nat
haftmann
parents: 58410
diff changeset
   159
efdc6c533bd3 prefer generic elimination rules for even/odd over specialized unfold rules for nat
haftmann
parents: 58410
diff changeset
   160
lemma get_odd_double:
efdc6c533bd3 prefer generic elimination rules for even/odd over specialized unfold rules for nat
haftmann
parents: 58410
diff changeset
   161
  "\<exists>i. get_odd n = 2 * i + 1" using get_odd by (blast elim: oddE)
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   162
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   163
section "Power function"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   164
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   165
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
   166
"float_power_bnds prec n l u =
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   167
  (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
   168
  else if odd n then
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   169
    (- power_up_fl prec (abs l) n,
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   170
      if u < 0 then - power_down_fl prec (abs u) n else 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 u < 0 then (power_down_fl prec (abs u) n, power_up_fl prec (abs l) n)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   172
  else (0, power_up_fl prec (max (abs l) (abs u)) n))"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   173
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   174
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
   175
  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
   176
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   177
lemma float_power_bnds:
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   178
  "(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
   179
  by (auto
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   180
    simp: float_power_bnds_def max_def real_power_up_fl real_power_down_fl minus_le_iff
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   181
    split: split_if_asm
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   182
    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
   183
    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
   184
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   185
lemma bnds_power:
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   186
  "\<forall> (x::real) l u. (l1, u1) = float_power_bnds prec n l u \<and> x \<in> {l .. u} \<longrightarrow>
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   187
    l1 \<le> x ^ n \<and> x ^ n \<le> u1"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   188
  using float_power_bnds by auto
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   189
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   190
section "Square root"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   191
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   192
text {*
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   193
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   194
The square root computation is implemented as newton iteration. As first first step we use the
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   195
nearest power of two greater than the square root.
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   196
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   197
*}
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   198
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   199
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
   200
"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
   201
"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
   202
                                  in Float 1 (- 1) * float_plus_up prec y (float_divr prec x y))"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   203
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
   204
lemma compute_sqrt_iteration_base[code]:
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
   205
  shows "sqrt_iteration prec n (Float m e) =
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
   206
    (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
   207
    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
   208
      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
   209
  using bitlen_Float by (cases n) simp_all
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
   210
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
   211
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
   212
"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
   213
              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
   214
                            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
   215
"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
   216
              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
   217
                            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
   218
by pat_completeness auto
55414
eab03e9cee8a renamed '{prod,sum,bool,unit}_case' to 'case_...'
blanchet
parents: 55413
diff changeset
   219
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
   220
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
   221
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
   222
declare ub_sqrt.simps[simp del]
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   223
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   224
lemma sqrt_ub_pos_pos_1:
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   225
  assumes "sqrt x < b" and "0 < b" and "0 < x"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   226
  shows "sqrt x < (b + x / b)/2"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   227
proof -
53077
a1b3784f8129 more symbols;
wenzelm
parents: 52286
diff changeset
   228
  from assms have "0 < (b - sqrt x)\<^sup>2 " by simp
a1b3784f8129 more symbols;
wenzelm
parents: 52286
diff changeset
   229
  also have "\<dots> = b\<^sup>2 - 2 * b * sqrt x + (sqrt x)\<^sup>2" by algebra
a1b3784f8129 more symbols;
wenzelm
parents: 52286
diff changeset
   230
  also have "\<dots> = b\<^sup>2 - 2 * b * sqrt x + x" using assms by simp
a1b3784f8129 more symbols;
wenzelm
parents: 52286
diff changeset
   231
  finally have "0 < b\<^sup>2 - 2 * b * sqrt x + x" .
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   232
  hence "0 < b / 2 - sqrt x + x / (2 * b)" using assms
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   233
    by (simp add: field_simps power2_eq_square)
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   234
  thus ?thesis by (simp add: field_simps)
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   235
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   236
31098
73dd67adf90a replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
hoelzl
parents: 30971
diff changeset
   237
lemma sqrt_iteration_bound: assumes "0 < real x"
54269
dcdfec41a325 tuned proofs in Approximation
hoelzl
parents: 54230
diff changeset
   238
  shows "sqrt x < sqrt_iteration prec n x"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   239
proof (induct n)
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   240
  case 0
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   241
  show ?case
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   242
  proof (cases x)
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   243
    case (Float m e)
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
   244
    hence "0 < m" using assms 
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
   245
      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
   246
      by (meson not_less powr_ge_pzero)
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   247
    hence "0 < sqrt m" by auto
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   248
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
   249
    have int_nat_bl: "(nat (bitlen m)) = bitlen m" using bitlen_nonneg by auto
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
   250
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
   251
    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
   252
      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
   253
    also have "\<dots> < 1 * 2 powr (e + nat (bitlen m))"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   254
    proof (rule mult_strict_right_mono, auto)
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   255
      show "m < 2^nat (bitlen m)" using bitlen_bounds[OF `0 < m`, THEN conjunct2]
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
   256
        unfolding real_of_int_less_iff[of m, symmetric] by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   257
    qed
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
   258
    finally have "sqrt x < sqrt (2 powr (e + bitlen m))" unfolding int_nat_bl by auto
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
   259
    also have "\<dots> \<le> 2 powr ((e + bitlen m) div 2 + 1)"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   260
    proof -
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   261
      let ?E = "e + bitlen m"
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
   262
      have E_mod_pow: "2 powr (?E mod 2) < 4"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   263
      proof (cases "?E mod 2 = 1")
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
   264
        case True thus ?thesis by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   265
      next
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
   266
        case False
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
   267
        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
   268
        have "?E mod 2 < 2" by auto
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
   269
        from this[THEN zless_imp_add1_zle]
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
   270
        have "?E mod 2 \<le> 0" using False by auto
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
   271
        from xt1(5)[OF `0 \<le> ?E mod 2` this]
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
   272
        show ?thesis by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   273
      qed
56889
48a745e1bde7 avoid the Complex constructor, use the more natural Re/Im view; moved csqrt to Complex.
hoelzl
parents: 56813
diff changeset
   274
      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
   275
        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
   276
      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
   277
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
   278
      have E_eq: "2 powr ?E = 2 powr (?E div 2 + ?E div 2 + ?E mod 2)" by auto
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
   279
      have "sqrt (2 powr ?E) = sqrt (2 powr (?E div 2) * 2 powr (?E div 2) * 2 powr (?E mod 2))"
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
   280
        unfolding E_eq unfolding powr_add[symmetric] by (simp add: int_of_reals del: real_of_ints)
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
   281
      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
   282
        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
   283
      also have "\<dots> < 2 powr (?E div 2) * 2 powr 1"
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
   284
        by (rule mult_strict_left_mono, auto intro: E_mod_pow)
57512
cc97b347b301 reduced name variants for assoc and commute on plus and mult
haftmann
parents: 57418
diff changeset
   285
      also have "\<dots> = 2 powr (?E div 2 + 1)" unfolding add.commute[of _ 1] powr_add[symmetric]
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
   286
        by simp
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   287
      finally show ?thesis by auto
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   288
    qed
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
   289
    finally show ?thesis using `0 < m`
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
   290
      unfolding Float
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   291
      by (subst compute_sqrt_iteration_base) (simp add: ac_simps)
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   292
  qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   293
next
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   294
  case (Suc n)
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   295
  let ?b = "sqrt_iteration prec n x"
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   296
  have "0 < sqrt x" using `0 < real x` by auto
31098
73dd67adf90a replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
hoelzl
parents: 30971
diff changeset
   297
  also have "\<dots> < real ?b" using Suc .
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   298
  finally have "sqrt x < (?b + x / ?b)/2" using sqrt_ub_pos_pos_1[OF Suc _ `0 < real x`] by auto
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   299
  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
   300
    by (rule divide_right_mono, auto simp add: float_divr)
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
   301
  also have "\<dots> = (Float 1 (- 1)) * (?b + (float_divr prec x ?b))" by simp
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   302
  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
   303
    by (auto simp add: algebra_simps float_plus_up_le)
49962
a8cc904a6820 Renamed {left,right}_distrib to distrib_{right,left}.
webertj
parents: 49351
diff changeset
   304
  finally show ?case unfolding sqrt_iteration.simps Let_def distrib_left .
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   305
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   306
31098
73dd67adf90a replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
hoelzl
parents: 30971
diff changeset
   307
lemma sqrt_iteration_lower_bound: assumes "0 < real x"
73dd67adf90a replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
hoelzl
parents: 30971
diff changeset
   308
  shows "0 < real (sqrt_iteration prec n x)" (is "0 < ?sqrt")
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   309
proof -
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   310
  have "0 < sqrt x" using assms by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   311
  also have "\<dots> < ?sqrt" using sqrt_iteration_bound[OF assms] .
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   312
  finally show ?thesis .
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   313
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   314
31098
73dd67adf90a replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
hoelzl
parents: 30971
diff changeset
   315
lemma lb_sqrt_lower_bound: assumes "0 \<le> real 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
   316
  shows "0 \<le> real (lb_sqrt prec x)"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   317
proof (cases "0 < x")
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   318
  case True hence "0 < real x" and "0 \<le> x" using `0 \<le> real x` by auto
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   319
  hence "0 < sqrt_iteration prec prec x" using sqrt_iteration_lower_bound by auto
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
   320
  hence "0 \<le> real (float_divl prec x (sqrt_iteration prec prec x))" using float_divl_lower_bound[OF `0 \<le> x`] unfolding less_eq_float_def 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
   321
  thus ?thesis unfolding lb_sqrt.simps using True by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   322
next
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   323
  case False with `0 \<le> real x` have "real x = 0" by auto
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   324
  thus ?thesis unfolding lb_sqrt.simps by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   325
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   326
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
   327
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
   328
proof -
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
   329
  { fix x :: float assume "0 < x"
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   330
    hence "0 < real x" and "0 \<le> real x" by auto
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   331
    hence sqrt_gt0: "0 < sqrt x" by auto
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   332
    hence sqrt_ub: "sqrt x < sqrt_iteration prec prec x" using sqrt_iteration_bound by auto
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   333
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   334
    have "(float_divl prec x (sqrt_iteration prec prec x)) \<le>
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   335
          x / (sqrt_iteration prec prec x)" by (rule float_divl)
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   336
    also have "\<dots> < x / sqrt 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
   337
      by (rule divide_strict_left_mono[OF sqrt_ub `0 < real 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
   338
               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
   339
    also have "\<dots> = sqrt x"
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   340
      unfolding inverse_eq_iff_eq[of _ "sqrt x", symmetric]
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
   341
                sqrt_divide_self_eq[OF `0 \<le> real x`, symmetric] by auto
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   342
    finally have "lb_sqrt prec x \<le> sqrt 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
   343
      unfolding lb_sqrt.simps if_P[OF `0 < x`] by auto }
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
   344
  note lb = 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
   345
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
   346
  { fix x :: float assume "0 < x"
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   347
    hence "0 < real x" by auto
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   348
    hence "0 < sqrt x" by auto
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   349
    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
   350
      using sqrt_iteration_bound by auto
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   351
    hence "sqrt x \<le> 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
   352
      unfolding ub_sqrt.simps if_P[OF `0 < x`] by auto }
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
   353
  note ub = 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
   354
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
   355
  show ?thesis
54269
dcdfec41a325 tuned proofs in Approximation
hoelzl
parents: 54230
diff changeset
   356
    using lb[of "-x"] ub[of "-x"] lb[of x] ub[of x]
dcdfec41a325 tuned proofs in Approximation
hoelzl
parents: 54230
diff changeset
   357
    by (auto simp add: lb_sqrt.simps ub_sqrt.simps real_sqrt_minus)
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   358
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   359
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   360
lemma bnds_sqrt: "\<forall> (x::real) lx ux. (l, u) = (lb_sqrt prec lx, ub_sqrt prec ux) \<and> x \<in> {lx .. ux} \<longrightarrow> l \<le> sqrt x \<and> sqrt x \<le> u"
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
   361
proof ((rule allI) +, rule impI, erule conjE, rule conjI)
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   362
  fix x :: real 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
   363
  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
   364
    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
   365
  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
   366
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   367
  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
   368
  from order_trans[OF _ this]
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   369
  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
   370
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   371
  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
   372
  from order_trans[OF this]
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   373
  show "sqrt x \<le> u" unfolding u using bnds_sqrt'[of ux prec] by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   374
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   375
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   376
section "Arcus tangens and \<pi>"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   377
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   378
subsection "Compute arcus tangens series"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   379
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   380
text {*
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   381
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   382
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
   383
@{term "{-1 :: real .. 1}"}. This is used to compute \<pi> and then the entire arcus tangens.
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   384
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   385
*}
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   386
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   387
fun ub_arctan_horner :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> float \<Rightarrow> float"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   388
and lb_arctan_horner :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> float \<Rightarrow> float" where
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   389
  "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
   390
| "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
   391
      (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
   392
| "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
   393
| "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
   394
      (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
   395
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
   396
lemma arctan_0_1_bounds':
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   397
  assumes "0 \<le> real y" "real y \<le> 1" and "even n"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   398
  shows "arctan (sqrt y) \<in>
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   399
      {(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
   400
proof -
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   401
  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
   402
  let ?S = "\<lambda>n. \<Sum> i=0..<n. ?c i"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   403
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   404
  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
   405
  have "sqrt y \<le> 1" using assms by auto
58709
efdc6c533bd3 prefer generic elimination rules for even/odd over specialized unfold rules for nat
haftmann
parents: 58410
diff changeset
   406
  from `even n` obtain m where "2 * m = n" by (blast elim: evenE)
31809
hoelzl
parents: 31790
diff changeset
   407
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   408
  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
   409
  proof (cases "sqrt y = 0")
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   410
    case False
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   411
    hence "0 < sqrt y" using `0 \<le> sqrt y` by auto
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   412
    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
   413
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   414
    have "\<bar> sqrt y \<bar> \<le> 1"  using `0 \<le> sqrt y` `sqrt y \<le> 1` by auto
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   415
    from mp[OF summable_Leibniz(2)[OF zeroseq_arctan_series[OF this]
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   416
      monoseq_arctan_series[OF this]] prem, THEN spec, of m, unfolded `2 * m = n`]
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   417
    show ?thesis unfolding arctan_series[OF `\<bar> sqrt y \<bar> \<le> 1`] Suc_eq_plus1 atLeast0LessThan .
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   418
  qed auto
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   419
  note arctan_bounds = this[unfolded atLeastAtMost_iff]
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   420
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   421
  have F: "\<And>n. 2 * Suc n + 1 = 2 * n + 1 + 2" by auto
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   422
31809
hoelzl
parents: 31790
diff changeset
   423
  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
   424
    and lb="\<lambda>n i k x. lb_arctan_horner prec n k x"
31809
hoelzl
parents: 31790
diff changeset
   425
    and ub="\<lambda>n i k x. ub_arctan_horner prec n k x",
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   426
    OF `0 \<le> real y` F lb_arctan_horner.simps ub_arctan_horner.simps]
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   427
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   428
  { have "(sqrt y * lb_arctan_horner prec n 1 y) \<le> ?S n"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   429
      using bounds(1) `0 \<le> sqrt y`
57512
cc97b347b301 reduced name variants for assoc and commute on plus and mult
haftmann
parents: 57418
diff changeset
   430
      unfolding power_add power_one_right mult.assoc[symmetric] setsum_left_distrib[symmetric]
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   431
      unfolding mult.commute[where 'a=real] mult.commute[of _ "2::nat"] power_mult
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   432
      by (auto intro!: mult_left_mono)
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   433
    also have "\<dots> \<le> arctan (sqrt y)" using arctan_bounds ..
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   434
    finally have "(sqrt y * lb_arctan_horner prec n 1 y) \<le> arctan (sqrt y)" . }
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   435
  moreover
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   436
  { have "arctan (sqrt y) \<le> ?S (Suc n)" using arctan_bounds ..
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   437
    also have "\<dots> \<le> (sqrt y * ub_arctan_horner prec (Suc n) 1 y)"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   438
      using bounds(2)[of "Suc n"] `0 \<le> sqrt y`
57512
cc97b347b301 reduced name variants for assoc and commute on plus and mult
haftmann
parents: 57418
diff changeset
   439
      unfolding power_add power_one_right mult.assoc[symmetric] setsum_left_distrib[symmetric]
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   440
      unfolding mult.commute[where 'a=real] mult.commute[of _ "2::nat"] power_mult
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   441
      by (auto intro!: mult_left_mono)
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   442
    finally have "arctan (sqrt y) \<le> (sqrt y * ub_arctan_horner prec (Suc n) 1 y)" . }
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   443
  ultimately show ?thesis by auto
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   444
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   445
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   446
lemma arctan_0_1_bounds: assumes "0 \<le> real y" "real y \<le> 1"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   447
  shows "arctan (sqrt y) \<in>
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   448
    {(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
   449
      (sqrt y * ub_arctan_horner prec (get_odd n) 1 y)}"
54269
dcdfec41a325 tuned proofs in Approximation
hoelzl
parents: 54230
diff changeset
   450
  using
dcdfec41a325 tuned proofs in Approximation
hoelzl
parents: 54230
diff changeset
   451
    arctan_0_1_bounds'[OF assms, of n prec]
dcdfec41a325 tuned proofs in Approximation
hoelzl
parents: 54230
diff changeset
   452
    arctan_0_1_bounds'[OF assms, of "n + 1" prec]
dcdfec41a325 tuned proofs in Approximation
hoelzl
parents: 54230
diff changeset
   453
    arctan_0_1_bounds'[OF assms, of "n - 1" prec]
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   454
  by (auto simp: get_even_def get_odd_def odd_pos simp del: ub_arctan_horner.simps
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   455
    lb_arctan_horner.simps)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   456
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   457
lemma arctan_lower_bound:
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   458
  assumes "0 \<le> x"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   459
  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
   460
proof -
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   461
  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
   462
    using assms
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   463
    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
   464
      (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
   465
  thus ?thesis by simp
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   466
qed
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   467
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   468
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
   469
  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
   470
    (auto intro!: derivative_eq_intros divide_nonpos_nonneg
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   471
      simp: inverse_eq_divide arctan_lower_bound)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   472
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   473
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
   474
  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
   475
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   476
lemma arctan_mult_le:
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   477
  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
   478
  shows "x * z \<le> arctan x"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   479
proof cases
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   480
  assume "x \<noteq> 0"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   481
  with assms have "z \<le> arctan y / y" by (simp add: field_simps)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   482
  also have "\<dots> \<le> arctan x / x" using assms `x \<noteq> 0` by (auto intro!: arctan_divide_mono)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   483
  finally show ?thesis using assms `x \<noteq> 0` by (simp add: field_simps)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   484
qed simp
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   485
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   486
lemma arctan_le_mult:
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   487
  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
   488
  shows "arctan y \<le> y * z"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   489
proof -
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   490
  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
   491
  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
   492
  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
   493
qed
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   494
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   495
lemma arctan_0_1_bounds_le:
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   496
  assumes "0 \<le> x" "x \<le> 1" "0 < real xl" "real xl \<le> x * x" "x * x \<le> real xu" "real xu \<le> 1"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   497
  shows "arctan x \<in>
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   498
      {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
   499
proof -
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   500
  from assms have "real xl \<le> 1" "sqrt (real xl) \<le> x" "x \<le> sqrt (real xu)" "0 \<le> real xu"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   501
    "0 \<le> real xl" "0 < sqrt (real xl)"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   502
    by (auto intro!: real_le_rsqrt real_le_lsqrt simp: power2_eq_square)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   503
  from arctan_0_1_bounds[OF `0 \<le> real xu`  `real xu \<le> 1`]
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   504
  have "sqrt (real xu) * real (lb_arctan_horner p1 (get_even n) 1 xu) \<le> arctan (sqrt (real xu))"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   505
    by simp
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   506
  from arctan_mult_le[OF `0 \<le> x` `x \<le> sqrt _`  this]
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   507
  have "x * real (lb_arctan_horner p1 (get_even n) 1 xu) \<le> arctan x" .
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   508
  moreover
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   509
  from arctan_0_1_bounds[OF `0 \<le> real xl`  `real xl \<le> 1`]
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   510
  have "arctan (sqrt (real xl)) \<le> sqrt (real xl) * real (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
   511
    by simp
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   512
  from arctan_le_mult[OF `0 < sqrt xl` `sqrt xl \<le> x` this]
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   513
  have "arctan x \<le> x * real (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
   514
  ultimately show ?thesis by simp
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   515
qed
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   516
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   517
lemma mult_nonneg_le_one: fixes a::real assumes "0 \<le> a" "0 \<le> b" "a \<le> 1" "b \<le> 1" shows "a * b \<le> 1"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   518
proof -
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   519
  have "a * b \<le> 1 * 1"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   520
    by (intro mult_mono assms) simp_all
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   521
  thus ?thesis by simp
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   522
qed
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   523
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   524
lemma arctan_0_1_bounds_round:
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   525
  assumes "0 \<le> real x" "real x \<le> 1"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   526
  shows "arctan x \<in>
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   527
      {real x * lb_arctan_horner p1 (get_even n) 1 (float_round_up (Suc p2) (x * x)) ..
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   528
        real x * ub_arctan_horner p3 (get_odd n) 1 (float_round_down (Suc p4) (x * x))}"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   529
  using assms
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   530
  apply (cases "x > 0")
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   531
   apply (intro arctan_0_1_bounds_le)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   532
   apply (auto simp: float_round_down.rep_eq float_round_up.rep_eq
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   533
    intro!: truncate_up_le1 mult_nonneg_le_one truncate_down_le truncate_up_le truncate_down_pos
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   534
      mult_pos_pos)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   535
  done
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   536
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   537
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   538
subsection "Compute \<pi>"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   539
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   540
definition ub_pi :: "nat \<Rightarrow> float" where
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   541
  "ub_pi prec =
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   542
    (let
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   543
      A = rapprox_rat prec 1 5 ;
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   544
      B = lapprox_rat prec 1 239
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   545
    in ((Float 1 2) * float_plus_up prec
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   546
      ((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
   547
        (float_round_down (Suc prec) (A * A)))))
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   548
      (- 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
   549
        (float_round_up (Suc prec) (B * B)))))))"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   550
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   551
definition lb_pi :: "nat \<Rightarrow> float" where
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   552
  "lb_pi prec =
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   553
    (let
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   554
      A = lapprox_rat prec 1 5 ;
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   555
      B = rapprox_rat prec 1 239
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   556
    in ((Float 1 2) * float_plus_down prec
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   557
      ((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
   558
        (float_round_up (Suc prec) (A * A)))))
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   559
      (- 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
   560
        (float_round_down (Suc prec) (B * B)))))))"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   561
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   562
lemma pi_boundaries: "pi \<in> {(lb_pi n) .. (ub_pi n)}"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   563
proof -
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   564
  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
   565
    unfolding machin[symmetric] by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   566
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   567
  { fix prec n :: nat fix k :: int assume "1 < k" hence "0 \<le> k" and "0 < k" and "1 \<le> k" by auto
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   568
    let ?k = "rapprox_rat prec 1 k"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   569
    let ?kl = "float_round_down (Suc prec) (?k * ?k)"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   570
    have "1 div k = 0" using div_pos_pos_trivial[OF _ `1 < k`] by auto
31809
hoelzl
parents: 31790
diff changeset
   571
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   572
    have "0 \<le> real ?k" by (rule order_trans[OF _ rapprox_rat]) (auto simp add: `0 \<le> k`)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   573
    have "real ?k \<le> 1"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   574
      by (auto simp add: `0 < k` `1 \<le> k` less_imp_le
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   575
        intro!: mult_nonneg_le_one order_trans[OF _ rapprox_rat] rapprox_rat_le1)
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   576
    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
   577
    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
   578
    also have "\<dots> \<le> (?k * ub_arctan_horner prec (get_odd n) 1 ?kl)"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   579
      using arctan_0_1_bounds_round[OF `0 \<le> real ?k` `real ?k \<le> 1`]
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   580
      by auto
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   581
    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
   582
  } note ub_arctan = this
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   583
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   584
  { fix prec n :: nat fix k :: int assume "1 < k" hence "0 \<le> k" and "0 < k" by auto
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   585
    let ?k = "lapprox_rat prec 1 k"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   586
    let ?ku = "float_round_up (Suc prec) (?k * ?k)"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   587
    have "1 div k = 0" using div_pos_pos_trivial[OF _ `1 < k`] by auto
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   588
    have "1 / k \<le> 1" using `1 < k` by auto
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   589
    have "0 \<le> real ?k" using lapprox_rat_nonneg[where x=1 and y=k, OF zero_le_one `0 \<le> k`]
58982
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58889
diff changeset
   590
      by (auto simp add: `1 div k = 0`)
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   591
    have "0 \<le> real (?k * ?k)" by simp
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   592
    have "real ?k \<le> 1" using lapprox_rat by (rule order_trans, auto simp add: `1 / k \<le> 1`)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   593
    hence "real (?k * ?k) \<le> 1" using `0 \<le> real ?k` by (auto intro!: mult_nonneg_le_one)
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   594
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   595
    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
   596
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   597
    have "?k * lb_arctan_horner prec (get_even n) 1 ?ku \<le> arctan ?k"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   598
      using arctan_0_1_bounds_round[OF `0 \<le> real ?k` `real ?k \<le> 1`]
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   599
      by auto
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   600
    also have "\<dots> \<le> arctan (1 / k)" using `?k \<le> 1 / k` by (rule arctan_monotone')
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   601
    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
   602
  } note lb_arctan = this
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   603
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   604
  have "pi \<le> ub_pi n "
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   605
    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
   606
    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
   607
    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
   608
      (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
   609
  moreover have "lb_pi n \<le> pi"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   610
    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
   611
    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
   612
    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
   613
      (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
   614
  ultimately show ?thesis by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   615
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   616
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   617
subsection "Compute arcus tangens in the entire domain"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   618
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
   619
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
   620
  "lb_arctan prec x =
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   621
    (let
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   622
      ub_horner = \<lambda> x. float_round_up prec
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   623
        (x *
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   624
          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
   625
      lb_horner = \<lambda> x. float_round_down prec
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   626
        (x *
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   627
          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
   628
    in
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   629
      if x < 0 then - ub_arctan prec (-x)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   630
      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
   631
      else if x \<le> Float 1 1 then
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   632
        Float 1 1 *
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   633
        lb_horner
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   634
          (float_divl prec x
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   635
            (float_plus_up prec 1
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   636
              (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
   637
      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
   638
        if inv > 1 then 0
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   639
        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
   640
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   641
| "ub_arctan prec x =
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
      lb_horner = \<lambda> x. float_round_down prec
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   644
        (x *
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   645
          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
   646
      ub_horner = \<lambda> x. float_round_up prec
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   647
        (x *
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   648
          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
   649
    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
   650
    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
   651
    else if x \<le> Float 1 1 then
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   652
      let y = float_divr prec x
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   653
        (float_plus_down
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   654
          (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
   655
      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
   656
    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
   657
by pat_completeness auto
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   658
termination
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   659
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
   660
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   661
declare ub_arctan_horner.simps[simp del]
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   662
declare lb_arctan_horner.simps[simp del]
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   663
31098
73dd67adf90a replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
hoelzl
parents: 30971
diff changeset
   664
lemma lb_arctan_bound': assumes "0 \<le> real x"
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   665
  shows "lb_arctan prec x \<le> arctan x"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   666
proof -
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   667
  have "\<not> x < 0" and "0 \<le> x"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   668
    using `0 \<le> real x` by (auto intro!: truncate_up_le )
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   669
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   670
  let "?ub_horner x" =
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   671
      "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
   672
    and "?lb_horner x" =
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   673
      "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
   674
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   675
  show ?thesis
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
   676
  proof (cases "x \<le> Float 1 (- 1)")
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   677
    case True hence "real x \<le> 1" by simp
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   678
    from arctan_0_1_bounds_round[OF `0 \<le> real x` `real x \<le> 1`]
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   679
    show ?thesis
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   680
      unfolding lb_arctan.simps Let_def if_not_P[OF `\<not> x < 0`] if_P[OF True] using `0 \<le> x`
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   681
      by (auto intro!: float_round_down_le)
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   682
  next
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   683
    case False hence "0 < real x" by auto
31098
73dd67adf90a replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
hoelzl
parents: 30971
diff changeset
   684
    let ?R = "1 + sqrt (1 + real x * real x)"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   685
    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
   686
    let ?fR = "float_plus_up prec 1 (ub_sqrt prec ?sxx)"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   687
    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
   688
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   689
    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
   690
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   691
    have "sqrt (1 + x*x) \<le> sqrt ?sxx"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   692
      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
   693
    also have "\<dots> \<le> ub_sqrt prec ?sxx"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   694
      using bnds_sqrt'[of ?sxx prec] by auto
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   695
    finally
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   696
    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
   697
    hence "?R \<le> ?fR" by (auto simp: float_plus_up.rep_eq plus_up_def intro!: truncate_up_le)
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   698
    hence "0 < ?fR" and "0 < real ?fR" using `0 < ?R` by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   699
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   700
    have monotone: "?DIV \<le> x / ?R"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   701
    proof -
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   702
      have "?DIV \<le> real x / ?fR" by (rule float_divl)
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   703
      also have "\<dots> \<le> x / ?R" by (rule divide_left_mono[OF `?R \<le> ?fR` `0 \<le> real x` mult_pos_pos[OF order_less_le_trans[OF divisor_gt0 `?R \<le> real ?fR`] divisor_gt0]])
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   704
      finally show ?thesis .
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   705
    qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   706
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   707
    show ?thesis
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   708
    proof (cases "x \<le> Float 1 1")
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   709
      case True
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
   710
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   711
      have "x \<le> sqrt (1 + x * x)" using real_sqrt_sum_squares_ge2[where x=1, unfolded numeral_2_eq_2] by auto
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   712
      also note `\<dots> \<le> (ub_sqrt prec ?sxx)`
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   713
      finally have "real x \<le> ?fR" by (auto simp: float_plus_up.rep_eq plus_up_def intro!: truncate_up_le)
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   714
      moreover have "?DIV \<le> real x / ?fR" by (rule float_divl)
31098
73dd67adf90a replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
hoelzl
parents: 30971
diff changeset
   715
      ultimately have "real ?DIV \<le> 1" unfolding divide_le_eq_1_pos[OF `0 < real ?fR`, symmetric] by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   716
54782
cd8f55c358c5 additional definitions and lemmas for Float
immler
parents: 54489
diff changeset
   717
      have "0 \<le> real ?DIV" using float_divl_lower_bound[OF `0 \<le> x`] `0 < ?fR` unfolding less_eq_float_def by auto
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
   718
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   719
      from arctan_0_1_bounds_round[OF `0 \<le> real (?DIV)` `real (?DIV) \<le> 1`]
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   720
      have "Float 1 1 * ?lb_horner ?DIV \<le> 2 * arctan ?DIV" by simp
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   721
      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
   722
        using arctan_monotone'[OF monotone] by (auto intro!: mult_left_mono arctan_monotone')
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   723
      also have "2 * arctan (x / ?R) = arctan x" using arctan_half[symmetric] unfolding numeral_2_eq_2 power_Suc2 power_0 mult_1_left .
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   724
      finally show ?thesis unfolding lb_arctan.simps Let_def if_not_P[OF `\<not> x < 0`] if_not_P[OF `\<not> x \<le> Float 1 (- 1)`] if_P[OF True]
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   725
        by (auto simp: float_round_down.rep_eq intro!: order_trans[OF mult_left_mono[OF truncate_down]])
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   726
    next
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   727
      case False
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   728
      hence "2 < real x" by auto
31098
73dd67adf90a replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
hoelzl
parents: 30971
diff changeset
   729
      hence "1 \<le> real x" by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   730
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   731
      let "?invx" = "float_divr prec 1 x"
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   732
      have "0 \<le> arctan x" using arctan_monotone'[OF `0 \<le> real x`] using arctan_tan[of 0, unfolded tan_zero] by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   733
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   734
      show ?thesis
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   735
      proof (cases "1 < ?invx")
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
   736
        case True
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
   737
        show ?thesis unfolding lb_arctan.simps Let_def if_not_P[OF `\<not> x < 0`] if_not_P[OF `\<not> x \<le> Float 1 (- 1)`] if_not_P[OF False] if_P[OF True]
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   738
          using `0 \<le> arctan x` by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   739
      next
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
   740
        case False
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   741
        hence "real ?invx \<le> 1" by auto
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
   742
        have "0 \<le> real ?invx" by (rule order_trans[OF _ float_divr], auto simp add: `0 \<le> real x`)
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
   743
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   744
        have "1 / x \<noteq> 0" and "0 < 1 / x" using `0 < real x` by auto
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   745
47601
050718fe6eee use real :: float => real as lifting-morphism so we can directlry use the rep_eq theorems
hoelzl
parents: 47600
diff changeset
   746
        have "arctan (1 / x) \<le> arctan ?invx" unfolding one_float.rep_eq[symmetric] by (rule arctan_monotone', rule float_divr)
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   747
        also have "\<dots> \<le> ?ub_horner ?invx" using arctan_0_1_bounds_round[OF `0 \<le> real ?invx` `real ?invx \<le> 1`]
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   748
          by (auto intro!: float_round_up_le)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   749
        also note float_round_up
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   750
        finally have "pi / 2 - float_round_up prec (?ub_horner ?invx) \<le> arctan x"
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   751
          using `0 \<le> arctan x` arctan_inverse[OF `1 / x \<noteq> 0`]
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
   752
          unfolding real_sgn_pos[OF `0 < 1 / real x`] le_diff_eq by auto
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
   753
        moreover
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
   754
        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
   755
          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
   756
        ultimately
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
   757
        show ?thesis unfolding lb_arctan.simps Let_def if_not_P[OF `\<not> x < 0`] if_not_P[OF `\<not> x \<le> Float 1 (- 1)`] if_not_P[OF `\<not> x \<le> Float 1 1`] if_not_P[OF False]
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   758
          by (auto intro!: float_plus_down_le)
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   759
      qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   760
    qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   761
  qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   762
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   763
31098
73dd67adf90a replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
hoelzl
parents: 30971
diff changeset
   764
lemma ub_arctan_bound': assumes "0 \<le> real x"
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   765
  shows "arctan x \<le> ub_arctan prec x"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   766
proof -
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   767
  have "\<not> x < 0" and "0 \<le> x" using `0 \<le> real x` by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   768
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   769
  let "?ub_horner x" = "float_round_up prec (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
   770
    and "?lb_horner x" = "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
   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)")
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   774
    case True hence "real x \<le> 1" by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   775
    show ?thesis unfolding ub_arctan.simps Let_def if_not_P[OF `\<not> x < 0`] if_P[OF True]
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   776
      using arctan_0_1_bounds_round[OF `0 \<le> real x` `real x \<le> 1`]
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   777
      by (auto intro!: float_round_up_le)
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   778
  next
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   779
    case False hence "0 < real x" by auto
31098
73dd67adf90a replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
hoelzl
parents: 30971
diff changeset
   780
    let ?R = "1 + sqrt (1 + real x * real x)"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   781
    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
   782
    let ?fR = "float_plus_down (Suc prec) 1 (lb_sqrt prec ?sxx)"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   783
    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
   784
31098
73dd67adf90a replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
hoelzl
parents: 30971
diff changeset
   785
    have sqr_ge0: "0 \<le> 1 + real x * real x" using sum_power2_ge_zero[of 1 "real x", unfolded numeral_2_eq_2] by auto
73dd67adf90a replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
hoelzl
parents: 30971
diff changeset
   786
    hence "0 \<le> real (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
   787
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   788
    hence divisor_gt0: "0 < ?R" by (auto intro: add_pos_nonneg)
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   789
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   790
    have "lb_sqrt prec ?sxx \<le> sqrt ?sxx"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   791
      using bnds_sqrt'[of ?sxx] by auto
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   792
    also have "\<dots> \<le> sqrt (1 + x*x)"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   793
      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
   794
    finally have "lb_sqrt prec ?sxx \<le> sqrt (1 + x*x)" .
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   795
    hence "?fR \<le> ?R" by (auto simp: float_plus_down.rep_eq plus_down_def truncate_down_le)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   796
    have "0 < real ?fR"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   797
      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
   798
        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
   799
        truncate_down_nonneg add_nonneg_nonneg)
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   800
    have monotone: "x / ?R \<le> (float_divr prec x ?fR)"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   801
    proof -
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   802
      from divide_left_mono[OF `?fR \<le> ?R` `0 \<le> real x` mult_pos_pos[OF divisor_gt0 `0 < real ?fR`]]
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   803
      have "x / ?R \<le> x / ?fR" .
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   804
      also have "\<dots> \<le> ?DIV" by (rule float_divr)
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   805
      finally show ?thesis .
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   806
    qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   807
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   808
    show ?thesis
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   809
    proof (cases "x \<le> Float 1 1")
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   810
      case True
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   811
      show ?thesis
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   812
      proof (cases "?DIV > 1")
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
   813
        case True
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
   814
        have "pi / 2 \<le> ub_pi prec * Float 1 (- 1)" 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
   815
        from order_less_le_trans[OF arctan_ubound this, THEN less_imp_le]
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
   816
        show ?thesis unfolding ub_arctan.simps Let_def if_not_P[OF `\<not> x < 0`] if_not_P[OF `\<not> x \<le> Float 1 (- 1)`] if_P[OF `x \<le> Float 1 1`] if_P[OF True] .
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   817
      next
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
   818
        case False
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   819
        hence "real ?DIV \<le> 1" by auto
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
   820
44349
f057535311c5 remove redundant lemma real_0_le_divide_iff in favor or zero_le_divide_iff
huffman
parents: 44306
diff changeset
   821
        have "0 \<le> x / ?R" using `0 \<le> real x` `0 < ?R` unfolding zero_le_divide_iff by auto
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
   822
        hence "0 \<le> real ?DIV" using monotone by (rule order_trans)
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
   823
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   824
        have "arctan x = 2 * arctan (x / ?R)" using arctan_half unfolding numeral_2_eq_2 power_Suc2 power_0 mult_1_left .
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   825
        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
   826
          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
   827
        also have "\<dots> \<le> (Float 1 1 * ?ub_horner ?DIV)" unfolding Float_num
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   828
          using arctan_0_1_bounds_round[OF `0 \<le> real ?DIV` `real ?DIV \<le> 1`]
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   829
          by (auto intro!: float_round_up_le)
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
   830
        finally show ?thesis unfolding ub_arctan.simps Let_def if_not_P[OF `\<not> x < 0`] if_not_P[OF `\<not> x \<le> Float 1 (- 1)`] if_P[OF `x \<le> Float 1 1`] if_not_P[OF False] .
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   831
      qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   832
    next
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   833
      case False
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   834
      hence "2 < real x" by auto
31098
73dd67adf90a replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
hoelzl
parents: 30971
diff changeset
   835
      hence "1 \<le> real x" by auto
73dd67adf90a replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
hoelzl
parents: 30971
diff changeset
   836
      hence "0 < real x" by auto
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   837
      hence "0 < x" by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   838
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   839
      let "?invx" = "float_divl prec 1 x"
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   840
      have "0 \<le> arctan x" using arctan_monotone'[OF `0 \<le> real x`] using arctan_tan[of 0, unfolded tan_zero] by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   841
31098
73dd67adf90a replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
hoelzl
parents: 30971
diff changeset
   842
      have "real ?invx \<le> 1" unfolding less_float_def by (rule order_trans[OF float_divl], auto simp add: `1 \<le> real x` divide_le_eq_1_pos[OF `0 < real x`])
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   843
      have "0 \<le> real ?invx" using `0 < x` by (intro float_divl_lower_bound) 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
   844
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   845
      have "1 / x \<noteq> 0" and "0 < 1 / x" using `0 < real x` by auto
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   846
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   847
      have "(?lb_horner ?invx) \<le> arctan (?invx)" using arctan_0_1_bounds_round[OF `0 \<le> real ?invx` `real ?invx \<le> 1`]
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   848
        by (auto intro!: 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
   849
      also have "\<dots> \<le> arctan (1 / x)" 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
   850
      finally have "arctan x \<le> pi / 2 - (?lb_horner ?invx)"
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   851
        using `0 \<le> arctan x` arctan_inverse[OF `1 / x \<noteq> 0`]
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   852
        unfolding real_sgn_pos[OF `0 < 1 / x`] le_diff_eq by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   853
      moreover
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
   854
      have "pi / 2 \<le> ub_pi prec * Float 1 (- 1)" unfolding Float_num times_divide_eq_right mult_1_right using pi_boundaries by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   855
      ultimately
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
   856
      show ?thesis unfolding ub_arctan.simps Let_def if_not_P[OF `\<not> x < 0`]if_not_P[OF `\<not> x \<le> Float 1 (- 1)`] if_not_P[OF False]
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   857
        by (auto intro!: float_round_up_le float_plus_up_le)
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   858
    qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   859
  qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   860
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   861
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   862
lemma arctan_boundaries:
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   863
  "arctan x \<in> {(lb_arctan prec x) .. (ub_arctan prec x)}"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   864
proof (cases "0 \<le> x")
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   865
  case True hence "0 \<le> real x" by auto
31098
73dd67adf90a replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
hoelzl
parents: 30971
diff changeset
   866
  show ?thesis using ub_arctan_bound'[OF `0 \<le> real x`] lb_arctan_bound'[OF `0 \<le> real x`] unfolding atLeastAtMost_iff by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   867
next
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   868
  let ?mx = "-x"
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   869
  case False hence "x < 0" and "0 \<le> real ?mx" by auto
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   870
  hence bounds: "lb_arctan prec ?mx \<le> arctan ?mx \<and> arctan ?mx \<le> ub_arctan prec ?mx"
31098
73dd67adf90a replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
hoelzl
parents: 30971
diff changeset
   871
    using ub_arctan_bound'[OF `0 \<le> real ?mx`] lb_arctan_bound'[OF `0 \<le> real ?mx`] by auto
47601
050718fe6eee use real :: float => real as lifting-morphism so we can directlry use the rep_eq theorems
hoelzl
parents: 47600
diff changeset
   872
  show ?thesis unfolding minus_float.rep_eq arctan_minus lb_arctan.simps[where x=x] ub_arctan.simps[where x=x] Let_def if_P[OF `x < 0`]
050718fe6eee use real :: float => real as lifting-morphism so we can directlry use the rep_eq theorems
hoelzl
parents: 47600
diff changeset
   873
    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
   874
    by (simp add: arctan_minus)
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   875
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   876
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   877
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
   878
proof (rule allI, rule allI, rule allI, rule impI)
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   879
  fix x :: real fix lx ux
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   880
  assume "(l, u) = (lb_arctan prec lx, ub_arctan prec ux) \<and> x \<in> {lx .. ux}"
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   881
  hence l: "lb_arctan prec lx = l " and u: "ub_arctan prec ux = u" and x: "x \<in> {lx .. ux}" by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   882
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   883
  { from arctan_boundaries[of lx prec, unfolded l]
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   884
    have "l \<le> arctan lx" by (auto simp del: lb_arctan.simps)
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   885
    also have "\<dots> \<le> arctan x" using x by (auto intro: arctan_monotone')
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   886
    finally have "l \<le> arctan x" .
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   887
  } moreover
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   888
  { have "arctan x \<le> arctan ux" using x by (auto intro: arctan_monotone')
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   889
    also have "\<dots> \<le> u" using arctan_boundaries[of ux prec, unfolded u] by (auto simp del: ub_arctan.simps)
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   890
    finally have "arctan x \<le> u" .
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   891
  } ultimately show "l \<le> arctan x \<and> arctan x \<le> u" ..
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   892
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   893
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   894
section "Sinus and Cosinus"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   895
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   896
subsection "Compute the cosinus and sinus series"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   897
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   898
fun ub_sin_cos_aux :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> float \<Rightarrow> float"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   899
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
   900
  "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
   901
| "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
   902
    (rapprox_rat prec 1 k) (-
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   903
      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
   904
| "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
   905
| "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
   906
    (lapprox_rat prec 1 k) (-
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   907
      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
   908
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   909
lemma cos_aux:
59730
b7c394c7a619 The factorial function, "fact", now has type "nat => 'a"
paulson <lp15@cam.ac.uk>
parents: 59658
diff changeset
   910
  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
   911
  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
   912
proof -
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
   913
  have "0 \<le> real (x * x)" by auto
59730
b7c394c7a619 The factorial function, "fact", now has type "nat => 'a"
paulson <lp15@cam.ac.uk>
parents: 59658
diff changeset
   914
  let "?f n" = "fact (2 * n) :: nat"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   915
31809
hoelzl
parents: 31790
diff changeset
   916
  { fix n
45129
1fce03e3e8ad tuned proofs -- eliminated vacuous "induct arbitrary: ..." situations;
wenzelm
parents: 44821
diff changeset
   917
    have F: "\<And>m. ((\<lambda>i. i + 2) ^^ n) m = m + 2 * n" by (induct n) auto
30971
7fbebf75b3ef funpow and relpow with shared "^^" syntax
haftmann
parents: 30968
diff changeset
   918
    have "?f (Suc n) = ?f n * ((\<lambda>i. i + 2) ^^ n) 1 * (((\<lambda>i. i + 2) ^^ n) 1 + 1)"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   919
      unfolding F by auto } note f_eq = this
31809
hoelzl
parents: 31790
diff changeset
   920
hoelzl
parents: 31790
diff changeset
   921
  from horner_bounds[where lb="lb_sin_cos_aux prec" and ub="ub_sin_cos_aux prec" and j'=0,
31098
73dd67adf90a replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
hoelzl
parents: 30971
diff changeset
   922
    OF `0 \<le> real (x * x)` f_eq lb_sin_cos_aux.simps ub_sin_cos_aux.simps]
73dd67adf90a replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
hoelzl
parents: 30971
diff changeset
   923
  show "?lb" and "?ub" by (auto simp add: power_mult power2_eq_square[of "real x"])
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   924
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   925
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   926
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
   927
  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
   928
    (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
   929
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   930
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
   931
  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
   932
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   933
lemma cos_boundaries: assumes "0 \<le> real x" and "x \<le> pi / 2"
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   934
  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))}"
31098
73dd67adf90a replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
hoelzl
parents: 30971
diff changeset
   935
proof (cases "real x = 0")
73dd67adf90a replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
hoelzl
parents: 30971
diff changeset
   936
  case False hence "real x \<noteq> 0" by auto
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   937
  hence "0 < x" and "0 < real x" using `0 \<le> real x` by auto
56544
b60d5d119489 made mult_pos_pos a simp rule
nipkow
parents: 56541
diff changeset
   938
  have "0 < x * x" using `0 < x` by simp
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   939
59730
b7c394c7a619 The factorial function, "fact", now has type "nat => 'a"
paulson <lp15@cam.ac.uk>
parents: 59658
diff changeset
   940
  { fix x n have "(\<Sum> i=0..<n. (-1::real) ^ i * (1/(fact (2 * i))) * x ^ (2 * i))
b7c394c7a619 The factorial function, "fact", now has type "nat => 'a"
paulson <lp15@cam.ac.uk>
parents: 59658
diff changeset
   941
    = (\<Sum> i = 0 ..< 2 * n. (if even(i) then ((- 1) ^ (i div 2))/((fact i)) else 0) * x ^ i)" (is "?sum = ?ifsum")
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   942
  proof -
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   943
    have "?sum = ?sum + (\<Sum> j = 0 ..< n. 0)" by auto
31809
hoelzl
parents: 31790
diff changeset
   944
    also have "\<dots> =
59730
b7c394c7a619 The factorial function, "fact", now has type "nat => 'a"
paulson <lp15@cam.ac.uk>
parents: 59658
diff changeset
   945
      (\<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
   946
    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
   947
      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
   948
    also have "\<dots> = (\<Sum> i = 0 ..< 2 * n. (if even i then (- 1) ^ (i div 2) / ((fact i)) else 0) * x ^ i)"
57418
6ab1c7cb0b8d fact consolidation
haftmann
parents: 56923
diff changeset
   949
      by (rule setsum.cong) auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   950
    finally show ?thesis by assumption
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   951
  qed } note morph_to_if_power = this
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   952
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   953
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   954
  { fix n :: nat assume "0 < n"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   955
    hence "0 < 2 * n" by auto
31098
73dd67adf90a replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
hoelzl
parents: 30971
diff changeset
   956
    obtain t where "0 < t" and "t < real x" and
59730
b7c394c7a619 The factorial function, "fact", now has type "nat => 'a"
paulson <lp15@cam.ac.uk>
parents: 59658
diff changeset
   957
      cos_eq: "cos x = (\<Sum> i = 0 ..< 2 * n. (if even(i) then ((- 1) ^ (i div 2))/((fact i)) else 0) * (real x) ^ i)
b7c394c7a619 The factorial function, "fact", now has type "nat => 'a"
paulson <lp15@cam.ac.uk>
parents: 59658
diff changeset
   958
      + (cos (t + 1/2 * (2 * n) * pi) / (fact (2*n))) * (real x)^(2*n)"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   959
      (is "_ = ?SUM + ?rest / ?fact * ?pow")
44306
33572a766836 fold definitions of sin_coeff and cos_coeff in Maclaurin lemmas
huffman
parents: 44305
diff changeset
   960
      using Maclaurin_cos_expansion2[OF `0 < real x` `0 < 2 * n`]
56195
c7dfd924a165 adapt to Isabelle/c726ecfb22b6
huffman
parents: 56073
diff changeset
   961
      unfolding cos_coeff_def atLeast0LessThan by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   962
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
   963
    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
   964
    also have "\<dots> = cos (t + n * pi)" by (simp add: cos_add)
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   965
    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
   966
    finally have "cos t * (- 1) ^ n = ?rest" .
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   967
    moreover
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   968
    have "t \<le> pi / 2" using `t < real x` and `x \<le> pi / 2` by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   969
    hence "0 \<le> cos t" using `0 < t` and cos_ge_zero by auto
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   970
    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
   971
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   972
    have "0 < ?fact" by auto
31098
73dd67adf90a replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
hoelzl
parents: 30971
diff changeset
   973
    have "0 < ?pow" using `0 < real x` by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   974
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   975
    {
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   976
      assume "even n"
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   977
      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
   978
        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
   979
      also have "\<dots> \<le> cos x"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   980
      proof -
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
   981
        from even[OF `even n`] `0 < ?fact` `0 < ?pow`
56571
f4635657d66f added divide_nonneg_nonneg and co; made it a simp rule
hoelzl
parents: 56544
diff changeset
   982
        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
   983
        thus ?thesis unfolding cos_eq by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   984
      qed
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   985
      finally have "(lb_sin_cos_aux prec n 1 1 (x * x)) \<le> cos x" .
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   986
    } note lb = this
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   987
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   988
    {
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   989
      assume "odd n"
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   990
      have "cos x \<le> ?SUM"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   991
      proof -
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
   992
        from `0 < ?fact` and `0 < ?pow` and odd[OF `odd n`]
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
   993
        have "0 \<le> (- ?rest) / ?fact * ?pow"
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
   994
          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
   995
        thus ?thesis unfolding cos_eq by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
   996
      qed
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
   997
      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
   998
        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
   999
      finally have "cos x \<le> (ub_sin_cos_aux prec n 1 1 (x * x))" .
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1000
    } note ub = this and lb
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1001
  } note ub = this(1) and lb = this(2)
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1002
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1003
  have "cos x \<le> (ub_sin_cos_aux prec (get_odd n) 1 1 (x * x))" using ub[OF odd_pos[OF get_odd] get_odd] .
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1004
  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
  1005
  proof (cases "0 < get_even n")
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1006
    case True show ?thesis using lb[OF True get_even] .
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1007
  next
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1008
    case False
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1009
    hence "get_even n = 0" by auto
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1010
    have "- (pi / 2) \<le> x" by (rule order_trans[OF _ `0 < real x`[THEN less_imp_le]], auto)
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1011
    with `x \<le> pi / 2`
47601
050718fe6eee use real :: float => real as lifting-morphism so we can directlry use the rep_eq theorems
hoelzl
parents: 47600
diff changeset
  1012
    show ?thesis unfolding `get_even n = 0` lb_sin_cos_aux.simps minus_float.rep_eq zero_float.rep_eq using cos_ge_zero by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1013
  qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1014
  ultimately show ?thesis by auto
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1015
next
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1016
  case True
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1017
  hence "x = 0"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1018
    by transfer
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1019
  thus ?thesis
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1020
    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
  1021
    by simp
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1022
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1023
31098
73dd67adf90a replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
hoelzl
parents: 30971
diff changeset
  1024
lemma sin_aux: assumes "0 \<le> real x"
59730
b7c394c7a619 The factorial function, "fact", now has type "nat => 'a"
paulson <lp15@cam.ac.uk>
parents: 59658
diff changeset
  1025
  shows "(x * lb_sin_cos_aux prec n 2 1 (x * x)) \<le> (\<Sum> i=0..<n. (- 1) ^ i * (1/(fact (2 * i + 1))) * x^(2 * i + 1))" (is "?lb")
b7c394c7a619 The factorial function, "fact", now has type "nat => 'a"
paulson <lp15@cam.ac.uk>
parents: 59658
diff changeset
  1026
  and "(\<Sum> i=0..<n. (- 1) ^ i * (1/(fact (2 * i + 1))) * x^(2 * i + 1)) \<le> (x * ub_sin_cos_aux prec n 2 1 (x * x))" (is "?ub")
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1027
proof -
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1028
  have "0 \<le> real (x * x)" by auto
59730
b7c394c7a619 The factorial function, "fact", now has type "nat => 'a"
paulson <lp15@cam.ac.uk>
parents: 59658
diff changeset
  1029
  let "?f n" = "fact (2 * n + 1) :: nat"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1030
31809
hoelzl
parents: 31790
diff changeset
  1031
  { fix n
45129
1fce03e3e8ad tuned proofs -- eliminated vacuous "induct arbitrary: ..." situations;
wenzelm
parents: 44821
diff changeset
  1032
    have F: "\<And>m. ((\<lambda>i. i + 2) ^^ n) m = m + 2 * n" by (induct n) auto
30971
7fbebf75b3ef funpow and relpow with shared "^^" syntax
haftmann
parents: 30968
diff changeset
  1033
    have "?f (Suc n) = ?f n * ((\<lambda>i. i + 2) ^^ n) 2 * (((\<lambda>i. i + 2) ^^ n) 2 + 1)"
59751
916c0f6c83e3 New material for complex sin, cos, tan, Ln, also some reorganisation
paulson <lp15@cam.ac.uk>
parents: 59741
diff changeset
  1034
      unfolding F by auto }
59730
b7c394c7a619 The factorial function, "fact", now has type "nat => 'a"
paulson <lp15@cam.ac.uk>
parents: 59658
diff changeset
  1035
  note f_eq = this
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1036
  from horner_bounds[where lb="lb_sin_cos_aux prec" and ub="ub_sin_cos_aux prec" and j'=0,
31098
73dd67adf90a replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
hoelzl
parents: 30971
diff changeset
  1037
    OF `0 \<le> real (x * x)` f_eq lb_sin_cos_aux.simps ub_sin_cos_aux.simps]
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1038
  show "?lb" and "?ub" using `0 \<le> real x`
57512
cc97b347b301 reduced name variants for assoc and commute on plus and mult
haftmann
parents: 57418
diff changeset
  1039
    unfolding power_add power_one_right mult.assoc[symmetric] setsum_left_distrib[symmetric]
59730
b7c394c7a619 The factorial function, "fact", now has type "nat => 'a"
paulson <lp15@cam.ac.uk>
parents: 59658
diff changeset
  1040
    unfolding mult.commute[where 'a=real] real_fact_nat
31098
73dd67adf90a replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
hoelzl
parents: 30971
diff changeset
  1041
    by (auto intro!: mult_left_mono simp add: power_mult power2_eq_square[of "real x"])
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1042
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1043
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1044
lemma sin_boundaries: assumes "0 \<le> real x" and "x \<le> pi / 2"
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1045
  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))}"
31098
73dd67adf90a replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
hoelzl
parents: 30971
diff changeset
  1046
proof (cases "real x = 0")
73dd67adf90a replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
hoelzl
parents: 30971
diff changeset
  1047
  case False hence "real x \<noteq> 0" by auto
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  1048
  hence "0 < x" and "0 < real x" using `0 \<le> real x` by auto
56544
b60d5d119489 made mult_pos_pos a simp rule
nipkow
parents: 56541
diff changeset
  1049
  have "0 < x * x" using `0 < x` by simp
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1050
59751
916c0f6c83e3 New material for complex sin, cos, tan, Ln, also some reorganisation
paulson <lp15@cam.ac.uk>
parents: 59741
diff changeset
  1051
  { fix x::real and n
59730
b7c394c7a619 The factorial function, "fact", now has type "nat => 'a"
paulson <lp15@cam.ac.uk>
parents: 59658
diff changeset
  1052
    have "(\<Sum>j = 0 ..< n. (- 1) ^ (((2 * j + 1) - Suc 0) div 2) / ((fact (2 * j + 1))) * x ^(2 * j + 1))
b7c394c7a619 The factorial function, "fact", now has type "nat => 'a"
paulson <lp15@cam.ac.uk>
parents: 59658
diff changeset
  1053
    = (\<Sum> i = 0 ..< 2 * n. (if even(i) then 0 else ((- 1) ^ ((i - Suc 0) div 2))/((fact i))) * x ^ i)" (is "?SUM = _")
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1054
    proof -
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1055
      have pow: "!!i. x ^ (2 * i + 1) = x * x ^ (2 * i)" by auto
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1056
      have "?SUM = (\<Sum> j = 0 ..< n. 0) + ?SUM" by auto
59730
b7c394c7a619 The factorial function, "fact", now has type "nat => 'a"
paulson <lp15@cam.ac.uk>
parents: 59658
diff changeset
  1057
      also have "\<dots> = (\<Sum> i = 0 ..< 2 * n. if even i then 0 else (- 1) ^ ((i - Suc 0) div 2) / ((fact i)) * x ^ i)"
56195
c7dfd924a165 adapt to Isabelle/c726ecfb22b6
huffman
parents: 56073
diff changeset
  1058
        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
  1059
      also have "\<dots> = (\<Sum> i = 0 ..< 2 * n. (if even i then 0 else (- 1) ^ ((i - Suc 0) div 2) / ((fact i))) * x ^ i)"
57418
6ab1c7cb0b8d fact consolidation
haftmann
parents: 56923
diff changeset
  1060
        by (rule setsum.cong) auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1061
      finally show ?thesis by assumption
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1062
    qed } note setsum_morph = this
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1063
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1064
  { fix n :: nat assume "0 < n"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1065
    hence "0 < 2 * n + 1" by auto
31098
73dd67adf90a replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
hoelzl
parents: 30971
diff changeset
  1066
    obtain t where "0 < t" and "t < real x" and
59730
b7c394c7a619 The factorial function, "fact", now has type "nat => 'a"
paulson <lp15@cam.ac.uk>
parents: 59658
diff changeset
  1067
      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 x) ^ i)
b7c394c7a619 The factorial function, "fact", now has type "nat => 'a"
paulson <lp15@cam.ac.uk>
parents: 59658
diff changeset
  1068
      + (sin (t + 1/2 * (2 * n + 1) * pi) / (fact (2*n + 1))) * (real x)^(2*n + 1)"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1069
      (is "_ = ?SUM + ?rest / ?fact * ?pow")
44306
33572a766836 fold definitions of sin_coeff and cos_coeff in Maclaurin lemmas
huffman
parents: 44305
diff changeset
  1070
      using Maclaurin_sin_expansion3[OF `0 < 2 * n + 1` `0 < real x`]
56195
c7dfd924a165 adapt to Isabelle/c726ecfb22b6
huffman
parents: 56073
diff changeset
  1071
      unfolding sin_coeff_def atLeast0LessThan by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1072
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
  1073
    have "?rest = cos t * (- 1) ^ n" unfolding sin_add cos_add real_of_nat_add distrib_right distrib_left by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1074
    moreover
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1075
    have "t \<le> pi / 2" using `t < real x` and `x \<le> pi / 2` by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1076
    hence "0 \<le> cos t" using `0 < t` and cos_ge_zero by auto
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1077
    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
  1078
44305
3bdc02eb1637 remove some redundant simp rules
huffman
parents: 42361
diff changeset
  1079
    have "0 < ?fact" by (simp del: fact_Suc)
31098
73dd67adf90a replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
hoelzl
parents: 30971
diff changeset
  1080
    have "0 < ?pow" using `0 < real x` by (rule zero_less_power)
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1081
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1082
    {
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1083
      assume "even n"
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1084
      have "(x * lb_sin_cos_aux prec n 2 1 (x * x)) \<le>
59730
b7c394c7a619 The factorial function, "fact", now has type "nat => 'a"
paulson <lp15@cam.ac.uk>
parents: 59658
diff changeset
  1085
            (\<Sum> i = 0 ..< 2 * n. (if even(i) then 0 else ((- 1) ^ ((i - Suc 0) div 2))/((fact i))) * (real x) ^ i)"
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  1086
        using sin_aux[OF `0 \<le> real x`] unfolding setsum_morph[symmetric] by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1087
      also have "\<dots> \<le> ?SUM" by auto
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1088
      also have "\<dots> \<le> sin x"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1089
      proof -
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  1090
        from even[OF `even n`] `0 < ?fact` `0 < ?pow`
56571
f4635657d66f added divide_nonneg_nonneg and co; made it a simp rule
hoelzl
parents: 56544
diff changeset
  1091
        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
  1092
        thus ?thesis unfolding sin_eq by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1093
      qed
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1094
      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
  1095
    } note lb = this
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1096
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1097
    {
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1098
      assume "odd n"
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1099
      have "sin x \<le> ?SUM"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1100
      proof -
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  1101
        from `0 < ?fact` and `0 < ?pow` and odd[OF `odd n`]
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  1102
        have "0 \<le> (- ?rest) / ?fact * ?pow"
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  1103
          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
  1104
        thus ?thesis unfolding sin_eq by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1105
      qed
59730
b7c394c7a619 The factorial function, "fact", now has type "nat => 'a"
paulson <lp15@cam.ac.uk>
parents: 59658
diff changeset
  1106
      also have "\<dots> \<le> (\<Sum> i = 0 ..< 2 * n. (if even(i) then 0 else ((- 1) ^ ((i - Suc 0) div 2))/((fact i))) * (real x) ^ i)"
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  1107
         by auto
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1108
      also have "\<dots> \<le> (x * ub_sin_cos_aux prec n 2 1 (x * x))"
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  1109
        using sin_aux[OF `0 \<le> real x`] unfolding setsum_morph[symmetric] by auto
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1110
      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
  1111
    } note ub = this and lb
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1112
  } note ub = this(1) and lb = this(2)
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1113
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1114
  have "sin x \<le> (x * ub_sin_cos_aux prec (get_odd n) 2 1 (x * x))" using ub[OF odd_pos[OF get_odd] get_odd] .
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1115
  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
  1116
  proof (cases "0 < get_even n")
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1117
    case True show ?thesis using lb[OF True get_even] .
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1118
  next
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1119
    case False
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1120
    hence "get_even n = 0" by auto
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1121
    with `x \<le> pi / 2` `0 \<le> real x`
47601
050718fe6eee use real :: float => real as lifting-morphism so we can directlry use the rep_eq theorems
hoelzl
parents: 47600
diff changeset
  1122
    show ?thesis unfolding `get_even n = 0` ub_sin_cos_aux.simps minus_float.rep_eq using sin_ge_zero by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1123
  qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1124
  ultimately show ?thesis by auto
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1125
next
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1126
  case True
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1127
  show ?thesis
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1128
  proof (cases "n = 0")
31809
hoelzl
parents: 31790
diff changeset
  1129
    case True
31098
73dd67adf90a replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
hoelzl
parents: 30971
diff changeset
  1130
    thus ?thesis unfolding `n = 0` get_even_def get_odd_def using `real x = 0` lapprox_rat[where x="-1" and y=1] by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1131
  next
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1132
    case False with not0_implies_Suc obtain m where "n = Suc m" by blast
31098
73dd67adf90a replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
hoelzl
parents: 30971
diff changeset
  1133
    thus ?thesis unfolding `n = Suc m` get_even_def get_odd_def using `real x = 0` rapprox_rat[where x=1 and y=1] lapprox_rat[where x=1 and y=1] by (cases "even (Suc m)", auto)
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1134
  qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1135
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1136
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1137
subsection "Compute the cosinus in the entire domain"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1138
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1139
definition lb_cos :: "nat \<Rightarrow> float \<Rightarrow> float" where
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1140
"lb_cos prec x = (let
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1141
    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
  1142
    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
  1143
  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
  1144
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
  1145
                       else half (half (horner (x * Float 1 (- 2)))))"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1146
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1147
definition ub_cos :: "nat \<Rightarrow> float \<Rightarrow> float" where
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1148
"ub_cos prec x = (let
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1149
    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
  1150
    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
  1151
  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
  1152
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
  1153
                       else half (half (horner (x * Float 1 (- 2)))))"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1154
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1155
lemma lb_cos: assumes "0 \<le> real x" and "x \<le> pi"
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1156
  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
  1157
proof -
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1158
  { fix x :: real
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1159
    have "cos x = cos (x / 2 + x / 2)" by auto
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1160
    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
  1161
      unfolding cos_add by auto
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1162
    also have "\<dots> = 2 * cos (x / 2) * cos (x / 2) - 1" by algebra
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1163
    finally have "cos x = 2 * cos (x / 2) * cos (x / 2) - 1" .
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1164
  } note x_half = this[symmetric]
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1165
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  1166
  have "\<not> x < 0" using `0 \<le> real x` by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1167
  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
  1168
  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
  1169
  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
  1170
  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
  1171
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1172
  show ?thesis
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
  1173
  proof (cases "x < Float 1 (- 1)")
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  1174
    case True hence "x \<le> pi / 2" using pi_ge_two by auto
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
  1175
    show ?thesis unfolding lb_cos_def[where x=x] ub_cos_def[where x=x] if_not_P[OF `\<not> x < 0`] if_P[OF `x < Float 1 (- 1)`] Let_def
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1176
      using cos_boundaries[OF `0 \<le> real x` `x \<le> pi / 2`] .
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1177
  next
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1178
    case False
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
  1179
    { fix y x :: float let ?x2 = "(x * Float 1 (- 1))"
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1180
      assume "y \<le> cos ?x2" and "-pi \<le> x" and "x \<le> pi"
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1181
      hence "- (pi / 2) \<le> ?x2" and "?x2 \<le> pi / 2" using pi_ge_two unfolding Float_num by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1182
      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
  1183
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1184
      have "(?lb_half y) \<le> cos x"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1185
      proof (cases "y < 0")
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  1186
        case True show ?thesis using cos_ge_minus_one unfolding if_P[OF True] by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1187
      next
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  1188
        case False
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  1189
        hence "0 \<le> real y" by auto
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1190
        from mult_mono[OF `y \<le> cos ?x2` `y \<le> cos ?x2` `0 \<le> cos ?x2` this]
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  1191
        have "real y * real y \<le> cos ?x2 * cos ?x2" .
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  1192
        hence "2 * real y * real y \<le> 2 * cos ?x2 * cos ?x2" by auto
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1193
        hence "2 * real y * real y - 1 \<le> 2 * cos (x / 2) * cos (x / 2) - 1" unfolding Float_num by auto
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1194
        thus ?thesis unfolding if_not_P[OF False] x_half Float_num
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1195
          by (auto intro!: float_plus_down_le)
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1196
      qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1197
    } 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
  1198
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
  1199
    { fix y x :: float let ?x2 = "(x * Float 1 (- 1))"
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1200
      assume ub: "cos ?x2 \<le> y" and "- pi \<le> x" and "x \<le> pi"
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1201
      hence "- (pi / 2) \<le> ?x2" and "?x2 \<le> pi / 2" using pi_ge_two unfolding Float_num by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1202
      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
  1203
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1204
      have "cos x \<le> (?ub_half y)"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1205
      proof -
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  1206
        have "0 \<le> real y" using `0 \<le> cos ?x2` ub by (rule order_trans)
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  1207
        from mult_mono[OF ub ub this `0 \<le> cos ?x2`]
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  1208
        have "cos ?x2 * cos ?x2 \<le> real y * real y" .
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  1209
        hence "2 * cos ?x2 * cos ?x2 \<le> 2 * real y * real y" by auto
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1210
        hence "2 * cos (x / 2) * cos (x / 2) - 1 \<le> 2 * real y * real y - 1" unfolding Float_num by auto
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1211
        thus ?thesis unfolding x_half Float_num
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1212
          by (auto intro!: float_plus_up_le)
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1213
      qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1214
    } 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
  1215
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
  1216
    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
  1217
    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
  1218
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1219
    have "-pi \<le> x" using pi_ge_zero[THEN le_imp_neg_le, unfolded minus_zero] `0 \<le> real x` 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
  1220
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1221
    show ?thesis
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1222
    proof (cases "x < 1")
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  1223
      case True hence "real x \<le> 1" by auto
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1224
      have "0 \<le> real ?x2" and "?x2 \<le> pi / 2" using pi_ge_two `0 \<le> real x` using assms by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1225
      from cos_boundaries[OF this]
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1226
      have lb: "(?lb_horner ?x2) \<le> ?cos ?x2" and ub: "?cos ?x2 \<le> (?ub_horner ?x2)" by auto
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1227
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1228
      have "(?lb x) \<le> ?cos x"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1229
      proof -
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1230
        from lb_half[OF lb `-pi \<le> x` `x \<le> pi`]
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
  1231
        show ?thesis unfolding lb_cos_def[where x=x] Let_def using `\<not> x < 0` `\<not> x < Float 1 (- 1)` `x < 1` by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1232
      qed
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1233
      moreover have "?cos x \<le> (?ub x)"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1234
      proof -
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1235
        from ub_half[OF ub `-pi \<le> x` `x \<le> pi`]
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
  1236
        show ?thesis unfolding ub_cos_def[where x=x] Let_def using `\<not> x < 0` `\<not> x < Float 1 (- 1)` `x < 1` by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1237
      qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1238
      ultimately show ?thesis by auto
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1239
    next
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1240
      case False
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1241
      have "0 \<le> real ?x4" and "?x4 \<le> pi / 2" using pi_ge_two `0 \<le> real x` `x \<le> pi` unfolding Float_num by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1242
      from cos_boundaries[OF this]
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1243
      have lb: "(?lb_horner ?x4) \<le> ?cos ?x4" and ub: "?cos ?x4 \<le> (?ub_horner ?x4)" 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
  1244
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
  1245
      have eq_4: "?x2 * Float 1 (- 1) = x * Float 1 (- 2)" 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
  1246
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1247
      have "(?lb x) \<le> ?cos x"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1248
      proof -
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1249
        have "-pi \<le> ?x2" and "?x2 \<le> pi" using pi_ge_two `0 \<le> real x` `x \<le> pi` by auto
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1250
        from lb_half[OF lb_half[OF lb this] `-pi \<le> x` `x \<le> pi`, unfolded eq_4]
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
  1251
        show ?thesis unfolding lb_cos_def[where x=x] if_not_P[OF `\<not> x < 0`] if_not_P[OF `\<not> x < Float 1 (- 1)`] if_not_P[OF `\<not> x < 1`] Let_def .
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1252
      qed
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1253
      moreover have "?cos x \<le> (?ub x)"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1254
      proof -
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1255
        have "-pi \<le> ?x2" and "?x2 \<le> pi" using pi_ge_two `0 \<le> real x` ` x \<le> pi` by auto
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1256
        from ub_half[OF ub_half[OF ub this] `-pi \<le> x` `x \<le> pi`, unfolded eq_4]
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
  1257
        show ?thesis unfolding ub_cos_def[where x=x] if_not_P[OF `\<not> x < 0`] if_not_P[OF `\<not> x < Float 1 (- 1)`] if_not_P[OF `\<not> x < 1`] Let_def .
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1258
      qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1259
      ultimately show ?thesis by auto
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1260
    qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1261
  qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1262
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1263
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1264
lemma lb_cos_minus: assumes "-pi \<le> x" and "real x \<le> 0"
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1265
  shows "cos (real(-x)) \<in> {(lb_cos prec (-x)) .. (ub_cos prec (-x))}"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1266
proof -
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1267
  have "0 \<le> real (-x)" and "(-x) \<le> pi" using `-pi \<le> x` `real x \<le> 0` by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1268
  from lb_cos[OF this] show ?thesis .
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1269
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1270
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
  1271
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
  1272
"bnds_cos prec lx ux = (let
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1273
    lpi = float_round_down prec (lb_pi prec) ;
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1274
    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
  1275
    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
  1276
    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
  1277
    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
  1278
  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
  1279
  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
  1280
  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
  1281
  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
  1282
  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
  1283
                                 else (Float (- 1) 0, Float 1 0))"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1284
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
  1285
lemma floor_int:
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1286
  obtains k :: int where "real k = (floor_fl f)"
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1287
  by (simp add: floor_fl_def)
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1288
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1289
lemma cos_periodic_nat[simp]: fixes n :: nat 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
  1290
proof (induct n arbitrary: 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
  1291
  case (Suc n)
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1292
  have split_pi_off: "x + (Suc n) * (2 * pi) = (x + n * (2 * pi)) + 2 * pi"
49962
a8cc904a6820 Renamed {left,right}_distrib to distrib_{right,left}.
webertj
parents: 49351
diff changeset
  1293
    unfolding Suc_eq_plus1 real_of_nat_add real_of_one distrib_right 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
  1294
  show ?case unfolding split_pi_off using Suc by auto
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
  1295
qed auto
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
  1296
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1297
lemma cos_periodic_int[simp]: fixes i :: int 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
  1298
proof (cases "0 \<le> i")
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1299
  case True hence i_nat: "real i = nat i" 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
  1300
  show ?thesis unfolding i_nat by auto
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
  1301
next
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1302
  case False hence i_nat: "i = - real (nat (-i))" by auto
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1303
  have "cos x = cos (x + i * (2 * pi) - i * (2 * pi))" by auto
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1304
  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
  1305
    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
  1306
  finally show ?thesis by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1307
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1308
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1309
lemma bnds_cos: "\<forall> (x::real) lx ux. (l, u) = bnds_cos prec lx ux \<and> x \<in> {lx .. ux} \<longrightarrow> l \<le> cos x \<and> cos x \<le> u"
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
  1310
proof ((rule allI | rule impI | erule conjE) +)
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1311
  fix x :: real fix lx ux
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1312
  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
  1313
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1314
  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
  1315
  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
  1316
  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
  1317
  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
  1318
  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
  1319
  let ?lx = "float_plus_down prec lx ?lx2"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1320
  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
  1321
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1322
  obtain k :: int where k: "k = real ?k" using floor_int .
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1323
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1324
  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
  1325
    using float_round_up[of "ub_pi prec" prec] pi_boundaries[of prec]
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1326
          float_round_down[of prec "lb_pi prec"] by auto
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1327
  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
  1328
    using x
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1329
    by (cases "k = 0")
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1330
      (auto intro!: add_mono
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1331
        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
  1332
        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
  1333
  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
  1334
    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
  1335
  note lx = this[THEN conjunct1] and ux = this[THEN conjunct2]
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1336
  hence lx_less_ux: "?lx \<le> real ?ux" by (rule order_trans)
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1337
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1338
  { 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
  1339
    with lpi[THEN le_imp_neg_le] lx
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1340
    have pi_lx: "- pi \<le> ?lx" and lx_0: "real ?lx \<le> 0"
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  1341
      by simp_all
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1342
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1343
    have "(lb_cos prec (- ?lx)) \<le> cos (real (- ?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
  1344
      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
  1345
    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
  1346
      using cos_monotone_minus_pi_0'[OF pi_lx lx x_le_0]
47601
050718fe6eee use real :: float => real as lifting-morphism so we can directlry use the rep_eq theorems
hoelzl
parents: 47600
diff changeset
  1347
      by (simp only: uminus_float.rep_eq real_of_int_minus
54230
b1d955791529 more simplification rules on unary and binary minus
haftmann
parents: 53077
diff changeset
  1348
        cos_minus mult_minus_left) simp
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1349
    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
  1350
      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
  1351
  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
  1352
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1353
  { 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
  1354
    with lx
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1355
    have pi_lx: "?lx \<le> pi" and lx_0: "0 \<le> real ?lx"
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  1356
      by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1357
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1358
    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
  1359
      using cos_monotone_0_pi_le[OF lx_0 lx pi_x]
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1360
      by (simp only: real_of_int_minus
54230
b1d955791529 more simplification rules on unary and binary minus
haftmann
parents: 53077
diff changeset
  1361
        cos_minus mult_minus_left) simp
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1362
    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
  1363
      using lb_cos[OF lx_0 pi_lx] by simp
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1364
    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
  1365
      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
  1366
  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
  1367
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1368
  { 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
  1369
    with ux
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1370
    have pi_ux: "- pi \<le> ?ux" and ux_0: "real ?ux \<le> 0"
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  1371
      by simp_all
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1372
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1373
    have "cos (x + (-k) * (2 * pi)) \<le> cos (real (- ?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
  1374
      using cos_monotone_minus_pi_0'[OF pi_x ux ux_0]
47601
050718fe6eee use real :: float => real as lifting-morphism so we can directlry use the rep_eq theorems
hoelzl
parents: 47600
diff changeset
  1375
      by (simp only: uminus_float.rep_eq real_of_int_minus
54230
b1d955791529 more simplification rules on unary and binary minus
haftmann
parents: 53077
diff changeset
  1376
          cos_minus mult_minus_left) simp
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1377
    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
  1378
      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
  1379
    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
  1380
      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
  1381
  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
  1382
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1383
  { 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
  1384
    with lpi ux
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1385
    have pi_ux: "?ux \<le> pi" and ux_0: "0 \<le> real ?ux"
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  1386
      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
  1387
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1388
    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
  1389
      using lb_cos[OF ux_0 pi_ux] by simp
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1390
    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
  1391
      using cos_monotone_0_pi_le[OF x_ge_0 ux pi_ux]
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1392
      by (simp only: real_of_int_minus
54230
b1d955791529 more simplification rules on unary and binary minus
haftmann
parents: 53077
diff changeset
  1393
        cos_minus mult_minus_left) simp
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1394
    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
  1395
      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
  1396
  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
  1397
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1398
  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
  1399
  proof (cases "- ?lpi \<le> ?lx \<and> ?ux \<le> 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
  1400
    case True with bnds
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
  1401
    have l: "l = lb_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
  1402
      and u: "u = 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
  1403
      by (auto simp add: bnds_cos_def Let_def)
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1404
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
  1405
    from True lpi[THEN le_imp_neg_le] lx ux
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1406
    have "- pi \<le> x - k * (2 * pi)"
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1407
      and "x - k * (2 * pi) \<le> 0"
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  1408
      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
  1409
    with True negative_ux negative_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
  1410
    show ?thesis unfolding l u by simp
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
  1411
  next case False note 1 = this show ?thesis
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
  1412
  proof (cases "0 \<le> ?lx \<and> ?ux \<le> ?lpi")
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
  1413
    case True with bnds 1
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
    have l: "l = lb_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
  1415
      and u: "u = 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
  1416
      by (auto simp add: bnds_cos_def Let_def)
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1417
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
  1418
    from True lpi lx ux
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1419
    have "0 \<le> x - k * (2 * pi)"
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1420
      and "x - k * (2 * pi) \<le> pi"
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  1421
      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
  1422
    with True positive_ux positive_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
  1423
    show ?thesis unfolding l u by simp
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
  1424
  next case False note 2 = this show ?thesis
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
  1425
  proof (cases "- ?lpi \<le> ?lx \<and> ?ux \<le> ?lpi")
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
  1426
    case True note Cond = this with bnds 1 2
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
  1427
    have l: "l = min (lb_cos prec (-?lx)) (lb_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
  1428
      and u: "u = Float 1 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
  1429
      by (auto simp add: bnds_cos_def Let_def)
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1430
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
  1431
    show ?thesis unfolding u l using negative_lx positive_ux Cond
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1432
      by (cases "x - k * (2 * pi) < 0") (auto simp add: real_of_float_min)
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1433
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
  1434
  next case False note 3 = this show ?thesis
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
  1435
  proof (cases "0 \<le> ?lx \<and> ?ux \<le> 2 * ?lpi")
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
    case True note Cond = this with bnds 1 2 3
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
  1437
    have l: "l = Float (- 1) 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
  1438
      and u: "u = max (ub_cos prec ?lx) (ub_cos prec (- (?ux - 2 * ?lpi)))"
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
      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
  1440
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
  1441
    have "cos x \<le> real u"
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1442
    proof (cases "x - k * (2 * pi) < pi")
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1443
      case True hence "x - k * (2 * pi) \<le> pi" by 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
  1444
      from positive_lx[OF Cond[THEN conjunct1] 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
  1445
      show ?thesis unfolding u by (simp add: real_of_float_max)
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1446
    next
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1447
      case False hence "pi \<le> x - k * (2 * pi)" by simp
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1448
      hence pi_x: "- pi \<le> x - k * (2 * pi) - 2 * pi" by simp
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1449
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  1450
      have "?ux \<le> 2 * pi" using Cond lpi by auto
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1451
      hence "x - k * (2 * pi) - 2 * pi \<le> 0" using ux by 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
  1452
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
  1453
      have ux_0: "real (?ux - 2 * ?lpi) \<le> 0"
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  1454
        using Cond 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
  1455
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
  1456
      from 2 and Cond have "\<not> ?ux \<le> ?lpi" by auto
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  1457
      hence "- ?lpi \<le> ?ux - 2 * ?lpi" by auto
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1458
      hence pi_ux: "- pi \<le> (?ux - 2 * ?lpi)"
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  1459
        using lpi[THEN le_imp_neg_le] 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
  1460
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1461
      have x_le_ux: "x - k * (2 * pi) - 2 * pi \<le> (?ux - 2 * ?lpi)"
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  1462
        using ux lpi by auto
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1463
      have "cos x = cos (x + (-k) * (2 * pi) + (-1::int) * (2 * pi))"
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  1464
        unfolding cos_periodic_int ..
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1465
      also have "\<dots> \<le> cos ((?ux - 2 * ?lpi))"
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  1466
        using cos_monotone_minus_pi_0'[OF pi_x x_le_ux ux_0]
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54269
diff changeset
  1467
        by (simp only: minus_float.rep_eq real_of_int_minus real_of_one
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54269
diff changeset
  1468
          mult_minus_left mult_1_left) simp
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1469
      also have "\<dots> = cos ((- (?ux - 2 * ?lpi)))"
47601
050718fe6eee use real :: float => real as lifting-morphism so we can directlry use the rep_eq theorems
hoelzl
parents: 47600
diff changeset
  1470
        unfolding uminus_float.rep_eq cos_minus ..
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1471
      also have "\<dots> \<le> (ub_cos prec (- (?ux - 2 * ?lpi)))"
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  1472
        using lb_cos_minus[OF pi_ux ux_0] by 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
  1473
      finally show ?thesis unfolding u by (simp add: real_of_float_max)
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1474
    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
  1475
    thus ?thesis unfolding l by auto
31508
1ea1c70aba00 Better approximation of cos around pi.
hoelzl
parents: 31468
diff changeset
  1476
  next case False note 4 = this show ?thesis
1ea1c70aba00 Better approximation of cos around pi.
hoelzl
parents: 31468
diff changeset
  1477
  proof (cases "-2 * ?lpi \<le> ?lx \<and> ?ux \<le> 0")
1ea1c70aba00 Better approximation of cos around pi.
hoelzl
parents: 31468
diff changeset
  1478
    case True note Cond = this with bnds 1 2 3 4
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
  1479
    have l: "l = Float (- 1) 0"
31508
1ea1c70aba00 Better approximation of cos around pi.
hoelzl
parents: 31468
diff changeset
  1480
      and u: "u = max (ub_cos prec (?lx + 2 * ?lpi)) (ub_cos prec (-?ux))"
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  1481
      by (auto simp add: bnds_cos_def Let_def)
31508
1ea1c70aba00 Better approximation of cos around pi.
hoelzl
parents: 31468
diff changeset
  1482
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1483
    have "cos x \<le> u"
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1484
    proof (cases "-pi < x - k * (2 * pi)")
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1485
      case True hence "-pi \<le> x - k * (2 * pi)" by simp
31508
1ea1c70aba00 Better approximation of cos around pi.
hoelzl
parents: 31468
diff changeset
  1486
      from negative_ux[OF this Cond[THEN conjunct2]]
1ea1c70aba00 Better approximation of cos around pi.
hoelzl
parents: 31468
diff changeset
  1487
      show ?thesis unfolding u by (simp add: real_of_float_max)
1ea1c70aba00 Better approximation of cos around pi.
hoelzl
parents: 31468
diff changeset
  1488
    next
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1489
      case False hence "x - k * (2 * pi) \<le> -pi" by simp
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1490
      hence pi_x: "x - k * (2 * pi) + 2 * pi \<le> pi" by simp
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1491
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  1492
      have "-2 * pi \<le> ?lx" using Cond lpi by auto
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1493
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1494
      hence "0 \<le> x - k * (2 * pi) + 2 * pi" using lx by simp
31508
1ea1c70aba00 Better approximation of cos around pi.
hoelzl
parents: 31468
diff changeset
  1495
1ea1c70aba00 Better approximation of cos around pi.
hoelzl
parents: 31468
diff changeset
  1496
      have lx_0: "0 \<le> real (?lx + 2 * ?lpi)"
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  1497
        using Cond lpi by auto
31508
1ea1c70aba00 Better approximation of cos around pi.
hoelzl
parents: 31468
diff changeset
  1498
1ea1c70aba00 Better approximation of cos around pi.
hoelzl
parents: 31468
diff changeset
  1499
      from 1 and Cond have "\<not> -?lpi \<le> ?lx" by auto
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  1500
      hence "?lx + 2 * ?lpi \<le> ?lpi" by auto
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1501
      hence pi_lx: "(?lx + 2 * ?lpi) \<le> pi"
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  1502
        using lpi[THEN le_imp_neg_le] by auto
31508
1ea1c70aba00 Better approximation of cos around pi.
hoelzl
parents: 31468
diff changeset
  1503
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1504
      have lx_le_x: "(?lx + 2 * ?lpi) \<le> x - k * (2 * pi) + 2 * pi"
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  1505
        using lx lpi by auto
31508
1ea1c70aba00 Better approximation of cos around pi.
hoelzl
parents: 31468
diff changeset
  1506
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1507
      have "cos x = cos (x + (-k) * (2 * pi) + (1 :: int) * (2 * pi))"
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  1508
        unfolding cos_periodic_int ..
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1509
      also have "\<dots> \<le> cos ((?lx + 2 * ?lpi))"
59751
916c0f6c83e3 New material for complex sin, cos, tan, Ln, also some reorganisation
paulson <lp15@cam.ac.uk>
parents: 59741
diff changeset
  1510
        using cos_monotone_0_pi_le[OF lx_0 lx_le_x pi_x]
47601
050718fe6eee use real :: float => real as lifting-morphism so we can directlry use the rep_eq theorems
hoelzl
parents: 47600
diff changeset
  1511
        by (simp only: minus_float.rep_eq real_of_int_minus real_of_one
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54269
diff changeset
  1512
          mult_minus_left mult_1_left) simp
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1513
      also have "\<dots> \<le> (ub_cos prec (?lx + 2 * ?lpi))"
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  1514
        using lb_cos[OF lx_0 pi_lx] by simp
31508
1ea1c70aba00 Better approximation of cos around pi.
hoelzl
parents: 31468
diff changeset
  1515
      finally show ?thesis unfolding u by (simp add: real_of_float_max)
1ea1c70aba00 Better approximation of cos around pi.
hoelzl
parents: 31468
diff changeset
  1516
    qed
1ea1c70aba00 Better approximation of cos around pi.
hoelzl
parents: 31468
diff changeset
  1517
    thus ?thesis unfolding l by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1518
  next
31508
1ea1c70aba00 Better approximation of cos around pi.
hoelzl
parents: 31468
diff changeset
  1519
    case False with bnds 1 2 3 4 show ?thesis by (auto simp add: bnds_cos_def Let_def)
1ea1c70aba00 Better approximation of cos around pi.
hoelzl
parents: 31468
diff changeset
  1520
  qed qed qed qed qed
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1521
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1522
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1523
section "Exponential function"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1524
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1525
subsection "Compute the series of the exponential function"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1526
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1527
fun ub_exp_horner :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> float \<Rightarrow> float" and lb_exp_horner :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> float \<Rightarrow> float" where
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1528
"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
  1529
"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
  1530
    (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
  1531
"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
  1532
"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
  1533
    (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
  1534
31098
73dd67adf90a replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
hoelzl
parents: 30971
diff changeset
  1535
lemma bnds_exp_horner: assumes "real x \<le> 0"
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1536
  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
  1537
proof -
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1538
  { fix n
30971
7fbebf75b3ef funpow and relpow with shared "^^" syntax
haftmann
parents: 30968
diff changeset
  1539
    have F: "\<And> m. ((\<lambda>i. i + 1) ^^ n) m = n + m" by (induct n, auto)
59751
916c0f6c83e3 New material for complex sin, cos, tan, Ln, also some reorganisation
paulson <lp15@cam.ac.uk>
parents: 59741
diff changeset
  1540
    have "fact (Suc n) = fact n * ((\<lambda>i::nat. i + 1) ^^ n) 1" unfolding F by auto
59730
b7c394c7a619 The factorial function, "fact", now has type "nat => 'a"
paulson <lp15@cam.ac.uk>
parents: 59658
diff changeset
  1541
  } note f_eq = 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
  1542
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1543
  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
  1544
    OF assms f_eq lb_exp_horner.simps ub_exp_horner.simps]
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1545
59730
b7c394c7a619 The factorial function, "fact", now has type "nat => 'a"
paulson <lp15@cam.ac.uk>
parents: 59658
diff changeset
  1546
  { have "lb_exp_horner prec (get_even n) 1 1 x \<le> (\<Sum>j = 0..<get_even n. 1 / (fact j) * real x ^ j)"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1547
      using bounds(1) by auto
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1548
    also have "\<dots> \<le> exp x"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1549
    proof -
59730
b7c394c7a619 The factorial function, "fact", now has type "nat => 'a"
paulson <lp15@cam.ac.uk>
parents: 59658
diff changeset
  1550
      obtain t where "\<bar>t\<bar> \<le> \<bar>real x\<bar>" and "exp x = (\<Sum>m = 0..<get_even n. real x ^ m / (fact m)) + exp t / (fact (get_even n)) * (real x) ^ (get_even n)"
56195
c7dfd924a165 adapt to Isabelle/c726ecfb22b6
huffman
parents: 56073
diff changeset
  1551
        using Maclaurin_exp_le unfolding atLeast0LessThan by blast
59730
b7c394c7a619 The factorial function, "fact", now has type "nat => 'a"
paulson <lp15@cam.ac.uk>
parents: 59658
diff changeset
  1552
      moreover have "0 \<le> exp t / (fact (get_even n)) * (real x) ^ (get_even n)"
56571
f4635657d66f added divide_nonneg_nonneg and co; made it a simp rule
hoelzl
parents: 56544
diff changeset
  1553
        by (auto simp: zero_le_even_power)
56536
aefb4a8da31f made mult_nonneg_nonneg a simp rule
nipkow
parents: 56483
diff changeset
  1554
      ultimately show ?thesis using get_odd exp_gt_zero by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1555
    qed
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1556
    finally have "lb_exp_horner prec (get_even n) 1 1 x \<le> exp x" .
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1557
  } moreover
31809
hoelzl
parents: 31790
diff changeset
  1558
  {
31098
73dd67adf90a replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
hoelzl
parents: 30971
diff changeset
  1559
    have x_less_zero: "real x ^ get_odd n \<le> 0"
73dd67adf90a replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
hoelzl
parents: 30971
diff changeset
  1560
    proof (cases "real x = 0")
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1561
      case True
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1562
      have "(get_odd n) \<noteq> 0" using get_odd[THEN odd_pos] by auto
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1563
      thus ?thesis unfolding True power_0_left by auto
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1564
    next
31098
73dd67adf90a replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
hoelzl
parents: 30971
diff changeset
  1565
      case False hence "real x < 0" using `real x \<le> 0` by auto
46545
haftmann
parents: 45481
diff changeset
  1566
      show ?thesis by (rule less_imp_le, auto simp add: power_less_zero_eq `real x < 0`)
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1567
    qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1568
59730
b7c394c7a619 The factorial function, "fact", now has type "nat => 'a"
paulson <lp15@cam.ac.uk>
parents: 59658
diff changeset
  1569
    obtain t where "\<bar>t\<bar> \<le> \<bar>real x\<bar>" and "exp x = (\<Sum>m = 0..<get_odd n. (real x) ^ m / (fact m)) + exp t / (fact (get_odd n)) * (real x) ^ (get_odd n)"
56195
c7dfd924a165 adapt to Isabelle/c726ecfb22b6
huffman
parents: 56073
diff changeset
  1570
      using Maclaurin_exp_le unfolding atLeast0LessThan by blast
59730
b7c394c7a619 The factorial function, "fact", now has type "nat => 'a"
paulson <lp15@cam.ac.uk>
parents: 59658
diff changeset
  1571
    moreover have "exp t / (fact (get_odd n)) * (real x) ^ (get_odd n) \<le> 0"
46545
haftmann
parents: 45481
diff changeset
  1572
      by (auto intro!: mult_nonneg_nonpos divide_nonpos_pos simp add: x_less_zero)
59730
b7c394c7a619 The factorial function, "fact", now has type "nat => 'a"
paulson <lp15@cam.ac.uk>
parents: 59658
diff changeset
  1573
    ultimately have "exp x \<le> (\<Sum>j = 0..<get_odd n. 1 / (fact j) * real x ^ j)"
56536
aefb4a8da31f made mult_nonneg_nonneg a simp rule
nipkow
parents: 56483
diff changeset
  1574
      using get_odd exp_gt_zero by auto
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1575
    also have "\<dots> \<le> ub_exp_horner prec (get_odd n) 1 1 x"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1576
      using bounds(2) by auto
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1577
    finally have "exp x \<le> ub_exp_horner prec (get_odd n) 1 1 x" .
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1578
  } ultimately show ?thesis by auto
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1579
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1580
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1581
lemma ub_exp_horner_nonneg: "real x \<le> 0 \<Longrightarrow> 0 \<le> real (ub_exp_horner prec (get_odd n) (Suc 0) (Suc 0) x)"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1582
  using bnds_exp_horner[of x prec n]
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1583
  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
  1584
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1585
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1586
subsection "Compute the exponential function on the entire domain"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1587
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1588
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
  1589
"lb_exp prec x =
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1590
  (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
  1591
  else
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1592
    let
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1593
      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
  1594
        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
  1595
    in
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1596
      if x < - 1 then
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1597
        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
  1598
      else horner x)" |
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1599
"ub_exp prec x =
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1600
  (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
  1601
  else if x < - 1 then
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1602
    power_up_fl prec
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1603
      (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
  1604
        (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
  1605
  else ub_exp_horner prec (get_odd (prec + 2)) 1 1 x)"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1606
by pat_completeness auto
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1607
termination
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1608
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
  1609
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1610
lemma exp_m1_ge_quarter: "(1 / 4 :: real) \<le> exp (- 1)"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1611
proof -
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1612
  have eq4: "4 = Suc (Suc (Suc (Suc 0)))" by auto
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1613
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
  1614
  have "1 / 4 = (Float 1 (- 2))" unfolding Float_num by auto
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1615
  also have "\<dots> \<le> lb_exp_horner 3 (get_even 3) 1 1 (- 1)"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1616
    by code_simp
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1617
  also have "\<dots> \<le> exp (- 1 :: float)" using bnds_exp_horner[where x="- 1"] by auto
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  1618
  finally show ?thesis by simp
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1619
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1620
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1621
lemma lb_exp_pos: assumes "\<not> 0 < x" shows "0 < lb_exp prec x"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1622
proof -
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1623
  let "?lb_horner x" = "lb_exp_horner prec (get_even (prec + 2)) 1 1 x"
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
  1624
  let "?horner x" = "let  y = ?lb_horner x  in if y \<le> 0 then Float 1 (- 2) else y"
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  1625
  have pos_horner: "\<And> x. 0 < ?horner x" unfolding Let_def by (cases "?lb_horner x \<le> 0", auto)
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1626
  moreover { fix x :: float fix num :: nat
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  1627
    have "0 < real (?horner x) ^ num" using `0 < ?horner x` by simp
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1628
    also have "\<dots> = (?horner x) ^ num" by auto
31098
73dd67adf90a replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
hoelzl
parents: 30971
diff changeset
  1629
    finally have "0 < real ((?horner x) ^ num)" .
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1630
  }
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1631
  ultimately show ?thesis
30968
10fef94f40fc adaptions due to rearrangment of power operation
haftmann
parents: 30952
diff changeset
  1632
    unfolding lb_exp.simps if_not_P[OF `\<not> 0 < x`] Let_def
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1633
    by (cases "floor_fl x", cases "x < - 1", auto simp: real_power_up_fl real_power_down_fl
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1634
      intro!: power_up_less power_down_pos)
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1635
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1636
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1637
lemma exp_boundaries': assumes "x \<le> 0"
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1638
  shows "exp x \<in> { (lb_exp prec x) .. (ub_exp prec x)}"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1639
proof -
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1640
  let "?lb_exp_horner x" = "lb_exp_horner prec (get_even (prec + 2)) 1 1 x"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1641
  let "?ub_exp_horner x" = "ub_exp_horner prec (get_odd (prec + 2)) 1 1 x"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1642
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  1643
  have "real x \<le> 0" and "\<not> x > 0" using `x \<le> 0` by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1644
  show ?thesis
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1645
  proof (cases "x < - 1")
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  1646
    case False hence "- 1 \<le> real x" by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1647
    show ?thesis
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1648
    proof (cases "?lb_exp_horner x \<le> 0")
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  1649
      from `\<not> x < - 1` have "- 1 \<le> real x" by auto
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1650
      hence "exp (- 1) \<le> exp x" unfolding exp_le_cancel_iff .
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1651
      from order_trans[OF exp_m1_ge_quarter this]
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
  1652
      have "Float 1 (- 2) \<le> exp x" unfolding Float_num .
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1653
      moreover case True
31098
73dd67adf90a replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
hoelzl
parents: 30971
diff changeset
  1654
      ultimately show ?thesis using bnds_exp_horner `real x \<le> 0` `\<not> x > 0` `\<not> x < - 1` by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1655
    next
31098
73dd67adf90a replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
hoelzl
parents: 30971
diff changeset
  1656
      case False thus ?thesis using bnds_exp_horner `real x \<le> 0` `\<not> x > 0` `\<not> x < - 1` by (auto simp add: Let_def)
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1657
    qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1658
  next
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1659
    case True
31809
hoelzl
parents: 31790
diff changeset
  1660
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1661
    let ?num = "nat (- int_floor_fl x)"
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1662
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  1663
    have "real (int_floor_fl x) < - 1" using int_floor_fl[of x] `x < - 1`
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  1664
      by simp
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1665
    hence "real (int_floor_fl x) < 0" by simp
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1666
    hence "int_floor_fl x < 0" by auto
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1667
    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
  1668
    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
  1669
    hence "0 < ?num"  by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1670
    hence "real ?num \<noteq> 0" by auto
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1671
    have num_eq: "real ?num = - int_floor_fl x" using `0 < nat (- int_floor_fl x)` by auto
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1672
    have "0 < - int_floor_fl x" using `0 < ?num`[unfolded real_of_nat_less_iff[symmetric]] by simp
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1673
    hence "real (int_floor_fl x) < 0" unfolding less_float_def by auto
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1674
    have fl_eq: "real (- int_floor_fl x) = real (- floor_fl x)"
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1675
      by (simp add: floor_fl_def int_floor_fl_def)
58982
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58889
diff changeset
  1676
    from `0 < - int_floor_fl x` have "0 \<le> real (- floor_fl x)"
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1677
      by (simp add: floor_fl_def int_floor_fl_def)
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1678
    from `real (int_floor_fl x) < 0` have "real (floor_fl x) < 0"
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1679
      by (simp add: floor_fl_def int_floor_fl_def)
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1680
    have "exp x \<le> ub_exp prec x"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1681
    proof -
31809
hoelzl
parents: 31790
diff changeset
  1682
      have div_less_zero: "real (float_divr prec x (- floor_fl x)) \<le> 0"
58982
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58889
diff changeset
  1683
        using float_divr_nonpos_pos_upper_bound[OF `real x \<le> 0` `0 \<le> real (- floor_fl x)`]
47601
050718fe6eee use real :: float => real as lifting-morphism so we can directlry use the rep_eq theorems
hoelzl
parents: 47600
diff changeset
  1684
        unfolding less_eq_float_def zero_float.rep_eq .
31809
hoelzl
parents: 31790
diff changeset
  1685
56479
91958d4b30f7 revert c1bbd3e22226, a14831ac3023, and 36489d77c484: divide_minus_left/right are again simp rules
hoelzl
parents: 56410
diff changeset
  1686
      have "exp x = exp (?num * (x / ?num))" using `real ?num \<noteq> 0` by auto
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1687
      also have "\<dots> = exp (x / ?num) ^ ?num" unfolding exp_real_of_nat_mult ..
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1688
      also have "\<dots> \<le> exp (float_divr prec x (- floor_fl x)) ^ ?num" unfolding num_eq fl_eq
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  1689
        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
  1690
      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
  1691
        unfolding real_of_float_power
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  1692
        by (rule power_mono, rule bnds_exp_horner[OF div_less_zero, unfolded atLeastAtMost_iff, THEN conjunct2], auto)
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1693
      also have "\<dots> \<le> real (power_up_fl prec (?ub_exp_horner (float_divr prec x (- floor_fl x))) ?num)"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1694
        by (auto simp add: real_power_up_fl intro!: power_up ub_exp_horner_nonneg div_less_zero)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1695
      finally show ?thesis unfolding ub_exp.simps if_not_P[OF `\<not> 0 < x`] if_P[OF `x < - 1`] floor_fl_def Let_def
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1696
        .
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1697
    qed
31809
hoelzl
parents: 31790
diff changeset
  1698
    moreover
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1699
    have "lb_exp prec x \<le> exp x"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1700
    proof -
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1701
      let ?divl = "float_divl prec x (- floor_fl x)"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1702
      let ?horner = "?lb_exp_horner ?divl"
31809
hoelzl
parents: 31790
diff changeset
  1703
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1704
      show ?thesis
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1705
      proof (cases "?horner \<le> 0")
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  1706
        case False hence "0 \<le> real ?horner" by auto
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  1707
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  1708
        have div_less_zero: "real (float_divl prec x (- floor_fl x)) \<le> 0"
56479
91958d4b30f7 revert c1bbd3e22226, a14831ac3023, and 36489d77c484: divide_minus_left/right are again simp rules
hoelzl
parents: 56410
diff changeset
  1709
          using `real (floor_fl x) < 0` `real x \<le> 0` by (auto intro!: order_trans[OF float_divl] divide_nonpos_neg)
91958d4b30f7 revert c1bbd3e22226, a14831ac3023, and 36489d77c484: divide_minus_left/right are again simp rules
hoelzl
parents: 56410
diff changeset
  1710
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1711
        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
  1712
          exp (float_divl prec x (- floor_fl x)) ^ ?num"
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1713
          using `0 \<le> real ?horner`[unfolded floor_fl_def[symmetric]] bnds_exp_horner[OF div_less_zero, unfolded atLeastAtMost_iff, THEN conjunct1] by (auto intro!: power_mono)
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1714
        also have "\<dots> \<le> exp (x / ?num) ^ ?num" 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
  1715
          using float_divl by (auto intro!: power_mono simp del: uminus_float.rep_eq)
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1716
        also have "\<dots> = exp (?num * (x / ?num))" unfolding exp_real_of_nat_mult ..
56479
91958d4b30f7 revert c1bbd3e22226, a14831ac3023, and 36489d77c484: divide_minus_left/right are again simp rules
hoelzl
parents: 56410
diff changeset
  1717
        also have "\<dots> = exp x" using `real ?num \<noteq> 0` by auto
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1718
        finally show ?thesis using False
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1719
          unfolding lb_exp.simps if_not_P[OF `\<not> 0 < x`] if_P[OF `x < - 1`] int_floor_fl_def Let_def if_not_P[OF False]
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1720
          by (auto simp: real_power_down_fl intro!: power_down_le)
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1721
      next
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  1722
        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
  1723
        have "power_down_fl prec (Float 1 (- 2))  ?num \<le> (Float 1 (- 2)) ^ ?num"
5b762cd73a8e Lots of new material on complex-valued functions. Modified simplification of (x/n)^k
paulson <lp15@cam.ac.uk>
parents: 59730
diff changeset
  1724
          by (metis Float_le_zero_iff less_imp_le linorder_not_less not_numeral_le_zero numeral_One power_down_fl)
5b762cd73a8e Lots of new material on complex-valued functions. Modified simplification of (x/n)^k
paulson <lp15@cam.ac.uk>
parents: 59730
diff changeset
  1725
        then have "power_down_fl prec (Float 1 (- 2))  ?num \<le> real (Float 1 (- 2)) ^ ?num"
5b762cd73a8e Lots of new material on complex-valued functions. Modified simplification of (x/n)^k
paulson <lp15@cam.ac.uk>
parents: 59730
diff changeset
  1726
          by simp
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1727
        also
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  1728
        have "real (floor_fl x) \<noteq> 0" and "real (floor_fl x) \<le> 0" using `real (floor_fl x) < 0` by auto
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  1729
        from divide_right_mono_neg[OF floor_fl[of x] `real (floor_fl x) \<le> 0`, unfolded divide_self[OF `real (floor_fl x) \<noteq> 0`]]
47601
050718fe6eee use real :: float => real as lifting-morphism so we can directlry use the rep_eq theorems
hoelzl
parents: 47600
diff changeset
  1730
        have "- 1 \<le> x / (- floor_fl x)" 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
  1731
        from order_trans[OF exp_m1_ge_quarter this[unfolded exp_le_cancel_iff[where x="- 1", symmetric]]]
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
  1732
        have "Float 1 (- 2) \<le> exp (x / (- floor_fl x))" unfolding Float_num .
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
  1733
        hence "real (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
  1734
          by (metis Float_num(5) power_mono zero_le_divide_1_iff zero_le_numeral)
56479
91958d4b30f7 revert c1bbd3e22226, a14831ac3023, and 36489d77c484: divide_minus_left/right are again simp rules
hoelzl
parents: 56410
diff changeset
  1735
        also have "\<dots> = exp x" unfolding num_eq fl_eq exp_real_of_nat_mult[symmetric] using `real (floor_fl x) \<noteq> 0` by auto
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  1736
        finally show ?thesis
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1737
          unfolding lb_exp.simps if_not_P[OF `\<not> 0 < x`] if_P[OF `x < - 1`] int_floor_fl_def Let_def if_P[OF True] real_of_float_power
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1738
          .
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1739
      qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1740
    qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1741
    ultimately show ?thesis by auto
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1742
  qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1743
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1744
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1745
lemma exp_boundaries: "exp x \<in> { lb_exp prec x .. ub_exp prec x }"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1746
proof -
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1747
  show ?thesis
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1748
  proof (cases "0 < x")
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  1749
    case False hence "x \<le> 0" by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1750
    from exp_boundaries'[OF this] show ?thesis .
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1751
  next
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  1752
    case True hence "-x \<le> 0" by auto
31809
hoelzl
parents: 31790
diff changeset
  1753
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1754
    have "lb_exp prec x \<le> exp x"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1755
    proof -
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1756
      from exp_boundaries'[OF `-x \<le> 0`]
47601
050718fe6eee use real :: float => real as lifting-morphism so we can directlry use the rep_eq theorems
hoelzl
parents: 47600
diff changeset
  1757
      have ub_exp: "exp (- real x) \<le> ub_exp prec (-x)" unfolding atLeastAtMost_iff minus_float.rep_eq by auto
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1758
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1759
      have "float_divl prec 1 (ub_exp prec (-x)) \<le> 1 / ub_exp prec (-x)" using float_divl[where x=1] by auto
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1760
      also have "\<dots> \<le> exp x"
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  1761
        using ub_exp[unfolded inverse_le_iff_le[OF order_less_le_trans[OF exp_gt_zero ub_exp] exp_gt_zero, symmetric]]
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  1762
        unfolding exp_minus nonzero_inverse_inverse_eq[OF exp_not_eq_zero] inverse_eq_divide by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1763
      finally show ?thesis unfolding lb_exp.simps if_P[OF True] .
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1764
    qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1765
    moreover
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1766
    have "exp x \<le> ub_exp prec x"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1767
    proof -
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  1768
      have "\<not> 0 < -x" using `0 < x` by auto
31809
hoelzl
parents: 31790
diff changeset
  1769
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1770
      from exp_boundaries'[OF `-x \<le> 0`]
47601
050718fe6eee use real :: float => real as lifting-morphism so we can directlry use the rep_eq theorems
hoelzl
parents: 47600
diff changeset
  1771
      have lb_exp: "lb_exp prec (-x) \<le> exp (- real x)" unfolding atLeastAtMost_iff minus_float.rep_eq by auto
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1772
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1773
      have "exp x \<le> (1 :: float) / lb_exp prec (-x)"
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  1774
        using lb_exp lb_exp_pos[OF `\<not> 0 < -x`, of prec]
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  1775
        by (simp del: lb_exp.simps add: exp_minus inverse_eq_divide field_simps)
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1776
      also have "\<dots> \<le> float_divr prec 1 (lb_exp prec (-x))" using float_divr .
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1777
      finally show ?thesis unfolding ub_exp.simps if_P[OF True] .
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1778
    qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1779
    ultimately show ?thesis by auto
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1780
  qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1781
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1782
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1783
lemma bnds_exp: "\<forall> (x::real) lx ux. (l, u) = (lb_exp prec lx, ub_exp prec ux) \<and> x \<in> {lx .. ux} \<longrightarrow> l \<le> exp x \<and> exp x \<le> u"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1784
proof (rule allI, rule allI, rule allI, rule impI)
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1785
  fix x::real and lx ux
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1786
  assume "(l, u) = (lb_exp prec lx, ub_exp prec ux) \<and> x \<in> {lx .. ux}"
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1787
  hence l: "lb_exp prec lx = l " and u: "ub_exp prec ux = u" and x: "x \<in> {lx .. ux}" by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1788
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1789
  { from exp_boundaries[of lx prec, unfolded l]
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1790
    have "l \<le> exp lx" by (auto simp del: lb_exp.simps)
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1791
    also have "\<dots> \<le> exp x" using x by auto
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1792
    finally have "l \<le> exp x" .
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1793
  } moreover
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1794
  { have "exp x \<le> exp ux" using x by auto
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1795
    also have "\<dots> \<le> u" using exp_boundaries[of ux prec, unfolded u] by (auto simp del: ub_exp.simps)
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1796
    finally have "exp x \<le> u" .
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1797
  } ultimately show "l \<le> exp x \<and> exp x \<le> u" ..
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1798
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1799
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1800
section "Logarithm"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1801
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1802
subsection "Compute the logarithm series"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1803
31809
hoelzl
parents: 31790
diff changeset
  1804
fun ub_ln_horner :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> float \<Rightarrow> float"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1805
and lb_ln_horner :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> float \<Rightarrow> float" where
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1806
"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
  1807
"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
  1808
    (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
  1809
"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
  1810
"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
  1811
    (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
  1812
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1813
lemma ln_bounds:
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1814
  assumes "0 \<le> x" and "x < 1"
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
  1815
  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
  1816
  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
  1817
proof -
30952
7ab2716dd93b power operation on functions with syntax o^; power operation on relations with syntax ^^
haftmann
parents: 30886
diff changeset
  1818
  let "?a n" = "(1/real (n +1)) * x ^ (Suc n)"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1819
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
  1820
  have ln_eq: "(\<Sum> i. (- 1) ^ i * ?a i) = ln (x + 1)"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1821
    using ln_series[of "x + 1"] `0 \<le> x` `x < 1` by auto
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1822
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1823
  have "norm x < 1" using assms by auto
31809
hoelzl
parents: 31790
diff changeset
  1824
  have "?a ----> 0" unfolding Suc_eq_plus1[symmetric] inverse_eq_divide[symmetric]
44568
e6f291cb5810 discontinue many legacy theorems about LIM and LIMSEQ, in favor of tendsto theorems
huffman
parents: 44349
diff changeset
  1825
    using tendsto_mult[OF LIMSEQ_inverse_real_of_nat LIMSEQ_Suc[OF LIMSEQ_power_zero[OF `norm x < 1`]]] by auto
56536
aefb4a8da31f made mult_nonneg_nonneg a simp rule
nipkow
parents: 56483
diff changeset
  1826
  { fix n have "0 \<le> ?a n" by (rule mult_nonneg_nonneg, auto simp: `0 \<le> x`) }
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1827
  { fix n have "?a (Suc n) \<le> ?a n" unfolding inverse_eq_divide[symmetric]
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1828
    proof (rule mult_mono)
56536
aefb4a8da31f made mult_nonneg_nonneg a simp rule
nipkow
parents: 56483
diff changeset
  1829
      show "0 \<le> x ^ Suc (Suc n)" by (auto simp add: `0 \<le> x`)
57512
cc97b347b301 reduced name variants for assoc and commute on plus and mult
haftmann
parents: 57418
diff changeset
  1830
      have "x ^ Suc (Suc n) \<le> x ^ Suc n * 1" unfolding power_Suc2 mult.assoc[symmetric]
56536
aefb4a8da31f made mult_nonneg_nonneg a simp rule
nipkow
parents: 56483
diff changeset
  1831
        by (rule mult_left_mono, fact less_imp_le[OF `x < 1`], auto simp: `0 \<le> x`)
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1832
      thus "x ^ Suc (Suc n) \<le> x ^ Suc n" by auto
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1833
    qed auto }
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1834
  from summable_Leibniz'(2,4)[OF `?a ----> 0` `\<And>n. 0 \<le> ?a n`, OF `\<And>n. ?a (Suc n) \<le> ?a n`, unfolded ln_eq]
56195
c7dfd924a165 adapt to Isabelle/c726ecfb22b6
huffman
parents: 56073
diff changeset
  1835
  show "?lb" and "?ub" unfolding atLeast0LessThan by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1836
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1837
31809
hoelzl
parents: 31790
diff changeset
  1838
lemma ln_float_bounds:
31098
73dd67adf90a replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
hoelzl
parents: 30971
diff changeset
  1839
  assumes "0 \<le> real x" and "real x < 1"
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1840
  shows "x * lb_ln_horner prec (get_even n) 1 x \<le> ln (x + 1)" (is "?lb \<le> ?ln")
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1841
  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
  1842
proof -
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1843
  obtain ev where ev: "get_even n = 2 * ev" using get_even_double ..
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1844
  obtain od where od: "get_odd n = 2 * od + 1" using get_odd_double ..
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1845
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
  1846
  let "?s n" = "(- 1) ^ n * (1 / real (1 + n)) * (real x)^(Suc n)"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1847
57512
cc97b347b301 reduced name variants for assoc and commute on plus and mult
haftmann
parents: 57418
diff changeset
  1848
  have "?lb \<le> setsum ?s {0 ..< 2 * ev}" unfolding power_Suc2 mult.assoc[symmetric] times_float.rep_eq setsum_left_distrib[symmetric] unfolding mult.commute[of "real x"] ev
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1849
    using horner_bounds(1)[where G="\<lambda> i k. Suc k" and F="\<lambda>x. x" and f="\<lambda>x. x" and lb="\<lambda>n i k x. lb_ln_horner prec n k x" and ub="\<lambda>n i k x. ub_ln_horner prec n k x" and j'=1 and n="2*ev",
31098
73dd67adf90a replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
hoelzl
parents: 30971
diff changeset
  1850
      OF `0 \<le> real x` refl lb_ln_horner.simps ub_ln_horner.simps] `0 \<le> real x`
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1851
    by (rule mult_right_mono)
31098
73dd67adf90a replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
hoelzl
parents: 30971
diff changeset
  1852
  also have "\<dots> \<le> ?ln" using ln_bounds(1)[OF `0 \<le> real x` `real x < 1`] by auto
31809
hoelzl
parents: 31790
diff changeset
  1853
  finally show "?lb \<le> ?ln" .
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1854
31098
73dd67adf90a replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
hoelzl
parents: 30971
diff changeset
  1855
  have "?ln \<le> setsum ?s {0 ..< 2 * od + 1}" using ln_bounds(2)[OF `0 \<le> real x` `real x < 1`] by auto
57512
cc97b347b301 reduced name variants for assoc and commute on plus and mult
haftmann
parents: 57418
diff changeset
  1856
  also have "\<dots> \<le> ?ub" unfolding power_Suc2 mult.assoc[symmetric] times_float.rep_eq setsum_left_distrib[symmetric] unfolding mult.commute[of "real x"] od
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1857
    using horner_bounds(2)[where G="\<lambda> i k. Suc k" and F="\<lambda>x. x" and f="\<lambda>x. x" and lb="\<lambda>n i k x. lb_ln_horner prec n k x" and ub="\<lambda>n i k x. ub_ln_horner prec n k x" and j'=1 and n="2*od+1",
31098
73dd67adf90a replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
hoelzl
parents: 30971
diff changeset
  1858
      OF `0 \<le> real x` refl lb_ln_horner.simps ub_ln_horner.simps] `0 \<le> real x`
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1859
    by (rule mult_right_mono)
31809
hoelzl
parents: 31790
diff changeset
  1860
  finally show "?ln \<le> ?ub" .
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1861
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1862
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
  1863
lemma ln_add: 
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
  1864
  fixes x::real assumes "0 < x" and "0 < y" shows "ln (x + y) = ln x + ln (1 + y / x)"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1865
proof -
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1866
  have "x \<noteq> 0" using assms by auto
49962
a8cc904a6820 Renamed {left,right}_distrib to distrib_{right,left}.
webertj
parents: 49351
diff changeset
  1867
  have "x + y = x * (1 + y / x)" unfolding distrib_left times_divide_eq_right nonzero_mult_divide_cancel_left[OF `x \<noteq> 0`] by auto
31809
hoelzl
parents: 31790
diff changeset
  1868
  moreover
56541
0e3abadbef39 made divide_pos_pos a simp rule
nipkow
parents: 56536
diff changeset
  1869
  have "0 < y / x" using assms by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1870
  hence "0 < 1 + y / x" by auto
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1871
  ultimately show ?thesis using ln_mult assms by auto
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1872
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1873
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1874
subsection "Compute the logarithm of 2"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1875
31809
hoelzl
parents: 31790
diff changeset
  1876
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
  1877
                                        in float_plus_up prec
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1878
                                          ((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
  1879
                                           (float_round_up prec (third * ub_ln_horner prec (get_odd prec) 1 third)))"
31809
hoelzl
parents: 31790
diff changeset
  1880
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
  1881
                                        in float_plus_down prec
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1882
                                          ((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
  1883
                                           (float_round_down prec (third * lb_ln_horner prec (get_even prec) 1 third)))"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1884
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1885
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
  1886
  and lb_ln2: "lb_ln2 prec \<le> ln 2" (is "?lb_ln2")
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1887
proof -
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1888
  let ?uthird = "rapprox_rat (max prec 1) 1 3"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1889
  let ?lthird = "lapprox_rat prec 1 3"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1890
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
  1891
  have ln2_sum: "ln 2 = ln (1/2 + 1) + ln (1 / 3 + 1::real)"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1892
    using ln_add[of "3 / 2" "1 / 2"] by auto
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1893
  have lb3: "?lthird \<le> 1 / 3" using lapprox_rat[of prec 1 3] by auto
31098
73dd67adf90a replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
hoelzl
parents: 30971
diff changeset
  1894
  hence lb3_ub: "real ?lthird < 1" by auto
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1895
  have lb3_lb: "0 \<le> real ?lthird" using lapprox_rat_nonneg[of 1 3] by auto
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1896
  have ub3: "1 / 3 \<le> ?uthird" using rapprox_rat[of 1 3] by auto
31098
73dd67adf90a replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
hoelzl
parents: 30971
diff changeset
  1897
  hence ub3_lb: "0 \<le> real ?uthird" by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1898
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
  1899
  have lb2: "0 \<le> real (Float 1 (- 1))" and ub2: "real (Float 1 (- 1)) < 1" unfolding Float_num by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1900
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1901
  have "0 \<le> (1::int)" and "0 < (3::int)" by auto
58982
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58889
diff changeset
  1902
  have ub3_ub: "real ?uthird < 1"
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58889
diff changeset
  1903
    by (simp add: Float.compute_rapprox_rat Float.compute_lapprox_rat rapprox_posrat_less1)
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1904
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1905
  have third_gt0: "(0 :: real) < 1 / 3 + 1" by auto
31098
73dd67adf90a replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
hoelzl
parents: 30971
diff changeset
  1906
  have uthird_gt0: "0 < real ?uthird + 1" using ub3_lb by auto
73dd67adf90a replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
hoelzl
parents: 30971
diff changeset
  1907
  have lthird_gt0: "0 < real ?lthird + 1" using lb3_lb by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1908
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1909
  show ?ub_ln2 unfolding ub_ln2_def Let_def ln2_sum Float_num(4)[symmetric]
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1910
  proof (rule float_plus_up_le, rule add_mono, fact ln_float_bounds(2)[OF lb2 ub2])
31098
73dd67adf90a replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
hoelzl
parents: 30971
diff changeset
  1911
    have "ln (1 / 3 + 1) \<le> ln (real ?uthird + 1)" 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
  1912
    also have "\<dots> \<le> ?uthird * ub_ln_horner prec (get_odd prec) 1 ?uthird"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1913
      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
  1914
    also note float_round_up
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1915
    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
  1916
  qed
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1917
  show ?lb_ln2 unfolding lb_ln2_def Let_def ln2_sum Float_num(4)[symmetric]
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1918
  proof (rule float_plus_down_le, rule add_mono, fact ln_float_bounds(1)[OF lb2 ub2])
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  1919
    have "?lthird * lb_ln_horner prec (get_even prec) 1 ?lthird \<le> ln (real ?lthird + 1)"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1920
      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
  1921
    note float_round_down_le[OF this]
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1922
    also have "\<dots> \<le> ln (1 / 3 + 1)" unfolding ln_le_cancel_iff[OF lthird_gt0 third_gt0] using lb3 by auto
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1923
    finally show "float_round_down prec (?lthird * lb_ln_horner prec (get_even prec) 1 ?lthird) \<le> ln (1 / 3 + 1)" .
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1924
  qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1925
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1926
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1927
subsection "Compute the logarithm in the entire domain"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1928
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1929
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
  1930
"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
  1931
            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
  1932
            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
  1933
                 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
  1934
            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
  1935
                                   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
  1936
                                        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
  1937
"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
  1938
            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
  1939
            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
  1940
                 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
  1941
            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
  1942
                                              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
  1943
                                   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
  1944
                                        Some (float_plus_down prec (float_round_down prec (lb_ln2 prec * (Float (exponent x + l) 0))) (horner (Float (mantissa x) (- l) - 1))))"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1945
by pat_completeness auto
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1946
55414
eab03e9cee8a renamed '{prod,sum,bool,unit}_case' to 'case_...'
blanchet
parents: 55413
diff changeset
  1947
termination proof (relation "measure (\<lambda> v. let (prec, x) = case_sum id id v in (if x < 1 then 1 else 0))", auto)
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  1948
  fix prec and x :: float assume "\<not> real x \<le> 0" and "real x < 1" and "real (float_divl (max prec (Suc 0)) 1 x) < 1"
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  1949
  hence "0 < real x" "1 \<le> max prec (Suc 0)" "real x < 1" by auto
58982
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58889
diff changeset
  1950
  from float_divl_pos_less1_bound[OF `0 < real x` `real x < 1`[THEN less_imp_le] `1 \<le> max prec (Suc 0)`]
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  1951
  show False using `real (float_divl (max prec (Suc 0)) 1 x) < 1` by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  1952
next
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  1953
  fix prec x assume "\<not> real x \<le> 0" and "real x < 1" and "real (float_divr prec 1 x) < 1"
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  1954
  hence "0 < x" by auto
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  1955
  from float_divr_pos_less1_lower_bound[OF `0 < x`, of prec] `real x < 1`
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  1956
  show False using `real (float_divr prec 1 x) < 1` by auto
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1957
qed
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1958
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1959
lemma float_pos_eq_mantissa_pos:  "x > 0 \<longleftrightarrow> mantissa x > 0"
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1960
  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
  1961
  apply (auto simp add: zero_less_mult_iff zero_float_def  dest: less_zeroE)
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
  1962
  by (metis not_le powr_ge_pzero)
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1963
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1964
lemma Float_pos_eq_mantissa_pos:  "Float m e > 0 \<longleftrightarrow> m > 0"
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1965
  using powr_gt_zero[of 2 "e"]
54269
dcdfec41a325 tuned proofs in Approximation
hoelzl
parents: 54230
diff changeset
  1966
  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
  1967
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1968
lemma Float_representation_aux:
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1969
  fixes m e
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1970
  defines "x \<equiv> Float m e"
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1971
  assumes "x > 0"
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1972
  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
  1973
    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
  1974
proof -
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1975
  from assms have mantissa_pos: "m > 0" "mantissa x > 0"
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  1976
    using Float_pos_eq_mantissa_pos[of m e] float_pos_eq_mantissa_pos[of x] by simp_all
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  1977
  thus ?th1 using bitlen_Float[of m e] assms by (auto simp add: zero_less_mult_iff intro!: arg_cong2[where f=Float])
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  1978
  have "x \<noteq> float_of 0"
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  1979
    unfolding zero_float_def[symmetric] using `0 < x` by auto
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1980
  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
  1981
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1982
  have "2 powr (1 - (real (bitlen (mantissa x)) + real i)) =
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1983
    2 powr (1 - (real (bitlen (mantissa x)))) * inverse (2 powr (real i))"
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1984
    by (simp add: powr_minus[symmetric] powr_add[symmetric] field_simps)
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1985
  hence "real (mantissa x) * 2 powr (1 - real (bitlen (mantissa x))) =
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1986
    (real (mantissa x) * 2 ^ i) * 2 powr (1 - real (bitlen (mantissa x * 2 ^ i)))"
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1987
    using `mantissa x > 0` by (simp add: powr_realpow)
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  1988
  then show ?th2
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  1989
    unfolding i by transfer auto
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1990
qed
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1991
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1992
lemma compute_ln[code]:
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1993
  fixes m e
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1994
  defines "x \<equiv> Float m e"
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  1995
  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
  1996
              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
  1997
            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
  1998
                 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
  1999
            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
  2000
                                   else let l = bitlen m - 1 in
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2001
                                        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
  2002
    (is ?th1)
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2003
  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
  2004
            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
  2005
            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
  2006
                 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
  2007
            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
  2008
                                              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
  2009
                                   else let l = bitlen m - 1 in
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2010
                                        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
  2011
    (is ?th2)
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2012
proof -
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2013
  from assms Float_pos_eq_mantissa_pos have "x > 0 \<Longrightarrow> m > 0" by simp
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2014
  thus ?th1 ?th2 using Float_representation_aux[of m e] unfolding x_def[symmetric]
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  2015
    by (auto dest: not_leE)
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2016
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2017
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2018
lemma ln_shifted_float: assumes "0 < m" 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
  2019
proof -
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2020
  let ?B = "2^nat (bitlen m - 1)"
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2021
  def bl \<equiv> "bitlen m - 1"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2022
  have "0 < real m" and "\<And>X. (0 :: real) < 2^X" and "0 < (2 :: real)" and "m \<noteq> 0" using assms by auto
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2023
  hence "0 \<le> bl" by (simp add: bitlen_def bl_def)
31468
b8267feaf342 Approximation: Corrected precision of ln on all real values
hoelzl
parents: 31467
diff changeset
  2024
  show ?thesis
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2025
  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
  2026
    case True
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2027
    thus ?thesis
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2028
      unfolding bl_def[symmetric] using `0 < real m` `0 \<le> bl`
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2029
      apply (simp add: ln_mult)
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2030
      apply (cases "e=0")
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2031
        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
  2032
        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
  2033
      done
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2034
  next
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2035
    case False hence "0 < -e" by auto
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2036
    have lne: "ln (2 powr real e) = ln (inverse (2 powr - e))" by (simp add: powr_minus)
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2037
    hence pow_gt0: "(0::real) < 2^nat (-e)" by auto
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2038
    hence inv_gt0: "(0::real) < inverse (2^nat (-e))" by auto
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2039
    show ?thesis using False unfolding bl_def[symmetric] using `0 < real m` `0 \<le> bl`
56483
5b82c58b665c generalize ln/log_powr; add log_base_powr/pow
hoelzl
parents: 56479
diff changeset
  2040
      by (auto simp add: lne ln_mult ln_powr ln_div field_simps)
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2041
  qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2042
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2043
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2044
lemma ub_ln_lb_ln_bounds': assumes "1 \<le> x"
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2045
  shows "the (lb_ln prec x) \<le> ln x \<and> ln x \<le> the (ub_ln prec x)"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2046
  (is "?lb \<le> ?ln \<and> ?ln \<le> ?ub")
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2047
proof (cases "x < Float 1 1")
31468
b8267feaf342 Approximation: Corrected precision of ln on all real values
hoelzl
parents: 31467
diff changeset
  2048
  case True
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  2049
  hence "real (x - 1) < 1" and "real x < 2" by auto
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  2050
  have "\<not> x \<le> 0" and "\<not> x < 1" using `1 \<le> x` by auto
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  2051
  hence "0 \<le> real (x - 1)" using `1 \<le> x` by auto
31468
b8267feaf342 Approximation: Corrected precision of ln on all real values
hoelzl
parents: 31467
diff changeset
  2052
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
  2053
  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
  2054
b8267feaf342 Approximation: Corrected precision of ln on all real values
hoelzl
parents: 31467
diff changeset
  2055
  show ?thesis
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
  2056
  proof (cases "x \<le> Float 3 (- 1)")
31468
b8267feaf342 Approximation: Corrected precision of ln on all real values
hoelzl
parents: 31467
diff changeset
  2057
    case True
b8267feaf342 Approximation: Corrected precision of ln on all real values
hoelzl
parents: 31467
diff changeset
  2058
    show ?thesis unfolding lb_ln.simps unfolding ub_ln.simps Let_def
b8267feaf342 Approximation: Corrected precision of ln on all real values
hoelzl
parents: 31467
diff changeset
  2059
      using ln_float_bounds[OF `0 \<le> real (x - 1)` `real (x - 1) < 1`, of prec] `\<not> x \<le> 0` `\<not> x < 1` True
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2060
      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
  2061
  next
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  2062
    case False hence *: "3 / 2 < x" by auto
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2063
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2064
    with ln_add[of "3 / 2" "x - 3 / 2"]
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2065
    have add: "ln x = ln (3 / 2) + ln (real x * 2 / 3)"
31468
b8267feaf342 Approximation: Corrected precision of ln on all real values
hoelzl
parents: 31467
diff changeset
  2066
      by (auto simp add: algebra_simps diff_divide_distrib)
b8267feaf342 Approximation: Corrected precision of ln on all real values
hoelzl
parents: 31467
diff changeset
  2067
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2068
    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
  2069
    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
  2070
b8267feaf342 Approximation: Corrected precision of ln on all real values
hoelzl
parents: 31467
diff changeset
  2071
    { have up: "real (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
  2072
        by (rule rapprox_rat_le1) simp_all
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2073
      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
  2074
        by (rule order_trans[OF _ rapprox_rat]) simp
31468
b8267feaf342 Approximation: Corrected precision of ln on all real values
hoelzl
parents: 31467
diff changeset
  2075
      from mult_less_le_imp_less[OF * low] *
b8267feaf342 Approximation: Corrected precision of ln on all real values
hoelzl
parents: 31467
diff changeset
  2076
      have pos: "0 < real (x * rapprox_rat prec 2 3 - 1)" by auto
b8267feaf342 Approximation: Corrected precision of ln on all real values
hoelzl
parents: 31467
diff changeset
  2077
b8267feaf342 Approximation: Corrected precision of ln on all real values
hoelzl
parents: 31467
diff changeset
  2078
      have "ln (real x * 2/3)
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  2079
        \<le> ln (real (x * rapprox_rat prec 2 3 - 1) + 1)"
31468
b8267feaf342 Approximation: Corrected precision of ln on all real values
hoelzl
parents: 31467
diff changeset
  2080
      proof (rule ln_le_cancel_iff[symmetric, THEN iffD1])
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  2081
        show "real x * 2 / 3 \<le> real (x * rapprox_rat prec 2 3 - 1) + 1"
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  2082
          using * low by auto
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  2083
        show "0 < real x * 2 / 3" using * by simp
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  2084
        show "0 < real (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
  2085
      qed
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2086
      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
  2087
      proof (rule float_round_up_le, rule ln_float_bounds(2))
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  2088
        from mult_less_le_imp_less[OF `real x < 2` up] low *
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  2089
        show "real (x * rapprox_rat prec 2 3 - 1) < 1" by auto
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  2090
        show "0 \<le> real (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
  2091
      qed
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2092
     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
  2093
          + ?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
  2094
        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
  2095
        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
  2096
      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
  2097
    }
31468
b8267feaf342 Approximation: Corrected precision of ln on all real values
hoelzl
parents: 31467
diff changeset
  2098
    moreover
b8267feaf342 Approximation: Corrected precision of ln on all real values
hoelzl
parents: 31467
diff changeset
  2099
    { 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
  2100
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2101
      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
  2102
        by (rule order_trans[OF lapprox_rat], simp)
31468
b8267feaf342 Approximation: Corrected precision of ln on all real values
hoelzl
parents: 31467
diff changeset
  2103
b8267feaf342 Approximation: Corrected precision of ln on all real values
hoelzl
parents: 31467
diff changeset
  2104
      have low: "0 \<le> real (lapprox_rat prec 2 3)"
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2105
        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
  2106
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2107
      have "?lb_horner ?max
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  2108
        \<le> ln (real ?max + 1)"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2109
      proof (rule float_round_down_le, rule ln_float_bounds(1))
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  2110
        from mult_less_le_imp_less[OF `real x < 2` up] * low
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  2111
        show "real ?max < 1" by (cases "real (lapprox_rat prec 2 3) = 0",
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  2112
          auto simp add: real_of_float_max)
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  2113
        show "0 \<le> real ?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
  2114
      qed
b8267feaf342 Approximation: Corrected precision of ln on all real values
hoelzl
parents: 31467
diff changeset
  2115
      also have "\<dots> \<le> ln (real x * 2/3)"
b8267feaf342 Approximation: Corrected precision of ln on all real values
hoelzl
parents: 31467
diff changeset
  2116
      proof (rule ln_le_cancel_iff[symmetric, THEN iffD1])
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  2117
        show "0 < real ?max + 1" by (auto simp add: real_of_float_max)
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  2118
        show "0 < real x * 2/3" using * by auto
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  2119
        show "real ?max + 1 \<le> real x * 2/3" using * up
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  2120
          by (cases "0 < real x * real (lapprox_posrat prec 2 3) - 1",
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2121
              auto simp add: max_def)
31468
b8267feaf342 Approximation: Corrected precision of ln on all real values
hoelzl
parents: 31467
diff changeset
  2122
      qed
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2123
      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
  2124
        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
  2125
        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
  2126
      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
  2127
    }
31468
b8267feaf342 Approximation: Corrected precision of ln on all real values
hoelzl
parents: 31467
diff changeset
  2128
    ultimately
b8267feaf342 Approximation: Corrected precision of ln on all real values
hoelzl
parents: 31467
diff changeset
  2129
    show ?thesis unfolding lb_ln.simps unfolding ub_ln.simps Let_def
b8267feaf342 Approximation: Corrected precision of ln on all real values
hoelzl
parents: 31467
diff changeset
  2130
      using `\<not> x \<le> 0` `\<not> x < 1` True False by auto
b8267feaf342 Approximation: Corrected precision of ln on all real values
hoelzl
parents: 31467
diff changeset
  2131
  qed
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2132
next
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2133
  case False
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
  2134
  hence "\<not> x \<le> 0" and "\<not> x < 1" "0 < x" "\<not> x \<le> Float 3 (- 1)"
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  2135
    using `1 \<le> x` by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2136
  show ?thesis
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2137
  proof -
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2138
    def m \<equiv> "mantissa x"
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2139
    def e \<equiv> "exponent x"
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2140
    from Float_mantissa_exponent[of x] have Float: "x = Float m e" by (simp add: m_def e_def)
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2141
    let ?s = "Float (e + (bitlen m - 1)) 0"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2142
    let ?x = "Float m (- (bitlen m - 1))"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2143
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
  2144
    have "0 < m" and "m \<noteq> 0" using `0 < x` Float powr_gt_zero[of 2 e] 
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
  2145
      apply (auto simp add: zero_less_mult_iff)
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
  2146
      using not_le powr_ge_pzero by blast
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2147
    def bl \<equiv> "bitlen m - 1" hence "bl \<ge> 0" using `m > 0` by (simp add: bitlen_def)
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2148
    have "1 \<le> Float m e" using `1 \<le> x` Float unfolding less_eq_float_def by auto
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2149
    from bitlen_div[OF `0 < m`] float_gt1_scale[OF `1 \<le> Float m e`] `bl \<ge> 0`
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2150
    have x_bnds: "0 \<le> real (?x - 1)" "real (?x - 1) < 1"
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2151
      unfolding bl_def[symmetric]
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2152
      by (auto simp: powr_realpow[symmetric] field_simps inverse_eq_divide)
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2153
         (auto simp : powr_minus field_simps inverse_eq_divide)
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2154
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2155
    {
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2156
      have "float_round_down prec (lb_ln2 prec * ?s) \<le> ln 2 * (e + (bitlen m - 1))" (is "real ?lb2 \<le> _")
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2157
        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
  2158
        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
  2159
        using lb_ln2[of prec]
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2160
      proof (rule mult_mono)
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2161
        from float_gt1_scale[OF `1 \<le> Float m e`]
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2162
        show "0 \<le> real (Float (e + (bitlen m - 1)) 0)" by simp
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2163
      qed auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2164
      moreover
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2165
      from ln_float_bounds(1)[OF x_bnds]
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2166
      have "float_round_down prec ((?x - 1) * lb_ln_horner prec (get_even prec) 1 (?x - 1)) \<le> ln ?x" (is "real ?lb_horner \<le> _")
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2167
        by (auto intro!: float_round_down_le)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2168
      ultimately have "float_plus_down prec ?lb2 ?lb_horner \<le> ln x"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2169
        unfolding Float ln_shifted_float[OF `0 < m`, 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
  2170
    }
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2171
    moreover
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2172
    {
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2173
      from ln_float_bounds(2)[OF x_bnds]
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2174
      have "ln ?x \<le> float_round_up prec ((?x - 1) * ub_ln_horner prec (get_odd prec) 1 (?x - 1))" (is "_ \<le> real ?ub_horner")
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2175
        by (auto intro!: float_round_up_le)
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2176
      moreover
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2177
      have "ln 2 * (e + (bitlen m - 1)) \<le> float_round_up prec (ub_ln2 prec * ?s)" (is "_ \<le> real ?ub2")
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2178
        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
  2179
        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
  2180
        using ub_ln2[of prec]
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2181
      proof (rule mult_mono)
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2182
        from float_gt1_scale[OF `1 \<le> Float m e`]
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  2183
        show "0 \<le> real (e + (bitlen m - 1))" by auto
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2184
      next
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
  2185
        have "0 \<le> ln (2 :: real)" by simp
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2186
        thus "0 \<le> real (ub_ln2 prec)" using ub_ln2[of prec] by arith
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2187
      qed auto
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2188
      ultimately have "ln x \<le> float_plus_up prec ?ub2 ?ub_horner"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2189
        unfolding Float ln_shifted_float[OF `0 < m`, of e]
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2190
        by (auto intro!: float_plus_up_le)
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2191
    }
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2192
    ultimately show ?thesis unfolding lb_ln.simps unfolding ub_ln.simps
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
  2193
      unfolding if_not_P[OF `\<not> x \<le> 0`] if_not_P[OF `\<not> x < 1`] if_not_P[OF False] if_not_P[OF `\<not> x \<le> Float 3 (- 1)`] Let_def
47601
050718fe6eee use real :: float => real as lifting-morphism so we can directlry use the rep_eq theorems
hoelzl
parents: 47600
diff changeset
  2194
      unfolding plus_float.rep_eq e_def[symmetric] m_def[symmetric] by simp
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2195
  qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2196
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2197
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  2198
lemma ub_ln_lb_ln_bounds:
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  2199
  assumes "0 < x"
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2200
  shows "the (lb_ln prec x) \<le> ln x \<and> ln x \<le> the (ub_ln prec x)"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2201
  (is "?lb \<le> ?ln \<and> ?ln \<le> ?ub")
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2202
proof (cases "x < 1")
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2203
  case False hence "1 \<le> x" unfolding less_float_def less_eq_float_def by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2204
  show ?thesis using ub_ln_lb_ln_bounds'[OF `1 \<le> x`] .
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2205
next
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  2206
  case True have "\<not> x \<le> 0" using `0 < x` by auto
58982
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58889
diff changeset
  2207
  from True have "real x \<le> 1" "x \<le> 1" by simp_all
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  2208
  have "0 < real x" and "real x \<noteq> 0" using `0 < x` by auto
31098
73dd67adf90a replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
hoelzl
parents: 30971
diff changeset
  2209
  hence A: "0 < 1 / real x" by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2210
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2211
  {
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2212
    let ?divl = "float_divl (max prec 1) 1 x"
58982
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58889
diff changeset
  2213
    have A': "1 \<le> ?divl" using float_divl_pos_less1_bound[OF `0 < real x` `real x \<le> 1`] by auto
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  2214
    hence B: "0 < real ?divl" by auto
31468
b8267feaf342 Approximation: Corrected precision of ln on all real values
hoelzl
parents: 31467
diff changeset
  2215
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2216
    have "ln ?divl \<le> ln (1 / x)" unfolding ln_le_cancel_iff[OF B A] using float_divl[of _ 1 x] by auto
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2217
    hence "ln x \<le> - ln ?divl" unfolding nonzero_inverse_eq_divide[OF `real x \<noteq> 0`, symmetric] ln_inverse[OF `0 < real x`] by auto
31468
b8267feaf342 Approximation: Corrected precision of ln on all real values
hoelzl
parents: 31467
diff changeset
  2218
    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
  2219
    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
  2220
  } moreover
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2221
  {
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2222
    let ?divr = "float_divr prec 1 x"
58982
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58889
diff changeset
  2223
    have A': "1 \<le> ?divr" using float_divr_pos_less1_lower_bound[OF `0 < x` `x \<le> 1`] unfolding less_eq_float_def less_float_def by auto
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  2224
    hence B: "0 < real ?divr" by auto
31468
b8267feaf342 Approximation: Corrected precision of ln on all real values
hoelzl
parents: 31467
diff changeset
  2225
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2226
    have "ln (1 / x) \<le> ln ?divr" unfolding ln_le_cancel_iff[OF A B] using float_divr[of 1 x] by auto
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2227
    hence "- ln ?divr \<le> ln x" unfolding nonzero_inverse_eq_divide[OF `real x \<noteq> 0`, symmetric] ln_inverse[OF `0 < real x`] by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2228
    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
  2229
    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
  2230
  }
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2231
  ultimately show ?thesis unfolding lb_ln.simps[where x=x]  ub_ln.simps[where x=x]
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2232
    unfolding if_not_P[OF `\<not> x \<le> 0`] if_P[OF True] by auto
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2233
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2234
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  2235
lemma lb_ln:
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  2236
  assumes "Some y = lb_ln prec x"
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2237
  shows "y \<le> ln x" and "0 < real x"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2238
proof -
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2239
  have "0 < x"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2240
  proof (rule ccontr)
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2241
    assume "\<not> 0 < x" hence "x \<le> 0" unfolding less_eq_float_def less_float_def by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2242
    thus False using assms by auto
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2243
  qed
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  2244
  thus "0 < real x" by auto
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2245
  have "the (lb_ln prec x) \<le> ln x" using ub_ln_lb_ln_bounds[OF `0 < x`] ..
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2246
  thus "y \<le> ln x" unfolding assms[symmetric] by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2247
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2248
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  2249
lemma ub_ln:
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  2250
  assumes "Some y = ub_ln prec x"
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2251
  shows "ln x \<le> y" and "0 < real x"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2252
proof -
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2253
  have "0 < x"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2254
  proof (rule ccontr)
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  2255
    assume "\<not> 0 < x" hence "x \<le> 0" by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2256
    thus False using assms by auto
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2257
  qed
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  2258
  thus "0 < real x" by auto
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2259
  have "ln x \<le> the (ub_ln prec x)" using ub_ln_lb_ln_bounds[OF `0 < x`] ..
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2260
  thus "ln x \<le> y" unfolding assms[symmetric] by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2261
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2262
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2263
lemma bnds_ln: "\<forall> (x::real) lx ux. (Some l, Some u) = (lb_ln prec lx, ub_ln prec ux) \<and> x \<in> {lx .. ux} \<longrightarrow> l \<le> ln x \<and> ln x \<le> u"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2264
proof (rule allI, rule allI, rule allI, rule impI)
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2265
  fix x::real and lx ux
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2266
  assume "(Some l, Some u) = (lb_ln prec lx, ub_ln prec ux) \<and> x \<in> {lx .. ux}"
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2267
  hence l: "Some l = lb_ln prec lx " and u: "Some u = ub_ln prec ux" and x: "x \<in> {lx .. ux}" by auto
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2268
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2269
  have "ln ux \<le> u" and "0 < real ux" using ub_ln u by auto
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2270
  have "l \<le> ln lx" and "0 < real lx" and "0 < x" using lb_ln[OF l] x by auto
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2271
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2272
  from ln_le_cancel_iff[OF `0 < real lx` `0 < x`] `l \<le> ln lx`
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2273
  have "l \<le> ln x" using x unfolding atLeastAtMost_iff by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2274
  moreover
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2275
  from ln_le_cancel_iff[OF `0 < x` `0 < real ux`] `ln ux \<le> real u`
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2276
  have "ln x \<le> u" using x unfolding atLeastAtMost_iff by auto
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2277
  ultimately show "l \<le> ln x \<and> ln x \<le> u" ..
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2278
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2279
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2280
section "Implement floatarith"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2281
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2282
subsection "Define syntax and semantics"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2283
58310
91ea607a34d8 updated news
blanchet
parents: 58249
diff changeset
  2284
datatype floatarith
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2285
  = Add floatarith floatarith
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2286
  | Minus floatarith
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2287
  | Mult floatarith floatarith
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2288
  | Inverse floatarith
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2289
  | Cos floatarith
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2290
  | Arctan floatarith
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2291
  | Abs floatarith
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2292
  | Max floatarith floatarith
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2293
  | Min floatarith floatarith
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2294
  | Pi
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2295
  | Sqrt floatarith
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2296
  | Exp floatarith
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2297
  | Ln floatarith
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2298
  | Power floatarith nat
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
  2299
  | Var nat
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2300
  | Num float
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2301
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  2302
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
  2303
"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
  2304
"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
  2305
"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
  2306
"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
  2307
"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
  2308
"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
  2309
"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
  2310
"interpret_floatarith (Max a b) vs    = max (interpret_floatarith a vs) (interpret_floatarith b vs)" |
73dd67adf90a replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
hoelzl
parents: 30971
diff changeset
  2311
"interpret_floatarith (Abs a) vs      = abs (interpret_floatarith a vs)" |
73dd67adf90a replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
hoelzl
parents: 30971
diff changeset
  2312
"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
  2313
"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
  2314
"interpret_floatarith (Exp a) vs      = exp (interpret_floatarith a vs)" |
73dd67adf90a replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
hoelzl
parents: 30971
diff changeset
  2315
"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
  2316
"interpret_floatarith (Power a n) vs  = (interpret_floatarith a vs)^n" |
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2317
"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
  2318
"interpret_floatarith (Var n) vs     = vs ! n"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2319
31811
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2320
lemma interpret_floatarith_divide: "interpret_floatarith (Mult a (Inverse b)) vs = (interpret_floatarith a vs) / (interpret_floatarith b vs)"
36778
739a9379e29b avoid using real-specific versions of generic lemmas
huffman
parents: 36531
diff changeset
  2321
  unfolding divide_inverse interpret_floatarith.simps ..
31811
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2322
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2323
lemma interpret_floatarith_diff: "interpret_floatarith (Add a (Minus b)) vs = (interpret_floatarith a vs) - (interpret_floatarith b vs)"
54230
b1d955791529 more simplification rules on unary and binary minus
haftmann
parents: 53077
diff changeset
  2324
  unfolding interpret_floatarith.simps by simp
31811
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2325
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
  2326
lemma interpret_floatarith_sin: "interpret_floatarith (Cos (Add (Mult Pi (Num (Float 1 (- 1)))) (Minus a))) vs =
31811
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2327
  sin (interpret_floatarith a vs)"
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2328
  unfolding sin_cos_eq interpret_floatarith.simps
54230
b1d955791529 more simplification rules on unary and binary minus
haftmann
parents: 53077
diff changeset
  2329
            interpret_floatarith_divide interpret_floatarith_diff
31811
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2330
  by auto
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2331
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2332
lemma interpret_floatarith_tan:
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
  2333
  "interpret_floatarith (Mult (Cos (Add (Mult Pi (Num (Float 1 (- 1)))) (Minus a))) (Inverse (Cos a))) vs =
31811
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2334
   tan (interpret_floatarith a vs)"
36778
739a9379e29b avoid using real-specific versions of generic lemmas
huffman
parents: 36531
diff changeset
  2335
  unfolding interpret_floatarith.simps(3,4) interpret_floatarith_sin tan_def divide_inverse
31811
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2336
  by auto
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2337
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
  2338
lemma interpret_floatarith_log: 
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
  2339
    "interpret_floatarith ((Mult (Ln x) (Inverse (Ln b)))) vs = 
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
  2340
     log (interpret_floatarith b vs) (interpret_floatarith x vs)"
36778
739a9379e29b avoid using real-specific versions of generic lemmas
huffman
parents: 36531
diff changeset
  2341
  unfolding log_def interpret_floatarith.simps divide_inverse ..
31811
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2342
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2343
lemma interpret_floatarith_num:
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2344
  shows "interpret_floatarith (Num (Float 0 0)) vs = 0"
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2345
  and "interpret_floatarith (Num (Float 1 0)) vs = 1"
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54269
diff changeset
  2346
  and "interpret_floatarith (Num (Float (- 1) 0)) vs = - 1"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46545
diff changeset
  2347
  and "interpret_floatarith (Num (Float (numeral a) 0)) vs = numeral a"
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54269
diff changeset
  2348
  and "interpret_floatarith (Num (Float (- numeral a) 0)) vs = - numeral a" by auto
31811
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2349
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2350
subsection "Implement approximation function"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2351
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2352
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
  2353
"lift_bin' (Some (l1, u1)) (Some (l2, u2)) f = Some (f l1 u1 l2 u2)" |
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2354
"lift_bin' a b f = None"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2355
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2356
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
  2357
"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
  2358
                                             | t \<Rightarrow> None)" |
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2359
"lift_un b f = None"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2360
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2361
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
  2362
"lift_un' (Some (l1, u1)) f = Some (f l1 u1)" |
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2363
"lift_un' b f = None"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2364
31811
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2365
definition
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2366
"bounded_by xs vs \<longleftrightarrow>
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2367
  (\<forall> i < length vs. case vs ! i of None \<Rightarrow> True
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2368
         | Some (l, u) \<Rightarrow> xs ! i \<in> { real l .. real u })"
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2369
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2370
lemma bounded_byE:
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2371
  assumes "bounded_by xs vs"
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2372
  shows "\<And> i. i < length vs \<Longrightarrow> case vs ! i of None \<Rightarrow> True
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2373
         | Some (l, u) \<Rightarrow> xs ! i \<in> { real l .. real u }"
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2374
  using assms bounded_by_def by blast
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2375
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2376
lemma bounded_by_update:
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2377
  assumes "bounded_by xs vs"
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2378
  and bnd: "xs ! i \<in> { real l .. real u }"
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2379
  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
  2380
proof -
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2381
{ fix j
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2382
  let ?vs = "vs[i := Some (l,u)]"
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2383
  assume "j < length ?vs" hence [simp]: "j < length vs" by simp
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2384
  have "case ?vs ! j of None \<Rightarrow> True | Some (l, u) \<Rightarrow> xs ! j \<in> { real l .. real u }"
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2385
  proof (cases "?vs ! j")
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2386
    case (Some b)
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2387
    thus ?thesis
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2388
    proof (cases "i = j")
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2389
      case True
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2390
      thus ?thesis using `?vs ! j = Some b` and bnd by auto
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2391
    next
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2392
      case False
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2393
      thus ?thesis using `bounded_by xs vs` unfolding bounded_by_def by auto
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2394
    qed
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2395
  qed auto }
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2396
  thus ?thesis unfolding bounded_by_def by auto
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2397
qed
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2398
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2399
lemma bounded_by_None:
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2400
  shows "bounded_by xs (replicate (length xs) None)"
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2401
  unfolding bounded_by_def by auto
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2402
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2403
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
  2404
"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
  2405
"approx prec (Add a b) bs   =
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2406
  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
  2407
    (\<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
  2408
"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
  2409
"approx prec (Mult a b) bs  =
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2410
  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
  2411
    (\<lambda> a1 a2 b1 b2.
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2412
      (float_plus_down prec (nprt a1 * pprt b2)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2413
          (float_plus_down prec (nprt a2 * nprt b2)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2414
            (float_plus_down prec (pprt a1 * pprt b1) (pprt a2 * nprt b1))),
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2415
        float_plus_up prec (pprt a2 * pprt b2)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2416
            (float_plus_up prec (pprt a1 * nprt b2)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2417
              (float_plus_up prec (nprt a2 * pprt b1) (nprt a1 * nprt b1)))))" |
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2418
"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
  2419
"approx prec (Cos a) bs     = lift_un' (approx' prec a bs) (bnds_cos prec)" |
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2420
"approx prec Pi bs          = Some (lb_pi prec, ub_pi prec)" |
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2421
"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
  2422
"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
  2423
"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
  2424
"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
  2425
"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
  2426
"approx prec (Exp a) bs     = lift_un' (approx' prec a bs) (\<lambda> l u. (lb_exp prec l, ub_exp prec u))" |
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2427
"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
  2428
"approx prec (Power a n) bs = lift_un' (approx' prec a bs) (float_power_bnds prec n)" |
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2429
"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
  2430
"approx prec (Var i) bs    = (if i < length bs then bs ! i else None)"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2431
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2432
lemma lift_bin'_ex:
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2433
  assumes lift_bin'_Some: "Some (l, u) = lift_bin' a b f"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2434
  shows "\<exists> l1 u1 l2 u2. Some (l1, u1) = a \<and> Some (l2, u2) = b"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2435
proof (cases a)
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2436
  case None hence "None = lift_bin' a b f" unfolding None lift_bin'.simps ..
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2437
  thus ?thesis using lift_bin'_Some by auto
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2438
next
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2439
  case (Some a')
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2440
  show ?thesis
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2441
  proof (cases b)
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2442
    case None hence "None = lift_bin' a b f" unfolding None lift_bin'.simps ..
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2443
    thus ?thesis using lift_bin'_Some by auto
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2444
  next
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2445
    case (Some b')
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2446
    obtain la ua where a': "a' = (la, ua)" by (cases a', auto)
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2447
    obtain lb ub where b': "b' = (lb, ub)" by (cases b', auto)
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2448
    thus ?thesis unfolding `a = Some a'` `b = Some b'` a' b' by auto
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2449
  qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2450
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2451
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2452
lemma lift_bin'_f:
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2453
  assumes lift_bin'_Some: "Some (l, u) = lift_bin' (g a) (g b) f"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2454
  and Pa: "\<And>l u. Some (l, u) = g a \<Longrightarrow> P l u a" and Pb: "\<And>l u. Some (l, u) = g b \<Longrightarrow> P l u b"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2455
  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
  2456
proof -
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2457
  obtain l1 u1 l2 u2
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2458
    where Sa: "Some (l1, u1) = g a" and Sb: "Some (l2, u2) = g b" using lift_bin'_ex[OF assms(1)] by auto
31809
hoelzl
parents: 31790
diff changeset
  2459
  have lu: "(l, u) = f l1 u1 l2 u2" using lift_bin'_Some[unfolded Sa[symmetric] Sb[symmetric] lift_bin'.simps] by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2460
  have "l = fst (f l1 u1 l2 u2)" and "u = snd (f l1 u1 l2 u2)" unfolding lu[symmetric] by auto
31809
hoelzl
parents: 31790
diff changeset
  2461
  thus ?thesis using Pa[OF Sa] Pb[OF Sb] by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2462
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2463
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2464
lemma approx_approx':
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2465
  assumes Pa: "\<And>l u. Some (l, u) = approx prec a vs \<Longrightarrow> l \<le> interpret_floatarith a xs \<and> interpret_floatarith a xs \<le> u"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2466
  and approx': "Some (l, u) = approx' prec a vs"
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2467
  shows "l \<le> interpret_floatarith a xs \<and> interpret_floatarith a xs \<le> u"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2468
proof -
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2469
  obtain l' u' where S: "Some (l', u') = approx prec a vs"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2470
    using approx' unfolding approx'.simps by (cases "approx prec a vs", auto)
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2471
  have l': "l = float_round_down prec l'" and u': "u = float_round_up prec u'"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2472
    using approx' unfolding approx'.simps S[symmetric] by auto
31809
hoelzl
parents: 31790
diff changeset
  2473
  show ?thesis unfolding l' u'
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2474
    using order_trans[OF Pa[OF S, THEN conjunct2] float_round_up[of u']]
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2475
    using order_trans[OF float_round_down[of _ l'] Pa[OF S, THEN conjunct1]] by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2476
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2477
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2478
lemma lift_bin':
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2479
  assumes lift_bin'_Some: "Some (l, u) = lift_bin' (approx' prec a bs) (approx' prec b bs) f"
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2480
  and Pa: "\<And>l u. Some (l, u) = approx prec a bs \<Longrightarrow> l \<le> interpret_floatarith a xs \<and> interpret_floatarith a xs \<le> u" (is "\<And>l u. _ = ?g a \<Longrightarrow> ?P l u a")
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2481
  and Pb: "\<And>l u. Some (l, u) = approx prec b bs \<Longrightarrow> l \<le> interpret_floatarith b xs \<and> interpret_floatarith b xs \<le> u"
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2482
  shows "\<exists> l1 u1 l2 u2. (l1 \<le> interpret_floatarith a xs \<and> interpret_floatarith a xs \<le> u1) \<and>
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2483
                        (l2 \<le> interpret_floatarith b xs \<and> interpret_floatarith b xs \<le> u2) \<and>
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2484
                        l = fst (f l1 u1 l2 u2) \<and> u = snd (f l1 u1 l2 u2)"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2485
proof -
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2486
  { fix l u assume "Some (l, u) = approx' prec a bs"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2487
    with approx_approx'[of prec a bs, OF _ this] Pa
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2488
    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
  2489
  { fix l u assume "Some (l, u) = approx' prec b bs"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2490
    with approx_approx'[of prec b bs, OF _ this] Pb
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2491
    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
  2492
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2493
  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
  2494
  show ?thesis by auto
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2495
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2496
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2497
lemma lift_un'_ex:
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2498
  assumes lift_un'_Some: "Some (l, u) = lift_un' a f"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2499
  shows "\<exists> l u. Some (l, u) = a"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2500
proof (cases a)
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2501
  case None hence "None = lift_un' a f" unfolding None lift_un'.simps ..
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2502
  thus ?thesis using lift_un'_Some by auto
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2503
next
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2504
  case (Some a')
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2505
  obtain la ua where a': "a' = (la, ua)" by (cases a', auto)
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2506
  thus ?thesis unfolding `a = Some a'` a' by auto
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2507
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2508
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2509
lemma lift_un'_f:
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2510
  assumes lift_un'_Some: "Some (l, u) = lift_un' (g a) f"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2511
  and Pa: "\<And>l u. Some (l, u) = g a \<Longrightarrow> P l u a"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2512
  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
  2513
proof -
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2514
  obtain l1 u1 where Sa: "Some (l1, u1) = g a" using lift_un'_ex[OF assms(1)] by auto
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2515
  have lu: "(l, u) = f l1 u1" using lift_un'_Some[unfolded Sa[symmetric] lift_un'.simps] by auto
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2516
  have "l = fst (f l1 u1)" and "u = snd (f l1 u1)" unfolding lu[symmetric] by auto
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2517
  thus ?thesis using Pa[OF Sa] by auto
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2518
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2519
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2520
lemma lift_un':
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2521
  assumes lift_un'_Some: "Some (l, u) = lift_un' (approx' prec a bs) f"
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2522
  and Pa: "\<And>l u. Some (l, u) = approx prec a bs \<Longrightarrow> l \<le> interpret_floatarith a xs \<and> interpret_floatarith a xs \<le> u" (is "\<And>l u. _ = ?g a \<Longrightarrow> ?P l u a")
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2523
  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
  2524
                        l = fst (f l1 u1) \<and> u = snd (f l1 u1)"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2525
proof -
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2526
  { fix l u assume "Some (l, u) = approx' prec a bs"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2527
    with approx_approx'[of prec a bs, OF _ this] Pa
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2528
    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
  2529
  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
  2530
  show ?thesis by auto
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2531
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2532
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2533
lemma lift_un'_bnds:
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2534
  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"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2535
  and lift_un'_Some: "Some (l, u) = lift_un' (approx' prec a bs) f"
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2536
  and Pa: "\<And>l u. Some (l, u) = approx prec a bs \<Longrightarrow> l \<le> interpret_floatarith a xs \<and> interpret_floatarith a xs \<le> u"
31098
73dd67adf90a replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
hoelzl
parents: 30971
diff changeset
  2537
  shows "real l \<le> f' (interpret_floatarith a xs) \<and> f' (interpret_floatarith a xs) \<le> real u"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2538
proof -
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2539
  from lift_un'[OF lift_un'_Some Pa]
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2540
  obtain l1 u1 where "l1 \<le> interpret_floatarith a xs" and "interpret_floatarith a xs \<le> u1" and "l = fst (f l1 u1)" and "u = snd (f l1 u1)" by blast
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2541
  hence "(l, u) = f l1 u1" and "interpret_floatarith a xs \<in> {l1 .. u1}" by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2542
  thus ?thesis using bnds by auto
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2543
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2544
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2545
lemma lift_un_ex:
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2546
  assumes lift_un_Some: "Some (l, u) = lift_un a f"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2547
  shows "\<exists> l u. Some (l, u) = a"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2548
proof (cases a)
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2549
  case None hence "None = lift_un a f" unfolding None lift_un.simps ..
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2550
  thus ?thesis using lift_un_Some by auto
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2551
next
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2552
  case (Some a')
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2553
  obtain la ua where a': "a' = (la, ua)" by (cases a', auto)
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2554
  thus ?thesis unfolding `a = Some a'` a' by auto
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2555
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2556
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2557
lemma lift_un_f:
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2558
  assumes lift_un_Some: "Some (l, u) = lift_un (g a) f"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2559
  and Pa: "\<And>l u. Some (l, u) = g a \<Longrightarrow> P l u a"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2560
  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
  2561
proof -
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2562
  obtain l1 u1 where Sa: "Some (l1, u1) = g a" using lift_un_ex[OF assms(1)] by auto
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2563
  have "fst (f l1 u1) \<noteq> None \<and> snd (f l1 u1) \<noteq> None"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2564
  proof (rule ccontr)
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2565
    assume "\<not> (fst (f l1 u1) \<noteq> None \<and> snd (f l1 u1) \<noteq> None)"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2566
    hence or: "fst (f l1 u1) = None \<or> snd (f l1 u1) = None" by auto
31809
hoelzl
parents: 31790
diff changeset
  2567
    hence "lift_un (g a) f = None"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2568
    proof (cases "fst (f l1 u1) = None")
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2569
      case True
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2570
      then obtain b where b: "f l1 u1 = (None, b)" by (cases "f l1 u1", auto)
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2571
      thus ?thesis unfolding Sa[symmetric] lift_un.simps b by auto
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2572
    next
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2573
      case False hence "snd (f l1 u1) = None" using or by auto
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2574
      with False obtain b where b: "f l1 u1 = (Some b, None)" by (cases "f l1 u1", auto)
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2575
      thus ?thesis unfolding Sa[symmetric] lift_un.simps b by auto
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2576
    qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2577
    thus False using lift_un_Some by auto
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2578
  qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2579
  then obtain a' b' where f: "f l1 u1 = (Some a', Some b')" by (cases "f l1 u1", auto)
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2580
  from lift_un_Some[unfolded Sa[symmetric] lift_un.simps f]
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2581
  have "Some l = fst (f l1 u1)" and "Some u = snd (f l1 u1)" unfolding f by auto
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2582
  thus ?thesis unfolding Sa[symmetric] lift_un.simps using Pa[OF Sa] by auto
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2583
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2584
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2585
lemma lift_un:
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2586
  assumes lift_un_Some: "Some (l, u) = lift_un (approx' prec a bs) f"
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2587
  and Pa: "\<And>l u. Some (l, u) = approx prec a bs \<Longrightarrow> l \<le> interpret_floatarith a xs \<and> interpret_floatarith a xs \<le> u" (is "\<And>l u. _ = ?g a \<Longrightarrow> ?P l u a")
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2588
  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
  2589
                  Some l = fst (f l1 u1) \<and> Some u = snd (f l1 u1)"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2590
proof -
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2591
  { fix l u assume "Some (l, u) = approx' prec a bs"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2592
    with approx_approx'[of prec a bs, OF _ this] Pa
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2593
    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
  2594
  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
  2595
  show ?thesis by auto
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2596
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2597
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2598
lemma lift_un_bnds:
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2599
  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"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2600
  and lift_un_Some: "Some (l, u) = lift_un (approx' prec a bs) f"
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2601
  and Pa: "\<And>l u. Some (l, u) = approx prec a bs \<Longrightarrow> l \<le> interpret_floatarith a xs \<and> interpret_floatarith a xs \<le> u"
31098
73dd67adf90a replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
hoelzl
parents: 30971
diff changeset
  2602
  shows "real l \<le> f' (interpret_floatarith a xs) \<and> f' (interpret_floatarith a xs) \<le> real u"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2603
proof -
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2604
  from lift_un[OF lift_un_Some Pa]
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2605
  obtain l1 u1 where "l1 \<le> interpret_floatarith a xs" and "interpret_floatarith a xs \<le> u1" and "Some l = fst (f l1 u1)" and "Some u = snd (f l1 u1)" by blast
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2606
  hence "(Some l, Some u) = f l1 u1" and "interpret_floatarith a xs \<in> {l1 .. u1}" by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2607
  thus ?thesis using bnds by auto
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2608
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2609
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2610
lemma approx:
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2611
  assumes "bounded_by xs vs"
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2612
  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
  2613
  shows "l \<le> interpret_floatarith arith xs \<and> interpret_floatarith arith xs \<le> u" (is "?P l u arith")
31809
hoelzl
parents: 31790
diff changeset
  2614
  using `Some (l, u) = approx prec arith vs`
45129
1fce03e3e8ad tuned proofs -- eliminated vacuous "induct arbitrary: ..." situations;
wenzelm
parents: 44821
diff changeset
  2615
proof (induct arith arbitrary: l u)
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2616
  case (Add a b)
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2617
  from lift_bin'[OF Add.prems[unfolded approx.simps]] Add.hyps
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2618
  obtain l1 u1 l2 u2 where "l = float_plus_down prec l1 l2" and "u = float_plus_up prec u1 u2"
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2619
    "l1 \<le> interpret_floatarith a xs" and "interpret_floatarith a xs \<le> u1"
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2620
    "l2 \<le> interpret_floatarith b xs" and "interpret_floatarith b xs \<le> u2" unfolding fst_conv snd_conv by blast
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2621
  thus ?case unfolding interpret_floatarith.simps by (auto intro!: float_plus_up_le float_plus_down_le)
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2622
next
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2623
  case (Minus a)
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2624
  from lift_un'[OF Minus.prems[unfolded approx.simps]] Minus.hyps
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2625
  obtain l1 u1 where "l = -u1" and "u = -l1"
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2626
    "l1 \<le> interpret_floatarith a xs" and "interpret_floatarith a xs \<le> u1" unfolding fst_conv snd_conv by blast
47601
050718fe6eee use real :: float => real as lifting-morphism so we can directlry use the rep_eq theorems
hoelzl
parents: 47600
diff changeset
  2627
  thus ?case unfolding interpret_floatarith.simps using minus_float.rep_eq by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2628
next
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2629
  case (Mult a b)
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2630
  from lift_bin'[OF Mult.prems[unfolded approx.simps]] Mult.hyps
31809
hoelzl
parents: 31790
diff changeset
  2631
  obtain l1 u1 l2 u2
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2632
    where l: "l = float_plus_down prec (nprt l1 * pprt u2) (float_plus_down prec (nprt u1 * nprt u2) (float_plus_down prec (pprt l1 * pprt l2) (pprt u1 * nprt l2)))"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2633
    and u: "u = float_plus_up prec (pprt u1 * pprt u2) (float_plus_up prec (pprt l1 * nprt u2) (float_plus_up prec (nprt u1 * pprt l2) (nprt l1 * nprt l2)))"
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2634
    and "l1 \<le> interpret_floatarith a xs" and "interpret_floatarith a xs \<le> u1"
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2635
    and "l2 \<le> interpret_floatarith b xs" and "interpret_floatarith b xs \<le> u2" unfolding fst_conv snd_conv by blast
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2636
  hence bnds:
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2637
    "nprt l1 * pprt u2 + nprt u1 * nprt u2 + pprt l1 * pprt l2 + pprt u1 * nprt l2 \<le> interpret_floatarith (Mult a b) xs" (is "?l \<le> _")
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2638
    "interpret_floatarith (Mult a b) xs \<le> pprt u1 * pprt u2 + pprt l1 * nprt u2 + nprt u1 * pprt l2 + nprt l1 * nprt l2" (is "_ \<le> ?u")
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2639
    unfolding interpret_floatarith.simps l u
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2640
    using mult_le_prts mult_ge_prts by auto
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2641
  from l u have "l \<le> ?l" "?u \<le> u"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2642
    by (auto intro!: float_plus_up_le float_plus_down_le)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2643
  thus ?case using bnds by simp
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2644
next
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2645
  case (Inverse a)
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2646
  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
  2647
  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
  2648
    and u': "Some u = (if 0 < l1 \<or> u1 < 0 then Some (float_divr prec 1 l1) else None)"
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2649
    and l1: "l1 \<le> interpret_floatarith a xs" and u1: "interpret_floatarith a xs \<le> u1" by blast
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2650
  have either: "0 < l1 \<or> u1 < 0" proof (rule ccontr) assume P: "\<not> (0 < l1 \<or> u1 < 0)" show False using l' unfolding if_not_P[OF P] by auto qed
31098
73dd67adf90a replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
hoelzl
parents: 30971
diff changeset
  2651
  moreover have l1_le_u1: "real l1 \<le> real u1" using l1 u1 by auto
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  2652
  ultimately have "real l1 \<noteq> 0" and "real u1 \<noteq> 0" by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2653
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2654
  have inv: "inverse u1 \<le> inverse (interpret_floatarith a xs)
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2655
           \<and> inverse (interpret_floatarith a xs) \<le> inverse l1"
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2656
  proof (cases "0 < l1")
31809
hoelzl
parents: 31790
diff changeset
  2657
    case True hence "0 < real u1" and "0 < real l1" "0 < interpret_floatarith a xs"
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  2658
      using l1_le_u1 l1 by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2659
    show ?thesis
31098
73dd67adf90a replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
hoelzl
parents: 30971
diff changeset
  2660
      unfolding inverse_le_iff_le[OF `0 < real u1` `0 < interpret_floatarith a xs`]
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  2661
        inverse_le_iff_le[OF `0 < interpret_floatarith a xs` `0 < real l1`]
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2662
      using l1 u1 by auto
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2663
  next
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2664
    case False hence "u1 < 0" using either by blast
31809
hoelzl
parents: 31790
diff changeset
  2665
    hence "real u1 < 0" and "real l1 < 0" "interpret_floatarith a xs < 0"
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  2666
      using l1_le_u1 u1 by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2667
    show ?thesis
31098
73dd67adf90a replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
hoelzl
parents: 30971
diff changeset
  2668
      unfolding inverse_le_iff_le_neg[OF `real u1 < 0` `interpret_floatarith a xs < 0`]
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  2669
        inverse_le_iff_le_neg[OF `interpret_floatarith a xs < 0` `real l1 < 0`]
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2670
      using l1 u1 by auto
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2671
  qed
31468
b8267feaf342 Approximation: Corrected precision of ln on all real values
hoelzl
parents: 31467
diff changeset
  2672
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2673
  from l' have "l = float_divl prec 1 u1" by (cases "0 < l1 \<or> u1 < 0", auto)
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2674
  hence "l \<le> inverse u1" unfolding nonzero_inverse_eq_divide[OF `real u1 \<noteq> 0`] using float_divl[of prec 1 u1] by auto
31098
73dd67adf90a replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
hoelzl
parents: 30971
diff changeset
  2675
  also have "\<dots> \<le> inverse (interpret_floatarith a xs)" using inv by auto
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2676
  finally have "l \<le> inverse (interpret_floatarith a xs)" .
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2677
  moreover
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2678
  from u' have "u = float_divr prec 1 l1" by (cases "0 < l1 \<or> u1 < 0", auto)
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2679
  hence "inverse l1 \<le> u" unfolding nonzero_inverse_eq_divide[OF `real l1 \<noteq> 0`] using float_divr[of 1 l1 prec] by auto
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2680
  hence "inverse (interpret_floatarith a xs) \<le> u" by (rule order_trans[OF inv[THEN conjunct2]])
31098
73dd67adf90a replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
hoelzl
parents: 30971
diff changeset
  2681
  ultimately show ?case unfolding interpret_floatarith.simps using l1 u1 by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2682
next
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2683
  case (Abs x)
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2684
  from lift_un'[OF Abs.prems[unfolded approx.simps], unfolded fst_conv snd_conv] Abs.hyps
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2685
  obtain l1 u1 where l': "l = (if l1 < 0 \<and> 0 < u1 then 0 else min \<bar>l1\<bar> \<bar>u1\<bar>)" and u': "u = max \<bar>l1\<bar> \<bar>u1\<bar>"
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2686
    and l1: "l1 \<le> interpret_floatarith x xs" and u1: "interpret_floatarith x xs \<le> u1" by blast
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  2687
  thus ?case unfolding l' u' 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
  2688
next
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2689
  case (Min a b)
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2690
  from lift_bin'[OF Min.prems[unfolded approx.simps], unfolded fst_conv snd_conv] Min.hyps
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2691
  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
  2692
    and l1: "l1 \<le> interpret_floatarith a xs" and u1: "interpret_floatarith a xs \<le> u1"
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2693
    and l1: "l2 \<le> interpret_floatarith b xs" and u1: "interpret_floatarith b xs \<le> u2" by blast
31098
73dd67adf90a replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
hoelzl
parents: 30971
diff changeset
  2694
  thus ?case unfolding l' u' by (auto simp add: real_of_float_min)
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2695
next
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2696
  case (Max a b)
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2697
  from lift_bin'[OF Max.prems[unfolded approx.simps], unfolded fst_conv snd_conv] Max.hyps
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2698
  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
  2699
    and l1: "l1 \<le> interpret_floatarith a xs" and u1: "interpret_floatarith a xs \<le> u1"
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2700
    and l1: "l2 \<le> interpret_floatarith b xs" and u1: "interpret_floatarith b xs \<le> u2" by blast
31098
73dd67adf90a replaced Ifloat => real_of_float and real, renamed ApproxEq => inequality, uneq => interpret_inequality, uneq' => approx_inequality, Ifloatarith => interpret_floatarith
hoelzl
parents: 30971
diff changeset
  2701
  thus ?case unfolding l' u' by (auto simp add: real_of_float_max)
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2702
next case (Cos a) with lift_un'_bnds[OF bnds_cos] show ?case by auto
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2703
next case (Arctan a) with lift_un'_bnds[OF bnds_arctan] show ?case by auto
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2704
next case Pi with pi_boundaries show ?case 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
  2705
next case (Sqrt a) with lift_un'_bnds[OF bnds_sqrt] show ?case by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2706
next case (Exp a) with lift_un'_bnds[OF bnds_exp] show ?case by auto
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2707
next case (Ln a) with lift_un_bnds[OF bnds_ln] show ?case by auto
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2708
next case (Power a n) with lift_un'_bnds[OF bnds_power] show ?case by auto
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2709
next case (Num f) thus ?case by auto
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2710
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
  2711
  case (Var n)
31811
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2712
  from this[symmetric] `bounded_by xs vs`[THEN bounded_byE, of n]
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2713
  show ?case by (cases "n < length vs", auto)
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2714
qed
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2715
58310
91ea607a34d8 updated news
blanchet
parents: 58249
diff changeset
  2716
datatype form = Bound floatarith floatarith floatarith form
31811
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2717
              | Assign floatarith floatarith form
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2718
              | Less floatarith floatarith
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2719
              | LessEqual floatarith floatarith
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2720
              | AtLeastAtMost floatarith floatarith floatarith
58986
ec7373051a6c disjunction and conjunction for forms
immler
parents: 58985
diff changeset
  2721
              | Conj form form
ec7373051a6c disjunction and conjunction for forms
immler
parents: 58985
diff changeset
  2722
              | Disj form form
31811
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2723
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2724
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
  2725
"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
  2726
"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
  2727
"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
  2728
"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
  2729
"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
  2730
"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
  2731
"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
  2732
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2733
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
  2734
"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
  2735
"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
  2736
  (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
  2737
   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
  2738
"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
  2739
   (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
  2740
   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
  2741
    | _ \<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
  2742
"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
  2743
   (case (approx prec a bs)
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2744
   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
  2745
    | _ \<Rightarrow> False)" |
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2746
"approx_form prec (Less a b) bs ss =
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2747
   (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
  2748
   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
  2749
    | _ \<Rightarrow> False)" |
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2750
"approx_form prec (LessEqual a b) bs ss =
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2751
   (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
  2752
   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
  2753
    | _ \<Rightarrow> False)" |
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2754
"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
  2755
   (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
  2756
   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
  2757
    | _ \<Rightarrow> False)" |
58986
ec7373051a6c disjunction and conjunction for forms
immler
parents: 58985
diff changeset
  2758
"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
  2759
"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
  2760
"approx_form _ _ _ _ = False"
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2761
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
  2762
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
  2763
31811
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2764
lemma approx_form_approx_form':
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2765
  assumes "approx_form' prec f s n l u bs ss" and "(x::real) \<in> { l .. u }"
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2766
  obtains l' u' where "x \<in> { l' .. u' }"
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  2767
    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
  2768
using assms proof (induct s arbitrary: l u)
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2769
  case 0
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2770
  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
  2771
  show thesis by auto
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2772
next
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2773
  case (Suc s)
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2774
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
  2775
  let ?m = "(l + u) * Float 1 (- 1)"
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2776
  have "real l \<le> ?m" and "?m \<le> real u"
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2777
    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
  2778
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2779
  with `x \<in> { l .. u }`
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2780
  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
  2781
  thus thesis
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2782
  proof (rule disjE)
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2783
    assume *: "x \<in> { l .. ?m }"
31811
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2784
    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
  2785
    show thesis by (simp add: Let_def lazy_conj)
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2786
  next
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2787
    assume *: "x \<in> { ?m .. u }"
31811
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2788
    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
  2789
    show thesis by (simp add: Let_def lazy_conj)
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2790
  qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2791
qed
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2792
31811
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2793
lemma approx_form_aux:
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2794
  assumes "approx_form prec f vs ss"
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  2795
    and "bounded_by xs vs"
31811
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2796
  shows "interpret_form f xs"
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2797
using assms proof (induct f arbitrary: vs)
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2798
  case (Bound x a b f)
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2799
  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
  2800
    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
  2801
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2802
  with Bound.prems obtain l u' l' u
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2803
    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
  2804
    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
  2805
    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
  2806
    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
  2807
31811
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2808
  { assume "xs ! n \<in> { interpret_floatarith a xs .. interpret_floatarith b xs }"
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2809
    with approx[OF Bound.prems(2) l_eq] and approx[OF Bound.prems(2) u_eq]
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2810
    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
  2811
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2812
    from approx_form_approx_form'[OF approx_form' this]
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2813
    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
  2814
      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
  2815
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2816
    from `bounded_by xs vs` bnds
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2817
    have "bounded_by xs (vs[n := Some (lx, ux)])" by (rule bounded_by_update)
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2818
    with Bound.hyps[OF approx_form]
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2819
    have "interpret_form f xs" by blast }
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2820
  thus ?case using interpret_form.simps x_eq and interpret_floatarith.simps by simp
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2821
next
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2822
  case (Assign x a f)
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2823
  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
  2824
    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
  2825
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  2826
  with Assign.prems obtain l u
31811
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2827
    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
  2828
    and x_eq: "x = Var n"
31811
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2829
    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
  2830
    by (cases "approx prec a vs") auto
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2831
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2832
  { assume bnds: "xs ! n = interpret_floatarith a xs"
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2833
    with approx[OF Assign.prems(2) bnd_eq]
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2834
    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
  2835
    from approx_form_approx_form'[OF approx_form' this]
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  2836
    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
  2837
      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
  2838
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2839
    from `bounded_by xs vs` bnds
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2840
    have "bounded_by xs (vs[n := Some (lx, ux)])" by (rule bounded_by_update)
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2841
    with Assign.hyps[OF approx_form]
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2842
    have "interpret_form f xs" by blast }
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2843
  thus ?case using interpret_form.simps x_eq and interpret_floatarith.simps by simp
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2844
next
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2845
  case (Less a b)
31811
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2846
  then obtain l u l' u'
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2847
    where l_eq: "Some (l, u) = approx prec a vs"
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  2848
      and u_eq: "Some (l', u') = approx prec b vs"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2849
      and inequality: "real (float_plus_up prec u (-l')) < 0"
31811
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2850
    by (cases "approx prec a vs", auto,
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2851
      cases "approx prec b vs", auto)
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2852
  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
  2853
    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
  2854
  show ?case by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2855
next
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2856
  case (LessEqual a b)
31811
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2857
  then obtain l u l' u'
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2858
    where l_eq: "Some (l, u) = approx prec a vs"
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  2859
      and u_eq: "Some (l', u') = approx prec b vs"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2860
      and inequality: "real (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
  2861
    by (cases "approx prec a vs", auto,
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2862
      cases "approx prec b vs", auto)
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2863
  from order_trans[OF float_plus_up inequality]
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2864
    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
  2865
  show ?case by auto
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2866
next
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2867
  case (AtLeastAtMost x a b)
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2868
  then obtain lx ux l u l' u'
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2869
    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
  2870
    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
  2871
    and u_eq: "Some (l', u') = approx prec b vs"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2872
    and inequality: "real (float_plus_up prec u (-lx)) \<le> 0" "real (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
  2873
    by (cases "approx prec x vs", auto,
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2874
      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
  2875
      cases "approx prec b vs", auto)
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2876
  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
  2877
    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
  2878
  show ?case by auto
58986
ec7373051a6c disjunction and conjunction for forms
immler
parents: 58985
diff changeset
  2879
qed auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2880
31811
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2881
lemma approx_form:
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2882
  assumes "n = length xs"
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2883
  assumes "approx_form prec f (replicate n None) ss"
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2884
  shows "interpret_form f xs"
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  2885
  using approx_form_aux[OF _ bounded_by_None] assms by auto
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  2886
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  2887
subsection {* Implementing Taylor series expansion *}
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  2888
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  2889
fun isDERIV :: "nat \<Rightarrow> floatarith \<Rightarrow> real list \<Rightarrow> bool" where
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  2890
"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
  2891
"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
  2892
"isDERIV x (Minus a) vs         = isDERIV x a vs" |
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  2893
"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
  2894
"isDERIV x (Cos a) vs           = isDERIV x a vs" |
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  2895
"isDERIV x (Arctan a) vs        = isDERIV x a vs" |
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  2896
"isDERIV x (Min a b) vs         = False" |
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  2897
"isDERIV x (Max a b) vs         = False" |
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  2898
"isDERIV x (Abs a) vs           = False" |
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  2899
"isDERIV x Pi vs                = True" |
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  2900
"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
  2901
"isDERIV x (Exp a) vs           = isDERIV x a vs" |
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  2902
"isDERIV x (Ln a) vs            = (isDERIV x a vs \<and> interpret_floatarith a vs > 0)" |
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  2903
"isDERIV x (Power a 0) vs       = True" |
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  2904
"isDERIV x (Power a (Suc n)) vs = isDERIV x a vs" |
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  2905
"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
  2906
"isDERIV x (Var n) vs          = True"
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  2907
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  2908
fun DERIV_floatarith :: "nat \<Rightarrow> floatarith \<Rightarrow> floatarith" where
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  2909
"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
  2910
"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
  2911
"DERIV_floatarith x (Minus a)         = Minus (DERIV_floatarith x a)" |
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  2912
"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
  2913
"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
  2914
"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
  2915
"DERIV_floatarith x (Min a b)         = Num 0" |
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  2916
"DERIV_floatarith x (Max a b)         = Num 0" |
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  2917
"DERIV_floatarith x (Abs a)           = Num 0" |
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  2918
"DERIV_floatarith x Pi                = Num 0" |
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  2919
"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
  2920
"DERIV_floatarith x (Exp a)           = Mult (Exp a) (DERIV_floatarith x a)" |
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  2921
"DERIV_floatarith x (Ln a)            = Mult (Inverse a) (DERIV_floatarith x a)" |
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  2922
"DERIV_floatarith x (Power a 0)       = Num 0" |
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  2923
"DERIV_floatarith x (Power a (Suc n)) = Mult (Num (Float (int (Suc n)) 0)) (Mult (Power a n) (DERIV_floatarith x a))" |
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  2924
"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
  2925
"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
  2926
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  2927
lemma DERIV_floatarith:
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  2928
  assumes "n < length vs"
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  2929
  assumes isDERIV: "isDERIV n f (vs[n := x])"
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  2930
  shows "DERIV (\<lambda> x'. interpret_floatarith f (vs[n := x'])) x :>
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  2931
               interpret_floatarith (DERIV_floatarith n f) (vs[n := x])"
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  2932
   (is "DERIV (?i f) x :> _")
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  2933
using isDERIV
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  2934
proof (induct f arbitrary: x)
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  2935
  case (Inverse a)
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  2936
  thus ?case
56381
0556204bc230 merged DERIV_intros, has_derivative_intros into derivative_intros
hoelzl
parents: 56195
diff changeset
  2937
    by (auto intro!: derivative_eq_intros simp add: algebra_simps power2_eq_square)
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  2938
next
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  2939
  case (Cos a)
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  2940
  thus ?case
56382
5a50109d51ab fix #0556204bc230
hoelzl
parents: 56381
diff changeset
  2941
    by (auto intro!: derivative_eq_intros
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  2942
           simp del: interpret_floatarith.simps(5)
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  2943
           simp add: interpret_floatarith_sin interpret_floatarith.simps(5)[of a])
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  2944
next
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  2945
  case (Power a n)
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  2946
  thus ?case
56382
5a50109d51ab fix #0556204bc230
hoelzl
parents: 56381
diff changeset
  2947
    by (cases n) (auto intro!: derivative_eq_intros simp del: power_Suc simp add: real_of_nat_def)
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  2948
next
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  2949
  case (Ln a)
56382
5a50109d51ab fix #0556204bc230
hoelzl
parents: 56381
diff changeset
  2950
  thus ?case by (auto intro!: derivative_eq_intros simp add: divide_inverse)
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  2951
next
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  2952
  case (Var i)
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  2953
  thus ?case using `n < length vs` by auto
56381
0556204bc230 merged DERIV_intros, has_derivative_intros into derivative_intros
hoelzl
parents: 56195
diff changeset
  2954
qed (auto intro!: derivative_eq_intros)
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  2955
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  2956
declare approx.simps[simp del]
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  2957
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  2958
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
  2959
"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
  2960
"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
  2961
"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
  2962
"isDERIV_approx prec x (Inverse a) vs       =
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  2963
  (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
  2964
"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
  2965
"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
  2966
"isDERIV_approx prec x (Min a b) vs         = False" |
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  2967
"isDERIV_approx prec x (Max a b) vs         = False" |
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  2968
"isDERIV_approx prec x (Abs a) vs           = False" |
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  2969
"isDERIV_approx prec x Pi vs                = True" |
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  2970
"isDERIV_approx prec x (Sqrt a) vs          =
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  2971
  (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
  2972
"isDERIV_approx prec x (Exp a) vs           = isDERIV_approx prec x a vs" |
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  2973
"isDERIV_approx prec x (Ln a) vs            =
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  2974
  (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
  2975
"isDERIV_approx prec x (Power a 0) vs       = True" |
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  2976
"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
  2977
"isDERIV_approx prec 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
  2978
"isDERIV_approx prec x (Var n) vs          = True"
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  2979
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  2980
lemma isDERIV_approx:
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  2981
  assumes "bounded_by xs vs"
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  2982
    and isDERIV_approx: "isDERIV_approx prec x f vs"
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  2983
  shows "isDERIV x f xs"
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  2984
  using isDERIV_approx
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  2985
proof (induct f)
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  2986
  case (Inverse a)
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  2987
  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
  2988
    and *: "0 < l \<or> u < 0"
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  2989
    by (cases "approx prec a vs") auto
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  2990
  with approx[OF `bounded_by xs vs` approx_Some]
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  2991
  have "interpret_floatarith a xs \<noteq> 0" by auto
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  2992
  thus ?case using Inverse by auto
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  2993
next
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  2994
  case (Ln a)
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  2995
  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
  2996
    and *: "0 < l"
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  2997
    by (cases "approx prec a vs") auto
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  2998
  with approx[OF `bounded_by xs vs` approx_Some]
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  2999
  have "0 < interpret_floatarith a xs" by auto
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3000
  thus ?case using Ln by auto
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3001
next
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3002
  case (Sqrt a)
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3003
  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
  3004
    and *: "0 < l"
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3005
    by (cases "approx prec a vs") auto
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3006
  with approx[OF `bounded_by xs vs` approx_Some]
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  3007
  have "0 < interpret_floatarith a xs" by auto
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3008
  thus ?case using Sqrt by auto
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3009
next
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3010
  case (Power a n) thus ?case by (cases n) auto
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3011
qed auto
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3012
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3013
lemma bounded_by_update_var:
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3014
  assumes "bounded_by xs vs" and "vs ! i = Some (l, u)"
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3015
    and bnd: "x \<in> { real l .. real u }"
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3016
  shows "bounded_by (xs[i := x]) vs"
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3017
proof (cases "i < length xs")
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3018
  case False
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3019
  thus ?thesis using `bounded_by xs vs` by auto
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3020
next
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3021
  let ?xs = "xs[i := x]"
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3022
  case True hence "i < length ?xs" by auto
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3023
  {
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3024
    fix j
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3025
    assume "j < length vs"
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3026
    have "case vs ! j of None \<Rightarrow> True | Some (l, u) \<Rightarrow> ?xs ! j \<in> { real l .. real u }"
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3027
    proof (cases "vs ! j")
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3028
      case (Some b)
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3029
      thus ?thesis
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3030
      proof (cases "i = j")
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3031
        case True
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3032
        thus ?thesis using `vs ! i = Some (l, u)` Some and bnd `i < length ?xs`
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3033
          by auto
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3034
      next
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3035
        case False
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3036
        thus ?thesis
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3037
          using `bounded_by xs vs`[THEN bounded_byE, OF `j < length vs`] Some by auto
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3038
      qed
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3039
    qed auto
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3040
  }
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3041
  thus ?thesis unfolding bounded_by_def by auto
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3042
qed
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3043
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3044
lemma isDERIV_approx':
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3045
  assumes "bounded_by xs vs"
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3046
    and vs_x: "vs ! x = Some (l, u)" and X_in: "X \<in> { real l .. real u }"
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3047
    and approx: "isDERIV_approx prec x f vs"
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3048
  shows "isDERIV x f (xs[x := X])"
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3049
proof -
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3050
  note bounded_by_update_var[OF `bounded_by xs vs` vs_x X_in] approx
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3051
  thus ?thesis by (rule isDERIV_approx)
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3052
qed
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3053
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3054
lemma DERIV_approx:
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3055
  assumes "n < length xs" and bnd: "bounded_by xs vs"
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3056
    and isD: "isDERIV_approx prec n f vs"
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3057
    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
  3058
  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
  3059
             DERIV (\<lambda> x. interpret_floatarith f (xs[n := x])) (xs!n) :> x"
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3060
         (is "\<exists> x. _ \<and> _ \<and> DERIV (?i f) _ :> _")
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3061
proof (rule exI[of _ "?i ?D (xs!n)"], rule conjI[OF _ conjI])
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3062
  let "?i f x" = "interpret_floatarith f (xs[n := x])"
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3063
  from approx[OF bnd app]
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  3064
  show "l \<le> ?i ?D (xs!n)" and "?i ?D (xs!n) \<le> u"
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3065
    using `n < length xs` by auto
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3066
  from DERIV_floatarith[OF `n < length xs`, of f "xs!n"] isDERIV_approx[OF bnd isD]
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3067
  show "DERIV (?i f) (xs!n) :> (?i ?D (xs!n))" by simp
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3068
qed
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3069
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3070
fun lift_bin :: "(float * float) option \<Rightarrow>
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3071
    (float * float) option \<Rightarrow> (float \<Rightarrow> float \<Rightarrow> float \<Rightarrow> float \<Rightarrow> (float * float) option) \<Rightarrow>
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3072
    (float * float) option" where
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3073
  "lift_bin (Some (l1, u1)) (Some (l2, u2)) f = f l1 u1 l2 u2"
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3074
| "lift_bin a b f = None"
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3075
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3076
lemma lift_bin:
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3077
  assumes lift_bin_Some: "Some (l, u) = lift_bin a b f"
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3078
  obtains l1 u1 l2 u2
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3079
  where "a = Some (l1, u1)"
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3080
    and "b = Some (l2, u2)"
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3081
    and "f l1 u1 l2 u2 = Some (l, u)"
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3082
  using assms by (cases a, simp, cases b, simp, auto)
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3083
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3084
fun approx_tse where
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3085
"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
  3086
"approx_tse prec n (Suc s) c k f bs =
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3087
  (if isDERIV_approx prec n f bs then
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3088
    lift_bin (approx prec f (bs[n := Some (c,c)]))
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3089
             (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
  3090
             (\<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
  3091
                 (Add (Var 0)
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3092
                      (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
  3093
                                 (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
  3094
                                       (Var (Suc 0))))) [Some (l1, u1), Some (l2, u2), bs!n])
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3095
  else approx prec f bs)"
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3096
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3097
lemma bounded_by_Cons:
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3098
  assumes bnd: "bounded_by xs vs"
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3099
    and x: "x \<in> { real l .. real u }"
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3100
  shows "bounded_by (x#xs) ((Some (l, u))#vs)"
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3101
proof -
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3102
  {
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3103
    fix i assume *: "i < length ((Some (l, u))#vs)"
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3104
    have "case ((Some (l,u))#vs) ! i of Some (l, u) \<Rightarrow> (x#xs)!i \<in> { real l .. real u } | None \<Rightarrow> True"
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3105
    proof (cases i)
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3106
      case 0 with x show ?thesis by auto
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3107
    next
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3108
      case (Suc i) with * have "i < length vs" by auto
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3109
      from bnd[THEN bounded_byE, OF this]
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3110
      show ?thesis unfolding Suc nth_Cons_Suc .
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3111
    qed
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3112
  }
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3113
  thus ?thesis by (auto simp add: bounded_by_def)
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3114
qed
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3115
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3116
lemma approx_tse_generic:
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3117
  assumes "bounded_by xs vs"
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3118
    and bnd_c: "bounded_by (xs[x := c]) vs" and "x < length vs" and "x < length xs"
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3119
    and bnd_x: "vs ! x = Some (lx, ux)"
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3120
    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
  3121
  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
  3122
      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
  3123
            (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
  3124
   \<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
  3125
                  interpret_floatarith ((DERIV_floatarith x ^^ i) f) (xs[x := c]) *
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  3126
                  (xs!x - c)^i) +
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3127
      inverse (real (\<Prod> j \<in> {k..<k+n}. j)) *
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3128
      interpret_floatarith ((DERIV_floatarith x ^^ n) f) (xs[x := t]) *
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  3129
      (xs!x - c)^n \<in> {l .. u})" (is "\<exists> n. ?taylor f k l u n")
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3130
using ate proof (induct s arbitrary: k f l u)
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3131
  case 0
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3132
  {
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3133
    fix t::real assume "t \<in> {lx .. ux}"
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3134
    note bounded_by_update_var[OF `bounded_by xs vs` bnd_x this]
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3135
    from approx[OF this 0[unfolded approx_tse.simps]]
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  3136
    have "(interpret_floatarith f (xs[x := t])) \<in> {l .. u}"
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3137
      by (auto simp add: algebra_simps)
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3138
  }
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3139
  thus ?case by (auto intro!: exI[of _ 0])
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3140
next
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3141
  case (Suc s)
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3142
  show ?case
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3143
  proof (cases "isDERIV_approx prec x f vs")
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3144
    case False
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3145
    note ap = Suc.prems[unfolded approx_tse.simps if_not_P[OF False]]
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3146
    {
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3147
      fix t::real assume "t \<in> {lx .. ux}"
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3148
      note bounded_by_update_var[OF `bounded_by xs vs` bnd_x this]
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3149
      from approx[OF this ap]
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  3150
      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
  3151
        by (auto simp add: algebra_simps)
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3152
    }
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3153
    thus ?thesis by (auto intro!: exI[of _ 0])
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3154
  next
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3155
    case True
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3156
    with Suc.prems
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3157
    obtain l1 u1 l2 u2
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3158
      where a: "Some (l1, u1) = approx prec f (vs[x := Some (c,c)])"
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3159
        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
  3160
        and final: "Some (l, u) = approx prec
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3161
          (Add (Var 0)
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3162
               (Mult (Inverse (Num (Float (int k) 0)))
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3163
                     (Mult (Add (Var (Suc (Suc 0))) (Minus (Num c)))
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3164
                           (Var (Suc 0))))) [Some (l1, u1), Some (l2, u2), vs!x]"
56073
29e308b56d23 enhanced simplifier solver for preconditions of rewrite rule, can now deal with conjunctions
nipkow
parents: 55506
diff changeset
  3165
      by (auto elim!: lift_bin)
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3166
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3167
    from bnd_c `x < length xs`
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  3168
    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
  3169
      by (auto intro!: bounded_by_update)
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3170
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3171
    from approx[OF this a]
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  3172
    have f_c: "interpret_floatarith ((DERIV_floatarith x ^^ 0) f) (xs[x := c]) \<in> { l1 .. u1 }"
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3173
              (is "?f 0 (real c) \<in> _")
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3174
      by auto
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3175
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3176
    {
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3177
      fix f :: "'a \<Rightarrow> 'a" fix n :: nat fix x :: 'a
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3178
      have "(f ^^ Suc n) x = (f ^^ n) (f x)"
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3179
        by (induct n) auto
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3180
    }
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3181
    note funpow_Suc = this[symmetric]
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3182
    from Suc.hyps[OF ate, unfolded this]
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3183
    obtain n
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  3184
      where DERIV_hyp: "\<And> m z. \<lbrakk> m < n ; (z::real) \<in> { lx .. ux } \<rbrakk> \<Longrightarrow> DERIV (?f (Suc m)) z :> ?f (Suc (Suc m)) z"
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  3185
      and hyp: "\<forall> t \<in> {real lx .. real ux}. (\<Sum> i = 0..<n. inverse (real (\<Prod> j \<in> {Suc k..<Suc k + i}. j)) * ?f (Suc i) c * (xs!x - c)^i) +
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  3186
           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
  3187
          (is "\<forall> t \<in> _. ?X (Suc k) f n t \<in> _")
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3188
      by blast
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3189
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3190
    {
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3191
      fix m and z::real
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  3192
      assume "m < Suc n" and bnd_z: "z \<in> { lx .. ux }"
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3193
      have "DERIV (?f m) z :> ?f (Suc m) z"
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3194
      proof (cases m)
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  3195
        case 0
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  3196
        with DERIV_floatarith[OF `x < length xs` isDERIV_approx'[OF `bounded_by xs vs` bnd_x bnd_z True]]
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  3197
        show ?thesis by simp
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3198
      next
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  3199
        case (Suc m')
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  3200
        hence "m' < n" using `m < Suc n` by auto
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  3201
        from DERIV_hyp[OF this bnd_z]
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  3202
        show ?thesis using Suc by simp
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3203
      qed
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3204
    } note DERIV = this
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3205
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3206
    have "\<And> k i. k < i \<Longrightarrow> {k ..< i} = insert k {Suc k ..< i}" by auto
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3207
    hence setprod_head_Suc: "\<And> k i. \<Prod> {k ..< k + Suc i} = k * \<Prod> {Suc k ..< Suc k + i}" by auto
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3208
    have setsum_move0: "\<And> k F. setsum F {0..<Suc k} = F 0 + setsum (\<lambda> k. F (Suc k)) {0..<k}"
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3209
      unfolding setsum_shift_bounds_Suc_ivl[symmetric]
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3210
      unfolding setsum_head_upt_Suc[OF zero_less_Suc] ..
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  3211
    def C \<equiv> "xs!x - c"
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  3212
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3213
    {
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3214
      fix t::real assume t: "t \<in> {lx .. ux}"
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3215
      hence "bounded_by [xs!x] [vs!x]"
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  3216
        using `bounded_by xs vs`[THEN bounded_byE, OF `x < length vs`]
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  3217
        by (cases "vs!x", auto simp add: bounded_by_def)
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3218
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3219
      with hyp[THEN bspec, OF t] f_c
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  3220
      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
  3221
        by (auto intro!: bounded_by_Cons)
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3222
      from approx[OF this final, unfolded atLeastAtMost_iff[symmetric]]
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  3223
      have "?X (Suc k) f n t * (xs!x - real 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
  3224
        by (auto simp add: algebra_simps)
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  3225
      also have "?X (Suc k) f n t * (xs!x - real c) * inverse (real k) + ?f 0 c =
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  3226
               (\<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
  3227
               inverse (real (\<Prod> j \<in> {k..<k+Suc n}. j)) * ?f (Suc n) t * (xs!x - c)^Suc n" (is "_ = ?T")
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  3228
        unfolding funpow_Suc C_def[symmetric] setsum_move0 setprod_head_Suc
35082
96a21dd3b349 rely less on ordered rewriting
haftmann
parents: 35028
diff changeset
  3229
        by (auto simp add: algebra_simps)
57512
cc97b347b301 reduced name variants for assoc and commute on plus and mult
haftmann
parents: 57418
diff changeset
  3230
          (simp only: mult.left_commute [of _ "inverse (real k)"] setsum_right_distrib [symmetric])
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3231
      finally have "?T \<in> {l .. u}" .
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3232
    }
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3233
    thus ?thesis using DERIV by blast
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3234
  qed
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3235
qed
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3236
59730
b7c394c7a619 The factorial function, "fact", now has type "nat => 'a"
paulson <lp15@cam.ac.uk>
parents: 59658
diff changeset
  3237
lemma setprod_fact: "real (\<Prod> {1..<1 + k}) = fact (k :: nat)"
59751
916c0f6c83e3 New material for complex sin, cos, tan, Ln, also some reorganisation
paulson <lp15@cam.ac.uk>
parents: 59741
diff changeset
  3238
  using fact_altdef_nat Suc_eq_plus1_left atLeastLessThanSuc_atLeastAtMost real_fact_nat
59730
b7c394c7a619 The factorial function, "fact", now has type "nat => 'a"
paulson <lp15@cam.ac.uk>
parents: 59658
diff changeset
  3239
  by presburger
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3240
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3241
lemma approx_tse:
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3242
  assumes "bounded_by xs vs"
49351
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3243
    and bnd_x: "vs ! x = Some (lx, ux)" and bnd_c: "real c \<in> {lx .. ux}"
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3244
    and "x < length vs" and "x < length xs"
0dd3449640b4 tuned proofs;
wenzelm
parents: 47621
diff changeset
  3245
    and ate: "Some (l, u) = approx_tse prec x s c 1 f vs"
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  3246
  shows "interpret_floatarith f xs \<in> { l .. u }"
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3247
proof -
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3248
  def F \<equiv> "\<lambda> n z. interpret_floatarith ((DERIV_floatarith x ^^ n) f) (xs[x := z])"
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3249
  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
  3250
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  3251
  hence "bounded_by (xs[x := c]) vs" and "x < length vs" "x < length xs"
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3252
    using `bounded_by xs vs` bnd_x bnd_c `x < length vs` `x < length xs`
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3253
    by (auto intro!: bounded_by_update_var)
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3254
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3255
  from approx_tse_generic[OF `bounded_by xs vs` this bnd_x ate]
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3256
  obtain n
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3257
    where DERIV: "\<forall> m z. m < n \<and> real lx \<le> z \<and> z \<le> real ux \<longrightarrow> DERIV (F m) z :> F (Suc m) z"
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  3258
    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
  3259
           (\<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
  3260
             inverse ((fact n)) * F n t * (xs!x - c)^n
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  3261
             \<in> {l .. u}" (is "\<And> t. _ \<Longrightarrow> ?taylor t \<in> _")
59730
b7c394c7a619 The factorial function, "fact", now has type "nat => 'a"
paulson <lp15@cam.ac.uk>
parents: 59658
diff changeset
  3262
    unfolding F_def atLeastAtMost_iff[symmetric] setprod_fact
b7c394c7a619 The factorial function, "fact", now has type "nat => 'a"
paulson <lp15@cam.ac.uk>
parents: 59658
diff changeset
  3263
    by blast
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3264
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  3265
  have bnd_xs: "xs ! x \<in> { lx .. ux }"
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3266
    using `bounded_by xs vs`[THEN bounded_byE, OF `x < length vs`] bnd_x by auto
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3267
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3268
  show ?thesis
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3269
  proof (cases n)
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3270
    case 0 thus ?thesis using hyp[OF bnd_xs] unfolding F_def by auto
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3271
  next
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3272
    case (Suc n')
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3273
    show ?thesis
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  3274
    proof (cases "xs ! x = c")
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3275
      case True
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3276
      from True[symmetric] hyp[OF bnd_xs] Suc show ?thesis
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  3277
        unfolding F_def Suc setsum_head_upt_Suc[OF zero_less_Suc] setsum_shift_bounds_Suc_ivl by auto
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3278
    next
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3279
      case False
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3280
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  3281
      have "lx \<le> real c" "real c \<le> ux" "lx \<le> xs!x" "xs!x \<le> ux"
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32920
diff changeset
  3282
        using Suc bnd_c `bounded_by xs vs`[THEN bounded_byE, OF `x < length vs`] bnd_x by auto
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3283
      from Taylor.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
  3284
      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
  3285
        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
  3286
           (\<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
  3287
           F (Suc n') t / (fact (Suc n')) * (xs ! x - c) ^ Suc n'"
56195
c7dfd924a165 adapt to Isabelle/c726ecfb22b6
huffman
parents: 56073
diff changeset
  3288
        unfolding atLeast0LessThan by blast
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3289
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  3290
      from t_bnd bnd_xs bnd_c have *: "t \<in> {lx .. ux}"
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  3291
        by (cases "xs ! x < c", auto)
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3292
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3293
      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
  3294
        unfolding fl_eq Suc by (auto simp add: algebra_simps divide_inverse)
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  3295
      also have "\<dots> \<in> {l .. u}" using * by (rule hyp)
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3296
      finally show ?thesis by simp
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3297
    qed
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3298
  qed
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3299
qed
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3300
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3301
fun approx_tse_form' where
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3302
"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
  3303
  (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
  3304
     of Some (l, u) \<Rightarrow> cmp l u | None \<Rightarrow> False)" |
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3305
"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
  3306
  (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
  3307
   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
  3308
      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
  3309
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3310
lemma approx_tse_form':
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  3311
  fixes x :: real
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  3312
  assumes "approx_tse_form' prec t f s l u cmp" and "x \<in> {l .. u}"
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  3313
  shows "\<exists> l' u' ly uy. x \<in> { l' .. u' } \<and> real l \<le> l' \<and> u' \<le> real u \<and> cmp ly uy \<and>
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
  3314
                  approx_tse prec 0 t ((l' + u') * Float 1 (- 1)) 1 f [Some (l', u')] = Some (ly, uy)"
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3315
using assms proof (induct s arbitrary: l u)
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3316
  case 0
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3317
  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
  3318
    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
  3319
    and **: "cmp ly uy" by (auto elim!: case_optionE)
46545
haftmann
parents: 45481
diff changeset
  3320
  with 0 show ?case by auto
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3321
next
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3322
  case (Suc s)
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
  3323
  let ?m = "(l + u) * Float 1 (- 1)"
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3324
  from Suc.prems
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3325
  have l: "approx_tse_form' prec t f s l ?m cmp"
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3326
    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
  3327
    by (auto simp add: Let_def lazy_conj)
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3328
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  3329
  have m_l: "real l \<le> ?m" and m_u: "?m \<le> real u"
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47108
diff changeset
  3330
    unfolding less_eq_float_def using Suc.prems by auto
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3331
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  3332
  with `x \<in> { l .. u }`
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  3333
  have "x \<in> { l .. ?m} \<or> x \<in> { ?m .. u }" by auto
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3334
  thus ?case
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3335
  proof (rule disjE)
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  3336
    assume "x \<in> { l .. ?m}"
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3337
    from Suc.hyps[OF l this]
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3338
    obtain l' u' ly uy
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  3339
      where "x \<in> { l' .. u' } \<and> real l \<le> l' \<and> real u' \<le> ?m \<and> cmp ly uy \<and>
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
  3340
                  approx_tse prec 0 t ((l' + u') * Float 1 (- 1)) 1 f [Some (l', u')] = Some (ly, uy)" by blast
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3341
    with m_u show ?thesis by (auto intro!: exI)
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3342
  next
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  3343
    assume "x \<in> { ?m .. u }"
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3344
    from Suc.hyps[OF u this]
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3345
    obtain l' u' ly uy
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  3346
      where "x \<in> { l' .. u' } \<and> ?m \<le> real l' \<and> u' \<le> real u \<and> cmp ly uy \<and>
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58310
diff changeset
  3347
                  approx_tse prec 0 t ((l' + u') * Float 1 (- 1)) 1 f [Some (l', u')] = Some (ly, uy)" by blast
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3348
    with m_u show ?thesis by (auto intro!: exI)
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3349
  qed
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3350
qed
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3351
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3352
lemma approx_tse_form'_less:
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  3353
  fixes x :: real
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3354
  assumes tse: "approx_tse_form' prec t (Add a (Minus b)) s l u (\<lambda> l u. 0 < l)"
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  3355
  and x: "x \<in> {l .. u}"
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3356
  shows "interpret_floatarith b [x] < interpret_floatarith a [x]"
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3357
proof -
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3358
  from approx_tse_form'[OF tse x]
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3359
  obtain l' u' ly uy
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  3360
    where x': "x \<in> { l' .. u' }" and "l \<le> real l'"
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  3361
    and "real u' \<le> 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
  3362
    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
  3363
    by blast
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3364
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3365
  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
  3366
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3367
  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
  3368
  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
  3369
    by auto
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  3370
  from order_less_le_trans[OF _ this, of 0] `0 < ly`
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3371
  show ?thesis by auto
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3372
qed
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3373
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3374
lemma approx_tse_form'_le:
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  3375
  fixes x :: real
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3376
  assumes tse: "approx_tse_form' prec t (Add a (Minus b)) s l u (\<lambda> l u. 0 \<le> l)"
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  3377
  and x: "x \<in> {l .. u}"
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3378
  shows "interpret_floatarith b [x] \<le> interpret_floatarith a [x]"
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3379
proof -
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3380
  from approx_tse_form'[OF tse x]
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3381
  obtain l' u' ly uy
40881
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  3382
    where x': "x \<in> { l' .. u' }" and "l \<le> real l'"
e84f82418e09 Use coercions in Approximation (by Dmitriy Traytel).
hoelzl
parents: 39556
diff changeset
  3383
    and "real 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
  3384
    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
  3385
    by blast
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3386
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3387
  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
  3388
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3389
  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
  3390
  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
  3391
    by auto
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  3392
  from order_trans[OF _ this, of 0] `0 \<le> ly`
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3393
  show ?thesis by auto
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3394
qed
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3395
58986
ec7373051a6c disjunction and conjunction for forms
immler
parents: 58985
diff changeset
  3396
fun approx_tse_concl where
ec7373051a6c disjunction and conjunction for forms
immler
parents: 58985
diff changeset
  3397
"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
  3398
    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
  3399
"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
  3400
    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
  3401
"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
  3402
    (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
  3403
      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
  3404
"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
  3405
    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
  3406
"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
  3407
    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
  3408
"approx_tse_concl _ _ _ _ _ _ _ _ \<longleftrightarrow> False"
ec7373051a6c disjunction and conjunction for forms
immler
parents: 58985
diff changeset
  3409
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3410
definition
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3411
"approx_tse_form prec t s f =
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3412
  (case 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
  3413
   of (Bound x a b f) \<Rightarrow> x = Var 0 \<and>
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3414
     (case (approx prec a [None], approx prec b [None])
58986
ec7373051a6c disjunction and conjunction for forms
immler
parents: 58985
diff changeset
  3415
      of (Some (l, u), Some (l', u')) \<Rightarrow> approx_tse_concl prec t f s l u l' u'
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3416
       | _ \<Rightarrow> False)
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3417
   | _ \<Rightarrow> False)"
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3418
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3419
lemma approx_tse_form:
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3420
  assumes "approx_tse_form prec t s f"
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3421
  shows "interpret_form f [x]"
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3422
proof (cases f)
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3423
  case (Bound i a b f') note f_def = this
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3424
  with assms obtain l u l' u'
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3425
    where a: "approx prec a [None] = Some (l, u)"
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3426
    and b: "approx prec b [None] = Some (l', u')"
55413
a8e96847523c adapted theories to '{case,rec}_{list,option}' names
blanchet
parents: 54782
diff changeset
  3427
    unfolding approx_tse_form_def by (auto elim!: case_optionE)
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3428
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
  3429
  from Bound assms have "i = Var 0" unfolding approx_tse_form_def by auto
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3430
  hence i: "interpret_floatarith i [x] = x" by auto
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3431
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3432
  { let "?f z" = "interpret_floatarith z [x]"
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3433
    assume "?f i \<in> { ?f a .. ?f b }"
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3434
    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
  3435
    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
  3436
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3437
    have "interpret_form f' [x]"
58986
ec7373051a6c disjunction and conjunction for forms
immler
parents: 58985
diff changeset
  3438
      using assms[unfolded Bound]
ec7373051a6c disjunction and conjunction for forms
immler
parents: 58985
diff changeset
  3439
    proof (induct f')
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3440
      case (Less lf rt)
58986
ec7373051a6c disjunction and conjunction for forms
immler
parents: 58985
diff changeset
  3441
      with a b
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3442
      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
  3443
        unfolding approx_tse_form_def by auto
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3444
      from approx_tse_form'_less[OF this bnd]
58986
ec7373051a6c disjunction and conjunction for forms
immler
parents: 58985
diff changeset
  3445
      show ?case using Less by auto
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3446
    next
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3447
      case (LessEqual lf rt)
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3448
      with Bound a b assms
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3449
      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
  3450
        unfolding approx_tse_form_def by auto
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3451
      from approx_tse_form'_le[OF this bnd]
58986
ec7373051a6c disjunction and conjunction for forms
immler
parents: 58985
diff changeset
  3452
      show ?case using LessEqual by auto
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3453
    next
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3454
      case (AtLeastAtMost x lf rt)
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3455
      with Bound a b assms
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3456
      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
  3457
        and "approx_tse_form' prec t (Add x (Minus lf)) s l u' (\<lambda> l u. 0 \<le> l)"
58986
ec7373051a6c disjunction and conjunction for forms
immler
parents: 58985
diff changeset
  3458
        unfolding approx_tse_form_def lazy_conj by (auto split: split_if_asm)
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3459
      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
  3460
      show ?case using AtLeastAtMost by auto
ec7373051a6c disjunction and conjunction for forms
immler
parents: 58985
diff changeset
  3461
    qed (auto simp: f_def approx_tse_form_def elim!: case_optionE)
ec7373051a6c disjunction and conjunction for forms
immler
parents: 58985
diff changeset
  3462
  } thus ?thesis unfolding f_def by auto
ec7373051a6c disjunction and conjunction for forms
immler
parents: 58985
diff changeset
  3463
qed (insert assms, auto simp add: approx_tse_form_def)
31863
e391eee8bf14 Implemented taylor series expansion for approximation
hoelzl
parents: 31811
diff changeset
  3464
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
  3465
text {* @{term approx_form_eval} is only used for the {\tt value}-command. *}
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
  3466
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
  3467
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
  3468
"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
  3469
   (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
  3470
   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
  3471
    | _ \<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
  3472
"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
  3473
   (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
  3474
   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
  3475
    | _ \<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
  3476
"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
  3477
"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
  3478
"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
  3479
   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
  3480
"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
  3481
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3482
subsection {* Implement proof method \texttt{approximation} *}
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3483
31811
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3484
lemmas interpret_form_equations = interpret_form.simps interpret_floatarith.simps interpret_floatarith_num
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
  3485
  interpret_floatarith_divide interpret_floatarith_diff interpret_floatarith_tan interpret_floatarith_log
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
  3486
  interpret_floatarith_sin
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3487
36985
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  3488
oracle approximation_oracle = {* fn (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
  3489
let
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  3490
  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
  3491
38716
3c3b4ad683d5 approximation_oracle: actually match true/false in ML, not arbitrary values;
wenzelm
parents: 38558
diff changeset
  3492
  fun term_of_bool true = @{term True}
3c3b4ad683d5 approximation_oracle: actually match true/false in ML, not arbitrary values;
wenzelm
parents: 38558
diff changeset
  3493
    | 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
  3494
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
  3495
  val mk_int = HOLogic.mk_number @{typ int} o @{code integer_of_int};
58988
6ebf918128b9 added quickcheck[approximation]
immler
parents: 58986
diff changeset
  3496
  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
  3497
    | 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
  3498
36985
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  3499
  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
  3500
    @{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
  3501
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  3502
  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
  3503
    | 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
  3504
        $ 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
  3505
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  3506
  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
  3507
    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
  3508
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49962
diff changeset
  3509
  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
  3510
    (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
  3511
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  3512
  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
  3513
        @{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
  3514
    | 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
  3515
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  3516
  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
  3517
    | 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
  3518
    | 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
  3519
    | 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
  3520
    | 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
  3521
    | 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
  3522
    | 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
  3523
    | 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
  3524
    | 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
  3525
    | 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
  3526
    | 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
  3527
    | floatarith_of_term (@{term Exp} $ a) = @{code Exp} (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
  3528
    | 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
  3529
    | 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
  3530
        @{code Power} (floatarith_of_term a, 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
  3531
    | 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
  3532
    | 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
  3533
    | 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
  3534
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  3535
  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
  3536
        (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
  3537
    | 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
  3538
        (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
  3539
    | 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
  3540
        (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
  3541
    | 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
  3542
        (floatarith_of_term a, floatarith_of_term b)
58986
ec7373051a6c disjunction and conjunction for forms
immler
parents: 58985
diff changeset
  3543
    | form_of_term (@{term Conj} $ a $ b) = @{code Conj}
ec7373051a6c disjunction and conjunction for forms
immler
parents: 58985
diff changeset
  3544
        (form_of_term a, form_of_term b)
ec7373051a6c disjunction and conjunction for forms
immler
parents: 58985
diff changeset
  3545
    | form_of_term (@{term Disj} $ a $ b) = @{code Disj}
ec7373051a6c disjunction and conjunction for forms
immler
parents: 58985
diff changeset
  3546
        (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
  3547
    | 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
  3548
        (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
  3549
    | 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
  3550
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  3551
  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
  3552
    | 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
  3553
        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
  3554
    | 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
  3555
        (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
  3556
    | 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
  3557
  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
  3558
        (@{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
  3559
          @{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
  3560
    | 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
  3561
        @{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
  3562
    | 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
  3563
        (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
  3564
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  3565
  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
  3566
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  3567
  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
  3568
        (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
  3569
    | 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
  3570
        @{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
  3571
    | 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
  3572
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  3573
  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
  3574
   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
  3575
        (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
  3576
    | @{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
  3577
        (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
  3578
    | @{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
  3579
        (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
  3580
    | _ => 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
  3581
52131
366fa32ee2a3 tuned signature;
wenzelm
parents: 52090
diff changeset
  3582
  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
  3583
59621
291934bac95e Thm.cterm_of and Thm.ctyp_of operate on local context;
wenzelm
parents: 59582
diff changeset
  3584
in Thm.global_cterm_of thy (Logic.mk_equals (t, normalize t)) end
36985
41c5d4002f60 spelt out normalizer explicitly -- avoid dynamic reference to code generator configuration; avoid using old Codegen.eval_term
haftmann
parents: 36960
diff changeset
  3585
*}
31099
03314c427b34 optimized Approximation by precompiling approx_inequality
hoelzl
parents: 31098
diff changeset
  3586
31811
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3587
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
  3588
  by auto
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3589
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3590
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
  3591
  by auto
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3592
59850
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59751
diff changeset
  3593
ML_file "approximation.ML"
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59751
diff changeset
  3594
30549
d2d7874648bd simplified method setup;
wenzelm
parents: 30510
diff changeset
  3595
method_setup approximation = {*
59850
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59751
diff changeset
  3596
  let val free = Args.context -- Args.term >> (fn (_, Free (n, _)) => n | (ctxt, t) =>
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59751
diff changeset
  3597
                   error ("Bad free variable: " ^ Syntax.string_of_term ctxt t));
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59751
diff changeset
  3598
  in
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59751
diff changeset
  3599
    Scan.lift Parse.nat
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59751
diff changeset
  3600
    --
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59751
diff changeset
  3601
    Scan.optional (Scan.lift (Args.$$$ "splitting" |-- Args.colon)
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59751
diff changeset
  3602
      |-- Parse.and_list' (free --| Scan.lift (Args.$$$ "=") -- Scan.lift Parse.nat)) []
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59751
diff changeset
  3603
    --
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59751
diff changeset
  3604
    Scan.option (Scan.lift (Args.$$$ "taylor" |-- Args.colon)
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59751
diff changeset
  3605
      |-- (free |-- Scan.lift (Args.$$$ "=") |-- Scan.lift Parse.nat))
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59751
diff changeset
  3606
    >>
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59751
diff changeset
  3607
    (fn ((prec, splitting), taylor) => fn ctxt =>
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59751
diff changeset
  3608
      SIMPLE_METHOD' (Approximation.approximation_tac prec splitting taylor ctxt))
f339ff48a6ee exposed approximation in ML
eberlm
parents: 59751
diff changeset
  3609
  end
55506
46f3e31c5a87 removed dead code;
wenzelm
parents: 55466
diff changeset
  3610
*} "real number approximation"
31811
64dea9a15031 Improved computation of bounds and implemented interval splitting for 'approximation'.
hoelzl
parents: 31810
diff changeset
  3611
58988
6ebf918128b9 added quickcheck[approximation]
immler
parents: 58986
diff changeset
  3612
6ebf918128b9 added quickcheck[approximation]
immler
parents: 58986
diff changeset
  3613
section "Quickcheck Generator"
6ebf918128b9 added quickcheck[approximation]
immler
parents: 58986
diff changeset
  3614
6ebf918128b9 added quickcheck[approximation]
immler
parents: 58986
diff changeset
  3615
ML_file "approximation_generator.ML"
6ebf918128b9 added quickcheck[approximation]
immler
parents: 58986
diff changeset
  3616
6ebf918128b9 added quickcheck[approximation]
immler
parents: 58986
diff changeset
  3617
setup "Approximation_Generator.setup"
6ebf918128b9 added quickcheck[approximation]
immler
parents: 58986
diff changeset
  3618
29805
a5da150bd0ab Add approximation method
hoelzl
parents:
diff changeset
  3619
end