src/HOL/Multivariate_Analysis/Cauchy_Integral_Thm.thy
changeset 62131 1baed43f453e
parent 62101 26c0a70f78a3
child 62175 8ffc4d0e652d
     1.1 --- a/src/HOL/Multivariate_Analysis/Cauchy_Integral_Thm.thy	Mon Jan 11 16:38:39 2016 +0100
     1.2 +++ b/src/HOL/Multivariate_Analysis/Cauchy_Integral_Thm.thy	Mon Jan 11 22:14:15 2016 +0000
     1.3 @@ -572,6 +572,12 @@
     1.4  lemma contour_integral_unique: "(f has_contour_integral i)  g \<Longrightarrow> contour_integral g f = i"
     1.5    by (auto simp: contour_integral_def has_contour_integral_def integral_def [symmetric])
     1.6  
     1.7 +corollary has_contour_integral_eqpath:
     1.8 +     "\<lbrakk>(f has_contour_integral y) p; f contour_integrable_on \<gamma>; 
     1.9 +       contour_integral p f = contour_integral \<gamma> f\<rbrakk>
    1.10 +      \<Longrightarrow> (f has_contour_integral y) \<gamma>"
    1.11 +using contour_integrable_on_def contour_integral_unique by auto
    1.12 +
    1.13  lemma has_contour_integral_integral:
    1.14      "f contour_integrable_on i \<Longrightarrow> (f has_contour_integral (contour_integral i f)) i"
    1.15    by (metis contour_integral_unique contour_integrable_on_def)
    1.16 @@ -2826,12 +2832,12 @@
    1.17  qed
    1.18  
    1.19  lemma contour_integrable_holomorphic_simple:
    1.20 -  assumes contf: "continuous_on s f"
    1.21 +  assumes fh: "f holomorphic_on s"
    1.22        and os: "open s"
    1.23        and g: "valid_path g" "path_image g \<subseteq> s"
    1.24 -      and fh: "f holomorphic_on s"
    1.25      shows "f contour_integrable_on g"
    1.26 -  apply (rule contour_integrable_holomorphic [OF contf os Finite_Set.finite.emptyI g])
    1.27 +  apply (rule contour_integrable_holomorphic [OF _ os Finite_Set.finite.emptyI g])
    1.28 +  apply (simp add: fh holomorphic_on_imp_continuous_on)
    1.29    using fh  by (simp add: complex_differentiable_def holomorphic_on_open os)
    1.30  
    1.31  lemma continuous_on_inversediff:
    1.32 @@ -2840,7 +2846,7 @@
    1.33  
    1.34  corollary contour_integrable_inversediff:
    1.35      "\<lbrakk>valid_path g; z \<notin> path_image g\<rbrakk> \<Longrightarrow> (\<lambda>w. 1 / (w-z)) contour_integrable_on g"
    1.36 -apply (rule contour_integrable_holomorphic_simple [of "UNIV-{z}", OF continuous_on_inversediff])
    1.37 +apply (rule contour_integrable_holomorphic_simple [of _ "UNIV-{z}"])
    1.38  apply (auto simp: holomorphic_on_open open_delete intro!: derivative_eq_intros)
    1.39  done
    1.40  
    1.41 @@ -5972,8 +5978,7 @@
    1.42    assumes contf: "continuous_on (cball z r) f"
    1.43        and holf: "f holomorphic_on ball z r"
    1.44        and w: "w \<in> ball z r"
    1.45 -    shows "((\<lambda>u. f u / (u - w) ^ (Suc k))
    1.46 -             has_contour_integral ((2 * pi * ii) / of_real(fact k) * (deriv ^^ k) f w))
    1.47 +    shows "((\<lambda>u. f u / (u - w) ^ (Suc k)) has_contour_integral ((2 * pi * ii) / (fact k) * (deriv ^^ k) f w))
    1.48             (circlepath z r)"
    1.49  using w
    1.50  proof (induction k arbitrary: w)
    1.51 @@ -5994,18 +5999,15 @@
    1.52      using Suc.prems assms has_field_derivative_higher_deriv by auto
    1.53    then have dnf_diff: "\<And>n. (deriv ^^ n) f complex_differentiable (at w)"
    1.54      by (force simp add: complex_differentiable_def)
    1.55 -  have "deriv (\<lambda>w. complex_of_real (2 * pi) * \<i> / complex_of_real (fact k) * (deriv ^^ k) f w) w =
    1.56 +  have "deriv (\<lambda>w. complex_of_real (2 * pi) * \<i> / (fact k) * (deriv ^^ k) f w) w =
    1.57            of_nat (Suc k) * contour_integral (circlepath z r) (\<lambda>u. f u / (u - w) ^ Suc (Suc k))"
    1.58 -    apply (rule DERIV_imp_deriv)
    1.59 -    apply (rule Cauchy_next_derivative_circlepath [OF f Suc.IH _ Suc.prems])
    1.60 -    apply auto
    1.61 -    done
    1.62 +    by (force intro!: DERIV_imp_deriv Cauchy_next_derivative_circlepath [OF f Suc.IH _ Suc.prems])
    1.63    also have "... = of_nat (Suc k) * X"
    1.64      by (simp only: con)
    1.65 -  finally have "deriv (\<lambda>w. ((2 * pi) * \<i> / of_real (fact k)) * (deriv ^^ k) f w) w = of_nat (Suc k) * X" .
    1.66 -  then have "((2 * pi) * \<i> / of_real (fact k)) * deriv (\<lambda>w. (deriv ^^ k) f w) w = of_nat (Suc k) * X"
    1.67 +  finally have "deriv (\<lambda>w. ((2 * pi) * \<i> / (fact k)) * (deriv ^^ k) f w) w = of_nat (Suc k) * X" .
    1.68 +  then have "((2 * pi) * \<i> / (fact k)) * deriv (\<lambda>w. (deriv ^^ k) f w) w = of_nat (Suc k) * X"
    1.69      by (metis complex_derivative_cmult dnf_diff)
    1.70 -  then have "deriv (\<lambda>w. (deriv ^^ k) f w) w = of_nat (Suc k) * X / ((2 * pi) * \<i> / of_real (fact k))"
    1.71 +  then have "deriv (\<lambda>w. (deriv ^^ k) f w) w = of_nat (Suc k) * X / ((2 * pi) * \<i> / (fact k))"
    1.72      by (simp add: field_simps)
    1.73    then show ?case
    1.74    using of_nat_eq_0_iff X by fastforce
    1.75 @@ -6017,8 +6019,7 @@
    1.76        and w: "w \<in> ball z r"
    1.77      shows "(\<lambda>u. f u / (u - w)^(Suc k)) contour_integrable_on (circlepath z r)"
    1.78             (is "?thes1")
    1.79 -      and "(deriv ^^ k) f w =
    1.80 -             of_real(fact k) / (2 * pi * ii) * contour_integral(circlepath z r) (\<lambda>u. f u/(u - w)^(Suc k))"
    1.81 +      and "(deriv ^^ k) f w = (fact k) / (2 * pi * ii) * contour_integral(circlepath z r) (\<lambda>u. f u/(u - w)^(Suc k))"
    1.82             (is "?thes2")
    1.83  proof -
    1.84    have *: "((\<lambda>u. f u / (u - w) ^ Suc k) has_contour_integral (2 * pi) * \<i> / (fact k) * (deriv ^^ k) f w)
    1.85 @@ -6033,7 +6034,7 @@
    1.86  
    1.87  corollary Cauchy_contour_integral_circlepath:
    1.88    assumes "continuous_on (cball z r) f" "f holomorphic_on ball z r" "w \<in> ball z r"
    1.89 -    shows "contour_integral(circlepath z r) (\<lambda>u. f u/(u - w)^(Suc k)) = (2 * pi * ii) * (deriv ^^ k) f w / of_real(fact k)"
    1.90 +    shows "contour_integral(circlepath z r) (\<lambda>u. f u/(u - w)^(Suc k)) = (2 * pi * ii) * (deriv ^^ k) f w / (fact k)"
    1.91  by (simp add: Cauchy_higher_derivative_integral_circlepath [OF assms])
    1.92  
    1.93  corollary Cauchy_contour_integral_circlepath_2:
    1.94 @@ -6048,14 +6049,14 @@
    1.95  theorem holomorphic_power_series:
    1.96    assumes holf: "f holomorphic_on ball z r"
    1.97        and w: "w \<in> ball z r"
    1.98 -    shows "((\<lambda>n. (deriv ^^ n) f z / of_real(fact n) * (w - z)^n) sums f w)"
    1.99 +    shows "((\<lambda>n. (deriv ^^ n) f z / (fact n) * (w - z)^n) sums f w)"
   1.100  proof -
   1.101    have fh': "f holomorphic_on cball z ((r + dist w z) / 2)"
   1.102       apply (rule holomorphic_on_subset [OF holf])
   1.103       apply (clarsimp simp del: divide_const_simps)
   1.104       apply (metis add.commute dist_commute le_less_trans mem_ball real_gt_half_sum w)
   1.105       done
   1.106 -  \<comment>\<open>Replacing @{term r} and the original (weak) premises\<close>
   1.107 +  --\<open>Replacing @{term r} and the original (weak) premises\<close>
   1.108    obtain r where "0 < r" and holfc: "f holomorphic_on cball z r" and w: "w \<in> ball z r"
   1.109      apply (rule that [of "(r + dist w z) / 2"])
   1.110        apply (simp_all add: fh')
   1.111 @@ -6102,7 +6103,7 @@
   1.112          apply (auto simp: geometric_sum [OF wzu_not1])
   1.113          apply (simp add: field_simps norm_mult [symmetric])
   1.114          done
   1.115 -      also have "... = norm ((u - z) ^ N * (w - u) - ((w - z) ^ N - (u - z) ^ N) * (u - w)) / (r ^ N * norm (u - w)) * norm (f u)"
   1.116 +      also have "... = norm ((u-z) ^ N * (w - u) - ((w - z) ^ N - (u-z) ^ N) * (u-w)) / (r ^ N * norm (u-w)) * norm (f u)"
   1.117          using \<open>0 < r\<close> r by (simp add: divide_simps norm_mult norm_divide norm_power dist_norm norm_minus_commute)
   1.118        also have "... = norm ((w - z) ^ N * (w - u)) / (r ^ N * norm (u - w)) * norm (f u)"
   1.119          by (simp add: algebra_simps)
   1.120 @@ -6148,7 +6149,7 @@
   1.121      using w by (auto simp: dist_commute dist_norm contour_integral_unique [OF Cauchy_integral_circlepath_simple [OF holfc]])
   1.122    then have "(\<lambda>k. contour_integral (circlepath z r) (\<lambda>u. f u / (u - z) ^ Suc k) * (w - z)^k / (\<i> * (of_real pi * 2)))
   1.123              sums ((2 * of_real pi * ii * f w) / (\<i> * (complex_of_real pi * 2)))"
   1.124 -    by (rule Series.sums_divide)
   1.125 +    by (rule sums_divide)
   1.126    then have "(\<lambda>n. (w - z) ^ n * contour_integral (circlepath z r) (\<lambda>u. f u / (u - z) ^ Suc n) / (\<i> * (of_real pi * 2)))
   1.127              sums f w"
   1.128      by (simp add: field_simps)
   1.129 @@ -6381,4 +6382,1024 @@
   1.130      using that by blast
   1.131  qed
   1.132  
   1.133 +
   1.134 +subsection\<open>Some more simple/convenient versions for applications.\<close>
   1.135 +
   1.136 +lemma holomorphic_uniform_sequence:
   1.137 +  assumes s: "open s"
   1.138 +      and hol_fn: "\<And>n. (f n) holomorphic_on s"
   1.139 +      and to_g: "\<And>x. x \<in> s
   1.140 +                     \<Longrightarrow> \<exists>d. 0 < d \<and> cball x d \<subseteq> s \<and>
   1.141 +                             (\<forall>e. 0 < e \<longrightarrow> eventually (\<lambda>n. \<forall>y \<in> cball x d. norm(f n y - g y) < e) sequentially)"
   1.142 +  shows "g holomorphic_on s"
   1.143 +proof -
   1.144 +  have "\<exists>f'. (g has_field_derivative f') (at z)" if "z \<in> s" for z
   1.145 +  proof -
   1.146 +    obtain r where "0 < r" and r: "cball z r \<subseteq> s"
   1.147 +               and fg: "\<forall>e. 0 < e \<longrightarrow> eventually (\<lambda>n. \<forall>y \<in> cball z r. norm(f n y - g y) < e) sequentially"
   1.148 +      using to_g [OF \<open>z \<in> s\<close>] by blast
   1.149 +    have *: "\<forall>\<^sub>F n in sequentially. continuous_on (cball z r) (f n) \<and> f n holomorphic_on ball z r"
   1.150 +      apply (intro eventuallyI conjI)
   1.151 +      using hol_fn holomorphic_on_imp_continuous_on holomorphic_on_subset r apply blast
   1.152 +      apply (metis hol_fn holomorphic_on_subset interior_cball interior_subset r)
   1.153 +      done
   1.154 +    show ?thesis
   1.155 +      apply (rule holomorphic_uniform_limit [OF *])
   1.156 +      using \<open>0 < r\<close> centre_in_ball fg
   1.157 +      apply (auto simp: holomorphic_on_open)
   1.158 +      done
   1.159 +  qed
   1.160 +  with s show ?thesis
   1.161 +    by (simp add: holomorphic_on_open)
   1.162 +qed
   1.163 +
   1.164 +lemma has_complex_derivative_uniform_sequence:
   1.165 +  fixes s :: "complex set"
   1.166 +  assumes s: "open s"
   1.167 +      and hfd: "\<And>n x. x \<in> s \<Longrightarrow> ((f n) has_field_derivative f' n x) (at x)"
   1.168 +      and to_g: "\<And>x. x \<in> s
   1.169 +             \<Longrightarrow> \<exists>d. 0 < d \<and> cball x d \<subseteq> s \<and>
   1.170 +                     (\<forall>e. 0 < e \<longrightarrow> eventually (\<lambda>n. \<forall>y \<in> cball x d. norm(f n y - g y) < e) sequentially)"
   1.171 +  shows "\<exists>g'. \<forall>x \<in> s. (g has_field_derivative g' x) (at x) \<and> ((\<lambda>n. f' n x) \<longlongrightarrow> g' x) sequentially"
   1.172 +proof -
   1.173 +  have y: "\<exists>y. (g has_field_derivative y) (at z) \<and> (\<lambda>n. f' n z) \<longlonglongrightarrow> y" if "z \<in> s" for z
   1.174 +  proof -
   1.175 +    obtain r where "0 < r" and r: "cball z r \<subseteq> s"
   1.176 +               and fg: "\<forall>e. 0 < e \<longrightarrow> eventually (\<lambda>n. \<forall>y \<in> cball z r. norm(f n y - g y) < e) sequentially"
   1.177 +      using to_g [OF \<open>z \<in> s\<close>] by blast
   1.178 +    have *: "\<forall>\<^sub>F n in sequentially. continuous_on (cball z r) (f n) \<and>
   1.179 +                                   (\<forall>w \<in> ball z r. ((f n) has_field_derivative (f' n w)) (at w))"
   1.180 +      apply (intro eventuallyI conjI)
   1.181 +      apply (meson hfd holomorphic_on_imp_continuous_on holomorphic_on_open holomorphic_on_subset r s)
   1.182 +      using ball_subset_cball hfd r apply blast
   1.183 +      done
   1.184 +    show ?thesis
   1.185 +      apply (rule has_complex_derivative_uniform_limit [OF *, of g])
   1.186 +      using \<open>0 < r\<close> centre_in_ball fg
   1.187 +      apply force+
   1.188 +      done
   1.189 +  qed
   1.190 +  show ?thesis
   1.191 +    by (rule bchoice) (blast intro: y)
   1.192 +qed
   1.193 +
   1.194 +
   1.195 +subsection\<open>On analytic functions defined by a series.\<close>
   1.196 +
   1.197 +lemma series_and_derivative_comparison:
   1.198 +  fixes s :: "complex set"
   1.199 +  assumes s: "open s"
   1.200 +      and h: "summable h"
   1.201 +      and hfd: "\<And>n x. x \<in> s \<Longrightarrow> (f n has_field_derivative f' n x) (at x)"
   1.202 +      and to_g: "\<And>n x. \<lbrakk>N \<le> n; x \<in> s\<rbrakk> \<Longrightarrow> norm(f n x) \<le> h n"
   1.203 +  obtains g g' where "\<forall>x \<in> s. ((\<lambda>n. f n x) sums g x) \<and> ((\<lambda>n. f' n x) sums g' x) \<and> (g has_field_derivative g' x) (at x)"
   1.204 +proof -
   1.205 +  obtain g where g: "\<And>e. e>0 \<Longrightarrow> \<exists>N. \<forall>n x. N \<le> n \<and> x \<in> s \<longrightarrow> dist (\<Sum>n<n. f n x) (g x) < e"
   1.206 +    using series_comparison_uniform [OF h to_g, of N s] by force
   1.207 +  have *: "\<exists>d>0. cball x d \<subseteq> s \<and> (\<forall>e>0. \<forall>\<^sub>F n in sequentially. \<forall>y\<in>cball x d. cmod ((\<Sum>a<n. f a y) - g y) < e)"
   1.208 +         if "x \<in> s" for x
   1.209 +  proof -
   1.210 +    obtain d where "d>0" and d: "cball x d \<subseteq> s"
   1.211 +      using open_contains_cball [of "s"] \<open>x \<in> s\<close> s by blast
   1.212 +    then show ?thesis
   1.213 +      apply (rule_tac x=d in exI)
   1.214 +      apply (auto simp: dist_norm eventually_sequentially)
   1.215 +      apply (metis g contra_subsetD dist_norm)
   1.216 +      done
   1.217 +  qed
   1.218 +  have "(\<forall>x\<in>s. (\<lambda>n. \<Sum>i<n. f i x) \<longlonglongrightarrow> g x)"
   1.219 +    using g by (force simp add: lim_sequentially)
   1.220 +  moreover have "\<exists>g'. \<forall>x\<in>s. (g has_field_derivative g' x) (at x) \<and> (\<lambda>n. \<Sum>i<n. f' i x) \<longlonglongrightarrow> g' x"
   1.221 +    by (rule has_complex_derivative_uniform_sequence [OF s]) (auto intro: * hfd DERIV_setsum)+
   1.222 +  ultimately show ?thesis
   1.223 +    by (force simp add: sums_def  conj_commute intro: that)
   1.224 +qed
   1.225 +
   1.226 +text\<open>A version where we only have local uniform/comparative convergence.\<close>
   1.227 +
   1.228 +lemma series_and_derivative_comparison_local:
   1.229 +  fixes s :: "complex set"
   1.230 +  assumes s: "open s"
   1.231 +      and hfd: "\<And>n x. x \<in> s \<Longrightarrow> (f n has_field_derivative f' n x) (at x)"
   1.232 +      and to_g: "\<And>x. x \<in> s \<Longrightarrow>
   1.233 +                      \<exists>d h N. 0 < d \<and> summable h \<and> (\<forall>n y. N \<le> n \<and> y \<in> ball x d \<longrightarrow> norm(f n y) \<le> h n)"
   1.234 +  shows "\<exists>g g'. \<forall>x \<in> s. ((\<lambda>n. f n x) sums g x) \<and> ((\<lambda>n. f' n x) sums g' x) \<and> (g has_field_derivative g' x) (at x)"
   1.235 +proof -
   1.236 +  have "\<exists>y. (\<lambda>n. f n z) sums (\<Sum>n. f n z) \<and> (\<lambda>n. f' n z) sums y \<and> ((\<lambda>x. \<Sum>n. f n x) has_field_derivative y) (at z)"
   1.237 +       if "z \<in> s" for z
   1.238 +  proof -
   1.239 +    obtain d h N where "0 < d" "summable h" and le_h: "\<And>n y. \<lbrakk>N \<le> n; y \<in> ball z d\<rbrakk> \<Longrightarrow> norm(f n y) \<le> h n"
   1.240 +      using to_g \<open>z \<in> s\<close> by blast
   1.241 +    then obtain r where "r>0" and r: "ball z r \<subseteq> ball z d \<inter> s" using \<open>z \<in> s\<close> s
   1.242 +      by (metis Int_iff open_ball centre_in_ball open_Int open_contains_ball_eq)
   1.243 +    have 1: "open (ball z d \<inter> s)"
   1.244 +      by (simp add: open_Int s)
   1.245 +    have 2: "\<And>n x. x \<in> ball z d \<inter> s \<Longrightarrow> (f n has_field_derivative f' n x) (at x)"
   1.246 +      by (auto simp: hfd)
   1.247 +    obtain g g' where gg': "\<forall>x \<in> ball z d \<inter> s. ((\<lambda>n. f n x) sums g x) \<and>
   1.248 +                                    ((\<lambda>n. f' n x) sums g' x) \<and> (g has_field_derivative g' x) (at x)"
   1.249 +      by (auto intro: le_h series_and_derivative_comparison [OF 1 \<open>summable h\<close> hfd])
   1.250 +    then have "(\<lambda>n. f' n z) sums g' z"
   1.251 +      by (meson \<open>0 < r\<close> centre_in_ball contra_subsetD r)
   1.252 +    moreover have "(\<lambda>n. f n z) sums (\<Sum>n. f n z)"
   1.253 +      by (metis summable_comparison_test' summable_sums centre_in_ball \<open>0 < d\<close> \<open>summable h\<close> le_h)
   1.254 +    moreover have "((\<lambda>x. \<Sum>n. f n x) has_field_derivative g' z) (at z)"
   1.255 +      apply (rule_tac f=g in DERIV_transform_at [OF _ \<open>0 < r\<close>])
   1.256 +      apply (simp add: gg' \<open>z \<in> s\<close> \<open>0 < d\<close>)
   1.257 +      apply (metis (full_types) contra_subsetD dist_commute gg' mem_ball r sums_unique)
   1.258 +      done
   1.259 +    ultimately show ?thesis by auto
   1.260 +  qed
   1.261 +  then show ?thesis
   1.262 +    by (rule_tac x="\<lambda>x. suminf  (\<lambda>n. f n x)" in exI) meson
   1.263 +qed
   1.264 +
   1.265 +
   1.266 +text\<open>Sometimes convenient to compare with a complex series of positive reals. (?)\<close>
   1.267 +
   1.268 +lemma series_and_derivative_comparison_complex:
   1.269 +  fixes s :: "complex set"
   1.270 +  assumes s: "open s"
   1.271 +      and hfd: "\<And>n x. x \<in> s \<Longrightarrow> (f n has_field_derivative f' n x) (at x)"
   1.272 +      and to_g: "\<And>x. x \<in> s \<Longrightarrow>
   1.273 +                      \<exists>d h N. 0 < d \<and> summable h \<and> range h \<subseteq> nonneg_Reals \<and> (\<forall>n y. N \<le> n \<and> y \<in> ball x d \<longrightarrow> cmod(f n y) \<le> cmod (h n))"
   1.274 +  shows "\<exists>g g'. \<forall>x \<in> s. ((\<lambda>n. f n x) sums g x) \<and> ((\<lambda>n. f' n x) sums g' x) \<and> (g has_field_derivative g' x) (at x)"
   1.275 +apply (rule series_and_derivative_comparison_local [OF s hfd], assumption)
   1.276 +apply (frule to_g)
   1.277 +apply (erule ex_forward)
   1.278 +apply (erule exE)
   1.279 +apply (rule_tac x="Re o h" in exI)
   1.280 +apply (erule ex_forward)
   1.281 +apply (simp add: summable_Re o_def )
   1.282 +apply (elim conjE all_forward)
   1.283 +apply (simp add: nonneg_Reals_cmod_eq_Re image_subset_iff)
   1.284 +done
   1.285 +
   1.286 +
   1.287 +text\<open>In particular, a power series is analytic inside circle of convergence.\<close>
   1.288 +
   1.289 +lemma power_series_and_derivative_0:
   1.290 +  fixes a :: "nat \<Rightarrow> complex" and r::real
   1.291 +  assumes "summable (\<lambda>n. a n * r^n)"
   1.292 +    shows "\<exists>g g'. \<forall>z. cmod z < r \<longrightarrow>
   1.293 +             ((\<lambda>n. a n * z^n) sums g z) \<and> ((\<lambda>n. of_nat n * a n * z^(n - 1)) sums g' z) \<and> (g has_field_derivative g' z) (at z)"
   1.294 +proof (cases "0 < r")
   1.295 +  case True
   1.296 +    have der: "\<And>n z. ((\<lambda>x. a n * x ^ n) has_field_derivative of_nat n * a n * z ^ (n - 1)) (at z)"
   1.297 +      by (rule derivative_eq_intros | simp)+
   1.298 +    have y_le: "\<lbrakk>cmod (z - y) * 2 < r - cmod z\<rbrakk> \<Longrightarrow> cmod y \<le> cmod (of_real r + of_real (cmod z)) / 2" for z y
   1.299 +      using \<open>r > 0\<close>
   1.300 +      apply (auto simp: algebra_simps norm_mult norm_divide norm_power of_real_add [symmetric] simp del: of_real_add)
   1.301 +      using norm_triangle_ineq2 [of y z]
   1.302 +      apply (simp only: diff_le_eq norm_minus_commute mult_2)
   1.303 +      done
   1.304 +    have "summable (\<lambda>n. a n * complex_of_real r ^ n)"
   1.305 +      using assms \<open>r > 0\<close> by simp
   1.306 +    moreover have "\<And>z. cmod z < r \<Longrightarrow> cmod ((of_real r + of_real (cmod z)) / 2) < cmod (of_real r)"
   1.307 +      using \<open>r > 0\<close>
   1.308 +      by (simp add: of_real_add [symmetric] del: of_real_add)
   1.309 +    ultimately have sum: "\<And>z. cmod z < r \<Longrightarrow> summable (\<lambda>n. of_real (cmod (a n)) * ((of_real r + complex_of_real (cmod z)) / 2) ^ n)"
   1.310 +      by (rule power_series_conv_imp_absconv_weak)
   1.311 +    have "\<exists>g g'. \<forall>z \<in> ball 0 r. (\<lambda>n.  (a n) * z ^ n) sums g z \<and>
   1.312 +               (\<lambda>n. of_nat n * (a n) * z ^ (n - 1)) sums g' z \<and> (g has_field_derivative g' z) (at z)"
   1.313 +      apply (rule series_and_derivative_comparison_complex [OF open_ball der])
   1.314 +      apply (rule_tac x="(r - norm z)/2" in exI)
   1.315 +      apply (simp add: dist_norm)
   1.316 +      apply (rule_tac x="\<lambda>n. of_real(norm(a n)*((r + norm z)/2)^n)" in exI)
   1.317 +      using \<open>r > 0\<close>
   1.318 +      apply (auto simp: sum nonneg_Reals_divide_I)
   1.319 +      apply (rule_tac x=0 in exI)
   1.320 +      apply (force simp: norm_mult norm_divide norm_power intro!: mult_left_mono power_mono y_le)
   1.321 +      done
   1.322 +  then show ?thesis
   1.323 +    by (simp add: dist_0_norm ball_def)
   1.324 +next
   1.325 +  case False then show ?thesis
   1.326 +    apply (simp add: not_less)
   1.327 +    using less_le_trans norm_not_less_zero by blast
   1.328 +qed
   1.329 +
   1.330 +proposition power_series_and_derivative:
   1.331 +  fixes a :: "nat \<Rightarrow> complex" and r::real
   1.332 +  assumes "summable (\<lambda>n. a n * r^n)"
   1.333 +    obtains g g' where "\<forall>z \<in> ball w r.
   1.334 +             ((\<lambda>n. a n * (z - w) ^ n) sums g z) \<and> ((\<lambda>n. of_nat n * a n * (z - w) ^ (n - 1)) sums g' z) \<and>
   1.335 +              (g has_field_derivative g' z) (at z)"
   1.336 +  using power_series_and_derivative_0 [OF assms]
   1.337 +  apply clarify
   1.338 +  apply (rule_tac g="(\<lambda>z. g(z - w))" in that)
   1.339 +  using DERIV_shift [where z="-w"] 
   1.340 +  apply (auto simp: norm_minus_commute Ball_def dist_norm)
   1.341 +  done
   1.342 +
   1.343 +proposition power_series_holomorphic:
   1.344 +  assumes "\<And>w. w \<in> ball z r \<Longrightarrow> ((\<lambda>n. a n*(w - z)^n) sums f w)"
   1.345 +    shows "f holomorphic_on ball z r"
   1.346 +proof -
   1.347 +  have "\<exists>f'. (f has_field_derivative f') (at w)" if w: "dist z w < r" for w
   1.348 +  proof -
   1.349 +    have inb: "z + complex_of_real ((dist z w + r) / 2) \<in> ball z r"
   1.350 +    proof -
   1.351 +      have wz: "cmod (w - z) < r" using w
   1.352 +        by (auto simp: divide_simps dist_norm norm_minus_commute)
   1.353 +      then have "0 \<le> r"
   1.354 +        by (meson less_eq_real_def norm_ge_zero order_trans)
   1.355 +      show ?thesis
   1.356 +        using w by (simp add: dist_norm \<open>0\<le>r\<close> of_real_add [symmetric] del: of_real_add)
   1.357 +    qed
   1.358 +    have sum: "summable (\<lambda>n. a n * of_real (((cmod (z - w) + r) / 2) ^ n))"
   1.359 +      using assms [OF inb] by (force simp add: summable_def dist_norm)
   1.360 +    obtain g g' where gg': "\<And>u. u \<in> ball z ((cmod (z - w) + r) / 2) \<Longrightarrow>
   1.361 +                               (\<lambda>n. a n * (u - z) ^ n) sums g u \<and>
   1.362 +                               (\<lambda>n. of_nat n * a n * (u - z) ^ (n - 1)) sums g' u \<and> (g has_field_derivative g' u) (at u)"
   1.363 +      by (rule power_series_and_derivative [OF sum, of z]) fastforce
   1.364 +    have [simp]: "g u = f u" if "cmod (u - w) < (r - cmod (z - w)) / 2" for u
   1.365 +    proof -
   1.366 +      have less: "cmod (z - u) * 2 < cmod (z - w) + r"
   1.367 +        using that dist_triangle2 [of z u w]
   1.368 +        by (simp add: dist_norm [symmetric] algebra_simps)
   1.369 +      show ?thesis
   1.370 +        apply (rule sums_unique2 [of "\<lambda>n. a n*(u - z)^n"])
   1.371 +        using gg' [of u] less w
   1.372 +        apply (auto simp: assms dist_norm)
   1.373 +        done
   1.374 +    qed
   1.375 +    show ?thesis
   1.376 +      apply (rule_tac x="g' w" in exI)
   1.377 +      apply (rule DERIV_transform_at [where f=g and d="(r - norm(z - w))/2"])
   1.378 +      using w gg' [of w]
   1.379 +      apply (auto simp: dist_norm)
   1.380 +      done
   1.381 +  qed
   1.382 +  then show ?thesis by (simp add: holomorphic_on_open)
   1.383 +qed
   1.384 +
   1.385 +corollary holomorphic_iff_power_series:
   1.386 +     "f holomorphic_on ball z r \<longleftrightarrow>
   1.387 +      (\<forall>w \<in> ball z r. (\<lambda>n. (deriv ^^ n) f z / (fact n) * (w - z)^n) sums f w)"
   1.388 +  apply (intro iffI ballI)
   1.389 +   using holomorphic_power_series  apply force
   1.390 +  apply (rule power_series_holomorphic [where a = "\<lambda>n. (deriv ^^ n) f z / (fact n)"])
   1.391 +  apply force
   1.392 +  done
   1.393 +
   1.394 +corollary power_series_analytic:
   1.395 +     "(\<And>w. w \<in> ball z r \<Longrightarrow> (\<lambda>n. a n*(w - z)^n) sums f w) \<Longrightarrow> f analytic_on ball z r"
   1.396 +  by (force simp add: analytic_on_open intro!: power_series_holomorphic)
   1.397 +
   1.398 +corollary analytic_iff_power_series:
   1.399 +     "f analytic_on ball z r \<longleftrightarrow>
   1.400 +      (\<forall>w \<in> ball z r. (\<lambda>n. (deriv ^^ n) f z / (fact n) * (w - z)^n) sums f w)"
   1.401 +  by (simp add: analytic_on_open holomorphic_iff_power_series)
   1.402 +
   1.403 +
   1.404 +subsection\<open>Equality between holomorphic functions, on open ball then connected set.\<close>
   1.405 +
   1.406 +lemma holomorphic_fun_eq_on_ball:
   1.407 +   "\<lbrakk>f holomorphic_on ball z r; g holomorphic_on ball z r;
   1.408 +     w \<in> ball z r;
   1.409 +     \<And>n. (deriv ^^ n) f z = (deriv ^^ n) g z\<rbrakk>
   1.410 +     \<Longrightarrow> f w = g w"
   1.411 +  apply (rule sums_unique2 [of "\<lambda>n. (deriv ^^ n) f z / (fact n) * (w - z)^n"])
   1.412 +  apply (auto simp: holomorphic_iff_power_series)
   1.413 +  done
   1.414 +
   1.415 +lemma holomorphic_fun_eq_0_on_ball:
   1.416 +   "\<lbrakk>f holomorphic_on ball z r;  w \<in> ball z r;
   1.417 +     \<And>n. (deriv ^^ n) f z = 0\<rbrakk>
   1.418 +     \<Longrightarrow> f w = 0"
   1.419 +  apply (rule sums_unique2 [of "\<lambda>n. (deriv ^^ n) f z / (fact n) * (w - z)^n"])
   1.420 +  apply (auto simp: holomorphic_iff_power_series)
   1.421 +  done
   1.422 +
   1.423 +lemma holomorphic_fun_eq_0_on_connected:
   1.424 +  assumes holf: "f holomorphic_on s" and "open s"
   1.425 +      and cons: "connected s"
   1.426 +      and der: "\<And>n. (deriv ^^ n) f z = 0"
   1.427 +      and "z \<in> s" "w \<in> s"
   1.428 +    shows "f w = 0"
   1.429 +proof -
   1.430 +  have *: "\<And>x e. \<lbrakk> \<forall>xa. (deriv ^^ xa) f x = 0;  ball x e \<subseteq> s\<rbrakk>
   1.431 +           \<Longrightarrow> ball x e \<subseteq> (\<Inter>n. {w \<in> s. (deriv ^^ n) f w = 0})"
   1.432 +    apply auto
   1.433 +    apply (rule holomorphic_fun_eq_0_on_ball [OF holomorphic_higher_deriv])
   1.434 +    apply (rule holomorphic_on_subset [OF holf], simp_all)
   1.435 +    by (metis funpow_add o_apply)
   1.436 +  have 1: "openin (subtopology euclidean s) (\<Inter>n. {w \<in> s. (deriv ^^ n) f w = 0})"
   1.437 +    apply (rule open_subset, force)
   1.438 +    using \<open>open s\<close>
   1.439 +    apply (simp add: open_contains_ball Ball_def)
   1.440 +    apply (erule all_forward)
   1.441 +    using "*" by blast
   1.442 +  have 2: "closedin (subtopology euclidean s) (\<Inter>n. {w \<in> s. (deriv ^^ n) f w = 0})"
   1.443 +    using assms
   1.444 +    by (auto intro: continuous_closed_in_preimage_constant holomorphic_on_imp_continuous_on holomorphic_higher_deriv)
   1.445 +  obtain e where "e>0" and e: "ball w e \<subseteq> s" using openE [OF \<open>open s\<close> \<open>w \<in> s\<close>] .
   1.446 +  then have holfb: "f holomorphic_on ball w e"
   1.447 +    using holf holomorphic_on_subset by blast
   1.448 +  have 3: "(\<Inter>n. {w \<in> s. (deriv ^^ n) f w = 0}) = s \<Longrightarrow> f w = 0"
   1.449 +    using \<open>e>0\<close> e by (force intro: holomorphic_fun_eq_0_on_ball [OF holfb])
   1.450 +  show ?thesis
   1.451 +    using cons der \<open>z \<in> s\<close>
   1.452 +    apply (simp add: connected_clopen)
   1.453 +    apply (drule_tac x="\<Inter>n. {w \<in> s. (deriv ^^ n) f w = 0}" in spec)
   1.454 +    apply (auto simp: 1 2 3)
   1.455 +    done
   1.456 +qed
   1.457 +
   1.458 +lemma holomorphic_fun_eq_on_connected:
   1.459 +  assumes "f holomorphic_on s" "g holomorphic_on s" and "open s"  "connected s"
   1.460 +      and "\<And>n. (deriv ^^ n) f z = (deriv ^^ n) g z"
   1.461 +      and "z \<in> s" "w \<in> s"
   1.462 +    shows "f w = g w"
   1.463 +  apply (rule holomorphic_fun_eq_0_on_connected [of "\<lambda>x. f x - g x" s z, simplified])
   1.464 +  apply (intro assms holomorphic_intros)
   1.465 +  using assms apply simp_all
   1.466 +  apply (subst higher_deriv_diff, auto)
   1.467 +  done
   1.468 +
   1.469 +lemma holomorphic_fun_eq_const_on_connected:
   1.470 +  assumes holf: "f holomorphic_on s" and "open s"
   1.471 +      and cons: "connected s"
   1.472 +      and der: "\<And>n. 0 < n \<Longrightarrow> (deriv ^^ n) f z = 0"
   1.473 +      and "z \<in> s" "w \<in> s"
   1.474 +    shows "f w = f z"
   1.475 +  apply (rule holomorphic_fun_eq_0_on_connected [of "\<lambda>w. f w - f z" s z, simplified])
   1.476 +  apply (intro assms holomorphic_intros)
   1.477 +  using assms apply simp_all
   1.478 +  apply (subst higher_deriv_diff)
   1.479 +  apply (intro holomorphic_intros | simp)+
   1.480 +  done
   1.481 +
   1.482 +
   1.483 +subsection\<open>Some basic lemmas about poles/singularities.\<close>
   1.484 +
   1.485 +lemma pole_lemma:
   1.486 +  assumes holf: "f holomorphic_on s" and a: "a \<in> interior s"
   1.487 +    shows "(\<lambda>z. if z = a then deriv f a
   1.488 +                 else (f z - f a) / (z - a)) holomorphic_on s" (is "?F holomorphic_on s")
   1.489 +proof -
   1.490 +  have F1: "?F complex_differentiable (at u within s)" if "u \<in> s" "u \<noteq> a" for u
   1.491 +  proof -
   1.492 +    have fcd: "f complex_differentiable at u within s"
   1.493 +      using holf holomorphic_on_def by (simp add: \<open>u \<in> s\<close>)
   1.494 +    have cd: "(\<lambda>z. (f z - f a) / (z - a)) complex_differentiable at u within s"
   1.495 +      by (rule fcd derivative_intros | simp add: that)+
   1.496 +    have "0 < dist a u" using that dist_nz by blast
   1.497 +    then show ?thesis
   1.498 +      by (rule complex_differentiable_transform_within [OF _ _ _ cd]) (auto simp: \<open>u \<in> s\<close>)
   1.499 +  qed
   1.500 +  have F2: "?F complex_differentiable at a" if "0 < e" "ball a e \<subseteq> s" for e
   1.501 +  proof -
   1.502 +    have holfb: "f holomorphic_on ball a e"
   1.503 +      by (rule holomorphic_on_subset [OF holf \<open>ball a e \<subseteq> s\<close>])
   1.504 +    have 2: "?F holomorphic_on ball a e - {a}"
   1.505 +      apply (rule holomorphic_on_subset [where s = "s - {a}"])
   1.506 +      apply (simp add: holomorphic_on_def complex_differentiable_def [symmetric])
   1.507 +      using mem_ball that
   1.508 +      apply (auto intro: F1 complex_differentiable_within_subset)
   1.509 +      done
   1.510 +    have "isCont (\<lambda>z. if z = a then deriv f a else (f z - f a) / (z - a)) x"
   1.511 +            if "dist a x < e" for x
   1.512 +    proof (cases "x=a")
   1.513 +      case True then show ?thesis
   1.514 +      using holfb \<open>0 < e\<close>
   1.515 +      apply (simp add: holomorphic_on_open complex_differentiable_def [symmetric])
   1.516 +      apply (drule_tac x=a in bspec)
   1.517 +      apply (auto simp: DERIV_deriv_iff_complex_differentiable [symmetric] continuous_at DERIV_iff2
   1.518 +                elim: rev_iffD1 [OF _ LIM_equal])
   1.519 +      done
   1.520 +    next
   1.521 +      case False with 2 that show ?thesis
   1.522 +        by (force simp: holomorphic_on_open open_Diff complex_differentiable_def [symmetric] complex_differentiable_imp_continuous_at)
   1.523 +    qed
   1.524 +    then have 1: "continuous_on (ball a e) ?F"
   1.525 +      by (clarsimp simp:  continuous_on_eq_continuous_at)
   1.526 +    have "?F holomorphic_on ball a e"
   1.527 +      by (auto intro: no_isolated_singularity [OF 1 2])
   1.528 +    with that show ?thesis
   1.529 +      by (simp add: holomorphic_on_open complex_differentiable_def [symmetric]
   1.530 +                    complex_differentiable_at_within)
   1.531 +  qed
   1.532 +  show ?thesis
   1.533 +  proof
   1.534 +    fix x assume "x \<in> s" show "?F complex_differentiable at x within s"
   1.535 +    proof (cases "x=a")
   1.536 +      case True then show ?thesis
   1.537 +      using a by (auto simp: mem_interior intro: complex_differentiable_at_within F2)
   1.538 +    next
   1.539 +      case False with F1 \<open>x \<in> s\<close>
   1.540 +      show ?thesis by blast
   1.541 +    qed
   1.542 +  qed
   1.543 +qed
   1.544 +
   1.545 +proposition pole_theorem:
   1.546 +  assumes holg: "g holomorphic_on s" and a: "a \<in> interior s"
   1.547 +      and eq: "\<And>z. z \<in> s - {a} \<Longrightarrow> g z = (z - a) * f z"
   1.548 +    shows "(\<lambda>z. if z = a then deriv g a
   1.549 +                 else f z - g a/(z - a)) holomorphic_on s"
   1.550 +  using pole_lemma [OF holg a]
   1.551 +  by (rule holomorphic_transform) (simp add: eq divide_simps)
   1.552 +
   1.553 +lemma pole_lemma_open:
   1.554 +  assumes "f holomorphic_on s" "open s"
   1.555 +    shows "(\<lambda>z. if z = a then deriv f a else (f z - f a)/(z - a)) holomorphic_on s"
   1.556 +proof (cases "a \<in> s")
   1.557 +  case True with assms interior_eq pole_lemma
   1.558 +    show ?thesis by fastforce
   1.559 +next
   1.560 +  case False with assms show ?thesis
   1.561 +    apply (simp add: holomorphic_on_def complex_differentiable_def [symmetric], clarify)
   1.562 +    apply (rule complex_differentiable_transform_within [where f = "\<lambda>z. (f z - f a)/(z - a)" and d = 1])
   1.563 +    apply (rule derivative_intros | force)+
   1.564 +    done
   1.565 +qed
   1.566 +
   1.567 +proposition pole_theorem_open:
   1.568 +  assumes holg: "g holomorphic_on s" and s: "open s"
   1.569 +      and eq: "\<And>z. z \<in> s - {a} \<Longrightarrow> g z = (z - a) * f z"
   1.570 +    shows "(\<lambda>z. if z = a then deriv g a
   1.571 +                 else f z - g a/(z - a)) holomorphic_on s"
   1.572 +  using pole_lemma_open [OF holg s]
   1.573 +  by (rule holomorphic_transform) (auto simp: eq divide_simps)
   1.574 +
   1.575 +proposition pole_theorem_0:
   1.576 +  assumes holg: "g holomorphic_on s" and a: "a \<in> interior s"
   1.577 +      and eq: "\<And>z. z \<in> s - {a} \<Longrightarrow> g z = (z - a) * f z"
   1.578 +      and [simp]: "f a = deriv g a" "g a = 0"
   1.579 +    shows "f holomorphic_on s"
   1.580 +  using pole_theorem [OF holg a eq]
   1.581 +  by (rule holomorphic_transform) (auto simp: eq divide_simps)
   1.582 +
   1.583 +proposition pole_theorem_open_0:
   1.584 +  assumes holg: "g holomorphic_on s" and s: "open s"
   1.585 +      and eq: "\<And>z. z \<in> s - {a} \<Longrightarrow> g z = (z - a) * f z"
   1.586 +      and [simp]: "f a = deriv g a" "g a = 0"
   1.587 +    shows "f holomorphic_on s"
   1.588 +  using pole_theorem_open [OF holg s eq]
   1.589 +  by (rule holomorphic_transform) (auto simp: eq divide_simps)
   1.590 +
   1.591 +lemma pole_theorem_analytic:
   1.592 +  assumes g: "g analytic_on s"
   1.593 +      and eq: "\<And>z. z \<in> s
   1.594 +             \<Longrightarrow> \<exists>d. 0 < d \<and> (\<forall>w \<in> ball z d - {a}. g w = (w - a) * f w)"
   1.595 +    shows "(\<lambda>z. if z = a then deriv g a
   1.596 +                 else f z - g a/(z - a)) analytic_on s"
   1.597 +using g
   1.598 +apply (simp add: analytic_on_def Ball_def)
   1.599 +apply (safe elim!: all_forward dest!: eq)
   1.600 +apply (rule_tac x="min d e" in exI, simp)
   1.601 +apply (rule pole_theorem_open)
   1.602 +apply (auto simp: holomorphic_on_subset subset_ball)
   1.603 +done
   1.604 +
   1.605 +lemma pole_theorem_analytic_0:
   1.606 +  assumes g: "g analytic_on s"
   1.607 +      and eq: "\<And>z. z \<in> s \<Longrightarrow> \<exists>d. 0 < d \<and> (\<forall>w \<in> ball z d - {a}. g w = (w - a) * f w)"
   1.608 +      and [simp]: "f a = deriv g a" "g a = 0"
   1.609 +    shows "f analytic_on s"
   1.610 +proof -
   1.611 +  have [simp]: "(\<lambda>z. if z = a then deriv g a else f z - g a / (z - a)) = f"
   1.612 +    by auto
   1.613 +  show ?thesis
   1.614 +    using pole_theorem_analytic [OF g eq] by simp
   1.615 +qed
   1.616 +
   1.617 +lemma pole_theorem_analytic_open_superset:
   1.618 +  assumes g: "g analytic_on s" and "s \<subseteq> t" "open t"
   1.619 +      and eq: "\<And>z. z \<in> t - {a} \<Longrightarrow> g z = (z - a) * f z"
   1.620 +    shows "(\<lambda>z. if z = a then deriv g a
   1.621 +                 else f z - g a/(z - a)) analytic_on s"
   1.622 +  apply (rule pole_theorem_analytic [OF g])
   1.623 +  apply (rule openE [OF \<open>open t\<close>])
   1.624 +  using assms eq by auto
   1.625 +
   1.626 +lemma pole_theorem_analytic_open_superset_0:
   1.627 +  assumes g: "g analytic_on s" "s \<subseteq> t" "open t" "\<And>z. z \<in> t - {a} \<Longrightarrow> g z = (z - a) * f z"
   1.628 +      and [simp]: "f a = deriv g a" "g a = 0"
   1.629 +    shows "f analytic_on s"
   1.630 +proof -
   1.631 +  have [simp]: "(\<lambda>z. if z = a then deriv g a else f z - g a / (z - a)) = f"
   1.632 +    by auto
   1.633 +  have "(\<lambda>z. if z = a then deriv g a else f z - g a/(z - a)) analytic_on s"
   1.634 +    by (rule pole_theorem_analytic_open_superset [OF g])
   1.635 +  then show ?thesis by simp
   1.636 +qed
   1.637 +
   1.638 +
   1.639 +
   1.640 +subsection\<open>General, homology form of Cauchy's theorem.\<close>
   1.641 +
   1.642 +text\<open>Proof is based on Dixon's, as presented in Lang's "Complex Analysis" book (page 147).\<close>
   1.643 +
   1.644 +text\<open>This version has @{term"polynomial_function \<gamma>"} as an additional assumption.\<close>
   1.645 +lemma Cauchy_integral_formula_global_weak:
   1.646 +    assumes u: "open u" and holf: "f holomorphic_on u"
   1.647 +        and z: "z \<in> u" and \<gamma>: "polynomial_function \<gamma>"
   1.648 +        and pasz: "path_image \<gamma> \<subseteq> u - {z}" and loop: "pathfinish \<gamma> = pathstart \<gamma>"
   1.649 +        and zero: "\<And>w. w \<notin> u \<Longrightarrow> winding_number \<gamma> w = 0"
   1.650 +      shows "((\<lambda>w. f w / (w - z)) has_contour_integral (2*pi * ii * winding_number \<gamma> z * f z)) \<gamma>"
   1.651 +proof -
   1.652 +  obtain \<gamma>' where pf\<gamma>': "polynomial_function \<gamma>'" and \<gamma>': "\<And>x. (\<gamma> has_vector_derivative (\<gamma>' x)) (at x)"
   1.653 +    using has_vector_derivative_polynomial_function [OF \<gamma>] by blast
   1.654 +  then have "bounded(path_image \<gamma>')"
   1.655 +    by (simp add: path_image_def compact_imp_bounded compact_continuous_image continuous_on_polymonial_function)
   1.656 +  then obtain B where "B>0" and B: "\<And>x. x \<in> path_image \<gamma>' \<Longrightarrow> norm x \<le> B"
   1.657 +    using bounded_pos by force
   1.658 +  def d \<equiv> "\<lambda>z w. if w = z then deriv f z else (f w - f z)/(w - z)"
   1.659 +  def v \<equiv> "{w. w \<notin> path_image \<gamma> \<and> winding_number \<gamma> w = 0}"
   1.660 +  have "path \<gamma>" "valid_path \<gamma>" using \<gamma>
   1.661 +    by (auto simp: path_polynomial_function valid_path_polynomial_function)
   1.662 +  then have ov: "open v"
   1.663 +    by (simp add: v_def open_winding_number_levelsets loop)
   1.664 +  have uv_Un: "u \<union> v = UNIV"
   1.665 +    using pasz zero by (auto simp: v_def)
   1.666 +  have conf: "continuous_on u f"
   1.667 +    by (metis holf holomorphic_on_imp_continuous_on)
   1.668 +  have hol_d: "(d y) holomorphic_on u" if "y \<in> u" for y
   1.669 +  proof -
   1.670 +    have *: "(\<lambda>c. if c = y then deriv f y else (f c - f y) / (c - y)) holomorphic_on u"
   1.671 +      by (simp add: holf pole_lemma_open u)
   1.672 +    then have "isCont (\<lambda>x. if x = y then deriv f y else (f x - f y) / (x - y)) y"
   1.673 +      using at_within_open complex_differentiable_imp_continuous_at holomorphic_on_def that u by fastforce
   1.674 +    then have "continuous_on u (d y)"
   1.675 +      apply (simp add: d_def continuous_on_eq_continuous_at u, clarify)
   1.676 +      using * holomorphic_on_def
   1.677 +      by (meson complex_differentiable_within_open complex_differentiable_imp_continuous_at u)
   1.678 +    moreover have "d y holomorphic_on u - {y}"
   1.679 +      apply (simp add: d_def holomorphic_on_open u open_delete complex_differentiable_def [symmetric])
   1.680 +      apply (intro ballI)
   1.681 +      apply (rename_tac w)
   1.682 +      apply (rule_tac d="dist w y" and f = "\<lambda>w. (f w - f y)/(w - y)" in complex_differentiable_transform_within)
   1.683 +      apply (auto simp: dist_pos_lt dist_commute intro!: derivative_intros)
   1.684 +      using analytic_on_imp_differentiable_at analytic_on_open holf u apply blast
   1.685 +      done
   1.686 +    ultimately show ?thesis
   1.687 +      by (rule no_isolated_singularity) (auto simp: u)
   1.688 +  qed
   1.689 +  have cint_fxy: "(\<lambda>x. (f x - f y) / (x - y)) contour_integrable_on \<gamma>" if "y \<notin> path_image \<gamma>" for y
   1.690 +    apply (rule contour_integrable_holomorphic_simple [where s = "u-{y}"])
   1.691 +    using \<open>valid_path \<gamma>\<close> pasz
   1.692 +    apply (auto simp: u open_delete)
   1.693 +    apply (rule continuous_intros holomorphic_intros continuous_on_subset [OF conf] holomorphic_on_subset [OF holf] |
   1.694 +                force simp add: that)+
   1.695 +    done
   1.696 +  def h \<equiv> "\<lambda>z. if z \<in> u then contour_integral \<gamma> (d z) else contour_integral \<gamma> (\<lambda>w. f w/(w - z))"
   1.697 +  have U: "\<And>z. z \<in> u \<Longrightarrow> ((d z) has_contour_integral h z) \<gamma>"
   1.698 +    apply (simp add: h_def)
   1.699 +    apply (rule has_contour_integral_integral [OF contour_integrable_holomorphic_simple [where s=u]])
   1.700 +    using u pasz \<open>valid_path \<gamma>\<close>
   1.701 +    apply (auto intro: holomorphic_on_imp_continuous_on hol_d)
   1.702 +    done
   1.703 +  have V: "((\<lambda>w. f w / (w - z)) has_contour_integral h z) \<gamma>" if z: "z \<in> v" for z
   1.704 +  proof -
   1.705 +    have 0: "0 = (f z) * 2 * of_real (2 * pi) * \<i> * winding_number \<gamma> z"
   1.706 +      using v_def z by auto
   1.707 +    then have "((\<lambda>x. 1 / (x - z)) has_contour_integral 0) \<gamma>"
   1.708 +     using z v_def  has_contour_integral_winding_number [OF \<open>valid_path \<gamma>\<close>] by fastforce
   1.709 +    then have "((\<lambda>x. f z * (1 / (x - z))) has_contour_integral 0) \<gamma>"
   1.710 +      using has_contour_integral_lmul by fastforce
   1.711 +    then have "((\<lambda>x. f z / (x - z)) has_contour_integral 0) \<gamma>"
   1.712 +      by (simp add: divide_simps)
   1.713 +    moreover have "((\<lambda>x. (f x - f z) / (x - z)) has_contour_integral contour_integral \<gamma> (d z)) \<gamma>"
   1.714 +      using z
   1.715 +      apply (auto simp: v_def)
   1.716 +      apply (metis (no_types, lifting) contour_integrable_eq d_def has_contour_integral_eq has_contour_integral_integral cint_fxy)
   1.717 +      done
   1.718 +    ultimately have *: "((\<lambda>x. f z / (x - z) + (f x - f z) / (x - z)) has_contour_integral (0 + contour_integral \<gamma> (d z))) \<gamma>"
   1.719 +      by (rule has_contour_integral_add)
   1.720 +    have "((\<lambda>w. f w / (w - z)) has_contour_integral contour_integral \<gamma> (d z)) \<gamma>"
   1.721 +            if  "z \<in> u"
   1.722 +      using * by (auto simp: divide_simps has_contour_integral_eq)
   1.723 +    moreover have "((\<lambda>w. f w / (w - z)) has_contour_integral contour_integral \<gamma> (\<lambda>w. f w / (w - z))) \<gamma>"
   1.724 +            if "z \<notin> u"
   1.725 +      apply (rule has_contour_integral_integral [OF contour_integrable_holomorphic_simple [where s=u]])
   1.726 +      using u pasz \<open>valid_path \<gamma>\<close> that
   1.727 +      apply (auto intro: holomorphic_on_imp_continuous_on hol_d)
   1.728 +      apply (rule continuous_intros conf holomorphic_intros holf | force)+
   1.729 +      done
   1.730 +    ultimately show ?thesis
   1.731 +      using z by (simp add: h_def)
   1.732 +  qed
   1.733 +  have znot: "z \<notin> path_image \<gamma>"
   1.734 +    using pasz by blast
   1.735 +  obtain d0 where "d0>0" and d0: "\<And>x y. x \<in> path_image \<gamma> \<Longrightarrow> y \<in> - u \<Longrightarrow> d0 \<le> dist x y"
   1.736 +    using separate_compact_closed [of "path_image \<gamma>" "-u"] pasz u
   1.737 +    by (fastforce simp add: \<open>path \<gamma>\<close> compact_path_image)
   1.738 +  obtain dd where "0 < dd" and dd: "{y + k | y k. y \<in> path_image \<gamma> \<and> k \<in> ball 0 dd} \<subseteq> u"
   1.739 +    apply (rule that [of "d0/2"])
   1.740 +    using \<open>0 < d0\<close>
   1.741 +    apply (auto simp: dist_norm dest: d0)
   1.742 +    done
   1.743 +  have "\<And>x x'. \<lbrakk>x \<in> path_image \<gamma>; dist x x' * 2 < dd\<rbrakk> \<Longrightarrow> \<exists>y k. x' = y + k \<and> y \<in> path_image \<gamma> \<and> dist 0 k * 2 \<le> dd"
   1.744 +    apply (rule_tac x=x in exI)
   1.745 +    apply (rule_tac x="x'-x" in exI)
   1.746 +    apply (force simp add: dist_norm)
   1.747 +    done
   1.748 +  then have 1: "path_image \<gamma> \<subseteq> interior {y + k |y k. y \<in> path_image \<gamma> \<and> k \<in> cball 0 (dd / 2)}"
   1.749 +    apply (clarsimp simp add: mem_interior)
   1.750 +    using \<open>0 < dd\<close>
   1.751 +    apply (rule_tac x="dd/2" in exI, auto)
   1.752 +    done
   1.753 +  obtain t where "compact t" and subt: "path_image \<gamma> \<subseteq> interior t" and t: "t \<subseteq> u"
   1.754 +    apply (rule that [OF _ 1])
   1.755 +    apply (fastforce simp add: \<open>valid_path \<gamma>\<close> compact_valid_path_image intro!: compact_sums)
   1.756 +    apply (rule order_trans [OF _ dd])
   1.757 +    using \<open>0 < dd\<close> by fastforce
   1.758 +  obtain L where "L>0"
   1.759 +           and L: "\<And>f B. \<lbrakk>f holomorphic_on interior t; \<And>z. z\<in>interior t \<Longrightarrow> cmod (f z) \<le> B\<rbrakk> \<Longrightarrow>
   1.760 +                         cmod (contour_integral \<gamma> f) \<le> L * B"
   1.761 +      using contour_integral_bound_exists [OF open_interior \<open>valid_path \<gamma>\<close> subt]
   1.762 +      by blast
   1.763 +  have "bounded(f ` t)"
   1.764 +    by (meson \<open>compact t\<close> compact_continuous_image compact_imp_bounded conf continuous_on_subset t)
   1.765 +  then obtain D where "D>0" and D: "\<And>x. x \<in> t \<Longrightarrow> norm (f x) \<le> D"
   1.766 +    by (auto simp: bounded_pos)
   1.767 +  obtain C where "C>0" and C: "\<And>x. x \<in> t \<Longrightarrow> norm x \<le> C"
   1.768 +    using \<open>compact t\<close> bounded_pos compact_imp_bounded by force
   1.769 +  have "dist (h y) 0 \<le> e" if "0 < e" and le: "D * L / e + C \<le> cmod y" for e y
   1.770 +  proof -
   1.771 +    have "D * L / e > 0"  using \<open>D>0\<close> \<open>L>0\<close> \<open>e>0\<close> by simp
   1.772 +    with le have ybig: "norm y > C" by force
   1.773 +    with C have "y \<notin> t"  by force
   1.774 +    then have ynot: "y \<notin> path_image \<gamma>"
   1.775 +      using subt interior_subset by blast
   1.776 +    have [simp]: "winding_number \<gamma> y = 0"
   1.777 +      apply (rule winding_number_zero_outside [of _ "cball 0 C"])
   1.778 +      using ybig interior_subset subt
   1.779 +      apply (force simp add: loop \<open>path \<gamma>\<close> dist_norm intro!: C)+
   1.780 +      done
   1.781 +    have [simp]: "h y = contour_integral \<gamma> (\<lambda>w. f w/(w - y))"
   1.782 +      by (rule contour_integral_unique [symmetric]) (simp add: v_def ynot V)
   1.783 +    have holint: "(\<lambda>w. f w / (w - y)) holomorphic_on interior t"
   1.784 +      apply (rule holomorphic_on_divide)
   1.785 +      using holf holomorphic_on_subset interior_subset t apply blast
   1.786 +      apply (rule holomorphic_intros)+
   1.787 +      using \<open>y \<notin> t\<close> interior_subset by auto
   1.788 +    have leD: "cmod (f z / (z - y)) \<le> D * (e / L / D)" if z: "z \<in> interior t" for z
   1.789 +    proof -
   1.790 +      have "D * L / e + cmod z \<le> cmod y"
   1.791 +        using le C [of z] z using interior_subset by force
   1.792 +      then have DL2: "D * L / e \<le> cmod (z - y)"
   1.793 +        using norm_triangle_ineq2 [of y z] by (simp add: norm_minus_commute)
   1.794 +      have "cmod (f z / (z - y)) = cmod (f z) * inverse (cmod (z - y))"
   1.795 +        by (simp add: norm_mult norm_inverse Fields.field_class.field_divide_inverse)
   1.796 +      also have "... \<le> D * (e / L / D)"
   1.797 +        apply (rule mult_mono)
   1.798 +        using that D interior_subset apply blast
   1.799 +        using \<open>L>0\<close> \<open>e>0\<close> \<open>D>0\<close> DL2
   1.800 +        apply (auto simp: norm_divide divide_simps algebra_simps)
   1.801 +        done
   1.802 +      finally show ?thesis .
   1.803 +    qed
   1.804 +    have "dist (h y) 0 = cmod (contour_integral \<gamma> (\<lambda>w. f w / (w - y)))"
   1.805 +      by (simp add: dist_norm)
   1.806 +    also have "... \<le> L * (D * (e / L / D))"
   1.807 +      by (rule L [OF holint leD])
   1.808 +    also have "... = e"
   1.809 +      using  \<open>L>0\<close> \<open>0 < D\<close> by auto
   1.810 +    finally show ?thesis .
   1.811 +  qed
   1.812 +  then have "(h \<longlongrightarrow> 0) at_infinity"
   1.813 +    by (meson Lim_at_infinityI)
   1.814 +  moreover have "h holomorphic_on UNIV"
   1.815 +  proof -
   1.816 +    have con_ff: "continuous (at (x,z)) (\<lambda>y. (f(snd y) - f(fst y)) / (snd y - fst y))"
   1.817 +                 if "x \<in> u" "z \<in> u" "x \<noteq> z" for x z
   1.818 +      using that conf
   1.819 +      apply (simp add: continuous_on_eq_continuous_at u)
   1.820 +      apply (simp | rule continuous_intros continuous_within_compose2 [where g=f])+
   1.821 +      done
   1.822 +    have con_fstsnd: "continuous_on UNIV (\<lambda>x. (fst x - snd x) ::complex)"
   1.823 +      by (rule continuous_intros)+
   1.824 +    have open_uu_Id: "open (u \<times> u - Id)"
   1.825 +      apply (rule open_Diff)
   1.826 +      apply (simp add: open_Times u)
   1.827 +      using continuous_closed_preimage_constant [OF con_fstsnd closed_UNIV, of 0]
   1.828 +      apply (auto simp: Id_fstsnd_eq algebra_simps)
   1.829 +      done
   1.830 +    have con_derf: "continuous (at z) (deriv f)" if "z \<in> u" for z
   1.831 +      apply (rule continuous_on_interior [of u])
   1.832 +      apply (simp add: holf holomorphic_deriv holomorphic_on_imp_continuous_on u)
   1.833 +      by (simp add: interior_open that u)
   1.834 +    have tendsto_f': "((\<lambda>x. if snd x = fst x then deriv f (fst x)
   1.835 +                                    else (f (snd x) - f (fst x)) / (snd x - fst x)) \<longlongrightarrow> deriv f x)
   1.836 +                      (at (x, x) within u \<times> u)" if "x \<in> u" for x
   1.837 +    proof (rule Lim_withinI)
   1.838 +      fix e::real assume "0 < e"
   1.839 +      obtain k1 where "k1>0" and k1: "\<And>x'. norm (x' - x) \<le> k1 \<Longrightarrow> norm (deriv f x' - deriv f x) < e"
   1.840 +        using \<open>0 < e\<close> continuous_within_E [OF con_derf [OF \<open>x \<in> u\<close>]]
   1.841 +        by (metis UNIV_I dist_norm)
   1.842 +      obtain k2 where "k2>0" and k2: "ball x k2 \<subseteq> u" by (blast intro: openE [OF u] \<open>x \<in> u\<close>)
   1.843 +      have neq: "norm ((f z' - f x') / (z' - x') - deriv f x) \<le> e"
   1.844 +                    if "z' \<noteq> x'" and less_k1: "norm (x'-x, z'-x) < k1" and less_k2: "norm (x'-x, z'-x) < k2"
   1.845 +                 for x' z'
   1.846 +      proof -
   1.847 +        have cs_less: "w \<in> closed_segment x' z' \<Longrightarrow> cmod (w - x) \<le> norm (x'-x, z'-x)" for w
   1.848 +          apply (drule segment_furthest_le [where y=x])
   1.849 +          by (metis (no_types) dist_commute dist_norm norm_fst_le norm_snd_le order_trans)
   1.850 +        have derf_le: "w \<in> closed_segment x' z' \<Longrightarrow> z' \<noteq> x' \<Longrightarrow> cmod (deriv f w - deriv f x) \<le> e" for w
   1.851 +          by (blast intro: cs_less less_k1 k1 [unfolded divide_const_simps dist_norm] less_imp_le le_less_trans)
   1.852 +        have f_has_der: "\<And>x. x \<in> u \<Longrightarrow> (f has_field_derivative deriv f x) (at x within u)"
   1.853 +          by (metis DERIV_deriv_iff_complex_differentiable at_within_open holf holomorphic_on_def u)
   1.854 +        have "closed_segment x' z' \<subseteq> u"
   1.855 +          by (rule order_trans [OF _ k2]) (simp add: cs_less  le_less_trans [OF _ less_k2] dist_complex_def norm_minus_commute subset_iff)
   1.856 +        then have cint_derf: "(deriv f has_contour_integral f z' - f x') (linepath x' z')"
   1.857 +          using contour_integral_primitive [OF f_has_der valid_path_linepath] pasz  by simp
   1.858 +        then have *: "((\<lambda>x. deriv f x / (z' - x')) has_contour_integral (f z' - f x') / (z' - x')) (linepath x' z')"
   1.859 +          by (rule has_contour_integral_div)
   1.860 +        have "norm ((f z' - f x') / (z' - x') - deriv f x) \<le> e/norm(z' - x') * norm(z' - x')"
   1.861 +          apply (rule has_contour_integral_bound_linepath [OF has_contour_integral_diff [OF *]])
   1.862 +          using has_contour_integral_div [where c = "z' - x'", OF has_contour_integral_const_linepath [of "deriv f x" z' x']]
   1.863 +                 \<open>e > 0\<close>  \<open>z' \<noteq> x'\<close>
   1.864 +          apply (auto simp: norm_divide divide_simps derf_le)
   1.865 +          done
   1.866 +        also have "... \<le> e" using \<open>0 < e\<close> by simp
   1.867 +        finally show ?thesis .
   1.868 +      qed
   1.869 +      show "\<exists>d>0. \<forall>xa\<in>u \<times> u.
   1.870 +                  0 < dist xa (x, x) \<and> dist xa (x, x) < d \<longrightarrow>
   1.871 +                  dist (if snd xa = fst xa then deriv f (fst xa) else (f (snd xa) - f (fst xa)) / (snd xa - fst xa))
   1.872 +                       (deriv f x)  \<le>  e"
   1.873 +        apply (rule_tac x="min k1 k2" in exI)
   1.874 +        using \<open>k1>0\<close> \<open>k2>0\<close> \<open>e>0\<close>
   1.875 +        apply (force simp: dist_norm neq intro: dual_order.strict_trans2 k1 less_imp_le norm_fst_le)
   1.876 +        done
   1.877 +    qed
   1.878 +    have con_pa_f: "continuous_on (path_image \<gamma>) f"
   1.879 +      by (meson holf holomorphic_on_imp_continuous_on holomorphic_on_subset interior_subset subt t)
   1.880 +    have le_B: "\<And>t. t \<in> {0..1} \<Longrightarrow> cmod (vector_derivative \<gamma> (at t)) \<le> B"
   1.881 +      apply (rule B)
   1.882 +      using \<gamma>' using path_image_def vector_derivative_at by fastforce
   1.883 +    have f_has_cint: "\<And>w. w \<in> v - path_image \<gamma> \<Longrightarrow> ((\<lambda>u. f u / (u - w) ^ 1) has_contour_integral h w) \<gamma>"
   1.884 +      by (simp add: V)
   1.885 +    have cond_uu: "continuous_on (u \<times> u) (\<lambda>y. d (fst y) (snd y))"
   1.886 +      apply (simp add: continuous_on_eq_continuous_within d_def continuous_within tendsto_f')
   1.887 +      apply (simp add: Lim_within_open_NO_MATCH open_Times u, clarify)
   1.888 +      apply (rule Lim_transform_within_open [OF _ open_uu_Id, where f = "(\<lambda>x. (f (snd x) - f (fst x)) / (snd x - fst x))"])
   1.889 +      using con_ff
   1.890 +      apply (auto simp: continuous_within)
   1.891 +      done
   1.892 +    have hol_dw: "(\<lambda>z. d z w) holomorphic_on u" if "w \<in> u" for w
   1.893 +    proof -
   1.894 +      have "continuous_on u ((\<lambda>y. d (fst y) (snd y)) o (\<lambda>z. (w,z)))"
   1.895 +        by (rule continuous_on_compose continuous_intros continuous_on_subset [OF cond_uu] | force intro: that)+
   1.896 +      then have *: "continuous_on u (\<lambda>z. if w = z then deriv f z else (f w - f z) / (w - z))"
   1.897 +        by (rule rev_iffD1 [OF _ continuous_on_cong [OF refl]]) (simp add: d_def field_simps)
   1.898 +      have **: "\<And>x. \<lbrakk>x \<in> u; x \<noteq> w\<rbrakk> \<Longrightarrow> (\<lambda>z. if w = z then deriv f z else (f w - f z) / (w - z)) complex_differentiable at x"
   1.899 +        apply (rule_tac f = "\<lambda>x. (f w - f x)/(w - x)" and d = "dist x w" in complex_differentiable_transform_within)
   1.900 +        apply (rule u derivative_intros holomorphic_on_imp_differentiable_at [OF holf] | force simp add: dist_commute)+
   1.901 +        done
   1.902 +      show ?thesis
   1.903 +        unfolding d_def
   1.904 +        apply (rule no_isolated_singularity [OF * _ u, where k = "{w}"])
   1.905 +        apply (auto simp: complex_differentiable_def [symmetric] holomorphic_on_open open_Diff u **)
   1.906 +        done
   1.907 +    qed
   1.908 +    { fix a b
   1.909 +      assume abu: "closed_segment a b \<subseteq> u"
   1.910 +      then have cont_dw: "\<And>w. w \<in> u \<Longrightarrow> (\<lambda>z. d z w) contour_integrable_on (linepath a b)"
   1.911 +        by (metis hol_dw continuous_on_subset contour_integrable_continuous_linepath holomorphic_on_imp_continuous_on)
   1.912 +      have *: "\<exists>da>0. \<forall>x'\<in>u. dist x' w < da \<longrightarrow>
   1.913 +                             dist (contour_integral (linepath a b) (\<lambda>z. d z x'))
   1.914 +                                  (contour_integral (linepath a b) (\<lambda>z. d z w)) \<le> ee"
   1.915 +              if "w \<in> u" "0 < ee" "a \<noteq> b" for w ee
   1.916 +      proof -
   1.917 +        obtain dd where "dd>0" and dd: "cball w dd \<subseteq> u" using open_contains_cball u \<open>w \<in> u\<close> by force
   1.918 +        let ?abdd = "{(z,t) |z t. z \<in> closed_segment a b \<and> t \<in> cball w dd}"
   1.919 +        have "uniformly_continuous_on ?abdd (\<lambda>y. d (fst y) (snd y))"
   1.920 +          apply (rule compact_uniformly_continuous)
   1.921 +          apply (rule continuous_on_subset[OF cond_uu])
   1.922 +          using abu dd
   1.923 +          apply (auto simp: compact_Times simp del: mem_cball)
   1.924 +          done
   1.925 +        then obtain kk where "kk>0"
   1.926 +            and kk: "\<And>x x'. \<lbrakk>x\<in>?abdd; x'\<in>?abdd; dist x' x < kk\<rbrakk> \<Longrightarrow>
   1.927 +                             dist ((\<lambda>y. d (fst y) (snd y)) x') ((\<lambda>y. d (fst y) (snd y)) x) < ee/norm(b - a)"
   1.928 +          apply (rule uniformly_continuous_onE [where e = "ee/norm(b - a)"])
   1.929 +          using \<open>0 < ee\<close> \<open>a \<noteq> b\<close> by auto
   1.930 +        have kk: "\<lbrakk>x1 \<in> closed_segment a b; norm (w - x2) \<le> dd;
   1.931 +                   x1' \<in> closed_segment a b; norm (w - x2') \<le> dd; norm ((x1', x2') - (x1, x2)) < kk\<rbrakk>
   1.932 +                  \<Longrightarrow> norm (d x1' x2' - d x1 x2) \<le> ee / cmod (b - a)"
   1.933 +                 for x1 x2 x1' x2'
   1.934 +          using kk [of "(x1,x2)" "(x1',x2')"] by (force simp add: dist_norm)
   1.935 +        have le_ee: "cmod (contour_integral (linepath a b) (\<lambda>x. d x x' - d x w)) \<le> ee"
   1.936 +                    if "x' \<in> u" "cmod (x' - w) < dd" "cmod (x' - w) < kk"  for x'
   1.937 +        proof -
   1.938 +          have "cmod (contour_integral (linepath a b) (\<lambda>x. d x x' - d x w)) \<le> ee/norm(b - a) * norm(b - a)"
   1.939 +            apply (rule has_contour_integral_bound_linepath [OF has_contour_integral_integral _ kk])
   1.940 +            apply (rule contour_integrable_diff [OF cont_dw cont_dw])
   1.941 +            using \<open>0 < ee\<close> \<open>a \<noteq> b\<close> \<open>0 < dd\<close> \<open>w \<in> u\<close> that
   1.942 +            apply (auto simp: norm_minus_commute)
   1.943 +            done
   1.944 +          also have "... = ee" using \<open>a \<noteq> b\<close> by simp
   1.945 +          finally show ?thesis .
   1.946 +        qed
   1.947 +        show ?thesis
   1.948 +          apply (rule_tac x="min dd kk" in exI)
   1.949 +          using \<open>0 < dd\<close> \<open>0 < kk\<close>
   1.950 +          apply (auto simp: dist_norm contour_integral_diff [OF cont_dw cont_dw, symmetric] \<open>w \<in> u\<close> intro: le_ee)
   1.951 +          done
   1.952 +      qed
   1.953 +      have cont_cint_d: "continuous_on u (\<lambda>w. contour_integral (linepath a b) (\<lambda>z. d z w))"
   1.954 +        apply (rule continuous_onI)
   1.955 +        apply (cases "a=b")
   1.956 +        apply (auto intro: *)
   1.957 +        done
   1.958 +      have cont_cint_d\<gamma>: "continuous_on {0..1} ((\<lambda>w. contour_integral (linepath a b) (\<lambda>z. d z w)) o \<gamma>)"
   1.959 +        apply (rule continuous_on_compose)
   1.960 +        using \<open>path \<gamma>\<close> path_def pasz
   1.961 +        apply (auto intro!: continuous_on_subset [OF cont_cint_d])
   1.962 +        apply (force simp add: path_image_def)
   1.963 +        done
   1.964 +      have cint_cint: "(\<lambda>w. contour_integral (linepath a b) (\<lambda>z. d z w)) contour_integrable_on \<gamma>"
   1.965 +        apply (simp add: contour_integrable_on)
   1.966 +        apply (rule integrable_continuous_real)
   1.967 +        apply (rule continuous_on_mult [OF cont_cint_d\<gamma> [unfolded o_def]])
   1.968 +        using pf\<gamma>'
   1.969 +        by (simp add: continuous_on_polymonial_function vector_derivative_at [OF \<gamma>'])
   1.970 +      have "contour_integral (linepath a b) h = contour_integral (linepath a b) (\<lambda>z. contour_integral \<gamma> (d z))"
   1.971 +        using abu  by (force simp add: h_def intro: contour_integral_eq)
   1.972 +      also have "... =  contour_integral \<gamma> (\<lambda>w. contour_integral (linepath a b) (\<lambda>z. d z w))"
   1.973 +        apply (rule contour_integral_swap)
   1.974 +        apply (simp add: split_def)
   1.975 +        apply (rule continuous_on_subset [OF cond_uu])
   1.976 +        using abu pasz \<open>valid_path \<gamma>\<close>
   1.977 +        apply (auto intro!: continuous_intros)
   1.978 +        by (metis \<gamma>' continuous_on_eq path_def path_polynomial_function pf\<gamma>' vector_derivative_at)
   1.979 +      finally have cint_h_eq:
   1.980 +          "contour_integral (linepath a b) h =
   1.981 +                    contour_integral \<gamma> (\<lambda>w. contour_integral (linepath a b) (\<lambda>z. d z w))" .
   1.982 +      note cint_cint cint_h_eq
   1.983 +    } note cint_h = this
   1.984 +    have conthu: "continuous_on u h"
   1.985 +    proof (simp add: continuous_on_sequentially, clarify)
   1.986 +      fix a x
   1.987 +      assume x: "x \<in> u" and au: "\<forall>n. a n \<in> u" and ax: "a \<longlonglongrightarrow> x"
   1.988 +      then have A1: "\<forall>\<^sub>F n in sequentially. d (a n) contour_integrable_on \<gamma>"
   1.989 +        by (meson U contour_integrable_on_def eventuallyI)
   1.990 +      obtain dd where "dd>0" and dd: "cball x dd \<subseteq> u" using open_contains_cball u x by force
   1.991 +      have A2: "\<forall>\<^sub>F n in sequentially. \<forall>xa\<in>path_image \<gamma>. cmod (d (a n) xa - d x xa) < ee" if "0 < ee" for ee
   1.992 +      proof -
   1.993 +        let ?ddpa = "{(w,z) |w z. w \<in> cball x dd \<and> z \<in> path_image \<gamma>}"
   1.994 +        have "uniformly_continuous_on ?ddpa (\<lambda>y. d (fst y) (snd y))"
   1.995 +          apply (rule compact_uniformly_continuous [OF continuous_on_subset[OF cond_uu]])
   1.996 +          using dd pasz \<open>valid_path \<gamma>\<close>
   1.997 +          apply (auto simp: compact_Times compact_valid_path_image simp del: mem_cball)
   1.998 +          done
   1.999 +        then obtain kk where "kk>0"
  1.1000 +            and kk: "\<And>x x'. \<lbrakk>x\<in>?ddpa; x'\<in>?ddpa; dist x' x < kk\<rbrakk> \<Longrightarrow>
  1.1001 +                             dist ((\<lambda>y. d (fst y) (snd y)) x') ((\<lambda>y. d (fst y) (snd y)) x) < ee"
  1.1002 +          apply (rule uniformly_continuous_onE [where e = ee])
  1.1003 +          using \<open>0 < ee\<close> by auto
  1.1004 +
  1.1005 +        have kk: "\<lbrakk>norm (w - x) \<le> dd; z \<in> path_image \<gamma>; norm ((w, z) - (x, z)) < kk\<rbrakk> \<Longrightarrow> norm (d w z - d x z) < ee"
  1.1006 +                 for  w z
  1.1007 +          using \<open>dd>0\<close> kk [of "(x,z)" "(w,z)"] by (force simp add: norm_minus_commute dist_norm)
  1.1008 +        show ?thesis
  1.1009 +          using ax unfolding lim_sequentially eventually_sequentially
  1.1010 +          apply (drule_tac x="min dd kk" in spec)
  1.1011 +          using \<open>dd > 0\<close> \<open>kk > 0\<close>
  1.1012 +          apply (fastforce simp: kk dist_norm)
  1.1013 +          done
  1.1014 +      qed
  1.1015 +      have tendsto_hx: "(\<lambda>n. contour_integral \<gamma> (d (a n))) \<longlonglongrightarrow> h x"
  1.1016 +        apply (simp add: contour_integral_unique [OF U, symmetric] x)
  1.1017 +        apply (rule contour_integral_uniform_limit [OF A1 A2 le_B])
  1.1018 +        apply (auto simp: \<open>valid_path \<gamma>\<close>)
  1.1019 +        done
  1.1020 +      then show "(h \<circ> a) \<longlonglongrightarrow> h x"
  1.1021 +        by (simp add: h_def x au o_def)
  1.1022 +    qed
  1.1023 +    show ?thesis
  1.1024 +    proof (simp add: holomorphic_on_open complex_differentiable_def [symmetric], clarify)
  1.1025 +      fix z0
  1.1026 +      consider "z0 \<in> v" | "z0 \<in> u" using uv_Un by blast
  1.1027 +      then show "h complex_differentiable at z0"
  1.1028 +      proof cases
  1.1029 +        assume "z0 \<in> v" then show ?thesis
  1.1030 +          using Cauchy_next_derivative [OF con_pa_f le_B f_has_cint _ ov] V f_has_cint \<open>valid_path \<gamma>\<close>
  1.1031 +          by (auto simp: complex_differentiable_def v_def)
  1.1032 +      next
  1.1033 +        assume "z0 \<in> u" then
  1.1034 +        obtain e where "e>0" and e: "ball z0 e \<subseteq> u" by (blast intro: openE [OF u])
  1.1035 +        have *: "contour_integral (linepath a b) h + contour_integral (linepath b c) h + contour_integral (linepath c a) h = 0"
  1.1036 +                if abc_subset: "convex hull {a, b, c} \<subseteq> ball z0 e"  for a b c
  1.1037 +        proof -
  1.1038 +          have *: "\<And>x1 x2 z. z \<in> u \<Longrightarrow> closed_segment x1 x2 \<subseteq> u \<Longrightarrow> (\<lambda>w. d w z) contour_integrable_on linepath x1 x2"
  1.1039 +            using  hol_dw holomorphic_on_imp_continuous_on u
  1.1040 +            by (auto intro!: contour_integrable_holomorphic_simple)
  1.1041 +          have abc: "closed_segment a b \<subseteq> u"  "closed_segment b c \<subseteq> u"  "closed_segment c a \<subseteq> u"
  1.1042 +            using that e segments_subset_convex_hull by fastforce+
  1.1043 +          have eq0: "\<And>w. w \<in> u \<Longrightarrow> contour_integral (linepath a b +++ linepath b c +++ linepath c a) (\<lambda>z. d z w) = 0"
  1.1044 +            apply (rule contour_integral_unique [OF Cauchy_theorem_triangle])
  1.1045 +            apply (rule holomorphic_on_subset [OF hol_dw])
  1.1046 +            using e abc_subset by auto
  1.1047 +          have "contour_integral \<gamma>
  1.1048 +                   (\<lambda>x. contour_integral (linepath a b) (\<lambda>z. d z x) +
  1.1049 +                        (contour_integral (linepath b c) (\<lambda>z. d z x) +
  1.1050 +                         contour_integral (linepath c a) (\<lambda>z. d z x)))  =  0"
  1.1051 +            apply (rule contour_integral_eq_0)
  1.1052 +            using abc pasz u
  1.1053 +            apply (subst contour_integral_join [symmetric], auto intro: eq0 *)+
  1.1054 +            done
  1.1055 +          then show ?thesis
  1.1056 +            by (simp add: cint_h abc contour_integrable_add contour_integral_add [symmetric] add_ac)
  1.1057 +        qed
  1.1058 +        show ?thesis
  1.1059 +          using e \<open>e > 0\<close>
  1.1060 +          by (auto intro!: holomorphic_on_imp_differentiable_at [OF _ open_ball] analytic_imp_holomorphic
  1.1061 +                           Morera_triangle continuous_on_subset [OF conthu] *)
  1.1062 +      qed
  1.1063 +    qed
  1.1064 +  qed
  1.1065 +  ultimately have [simp]: "h z = 0" for z
  1.1066 +    by (meson Liouville_weak)
  1.1067 +  have "((\<lambda>w. 1 / (w - z)) has_contour_integral complex_of_real (2 * pi) * \<i> * winding_number \<gamma> z) \<gamma>"
  1.1068 +    by (rule has_contour_integral_winding_number [OF \<open>valid_path \<gamma>\<close> znot])
  1.1069 +  then have "((\<lambda>w. f z * (1 / (w - z))) has_contour_integral complex_of_real (2 * pi) * \<i> * winding_number \<gamma> z * f z) \<gamma>"
  1.1070 +    by (metis mult.commute has_contour_integral_lmul)
  1.1071 +  then have 1: "((\<lambda>w. f z / (w - z)) has_contour_integral complex_of_real (2 * pi) * \<i> * winding_number \<gamma> z * f z) \<gamma>"
  1.1072 +    by (simp add: divide_simps)
  1.1073 +  moreover have 2: "((\<lambda>w. (f w - f z) / (w - z)) has_contour_integral 0) \<gamma>"
  1.1074 +    using U [OF z] pasz d_def by (force elim: has_contour_integral_eq [where g = "\<lambda>w. (f w - f z)/(w - z)"])
  1.1075 +  show ?thesis
  1.1076 +    using has_contour_integral_add [OF 1 2]  by (simp add: diff_divide_distrib)
  1.1077 +qed
  1.1078 +
  1.1079 +
  1.1080 +theorem Cauchy_integral_formula_global:
  1.1081 +    assumes s: "open s" and holf: "f holomorphic_on s"
  1.1082 +        and z: "z \<in> s" and vpg: "valid_path \<gamma>"
  1.1083 +        and pasz: "path_image \<gamma> \<subseteq> s - {z}" and loop: "pathfinish \<gamma> = pathstart \<gamma>"
  1.1084 +        and zero: "\<And>w. w \<notin> s \<Longrightarrow> winding_number \<gamma> w = 0"
  1.1085 +      shows "((\<lambda>w. f w / (w - z)) has_contour_integral (2*pi * ii * winding_number \<gamma> z * f z)) \<gamma>"
  1.1086 +proof -
  1.1087 +  have "path \<gamma>" using vpg by (blast intro: valid_path_imp_path)
  1.1088 +  have hols: "(\<lambda>w. f w / (w - z)) holomorphic_on s - {z}" "(\<lambda>w. 1 / (w - z)) holomorphic_on s - {z}"
  1.1089 +    by (rule holomorphic_intros holomorphic_on_subset [OF holf] | force)+
  1.1090 +  then have cint_fw: "(\<lambda>w. f w / (w - z)) contour_integrable_on \<gamma>"
  1.1091 +    by (meson contour_integrable_holomorphic_simple holomorphic_on_imp_continuous_on open_delete s vpg pasz)
  1.1092 +  obtain d where "d>0"
  1.1093 +      and d: "\<And>g h. \<lbrakk>valid_path g; valid_path h; \<forall>t\<in>{0..1}. cmod (g t - \<gamma> t) < d \<and> cmod (h t - \<gamma> t) < d;
  1.1094 +                     pathstart h = pathstart g \<and> pathfinish h = pathfinish g\<rbrakk>
  1.1095 +                     \<Longrightarrow> path_image h \<subseteq> s - {z} \<and> (\<forall>f. f holomorphic_on s - {z} \<longrightarrow> contour_integral h f = contour_integral g f)"
  1.1096 +    using contour_integral_nearby_ends [OF _ \<open>path \<gamma>\<close> pasz] s by (simp add: open_Diff) metis
  1.1097 +  obtain p where polyp: "polynomial_function p"
  1.1098 +             and ps: "pathstart p = pathstart \<gamma>" and pf: "pathfinish p = pathfinish \<gamma>" and led: "\<forall>t\<in>{0..1}. cmod (p t - \<gamma> t) < d"
  1.1099 +    using path_approx_polynomial_function [OF \<open>path \<gamma>\<close> \<open>d > 0\<close>] by blast
  1.1100 +  then have ploop: "pathfinish p = pathstart p" using loop by auto
  1.1101 +  have vpp: "valid_path p"  using polyp valid_path_polynomial_function by blast
  1.1102 +  have [simp]: "z \<notin> path_image \<gamma>" using pasz by blast
  1.1103 +  have paps: "path_image p \<subseteq> s - {z}" and cint_eq: "(\<And>f. f holomorphic_on s - {z} \<Longrightarrow> contour_integral p f = contour_integral \<gamma> f)"
  1.1104 +    using pf ps led d [OF vpg vpp] \<open>d > 0\<close> by auto
  1.1105 +  have wn_eq: "winding_number p z = winding_number \<gamma> z"
  1.1106 +    using vpp paps
  1.1107 +    by (simp add: subset_Diff_insert vpg valid_path_polynomial_function winding_number_valid_path cint_eq hols)
  1.1108 +  have "winding_number p w = winding_number \<gamma> w" if "w \<notin> s" for w
  1.1109 +  proof -
  1.1110 +    have hol: "(\<lambda>v. 1 / (v - w)) holomorphic_on s - {z}"
  1.1111 +      using that by (force intro: holomorphic_intros holomorphic_on_subset [OF holf])
  1.1112 +   have "w \<notin> path_image p" "w \<notin> path_image \<gamma>" using paps pasz that by auto
  1.1113 +   then show ?thesis
  1.1114 +    using vpp vpg by (simp add: subset_Diff_insert valid_path_polynomial_function winding_number_valid_path cint_eq [OF hol])
  1.1115 +  qed
  1.1116 +  then have wn0: "\<And>w. w \<notin> s \<Longrightarrow> winding_number p w = 0"
  1.1117 +    by (simp add: zero)
  1.1118 +  show ?thesis
  1.1119 +    using Cauchy_integral_formula_global_weak [OF s holf z polyp paps ploop wn0] hols
  1.1120 +    by (metis wn_eq cint_eq has_contour_integral_eqpath cint_fw cint_eq)
  1.1121 +qed
  1.1122 +
  1.1123 +theorem Cauchy_theorem_global:
  1.1124 +    assumes s: "open s" and holf: "f holomorphic_on s"
  1.1125 +        and vpg: "valid_path \<gamma>" and loop: "pathfinish \<gamma> = pathstart \<gamma>"
  1.1126 +        and pas: "path_image \<gamma> \<subseteq> s"
  1.1127 +        and zero: "\<And>w. w \<notin> s \<Longrightarrow> winding_number \<gamma> w = 0"
  1.1128 +      shows "(f has_contour_integral 0) \<gamma>"
  1.1129 +proof -
  1.1130 +  obtain z where "z \<in> s" and znot: "z \<notin> path_image \<gamma>"
  1.1131 +  proof -
  1.1132 +    have "compact (path_image \<gamma>)"
  1.1133 +      using compact_valid_path_image vpg by blast
  1.1134 +    then have "path_image \<gamma> \<noteq> s"
  1.1135 +      by (metis (no_types) compact_open path_image_nonempty s)
  1.1136 +    with pas show ?thesis by (blast intro: that)
  1.1137 +  qed
  1.1138 +  then have pasz: "path_image \<gamma> \<subseteq> s - {z}" using pas by blast
  1.1139 +  have hol: "(\<lambda>w. (w - z) * f w) holomorphic_on s"
  1.1140 +    by (rule holomorphic_intros holf)+
  1.1141 +  show ?thesis
  1.1142 +    using Cauchy_integral_formula_global [OF s hol \<open>z \<in> s\<close> vpg pasz loop zero]
  1.1143 +    by (auto simp: znot elim!: has_contour_integral_eq)
  1.1144 +qed
  1.1145 +
  1.1146 +corollary Cauchy_theorem_global_outside:
  1.1147 +    assumes "open s" "f holomorphic_on s" "valid_path \<gamma>"  "pathfinish \<gamma> = pathstart \<gamma>" "path_image \<gamma> \<subseteq> s"
  1.1148 +            "\<And>w. w \<notin> s \<Longrightarrow> w \<in> outside(path_image \<gamma>)"
  1.1149 +      shows "(f has_contour_integral 0) \<gamma>"
  1.1150 +by (metis Cauchy_theorem_global assms winding_number_zero_in_outside valid_path_imp_path)
  1.1151 +
  1.1152 +
  1.1153  end