Added derivation lemmas for power series and theorems for the pi, arcus tangens and logarithm series
authorhoelzl
Thu Feb 05 11:34:42 2009 +0100 (2009-02-05)
changeset 29803c56a5571f60a
parent 29802 d201a838d0f7
child 29804 e15b74577368
Added derivation lemmas for power series and theorems for the pi, arcus tangens and logarithm series
src/HOL/Deriv.thy
src/HOL/Lim.thy
src/HOL/MacLaurin.thy
src/HOL/Parity.thy
src/HOL/SEQ.thy
src/HOL/Series.thy
src/HOL/Transcendental.thy
     1.1 --- a/src/HOL/Deriv.thy	Wed Feb 04 18:10:07 2009 +0100
     1.2 +++ b/src/HOL/Deriv.thy	Thu Feb 05 11:34:42 2009 +0100
     1.3 @@ -1038,6 +1038,15 @@
     1.4    qed
     1.5  qed
     1.6  
     1.7 +lemma MVT2:
     1.8 +     "[| a < b; \<forall>x. a \<le> x & x \<le> b --> DERIV f x :> f'(x) |]
     1.9 +      ==> \<exists>z::real. a < z & z < b & (f b - f a = (b - a) * f'(z))"
    1.10 +apply (drule MVT)
    1.11 +apply (blast intro: DERIV_isCont)
    1.12 +apply (force dest: order_less_imp_le simp add: differentiable_def)
    1.13 +apply (blast dest: DERIV_unique order_less_imp_le)
    1.14 +done
    1.15 +
    1.16  
    1.17  text{*A function is constant if its derivative is 0 over an interval.*}
    1.18  
    1.19 @@ -1073,6 +1082,30 @@
    1.20  apply (blast dest: DERIV_isconst1)
    1.21  done
    1.22  
    1.23 +lemma DERIV_isconst3: fixes a b x y :: real
    1.24 +  assumes "a < b" and "x \<in> {a <..< b}" and "y \<in> {a <..< b}"
    1.25 +  assumes derivable: "\<And>x. x \<in> {a <..< b} \<Longrightarrow> DERIV f x :> 0"
    1.26 +  shows "f x = f y"
    1.27 +proof (cases "x = y")
    1.28 +  case False
    1.29 +  let ?a = "min x y"
    1.30 +  let ?b = "max x y"
    1.31 +  
    1.32 +  have "\<forall>z. ?a \<le> z \<and> z \<le> ?b \<longrightarrow> DERIV f z :> 0"
    1.33 +  proof (rule allI, rule impI)
    1.34 +    fix z :: real assume "?a \<le> z \<and> z \<le> ?b"
    1.35 +    hence "a < z" and "z < b" using `x \<in> {a <..< b}` and `y \<in> {a <..< b}` by auto
    1.36 +    hence "z \<in> {a<..<b}" by auto
    1.37 +    thus "DERIV f z :> 0" by (rule derivable)
    1.38 +  qed
    1.39 +  hence isCont: "\<forall>z. ?a \<le> z \<and> z \<le> ?b \<longrightarrow> isCont f z"
    1.40 +    and DERIV: "\<forall>z. ?a < z \<and> z < ?b \<longrightarrow> DERIV f z :> 0" using DERIV_isCont by auto
    1.41 +
    1.42 +  have "?a < ?b" using `x \<noteq> y` by auto
    1.43 +  from DERIV_isconst2[OF this isCont DERIV, of x] and DERIV_isconst2[OF this isCont DERIV, of y]
    1.44 +  show ?thesis by auto
    1.45 +qed auto
    1.46 +
    1.47  lemma DERIV_isconst_all:
    1.48    fixes f :: "real => real"
    1.49    shows "\<forall>x. DERIV f x :> 0 ==> f(x) = f(y)"
     2.1 --- a/src/HOL/Lim.thy	Wed Feb 04 18:10:07 2009 +0100
     2.2 +++ b/src/HOL/Lim.thy	Thu Feb 05 11:34:42 2009 +0100
     2.3 @@ -532,6 +532,44 @@
     2.4  lemma isCont_abs [simp]: "isCont abs (a::real)"
     2.5  by (rule isCont_rabs [OF isCont_ident])
     2.6  
     2.7 +lemma isCont_setsum: fixes A :: "nat set" assumes "finite A"
     2.8 +  shows "\<forall> i \<in> A. isCont (f i) x \<Longrightarrow> isCont (\<lambda> x. \<Sum> i \<in> A. f i x) x"
     2.9 +  using `finite A`
    2.10 +proof induct
    2.11 +  case (insert a F) show "isCont (\<lambda> x. \<Sum> i \<in> (insert a F). f i x) x" 
    2.12 +    unfolding setsum_insert[OF `finite F` `a \<notin> F`] by (rule isCont_add, auto simp add: insert)
    2.13 +qed auto
    2.14 +
    2.15 +lemma LIM_less_bound: fixes f :: "real \<Rightarrow> real" assumes "b < x"
    2.16 +  and all_le: "\<forall> x' \<in> { b <..< x}. 0 \<le> f x'" and isCont: "isCont f x"
    2.17 +  shows "0 \<le> f x"
    2.18 +proof (rule ccontr)
    2.19 +  assume "\<not> 0 \<le> f x" hence "f x < 0" by auto
    2.20 +  hence "0 < - f x / 2" by auto
    2.21 +  from isCont[unfolded isCont_def, THEN LIM_D, OF this]
    2.22 +  obtain s where "s > 0" and s_D: "\<And>x'. \<lbrakk> x' \<noteq> x ; \<bar> x' - x \<bar> < s \<rbrakk> \<Longrightarrow> \<bar> f x' - f x \<bar> < - f x / 2" by auto
    2.23 +
    2.24 +  let ?x = "x - min (s / 2) ((x - b) / 2)"
    2.25 +  have "?x < x" and "\<bar> ?x - x \<bar> < s"
    2.26 +    using `b < x` and `0 < s` by auto
    2.27 +  have "b < ?x"
    2.28 +  proof (cases "s < x - b")
    2.29 +    case True thus ?thesis using `0 < s` by auto
    2.30 +  next
    2.31 +    case False hence "s / 2 \<ge> (x - b) / 2" by auto
    2.32 +    from inf_absorb2[OF this, unfolded inf_real_def]
    2.33 +    have "?x = (x + b) / 2" by auto
    2.34 +    thus ?thesis using `b < x` by auto
    2.35 +  qed
    2.36 +  hence "0 \<le> f ?x" using all_le `?x < x` by auto
    2.37 +  moreover have "\<bar>f ?x - f x\<bar> < - f x / 2"
    2.38 +    using s_D[OF _ `\<bar> ?x - x \<bar> < s`] `?x < x` by auto
    2.39 +  hence "f ?x - f x < - f x / 2" by auto
    2.40 +  hence "f ?x < f x / 2" by auto
    2.41 +  hence "f ?x < 0" using `f x < 0` by auto
    2.42 +  thus False using `0 \<le> f ?x` by auto
    2.43 +qed
    2.44 +  
    2.45  
    2.46  subsection {* Uniform Continuity *}
    2.47  
     3.1 --- a/src/HOL/MacLaurin.thy	Wed Feb 04 18:10:07 2009 +0100
     3.2 +++ b/src/HOL/MacLaurin.thy	Thu Feb 05 11:34:42 2009 +0100
     3.3 @@ -389,15 +389,6 @@
     3.4  
     3.5  subsection{*Version for Sine Function*}
     3.6  
     3.7 -lemma MVT2:
     3.8 -     "[| a < b; \<forall>x. a \<le> x & x \<le> b --> DERIV f x :> f'(x) |]
     3.9 -      ==> \<exists>z::real. a < z & z < b & (f b - f a = (b - a) * f'(z))"
    3.10 -apply (drule MVT)
    3.11 -apply (blast intro: DERIV_isCont)
    3.12 -apply (force dest: order_less_imp_le simp add: differentiable_def)
    3.13 -apply (blast dest: DERIV_unique order_less_imp_le)
    3.14 -done
    3.15 -
    3.16  lemma mod_exhaust_less_4:
    3.17    "m mod 4 = 0 | m mod 4 = 1 | m mod 4 = 2 | m mod 4 = (3::nat)"
    3.18  by auto
     4.1 --- a/src/HOL/Parity.thy	Wed Feb 04 18:10:07 2009 +0100
     4.2 +++ b/src/HOL/Parity.thy	Thu Feb 05 11:34:42 2009 +0100
     4.3 @@ -291,6 +291,40 @@
     4.4    apply simp
     4.5    done
     4.6  
     4.7 +lemma power_mono_even: fixes x y :: "'a :: {recpower, ordered_idom}"
     4.8 +  assumes "even n" and "\<bar>x\<bar> \<le> \<bar>y\<bar>"
     4.9 +  shows "x^n \<le> y^n"
    4.10 +proof -
    4.11 +  have "0 \<le> \<bar>x\<bar>" by auto
    4.12 +  with `\<bar>x\<bar> \<le> \<bar>y\<bar>`
    4.13 +  have "\<bar>x\<bar>^n \<le> \<bar>y\<bar>^n" by (rule power_mono)
    4.14 +  thus ?thesis unfolding power_even_abs[OF `even n`] .
    4.15 +qed
    4.16 +
    4.17 +lemma odd_pos: "odd (n::nat) \<Longrightarrow> 0 < n" by presburger
    4.18 +
    4.19 +lemma power_mono_odd: fixes x y :: "'a :: {recpower, ordered_idom}"
    4.20 +  assumes "odd n" and "x \<le> y"
    4.21 +  shows "x^n \<le> y^n"
    4.22 +proof (cases "y < 0")
    4.23 +  case True with `x \<le> y` have "-y \<le> -x" and "0 \<le> -y" by auto
    4.24 +  hence "(-y)^n \<le> (-x)^n" by (rule power_mono)
    4.25 +  thus ?thesis unfolding power_minus_odd[OF `odd n`] by auto
    4.26 +next
    4.27 +  case False
    4.28 +  show ?thesis
    4.29 +  proof (cases "x < 0")
    4.30 +    case True hence "n \<noteq> 0" and "x \<le> 0" using `odd n`[THEN odd_pos] by auto
    4.31 +    hence "x^n \<le> 0" unfolding power_le_zero_eq using `odd n` by auto
    4.32 +    moreover
    4.33 +    from `\<not> y < 0` have "0 \<le> y" by auto
    4.34 +    hence "0 \<le> y^n" by auto
    4.35 +    ultimately show ?thesis by auto
    4.36 +  next
    4.37 +    case False hence "0 \<le> x" by auto
    4.38 +    with `x \<le> y` show ?thesis using power_mono by auto
    4.39 +  qed
    4.40 +qed
    4.41  
    4.42  subsection {* General Lemmas About Division *}
    4.43  
    4.44 @@ -405,8 +439,6 @@
    4.45  
    4.46  subsection {* Miscellaneous *}
    4.47  
    4.48 -lemma odd_pos: "odd (n::nat) \<Longrightarrow> 0 < n" by presburger
    4.49 -
    4.50  lemma [presburger]:"(x + 1) div 2 = x div 2 \<longleftrightarrow> even (x::int)" by presburger
    4.51  lemma [presburger]: "(x + 1) div 2 = x div 2 + 1 \<longleftrightarrow> odd (x::int)" by presburger
    4.52  lemma even_plus_one_div_two: "even (x::int) ==> (x + 1) div 2 = x div 2"  by presburger
     5.1 --- a/src/HOL/SEQ.thy	Wed Feb 04 18:10:07 2009 +0100
     5.2 +++ b/src/HOL/SEQ.thy	Thu Feb 05 11:34:42 2009 +0100
     5.3 @@ -346,6 +346,11 @@
     5.4  lemma LIMSEQ_Suc_iff: "(\<lambda>n. f (Suc n)) ----> l = f ----> l"
     5.5  by (blast intro: LIMSEQ_imp_Suc LIMSEQ_Suc)
     5.6  
     5.7 +lemma LIMSEQ_linear: "\<lbrakk> X ----> x ; l > 0 \<rbrakk> \<Longrightarrow> (\<lambda> n. X (n * l)) ----> x"
     5.8 +  unfolding LIMSEQ_def
     5.9 +  by (metis div_le_dividend div_mult_self1_is_m le_trans nat_mult_commute)
    5.10 +
    5.11 +
    5.12  lemma add_diff_add:
    5.13    fixes a b c d :: "'a::ab_group_add"
    5.14    shows "(a + c) - (b + d) = (a - b) + (c - d)"
    5.15 @@ -678,6 +683,69 @@
    5.16  lemma mono_SucI2: "\<forall>n. X (Suc n) \<le> X n ==> monoseq X"
    5.17  by (simp add: monoseq_Suc)
    5.18  
    5.19 +lemma monoseq_minus: assumes "monoseq a"
    5.20 +  shows "monoseq (\<lambda> n. - a n)"
    5.21 +proof (cases "\<forall> m. \<forall> n \<ge> m. a m \<le> a n")
    5.22 +  case True
    5.23 +  hence "\<forall> m. \<forall> n \<ge> m. - a n \<le> - a m" by auto
    5.24 +  thus ?thesis by (rule monoI2)
    5.25 +next
    5.26 +  case False
    5.27 +  hence "\<forall> m. \<forall> n \<ge> m. - a m \<le> - a n" using `monoseq a`[unfolded monoseq_def] by auto
    5.28 +  thus ?thesis by (rule monoI1)
    5.29 +qed
    5.30 +
    5.31 +lemma monoseq_le: assumes "monoseq a" and "a ----> x"
    5.32 +  shows "((\<forall> n. a n \<le> x) \<and> (\<forall>m. \<forall>n\<ge>m. a m \<le> a n)) \<or> 
    5.33 +         ((\<forall> n. x \<le> a n) \<and> (\<forall>m. \<forall>n\<ge>m. a n \<le> a m))"
    5.34 +proof -
    5.35 +  { fix x n fix a :: "nat \<Rightarrow> real"
    5.36 +    assume "a ----> x" and "\<forall> m. \<forall> n \<ge> m. a m \<le> a n"
    5.37 +    hence monotone: "\<And> m n. m \<le> n \<Longrightarrow> a m \<le> a n" by auto
    5.38 +    have "a n \<le> x"
    5.39 +    proof (rule ccontr)
    5.40 +      assume "\<not> a n \<le> x" hence "x < a n" by auto
    5.41 +      hence "0 < a n - x" by auto
    5.42 +      from `a ----> x`[THEN LIMSEQ_D, OF this]
    5.43 +      obtain no where "\<And>n'. no \<le> n' \<Longrightarrow> norm (a n' - x) < a n - x" by blast
    5.44 +      hence "norm (a (max no n) - x) < a n - x" by auto
    5.45 +      moreover
    5.46 +      { fix n' have "n \<le> n' \<Longrightarrow> x < a n'" using monotone[where m=n and n=n'] and `x < a n` by auto }
    5.47 +      hence "x < a (max no n)" by auto
    5.48 +      ultimately
    5.49 +      have "a (max no n) < a n" by auto
    5.50 +      with monotone[where m=n and n="max no n"]
    5.51 +      show False by auto
    5.52 +    qed
    5.53 +  } note top_down = this
    5.54 +  { fix x n m fix a :: "nat \<Rightarrow> real"
    5.55 +    assume "a ----> x" and "monoseq a" and "a m < x"
    5.56 +    have "a n \<le> x \<and> (\<forall> m. \<forall> n \<ge> m. a m \<le> a n)"
    5.57 +    proof (cases "\<forall> m. \<forall> n \<ge> m. a m \<le> a n")
    5.58 +      case True with top_down and `a ----> x` show ?thesis by auto
    5.59 +    next
    5.60 +      case False with `monoseq a`[unfolded monoseq_def] have "\<forall> m. \<forall> n \<ge> m. - a m \<le> - a n" by auto
    5.61 +      hence "- a m \<le> - x" using top_down[OF LIMSEQ_minus[OF `a ----> x`]] by blast
    5.62 +      hence False using `a m < x` by auto
    5.63 +      thus ?thesis ..
    5.64 +    qed
    5.65 +  } note when_decided = this
    5.66 +
    5.67 +  show ?thesis
    5.68 +  proof (cases "\<exists> m. a m \<noteq> x")
    5.69 +    case True then obtain m where "a m \<noteq> x" by auto
    5.70 +    show ?thesis
    5.71 +    proof (cases "a m < x")
    5.72 +      case True with when_decided[OF `a ----> x` `monoseq a`, where m2=m]
    5.73 +      show ?thesis by blast
    5.74 +    next
    5.75 +      case False hence "- a m < - x" using `a m \<noteq> x` by auto
    5.76 +      with when_decided[OF LIMSEQ_minus[OF `a ----> x`] monoseq_minus[OF `monoseq a`], where m2=m]
    5.77 +      show ?thesis by auto
    5.78 +    qed
    5.79 +  qed auto
    5.80 +qed
    5.81 +
    5.82  text{*Bounded Sequence*}
    5.83  
    5.84  lemma BseqD: "Bseq X ==> \<exists>K. 0 < K & (\<forall>n. norm (X n) \<le> K)"
     6.1 --- a/src/HOL/Series.thy	Wed Feb 04 18:10:07 2009 +0100
     6.2 +++ b/src/HOL/Series.thy	Thu Feb 05 11:34:42 2009 +0100
     6.3 @@ -140,6 +140,24 @@
     6.4      suminf f = (SUM i = 0..< k. f i) + suminf (%n. f(n + k))"
     6.5  by (auto simp add: suminf_minus_initial_segment)
     6.6  
     6.7 +lemma suminf_exist_split: fixes r :: real assumes "0 < r" and "summable a"
     6.8 +  shows "\<exists> N. \<forall> n \<ge> N. \<bar> \<Sum> i. a (i + n) \<bar> < r"
     6.9 +proof -
    6.10 +  from LIMSEQ_D[OF summable_sumr_LIMSEQ_suminf[OF `summable a`] `0 < r`]
    6.11 +  obtain N :: nat where "\<forall> n \<ge> N. norm (setsum a {0..<n} - suminf a) < r" by auto
    6.12 +  thus ?thesis unfolding suminf_minus_initial_segment[OF `summable a` refl] abs_minus_commute real_norm_def
    6.13 +    by auto
    6.14 +qed
    6.15 +
    6.16 +lemma sums_Suc: assumes sumSuc: "(\<lambda> n. f (Suc n)) sums l" shows "f sums (l + f 0)"
    6.17 +proof -
    6.18 +  from sumSuc[unfolded sums_def]
    6.19 +  have "(\<lambda>i. \<Sum>n = Suc 0..<Suc i. f n) ----> l" unfolding setsum_reindex[OF inj_Suc] image_Suc_atLeastLessThan[symmetric] comp_def .
    6.20 +  from LIMSEQ_add_const[OF this, where b="f 0"] 
    6.21 +  have "(\<lambda>i. \<Sum>n = 0..<Suc i. f n) ----> l + f 0" unfolding add_commute setsum_head_upt_Suc[OF zero_less_Suc] .
    6.22 +  thus ?thesis unfolding sums_def by (rule LIMSEQ_imp_Suc)
    6.23 +qed
    6.24 +
    6.25  lemma series_zero: 
    6.26       "(\<forall>m. n \<le> m --> f(m) = 0) ==> f sums (setsum f {0..<n})"
    6.27  apply (simp add: sums_def LIMSEQ_def diff_minus[symmetric], safe)
     7.1 --- a/src/HOL/Transcendental.thy	Wed Feb 04 18:10:07 2009 +0100
     7.2 +++ b/src/HOL/Transcendental.thy	Thu Feb 05 11:34:42 2009 +0100
     7.3 @@ -113,6 +113,208 @@
     7.4        ==> summable (%n. f(n) * (z ^ n))"
     7.5  by (rule powser_insidea [THEN summable_norm_cancel])
     7.6  
     7.7 +lemma sum_split_even_odd: fixes f :: "nat \<Rightarrow> real" shows
     7.8 +  "(\<Sum> i = 0 ..< 2 * n. if even i then f i else g i) = 
     7.9 +   (\<Sum> i = 0 ..< n. f (2 * i)) + (\<Sum> i = 0 ..< n. g (2 * i + 1))"
    7.10 +proof (induct n)
    7.11 +  case (Suc n)
    7.12 +  have "(\<Sum> i = 0 ..< 2 * Suc n. if even i then f i else g i) = 
    7.13 +        (\<Sum> i = 0 ..< n. f (2 * i)) + (\<Sum> i = 0 ..< n. g (2 * i + 1)) + (f (2 * n) + g (2 * n + 1))"
    7.14 +    using Suc.hyps by auto
    7.15 +  also have "\<dots> = (\<Sum> i = 0 ..< Suc n. f (2 * i)) + (\<Sum> i = 0 ..< Suc n. g (2 * i + 1))" by auto
    7.16 +  finally show ?case .
    7.17 +qed auto
    7.18 +
    7.19 +lemma sums_if': fixes g :: "nat \<Rightarrow> real" assumes "g sums x"
    7.20 +  shows "(\<lambda> n. if even n then 0 else g ((n - 1) div 2)) sums x"
    7.21 +  unfolding sums_def
    7.22 +proof (rule LIMSEQ_I)
    7.23 +  fix r :: real assume "0 < r"
    7.24 +  from `g sums x`[unfolded sums_def, THEN LIMSEQ_D, OF this]
    7.25 +  obtain no where no_eq: "\<And> n. n \<ge> no \<Longrightarrow> (norm (setsum g { 0..<n } - x) < r)" by blast
    7.26 +
    7.27 +  let ?SUM = "\<lambda> m. \<Sum> i = 0 ..< m. if even i then 0 else g ((i - 1) div 2)"
    7.28 +  { fix m assume "m \<ge> 2 * no" hence "m div 2 \<ge> no" by auto
    7.29 +    have sum_eq: "?SUM (2 * (m div 2)) = setsum g { 0 ..< m div 2 }" 
    7.30 +      using sum_split_even_odd by auto
    7.31 +    hence "(norm (?SUM (2 * (m div 2)) - x) < r)" using no_eq unfolding sum_eq using `m div 2 \<ge> no` by auto
    7.32 +    moreover
    7.33 +    have "?SUM (2 * (m div 2)) = ?SUM m"
    7.34 +    proof (cases "even m")
    7.35 +      case True show ?thesis unfolding even_nat_div_two_times_two[OF True, unfolded numeral_2_eq_2[symmetric]] ..
    7.36 +    next
    7.37 +      case False hence "even (Suc m)" by auto
    7.38 +      from even_nat_div_two_times_two[OF this, unfolded numeral_2_eq_2[symmetric]] odd_nat_plus_one_div_two[OF False, unfolded numeral_2_eq_2[symmetric]]
    7.39 +      have eq: "Suc (2 * (m div 2)) = m" by auto
    7.40 +      hence "even (2 * (m div 2))" using `odd m` by auto
    7.41 +      have "?SUM m = ?SUM (Suc (2 * (m div 2)))" unfolding eq ..
    7.42 +      also have "\<dots> = ?SUM (2 * (m div 2))" using `even (2 * (m div 2))` by auto
    7.43 +      finally show ?thesis by auto
    7.44 +    qed
    7.45 +    ultimately have "(norm (?SUM m - x) < r)" by auto
    7.46 +  }
    7.47 +  thus "\<exists> no. \<forall> m \<ge> no. norm (?SUM m - x) < r" by blast
    7.48 +qed
    7.49 +
    7.50 +lemma sums_if: fixes g :: "nat \<Rightarrow> real" assumes "g sums x" and "f sums y"
    7.51 +  shows "(\<lambda> n. if even n then f (n div 2) else g ((n - 1) div 2)) sums (x + y)"
    7.52 +proof -
    7.53 +  let ?s = "\<lambda> n. if even n then 0 else f ((n - 1) div 2)"
    7.54 +  { fix B T E have "(if B then (0 :: real) else E) + (if B then T else 0) = (if B then T else E)"
    7.55 +      by (cases B) auto } note if_sum = this
    7.56 +  have g_sums: "(\<lambda> n. if even n then 0 else g ((n - 1) div 2)) sums x" using sums_if'[OF `g sums x`] .
    7.57 +  { 
    7.58 +    have "?s 0 = 0" by auto
    7.59 +    have Suc_m1: "\<And> n. Suc n - 1 = n" by auto
    7.60 +    { fix B T E have "(if \<not> B then T else E) = (if B then E else T)" by auto } note if_eq = this
    7.61 +
    7.62 +    have "?s sums y" using sums_if'[OF `f sums y`] .
    7.63 +    from this[unfolded sums_def, THEN LIMSEQ_Suc] 
    7.64 +    have "(\<lambda> n. if even n then f (n div 2) else 0) sums y"
    7.65 +      unfolding sums_def setsum_shift_lb_Suc0_0_upt[where f="?s", OF `?s 0 = 0`, symmetric]
    7.66 +                image_Suc_atLeastLessThan[symmetric] setsum_reindex[OF inj_Suc, unfolded comp_def]
    7.67 +                even_nat_Suc Suc_m1 if_eq .
    7.68 +  } from sums_add[OF g_sums this]
    7.69 +  show ?thesis unfolding if_sum .
    7.70 +qed
    7.71 +
    7.72 +subsection {* Alternating series test / Leibniz formula *}
    7.73 +
    7.74 +lemma sums_alternating_upper_lower:
    7.75 +  fixes a :: "nat \<Rightarrow> real"
    7.76 +  assumes mono: "\<And>n. a (Suc n) \<le> a n" and a_pos: "\<And>n. 0 \<le> a n" and "a ----> 0"
    7.77 +  shows "\<exists>l. ((\<forall>n. (\<Sum>i=0..<2*n. -1^i*a i) \<le> l) \<and> (\<lambda> n. \<Sum>i=0..<2*n. -1^i*a i) ----> l) \<and> 
    7.78 +             ((\<forall>n. l \<le> (\<Sum>i=0..<2*n + 1. -1^i*a i)) \<and> (\<lambda> n. \<Sum>i=0..<2*n + 1. -1^i*a i) ----> l)"
    7.79 +  (is "\<exists>l. ((\<forall>n. ?f n \<le> l) \<and> _) \<and> ((\<forall>n. l \<le> ?g n) \<and> _)")
    7.80 +proof -
    7.81 +  have fg_diff: "\<And>n. ?f n - ?g n = - a (2 * n)" by auto
    7.82 +
    7.83 +  have "\<forall> n. ?f n \<le> ?f (Suc n)"
    7.84 +  proof fix n show "?f n \<le> ?f (Suc n)" using mono[of "2*n"] by auto qed
    7.85 +  moreover
    7.86 +  have "\<forall> n. ?g (Suc n) \<le> ?g n"
    7.87 +  proof fix n show "?g (Suc n) \<le> ?g n" using mono[of "Suc (2*n)"] by auto qed
    7.88 +  moreover
    7.89 +  have "\<forall> n. ?f n \<le> ?g n" 
    7.90 +  proof fix n show "?f n \<le> ?g n" using fg_diff a_pos by auto qed
    7.91 +  moreover
    7.92 +  have "(\<lambda> n. ?f n - ?g n) ----> 0" unfolding fg_diff
    7.93 +  proof (rule LIMSEQ_I)
    7.94 +    fix r :: real assume "0 < r"
    7.95 +    with `a ----> 0`[THEN LIMSEQ_D] 
    7.96 +    obtain N where "\<And> n. n \<ge> N \<Longrightarrow> norm (a n - 0) < r" by auto
    7.97 +    hence "\<forall> n \<ge> N. norm (- a (2 * n) - 0) < r" by auto
    7.98 +    thus "\<exists> N. \<forall> n \<ge> N. norm (- a (2 * n) - 0) < r" by auto
    7.99 +  qed
   7.100 +  ultimately
   7.101 +  show ?thesis by (rule lemma_nest_unique)
   7.102 +qed 
   7.103 +
   7.104 +lemma summable_Leibniz': fixes a :: "nat \<Rightarrow> real"
   7.105 +  assumes a_zero: "a ----> 0" and a_pos: "\<And> n. 0 \<le> a n"
   7.106 +  and a_monotone: "\<And> n. a (Suc n) \<le> a n"
   7.107 +  shows summable: "summable (\<lambda> n. (-1)^n * a n)"
   7.108 +  and "\<And>n. (\<Sum>i=0..<2*n. (-1)^i*a i) \<le> (\<Sum>i. (-1)^i*a i)"
   7.109 +  and "(\<lambda>n. \<Sum>i=0..<2*n. (-1)^i*a i) ----> (\<Sum>i. (-1)^i*a i)"
   7.110 +  and "\<And>n. (\<Sum>i. (-1)^i*a i) \<le> (\<Sum>i=0..<2*n+1. (-1)^i*a i)"
   7.111 +  and "(\<lambda>n. \<Sum>i=0..<2*n+1. (-1)^i*a i) ----> (\<Sum>i. (-1)^i*a i)"
   7.112 +proof -
   7.113 +  let "?S n" = "(-1)^n * a n"
   7.114 +  let "?P n" = "\<Sum>i=0..<n. ?S i"
   7.115 +  let "?f n" = "?P (2 * n)"
   7.116 +  let "?g n" = "?P (2 * n + 1)"
   7.117 +  obtain l :: real where below_l: "\<forall> n. ?f n \<le> l" and "?f ----> l" and above_l: "\<forall> n. l \<le> ?g n" and "?g ----> l"
   7.118 +    using sums_alternating_upper_lower[OF a_monotone a_pos a_zero] by blast
   7.119 +  
   7.120 +  let ?Sa = "\<lambda> m. \<Sum> n = 0..<m. ?S n"
   7.121 +  have "?Sa ----> l"
   7.122 +  proof (rule LIMSEQ_I)
   7.123 +    fix r :: real assume "0 < r"
   7.124 +
   7.125 +    with `?f ----> l`[THEN LIMSEQ_D] 
   7.126 +    obtain f_no where f: "\<And> n. n \<ge> f_no \<Longrightarrow> norm (?f n - l) < r" by auto
   7.127 +
   7.128 +    from `0 < r` `?g ----> l`[THEN LIMSEQ_D] 
   7.129 +    obtain g_no where g: "\<And> n. n \<ge> g_no \<Longrightarrow> norm (?g n - l) < r" by auto
   7.130 +
   7.131 +    { fix n :: nat
   7.132 +      assume "n \<ge> (max (2 * f_no) (2 * g_no))" hence "n \<ge> 2 * f_no" and "n \<ge> 2 * g_no" by auto
   7.133 +      have "norm (?Sa n - l) < r"
   7.134 +      proof (cases "even n")
   7.135 +	case True from even_nat_div_two_times_two[OF this]
   7.136 +	have n_eq: "2 * (n div 2) = n" unfolding numeral_2_eq_2[symmetric] by auto
   7.137 +	with `n \<ge> 2 * f_no` have "n div 2 \<ge> f_no" by auto
   7.138 +	from f[OF this]
   7.139 +	show ?thesis unfolding n_eq atLeastLessThanSuc_atLeastAtMost .
   7.140 +      next
   7.141 +	case False hence "even (n - 1)" using even_num_iff odd_pos by auto 
   7.142 +	from even_nat_div_two_times_two[OF this]
   7.143 +	have n_eq: "2 * ((n - 1) div 2) = n - 1" unfolding numeral_2_eq_2[symmetric] by auto
   7.144 +	hence range_eq: "n - 1 + 1 = n" using odd_pos[OF False] by auto
   7.145 +
   7.146 +	from n_eq `n \<ge> 2 * g_no` have "(n - 1) div 2 \<ge> g_no" by auto
   7.147 +	from g[OF this]
   7.148 +	show ?thesis unfolding n_eq atLeastLessThanSuc_atLeastAtMost range_eq .
   7.149 +      qed
   7.150 +    }
   7.151 +    thus "\<exists> no. \<forall> n \<ge> no. norm (?Sa n - l) < r" by blast
   7.152 +  qed
   7.153 +  hence sums_l: "(\<lambda>i. (-1)^i * a i) sums l" unfolding sums_def atLeastLessThanSuc_atLeastAtMost[symmetric] .
   7.154 +  thus "summable ?S" using summable_def by auto
   7.155 +
   7.156 +  have "l = suminf ?S" using sums_unique[OF sums_l] .
   7.157 +
   7.158 +  { fix n show "suminf ?S \<le> ?g n" unfolding sums_unique[OF sums_l, symmetric] using above_l by auto }
   7.159 +  { fix n show "?f n \<le> suminf ?S" unfolding sums_unique[OF sums_l, symmetric] using below_l by auto }
   7.160 +  show "?g ----> suminf ?S" using `?g ----> l` `l = suminf ?S` by auto
   7.161 +  show "?f ----> suminf ?S" using `?f ----> l` `l = suminf ?S` by auto
   7.162 +qed
   7.163 +
   7.164 +theorem summable_Leibniz: fixes a :: "nat \<Rightarrow> real"
   7.165 +  assumes a_zero: "a ----> 0" and "monoseq a"
   7.166 +  shows "summable (\<lambda> n. (-1)^n * a n)" (is "?summable")
   7.167 +  and "0 < a 0 \<longrightarrow> (\<forall>n. (\<Sum>i. -1^i*a i) \<in> { \<Sum>i=0..<2*n. -1^i * a i .. \<Sum>i=0..<2*n+1. -1^i * a i})" (is "?pos")
   7.168 +  and "a 0 < 0 \<longrightarrow> (\<forall>n. (\<Sum>i. -1^i*a i) \<in> { \<Sum>i=0..<2*n+1. -1^i * a i .. \<Sum>i=0..<2*n. -1^i * a i})" (is "?neg")
   7.169 +  and "(\<lambda>n. \<Sum>i=0..<2*n. -1^i*a i) ----> (\<Sum>i. -1^i*a i)" (is "?f")
   7.170 +  and "(\<lambda>n. \<Sum>i=0..<2*n+1. -1^i*a i) ----> (\<Sum>i. -1^i*a i)" (is "?g")
   7.171 +proof -
   7.172 +  have "?summable \<and> ?pos \<and> ?neg \<and> ?f \<and> ?g"
   7.173 +  proof (cases "(\<forall> n. 0 \<le> a n) \<and> (\<forall>m. \<forall>n\<ge>m. a n \<le> a m)")
   7.174 +    case True
   7.175 +    hence ord: "\<And>n m. m \<le> n \<Longrightarrow> a n \<le> a m" and ge0: "\<And> n. 0 \<le> a n" by auto
   7.176 +    { fix n have "a (Suc n) \<le> a n" using ord[where n="Suc n" and m=n] by auto }
   7.177 +    note leibniz = summable_Leibniz'[OF `a ----> 0` ge0] and mono = this
   7.178 +    from leibniz[OF mono]
   7.179 +    show ?thesis using `0 \<le> a 0` by auto
   7.180 +  next
   7.181 +    let ?a = "\<lambda> n. - a n"
   7.182 +    case False
   7.183 +    with monoseq_le[OF `monoseq a` `a ----> 0`]
   7.184 +    have "(\<forall> n. a n \<le> 0) \<and> (\<forall>m. \<forall>n\<ge>m. a m \<le> a n)" by auto
   7.185 +    hence ord: "\<And>n m. m \<le> n \<Longrightarrow> ?a n \<le> ?a m" and ge0: "\<And> n. 0 \<le> ?a n" by auto
   7.186 +    { fix n have "?a (Suc n) \<le> ?a n" using ord[where n="Suc n" and m=n] by auto }
   7.187 +    note monotone = this
   7.188 +    note leibniz = summable_Leibniz'[OF _ ge0, of "\<lambda>x. x", OF LIMSEQ_minus[OF `a ----> 0`, unfolded minus_zero] monotone]
   7.189 +    have "summable (\<lambda> n. (-1)^n * ?a n)" using leibniz(1) by auto
   7.190 +    then obtain l where "(\<lambda> n. (-1)^n * ?a n) sums l" unfolding summable_def by auto
   7.191 +    from this[THEN sums_minus]
   7.192 +    have "(\<lambda> n. (-1)^n * a n) sums -l" by auto
   7.193 +    hence ?summable unfolding summable_def by auto
   7.194 +    moreover
   7.195 +    have "\<And> a b :: real. \<bar> - a - - b \<bar> = \<bar>a - b\<bar>" unfolding minus_diff_minus by auto
   7.196 +    
   7.197 +    from suminf_minus[OF leibniz(1), unfolded mult_minus_right minus_minus]
   7.198 +    have move_minus: "(\<Sum>n. - (-1 ^ n * a n)) = - (\<Sum>n. -1 ^ n * a n)" by auto
   7.199 +
   7.200 +    have ?pos using `0 \<le> ?a 0` by auto
   7.201 +    moreover have ?neg using leibniz(2,4) unfolding mult_minus_right setsum_negf move_minus neg_le_iff_le by auto
   7.202 +    moreover have ?f and ?g using leibniz(3,5)[unfolded mult_minus_right setsum_negf move_minus, THEN LIMSEQ_minus_cancel] by auto
   7.203 +    ultimately show ?thesis by auto
   7.204 +  qed
   7.205 +  from this[THEN conjunct1] this[THEN conjunct2, THEN conjunct1] this[THEN conjunct2, THEN conjunct2, THEN conjunct1] this[THEN conjunct2, THEN conjunct2, THEN conjunct2, THEN conjunct1]
   7.206 +       this[THEN conjunct2, THEN conjunct2, THEN conjunct2, THEN conjunct2]
   7.207 +  show ?summable and ?pos and ?neg and ?f and ?g .
   7.208 +qed
   7.209  
   7.210  subsection {* Term-by-Term Differentiability of Power Series *}
   7.211  
   7.212 @@ -432,6 +634,188 @@
   7.213  lemma inv_real_of_nat_fact_ge_zero [simp]: "0 \<le> inverse (real (fact n))"
   7.214  by (auto intro: order_less_imp_le)
   7.215  
   7.216 +subsection {* Derivability of power series *}
   7.217 +
   7.218 +lemma DERIV_series': fixes f :: "real \<Rightarrow> nat \<Rightarrow> real"
   7.219 +  assumes DERIV_f: "\<And> n. DERIV (\<lambda> x. f x n) x0 :> (f' x0 n)"
   7.220 +  and allf_summable: "\<And> x. x \<in> {a <..< b} \<Longrightarrow> summable (f x)" and x0_in_I: "x0 \<in> {a <..< b}"
   7.221 +  and "summable (f' x0)"
   7.222 +  and "summable L" and L_def: "\<And> n x y. \<lbrakk> x \<in> { a <..< b} ; y \<in> { a <..< b} \<rbrakk> \<Longrightarrow> \<bar> f x n - f y n \<bar> \<le> L n * \<bar> x - y \<bar>"
   7.223 +  shows "DERIV (\<lambda> x. suminf (f x)) x0 :> (suminf (f' x0))"
   7.224 +  unfolding deriv_def
   7.225 +proof (rule LIM_I)
   7.226 +  fix r :: real assume "0 < r" hence "0 < r/3" by auto
   7.227 +
   7.228 +  obtain N_L where N_L: "\<And> n. N_L \<le> n \<Longrightarrow> \<bar> \<Sum> i. L (i + n) \<bar> < r/3" 
   7.229 +    using suminf_exist_split[OF `0 < r/3` `summable L`] by auto
   7.230 +
   7.231 +  obtain N_f' where N_f': "\<And> n. N_f' \<le> n \<Longrightarrow> \<bar> \<Sum> i. f' x0 (i + n) \<bar> < r/3" 
   7.232 +    using suminf_exist_split[OF `0 < r/3` `summable (f' x0)`] by auto
   7.233 +
   7.234 +  let ?N = "Suc (max N_L N_f')"
   7.235 +  have "\<bar> \<Sum> i. f' x0 (i + ?N) \<bar> < r/3" (is "?f'_part < r/3") and
   7.236 +    L_estimate: "\<bar> \<Sum> i. L (i + ?N) \<bar> < r/3" using N_L[of "?N"] and N_f' [of "?N"] by auto
   7.237 +
   7.238 +  let "?diff i x" = "(f (x0 + x) i - f x0 i) / x"
   7.239 +
   7.240 +  let ?r = "r / (3 * real ?N)"
   7.241 +  have "0 < 3 * real ?N" by auto
   7.242 +  from divide_pos_pos[OF `0 < r` this]
   7.243 +  have "0 < ?r" .
   7.244 +
   7.245 +  let "?s 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)"
   7.246 +  def S' \<equiv> "Min (?s ` { 0 ..< ?N })"
   7.247 +
   7.248 +  have "0 < S'" unfolding S'_def
   7.249 +  proof (rule iffD2[OF Min_gr_iff])
   7.250 +    show "\<forall> x \<in> (?s ` { 0 ..< ?N }). 0 < x"
   7.251 +    proof (rule ballI)
   7.252 +      fix x assume "x \<in> ?s ` {0..<?N}"
   7.253 +      then obtain n where "x = ?s n" and "n \<in> {0..<?N}" using image_iff[THEN iffD1] by blast
   7.254 +      from DERIV_D[OF DERIV_f[where n=n], THEN LIM_D, OF `0 < ?r`, unfolded real_norm_def] 
   7.255 +      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)" by auto
   7.256 +      have "0 < ?s n" by (rule someI2[where a=s], auto simp add: s_bound)
   7.257 +      thus "0 < x" unfolding `x = ?s n` .
   7.258 +    qed
   7.259 +  qed auto
   7.260 +
   7.261 +  def S \<equiv> "min (min (x0 - a) (b - x0)) S'"
   7.262 +  hence "0 < S" and S_a: "S \<le> x0 - a" and S_b: "S \<le> b - x0" and "S \<le> S'" using x0_in_I and `0 < S'`
   7.263 +    by auto
   7.264 +
   7.265 +  { fix x assume "x \<noteq> 0" and "\<bar> x \<bar> < S"
   7.266 +    hence x_in_I: "x0 + x \<in> { a <..< b }" using S_a S_b by auto
   7.267 +    
   7.268 +    note diff_smbl = summable_diff[OF allf_summable[OF x_in_I] allf_summable[OF x0_in_I]]
   7.269 +    note div_smbl = summable_divide[OF diff_smbl]
   7.270 +    note all_smbl = summable_diff[OF div_smbl `summable (f' x0)`]
   7.271 +    note ign = summable_ignore_initial_segment[where k="?N"]
   7.272 +    note diff_shft_smbl = summable_diff[OF ign[OF allf_summable[OF x_in_I]] ign[OF allf_summable[OF x0_in_I]]]
   7.273 +    note div_shft_smbl = summable_divide[OF diff_shft_smbl]
   7.274 +    note all_shft_smbl = summable_diff[OF div_smbl ign[OF `summable (f' x0)`]]
   7.275 +
   7.276 +    { fix n
   7.277 +      have "\<bar> ?diff (n + ?N) x \<bar> \<le> L (n + ?N) * \<bar> (x0 + x) - x0 \<bar> / \<bar> x \<bar>" 
   7.278 +	using divide_right_mono[OF L_def[OF x_in_I x0_in_I] abs_ge_zero] unfolding abs_divide .
   7.279 +      hence "\<bar> ( \<bar> ?diff (n + ?N) x \<bar>) \<bar> \<le> L (n + ?N)" using `x \<noteq> 0` by auto
   7.280 +    } note L_ge = summable_le2[OF allI[OF this] ign[OF `summable L`]]
   7.281 +    from order_trans[OF summable_rabs[OF conjunct1[OF L_ge]] L_ge[THEN conjunct2]]
   7.282 +    have "\<bar> \<Sum> i. ?diff (i + ?N) x \<bar> \<le> (\<Sum> i. L (i + ?N))" .
   7.283 +    hence "\<bar> \<Sum> i. ?diff (i + ?N) x \<bar> \<le> r / 3" (is "?L_part \<le> r/3") using L_estimate by auto
   7.284 +
   7.285 +    have "\<bar>\<Sum>n \<in> { 0 ..< ?N}. ?diff n x - f' x0 n \<bar> \<le> (\<Sum>n \<in> { 0 ..< ?N}. \<bar>?diff n x - f' x0 n \<bar>)" ..
   7.286 +    also have "\<dots> < (\<Sum>n \<in> { 0 ..< ?N}. ?r)"
   7.287 +    proof (rule setsum_strict_mono)
   7.288 +      fix n assume "n \<in> { 0 ..< ?N}"
   7.289 +      have "\<bar> x \<bar> < S" using `\<bar> x \<bar> < S` .
   7.290 +      also have "S \<le> S'" using `S \<le> S'` .
   7.291 +      also have "S' \<le> ?s n" unfolding S'_def 
   7.292 +      proof (rule Min_le_iff[THEN iffD2])
   7.293 +	have "?s n \<in> (?s ` {0..<?N}) \<and> ?s n \<le> ?s n" using `n \<in> { 0 ..< ?N}` by auto
   7.294 +	thus "\<exists> a \<in> (?s ` {0..<?N}). a \<le> ?s n" by blast
   7.295 +      qed auto
   7.296 +      finally have "\<bar> x \<bar> < ?s n" .
   7.297 +
   7.298 +      from DERIV_D[OF DERIV_f[where n=n], THEN LIM_D, OF `0 < ?r`, unfolded real_norm_def diff_0_right, unfolded some_eq_ex[symmetric], THEN conjunct2]
   7.299 +      have "\<forall>x. x \<noteq> 0 \<and> \<bar>x\<bar> < ?s n \<longrightarrow> \<bar>?diff n x - f' x0 n\<bar> < ?r" .
   7.300 +      with `x \<noteq> 0` and `\<bar>x\<bar> < ?s n`
   7.301 +      show "\<bar>?diff n x - f' x0 n\<bar> < ?r" by blast
   7.302 +    qed auto
   7.303 +    also have "\<dots> = of_nat (card {0 ..< ?N}) * ?r" by (rule setsum_constant)
   7.304 +    also have "\<dots> = real ?N * ?r" unfolding real_eq_of_nat by auto
   7.305 +    also have "\<dots> = r/3" by auto
   7.306 +    finally have "\<bar>\<Sum>n \<in> { 0 ..< ?N}. ?diff n x - f' x0 n \<bar> < r / 3" (is "?diff_part < r / 3") .
   7.307 +
   7.308 +    from suminf_diff[OF allf_summable[OF x_in_I] allf_summable[OF x0_in_I]]
   7.309 +    have "\<bar> (suminf (f (x0 + x)) - (suminf (f x0))) / x - suminf (f' x0) \<bar> = 
   7.310 +                    \<bar> \<Sum>n. ?diff n x - f' x0 n \<bar>" unfolding suminf_diff[OF div_smbl `summable (f' x0)`, symmetric] using suminf_divide[OF diff_smbl, symmetric] by auto
   7.311 +    also have "\<dots> \<le> ?diff_part + \<bar> (\<Sum>n. ?diff (n + ?N) x) - (\<Sum> n. f' x0 (n + ?N)) \<bar>" unfolding suminf_split_initial_segment[OF all_smbl, where k="?N"] unfolding suminf_diff[OF div_shft_smbl ign[OF `summable (f' x0)`]] by (rule abs_triangle_ineq)
   7.312 +    also have "\<dots> \<le> ?diff_part + ?L_part + ?f'_part" using abs_triangle_ineq4 by auto
   7.313 +    also have "\<dots> < r /3 + r/3 + r/3" 
   7.314 +      using `?diff_part < r/3` `?L_part \<le> r/3` and `?f'_part < r/3` by auto
   7.315 +    finally have "\<bar> (suminf (f (x0 + x)) - (suminf (f x0))) / x - suminf (f' x0) \<bar> < r"
   7.316 +      by auto
   7.317 +  } thus "\<exists> s > 0. \<forall> x. x \<noteq> 0 \<and> norm (x - 0) < s \<longrightarrow> 
   7.318 +      norm (((\<Sum>n. f (x0 + x) n) - (\<Sum>n. f x0 n)) / x - (\<Sum>n. f' x0 n)) < r" using `0 < S`
   7.319 +    unfolding real_norm_def diff_0_right by blast
   7.320 +qed
   7.321 +
   7.322 +lemma DERIV_power_series': fixes f :: "nat \<Rightarrow> real"
   7.323 +  assumes converges: "\<And> x. x \<in> {-R <..< R} \<Longrightarrow> summable (\<lambda> n. f n * real (Suc n) * x^n)"
   7.324 +  and x0_in_I: "x0 \<in> {-R <..< R}" and "0 < R"
   7.325 +  shows "DERIV (\<lambda> x. (\<Sum> n. f n * x^(Suc n))) x0 :> (\<Sum> n. f n * real (Suc n) * x0^n)"
   7.326 +  (is "DERIV (\<lambda> x. (suminf (?f x))) x0 :> (suminf (?f' x0))")
   7.327 +proof -
   7.328 +  { fix R' assume "0 < R'" and "R' < R" and "-R' < x0" and "x0 < R'"
   7.329 +    hence "x0 \<in> {-R' <..< R'}" and "R' \<in> {-R <..< R}" and "x0 \<in> {-R <..< R}" by auto
   7.330 +    have "DERIV (\<lambda> x. (suminf (?f x))) x0 :> (suminf (?f' x0))"
   7.331 +    proof (rule DERIV_series')
   7.332 +      show "summable (\<lambda> n. \<bar>f n * real (Suc n) * R'^n\<bar>)"
   7.333 +      proof -
   7.334 +	have "(R' + R) / 2 < R" and "0 < (R' + R) / 2" using `0 < R'` `0 < R` `R' < R` by auto
   7.335 +	hence in_Rball: "(R' + R) / 2 \<in> {-R <..< R}" using `R' < R` by auto
   7.336 +	have "norm R' < norm ((R' + R) / 2)" using `0 < R'` `0 < R` `R' < R` by auto
   7.337 +	from powser_insidea[OF converges[OF in_Rball] this] show ?thesis by auto
   7.338 +      qed
   7.339 +      { fix n x y assume "x \<in> {-R' <..< R'}" and "y \<in> {-R' <..< R'}"
   7.340 +	show "\<bar>?f x n - ?f y n\<bar> \<le> \<bar>f n * real (Suc n) * R'^n\<bar> * \<bar>x-y\<bar>"
   7.341 +	proof -
   7.342 +	  have "\<bar>f n * x ^ (Suc n) - f n * y ^ (Suc n)\<bar> = (\<bar>f n\<bar> * \<bar>x-y\<bar>) * \<bar>\<Sum>p = 0..<Suc n. x ^ p * y ^ (n - p)\<bar>" 
   7.343 +	    unfolding right_diff_distrib[symmetric] lemma_realpow_diff_sumr2 abs_mult by auto
   7.344 +	  also have "\<dots> \<le> (\<bar>f n\<bar> * \<bar>x-y\<bar>) * (\<bar>real (Suc n)\<bar> * \<bar>R' ^ n\<bar>)" 
   7.345 +	  proof (rule mult_left_mono)
   7.346 +	    have "\<bar>\<Sum>p = 0..<Suc n. x ^ p * y ^ (n - p)\<bar> \<le> (\<Sum>p = 0..<Suc n. \<bar>x ^ p * y ^ (n - p)\<bar>)" by (rule setsum_abs)
   7.347 +	    also have "\<dots> \<le> (\<Sum>p = 0..<Suc n. R' ^ n)"
   7.348 +	    proof (rule setsum_mono)
   7.349 +	      fix p assume "p \<in> {0..<Suc n}" hence "p \<le> n" by auto
   7.350 +	      { fix n fix x :: real assume "x \<in> {-R'<..<R'}"
   7.351 +		hence "\<bar>x\<bar> \<le> R'"  by auto
   7.352 +		hence "\<bar>x^n\<bar> \<le> R'^n" unfolding power_abs by (rule power_mono, auto)
   7.353 +	      } from mult_mono[OF this[OF `x \<in> {-R'<..<R'}`, of p] this[OF `y \<in> {-R'<..<R'}`, of "n-p"]] `0 < R'`
   7.354 +	      have "\<bar>x^p * y^(n-p)\<bar> \<le> R'^p * R'^(n-p)" unfolding abs_mult by auto
   7.355 +	      thus "\<bar>x^p * y^(n-p)\<bar> \<le> R'^n" unfolding power_add[symmetric] using `p \<le> n` by auto
   7.356 +	    qed
   7.357 +	    also have "\<dots> = real (Suc n) * R' ^ n" unfolding setsum_constant card_atLeastLessThan real_of_nat_def by auto
   7.358 +	    finally show "\<bar>\<Sum>p = 0..<Suc n. x ^ p * y ^ (n - p)\<bar> \<le> \<bar>real (Suc n)\<bar> * \<bar>R' ^ n\<bar>" unfolding abs_real_of_nat_cancel abs_of_nonneg[OF zero_le_power[OF less_imp_le[OF `0 < R'`]]] .
   7.359 +	    show "0 \<le> \<bar>f n\<bar> * \<bar>x - y\<bar>" unfolding abs_mult[symmetric] by auto
   7.360 +	  qed
   7.361 +	  also have "\<dots> = \<bar>f n * real (Suc n) * R' ^ n\<bar> * \<bar>x - y\<bar>" unfolding abs_mult real_mult_assoc[symmetric] by algebra
   7.362 +	  finally show ?thesis .
   7.363 +	qed }
   7.364 +      { fix n
   7.365 +	from DERIV_pow[of "Suc n" x0, THEN DERIV_cmult[where c="f n"]]
   7.366 +	show "DERIV (\<lambda> x. ?f x n) x0 :> (?f' x0 n)" unfolding real_mult_assoc by auto }
   7.367 +      { fix x assume "x \<in> {-R' <..< R'}" hence "R' \<in> {-R <..< R}" and "norm x < norm R'" using assms `R' < R` by auto
   7.368 +	have "summable (\<lambda> n. f n * x^n)"
   7.369 +	proof (rule summable_le2[THEN conjunct1, OF _ powser_insidea[OF converges[OF `R' \<in> {-R <..< R}`] `norm x < norm R'`]], rule allI)
   7.370 +	  fix n
   7.371 +	  have le: "\<bar>f n\<bar> * 1 \<le> \<bar>f n\<bar> * real (Suc n)" by (rule mult_left_mono, auto)
   7.372 +	  show "\<bar>f n * x ^ n\<bar> \<le> norm (f n * real (Suc n) * x ^ n)" unfolding real_norm_def abs_mult
   7.373 +	    by (rule mult_right_mono, auto simp add: le[unfolded mult_1_right])
   7.374 +	qed
   7.375 +	from this[THEN summable_mult2[where c=x], unfolded real_mult_assoc, unfolded real_mult_commute]
   7.376 +	show "summable (?f x)" by auto }
   7.377 +      show "summable (?f' x0)" using converges[OF `x0 \<in> {-R <..< R}`] .
   7.378 +      show "x0 \<in> {-R' <..< R'}" using `x0 \<in> {-R' <..< R'}` .
   7.379 +    qed
   7.380 +  } note for_subinterval = this
   7.381 +  let ?R = "(R + \<bar>x0\<bar>) / 2"
   7.382 +  have "\<bar>x0\<bar> < ?R" using assms by auto
   7.383 +  hence "- ?R < x0"
   7.384 +  proof (cases "x0 < 0")
   7.385 +    case True
   7.386 +    hence "- x0 < ?R" using `\<bar>x0\<bar> < ?R` by auto
   7.387 +    thus ?thesis unfolding neg_less_iff_less[symmetric, of "- x0"] by auto
   7.388 +  next
   7.389 +    case False
   7.390 +    have "- ?R < 0" using assms by auto
   7.391 +    also have "\<dots> \<le> x0" using False by auto 
   7.392 +    finally show ?thesis .
   7.393 +  qed
   7.394 +  hence "0 < ?R" "?R < R" "- ?R < x0" and "x0 < ?R" using assms by auto
   7.395 +  from for_subinterval[OF this]
   7.396 +  show ?thesis .
   7.397 +qed
   7.398  
   7.399  subsection {* Exponential Function *}
   7.400  
   7.401 @@ -830,6 +1214,37 @@
   7.402  apply (simp_all add: abs_if isCont_ln)
   7.403  done
   7.404  
   7.405 +lemma ln_series: assumes "0 < x" and "x < 2"
   7.406 +  shows "ln x = (\<Sum> n. (-1)^n * (1 / real (n + 1)) * (x - 1)^(Suc n))" (is "ln x = suminf (?f (x - 1))")
   7.407 +proof -
   7.408 +  let "?f' x n" = "(-1)^n * (x - 1)^n"
   7.409 +
   7.410 +  have "ln x - suminf (?f (x - 1)) = ln 1 - suminf (?f (1 - 1))"
   7.411 +  proof (rule DERIV_isconst3[where x=x])
   7.412 +    fix x :: real assume "x \<in> {0 <..< 2}" hence "0 < x" and "x < 2" by auto
   7.413 +    have "norm (1 - x) < 1" using `0 < x` and `x < 2` by auto
   7.414 +    have "1 / x = 1 / (1 - (1 - x))" by auto
   7.415 +    also have "\<dots> = (\<Sum> n. (1 - x)^n)" using geometric_sums[OF `norm (1 - x) < 1`] by (rule sums_unique)
   7.416 +    also have "\<dots> = suminf (?f' x)" unfolding power_mult_distrib[symmetric] by (rule arg_cong[where f=suminf], rule arg_cong[where f="op ^"], auto)
   7.417 +    finally have "DERIV ln x :> suminf (?f' x)" using DERIV_ln[OF `0 < x`] unfolding real_divide_def by auto
   7.418 +    moreover
   7.419 +    have repos: "\<And> h x :: real. h - 1 + x = h + x - 1" by auto
   7.420 +    have "DERIV (\<lambda>x. suminf (?f x)) (x - 1) :> (\<Sum>n. (-1)^n * (1 / real (n + 1)) * real (Suc n) * (x - 1) ^ n)"
   7.421 +    proof (rule DERIV_power_series')
   7.422 +      show "x - 1 \<in> {- 1<..<1}" and "(0 :: real) < 1" using `0 < x` `x < 2` by auto
   7.423 +      { fix x :: real assume "x \<in> {- 1<..<1}" hence "norm (-x) < 1" by auto
   7.424 +	show "summable (\<lambda>n. -1 ^ n * (1 / real (n + 1)) * real (Suc n) * x ^ n)"
   7.425 +	  by (auto simp del: power_mult_distrib simp add: power_mult_distrib[symmetric] summable_geometric[OF `norm (-x) < 1`])
   7.426 +      }
   7.427 +    qed
   7.428 +    hence "DERIV (\<lambda>x. suminf (?f x)) (x - 1) :> suminf (?f' x)" by auto
   7.429 +    hence "DERIV (\<lambda>x. suminf (?f (x - 1))) x :> suminf (?f' x)" unfolding DERIV_iff repos .
   7.430 +    ultimately have "DERIV (\<lambda>x. ln x - suminf (?f (x - 1))) x :> (suminf (?f' x) - suminf (?f' x))"
   7.431 +      by (rule DERIV_diff)
   7.432 +    thus "DERIV (\<lambda>x. ln x - suminf (?f (x - 1))) x :> 0" by auto
   7.433 +  qed (auto simp add: assms)
   7.434 +  thus ?thesis by (auto simp add: suminf_zero)
   7.435 +qed
   7.436  
   7.437  subsection {* Sine and Cosine *}
   7.438  
   7.439 @@ -1378,6 +1793,12 @@
   7.440  lemma minus_pi_half_less_zero: "-(pi/2) < 0"
   7.441  by simp
   7.442  
   7.443 +lemma m2pi_less_pi: "- (2 * pi) < pi"
   7.444 +proof -
   7.445 +  have "- (2 * pi) < 0" and "0 < pi" by auto
   7.446 +  from order_less_trans[OF this] show ?thesis .
   7.447 +qed
   7.448 +
   7.449  lemma sin_pi_half [simp]: "sin(pi/2) = 1"
   7.450  apply (cut_tac x = "pi/2" in sin_cos_squared_add2)
   7.451  apply (cut_tac sin_gt_zero [OF pi_half_gt_zero pi_half_less_two])
   7.452 @@ -1487,6 +1908,24 @@
   7.453  apply (auto intro!: cos_gt_zero_pi simp del: sin_cos_eq [symmetric])
   7.454  done
   7.455  
   7.456 +
   7.457 +lemma pi_ge_two: "2 \<le> pi"
   7.458 +proof (rule ccontr)
   7.459 +  assume "\<not> 2 \<le> pi" hence "pi < 2" by auto
   7.460 +  have "\<exists>y > pi. y < 2 \<and> y < 2 * pi"
   7.461 +  proof (cases "2 < 2 * pi")
   7.462 +    case True with dense[OF `pi < 2`] show ?thesis by auto
   7.463 +  next
   7.464 +    case False have "pi < 2 * pi" by auto
   7.465 +    from dense[OF this] and False show ?thesis by auto
   7.466 +  qed
   7.467 +  then obtain y where "pi < y" and "y < 2" and "y < 2 * pi" by blast
   7.468 +  hence "0 < sin y" using sin_gt_zero by auto
   7.469 +  moreover 
   7.470 +  have "sin y < 0" using sin_gt_zero_pi[of "y - pi"] `pi < y` and `y < 2 * pi` sin_periodic_pi[of "y - pi"] by auto
   7.471 +  ultimately show False by auto
   7.472 +qed
   7.473 +
   7.474  lemma sin_ge_zero: "[| 0 \<le> x; x \<le> pi |] ==> 0 \<le> sin x"
   7.475  by (auto simp add: order_le_less sin_gt_zero_pi)
   7.476  
   7.477 @@ -1586,6 +2025,48 @@
   7.478  apply (auto simp add: even_mult_two_ex)
   7.479  done
   7.480  
   7.481 +lemma cos_monotone_0_pi: assumes "0 \<le> y" and "y < x" and "x \<le> pi"
   7.482 +  shows "cos x < cos y"
   7.483 +proof -
   7.484 +  have "- (x - y) < 0" by (auto!)
   7.485 +
   7.486 +  from MVT2[OF `y < x` DERIV_cos[THEN impI, THEN allI]]
   7.487 +  obtain z where "y < z" and "z < x" and cos_diff: "cos x - cos y = (x - y) * - sin z" by auto
   7.488 +  hence "0 < z" and "z < pi" by (auto!)
   7.489 +  hence "0 < sin z" using sin_gt_zero_pi by auto
   7.490 +  hence "cos x - cos y < 0" unfolding cos_diff minus_mult_commute[symmetric] using `- (x - y) < 0` by (rule mult_pos_neg2)
   7.491 +  thus ?thesis by auto
   7.492 +qed
   7.493 +
   7.494 +lemma cos_monotone_0_pi': assumes "0 \<le> y" and "y \<le> x" and "x \<le> pi" shows "cos x \<le> cos y"
   7.495 +proof (cases "y < x")
   7.496 +  case True show ?thesis using cos_monotone_0_pi[OF `0 \<le> y` True `x \<le> pi`] by auto
   7.497 +next
   7.498 +  case False hence "y = x" using `y \<le> x` by auto
   7.499 +  thus ?thesis by auto
   7.500 +qed
   7.501 +
   7.502 +lemma cos_monotone_minus_pi_0: assumes "-pi \<le> y" and "y < x" and "x \<le> 0"
   7.503 +  shows "cos y < cos x"
   7.504 +proof -
   7.505 +  have "0 \<le> -x" and "-x < -y" and "-y \<le> pi" by (auto!)
   7.506 +  from cos_monotone_0_pi[OF this]
   7.507 +  show ?thesis unfolding cos_minus .
   7.508 +qed
   7.509 +
   7.510 +lemma cos_monotone_minus_pi_0': assumes "-pi \<le> y" and "y \<le> x" and "x \<le> 0" shows "cos y \<le> cos x"
   7.511 +proof (cases "y < x")
   7.512 +  case True show ?thesis using cos_monotone_minus_pi_0[OF `-pi \<le> y` True `x \<le> 0`] by auto
   7.513 +next
   7.514 +  case False hence "y = x" using `y \<le> x` by auto
   7.515 +  thus ?thesis by auto
   7.516 +qed
   7.517 +
   7.518 +lemma sin_monotone_2pi': assumes "- (pi / 2) \<le> y" and "y \<le> x" and "x \<le> pi / 2" shows "sin y \<le> sin x"
   7.519 +proof -
   7.520 +  have "0 \<le> y + pi / 2" and "y + pi / 2 \<le> x + pi / 2" and "x + pi /2 \<le> pi" using pi_ge_two by (auto!)
   7.521 +  from cos_monotone_0_pi'[OF this] show ?thesis unfolding minus_sin_cos_eq[symmetric] by auto
   7.522 +qed
   7.523  
   7.524  subsection {* Tangent *}
   7.525  
   7.526 @@ -1653,6 +2134,22 @@
   7.527    thus ?thesis by simp
   7.528  qed
   7.529  
   7.530 +lemma tan_half: fixes x :: real assumes "- (pi / 2) < x" and "x < pi / 2"
   7.531 +  shows "tan x = sin (2 * x) / (cos (2 * x) + 1)"
   7.532 +proof -
   7.533 +  from cos_gt_zero_pi[OF `- (pi / 2) < x` `x < pi / 2`]
   7.534 +  have "cos x \<noteq> 0" by auto
   7.535 +
   7.536 +  have minus_cos_2x: "\<And>X. X - cos (2*x) = X - (cos x) ^ 2 + (sin x) ^ 2" unfolding cos_double by algebra
   7.537 +
   7.538 +  have "tan x = (tan x + tan x) / 2" by auto
   7.539 +  also have "\<dots> = sin (x + x) / (cos x * cos x) / 2" unfolding add_tan_eq[OF `cos x \<noteq> 0` `cos x \<noteq> 0`] ..
   7.540 +  also have "\<dots> = sin (2 * x) / ((cos x) ^ 2 + (cos x) ^ 2 + cos (2*x) - cos (2*x))" unfolding divide_divide_eq_left numeral_2_eq_2 by auto
   7.541 +  also have "\<dots> = sin (2 * x) / ((cos x) ^ 2 + cos (2*x) + (sin x)^2)" unfolding minus_cos_2x by auto
   7.542 +  also have "\<dots> = sin (2 * x) / (cos (2*x) + 1)" by auto
   7.543 +  finally show ?thesis .
   7.544 +qed
   7.545 +
   7.546  lemma lemma_DERIV_tan:
   7.547       "cos x \<noteq> 0 ==> DERIV (%x. sin(x)/cos(x)) x :> inverse((cos x)\<twosuperior>)"
   7.548  apply (rule lemma_DERIV_subst)
   7.549 @@ -1726,6 +2223,73 @@
   7.550  	    simp add: cos_gt_zero_pi [THEN less_imp_neq, THEN not_sym]) 
   7.551  done
   7.552  
   7.553 +lemma tan_monotone: assumes "- (pi / 2) < y" and "y < x" and "x < pi / 2"
   7.554 +  shows "tan y < tan x"
   7.555 +proof -
   7.556 +  have "\<forall> x'. y \<le> x' \<and> x' \<le> x \<longrightarrow> DERIV tan x' :> inverse (cos x'^2)"
   7.557 +  proof (rule allI, rule impI)
   7.558 +    fix x' :: real assume "y \<le> x' \<and> x' \<le> x"
   7.559 +    hence "-(pi/2) < x'" and "x' < pi/2" by (auto!)
   7.560 +    from cos_gt_zero_pi[OF this]
   7.561 +    have "cos x' \<noteq> 0" by auto
   7.562 +    thus "DERIV tan x' :> inverse (cos x'^2)" by (rule DERIV_tan)
   7.563 +  qed
   7.564 +  from MVT2[OF `y < x` this] 
   7.565 +  obtain z where "y < z" and "z < x" and tan_diff: "tan x - tan y = (x - y) * inverse ((cos z)\<twosuperior>)" by auto
   7.566 +  hence "- (pi / 2) < z" and "z < pi / 2" by (auto!)
   7.567 +  hence "0 < cos z" using cos_gt_zero_pi by auto
   7.568 +  hence inv_pos: "0 < inverse ((cos z)\<twosuperior>)" by auto
   7.569 +  have "0 < x - y" using `y < x` by auto
   7.570 +  from real_mult_order[OF this inv_pos]
   7.571 +  have "0 < tan x - tan y" unfolding tan_diff by auto
   7.572 +  thus ?thesis by auto
   7.573 +qed
   7.574 +
   7.575 +lemma tan_monotone': assumes "- (pi / 2) < y" and "y < pi / 2" and "- (pi / 2) < x" and "x < pi / 2"
   7.576 +  shows "(y < x) = (tan y < tan x)"
   7.577 +proof
   7.578 +  assume "y < x" thus "tan y < tan x" using tan_monotone and `- (pi / 2) < y` and `x < pi / 2` by auto
   7.579 +next
   7.580 +  assume "tan y < tan x"
   7.581 +  show "y < x"
   7.582 +  proof (rule ccontr)
   7.583 +    assume "\<not> y < x" hence "x \<le> y" by auto
   7.584 +    hence "tan x \<le> tan y" 
   7.585 +    proof (cases "x = y")
   7.586 +      case True thus ?thesis by auto
   7.587 +    next
   7.588 +      case False hence "x < y" using `x \<le> y` by auto
   7.589 +      from tan_monotone[OF `- (pi/2) < x` this `y < pi / 2`] show ?thesis by auto
   7.590 +    qed
   7.591 +    thus False using `tan y < tan x` by auto
   7.592 +  qed
   7.593 +qed
   7.594 +
   7.595 +lemma tan_inverse: "1 / (tan y) = tan (pi / 2 - y)" unfolding tan_def sin_cos_eq[of y] cos_sin_eq[of y] by auto
   7.596 +
   7.597 +lemma tan_periodic_pi[simp]: "tan (x + pi) = tan x" 
   7.598 +  by (simp add: tan_def)
   7.599 +
   7.600 +lemma tan_periodic_nat[simp]: fixes n :: nat shows "tan (x + real n * pi) = tan x" 
   7.601 +proof (induct n arbitrary: x)
   7.602 +  case (Suc n)
   7.603 +  have split_pi_off: "x + real (Suc n) * pi = (x + real n * pi) + pi" unfolding Suc_plus1 real_of_nat_add real_of_one real_add_mult_distrib by auto
   7.604 +  show ?case unfolding split_pi_off using Suc by auto
   7.605 +qed auto
   7.606 +
   7.607 +lemma tan_periodic_int[simp]: fixes i :: int shows "tan (x + real i * pi) = tan x"
   7.608 +proof (cases "0 \<le> i")
   7.609 +  case True hence i_nat: "real i = real (nat i)" by auto
   7.610 +  show ?thesis unfolding i_nat by auto
   7.611 +next
   7.612 +  case False hence i_nat: "real i = - real (nat (-i))" by auto
   7.613 +  have "tan x = tan (x + real i * pi - real i * pi)" by auto
   7.614 +  also have "\<dots> = tan (x + real i * pi)" unfolding i_nat mult_minus_left diff_minus_eq_add by (rule tan_periodic_nat)
   7.615 +  finally show ?thesis by auto
   7.616 +qed
   7.617 +
   7.618 +lemma tan_periodic_n[simp]: "tan (x + number_of n * pi) = tan x"
   7.619 +  using tan_periodic_int[of _ "number_of n" ] unfolding real_number_of .
   7.620  
   7.621  subsection {* Inverse Trigonometric Functions *}
   7.622  
   7.623 @@ -1968,7 +2532,6 @@
   7.624  apply (simp, simp, simp, rule isCont_arctan)
   7.625  done
   7.626  
   7.627 -
   7.628  subsection {* More Theorems about Sin and Cos *}
   7.629  
   7.630  lemma cos_45: "cos (pi / 4) = sqrt 2 / 2"
   7.631 @@ -2115,6 +2678,464 @@
   7.632  lemma cos_one_sin_zero: "cos x = 1 ==> sin x = 0"
   7.633  by (cut_tac x = x in sin_cos_squared_add3, auto)
   7.634  
   7.635 +subsection {* Machins formula *}
   7.636 +
   7.637 +lemma tan_total_pi4: assumes "\<bar>x\<bar> < 1"
   7.638 +  shows "\<exists> z. - (pi / 4) < z \<and> z < pi / 4 \<and> tan z = x"
   7.639 +proof -
   7.640 +  obtain z where "- (pi / 2) < z" and "z < pi / 2" and "tan z = x" using tan_total by blast
   7.641 +  have "tan (pi / 4) = 1" and "tan (- (pi / 4)) = - 1" using tan_45 tan_minus by auto
   7.642 +  have "z \<noteq> pi / 4" 
   7.643 +  proof (rule ccontr)
   7.644 +    assume "\<not> (z \<noteq> pi / 4)" hence "z = pi / 4" by auto
   7.645 +    have "tan z = 1" unfolding `z = pi / 4` `tan (pi / 4) = 1` ..
   7.646 +    thus False unfolding `tan z = x` using `\<bar>x\<bar> < 1` by auto
   7.647 +  qed
   7.648 +  have "z \<noteq> - (pi / 4)"
   7.649 +  proof (rule ccontr)
   7.650 +    assume "\<not> (z \<noteq> - (pi / 4))" hence "z = - (pi / 4)" by auto
   7.651 +    have "tan z = - 1" unfolding `z = - (pi / 4)` `tan (- (pi / 4)) = - 1` ..
   7.652 +    thus False unfolding `tan z = x` using `\<bar>x\<bar> < 1` by auto
   7.653 +  qed
   7.654 +
   7.655 +  have "z < pi / 4"
   7.656 +  proof (rule ccontr)
   7.657 +    assume "\<not> (z < pi / 4)" hence "pi / 4 < z" using `z \<noteq> pi / 4` by auto
   7.658 +    have "- (pi / 2) < pi / 4" using m2pi_less_pi by auto
   7.659 +    from tan_monotone[OF this `pi / 4 < z` `z < pi / 2`] 
   7.660 +    have "1 < x" unfolding `tan z = x` `tan (pi / 4) = 1` .
   7.661 +    thus False using `\<bar>x\<bar> < 1` by auto
   7.662 +  qed
   7.663 +  moreover 
   7.664 +  have "-(pi / 4) < z"
   7.665 +  proof (rule ccontr)
   7.666 +    assume "\<not> (-(pi / 4) < z)" hence "z < - (pi / 4)" using `z \<noteq> - (pi / 4)` by auto
   7.667 +    have "-(pi / 4) < pi / 2" using m2pi_less_pi by auto
   7.668 +    from tan_monotone[OF `-(pi / 2) < z` `z < -(pi / 4)` this]
   7.669 +    have "x < - 1" unfolding `tan z = x` `tan (-(pi / 4)) = - 1` .
   7.670 +    thus False using `\<bar>x\<bar> < 1` by auto
   7.671 +  qed
   7.672 +  ultimately show ?thesis using `tan z = x` by auto
   7.673 +qed
   7.674 +
   7.675 +lemma arctan_add: assumes "\<bar>x\<bar> \<le> 1" and "\<bar>y\<bar> < 1"
   7.676 +  shows "arctan x + arctan y = arctan ((x + y) / (1 - x * y))"
   7.677 +proof -
   7.678 +  obtain y' where "-(pi/4) < y'" and "y' < pi/4" and "tan y' = y" using tan_total_pi4[OF `\<bar>y\<bar> < 1`] by blast
   7.679 +
   7.680 +  have "pi / 4 < pi / 2" by auto
   7.681 +
   7.682 +  have "\<exists> x'. -(pi/4) \<le> x' \<and> x' \<le> pi/4 \<and> tan x' = x"
   7.683 +  proof (cases "\<bar>x\<bar> < 1")
   7.684 +    case True from tan_total_pi4[OF this] obtain x' where "-(pi/4) < x'" and "x' < pi/4" and "tan x' = x" by blast
   7.685 +    hence "-(pi/4) \<le> x'" and "x' \<le> pi/4" and "tan x' = x" by auto
   7.686 +    thus ?thesis by auto
   7.687 +  next
   7.688 +    case False
   7.689 +    show ?thesis
   7.690 +    proof (cases "x = 1")
   7.691 +      case True hence "tan (pi/4) = x" using tan_45 by auto
   7.692 +      moreover 
   7.693 +      have "- pi \<le> pi" unfolding minus_le_self_iff by auto
   7.694 +      hence "-(pi/4) \<le> pi/4" and "pi/4 \<le> pi/4" by auto
   7.695 +      ultimately show ?thesis by blast
   7.696 +    next
   7.697 +      case False hence "x = -1" using `\<not> \<bar>x\<bar> < 1` and `\<bar>x\<bar> \<le> 1` by auto
   7.698 +      hence "tan (-(pi/4)) = x" using tan_45 tan_minus by auto
   7.699 +      moreover 
   7.700 +      have "- pi \<le> pi" unfolding minus_le_self_iff by auto
   7.701 +      hence "-(pi/4) \<le> pi/4" and "-(pi/4) \<le> -(pi/4)" by auto
   7.702 +      ultimately show ?thesis by blast
   7.703 +    qed
   7.704 +  qed
   7.705 +  then obtain x' where "-(pi/4) \<le> x'" and "x' \<le> pi/4" and "tan x' = x" by blast
   7.706 +  hence "-(pi/2) < x'" and "x' < pi/2" using order_le_less_trans[OF `x' \<le> pi/4` `pi / 4 < pi / 2`] by auto
   7.707 +
   7.708 +  have "cos x' \<noteq> 0" using cos_gt_zero_pi[THEN less_imp_neq] and `-(pi/2) < x'` and `x' < pi/2` by auto
   7.709 +  moreover have "cos y' \<noteq> 0" using cos_gt_zero_pi[THEN less_imp_neq] and `-(pi/4) < y'` and `y' < pi/4` by auto
   7.710 +  ultimately have "cos x' * cos y' \<noteq> 0" by auto
   7.711 +
   7.712 +  have divide_nonzero_divide: "\<And> A B C :: real. C \<noteq> 0 \<Longrightarrow> (A / C) / (B / C) = A / B" by auto
   7.713 +  have divide_mult_commute: "\<And> A B C D :: real. A * B / (C * D) = (A / C) * (B / D)" by auto
   7.714 +
   7.715 +  have "tan (x' + y') = sin (x' + y') / (cos x' * cos y' - sin x' * sin y')" unfolding tan_def cos_add ..
   7.716 +  also have "\<dots> = (tan x' + tan y') / ((cos x' * cos y' - sin x' * sin y') / (cos x' * cos y'))" unfolding add_tan_eq[OF `cos x' \<noteq> 0` `cos y' \<noteq> 0`] divide_nonzero_divide[OF `cos x' * cos y' \<noteq> 0`] ..
   7.717 +  also have "\<dots> = (tan x' + tan y') / (1 - tan x' * tan y')" unfolding tan_def diff_divide_distrib divide_self[OF `cos x' * cos y' \<noteq> 0`] unfolding divide_mult_commute ..
   7.718 +  finally have tan_eq: "tan (x' + y') = (x + y) / (1 - x * y)" unfolding `tan y' = y` `tan x' = x` .
   7.719 +
   7.720 +  have "arctan (tan (x' + y')) = x' + y'" using `-(pi/4) < y'` `-(pi/4) \<le> x'` `y' < pi/4` and `x' \<le> pi/4` by (auto intro!: arctan_tan)
   7.721 +  moreover have "arctan (tan (x')) = x'" using `-(pi/2) < x'` and `x' < pi/2` by (auto intro!: arctan_tan)
   7.722 +  moreover have "arctan (tan (y')) = y'" using `-(pi/4) < y'` and `y' < pi/4` by (auto intro!: arctan_tan)
   7.723 +  ultimately have "arctan x + arctan y = arctan (tan (x' + y'))" unfolding `tan y' = y` [symmetric] `tan x' = x`[symmetric] by auto
   7.724 +  thus "arctan x + arctan y = arctan ((x + y) / (1 - x * y))" unfolding tan_eq .
   7.725 +qed
   7.726 +
   7.727 +lemma arctan1_eq_pi4: "arctan 1 = pi / 4" unfolding tan_45[symmetric] by (rule arctan_tan, auto simp add: m2pi_less_pi)
   7.728 +
   7.729 +theorem machin: "pi / 4 = 4 * arctan (1/5) - arctan (1 / 239)"
   7.730 +proof -
   7.731 +  have "\<bar>1 / 5\<bar> < (1 :: real)" by auto
   7.732 +  from arctan_add[OF less_imp_le[OF this] this]
   7.733 +  have "2 * arctan (1 / 5) = arctan (5 / 12)" by auto
   7.734 +  moreover
   7.735 +  have "\<bar>5 / 12\<bar> < (1 :: real)" by auto
   7.736 +  from arctan_add[OF less_imp_le[OF this] this]
   7.737 +  have "2 * arctan (5 / 12) = arctan (120 / 119)" by auto
   7.738 +  moreover 
   7.739 +  have "\<bar>1\<bar> \<le> (1::real)" and "\<bar>1 / 239\<bar> < (1::real)" by auto
   7.740 +  from arctan_add[OF this]
   7.741 +  have "arctan 1 + arctan (1 / 239) = arctan (120 / 119)" by auto
   7.742 +  ultimately have "arctan 1 + arctan (1 / 239) = 4 * arctan (1 / 5)" by auto
   7.743 +  thus ?thesis unfolding arctan1_eq_pi4 by algebra
   7.744 +qed
   7.745 +subsection {* Introducing the arcus tangens power series *}
   7.746 +
   7.747 +lemma monoseq_arctan_series: fixes x :: real
   7.748 +  assumes "\<bar>x\<bar> \<le> 1" shows "monoseq (\<lambda> n. 1 / real (n*2+1) * x^(n*2+1))" (is "monoseq ?a")
   7.749 +proof (cases "x = 0") case True thus ?thesis unfolding monoseq_def by auto
   7.750 +next
   7.751 +  case False
   7.752 +  have "norm x \<le> 1" and "x \<le> 1" and "-1 \<le> x" using assms by auto
   7.753 +  show "monoseq ?a"
   7.754 +  proof -
   7.755 +    { fix n fix x :: real assume "0 \<le> x" and "x \<le> 1"
   7.756 +      have "1 / real (Suc (Suc n * 2)) * x ^ Suc (Suc n * 2) \<le> 1 / real (Suc (n * 2)) * x ^ Suc (n * 2)"
   7.757 +      proof (rule mult_mono)
   7.758 +	show "1 / real (Suc (Suc n * 2)) \<le> 1 / real (Suc (n * 2))" by (rule frac_le) simp_all
   7.759 +	show "0 \<le> 1 / real (Suc (n * 2))" by auto
   7.760 +	show "x ^ Suc (Suc n * 2) \<le> x ^ Suc (n * 2)" by (rule power_decreasing) (simp_all add: `0 \<le> x` `x \<le> 1`)
   7.761 +	show "0 \<le> x ^ Suc (Suc n * 2)" by (rule zero_le_power) (simp add: `0 \<le> x`)
   7.762 +      qed
   7.763 +    } note mono = this
   7.764 +    
   7.765 +    show ?thesis
   7.766 +    proof (cases "0 \<le> x")
   7.767 +      case True from mono[OF this `x \<le> 1`, THEN allI]
   7.768 +      show ?thesis unfolding Suc_plus1[symmetric] by (rule mono_SucI2)
   7.769 +    next
   7.770 +      case False hence "0 \<le> -x" and "-x \<le> 1" using `-1 \<le> x` by auto
   7.771 +      from mono[OF this]
   7.772 +      have "\<And>n. 1 / real (Suc (Suc n * 2)) * x ^ Suc (Suc n * 2) \<ge> 1 / real (Suc (n * 2)) * x ^ Suc (n * 2)" using `0 \<le> -x` by auto
   7.773 +      thus ?thesis unfolding Suc_plus1[symmetric] by (rule mono_SucI1[OF allI])
   7.774 +    qed
   7.775 +  qed
   7.776 +qed
   7.777 +
   7.778 +lemma zeroseq_arctan_series: fixes x :: real
   7.779 +  assumes "\<bar>x\<bar> \<le> 1" shows "(\<lambda> n. 1 / real (n*2+1) * x^(n*2+1)) ----> 0" (is "?a ----> 0")
   7.780 +proof (cases "x = 0") case True thus ?thesis by (auto simp add: LIMSEQ_const)
   7.781 +next
   7.782 +  case False
   7.783 +  have "norm x \<le> 1" and "x \<le> 1" and "-1 \<le> x" using assms by auto
   7.784 +  show "?a ----> 0"
   7.785 +  proof (cases "\<bar>x\<bar> < 1")
   7.786 +    case True hence "norm x < 1" by auto
   7.787 +    from LIMSEQ_mult[OF LIMSEQ_inverse_real_of_nat LIMSEQ_power_zero[OF `norm x < 1`, THEN LIMSEQ_Suc]]
   7.788 +    show ?thesis unfolding inverse_eq_divide Suc_plus1 using LIMSEQ_linear[OF _ pos2] by auto
   7.789 +  next
   7.790 +    case False hence "x = -1 \<or> x = 1" using `\<bar>x\<bar> \<le> 1` by auto
   7.791 +    hence n_eq: "\<And> n. x ^ (n * 2 + 1) = x" by auto
   7.792 +    from LIMSEQ_mult[OF LIMSEQ_inverse_real_of_nat[THEN LIMSEQ_linear, OF pos2, unfolded inverse_eq_divide] LIMSEQ_const[of x]]
   7.793 +    show ?thesis unfolding n_eq by auto
   7.794 +  qed
   7.795 +qed
   7.796 +
   7.797 +lemma summable_arctan_series: fixes x :: real and n :: nat
   7.798 +  assumes "\<bar>x\<bar> \<le> 1" shows "summable (\<lambda> k. (-1)^k * (1 / real (k*2+1) * x ^ (k*2+1)))" (is "summable (?c x)")
   7.799 +  by (rule summable_Leibniz(1), rule zeroseq_arctan_series[OF assms], rule monoseq_arctan_series[OF assms])
   7.800 +
   7.801 +lemma less_one_imp_sqr_less_one: fixes x :: real assumes "\<bar>x\<bar> < 1" shows "x^2 < 1"
   7.802 +proof -
   7.803 +  from mult_mono1[OF less_imp_le[OF `\<bar>x\<bar> < 1`] abs_ge_zero[of x]]
   7.804 +  have "\<bar> x^2 \<bar> < 1" using `\<bar> x \<bar> < 1` unfolding numeral_2_eq_2 power_Suc2 by auto
   7.805 +  thus ?thesis using zero_le_power2 by auto
   7.806 +qed 
   7.807 +
   7.808 +lemma DERIV_arctan_series: assumes "\<bar> x \<bar> < 1"
   7.809 +  shows "DERIV (\<lambda> x'. \<Sum> k. (-1)^k * (1 / real (k*2+1) * x' ^ (k*2+1))) x :> (\<Sum> k. (-1)^k * x^(k*2))" (is "DERIV ?arctan _ :> ?Int")
   7.810 +proof -
   7.811 +  let "?f n" = "if even n then (-1)^(n div 2) * 1 / real (Suc n) else 0"
   7.812 +
   7.813 +  { fix n :: nat assume "even n" hence "2 * (n div 2) = n" by presburger } note n_even=this
   7.814 +  have if_eq: "\<And> n x'. ?f n * real (Suc n) * x'^n = (if even n then (-1)^(n div 2) * x'^(2 * (n div 2)) else 0)" using n_even by auto
   7.815 +
   7.816 +  { fix x :: real assume "\<bar>x\<bar> < 1" hence "x^2 < 1" by (rule less_one_imp_sqr_less_one)
   7.817 +    have "summable (\<lambda> n. -1 ^ n * (x^2) ^n)"
   7.818 +      by (rule summable_Leibniz(1), auto intro!: LIMSEQ_realpow_zero monoseq_realpow `x^2 < 1` order_less_imp_le[OF `x^2 < 1`])
   7.819 +    hence "summable (\<lambda> n. -1 ^ n * x^(2*n))" unfolding power_mult .
   7.820 +  } note summable_Integral = this
   7.821 +
   7.822 +  { fix f :: "nat \<Rightarrow> real"
   7.823 +    have "\<And> x. f sums x = (\<lambda> n. if even n then f (n div 2) else 0) sums x"
   7.824 +    proof
   7.825 +      fix x :: real assume "f sums x" 
   7.826 +      from sums_if[OF sums_zero this]
   7.827 +      show "(\<lambda> n. if even n then f (n div 2) else 0) sums x" by auto
   7.828 +    next
   7.829 +      fix x :: real assume "(\<lambda> n. if even n then f (n div 2) else 0) sums x"
   7.830 +      from LIMSEQ_linear[OF this[unfolded sums_def] pos2, unfolded sum_split_even_odd[unfolded mult_commute]]
   7.831 +      show "f sums x" unfolding sums_def by auto
   7.832 +    qed
   7.833 +    hence "op sums f = op sums (\<lambda> n. if even n then f (n div 2) else 0)" ..
   7.834 +  } note sums_even = this
   7.835 +
   7.836 +  have Int_eq: "(\<Sum> n. ?f n * real (Suc n) * x^n) = ?Int" unfolding if_eq mult_commute[of _ 2] suminf_def sums_even[of "\<lambda> n. -1 ^ n * x ^ (2 * n)", symmetric]
   7.837 +    by auto
   7.838 +
   7.839 +  { fix x :: real
   7.840 +    have if_eq': "\<And> n. (if even n then -1 ^ (n div 2) * 1 / real (Suc n) else 0) * x ^ Suc n = 
   7.841 +      (if even n then -1 ^ (n div 2) * (1 / real (Suc (2 * (n div 2))) * x ^ Suc (2 * (n div 2))) else 0)"
   7.842 +      using n_even by auto
   7.843 +    have idx_eq: "\<And> n. n * 2 + 1 = Suc (2 * n)" by auto 
   7.844 +    have "(\<Sum> n. ?f n * x^(Suc n)) = ?arctan x" unfolding if_eq' idx_eq suminf_def sums_even[of "\<lambda> n. -1 ^ n * (1 / real (Suc (2 * n)) * x ^ Suc (2 * n))", symmetric]
   7.845 +      by auto
   7.846 +  } note arctan_eq = this
   7.847 +
   7.848 +  have "DERIV (\<lambda> x. \<Sum> n. ?f n * x^(Suc n)) x :> (\<Sum> n. ?f n * real (Suc n) * x^n)"
   7.849 +  proof (rule DERIV_power_series')
   7.850 +    show "x \<in> {- 1 <..< 1}" using `\<bar> x \<bar> < 1` by auto
   7.851 +    { fix x' :: real assume x'_bounds: "x' \<in> {- 1 <..< 1}"
   7.852 +      hence "\<bar>x'\<bar> < 1" by auto
   7.853 +
   7.854 +      let ?S = "\<Sum> n. (-1)^n * x'^(2 * n)"
   7.855 +      show "summable (\<lambda> n. ?f n * real (Suc n) * x'^n)" unfolding if_eq
   7.856 +	by (rule sums_summable[where l="0 + ?S"], rule sums_if, rule sums_zero, rule summable_sums, rule summable_Integral[OF `\<bar>x'\<bar> < 1`])
   7.857 +    }
   7.858 +  qed auto
   7.859 +  thus ?thesis unfolding Int_eq arctan_eq .
   7.860 +qed
   7.861 +
   7.862 +lemma arctan_series: assumes "\<bar> x \<bar> \<le> 1"
   7.863 +  shows "arctan x = (\<Sum> k. (-1)^k * (1 / real (k*2+1) * x ^ (k*2+1)))" (is "_ = suminf (\<lambda> n. ?c x n)")
   7.864 +proof -
   7.865 +  let "?c' x n" = "(-1)^n * x^(n*2)"
   7.866 +
   7.867 +  { fix r x :: real assume "0 < r" and "r < 1" and "\<bar> x \<bar> < r"
   7.868 +    have "\<bar>x\<bar> < 1" using `r < 1` and `\<bar>x\<bar> < r` by auto
   7.869 +    from DERIV_arctan_series[OF this]
   7.870 +    have "DERIV (\<lambda> x. suminf (?c x)) x :> (suminf (?c' x))" .
   7.871 +  } note DERIV_arctan_suminf = this
   7.872 +
   7.873 +  { fix x :: real assume "\<bar>x\<bar> \<le> 1" note summable_Leibniz[OF zeroseq_arctan_series[OF this] monoseq_arctan_series[OF this]] }
   7.874 +  note arctan_series_borders = this
   7.875 +
   7.876 +  { fix x :: real assume "\<bar>x\<bar> < 1" have "arctan x = (\<Sum> k. ?c x k)"
   7.877 +  proof -
   7.878 +    obtain r where "\<bar>x\<bar> < r" and "r < 1" using dense[OF `\<bar>x\<bar> < 1`] by blast
   7.879 +    hence "0 < r" and "-r < x" and "x < r" by auto
   7.880 +
   7.881 +    have suminf_eq_arctan_bounded: "\<And> x a b. \<lbrakk> -r < a ; b < r ; a < b ; a \<le> x ; x \<le> b \<rbrakk> \<Longrightarrow> suminf (?c x) - arctan x = suminf (?c a) - arctan a"
   7.882 +    proof -
   7.883 +      fix x a b assume "-r < a" and "b < r" and "a < b" and "a \<le> x" and "x \<le> b"
   7.884 +      hence "\<bar>x\<bar> < r" by auto
   7.885 +      show "suminf (?c x) - arctan x = suminf (?c a) - arctan a"
   7.886 +      proof (rule DERIV_isconst2[of "a" "b"])
   7.887 +	show "a < b" and "a \<le> x" and "x \<le> b" using `a < b` `a \<le> x` `x \<le> b` by auto
   7.888 +	have "\<forall> x. -r < x \<and> x < r \<longrightarrow> DERIV (\<lambda> x. suminf (?c x) - arctan x) x :> 0"
   7.889 +	proof (rule allI, rule impI)
   7.890 +	  fix x assume "-r < x \<and> x < r" hence "\<bar>x\<bar> < r" by auto
   7.891 +	  hence "\<bar>x\<bar> < 1" using `r < 1` by auto
   7.892 +	  have "\<bar> - (x^2) \<bar> < 1" using less_one_imp_sqr_less_one[OF `\<bar>x\<bar> < 1`] by auto
   7.893 +	  hence "(\<lambda> n. (- (x^2)) ^ n) sums (1 / (1 - (- (x^2))))" unfolding real_norm_def[symmetric] by (rule geometric_sums)
   7.894 +	  hence "(?c' x) sums (1 / (1 - (- (x^2))))" unfolding power_mult_distrib[symmetric] power_mult nat_mult_commute[of _ 2] by auto
   7.895 +	  hence suminf_c'_eq_geom: "inverse (1 + x^2) = suminf (?c' x)" using sums_unique unfolding inverse_eq_divide by auto
   7.896 +	  have "DERIV (\<lambda> x. suminf (?c x)) x :> (inverse (1 + x^2))" unfolding suminf_c'_eq_geom
   7.897 +	    by (rule DERIV_arctan_suminf[OF `0 < r` `r < 1` `\<bar>x\<bar> < r`])
   7.898 +	  from DERIV_add_minus[OF this DERIV_arctan]
   7.899 +	  show "DERIV (\<lambda> x. suminf (?c x) - arctan x) x :> 0" unfolding diff_minus by auto
   7.900 +	qed
   7.901 +	hence DERIV_in_rball: "\<forall> y. a \<le> y \<and> y \<le> b \<longrightarrow> DERIV (\<lambda> x. suminf (?c x) - arctan x) y :> 0" using `-r < a` `b < r` by auto
   7.902 +	thus "\<forall> y. a < y \<and> y < b \<longrightarrow> DERIV (\<lambda> x. suminf (?c x) - arctan x) y :> 0" using `\<bar>x\<bar> < r` by auto
   7.903 +	show "\<forall> y. a \<le> y \<and> y \<le> b \<longrightarrow> isCont (\<lambda> x. suminf (?c x) - arctan x) y" using DERIV_in_rball DERIV_isCont by auto
   7.904 +      qed
   7.905 +    qed
   7.906 +    
   7.907 +    have suminf_arctan_zero: "suminf (?c 0) - arctan 0 = 0"
   7.908 +      unfolding Suc_plus1[symmetric] power_Suc2 mult_zero_right arctan_zero_zero suminf_zero by auto
   7.909 +    
   7.910 +    have "suminf (?c x) - arctan x = 0"
   7.911 +    proof (cases "x = 0")
   7.912 +      case True thus ?thesis using suminf_arctan_zero by auto
   7.913 +    next
   7.914 +      case False hence "0 < \<bar>x\<bar>" and "- \<bar>x\<bar> < \<bar>x\<bar>" by auto
   7.915 +      have "suminf (?c (-\<bar>x\<bar>)) - arctan (-\<bar>x\<bar>) = suminf (?c 0) - arctan 0"
   7.916 +	by (rule suminf_eq_arctan_bounded[where x="0" and a="-\<bar>x\<bar>" and b="\<bar>x\<bar>", symmetric], auto simp add: `\<bar>x\<bar> < r` `-\<bar>x\<bar> < \<bar>x\<bar>`)
   7.917 +      moreover
   7.918 +      have "suminf (?c x) - arctan x = suminf (?c (-\<bar>x\<bar>)) - arctan (-\<bar>x\<bar>)"
   7.919 +	by (rule suminf_eq_arctan_bounded[where x="x" and a="-\<bar>x\<bar>" and b="\<bar>x\<bar>"], auto simp add: `\<bar>x\<bar> < r` `-\<bar>x\<bar> < \<bar>x\<bar>`)
   7.920 +      ultimately 
   7.921 +      show ?thesis using suminf_arctan_zero by auto
   7.922 +    qed
   7.923 +    thus ?thesis by auto
   7.924 +  qed } note when_less_one = this
   7.925 +
   7.926 +  show "arctan x = suminf (\<lambda> n. ?c x n)"
   7.927 +  proof (cases "\<bar>x\<bar> < 1")
   7.928 +    case True thus ?thesis by (rule when_less_one)
   7.929 +  next case False hence "\<bar>x\<bar> = 1" using `\<bar>x\<bar> \<le> 1` by auto
   7.930 +    let "?a x n" = "\<bar>1 / real (n*2+1) * x^(n*2+1)\<bar>"
   7.931 +    let "?diff x n" = "\<bar> arctan x - (\<Sum> i = 0..<n. ?c x i)\<bar>"
   7.932 +    { fix n :: nat
   7.933 +      have "0 < (1 :: real)" by auto
   7.934 +      moreover
   7.935 +      { fix x :: real assume "0 < x" and "x < 1" hence "\<bar>x\<bar> \<le> 1" and "\<bar>x\<bar> < 1" by auto
   7.936 +	from `0 < x` have "0 < 1 / real (0 * 2 + (1::nat)) * x ^ (0 * 2 + 1)" by auto
   7.937 +	note bounds = mp[OF arctan_series_borders(2)[OF `\<bar>x\<bar> \<le> 1`] this, unfolded when_less_one[OF `\<bar>x\<bar> < 1`, symmetric], THEN spec]
   7.938 +	have "0 < 1 / real (n*2+1) * x^(n*2+1)" by (rule mult_pos_pos, auto simp only: zero_less_power[OF `0 < x`], auto)
   7.939 +	hence a_pos: "?a x n = 1 / real (n*2+1) * x^(n*2+1)" by (rule abs_of_pos)
   7.940 +        have "?diff x n \<le> ?a x n"
   7.941 +	proof (cases "even n")
   7.942 +	  case True hence sgn_pos: "(-1)^n = (1::real)" by auto
   7.943 +	  from `even n` obtain m where "2 * m = n" unfolding even_mult_two_ex by auto
   7.944 +	  from bounds[of m, unfolded this atLeastAtMost_iff]
   7.945 +	  have "\<bar>arctan x - (\<Sum>i = 0..<n. (?c x i))\<bar> \<le> (\<Sum>i = 0..<n + 1. (?c x i)) - (\<Sum>i = 0..<n. (?c x i))" by auto
   7.946 +	  also have "\<dots> = ?c x n" by auto
   7.947 +	  also have "\<dots> = ?a x n" unfolding sgn_pos a_pos by auto
   7.948 +	  finally show ?thesis .
   7.949 +	next
   7.950 +	  case False hence sgn_neg: "(-1)^n = (-1::real)" by auto
   7.951 +	  from `odd n` obtain m where m_def: "2 * m + 1 = n" unfolding odd_Suc_mult_two_ex by auto
   7.952 +	  hence m_plus: "2 * (m + 1) = n + 1" by auto
   7.953 +	  from bounds[of "m + 1", unfolded this atLeastAtMost_iff, THEN conjunct1] bounds[of m, unfolded m_def atLeastAtMost_iff, THEN conjunct2]
   7.954 +	  have "\<bar>arctan x - (\<Sum>i = 0..<n. (?c x i))\<bar> \<le> (\<Sum>i = 0..<n. (?c x i)) - (\<Sum>i = 0..<n+1. (?c x i))" by auto
   7.955 +	  also have "\<dots> = - ?c x n" by auto
   7.956 +	  also have "\<dots> = ?a x n" unfolding sgn_neg a_pos by auto
   7.957 +	  finally show ?thesis .
   7.958 +	qed
   7.959 +        hence "0 \<le> ?a x n - ?diff x n" by auto
   7.960 +      }
   7.961 +      hence "\<forall> x \<in> { 0 <..< 1 }. 0 \<le> ?a x n - ?diff x n" by auto
   7.962 +      moreover have "\<And>x. isCont (\<lambda> x. ?a x n - ?diff x n) x"
   7.963 +	unfolding real_diff_def divide_inverse
   7.964 +	by (auto intro!: isCont_add isCont_rabs isCont_ident isCont_minus isCont_arctan isCont_inverse isCont_mult isCont_power isCont_const isCont_setsum)
   7.965 +      ultimately have "0 \<le> ?a 1 n - ?diff 1 n" by (rule LIM_less_bound)
   7.966 +      hence "?diff 1 n \<le> ?a 1 n" by auto
   7.967 +    }
   7.968 +    have "?a 1 ----> 0" unfolding LIMSEQ_rabs_zero power_one divide_inverse by (auto intro!: LIMSEQ_mult LIMSEQ_linear LIMSEQ_inverse_real_of_nat)
   7.969 +    have "?diff 1 ----> 0"
   7.970 +    proof (rule LIMSEQ_I)
   7.971 +      fix r :: real assume "0 < r"
   7.972 +      obtain N :: nat where N_I: "\<And> n. N \<le> n \<Longrightarrow> ?a 1 n < r" using LIMSEQ_D[OF `?a 1 ----> 0` `0 < r`] by auto
   7.973 +      { fix n assume "N \<le> n" from `?diff 1 n \<le> ?a 1 n` N_I[OF this]
   7.974 +	have "norm (?diff 1 n - 0) < r" by auto }
   7.975 +      thus "\<exists> N. \<forall> n \<ge> N. norm (?diff 1 n - 0) < r" by blast
   7.976 +    qed
   7.977 +    from this[unfolded LIMSEQ_rabs_zero real_diff_def add_commute[of "arctan 1"], THEN LIMSEQ_add_const, of "- arctan 1", THEN LIMSEQ_minus]
   7.978 +    have "(?c 1) sums (arctan 1)" unfolding sums_def by auto
   7.979 +    hence "arctan 1 = (\<Sum> i. ?c 1 i)" by (rule sums_unique)
   7.980 +    
   7.981 +    show ?thesis
   7.982 +    proof (cases "x = 1", simp add: `arctan 1 = (\<Sum> i. ?c 1 i)`)
   7.983 +      assume "x \<noteq> 1" hence "x = -1" using `\<bar>x\<bar> = 1` by auto
   7.984 +      
   7.985 +      have "- (pi / 2) < 0" using pi_gt_zero by auto
   7.986 +      have "- (2 * pi) < 0" using pi_gt_zero by auto
   7.987 +      
   7.988 +      have c_minus_minus: "\<And> i. ?c (- 1) i = - ?c 1 i" by auto
   7.989 +    
   7.990 +      have "arctan (- 1) = arctan (tan (-(pi / 4)))" unfolding tan_45 tan_minus ..
   7.991 +      also have "\<dots> = - (pi / 4)" by (rule arctan_tan, auto simp add: order_less_trans[OF `- (pi / 2) < 0` pi_gt_zero])
   7.992 +      also have "\<dots> = - (arctan (tan (pi / 4)))" unfolding neg_equal_iff_equal by (rule arctan_tan[symmetric], auto simp add: order_less_trans[OF `- (2 * pi) < 0` pi_gt_zero])
   7.993 +      also have "\<dots> = - (arctan 1)" unfolding tan_45 ..
   7.994 +      also have "\<dots> = - (\<Sum> i. ?c 1 i)" using `arctan 1 = (\<Sum> i. ?c 1 i)` by auto
   7.995 +      also have "\<dots> = (\<Sum> i. ?c (- 1) i)" using suminf_minus[OF sums_summable[OF `(?c 1) sums (arctan 1)`]] unfolding c_minus_minus by auto
   7.996 +      finally show ?thesis using `x = -1` by auto
   7.997 +    qed
   7.998 +  qed
   7.999 +qed
  7.1000 +
  7.1001 +lemma arctan_half: fixes x :: real
  7.1002 +  shows "arctan x = 2 * arctan (x / (1 + sqrt(1 + x^2)))"
  7.1003 +proof -
  7.1004 +  obtain y where low: "- (pi / 2) < y" and high: "y < pi / 2" and y_eq: "tan y = x" using tan_total by blast
  7.1005 +  hence low2: "- (pi / 2) < y / 2" and high2: "y / 2 < pi / 2" by auto
  7.1006 +
  7.1007 +  have divide_nonzero_divide: "\<And> A B C :: real. C \<noteq> 0 \<Longrightarrow> A / B = (A / C) / (B / C)" by auto
  7.1008 +  
  7.1009 +  have "0 < cos y" using cos_gt_zero_pi[OF low high] .
  7.1010 +  hence "cos y \<noteq> 0" and cos_sqrt: "sqrt ((cos y) ^ 2) = cos y" by auto
  7.1011 +
  7.1012 +  have "1 + (tan y)^2 = 1 + sin y^2 / cos y^2" unfolding tan_def power_divide ..
  7.1013 +  also have "\<dots> = cos y^2 / cos y^2 + sin y^2 / cos y^2" using `cos y \<noteq> 0` by auto
  7.1014 +  also have "\<dots> = 1 / cos y^2" unfolding add_divide_distrib[symmetric] sin_cos_squared_add2 ..
  7.1015 +  finally have "1 + (tan y)^2 = 1 / cos y^2" .
  7.1016 +
  7.1017 +  have "sin y / (cos y + 1) = tan y / ((cos y + 1) / cos y)" unfolding tan_def divide_nonzero_divide[OF `cos y \<noteq> 0`, symmetric] ..
  7.1018 +  also have "\<dots> = tan y / (1 + 1 / cos y)" using `cos y \<noteq> 0` unfolding add_divide_distrib by auto
  7.1019 +  also have "\<dots> = tan y / (1 + 1 / sqrt(cos y^2))" unfolding cos_sqrt ..
  7.1020 +  also have "\<dots> = tan y / (1 + sqrt(1 / cos y^2))" unfolding real_sqrt_divide by auto
  7.1021 +  finally have eq: "sin y / (cos y + 1) = tan y / (1 + sqrt(1 + (tan y)^2))" unfolding `1 + (tan y)^2 = 1 / cos y^2` .
  7.1022 +
  7.1023 +  have "arctan x = y" using arctan_tan low high y_eq by auto
  7.1024 +  also have "\<dots> = 2 * (arctan (tan (y/2)))" using arctan_tan[OF low2 high2] by auto
  7.1025 +  also have "\<dots> = 2 * (arctan (sin y / (cos y + 1)))" unfolding tan_half[OF low2 high2] by auto
  7.1026 +  finally show ?thesis unfolding eq `tan y = x` .
  7.1027 +qed
  7.1028 +
  7.1029 +lemma arctan_monotone: assumes "x < y"
  7.1030 +  shows "arctan x < arctan y"
  7.1031 +proof -
  7.1032 +  obtain z where "-(pi / 2) < z" and "z < pi / 2" and "tan z = x" using tan_total by blast
  7.1033 +  obtain w where "-(pi / 2) < w" and "w < pi / 2" and "tan w = y" using tan_total by blast
  7.1034 +  have "z < w" unfolding tan_monotone'[OF `-(pi / 2) < z` `z < pi / 2` `-(pi / 2) < w` `w < pi / 2`] `tan z = x` `tan w = y` using `x < y` .
  7.1035 +  thus ?thesis
  7.1036 +    unfolding `tan z = x`[symmetric] arctan_tan[OF `-(pi / 2) < z` `z < pi / 2`]
  7.1037 +    unfolding `tan w = y`[symmetric] arctan_tan[OF `-(pi / 2) < w` `w < pi / 2`] .
  7.1038 +qed
  7.1039 +
  7.1040 +lemma arctan_monotone': assumes "x \<le> y" shows "arctan x \<le> arctan y"
  7.1041 +proof (cases "x = y") 
  7.1042 +  case False hence "x < y" using `x \<le> y` by auto from arctan_monotone[OF this] show ?thesis by auto
  7.1043 +qed auto
  7.1044 +
  7.1045 +lemma arctan_minus: "arctan (- x) = - arctan x" 
  7.1046 +proof -
  7.1047 +  obtain y where "- (pi / 2) < y" and "y < pi / 2" and "tan y = x" using tan_total by blast
  7.1048 +  thus ?thesis unfolding `tan y = x`[symmetric] tan_minus[symmetric] using arctan_tan[of y] arctan_tan[of "-y"] by auto 
  7.1049 +qed
  7.1050 +
  7.1051 +lemma arctan_inverse: assumes "x \<noteq> 0" shows "arctan (1 / x) = sgn x * pi / 2 - arctan x"
  7.1052 +proof -
  7.1053 +  obtain y where "- (pi / 2) < y" and "y < pi / 2" and "tan y = x" using tan_total by blast
  7.1054 +  hence "y = arctan x" unfolding `tan y = x`[symmetric] using arctan_tan by auto
  7.1055 +
  7.1056 +  { fix y x :: real assume "0 < y" and "y < pi /2" and "y = arctan x" and "tan y = x" hence "- (pi / 2) < y" by auto
  7.1057 +    have "tan y > 0" using tan_monotone'[OF _ _ `- (pi / 2) < y` `y < pi / 2`, of 0] tan_zero `0 < y` by auto
  7.1058 +    hence "x > 0" using `tan y = x` by auto
  7.1059 +
  7.1060 +    have "- (pi / 2) < pi / 2 - y" using `y > 0` `y < pi / 2` by auto
  7.1061 +    moreover have "pi / 2 - y < pi / 2" using `y > 0` `y < pi / 2` by auto
  7.1062 +    ultimately have "arctan (1 / x) = pi / 2 - y" unfolding `tan y = x`[symmetric] tan_inverse using arctan_tan by auto
  7.1063 +    hence "arctan (1 / x) = sgn x * pi / 2 - arctan x" unfolding `y = arctan x` real_sgn_pos[OF `x > 0`] by auto
  7.1064 +  } note pos_y = this
  7.1065 +
  7.1066 +  show ?thesis
  7.1067 +  proof (cases "y > 0")
  7.1068 +    case True from pos_y[OF this `y < pi / 2` `y = arctan x` `tan y = x`] show ?thesis .
  7.1069 +  next
  7.1070 +    case False hence "y \<le> 0" by auto
  7.1071 +    moreover have "y \<noteq> 0" 
  7.1072 +    proof (rule ccontr)
  7.1073 +      assume "\<not> y \<noteq> 0" hence "y = 0" by auto
  7.1074 +      have "x = 0" unfolding `tan y = x`[symmetric] `y = 0` tan_zero ..
  7.1075 +      thus False using `x \<noteq> 0` by auto
  7.1076 +    qed
  7.1077 +    ultimately have "y < 0" by auto
  7.1078 +    hence "0 < - y" and "-y < pi / 2" using `- (pi / 2) < y` by auto
  7.1079 +    moreover have "-y = arctan (-x)" unfolding arctan_minus `y = arctan x` ..
  7.1080 +    moreover have "tan (-y) = -x" unfolding tan_minus `tan y = x` ..
  7.1081 +    ultimately have "arctan (1 / -x) = sgn (-x) * pi / 2 - arctan (-x)" using pos_y by blast
  7.1082 +    hence "arctan (- (1 / x)) = - (sgn x * pi / 2 - arctan x)" unfolding arctan_minus[of x] divide_minus_right sgn_minus by auto
  7.1083 +    thus ?thesis unfolding arctan_minus neg_equal_iff_equal .
  7.1084 +  qed
  7.1085 +qed
  7.1086 +
  7.1087 +theorem pi_series: "pi / 4 = (\<Sum> k. (-1)^k * 1 / real (k*2+1))" (is "_ = ?SUM")
  7.1088 +proof -
  7.1089 +  have "pi / 4 = arctan 1" using arctan1_eq_pi4 by auto
  7.1090 +  also have "\<dots> = ?SUM" using arctan_series[of 1] by auto
  7.1091 +  finally show ?thesis by auto
  7.1092 +qed
  7.1093  
  7.1094  subsection {* Existence of Polar Coordinates *}
  7.1095