1(*  Title:      HOL/Transcendental.thy
2    Author:     Jacques D. Fleuriot, University of Cambridge, University of Edinburgh
3    Author:     Lawrence C Paulson
4    Author:     Jeremy Avigad
5*)
6
7section \<open>Power Series, Transcendental Functions etc.\<close>
8
9theory Transcendental
10imports Series Deriv NthRoot
11begin
12
13text \<open>A theorem about the factcorial function on the reals.\<close>
14
15lemma square_fact_le_2_fact: "fact n * fact n \<le> (fact (2 * n) :: real)"
16proof (induct n)
17  case 0
18  then show ?case by simp
19next
20  case (Suc n)
21  have "(fact (Suc n)) * (fact (Suc n)) = of_nat (Suc n) * of_nat (Suc n) * (fact n * fact n :: real)"
22    by (simp add: field_simps)
23  also have "\<dots> \<le> of_nat (Suc n) * of_nat (Suc n) * fact (2 * n)"
24    by (rule mult_left_mono [OF Suc]) simp
25  also have "\<dots> \<le> of_nat (Suc (Suc (2 * n))) * of_nat (Suc (2 * n)) * fact (2 * n)"
26    by (rule mult_right_mono)+ (auto simp: field_simps)
27  also have "\<dots> = fact (2 * Suc n)" by (simp add: field_simps)
28  finally show ?case .
29qed
30
31lemma fact_in_Reals: "fact n \<in> \<real>"
32  by (induction n) auto
33
34lemma of_real_fact [simp]: "of_real (fact n) = fact n"
35  by (metis of_nat_fact of_real_of_nat_eq)
36
37lemma pochhammer_of_real: "pochhammer (of_real x) n = of_real (pochhammer x n)"
38  by (simp add: pochhammer_prod)
39
40lemma norm_fact [simp]: "norm (fact n :: 'a::real_normed_algebra_1) = fact n"
41proof -
42  have "(fact n :: 'a) = of_real (fact n)"
43    by simp
44  also have "norm \<dots> = fact n"
45    by (subst norm_of_real) simp
46  finally show ?thesis .
47qed
48
49lemma root_test_convergence:
50  fixes f :: "nat \<Rightarrow> 'a::banach"
51  assumes f: "(\<lambda>n. root n (norm (f n))) \<longlonglongrightarrow> x" \<comment> \<open>could be weakened to lim sup\<close>
52    and "x < 1"
53  shows "summable f"
54proof -
55  have "0 \<le> x"
56    by (rule LIMSEQ_le[OF tendsto_const f]) (auto intro!: exI[of _ 1])
57  from \<open>x < 1\<close> obtain z where z: "x < z" "z < 1"
58    by (metis dense)
59  from f \<open>x < z\<close> have "eventually (\<lambda>n. root n (norm (f n)) < z) sequentially"
60    by (rule order_tendstoD)
61  then have "eventually (\<lambda>n. norm (f n) \<le> z^n) sequentially"
62    using eventually_ge_at_top
63  proof eventually_elim
64    fix n
65    assume less: "root n (norm (f n)) < z" and n: "1 \<le> n"
66    from power_strict_mono[OF less, of n] n show "norm (f n) \<le> z ^ n"
67      by simp
68  qed
69  then show "summable f"
70    unfolding eventually_sequentially
71    using z \<open>0 \<le> x\<close> by (auto intro!: summable_comparison_test[OF _  summable_geometric])
72qed
73
74subsection \<open>More facts about binomial coefficients\<close>
75
76text \<open>
77  These facts could have been proven before, but having real numbers
78  makes the proofs a lot easier.
79\<close>
80
81lemma central_binomial_odd:
82  "odd n \<Longrightarrow> n choose (Suc (n div 2)) = n choose (n div 2)"
83proof -
84  assume "odd n"
85  hence "Suc (n div 2) \<le> n" by presburger
86  hence "n choose (Suc (n div 2)) = n choose (n - Suc (n div 2))"
87    by (rule binomial_symmetric)
88  also from \<open>odd n\<close> have "n - Suc (n div 2) = n div 2" by presburger
89  finally show ?thesis .
90qed
91
92lemma binomial_less_binomial_Suc:
93  assumes k: "k < n div 2"
94  shows   "n choose k < n choose (Suc k)"
95proof -
96  from k have k': "k \<le> n" "Suc k \<le> n" by simp_all
97  from k' have "real (n choose k) = fact n / (fact k * fact (n - k))"
98    by (simp add: binomial_fact)
99  also from k' have "n - k = Suc (n - Suc k)" by simp
100  also from k' have "fact \<dots> = (real n - real k) * fact (n - Suc k)"
101    by (subst fact_Suc) (simp_all add: of_nat_diff)
102  also from k have "fact k = fact (Suc k) / (real k + 1)" by (simp add: field_simps)
103  also have "fact n / (fact (Suc k) / (real k + 1) * ((real n - real k) * fact (n - Suc k))) =
104               (n choose (Suc k)) * ((real k + 1) / (real n - real k))"
105    using k by (simp add: divide_simps binomial_fact)
106  also from assms have "(real k + 1) / (real n - real k) < 1" by simp
107  finally show ?thesis using k by (simp add: mult_less_cancel_left)
108qed
109
110lemma binomial_strict_mono:
111  assumes "k < k'" "2*k' \<le> n"
112  shows   "n choose k < n choose k'"
113proof -
114  from assms have "k \<le> k' - 1" by simp
115  thus ?thesis
116  proof (induction rule: inc_induct)
117    case base
118    with assms binomial_less_binomial_Suc[of "k' - 1" n]
119      show ?case by simp
120  next
121    case (step k)
122    from step.prems step.hyps assms have "n choose k < n choose (Suc k)"
123      by (intro binomial_less_binomial_Suc) simp_all
124    also have "\<dots> < n choose k'" by (rule step.IH)
125    finally show ?case .
126  qed
127qed
128
129lemma binomial_mono:
130  assumes "k \<le> k'" "2*k' \<le> n"
131  shows   "n choose k \<le> n choose k'"
132  using assms binomial_strict_mono[of k k' n] by (cases "k = k'") simp_all
133
134lemma binomial_strict_antimono:
135  assumes "k < k'" "2 * k \<ge> n" "k' \<le> n"
136  shows   "n choose k > n choose k'"
137proof -
138  from assms have "n choose (n - k) > n choose (n - k')"
139    by (intro binomial_strict_mono) (simp_all add: algebra_simps)
140  with assms show ?thesis by (simp add: binomial_symmetric [symmetric])
141qed
142
143lemma binomial_antimono:
144  assumes "k \<le> k'" "k \<ge> n div 2" "k' \<le> n"
145  shows   "n choose k \<ge> n choose k'"
146proof (cases "k = k'")
147  case False
148  note not_eq = False
149  show ?thesis
150  proof (cases "k = n div 2 \<and> odd n")
151    case False
152    with assms(2) have "2*k \<ge> n" by presburger
153    with not_eq assms binomial_strict_antimono[of k k' n]
154      show ?thesis by simp
155  next
156    case True
157    have "n choose k' \<le> n choose (Suc (n div 2))"
158    proof (cases "k' = Suc (n div 2)")
159      case False
160      with assms True not_eq have "Suc (n div 2) < k'" by simp
161      with assms binomial_strict_antimono[of "Suc (n div 2)" k' n] True
162        show ?thesis by auto
163    qed simp_all
164    also from True have "\<dots> = n choose k" by (simp add: central_binomial_odd)
165    finally show ?thesis .
166  qed
167qed simp_all
168
169lemma binomial_maximum: "n choose k \<le> n choose (n div 2)"
170proof -
171  have "k \<le> n div 2 \<longleftrightarrow> 2*k \<le> n" by linarith
172  consider "2*k \<le> n" | "2*k \<ge> n" "k \<le> n" | "k > n" by linarith
173  thus ?thesis
174  proof cases
175    case 1
176    thus ?thesis by (intro binomial_mono) linarith+
177  next
178    case 2
179    thus ?thesis by (intro binomial_antimono) simp_all
180  qed (simp_all add: binomial_eq_0)
181qed
182
183lemma binomial_maximum': "(2*n) choose k \<le> (2*n) choose n"
184  using binomial_maximum[of "2*n"] by simp
185
186lemma central_binomial_lower_bound:
187  assumes "n > 0"
188  shows   "4^n / (2*real n) \<le> real ((2*n) choose n)"
189proof -
190  from binomial[of 1 1 "2*n"]
191    have "4 ^ n = (\<Sum>k\<le>2*n. (2*n) choose k)"
192    by (simp add: power_mult power2_eq_square One_nat_def [symmetric] del: One_nat_def)
193  also have "{..2*n} = {0<..<2*n} \<union> {0,2*n}" by auto
194  also have "(\<Sum>k\<in>\<dots>. (2*n) choose k) =
195             (\<Sum>k\<in>{0<..<2*n}. (2*n) choose k) + (\<Sum>k\<in>{0,2*n}. (2*n) choose k)"
196    by (subst sum.union_disjoint) auto
197  also have "(\<Sum>k\<in>{0,2*n}. (2*n) choose k) \<le> (\<Sum>k\<le>1. (n choose k)\<^sup>2)"
198    by (cases n) simp_all
199  also from assms have "\<dots> \<le> (\<Sum>k\<le>n. (n choose k)\<^sup>2)"
200    by (intro sum_mono2) auto
201  also have "\<dots> = (2*n) choose n" by (rule choose_square_sum)
202  also have "(\<Sum>k\<in>{0<..<2*n}. (2*n) choose k) \<le> (\<Sum>k\<in>{0<..<2*n}. (2*n) choose n)"
203    by (intro sum_mono binomial_maximum')
204  also have "\<dots> = card {0<..<2*n} * ((2*n) choose n)" by simp
205  also have "card {0<..<2*n} \<le> 2*n - 1" by (cases n) simp_all
206  also have "(2 * n - 1) * (2 * n choose n) + (2 * n choose n) = ((2*n) choose n) * (2*n)"
207    using assms by (simp add: algebra_simps)
208  finally have "4 ^ n \<le> (2 * n choose n) * (2 * n)" by simp_all
209  hence "real (4 ^ n) \<le> real ((2 * n choose n) * (2 * n))"
210    by (subst of_nat_le_iff)
211  with assms show ?thesis by (simp add: field_simps)
212qed
213
214
215subsection \<open>Properties of Power Series\<close>
216
217lemma powser_zero [simp]: "(\<Sum>n. f n * 0 ^ n) = f 0"
218  for f :: "nat \<Rightarrow> 'a::real_normed_algebra_1"
219proof -
220  have "(\<Sum>n<1. f n * 0 ^ n) = (\<Sum>n. f n * 0 ^ n)"
221    by (subst suminf_finite[where N="{0}"]) (auto simp: power_0_left)
222  then show ?thesis by simp
223qed
224
225lemma powser_sums_zero: "(\<lambda>n. a n * 0^n) sums a 0"
226  for a :: "nat \<Rightarrow> 'a::real_normed_div_algebra"
227  using sums_finite [of "{0}" "\<lambda>n. a n * 0 ^ n"]
228  by simp
229
230lemma powser_sums_zero_iff [simp]: "(\<lambda>n. a n * 0^n) sums x \<longleftrightarrow> a 0 = x"
231  for a :: "nat \<Rightarrow> 'a::real_normed_div_algebra"
232  using powser_sums_zero sums_unique2 by blast
233
234text \<open>
235  Power series has a circle or radius of convergence: if it sums for \<open>x\<close>,
236  then it sums absolutely for \<open>z\<close> with @{term "\<bar>z\<bar> < \<bar>x\<bar>"}.\<close>
237
238lemma powser_insidea:
239  fixes x z :: "'a::real_normed_div_algebra"
240  assumes 1: "summable (\<lambda>n. f n * x^n)"
241    and 2: "norm z < norm x"
242  shows "summable (\<lambda>n. norm (f n * z ^ n))"
243proof -
244  from 2 have x_neq_0: "x \<noteq> 0" by clarsimp
245  from 1 have "(\<lambda>n. f n * x^n) \<longlonglongrightarrow> 0"
246    by (rule summable_LIMSEQ_zero)
247  then have "convergent (\<lambda>n. f n * x^n)"
248    by (rule convergentI)
249  then have "Cauchy (\<lambda>n. f n * x^n)"
250    by (rule convergent_Cauchy)
251  then have "Bseq (\<lambda>n. f n * x^n)"
252    by (rule Cauchy_Bseq)
253  then obtain K where 3: "0 < K" and 4: "\<forall>n. norm (f n * x^n) \<le> K"
254    by (auto simp: Bseq_def)
255  have "\<exists>N. \<forall>n\<ge>N. norm (norm (f n * z ^ n)) \<le> K * norm (z ^ n) * inverse (norm (x^n))"
256  proof (intro exI allI impI)
257    fix n :: nat
258    assume "0 \<le> n"
259    have "norm (norm (f n * z ^ n)) * norm (x^n) =
260          norm (f n * x^n) * norm (z ^ n)"
261      by (simp add: norm_mult abs_mult)
262    also have "\<dots> \<le> K * norm (z ^ n)"
263      by (simp only: mult_right_mono 4 norm_ge_zero)
264    also have "\<dots> = K * norm (z ^ n) * (inverse (norm (x^n)) * norm (x^n))"
265      by (simp add: x_neq_0)
266    also have "\<dots> = K * norm (z ^ n) * inverse (norm (x^n)) * norm (x^n)"
267      by (simp only: mult.assoc)
268    finally show "norm (norm (f n * z ^ n)) \<le> K * norm (z ^ n) * inverse (norm (x^n))"
269      by (simp add: mult_le_cancel_right x_neq_0)
270  qed
271  moreover have "summable (\<lambda>n. K * norm (z ^ n) * inverse (norm (x^n)))"
272  proof -
273    from 2 have "norm (norm (z * inverse x)) < 1"
274      using x_neq_0
275      by (simp add: norm_mult nonzero_norm_inverse divide_inverse [where 'a=real, symmetric])
276    then have "summable (\<lambda>n. norm (z * inverse x) ^ n)"
277      by (rule summable_geometric)
278    then have "summable (\<lambda>n. K * norm (z * inverse x) ^ n)"
279      by (rule summable_mult)
280    then show "summable (\<lambda>n. K * norm (z ^ n) * inverse (norm (x^n)))"
281      using x_neq_0
282      by (simp add: norm_mult nonzero_norm_inverse power_mult_distrib
283          power_inverse norm_power mult.assoc)
284  qed
285  ultimately show "summable (\<lambda>n. norm (f n * z ^ n))"
286    by (rule summable_comparison_test)
287qed
288
289lemma powser_inside:
290  fixes f :: "nat \<Rightarrow> 'a::{real_normed_div_algebra,banach}"
291  shows
292    "summable (\<lambda>n. f n * (x^n)) \<Longrightarrow> norm z < norm x \<Longrightarrow>
293      summable (\<lambda>n. f n * (z ^ n))"
294  by (rule powser_insidea [THEN summable_norm_cancel])
295
296lemma powser_times_n_limit_0:
297  fixes x :: "'a::{real_normed_div_algebra,banach}"
298  assumes "norm x < 1"
299    shows "(\<lambda>n. of_nat n * x ^ n) \<longlonglongrightarrow> 0"
300proof -
301  have "norm x / (1 - norm x) \<ge> 0"
302    using assms by (auto simp: divide_simps)
303  moreover obtain N where N: "norm x / (1 - norm x) < of_int N"
304    using ex_le_of_int by (meson ex_less_of_int)
305  ultimately have N0: "N>0"
306    by auto
307  then have *: "real_of_int (N + 1) * norm x / real_of_int N < 1"
308    using N assms by (auto simp: field_simps)
309  have **: "real_of_int N * (norm x * (real_of_nat (Suc n) * norm (x ^ n))) \<le>
310      real_of_nat n * (norm x * ((1 + N) * norm (x ^ n)))" if "N \<le> int n" for n :: nat
311  proof -
312    from that have "real_of_int N * real_of_nat (Suc n) \<le> real_of_nat n * real_of_int (1 + N)"
313      by (simp add: algebra_simps)
314    then have "(real_of_int N * real_of_nat (Suc n)) * (norm x * norm (x ^ n)) \<le>
315        (real_of_nat n *  (1 + N)) * (norm x * norm (x ^ n))"
316      using N0 mult_mono by fastforce
317    then show ?thesis
318      by (simp add: algebra_simps)
319  qed
320  show ?thesis using *
321    by (rule summable_LIMSEQ_zero [OF summable_ratio_test, where N1="nat N"])
322      (simp add: N0 norm_mult field_simps ** del: of_nat_Suc of_int_add)
323qed
324
325corollary lim_n_over_pown:
326  fixes x :: "'a::{real_normed_field,banach}"
327  shows "1 < norm x \<Longrightarrow> ((\<lambda>n. of_nat n / x^n) \<longlongrightarrow> 0) sequentially"
328  using powser_times_n_limit_0 [of "inverse x"]
329  by (simp add: norm_divide divide_simps)
330
331lemma sum_split_even_odd:
332  fixes f :: "nat \<Rightarrow> real"
333  shows "(\<Sum>i<2 * n. if even i then f i else g i) = (\<Sum>i<n. f (2 * i)) + (\<Sum>i<n. g (2 * i + 1))"
334proof (induct n)
335  case 0
336  then show ?case by simp
337next
338  case (Suc n)
339  have "(\<Sum>i<2 * Suc n. if even i then f i else g i) =
340    (\<Sum>i<n. f (2 * i)) + (\<Sum>i<n. g (2 * i + 1)) + (f (2 * n) + g (2 * n + 1))"
341    using Suc.hyps unfolding One_nat_def by auto
342  also have "\<dots> = (\<Sum>i<Suc n. f (2 * i)) + (\<Sum>i<Suc n. g (2 * i + 1))"
343    by auto
344  finally show ?case .
345qed
346
347lemma sums_if':
348  fixes g :: "nat \<Rightarrow> real"
349  assumes "g sums x"
350  shows "(\<lambda> n. if even n then 0 else g ((n - 1) div 2)) sums x"
351  unfolding sums_def
352proof (rule LIMSEQ_I)
353  fix r :: real
354  assume "0 < r"
355  from \<open>g sums x\<close>[unfolded sums_def, THEN LIMSEQ_D, OF this]
356  obtain no where no_eq: "\<And>n. n \<ge> no \<Longrightarrow> (norm (sum g {..<n} - x) < r)"
357    by blast
358
359  let ?SUM = "\<lambda> m. \<Sum>i<m. if even i then 0 else g ((i - 1) div 2)"
360  have "(norm (?SUM m - x) < r)" if "m \<ge> 2 * no" for m
361  proof -
362    from that have "m div 2 \<ge> no" by auto
363    have sum_eq: "?SUM (2 * (m div 2)) = sum g {..< m div 2}"
364      using sum_split_even_odd by auto
365    then have "(norm (?SUM (2 * (m div 2)) - x) < r)"
366      using no_eq unfolding sum_eq using \<open>m div 2 \<ge> no\<close> by auto
367    moreover
368    have "?SUM (2 * (m div 2)) = ?SUM m"
369    proof (cases "even m")
370      case True
371      then show ?thesis
372        by (auto simp: even_two_times_div_two)
373    next
374      case False
375      then have eq: "Suc (2 * (m div 2)) = m" by simp
376      then have "even (2 * (m div 2))" using \<open>odd m\<close> by auto
377      have "?SUM m = ?SUM (Suc (2 * (m div 2)))" unfolding eq ..
378      also have "\<dots> = ?SUM (2 * (m div 2))" using \<open>even (2 * (m div 2))\<close> by auto
379      finally show ?thesis by auto
380    qed
381    ultimately show ?thesis by auto
382  qed
383  then show "\<exists>no. \<forall> m \<ge> no. norm (?SUM m - x) < r"
384    by blast
385qed
386
387lemma sums_if:
388  fixes g :: "nat \<Rightarrow> real"
389  assumes "g sums x" and "f sums y"
390  shows "(\<lambda> n. if even n then f (n div 2) else g ((n - 1) div 2)) sums (x + y)"
391proof -
392  let ?s = "\<lambda> n. if even n then 0 else f ((n - 1) div 2)"
393  have if_sum: "(if B then (0 :: real) else E) + (if B then T else 0) = (if B then T else E)"
394    for B T E
395    by (cases B) auto
396  have g_sums: "(\<lambda> n. if even n then 0 else g ((n - 1) div 2)) sums x"
397    using sums_if'[OF \<open>g sums x\<close>] .
398  have if_eq: "\<And>B T E. (if \<not> B then T else E) = (if B then E else T)"
399    by auto
400  have "?s sums y" using sums_if'[OF \<open>f sums y\<close>] .
401  from this[unfolded sums_def, THEN LIMSEQ_Suc]
402  have "(\<lambda>n. if even n then f (n div 2) else 0) sums y"
403    by (simp add: lessThan_Suc_eq_insert_0 sum_atLeast1_atMost_eq image_Suc_lessThan
404        if_eq sums_def cong del: if_weak_cong)
405  from sums_add[OF g_sums this] show ?thesis
406    by (simp only: if_sum)
407qed
408
409subsection \<open>Alternating series test / Leibniz formula\<close>
410(* FIXME: generalise these results from the reals via type classes? *)
411
412lemma sums_alternating_upper_lower:
413  fixes a :: "nat \<Rightarrow> real"
414  assumes mono: "\<And>n. a (Suc n) \<le> a n"
415    and a_pos: "\<And>n. 0 \<le> a n"
416    and "a \<longlonglongrightarrow> 0"
417  shows "\<exists>l. ((\<forall>n. (\<Sum>i<2*n. (- 1)^i*a i) \<le> l) \<and> (\<lambda> n. \<Sum>i<2*n. (- 1)^i*a i) \<longlonglongrightarrow> l) \<and>
418             ((\<forall>n. l \<le> (\<Sum>i<2*n + 1. (- 1)^i*a i)) \<and> (\<lambda> n. \<Sum>i<2*n + 1. (- 1)^i*a i) \<longlonglongrightarrow> l)"
419  (is "\<exists>l. ((\<forall>n. ?f n \<le> l) \<and> _) \<and> ((\<forall>n. l \<le> ?g n) \<and> _)")
420proof (rule nested_sequence_unique)
421  have fg_diff: "\<And>n. ?f n - ?g n = - a (2 * n)" by auto
422
423  show "\<forall>n. ?f n \<le> ?f (Suc n)"
424  proof
425    show "?f n \<le> ?f (Suc n)" for n
426      using mono[of "2*n"] by auto
427  qed
428  show "\<forall>n. ?g (Suc n) \<le> ?g n"
429  proof
430    show "?g (Suc n) \<le> ?g n" for n
431      using mono[of "Suc (2*n)"] by auto
432  qed
433  show "\<forall>n. ?f n \<le> ?g n"
434  proof
435    show "?f n \<le> ?g n" for n
436      using fg_diff a_pos by auto
437  qed
438  show "(\<lambda>n. ?f n - ?g n) \<longlonglongrightarrow> 0"
439    unfolding fg_diff
440  proof (rule LIMSEQ_I)
441    fix r :: real
442    assume "0 < r"
443    with \<open>a \<longlonglongrightarrow> 0\<close>[THEN LIMSEQ_D] obtain N where "\<And> n. n \<ge> N \<Longrightarrow> norm (a n - 0) < r"
444      by auto
445    then have "\<forall>n \<ge> N. norm (- a (2 * n) - 0) < r"
446      by auto
447    then show "\<exists>N. \<forall>n \<ge> N. norm (- a (2 * n) - 0) < r"
448      by auto
449  qed
450qed
451
452lemma summable_Leibniz':
453  fixes a :: "nat \<Rightarrow> real"
454  assumes a_zero: "a \<longlonglongrightarrow> 0"
455    and a_pos: "\<And>n. 0 \<le> a n"
456    and a_monotone: "\<And>n. a (Suc n) \<le> a n"
457  shows summable: "summable (\<lambda> n. (-1)^n * a n)"
458    and "\<And>n. (\<Sum>i<2*n. (-1)^i*a i) \<le> (\<Sum>i. (-1)^i*a i)"
459    and "(\<lambda>n. \<Sum>i<2*n. (-1)^i*a i) \<longlonglongrightarrow> (\<Sum>i. (-1)^i*a i)"
460    and "\<And>n. (\<Sum>i. (-1)^i*a i) \<le> (\<Sum>i<2*n+1. (-1)^i*a i)"
461    and "(\<lambda>n. \<Sum>i<2*n+1. (-1)^i*a i) \<longlonglongrightarrow> (\<Sum>i. (-1)^i*a i)"
462proof -
463  let ?S = "\<lambda>n. (-1)^n * a n"
464  let ?P = "\<lambda>n. \<Sum>i<n. ?S i"
465  let ?f = "\<lambda>n. ?P (2 * n)"
466  let ?g = "\<lambda>n. ?P (2 * n + 1)"
467  obtain l :: real
468    where below_l: "\<forall> n. ?f n \<le> l"
469      and "?f \<longlonglongrightarrow> l"
470      and above_l: "\<forall> n. l \<le> ?g n"
471      and "?g \<longlonglongrightarrow> l"
472    using sums_alternating_upper_lower[OF a_monotone a_pos a_zero] by blast
473
474  let ?Sa = "\<lambda>m. \<Sum>n<m. ?S n"
475  have "?Sa \<longlonglongrightarrow> l"
476  proof (rule LIMSEQ_I)
477    fix r :: real
478    assume "0 < r"
479    with \<open>?f \<longlonglongrightarrow> l\<close>[THEN LIMSEQ_D]
480    obtain f_no where f: "\<And>n. n \<ge> f_no \<Longrightarrow> norm (?f n - l) < r"
481      by auto
482    from \<open>0 < r\<close> \<open>?g \<longlonglongrightarrow> l\<close>[THEN LIMSEQ_D]
483    obtain g_no where g: "\<And>n. n \<ge> g_no \<Longrightarrow> norm (?g n - l) < r"
484      by auto
485    have "norm (?Sa n - l) < r" if "n \<ge> (max (2 * f_no) (2 * g_no))" for n
486    proof -
487      from that have "n \<ge> 2 * f_no" and "n \<ge> 2 * g_no" by auto
488      show ?thesis
489      proof (cases "even n")
490        case True
491        then have n_eq: "2 * (n div 2) = n"
492          by (simp add: even_two_times_div_two)
493        with \<open>n \<ge> 2 * f_no\<close> have "n div 2 \<ge> f_no"
494          by auto
495        from f[OF this] show ?thesis
496          unfolding n_eq atLeastLessThanSuc_atLeastAtMost .
497      next
498        case False
499        then have "even (n - 1)" by simp
500        then have n_eq: "2 * ((n - 1) div 2) = n - 1"
501          by (simp add: even_two_times_div_two)
502        then have range_eq: "n - 1 + 1 = n"
503          using odd_pos[OF False] by auto
504        from n_eq \<open>n \<ge> 2 * g_no\<close> have "(n - 1) div 2 \<ge> g_no"
505          by auto
506        from g[OF this] show ?thesis
507          by (simp only: n_eq range_eq)
508      qed
509    qed
510    then show "\<exists>no. \<forall>n \<ge> no. norm (?Sa n - l) < r" by blast
511  qed
512  then have sums_l: "(\<lambda>i. (-1)^i * a i) sums l"
513    by (simp only: sums_def)
514  then show "summable ?S"
515    by (auto simp: summable_def)
516
517  have "l = suminf ?S" by (rule sums_unique[OF sums_l])
518
519  fix n
520  show "suminf ?S \<le> ?g n"
521    unfolding sums_unique[OF sums_l, symmetric] using above_l by auto
522  show "?f n \<le> suminf ?S"
523    unfolding sums_unique[OF sums_l, symmetric] using below_l by auto
524  show "?g \<longlonglongrightarrow> suminf ?S"
525    using \<open>?g \<longlonglongrightarrow> l\<close> \<open>l = suminf ?S\<close> by auto
526  show "?f \<longlonglongrightarrow> suminf ?S"
527    using \<open>?f \<longlonglongrightarrow> l\<close> \<open>l = suminf ?S\<close> by auto
528qed
529
530theorem summable_Leibniz:
531  fixes a :: "nat \<Rightarrow> real"
532  assumes a_zero: "a \<longlonglongrightarrow> 0"
533    and "monoseq a"
534  shows "summable (\<lambda> n. (-1)^n * a n)" (is "?summable")
535    and "0 < a 0 \<longrightarrow>
536      (\<forall>n. (\<Sum>i. (- 1)^i*a i) \<in> { \<Sum>i<2*n. (- 1)^i * a i .. \<Sum>i<2*n+1. (- 1)^i * a i})" (is "?pos")
537    and "a 0 < 0 \<longrightarrow>
538      (\<forall>n. (\<Sum>i. (- 1)^i*a i) \<in> { \<Sum>i<2*n+1. (- 1)^i * a i .. \<Sum>i<2*n. (- 1)^i * a i})" (is "?neg")
539    and "(\<lambda>n. \<Sum>i<2*n. (- 1)^i*a i) \<longlonglongrightarrow> (\<Sum>i. (- 1)^i*a i)" (is "?f")
540    and "(\<lambda>n. \<Sum>i<2*n+1. (- 1)^i*a i) \<longlonglongrightarrow> (\<Sum>i. (- 1)^i*a i)" (is "?g")
541proof -
542  have "?summable \<and> ?pos \<and> ?neg \<and> ?f \<and> ?g"
543  proof (cases "(\<forall>n. 0 \<le> a n) \<and> (\<forall>m. \<forall>n\<ge>m. a n \<le> a m)")
544    case True
545    then have ord: "\<And>n m. m \<le> n \<Longrightarrow> a n \<le> a m"
546      and ge0: "\<And>n. 0 \<le> a n"
547      by auto
548    have mono: "a (Suc n) \<le> a n" for n
549      using ord[where n="Suc n" and m=n] by auto
550    note leibniz = summable_Leibniz'[OF \<open>a \<longlonglongrightarrow> 0\<close> ge0]
551    from leibniz[OF mono]
552    show ?thesis using \<open>0 \<le> a 0\<close> by auto
553  next
554    let ?a = "\<lambda>n. - a n"
555    case False
556    with monoseq_le[OF \<open>monoseq a\<close> \<open>a \<longlonglongrightarrow> 0\<close>]
557    have "(\<forall> n. a n \<le> 0) \<and> (\<forall>m. \<forall>n\<ge>m. a m \<le> a n)" by auto
558    then have ord: "\<And>n m. m \<le> n \<Longrightarrow> ?a n \<le> ?a m" and ge0: "\<And> n. 0 \<le> ?a n"
559      by auto
560    have monotone: "?a (Suc n) \<le> ?a n" for n
561      using ord[where n="Suc n" and m=n] by auto
562    note leibniz =
563      summable_Leibniz'[OF _ ge0, of "\<lambda>x. x",
564        OF tendsto_minus[OF \<open>a \<longlonglongrightarrow> 0\<close>, unfolded minus_zero] monotone]
565    have "summable (\<lambda> n. (-1)^n * ?a n)"
566      using leibniz(1) by auto
567    then obtain l where "(\<lambda> n. (-1)^n * ?a n) sums l"
568      unfolding summable_def by auto
569    from this[THEN sums_minus] have "(\<lambda> n. (-1)^n * a n) sums -l"
570      by auto
571    then have ?summable by (auto simp: summable_def)
572    moreover
573    have "\<bar>- a - - b\<bar> = \<bar>a - b\<bar>" for a b :: real
574      unfolding minus_diff_minus by auto
575
576    from suminf_minus[OF leibniz(1), unfolded mult_minus_right minus_minus]
577    have move_minus: "(\<Sum>n. - ((- 1) ^ n * a n)) = - (\<Sum>n. (- 1) ^ n * a n)"
578      by auto
579
580    have ?pos using \<open>0 \<le> ?a 0\<close> by auto
581    moreover have ?neg
582      using leibniz(2,4)
583      unfolding mult_minus_right sum_negf move_minus neg_le_iff_le
584      by auto
585    moreover have ?f and ?g
586      using leibniz(3,5)[unfolded mult_minus_right sum_negf move_minus, THEN tendsto_minus_cancel]
587      by auto
588    ultimately show ?thesis by auto
589  qed
590  then show ?summable and ?pos and ?neg and ?f and ?g
591    by safe
592qed
593
594
595subsection \<open>Term-by-Term Differentiability of Power Series\<close>
596
597definition diffs :: "(nat \<Rightarrow> 'a::ring_1) \<Rightarrow> nat \<Rightarrow> 'a"
598  where "diffs c = (\<lambda>n. of_nat (Suc n) * c (Suc n))"
599
600text \<open>Lemma about distributing negation over it.\<close>
601lemma diffs_minus: "diffs (\<lambda>n. - c n) = (\<lambda>n. - diffs c n)"
602  by (simp add: diffs_def)
603
604lemma diffs_equiv:
605  fixes x :: "'a::{real_normed_vector,ring_1}"
606  shows "summable (\<lambda>n. diffs c n * x^n) \<Longrightarrow>
607    (\<lambda>n. of_nat n * c n * x^(n - Suc 0)) sums (\<Sum>n. diffs c n * x^n)"
608  unfolding diffs_def
609  by (simp add: summable_sums sums_Suc_imp)
610
611lemma lemma_termdiff1:
612  fixes z :: "'a :: {monoid_mult,comm_ring}"
613  shows "(\<Sum>p<m. (((z + h) ^ (m - p)) * (z ^ p)) - (z ^ m)) =
614    (\<Sum>p<m. (z ^ p) * (((z + h) ^ (m - p)) - (z ^ (m - p))))"
615  by (auto simp: algebra_simps power_add [symmetric])
616
617lemma sumr_diff_mult_const2: "sum f {..<n} - of_nat n * r = (\<Sum>i<n. f i - r)"
618  for r :: "'a::ring_1"
619  by (simp add: sum_subtractf)
620
621lemma lemma_realpow_rev_sumr:
622  "(\<Sum>p<Suc n. (x ^ p) * (y ^ (n - p))) = (\<Sum>p<Suc n. (x ^ (n - p)) * (y ^ p))"
623  by (subst nat_diff_sum_reindex[symmetric]) simp
624
625lemma lemma_termdiff2:
626  fixes h :: "'a::field"
627  assumes h: "h \<noteq> 0"
628  shows "((z + h) ^ n - z ^ n) / h - of_nat n * z ^ (n - Suc 0) =
629         h * (\<Sum>p< n - Suc 0. \<Sum>q< n - Suc 0 - p. (z + h) ^ q * z ^ (n - 2 - q))"
630    (is "?lhs = ?rhs")
631proof (cases n)
632  case (Suc n)
633  have 0: "\<And>x k. (\<Sum>n<Suc k. h * (z ^ x * (z ^ (k - n) * (h + z) ^ n))) =
634                 (\<Sum>j<Suc k.  h * ((h + z) ^ j * z ^ (x + k - j)))"
635    apply (rule sum.cong [OF refl])
636    by (simp add: power_add [symmetric] mult.commute)
637  have *: "(\<Sum>i<n. z ^ i * ((z + h) ^ (n - i) - z ^ (n - i))) =
638           (\<Sum>i<n. \<Sum>j<n - i. h * ((z + h) ^ j * z ^ (n - Suc j)))"
639    apply (rule sum.cong [OF refl])
640    apply (clarsimp simp add: less_iff_Suc_add sum_distrib_left diff_power_eq_sum ac_simps 0
641        simp del: sum_lessThan_Suc power_Suc)
642    done
643  have "h * ?lhs = h * ?rhs"
644    apply (simp add: right_diff_distrib diff_divide_distrib h mult.assoc [symmetric])
645    using Suc
646    apply (simp add: diff_power_eq_sum h right_diff_distrib [symmetric] mult.assoc
647        del: power_Suc sum_lessThan_Suc of_nat_Suc)
648    apply (subst lemma_realpow_rev_sumr)
649    apply (subst sumr_diff_mult_const2)
650    apply (simp add: lemma_termdiff1 sum_distrib_left *)
651    done
652  then show ?thesis
653    by (simp add: h)
654qed auto
655
656
657lemma real_sum_nat_ivl_bounded2:
658  fixes K :: "'a::linordered_semidom"
659  assumes f: "\<And>p::nat. p < n \<Longrightarrow> f p \<le> K"
660    and K: "0 \<le> K"
661  shows "sum f {..<n-k} \<le> of_nat n * K"
662  apply (rule order_trans [OF sum_mono [OF f]])
663  apply (auto simp: mult_right_mono K)
664  done
665
666lemma lemma_termdiff3:
667  fixes h z :: "'a::real_normed_field"
668  assumes 1: "h \<noteq> 0"
669    and 2: "norm z \<le> K"
670    and 3: "norm (z + h) \<le> K"
671  shows "norm (((z + h) ^ n - z ^ n) / h - of_nat n * z ^ (n - Suc 0)) \<le>
672    of_nat n * of_nat (n - Suc 0) * K ^ (n - 2) * norm h"
673proof -
674  have "norm (((z + h) ^ n - z ^ n) / h - of_nat n * z ^ (n - Suc 0)) =
675    norm (\<Sum>p<n - Suc 0. \<Sum>q<n - Suc 0 - p. (z + h) ^ q * z ^ (n - 2 - q)) * norm h"
676    by (metis (lifting, no_types) lemma_termdiff2 [OF 1] mult.commute norm_mult)
677  also have "\<dots> \<le> of_nat n * (of_nat (n - Suc 0) * K ^ (n - 2)) * norm h"
678  proof (rule mult_right_mono [OF _ norm_ge_zero])
679    from norm_ge_zero 2 have K: "0 \<le> K"
680      by (rule order_trans)
681    have le_Kn: "\<And>i j n. i + j = n \<Longrightarrow> norm ((z + h) ^ i * z ^ j) \<le> K ^ n"
682      apply (erule subst)
683      apply (simp only: norm_mult norm_power power_add)
684      apply (intro mult_mono power_mono 2 3 norm_ge_zero zero_le_power K)
685      done
686    show "norm (\<Sum>p<n - Suc 0. \<Sum>q<n - Suc 0 - p. (z + h) ^ q * z ^ (n - 2 - q)) \<le>
687        of_nat n * (of_nat (n - Suc 0) * K ^ (n - 2))"
688      apply (intro
689          order_trans [OF norm_sum]
690          real_sum_nat_ivl_bounded2
691          mult_nonneg_nonneg
692          of_nat_0_le_iff
693          zero_le_power K)
694      apply (rule le_Kn, simp)
695      done
696  qed
697  also have "\<dots> = of_nat n * of_nat (n - Suc 0) * K ^ (n - 2) * norm h"
698    by (simp only: mult.assoc)
699  finally show ?thesis .
700qed
701
702lemma lemma_termdiff4:
703  fixes f :: "'a::real_normed_vector \<Rightarrow> 'b::real_normed_vector"
704    and k :: real
705  assumes k: "0 < k"
706    and le: "\<And>h. h \<noteq> 0 \<Longrightarrow> norm h < k \<Longrightarrow> norm (f h) \<le> K * norm h"
707  shows "f \<midarrow>0\<rightarrow> 0"
708proof (rule tendsto_norm_zero_cancel)
709  show "(\<lambda>h. norm (f h)) \<midarrow>0\<rightarrow> 0"
710  proof (rule real_tendsto_sandwich)
711    show "eventually (\<lambda>h. 0 \<le> norm (f h)) (at 0)"
712      by simp
713    show "eventually (\<lambda>h. norm (f h) \<le> K * norm h) (at 0)"
714      using k by (auto simp: eventually_at dist_norm le)
715    show "(\<lambda>h. 0) \<midarrow>(0::'a)\<rightarrow> (0::real)"
716      by (rule tendsto_const)
717    have "(\<lambda>h. K * norm h) \<midarrow>(0::'a)\<rightarrow> K * norm (0::'a)"
718      by (intro tendsto_intros)
719    then show "(\<lambda>h. K * norm h) \<midarrow>(0::'a)\<rightarrow> 0"
720      by simp
721  qed
722qed
723
724lemma lemma_termdiff5:
725  fixes g :: "'a::real_normed_vector \<Rightarrow> nat \<Rightarrow> 'b::banach"
726    and k :: real
727  assumes k: "0 < k"
728    and f: "summable f"
729    and le: "\<And>h n. h \<noteq> 0 \<Longrightarrow> norm h < k \<Longrightarrow> norm (g h n) \<le> f n * norm h"
730  shows "(\<lambda>h. suminf (g h)) \<midarrow>0\<rightarrow> 0"
731proof (rule lemma_termdiff4 [OF k])
732  fix h :: 'a
733  assume "h \<noteq> 0" and "norm h < k"
734  then have 1: "\<forall>n. norm (g h n) \<le> f n * norm h"
735    by (simp add: le)
736  then have "\<exists>N. \<forall>n\<ge>N. norm (norm (g h n)) \<le> f n * norm h"
737    by simp
738  moreover from f have 2: "summable (\<lambda>n. f n * norm h)"
739    by (rule summable_mult2)
740  ultimately have 3: "summable (\<lambda>n. norm (g h n))"
741    by (rule summable_comparison_test)
742  then have "norm (suminf (g h)) \<le> (\<Sum>n. norm (g h n))"
743    by (rule summable_norm)
744  also from 1 3 2 have "(\<Sum>n. norm (g h n)) \<le> (\<Sum>n. f n * norm h)"
745    by (rule suminf_le)
746  also from f have "(\<Sum>n. f n * norm h) = suminf f * norm h"
747    by (rule suminf_mult2 [symmetric])
748  finally show "norm (suminf (g h)) \<le> suminf f * norm h" .
749qed
750
751
752(* FIXME: Long proofs *)
753
754lemma termdiffs_aux:
755  fixes x :: "'a::{real_normed_field,banach}"
756  assumes 1: "summable (\<lambda>n. diffs (diffs c) n * K ^ n)"
757    and 2: "norm x < norm K"
758  shows "(\<lambda>h. \<Sum>n. c n * (((x + h) ^ n - x^n) / h - of_nat n * x ^ (n - Suc 0))) \<midarrow>0\<rightarrow> 0"
759proof -
760  from dense [OF 2] obtain r where r1: "norm x < r" and r2: "r < norm K"
761    by fast
762  from norm_ge_zero r1 have r: "0 < r"
763    by (rule order_le_less_trans)
764  then have r_neq_0: "r \<noteq> 0" by simp
765  show ?thesis
766  proof (rule lemma_termdiff5)
767    show "0 < r - norm x"
768      using r1 by simp
769    from r r2 have "norm (of_real r::'a) < norm K"
770      by simp
771    with 1 have "summable (\<lambda>n. norm (diffs (diffs c) n * (of_real r ^ n)))"
772      by (rule powser_insidea)
773    then have "summable (\<lambda>n. diffs (diffs (\<lambda>n. norm (c n))) n * r ^ n)"
774      using r by (simp add: diffs_def norm_mult norm_power del: of_nat_Suc)
775    then have "summable (\<lambda>n. of_nat n * diffs (\<lambda>n. norm (c n)) n * r ^ (n - Suc 0))"
776      by (rule diffs_equiv [THEN sums_summable])
777    also have "(\<lambda>n. of_nat n * diffs (\<lambda>n. norm (c n)) n * r ^ (n - Suc 0)) =
778      (\<lambda>n. diffs (\<lambda>m. of_nat (m - Suc 0) * norm (c m) * inverse r) n * (r ^ n))"
779      apply (rule ext)
780      apply (case_tac n)
781      apply (simp_all add: diffs_def r_neq_0)
782      done
783    finally have "summable
784      (\<lambda>n. of_nat n * (of_nat (n - Suc 0) * norm (c n) * inverse r) * r ^ (n - Suc 0))"
785      by (rule diffs_equiv [THEN sums_summable])
786    also have
787      "(\<lambda>n. of_nat n * (of_nat (n - Suc 0) * norm (c n) * inverse r) * r ^ (n - Suc 0)) =
788       (\<lambda>n. norm (c n) * of_nat n * of_nat (n - Suc 0) * r ^ (n - 2))"
789      apply (rule ext)
790      apply (case_tac n, simp)
791      apply (rename_tac nat)
792      apply (case_tac nat, simp)
793      apply (simp add: r_neq_0)
794      done
795    finally show "summable (\<lambda>n. norm (c n) * of_nat n * of_nat (n - Suc 0) * r ^ (n - 2))" .
796  next
797    fix h :: 'a
798    fix n :: nat
799    assume h: "h \<noteq> 0"
800    assume "norm h < r - norm x"
801    then have "norm x + norm h < r" by simp
802    with norm_triangle_ineq have xh: "norm (x + h) < r"
803      by (rule order_le_less_trans)
804    show "norm (c n * (((x + h) ^ n - x^n) / h - of_nat n * x ^ (n - Suc 0))) \<le>
805      norm (c n) * of_nat n * of_nat (n - Suc 0) * r ^ (n - 2) * norm h"
806      apply (simp only: norm_mult mult.assoc)
807      apply (rule mult_left_mono [OF _ norm_ge_zero])
808      apply (simp add: mult.assoc [symmetric])
809      apply (metis h lemma_termdiff3 less_eq_real_def r1 xh)
810      done
811  qed
812qed
813
814lemma termdiffs:
815  fixes K x :: "'a::{real_normed_field,banach}"
816  assumes 1: "summable (\<lambda>n. c n * K ^ n)"
817    and 2: "summable (\<lambda>n. (diffs c) n * K ^ n)"
818    and 3: "summable (\<lambda>n. (diffs (diffs c)) n * K ^ n)"
819    and 4: "norm x < norm K"
820  shows "DERIV (\<lambda>x. \<Sum>n. c n * x^n) x :> (\<Sum>n. (diffs c) n * x^n)"
821  unfolding DERIV_def
822proof (rule LIM_zero_cancel)
823  show "(\<lambda>h. (suminf (\<lambda>n. c n * (x + h) ^ n) - suminf (\<lambda>n. c n * x^n)) / h
824            - suminf (\<lambda>n. diffs c n * x^n)) \<midarrow>0\<rightarrow> 0"
825  proof (rule LIM_equal2)
826    show "0 < norm K - norm x"
827      using 4 by (simp add: less_diff_eq)
828  next
829    fix h :: 'a
830    assume "norm (h - 0) < norm K - norm x"
831    then have "norm x + norm h < norm K" by simp
832    then have 5: "norm (x + h) < norm K"
833      by (rule norm_triangle_ineq [THEN order_le_less_trans])
834    have "summable (\<lambda>n. c n * x^n)"
835      and "summable (\<lambda>n. c n * (x + h) ^ n)"
836      and "summable (\<lambda>n. diffs c n * x^n)"
837      using 1 2 4 5 by (auto elim: powser_inside)
838    then have "((\<Sum>n. c n * (x + h) ^ n) - (\<Sum>n. c n * x^n)) / h - (\<Sum>n. diffs c n * x^n) =
839          (\<Sum>n. (c n * (x + h) ^ n - c n * x^n) / h - of_nat n * c n * x ^ (n - Suc 0))"
840      by (intro sums_unique sums_diff sums_divide diffs_equiv summable_sums)
841    then show "((\<Sum>n. c n * (x + h) ^ n) - (\<Sum>n. c n * x^n)) / h - (\<Sum>n. diffs c n * x^n) =
842          (\<Sum>n. c n * (((x + h) ^ n - x^n) / h - of_nat n * x ^ (n - Suc 0)))"
843      by (simp add: algebra_simps)
844  next
845    show "(\<lambda>h. \<Sum>n. c n * (((x + h) ^ n - x^n) / h - of_nat n * x ^ (n - Suc 0))) \<midarrow>0\<rightarrow> 0"
846      by (rule termdiffs_aux [OF 3 4])
847  qed
848qed
849
850subsection \<open>The Derivative of a Power Series Has the Same Radius of Convergence\<close>
851
852lemma termdiff_converges:
853  fixes x :: "'a::{real_normed_field,banach}"
854  assumes K: "norm x < K"
855    and sm: "\<And>x. norm x < K \<Longrightarrow> summable(\<lambda>n. c n * x ^ n)"
856  shows "summable (\<lambda>n. diffs c n * x ^ n)"
857proof (cases "x = 0")
858  case True
859  then show ?thesis
860    using powser_sums_zero sums_summable by auto
861next
862  case False
863  then have "K > 0"
864    using K less_trans zero_less_norm_iff by blast
865  then obtain r :: real where r: "norm x < norm r" "norm r < K" "r > 0"
866    using K False
867    by (auto simp: field_simps abs_less_iff add_pos_pos intro: that [of "(norm x + K) / 2"])
868  have to0: "(\<lambda>n. of_nat n * (x / of_real r) ^ n) \<longlonglongrightarrow> 0"
869    using r by (simp add: norm_divide powser_times_n_limit_0 [of "x / of_real r"])
870  obtain N where N: "\<And>n. n\<ge>N \<Longrightarrow> real_of_nat n * norm x ^ n < r ^ n"
871    using r LIMSEQ_D [OF to0, of 1]
872    by (auto simp: norm_divide norm_mult norm_power field_simps)
873  have "summable (\<lambda>n. (of_nat n * c n) * x ^ n)"
874  proof (rule summable_comparison_test')
875    show "summable (\<lambda>n. norm (c n * of_real r ^ n))"
876      apply (rule powser_insidea [OF sm [of "of_real ((r+K)/2)"]])
877      using N r norm_of_real [of "r + K", where 'a = 'a] by auto
878    show "\<And>n. N \<le> n \<Longrightarrow> norm (of_nat n * c n * x ^ n) \<le> norm (c n * of_real r ^ n)"
879      using N r by (fastforce simp add: norm_mult norm_power less_eq_real_def)
880  qed
881  then have "summable (\<lambda>n. (of_nat (Suc n) * c(Suc n)) * x ^ Suc n)"
882    using summable_iff_shift [of "\<lambda>n. of_nat n * c n * x ^ n" 1]
883    by simp
884  then have "summable (\<lambda>n. (of_nat (Suc n) * c(Suc n)) * x ^ n)"
885    using False summable_mult2 [of "\<lambda>n. (of_nat (Suc n) * c(Suc n) * x ^ n) * x" "inverse x"]
886    by (simp add: mult.assoc) (auto simp: ac_simps)
887  then show ?thesis
888    by (simp add: diffs_def)
889qed
890
891lemma termdiff_converges_all:
892  fixes x :: "'a::{real_normed_field,banach}"
893  assumes "\<And>x. summable (\<lambda>n. c n * x^n)"
894  shows "summable (\<lambda>n. diffs c n * x^n)"
895  by (rule termdiff_converges [where K = "1 + norm x"]) (use assms in auto)
896
897lemma termdiffs_strong:
898  fixes K x :: "'a::{real_normed_field,banach}"
899  assumes sm: "summable (\<lambda>n. c n * K ^ n)"
900    and K: "norm x < norm K"
901  shows "DERIV (\<lambda>x. \<Sum>n. c n * x^n) x :> (\<Sum>n. diffs c n * x^n)"
902proof -
903  have K2: "norm ((of_real (norm K) + of_real (norm x)) / 2 :: 'a) < norm K"
904    using K
905    apply (auto simp: norm_divide field_simps)
906    apply (rule le_less_trans [of _ "of_real (norm K) + of_real (norm x)"])
907     apply (auto simp: mult_2_right norm_triangle_mono)
908    done
909  then have [simp]: "norm ((of_real (norm K) + of_real (norm x)) :: 'a) < norm K * 2"
910    by simp
911  have "summable (\<lambda>n. c n * (of_real (norm x + norm K) / 2) ^ n)"
912    by (metis K2 summable_norm_cancel [OF powser_insidea [OF sm]] add.commute of_real_add)
913  moreover have "\<And>x. norm x < norm K \<Longrightarrow> summable (\<lambda>n. diffs c n * x ^ n)"
914    by (blast intro: sm termdiff_converges powser_inside)
915  moreover have "\<And>x. norm x < norm K \<Longrightarrow> summable (\<lambda>n. diffs(diffs c) n * x ^ n)"
916    by (blast intro: sm termdiff_converges powser_inside)
917  ultimately show ?thesis
918    apply (rule termdiffs [where K = "of_real (norm x + norm K) / 2"])
919    using K
920      apply (auto simp: field_simps)
921    apply (simp flip: of_real_add)
922    done
923qed
924
925lemma termdiffs_strong_converges_everywhere:
926  fixes K x :: "'a::{real_normed_field,banach}"
927  assumes "\<And>y. summable (\<lambda>n. c n * y ^ n)"
928  shows "((\<lambda>x. \<Sum>n. c n * x^n) has_field_derivative (\<Sum>n. diffs c n * x^n)) (at x)"
929  using termdiffs_strong[OF assms[of "of_real (norm x + 1)"], of x]
930  by (force simp del: of_real_add)
931
932lemma termdiffs_strong':
933  fixes z :: "'a :: {real_normed_field,banach}"
934  assumes "\<And>z. norm z < K \<Longrightarrow> summable (\<lambda>n. c n * z ^ n)"
935  assumes "norm z < K"
936  shows   "((\<lambda>z. \<Sum>n. c n * z^n) has_field_derivative (\<Sum>n. diffs c n * z^n)) (at z)"
937proof (rule termdiffs_strong)
938  define L :: real where "L =  (norm z + K) / 2"
939  have "0 \<le> norm z" by simp
940  also note \<open>norm z < K\<close>
941  finally have K: "K \<ge> 0" by simp
942  from assms K have L: "L \<ge> 0" "norm z < L" "L < K" by (simp_all add: L_def)
943  from L show "norm z < norm (of_real L :: 'a)" by simp
944  from L show "summable (\<lambda>n. c n * of_real L ^ n)" by (intro assms(1)) simp_all
945qed
946
947lemma termdiffs_sums_strong:
948  fixes z :: "'a :: {banach,real_normed_field}"
949  assumes sums: "\<And>z. norm z < K \<Longrightarrow> (\<lambda>n. c n * z ^ n) sums f z"
950  assumes deriv: "(f has_field_derivative f') (at z)"
951  assumes norm: "norm z < K"
952  shows   "(\<lambda>n. diffs c n * z ^ n) sums f'"
953proof -
954  have summable: "summable (\<lambda>n. diffs c n * z^n)"
955    by (intro termdiff_converges[OF norm] sums_summable[OF sums])
956  from norm have "eventually (\<lambda>z. z \<in> norm -` {..<K}) (nhds z)"
957    by (intro eventually_nhds_in_open open_vimage)
958       (simp_all add: continuous_on_norm continuous_on_id)
959  hence eq: "eventually (\<lambda>z. (\<Sum>n. c n * z^n) = f z) (nhds z)"
960    by eventually_elim (insert sums, simp add: sums_iff)
961
962  have "((\<lambda>z. \<Sum>n. c n * z^n) has_field_derivative (\<Sum>n. diffs c n * z^n)) (at z)"
963    by (intro termdiffs_strong'[OF _ norm] sums_summable[OF sums])
964  hence "(f has_field_derivative (\<Sum>n. diffs c n * z^n)) (at z)"
965    by (subst (asm) DERIV_cong_ev[OF refl eq refl])
966  from this and deriv have "(\<Sum>n. diffs c n * z^n) = f'" by (rule DERIV_unique)
967  with summable show ?thesis by (simp add: sums_iff)
968qed
969
970lemma isCont_powser:
971  fixes K x :: "'a::{real_normed_field,banach}"
972  assumes "summable (\<lambda>n. c n * K ^ n)"
973  assumes "norm x < norm K"
974  shows "isCont (\<lambda>x. \<Sum>n. c n * x^n) x"
975  using termdiffs_strong[OF assms] by (blast intro!: DERIV_isCont)
976
977lemmas isCont_powser' = isCont_o2[OF _ isCont_powser]
978
979lemma isCont_powser_converges_everywhere:
980  fixes K x :: "'a::{real_normed_field,banach}"
981  assumes "\<And>y. summable (\<lambda>n. c n * y ^ n)"
982  shows "isCont (\<lambda>x. \<Sum>n. c n * x^n) x"
983  using termdiffs_strong[OF assms[of "of_real (norm x + 1)"], of x]
984  by (force intro!: DERIV_isCont simp del: of_real_add)
985
986lemma powser_limit_0:
987  fixes a :: "nat \<Rightarrow> 'a::{real_normed_field,banach}"
988  assumes s: "0 < s"
989    and sm: "\<And>x. norm x < s \<Longrightarrow> (\<lambda>n. a n * x ^ n) sums (f x)"
990  shows "(f \<longlongrightarrow> a 0) (at 0)"
991proof -
992  have "norm (of_real s / 2 :: 'a) < s"
993    using s  by (auto simp: norm_divide)
994  then have "summable (\<lambda>n. a n * (of_real s / 2) ^ n)"
995    by (rule sums_summable [OF sm])
996  then have "((\<lambda>x. \<Sum>n. a n * x ^ n) has_field_derivative (\<Sum>n. diffs a n * 0 ^ n)) (at 0)"
997    by (rule termdiffs_strong) (use s in \<open>auto simp: norm_divide\<close>)
998  then have "isCont (\<lambda>x. \<Sum>n. a n * x ^ n) 0"
999    by (blast intro: DERIV_continuous)
1000  then have "((\<lambda>x. \<Sum>n. a n * x ^ n) \<longlongrightarrow> a 0) (at 0)"
1001    by (simp add: continuous_within)
1002  then show ?thesis
1003    apply (rule Lim_transform)
1004    apply (clarsimp simp: LIM_eq)
1005    apply (rule_tac x=s in exI)
1006    using s sm sums_unique by fastforce
1007qed
1008
1009lemma powser_limit_0_strong:
1010  fixes a :: "nat \<Rightarrow> 'a::{real_normed_field,banach}"
1011  assumes s: "0 < s"
1012    and sm: "\<And>x. x \<noteq> 0 \<Longrightarrow> norm x < s \<Longrightarrow> (\<lambda>n. a n * x ^ n) sums (f x)"
1013  shows "(f \<longlongrightarrow> a 0) (at 0)"
1014proof -
1015  have *: "((\<lambda>x. if x = 0 then a 0 else f x) \<longlongrightarrow> a 0) (at 0)"
1016    by (rule powser_limit_0 [OF s]) (auto simp: powser_sums_zero sm)
1017  show ?thesis
1018    apply (subst LIM_equal [where g = "(\<lambda>x. if x = 0 then a 0 else f x)"])
1019     apply (simp_all add: *)
1020    done
1021qed
1022
1023
1024subsection \<open>Derivability of power series\<close>
1025
1026lemma DERIV_series':
1027  fixes f :: "real \<Rightarrow> nat \<Rightarrow> real"
1028  assumes DERIV_f: "\<And> n. DERIV (\<lambda> x. f x n) x0 :> (f' x0 n)"
1029    and allf_summable: "\<And> x. x \<in> {a <..< b} \<Longrightarrow> summable (f x)"
1030    and x0_in_I: "x0 \<in> {a <..< b}"
1031    and "summable (f' x0)"
1032    and "summable L"
1033    and L_def: "\<And>n x y. x \<in> {a <..< b} \<Longrightarrow> y \<in> {a <..< b} \<Longrightarrow> \<bar>f x n - f y n\<bar> \<le> L n * \<bar>x - y\<bar>"
1034  shows "DERIV (\<lambda> x. suminf (f x)) x0 :> (suminf (f' x0))"
1035  unfolding DERIV_def
1036proof (rule LIM_I)
1037  fix r :: real
1038  assume "0 < r" then have "0 < r/3" by auto
1039
1040  obtain N_L where N_L: "\<And> n. N_L \<le> n \<Longrightarrow> \<bar> \<Sum> i. L (i + n) \<bar> < r/3"
1041    using suminf_exist_split[OF \<open>0 < r/3\<close> \<open>summable L\<close>] by auto
1042
1043  obtain N_f' where N_f': "\<And> n. N_f' \<le> n \<Longrightarrow> \<bar> \<Sum> i. f' x0 (i + n) \<bar> < r/3"
1044    using suminf_exist_split[OF \<open>0 < r/3\<close> \<open>summable (f' x0)\<close>] by auto
1045
1046  let ?N = "Suc (max N_L N_f')"
1047  have "\<bar> \<Sum> i. f' x0 (i + ?N) \<bar> < r/3" (is "?f'_part < r/3")
1048    and L_estimate: "\<bar> \<Sum> i. L (i + ?N) \<bar> < r/3"
1049    using N_L[of "?N"] and N_f' [of "?N"] by auto
1050
1051  let ?diff = "\<lambda>i x. (f (x0 + x) i - f x0 i) / x"
1052
1053  let ?r = "r / (3 * real ?N)"
1054  from \<open>0 < r\<close> have "0 < ?r" by simp
1055
1056  let ?s = "\<lambda>n. SOME s. 0 < s \<and> (\<forall> x. x \<noteq> 0 \<and> \<bar> x \<bar> < s \<longrightarrow> \<bar> ?diff n x - f' x0 n \<bar> < ?r)"
1057  define S' where "S' = Min (?s ` {..< ?N })"
1058
1059  have "0 < S'"
1060    unfolding S'_def
1061  proof (rule iffD2[OF Min_gr_iff])
1062    show "\<forall>x \<in> (?s ` {..< ?N }). 0 < x"
1063    proof
1064      fix x
1065      assume "x \<in> ?s ` {..<?N}"
1066      then obtain n where "x = ?s n" and "n \<in> {..<?N}"
1067        using image_iff[THEN iffD1] by blast
1068      from DERIV_D[OF DERIV_f[where n=n], THEN LIM_D, OF \<open>0 < ?r\<close>, unfolded real_norm_def]
1069      obtain s where s_bound: "0 < s \<and> (\<forall>x. x \<noteq> 0 \<and> \<bar>x\<bar> < s \<longrightarrow> \<bar>?diff n x - f' x0 n\<bar> < ?r)"
1070        by auto
1071      have "0 < ?s n"
1072        by (rule someI2[where a=s]) (auto simp: s_bound simp del: of_nat_Suc)
1073      then show "0 < x" by (simp only: \<open>x = ?s n\<close>)
1074    qed
1075  qed auto
1076
1077  define S where "S = min (min (x0 - a) (b - x0)) S'"
1078  then have "0 < S" and S_a: "S \<le> x0 - a" and S_b: "S \<le> b - x0"
1079    and "S \<le> S'" using x0_in_I and \<open>0 < S'\<close>
1080    by auto
1081
1082  have "\<bar>(suminf (f (x0 + x)) - suminf (f x0)) / x - suminf (f' x0)\<bar> < r"
1083    if "x \<noteq> 0" and "\<bar>x\<bar> < S" for x
1084  proof -
1085    from that have x_in_I: "x0 + x \<in> {a <..< b}"
1086      using S_a S_b by auto
1087
1088    note diff_smbl = summable_diff[OF allf_summable[OF x_in_I] allf_summable[OF x0_in_I]]
1089    note div_smbl = summable_divide[OF diff_smbl]
1090    note all_smbl = summable_diff[OF div_smbl \<open>summable (f' x0)\<close>]
1091    note ign = summable_ignore_initial_segment[where k="?N"]
1092    note diff_shft_smbl = summable_diff[OF ign[OF allf_summable[OF x_in_I]] ign[OF allf_summable[OF x0_in_I]]]
1093    note div_shft_smbl = summable_divide[OF diff_shft_smbl]
1094    note all_shft_smbl = summable_diff[OF div_smbl ign[OF \<open>summable (f' x0)\<close>]]
1095
1096    have 1: "\<bar>(\<bar>?diff (n + ?N) x\<bar>)\<bar> \<le> L (n + ?N)" for n
1097    proof -
1098      have "\<bar>?diff (n + ?N) x\<bar> \<le> L (n + ?N) * \<bar>(x0 + x) - x0\<bar> / \<bar>x\<bar>"
1099        using divide_right_mono[OF L_def[OF x_in_I x0_in_I] abs_ge_zero]
1100        by (simp only: abs_divide)
1101      with \<open>x \<noteq> 0\<close> show ?thesis by auto
1102    qed
1103    note 2 = summable_rabs_comparison_test[OF _ ign[OF \<open>summable L\<close>]]
1104    from 1 have "\<bar> \<Sum> i. ?diff (i + ?N) x \<bar> \<le> (\<Sum> i. L (i + ?N))"
1105      by (metis (lifting) abs_idempotent
1106          order_trans[OF summable_rabs[OF 2] suminf_le[OF _ 2 ign[OF \<open>summable L\<close>]]])
1107    then have "\<bar>\<Sum>i. ?diff (i + ?N) x\<bar> \<le> r / 3" (is "?L_part \<le> r/3")
1108      using L_estimate by auto
1109
1110    have "\<bar>\<Sum>n<?N. ?diff n x - f' x0 n\<bar> \<le> (\<Sum>n<?N. \<bar>?diff n x - f' x0 n\<bar>)" ..
1111    also have "\<dots> < (\<Sum>n<?N. ?r)"
1112    proof (rule sum_strict_mono)
1113      fix n
1114      assume "n \<in> {..< ?N}"
1115      have "\<bar>x\<bar> < S" using \<open>\<bar>x\<bar> < S\<close> .
1116      also have "S \<le> S'" using \<open>S \<le> S'\<close> .
1117      also have "S' \<le> ?s n"
1118        unfolding S'_def
1119      proof (rule Min_le_iff[THEN iffD2])
1120        have "?s n \<in> (?s ` {..<?N}) \<and> ?s n \<le> ?s n"
1121          using \<open>n \<in> {..< ?N}\<close> by auto
1122        then show "\<exists> a \<in> (?s ` {..<?N}). a \<le> ?s n"
1123          by blast
1124      qed auto
1125      finally have "\<bar>x\<bar> < ?s n" .
1126
1127      from DERIV_D[OF DERIV_f[where n=n], THEN LIM_D, OF \<open>0 < ?r\<close>,
1128          unfolded real_norm_def diff_0_right, unfolded some_eq_ex[symmetric], THEN conjunct2]
1129      have "\<forall>x. x \<noteq> 0 \<and> \<bar>x\<bar> < ?s n \<longrightarrow> \<bar>?diff n x - f' x0 n\<bar> < ?r" .
1130      with \<open>x \<noteq> 0\<close> and \<open>\<bar>x\<bar> < ?s n\<close> show "\<bar>?diff n x - f' x0 n\<bar> < ?r"
1131        by blast
1132    qed auto
1133    also have "\<dots> = of_nat (card {..<?N}) * ?r"
1134      by (rule sum_constant)
1135    also have "\<dots> = real ?N * ?r"
1136      by simp
1137    also have "\<dots> = r/3"
1138      by (auto simp del: of_nat_Suc)
1139    finally have "\<bar>\<Sum>n<?N. ?diff n x - f' x0 n \<bar> < r / 3" (is "?diff_part < r / 3") .
1140
1141    from suminf_diff[OF allf_summable[OF x_in_I] allf_summable[OF x0_in_I]]
1142    have "\<bar>(suminf (f (x0 + x)) - (suminf (f x0))) / x - suminf (f' x0)\<bar> =
1143        \<bar>\<Sum>n. ?diff n x - f' x0 n\<bar>"
1144      unfolding suminf_diff[OF div_smbl \<open>summable (f' x0)\<close>, symmetric]
1145      using suminf_divide[OF diff_smbl, symmetric] by auto
1146    also have "\<dots> \<le> ?diff_part + \<bar>(\<Sum>n. ?diff (n + ?N) x) - (\<Sum> n. f' x0 (n + ?N))\<bar>"
1147      unfolding suminf_split_initial_segment[OF all_smbl, where k="?N"]
1148      unfolding suminf_diff[OF div_shft_smbl ign[OF \<open>summable (f' x0)\<close>]]
1149      apply (simp only: add.commute)
1150      using abs_triangle_ineq by blast
1151    also have "\<dots> \<le> ?diff_part + ?L_part + ?f'_part"
1152      using abs_triangle_ineq4 by auto
1153    also have "\<dots> < r /3 + r/3 + r/3"
1154      using \<open>?diff_part < r/3\<close> \<open>?L_part \<le> r/3\<close> and \<open>?f'_part < r/3\<close>
1155      by (rule add_strict_mono [OF add_less_le_mono])
1156    finally show ?thesis
1157      by auto
1158  qed
1159  then show "\<exists>s > 0. \<forall> x. x \<noteq> 0 \<and> norm (x - 0) < s \<longrightarrow>
1160      norm (((\<Sum>n. f (x0 + x) n) - (\<Sum>n. f x0 n)) / x - (\<Sum>n. f' x0 n)) < r"
1161    using \<open>0 < S\<close> by auto
1162qed
1163
1164lemma DERIV_power_series':
1165  fixes f :: "nat \<Rightarrow> real"
1166  assumes converges: "\<And>x. x \<in> {-R <..< R} \<Longrightarrow> summable (\<lambda>n. f n * real (Suc n) * x^n)"
1167    and x0_in_I: "x0 \<in> {-R <..< R}"
1168    and "0 < R"
1169  shows "DERIV (\<lambda>x. (\<Sum>n. f n * x^(Suc n))) x0 :> (\<Sum>n. f n * real (Suc n) * x0^n)"
1170    (is "DERIV (\<lambda>x. suminf (?f x)) x0 :> suminf (?f' x0)")
1171proof -
1172  have for_subinterval: "DERIV (\<lambda>x. suminf (?f x)) x0 :> suminf (?f' x0)"
1173    if "0 < R'" and "R' < R" and "-R' < x0" and "x0 < R'" for R'
1174  proof -
1175    from that have "x0 \<in> {-R' <..< R'}" and "R' \<in> {-R <..< R}" and "x0 \<in> {-R <..< R}"
1176      by auto
1177    show ?thesis
1178    proof (rule DERIV_series')
1179      show "summable (\<lambda> n. \<bar>f n * real (Suc n) * R'^n\<bar>)"
1180      proof -
1181        have "(R' + R) / 2 < R" and "0 < (R' + R) / 2"
1182          using \<open>0 < R'\<close> \<open>0 < R\<close> \<open>R' < R\<close> by (auto simp: field_simps)
1183        then have in_Rball: "(R' + R) / 2 \<in> {-R <..< R}"
1184          using \<open>R' < R\<close> by auto
1185        have "norm R' < norm ((R' + R) / 2)"
1186          using \<open>0 < R'\<close> \<open>0 < R\<close> \<open>R' < R\<close> by (auto simp: field_simps)
1187        from powser_insidea[OF converges[OF in_Rball] this] show ?thesis
1188          by auto
1189      qed
1190    next
1191      fix n x y
1192      assume "x \<in> {-R' <..< R'}" and "y \<in> {-R' <..< R'}"
1193      show "\<bar>?f x n - ?f y n\<bar> \<le> \<bar>f n * real (Suc n) * R'^n\<bar> * \<bar>x-y\<bar>"
1194      proof -
1195        have "\<bar>f n * x ^ (Suc n) - f n * y ^ (Suc n)\<bar> =
1196          (\<bar>f n\<bar> * \<bar>x-y\<bar>) * \<bar>\<Sum>p<Suc n. x ^ p * y ^ (n - p)\<bar>"
1197          unfolding right_diff_distrib[symmetric] diff_power_eq_sum abs_mult
1198          by auto
1199        also have "\<dots> \<le> (\<bar>f n\<bar> * \<bar>x-y\<bar>) * (\<bar>real (Suc n)\<bar> * \<bar>R' ^ n\<bar>)"
1200        proof (rule mult_left_mono)
1201          have "\<bar>\<Sum>p<Suc n. x ^ p * y ^ (n - p)\<bar> \<le> (\<Sum>p<Suc n. \<bar>x ^ p * y ^ (n - p)\<bar>)"
1202            by (rule sum_abs)
1203          also have "\<dots> \<le> (\<Sum>p<Suc n. R' ^ n)"
1204          proof (rule sum_mono)
1205            fix p
1206            assume "p \<in> {..<Suc n}"
1207            then have "p \<le> n" by auto
1208            have "\<bar>x^n\<bar> \<le> R'^n" if  "x \<in> {-R'<..<R'}" for n and x :: real
1209            proof -
1210              from that have "\<bar>x\<bar> \<le> R'" by auto
1211              then show ?thesis
1212                unfolding power_abs by (rule power_mono) auto
1213            qed
1214            from mult_mono[OF this[OF \<open>x \<in> {-R'<..<R'}\<close>, of p] this[OF \<open>y \<in> {-R'<..<R'}\<close>, of "n-p"]]
1215              and \<open>0 < R'\<close>
1216            have "\<bar>x^p * y^(n - p)\<bar> \<le> R'^p * R'^(n - p)"
1217              unfolding abs_mult by auto
1218            then show "\<bar>x^p * y^(n - p)\<bar> \<le> R'^n"
1219              unfolding power_add[symmetric] using \<open>p \<le> n\<close> by auto
1220          qed
1221          also have "\<dots> = real (Suc n) * R' ^ n"
1222            unfolding sum_constant card_atLeastLessThan by auto
1223          finally show "\<bar>\<Sum>p<Suc n. x ^ p * y ^ (n - p)\<bar> \<le> \<bar>real (Suc n)\<bar> * \<bar>R' ^ n\<bar>"
1224            unfolding abs_of_nonneg[OF zero_le_power[OF less_imp_le[OF \<open>0 < R'\<close>]]]
1225            by linarith
1226          show "0 \<le> \<bar>f n\<bar> * \<bar>x - y\<bar>"
1227            unfolding abs_mult[symmetric] by auto
1228        qed
1229        also have "\<dots> = \<bar>f n * real (Suc n) * R' ^ n\<bar> * \<bar>x - y\<bar>"
1230          unfolding abs_mult mult.assoc[symmetric] by algebra
1231        finally show ?thesis .
1232      qed
1233    next
1234      show "DERIV (\<lambda>x. ?f x n) x0 :> ?f' x0 n" for n
1235        by (auto intro!: derivative_eq_intros simp del: power_Suc)
1236    next
1237      fix x
1238      assume "x \<in> {-R' <..< R'}"
1239      then have "R' \<in> {-R <..< R}" and "norm x < norm R'"
1240        using assms \<open>R' < R\<close> by auto
1241      have "summable (\<lambda>n. f n * x^n)"
1242      proof (rule summable_comparison_test, intro exI allI impI)
1243        fix n
1244        have le: "\<bar>f n\<bar> * 1 \<le> \<bar>f n\<bar> * real (Suc n)"
1245          by (rule mult_left_mono) auto
1246        show "norm (f n * x^n) \<le> norm (f n * real (Suc n) * x^n)"
1247          unfolding real_norm_def abs_mult
1248          using le mult_right_mono by fastforce
1249      qed (rule powser_insidea[OF converges[OF \<open>R' \<in> {-R <..< R}\<close>] \<open>norm x < norm R'\<close>])
1250      from this[THEN summable_mult2[where c=x], simplified mult.assoc, simplified mult.commute]
1251      show "summable (?f x)" by auto
1252    next
1253      show "summable (?f' x0)"
1254        using converges[OF \<open>x0 \<in> {-R <..< R}\<close>] .
1255      show "x0 \<in> {-R' <..< R'}"
1256        using \<open>x0 \<in> {-R' <..< R'}\<close> .
1257    qed
1258  qed
1259  let ?R = "(R + \<bar>x0\<bar>) / 2"
1260  have "\<bar>x0\<bar> < ?R"
1261    using assms by (auto simp: field_simps)
1262  then have "- ?R < x0"
1263  proof (cases "x0 < 0")
1264    case True
1265    then have "- x0 < ?R"
1266      using \<open>\<bar>x0\<bar> < ?R\<close> by auto
1267    then show ?thesis
1268      unfolding neg_less_iff_less[symmetric, of "- x0"] by auto
1269  next
1270    case False
1271    have "- ?R < 0" using assms by auto
1272    also have "\<dots> \<le> x0" using False by auto
1273    finally show ?thesis .
1274  qed
1275  then have "0 < ?R" "?R < R" "- ?R < x0" and "x0 < ?R"
1276    using assms by (auto simp: field_simps)
1277  from for_subinterval[OF this] show ?thesis .
1278qed
1279
1280lemma geometric_deriv_sums:
1281  fixes z :: "'a :: {real_normed_field,banach}"
1282  assumes "norm z < 1"
1283  shows   "(\<lambda>n. of_nat (Suc n) * z ^ n) sums (1 / (1 - z)^2)"
1284proof -
1285  have "(\<lambda>n. diffs (\<lambda>n. 1) n * z^n) sums (1 / (1 - z)^2)"
1286  proof (rule termdiffs_sums_strong)
1287    fix z :: 'a assume "norm z < 1"
1288    thus "(\<lambda>n. 1 * z^n) sums (1 / (1 - z))" by (simp add: geometric_sums)
1289  qed (insert assms, auto intro!: derivative_eq_intros simp: power2_eq_square)
1290  thus ?thesis unfolding diffs_def by simp
1291qed
1292
1293lemma isCont_pochhammer [continuous_intros]: "isCont (\<lambda>z. pochhammer z n) z"
1294  for z :: "'a::real_normed_field"
1295  by (induct n) (auto simp: pochhammer_rec')
1296
1297lemma continuous_on_pochhammer [continuous_intros]: "continuous_on A (\<lambda>z. pochhammer z n)"
1298  for A :: "'a::real_normed_field set"
1299  by (intro continuous_at_imp_continuous_on ballI isCont_pochhammer)
1300
1301lemmas continuous_on_pochhammer' [continuous_intros] =
1302  continuous_on_compose2[OF continuous_on_pochhammer _ subset_UNIV]
1303
1304
1305subsection \<open>Exponential Function\<close>
1306
1307definition exp :: "'a \<Rightarrow> 'a::{real_normed_algebra_1,banach}"
1308  where "exp = (\<lambda>x. \<Sum>n. x^n /\<^sub>R fact n)"
1309
1310lemma summable_exp_generic:
1311  fixes x :: "'a::{real_normed_algebra_1,banach}"
1312  defines S_def: "S \<equiv> \<lambda>n. x^n /\<^sub>R fact n"
1313  shows "summable S"
1314proof -
1315  have S_Suc: "\<And>n. S (Suc n) = (x * S n) /\<^sub>R (Suc n)"
1316    unfolding S_def by (simp del: mult_Suc)
1317  obtain r :: real where r0: "0 < r" and r1: "r < 1"
1318    using dense [OF zero_less_one] by fast
1319  obtain N :: nat where N: "norm x < real N * r"
1320    using ex_less_of_nat_mult r0 by auto
1321  from r1 show ?thesis
1322  proof (rule summable_ratio_test [rule_format])
1323    fix n :: nat
1324    assume n: "N \<le> n"
1325    have "norm x \<le> real N * r"
1326      using N by (rule order_less_imp_le)
1327    also have "real N * r \<le> real (Suc n) * r"
1328      using r0 n by (simp add: mult_right_mono)
1329    finally have "norm x * norm (S n) \<le> real (Suc n) * r * norm (S n)"
1330      using norm_ge_zero by (rule mult_right_mono)
1331    then have "norm (x * S n) \<le> real (Suc n) * r * norm (S n)"
1332      by (rule order_trans [OF norm_mult_ineq])
1333    then have "norm (x * S n) / real (Suc n) \<le> r * norm (S n)"
1334      by (simp add: pos_divide_le_eq ac_simps)
1335    then show "norm (S (Suc n)) \<le> r * norm (S n)"
1336      by (simp add: S_Suc inverse_eq_divide)
1337  qed
1338qed
1339
1340lemma summable_norm_exp: "summable (\<lambda>n. norm (x^n /\<^sub>R fact n))"
1341  for x :: "'a::{real_normed_algebra_1,banach}"
1342proof (rule summable_norm_comparison_test [OF exI, rule_format])
1343  show "summable (\<lambda>n. norm x^n /\<^sub>R fact n)"
1344    by (rule summable_exp_generic)
1345  show "norm (x^n /\<^sub>R fact n) \<le> norm x^n /\<^sub>R fact n" for n
1346    by (simp add: norm_power_ineq)
1347qed
1348
1349lemma summable_exp: "summable (\<lambda>n. inverse (fact n) * x^n)"
1350  for x :: "'a::{real_normed_field,banach}"
1351  using summable_exp_generic [where x=x]
1352  by (simp add: scaleR_conv_of_real nonzero_of_real_inverse)
1353
1354lemma exp_converges: "(\<lambda>n. x^n /\<^sub>R fact n) sums exp x"
1355  unfolding exp_def by (rule summable_exp_generic [THEN summable_sums])
1356
1357lemma exp_fdiffs:
1358  "diffs (\<lambda>n. inverse (fact n)) = (\<lambda>n. inverse (fact n :: 'a::{real_normed_field,banach}))"
1359  by (simp add: diffs_def mult_ac nonzero_inverse_mult_distrib nonzero_of_real_inverse
1360      del: mult_Suc of_nat_Suc)
1361
1362lemma diffs_of_real: "diffs (\<lambda>n. of_real (f n)) = (\<lambda>n. of_real (diffs f n))"
1363  by (simp add: diffs_def)
1364
1365lemma DERIV_exp [simp]: "DERIV exp x :> exp x"
1366  unfolding exp_def scaleR_conv_of_real
1367proof (rule DERIV_cong)
1368  have sinv: "summable (\<lambda>n. of_real (inverse (fact n)) * x ^ n)" for x::'a
1369    by (rule exp_converges [THEN sums_summable, unfolded scaleR_conv_of_real])
1370  note xx = exp_converges [THEN sums_summable, unfolded scaleR_conv_of_real]
1371  show "((\<lambda>x. \<Sum>n. of_real (inverse (fact n)) * x ^ n) has_field_derivative
1372        (\<Sum>n. diffs (\<lambda>n. of_real (inverse (fact n))) n * x ^ n))  (at x)"
1373    by (rule termdiffs [where K="of_real (1 + norm x)"]) (simp_all only: diffs_of_real exp_fdiffs sinv norm_of_real)
1374  show "(\<Sum>n. diffs (\<lambda>n. of_real (inverse (fact n))) n * x ^ n) = (\<Sum>n. of_real (inverse (fact n)) * x ^ n)"
1375    by (simp add: diffs_of_real exp_fdiffs)
1376qed
1377
1378declare DERIV_exp[THEN DERIV_chain2, derivative_intros]
1379  and DERIV_exp[THEN DERIV_chain2, unfolded has_field_derivative_def, derivative_intros]
1380
1381lemmas has_derivative_exp[derivative_intros] = DERIV_exp[THEN DERIV_compose_FDERIV]
1382
1383lemma norm_exp: "norm (exp x) \<le> exp (norm x)"
1384proof -
1385  from summable_norm[OF summable_norm_exp, of x]
1386  have "norm (exp x) \<le> (\<Sum>n. inverse (fact n) * norm (x^n))"
1387    by (simp add: exp_def)
1388  also have "\<dots> \<le> exp (norm x)"
1389    using summable_exp_generic[of "norm x"] summable_norm_exp[of x]
1390    by (auto simp: exp_def intro!: suminf_le norm_power_ineq)
1391  finally show ?thesis .
1392qed
1393
1394lemma isCont_exp: "isCont exp x"
1395  for x :: "'a::{real_normed_field,banach}"
1396  by (rule DERIV_exp [THEN DERIV_isCont])
1397
1398lemma isCont_exp' [simp]: "isCont f a \<Longrightarrow> isCont (\<lambda>x. exp (f x)) a"
1399  for f :: "_ \<Rightarrow>'a::{real_normed_field,banach}"
1400  by (rule isCont_o2 [OF _ isCont_exp])
1401
1402lemma tendsto_exp [tendsto_intros]: "(f \<longlongrightarrow> a) F \<Longrightarrow> ((\<lambda>x. exp (f x)) \<longlongrightarrow> exp a) F"
1403  for f:: "_ \<Rightarrow>'a::{real_normed_field,banach}"
1404  by (rule isCont_tendsto_compose [OF isCont_exp])
1405
1406lemma continuous_exp [continuous_intros]: "continuous F f \<Longrightarrow> continuous F (\<lambda>x. exp (f x))"
1407  for f :: "_ \<Rightarrow>'a::{real_normed_field,banach}"
1408  unfolding continuous_def by (rule tendsto_exp)
1409
1410lemma continuous_on_exp [continuous_intros]: "continuous_on s f \<Longrightarrow> continuous_on s (\<lambda>x. exp (f x))"
1411  for f :: "_ \<Rightarrow>'a::{real_normed_field,banach}"
1412  unfolding continuous_on_def by (auto intro: tendsto_exp)
1413
1414
1415subsubsection \<open>Properties of the Exponential Function\<close>
1416
1417lemma exp_zero [simp]: "exp 0 = 1"
1418  unfolding exp_def by (simp add: scaleR_conv_of_real)
1419
1420lemma exp_series_add_commuting:
1421  fixes x y :: "'a::{real_normed_algebra_1,banach}"
1422  defines S_def: "S \<equiv> \<lambda>x n. x^n /\<^sub>R fact n"
1423  assumes comm: "x * y = y * x"
1424  shows "S (x + y) n = (\<Sum>i\<le>n. S x i * S y (n - i))"
1425proof (induct n)
1426  case 0
1427  show ?case
1428    unfolding S_def by simp
1429next
1430  case (Suc n)
1431  have S_Suc: "\<And>x n. S x (Suc n) = (x * S x n) /\<^sub>R real (Suc n)"
1432    unfolding S_def by (simp del: mult_Suc)
1433  then have times_S: "\<And>x n. x * S x n = real (Suc n) *\<^sub>R S x (Suc n)"
1434    by simp
1435  have S_comm: "\<And>n. S x n * y = y * S x n"
1436    by (simp add: power_commuting_commutes comm S_def)
1437
1438  have "real (Suc n) *\<^sub>R S (x + y) (Suc n) = (x + y) * S (x + y) n"
1439    by (simp only: times_S)
1440  also have "\<dots> = (x + y) * (\<Sum>i\<le>n. S x i * S y (n - i))"
1441    by (simp only: Suc)
1442  also have "\<dots> = x * (\<Sum>i\<le>n. S x i * S y (n - i)) + y * (\<Sum>i\<le>n. S x i * S y (n - i))"
1443    by (rule distrib_right)
1444  also have "\<dots> = (\<Sum>i\<le>n. x * S x i * S y (n - i)) + (\<Sum>i\<le>n. S x i * y * S y (n - i))"
1445    by (simp add: sum_distrib_left ac_simps S_comm)
1446  also have "\<dots> = (\<Sum>i\<le>n. x * S x i * S y (n - i)) + (\<Sum>i\<le>n. S x i * (y * S y (n - i)))"
1447    by (simp add: ac_simps)
1448  also have "\<dots> = (\<Sum>i\<le>n. real (Suc i) *\<^sub>R (S x (Suc i) * S y (n - i))) +
1449      (\<Sum>i\<le>n. real (Suc n - i) *\<^sub>R (S x i * S y (Suc n - i)))"
1450    by (simp add: times_S Suc_diff_le)
1451  also have "(\<Sum>i\<le>n. real (Suc i) *\<^sub>R (S x (Suc i) * S y (n - i))) =
1452      (\<Sum>i\<le>Suc n. real i *\<^sub>R (S x i * S y (Suc n - i)))"
1453    by (subst sum_atMost_Suc_shift) simp
1454  also have "(\<Sum>i\<le>n. real (Suc n - i) *\<^sub>R (S x i * S y (Suc n - i))) =
1455      (\<Sum>i\<le>Suc n. real (Suc n - i) *\<^sub>R (S x i * S y (Suc n - i)))"
1456    by simp
1457  also have "(\<Sum>i\<le>Suc n. real i *\<^sub>R (S x i * S y (Suc n - i))) +
1458        (\<Sum>i\<le>Suc n. real (Suc n - i) *\<^sub>R (S x i * S y (Suc n - i))) =
1459      (\<Sum>i\<le>Suc n. real (Suc n) *\<^sub>R (S x i * S y (Suc n - i)))"
1460    by (simp only: sum.distrib [symmetric] scaleR_left_distrib [symmetric]
1461        of_nat_add [symmetric]) simp
1462  also have "\<dots> = real (Suc n) *\<^sub>R (\<Sum>i\<le>Suc n. S x i * S y (Suc n - i))"
1463    by (simp only: scaleR_right.sum)
1464  finally show "S (x + y) (Suc n) = (\<Sum>i\<le>Suc n. S x i * S y (Suc n - i))"
1465    by (simp del: sum_cl_ivl_Suc)
1466qed
1467
1468lemma exp_add_commuting: "x * y = y * x \<Longrightarrow> exp (x + y) = exp x * exp y"
1469  by (simp only: exp_def Cauchy_product summable_norm_exp exp_series_add_commuting)
1470
1471lemma exp_times_arg_commute: "exp A * A = A * exp A"
1472  by (simp add: exp_def suminf_mult[symmetric] summable_exp_generic power_commutes suminf_mult2)
1473
1474lemma exp_add: "exp (x + y) = exp x * exp y"
1475  for x y :: "'a::{real_normed_field,banach}"
1476  by (rule exp_add_commuting) (simp add: ac_simps)
1477
1478lemma exp_double: "exp(2 * z) = exp z ^ 2"
1479  by (simp add: exp_add_commuting mult_2 power2_eq_square)
1480
1481lemmas mult_exp_exp = exp_add [symmetric]
1482
1483lemma exp_of_real: "exp (of_real x) = of_real (exp x)"
1484  unfolding exp_def
1485  apply (subst suminf_of_real [OF summable_exp_generic])
1486  apply (simp add: scaleR_conv_of_real)
1487  done
1488
1489lemmas of_real_exp = exp_of_real[symmetric]
1490
1491corollary exp_in_Reals [simp]: "z \<in> \<real> \<Longrightarrow> exp z \<in> \<real>"
1492  by (metis Reals_cases Reals_of_real exp_of_real)
1493
1494lemma exp_not_eq_zero [simp]: "exp x \<noteq> 0"
1495proof
1496  have "exp x * exp (- x) = 1"
1497    by (simp add: exp_add_commuting[symmetric])
1498  also assume "exp x = 0"
1499  finally show False by simp
1500qed
1501
1502lemma exp_minus_inverse: "exp x * exp (- x) = 1"
1503  by (simp add: exp_add_commuting[symmetric])
1504
1505lemma exp_minus: "exp (- x) = inverse (exp x)"
1506  for x :: "'a::{real_normed_field,banach}"
1507  by (intro inverse_unique [symmetric] exp_minus_inverse)
1508
1509lemma exp_diff: "exp (x - y) = exp x / exp y"
1510  for x :: "'a::{real_normed_field,banach}"
1511  using exp_add [of x "- y"] by (simp add: exp_minus divide_inverse)
1512
1513lemma exp_of_nat_mult: "exp (of_nat n * x) = exp x ^ n"
1514  for x :: "'a::{real_normed_field,banach}"
1515  by (induct n) (auto simp: distrib_left exp_add mult.commute)
1516
1517corollary exp_of_nat2_mult: "exp (x * of_nat n) = exp x ^ n"
1518  for x :: "'a::{real_normed_field,banach}"
1519  by (metis exp_of_nat_mult mult_of_nat_commute)
1520
1521lemma exp_sum: "finite I \<Longrightarrow> exp (sum f I) = prod (\<lambda>x. exp (f x)) I"
1522  by (induct I rule: finite_induct) (auto simp: exp_add_commuting mult.commute)
1523
1524lemma exp_divide_power_eq:
1525  fixes x :: "'a::{real_normed_field,banach}"
1526  assumes "n > 0"
1527  shows "exp (x / of_nat n) ^ n = exp x"
1528  using assms
1529proof (induction n arbitrary: x)
1530  case (Suc n)
1531  show ?case
1532  proof (cases "n = 0")
1533    case True
1534    then show ?thesis by simp
1535  next
1536    case False
1537    then have [simp]: "x * of_nat n / (1 + of_nat n) / of_nat n = x / (1 + of_nat n)"
1538      by simp
1539    have [simp]: "x / (1 + of_nat n) + x * of_nat n / (1 + of_nat n) = x"
1540      apply (simp add: divide_simps)
1541      using of_nat_eq_0_iff apply (fastforce simp: distrib_left)
1542      done
1543    show ?thesis
1544      using Suc.IH [of "x * of_nat n / (1 + of_nat n)"] False
1545      by (simp add: exp_add [symmetric])
1546  qed
1547qed simp
1548
1549
1550subsubsection \<open>Properties of the Exponential Function on Reals\<close>
1551
1552text \<open>Comparisons of @{term "exp x"} with zero.\<close>
1553
1554text \<open>Proof: because every exponential can be seen as a square.\<close>
1555lemma exp_ge_zero [simp]: "0 \<le> exp x"
1556  for x :: real
1557proof -
1558  have "0 \<le> exp (x/2) * exp (x/2)"
1559    by simp
1560  then show ?thesis
1561    by (simp add: exp_add [symmetric])
1562qed
1563
1564lemma exp_gt_zero [simp]: "0 < exp x"
1565  for x :: real
1566  by (simp add: order_less_le)
1567
1568lemma not_exp_less_zero [simp]: "\<not> exp x < 0"
1569  for x :: real
1570  by (simp add: not_less)
1571
1572lemma not_exp_le_zero [simp]: "\<not> exp x \<le> 0"
1573  for x :: real
1574  by (simp add: not_le)
1575
1576lemma abs_exp_cancel [simp]: "\<bar>exp x\<bar> = exp x"
1577  for x :: real
1578  by simp
1579
1580text \<open>Strict monotonicity of exponential.\<close>
1581
1582lemma exp_ge_add_one_self_aux:
1583  fixes x :: real
1584  assumes "0 \<le> x"
1585  shows "1 + x \<le> exp x"
1586  using order_le_imp_less_or_eq [OF assms]
1587proof
1588  assume "0 < x"
1589  have "1 + x \<le> (\<Sum>n<2. inverse (fact n) * x^n)"
1590    by (auto simp: numeral_2_eq_2)
1591  also have "\<dots> \<le> (\<Sum>n. inverse (fact n) * x^n)"
1592    apply (rule sum_le_suminf [OF summable_exp])
1593    using \<open>0 < x\<close>
1594    apply (auto  simp add: zero_le_mult_iff)
1595    done
1596  finally show "1 + x \<le> exp x"
1597    by (simp add: exp_def)
1598qed auto
1599
1600lemma exp_gt_one: "0 < x \<Longrightarrow> 1 < exp x"
1601  for x :: real
1602proof -
1603  assume x: "0 < x"
1604  then have "1 < 1 + x" by simp
1605  also from x have "1 + x \<le> exp x"
1606    by (simp add: exp_ge_add_one_self_aux)
1607  finally show ?thesis .
1608qed
1609
1610lemma exp_less_mono:
1611  fixes x y :: real
1612  assumes "x < y"
1613  shows "exp x < exp y"
1614proof -
1615  from \<open>x < y\<close> have "0 < y - x" by simp
1616  then have "1 < exp (y - x)" by (rule exp_gt_one)
1617  then have "1 < exp y / exp x" by (simp only: exp_diff)
1618  then show "exp x < exp y" by simp
1619qed
1620
1621lemma exp_less_cancel: "exp x < exp y \<Longrightarrow> x < y"
1622  for x y :: real
1623  unfolding linorder_not_le [symmetric]
1624  by (auto simp: order_le_less exp_less_mono)
1625
1626lemma exp_less_cancel_iff [iff]: "exp x < exp y \<longleftrightarrow> x < y"
1627  for x y :: real
1628  by (auto intro: exp_less_mono exp_less_cancel)
1629
1630lemma exp_le_cancel_iff [iff]: "exp x \<le> exp y \<longleftrightarrow> x \<le> y"
1631  for x y :: real
1632  by (auto simp: linorder_not_less [symmetric])
1633
1634lemma exp_inj_iff [iff]: "exp x = exp y \<longleftrightarrow> x = y"
1635  for x y :: real
1636  by (simp add: order_eq_iff)
1637
1638text \<open>Comparisons of @{term "exp x"} with one.\<close>
1639
1640lemma one_less_exp_iff [simp]: "1 < exp x \<longleftrightarrow> 0 < x"
1641  for x :: real
1642  using exp_less_cancel_iff [where x = 0 and y = x] by simp
1643
1644lemma exp_less_one_iff [simp]: "exp x < 1 \<longleftrightarrow> x < 0"
1645  for x :: real
1646  using exp_less_cancel_iff [where x = x and y = 0] by simp
1647
1648lemma one_le_exp_iff [simp]: "1 \<le> exp x \<longleftrightarrow> 0 \<le> x"
1649  for x :: real
1650  using exp_le_cancel_iff [where x = 0 and y = x] by simp
1651
1652lemma exp_le_one_iff [simp]: "exp x \<le> 1 \<longleftrightarrow> x \<le> 0"
1653  for x :: real
1654  using exp_le_cancel_iff [where x = x and y = 0] by simp
1655
1656lemma exp_eq_one_iff [simp]: "exp x = 1 \<longleftrightarrow> x = 0"
1657  for x :: real
1658  using exp_inj_iff [where x = x and y = 0] by simp
1659
1660lemma lemma_exp_total: "1 \<le> y \<Longrightarrow> \<exists>x. 0 \<le> x \<and> x \<le> y - 1 \<and> exp x = y"
1661  for y :: real
1662proof (rule IVT)
1663  assume "1 \<le> y"
1664  then have "0 \<le> y - 1" by simp
1665  then have "1 + (y - 1) \<le> exp (y - 1)"
1666    by (rule exp_ge_add_one_self_aux)
1667  then show "y \<le> exp (y - 1)" by simp
1668qed (simp_all add: le_diff_eq)
1669
1670lemma exp_total: "0 < y \<Longrightarrow> \<exists>x. exp x = y"
1671  for y :: real
1672proof (rule linorder_le_cases [of 1 y])
1673  assume "1 \<le> y"
1674  then show "\<exists>x. exp x = y"
1675    by (fast dest: lemma_exp_total)
1676next
1677  assume "0 < y" and "y \<le> 1"
1678  then have "1 \<le> inverse y"
1679    by (simp add: one_le_inverse_iff)
1680  then obtain x where "exp x = inverse y"
1681    by (fast dest: lemma_exp_total)
1682  then have "exp (- x) = y"
1683    by (simp add: exp_minus)
1684  then show "\<exists>x. exp x = y" ..
1685qed
1686
1687
1688subsection \<open>Natural Logarithm\<close>
1689
1690class ln = real_normed_algebra_1 + banach +
1691  fixes ln :: "'a \<Rightarrow> 'a"
1692  assumes ln_one [simp]: "ln 1 = 0"
1693
1694definition powr :: "'a \<Rightarrow> 'a \<Rightarrow> 'a::ln"  (infixr "powr" 80)
1695  \<comment> \<open>exponentation via ln and exp\<close>
1696  where  [code del]: "x powr a \<equiv> if x = 0 then 0 else exp (a * ln x)"
1697
1698lemma powr_0 [simp]: "0 powr z = 0"
1699  by (simp add: powr_def)
1700
1701
1702instantiation real :: ln
1703begin
1704
1705definition ln_real :: "real \<Rightarrow> real"
1706  where "ln_real x = (THE u. exp u = x)"
1707
1708instance
1709  by intro_classes (simp add: ln_real_def)
1710
1711end
1712
1713lemma powr_eq_0_iff [simp]: "w powr z = 0 \<longleftrightarrow> w = 0"
1714  by (simp add: powr_def)
1715
1716lemma ln_exp [simp]: "ln (exp x) = x"
1717  for x :: real
1718  by (simp add: ln_real_def)
1719
1720lemma exp_ln [simp]: "0 < x \<Longrightarrow> exp (ln x) = x"
1721  for x :: real
1722  by (auto dest: exp_total)
1723
1724lemma exp_ln_iff [simp]: "exp (ln x) = x \<longleftrightarrow> 0 < x"
1725  for x :: real
1726  by (metis exp_gt_zero exp_ln)
1727
1728lemma ln_unique: "exp y = x \<Longrightarrow> ln x = y"
1729  for x :: real
1730  by (erule subst) (rule ln_exp)
1731
1732lemma ln_mult: "0 < x \<Longrightarrow> 0 < y \<Longrightarrow> ln (x * y) = ln x + ln y"
1733  for x :: real
1734  by (rule ln_unique) (simp add: exp_add)
1735
1736lemma ln_prod: "finite I \<Longrightarrow> (\<And>i. i \<in> I \<Longrightarrow> f i > 0) \<Longrightarrow> ln (prod f I) = sum (\<lambda>x. ln(f x)) I"
1737  for f :: "'a \<Rightarrow> real"
1738  by (induct I rule: finite_induct) (auto simp: ln_mult prod_pos)
1739
1740lemma ln_inverse: "0 < x \<Longrightarrow> ln (inverse x) = - ln x"
1741  for x :: real
1742  by (rule ln_unique) (simp add: exp_minus)
1743
1744lemma ln_div: "0 < x \<Longrightarrow> 0 < y \<Longrightarrow> ln (x / y) = ln x - ln y"
1745  for x :: real
1746  by (rule ln_unique) (simp add: exp_diff)
1747
1748lemma ln_realpow: "0 < x \<Longrightarrow> ln (x^n) = real n * ln x"
1749  by (rule ln_unique) (simp add: exp_of_nat_mult)
1750
1751lemma ln_less_cancel_iff [simp]: "0 < x \<Longrightarrow> 0 < y \<Longrightarrow> ln x < ln y \<longleftrightarrow> x < y"
1752  for x :: real
1753  by (subst exp_less_cancel_iff [symmetric]) simp
1754
1755lemma ln_le_cancel_iff [simp]: "0 < x \<Longrightarrow> 0 < y \<Longrightarrow> ln x \<le> ln y \<longleftrightarrow> x \<le> y"
1756  for x :: real
1757  by (simp add: linorder_not_less [symmetric])
1758
1759lemma ln_inj_iff [simp]: "0 < x \<Longrightarrow> 0 < y \<Longrightarrow> ln x = ln y \<longleftrightarrow> x = y"
1760  for x :: real
1761  by (simp add: order_eq_iff)
1762
1763lemma ln_add_one_self_le_self: "0 \<le> x \<Longrightarrow> ln (1 + x) \<le> x"
1764  for x :: real
1765  by (rule exp_le_cancel_iff [THEN iffD1]) (simp add: exp_ge_add_one_self_aux)
1766
1767lemma ln_less_self [simp]: "0 < x \<Longrightarrow> ln x < x"
1768  for x :: real
1769  by (rule order_less_le_trans [where y = "ln (1 + x)"]) (simp_all add: ln_add_one_self_le_self)
1770
1771lemma ln_ge_iff: "\<And>x::real. 0 < x \<Longrightarrow> y \<le> ln x \<longleftrightarrow> exp y \<le> x"
1772  using exp_le_cancel_iff exp_total by force
1773
1774lemma ln_ge_zero [simp]: "1 \<le> x \<Longrightarrow> 0 \<le> ln x"
1775  for x :: real
1776  using ln_le_cancel_iff [of 1 x] by simp
1777
1778lemma ln_ge_zero_imp_ge_one: "0 \<le> ln x \<Longrightarrow> 0 < x \<Longrightarrow> 1 \<le> x"
1779  for x :: real
1780  using ln_le_cancel_iff [of 1 x] by simp
1781
1782lemma ln_ge_zero_iff [simp]: "0 < x \<Longrightarrow> 0 \<le> ln x \<longleftrightarrow> 1 \<le> x"
1783  for x :: real
1784  using ln_le_cancel_iff [of 1 x] by simp
1785
1786lemma ln_less_zero_iff [simp]: "0 < x \<Longrightarrow> ln x < 0 \<longleftrightarrow> x < 1"
1787  for x :: real
1788  using ln_less_cancel_iff [of x 1] by simp
1789
1790lemma ln_le_zero_iff [simp]: "0 < x \<Longrightarrow> ln x \<le> 0 \<longleftrightarrow> x \<le> 1"
1791  for x :: real
1792  by (metis less_numeral_extra(1) ln_le_cancel_iff ln_one)
1793
1794lemma ln_gt_zero: "1 < x \<Longrightarrow> 0 < ln x"
1795  for x :: real
1796  using ln_less_cancel_iff [of 1 x] by simp
1797
1798lemma ln_gt_zero_imp_gt_one: "0 < ln x \<Longrightarrow> 0 < x \<Longrightarrow> 1 < x"
1799  for x :: real
1800  using ln_less_cancel_iff [of 1 x] by simp
1801
1802lemma ln_gt_zero_iff [simp]: "0 < x \<Longrightarrow> 0 < ln x \<longleftrightarrow> 1 < x"
1803  for x :: real
1804  using ln_less_cancel_iff [of 1 x] by simp
1805
1806lemma ln_eq_zero_iff [simp]: "0 < x \<Longrightarrow> ln x = 0 \<longleftrightarrow> x = 1"
1807  for x :: real
1808  using ln_inj_iff [of x 1] by simp
1809
1810lemma ln_less_zero: "0 < x \<Longrightarrow> x < 1 \<Longrightarrow> ln x < 0"
1811  for x :: real
1812  by simp
1813
1814lemma ln_neg_is_const: "x \<le> 0 \<Longrightarrow> ln x = (THE x. False)"
1815  for x :: real
1816  by (auto simp: ln_real_def intro!: arg_cong[where f = The])
1817
1818lemma isCont_ln:
1819  fixes x :: real
1820  assumes "x \<noteq> 0"
1821  shows "isCont ln x"
1822proof (cases "0 < x")
1823  case True
1824  then have "isCont ln (exp (ln x))"
1825    by (intro isCont_inverse_function[where d = "\<bar>x\<bar>" and f = exp]) auto
1826  with True show ?thesis
1827    by simp
1828next
1829  case False
1830  with \<open>x \<noteq> 0\<close> show "isCont ln x"
1831    unfolding isCont_def
1832    by (subst filterlim_cong[OF _ refl, of _ "nhds (ln 0)" _ "\<lambda>_. ln 0"])
1833       (auto simp: ln_neg_is_const not_less eventually_at dist_real_def
1834         intro!: exI[of _ "\<bar>x\<bar>"])
1835qed
1836
1837lemma tendsto_ln [tendsto_intros]: "(f \<longlongrightarrow> a) F \<Longrightarrow> a \<noteq> 0 \<Longrightarrow> ((\<lambda>x. ln (f x)) \<longlongrightarrow> ln a) F"
1838  for a :: real
1839  by (rule isCont_tendsto_compose [OF isCont_ln])
1840
1841lemma continuous_ln:
1842  "continuous F f \<Longrightarrow> f (Lim F (\<lambda>x. x)) \<noteq> 0 \<Longrightarrow> continuous F (\<lambda>x. ln (f x :: real))"
1843  unfolding continuous_def by (rule tendsto_ln)
1844
1845lemma isCont_ln' [continuous_intros]:
1846  "continuous (at x) f \<Longrightarrow> f x \<noteq> 0 \<Longrightarrow> continuous (at x) (\<lambda>x. ln (f x :: real))"
1847  unfolding continuous_at by (rule tendsto_ln)
1848
1849lemma continuous_within_ln [continuous_intros]:
1850  "continuous (at x within s) f \<Longrightarrow> f x \<noteq> 0 \<Longrightarrow> continuous (at x within s) (\<lambda>x. ln (f x :: real))"
1851  unfolding continuous_within by (rule tendsto_ln)
1852
1853lemma continuous_on_ln [continuous_intros]:
1854  "continuous_on s f \<Longrightarrow> (\<forall>x\<in>s. f x \<noteq> 0) \<Longrightarrow> continuous_on s (\<lambda>x. ln (f x :: real))"
1855  unfolding continuous_on_def by (auto intro: tendsto_ln)
1856
1857lemma DERIV_ln: "0 < x \<Longrightarrow> DERIV ln x :> inverse x"
1858  for x :: real
1859  by (rule DERIV_inverse_function [where f=exp and a=0 and b="x+1"])
1860    (auto intro: DERIV_cong [OF DERIV_exp exp_ln] isCont_ln)
1861
1862lemma DERIV_ln_divide: "0 < x \<Longrightarrow> DERIV ln x :> 1 / x"
1863  for x :: real
1864  by (rule DERIV_ln[THEN DERIV_cong]) (simp_all add: divide_inverse)
1865
1866declare DERIV_ln_divide[THEN DERIV_chain2, derivative_intros]
1867  and DERIV_ln_divide[THEN DERIV_chain2, unfolded has_field_derivative_def, derivative_intros]
1868
1869lemmas has_derivative_ln[derivative_intros] = DERIV_ln[THEN DERIV_compose_FDERIV]
1870
1871lemma ln_series:
1872  assumes "0 < x" and "x < 2"
1873  shows "ln x = (\<Sum> n. (-1)^n * (1 / real (n + 1)) * (x - 1)^(Suc n))"
1874    (is "ln x = suminf (?f (x - 1))")
1875proof -
1876  let ?f' = "\<lambda>x n. (-1)^n * (x - 1)^n"
1877
1878  have "ln x - suminf (?f (x - 1)) = ln 1 - suminf (?f (1 - 1))"
1879  proof (rule DERIV_isconst3 [where x = x])
1880    fix x :: real
1881    assume "x \<in> {0 <..< 2}"
1882    then have "0 < x" and "x < 2" by auto
1883    have "norm (1 - x) < 1"
1884      using \<open>0 < x\<close> and \<open>x < 2\<close> by auto
1885    have "1 / x = 1 / (1 - (1 - x))" by auto
1886    also have "\<dots> = (\<Sum> n. (1 - x)^n)"
1887      using geometric_sums[OF \<open>norm (1 - x) < 1\<close>] by (rule sums_unique)
1888    also have "\<dots> = suminf (?f' x)"
1889      unfolding power_mult_distrib[symmetric]
1890      by (rule arg_cong[where f=suminf], rule arg_cong[where f="(^)"], auto)
1891    finally have "DERIV ln x :> suminf (?f' x)"
1892      using DERIV_ln[OF \<open>0 < x\<close>] unfolding divide_inverse by auto
1893    moreover
1894    have repos: "\<And> h x :: real. h - 1 + x = h + x - 1" by auto
1895    have "DERIV (\<lambda>x. suminf (?f x)) (x - 1) :>
1896      (\<Sum>n. (-1)^n * (1 / real (n + 1)) * real (Suc n) * (x - 1) ^ n)"
1897    proof (rule DERIV_power_series')
1898      show "x - 1 \<in> {- 1<..<1}" and "(0 :: real) < 1"
1899        using \<open>0 < x\<close> \<open>x < 2\<close> by auto
1900    next
1901      fix x :: real
1902      assume "x \<in> {- 1<..<1}"
1903      then have "norm (-x) < 1" by auto
1904      show "summable (\<lambda>n. (- 1) ^ n * (1 / real (n + 1)) * real (Suc n) * x^n)"
1905        unfolding One_nat_def
1906        by (auto simp: power_mult_distrib[symmetric] summable_geometric[OF \<open>norm (-x) < 1\<close>])
1907    qed
1908    then have "DERIV (\<lambda>x. suminf (?f x)) (x - 1) :> suminf (?f' x)"
1909      unfolding One_nat_def by auto
1910    then have "DERIV (\<lambda>x. suminf (?f (x - 1))) x :> suminf (?f' x)"
1911      unfolding DERIV_def repos .
1912    ultimately have "DERIV (\<lambda>x. ln x - suminf (?f (x - 1))) x :> suminf (?f' x) - suminf (?f' x)"
1913      by (rule DERIV_diff)
1914    then show "DERIV (\<lambda>x. ln x - suminf (?f (x - 1))) x :> 0" by auto
1915  qed (auto simp: assms)
1916  then show ?thesis by auto
1917qed
1918
1919lemma exp_first_terms:
1920  fixes x :: "'a::{real_normed_algebra_1,banach}"
1921  shows "exp x = (\<Sum>n<k. inverse(fact n) *\<^sub>R (x ^ n)) + (\<Sum>n. inverse(fact (n + k)) *\<^sub>R (x ^ (n + k)))"
1922proof -
1923  have "exp x = suminf (\<lambda>n. inverse(fact n) *\<^sub>R (x^n))"
1924    by (simp add: exp_def)
1925  also from summable_exp_generic have "\<dots> = (\<Sum> n. inverse(fact(n+k)) *\<^sub>R (x ^ (n + k))) +
1926    (\<Sum> n::nat<k. inverse(fact n) *\<^sub>R (x^n))" (is "_ = _ + ?a")
1927    by (rule suminf_split_initial_segment)
1928  finally show ?thesis by simp
1929qed
1930
1931lemma exp_first_term: "exp x = 1 + (\<Sum>n. inverse (fact (Suc n)) *\<^sub>R (x ^ Suc n))"
1932  for x :: "'a::{real_normed_algebra_1,banach}"
1933  using exp_first_terms[of x 1] by simp
1934
1935lemma exp_first_two_terms: "exp x = 1 + x + (\<Sum>n. inverse (fact (n + 2)) *\<^sub>R (x ^ (n + 2)))"
1936  for x :: "'a::{real_normed_algebra_1,banach}"
1937  using exp_first_terms[of x 2] by (simp add: eval_nat_numeral)
1938
1939lemma exp_bound:
1940  fixes x :: real
1941  assumes a: "0 \<le> x"
1942    and b: "x \<le> 1"
1943  shows "exp x \<le> 1 + x + x\<^sup>2"
1944proof -
1945  have "suminf (\<lambda>n. inverse(fact (n+2)) * (x ^ (n + 2))) \<le> x\<^sup>2"
1946  proof -
1947    have "(\<lambda>n. x\<^sup>2 / 2 * (1 / 2) ^ n) sums (x\<^sup>2 / 2 * (1 / (1 - 1 / 2)))"
1948      by (intro sums_mult geometric_sums) simp
1949    then have sumsx: "(\<lambda>n. x\<^sup>2 / 2 * (1 / 2) ^ n) sums x\<^sup>2"
1950      by simp
1951    have "suminf (\<lambda>n. inverse(fact (n+2)) * (x ^ (n + 2))) \<le> suminf (\<lambda>n. (x\<^sup>2/2) * ((1/2)^n))"
1952    proof (intro suminf_le allI)
1953      show "inverse (fact (n + 2)) * x ^ (n + 2) \<le> (x\<^sup>2/2) * ((1/2)^n)" for n :: nat
1954      proof -
1955        have "(2::nat) * 2 ^ n \<le> fact (n + 2)"
1956          by (induct n) simp_all
1957        then have "real ((2::nat) * 2 ^ n) \<le> real_of_nat (fact (n + 2))"
1958          by (simp only: of_nat_le_iff)
1959        then have "((2::real) * 2 ^ n) \<le> fact (n + 2)"
1960          unfolding of_nat_fact by simp
1961        then have "inverse (fact (n + 2)) \<le> inverse ((2::real) * 2 ^ n)"
1962          by (rule le_imp_inverse_le) simp
1963        then have "inverse (fact (n + 2)) \<le> 1/(2::real) * (1/2)^n"
1964          by (simp add: power_inverse [symmetric])
1965        then have "inverse (fact (n + 2)) * (x^n * x\<^sup>2) \<le> 1/2 * (1/2)^n * (1 * x\<^sup>2)"
1966          by (rule mult_mono) (rule mult_mono, simp_all add: power_le_one a b)
1967        then show ?thesis
1968          unfolding power_add by (simp add: ac_simps del: fact_Suc)
1969      qed
1970      show "summable (\<lambda>n. inverse (fact (n + 2)) * x ^ (n + 2))"
1971        by (rule summable_exp [THEN summable_ignore_initial_segment])
1972      show "summable (\<lambda>n. x\<^sup>2 / 2 * (1 / 2) ^ n)"
1973        by (rule sums_summable [OF sumsx])
1974    qed
1975    also have "\<dots> = x\<^sup>2"
1976      by (rule sums_unique [THEN sym]) (rule sumsx)
1977    finally show ?thesis .
1978  qed
1979  then show ?thesis
1980    unfolding exp_first_two_terms by auto
1981qed
1982
1983corollary exp_half_le2: "exp(1/2) \<le> (2::real)"
1984  using exp_bound [of "1/2"]
1985  by (simp add: field_simps)
1986
1987corollary exp_le: "exp 1 \<le> (3::real)"
1988  using exp_bound [of 1]
1989  by (simp add: field_simps)
1990
1991lemma exp_bound_half: "norm z \<le> 1/2 \<Longrightarrow> norm (exp z) \<le> 2"
1992  by (blast intro: order_trans intro!: exp_half_le2 norm_exp)
1993
1994lemma exp_bound_lemma:
1995  assumes "norm z \<le> 1/2"
1996  shows "norm (exp z) \<le> 1 + 2 * norm z"
1997proof -
1998  have *: "(norm z)\<^sup>2 \<le> norm z * 1"
1999    unfolding power2_eq_square
2000    by (rule mult_left_mono) (use assms in auto)
2001  have "norm (exp z) \<le> exp (norm z)"
2002    by (rule norm_exp)
2003  also have "\<dots> \<le> 1 + (norm z) + (norm z)\<^sup>2"
2004    using assms exp_bound by auto
2005  also have "\<dots> \<le> 1 + 2 * norm z"
2006    using * by auto
2007  finally show ?thesis .
2008qed
2009
2010lemma real_exp_bound_lemma: "0 \<le> x \<Longrightarrow> x \<le> 1/2 \<Longrightarrow> exp x \<le> 1 + 2 * x"
2011  for x :: real
2012  using exp_bound_lemma [of x] by simp
2013
2014lemma ln_one_minus_pos_upper_bound:
2015  fixes x :: real
2016  assumes a: "0 \<le> x" and b: "x < 1"
2017  shows "ln (1 - x) \<le> - x"
2018proof -
2019  have "(1 - x) * (1 + x + x\<^sup>2) = 1 - x^3"
2020    by (simp add: algebra_simps power2_eq_square power3_eq_cube)
2021  also have "\<dots> \<le> 1"
2022    by (auto simp: a)
2023  finally have "(1 - x) * (1 + x + x\<^sup>2) \<le> 1" .
2024  moreover have c: "0 < 1 + x + x\<^sup>2"
2025    by (simp add: add_pos_nonneg a)
2026  ultimately have "1 - x \<le> 1 / (1 + x + x\<^sup>2)"
2027    by (elim mult_imp_le_div_pos)
2028  also have "\<dots> \<le> 1 / exp x"
2029    by (metis a abs_one b exp_bound exp_gt_zero frac_le less_eq_real_def real_sqrt_abs
2030        real_sqrt_pow2_iff real_sqrt_power)
2031  also have "\<dots> = exp (- x)"
2032    by (auto simp: exp_minus divide_inverse)
2033  finally have "1 - x \<le> exp (- x)" .
2034  also have "1 - x = exp (ln (1 - x))"
2035    by (metis b diff_0 exp_ln_iff less_iff_diff_less_0 minus_diff_eq)
2036  finally have "exp (ln (1 - x)) \<le> exp (- x)" .
2037  then show ?thesis
2038    by (auto simp only: exp_le_cancel_iff)
2039qed
2040
2041lemma exp_ge_add_one_self [simp]: "1 + x \<le> exp x"
2042  for x :: real
2043proof (cases "0 \<le> x \<or> x \<le> -1")
2044  case True
2045  then show ?thesis
2046    apply (rule disjE)
2047     apply (simp add: exp_ge_add_one_self_aux)
2048    using exp_ge_zero order_trans real_add_le_0_iff by blast
2049next
2050  case False
2051  then have ln1: "ln (1 + x) \<le> x"
2052    using ln_one_minus_pos_upper_bound [of "-x"] by simp
2053  have "1 + x = exp (ln (1 + x))"
2054    using False by auto
2055  also have "\<dots> \<le> exp x"
2056    by (simp add: ln1)
2057  finally show ?thesis .
2058qed
2059
2060lemma ln_one_plus_pos_lower_bound:
2061  fixes x :: real
2062  assumes a: "0 \<le> x" and b: "x \<le> 1"
2063  shows "x - x\<^sup>2 \<le> ln (1 + x)"
2064proof -
2065  have "exp (x - x\<^sup>2) = exp x / exp (x\<^sup>2)"
2066    by (rule exp_diff)
2067  also have "\<dots> \<le> (1 + x + x\<^sup>2) / exp (x \<^sup>2)"
2068    by (metis a b divide_right_mono exp_bound exp_ge_zero)
2069  also have "\<dots> \<le> (1 + x + x\<^sup>2) / (1 + x\<^sup>2)"
2070    by (simp add: a divide_left_mono add_pos_nonneg)
2071  also from a have "\<dots> \<le> 1 + x"
2072    by (simp add: field_simps add_strict_increasing zero_le_mult_iff)
2073  finally have "exp (x - x\<^sup>2) \<le> 1 + x" .
2074  also have "\<dots> = exp (ln (1 + x))"
2075  proof -
2076    from a have "0 < 1 + x" by auto
2077    then show ?thesis
2078      by (auto simp only: exp_ln_iff [THEN sym])
2079  qed
2080  finally have "exp (x - x\<^sup>2) \<le> exp (ln (1 + x))" .
2081  then show ?thesis
2082    by (metis exp_le_cancel_iff)
2083qed
2084
2085lemma ln_one_minus_pos_lower_bound:
2086  fixes x :: real
2087  assumes a: "0 \<le> x" and b: "x \<le> 1 / 2"
2088  shows "- x - 2 * x\<^sup>2 \<le> ln (1 - x)"
2089proof -
2090  from b have c: "x < 1" by auto
2091  then have "ln (1 - x) = - ln (1 + x / (1 - x))"
2092    by (auto simp: ln_inverse [symmetric] field_simps intro: arg_cong [where f=ln])
2093  also have "- (x / (1 - x)) \<le> \<dots>"
2094  proof -
2095    have "ln (1 + x / (1 - x)) \<le> x / (1 - x)"
2096      using a c by (intro ln_add_one_self_le_self) auto
2097    then show ?thesis
2098      by auto
2099  qed
2100  also have "- (x / (1 - x)) = - x / (1 - x)"
2101    by auto
2102  finally have d: "- x / (1 - x) \<le> ln (1 - x)" .
2103  have "0 < 1 - x" using a b by simp
2104  then have e: "- x - 2 * x\<^sup>2 \<le> - x / (1 - x)"
2105    using mult_right_le_one_le[of "x * x" "2 * x"] a b
2106    by (simp add: field_simps power2_eq_square)
2107  from e d show "- x - 2 * x\<^sup>2 \<le> ln (1 - x)"
2108    by (rule order_trans)
2109qed
2110
2111lemma ln_add_one_self_le_self2:
2112  fixes x :: real
2113  shows "-1 < x \<Longrightarrow> ln (1 + x) \<le> x"
2114  by (metis diff_gt_0_iff_gt diff_minus_eq_add exp_ge_add_one_self exp_le_cancel_iff exp_ln minus_less_iff)
2115
2116lemma abs_ln_one_plus_x_minus_x_bound_nonneg:
2117  fixes x :: real
2118  assumes x: "0 \<le> x" and x1: "x \<le> 1"
2119  shows "\<bar>ln (1 + x) - x\<bar> \<le> x\<^sup>2"
2120proof -
2121  from x have "ln (1 + x) \<le> x"
2122    by (rule ln_add_one_self_le_self)
2123  then have "ln (1 + x) - x \<le> 0"
2124    by simp
2125  then have "\<bar>ln(1 + x) - x\<bar> = - (ln(1 + x) - x)"
2126    by (rule abs_of_nonpos)
2127  also have "\<dots> = x - ln (1 + x)"
2128    by simp
2129  also have "\<dots> \<le> x\<^sup>2"
2130  proof -
2131    from x x1 have "x - x\<^sup>2 \<le> ln (1 + x)"
2132      by (intro ln_one_plus_pos_lower_bound)
2133    then show ?thesis
2134      by simp
2135  qed
2136  finally show ?thesis .
2137qed
2138
2139lemma abs_ln_one_plus_x_minus_x_bound_nonpos:
2140  fixes x :: real
2141  assumes a: "-(1 / 2) \<le> x" and b: "x \<le> 0"
2142  shows "\<bar>ln (1 + x) - x\<bar> \<le> 2 * x\<^sup>2"
2143proof -
2144  have *: "- (-x) - 2 * (-x)\<^sup>2 \<le> ln (1 - (- x))"
2145    by (metis a b diff_zero ln_one_minus_pos_lower_bound minus_diff_eq neg_le_iff_le) 
2146  have "\<bar>ln (1 + x) - x\<bar> = x - ln (1 - (- x))"
2147    using a ln_add_one_self_le_self2 [of x] by (simp add: abs_if)
2148  also have "\<dots> \<le> 2 * x\<^sup>2"
2149    using * by (simp add: algebra_simps)
2150  finally show ?thesis .
2151qed
2152
2153lemma abs_ln_one_plus_x_minus_x_bound:
2154  fixes x :: real
2155  assumes "\<bar>x\<bar> \<le> 1 / 2"
2156  shows "\<bar>ln (1 + x) - x\<bar> \<le> 2 * x\<^sup>2"
2157proof (cases "0 \<le> x")
2158  case True
2159  then show ?thesis
2160    using abs_ln_one_plus_x_minus_x_bound_nonneg assms by fastforce
2161next
2162  case False
2163  then show ?thesis
2164    using abs_ln_one_plus_x_minus_x_bound_nonpos assms by auto
2165qed
2166
2167lemma ln_x_over_x_mono:
2168  fixes x :: real
2169  assumes x: "exp 1 \<le> x" "x \<le> y"
2170  shows "ln y / y \<le> ln x / x"
2171proof -
2172  note x
2173  moreover have "0 < exp (1::real)" by simp
2174  ultimately have a: "0 < x" and b: "0 < y"
2175    by (fast intro: less_le_trans order_trans)+
2176  have "x * ln y - x * ln x = x * (ln y - ln x)"
2177    by (simp add: algebra_simps)
2178  also have "\<dots> = x * ln (y / x)"
2179    by (simp only: ln_div a b)
2180  also have "y / x = (x + (y - x)) / x"
2181    by simp
2182  also have "\<dots> = 1 + (y - x) / x"
2183    using x a by (simp add: field_simps)
2184  also have "x * ln (1 + (y - x) / x) \<le> x * ((y - x) / x)"
2185    using x a
2186    by (intro mult_left_mono ln_add_one_self_le_self) simp_all
2187  also have "\<dots> = y - x"
2188    using a by simp
2189  also have "\<dots> = (y - x) * ln (exp 1)" by simp
2190  also have "\<dots> \<le> (y - x) * ln x"
2191    using a x exp_total of_nat_1 x(1)  by (fastforce intro: mult_left_mono)
2192  also have "\<dots> = y * ln x - x * ln x"
2193    by (rule left_diff_distrib)
2194  finally have "x * ln y \<le> y * ln x"
2195    by arith
2196  then have "ln y \<le> (y * ln x) / x"
2197    using a by (simp add: field_simps)
2198  also have "\<dots> = y * (ln x / x)" by simp
2199  finally show ?thesis
2200    using b by (simp add: field_simps)
2201qed
2202
2203lemma ln_le_minus_one: "0 < x \<Longrightarrow> ln x \<le> x - 1"
2204  for x :: real
2205  using exp_ge_add_one_self[of "ln x"] by simp
2206
2207corollary ln_diff_le: "0 < x \<Longrightarrow> 0 < y \<Longrightarrow> ln x - ln y \<le> (x - y) / y"
2208  for x :: real
2209  by (simp add: ln_div [symmetric] diff_divide_distrib ln_le_minus_one)
2210
2211lemma ln_eq_minus_one:
2212  fixes x :: real
2213  assumes "0 < x" "ln x = x - 1"
2214  shows "x = 1"
2215proof -
2216  let ?l = "\<lambda>y. ln y - y + 1"
2217  have D: "\<And>x::real. 0 < x \<Longrightarrow> DERIV ?l x :> (1 / x - 1)"
2218    by (auto intro!: derivative_eq_intros)
2219
2220  show ?thesis
2221  proof (cases rule: linorder_cases)
2222    assume "x < 1"
2223    from dense[OF \<open>x < 1\<close>] obtain a where "x < a" "a < 1" by blast
2224    from \<open>x < a\<close> have "?l x < ?l a"
2225    proof (rule DERIV_pos_imp_increasing, safe)
2226      fix y
2227      assume "x \<le> y" "y \<le> a"
2228      with \<open>0 < x\<close> \<open>a < 1\<close> have "0 < 1 / y - 1" "0 < y"
2229        by (auto simp: field_simps)
2230      with D show "\<exists>z. DERIV ?l y :> z \<and> 0 < z" by blast
2231    qed
2232    also have "\<dots> \<le> 0"
2233      using ln_le_minus_one \<open>0 < x\<close> \<open>x < a\<close> by (auto simp: field_simps)
2234    finally show "x = 1" using assms by auto
2235  next
2236    assume "1 < x"
2237    from dense[OF this] obtain a where "1 < a" "a < x" by blast
2238    from \<open>a < x\<close> have "?l x < ?l a"
2239    proof (rule DERIV_neg_imp_decreasing)
2240      fix y
2241      assume "a \<le> y" "y \<le> x"
2242      with \<open>1 < a\<close> have "1 / y - 1 < 0" "0 < y"
2243        by (auto simp: field_simps)
2244      with D show "\<exists>z. DERIV ?l y :> z \<and> z < 0"
2245        by blast
2246    qed
2247    also have "\<dots> \<le> 0"
2248      using ln_le_minus_one \<open>1 < a\<close> by (auto simp: field_simps)
2249    finally show "x = 1" using assms by auto
2250  next
2251    assume "x = 1"
2252    then show ?thesis by simp
2253  qed
2254qed
2255
2256lemma ln_x_over_x_tendsto_0: "((\<lambda>x::real. ln x / x) \<longlongrightarrow> 0) at_top"
2257proof (rule lhospital_at_top_at_top[where f' = inverse and g' = "\<lambda>_. 1"])
2258  from eventually_gt_at_top[of "0::real"]
2259  show "\<forall>\<^sub>F x in at_top. (ln has_real_derivative inverse x) (at x)"
2260    by eventually_elim (auto intro!: derivative_eq_intros simp: field_simps)
2261qed (use tendsto_inverse_0 in
2262      \<open>auto simp: filterlim_ident dest!: tendsto_mono[OF at_top_le_at_infinity]\<close>)
2263
2264lemma exp_ge_one_plus_x_over_n_power_n:
2265  assumes "x \<ge> - real n" "n > 0"
2266  shows "(1 + x / of_nat n) ^ n \<le> exp x"
2267proof (cases "x = - of_nat n")
2268  case False
2269  from assms False have "(1 + x / of_nat n) ^ n = exp (of_nat n * ln (1 + x / of_nat n))"
2270    by (subst exp_of_nat_mult, subst exp_ln) (simp_all add: field_simps)
2271  also from assms False have "ln (1 + x / real n) \<le> x / real n"
2272    by (intro ln_add_one_self_le_self2) (simp_all add: field_simps)
2273  with assms have "exp (of_nat n * ln (1 + x / of_nat n)) \<le> exp x"
2274    by (simp add: field_simps)
2275  finally show ?thesis .
2276next
2277  case True
2278  then show ?thesis by (simp add: zero_power)
2279qed
2280
2281lemma exp_ge_one_minus_x_over_n_power_n:
2282  assumes "x \<le> real n" "n > 0"
2283  shows "(1 - x / of_nat n) ^ n \<le> exp (-x)"
2284  using exp_ge_one_plus_x_over_n_power_n[of n "-x"] assms by simp
2285
2286lemma exp_at_bot: "(exp \<longlongrightarrow> (0::real)) at_bot"
2287  unfolding tendsto_Zfun_iff
2288proof (rule ZfunI, simp add: eventually_at_bot_dense)
2289  fix r :: real
2290  assume "0 < r"
2291  have "exp x < r" if "x < ln r" for x
2292    by (metis \<open>0 < r\<close> exp_less_mono exp_ln that)
2293  then show "\<exists>k. \<forall>n<k. exp n < r" by auto
2294qed
2295
2296lemma exp_at_top: "LIM x at_top. exp x :: real :> at_top"
2297  by (rule filterlim_at_top_at_top[where Q="\<lambda>x. True" and P="\<lambda>x. 0 < x" and g=ln])
2298    (auto intro: eventually_gt_at_top)
2299
2300lemma lim_exp_minus_1: "((\<lambda>z::'a. (exp(z) - 1) / z) \<longlongrightarrow> 1) (at 0)"
2301  for x :: "'a::{real_normed_field,banach}"
2302proof -
2303  have "((\<lambda>z::'a. exp(z) - 1) has_field_derivative 1) (at 0)"
2304    by (intro derivative_eq_intros | simp)+
2305  then show ?thesis
2306    by (simp add: Deriv.has_field_derivative_iff)
2307qed
2308
2309lemma ln_at_0: "LIM x at_right 0. ln (x::real) :> at_bot"
2310  by (rule filterlim_at_bot_at_right[where Q="\<lambda>x. 0 < x" and P="\<lambda>x. True" and g=exp])
2311     (auto simp: eventually_at_filter)
2312
2313lemma ln_at_top: "LIM x at_top. ln (x::real) :> at_top"
2314  by (rule filterlim_at_top_at_top[where Q="\<lambda>x. 0 < x" and P="\<lambda>x. True" and g=exp])
2315     (auto intro: eventually_gt_at_top)
2316
2317lemma filtermap_ln_at_top: "filtermap (ln::real \<Rightarrow> real) at_top = at_top"
2318  by (intro filtermap_fun_inverse[of exp] exp_at_top ln_at_top) auto
2319
2320lemma filtermap_exp_at_top: "filtermap (exp::real \<Rightarrow> real) at_top = at_top"
2321  by (intro filtermap_fun_inverse[of ln] exp_at_top ln_at_top)
2322     (auto simp: eventually_at_top_dense)
2323
2324lemma filtermap_ln_at_right: "filtermap ln (at_right (0::real)) = at_bot"
2325  by (auto intro!: filtermap_fun_inverse[where g="\<lambda>x. exp x"] ln_at_0
2326      simp: filterlim_at exp_at_bot)
2327
2328lemma tendsto_power_div_exp_0: "((\<lambda>x. x ^ k / exp x) \<longlongrightarrow> (0::real)) at_top"
2329proof (induct k)
2330  case 0
2331  show "((\<lambda>x. x ^ 0 / exp x) \<longlongrightarrow> (0::real)) at_top"
2332    by (simp add: inverse_eq_divide[symmetric])
2333       (metis filterlim_compose[OF tendsto_inverse_0] exp_at_top filterlim_mono
2334         at_top_le_at_infinity order_refl)
2335next
2336  case (Suc k)
2337  show ?case
2338  proof (rule lhospital_at_top_at_top)
2339    show "eventually (\<lambda>x. DERIV (\<lambda>x. x ^ Suc k) x :> (real (Suc k) * x^k)) at_top"
2340      by eventually_elim (intro derivative_eq_intros, auto)
2341    show "eventually (\<lambda>x. DERIV exp x :> exp x) at_top"
2342      by eventually_elim auto
2343    show "eventually (\<lambda>x. exp x \<noteq> 0) at_top"
2344      by auto
2345    from tendsto_mult[OF tendsto_const Suc, of "real (Suc k)"]
2346    show "((\<lambda>x. real (Suc k) * x ^ k / exp x) \<longlongrightarrow> 0) at_top"
2347      by simp
2348  qed (rule exp_at_top)
2349qed
2350
2351subsubsection\<open> A couple of simple bounds\<close>
2352
2353lemma exp_plus_inverse_exp:
2354  fixes x::real
2355  shows "2 \<le> exp x + inverse (exp x)"
2356proof -
2357  have "2 \<le> exp x + exp (-x)"
2358    using exp_ge_add_one_self [of x] exp_ge_add_one_self [of "-x"]
2359    by linarith
2360  then show ?thesis
2361    by (simp add: exp_minus)
2362qed
2363
2364lemma real_le_x_sinh:
2365  fixes x::real
2366  assumes "0 \<le> x"
2367  shows "x \<le> (exp x - inverse(exp x)) / 2"
2368proof -
2369  have *: "exp a - inverse(exp a) - 2*a \<le> exp b - inverse(exp b) - 2*b" if "a \<le> b" for a b::real
2370    using exp_plus_inverse_exp
2371    by (fastforce intro: derivative_eq_intros DERIV_nonneg_imp_nondecreasing [OF that])
2372  show ?thesis
2373    using*[OF assms] by simp
2374qed
2375
2376lemma real_le_abs_sinh:
2377  fixes x::real
2378  shows "abs x \<le> abs((exp x - inverse(exp x)) / 2)"
2379proof (cases "0 \<le> x")
2380  case True
2381  show ?thesis
2382    using real_le_x_sinh [OF True] True by (simp add: abs_if)
2383next
2384  case False
2385  have "-x \<le> (exp(-x) - inverse(exp(-x))) / 2"
2386    by (meson False linear neg_le_0_iff_le real_le_x_sinh)
2387  also have "\<dots> \<le> \<bar>(exp x - inverse (exp x)) / 2\<bar>"
2388    by (metis (no_types, hide_lams) abs_divide abs_le_iff abs_minus_cancel
2389       add.inverse_inverse exp_minus minus_diff_eq order_refl)
2390  finally show ?thesis
2391    using False by linarith
2392qed
2393
2394subsection\<open>The general logarithm\<close>
2395
2396definition log :: "real \<Rightarrow> real \<Rightarrow> real"
2397  \<comment> \<open>logarithm of @{term x} to base @{term a}\<close>
2398  where "log a x = ln x / ln a"
2399
2400lemma tendsto_log [tendsto_intros]:
2401  "(f \<longlongrightarrow> a) F \<Longrightarrow> (g \<longlongrightarrow> b) F \<Longrightarrow> 0 < a \<Longrightarrow> a \<noteq> 1 \<Longrightarrow> 0 < b \<Longrightarrow>
2402    ((\<lambda>x. log (f x) (g x)) \<longlongrightarrow> log a b) F"
2403  unfolding log_def by (intro tendsto_intros) auto
2404
2405lemma continuous_log:
2406  assumes "continuous F f"
2407    and "continuous F g"
2408    and "0 < f (Lim F (\<lambda>x. x))"
2409    and "f (Lim F (\<lambda>x. x)) \<noteq> 1"
2410    and "0 < g (Lim F (\<lambda>x. x))"
2411  shows "continuous F (\<lambda>x. log (f x) (g x))"
2412  using assms unfolding continuous_def by (rule tendsto_log)
2413
2414lemma continuous_at_within_log[continuous_intros]:
2415  assumes "continuous (at a within s) f"
2416    and "continuous (at a within s) g"
2417    and "0 < f a"
2418    and "f a \<noteq> 1"
2419    and "0 < g a"
2420  shows "continuous (at a within s) (\<lambda>x. log (f x) (g x))"
2421  using assms unfolding continuous_within by (rule tendsto_log)
2422
2423lemma isCont_log[continuous_intros, simp]:
2424  assumes "isCont f a" "isCont g a" "0 < f a" "f a \<noteq> 1" "0 < g a"
2425  shows "isCont (\<lambda>x. log (f x) (g x)) a"
2426  using assms unfolding continuous_at by (rule tendsto_log)
2427
2428lemma continuous_on_log[continuous_intros]:
2429  assumes "continuous_on s f" "continuous_on s g"
2430    and "\<forall>x\<in>s. 0 < f x" "\<forall>x\<in>s. f x \<noteq> 1" "\<forall>x\<in>s. 0 < g x"
2431  shows "continuous_on s (\<lambda>x. log (f x) (g x))"
2432  using assms unfolding continuous_on_def by (fast intro: tendsto_log)
2433
2434lemma powr_one_eq_one [simp]: "1 powr a = 1"
2435  by (simp add: powr_def)
2436
2437lemma powr_zero_eq_one [simp]: "x powr 0 = (if x = 0 then 0 else 1)"
2438  by (simp add: powr_def)
2439
2440lemma powr_one_gt_zero_iff [simp]: "x powr 1 = x \<longleftrightarrow> 0 \<le> x"
2441  for x :: real
2442  by (auto simp: powr_def)
2443declare powr_one_gt_zero_iff [THEN iffD2, simp]
2444
2445lemma powr_diff:
2446  fixes w:: "'a::{ln,real_normed_field}" shows  "w powr (z1 - z2) = w powr z1 / w powr z2"
2447  by (simp add: powr_def algebra_simps exp_diff)
2448
2449lemma powr_mult: "0 \<le> x \<Longrightarrow> 0 \<le> y \<Longrightarrow> (x * y) powr a = (x powr a) * (y powr a)"
2450  for a x y :: real
2451  by (simp add: powr_def exp_add [symmetric] ln_mult distrib_left)
2452
2453lemma powr_ge_pzero [simp]: "0 \<le> x powr y"
2454  for x y :: real
2455  by (simp add: powr_def)
2456
2457lemma powr_non_neg[simp]: "\<not>a powr x < 0" for a x::real
2458  using powr_ge_pzero[of a x] by arith
2459
2460lemma powr_divide: "0 < x \<Longrightarrow> 0 < y \<Longrightarrow> (x / y) powr a = (x powr a) / (y powr a)"
2461  for a b x :: real
2462  apply (simp add: divide_inverse positive_imp_inverse_positive powr_mult)
2463  apply (simp add: powr_def exp_minus [symmetric] exp_add [symmetric] ln_inverse)
2464  done
2465
2466lemma powr_add: "x powr (a + b) = (x powr a) * (x powr b)"
2467  for a b x :: "'a::{ln,real_normed_field}"
2468  by (simp add: powr_def exp_add [symmetric] distrib_right)
2469
2470lemma powr_mult_base: "0 < x \<Longrightarrow>x * x powr y = x powr (1 + y)"
2471  for x :: real
2472  by (auto simp: powr_add)
2473
2474lemma powr_powr: "(x powr a) powr b = x powr (a * b)"
2475  for a b x :: real
2476  by (simp add: powr_def)
2477
2478lemma powr_powr_swap: "(x powr a) powr b = (x powr b) powr a"
2479  for a b x :: real
2480  by (simp add: powr_powr mult.commute)
2481
2482lemma powr_minus: "x powr (- a) = inverse (x powr a)"
2483      for a x :: "'a::{ln,real_normed_field}"
2484  by (simp add: powr_def exp_minus [symmetric])
2485
2486lemma powr_minus_divide: "x powr (- a) = 1/(x powr a)"
2487      for a x :: "'a::{ln,real_normed_field}"
2488  by (simp add: divide_inverse powr_minus)
2489
2490lemma divide_powr_uminus: "a / b powr c = a * b powr (- c)"
2491  for a b c :: real
2492  by (simp add: powr_minus_divide)
2493
2494lemma powr_less_mono: "a < b \<Longrightarrow> 1 < x \<Longrightarrow> x powr a < x powr b"
2495  for a b x :: real
2496  by (simp add: powr_def)
2497
2498lemma powr_less_cancel: "x powr a < x powr b \<Longrightarrow> 1 < x \<Longrightarrow> a < b"
2499  for a b x :: real
2500  by (simp add: powr_def)
2501
2502lemma powr_less_cancel_iff [simp]: "1 < x \<Longrightarrow> x powr a < x powr b \<longleftrightarrow> a < b"
2503  for a b x :: real
2504  by (blast intro: powr_less_cancel powr_less_mono)
2505
2506lemma powr_le_cancel_iff [simp]: "1 < x \<Longrightarrow> x powr a \<le> x powr b \<longleftrightarrow> a \<le> b"
2507  for a b x :: real
2508  by (simp add: linorder_not_less [symmetric])
2509
2510lemma powr_realpow: "0 < x \<Longrightarrow> x powr (real n) = x^n"
2511by (induction n) (simp_all add: ac_simps powr_add)
2512
2513lemma log_ln: "ln x = log (exp(1)) x"
2514  by (simp add: log_def)
2515
2516lemma DERIV_log:
2517  assumes "x > 0"
2518  shows "DERIV (\<lambda>y. log b y) x :> 1 / (ln b * x)"
2519proof -
2520  define lb where "lb = 1 / ln b"
2521  moreover have "DERIV (\<lambda>y. lb * ln y) x :> lb / x"
2522    using \<open>x > 0\<close> by (auto intro!: derivative_eq_intros)
2523  ultimately show ?thesis
2524    by (simp add: log_def)
2525qed
2526
2527lemmas DERIV_log[THEN DERIV_chain2, derivative_intros]
2528  and DERIV_log[THEN DERIV_chain2, unfolded has_field_derivative_def, derivative_intros]
2529
2530lemma powr_log_cancel [simp]: "0 < a \<Longrightarrow> a \<noteq> 1 \<Longrightarrow> 0 < x \<Longrightarrow> a powr (log a x) = x"
2531  by (simp add: powr_def log_def)
2532
2533lemma log_powr_cancel [simp]: "0 < a \<Longrightarrow> a \<noteq> 1 \<Longrightarrow> log a (a powr y) = y"
2534  by (simp add: log_def powr_def)
2535
2536lemma log_mult:
2537  "0 < a \<Longrightarrow> a \<noteq> 1 \<Longrightarrow> 0 < x \<Longrightarrow> 0 < y \<Longrightarrow>
2538    log a (x * y) = log a x + log a y"
2539  by (simp add: log_def ln_mult divide_inverse distrib_right)
2540
2541lemma log_eq_div_ln_mult_log:
2542  "0 < a \<Longrightarrow> a \<noteq> 1 \<Longrightarrow> 0 < b \<Longrightarrow> b \<noteq> 1 \<Longrightarrow> 0 < x \<Longrightarrow>
2543    log a x = (ln b/ln a) * log b x"
2544  by (simp add: log_def divide_inverse)
2545
2546text\<open>Base 10 logarithms\<close>
2547lemma log_base_10_eq1: "0 < x \<Longrightarrow> log 10 x = (ln (exp 1) / ln 10) * ln x"
2548  by (simp add: log_def)
2549
2550lemma log_base_10_eq2: "0 < x \<Longrightarrow> log 10 x = (log 10 (exp 1)) * ln x"
2551  by (simp add: log_def)
2552
2553lemma log_one [simp]: "log a 1 = 0"
2554  by (simp add: log_def)
2555
2556lemma log_eq_one [simp]: "0 < a \<Longrightarrow> a \<noteq> 1 \<Longrightarrow> log a a = 1"
2557  by (simp add: log_def)
2558
2559lemma log_inverse: "0 < a \<Longrightarrow> a \<noteq> 1 \<Longrightarrow> 0 < x \<Longrightarrow> log a (inverse x) = - log a x"
2560  using ln_inverse log_def by auto
2561
2562lemma log_divide: "0 < a \<Longrightarrow> a \<noteq> 1 \<Longrightarrow> 0 < x \<Longrightarrow> 0 < y \<Longrightarrow> log a (x/y) = log a x - log a y"
2563  by (simp add: log_mult divide_inverse log_inverse)
2564
2565lemma powr_gt_zero [simp]: "0 < x powr a \<longleftrightarrow> x \<noteq> 0"
2566  for a x :: real
2567  by (simp add: powr_def)
2568
2569lemma powr_nonneg_iff[simp]: "a powr x \<le> 0 \<longleftrightarrow> a = 0"
2570  for a x::real
2571  by (meson not_less powr_gt_zero)
2572
2573lemma log_add_eq_powr: "0 < b \<Longrightarrow> b \<noteq> 1 \<Longrightarrow> 0 < x \<Longrightarrow> log b x + y = log b (x * b powr y)"
2574  and add_log_eq_powr: "0 < b \<Longrightarrow> b \<noteq> 1 \<Longrightarrow> 0 < x \<Longrightarrow> y + log b x = log b (b powr y * x)"
2575  and log_minus_eq_powr: "0 < b \<Longrightarrow> b \<noteq> 1 \<Longrightarrow> 0 < x \<Longrightarrow> log b x - y = log b (x * b powr -y)"
2576  and minus_log_eq_powr: "0 < b \<Longrightarrow> b \<noteq> 1 \<Longrightarrow> 0 < x \<Longrightarrow> y - log b x = log b (b powr y / x)"
2577  by (simp_all add: log_mult log_divide)
2578
2579lemma log_less_cancel_iff [simp]: "1 < a \<Longrightarrow> 0 < x \<Longrightarrow> 0 < y \<Longrightarrow> log a x < log a y \<longleftrightarrow> x < y"
2580  using powr_less_cancel_iff [of a] powr_log_cancel [of a x] powr_log_cancel [of a y]
2581  by (metis less_eq_real_def less_trans not_le zero_less_one)
2582
2583lemma log_inj:
2584  assumes "1 < b"
2585  shows "inj_on (log b) {0 <..}"
2586proof (rule inj_onI, simp)
2587  fix x y
2588  assume pos: "0 < x" "0 < y" and *: "log b x = log b y"
2589  show "x = y"
2590  proof (cases rule: linorder_cases)
2591    assume "x = y"
2592    then show ?thesis by simp
2593  next
2594    assume "x < y"
2595    then have "log b x < log b y"
2596      using log_less_cancel_iff[OF \<open>1 < b\<close>] pos by simp
2597    then show ?thesis using * by simp
2598  next
2599    assume "y < x"
2600    then have "log b y < log b x"
2601      using log_less_cancel_iff[OF \<open>1 < b\<close>] pos by simp
2602    then show ?thesis using * by simp
2603  qed
2604qed
2605
2606lemma log_le_cancel_iff [simp]: "1 < a \<Longrightarrow> 0 < x \<Longrightarrow> 0 < y \<Longrightarrow> log a x \<le> log a y \<longleftrightarrow> x \<le> y"
2607  by (simp add: linorder_not_less [symmetric])
2608
2609lemma zero_less_log_cancel_iff[simp]: "1 < a \<Longrightarrow> 0 < x \<Longrightarrow> 0 < log a x \<longleftrightarrow> 1 < x"
2610  using log_less_cancel_iff[of a 1 x] by simp
2611
2612lemma zero_le_log_cancel_iff[simp]: "1 < a \<Longrightarrow> 0 < x \<Longrightarrow> 0 \<le> log a x \<longleftrightarrow> 1 \<le> x"
2613  using log_le_cancel_iff[of a 1 x] by simp
2614
2615lemma log_less_zero_cancel_iff[simp]: "1 < a \<Longrightarrow> 0 < x \<Longrightarrow> log a x < 0 \<longleftrightarrow> x < 1"
2616  using log_less_cancel_iff[of a x 1] by simp
2617
2618lemma log_le_zero_cancel_iff[simp]: "1 < a \<Longrightarrow> 0 < x \<Longrightarrow> log a x \<le> 0 \<longleftrightarrow> x \<le> 1"
2619  using log_le_cancel_iff[of a x 1] by simp
2620
2621lemma one_less_log_cancel_iff[simp]: "1 < a \<Longrightarrow> 0 < x \<Longrightarrow> 1 < log a x \<longleftrightarrow> a < x"
2622  using log_less_cancel_iff[of a a x] by simp
2623
2624lemma one_le_log_cancel_iff[simp]: "1 < a \<Longrightarrow> 0 < x \<Longrightarrow> 1 \<le> log a x \<longleftrightarrow> a \<le> x"
2625  using log_le_cancel_iff[of a a x] by simp
2626
2627lemma log_less_one_cancel_iff[simp]: "1 < a \<Longrightarrow> 0 < x \<Longrightarrow> log a x < 1 \<longleftrightarrow> x < a"
2628  using log_less_cancel_iff[of a x a] by simp
2629
2630lemma log_le_one_cancel_iff[simp]: "1 < a \<Longrightarrow> 0 < x \<Longrightarrow> log a x \<le> 1 \<longleftrightarrow> x \<le> a"
2631  using log_le_cancel_iff[of a x a] by simp
2632
2633lemma le_log_iff:
2634  fixes b x y :: real
2635  assumes "1 < b" "x > 0"
2636  shows "y \<le> log b x \<longleftrightarrow> b powr y \<le> x"
2637  using assms
2638  by (metis less_irrefl less_trans powr_le_cancel_iff powr_log_cancel zero_less_one)
2639
2640lemma less_log_iff:
2641  assumes "1 < b" "x > 0"
2642  shows "y < log b x \<longleftrightarrow> b powr y < x"
2643  by (metis assms dual_order.strict_trans less_irrefl powr_less_cancel_iff
2644    powr_log_cancel zero_less_one)
2645
2646lemma
2647  assumes "1 < b" "x > 0"
2648  shows log_less_iff: "log b x < y \<longleftrightarrow> x < b powr y"
2649    and log_le_iff: "log b x \<le> y \<longleftrightarrow> x \<le> b powr y"
2650  using le_log_iff[OF assms, of y] less_log_iff[OF assms, of y]
2651  by auto
2652
2653lemmas powr_le_iff = le_log_iff[symmetric]
2654  and powr_less_iff = less_log_iff[symmetric]
2655  and less_powr_iff = log_less_iff[symmetric]
2656  and le_powr_iff = log_le_iff[symmetric]
2657
2658lemma le_log_of_power:
2659  assumes "b ^ n \<le> m" "1 < b"
2660  shows "n \<le> log b m"
2661proof -
2662  from assms have "0 < m" by (metis less_trans zero_less_power less_le_trans zero_less_one)
2663  thus ?thesis using assms by (simp add: le_log_iff powr_realpow)
2664qed
2665
2666lemma le_log2_of_power: "2 ^ n \<le> m \<Longrightarrow> n \<le> log 2 m" for m n :: nat
2667using le_log_of_power[of 2] by simp
2668
2669lemma log_of_power_le: "\<lbrakk> m \<le> b ^ n; b > 1; m > 0 \<rbrakk> \<Longrightarrow> log b (real m) \<le> n"
2670by (simp add: log_le_iff powr_realpow)
2671
2672lemma log2_of_power_le: "\<lbrakk> m \<le> 2 ^ n; m > 0 \<rbrakk> \<Longrightarrow> log 2 m \<le> n" for m n :: nat
2673using log_of_power_le[of _ 2] by simp
2674
2675lemma log_of_power_less: "\<lbrakk> m < b ^ n; b > 1; m > 0 \<rbrakk> \<Longrightarrow> log b (real m) < n"
2676by (simp add: log_less_iff powr_realpow)
2677
2678lemma log2_of_power_less: "\<lbrakk> m < 2 ^ n; m > 0 \<rbrakk> \<Longrightarrow> log 2 m < n" for m n :: nat
2679using log_of_power_less[of _ 2] by simp
2680
2681lemma less_log_of_power:
2682  assumes "b ^ n < m" "1 < b"
2683  shows "n < log b m"
2684proof -
2685  have "0 < m" by (metis assms less_trans zero_less_power zero_less_one)
2686  thus ?thesis using assms by (simp add: less_log_iff powr_realpow)
2687qed
2688
2689lemma less_log2_of_power: "2 ^ n < m \<Longrightarrow> n < log 2 m" for m n :: nat
2690using less_log_of_power[of 2] by simp
2691
2692lemma gr_one_powr[simp]:
2693  fixes x y :: real shows "\<lbrakk> x > 1; y > 0 \<rbrakk> \<Longrightarrow> 1 < x powr y"
2694by(simp add: less_powr_iff)
2695
2696lemma floor_log_eq_powr_iff: "x > 0 \<Longrightarrow> b > 1 \<Longrightarrow> \<lfloor>log b x\<rfloor> = k \<longleftrightarrow> b powr k \<le> x \<and> x < b powr (k + 1)"
2697  by (auto simp: floor_eq_iff powr_le_iff less_powr_iff)
2698
2699lemma floor_log_nat_eq_powr_iff: fixes b n k :: nat
2700  shows "\<lbrakk> b \<ge> 2; k > 0 \<rbrakk> \<Longrightarrow>
2701  floor (log b (real k)) = n \<longleftrightarrow> b^n \<le> k \<and> k < b^(n+1)"
2702by (auto simp: floor_log_eq_powr_iff powr_add powr_realpow
2703               of_nat_power[symmetric] of_nat_mult[symmetric] ac_simps
2704         simp del: of_nat_power of_nat_mult)
2705
2706lemma floor_log_nat_eq_if: fixes b n k :: nat
2707  assumes "b^n \<le> k" "k < b^(n+1)" "b \<ge> 2"
2708  shows "floor (log b (real k)) = n"
2709proof -
2710  have "k \<ge> 1" using assms(1,3) one_le_power[of b n] by linarith
2711  with assms show ?thesis by(simp add: floor_log_nat_eq_powr_iff)
2712qed
2713
2714lemma ceiling_log_eq_powr_iff: "\<lbrakk> x > 0; b > 1 \<rbrakk>
2715  \<Longrightarrow> \<lceil>log b x\<rceil> = int k + 1 \<longleftrightarrow> b powr k < x \<and> x \<le> b powr (k + 1)"
2716by (auto simp: ceiling_eq_iff powr_less_iff le_powr_iff)
2717
2718lemma ceiling_log_nat_eq_powr_iff: fixes b n k :: nat
2719  shows "\<lbrakk> b \<ge> 2; k > 0 \<rbrakk> \<Longrightarrow>
2720  ceiling (log b (real k)) = int n + 1 \<longleftrightarrow> (b^n < k \<and> k \<le> b^(n+1))"
2721using ceiling_log_eq_powr_iff
2722by (auto simp: powr_add powr_realpow of_nat_power[symmetric] of_nat_mult[symmetric] ac_simps
2723         simp del: of_nat_power of_nat_mult)
2724
2725lemma ceiling_log_nat_eq_if: fixes b n k :: nat
2726  assumes "b^n < k" "k \<le> b^(n+1)" "b \<ge> 2"
2727  shows "ceiling (log b (real k)) = int n + 1"
2728proof -
2729  have "k \<ge> 1" using assms(1,3) one_le_power[of b n] by linarith
2730  with assms show ?thesis by(simp add: ceiling_log_nat_eq_powr_iff)
2731qed
2732
2733lemma floor_log2_div2: fixes n :: nat assumes "n \<ge> 2"
2734shows "floor(log 2 n) = floor(log 2 (n div 2)) + 1"
2735proof cases
2736  assume "n=2" thus ?thesis by simp
2737next
2738  let ?m = "n div 2"
2739  assume "n\<noteq>2"
2740  hence "1 \<le> ?m" using assms by arith
2741  then obtain i where i: "2 ^ i \<le> ?m" "?m < 2 ^ (i + 1)"
2742    using ex_power_ivl1[of 2 ?m] by auto
2743  have "2^(i+1) \<le> 2*?m" using i(1) by simp
2744  also have "2*?m \<le> n" by arith
2745  finally have *: "2^(i+1) \<le> \<dots>" .
2746  have "n < 2^(i+1+1)" using i(2) by simp
2747  from floor_log_nat_eq_if[OF * this] floor_log_nat_eq_if[OF i]
2748  show ?thesis by simp
2749qed
2750
2751lemma ceiling_log2_div2: assumes "n \<ge> 2"
2752shows "ceiling(log 2 (real n)) = ceiling(log 2 ((n-1) div 2 + 1)) + 1"
2753proof cases
2754  assume "n=2" thus ?thesis by simp
2755next
2756  let ?m = "(n-1) div 2 + 1"
2757  assume "n\<noteq>2"
2758  hence "2 \<le> ?m" using assms by arith
2759  then obtain i where i: "2 ^ i < ?m" "?m \<le> 2 ^ (i + 1)"
2760    using ex_power_ivl2[of 2 ?m] by auto
2761  have "n \<le> 2*?m" by arith
2762  also have "2*?m \<le> 2 ^ ((i+1)+1)" using i(2) by simp
2763  finally have *: "n \<le> \<dots>" .
2764  have "2^(i+1) < n" using i(1) by (auto simp: less_Suc_eq_0_disj)
2765  from ceiling_log_nat_eq_if[OF this *] ceiling_log_nat_eq_if[OF i]
2766  show ?thesis by simp
2767qed
2768
2769lemma powr_real_of_int:
2770  "x > 0 \<Longrightarrow> x powr real_of_int n = (if n \<ge> 0 then x ^ nat n else inverse (x ^ nat (- n)))"
2771  using powr_realpow[of x "nat n"] powr_realpow[of x "nat (-n)"]
2772  by (auto simp: field_simps powr_minus)
2773
2774lemma powr_numeral [simp]: "0 < x \<Longrightarrow> x powr (numeral n :: real) = x ^ (numeral n)"
2775  by (metis of_nat_numeral powr_realpow)
2776
2777lemma powr_int:
2778  assumes "x > 0"
2779  shows "x powr i = (if i \<ge> 0 then x ^ nat i else 1 / x ^ nat (-i))"
2780proof (cases "i < 0")
2781  case True
2782  have r: "x powr i = 1 / x powr (- i)"
2783    by (simp add: powr_minus field_simps)
2784  show ?thesis using \<open>i < 0\<close> \<open>x > 0\<close>
2785    by (simp add: r field_simps powr_realpow[symmetric])
2786next
2787  case False
2788  then show ?thesis
2789    by (simp add: assms powr_realpow[symmetric])
2790qed
2791
2792lemma compute_powr[code]:
2793  fixes i :: real
2794  shows "b powr i =
2795    (if b \<le> 0 then Code.abort (STR ''op powr with nonpositive base'') (\<lambda>_. b powr i)
2796     else if \<lfloor>i\<rfloor> = i then (if 0 \<le> i then b ^ nat \<lfloor>i\<rfloor> else 1 / b ^ nat \<lfloor>- i\<rfloor>)
2797     else Code.abort (STR ''op powr with non-integer exponent'') (\<lambda>_. b powr i))"
2798  by (auto simp: powr_int)
2799
2800lemma powr_one: "0 \<le> x \<Longrightarrow> x powr 1 = x"
2801  for x :: real
2802  using powr_realpow [of x 1] by simp
2803
2804lemma powr_neg_one: "0 < x \<Longrightarrow> x powr - 1 = 1 / x"
2805  for x :: real
2806  using powr_int [of x "- 1"] by simp
2807
2808lemma powr_neg_numeral: "0 < x \<Longrightarrow> x powr - numeral n = 1 / x ^ numeral n"
2809  for x :: real
2810  using powr_int [of x "- numeral n"] by simp
2811
2812lemma root_powr_inverse: "0 < n \<Longrightarrow> 0 < x \<Longrightarrow> root n x = x powr (1/n)"
2813  by (rule real_root_pos_unique) (auto simp: powr_realpow[symmetric] powr_powr)
2814
2815lemma ln_powr: "x \<noteq> 0 \<Longrightarrow> ln (x powr y) = y * ln x"
2816  for x :: real
2817  by (simp add: powr_def)
2818
2819lemma ln_root: "n > 0 \<Longrightarrow> b > 0 \<Longrightarrow> ln (root n b) =  ln b / n"
2820  by (simp add: root_powr_inverse ln_powr)
2821
2822lemma ln_sqrt: "0 < x \<Longrightarrow> ln (sqrt x) = ln x / 2"
2823  by (simp add: ln_powr ln_powr[symmetric] mult.commute)
2824
2825lemma log_root: "n > 0 \<Longrightarrow> a > 0 \<Longrightarrow> log b (root n a) =  log b a / n"
2826  by (simp add: log_def ln_root)
2827
2828lemma log_powr: "x \<noteq> 0 \<Longrightarrow> log b (x powr y) = y * log b x"
2829  by (simp add: log_def ln_powr)
2830
2831(* [simp] is not worth it, interferes with some proofs *)
2832lemma log_nat_power: "0 < x \<Longrightarrow> log b (x^n) = real n * log b x"
2833  by (simp add: log_powr powr_realpow [symmetric])
2834
2835lemma log_of_power_eq:
2836  assumes "m = b ^ n" "b > 1"
2837  shows "n = log b (real m)"
2838proof -
2839  have "n = log b (b ^ n)" using assms(2) by (simp add: log_nat_power)
2840  also have "\<dots> = log b m" using assms by simp
2841  finally show ?thesis .
2842qed
2843
2844lemma log2_of_power_eq: "m = 2 ^ n \<Longrightarrow> n = log 2 m" for m n :: nat
2845using log_of_power_eq[of _ 2] by simp
2846
2847lemma log_base_change: "0 < a \<Longrightarrow> a \<noteq> 1 \<Longrightarrow> log b x = log a x / log a b"
2848  by (simp add: log_def)
2849
2850lemma log_base_pow: "0 < a \<Longrightarrow> log (a ^ n) x = log a x / n"
2851  by (simp add: log_def ln_realpow)
2852
2853lemma log_base_powr: "a \<noteq> 0 \<Longrightarrow> log (a powr b) x = log a x / b"
2854  by (simp add: log_def ln_powr)
2855
2856lemma log_base_root: "n > 0 \<Longrightarrow> b > 0 \<Longrightarrow> log (root n b) x = n * (log b x)"
2857  by (simp add: log_def ln_root)
2858
2859lemma ln_bound: "0 < x \<Longrightarrow> ln x \<le> x" for x :: real
2860  using ln_le_minus_one by force
2861
2862lemma powr_mono:
2863  fixes x :: real
2864  assumes "a \<le> b" and "1 \<le> x" shows "x powr a \<le> x powr b"
2865  using assms less_eq_real_def by auto
2866
2867lemma ge_one_powr_ge_zero: "1 \<le> x \<Longrightarrow> 0 \<le> a \<Longrightarrow> 1 \<le> x powr a"
2868  for x :: real
2869  using powr_mono by fastforce
2870
2871lemma powr_less_mono2: "0 < a \<Longrightarrow> 0 \<le> x \<Longrightarrow> x < y \<Longrightarrow> x powr a < y powr a"
2872  for x :: real
2873  by (simp add: powr_def)
2874
2875lemma powr_less_mono2_neg: "a < 0 \<Longrightarrow> 0 < x \<Longrightarrow> x < y \<Longrightarrow> y powr a < x powr a"
2876  for x :: real
2877  by (simp add: powr_def)
2878
2879lemma powr_mono2: "x powr a \<le> y powr a" if "0 \<le> a" "0 \<le> x" "x \<le> y"
2880  for x :: real
2881  using less_eq_real_def powr_less_mono2 that by auto
2882
2883lemma powr_le1: "0 \<le> a \<Longrightarrow> 0 \<le> x \<Longrightarrow> x \<le> 1 \<Longrightarrow> x powr a \<le> 1"
2884  for x :: real
2885  using powr_mono2 by fastforce
2886
2887lemma powr_mono2':
2888  fixes a x y :: real
2889  assumes "a \<le> 0" "x > 0" "x \<le> y"
2890  shows "x powr a \<ge> y powr a"
2891proof -
2892  from assms have "x powr - a \<le> y powr - a"
2893    by (intro powr_mono2) simp_all
2894  with assms show ?thesis
2895    by (auto simp: powr_minus field_simps)
2896qed
2897
2898lemma powr_mono_both:
2899  fixes x :: real
2900  assumes "0 \<le> a" "a \<le> b" "1 \<le> x" "x \<le> y"
2901    shows "x powr a \<le> y powr b"
2902  by (meson assms order.trans powr_mono powr_mono2 zero_le_one)
2903
2904lemma powr_inj: "0 < a \<Longrightarrow> a \<noteq> 1 \<Longrightarrow> a powr x = a powr y \<longleftrightarrow> x = y"
2905  for x :: real
2906  unfolding powr_def exp_inj_iff by simp
2907
2908lemma powr_half_sqrt: "0 \<le> x \<Longrightarrow> x powr (1/2) = sqrt x"
2909  by (simp add: powr_def root_powr_inverse sqrt_def)
2910
2911lemma ln_powr_bound: "1 \<le> x \<Longrightarrow> 0 < a \<Longrightarrow> ln x \<le> (x powr a) / a"
2912  for x :: real
2913  by (metis exp_gt_zero linear ln_eq_zero_iff ln_exp ln_less_self ln_powr mult.commute
2914      mult_imp_le_div_pos not_less powr_gt_zero)
2915
2916lemma ln_powr_bound2:
2917  fixes x :: real
2918  assumes "1 < x" and "0 < a"
2919  shows "(ln x) powr a \<le> (a powr a) * x"
2920proof -
2921  from assms have "ln x \<le> (x powr (1 / a)) / (1 / a)"
2922    by (metis less_eq_real_def ln_powr_bound zero_less_divide_1_iff)
2923  also have "\<dots> = a * (x powr (1 / a))"
2924    by simp
2925  finally have "(ln x) powr a \<le> (a * (x powr (1 / a))) powr a"
2926    by (metis assms less_imp_le ln_gt_zero powr_mono2)
2927  also have "\<dots> = (a powr a) * ((x powr (1 / a)) powr a)"
2928    using assms powr_mult by auto
2929  also have "(x powr (1 / a)) powr a = x powr ((1 / a) * a)"
2930    by (rule powr_powr)
2931  also have "\<dots> = x" using assms
2932    by auto
2933  finally show ?thesis .
2934qed
2935
2936lemma tendsto_powr:
2937  fixes a b :: real
2938  assumes f: "(f \<longlongrightarrow> a) F"
2939    and g: "(g \<longlongrightarrow> b) F"
2940    and a: "a \<noteq> 0"
2941  shows "((\<lambda>x. f x powr g x) \<longlongrightarrow> a powr b) F"
2942  unfolding powr_def
2943proof (rule filterlim_If)
2944  from f show "((\<lambda>x. 0) \<longlongrightarrow> (if a = 0 then 0 else exp (b * ln a))) (inf F (principal {x. f x = 0}))"
2945    by simp (auto simp: filterlim_iff eventually_inf_principal elim: eventually_mono dest: t1_space_nhds)
2946  from f g a show "((\<lambda>x. exp (g x * ln (f x))) \<longlongrightarrow> (if a = 0 then 0 else exp (b * ln a)))
2947      (inf F (principal {x. f x \<noteq> 0}))"
2948    by (auto intro!: tendsto_intros intro: tendsto_mono inf_le1)
2949qed
2950
2951lemma tendsto_powr'[tendsto_intros]:
2952  fixes a :: real
2953  assumes f: "(f \<longlongrightarrow> a) F"
2954    and g: "(g \<longlongrightarrow> b) F"
2955    and a: "a \<noteq> 0 \<or> (b > 0 \<and> eventually (\<lambda>x. f x \<ge> 0) F)"
2956  shows "((\<lambda>x. f x powr g x) \<longlongrightarrow> a powr b) F"
2957proof -
2958  from a consider "a \<noteq> 0" | "a = 0" "b > 0" "eventually (\<lambda>x. f x \<ge> 0) F"
2959    by auto
2960  then show ?thesis
2961  proof cases
2962    case 1
2963    with f g show ?thesis by (rule tendsto_powr)
2964  next
2965    case 2
2966    have "((\<lambda>x. if f x = 0 then 0 else exp (g x * ln (f x))) \<longlongrightarrow> 0) F"
2967    proof (intro filterlim_If)
2968      have "filterlim f (principal {0<..}) (inf F (principal {z. f z \<noteq> 0}))"
2969        using \<open>eventually (\<lambda>x. f x \<ge> 0) F\<close>
2970        by (auto simp: filterlim_iff eventually_inf_principal
2971            eventually_principal elim: eventually_mono)
2972      moreover have "filterlim f (nhds a) (inf F (principal {z. f z \<noteq> 0}))"
2973        by (rule tendsto_mono[OF _ f]) simp_all
2974      ultimately have f: "filterlim f (at_right 0) (inf F (principal {x. f x \<noteq> 0}))"
2975        by (simp add: at_within_def filterlim_inf \<open>a = 0\<close>)
2976      have g: "(g \<longlongrightarrow> b) (inf F (principal {z. f z \<noteq> 0}))"
2977        by (rule tendsto_mono[OF _ g]) simp_all
2978      show "((\<lambda>x. exp (g x * ln (f x))) \<longlongrightarrow> 0) (inf F (principal {x. f x \<noteq> 0}))"
2979        by (rule filterlim_compose[OF exp_at_bot] filterlim_tendsto_pos_mult_at_bot
2980                 filterlim_compose[OF ln_at_0] f g \<open>b > 0\<close>)+
2981    qed simp_all
2982    with \<open>a = 0\<close> show ?thesis
2983      by (simp add: powr_def)
2984  qed
2985qed
2986
2987lemma continuous_powr:
2988  assumes "continuous F f"
2989    and "continuous F g"
2990    and "f (Lim F (\<lambda>x. x)) \<noteq> 0"
2991  shows "continuous F (\<lambda>x. (f x) powr (g x :: real))"
2992  using assms unfolding continuous_def by (rule tendsto_powr)
2993
2994lemma continuous_at_within_powr[continuous_intros]:
2995  fixes f g :: "_ \<Rightarrow> real"
2996  assumes "continuous (at a within s) f"
2997    and "continuous (at a within s) g"
2998    and "f a \<noteq> 0"
2999  shows "continuous (at a within s) (\<lambda>x. (f x) powr (g x))"
3000  using assms unfolding continuous_within by (rule tendsto_powr)
3001
3002lemma isCont_powr[continuous_intros, simp]:
3003  fixes f g :: "_ \<Rightarrow> real"
3004  assumes "isCont f a" "isCont g a" "f a \<noteq> 0"
3005  shows "isCont (\<lambda>x. (f x) powr g x) a"
3006  using assms unfolding continuous_at by (rule tendsto_powr)
3007
3008lemma continuous_on_powr[continuous_intros]:
3009  fixes f g :: "_ \<Rightarrow> real"
3010  assumes "continuous_on s f" "continuous_on s g" and "\<forall>x\<in>s. f x \<noteq> 0"
3011  shows "continuous_on s (\<lambda>x. (f x) powr (g x))"
3012  using assms unfolding continuous_on_def by (fast intro: tendsto_powr)
3013
3014lemma tendsto_powr2:
3015  fixes a :: real
3016  assumes f: "(f \<longlongrightarrow> a) F"
3017    and g: "(g \<longlongrightarrow> b) F"
3018    and "\<forall>\<^sub>F x in F. 0 \<le> f x"
3019    and b: "0 < b"
3020  shows "((\<lambda>x. f x powr g x) \<longlongrightarrow> a powr b) F"
3021  using tendsto_powr'[of f a F g b] assms by auto
3022
3023lemma has_derivative_powr[derivative_intros]:
3024  assumes g[derivative_intros]: "(g has_derivative g') (at x within X)"
3025    and f[derivative_intros]:"(f has_derivative f') (at x within X)"
3026  assumes pos: "0 < g x" and "x \<in> X"
3027  shows "((\<lambda>x. g x powr f x::real) has_derivative (\<lambda>h. (g x powr f x) * (f' h * ln (g x) + g' h * f x / g x))) (at x within X)"
3028proof -
3029  have "\<forall>\<^sub>F x in at x within X. g x > 0"
3030    by (rule order_tendstoD[OF _ pos])
3031      (rule has_derivative_continuous[OF g, unfolded continuous_within])
3032  then obtain d where "d > 0" and pos': "\<And>x'. x' \<in> X \<Longrightarrow> dist x' x < d \<Longrightarrow> 0 < g x'"
3033    using pos unfolding eventually_at by force
3034  have "((\<lambda>x. exp (f x * ln (g x))) has_derivative
3035    (\<lambda>h. (g x powr f x) * (f' h * ln (g x) + g' h * f x / g x))) (at x within X)"
3036    using pos
3037    by (auto intro!: derivative_eq_intros simp: divide_simps powr_def)
3038  then show ?thesis
3039    by (rule has_derivative_transform_within[OF _ \<open>d > 0\<close> \<open>x \<in> X\<close>]) (auto simp: powr_def dest: pos')
3040qed
3041
3042lemma DERIV_powr:
3043  fixes r :: real
3044  assumes g: "DERIV g x :> m"
3045    and pos: "g x > 0"
3046    and f: "DERIV f x :> r"
3047  shows "DERIV (\<lambda>x. g x powr f x) x :> (g x powr f x) * (r * ln (g x) + m * f x / g x)"
3048  using assms
3049  by (auto intro!: derivative_eq_intros ext simp: has_field_derivative_def algebra_simps)
3050
3051lemma DERIV_fun_powr:
3052  fixes r :: real
3053  assumes g: "DERIV g x :> m"
3054    and pos: "g x > 0"
3055  shows "DERIV (\<lambda>x. (g x) powr r) x :> r * (g x) powr (r - of_nat 1) * m"
3056  using DERIV_powr[OF g pos DERIV_const, of r] pos
3057  by (simp add: powr_diff field_simps)
3058
3059lemma has_real_derivative_powr:
3060  assumes "z > 0"
3061  shows "((\<lambda>z. z powr r) has_real_derivative r * z powr (r - 1)) (at z)"
3062proof (subst DERIV_cong_ev[OF refl _ refl])
3063  from assms have "eventually (\<lambda>z. z \<noteq> 0) (nhds z)"
3064    by (intro t1_space_nhds) auto
3065  then show "eventually (\<lambda>z. z powr r = exp (r * ln z)) (nhds z)"
3066    unfolding powr_def by eventually_elim simp
3067  from assms show "((\<lambda>z. exp (r * ln z)) has_real_derivative r * z powr (r - 1)) (at z)"
3068    by (auto intro!: derivative_eq_intros simp: powr_def field_simps exp_diff)
3069qed
3070
3071declare has_real_derivative_powr[THEN DERIV_chain2, derivative_intros]
3072
3073lemma tendsto_zero_powrI:
3074  assumes "(f \<longlongrightarrow> (0::real)) F" "(g \<longlongrightarrow> b) F" "\<forall>\<^sub>F x in F. 0 \<le> f x" "0 < b"
3075  shows "((\<lambda>x. f x powr g x) \<longlongrightarrow> 0) F"
3076  using tendsto_powr2[OF assms] by simp
3077
3078lemma continuous_on_powr':
3079  fixes f g :: "_ \<Rightarrow> real"
3080  assumes "continuous_on s f" "continuous_on s g"
3081    and "\<forall>x\<in>s. f x \<ge> 0 \<and> (f x = 0 \<longrightarrow> g x > 0)"
3082  shows "continuous_on s (\<lambda>x. (f x) powr (g x))"
3083  unfolding continuous_on_def
3084proof
3085  fix x
3086  assume x: "x \<in> s"
3087  from assms x show "((\<lambda>x. f x powr g x) \<longlongrightarrow> f x powr g x) (at x within s)"
3088  proof (cases "f x = 0")
3089    case True
3090    from assms(3) have "eventually (\<lambda>x. f x \<ge> 0) (at x within s)"
3091      by (auto simp: at_within_def eventually_inf_principal)
3092    with True x assms show ?thesis
3093      by (auto intro!: tendsto_zero_powrI[of f _ g "g x"] simp: continuous_on_def)
3094  next
3095    case False
3096    with assms x show ?thesis
3097      by (auto intro!: tendsto_powr' simp: continuous_on_def)
3098  qed
3099qed
3100
3101lemma tendsto_neg_powr:
3102  assumes "s < 0"
3103    and f: "LIM x F. f x :> at_top"
3104  shows "((\<lambda>x. f x powr s) \<longlongrightarrow> (0::real)) F"
3105proof -
3106  have "((\<lambda>x. exp (s * ln (f x))) \<longlongrightarrow> (0::real)) F" (is "?X")
3107    by (auto intro!: filterlim_compose[OF exp_at_bot] filterlim_compose[OF ln_at_top]
3108        filterlim_tendsto_neg_mult_at_bot assms)
3109  also have "?X \<longleftrightarrow> ((\<lambda>x. f x powr s) \<longlongrightarrow> (0::real)) F"
3110    using f filterlim_at_top_dense[of f F]
3111    by (intro filterlim_cong[OF refl refl]) (auto simp: neq_iff powr_def elim: eventually_mono)
3112  finally show ?thesis .
3113qed
3114
3115lemma tendsto_exp_limit_at_right: "((\<lambda>y. (1 + x * y) powr (1 / y)) \<longlongrightarrow> exp x) (at_right 0)"
3116  for x :: real
3117proof (cases "x = 0")
3118  case True
3119  then show ?thesis by simp
3120next
3121  case False
3122  have "((\<lambda>y. ln (1 + x * y)::real) has_real_derivative 1 * x) (at 0)"
3123    by (auto intro!: derivative_eq_intros)
3124  then have "((\<lambda>y. ln (1 + x * y) / y) \<longlongrightarrow> x) (at 0)"
3125    by (auto simp: has_field_derivative_def field_has_derivative_at)
3126  then have *: "((\<lambda>y. exp (ln (1 + x * y) / y)) \<longlongrightarrow> exp x) (at 0)"
3127    by (rule tendsto_intros)
3128  then show ?thesis
3129  proof (rule filterlim_mono_eventually)
3130    show "eventually (\<lambda>xa. exp (ln (1 + x * xa) / xa) = (1 + x * xa) powr (1 / xa)) (at_right 0)"
3131      unfolding eventually_at_right[OF zero_less_one]
3132      using False
3133      by (intro exI[of _ "1 / \<bar>x\<bar>"]) (auto simp: field_simps powr_def abs_if add_nonneg_eq_0_iff)
3134  qed (simp_all add: at_eq_sup_left_right)
3135qed
3136
3137lemma tendsto_exp_limit_at_top: "((\<lambda>y. (1 + x / y) powr y) \<longlongrightarrow> exp x) at_top"
3138  for x :: real
3139  by (simp add: filterlim_at_top_to_right inverse_eq_divide tendsto_exp_limit_at_right)
3140
3141lemma tendsto_exp_limit_sequentially: "(\<lambda>n. (1 + x / n) ^ n) \<longlonglongrightarrow> exp x"
3142  for x :: real
3143proof (rule filterlim_mono_eventually)
3144  from reals_Archimedean2 [of "\<bar>x\<bar>"] obtain n :: nat where *: "real n > \<bar>x\<bar>" ..
3145  then have "eventually (\<lambda>n :: nat. 0 < 1 + x / real n) at_top"
3146    by (intro eventually_sequentiallyI [of n]) (auto simp: divide_simps)
3147  then show "eventually (\<lambda>n. (1 + x / n) powr n = (1 + x / n) ^ n) at_top"
3148    by (rule eventually_mono) (erule powr_realpow)
3149  show "(\<lambda>n. (1 + x / real n) powr real n) \<longlonglongrightarrow> exp x"
3150    by (rule filterlim_compose [OF tendsto_exp_limit_at_top filterlim_real_sequentially])
3151qed auto
3152
3153
3154subsection \<open>Sine and Cosine\<close>
3155
3156definition sin_coeff :: "nat \<Rightarrow> real"
3157  where "sin_coeff = (\<lambda>n. if even n then 0 else (- 1) ^ ((n - Suc 0) div 2) / (fact n))"
3158
3159definition cos_coeff :: "nat \<Rightarrow> real"
3160  where "cos_coeff = (\<lambda>n. if even n then ((- 1) ^ (n div 2)) / (fact n) else 0)"
3161
3162definition sin :: "'a \<Rightarrow> 'a::{real_normed_algebra_1,banach}"
3163  where "sin = (\<lambda>x. \<Sum>n. sin_coeff n *\<^sub>R x^n)"
3164
3165definition cos :: "'a \<Rightarrow> 'a::{real_normed_algebra_1,banach}"
3166  where "cos = (\<lambda>x. \<Sum>n. cos_coeff n *\<^sub>R x^n)"
3167
3168lemma sin_coeff_0 [simp]: "sin_coeff 0 = 0"
3169  unfolding sin_coeff_def by simp
3170
3171lemma cos_coeff_0 [simp]: "cos_coeff 0 = 1"
3172  unfolding cos_coeff_def by simp
3173
3174lemma sin_coeff_Suc: "sin_coeff (Suc n) = cos_coeff n / real (Suc n)"
3175  unfolding cos_coeff_def sin_coeff_def
3176  by (simp del: mult_Suc)
3177
3178lemma cos_coeff_Suc: "cos_coeff (Suc n) = - sin_coeff n / real (Suc n)"
3179  unfolding cos_coeff_def sin_coeff_def
3180  by (simp del: mult_Suc) (auto elim: oddE)
3181
3182lemma summable_norm_sin: "summable (\<lambda>n. norm (sin_coeff n *\<^sub>R x^n))"
3183  for x :: "'a::{real_normed_algebra_1,banach}"
3184  unfolding sin_coeff_def
3185  apply (rule summable_comparison_test [OF _ summable_norm_exp [where x=x]])
3186  apply (auto simp: divide_inverse abs_mult power_abs [symmetric] zero_le_mult_iff)
3187  done
3188
3189lemma summable_norm_cos: "summable (\<lambda>n. norm (cos_coeff n *\<^sub>R x^n))"
3190  for x :: "'a::{real_normed_algebra_1,banach}"
3191  unfolding cos_coeff_def
3192  apply (rule summable_comparison_test [OF _ summable_norm_exp [where x=x]])
3193  apply (auto simp: divide_inverse abs_mult power_abs [symmetric] zero_le_mult_iff)
3194  done
3195
3196lemma sin_converges: "(\<lambda>n. sin_coeff n *\<^sub>R x^n) sums sin x"
3197  unfolding sin_def
3198  by (metis (full_types) summable_norm_cancel summable_norm_sin summable_sums)
3199
3200lemma cos_converges: "(\<lambda>n. cos_coeff n *\<^sub>R x^n) sums cos x"
3201  unfolding cos_def
3202  by (metis (full_types) summable_norm_cancel summable_norm_cos summable_sums)
3203
3204lemma sin_of_real: "sin (of_real x) = of_real (sin x)"
3205  for x :: real
3206proof -
3207  have "(\<lambda>n. of_real (sin_coeff n *\<^sub>R  x^n)) = (\<lambda>n. sin_coeff n *\<^sub>R  (of_real x)^n)"
3208  proof
3209    show "of_real (sin_coeff n *\<^sub>R  x^n) = sin_coeff n *\<^sub>R of_real x^n" for n
3210      by (simp add: scaleR_conv_of_real)
3211  qed
3212  also have "\<dots> sums (sin (of_real x))"
3213    by (rule sin_converges)
3214  finally have "(\<lambda>n. of_real (sin_coeff n *\<^sub>R x^n)) sums (sin (of_real x))" .
3215  then show ?thesis
3216    using sums_unique2 sums_of_real [OF sin_converges]
3217    by blast
3218qed
3219
3220corollary sin_in_Reals [simp]: "z \<in> \<real> \<Longrightarrow> sin z \<in> \<real>"
3221  by (metis Reals_cases Reals_of_real sin_of_real)
3222
3223lemma cos_of_real: "cos (of_real x) = of_real (cos x)"
3224  for x :: real
3225proof -
3226  have "(\<lambda>n. of_real (cos_coeff n *\<^sub>R  x^n)) = (\<lambda>n. cos_coeff n *\<^sub>R  (of_real x)^n)"
3227  proof
3228    show "of_real (cos_coeff n *\<^sub>R  x^n) = cos_coeff n *\<^sub>R of_real x^n" for n
3229      by (simp add: scaleR_conv_of_real)
3230  qed
3231  also have "\<dots> sums (cos (of_real x))"
3232    by (rule cos_converges)
3233  finally have "(\<lambda>n. of_real (cos_coeff n *\<^sub>R x^n)) sums (cos (of_real x))" .
3234  then show ?thesis
3235    using sums_unique2 sums_of_real [OF cos_converges]
3236    by blast
3237qed
3238
3239corollary cos_in_Reals [simp]: "z \<in> \<real> \<Longrightarrow> cos z \<in> \<real>"
3240  by (metis Reals_cases Reals_of_real cos_of_real)
3241
3242lemma diffs_sin_coeff: "diffs sin_coeff = cos_coeff"
3243  by (simp add: diffs_def sin_coeff_Suc del: of_nat_Suc)
3244
3245lemma diffs_cos_coeff: "diffs cos_coeff = (\<lambda>n. - sin_coeff n)"
3246  by (simp add: diffs_def cos_coeff_Suc del: of_nat_Suc)
3247
3248lemma sin_int_times_real: "sin (of_int m * of_real x) = of_real (sin (of_int m * x))"
3249  by (metis sin_of_real of_real_mult of_real_of_int_eq)
3250
3251lemma cos_int_times_real: "cos (of_int m * of_real x) = of_real (cos (of_int m * x))"
3252  by (metis cos_of_real of_real_mult of_real_of_int_eq)
3253
3254text \<open>Now at last we can get the derivatives of exp, sin and cos.\<close>
3255
3256lemma DERIV_sin [simp]: "DERIV sin x :> cos x"
3257  for x :: "'a::{real_normed_field,banach}"
3258  unfolding sin_def cos_def scaleR_conv_of_real
3259  apply (rule DERIV_cong)
3260   apply (rule termdiffs [where K="of_real (norm x) + 1 :: 'a"])
3261      apply (simp_all add: norm_less_p1 diffs_of_real diffs_sin_coeff diffs_cos_coeff
3262              summable_minus_iff scaleR_conv_of_real [symmetric]
3263              summable_norm_sin [THEN summable_norm_cancel]
3264              summable_norm_cos [THEN summable_norm_cancel])
3265  done
3266
3267declare DERIV_sin[THEN DERIV_chain2, derivative_intros]
3268  and DERIV_sin[THEN DERIV_chain2, unfolded has_field_derivative_def, derivative_intros]
3269
3270lemmas has_derivative_sin[derivative_intros] = DERIV_sin[THEN DERIV_compose_FDERIV]
3271
3272lemma DERIV_cos [simp]: "DERIV cos x :> - sin x"
3273  for x :: "'a::{real_normed_field,banach}"
3274  unfolding sin_def cos_def scaleR_conv_of_real
3275  apply (rule DERIV_cong)
3276   apply (rule termdiffs [where K="of_real (norm x) + 1 :: 'a"])
3277      apply (simp_all add: norm_less_p1 diffs_of_real diffs_minus suminf_minus
3278              diffs_sin_coeff diffs_cos_coeff
3279              summable_minus_iff scaleR_conv_of_real [symmetric]
3280              summable_norm_sin [THEN summable_norm_cancel]
3281              summable_norm_cos [THEN summable_norm_cancel])
3282  done
3283
3284declare DERIV_cos[THEN DERIV_chain2, derivative_intros]
3285  and DERIV_cos[THEN DERIV_chain2, unfolded has_field_derivative_def, derivative_intros]
3286
3287lemmas has_derivative_cos[derivative_intros] = DERIV_cos[THEN DERIV_compose_FDERIV]
3288
3289lemma isCont_sin: "isCont sin x"
3290  for x :: "'a::{real_normed_field,banach}"
3291  by (rule DERIV_sin [THEN DERIV_isCont])
3292
3293lemma isCont_cos: "isCont cos x"
3294  for x :: "'a::{real_normed_field,banach}"
3295  by (rule DERIV_cos [THEN DERIV_isCont])
3296
3297lemma isCont_sin' [simp]: "isCont f a \<Longrightarrow> isCont (\<lambda>x. sin (f x)) a"
3298  for f :: "_ \<Rightarrow> 'a::{real_normed_field,banach}"
3299  by (rule isCont_o2 [OF _ isCont_sin])
3300
3301(* FIXME a context for f would be better *)
3302
3303lemma isCont_cos' [simp]: "isCont f a \<Longrightarrow> isCont (\<lambda>x. cos (f x)) a"
3304  for f :: "_ \<Rightarrow> 'a::{real_normed_field,banach}"
3305  by (rule isCont_o2 [OF _ isCont_cos])
3306
3307lemma tendsto_sin [tendsto_intros]: "(f \<longlongrightarrow> a) F \<Longrightarrow> ((\<lambda>x. sin (f x)) \<longlongrightarrow> sin a) F"
3308  for f :: "_ \<Rightarrow> 'a::{real_normed_field,banach}"
3309  by (rule isCont_tendsto_compose [OF isCont_sin])
3310
3311lemma tendsto_cos [tendsto_intros]: "(f \<longlongrightarrow> a) F \<Longrightarrow> ((\<lambda>x. cos (f x)) \<longlongrightarrow> cos a) F"
3312  for f :: "_ \<Rightarrow> 'a::{real_normed_field,banach}"
3313  by (rule isCont_tendsto_compose [OF isCont_cos])
3314
3315lemma continuous_sin [continuous_intros]: "continuous F f \<Longrightarrow> continuous F (\<lambda>x. sin (f x))"
3316  for f :: "_ \<Rightarrow> 'a::{real_normed_field,banach}"
3317  unfolding continuous_def by (rule tendsto_sin)
3318
3319lemma continuous_on_sin [continuous_intros]: "continuous_on s f \<Longrightarrow> continuous_on s (\<lambda>x. sin (f x))"
3320  for f :: "_ \<Rightarrow> 'a::{real_normed_field,banach}"
3321  unfolding continuous_on_def by (auto intro: tendsto_sin)
3322
3323lemma continuous_within_sin: "continuous (at z within s) sin"
3324  for z :: "'a::{real_normed_field,banach}"
3325  by (simp add: continuous_within tendsto_sin)
3326
3327lemma continuous_cos [continuous_intros]: "continuous F f \<Longrightarrow> continuous F (\<lambda>x. cos (f x))"
3328  for f :: "_ \<Rightarrow> 'a::{real_normed_field,banach}"
3329  unfolding continuous_def by (rule tendsto_cos)
3330
3331lemma continuous_on_cos [continuous_intros]: "continuous_on s f \<Longrightarrow> continuous_on s (\<lambda>x. cos (f x))"
3332  for f :: "_ \<Rightarrow> 'a::{real_normed_field,banach}"
3333  unfolding continuous_on_def by (auto intro: tendsto_cos)
3334
3335lemma continuous_within_cos: "continuous (at z within s) cos"
3336  for z :: "'a::{real_normed_field,banach}"
3337  by (simp add: continuous_within tendsto_cos)
3338
3339
3340subsection \<open>Properties of Sine and Cosine\<close>
3341
3342lemma sin_zero [simp]: "sin 0 = 0"
3343  by (simp add: sin_def sin_coeff_def scaleR_conv_of_real)
3344
3345lemma cos_zero [simp]: "cos 0 = 1"
3346  by (simp add: cos_def cos_coeff_def scaleR_conv_of_real)
3347
3348lemma DERIV_fun_sin: "DERIV g x :> m \<Longrightarrow> DERIV (\<lambda>x. sin (g x)) x :> cos (g x) * m"
3349  by (auto intro!: derivative_intros)
3350
3351lemma DERIV_fun_cos: "DERIV g x :> m \<Longrightarrow> DERIV (\<lambda>x. cos(g x)) x :> - sin (g x) * m"
3352  by (auto intro!: derivative_eq_intros)
3353
3354
3355subsection \<open>Deriving the Addition Formulas\<close>
3356
3357text \<open>The product of two cosine series.\<close>
3358lemma cos_x_cos_y:
3359  fixes x :: "'a::{real_normed_field,banach}"
3360  shows
3361    "(\<lambda>p. \<Sum>n\<le>p.
3362        if even p \<and> even n
3363        then ((-1) ^ (p div 2) * (p choose n) / (fact p)) *\<^sub>R (x^n) * y^(p-n) else 0)
3364      sums (cos x * cos y)"
3365proof -
3366  have "(cos_coeff n * cos_coeff (p - n)) *\<^sub>R (x^n * y^(p - n)) =
3367    (if even p \<and> even n then ((-1) ^ (p div 2) * (p choose n) / (fact p)) *\<^sub>R (x^n) * y^(p - n)
3368     else 0)"
3369    if "n \<le> p" for n p :: nat
3370  proof -
3371    from that have *: "even n \<Longrightarrow> even p \<Longrightarrow>
3372        (-1) ^ (n div 2) * (-1) ^ ((p - n) div 2) = (-1 :: real) ^ (p div 2)"
3373      by (metis div_add power_add le_add_diff_inverse odd_add)
3374    with that show ?thesis
3375      by (auto simp: algebra_simps cos_coeff_def binomial_fact)
3376  qed
3377  then have "(\<lambda>p. \<Sum>n\<le>p. if even p \<and> even n
3378                  then ((-1) ^ (p div 2) * (p choose n) / (fact p)) *\<^sub>R (x^n) * y^(p-n) else 0) =
3379             (\<lambda>p. \<Sum>n\<le>p. (cos_coeff n * cos_coeff (p - n)) *\<^sub>R (x^n * y^(p-n)))"
3380    by simp
3381  also have "\<dots> = (\<lambda>p. \<Sum>n\<le>p. (cos_coeff n *\<^sub>R x^n) * (cos_coeff (p - n) *\<^sub>R y^(p-n)))"
3382    by (simp add: algebra_simps)
3383  also have "\<dots> sums (cos x * cos y)"
3384    using summable_norm_cos
3385    by (auto simp: cos_def scaleR_conv_of_real intro!: Cauchy_product_sums)
3386  finally show ?thesis .
3387qed
3388
3389text \<open>The product of two sine series.\<close>
3390lemma sin_x_sin_y:
3391  fixes x :: "'a::{real_normed_field,banach}"
3392  shows
3393    "(\<lambda>p. \<Sum>n\<le>p.
3394        if even p \<and> odd n
3395        then - ((-1) ^ (p div 2) * (p choose n) / (fact p)) *\<^sub>R (x^n) * y^(p-n)
3396        else 0)
3397      sums (sin x * sin y)"
3398proof -
3399  have "(sin_coeff n * sin_coeff (p - n)) *\<^sub>R (x^n * y^(p-n)) =
3400    (if even p \<and> odd n
3401     then -((-1) ^ (p div 2) * (p choose n) / (fact p)) *\<^sub>R (x^n) * y^(p-n)
3402     else 0)"
3403    if "n \<le> p" for n p :: nat
3404  proof -
3405    have "(-1) ^ ((n - Suc 0) div 2) * (-1) ^ ((p - Suc n) div 2) = - ((-1 :: real) ^ (p div 2))"
3406      if np: "odd n" "even p"
3407    proof -
3408      from \<open>n \<le> p\<close> np have *: "n - Suc 0 + (p - Suc n) = p - Suc (Suc 0)" "Suc (Suc 0) \<le> p"
3409        by arith+
3410      have "(p - Suc (Suc 0)) div 2 = p div 2 - Suc 0"
3411        by simp
3412      with \<open>n \<le> p\<close> np * show ?thesis
3413        apply (simp add: power_add [symmetric] div_add [symmetric] del: div_add)
3414        apply (metis (no_types) One_nat_def Suc_1 le_div_geq minus_minus
3415            mult.left_neutral mult_minus_left power.simps(2) zero_less_Suc)
3416        done
3417    qed
3418    then show ?thesis
3419      using \<open>n\<le>p\<close> by (auto simp: algebra_simps sin_coeff_def binomial_fact)
3420  qed
3421  then have "(\<lambda>p. \<Sum>n\<le>p. if even p \<and> odd n
3422               then - ((-1) ^ (p div 2) * (p choose n) / (fact p)) *\<^sub>R (x^n) * y^(p-n) else 0) =
3423             (\<lambda>p. \<Sum>n\<le>p. (sin_coeff n * sin_coeff (p - n)) *\<^sub>R (x^n * y^(p-n)))"
3424    by simp
3425  also have "\<dots> = (\<lambda>p. \<Sum>n\<le>p. (sin_coeff n *\<^sub>R x^n) * (sin_coeff (p - n) *\<^sub>R y^(p-n)))"
3426    by (simp add: algebra_simps)
3427  also have "\<dots> sums (sin x * sin y)"
3428    using summable_norm_sin
3429    by (auto simp: sin_def scaleR_conv_of_real intro!: Cauchy_product_sums)
3430  finally show ?thesis .
3431qed
3432
3433lemma sums_cos_x_plus_y:
3434  fixes x :: "'a::{real_normed_field,banach}"
3435  shows
3436    "(\<lambda>p. \<Sum>n\<le>p.
3437        if even p
3438        then ((-1) ^ (p div 2) * (p choose n) / (fact p)) *\<^sub>R (x^n) * y^(p-n)
3439        else 0)
3440      sums cos (x + y)"
3441proof -
3442  have
3443    "(\<Sum>n\<le>p.
3444      if even p then ((-1) ^ (p div 2) * (p choose n) / (fact p)) *\<^sub>R (x^n) * y^(p-n)
3445      else 0) = cos_coeff p *\<^sub>R ((x + y) ^ p)"
3446    for p :: nat
3447  proof -
3448    have
3449      "(\<Sum>n\<le>p. if even p then ((-1) ^ (p div 2) * (p choose n) / (fact p)) *\<^sub>R (x^n) * y^(p-n) else 0) =
3450       (if even p then \<Sum>n\<le>p. ((-1) ^ (p div 2) * (p choose n) / (fact p)) *\<^sub>R (x^n) * y^(p-n) else 0)"
3451      by simp
3452    also have "\<dots> =
3453       (if even p
3454        then of_real ((-1) ^ (p div 2) / (fact p)) * (\<Sum>n\<le>p. (p choose n) *\<^sub>R (x^n) * y^(p-n))
3455        else 0)"
3456      by (auto simp: sum_distrib_left field_simps scaleR_conv_of_real nonzero_of_real_divide)
3457    also have "\<dots> = cos_coeff p *\<^sub>R ((x + y) ^ p)"
3458      by (simp add: cos_coeff_def binomial_ring [of x y]  scaleR_conv_of_real atLeast0AtMost)
3459    finally show ?thesis .
3460  qed
3461  then have
3462    "(\<lambda>p. \<Sum>n\<le>p.
3463        if even p
3464        then ((-1) ^ (p div 2) * (p choose n) / (fact p)) *\<^sub>R (x^n) * y^(p-n)
3465        else 0) = (\<lambda>p. cos_coeff p *\<^sub>R ((x+y)^p))"
3466    by simp
3467   also have "\<dots> sums cos (x + y)"
3468    by (rule cos_converges)
3469   finally show ?thesis .
3470qed
3471
3472theorem cos_add:
3473  fixes x :: "'a::{real_normed_field,banach}"
3474  shows "cos (x + y) = cos x * cos y - sin x * sin y"
3475proof -
3476  have
3477    "(if even p \<and> even n
3478      then ((- 1) ^ (p div 2) * int (p choose n) / (fact p)) *\<^sub>R (x^n) * y^(p-n) else 0) -
3479     (if even p \<and> odd n
3480      then - ((- 1) ^ (p div 2) * int (p choose n) / (fact p)) *\<^sub>R (x^n) * y^(p-n) else 0) =
3481     (if even p then ((-1) ^ (p div 2) * (p choose n) / (fact p)) *\<^sub>R (x^n) * y^(p-n) else 0)"
3482    if "n \<le> p" for n p :: nat
3483    by simp
3484  then have
3485    "(\<lambda>p. \<Sum>n\<le>p. (if even p then ((-1) ^ (p div 2) * (p choose n) / (fact p)) *\<^sub>R (x^n) * y^(p-n) else 0))
3486      sums (cos x * cos y - sin x * sin y)"
3487    using sums_diff [OF cos_x_cos_y [of x y] sin_x_sin_y [of x y]]
3488    by (simp add: sum_subtractf [symmetric])
3489  then show ?thesis
3490    by (blast intro: sums_cos_x_plus_y sums_unique2)
3491qed
3492
3493lemma sin_minus_converges: "(\<lambda>n. - (sin_coeff n *\<^sub>R (-x)^n)) sums sin x"
3494proof -
3495  have [simp]: "\<And>n. - (sin_coeff n *\<^sub>R (-x)^n) = (sin_coeff n *\<^sub>R x^n)"
3496    by (auto simp: sin_coeff_def elim!: oddE)
3497  show ?thesis
3498    by (simp add: sin_def summable_norm_sin [THEN summable_norm_cancel, THEN summable_sums])
3499qed
3500
3501lemma sin_minus [simp]: "sin (- x) = - sin x"
3502  for x :: "'a::{real_normed_algebra_1,banach}"
3503  using sin_minus_converges [of x]
3504  by (auto simp: sin_def summable_norm_sin [THEN summable_norm_cancel]
3505      suminf_minus sums_iff equation_minus_iff)
3506
3507lemma cos_minus_converges: "(\<lambda>n. (cos_coeff n *\<^sub>R (-x)^n)) sums cos x"
3508proof -
3509  have [simp]: "\<And>n. (cos_coeff n *\<^sub>R (-x)^n) = (cos_coeff n *\<^sub>R x^n)"
3510    by (auto simp: Transcendental.cos_coeff_def elim!: evenE)
3511  show ?thesis
3512    by (simp add: cos_def summable_norm_cos [THEN summable_norm_cancel, THEN summable_sums])
3513qed
3514
3515lemma cos_minus [simp]: "cos (-x) = cos x"
3516  for x :: "'a::{real_normed_algebra_1,banach}"
3517  using cos_minus_converges [of x]
3518  by (simp add: cos_def summable_norm_cos [THEN summable_norm_cancel]
3519      suminf_minus sums_iff equation_minus_iff)
3520
3521lemma sin_cos_squared_add [simp]: "(sin x)\<^sup>2 + (cos x)\<^sup>2 = 1"
3522  for x :: "'a::{real_normed_field,banach}"
3523  using cos_add [of x "-x"]
3524  by (simp add: power2_eq_square algebra_simps)
3525
3526lemma sin_cos_squared_add2 [simp]: "(cos x)\<^sup>2 + (sin x)\<^sup>2 = 1"
3527  for x :: "'a::{real_normed_field,banach}"
3528  by (subst add.commute, rule sin_cos_squared_add)
3529
3530lemma sin_cos_squared_add3 [simp]: "cos x * cos x + sin x * sin x = 1"
3531  for x :: "'a::{real_normed_field,banach}"
3532  using sin_cos_squared_add2 [unfolded power2_eq_square] .
3533
3534lemma sin_squared_eq: "(sin x)\<^sup>2 = 1 - (cos x)\<^sup>2"
3535  for x :: "'a::{real_normed_field,banach}"
3536  unfolding eq_diff_eq by (rule sin_cos_squared_add)
3537
3538lemma cos_squared_eq: "(cos x)\<^sup>2 = 1 - (sin x)\<^sup>2"
3539  for x :: "'a::{real_normed_field,banach}"
3540  unfolding eq_diff_eq by (rule sin_cos_squared_add2)
3541
3542lemma abs_sin_le_one [simp]: "\<bar>sin x\<bar> \<le> 1"
3543  for x :: real
3544  by (rule power2_le_imp_le) (simp_all add: sin_squared_eq)
3545
3546lemma sin_ge_minus_one [simp]: "- 1 \<le> sin x"
3547  for x :: real
3548  using abs_sin_le_one [of x] by (simp add: abs_le_iff)
3549
3550lemma sin_le_one [simp]: "sin x \<le> 1"
3551  for x :: real
3552  using abs_sin_le_one [of x] by (simp add: abs_le_iff)
3553
3554lemma abs_cos_le_one [simp]: "\<bar>cos x\<bar> \<le> 1"
3555  for x :: real
3556  by (rule power2_le_imp_le) (simp_all add: cos_squared_eq)
3557
3558lemma cos_ge_minus_one [simp]: "- 1 \<le> cos x"
3559  for x :: real
3560  using abs_cos_le_one [of x] by (simp add: abs_le_iff)
3561
3562lemma cos_le_one [simp]: "cos x \<le> 1"
3563  for x :: real
3564  using abs_cos_le_one [of x] by (simp add: abs_le_iff)
3565
3566lemma cos_diff: "cos (x - y) = cos x * cos y + sin x * sin y"
3567  for x :: "'a::{real_normed_field,banach}"
3568  using cos_add [of x "- y"] by simp
3569
3570lemma cos_double: "cos(2*x) = (cos x)\<^sup>2 - (sin x)\<^sup>2"
3571  for x :: "'a::{real_normed_field,banach}"
3572  using cos_add [where x=x and y=x] by (simp add: power2_eq_square)
3573
3574lemma sin_cos_le1: "\<bar>sin x * sin y + cos x * cos y\<bar> \<le> 1"
3575  for x :: real
3576  using cos_diff [of x y] by (metis abs_cos_le_one add.commute)
3577
3578lemma DERIV_fun_pow: "DERIV g x :> m \<Longrightarrow> DERIV (\<lambda>x. (g x) ^ n) x :> real n * (g x) ^ (n - 1) * m"
3579  by (auto intro!: derivative_eq_intros simp:)
3580
3581lemma DERIV_fun_exp: "DERIV g x :> m \<Longrightarrow> DERIV (\<lambda>x. exp (g x)) x :> exp (g x) * m"
3582  by (auto intro!: derivative_intros)
3583
3584
3585subsection \<open>The Constant Pi\<close>
3586
3587definition pi :: real
3588  where "pi = 2 * (THE x. 0 \<le> x \<and> x \<le> 2 \<and> cos x = 0)"
3589
3590text \<open>Show that there's a least positive @{term x} with @{term "cos x = 0"};
3591   hence define pi.\<close>
3592
3593lemma sin_paired: "(\<lambda>n. (- 1) ^ n / (fact (2 * n + 1)) * x ^ (2 * n + 1)) sums  sin x"
3594  for x :: real
3595proof -
3596  have "(\<lambda>n. \<Sum>k = n*2..<n * 2 + 2. sin_coeff k * x ^ k) sums sin x"
3597    by (rule sums_group) (use sin_converges [of x, unfolded scaleR_conv_of_real] in auto)
3598  then show ?thesis
3599    by (simp add: sin_coeff_def ac_simps)
3600qed
3601
3602lemma sin_gt_zero_02:
3603  fixes x :: real
3604  assumes "0 < x" and "x < 2"
3605  shows "0 < sin x"
3606proof -
3607  let ?f = "\<lambda>n::nat. \<Sum>k = n*2..<n*2+2. (- 1) ^ k / (fact (2*k+1)) * x^(2*k+1)"
3608  have pos: "\<forall>n. 0 < ?f n"
3609  proof
3610    fix n :: nat
3611    let ?k2 = "real (Suc (Suc (4 * n)))"
3612    let ?k3 = "real (Suc (Suc (Suc (4 * n))))"
3613    have "x * x < ?k2 * ?k3"
3614      using assms by (intro mult_strict_mono', simp_all)
3615    then have "x * x * x * x ^ (n * 4) < ?k2 * ?k3 * x * x ^ (n * 4)"
3616      by (intro mult_strict_right_mono zero_less_power \<open>0 < x\<close>)
3617    then show "0 < ?f n"
3618      by (simp add: divide_simps mult_ac del: mult_Suc)
3619qed
3620  have sums: "?f sums sin x"
3621    by (rule sin_paired [THEN sums_group]) simp
3622  show "0 < sin x"
3623    unfolding sums_unique [OF sums]
3624    using sums_summable [OF sums] pos
3625    by (rule suminf_pos)
3626qed
3627
3628lemma cos_double_less_one: "0 < x \<Longrightarrow> x < 2 \<Longrightarrow> cos (2 * x) < 1"
3629  for x :: real
3630  using sin_gt_zero_02 [where x = x] by (auto simp: cos_squared_eq cos_double)
3631
3632lemma cos_paired: "(\<lambda>n. (- 1) ^ n / (fact (2 * n)) * x ^ (2 * n)) sums cos x"
3633  for x :: real
3634proof -
3635  have "(\<lambda>n. \<Sum>k = n * 2..<n * 2 + 2. cos_coeff k * x ^ k) sums cos x"
3636    by (rule sums_group) (use cos_converges [of x, unfolded scaleR_conv_of_real] in auto)
3637  then show ?thesis
3638    by (simp add: cos_coeff_def ac_simps)
3639qed
3640
3641lemma sum_pos_lt_pair:
3642  fixes f :: "nat \<Rightarrow> real"
3643  assumes f: "summable f" and fplus: "\<And>d. 0 < f (k + (Suc(Suc 0) * d)) + f (k + ((Suc (Suc 0) * d) + 1))"
3644  shows "sum f {..<k} < suminf f"
3645proof -
3646  have "(\<lambda>n. \<Sum>n = n * Suc (Suc 0)..<n * Suc (Suc 0) +  Suc (Suc 0). f (n + k)) 
3647             sums (\<Sum>n. f (n + k))"
3648  proof (rule sums_group)
3649    show "(\<lambda>n. f (n + k)) sums (\<Sum>n. f (n + k))"
3650      by (simp add: f summable_iff_shift summable_sums)
3651  qed auto
3652  with fplus have "0 < (\<Sum>n. f (n + k))"
3653    apply (simp add: add.commute)
3654    apply (metis (no_types, lifting) suminf_pos summable_def sums_unique)
3655    done
3656  then show ?thesis
3657    by (simp add: f suminf_minus_initial_segment)
3658qed
3659
3660lemma cos_two_less_zero [simp]: "cos 2 < (0::real)"
3661proof -
3662  note fact_Suc [simp del]
3663  from sums_minus [OF cos_paired]
3664  have *: "(\<lambda>n. - ((- 1) ^ n * 2 ^ (2 * n) / fact (2 * n))) sums - cos (2::real)"
3665    by simp
3666  then have sm: "summable (\<lambda>n. - ((- 1::real) ^ n * 2 ^ (2 * n) / (fact (2 * n))))"
3667    by (rule sums_summable)
3668  have "0 < (\<Sum>n<Suc (Suc (Suc 0)). - ((- 1::real) ^ n * 2 ^ (2 * n) / (fact (2 * n))))"
3669    by (simp add: fact_num_eq_if power_eq_if)
3670  moreover have "(\<Sum>n<Suc (Suc (Suc 0)). - ((- 1::real) ^ n  * 2 ^ (2 * n) / (fact (2 * n)))) <
3671    (\<Sum>n. - ((- 1) ^ n * 2 ^ (2 * n) / (fact (2 * n))))"
3672  proof -
3673    {
3674      fix d
3675      let ?six4d = "Suc (Suc (Suc (Suc (Suc (Suc (4 * d))))))"
3676      have "(4::real) * (fact (?six4d)) < (Suc (Suc (?six4d)) * fact (Suc (?six4d)))"
3677        unfolding of_nat_mult by (rule mult_strict_mono) (simp_all add: fact_less_mono)
3678      then have "(4::real) * (fact (?six4d)) < (fact (Suc (Suc (?six4d))))"
3679        by (simp only: fact_Suc [of "Suc (?six4d)"] of_nat_mult of_nat_fact)
3680      then have "(4::real) * inverse (fact (Suc (Suc (?six4d)))) < inverse (fact (?six4d))"
3681        by (simp add: inverse_eq_divide less_divide_eq)
3682    }
3683    then show ?thesis
3684      by (force intro!: sum_pos_lt_pair [OF sm] simp add: divide_inverse algebra_simps)
3685  qed
3686  ultimately have "0 < (\<Sum>n. - ((- 1::real) ^ n * 2 ^ (2 * n) / (fact (2 * n))))"
3687    by (rule order_less_trans)
3688  moreover from * have "- cos 2 = (\<Sum>n. - ((- 1::real) ^ n * 2 ^ (2 * n) / (fact (2 * n))))"
3689    by (rule sums_unique)
3690  ultimately have "(0::real) < - cos 2" by simp
3691  then show ?thesis by simp
3692qed
3693
3694lemmas cos_two_neq_zero [simp] = cos_two_less_zero [THEN less_imp_neq]
3695lemmas cos_two_le_zero [simp] = cos_two_less_zero [THEN order_less_imp_le]
3696
3697lemma cos_is_zero: "\<exists>!x::real. 0 \<le> x \<and> x \<le> 2 \<and> cos x = 0"
3698proof (rule ex_ex1I)
3699  show "\<exists>x::real. 0 \<le> x \<and> x \<le> 2 \<and> cos x = 0"
3700    by (rule IVT2) simp_all
3701next
3702  fix a b :: real
3703  assume ab: "0 \<le> a \<and> a \<le> 2 \<and> cos a = 0" "0 \<le> b \<and> b \<le> 2 \<and> cos b = 0"
3704  have cosd: "\<And>x::real. cos differentiable (at x)"
3705    unfolding real_differentiable_def by (auto intro: DERIV_cos)
3706  show "a = b"
3707  proof (cases a b rule: linorder_cases)
3708    case less
3709    then obtain z where "a < z" "z < b" "(cos has_real_derivative 0) (at z)"
3710      using Rolle by (metis cosd isCont_cos ab)
3711    then have "sin z = 0"
3712      using DERIV_cos DERIV_unique neg_equal_0_iff_equal by blast
3713    then show ?thesis
3714      by (metis \<open>a < z\<close> \<open>z < b\<close> ab order_less_le_trans less_le sin_gt_zero_02)
3715  next
3716    case greater
3717    then obtain z where "b < z" "z < a" "(cos has_real_derivative 0) (at z)"
3718      using Rolle by (metis cosd isCont_cos ab)
3719    then have "sin z = 0"
3720      using DERIV_cos DERIV_unique neg_equal_0_iff_equal by blast
3721    then show ?thesis
3722      by (metis \<open>b < z\<close> \<open>z < a\<close> ab order_less_le_trans less_le sin_gt_zero_02)
3723  qed auto
3724qed
3725
3726lemma pi_half: "pi/2 = (THE x. 0 \<le> x \<and> x \<le> 2 \<and> cos x = 0)"
3727  by (simp add: pi_def)
3728
3729lemma cos_pi_half [simp]: "cos (pi/2) = 0"
3730  by (simp add: pi_half cos_is_zero [THEN theI'])
3731
3732lemma cos_of_real_pi_half [simp]: "cos ((of_real pi/2) :: 'a) = 0"
3733  if "SORT_CONSTRAINT('a::{real_field,banach,real_normed_algebra_1})"
3734  by (metis cos_pi_half cos_of_real eq_numeral_simps(4)
3735      nonzero_of_real_divide of_real_0 of_real_numeral)
3736
3737lemma pi_half_gt_zero [simp]: "0 < pi/2"
3738proof -
3739  have "0 \<le> pi/2"
3740    by (simp add: pi_half cos_is_zero [THEN theI'])
3741  then show ?thesis
3742    by (metis cos_pi_half cos_zero less_eq_real_def one_neq_zero)
3743qed
3744
3745lemmas pi_half_neq_zero [simp] = pi_half_gt_zero [THEN less_imp_neq, symmetric]
3746lemmas pi_half_ge_zero [simp] = pi_half_gt_zero [THEN order_less_imp_le]
3747
3748lemma pi_half_less_two [simp]: "pi/2 < 2"
3749proof -
3750  have "pi/2 \<le> 2"
3751    by (simp add: pi_half cos_is_zero [THEN theI'])
3752  then show ?thesis
3753    by (metis cos_pi_half cos_two_neq_zero le_less)
3754qed
3755
3756lemmas pi_half_neq_two [simp] = pi_half_less_two [THEN less_imp_neq]
3757lemmas pi_half_le_two [simp] =  pi_half_less_two [THEN order_less_imp_le]
3758
3759lemma pi_gt_zero [simp]: "0 < pi"
3760  using pi_half_gt_zero by simp
3761
3762lemma pi_ge_zero [simp]: "0 \<le> pi"
3763  by (rule pi_gt_zero [THEN order_less_imp_le])
3764
3765lemma pi_neq_zero [simp]: "pi \<noteq> 0"
3766  by (rule pi_gt_zero [THEN less_imp_neq, symmetric])
3767
3768lemma pi_not_less_zero [simp]: "\<not> pi < 0"
3769  by (simp add: linorder_not_less)
3770
3771lemma minus_pi_half_less_zero: "-(pi/2) < 0"
3772  by simp
3773
3774lemma m2pi_less_pi: "- (2*pi) < pi"
3775  by simp
3776
3777lemma sin_pi_half [simp]: "sin(pi/2) = 1"
3778  using sin_cos_squared_add2 [where x = "pi/2"]
3779  using sin_gt_zero_02 [OF pi_half_gt_zero pi_half_less_two]
3780  by (simp add: power2_eq_1_iff)
3781
3782lemma sin_of_real_pi_half [simp]: "sin ((of_real pi/2) :: 'a) = 1"
3783  if "SORT_CONSTRAINT('a::{real_field,banach,real_normed_algebra_1})"
3784  using sin_pi_half
3785  by (metis sin_pi_half eq_numeral_simps(4) nonzero_of_real_divide of_real_1 of_real_numeral sin_of_real)
3786
3787lemma sin_cos_eq: "sin x = cos (of_real pi/2 - x)"
3788  for x :: "'a::{real_normed_field,banach}"
3789  by (simp add: cos_diff)
3790
3791lemma minus_sin_cos_eq: "- sin x = cos (x + of_real pi/2)"
3792  for x :: "'a::{real_normed_field,banach}"
3793  by (simp add: cos_add nonzero_of_real_divide)
3794
3795lemma cos_sin_eq: "cos x = sin (of_real pi/2 - x)"
3796  for x :: "'a::{real_normed_field,banach}"
3797  using sin_cos_eq [of "of_real pi/2 - x"] by simp
3798
3799lemma sin_add: "sin (x + y) = sin x * cos y + cos x * sin y"
3800  for x :: "'a::{real_normed_field,banach}"
3801  using cos_add [of "of_real pi/2 - x" "-y"]
3802  by (simp add: cos_sin_eq) (simp add: sin_cos_eq)
3803
3804lemma sin_diff: "sin (x - y) = sin x * cos y - cos x * sin y"
3805  for x :: "'a::{real_normed_field,banach}"
3806  using sin_add [of x "- y"] by simp
3807
3808lemma sin_double: "sin(2 * x) = 2 * sin x * cos x"
3809  for x :: "'a::{real_normed_field,banach}"
3810  using sin_add [where x=x and y=x] by simp
3811
3812lemma cos_of_real_pi [simp]: "cos (of_real pi) = -1"
3813  using cos_add [where x = "pi/2" and y = "pi/2"]
3814  by (simp add: cos_of_real)
3815
3816lemma sin_of_real_pi [simp]: "sin (of_real pi) = 0"
3817  using sin_add [where x = "pi/2" and y = "pi/2"]
3818  by (simp add: sin_of_real)
3819
3820lemma cos_pi [simp]: "cos pi = -1"
3821  using cos_add [where x = "pi/2" and y = "pi/2"] by simp
3822
3823lemma sin_pi [simp]: "sin pi = 0"
3824  using sin_add [where x = "pi/2" and y = "pi/2"] by simp
3825
3826lemma sin_periodic_pi [simp]: "sin (x + pi) = - sin x"
3827  by (simp add: sin_add)
3828
3829lemma sin_periodic_pi2 [simp]: "sin (pi + x) = - sin x"
3830  by (simp add: sin_add)
3831
3832lemma cos_periodic_pi [simp]: "cos (x + pi) = - cos x"
3833  by (simp add: cos_add)
3834
3835lemma cos_periodic_pi2 [simp]: "cos (pi + x) = - cos x"
3836  by (simp add: cos_add)
3837
3838lemma sin_periodic [simp]: "sin (x + 2 * pi) = sin x"
3839  by (simp add: sin_add sin_double cos_double)
3840
3841lemma cos_periodic [simp]: "cos (x + 2 * pi) = cos x"
3842  by (simp add: cos_add sin_double cos_double)
3843
3844lemma cos_npi [simp]: "cos (real n * pi) = (- 1) ^ n"
3845  by (induct n) (auto simp: distrib_right)
3846
3847lemma cos_npi2 [simp]: "cos (pi * real n) = (- 1) ^ n"
3848  by (metis cos_npi mult.commute)
3849
3850lemma sin_npi [simp]: "sin (real n * pi) = 0"
3851  for n :: nat
3852  by (induct n) (auto simp: distrib_right)
3853
3854lemma sin_npi2 [simp]: "sin (pi * real n) = 0"
3855  for n :: nat
3856  by (simp add: mult.commute [of pi])
3857
3858lemma cos_two_pi [simp]: "cos (2 * pi) = 1"
3859  by (simp add: cos_double)
3860
3861lemma sin_two_pi [simp]: "sin (2 * pi) = 0"
3862  by (simp add: sin_double)
3863
3864lemma sin_times_sin: "sin w * sin z = (cos (w - z) - cos (w + z)) / 2"
3865  for w :: "'a::{real_normed_field,banach}"
3866  by (simp add: cos_diff cos_add)
3867
3868lemma sin_times_cos: "sin w * cos z = (sin (w + z) + sin (w - z)) / 2"
3869  for w :: "'a::{real_normed_field,banach}"
3870  by (simp add: sin_diff sin_add)
3871
3872lemma cos_times_sin: "cos w * sin z = (sin (w + z) - sin (w - z)) / 2"
3873  for w :: "'a::{real_normed_field,banach}"
3874  by (simp add: sin_diff sin_add)
3875
3876lemma cos_times_cos: "cos w * cos z = (cos (w - z) + cos (w + z)) / 2"
3877  for w :: "'a::{real_normed_field,banach}"
3878  by (simp add: cos_diff cos_add)
3879
3880lemma sin_plus_sin: "sin w + sin z = 2 * sin ((w + z) / 2) * cos ((w - z) / 2)"
3881  for w :: "'a::{real_normed_field,banach}" 
3882  apply (simp add: mult.assoc sin_times_cos)
3883  apply (simp add: field_simps)
3884  done
3885
3886lemma sin_diff_sin: "sin w - sin z = 2 * sin ((w - z) / 2) * cos ((w + z) / 2)"
3887  for w :: "'a::{real_normed_field,banach}"
3888  apply (simp add: mult.assoc sin_times_cos)
3889  apply (simp add: field_simps)
3890  done
3891
3892lemma cos_plus_cos: "cos w + cos z = 2 * cos ((w + z) / 2) * cos ((w - z) / 2)"
3893  for w :: "'a::{real_normed_field,banach,field}"
3894  apply (simp add: mult.assoc cos_times_cos)
3895  apply (simp add: field_simps)
3896  done
3897
3898lemma cos_diff_cos: "cos w - cos z = 2 * sin ((w + z) / 2) * sin ((z - w) / 2)"
3899  for w :: "'a::{real_normed_field,banach,field}"
3900  apply (simp add: mult.assoc sin_times_sin)
3901  apply (simp add: field_simps)
3902  done
3903
3904lemma cos_double_cos: "cos (2 * z) = 2 * cos z ^ 2 - 1"
3905  for z :: "'a::{real_normed_field,banach}"
3906  by (simp add: cos_double sin_squared_eq)
3907
3908lemma cos_double_sin: "cos (2 * z) = 1 - 2 * sin z ^ 2"
3909  for z :: "'a::{real_normed_field,banach}"
3910  by (simp add: cos_double sin_squared_eq)
3911
3912lemma sin_pi_minus [simp]: "sin (pi - x) = sin x"
3913  by (metis sin_minus sin_periodic_pi minus_minus uminus_add_conv_diff)
3914
3915lemma cos_pi_minus [simp]: "cos (pi - x) = - (cos x)"
3916  by (metis cos_minus cos_periodic_pi uminus_add_conv_diff)
3917
3918lemma sin_minus_pi [simp]: "sin (x - pi) = - (sin x)"
3919  by (simp add: sin_diff)
3920
3921lemma cos_minus_pi [simp]: "cos (x - pi) = - (cos x)"
3922  by (simp add: cos_diff)
3923
3924lemma sin_2pi_minus [simp]: "sin (2 * pi - x) = - (sin x)"
3925  by (metis sin_periodic_pi2 add_diff_eq mult_2 sin_pi_minus)
3926
3927lemma cos_2pi_minus [simp]: "cos (2 * pi - x) = cos x"
3928  by (metis (no_types, hide_lams) cos_add cos_minus cos_two_pi sin_minus sin_two_pi
3929      diff_0_right minus_diff_eq mult_1 mult_zero_left uminus_add_conv_diff)
3930
3931lemma sin_gt_zero2: "0 < x \<Longrightarrow> x < pi/2 \<Longrightarrow> 0 < sin x"
3932  by (metis sin_gt_zero_02 order_less_trans pi_half_less_two)
3933
3934lemma sin_less_zero:
3935  assumes "- pi/2 < x" and "x < 0"
3936  shows "sin x < 0"
3937proof -
3938  have "0 < sin (- x)"
3939    using assms by (simp only: sin_gt_zero2)
3940  then show ?thesis by simp
3941qed
3942
3943lemma pi_less_4: "pi < 4"
3944  using pi_half_less_two by auto
3945
3946lemma cos_gt_zero: "0 < x \<Longrightarrow> x < pi/2 \<Longrightarrow> 0 < cos x"
3947  by (simp add: cos_sin_eq sin_gt_zero2)
3948
3949lemma cos_gt_zero_pi: "-(pi/2) < x \<Longrightarrow> x < pi/2 \<Longrightarrow> 0 < cos x"
3950  using cos_gt_zero [of x] cos_gt_zero [of "-x"]
3951  by (cases rule: linorder_cases [of x 0]) auto
3952
3953lemma cos_ge_zero: "-(pi/2) \<le> x \<Longrightarrow> x \<le> pi/2 \<Longrightarrow> 0 \<le> cos x"
3954  by (auto simp: order_le_less cos_gt_zero_pi)
3955    (metis cos_pi_half eq_divide_eq eq_numeral_simps(4))
3956
3957lemma sin_gt_zero: "0 < x \<Longrightarrow> x < pi \<Longrightarrow> 0 < sin x"
3958  by (simp add: sin_cos_eq cos_gt_zero_pi)
3959
3960lemma sin_lt_zero: "pi < x \<Longrightarrow> x < 2 * pi \<Longrightarrow> sin x < 0"
3961  using sin_gt_zero [of "x - pi"]
3962  by (simp add: sin_diff)
3963
3964lemma pi_ge_two: "2 \<le> pi"
3965proof (rule ccontr)
3966  assume "\<not> ?thesis"
3967  then have "pi < 2" by auto
3968  have "\<exists>y > pi. y < 2 \<and> y < 2 * pi"
3969  proof (cases "2 < 2 * pi")
3970    case True
3971    with dense[OF \<open>pi < 2\<close>] show ?thesis by auto
3972  next
3973    case False
3974    have "pi < 2 * pi" by auto
3975    from dense[OF this] and False show ?thesis by auto
3976  qed
3977  then obtain y where "pi < y" and "y < 2" and "y < 2 * pi"
3978    by blast
3979  then have "0 < sin y"
3980    using sin_gt_zero_02 by auto
3981  moreover have "sin y < 0"
3982    using sin_gt_zero[of "y - pi"] \<open>pi < y\<close> and \<open>y < 2 * pi\<close> sin_periodic_pi[of "y - pi"]
3983    by auto
3984  ultimately show False by auto
3985qed
3986
3987lemma sin_ge_zero: "0 \<le> x \<Longrightarrow> x \<le> pi \<Longrightarrow> 0 \<le> sin x"
3988  by (auto simp: order_le_less sin_gt_zero)
3989
3990lemma sin_le_zero: "pi \<le> x \<Longrightarrow> x < 2 * pi \<Longrightarrow> sin x \<le> 0"
3991  using sin_ge_zero [of "x - pi"] by (simp add: sin_diff)
3992
3993lemma sin_pi_divide_n_ge_0 [simp]:
3994  assumes "n \<noteq> 0"
3995  shows "0 \<le> sin (pi / real n)"
3996  by (rule sin_ge_zero) (use assms in \<open>simp_all add: divide_simps\<close>)
3997
3998lemma sin_pi_divide_n_gt_0:
3999  assumes "2 \<le> n"
4000  shows "0 < sin (pi / real n)"
4001  by (rule sin_gt_zero) (use assms in \<open>simp_all add: divide_simps\<close>)
4002
4003text\<open>Proof resembles that of @{text cos_is_zero} but with @{term pi} for the upper bound\<close>
4004lemma cos_total:
4005  assumes y: "-1 \<le> y" "y \<le> 1"
4006  shows "\<exists>!x. 0 \<le> x \<and> x \<le> pi \<and> cos x = y"
4007proof (rule ex_ex1I)
4008  show "\<exists>x::real. 0 \<le> x \<and> x \<le> pi \<and> cos x = y"
4009    by (rule IVT2) (simp_all add: y)
4010next
4011  fix a b :: real
4012  assume ab: "0 \<le> a \<and> a \<le> pi \<and> cos a = y" "0 \<le> b \<and> b \<le> pi \<and> cos b = y"
4013  have cosd: "\<And>x::real. cos differentiable (at x)"
4014    unfolding real_differentiable_def by (auto intro: DERIV_cos)
4015  show "a = b"
4016  proof (cases a b rule: linorder_cases)
4017    case less
4018    then obtain z where "a < z" "z < b" "(cos has_real_derivative 0) (at z)"
4019      using Rolle by (metis cosd isCont_cos ab)
4020    then have "sin z = 0"
4021      using DERIV_cos DERIV_unique neg_equal_0_iff_equal by blast
4022    then show ?thesis
4023      by (metis \<open>a < z\<close> \<open>z < b\<close> ab order_less_le_trans less_le sin_gt_zero)
4024  next
4025    case greater
4026    then obtain z where "b < z" "z < a" "(cos has_real_derivative 0) (at z)"
4027      using Rolle by (metis cosd isCont_cos ab)
4028    then have "sin z = 0"
4029      using DERIV_cos DERIV_unique neg_equal_0_iff_equal by blast
4030    then show ?thesis
4031      by (metis \<open>b < z\<close> \<open>z < a\<close> ab order_less_le_trans less_le sin_gt_zero)
4032  qed auto
4033qed
4034
4035lemma sin_total:
4036  assumes y: "-1 \<le> y" "y \<le> 1"
4037  shows "\<exists>!x. - (pi/2) \<le> x \<and> x \<le> pi/2 \<and> sin x = y"
4038proof -
4039  from cos_total [OF y]
4040  obtain x where x: "0 \<le> x" "x \<le> pi" "cos x = y"
4041    and uniq: "\<And>x'. 0 \<le> x' \<Longrightarrow> x' \<le> pi \<Longrightarrow> cos x' = y \<Longrightarrow> x' = x "
4042    by blast
4043  show ?thesis
4044    unfolding sin_cos_eq
4045  proof (rule ex1I [where a="pi/2 - x"])
4046    show "- (pi/2) \<le> z \<and> z \<le> pi/2 \<and> cos (of_real pi/2 - z) = y \<Longrightarrow>
4047          z = pi/2 - x" for z
4048      using uniq [of "pi/2 -z"] by auto
4049  qed (use x in auto)
4050qed
4051
4052lemma cos_zero_lemma:
4053  assumes "0 \<le> x" "cos x = 0"
4054  shows "\<exists>n. odd n \<and> x = of_nat n * (pi/2) \<and> n > 0"
4055proof -
4056  have xle: "x < (1 + real_of_int \<lfloor>x/pi\<rfloor>) * pi"
4057    using floor_correct [of "x/pi"]
4058    by (simp add: add.commute divide_less_eq)
4059  obtain n where "real n * pi \<le> x" "x < real (Suc n) * pi"
4060  proof 
4061    show "real (nat \<lfloor>x / pi\<rfloor>) * pi \<le> x"
4062      using assms floor_divide_lower [of pi x] by auto
4063    show "x < real (Suc (nat \<lfloor>x / pi\<rfloor>)) * pi"
4064      using assms floor_divide_upper [of pi x]  by (simp add: xle)
4065  qed
4066  then have x: "0 \<le> x - n * pi" "(x - n * pi) \<le> pi" "cos (x - n * pi) = 0"
4067    by (auto simp: algebra_simps cos_diff assms)
4068  then have "\<exists>!x. 0 \<le> x \<and> x \<le> pi \<and> cos x = 0"
4069    by (auto simp: intro!: cos_total)
4070  then obtain \<theta> where \<theta>: "0 \<le> \<theta>" "\<theta> \<le> pi" "cos \<theta> = 0"
4071    and uniq: "\<And>\<phi>. 0 \<le> \<phi> \<Longrightarrow> \<phi> \<le> pi \<Longrightarrow> cos \<phi> = 0 \<Longrightarrow> \<phi> = \<theta>"
4072    by blast
4073  then have "x - real n * pi = \<theta>"
4074    using x by blast
4075  moreover have "pi/2 = \<theta>"
4076    using pi_half_ge_zero uniq by fastforce
4077  ultimately show ?thesis
4078    by (rule_tac x = "Suc (2 * n)" in exI) (simp add: algebra_simps)
4079qed
4080
4081lemma sin_zero_lemma: "0 \<le> x \<Longrightarrow> sin x = 0 \<Longrightarrow> \<exists>n::nat. even n \<and> x = real n * (pi/2)"
4082  using cos_zero_lemma [of "x + pi/2"]
4083  apply (clarsimp simp add: cos_add)
4084  apply (rule_tac x = "n - 1" in exI)
4085  apply (simp add: algebra_simps of_nat_diff)
4086  done
4087
4088lemma cos_zero_iff:
4089  "cos x = 0 \<longleftrightarrow> ((\<exists>n. odd n \<and> x = real n * (pi/2)) \<or> (\<exists>n. odd n \<and> x = - (real n * (pi/2))))"
4090  (is "?lhs = ?rhs")
4091proof -
4092  have *: "cos (real n * pi/2) = 0" if "odd n" for n :: nat
4093  proof -
4094    from that obtain m where "n = 2 * m + 1" ..
4095    then show ?thesis
4096      by (simp add: field_simps) (simp add: cos_add add_divide_distrib)
4097  qed
4098  show ?thesis
4099  proof
4100    show ?rhs if ?lhs
4101      using that cos_zero_lemma [of x] cos_zero_lemma [of "-x"] by force
4102    show ?lhs if ?rhs
4103      using that by (auto dest: * simp del: eq_divide_eq_numeral1)
4104  qed
4105qed
4106
4107lemma sin_zero_iff:
4108  "sin x = 0 \<longleftrightarrow> ((\<exists>n. even n \<and> x = real n * (pi/2)) \<or> (\<exists>n. even n \<and> x = - (real n * (pi/2))))"
4109  (is "?lhs = ?rhs")
4110proof
4111  show ?rhs if ?lhs
4112    using that sin_zero_lemma [of x] sin_zero_lemma [of "-x"] by force
4113  show ?lhs if ?rhs
4114    using that by (auto elim: evenE)
4115qed
4116
4117lemma cos_zero_iff_int: "cos x = 0 \<longleftrightarrow> (\<exists>n. odd n \<and> x = of_int n * (pi/2))"
4118proof -
4119  have 1: "\<And>n. odd n \<Longrightarrow> \<exists>i. odd i \<and> real n = real_of_int i"
4120    by (metis even_of_nat of_int_of_nat_eq)
4121  have 2: "\<And>n. odd n \<Longrightarrow> \<exists>i. odd i \<and> - (real n * pi) = real_of_int i * pi"
4122    by (metis even_minus even_of_nat mult.commute mult_minus_right of_int_minus of_int_of_nat_eq)
4123  have 3: "\<lbrakk>odd i;  \<forall>n. even n \<or> real_of_int i \<noteq> - (real n)\<rbrakk>
4124         \<Longrightarrow> \<exists>n. odd n \<and> real_of_int i = real n" for i
4125    by (cases i rule: int_cases2) auto
4126  show ?thesis
4127    by (force simp: cos_zero_iff intro!: 1 2 3)
4128qed
4129
4130lemma sin_zero_iff_int: "sin x = 0 \<longleftrightarrow> (\<exists>n. even n \<and> x = of_int n * (pi/2))"
4131proof safe
4132  assume "sin x = 0"
4133  then show "\<exists>n. even n \<and> x = of_int n * (pi/2)"
4134    apply (simp add: sin_zero_iff, safe)
4135     apply (metis even_of_nat of_int_of_nat_eq)
4136    apply (rule_tac x="- (int n)" in exI)
4137    apply simp
4138    done
4139next
4140  fix i :: int
4141  assume "even i"
4142  then show "sin (of_int i * (pi/2)) = 0"
4143    by (cases i rule: int_cases2, simp_all add: sin_zero_iff)
4144qed
4145
4146lemma sin_zero_iff_int2: "sin x = 0 \<longleftrightarrow> (\<exists>n::int. x = of_int n * pi)"
4147  apply (simp only: sin_zero_iff_int)
4148  apply (safe elim!: evenE)
4149   apply (simp_all add: field_simps)
4150  using dvd_triv_left apply fastforce
4151  done
4152
4153lemma sin_npi_int [simp]: "sin (pi * of_int n) = 0"
4154  by (simp add: sin_zero_iff_int2)
4155
4156lemma cos_monotone_0_pi:
4157  assumes "0 \<le> y" and "y < x" and "x \<le> pi"
4158  shows "cos x < cos y"
4159proof -
4160  have "- (x - y) < 0" using assms by auto
4161  from MVT2[OF \<open>y < x\<close> DERIV_cos]
4162  obtain z where "y < z" and "z < x" and cos_diff: "cos x - cos y = (x - y) * - sin z"
4163    by auto
4164  then have "0 < z" and "z < pi"
4165    using assms by auto
4166  then have "0 < sin z"
4167    using sin_gt_zero by auto
4168  then have "cos x - cos y < 0"
4169    unfolding cos_diff minus_mult_commute[symmetric]
4170    using \<open>- (x - y) < 0\<close> by (rule mult_pos_neg2)
4171  then show ?thesis by auto
4172qed
4173
4174lemma cos_monotone_0_pi_le:
4175  assumes "0 \<le> y" and "y \<le> x" and "x \<le> pi"
4176  shows "cos x \<le> cos y"
4177proof (cases "y < x")
4178  case True
4179  show ?thesis
4180    using cos_monotone_0_pi[OF \<open>0 \<le> y\<close> True \<open>x \<le> pi\<close>] by auto
4181next
4182  case False
4183  then have "y = x" using \<open>y \<le> x\<close> by auto
4184  then show ?thesis by auto
4185qed
4186
4187lemma cos_monotone_minus_pi_0:
4188  assumes "- pi \<le> y" and "y < x" and "x \<le> 0"
4189  shows "cos y < cos x"
4190proof -
4191  have "0 \<le> - x" and "- x < - y" and "- y \<le> pi"
4192    using assms by auto
4193  from cos_monotone_0_pi[OF this] show ?thesis
4194    unfolding cos_minus .
4195qed
4196
4197lemma cos_monotone_minus_pi_0':
4198  assumes "- pi \<le> y" and "y \<le> x" and "x \<le> 0"
4199  shows "cos y \<le> cos x"
4200proof (cases "y < x")
4201  case True
4202  show ?thesis using cos_monotone_minus_pi_0[OF \<open>-pi \<le> y\<close> True \<open>x \<le> 0\<close>]
4203    by auto
4204next
4205  case False
4206  then have "y = x" using \<open>y \<le> x\<close> by auto
4207  then show ?thesis by auto
4208qed
4209
4210lemma sin_monotone_2pi:
4211  assumes "- (pi/2) \<le> y" and "y < x" and "x \<le> pi/2"
4212  shows "sin y < sin x"
4213  unfolding sin_cos_eq
4214  using assms by (auto intro: cos_monotone_0_pi)
4215
4216lemma sin_monotone_2pi_le:
4217  assumes "- (pi/2) \<le> y" and "y \<le> x" and "x \<le> pi/2"
4218  shows "sin y \<le> sin x"
4219  by (metis assms le_less sin_monotone_2pi)
4220
4221lemma sin_x_le_x:
4222  fixes x :: real
4223  assumes x: "x \<ge> 0"
4224  shows "sin x \<le> x"
4225proof -
4226  let ?f = "\<lambda>x. x - sin x"
4227  from x have "?f x \<ge> ?f 0"
4228    apply (rule DERIV_nonneg_imp_nondecreasing)
4229    apply (intro allI impI exI[of _ "1 - cos x" for x])
4230    apply (auto intro!: derivative_eq_intros simp: field_simps)
4231    done
4232  then show "sin x \<le> x" by simp
4233qed
4234
4235lemma sin_x_ge_neg_x:
4236  fixes x :: real
4237  assumes x: "x \<ge> 0"
4238  shows "sin x \<ge> - x"
4239proof -
4240  let ?f = "\<lambda>x. x + sin x"
4241  from x have "?f x \<ge> ?f 0"
4242    apply (rule DERIV_nonneg_imp_nondecreasing)
4243    apply (intro allI impI exI[of _ "1 + cos x" for x])
4244    apply (auto intro!: derivative_eq_intros simp: field_simps real_0_le_add_iff)
4245    done
4246  then show "sin x \<ge> -x" by simp
4247qed
4248
4249lemma abs_sin_x_le_abs_x: "\<bar>sin x\<bar> \<le> \<bar>x\<bar>"
4250  for x :: real
4251  using sin_x_ge_neg_x [of x] sin_x_le_x [of x] sin_x_ge_neg_x [of "-x"] sin_x_le_x [of "-x"]
4252  by (auto simp: abs_real_def)
4253
4254
4255subsection \<open>More Corollaries about Sine and Cosine\<close>
4256
4257lemma sin_cos_npi [simp]: "sin (real (Suc (2 * n)) * pi/2) = (-1) ^ n"
4258proof -
4259  have "sin ((real n + 1/2) * pi) = cos (real n * pi)"
4260    by (auto simp: algebra_simps sin_add)
4261  then show ?thesis
4262    by (simp add: distrib_right add_divide_distrib add.commute mult.commute [of pi])
4263qed
4264
4265lemma cos_2npi [simp]: "cos (2 * real n * pi) = 1"
4266  for n :: nat
4267  by (cases "even n") (simp_all add: cos_double mult.assoc)
4268
4269lemma cos_3over2_pi [simp]: "cos (3/2*pi) = 0"
4270proof -
4271  have "cos (3/2*pi) = cos (pi + pi/2)"
4272    by simp
4273  also have "... = 0"
4274    by (subst cos_add, simp)
4275  finally show ?thesis .
4276qed
4277
4278lemma sin_2npi [simp]: "sin (2 * real n * pi) = 0"
4279  for n :: nat
4280  by (auto simp: mult.assoc sin_double)
4281
4282lemma sin_3over2_pi [simp]: "sin (3/2*pi) = - 1"
4283proof -
4284  have "sin (3/2*pi) = sin (pi + pi/2)"
4285    by simp
4286  also have "... = -1"
4287    by (subst sin_add, simp)
4288  finally show ?thesis .
4289qed
4290
4291lemma cos_pi_eq_zero [simp]: "cos (pi * real (Suc (2 * m)) / 2) = 0"
4292  by (simp only: cos_add sin_add of_nat_Suc distrib_right distrib_left add_divide_distrib, auto)
4293
4294lemma DERIV_cos_add [simp]: "DERIV (\<lambda>x. cos (x + k)) xa :> - sin (xa + k)"
4295  by (auto intro!: derivative_eq_intros)
4296
4297lemma sin_zero_norm_cos_one:
4298  fixes x :: "'a::{real_normed_field,banach}"
4299  assumes "sin x = 0"
4300  shows "norm (cos x) = 1"
4301  using sin_cos_squared_add [of x, unfolded assms]
4302  by (simp add: square_norm_one)
4303
4304lemma sin_zero_abs_cos_one: "sin x = 0 \<Longrightarrow> \<bar>cos x\<bar> = (1::real)"
4305  using sin_zero_norm_cos_one by fastforce
4306
4307lemma cos_one_sin_zero:
4308  fixes x :: "'a::{real_normed_field,banach}"
4309  assumes "cos x = 1"
4310  shows "sin x = 0"
4311  using sin_cos_squared_add [of x, unfolded assms]
4312  by simp
4313
4314lemma sin_times_pi_eq_0: "sin (x * pi) = 0 \<longleftrightarrow> x \<in> \<int>"
4315  by (simp add: sin_zero_iff_int2) (metis Ints_cases Ints_of_int)
4316
4317lemma cos_one_2pi: "cos x = 1 \<longleftrightarrow> (\<exists>n::nat. x = n * 2 * pi) \<or> (\<exists>n::nat. x = - (n * 2 * pi))"
4318  (is "?lhs = ?rhs")
4319proof
4320  assume ?lhs
4321  then have "sin x = 0"
4322    by (simp add: cos_one_sin_zero)
4323  then show ?rhs
4324  proof (simp only: sin_zero_iff, elim exE disjE conjE)
4325    fix n :: nat
4326    assume n: "even n" "x = real n * (pi/2)"
4327    then obtain m where m: "n = 2 * m"
4328      using dvdE by blast
4329    then have me: "even m" using \<open>?lhs\<close> n
4330      by (auto simp: field_simps) (metis one_neq_neg_one  power_minus_odd power_one)
4331    show ?rhs
4332      using m me n
4333      by (auto simp: field_simps elim!: evenE)
4334  next
4335    fix n :: nat
4336    assume n: "even n" "x = - (real n * (pi/2))"
4337    then obtain m where m: "n = 2 * m"
4338      using dvdE by blast
4339    then have me: "even m" using \<open>?lhs\<close> n
4340      by (auto simp: field_simps) (metis one_neq_neg_one  power_minus_odd power_one)
4341    show ?rhs
4342      using m me n
4343      by (auto simp: field_simps elim!: evenE)
4344  qed
4345next
4346  assume ?rhs
4347  then show "cos x = 1"
4348    by (metis cos_2npi cos_minus mult.assoc mult.left_commute)
4349qed
4350
4351lemma cos_one_2pi_int: "cos x = 1 \<longleftrightarrow> (\<exists>n::int. x = n * 2 * pi)" (is "?lhs = ?rhs")
4352proof
4353  assume "cos x = 1"
4354  then show ?rhs
4355    by (metis cos_one_2pi mult.commute mult_minus_right of_int_minus of_int_of_nat_eq)
4356next
4357  assume ?rhs
4358  then show "cos x = 1"
4359    by (clarsimp simp add: cos_one_2pi) (metis mult_minus_right of_int_of_nat)
4360qed
4361
4362lemma cos_npi_int [simp]:
4363  fixes n::int shows "cos (pi * of_int n) = (if even n then 1 else -1)"
4364    by (auto simp: algebra_simps cos_one_2pi_int elim!: oddE evenE)
4365
4366lemma sin_cos_sqrt: "0 \<le> sin x \<Longrightarrow> sin x = sqrt (1 - (cos(x) ^ 2))"
4367  using sin_squared_eq real_sqrt_unique by fastforce
4368
4369lemma sin_eq_0_pi: "- pi < x \<Longrightarrow> x < pi \<Longrightarrow> sin x = 0 \<Longrightarrow> x = 0"
4370  by (metis sin_gt_zero sin_minus minus_less_iff neg_0_less_iff_less not_less_iff_gr_or_eq)
4371
4372lemma cos_treble_cos: "cos (3 * x) = 4 * cos x ^ 3 - 3 * cos x"
4373  for x :: "'a::{real_normed_field,banach}"
4374proof -
4375  have *: "(sin x * (sin x * 3)) = 3 - (cos x * (cos x * 3))"
4376    by (simp add: mult.assoc [symmetric] sin_squared_eq [unfolded power2_eq_square])
4377  have "cos(3 * x) = cos(2*x + x)"
4378    by simp
4379  also have "\<dots> = 4 * cos x ^ 3 - 3 * cos x"
4380    apply (simp only: cos_add cos_double sin_double)
4381    apply (simp add: * field_simps power2_eq_square power3_eq_cube)
4382    done
4383  finally show ?thesis .
4384qed
4385
4386lemma cos_45: "cos (pi / 4) = sqrt 2 / 2"
4387proof -
4388  let ?c = "cos (pi / 4)"
4389  let ?s = "sin (pi / 4)"
4390  have nonneg: "0 \<le> ?c"
4391    by (simp add: cos_ge_zero)
4392  have "0 = cos (pi / 4 + pi / 4)"
4393    by simp
4394  also have "cos (pi / 4 + pi / 4) = ?c\<^sup>2 - ?s\<^sup>2"
4395    by (simp only: cos_add power2_eq_square)
4396  also have "\<dots> = 2 * ?c\<^sup>2 - 1"
4397    by (simp add: sin_squared_eq)
4398  finally have "?c\<^sup>2 = (sqrt 2 / 2)\<^sup>2"
4399    by (simp add: power_divide)
4400  then show ?thesis
4401    using nonneg by (rule power2_eq_imp_eq) simp
4402qed
4403
4404lemma cos_30: "cos (pi / 6) = sqrt 3/2"
4405proof -
4406  let ?c = "cos (pi / 6)"
4407  let ?s = "sin (pi / 6)"
4408  have pos_c: "0 < ?c"
4409    by (rule cos_gt_zero) simp_all
4410  have "0 = cos (pi / 6 + pi / 6 + pi / 6)"
4411    by simp
4412  also have "\<dots> = (?c * ?c - ?s * ?s) * ?c - (?s * ?c + ?c * ?s) * ?s"
4413    by (simp only: cos_add sin_add)
4414  also have "\<dots> = ?c * (?c\<^sup>2 - 3 * ?s\<^sup>2)"
4415    by (simp add: algebra_simps power2_eq_square)
4416  finally have "?c\<^sup>2 = (sqrt 3/2)\<^sup>2"
4417    using pos_c by (simp add: sin_squared_eq power_divide)
4418  then show ?thesis
4419    using pos_c [THEN order_less_imp_le]
4420    by (rule power2_eq_imp_eq) simp
4421qed
4422
4423lemma sin_45: "sin (pi / 4) = sqrt 2 / 2"
4424  by (simp add: sin_cos_eq cos_45)
4425
4426lemma sin_60: "sin (pi / 3) = sqrt 3/2"
4427  by (simp add: sin_cos_eq cos_30)
4428
4429lemma cos_60: "cos (pi / 3) = 1 / 2"
4430proof -
4431  have "0 \<le> cos (pi / 3)"
4432    by (rule cos_ge_zero) (use pi_half_ge_zero in \<open>linarith+\<close>)
4433  then show ?thesis
4434    by (simp add: cos_squared_eq sin_60 power_divide power2_eq_imp_eq)
4435qed
4436
4437lemma sin_30: "sin (pi / 6) = 1 / 2"
4438  by (simp add: sin_cos_eq cos_60)
4439
4440lemma cos_integer_2pi: "n \<in> \<int> \<Longrightarrow> cos(2 * pi * n) = 1"
4441  by (metis Ints_cases cos_one_2pi_int mult.assoc mult.commute)
4442
4443lemma sin_integer_2pi: "n \<in> \<int> \<Longrightarrow> sin(2 * pi * n) = 0"
4444  by (metis sin_two_pi Ints_mult mult.assoc mult.commute sin_times_pi_eq_0)
4445
4446lemma cos_int_2pin [simp]: "cos ((2 * pi) * of_int n) = 1"
4447  by (simp add: cos_one_2pi_int)
4448
4449lemma sin_int_2pin [simp]: "sin ((2 * pi) * of_int n) = 0"
4450  by (metis Ints_of_int sin_integer_2pi)
4451
4452lemma sincos_principal_value: "\<exists>y. (- pi < y \<and> y \<le> pi) \<and> (sin y = sin x \<and> cos y = cos x)"
4453  apply (rule exI [where x="pi - (2 * pi) * frac ((pi - x) / (2 * pi))"])
4454  apply (auto simp: field_simps frac_lt_1)
4455   apply (simp_all add: frac_def divide_simps)
4456   apply (simp_all add: add_divide_distrib diff_divide_distrib)
4457   apply (simp_all add: sin_diff cos_diff mult.assoc [symmetric] cos_integer_2pi sin_integer_2pi)
4458  done
4459
4460
4461subsection \<open>Tangent\<close>
4462
4463definition tan :: "'a \<Rightarrow> 'a::{real_normed_field,banach}"
4464  where "tan = (\<lambda>x. sin x / cos x)"
4465
4466lemma tan_of_real: "of_real (tan x) = (tan (of_real x) :: 'a::{real_normed_field,banach})"
4467  by (simp add: tan_def sin_of_real cos_of_real)
4468
4469lemma tan_in_Reals [simp]: "z \<in> \<real> \<Longrightarrow> tan z \<in> \<real>"
4470  for z :: "'a::{real_normed_field,banach}"
4471  by (simp add: tan_def)
4472
4473lemma tan_zero [simp]: "tan 0 = 0"
4474  by (simp add: tan_def)
4475
4476lemma tan_pi [simp]: "tan pi = 0"
4477  by (simp add: tan_def)
4478
4479lemma tan_npi [simp]: "tan (real n * pi) = 0"
4480  for n :: nat
4481  by (simp add: tan_def)
4482
4483lemma tan_minus [simp]: "tan (- x) = - tan x"
4484  by (simp add: tan_def)
4485
4486lemma tan_periodic [simp]: "tan (x + 2 * pi) = tan x"
4487  by (simp add: tan_def)
4488
4489lemma lemma_tan_add1: "cos x \<noteq> 0 \<Longrightarrow> cos y \<noteq> 0 \<Longrightarrow> 1 - tan x * tan y = cos (x + y)/(cos x * cos y)"
4490  by (simp add: tan_def cos_add field_simps)
4491
4492lemma add_tan_eq: "cos x \<noteq> 0 \<Longrightarrow> cos y \<noteq> 0 \<Longrightarrow> tan x + tan y = sin(x + y)/(cos x * cos y)"
4493  for x :: "'a::{real_normed_field,banach}"
4494  by (simp add: tan_def sin_add field_simps)
4495
4496lemma tan_add:
4497  "cos x \<noteq> 0 \<Longrightarrow> cos y \<noteq> 0 \<Longrightarrow> cos (x + y) \<noteq> 0 \<Longrightarrow> tan (x + y) = (tan x + tan y)/(1 - tan x * tan y)"
4498  for x :: "'a::{real_normed_field,banach}"
4499  by (simp add: add_tan_eq lemma_tan_add1 field_simps) (simp add: tan_def)
4500
4501lemma tan_double: "cos x \<noteq> 0 \<Longrightarrow> cos (2 * x) \<noteq> 0 \<Longrightarrow> tan (2 * x) = (2 * tan x) / (1 - (tan x)\<^sup>2)"
4502  for x :: "'a::{real_normed_field,banach}"
4503  using tan_add [of x x] by (simp add: power2_eq_square)
4504
4505lemma tan_gt_zero: "0 < x \<Longrightarrow> x < pi/2 \<Longrightarrow> 0 < tan x"
4506  by (simp add: tan_def zero_less_divide_iff sin_gt_zero2 cos_gt_zero_pi)
4507
4508lemma tan_less_zero:
4509  assumes "- pi/2 < x" and "x < 0"
4510  shows "tan x < 0"
4511proof -
4512  have "0 < tan (- x)"
4513    using assms by (simp only: tan_gt_zero)
4514  then show ?thesis by simp
4515qed
4516
4517lemma tan_half: "tan x = sin (2 * x) / (cos (2 * x) + 1)"
4518  for x :: "'a::{real_normed_field,banach,field}"
4519  unfolding tan_def sin_double cos_double sin_squared_eq
4520  by (simp add: power2_eq_square)
4521
4522lemma tan_30: "tan (pi / 6) = 1 / sqrt 3"
4523  unfolding tan_def by (simp add: sin_30 cos_30)
4524
4525lemma tan_45: "tan (pi / 4) = 1"
4526  unfolding tan_def by (simp add: sin_45 cos_45)
4527
4528lemma tan_60: "tan (pi / 3) = sqrt 3"
4529  unfolding tan_def by (simp add: sin_60 cos_60)
4530
4531lemma DERIV_tan [simp]: "cos x \<noteq> 0 \<Longrightarrow> DERIV tan x :> inverse ((cos x)\<^sup>2)"
4532  for x :: "'a::{real_normed_field,banach}"
4533  unfolding tan_def
4534  by (auto intro!: derivative_eq_intros, simp add: divide_inverse power2_eq_square)
4535
4536declare DERIV_tan[THEN DERIV_chain2, derivative_intros]
4537  and DERIV_tan[THEN DERIV_chain2, unfolded has_field_derivative_def, derivative_intros]
4538
4539lemmas has_derivative_tan[derivative_intros] = DERIV_tan[THEN DERIV_compose_FDERIV]
4540
4541lemma isCont_tan: "cos x \<noteq> 0 \<Longrightarrow> isCont tan x"
4542  for x :: "'a::{real_normed_field,banach}"
4543  by (rule DERIV_tan [THEN DERIV_isCont])
4544
4545lemma isCont_tan' [simp,continuous_intros]:
4546  fixes a :: "'a::{real_normed_field,banach}" and f :: "'a \<Rightarrow> 'a"
4547  shows "isCont f a \<Longrightarrow> cos (f a) \<noteq> 0 \<Longrightarrow> isCont (\<lambda>x. tan (f x)) a"
4548  by (rule isCont_o2 [OF _ isCont_tan])
4549
4550lemma tendsto_tan [tendsto_intros]:
4551  fixes f :: "'a \<Rightarrow> 'a::{real_normed_field,banach}"
4552  shows "(f \<longlongrightarrow> a) F \<Longrightarrow> cos a \<noteq> 0 \<Longrightarrow> ((\<lambda>x. tan (f x)) \<longlongrightarrow> tan a) F"
4553  by (rule isCont_tendsto_compose [OF isCont_tan])
4554
4555lemma continuous_tan:
4556  fixes f :: "'a \<Rightarrow> 'a::{real_normed_field,banach}"
4557  shows "continuous F f \<Longrightarrow> cos (f (Lim F (\<lambda>x. x))) \<noteq> 0 \<Longrightarrow> continuous F (\<lambda>x. tan (f x))"
4558  unfolding continuous_def by (rule tendsto_tan)
4559
4560lemma continuous_on_tan [continuous_intros]:
4561  fixes f :: "'a \<Rightarrow> 'a::{real_normed_field,banach}"
4562  shows "continuous_on s f \<Longrightarrow> (\<forall>x\<in>s. cos (f x) \<noteq> 0) \<Longrightarrow> continuous_on s (\<lambda>x. tan (f x))"
4563  unfolding continuous_on_def by (auto intro: tendsto_tan)
4564
4565lemma continuous_within_tan [continuous_intros]:
4566  fixes f :: "'a \<Rightarrow> 'a::{real_normed_field,banach}"
4567  shows "continuous (at x within s) f \<Longrightarrow>
4568    cos (f x) \<noteq> 0 \<Longrightarrow> continuous (at x within s) (\<lambda>x. tan (f x))"
4569  unfolding continuous_within by (rule tendsto_tan)
4570
4571lemma LIM_cos_div_sin: "(\<lambda>x. cos(x)/sin(x)) \<midarrow>pi/2\<rightarrow> 0"
4572  by (rule LIM_cong_limit, (rule tendsto_intros)+, simp_all)
4573
4574lemma lemma_tan_total: 
4575  assumes "0 < y" shows "\<exists>x. 0 < x \<and> x < pi/2 \<and> y < tan x"
4576proof -
4577  obtain s where "0 < s" 
4578    and s: "\<And>x. \<lbrakk>x \<noteq> pi/2; norm (x - pi/2) < s\<rbrakk> \<Longrightarrow> norm (cos x / sin x - 0) < inverse y"
4579    using LIM_D [OF LIM_cos_div_sin, of "inverse y"] that assms by force
4580  obtain e where e: "0 < e" "e < s" "e < pi/2"
4581    using \<open>0 < s\<close> field_lbound_gt_zero pi_half_gt_zero by blast
4582  show ?thesis
4583  proof (intro exI conjI)
4584    have "0 < sin e" "0 < cos e"
4585      using e by (auto intro: cos_gt_zero sin_gt_zero2 simp: mult.commute)
4586    then 
4587    show "y < tan (pi/2 - e)"
4588      using s [of "pi/2 - e"] e assms
4589      by (simp add: tan_def sin_diff cos_diff) (simp add: field_simps split: if_split_asm)
4590  qed (use e in auto)
4591qed
4592
4593lemma tan_total_pos: 
4594  assumes "0 \<le> y" shows "\<exists>x. 0 \<le> x \<and> x < pi/2 \<and> tan x = y"
4595proof (cases "y = 0")
4596  case True
4597  then show ?thesis
4598    using pi_half_gt_zero tan_zero by blast
4599next
4600  case False
4601  with assms have "y > 0"
4602    by linarith
4603  obtain x where x: "0 < x" "x < pi/2" "y < tan x"
4604    using lemma_tan_total \<open>0 < y\<close> by blast
4605  have "\<exists>u\<ge>0. u \<le> x \<and> tan u = y"
4606  proof (intro IVT allI impI)
4607    show "isCont tan u" if "0 \<le> u \<and> u \<le> x" for u
4608    proof -
4609      have "cos u \<noteq> 0"
4610        using antisym_conv2 cos_gt_zero that x(2) by fastforce
4611      with assms show ?thesis
4612        by (auto intro!: DERIV_tan [THEN DERIV_isCont])
4613    qed
4614  qed (use assms x in auto)
4615  then show ?thesis
4616    using x(2) by auto
4617qed
4618    
4619lemma lemma_tan_total1: "\<exists>x. -(pi/2) < x \<and> x < (pi/2) \<and> tan x = y"
4620proof (cases "0::real" y rule: le_cases)
4621  case le
4622  then show ?thesis
4623    by (meson less_le_trans minus_pi_half_less_zero tan_total_pos)
4624next
4625  case ge
4626  with tan_total_pos [of "-y"] obtain x where "0 \<le> x" "x < pi / 2" "tan x = - y"
4627    by force
4628  then show ?thesis
4629    by (rule_tac x="-x" in exI) auto
4630qed
4631
4632proposition tan_total: "\<exists>! x. -(pi/2) < x \<and> x < (pi/2) \<and> tan x = y"
4633proof -
4634  have "u = v" if u: "- (pi / 2) < u" "u < pi / 2" and v: "- (pi / 2) < v" "v < pi / 2"
4635    and eq: "tan u = tan v" for u v
4636  proof (cases u v rule: linorder_cases)
4637    case less
4638    have "\<And>x. u \<le> x \<and> x \<le> v \<longrightarrow> isCont tan x"
4639      by (metis cos_gt_zero_pi isCont_tan less_numeral_extra(3) less_trans order.not_eq_order_implies_strict u v)
4640    moreover have "\<And>x. u < x \<and> x < v \<Longrightarrow> tan differentiable (at x)"
4641      by (metis DERIV_tan cos_gt_zero_pi differentiableI less_numeral_extra(3) order.strict_trans u(1) v(2))
4642    ultimately obtain z where "u < z" "z < v" "DERIV tan z :> 0"
4643      by (metis less Rolle eq)
4644    moreover have "cos z \<noteq> 0"
4645      by (metis (no_types) \<open>u < z\<close> \<open>z < v\<close> cos_gt_zero_pi less_le_trans linorder_not_less not_less_iff_gr_or_eq u(1) v(2))
4646    ultimately show ?thesis
4647      using DERIV_unique [OF _ DERIV_tan] by fastforce
4648  next
4649    case greater
4650    have "\<And>x. v \<le> x \<and> x \<le> u \<Longrightarrow> isCont tan x"
4651      by (metis cos_gt_zero_pi isCont_tan less_numeral_extra(3) less_trans order.not_eq_order_implies_strict u v)
4652    moreover have "\<And>x. v < x \<and> x < u \<Longrightarrow> tan differentiable (at x)"
4653      by (metis DERIV_tan cos_gt_zero_pi differentiableI less_numeral_extra(3) order.strict_trans u(2) v(1))
4654    ultimately obtain z where "v < z" "z < u" "DERIV tan z :> 0"
4655      by (metis greater Rolle eq)
4656    moreover have "cos z \<noteq> 0"
4657      by (metis  \<open>v < z\<close> \<open>z < u\<close> cos_gt_zero_pi less_le_trans linorder_not_less not_less_iff_gr_or_eq u(2) v(1))
4658    ultimately show ?thesis
4659      using DERIV_unique [OF _ DERIV_tan] by fastforce
4660  qed auto
4661  then have "\<exists>!x. - (pi / 2) < x \<and> x < pi / 2 \<and> tan x = y" 
4662    if x: "- (pi / 2) < x" "x < pi / 2" "tan x = y" for x
4663    using that by auto
4664  then show ?thesis
4665    using lemma_tan_total1 [where y = y]
4666    by auto
4667qed
4668
4669lemma tan_monotone:
4670  assumes "- (pi/2) < y" and "y < x" and "x < pi/2"
4671  shows "tan y < tan x"
4672proof -
4673  have "DERIV tan x' :> inverse ((cos x')\<^sup>2)" if "y \<le> x'" "x' \<le> x" for x'
4674  proof -
4675    have "-(pi/2) < x'" and "x' < pi/2"
4676      using that assms by auto
4677    with cos_gt_zero_pi have "cos x' \<noteq> 0" by force
4678    then show "DERIV tan x' :> inverse ((cos x')\<^sup>2)"
4679      by (rule DERIV_tan)
4680  qed
4681  from MVT2[OF \<open>y < x\<close> this]
4682  obtain z where "y < z" and "z < x"
4683    and tan_diff: "tan x - tan y = (x - y) * inverse ((cos z)\<^sup>2)" by auto
4684  then have "- (pi/2) < z" and "z < pi/2"
4685    using assms by auto
4686  then have "0 < cos z"
4687    using cos_gt_zero_pi by auto
4688  then have inv_pos: "0 < inverse ((cos z)\<^sup>2)"
4689    by auto
4690  have "0 < x - y" using \<open>y < x\<close> by auto
4691  with inv_pos have "0 < tan x - tan y"
4692    unfolding tan_diff by auto
4693  then show ?thesis by auto
4694qed
4695
4696lemma tan_monotone':
4697  assumes "- (pi/2) < y"
4698    and "y < pi/2"
4699    and "- (pi/2) < x"
4700    and "x < pi/2"
4701  shows "y < x \<longleftrightarrow> tan y < tan x"
4702proof
4703  assume "y < x"
4704  then show "tan y < tan x"
4705    using tan_monotone and \<open>- (pi/2) < y\<close> and \<open>x < pi/2\<close> by auto
4706next
4707  assume "tan y < tan x"
4708  show "y < x"
4709  proof (rule ccontr)
4710    assume "\<not> ?thesis"
4711    then have "x \<le> y" by auto
4712    then have "tan x \<le> tan y"
4713    proof (cases "x = y")
4714      case True
4715      then show ?thesis by auto
4716    next
4717      case False
4718      then have "x < y" using \<open>x \<le> y\<close> by auto
4719      from tan_monotone[OF \<open>- (pi/2) < x\<close> this \<open>y < pi/2\<close>] show ?thesis
4720        by auto
4721    qed
4722    then show False
4723      using \<open>tan y < tan x\<close> by auto
4724  qed
4725qed
4726
4727lemma tan_inverse: "1 / (tan y) = tan (pi/2 - y)"
4728  unfolding tan_def sin_cos_eq[of y] cos_sin_eq[of y] by auto
4729
4730lemma tan_periodic_pi[simp]: "tan (x + pi) = tan x"
4731  by (simp add: tan_def)
4732
4733lemma tan_periodic_nat[simp]: "tan (x + real n * pi) = tan x"
4734  for n :: nat
4735proof (induct n arbitrary: x)
4736  case 0
4737  then show ?case by simp
4738next
4739  case (Suc n)
4740  have split_pi_off: "x + real (Suc n) * pi = (x + real n * pi) + pi"
4741    unfolding Suc_eq_plus1 of_nat_add  distrib_right by auto
4742  show ?case
4743    unfolding split_pi_off using Suc by auto
4744qed
4745
4746lemma tan_periodic_int[simp]: "tan (x + of_int i * pi) = tan x"
4747proof (cases "0 \<le> i")
4748  case True
4749  then have i_nat: "of_int i = of_int (nat i)" by auto
4750  show ?thesis unfolding i_nat
4751    by (metis of_int_of_nat_eq tan_periodic_nat)
4752next
4753  case False
4754  then have i_nat: "of_int i = - of_int (nat (- i))" by auto
4755  have "tan x = tan (x + of_int i * pi - of_int i * pi)"
4756    by auto
4757  also have "\<dots> = tan (x + of_int i * pi)"
4758    unfolding i_nat mult_minus_left diff_minus_eq_add
4759    by (metis of_int_of_nat_eq tan_periodic_nat)
4760  finally show ?thesis by auto
4761qed
4762
4763lemma tan_periodic_n[simp]: "tan (x + numeral n * pi) = tan x"
4764  using tan_periodic_int[of _ "numeral n" ] by simp
4765
4766lemma tan_minus_45: "tan (-(pi/4)) = -1"
4767  unfolding tan_def by (simp add: sin_45 cos_45)
4768
4769lemma tan_diff:
4770  "cos x \<noteq> 0 \<Longrightarrow> cos y \<noteq> 0 \<Longrightarrow> cos (x - y) \<noteq> 0 \<Longrightarrow> tan (x - y) = (tan x - tan y)/(1 + tan x * tan y)"
4771  for x :: "'a::{real_normed_field,banach}"
4772  using tan_add [of x "-y"] by simp
4773
4774lemma tan_pos_pi2_le: "0 \<le> x \<Longrightarrow> x < pi/2 \<Longrightarrow> 0 \<le> tan x"
4775  using less_eq_real_def tan_gt_zero by auto
4776
4777lemma cos_tan: "\<bar>x\<bar> < pi/2 \<Longrightarrow> cos x = 1 / sqrt (1 + tan x ^ 2)"
4778  using cos_gt_zero_pi [of x]
4779  by (simp add: divide_simps tan_def real_sqrt_divide abs_if split: if_split_asm)
4780
4781lemma sin_tan: "\<bar>x\<bar> < pi/2 \<Longrightarrow> sin x = tan x / sqrt (1 + tan x ^ 2)"
4782  using cos_gt_zero [of "x"] cos_gt_zero [of "-x"]
4783  by (force simp: divide_simps tan_def real_sqrt_divide abs_if split: if_split_asm)
4784
4785lemma tan_mono_le: "-(pi/2) < x \<Longrightarrow> x \<le> y \<Longrightarrow> y < pi/2 \<Longrightarrow> tan x \<le> tan y"
4786  using less_eq_real_def tan_monotone by auto
4787
4788lemma tan_mono_lt_eq:
4789  "-(pi/2) < x \<Longrightarrow> x < pi/2 \<Longrightarrow> -(pi/2) < y \<Longrightarrow> y < pi/2 \<Longrightarrow> tan x < tan y \<longleftrightarrow> x < y"
4790  using tan_monotone' by blast
4791
4792lemma tan_mono_le_eq:
4793  "-(pi/2) < x \<Longrightarrow> x < pi/2 \<Longrightarrow> -(pi/2) < y \<Longrightarrow> y < pi/2 \<Longrightarrow> tan x \<le> tan y \<longleftrightarrow> x \<le> y"
4794  by (meson tan_mono_le not_le tan_monotone)
4795
4796lemma tan_bound_pi2: "\<bar>x\<bar> < pi/4 \<Longrightarrow> \<bar>tan x\<bar> < 1"
4797  using tan_45 tan_monotone [of x "pi/4"] tan_monotone [of "-x" "pi/4"]
4798  by (auto simp: abs_if split: if_split_asm)
4799
4800lemma tan_cot: "tan(pi/2 - x) = inverse(tan x)"
4801  by (simp add: tan_def sin_diff cos_diff)
4802
4803
4804subsection \<open>Cotangent\<close>
4805
4806definition cot :: "'a \<Rightarrow> 'a::{real_normed_field,banach}"
4807  where "cot = (\<lambda>x. cos x / sin x)"
4808
4809lemma cot_of_real: "of_real (cot x) = (cot (of_real x) :: 'a::{real_normed_field,banach})"
4810  by (simp add: cot_def sin_of_real cos_of_real)
4811
4812lemma cot_in_Reals [simp]: "z \<in> \<real> \<Longrightarrow> cot z \<in> \<real>"
4813  for z :: "'a::{real_normed_field,banach}"
4814  by (simp add: cot_def)
4815
4816lemma cot_zero [simp]: "cot 0 = 0"
4817  by (simp add: cot_def)
4818
4819lemma cot_pi [simp]: "cot pi = 0"
4820  by (simp add: cot_def)
4821
4822lemma cot_npi [simp]: "cot (real n * pi) = 0"
4823  for n :: nat
4824  by (simp add: cot_def)
4825
4826lemma cot_minus [simp]: "cot (- x) = - cot x"
4827  by (simp add: cot_def)
4828
4829lemma cot_periodic [simp]: "cot (x + 2 * pi) = cot x"
4830  by (simp add: cot_def)
4831
4832lemma cot_altdef: "cot x = inverse (tan x)"
4833  by (simp add: cot_def tan_def)
4834
4835lemma tan_altdef: "tan x = inverse (cot x)"
4836  by (simp add: cot_def tan_def)
4837
4838lemma tan_cot': "tan (pi/2 - x) = cot x"
4839  by (simp add: tan_cot cot_altdef)
4840
4841lemma cot_gt_zero: "0 < x \<Longrightarrow> x < pi/2 \<Longrightarrow> 0 < cot x"
4842  by (simp add: cot_def zero_less_divide_iff sin_gt_zero2 cos_gt_zero_pi)
4843
4844lemma cot_less_zero:
4845  assumes lb: "- pi/2 < x" and "x < 0"
4846  shows "cot x < 0"
4847proof -
4848  have "0 < cot (- x)"
4849    using assms by (simp only: cot_gt_zero)
4850  then show ?thesis by simp
4851qed
4852
4853lemma DERIV_cot [simp]: "sin x \<noteq> 0 \<Longrightarrow> DERIV cot x :> -inverse ((sin x)\<^sup>2)"
4854  for x :: "'a::{real_normed_field,banach}"
4855  unfolding cot_def using cos_squared_eq[of x]
4856  by (auto intro!: derivative_eq_intros) (simp add: divide_inverse power2_eq_square)
4857
4858lemma isCont_cot: "sin x \<noteq> 0 \<Longrightarrow> isCont cot x"
4859  for x :: "'a::{real_normed_field,banach}"
4860  by (rule DERIV_cot [THEN DERIV_isCont])
4861
4862lemma isCont_cot' [simp,continuous_intros]:
4863  "isCont f a \<Longrightarrow> sin (f a) \<noteq> 0 \<Longrightarrow> isCont (\<lambda>x. cot (f x)) a"
4864  for a :: "'a::{real_normed_field,banach}" and f :: "'a \<Rightarrow> 'a"
4865  by (rule isCont_o2 [OF _ isCont_cot])
4866
4867lemma tendsto_cot [tendsto_intros]: "(f \<longlongrightarrow> a) F \<Longrightarrow> sin a \<noteq> 0 \<Longrightarrow> ((\<lambda>x. cot (f x)) \<longlongrightarrow> cot a) F"
4868  for f :: "'a \<Rightarrow> 'a::{real_normed_field,banach}"
4869  by (rule isCont_tendsto_compose [OF isCont_cot])
4870
4871lemma continuous_cot:
4872  "continuous F f \<Longrightarrow> sin (f (Lim F (\<lambda>x. x))) \<noteq> 0 \<Longrightarrow> continuous F (\<lambda>x. cot (f x))"
4873  for f :: "'a \<Rightarrow> 'a::{real_normed_field,banach}"
4874  unfolding continuous_def by (rule tendsto_cot)
4875
4876lemma continuous_on_cot [continuous_intros]:
4877  fixes f :: "'a \<Rightarrow> 'a::{real_normed_field,banach}"
4878  shows "continuous_on s f \<Longrightarrow> (\<forall>x\<in>s. sin (f x) \<noteq> 0) \<Longrightarrow> continuous_on s (\<lambda>x. cot (f x))"
4879  unfolding continuous_on_def by (auto intro: tendsto_cot)
4880
4881lemma continuous_within_cot [continuous_intros]:
4882  fixes f :: "'a \<Rightarrow> 'a::{real_normed_field,banach}"
4883  shows "continuous (at x within s) f \<Longrightarrow> sin (f x) \<noteq> 0 \<Longrightarrow> continuous (at x within s) (\<lambda>x. cot (f x))"
4884  unfolding continuous_within by (rule tendsto_cot)
4885
4886
4887subsection \<open>Inverse Trigonometric Functions\<close>
4888
4889definition arcsin :: "real \<Rightarrow> real"
4890  where "arcsin y = (THE x. -(pi/2) \<le> x \<and> x \<le> pi/2 \<and> sin x = y)"
4891
4892definition arccos :: "real \<Rightarrow> real"
4893  where "arccos y = (THE x. 0 \<le> x \<and> x \<le> pi \<and> cos x = y)"
4894
4895definition arctan :: "real \<Rightarrow> real"
4896  where "arctan y = (THE x. -(pi/2) < x \<and> x < pi/2 \<and> tan x = y)"
4897
4898lemma arcsin: "- 1 \<le> y \<Longrightarrow> y \<le> 1 \<Longrightarrow> - (pi/2) \<le> arcsin y \<and> arcsin y \<le> pi/2 \<and> sin (arcsin y) = y"
4899  unfolding arcsin_def by (rule theI' [OF sin_total])
4900
4901lemma arcsin_pi: "- 1 \<le> y \<Longrightarrow> y \<le> 1 \<Longrightarrow> - (pi/2) \<le> arcsin y \<and> arcsin y \<le> pi \<and> sin (arcsin y) = y"
4902  by (drule (1) arcsin) (force intro: order_trans)
4903
4904lemma sin_arcsin [simp]: "- 1 \<le> y \<Longrightarrow> y \<le> 1 \<Longrightarrow> sin (arcsin y) = y"
4905  by (blast dest: arcsin)
4906
4907lemma arcsin_bounded: "- 1 \<le> y \<Longrightarrow> y \<le> 1 \<Longrightarrow> - (pi/2) \<le> arcsin y \<and> arcsin y \<le> pi/2"
4908  by (blast dest: arcsin)
4909
4910lemma arcsin_lbound: "- 1 \<le> y \<Longrightarrow> y \<le> 1 \<Longrightarrow> - (pi/2) \<le> arcsin y"
4911  by (blast dest: arcsin)
4912
4913lemma arcsin_ubound: "- 1 \<le> y \<Longrightarrow> y \<le> 1 \<Longrightarrow> arcsin y \<le> pi/2"
4914  by (blast dest: arcsin)
4915
4916lemma arcsin_lt_bounded:
4917  assumes "- 1 < y" "y < 1"
4918  shows  "- (pi/2) < arcsin y \<and> arcsin y < pi/2"
4919proof -
4920  have "arcsin y \<noteq> pi/2"
4921    by (metis arcsin assms not_less not_less_iff_gr_or_eq sin_pi_half)
4922  moreover have "arcsin y \<noteq> - pi/2"
4923    by (metis arcsin assms minus_divide_left not_less not_less_iff_gr_or_eq sin_minus sin_pi_half)
4924  ultimately show ?thesis
4925    using arcsin_bounded [of y] assms by auto
4926qed
4927
4928lemma arcsin_sin: "- (pi/2) \<le> x \<Longrightarrow> x \<le> pi/2 \<Longrightarrow> arcsin (sin x) = x"
4929  unfolding arcsin_def
4930  using the1_equality [OF sin_total]  by simp
4931
4932lemma arcsin_0 [simp]: "arcsin 0 = 0"
4933  using arcsin_sin [of 0] by simp
4934
4935lemma arcsin_1 [simp]: "arcsin 1 = pi/2"
4936  using arcsin_sin [of "pi/2"] by simp
4937
4938lemma arcsin_minus_1 [simp]: "arcsin (- 1) = - (pi/2)"
4939  using arcsin_sin [of "- pi/2"] by simp
4940
4941lemma arcsin_minus: "- 1 \<le> x \<Longrightarrow> x \<le> 1 \<Longrightarrow> arcsin (- x) = - arcsin x"
4942  by (metis (no_types, hide_lams) arcsin arcsin_sin minus_minus neg_le_iff_le sin_minus)
4943
4944lemma arcsin_eq_iff: "\<bar>x\<bar> \<le> 1 \<Longrightarrow> \<bar>y\<bar> \<le> 1 \<Longrightarrow> arcsin x = arcsin y \<longleftrightarrow> x = y"
4945  by (metis abs_le_iff arcsin minus_le_iff)
4946
4947lemma cos_arcsin_nonzero: "- 1 < x \<Longrightarrow> x < 1 \<Longrightarrow> cos (arcsin x) \<noteq> 0"
4948  using arcsin_lt_bounded cos_gt_zero_pi by force
4949
4950lemma arccos: "- 1 \<le> y \<Longrightarrow> y \<le> 1 \<Longrightarrow> 0 \<le> arccos y \<and> arccos y \<le> pi \<and> cos (arccos y) = y"
4951  unfolding arccos_def by (rule theI' [OF cos_total])
4952
4953lemma cos_arccos [simp]: "- 1 \<le> y \<Longrightarrow> y \<le> 1 \<Longrightarrow> cos (arccos y) = y"
4954  by (blast dest: arccos)
4955
4956lemma arccos_bounded: "- 1 \<le> y \<Longrightarrow> y \<le> 1 \<Longrightarrow> 0 \<le> arccos y \<and> arccos y \<le> pi"
4957  by (blast dest: arccos)
4958
4959lemma arccos_lbound: "- 1 \<le> y \<Longrightarrow> y \<le> 1 \<Longrightarrow> 0 \<le> arccos y"
4960  by (blast dest: arccos)
4961
4962lemma arccos_ubound: "- 1 \<le> y \<Longrightarrow> y \<le> 1 \<Longrightarrow> arccos y \<le> pi"
4963  by (blast dest: arccos)
4964
4965lemma arccos_lt_bounded: 
4966  assumes "- 1 < y" "y < 1"
4967  shows  "0 < arccos y \<and> arccos y < pi"
4968proof -
4969  have "arccos y \<noteq> 0"
4970    by (metis (no_types) arccos assms(1) assms(2) cos_zero less_eq_real_def less_irrefl)
4971  moreover have "arccos y \<noteq> -pi"
4972    by (metis arccos assms(1) assms(2) cos_minus cos_pi not_less not_less_iff_gr_or_eq)
4973  ultimately show ?thesis
4974    using arccos_bounded [of y] assms
4975    by (metis arccos cos_pi not_less not_less_iff_gr_or_eq)
4976qed
4977
4978lemma arccos_cos: "0 \<le> x \<Longrightarrow> x \<le> pi \<Longrightarrow> arccos (cos x) = x"
4979  by (auto simp: arccos_def intro!: the1_equality cos_total)
4980
4981lemma arccos_cos2: "x \<le> 0 \<Longrightarrow> - pi \<le> x \<Longrightarrow> arccos (cos x) = -x"
4982  by (auto simp: arccos_def intro!: the1_equality cos_total)
4983
4984lemma cos_arcsin:
4985  assumes "- 1 \<le> x" "x \<le> 1"
4986  shows "cos (arcsin x) = sqrt (1 - x\<^sup>2)"
4987proof (rule power2_eq_imp_eq)
4988  show "(cos (arcsin x))\<^sup>2 = (sqrt (1 - x\<^sup>2))\<^sup>2"
4989    by (simp add: square_le_1 assms cos_squared_eq)
4990  show "0 \<le> cos (arcsin x)"
4991    using arcsin assms cos_ge_zero by blast
4992  show "0 \<le> sqrt (1 - x\<^sup>2)"
4993    by (simp add: square_le_1 assms)
4994qed
4995
4996lemma sin_arccos:
4997  assumes "- 1 \<le> x" "x \<le> 1"
4998  shows "sin (arccos x) = sqrt (1 - x\<^sup>2)"
4999proof (rule power2_eq_imp_eq)
5000  show "(sin (arccos x))\<^sup>2 = (sqrt (1 - x\<^sup>2))\<^sup>2"
5001    by (simp add: square_le_1 assms sin_squared_eq)
5002  show "0 \<le> sin (arccos x)"
5003    by (simp add: arccos_bounded assms sin_ge_zero)
5004  show "0 \<le> sqrt (1 - x\<^sup>2)"
5005    by (simp add: square_le_1 assms)
5006qed
5007
5008lemma arccos_0 [simp]: "arccos 0 = pi/2"
5009  by (metis arccos_cos cos_gt_zero cos_pi cos_pi_half pi_gt_zero
5010      pi_half_ge_zero not_le not_zero_less_neg_numeral numeral_One)
5011
5012lemma arccos_1 [simp]: "arccos 1 = 0"
5013  using arccos_cos by force
5014
5015lemma arccos_minus_1 [simp]: "arccos (- 1) = pi"
5016  by (metis arccos_cos cos_pi order_refl pi_ge_zero)
5017
5018lemma arccos_minus: "-1 \<le> x \<Longrightarrow> x \<le> 1 \<Longrightarrow> arccos (- x) = pi - arccos x"
5019  by (metis arccos_cos arccos_cos2 cos_minus_pi cos_total diff_le_0_iff_le le_add_same_cancel1
5020      minus_diff_eq uminus_add_conv_diff)
5021
5022corollary arccos_minus_abs:
5023  assumes "\<bar>x\<bar> \<le> 1"
5024  shows "arccos (- x) = pi - arccos x"
5025using assms by (simp add: arccos_minus)
5026
5027lemma sin_arccos_nonzero: "- 1 < x \<Longrightarrow> x < 1 \<Longrightarrow> sin (arccos x) \<noteq> 0"
5028  using arccos_lt_bounded sin_gt_zero by force
5029
5030lemma arctan: "- (pi/2) < arctan y \<and> arctan y < pi/2 \<and> tan (arctan y) = y"
5031  unfolding arctan_def by (rule theI' [OF tan_total])
5032
5033lemma tan_arctan: "tan (arctan y) = y"
5034  by (simp add: arctan)
5035
5036lemma arctan_bounded: "- (pi/2) < arctan y \<and> arctan y < pi/2"
5037  by (auto simp only: arctan)
5038
5039lemma arctan_lbound: "- (pi/2) < arctan y"
5040  by (simp add: arctan)
5041
5042lemma arctan_ubound: "arctan y < pi/2"
5043  by (auto simp only: arctan)
5044
5045lemma arctan_unique:
5046  assumes "-(pi/2) < x"
5047    and "x < pi/2"
5048    and "tan x = y"
5049  shows "arctan y = x"
5050  using assms arctan [of y] tan_total [of y] by (fast elim: ex1E)
5051
5052lemma arctan_tan: "-(pi/2) < x \<Longrightarrow> x < pi/2 \<Longrightarrow> arctan (tan x) = x"
5053  by (rule arctan_unique) simp_all
5054
5055lemma arctan_zero_zero [simp]: "arctan 0 = 0"
5056  by (rule arctan_unique) simp_all
5057
5058lemma arctan_minus: "arctan (- x) = - arctan x"
5059  using arctan [of "x"] by (auto simp: arctan_unique)
5060
5061lemma cos_arctan_not_zero [simp]: "cos (arctan x) \<noteq> 0"
5062  by (intro less_imp_neq [symmetric] cos_gt_zero_pi arctan_lbound arctan_ubound)
5063
5064lemma cos_arctan: "cos (arctan x) = 1 / sqrt (1 + x\<^sup>2)"
5065proof (rule power2_eq_imp_eq)
5066  have "0 < 1 + x\<^sup>2" by (simp add: add_pos_nonneg)
5067  show "0 \<le> 1 / sqrt (1 + x\<^sup>2)" by simp
5068  show "0 \<le> cos (arctan x)"
5069    by (intro less_imp_le cos_gt_zero_pi arctan_lbound arctan_ubound)
5070  have "(cos (arctan x))\<^sup>2 * (1 + (tan (arctan x))\<^sup>2) = 1"
5071    unfolding tan_def by (simp add: distrib_left power_divide)
5072  then show "(cos (arctan x))\<^sup>2 = (1 / sqrt (1 + x\<^sup>2))\<^sup>2"
5073    using \<open>0 < 1 + x\<^sup>2\<close> by (simp add: arctan power_divide eq_divide_eq)
5074qed
5075
5076lemma sin_arctan: "sin (arctan x) = x / sqrt (1 + x\<^sup>2)"
5077  using add_pos_nonneg [OF zero_less_one zero_le_power2 [of x]]
5078  using tan_arctan [of x] unfolding tan_def cos_arctan
5079  by (simp add: eq_divide_eq)
5080
5081lemma tan_sec: "cos x \<noteq> 0 \<Longrightarrow> 1 + (tan x)\<^sup>2 = (inverse (cos x))\<^sup>2"
5082  for x :: "'a::{real_normed_field,banach,field}"
5083  by (simp add: add_divide_eq_iff inverse_eq_divide power2_eq_square tan_def)
5084
5085lemma arctan_less_iff: "arctan x < arctan y \<longleftrightarrow> x < y"
5086  by (metis tan_monotone' arctan_lbound arctan_ubound tan_arctan)
5087
5088lemma arctan_le_iff: "arctan x \<le> arctan y \<longleftrightarrow> x \<le> y"
5089  by (simp only: not_less [symmetric] arctan_less_iff)
5090
5091lemma arctan_eq_iff: "arctan x = arctan y \<longleftrightarrow> x = y"
5092  by (simp only: eq_iff [where 'a=real] arctan_le_iff)
5093
5094lemma zero_less_arctan_iff [simp]: "0 < arctan x \<longleftrightarrow> 0 < x"
5095  using arctan_less_iff [of 0 x] by simp
5096
5097lemma arctan_less_zero_iff [simp]: "arctan x < 0 \<longleftrightarrow> x < 0"
5098  using arctan_less_iff [of x 0] by simp
5099
5100lemma zero_le_arctan_iff [simp]: "0 \<le> arctan x \<longleftrightarrow> 0 \<le> x"
5101  using arctan_le_iff [of 0 x] by simp
5102
5103lemma arctan_le_zero_iff [simp]: "arctan x \<le> 0 \<longleftrightarrow> x \<le> 0"
5104  using arctan_le_iff [of x 0] by simp
5105
5106lemma arctan_eq_zero_iff [simp]: "arctan x = 0 \<longleftrightarrow> x = 0"
5107  using arctan_eq_iff [of x 0] by simp
5108
5109lemma continuous_on_arcsin': "continuous_on {-1 .. 1} arcsin"
5110proof -
5111  have "continuous_on (sin ` {- pi/2 .. pi/2}) arcsin"
5112    by (rule continuous_on_inv) (auto intro: continuous_intros simp: arcsin_sin)
5113  also have "sin ` {- pi/2 .. pi/2} = {-1 .. 1}"
5114  proof safe
5115    fix x :: real
5116    assume "x \<in> {-1..1}"
5117    then show "x \<in> sin ` {- pi/2..pi/2}"
5118      using arcsin_lbound arcsin_ubound
5119      by (intro image_eqI[where x="arcsin x"]) auto
5120  qed simp
5121  finally show ?thesis .
5122qed
5123
5124lemma continuous_on_arcsin [continuous_intros]:
5125  "continuous_on s f \<Longrightarrow> (\<forall>x\<in>s. -1 \<le> f x \<and> f x \<le> 1) \<Longrightarrow> continuous_on s (\<lambda>x. arcsin (f x))"
5126  using continuous_on_compose[of s f, OF _ continuous_on_subset[OF  continuous_on_arcsin']]
5127  by (auto simp: comp_def subset_eq)
5128
5129lemma isCont_arcsin: "-1 < x \<Longrightarrow> x < 1 \<Longrightarrow> isCont arcsin x"
5130  using continuous_on_arcsin'[THEN continuous_on_subset, of "{ -1 <..< 1 }"]
5131  by (auto simp: continuous_on_eq_continuous_at subset_eq)
5132
5133lemma continuous_on_arccos': "continuous_on {-1 .. 1} arccos"
5134proof -
5135  have "continuous_on (cos ` {0 .. pi}) arccos"
5136    by (rule continuous_on_inv) (auto intro: continuous_intros simp: arccos_cos)
5137  also have "cos ` {0 .. pi} = {-1 .. 1}"
5138  proof safe
5139    fix x :: real
5140    assume "x \<in> {-1..1}"
5141    then show "x \<in> cos ` {0..pi}"
5142      using arccos_lbound arccos_ubound
5143      by (intro image_eqI[where x="arccos x"]) auto
5144  qed simp
5145  finally show ?thesis .
5146qed
5147
5148lemma continuous_on_arccos [continuous_intros]:
5149  "continuous_on s f \<Longrightarrow> (\<forall>x\<in>s. -1 \<le> f x \<and> f x \<le> 1) \<Longrightarrow> continuous_on s (\<lambda>x. arccos (f x))"
5150  using continuous_on_compose[of s f, OF _ continuous_on_subset[OF  continuous_on_arccos']]
5151  by (auto simp: comp_def subset_eq)
5152
5153lemma isCont_arccos: "-1 < x \<Longrightarrow> x < 1 \<Longrightarrow> isCont arccos x"
5154  using continuous_on_arccos'[THEN continuous_on_subset, of "{ -1 <..< 1 }"]
5155  by (auto simp: continuous_on_eq_continuous_at subset_eq)
5156
5157lemma isCont_arctan: "isCont arctan x"
5158proof -
5159  obtain u where u: "- (pi / 2) < u" "u < arctan x"
5160    by (meson arctan arctan_less_iff linordered_field_no_lb)
5161  obtain v where v: "arctan x < v" "v < pi / 2"
5162    by (meson arctan_less_iff arctan_ubound linordered_field_no_ub)
5163  have "isCont arctan (tan (arctan x))"
5164  proof (rule isCont_inverse_function2 [of u "arctan x" v])
5165    show "\<And>z. \<lbrakk>u \<le> z; z \<le> v\<rbrakk> \<Longrightarrow> arctan (tan z) = z"
5166      using arctan_unique u(1) v(2) by auto
5167    then show "\<And>z. \<lbrakk>u \<le> z; z \<le> v\<rbrakk> \<Longrightarrow> isCont tan z"
5168      by (metis arctan cos_gt_zero_pi isCont_tan less_irrefl)
5169  qed (use u v in auto)
5170  then show ?thesis
5171    by (simp add: arctan)
5172qed
5173
5174lemma tendsto_arctan [tendsto_intros]: "(f \<longlongrightarrow> x) F \<Longrightarrow> ((\<lambda>x. arctan (f x)) \<longlongrightarrow> arctan x) F"
5175  by (rule isCont_tendsto_compose [OF isCont_arctan])
5176
5177lemma continuous_arctan [continuous_intros]: "continuous F f \<Longrightarrow> continuous F (\<lambda>x. arctan (f x))"
5178  unfolding continuous_def by (rule tendsto_arctan)
5179
5180lemma continuous_on_arctan [continuous_intros]:
5181  "continuous_on s f \<Longrightarrow> continuous_on s (\<lambda>x. arctan (f x))"
5182  unfolding continuous_on_def by (auto intro: tendsto_arctan)
5183
5184lemma DERIV_arcsin:
5185  assumes "- 1 < x" "x < 1"
5186  shows "DERIV arcsin x :> inverse (sqrt (1 - x\<^sup>2))"
5187proof (rule DERIV_inverse_function)
5188  show "(sin has_real_derivative sqrt (1 - x\<^sup>2)) (at (arcsin x))"
5189    by (rule derivative_eq_intros | use assms cos_arcsin in force)+
5190  show "sqrt (1 - x\<^sup>2) \<noteq> 0"
5191    using abs_square_eq_1 assms by force
5192qed (use assms isCont_arcsin in auto)
5193
5194lemma DERIV_arccos:
5195  assumes "- 1 < x" "x < 1"
5196  shows "DERIV arccos x :> inverse (- sqrt (1 - x\<^sup>2))"
5197proof (rule DERIV_inverse_function)
5198  show "(cos has_real_derivative - sqrt (1 - x\<^sup>2)) (at (arccos x))"
5199    by (rule derivative_eq_intros | use assms sin_arccos in force)+
5200  show "- sqrt (1 - x\<^sup>2) \<noteq> 0"
5201    using abs_square_eq_1 assms by force
5202qed (use assms isCont_arccos in auto)
5203
5204lemma DERIV_arctan: "DERIV arctan x :> inverse (1 + x\<^sup>2)"
5205proof (rule DERIV_inverse_function [where f=tan and a="x - 1" and b="x + 1"])
5206  show "(tan has_real_derivative 1 + x\<^sup>2) (at (arctan x))"
5207    apply (rule derivative_eq_intros | simp)+
5208    by (metis arctan cos_arctan_not_zero power_inverse tan_sec)
5209  show "\<And>y. \<lbrakk>x - 1 < y; y < x + 1\<rbrakk> \<Longrightarrow> tan (arctan y) = y"
5210    using tan_arctan by blast
5211  show "1 + x\<^sup>2 \<noteq> 0"
5212    by (metis power_one sum_power2_eq_zero_iff zero_neq_one)
5213qed (use isCont_arctan in auto)
5214
5215declare
5216  DERIV_arcsin[THEN DERIV_chain2, derivative_intros]
5217  DERIV_arcsin[THEN DERIV_chain2, unfolded has_field_derivative_def, derivative_intros]
5218  DERIV_arccos[THEN DERIV_chain2, derivative_intros]
5219  DERIV_arccos[THEN DERIV_chain2, unfolded has_field_derivative_def, derivative_intros]
5220  DERIV_arctan[THEN DERIV_chain2, derivative_intros]
5221  DERIV_arctan[THEN DERIV_chain2, unfolded has_field_derivative_def, derivative_intros]
5222
5223lemmas has_derivative_arctan[derivative_intros] = DERIV_arctan[THEN DERIV_compose_FDERIV]
5224  and has_derivative_arccos[derivative_intros] = DERIV_arccos[THEN DERIV_compose_FDERIV]
5225  and has_derivative_arcsin[derivative_intros] = DERIV_arcsin[THEN DERIV_compose_FDERIV]
5226
5227lemma filterlim_tan_at_right: "filterlim tan at_bot (at_right (- (pi/2)))"
5228  by (rule filterlim_at_bot_at_right[where Q="\<lambda>x. - pi/2 < x \<and> x < pi/2" and P="\<lambda>x. True" and g=arctan])
5229     (auto simp: arctan le_less eventually_at dist_real_def simp del: less_divide_eq_numeral1
5230           intro!: tan_monotone exI[of _ "pi/2"])
5231
5232lemma filterlim_tan_at_left: "filterlim tan at_top (at_left (pi/2))"
5233  by (rule filterlim_at_top_at_left[where Q="\<lambda>x. - pi/2 < x \<and> x < pi/2" and P="\<lambda>x. True" and g=arctan])
5234     (auto simp: arctan le_less eventually_at dist_real_def simp del: less_divide_eq_numeral1
5235           intro!: tan_monotone exI[of _ "pi/2"])
5236
5237lemma tendsto_arctan_at_top: "(arctan \<longlongrightarrow> (pi/2)) at_top"
5238proof (rule tendstoI)
5239  fix e :: real
5240  assume "0 < e"
5241  define y where "y = pi/2 - min (pi/2) e"
5242  then have y: "0 \<le> y" "y < pi/2" "pi/2 \<le> e + y"
5243    using \<open>0 < e\<close> by auto
5244  show "eventually (\<lambda>x. dist (arctan x) (pi/2) < e) at_top"
5245  proof (intro eventually_at_top_dense[THEN iffD2] exI allI impI)
5246    fix x
5247    assume "tan y < x"
5248    then have "arctan (tan y) < arctan x"
5249      by (simp add: arctan_less_iff)
5250    with y have "y < arctan x"
5251      by (subst (asm) arctan_tan) simp_all
5252    with arctan_ubound[of x, arith] y \<open>0 < e\<close>
5253    show "dist (arctan x) (pi/2) < e"
5254      by (simp add: dist_real_def)
5255  qed
5256qed
5257
5258lemma tendsto_arctan_at_bot: "(arctan \<longlongrightarrow> - (pi/2)) at_bot"
5259  unfolding filterlim_at_bot_mirror arctan_minus
5260  by (intro tendsto_minus tendsto_arctan_at_top)
5261
5262
5263subsection \<open>Prove Totality of the Trigonometric Functions\<close>
5264
5265lemma cos_arccos_abs: "\<bar>y\<bar> \<le> 1 \<Longrightarrow> cos (arccos y) = y"
5266  by (simp add: abs_le_iff)
5267
5268lemma sin_arccos_abs: "\<bar>y\<bar> \<le> 1 \<Longrightarrow> sin (arccos y) = sqrt (1 - y\<^sup>2)"
5269  by (simp add: sin_arccos abs_le_iff)
5270
5271lemma sin_mono_less_eq:
5272  "- (pi/2) \<le> x \<Longrightarrow> x \<le> pi/2 \<Longrightarrow> - (pi/2) \<le> y \<Longrightarrow> y \<le> pi/2 \<Longrightarrow> sin x < sin y \<longleftrightarrow> x < y"
5273  by (metis not_less_iff_gr_or_eq sin_monotone_2pi)
5274
5275lemma sin_mono_le_eq:
5276  "- (pi/2) \<le> x \<Longrightarrow> x \<le> pi/2 \<Longrightarrow> - (pi/2) \<le> y \<Longrightarrow> y \<le> pi/2 \<Longrightarrow> sin x \<le> sin y \<longleftrightarrow> x \<le> y"
5277  by (meson leD le_less_linear sin_monotone_2pi sin_monotone_2pi_le)
5278
5279lemma sin_inj_pi:
5280  "- (pi/2) \<le> x \<Longrightarrow> x \<le> pi/2 \<Longrightarrow> - (pi/2) \<le> y \<Longrightarrow> y \<le> pi/2 \<Longrightarrow> sin x = sin y \<Longrightarrow> x = y"
5281  by (metis arcsin_sin)
5282
5283lemma cos_mono_less_eq: "0 \<le> x \<Longrightarrow> x \<le> pi \<Longrightarrow> 0 \<le> y \<Longrightarrow> y \<le> pi \<Longrightarrow> cos x < cos y \<longleftrightarrow> y < x"
5284  by (meson cos_monotone_0_pi cos_monotone_0_pi_le leD le_less_linear)
5285
5286lemma cos_mono_le_eq: "0 \<le> x \<Longrightarrow> x \<le> pi \<Longrightarrow> 0 \<le> y \<Longrightarrow> y \<le> pi \<Longrightarrow> cos x \<le> cos y \<longleftrightarrow> y \<le> x"
5287  by (metis arccos_cos cos_monotone_0_pi_le eq_iff linear)
5288
5289lemma cos_inj_pi: "0 \<le> x \<Longrightarrow> x \<le> pi \<Longrightarrow> 0 \<le> y \<Longrightarrow> y \<le> pi \<Longrightarrow> cos x = cos y \<Longrightarrow> x = y"
5290  by (metis arccos_cos)
5291
5292lemma arccos_le_pi2: "\<lbrakk>0 \<le> y; y \<le> 1\<rbrakk> \<Longrightarrow> arccos y \<le> pi/2"
5293  by (metis (mono_tags) arccos_0 arccos cos_le_one cos_monotone_0_pi_le
5294      cos_pi cos_pi_half pi_half_ge_zero antisym_conv less_eq_neg_nonpos linear minus_minus order.trans order_refl)
5295
5296lemma sincos_total_pi_half:
5297  assumes "0 \<le> x" "0 \<le> y" "x\<^sup>2 + y\<^sup>2 = 1"
5298  shows "\<exists>t. 0 \<le> t \<and> t \<le> pi/2 \<and> x = cos t \<and> y = sin t"
5299proof -
5300  have x1: "x \<le> 1"
5301    using assms by (metis le_add_same_cancel1 power2_le_imp_le power_one zero_le_power2)
5302  with assms have *: "0 \<le> arccos x" "cos (arccos x) = x"
5303    by (auto simp: arccos)
5304  from assms have "y = sqrt (1 - x\<^sup>2)"
5305    by (metis abs_of_nonneg add.commute add_diff_cancel real_sqrt_abs)
5306  with x1 * assms arccos_le_pi2 [of x] show ?thesis
5307    by (rule_tac x="arccos x" in exI) (auto simp: sin_arccos)
5308qed
5309
5310lemma sincos_total_pi:
5311  assumes "0 \<le> y" "x\<^sup>2 + y\<^sup>2 = 1"
5312  shows "\<exists>t. 0 \<le> t \<and> t \<le> pi \<and> x = cos t \<and> y = sin t"
5313proof (cases rule: le_cases [of 0 x])
5314  case le
5315  from sincos_total_pi_half [OF le] show ?thesis
5316    by (metis pi_ge_two pi_half_le_two add.commute add_le_cancel_left add_mono assms)
5317next
5318  case ge
5319  then have "0 \<le> -x"
5320    by simp
5321  then obtain t where t: "t\<ge>0" "t \<le> pi/2" "-x = cos t" "y = sin t"
5322    using sincos_total_pi_half assms
5323    by auto (metis \<open>0 \<le> - x\<close> power2_minus)
5324  show ?thesis
5325    by (rule exI [where x = "pi -t"]) (use t in auto)
5326qed
5327
5328lemma sincos_total_2pi_le:
5329  assumes "x\<^sup>2 + y\<^sup>2 = 1"
5330  shows "\<exists>t. 0 \<le> t \<and> t \<le> 2 * pi \<and> x = cos t \<and> y = sin t"
5331proof (cases rule: le_cases [of 0 y])
5332  case le
5333  from sincos_total_pi [OF le] show ?thesis
5334    by (metis assms le_add_same_cancel1 mult.commute mult_2_right order.trans)
5335next
5336  case ge
5337  then have "0 \<le> -y"
5338    by simp
5339  then obtain t where t: "t\<ge>0" "t \<le> pi" "x = cos t" "-y = sin t"
5340    using sincos_total_pi assms
5341    by auto (metis \<open>0 \<le> - y\<close> power2_minus)
5342  show ?thesis
5343    by (rule exI [where x = "2 * pi - t"]) (use t in auto)
5344qed
5345
5346lemma sincos_total_2pi:
5347  assumes "x\<^sup>2 + y\<^sup>2 = 1"
5348  obtains t where "0 \<le> t" "t < 2*pi" "x = cos t" "y = sin t"
5349proof -
5350  from sincos_total_2pi_le [OF assms]
5351  obtain t where t: "0 \<le> t" "t \<le> 2*pi" "x = cos t" "y = sin t"
5352    by blast
5353  show ?thesis
5354    by (cases "t = 2 * pi") (use t that in \<open>force+\<close>)
5355qed
5356
5357lemma arcsin_less_mono: "\<bar>x\<bar> \<le> 1 \<Longrightarrow> \<bar>y\<bar> \<le> 1 \<Longrightarrow> arcsin x < arcsin y \<longleftrightarrow> x < y"
5358  by (rule trans [OF sin_mono_less_eq [symmetric]]) (use arcsin_ubound arcsin_lbound in auto)
5359
5360lemma arcsin_le_mono: "\<bar>x\<bar> \<le> 1 \<Longrightarrow> \<bar>y\<bar> \<le> 1 \<Longrightarrow> arcsin x \<le> arcsin y \<longleftrightarrow> x \<le> y"
5361  using arcsin_less_mono not_le by blast
5362
5363lemma arcsin_less_arcsin: "- 1 \<le> x \<Longrightarrow> x < y \<Longrightarrow> y \<le> 1 \<Longrightarrow> arcsin x < arcsin y"
5364  using arcsin_less_mono by auto
5365
5366lemma arcsin_le_arcsin: "- 1 \<le> x \<Longrightarrow> x \<le> y \<Longrightarrow> y \<le> 1 \<Longrightarrow> arcsin x \<le> arcsin y"
5367  using arcsin_le_mono by auto
5368
5369lemma arccos_less_mono: "\<bar>x\<bar> \<le> 1 \<Longrightarrow> \<bar>y\<bar> \<le> 1 \<Longrightarrow> arccos x < arccos y \<longleftrightarrow> y < x"
5370  by (rule trans [OF cos_mono_less_eq [symmetric]]) (use arccos_ubound arccos_lbound in auto)
5371
5372lemma arccos_le_mono: "\<bar>x\<bar> \<le> 1 \<Longrightarrow> \<bar>y\<bar> \<le> 1 \<Longrightarrow> arccos x \<le> arccos y \<longleftrightarrow> y \<le> x"
5373  using arccos_less_mono [of y x] by (simp add: not_le [symmetric])
5374
5375lemma arccos_less_arccos: "- 1 \<le> x \<Longrightarrow> x < y \<Longrightarrow> y \<le> 1 \<Longrightarrow> arccos y < arccos x"
5376  using arccos_less_mono by auto
5377
5378lemma arccos_le_arccos: "- 1 \<le> x \<Longrightarrow> x \<le> y \<Longrightarrow> y \<le> 1 \<Longrightarrow> arccos y \<le> arccos x"
5379  using arccos_le_mono by auto
5380
5381lemma arccos_eq_iff: "\<bar>x\<bar> \<le> 1 \<and> \<bar>y\<bar> \<le> 1 \<Longrightarrow> arccos x = arccos y \<longleftrightarrow> x = y"
5382  using cos_arccos_abs by fastforce
5383
5384
5385lemma arccos_cos_eq_abs:
5386  assumes "\<bar>\<theta>\<bar> \<le> pi"
5387  shows "arccos (cos \<theta>) = \<bar>\<theta>\<bar>"
5388  unfolding arccos_def
5389proof (intro the_equality conjI; clarify?)
5390  show "cos \<bar>\<theta>\<bar> = cos \<theta>"
5391    by (simp add: abs_real_def)
5392  show "x = \<bar>\<theta>\<bar>" if "cos x = cos \<theta>" "0 \<le> x" "x \<le> pi" for x
5393    by (simp add: \<open>cos \<bar>\<theta>\<bar> = cos \<theta>\<close> assms cos_inj_pi that)
5394qed (use assms in auto)
5395
5396lemma arccos_cos_eq_abs_2pi:
5397  obtains k where "arccos (cos \<theta>) = \<bar>\<theta> - of_int k * (2 * pi)\<bar>"
5398proof -
5399  define k where "k \<equiv>  \<lfloor>(\<theta> + pi) / (2 * pi)\<rfloor>"
5400  have lepi: "\<bar>\<theta> - of_int k * (2 * pi)\<bar> \<le> pi"
5401    using floor_divide_lower [of "2*pi" "\<theta> + pi"] floor_divide_upper [of "2*pi" "\<theta> + pi"]
5402    by (auto simp: k_def abs_if algebra_simps)
5403  have "arccos (cos \<theta>) = arccos (cos (\<theta> - of_int k * (2 * pi)))"
5404    using cos_int_2pin sin_int_2pin by (simp add: cos_diff mult.commute)
5405  also have "\<dots> = \<bar>\<theta> - of_int k * (2 * pi)\<bar>"
5406    using arccos_cos_eq_abs lepi by blast
5407  finally show ?thesis
5408    using that by metis
5409qed
5410
5411lemma cos_limit_1:
5412  assumes "(\<lambda>j. cos (\<theta> j)) \<longlonglongrightarrow> 1"
5413  shows "\<exists>k. (\<lambda>j. \<theta> j - of_int (k j) * (2 * pi)) \<longlonglongrightarrow> 0"
5414proof -
5415  have "\<forall>\<^sub>F j in sequentially. cos (\<theta> j) \<in> {- 1..1}"
5416    by auto
5417  then have "(\<lambda>j. arccos (cos (\<theta> j))) \<longlonglongrightarrow> arccos 1"
5418    using continuous_on_tendsto_compose [OF continuous_on_arccos' assms] by auto
5419  moreover have "\<And>j. \<exists>k. arccos (cos (\<theta> j)) = \<bar>\<theta> j - of_int k * (2 * pi)\<bar>"
5420    using arccos_cos_eq_abs_2pi by metis
5421  then have "\<exists>k. \<forall>j. arccos (cos (\<theta> j)) = \<bar>\<theta> j - of_int (k j) * (2 * pi)\<bar>"
5422    by metis
5423  ultimately have "\<exists>k. (\<lambda>j. \<bar>\<theta> j - of_int (k j) * (2 * pi)\<bar>) \<longlonglongrightarrow> 0"
5424    by auto
5425  then show ?thesis
5426    by (simp add: tendsto_rabs_zero_iff)
5427qed
5428
5429lemma cos_diff_limit_1:
5430  assumes "(\<lambda>j. cos (\<theta> j - \<Theta>)) \<longlonglongrightarrow> 1"
5431  obtains k where "(\<lambda>j. \<theta> j - of_int (k j) * (2 * pi)) \<longlonglongrightarrow> \<Theta>"
5432proof -
5433  obtain k where "(\<lambda>j. (\<theta> j - \<Theta>) - of_int (k j) * (2 * pi)) \<longlonglongrightarrow> 0"
5434    using cos_limit_1 [OF assms] by auto
5435  then have "(\<lambda>j. \<Theta> + ((\<theta> j - \<Theta>) - of_int (k j) * (2 * pi))) \<longlonglongrightarrow> \<Theta> + 0"
5436    by (rule tendsto_add [OF tendsto_const])
5437  with that show ?thesis
5438    by auto
5439qed
5440
5441subsection \<open>Machin's formula\<close>
5442
5443lemma arctan_one: "arctan 1 = pi / 4"
5444  by (rule arctan_unique) (simp_all add: tan_45 m2pi_less_pi)
5445
5446lemma tan_total_pi4:
5447  assumes "\<bar>x\<bar> < 1"
5448  shows "\<exists>z. - (pi / 4) < z \<and> z < pi / 4 \<and> tan z = x"
5449proof
5450  show "- (pi / 4) < arctan x \<and> arctan x < pi / 4 \<and> tan (arctan x) = x"
5451    unfolding arctan_one [symmetric] arctan_minus [symmetric]
5452    unfolding arctan_less_iff
5453    using assms by (auto simp: arctan)
5454qed
5455
5456lemma arctan_add:
5457  assumes "\<bar>x\<bar> \<le> 1" "\<bar>y\<bar> < 1"
5458  shows "arctan x + arctan y = arctan ((x + y) / (1 - x * y))"
5459proof (rule arctan_unique [symmetric])
5460  have "- (pi / 4) \<le> arctan x" "- (pi / 4) < arctan y"
5461    unfolding arctan_one [symmetric] arctan_minus [symmetric]
5462    unfolding arctan_le_iff arctan_less_iff
5463    using assms by auto
5464  from add_le_less_mono [OF this] show 1: "- (pi/2) < arctan x + arctan y"
5465    by simp
5466  have "arctan x \<le> pi / 4" "arctan y < pi / 4"
5467    unfolding arctan_one [symmetric]
5468    unfolding arctan_le_iff arctan_less_iff
5469    using assms by auto
5470  from add_le_less_mono [OF this] show 2: "arctan x + arctan y < pi/2"
5471    by simp
5472  show "tan (arctan x + arctan y) = (x + y) / (1 - x * y)"
5473    using cos_gt_zero_pi [OF 1 2] by (simp add: arctan tan_add)
5474qed
5475
5476lemma arctan_double: "\<bar>x\<bar> < 1 \<Longrightarrow> 2 * arctan x = arctan ((2 * x) / (1 - x\<^sup>2))"
5477  by (metis arctan_add linear mult_2 not_less power2_eq_square)
5478
5479theorem machin: "pi / 4 = 4 * arctan (1 / 5) - arctan (1 / 239)"
5480proof -
5481  have "\<bar>1 / 5\<bar> < (1 :: real)"
5482    by auto
5483  from arctan_add[OF less_imp_le[OF this] this] have "2 * arctan (1 / 5) = arctan (5 / 12)"
5484    by auto
5485  moreover
5486  have "\<bar>5 / 12\<bar> < (1 :: real)"
5487    by auto
5488  from arctan_add[OF less_imp_le[OF this] this] have "2 * arctan (5 / 12) = arctan (120 / 119)"
5489    by auto
5490  moreover
5491  have "\<bar>1\<bar> \<le> (1::real)" and "\<bar>1 / 239\<bar> < (1::real)"
5492    by auto
5493  from arctan_add[OF this] have "arctan 1 + arctan (1 / 239) = arctan (120 / 119)"
5494    by auto
5495  ultimately have "arctan 1 + arctan (1 / 239) = 4 * arctan (1 / 5)"
5496    by auto
5497  then show ?thesis
5498    unfolding arctan_one by algebra
5499qed
5500
5501lemma machin_Euler: "5 * arctan (1 / 7) + 2 * arctan (3 / 79) = pi / 4"
5502proof -
5503  have 17: "\<bar>1 / 7\<bar> < (1 :: real)" by auto
5504  with arctan_double have "2 * arctan (1 / 7) = arctan (7 / 24)"
5505    by simp (simp add: field_simps)
5506  moreover
5507  have "\<bar>7 / 24\<bar> < (1 :: real)" by auto
5508  with arctan_double have "2 * arctan (7 / 24) = arctan (336 / 527)"
5509    by simp (simp add: field_simps)
5510  moreover
5511  have "\<bar>336 / 527\<bar> < (1 :: real)" by auto
5512  from arctan_add[OF less_imp_le[OF 17] this]
5513  have "arctan(1/7) + arctan (336 / 527) = arctan (2879 / 3353)"
5514    by auto
5515  ultimately have I: "5 * arctan (1 / 7) = arctan (2879 / 3353)" by auto
5516  have 379: "\<bar>3 / 79\<bar> < (1 :: real)" by auto
5517  with arctan_double have II: "2 * arctan (3 / 79) = arctan (237 / 3116)"
5518    by simp (simp add: field_simps)
5519  have *: "\<bar>2879 / 3353\<bar> < (1 :: real)" by auto
5520  have "\<bar>237 / 3116\<bar> < (1 :: real)" by auto
5521  from arctan_add[OF less_imp_le[OF *] this] have "arctan (2879/3353) + arctan (237/3116) = pi/4"
5522    by (simp add: arctan_one)
5523  with I II show ?thesis by auto
5524qed
5525
5526(*But could also prove MACHIN_GAUSS:
5527  12 * arctan(1/18) + 8 * arctan(1/57) - 5 * arctan(1/239) = pi/4*)
5528
5529
5530subsection \<open>Introducing the inverse tangent power series\<close>
5531
5532lemma monoseq_arctan_series:
5533  fixes x :: real
5534  assumes "\<bar>x\<bar> \<le> 1"
5535  shows "monoseq (\<lambda>n. 1 / real (n * 2 + 1) * x^(n * 2 + 1))"
5536    (is "monoseq ?a")
5537proof (cases "x = 0")
5538  case True
5539  then show ?thesis by (auto simp: monoseq_def)
5540next
5541  case False
5542  have "norm x \<le> 1" and "x \<le> 1" and "-1 \<le> x"
5543    using assms by auto
5544  show "monoseq ?a"
5545  proof -
5546    have mono: "1 / real (Suc (Suc n * 2)) * x ^ Suc (Suc n * 2) \<le>
5547        1 / real (Suc (n * 2)) * x ^ Suc (n * 2)"
5548      if "0 \<le> x" and "x \<le> 1" for n and x :: real
5549    proof (rule mult_mono)
5550      show "1 / real (Suc (Suc n * 2)) \<le> 1 / real (Suc (n * 2))"
5551        by (rule frac_le) simp_all
5552      show "0 \<le> 1 / real (Suc (n * 2))"
5553        by auto
5554      show "x ^ Suc (Suc n * 2) \<le> x ^ Suc (n * 2)"
5555        by (rule power_decreasing) (simp_all add: \<open>0 \<le> x\<close> \<open>x \<le> 1\<close>)
5556      show "0 \<le> x ^ Suc (Suc n * 2)"
5557        by (rule zero_le_power) (simp add: \<open>0 \<le> x\<close>)
5558    qed
5559    show ?thesis
5560    proof (cases "0 \<le> x")
5561      case True
5562      from mono[OF this \<open>x \<le> 1\<close>, THEN allI]
5563      show ?thesis
5564        unfolding Suc_eq_plus1[symmetric] by (rule mono_SucI2)
5565    next
5566      case False
5567      then have "0 \<le> - x" and "- x \<le> 1"
5568        using \<open>-1 \<le> x\<close> by auto
5569      from mono[OF this]
5570      have "1 / real (Suc (Suc n * 2)) * x ^ Suc (Suc n * 2) \<ge>
5571          1 / real (Suc (n * 2)) * x ^ Suc (n * 2)" for n
5572        using \<open>0 \<le> -x\<close> by auto
5573      then show ?thesis
5574        unfolding Suc_eq_plus1[symmetric] by (rule mono_SucI1[OF allI])
5575    qed
5576  qed
5577qed
5578
5579lemma zeroseq_arctan_series:
5580  fixes x :: real
5581  assumes "\<bar>x\<bar> \<le> 1"
5582  shows "(\<lambda>n. 1 / real (n * 2 + 1) * x^(n * 2 + 1)) \<longlonglongrightarrow> 0"
5583    (is "?a \<longlonglongrightarrow> 0")
5584proof (cases "x = 0")
5585  case True
5586  then show ?thesis by simp
5587next
5588  case False
5589  have "norm x \<le> 1" and "x \<le> 1" and "-1 \<le> x"
5590    using assms by auto
5591  show "?a \<longlonglongrightarrow> 0"
5592  proof (cases "\<bar>x\<bar> < 1")
5593    case True
5594    then have "norm x < 1" by auto
5595    from tendsto_mult[OF LIMSEQ_inverse_real_of_nat LIMSEQ_power_zero[OF \<open>norm x < 1\<close>, THEN LIMSEQ_Suc]]
5596    have "(\<lambda>n. 1 / real (n + 1) * x ^ (n + 1)) \<longlonglongrightarrow> 0"
5597      unfolding inverse_eq_divide Suc_eq_plus1 by simp
5598    then show ?thesis
5599      using pos2 by (rule LIMSEQ_linear)
5600  next
5601    case False
5602    then have "x = -1 \<or> x = 1"
5603      using \<open>\<bar>x\<bar> \<le> 1\<close> by auto
5604    then have n_eq: "\<And> n. x ^ (n * 2 + 1) = x"
5605      unfolding One_nat_def by auto
5606    from tendsto_mult[OF LIMSEQ_inverse_real_of_nat[THEN LIMSEQ_linear, OF pos2, unfolded inverse_eq_divide] tendsto_const[of x]]
5607    show ?thesis
5608      unfolding n_eq Suc_eq_plus1 by auto
5609  qed
5610qed
5611
5612lemma summable_arctan_series:
5613  fixes n :: nat
5614  assumes "\<bar>x\<bar> \<le> 1"
5615  shows "summable (\<lambda> k. (-1)^k * (1 / real (k*2+1) * x ^ (k*2+1)))"
5616    (is "summable (?c x)")
5617  by (rule summable_Leibniz(1),
5618      rule zeroseq_arctan_series[OF assms],
5619      rule monoseq_arctan_series[OF assms])
5620
5621lemma DERIV_arctan_series:
5622  assumes "\<bar>x\<bar> < 1"
5623  shows "DERIV (\<lambda>x'. \<Sum>k. (-1)^k * (1 / real (k * 2 + 1) * x' ^ (k * 2 + 1))) x :>
5624      (\<Sum>k. (-1)^k * x^(k * 2))"
5625    (is "DERIV ?arctan _ :> ?Int")
5626proof -
5627  let ?f = "\<lambda>n. if even n then (-1)^(n div 2) * 1 / real (Suc n) else 0"
5628
5629  have n_even: "even n \<Longrightarrow> 2 * (n div 2) = n" for n :: nat
5630    by presburger
5631  then have if_eq: "?f n * real (Suc n) * x'^n =
5632      (if even n then (-1)^(n div 2) * x'^(2 * (n div 2)) else 0)"
5633    for n x'
5634    by auto
5635
5636  have summable_Integral: "summable (\<lambda> n. (- 1) ^ n * x^(2 * n))" if "\<bar>x\<bar> < 1" for x :: real
5637  proof -
5638    from that have "x\<^sup>2 < 1"
5639      by (simp add: abs_square_less_1)
5640    have "summable (\<lambda> n. (- 1) ^ n * (x\<^sup>2) ^n)"
5641      by (rule summable_Leibniz(1))
5642        (auto intro!: LIMSEQ_realpow_zero monoseq_realpow \<open>x\<^sup>2 < 1\<close> order_less_imp_le[OF \<open>x\<^sup>2 < 1\<close>])
5643    then show ?thesis
5644      by (simp only: power_mult)
5645  qed
5646
5647  have sums_even: "(sums) f = (sums) (\<lambda> n. if even n then f (n div 2) else 0)"
5648    for f :: "nat \<Rightarrow> real"
5649  proof -
5650    have "f sums x = (\<lambda> n. if even n then f (n div 2) else 0) sums x" for x :: real
5651    proof
5652      assume "f sums x"
5653      from sums_if[OF sums_zero this] show "(\<lambda>n. if even n then f (n div 2) else 0) sums x"
5654        by auto
5655    next
5656      assume "(\<lambda> n. if even n then f (n div 2) else 0) sums x"
5657      from LIMSEQ_linear[OF this[simplified sums_def] pos2, simplified sum_split_even_odd[simplified mult.commute]]
5658      show "f sums x"
5659        unfolding sums_def by auto
5660    qed
5661    then show ?thesis ..
5662  qed
5663
5664  have Int_eq: "(\<Sum>n. ?f n * real (Suc n) * x^n) = ?Int"
5665    unfolding if_eq mult.commute[of _ 2]
5666      suminf_def sums_even[of "\<lambda> n. (- 1) ^ n * x ^ (2 * n)", symmetric]
5667    by auto
5668
5669  have arctan_eq: "(\<Sum>n. ?f n * x^(Suc n)) = ?arctan x" for x
5670  proof -
5671    have if_eq': "\<And>n. (if even n then (- 1) ^ (n div 2) * 1 / real (Suc n) else 0) * x ^ Suc n =
5672      (if even n then (- 1) ^ (n div 2) * (1 / real (Suc (2 * (n div 2))) * x ^ Suc (2 * (n div 2))) else 0)"
5673      using n_even by auto
5674    have idx_eq: "\<And>n. n * 2 + 1 = Suc (2 * n)"
5675      by auto
5676    then show ?thesis
5677      unfolding if_eq' idx_eq suminf_def
5678        sums_even[of "\<lambda> n. (- 1) ^ n * (1 / real (Suc (2 * n)) * x ^ Suc (2 * n))", symmetric]
5679      by auto
5680  qed
5681
5682  have "DERIV (\<lambda> x. \<Sum> n. ?f n * x^(Suc n)) x :> (\<Sum>n. ?f n * real (Suc n) * x^n)"
5683  proof (rule DERIV_power_series')
5684    show "x \<in> {- 1 <..< 1}"
5685      using \<open>\<bar> x \<bar> < 1\<close> by auto
5686    show "summable (\<lambda> n. ?f n * real (Suc n) * x'^n)"
5687      if x'_bounds: "x' \<in> {- 1 <..< 1}" for x' :: real
5688    proof -
5689      from that have "\<bar>x'\<bar> < 1" by auto
5690      then show ?thesis
5691        using that sums_summable sums_if [OF sums_0 [of "\<lambda>x. 0"] summable_sums [OF summable_Integral]]   
5692        by (auto simp add: if_distrib [of "\<lambda>x. x * y" for y] cong: if_cong)
5693    qed
5694  qed auto
5695  then show ?thesis
5696    by (simp only: Int_eq arctan_eq)
5697qed
5698
5699lemma arctan_series:
5700  assumes "\<bar>x\<bar> \<le> 1"
5701  shows "arctan x = (\<Sum>k. (-1)^k * (1 / real (k * 2 + 1) * x ^ (k * 2 + 1)))"
5702    (is "_ = suminf (\<lambda> n. ?c x n)")
5703proof -
5704  let ?c' = "\<lambda>x n. (-1)^n * x^(n*2)"
5705
5706  have DERIV_arctan_suminf: "DERIV (\<lambda> x. suminf (?c x)) x :> (suminf (?c' x))"
5707    if "0 < r" and "r < 1" and "\<bar>x\<bar> < r" for r x :: real
5708  proof (rule DERIV_arctan_series)
5709    from that show "\<bar>x\<bar> < 1"
5710      using \<open>r < 1\<close> and \<open>\<bar>x\<bar> < r\<close> by auto
5711  qed
5712
5713  {
5714    fix x :: real
5715    assume "\<bar>x\<bar> \<le> 1"
5716    note summable_Leibniz[OF zeroseq_arctan_series[OF this] monoseq_arctan_series[OF this]]
5717  } note arctan_series_borders = this
5718
5719  have when_less_one: "arctan x = (\<Sum>k. ?c x k)" if "\<bar>x\<bar> < 1" for x :: real
5720  proof -
5721    obtain r where "\<bar>x\<bar> < r" and "r < 1"
5722      using dense[OF \<open>\<bar>x\<bar> < 1\<close>] by blast
5723    then have "0 < r" and "- r < x" and "x < r" by auto
5724
5725    have suminf_eq_arctan_bounded: "suminf (?c x) - arctan x = suminf (?c a) - arctan a"
5726      if "-r < a" and "b < r" and "a < b" and "a \<le> x" and "x \<le> b" for x a b
5727    proof -
5728      from that have "\<bar>x\<bar> < r" by auto
5729      show "suminf (?c x) - arctan x = suminf (?c a) - arctan a"
5730      proof (rule DERIV_isconst2[of "a" "b"])
5731        show "a < b" and "a \<le> x" and "x \<le> b"
5732          using \<open>a < b\<close> \<open>a \<le> x\<close> \<open>x \<le> b\<close> by auto
5733        have "\<forall>x. - r < x \<and> x < r \<longrightarrow> DERIV (\<lambda> x. suminf (?c x) - arctan x) x :> 0"
5734        proof (rule allI, rule impI)
5735          fix x
5736          assume "-r < x \<and> x < r"
5737          then have "\<bar>x\<bar> < r" by auto
5738          with \<open>r < 1\<close> have "\<bar>x\<bar> < 1" by auto
5739          have "\<bar>- (x\<^sup>2)\<bar> < 1" using abs_square_less_1 \<open>\<bar>x\<bar> < 1\<close> by auto
5740          then have "(\<lambda>n. (- (x\<^sup>2)) ^ n) sums (1 / (1 - (- (x\<^sup>2))))"
5741            unfolding real_norm_def[symmetric] by (rule geometric_sums)
5742          then have "(?c' x) sums (1 / (1 - (- (x\<^sup>2))))"
5743            unfolding power_mult_distrib[symmetric] power_mult mult.commute[of _ 2] by auto
5744          then have suminf_c'_eq_geom: "inverse (1 + x\<^sup>2) = suminf (?c' x)"
5745            using sums_unique unfolding inverse_eq_divide by auto
5746          have "DERIV (\<lambda> x. suminf (?c x)) x :> (inverse (1 + x\<^sup>2))"
5747            unfolding suminf_c'_eq_geom
5748            by (rule DERIV_arctan_suminf[OF \<open>0 < r\<close> \<open>r < 1\<close> \<open>\<bar>x\<bar> < r\<close>])
5749          from DERIV_diff [OF this DERIV_arctan] show "DERIV (\<lambda>x. suminf (?c x) - arctan x) x :> 0"
5750            by auto
5751        qed
5752        then have DERIV_in_rball: "\<forall>y. a \<le> y \<and> y \<le> b \<longrightarrow> DERIV (\<lambda>x. suminf (?c x) - arctan x) y :> 0"
5753          using \<open>-r < a\<close> \<open>b < r\<close> by auto
5754        then show "\<And>y. \<lbrakk>a < y; y < b\<rbrakk> \<Longrightarrow> DERIV (\<lambda>x. suminf (?c x) - arctan x) y :> 0"
5755          using \<open>\<bar>x\<bar> < r\<close> by auto
5756        show "\<And>y. \<lbrakk>a \<le> y; y \<le> b\<rbrakk> \<Longrightarrow>  isCont (\<lambda>x. suminf (?c x) - arctan x) y"
5757          using DERIV_in_rball DERIV_isCont by auto
5758      qed
5759    qed
5760
5761    have suminf_arctan_zero: "suminf (?c 0) - arctan 0 = 0"
5762      unfolding Suc_eq_plus1[symmetric] power_Suc2 mult_zero_right arctan_zero_zero suminf_zero
5763      by auto
5764
5765    have "suminf (?c x) - arctan x = 0"
5766    proof (cases "x = 0")
5767      case True
5768      then show ?thesis
5769        using suminf_arctan_zero by auto
5770    next
5771      case False
5772      then have "0 < \<bar>x\<bar>" and "- \<bar>x\<bar> < \<bar>x\<bar>"
5773        by auto
5774      have "suminf (?c (- \<bar>x\<bar>)) - arctan (- \<bar>x\<bar>) = suminf (?c 0) - arctan 0"
5775        by (rule suminf_eq_arctan_bounded[where x1=0 and a1="-\<bar>x\<bar>" and b1="\<bar>x\<bar>", symmetric])
5776          (simp_all only: \<open>\<bar>x\<bar> < r\<close> \<open>-\<bar>x\<bar> < \<bar>x\<bar>\<close> neg_less_iff_less)
5777      moreover
5778      have "suminf (?c x) - arctan x = suminf (?c (- \<bar>x\<bar>)) - arctan (- \<bar>x\<bar>)"
5779        by (rule suminf_eq_arctan_bounded[where x1=x and a1="- \<bar>x\<bar>" and b1="\<bar>x\<bar>"])
5780           (simp_all only: \<open>\<bar>x\<bar> < r\<close> \<open>- \<bar>x\<bar> < \<bar>x\<bar>\<close> neg_less_iff_less)
5781      ultimately show ?thesis
5782        using suminf_arctan_zero by auto
5783    qed
5784    then show ?thesis by auto
5785  qed
5786
5787  show "arctan x = suminf (\<lambda>n. ?c x n)"
5788  proof (cases "\<bar>x\<bar> < 1")
5789    case True
5790    then show ?thesis by (rule when_less_one)
5791  next
5792    case False
5793    then have "\<bar>x\<bar> = 1" using \<open>\<bar>x\<bar> \<le> 1\<close> by auto
5794    let ?a = "\<lambda>x n. \<bar>1 / real (n * 2 + 1) * x^(n * 2 + 1)\<bar>"
5795    let ?diff = "\<lambda>x n. \<bar>arctan x - (\<Sum>i<n. ?c x i)\<bar>"
5796    have "?diff 1 n \<le> ?a 1 n" for n :: nat
5797    proof -
5798      have "0 < (1 :: real)" by auto
5799      moreover
5800      have "?diff x n \<le> ?a x n" if "0 < x" and "x < 1" for x :: real
5801      proof -
5802        from that have "\<bar>x\<bar> \<le> 1" and "\<bar>x\<bar> < 1"
5803          by auto
5804        from \<open>0 < x\<close> have "0 < 1 / real (0 * 2 + (1::nat)) * x ^ (0 * 2 + 1)"
5805          by auto
5806        note bounds = mp[OF arctan_series_borders(2)[OF \<open>\<bar>x\<bar> \<le> 1\<close>] this, unfolded when_less_one[OF \<open>\<bar>x\<bar> < 1\<close>, symmetric], THEN spec]
5807        have "0 < 1 / real (n*2+1) * x^(n*2+1)"
5808          by (rule mult_pos_pos) (simp_all only: zero_less_power[OF \<open>0 < x\<close>], auto)
5809        then have a_pos: "?a x n = 1 / real (n*2+1) * x^(n*2+1)"
5810          by (rule abs_of_pos)
5811        show ?thesis
5812        proof (cases "even n")
5813          case True
5814          then have sgn_pos: "(-1)^n = (1::real)" by auto
5815          from \<open>even n\<close> obtain m where "n = 2 * m" ..
5816          then have "2 * m = n" ..
5817          from bounds[of m, unfolded this atLeastAtMost_iff]
5818          have "\<bar>arctan x - (\<Sum>i<n. (?c x i))\<bar> \<le> (\<Sum>i<n + 1. (?c x i)) - (\<Sum>i<n. (?c x i))"
5819            by auto
5820          also have "\<dots> = ?c x n" by auto
5821          also have "\<dots> = ?a x n" unfolding sgn_pos a_pos by auto
5822          finally show ?thesis .
5823        next
5824          case False
5825          then have sgn_neg: "(-1)^n = (-1::real)" by auto
5826          from \<open>odd n\<close> obtain m where "n = 2 * m + 1" ..
5827          then have m_def: "2 * m + 1 = n" ..
5828          then have m_plus: "2 * (m + 1) = n + 1" by auto
5829          from bounds[of "m + 1", unfolded this atLeastAtMost_iff, THEN conjunct1] bounds[of m, unfolded m_def atLeastAtMost_iff, THEN conjunct2]
5830          have "\<bar>arctan x - (\<Sum>i<n. (?c x i))\<bar> \<le> (\<Sum>i<n. (?c x i)) - (\<Sum>i<n+1. (?c x i))" by auto
5831          also have "\<dots> = - ?c x n" by auto
5832          also have "\<dots> = ?a x n" unfolding sgn_neg a_pos by auto
5833          finally show ?thesis .
5834        qed
5835      qed
5836      hence "\<forall>x \<in> { 0 <..< 1 }. 0 \<le> ?a x n - ?diff x n" by auto
5837      moreover have "isCont (\<lambda> x. ?a x n - ?diff x n) x" for x
5838        unfolding diff_conv_add_uminus divide_inverse
5839        by (auto intro!: isCont_add isCont_rabs continuous_ident isCont_minus isCont_arctan
5840          continuous_at_within_inverse isCont_mult isCont_power continuous_const isCont_sum
5841          simp del: add_uminus_conv_diff)
5842      ultimately have "0 \<le> ?a 1 n - ?diff 1 n"
5843        by (rule LIM_less_bound)
5844      then show ?thesis by auto
5845    qed
5846    have "?a 1 \<longlonglongrightarrow> 0"
5847      unfolding tendsto_rabs_zero_iff power_one divide_inverse One_nat_def
5848      by (auto intro!: tendsto_mult LIMSEQ_linear LIMSEQ_inverse_real_of_nat simp del: of_nat_Suc)
5849    have "?diff 1 \<longlonglongrightarrow> 0"
5850    proof (rule LIMSEQ_I)
5851      fix r :: real
5852      assume "0 < r"
5853      obtain N :: nat where N_I: "N \<le> n \<Longrightarrow> ?a 1 n < r" for n
5854        using LIMSEQ_D[OF \<open>?a 1 \<longlonglongrightarrow> 0\<close> \<open>0 < r\<close>] by auto
5855      have "norm (?diff 1 n - 0) < r" if "N \<le> n" for n
5856        using \<open>?diff 1 n \<le> ?a 1 n\<close> N_I[OF that] by auto
5857      then show "\<exists>N. \<forall> n \<ge> N. norm (?diff 1 n - 0) < r" by blast
5858    qed
5859    from this [unfolded tendsto_rabs_zero_iff, THEN tendsto_add [OF _ tendsto_const], of "- arctan 1", THEN tendsto_minus]
5860    have "(?c 1) sums (arctan 1)" unfolding sums_def by auto
5861    then have "arctan 1 = (\<Sum>i. ?c 1 i)" by (rule sums_unique)
5862
5863    show ?thesis
5864    proof (cases "x = 1")
5865      case True
5866      then show ?thesis by (simp add: \<open>arctan 1 = (\<Sum> i. ?c 1 i)\<close>)
5867    next
5868      case False
5869      then have "x = -1" using \<open>\<bar>x\<bar> = 1\<close> by auto
5870
5871      have "- (pi/2) < 0" using pi_gt_zero by auto
5872      have "- (2 * pi) < 0" using pi_gt_zero by auto
5873
5874      have c_minus_minus: "?c (- 1) i = - ?c 1 i" for i by auto
5875
5876      have "arctan (- 1) = arctan (tan (-(pi / 4)))"
5877        unfolding tan_45 tan_minus ..
5878      also have "\<dots> = - (pi / 4)"
5879        by (rule arctan_tan) (auto simp: order_less_trans[OF \<open>- (pi/2) < 0\<close> pi_gt_zero])
5880      also have "\<dots> = - (arctan (tan (pi / 4)))"
5881        unfolding neg_equal_iff_equal
5882        by (rule arctan_tan[symmetric]) (auto simp: order_less_trans[OF \<open>- (2 * pi) < 0\<close> pi_gt_zero])
5883      also have "\<dots> = - (arctan 1)"
5884        unfolding tan_45 ..
5885      also have "\<dots> = - (\<Sum> i. ?c 1 i)"
5886        using \<open>arctan 1 = (\<Sum> i. ?c 1 i)\<close> by auto
5887      also have "\<dots> = (\<Sum> i. ?c (- 1) i)"
5888        using suminf_minus[OF sums_summable[OF \<open>(?c 1) sums (arctan 1)\<close>]]
5889        unfolding c_minus_minus by auto
5890      finally show ?thesis using \<open>x = -1\<close> by auto
5891    qed
5892  qed
5893qed
5894
5895lemma arctan_half: "arctan x = 2 * arctan (x / (1 + sqrt(1 + x\<^sup>2)))"
5896  for x :: real
5897proof -
5898  obtain y where low: "- (pi/2) < y" and high: "y < pi/2" and y_eq: "tan y = x"
5899    using tan_total by blast
5900  then have low2: "- (pi/2) < y / 2" and high2: "y / 2 < pi/2"
5901    by auto
5902
5903  have "0 < cos y" by (rule cos_gt_zero_pi[OF low high])
5904  then have "cos y \<noteq> 0" and cos_sqrt: "sqrt ((cos y)\<^sup>2) = cos y"
5905    by auto
5906
5907  have "1 + (tan y)\<^sup>2 = 1 + (sin y)\<^sup>2 / (cos y)\<^sup>2"
5908    unfolding tan_def power_divide ..
5909  also have "\<dots> = (cos y)\<^sup>2 / (cos y)\<^sup>2 + (sin y)\<^sup>2 / (cos y)\<^sup>2"
5910    using \<open>cos y \<noteq> 0\<close> by auto
5911  also have "\<dots> = 1 / (cos y)\<^sup>2"
5912    unfolding add_divide_distrib[symmetric] sin_cos_squared_add2 ..
5913  finally have "1 + (tan y)\<^sup>2 = 1 / (cos y)\<^sup>2" .
5914
5915  have "sin y / (cos y + 1) = tan y / ((cos y + 1) / cos y)"
5916    unfolding tan_def using \<open>cos y \<noteq> 0\<close> by (simp add: field_simps)
5917  also have "\<dots> = tan y / (1 + 1 / cos y)"
5918    using \<open>cos y \<noteq> 0\<close> unfolding add_divide_distrib by auto
5919  also have "\<dots> = tan y / (1 + 1 / sqrt ((cos y)\<^sup>2))"
5920    unfolding cos_sqrt ..
5921  also have "\<dots> = tan y / (1 + sqrt (1 / (cos y)\<^sup>2))"
5922    unfolding real_sqrt_divide by auto
5923  finally have eq: "sin y / (cos y + 1) = tan y / (1 + sqrt(1 + (tan y)\<^sup>2))"
5924    unfolding \<open>1 + (tan y)\<^sup>2 = 1 / (cos y)\<^sup>2\<close> .
5925
5926  have "arctan x = y"
5927    using arctan_tan low high y_eq by auto
5928  also have "\<dots> = 2 * (arctan (tan (y/2)))"
5929    using arctan_tan[OF low2 high2] by auto
5930  also have "\<dots> = 2 * (arctan (sin y / (cos y + 1)))"
5931    unfolding tan_half by auto
5932  finally show ?thesis
5933    unfolding eq \<open>tan y = x\<close> .
5934qed
5935
5936lemma arctan_monotone: "x < y \<Longrightarrow> arctan x < arctan y"
5937  by (simp only: arctan_less_iff)
5938
5939lemma arctan_monotone': "x \<le> y \<Longrightarrow> arctan x \<le> arctan y"
5940  by (simp only: arctan_le_iff)
5941
5942lemma arctan_inverse:
5943  assumes "x \<noteq> 0"
5944  shows "arctan (1 / x) = sgn x * pi/2 - arctan x"
5945proof (rule arctan_unique)
5946  show "- (pi/2) < sgn x * pi/2 - arctan x"
5947    using arctan_bounded [of x] assms
5948    unfolding sgn_real_def
5949    apply (auto simp: arctan algebra_simps)
5950    apply (drule zero_less_arctan_iff [THEN iffD2], arith)
5951    done
5952  show "sgn x * pi/2 - arctan x < pi/2"
5953    using arctan_bounded [of "- x"] assms
5954    unfolding sgn_real_def arctan_minus
5955    by (auto simp: algebra_simps)
5956  show "tan (sgn x * pi/2 - arctan x) = 1 / x"
5957    unfolding tan_inverse [of "arctan x", unfolded tan_arctan]
5958    unfolding sgn_real_def
5959    by (simp add: tan_def cos_arctan sin_arctan sin_diff cos_diff)
5960qed
5961
5962theorem pi_series: "pi / 4 = (\<Sum>k. (-1)^k * 1 / real (k * 2 + 1))"
5963  (is "_ = ?SUM")
5964proof -
5965  have "pi / 4 = arctan 1"
5966    using arctan_one by auto
5967  also have "\<dots> = ?SUM"
5968    using arctan_series[of 1] by auto
5969  finally show ?thesis by auto
5970qed
5971
5972
5973subsection \<open>Existence of Polar Coordinates\<close>
5974
5975lemma cos_x_y_le_one: "\<bar>x / sqrt (x\<^sup>2 + y\<^sup>2)\<bar> \<le> 1"
5976  by (rule power2_le_imp_le [OF _ zero_le_one])
5977    (simp add: power_divide divide_le_eq not_sum_power2_lt_zero)
5978
5979lemmas cos_arccos_lemma1 = cos_arccos_abs [OF cos_x_y_le_one]
5980
5981lemmas sin_arccos_lemma1 = sin_arccos_abs [OF cos_x_y_le_one]
5982
5983lemma polar_Ex: "\<exists>r::real. \<exists>a. x = r * cos a \<and> y = r * sin a"
5984proof -
5985  have polar_ex1: "0 < y \<Longrightarrow> \<exists>r a. x = r * cos a \<and> y = r * sin a" for y
5986    apply (rule exI [where x = "sqrt (x\<^sup>2 + y\<^sup>2)"])
5987    apply (rule exI [where x = "arccos (x / sqrt (x\<^sup>2 + y\<^sup>2))"])
5988    apply (simp add: cos_arccos_lemma1 sin_arccos_lemma1 power_divide
5989        real_sqrt_mult [symmetric] right_diff_distrib)
5990    done
5991  show ?thesis
5992  proof (cases "0::real" y rule: linorder_cases)
5993    case less
5994    then show ?thesis
5995      by (rule polar_ex1)
5996  next
5997    case equal
5998    then show ?thesis
5999      by (force simp: intro!: cos_zero sin_zero)
6000  next
6001    case greater
6002    with polar_ex1 [where y="-y"] show ?thesis
6003      by auto (metis cos_minus minus_minus minus_mult_right sin_minus)
6004  qed
6005qed
6006
6007
6008subsection \<open>Basics about polynomial functions: products, extremal behaviour and root counts\<close>
6009
6010lemma pairs_le_eq_Sigma: "{(i, j). i + j \<le> m} = Sigma (atMost m) (\<lambda>r. atMost (m - r))"
6011  for m :: nat
6012  by auto
6013
6014lemma sum_up_index_split: "(\<Sum>k\<le>m + n. f k) = (\<Sum>k\<le>m. f k) + (\<Sum>k = Suc m..m + n. f k)"
6015  by (metis atLeast0AtMost Suc_eq_plus1 le0 sum_ub_add_nat)
6016
6017lemma Sigma_interval_disjoint: "(SIGMA i:A. {..v i}) \<inter> (SIGMA i:A.{v i<..w}) = {}"
6018  for w :: "'a::order"
6019  by auto
6020
6021lemma product_atMost_eq_Un: "A \<times> {..m} = (SIGMA i:A.{..m - i}) \<union> (SIGMA i:A.{m - i<..m})"
6022  for m :: nat
6023  by auto
6024
6025lemma polynomial_product: (*with thanks to Chaitanya Mangla*)
6026  fixes x :: "'a::idom"
6027  assumes m: "\<And>i. i > m \<Longrightarrow> a i = 0"
6028    and n: "\<And>j. j > n \<Longrightarrow> b j = 0"
6029  shows "(\<Sum>i\<le>m. (a i) * x ^ i) * (\<Sum>j\<le>n. (b j) * x ^ j) =
6030    (\<Sum>r\<le>m + n. (\<Sum>k\<le>r. (a k) * (b (r - k))) * x ^ r)"
6031proof -
6032  have "(\<Sum>i\<le>m. (a i) * x ^ i) * (\<Sum>j\<le>n. (b j) * x ^ j) = (\<Sum>i\<le>m. \<Sum>j\<le>n. (a i * x ^ i) * (b j * x ^ j))"
6033    by (rule sum_product)
6034  also have "\<dots> = (\<Sum>i\<le>m + n. \<Sum>j\<le>n + m. a i * x ^ i * (b j * x ^ j))"
6035    using assms by (auto simp: sum_up_index_split)
6036  also have "\<dots> = (\<Sum>r\<le>m + n. \<Sum>j\<le>m + n - r. a r * x ^ r * (b j * x ^ j))"
6037    apply (simp add: add_ac sum.Sigma product_atMost_eq_Un)
6038    apply (clarsimp simp add: sum_Un Sigma_interval_disjoint intro!: sum.neutral)
6039    apply (metis add_diff_assoc2 add.commute add_lessD1 leD m n nat_le_linear neqE)
6040    done
6041  also have "\<dots> = (\<Sum>(i,j)\<in>{(i,j). i+j \<le> m+n}. (a i * x ^ i) * (b j * x ^ j))"
6042    by (auto simp: pairs_le_eq_Sigma sum.Sigma)
6043  also have "\<dots> = (\<Sum>r\<le>m + n. (\<Sum>k\<le>r. (a k) * (b (r - k))) * x ^ r)"
6044    apply (subst sum_triangle_reindex_eq)
6045    apply (auto simp: algebra_simps sum_distrib_left intro!: sum.cong)
6046    apply (metis le_add_diff_inverse power_add)
6047    done
6048  finally show ?thesis .
6049qed
6050
6051lemma polynomial_product_nat:
6052  fixes x :: nat
6053  assumes m: "\<And>i. i > m \<Longrightarrow> a i = 0"
6054    and n: "\<And>j. j > n \<Longrightarrow> b j = 0"
6055  shows "(\<Sum>i\<le>m. (a i) * x ^ i) * (\<Sum>j\<le>n. (b j) * x ^ j) =
6056    (\<Sum>r\<le>m + n. (\<Sum>k\<le>r. (a k) * (b (r - k))) * x ^ r)"
6057  using polynomial_product [of m a n b x] assms
6058  by (simp only: of_nat_mult [symmetric] of_nat_power [symmetric]
6059      of_nat_eq_iff Int.int_sum [symmetric])
6060
6061lemma polyfun_diff: (*COMPLEX_SUB_POLYFUN in HOL Light*)
6062  fixes x :: "'a::idom"
6063  assumes "1 \<le> n"
6064  shows "(\<Sum>i\<le>n. a i * x^i) - (\<Sum>i\<le>n. a i * y^i) =
6065    (x - y) * (\<Sum>j<n. (\<Sum>i=Suc j..n. a i * y^(i - j - 1)) * x^j)"
6066proof -
6067  have h: "bij_betw (\<lambda>(i,j). (j,i)) ((SIGMA i : atMost n. lessThan i)) (SIGMA j : lessThan n. {Suc j..n})"
6068    by (auto simp: bij_betw_def inj_on_def)
6069  have "(\<Sum>i\<le>n. a i * x^i) - (\<Sum>i\<le>n. a i * y^i) = (\<Sum>i\<le>n. a i * (x^i - y^i))"
6070    by (simp add: right_diff_distrib sum_subtractf)
6071  also have "\<dots> = (\<Sum>i\<le>n. a i * (x - y) * (\<Sum>j<i. y^(i - Suc j) * x^j))"
6072    by (simp add: power_diff_sumr2 mult.assoc)
6073  also have "\<dots> = (\<Sum>i\<le>n. \<Sum>j<i. a i * (x - y) * (y^(i - Suc j) * x^j))"
6074    by (simp add: sum_distrib_left)
6075  also have "\<dots> = (\<Sum>(i,j) \<in> (SIGMA i : atMost n. lessThan i). a i * (x - y) * (y^(i - Suc j) * x^j))"
6076    by (simp add: sum.Sigma)
6077  also have "\<dots> = (\<Sum>(j,i) \<in> (SIGMA j : lessThan n. {Suc j..n}). a i * (x - y) * (y^(i - Suc j) * x^j))"
6078    by (auto simp: sum.reindex_bij_betw [OF h, symmetric] intro: sum.strong_cong)
6079  also have "\<dots> = (\<Sum>j<n. \<Sum>i=Suc j..n. a i * (x - y) * (y^(i - Suc j) * x^j))"
6080    by (simp add: sum.Sigma)
6081  also have "\<dots> = (x - y) * (\<Sum>j<n. (\<Sum>i=Suc j..n. a i * y^(i - j - 1)) * x^j)"
6082    by (simp add: sum_distrib_left mult_ac)
6083  finally show ?thesis .
6084qed
6085
6086lemma polyfun_diff_alt: (*COMPLEX_SUB_POLYFUN_ALT in HOL Light*)
6087  fixes x :: "'a::idom"
6088  assumes "1 \<le> n"
6089  shows "(\<Sum>i\<le>n. a i * x^i) - (\<Sum>i\<le>n. a i * y^i) =
6090    (x - y) * ((\<Sum>j<n. \<Sum>k<n-j. a(j + k + 1) * y^k * x^j))"
6091proof -
6092  have "(\<Sum>i=Suc j..n. a i * y^(i - j - 1)) = (\<Sum>k<n-j. a(j+k+1) * y^k)"
6093    if "j < n" for j :: nat
6094  proof -
6095    have h: "bij_betw (\<lambda>i. i - (j + 1)) {Suc j..n} (lessThan (n-j))"
6096      apply (auto simp: bij_betw_def inj_on_def)
6097      apply (rule_tac x="x + Suc j" in image_eqI, auto)
6098      done
6099    then show ?thesis
6100      by (auto simp: sum.reindex_bij_betw [OF h, symmetric] intro: sum.strong_cong)
6101  qed
6102  then show ?thesis
6103    by (simp add: polyfun_diff [OF assms] sum_distrib_right)
6104qed
6105
6106lemma polyfun_linear_factor:  (*COMPLEX_POLYFUN_LINEAR_FACTOR in HOL Light*)
6107  fixes a :: "'a::idom"
6108  shows "\<exists>b. \<forall>z. (\<Sum>i\<le>n. c(i) * z^i) = (z - a) * (\<Sum>i<n. b(i) * z^i) + (\<Sum>i\<le>n. c(i) * a^i)"
6109proof (cases "n = 0")
6110  case True then show ?thesis
6111    by simp
6112next
6113  case False
6114  have "(\<exists>b. \<forall>z. (\<Sum>i\<le>n. c i * z^i) = (z - a) * (\<Sum>i<n. b i * z^i) + (\<Sum>i\<le>n. c i * a^i)) \<longleftrightarrow>
6115        (\<exists>b. \<forall>z. (\<Sum>i\<le>n. c i * z^i) - (\<Sum>i\<le>n. c i * a^i) = (z - a) * (\<Sum>i<n. b i * z^i))"
6116    by (simp add: algebra_simps)
6117  also have "\<dots> \<longleftrightarrow>
6118    (\<exists>b. \<forall>z. (z - a) * (\<Sum>j<n. (\<Sum>i = Suc j..n. c i * a^(i - Suc j)) * z^j) =
6119      (z - a) * (\<Sum>i<n. b i * z^i))"
6120    using False by (simp add: polyfun_diff)
6121  also have "\<dots> = True" by auto
6122  finally show ?thesis
6123    by simp
6124qed
6125
6126lemma polyfun_linear_factor_root:  (*COMPLEX_POLYFUN_LINEAR_FACTOR_ROOT in HOL Light*)
6127  fixes a :: "'a::idom"
6128  assumes "(\<Sum>i\<le>n. c(i) * a^i) = 0"
6129  obtains b where "\<And>z. (\<Sum>i\<le>n. c i * z^i) = (z - a) * (\<Sum>i<n. b i * z^i)"
6130  using polyfun_linear_factor [of c n a] assms by auto
6131
6132(*The material of this section, up until this point, could go into a new theory of polynomials
6133  based on Main alone. The remaining material involves limits, continuity, series, etc.*)
6134
6135lemma isCont_polynom: "isCont (\<lambda>w. \<Sum>i\<le>n. c i * w^i) a"
6136  for c :: "nat \<Rightarrow> 'a::real_normed_div_algebra"
6137  by simp
6138
6139lemma zero_polynom_imp_zero_coeffs:
6140  fixes c :: "nat \<Rightarrow> 'a::{ab_semigroup_mult,real_normed_div_algebra}"
6141  assumes "\<And>w. (\<Sum>i\<le>n. c i * w^i) = 0"  "k \<le> n"
6142  shows "c k = 0"
6143  using assms
6144proof (induction n arbitrary: c k)
6145  case 0
6146  then show ?case
6147    by simp
6148next
6149  case (Suc n c k)
6150  have [simp]: "c 0 = 0" using Suc.prems(1) [of 0]
6151    by simp
6152  have "(\<Sum>i\<le>Suc n. c i * w^i) = w * (\<Sum>i\<le>n. c (Suc i) * w^i)" for w
6153  proof -
6154    have "(\<Sum>i\<le>Suc n. c i * w^i) = (\<Sum>i\<le>n. c (Suc i) * w ^ Suc i)"
6155      unfolding Set_Interval.sum_atMost_Suc_shift
6156      by simp
6157    also have "\<dots> = w * (\<Sum>i\<le>n. c (Suc i) * w^i)"
6158      by (simp add: sum_distrib_left ac_simps)
6159    finally show ?thesis .
6160  qed
6161  then have w: "\<And>w. w \<noteq> 0 \<Longrightarrow> (\<Sum>i\<le>n. c (Suc i) * w^i) = 0"
6162    using Suc  by auto
6163  then have "(\<lambda>h. \<Sum>i\<le>n. c (Suc i) * h^i) \<midarrow>0\<rightarrow> 0"
6164    by (simp cong: LIM_cong)  \<comment> \<open>the case \<open>w = 0\<close> by continuity\<close>
6165  then have "(\<Sum>i\<le>n. c (Suc i) * 0^i) = 0"
6166    using isCont_polynom [of 0 "\<lambda>i. c (Suc i)" n] LIM_unique
6167    by (force simp: Limits.isCont_iff)
6168  then have "\<And>w. (\<Sum>i\<le>n. c (Suc i) * w^i) = 0"
6169    using w by metis
6170  then have "\<And>i. i \<le> n \<Longrightarrow> c (Suc i) = 0"
6171    using Suc.IH [of "\<lambda>i. c (Suc i)"] by blast
6172  then show ?case using \<open>k \<le> Suc n\<close>
6173    by (cases k) auto
6174qed
6175
6176lemma polyfun_rootbound: (*COMPLEX_POLYFUN_ROOTBOUND in HOL Light*)
6177  fixes c :: "nat \<Rightarrow> 'a::{idom,real_normed_div_algebra}"
6178  assumes "c k \<noteq> 0" "k\<le>n"
6179  shows "finite {z. (\<Sum>i\<le>n. c(i) * z^i) = 0} \<and> card {z. (\<Sum>i\<le>n. c(i) * z^i) = 0} \<le> n"
6180  using assms
6181proof (induction n arbitrary: c k)
6182  case 0
6183  then show ?case
6184    by simp
6185next
6186  case (Suc m c k)
6187  let ?succase = ?case
6188  show ?case
6189  proof (cases "{z. (\<Sum>i\<le>Suc m. c(i) * z^i) = 0} = {}")
6190    case True
6191    then show ?succase
6192      by simp
6193  next
6194    case False
6195    then obtain z0 where z0: "(\<Sum>i\<le>Suc m. c(i) * z0^i) = 0"
6196      by blast
6197    then obtain b where b: "\<And>w. (\<Sum>i\<le>Suc m. c i * w^i) = (w - z0) * (\<Sum>i\<le>m. b i * w^i)"
6198      using polyfun_linear_factor_root [OF z0, unfolded lessThan_Suc_atMost]
6199      by blast
6200    then have eq: "{z. (\<Sum>i\<le>Suc m. c i * z^i) = 0} = insert z0 {z. (\<Sum>i\<le>m. b i * z^i) = 0}"
6201      by auto
6202    have "\<not> (\<forall>k\<le>m. b k = 0)"
6203    proof
6204      assume [simp]: "\<forall>k\<le>m. b k = 0"
6205      then have "\<And>w. (\<Sum>i\<le>m. b i * w^i) = 0"
6206        by simp
6207      then have "\<And>w. (\<Sum>i\<le>Suc m. c i * w^i) = 0"
6208        using b by simp
6209      then have "\<And>k. k \<le> Suc m \<Longrightarrow> c k = 0"
6210        using zero_polynom_imp_zero_coeffs by blast
6211      then show False using Suc.prems by blast
6212    qed
6213    then obtain k' where bk': "b k' \<noteq> 0" "k' \<le> m"
6214      by blast
6215    show ?succase
6216      using Suc.IH [of b k'] bk'
6217      by (simp add: eq card_insert_if del: sum_atMost_Suc)
6218    qed
6219qed
6220
6221lemma
6222  fixes c :: "nat \<Rightarrow> 'a::{idom,real_normed_div_algebra}"
6223  assumes "c k \<noteq> 0" "k\<le>n"
6224  shows polyfun_roots_finite: "finite {z. (\<Sum>i\<le>n. c(i) * z^i) = 0}"
6225    and polyfun_roots_card: "card {z. (\<Sum>i\<le>n. c(i) * z^i) = 0} \<le> n"
6226  using polyfun_rootbound assms by auto
6227
6228lemma polyfun_finite_roots: (*COMPLEX_POLYFUN_FINITE_ROOTS in HOL Light*)
6229  fixes c :: "nat \<Rightarrow> 'a::{idom,real_normed_div_algebra}"
6230  shows "finite {x. (\<Sum>i\<le>n. c i * x^i) = 0} \<longleftrightarrow> (\<exists>i\<le>n. c i \<noteq> 0)"
6231    (is "?lhs = ?rhs")
6232proof
6233  assume ?lhs
6234  moreover have "\<not> finite {x. (\<Sum>i\<le>n. c i * x^i) = 0}" if "\<forall>i\<le>n. c i = 0"
6235  proof -
6236    from that have "\<And>x. (\<Sum>i\<le>n. c i * x^i) = 0"
6237      by simp
6238    then show ?thesis
6239      using ex_new_if_finite [OF infinite_UNIV_char_0 [where 'a='a]]
6240      by auto
6241  qed
6242  ultimately show ?rhs by metis
6243next
6244  assume ?rhs
6245  with polyfun_rootbound show ?lhs by blast
6246qed
6247
6248lemma polyfun_eq_0: "(\<forall>x. (\<Sum>i\<le>n. c i * x^i) = 0) \<longleftrightarrow> (\<forall>i\<le>n. c i = 0)"
6249  for c :: "nat \<Rightarrow> 'a::{idom,real_normed_div_algebra}"
6250  (*COMPLEX_POLYFUN_EQ_0 in HOL Light*)
6251  using zero_polynom_imp_zero_coeffs by auto
6252
6253lemma polyfun_eq_coeffs: "(\<forall>x. (\<Sum>i\<le>n. c i * x^i) = (\<Sum>i\<le>n. d i * x^i)) \<longleftrightarrow> (\<forall>i\<le>n. c i = d i)"
6254  for c :: "nat \<Rightarrow> 'a::{idom,real_normed_div_algebra}"
6255proof -
6256  have "(\<forall>x. (\<Sum>i\<le>n. c i * x^i) = (\<Sum>i\<le>n. d i * x^i)) \<longleftrightarrow> (\<forall>x. (\<Sum>i\<le>n. (c i - d i) * x^i) = 0)"
6257    by (simp add: left_diff_distrib Groups_Big.sum_subtractf)
6258  also have "\<dots> \<longleftrightarrow> (\<forall>i\<le>n. c i - d i = 0)"
6259    by (rule polyfun_eq_0)
6260  finally show ?thesis
6261    by simp
6262qed
6263
6264lemma polyfun_eq_const: (*COMPLEX_POLYFUN_EQ_CONST in HOL Light*)
6265  fixes c :: "nat \<Rightarrow> 'a::{idom,real_normed_div_algebra}"
6266  shows "(\<forall>x. (\<Sum>i\<le>n. c i * x^i) = k) \<longleftrightarrow> c 0 = k \<and> (\<forall>i \<in> {1..n}. c i = 0)"
6267    (is "?lhs = ?rhs")
6268proof -
6269  have *: "\<forall>x. (\<Sum>i\<le>n. (if i=0 then k else 0) * x^i) = k"
6270    by (induct n) auto
6271  show ?thesis
6272  proof
6273    assume ?lhs
6274    with * have "(\<forall>i\<le>n. c i = (if i=0 then k else 0))"
6275      by (simp add: polyfun_eq_coeffs [symmetric])
6276    then show ?rhs by simp
6277  next
6278    assume ?rhs
6279    then show ?lhs by (induct n) auto
6280  qed
6281qed
6282
6283lemma root_polyfun:
6284  fixes z :: "'a::idom"
6285  assumes "1 \<le> n"
6286  shows "z^n = a \<longleftrightarrow> (\<Sum>i\<le>n. (if i = 0 then -a else if i=n then 1 else 0) * z^i) = 0"
6287  using assms by (cases n) (simp_all add: sum_head_Suc atLeast0AtMost [symmetric])
6288
6289lemma
6290  assumes "SORT_CONSTRAINT('a::{idom,real_normed_div_algebra})"
6291    and "1 \<le> n"
6292  shows finite_roots_unity: "finite {z::'a. z^n = 1}"
6293    and card_roots_unity: "card {z::'a. z^n = 1} \<le> n"
6294  using polyfun_rootbound [of "\<lambda>i. if i = 0 then -1 else if i=n then 1 else 0" n n] assms(2)
6295  by (auto simp: root_polyfun [OF assms(2)])
6296
6297
6298subsection \<open>Hyperbolic functions\<close>
6299
6300definition sinh :: "'a :: {banach, real_normed_algebra_1} \<Rightarrow> 'a" where
6301  "sinh x = (exp x - exp (-x)) /\<^sub>R 2"
6302
6303definition cosh :: "'a :: {banach, real_normed_algebra_1} \<Rightarrow> 'a" where
6304  "cosh x = (exp x + exp (-x)) /\<^sub>R 2"
6305
6306definition tanh :: "'a :: {banach, real_normed_field} \<Rightarrow> 'a" where
6307  "tanh x = sinh x / cosh x"
6308
6309definition arsinh :: "'a :: {banach, real_normed_algebra_1, ln} \<Rightarrow> 'a" where
6310  "arsinh x = ln (x + (x^2 + 1) powr of_real (1/2))"
6311
6312definition arcosh :: "'a :: {banach, real_normed_algebra_1, ln} \<Rightarrow> 'a" where
6313  "arcosh x = ln (x + (x^2 - 1) powr of_real (1/2))"
6314
6315definition artanh :: "'a :: {banach, real_normed_field, ln} \<Rightarrow> 'a" where
6316  "artanh x = ln ((1 + x) / (1 - x)) / 2"
6317
6318lemma arsinh_0 [simp]: "arsinh 0 = 0"
6319  by (simp add: arsinh_def)
6320
6321lemma arcosh_1 [simp]: "arcosh 1 = 0"
6322  by (simp add: arcosh_def)
6323
6324lemma artanh_0 [simp]: "artanh 0 = 0"
6325  by (simp add: artanh_def)
6326
6327lemma tanh_altdef:
6328  "tanh x = (exp x - exp (-x)) / (exp x + exp (-x))"
6329proof -
6330  have "tanh x = (2 *\<^sub>R sinh x) / (2 *\<^sub>R cosh x)"
6331    by (simp add: tanh_def scaleR_conv_of_real)
6332  also have "2 *\<^sub>R sinh x = exp x - exp (-x)"
6333    by (simp add: sinh_def)
6334  also have "2 *\<^sub>R cosh x = exp x + exp (-x)"
6335    by (simp add: cosh_def)
6336  finally show ?thesis .
6337qed
6338
6339lemma tanh_real_altdef: "tanh (x::real) = (1 - exp (- 2 * x)) / (1 + exp (- 2 * x))"
6340proof -
6341  have [simp]: "exp (2 * x) = exp x * exp x" "exp (x * 2) = exp x * exp x"
6342    by (subst exp_add [symmetric]; simp)+
6343  have "tanh x = (2 * exp (-x) * sinh x) / (2 * exp (-x) * cosh x)"
6344    by (simp add: tanh_def)
6345  also have "2 * exp (-x) * sinh x = 1 - exp (-2*x)"
6346    by (simp add: exp_minus field_simps sinh_def)
6347  also have "2 * exp (-x) * cosh x = 1 + exp (-2*x)"
6348    by (simp add: exp_minus field_simps cosh_def)
6349  finally show ?thesis .
6350qed
6351
6352
6353lemma sinh_converges: "(\<lambda>n. if even n then 0 else x ^ n /\<^sub>R fact n) sums sinh x"
6354proof -
6355  have "(\<lambda>n. (x ^ n /\<^sub>R fact n - (-x) ^ n /\<^sub>R fact n) /\<^sub>R 2) sums sinh x"
6356    unfolding sinh_def by (intro sums_scaleR_right sums_diff exp_converges)
6357  also have "(\<lambda>n. (x ^ n /\<^sub>R fact n - (-x) ^ n /\<^sub>R fact n) /\<^sub>R 2) =
6358               (\<lambda>n. if even n then 0 else x ^ n /\<^sub>R fact n)" by auto
6359  finally show ?thesis .
6360qed
6361
6362lemma cosh_converges: "(\<lambda>n. if even n then x ^ n /\<^sub>R fact n else 0) sums cosh x"
6363proof -
6364  have "(\<lambda>n. (x ^ n /\<^sub>R fact n + (-x) ^ n /\<^sub>R fact n) /\<^sub>R 2) sums cosh x"
6365    unfolding cosh_def by (intro sums_scaleR_right sums_add exp_converges)
6366  also have "(\<lambda>n. (x ^ n /\<^sub>R fact n + (-x) ^ n /\<^sub>R fact n) /\<^sub>R 2) =
6367               (\<lambda>n. if even n then x ^ n /\<^sub>R fact n else 0)" by auto
6368  finally show ?thesis .
6369qed
6370
6371lemma sinh_0 [simp]: "sinh 0 = 0"
6372  by (simp add: sinh_def)
6373
6374lemma cosh_0 [simp]: "cosh 0 = 1"
6375proof -
6376  have "cosh 0 = (1/2) *\<^sub>R (1 + 1)" by (simp add: cosh_def)
6377  also have "\<dots> = 1" by (rule scaleR_half_double)
6378  finally show ?thesis .
6379qed
6380
6381lemma tanh_0 [simp]: "tanh 0 = 0"
6382  by (simp add: tanh_def)
6383
6384lemma sinh_minus [simp]: "sinh (- x) = -sinh x"
6385  by (simp add: sinh_def algebra_simps)
6386
6387lemma cosh_minus [simp]: "cosh (- x) = cosh x"
6388  by (simp add: cosh_def algebra_simps)
6389
6390lemma tanh_minus [simp]: "tanh (-x) = -tanh x"
6391  by (simp add: tanh_def)
6392
6393lemma sinh_ln_real: "x > 0 \<Longrightarrow> sinh (ln x :: real) = (x - inverse x) / 2"
6394  by (simp add: sinh_def exp_minus)
6395
6396lemma cosh_ln_real: "x > 0 \<Longrightarrow> cosh (ln x :: real) = (x + inverse x) / 2"
6397  by (simp add: cosh_def exp_minus)
6398
6399lemma tanh_ln_real: "x > 0 \<Longrightarrow> tanh (ln x :: real) = (x ^ 2 - 1) / (x ^ 2 + 1)"
6400  by (simp add: tanh_def sinh_ln_real cosh_ln_real divide_simps power2_eq_square)
6401
6402lemma has_field_derivative_scaleR_right [derivative_intros]:
6403  "(f has_field_derivative D) F \<Longrightarrow> ((\<lambda>x. c *\<^sub>R f x) has_field_derivative (c *\<^sub>R D)) F"
6404  unfolding has_field_derivative_def
6405  using has_derivative_scaleR_right[of f "\<lambda>x. D * x" F c]
6406  by (simp add: mult_scaleR_left [symmetric] del: mult_scaleR_left)
6407
6408lemma has_field_derivative_sinh [THEN DERIV_chain2, derivative_intros]:
6409  "(sinh has_field_derivative cosh x) (at (x :: 'a :: {banach, real_normed_field}))"
6410  unfolding sinh_def cosh_def by (auto intro!: derivative_eq_intros)
6411
6412lemma has_field_derivative_cosh [THEN DERIV_chain2, derivative_intros]:
6413  "(cosh has_field_derivative sinh x) (at (x :: 'a :: {banach, real_normed_field}))"
6414  unfolding sinh_def cosh_def by (auto intro!: derivative_eq_intros)
6415
6416lemma has_field_derivative_tanh [THEN DERIV_chain2, derivative_intros]:
6417  "cosh x \<noteq> 0 \<Longrightarrow> (tanh has_field_derivative 1 - tanh x ^ 2)
6418                     (at (x :: 'a :: {banach, real_normed_field}))"
6419  unfolding tanh_def by (auto intro!: derivative_eq_intros simp: power2_eq_square divide_simps)
6420
6421lemma has_derivative_sinh [derivative_intros]:
6422  fixes g :: "'a \<Rightarrow> ('a :: {banach, real_normed_field})"
6423  assumes "(g has_derivative (\<lambda>x. Db * x)) (at x within s)"
6424  shows   "((\<lambda>x. sinh (g x)) has_derivative (\<lambda>y. (cosh (g x) * Db) * y)) (at x within s)"
6425proof -
6426  have "((\<lambda>x. - g x) has_derivative (\<lambda>y. -(Db * y))) (at x within s)"
6427    using assms by (intro derivative_intros)
6428  also have "(\<lambda>y. -(Db * y)) = (\<lambda>x. (-Db) * x)" by (simp add: fun_eq_iff)
6429  finally have "((\<lambda>x. sinh (g x)) has_derivative
6430    (\<lambda>y. (exp (g x) * Db * y - exp (-g x) * (-Db) * y) /\<^sub>R 2)) (at x within s)"
6431    unfolding sinh_def by (intro derivative_intros assms)
6432  also have "(\<lambda>y. (exp (g x) * Db * y - exp (-g x) * (-Db) * y) /\<^sub>R 2) = (\<lambda>y. (cosh (g x) * Db) * y)"
6433    by (simp add: fun_eq_iff cosh_def algebra_simps)
6434  finally show ?thesis .
6435qed
6436
6437lemma has_derivative_cosh [derivative_intros]:
6438  fixes g :: "'a \<Rightarrow> ('a :: {banach, real_normed_field})"
6439  assumes "(g has_derivative (\<lambda>y. Db * y)) (at x within s)"
6440  shows   "((\<lambda>x. cosh (g x)) has_derivative (\<lambda>y. (sinh (g x) * Db) * y)) (at x within s)"
6441proof -
6442  have "((\<lambda>x. - g x) has_derivative (\<lambda>y. -(Db * y))) (at x within s)"
6443    using assms by (intro derivative_intros)
6444  also have "(\<lambda>y. -(Db * y)) = (\<lambda>y. (-Db) * y)" by (simp add: fun_eq_iff)
6445  finally have "((\<lambda>x. cosh (g x)) has_derivative
6446    (\<lambda>y. (exp (g x) * Db * y + exp (-g x) * (-Db) * y) /\<^sub>R 2)) (at x within s)"
6447    unfolding cosh_def by (intro derivative_intros assms)
6448  also have "(\<lambda>y. (exp (g x) * Db * y + exp (-g x) * (-Db) * y) /\<^sub>R 2) = (\<lambda>y. (sinh (g x) * Db) * y)"
6449    by (simp add: fun_eq_iff sinh_def algebra_simps)
6450  finally show ?thesis .
6451qed
6452
6453lemma sinh_plus_cosh: "sinh x + cosh x = exp x"
6454proof -
6455  have "sinh x + cosh x = (1 / 2) *\<^sub>R (exp x + exp x)"
6456    by (simp add: sinh_def cosh_def algebra_simps)
6457  also have "\<dots> = exp x" by (rule scaleR_half_double)
6458  finally show ?thesis .
6459qed
6460
6461lemma cosh_plus_sinh: "cosh x + sinh x = exp x"
6462  by (subst add.commute) (rule sinh_plus_cosh)
6463
6464lemma cosh_minus_sinh: "cosh x - sinh x = exp (-x)"
6465proof -
6466  have "cosh x - sinh x = (1 / 2) *\<^sub>R (exp (-x) + exp (-x))"
6467    by (simp add: sinh_def cosh_def algebra_simps)
6468  also have "\<dots> = exp (-x)" by (rule scaleR_half_double)
6469  finally show ?thesis .
6470qed
6471
6472lemma sinh_minus_cosh: "sinh x - cosh x = -exp (-x)"
6473  using cosh_minus_sinh[of x] by (simp add: algebra_simps)
6474
6475
6476context
6477  fixes x :: "'a :: {real_normed_field, banach}"
6478begin
6479
6480lemma sinh_zero_iff: "sinh x = 0 \<longleftrightarrow> exp x \<in> {1, -1}"
6481  by (auto simp: sinh_def field_simps exp_minus power2_eq_square square_eq_1_iff)
6482
6483lemma cosh_zero_iff: "cosh x = 0 \<longleftrightarrow> exp x ^ 2 = -1"
6484  by (auto simp: cosh_def exp_minus field_simps power2_eq_square eq_neg_iff_add_eq_0)
6485
6486lemma cosh_square_eq: "cosh x ^ 2 = sinh x ^ 2 + 1"
6487  by (simp add: cosh_def sinh_def algebra_simps power2_eq_square exp_add [symmetric]
6488                scaleR_conv_of_real)
6489
6490lemma sinh_square_eq: "sinh x ^ 2 = cosh x ^ 2 - 1"
6491  by (simp add: cosh_square_eq)
6492
6493lemma hyperbolic_pythagoras: "cosh x ^ 2 - sinh x ^ 2 = 1"
6494  by (simp add: cosh_square_eq)
6495
6496lemma sinh_add: "sinh (x + y) = sinh x * cosh y + cosh x * sinh y"
6497  by (simp add: sinh_def cosh_def algebra_simps scaleR_conv_of_real exp_add [symmetric])
6498
6499lemma sinh_diff: "sinh (x - y) = sinh x * cosh y - cosh x * sinh y"
6500  by (simp add: sinh_def cosh_def algebra_simps scaleR_conv_of_real exp_add [symmetric])
6501
6502lemma cosh_add: "cosh (x + y) = cosh x * cosh y + sinh x * sinh y"
6503  by (simp add: sinh_def cosh_def algebra_simps scaleR_conv_of_real exp_add [symmetric])
6504
6505lemma cosh_diff: "cosh (x - y) = cosh x * cosh y - sinh x * sinh y"
6506  by (simp add: sinh_def cosh_def algebra_simps scaleR_conv_of_real exp_add [symmetric])
6507
6508lemma tanh_add:
6509  "cosh x \<noteq> 0 \<Longrightarrow> cosh y \<noteq> 0 \<Longrightarrow> tanh (x + y) = (tanh x + tanh y) / (1 + tanh x * tanh y)"
6510  by (simp add: tanh_def sinh_add cosh_add divide_simps)
6511
6512lemma sinh_double: "sinh (2 * x) = 2 * sinh x * cosh x"
6513  using sinh_add[of x] by simp
6514
6515lemma cosh_double: "cosh (2 * x) = cosh x ^ 2 + sinh x ^ 2"
6516  using cosh_add[of x] by (simp add: power2_eq_square)
6517
6518end
6519
6520lemma sinh_field_def: "sinh z = (exp z - exp (-z)) / (2 :: 'a :: {banach, real_normed_field})"
6521  by (simp add: sinh_def scaleR_conv_of_real)
6522
6523lemma cosh_field_def: "cosh z = (exp z + exp (-z)) / (2 :: 'a :: {banach, real_normed_field})"
6524  by (simp add: cosh_def scaleR_conv_of_real)
6525
6526
6527subsubsection \<open>More specific properties of the real functions\<close>
6528
6529lemma sinh_real_zero_iff [simp]: "sinh (x::real) = 0 \<longleftrightarrow> x = 0"
6530proof -
6531  have "(-1 :: real) < 0" by simp
6532  also have "0 < exp x" by simp
6533  finally have "exp x \<noteq> -1" by (intro notI) simp
6534  thus ?thesis by (subst sinh_zero_iff) simp
6535qed
6536
6537lemma plus_inverse_ge_2:
6538  fixes x :: real
6539  assumes "x > 0"
6540  shows   "x + inverse x \<ge> 2"
6541proof -
6542  have "0 \<le> (x - 1) ^ 2" by simp
6543  also have "\<dots> = x^2 - 2*x + 1" by (simp add: power2_eq_square algebra_simps)
6544  finally show ?thesis using assms by (simp add: field_simps power2_eq_square)
6545qed
6546
6547lemma sinh_real_nonneg_iff [simp]: "sinh (x :: real) \<ge> 0 \<longleftrightarrow> x \<ge> 0"
6548  by (simp add: sinh_def)
6549
6550lemma sinh_real_pos_iff [simp]: "sinh (x :: real) > 0 \<longleftrightarrow> x > 0"
6551  by (simp add: sinh_def)
6552
6553lemma sinh_real_nonpos_iff [simp]: "sinh (x :: real) \<le> 0 \<longleftrightarrow> x \<le> 0"
6554  by (simp add: sinh_def)
6555
6556lemma sinh_real_neg_iff [simp]: "sinh (x :: real) < 0 \<longleftrightarrow> x < 0"
6557  by (simp add: sinh_def)
6558
6559lemma cosh_real_ge_1: "cosh (x :: real) \<ge> 1"
6560  using plus_inverse_ge_2[of "exp x"] by (simp add: cosh_def exp_minus)
6561
6562lemma cosh_real_pos [simp]: "cosh (x :: real) > 0"
6563  using cosh_real_ge_1[of x] by simp
6564
6565lemma cosh_real_nonneg[simp]: "cosh (x :: real) \<ge> 0"
6566  using cosh_real_ge_1[of x] by simp
6567
6568lemma cosh_real_nonzero [simp]: "cosh (x :: real) \<noteq> 0"
6569  using cosh_real_ge_1[of x] by simp
6570
6571lemma tanh_real_nonneg_iff [simp]: "tanh (x :: real) \<ge> 0 \<longleftrightarrow> x \<ge> 0"
6572  by (simp add: tanh_def field_simps)
6573
6574lemma tanh_real_pos_iff [simp]: "tanh (x :: real) > 0 \<longleftrightarrow> x > 0"
6575  by (simp add: tanh_def field_simps)
6576
6577lemma tanh_real_nonpos_iff [simp]: "tanh (x :: real) \<le> 0 \<longleftrightarrow> x \<le> 0"
6578  by (simp add: tanh_def field_simps)
6579
6580lemma tanh_real_neg_iff [simp]: "tanh (x :: real) < 0 \<longleftrightarrow> x < 0"
6581  by (simp add: tanh_def field_simps)
6582
6583lemma tanh_real_zero_iff [simp]: "tanh (x :: real) = 0 \<longleftrightarrow> x = 0"
6584  by (simp add: tanh_def field_simps)
6585
6586lemma arsinh_real_def: "arsinh (x::real) = ln (x + sqrt (x^2 + 1))"
6587  by (simp add: arsinh_def powr_half_sqrt)
6588
6589lemma arcosh_real_def: "x \<ge> 1 \<Longrightarrow> arcosh (x::real) = ln (x + sqrt (x^2 - 1))"
6590  by (simp add: arcosh_def powr_half_sqrt)
6591
6592lemma arsinh_real_aux: "0 < x + sqrt (x ^ 2 + 1 :: real)"
6593proof (cases "x < 0")
6594  case True
6595  have "(-x) ^ 2 = x ^ 2" by simp
6596  also have "x ^ 2 < x ^ 2 + 1" by simp
6597  finally have "sqrt ((-x) ^ 2) < sqrt (x ^ 2 + 1)"
6598    by (rule real_sqrt_less_mono)
6599  thus ?thesis using True by simp
6600qed (auto simp: add_nonneg_pos)
6601
6602lemma arsinh_minus_real [simp]: "arsinh (-x::real) = -arsinh x"
6603proof -
6604  have "arsinh (-x) = ln (sqrt (x\<^sup>2 + 1) - x)"
6605    by (simp add: arsinh_real_def)
6606  also have "sqrt (x^2 + 1) - x = inverse (sqrt (x^2 + 1) + x)"
6607    using arsinh_real_aux[of x] by (simp add: divide_simps algebra_simps power2_eq_square)
6608  also have "ln \<dots> = -arsinh x"
6609    using arsinh_real_aux[of x] by (simp add: arsinh_real_def ln_inverse)
6610  finally show ?thesis .
6611qed
6612
6613lemma artanh_minus_real [simp]:
6614  assumes "abs x < 1"
6615  shows   "artanh (-x::real) = -artanh x"
6616  using assms by (simp add: artanh_def ln_div field_simps)
6617
6618lemma sinh_less_cosh_real: "sinh (x :: real) < cosh x"
6619  by (simp add: sinh_def cosh_def)
6620
6621lemma sinh_le_cosh_real: "sinh (x :: real) \<le> cosh x"
6622  by (simp add: sinh_def cosh_def)
6623
6624lemma tanh_real_lt_1: "tanh (x :: real) < 1"
6625  by (simp add: tanh_def sinh_less_cosh_real)
6626
6627lemma tanh_real_gt_neg1: "tanh (x :: real) > -1"
6628proof -
6629  have "- cosh x < sinh x" by (simp add: sinh_def cosh_def divide_simps)
6630  thus ?thesis by (simp add: tanh_def field_simps)
6631qed
6632
6633lemma tanh_real_bounds: "tanh (x :: real) \<in> {-1<..<1}"
6634  using tanh_real_lt_1 tanh_real_gt_neg1 by simp
6635
6636context
6637  fixes x :: real
6638begin
6639
6640lemma arsinh_sinh_real: "arsinh (sinh x) = x"
6641  by (simp add: arsinh_real_def powr_def sinh_square_eq sinh_plus_cosh)
6642
6643lemma arcosh_cosh_real: "x \<ge> 0 \<Longrightarrow> arcosh (cosh x) = x"
6644  by (simp add: arcosh_real_def powr_def cosh_square_eq cosh_real_ge_1 cosh_plus_sinh)
6645
6646lemma artanh_tanh_real: "artanh (tanh x) = x"
6647proof -
6648  have "artanh (tanh x) = ln (cosh x * (cosh x + sinh x) / (cosh x * (cosh x - sinh x))) / 2"
6649    by (simp add: artanh_def tanh_def divide_simps)
6650  also have "cosh x * (cosh x + sinh x) / (cosh x * (cosh x - sinh x)) =
6651               (cosh x + sinh x) / (cosh x - sinh x)" by simp
6652  also have "\<dots> = (exp x)^2"
6653    by (simp add: cosh_plus_sinh cosh_minus_sinh exp_minus field_simps power2_eq_square)
6654  also have "ln ((exp x)^2) / 2 = x" by (simp add: ln_realpow)
6655  finally show ?thesis .
6656qed
6657
6658end
6659
6660lemma sinh_real_strict_mono: "strict_mono (sinh :: real \<Rightarrow> real)"
6661  by (rule pos_deriv_imp_strict_mono derivative_intros)+ auto
6662
6663lemma cosh_real_strict_mono:
6664  assumes "0 \<le> x" and "x < (y::real)"
6665  shows   "cosh x < cosh y"
6666proof -
6667  from assms have "\<exists>z>x. z < y \<and> cosh y - cosh x = (y - x) * sinh z"
6668    by (intro MVT2) (auto dest: connectedD_interval intro!: derivative_eq_intros)
6669  then obtain z where z: "z > x" "z < y" "cosh y - cosh x = (y - x) * sinh z" by blast
6670  note \<open>cosh y - cosh x = (y - x) * sinh z\<close>
6671  also from \<open>z > x\<close> and assms have "(y - x) * sinh z > 0" by (intro mult_pos_pos) auto
6672  finally show "cosh x < cosh y" by simp
6673qed
6674
6675lemma tanh_real_strict_mono: "strict_mono (tanh :: real \<Rightarrow> real)"
6676proof -
6677  have *: "tanh x ^ 2 < 1" for x :: real
6678    using tanh_real_bounds[of x] by (simp add: abs_square_less_1 abs_if)
6679  show ?thesis
6680    by (rule pos_deriv_imp_strict_mono) (insert *, auto intro!: derivative_intros)
6681qed
6682
6683lemma sinh_real_abs [simp]: "sinh (abs x :: real) = abs (sinh x)"
6684  by (simp add: abs_if)
6685
6686lemma cosh_real_abs [simp]: "cosh (abs x :: real) = cosh x"
6687  by (simp add: abs_if)
6688
6689lemma tanh_real_abs [simp]: "tanh (abs x :: real) = abs (tanh x)"
6690  by (auto simp: abs_if)
6691
6692lemma sinh_real_eq_iff [simp]: "sinh x = sinh y \<longleftrightarrow> x = (y :: real)"
6693  using sinh_real_strict_mono by (simp add: strict_mono_eq)
6694
6695lemma tanh_real_eq_iff [simp]: "tanh x = tanh y \<longleftrightarrow> x = (y :: real)"
6696  using tanh_real_strict_mono by (simp add: strict_mono_eq)
6697
6698lemma cosh_real_eq_iff [simp]: "cosh x = cosh y \<longleftrightarrow> abs x = abs (y :: real)"
6699proof -
6700  have "cosh x = cosh y \<longleftrightarrow> x = y" if "x \<ge> 0" "y \<ge> 0" for x y :: real
6701    using cosh_real_strict_mono[of x y] cosh_real_strict_mono[of y x] that
6702    by (cases x y rule: linorder_cases) auto
6703  from this[of "abs x" "abs y"] show ?thesis by simp
6704qed
6705
6706lemma sinh_real_le_iff [simp]: "sinh x \<le> sinh y \<longleftrightarrow> x \<le> (y::real)"
6707  using sinh_real_strict_mono by (simp add: strict_mono_less_eq)
6708
6709lemma cosh_real_nonneg_le_iff: "x \<ge> 0 \<Longrightarrow> y \<ge> 0 \<Longrightarrow> cosh x \<le> cosh y \<longleftrightarrow> x \<le> (y::real)"
6710  using cosh_real_strict_mono[of x y] cosh_real_strict_mono[of y x]
6711  by (cases x y rule: linorder_cases) auto
6712
6713lemma cosh_real_nonpos_le_iff: "x \<le> 0 \<Longrightarrow> y \<le> 0 \<Longrightarrow> cosh x \<le> cosh y \<longleftrightarrow> x \<ge> (y::real)"
6714  using cosh_real_nonneg_le_iff[of "-x" "-y"] by simp
6715
6716lemma tanh_real_le_iff [simp]: "tanh x \<le> tanh y \<longleftrightarrow> x \<le> (y::real)"
6717  using tanh_real_strict_mono by (simp add: strict_mono_less_eq)
6718
6719
6720lemma sinh_real_less_iff [simp]: "sinh x < sinh y \<longleftrightarrow> x < (y::real)"
6721  using sinh_real_strict_mono by (simp add: strict_mono_less)
6722
6723lemma cosh_real_nonneg_less_iff: "x \<ge> 0 \<Longrightarrow> y \<ge> 0 \<Longrightarrow> cosh x < cosh y \<longleftrightarrow> x < (y::real)"
6724  using cosh_real_strict_mono[of x y] cosh_real_strict_mono[of y x]
6725  by (cases x y rule: linorder_cases) auto
6726
6727lemma cosh_real_nonpos_less_iff: "x \<le> 0 \<Longrightarrow> y \<le> 0 \<Longrightarrow> cosh x < cosh y \<longleftrightarrow> x > (y::real)"
6728  using cosh_real_nonneg_less_iff[of "-x" "-y"] by simp
6729
6730lemma tanh_real_less_iff [simp]: "tanh x < tanh y \<longleftrightarrow> x < (y::real)"
6731  using tanh_real_strict_mono by (simp add: strict_mono_less)
6732
6733
6734subsubsection \<open>Limits\<close>
6735
6736lemma sinh_real_at_top: "filterlim (sinh :: real \<Rightarrow> real) at_top at_top"
6737proof -
6738  have *: "((\<lambda>x. - exp (- x)) \<longlongrightarrow> (-0::real)) at_top"
6739    by (intro tendsto_minus filterlim_compose[OF exp_at_bot] filterlim_uminus_at_bot_at_top)
6740  have "filterlim (\<lambda>x. (1 / 2) * (-exp (-x) + exp x) :: real) at_top at_top"
6741    by (rule filterlim_tendsto_pos_mult_at_top[OF _ _
6742               filterlim_tendsto_add_at_top[OF *]] tendsto_const)+ (auto simp: exp_at_top)
6743  also have "(\<lambda>x. (1 / 2) * (-exp (-x) + exp x) :: real) = sinh"
6744    by (simp add: fun_eq_iff sinh_def)
6745  finally show ?thesis .
6746qed
6747
6748lemma sinh_real_at_bot: "filterlim (sinh :: real \<Rightarrow> real) at_bot at_bot"
6749proof -
6750  have "filterlim (\<lambda>x. -sinh x :: real) at_bot at_top"
6751    by (simp add: filterlim_uminus_at_top [symmetric] sinh_real_at_top)
6752  also have "(\<lambda>x. -sinh x :: real) = (\<lambda>x. sinh (-x))" by simp
6753  finally show ?thesis by (subst filterlim_at_bot_mirror)
6754qed
6755
6756lemma cosh_real_at_top: "filterlim (cosh :: real \<Rightarrow> real) at_top at_top"
6757proof -
6758  have *: "((\<lambda>x. exp (- x)) \<longlongrightarrow> (0::real)) at_top"
6759    by (intro filterlim_compose[OF exp_at_bot] filterlim_uminus_at_bot_at_top)
6760  have "filterlim (\<lambda>x. (1 / 2) * (exp (-x) + exp x) :: real) at_top at_top"
6761    by (rule filterlim_tendsto_pos_mult_at_top[OF _ _
6762               filterlim_tendsto_add_at_top[OF *]] tendsto_const)+ (auto simp: exp_at_top)
6763  also have "(\<lambda>x. (1 / 2) * (exp (-x) + exp x) :: real) = cosh"
6764    by (simp add: fun_eq_iff cosh_def)
6765  finally show ?thesis .
6766qed
6767
6768lemma cosh_real_at_bot: "filterlim (cosh :: real \<Rightarrow> real) at_top at_bot"
6769proof -
6770  have "filterlim (\<lambda>x. cosh (-x) :: real) at_top at_top"
6771    by (simp add: cosh_real_at_top)
6772  thus ?thesis by (subst filterlim_at_bot_mirror)
6773qed
6774
6775lemma tanh_real_at_top: "(tanh \<longlongrightarrow> (1::real)) at_top"
6776proof -
6777  have "((\<lambda>x::real. (1 - exp (- 2 * x)) / (1 + exp (- 2 * x))) \<longlongrightarrow> (1 - 0) / (1 + 0)) at_top"
6778    by (intro tendsto_intros filterlim_compose[OF exp_at_bot]
6779              filterlim_tendsto_neg_mult_at_bot[OF tendsto_const] filterlim_ident) auto
6780  also have "(\<lambda>x::real. (1 - exp (- 2 * x)) / (1 + exp (- 2 * x))) = tanh"
6781    by (rule ext) (simp add: tanh_real_altdef)
6782  finally show ?thesis by simp
6783qed
6784
6785lemma tanh_real_at_bot: "(tanh \<longlongrightarrow> (-1::real)) at_bot"
6786proof -
6787  have "((\<lambda>x::real. -tanh x) \<longlongrightarrow> -1) at_top"
6788    by (intro tendsto_minus tanh_real_at_top)
6789  also have "(\<lambda>x. -tanh x :: real) = (\<lambda>x. tanh (-x))" by simp
6790  finally show ?thesis by (subst filterlim_at_bot_mirror)
6791qed
6792
6793
6794subsubsection \<open>Properties of the inverse hyperbolic functions\<close>
6795
6796lemma isCont_sinh: "isCont sinh (x :: 'a :: {real_normed_field, banach})"
6797  unfolding sinh_def [abs_def] by (auto intro!: continuous_intros)
6798
6799lemma isCont_cosh: "isCont cosh (x :: 'a :: {real_normed_field, banach})"
6800  unfolding cosh_def [abs_def] by (auto intro!: continuous_intros)
6801
6802lemma isCont_tanh: "cosh x \<noteq> 0 \<Longrightarrow> isCont tanh (x :: 'a :: {real_normed_field, banach})"
6803  unfolding tanh_def [abs_def]
6804  by (auto intro!: continuous_intros isCont_divide isCont_sinh isCont_cosh)
6805
6806lemma continuous_on_sinh [continuous_intros]:
6807  fixes f :: "_ \<Rightarrow>'a::{real_normed_field,banach}"
6808  assumes "continuous_on A f"
6809  shows   "continuous_on A (\<lambda>x. sinh (f x))"
6810  unfolding sinh_def using assms by (intro continuous_intros)
6811
6812lemma continuous_on_cosh [continuous_intros]:
6813  fixes f :: "_ \<Rightarrow>'a::{real_normed_field,banach}"
6814  assumes "continuous_on A f"
6815  shows   "continuous_on A (\<lambda>x. cosh (f x))"
6816  unfolding cosh_def using assms by (intro continuous_intros)
6817
6818lemma continuous_sinh [continuous_intros]:
6819  fixes f :: "_ \<Rightarrow>'a::{real_normed_field,banach}"
6820  assumes "continuous F f"
6821  shows   "continuous F (\<lambda>x. sinh (f x))"
6822  unfolding sinh_def using assms by (intro continuous_intros)
6823
6824lemma continuous_cosh [continuous_intros]:
6825  fixes f :: "_ \<Rightarrow>'a::{real_normed_field,banach}"
6826  assumes "continuous F f"
6827  shows   "continuous F (\<lambda>x. cosh (f x))"
6828  unfolding cosh_def using assms by (intro continuous_intros)
6829
6830lemma continuous_on_tanh [continuous_intros]:
6831  fixes f :: "_ \<Rightarrow>'a::{real_normed_field,banach}"
6832  assumes "continuous_on A f" "\<And>x. x \<in> A \<Longrightarrow> cosh (f x) \<noteq> 0"
6833  shows   "continuous_on A (\<lambda>x. tanh (f x))"
6834  unfolding tanh_def using assms by (intro continuous_intros) auto
6835
6836lemma continuous_at_within_tanh [continuous_intros]:
6837  fixes f :: "_ \<Rightarrow>'a::{real_normed_field,banach}"
6838  assumes "continuous (at x within A) f" "cosh (f x) \<noteq> 0"
6839  shows   "continuous (at x within A) (\<lambda>x. tanh (f x))"
6840  unfolding tanh_def using assms by (intro continuous_intros continuous_divide) auto
6841
6842lemma continuous_tanh [continuous_intros]:
6843  fixes f :: "_ \<Rightarrow>'a::{real_normed_field,banach}"
6844  assumes "continuous F f" "cosh (f (Lim F (\<lambda>x. x))) \<noteq> 0"
6845  shows   "continuous F (\<lambda>x. tanh (f x))"
6846  unfolding tanh_def using assms by (intro continuous_intros continuous_divide) auto
6847
6848lemma tendsto_sinh [tendsto_intros]:
6849  fixes f :: "_ \<Rightarrow>'a::{real_normed_field,banach}"
6850  shows "(f \<longlongrightarrow> a) F \<Longrightarrow> ((\<lambda>x. sinh (f x)) \<longlongrightarrow> sinh a) F"
6851  by (rule isCont_tendsto_compose [OF isCont_sinh])
6852
6853lemma tendsto_cosh [tendsto_intros]:
6854  fixes f :: "_ \<Rightarrow>'a::{real_normed_field,banach}"
6855  shows "(f \<longlongrightarrow> a) F \<Longrightarrow> ((\<lambda>x. cosh (f x)) \<longlongrightarrow> cosh a) F"
6856  by (rule isCont_tendsto_compose [OF isCont_cosh])
6857
6858lemma tendsto_tanh [tendsto_intros]:
6859  fixes f :: "_ \<Rightarrow>'a::{real_normed_field,banach}"
6860  shows "(f \<longlongrightarrow> a) F \<Longrightarrow> cosh a \<noteq> 0 \<Longrightarrow> ((\<lambda>x. tanh (f x)) \<longlongrightarrow> tanh a) F"
6861  by (rule isCont_tendsto_compose [OF isCont_tanh])
6862
6863
6864lemma arsinh_real_has_field_derivative [derivative_intros]:
6865  fixes x :: real
6866  shows "(arsinh has_field_derivative (1 / (sqrt (x ^ 2 + 1)))) (at x within A)"
6867proof -
6868  have pos: "1 + x ^ 2 > 0" by (intro add_pos_nonneg) auto
6869  from pos arsinh_real_aux[of x] show ?thesis unfolding arsinh_def [abs_def]
6870    by (auto intro!: derivative_eq_intros simp: powr_minus powr_half_sqrt divide_simps)
6871qed
6872
6873lemma arcosh_real_has_field_derivative [derivative_intros]:
6874  fixes x :: real
6875  assumes "x > 1"
6876  shows   "(arcosh has_field_derivative (1 / (sqrt (x ^ 2 - 1)))) (at x within A)"
6877proof -
6878  from assms have "x + sqrt (x\<^sup>2 - 1) > 0" by (simp add: add_pos_pos)
6879  thus ?thesis using assms unfolding arcosh_def [abs_def]
6880    by (auto intro!: derivative_eq_intros
6881             simp: powr_minus powr_half_sqrt divide_simps power2_eq_1_iff)
6882qed
6883
6884lemma artanh_real_has_field_derivative [derivative_intros]:
6885  fixes x :: real
6886  assumes "abs x < 1"
6887  shows   "(artanh has_field_derivative (1 / (1 - x ^ 2))) (at x within A)"
6888proof -
6889  from assms have "x > -1" "x < 1" by linarith+
6890  hence "(artanh has_field_derivative (4 - 4 * x) / ((1 + x) * (1 - x) * (1 - x) * 4))
6891           (at x within A)" unfolding artanh_def [abs_def]
6892    by (auto intro!: derivative_eq_intros simp: powr_minus powr_half_sqrt)
6893  also have "(4 - 4 * x) / ((1 + x) * (1 - x) * (1 - x) * 4) = 1 / ((1 + x) * (1 - x))"
6894    by (simp add: divide_simps)
6895  also have "(1 + x) * (1 - x) = 1 - x ^ 2" by (simp add: algebra_simps power2_eq_square)
6896  finally show ?thesis .
6897qed
6898
6899lemma continuous_on_arsinh [continuous_intros]: "continuous_on A (arsinh :: real \<Rightarrow> real)"
6900  by (rule DERIV_continuous_on derivative_intros)+
6901
6902lemma continuous_on_arcosh [continuous_intros]:
6903  assumes "A \<subseteq> {1..}"
6904  shows   "continuous_on A (arcosh :: real \<Rightarrow> real)"
6905proof -
6906  have pos: "x + sqrt (x ^ 2 - 1) > 0" if "x \<ge> 1" for x
6907    using that by (intro add_pos_nonneg) auto
6908  show ?thesis
6909  unfolding arcosh_def [abs_def]
6910  by (intro continuous_on_subset [OF _ assms] continuous_on_ln continuous_on_add
6911               continuous_on_id continuous_on_powr')
6912     (auto dest: pos simp: powr_half_sqrt intro!: continuous_intros)
6913qed
6914
6915lemma continuous_on_artanh [continuous_intros]:
6916  assumes "A \<subseteq> {-1<..<1}"
6917  shows   "continuous_on A (artanh :: real \<Rightarrow> real)"
6918  unfolding artanh_def [abs_def]
6919  by (intro continuous_on_subset [OF _ assms]) (auto intro!: continuous_intros)
6920
6921lemma continuous_on_arsinh' [continuous_intros]:
6922  fixes f :: "real \<Rightarrow> real"
6923  assumes "continuous_on A f"
6924  shows   "continuous_on A (\<lambda>x. arsinh (f x))"
6925  by (rule continuous_on_compose2[OF continuous_on_arsinh assms]) auto
6926
6927lemma continuous_on_arcosh' [continuous_intros]:
6928  fixes f :: "real \<Rightarrow> real"
6929  assumes "continuous_on A f" "\<And>x. x \<in> A \<Longrightarrow> f x \<ge> 1"
6930  shows   "continuous_on A (\<lambda>x. arcosh (f x))"
6931  by (rule continuous_on_compose2[OF continuous_on_arcosh assms(1) order.refl])
6932     (use assms(2) in auto)
6933
6934lemma continuous_on_artanh' [continuous_intros]:
6935  fixes f :: "real \<Rightarrow> real"
6936  assumes "continuous_on A f" "\<And>x. x \<in> A \<Longrightarrow> f x \<in> {-1<..<1}"
6937  shows   "continuous_on A (\<lambda>x. artanh (f x))"
6938  by (rule continuous_on_compose2[OF continuous_on_artanh assms(1) order.refl])
6939     (use assms(2) in auto)
6940
6941lemma isCont_arsinh [continuous_intros]: "isCont arsinh (x :: real)"
6942  using continuous_on_arsinh[of UNIV] by (auto simp: continuous_on_eq_continuous_at)
6943
6944lemma isCont_arcosh [continuous_intros]:
6945  assumes "x > 1"
6946  shows   "isCont arcosh (x :: real)"
6947proof -
6948  have "continuous_on {1::real<..} arcosh"
6949    by (rule continuous_on_arcosh) auto
6950  with assms show ?thesis by (auto simp: continuous_on_eq_continuous_at)
6951qed
6952
6953lemma isCont_artanh [continuous_intros]:
6954  assumes "x > -1" "x < 1"
6955  shows   "isCont artanh (x :: real)"
6956proof -
6957  have "continuous_on {-1<..<(1::real)} artanh"
6958    by (rule continuous_on_artanh) auto
6959  with assms show ?thesis by (auto simp: continuous_on_eq_continuous_at)
6960qed
6961
6962lemma tendsto_arsinh [tendsto_intros]: "(f \<longlongrightarrow> a) F \<Longrightarrow> ((\<lambda>x. arsinh (f x)) \<longlongrightarrow> arsinh a) F"
6963  for f :: "_ \<Rightarrow> real"
6964  by (rule isCont_tendsto_compose [OF isCont_arsinh])
6965
6966lemma tendsto_arcosh_strong [tendsto_intros]:
6967  fixes f :: "_ \<Rightarrow> real"
6968  assumes "(f \<longlongrightarrow> a) F" "a \<ge> 1" "eventually (\<lambda>x. f x \<ge> 1) F"
6969  shows   "((\<lambda>x. arcosh (f x)) \<longlongrightarrow> arcosh a) F"
6970  by (rule continuous_on_tendsto_compose[OF continuous_on_arcosh[OF order.refl]])
6971     (use assms in auto)
6972
6973lemma tendsto_arcosh:
6974  fixes f :: "_ \<Rightarrow> real"
6975  assumes "(f \<longlongrightarrow> a) F" "a > 1"
6976  shows "((\<lambda>x. arcosh (f x)) \<longlongrightarrow> arcosh a) F"
6977  by (rule isCont_tendsto_compose [OF isCont_arcosh]) (use assms in auto)
6978
6979lemma tendsto_arcosh_at_left_1: "(arcosh \<longlongrightarrow> 0) (at_right (1::real))"
6980proof -
6981  have "(arcosh \<longlongrightarrow> arcosh 1) (at_right (1::real))"
6982    by (rule tendsto_arcosh_strong) (auto simp: eventually_at intro!: exI[of _ 1])
6983  thus ?thesis by simp
6984qed
6985
6986lemma tendsto_artanh [tendsto_intros]:
6987  fixes f :: "'a \<Rightarrow> real"
6988  assumes "(f \<longlongrightarrow> a) F" "a > -1" "a < 1"
6989  shows   "((\<lambda>x. artanh (f x)) \<longlongrightarrow> artanh a) F"
6990  by (rule isCont_tendsto_compose [OF isCont_artanh]) (use assms in auto)
6991
6992lemma continuous_arsinh [continuous_intros]:
6993  "continuous F f \<Longrightarrow> continuous F (\<lambda>x. arsinh (f x :: real))"
6994  unfolding continuous_def by (rule tendsto_arsinh)
6995
6996(* TODO: This rule does not work for one-sided continuity at 1 *)
6997lemma continuous_arcosh_strong [continuous_intros]:
6998  assumes "continuous F f" "eventually (\<lambda>x. f x \<ge> 1) F"
6999  shows   "continuous F (\<lambda>x. arcosh (f x :: real))"
7000proof (cases "F = bot")
7001  case False
7002  show ?thesis
7003    unfolding continuous_def
7004  proof (intro tendsto_arcosh_strong)
7005    show "1 \<le> f (Lim F (\<lambda>x. x))"
7006      using assms False unfolding continuous_def by (rule tendsto_lowerbound)
7007  qed (insert assms, auto simp: continuous_def)
7008qed auto
7009
7010lemma continuous_arcosh:
7011  "continuous F f \<Longrightarrow> f (Lim F (\<lambda>x. x)) > 1 \<Longrightarrow> continuous F (\<lambda>x. arcosh (f x :: real))"
7012  unfolding continuous_def by (rule tendsto_arcosh) auto
7013
7014lemma continuous_artanh [continuous_intros]:
7015  "continuous F f \<Longrightarrow> f (Lim F (\<lambda>x. x)) \<in> {-1<..<1} \<Longrightarrow> continuous F (\<lambda>x. artanh (f x :: real))"
7016  unfolding continuous_def by (rule tendsto_artanh) auto
7017
7018lemma arsinh_real_at_top:
7019  "filterlim (arsinh :: real \<Rightarrow> real) at_top at_top"
7020proof (subst filterlim_cong[OF refl refl])
7021  show "filterlim (\<lambda>x. ln (x + sqrt (1 + x\<^sup>2))) at_top at_top"
7022    by (intro filterlim_compose[OF ln_at_top filterlim_at_top_add_at_top] filterlim_ident
7023              filterlim_compose[OF sqrt_at_top] filterlim_tendsto_add_at_top[OF tendsto_const]
7024              filterlim_pow_at_top) auto
7025qed (auto intro!: eventually_mono[OF eventually_ge_at_top[of 1]] simp: arsinh_real_def add_ac)
7026
7027lemma arsinh_real_at_bot:
7028  "filterlim (arsinh :: real \<Rightarrow> real) at_bot at_bot"
7029proof -
7030  have "filterlim (\<lambda>x::real. -arsinh x) at_bot at_top"
7031    by (subst filterlim_uminus_at_top [symmetric]) (rule arsinh_real_at_top)
7032  also have "(\<lambda>x::real. -arsinh x) = (\<lambda>x. arsinh (-x))" by simp
7033  finally show ?thesis
7034    by (subst filterlim_at_bot_mirror)
7035qed
7036
7037lemma arcosh_real_at_top:
7038  "filterlim (arcosh :: real \<Rightarrow> real) at_top at_top"
7039proof (subst filterlim_cong[OF refl refl])
7040  show "filterlim (\<lambda>x. ln (x + sqrt (-1 + x\<^sup>2))) at_top at_top"
7041    by (intro filterlim_compose[OF ln_at_top filterlim_at_top_add_at_top] filterlim_ident
7042              filterlim_compose[OF sqrt_at_top] filterlim_tendsto_add_at_top[OF tendsto_const]
7043              filterlim_pow_at_top) auto
7044qed (auto intro!: eventually_mono[OF eventually_ge_at_top[of 1]] simp: arcosh_real_def)
7045
7046lemma artanh_real_at_left_1:
7047  "filterlim (artanh :: real \<Rightarrow> real) at_top (at_left 1)"
7048proof -
7049  have *: "filterlim (\<lambda>x::real. (1 + x) / (1 - x)) at_top (at_left 1)"
7050    by (rule LIM_at_top_divide)
7051       (auto intro!: tendsto_eq_intros eventually_mono[OF eventually_at_left_real[of 0]])
7052  have "filterlim (\<lambda>x::real. (1/2) * ln ((1 + x) / (1 - x))) at_top (at_left 1)"
7053    by (intro filterlim_tendsto_pos_mult_at_top[OF tendsto_const] *
7054                 filterlim_compose[OF ln_at_top]) auto
7055  also have "(\<lambda>x::real. (1/2) * ln ((1 + x) / (1 - x))) = artanh"
7056    by (simp add: artanh_def [abs_def])
7057  finally show ?thesis .
7058qed
7059
7060lemma artanh_real_at_right_1:
7061  "filterlim (artanh :: real \<Rightarrow> real) at_bot (at_right (-1))"
7062proof -
7063  have "?thesis \<longleftrightarrow> filterlim (\<lambda>x::real. -artanh x) at_top (at_right (-1))"
7064    by (simp add: filterlim_uminus_at_bot)
7065  also have "\<dots> \<longleftrightarrow> filterlim (\<lambda>x::real. artanh (-x)) at_top (at_right (-1))"
7066    by (intro filterlim_cong refl eventually_mono[OF eventually_at_right_real[of "-1" "1"]]) auto
7067  also have "\<dots> \<longleftrightarrow> filterlim (artanh :: real \<Rightarrow> real) at_top (at_left 1)"
7068    by (simp add: filterlim_at_left_to_right)
7069  also have \<dots> by (rule artanh_real_at_left_1)
7070  finally show ?thesis .
7071qed
7072
7073
7074subsection \<open>Simprocs for root and power literals\<close>
7075
7076lemma numeral_powr_numeral_real [simp]:
7077  "numeral m powr numeral n = (numeral m ^ numeral n :: real)"
7078  by (simp add: powr_numeral)
7079
7080context
7081begin
7082
7083private lemma sqrt_numeral_simproc_aux:
7084  assumes "m * m \<equiv> n"
7085  shows   "sqrt (numeral n :: real) \<equiv> numeral m"
7086proof -
7087  have "numeral n \<equiv> numeral m * (numeral m :: real)" by (simp add: assms [symmetric])
7088  moreover have "sqrt \<dots> \<equiv> numeral m" by (subst real_sqrt_abs2) simp
7089  ultimately show "sqrt (numeral n :: real) \<equiv> numeral m" by simp
7090qed
7091
7092private lemma root_numeral_simproc_aux:
7093  assumes "Num.pow m n \<equiv> x"
7094  shows   "root (numeral n) (numeral x :: real) \<equiv> numeral m"
7095  by (subst assms [symmetric], subst numeral_pow, subst real_root_pos2) simp_all
7096
7097private lemma powr_numeral_simproc_aux:
7098  assumes "Num.pow y n = x"
7099  shows   "numeral x powr (m / numeral n :: real) \<equiv> numeral y powr m"
7100  by (subst assms [symmetric], subst numeral_pow, subst powr_numeral [symmetric])
7101     (simp, subst powr_powr, simp_all)
7102
7103private lemma numeral_powr_inverse_eq:
7104  "numeral x powr (inverse (numeral n)) = numeral x powr (1 / numeral n :: real)"
7105  by simp
7106
7107
7108ML \<open>
7109
7110signature ROOT_NUMERAL_SIMPROC = sig
7111
7112val sqrt : int option -> int -> int option
7113val sqrt' : int option -> int -> int option
7114val nth_root : int option -> int -> int -> int option
7115val nth_root' : int option -> int -> int -> int option
7116val sqrt_simproc : Proof.context -> cterm -> thm option
7117val root_simproc : int * int -> Proof.context -> cterm -> thm option
7118val powr_simproc : int * int -> Proof.context -> cterm -> thm option
7119
7120end
7121
7122structure Root_Numeral_Simproc : ROOT_NUMERAL_SIMPROC = struct
7123
7124fun iterate NONE p f x =
7125      let
7126        fun go x = if p x then x else go (f x)
7127      in
7128        SOME (go x)
7129      end
7130  | iterate (SOME threshold) p f x =
7131      let
7132        fun go (threshold, x) = 
7133          if p x then SOME x else if threshold = 0 then NONE else go (threshold - 1, f x)
7134      in
7135        go (threshold, x)
7136      end  
7137
7138
7139fun nth_root _ 1 x = SOME x
7140  | nth_root _ _ 0 = SOME 0
7141  | nth_root _ _ 1 = SOME 1
7142  | nth_root threshold n x =
7143  let
7144    fun newton_step y = ((n - 1) * y + x div Integer.pow (n - 1) y) div n
7145    fun is_root y = Integer.pow n y <= x andalso x < Integer.pow n (y + 1)
7146  in
7147    if x < n then
7148      SOME 1
7149    else if x < Integer.pow n 2 then 
7150      SOME 1 
7151    else 
7152      let
7153        val y = Real.floor (Math.pow (Real.fromInt x, Real.fromInt 1 / Real.fromInt n))
7154      in
7155        if is_root y then
7156          SOME y
7157        else
7158          iterate threshold is_root newton_step ((x + n - 1) div n)
7159      end
7160  end
7161
7162fun nth_root' _ 1 x = SOME x
7163  | nth_root' _ _ 0 = SOME 0
7164  | nth_root' _ _ 1 = SOME 1
7165  | nth_root' threshold n x = if x < n then NONE else if x < Integer.pow n 2 then NONE else
7166      case nth_root threshold n x of
7167        NONE => NONE
7168      | SOME y => if Integer.pow n y = x then SOME y else NONE
7169
7170fun sqrt _ 0 = SOME 0
7171  | sqrt _ 1 = SOME 1
7172  | sqrt threshold n =
7173    let
7174      fun aux (a, b) = if n >= b * b then aux (b, b * b) else (a, b)
7175      val (lower_root, lower_n) = aux (1, 2)
7176      fun newton_step x = (x + n div x) div 2
7177      fun is_sqrt r = r*r <= n andalso n < (r+1)*(r+1)
7178      val y = Real.floor (Math.sqrt (Real.fromInt n))
7179    in
7180      if is_sqrt y then 
7181        SOME y
7182      else
7183        Option.mapPartial (iterate threshold is_sqrt newton_step o (fn x => x * lower_root)) 
7184          (sqrt threshold (n div lower_n))
7185    end
7186
7187fun sqrt' threshold x =
7188  case sqrt threshold x of
7189    NONE => NONE
7190  | SOME y => if y * y = x then SOME y else NONE
7191
7192fun sqrt_simproc ctxt ct =
7193  let
7194    val n = ct |> Thm.term_of |> dest_comb |> snd |> dest_comb |> snd |> HOLogic.dest_numeral
7195  in
7196    case sqrt' (SOME 10000) n of
7197      NONE => NONE
7198    | SOME m => 
7199        SOME (Thm.instantiate' [] (map (SOME o Thm.cterm_of ctxt o HOLogic.mk_numeral) [m, n])
7200                  @{thm sqrt_numeral_simproc_aux})
7201  end
7202    handle TERM _ => NONE
7203
7204fun root_simproc (threshold1, threshold2) ctxt ct =
7205  let
7206    val [n, x] = 
7207      ct |> Thm.term_of |> strip_comb |> snd |> map (dest_comb #> snd #> HOLogic.dest_numeral)
7208  in
7209    if n > threshold1 orelse x > threshold2 then NONE else
7210      case nth_root' (SOME 100) n x of
7211        NONE => NONE
7212      | SOME m => 
7213          SOME (Thm.instantiate' [] (map (SOME o Thm.cterm_of ctxt o HOLogic.mk_numeral) [m, n, x])
7214            @{thm root_numeral_simproc_aux})
7215  end
7216    handle TERM _ => NONE
7217         | Match => NONE
7218
7219fun powr_simproc (threshold1, threshold2) ctxt ct =
7220  let
7221    val eq_thm = Conv.try_conv (Conv.rewr_conv @{thm numeral_powr_inverse_eq}) ct
7222    val ct = Thm.dest_equals_rhs (Thm.cprop_of eq_thm)
7223    val (_, [x, t]) = strip_comb (Thm.term_of ct)
7224    val (_, [m, n]) = strip_comb t
7225    val [x, n] = map (dest_comb #> snd #> HOLogic.dest_numeral) [x, n]
7226  in
7227    if n > threshold1 orelse x > threshold2 then NONE else
7228      case nth_root' (SOME 100) n x of
7229        NONE => NONE
7230      | SOME y => 
7231          let
7232            val [y, n, x] = map HOLogic.mk_numeral [y, n, x]
7233            val thm = Thm.instantiate' [] (map (SOME o Thm.cterm_of ctxt) [y, n, x, m])
7234              @{thm powr_numeral_simproc_aux}
7235          in
7236            SOME (@{thm transitive} OF [eq_thm, thm])
7237          end
7238  end
7239    handle TERM _ => NONE
7240         | Match => NONE
7241
7242end
7243\<close>
7244
7245end
7246
7247simproc_setup sqrt_numeral ("sqrt (numeral n)") = 
7248  \<open>K Root_Numeral_Simproc.sqrt_simproc\<close>
7249  
7250simproc_setup root_numeral ("root (numeral n) (numeral x)") = 
7251  \<open>K (Root_Numeral_Simproc.root_simproc (200, Integer.pow 200 2))\<close>
7252
7253simproc_setup powr_divide_numeral 
7254  ("numeral x powr (m / numeral n :: real)" | "numeral x powr (inverse (numeral n) :: real)") = 
7255    \<open>K (Root_Numeral_Simproc.powr_simproc (200, Integer.pow 200 2))\<close>
7256
7257
7258lemma "root 100 1267650600228229401496703205376 = 2"
7259  by simp
7260    
7261lemma "sqrt 196 = 14" 
7262  by simp
7263
7264lemma "256 powr (7 / 4 :: real) = 16384"
7265  by simp
7266    
7267lemma "27 powr (inverse 3) = (3::real)"
7268  by simp
7269
7270end
7271