src/HOL/Analysis/Conformal_Mappings.thy
author wenzelm
Mon Mar 25 17:21:26 2019 +0100 (2 months ago)
changeset 69981 3dced198b9ec
parent 69745 aec42cee2521
child 70065 cc89a395b5a3
permissions -rw-r--r--
more strict AFP properties;
     1 section \<open>Conformal Mappings and Consequences of Cauchy's Integral Theorem\<close>
     2 
     3 text\<open>By John Harrison et al.  Ported from HOL Light by L C Paulson (2016)\<close>
     4 
     5 text\<open>Also Cauchy's residue theorem by Wenda Li (2016)\<close>
     6 
     7 theory Conformal_Mappings
     8 imports Cauchy_Integral_Theorem
     9 
    10 begin
    11 
    12 (* FIXME mv to Cauchy_Integral_Theorem.thy *)
    13 subsection\<open>Cauchy's inequality and more versions of Liouville\<close>
    14 
    15 lemma Cauchy_higher_deriv_bound:
    16     assumes holf: "f holomorphic_on (ball z r)"
    17         and contf: "continuous_on (cball z r) f"
    18         and fin : "\<And>w. w \<in> ball z r \<Longrightarrow> f w \<in> ball y B0"
    19         and "0 < r" and "0 < n"
    20       shows "norm ((deriv ^^ n) f z) \<le> (fact n) * B0 / r^n"
    21 proof -
    22   have "0 < B0" using \<open>0 < r\<close> fin [of z]
    23     by (metis ball_eq_empty ex_in_conv fin not_less)
    24   have le_B0: "\<And>w. cmod (w - z) \<le> r \<Longrightarrow> cmod (f w - y) \<le> B0"
    25     apply (rule continuous_on_closure_norm_le [of "ball z r" "\<lambda>w. f w - y"])
    26     apply (auto simp: \<open>0 < r\<close>  dist_norm norm_minus_commute)
    27     apply (rule continuous_intros contf)+
    28     using fin apply (simp add: dist_commute dist_norm less_eq_real_def)
    29     done
    30   have "(deriv ^^ n) f z = (deriv ^^ n) (\<lambda>w. f w) z - (deriv ^^ n) (\<lambda>w. y) z"
    31     using \<open>0 < n\<close> by simp
    32   also have "... = (deriv ^^ n) (\<lambda>w. f w - y) z"
    33     by (rule higher_deriv_diff [OF holf, symmetric]) (auto simp: \<open>0 < r\<close>)
    34   finally have "(deriv ^^ n) f z = (deriv ^^ n) (\<lambda>w. f w - y) z" .
    35   have contf': "continuous_on (cball z r) (\<lambda>u. f u - y)"
    36     by (rule contf continuous_intros)+
    37   have holf': "(\<lambda>u. (f u - y)) holomorphic_on (ball z r)"
    38     by (simp add: holf holomorphic_on_diff)
    39   define a where "a = (2 * pi)/(fact n)"
    40   have "0 < a"  by (simp add: a_def)
    41   have "B0/r^(Suc n)*2 * pi * r = a*((fact n)*B0/r^n)"
    42     using \<open>0 < r\<close> by (simp add: a_def divide_simps)
    43   have der_dif: "(deriv ^^ n) (\<lambda>w. f w - y) z = (deriv ^^ n) f z"
    44     using \<open>0 < r\<close> \<open>0 < n\<close>
    45     by (auto simp: higher_deriv_diff [OF holf holomorphic_on_const])
    46   have "norm ((2 * of_real pi * \<i>)/(fact n) * (deriv ^^ n) (\<lambda>w. f w - y) z)
    47         \<le> (B0/r^(Suc n)) * (2 * pi * r)"
    48     apply (rule has_contour_integral_bound_circlepath [of "(\<lambda>u. (f u - y)/(u - z)^(Suc n))" _ z])
    49     using Cauchy_has_contour_integral_higher_derivative_circlepath [OF contf' holf']
    50     using \<open>0 < B0\<close> \<open>0 < r\<close>
    51     apply (auto simp: norm_divide norm_mult norm_power divide_simps le_B0)
    52     done
    53   then show ?thesis
    54     using \<open>0 < r\<close>
    55     by (auto simp: norm_divide norm_mult norm_power field_simps der_dif le_B0)
    56 qed
    57 
    58 lemma Cauchy_inequality:
    59     assumes holf: "f holomorphic_on (ball \<xi> r)"
    60         and contf: "continuous_on (cball \<xi> r) f"
    61         and "0 < r"
    62         and nof: "\<And>x. norm(\<xi>-x) = r \<Longrightarrow> norm(f x) \<le> B"
    63       shows "norm ((deriv ^^ n) f \<xi>) \<le> (fact n) * B / r^n"
    64 proof -
    65   obtain x where "norm (\<xi>-x) = r"
    66     by (metis abs_of_nonneg add_diff_cancel_left' \<open>0 < r\<close> diff_add_cancel
    67                  dual_order.strict_implies_order norm_of_real)
    68   then have "0 \<le> B"
    69     by (metis nof norm_not_less_zero not_le order_trans)
    70   have  "((\<lambda>u. f u / (u - \<xi>) ^ Suc n) has_contour_integral (2 * pi) * \<i> / fact n * (deriv ^^ n) f \<xi>)
    71          (circlepath \<xi> r)"
    72     apply (rule Cauchy_has_contour_integral_higher_derivative_circlepath [OF contf holf])
    73     using \<open>0 < r\<close> by simp
    74   then have "norm ((2 * pi * \<i>)/(fact n) * (deriv ^^ n) f \<xi>) \<le> (B / r^(Suc n)) * (2 * pi * r)"
    75     apply (rule has_contour_integral_bound_circlepath)
    76     using \<open>0 \<le> B\<close> \<open>0 < r\<close>
    77     apply (simp_all add: norm_divide norm_power nof frac_le norm_minus_commute del: power_Suc)
    78     done
    79   then show ?thesis using \<open>0 < r\<close>
    80     by (simp add: norm_divide norm_mult field_simps)
    81 qed
    82 
    83 lemma Liouville_polynomial:
    84     assumes holf: "f holomorphic_on UNIV"
    85         and nof: "\<And>z. A \<le> norm z \<Longrightarrow> norm(f z) \<le> B * norm z ^ n"
    86       shows "f \<xi> = (\<Sum>k\<le>n. (deriv^^k) f 0 / fact k * \<xi> ^ k)"
    87 proof (cases rule: le_less_linear [THEN disjE])
    88   assume "B \<le> 0"
    89   then have "\<And>z. A \<le> norm z \<Longrightarrow> norm(f z) = 0"
    90     by (metis nof less_le_trans zero_less_mult_iff neqE norm_not_less_zero norm_power not_le)
    91   then have f0: "(f \<longlongrightarrow> 0) at_infinity"
    92     using Lim_at_infinity by force
    93   then have [simp]: "f = (\<lambda>w. 0)"
    94     using Liouville_weak [OF holf, of 0]
    95     by (simp add: eventually_at_infinity f0) meson
    96   show ?thesis by simp
    97 next
    98   assume "0 < B"
    99   have "((\<lambda>k. (deriv ^^ k) f 0 / (fact k) * (\<xi> - 0)^k) sums f \<xi>)"
   100     apply (rule holomorphic_power_series [where r = "norm \<xi> + 1"])
   101     using holf holomorphic_on_subset apply auto
   102     done
   103   then have sumsf: "((\<lambda>k. (deriv ^^ k) f 0 / (fact k) * \<xi>^k) sums f \<xi>)" by simp
   104   have "(deriv ^^ k) f 0 / fact k * \<xi> ^ k = 0" if "k>n" for k
   105   proof (cases "(deriv ^^ k) f 0 = 0")
   106     case True then show ?thesis by simp
   107   next
   108     case False
   109     define w where "w = complex_of_real (fact k * B / cmod ((deriv ^^ k) f 0) + (\<bar>A\<bar> + 1))"
   110     have "1 \<le> abs (fact k * B / cmod ((deriv ^^ k) f 0) + (\<bar>A\<bar> + 1))"
   111       using \<open>0 < B\<close> by simp
   112     then have wge1: "1 \<le> norm w"
   113       by (metis norm_of_real w_def)
   114     then have "w \<noteq> 0" by auto
   115     have kB: "0 < fact k * B"
   116       using \<open>0 < B\<close> by simp
   117     then have "0 \<le> fact k * B / cmod ((deriv ^^ k) f 0)"
   118       by simp
   119     then have wgeA: "A \<le> cmod w"
   120       by (simp only: w_def norm_of_real)
   121     have "fact k * B / cmod ((deriv ^^ k) f 0) < abs (fact k * B / cmod ((deriv ^^ k) f 0) + (\<bar>A\<bar> + 1))"
   122       using \<open>0 < B\<close> by simp
   123     then have wge: "fact k * B / cmod ((deriv ^^ k) f 0) < norm w"
   124       by (metis norm_of_real w_def)
   125     then have "fact k * B / norm w < cmod ((deriv ^^ k) f 0)"
   126       using False by (simp add: divide_simps mult.commute split: if_split_asm)
   127     also have "... \<le> fact k * (B * norm w ^ n) / norm w ^ k"
   128       apply (rule Cauchy_inequality)
   129          using holf holomorphic_on_subset apply force
   130         using holf holomorphic_on_imp_continuous_on holomorphic_on_subset apply blast
   131        using \<open>w \<noteq> 0\<close> apply simp
   132        by (metis nof wgeA dist_0_norm dist_norm)
   133     also have "... = fact k * (B * 1 / cmod w ^ (k-n))"
   134       apply (simp only: mult_cancel_left times_divide_eq_right [symmetric])
   135       using \<open>k>n\<close> \<open>w \<noteq> 0\<close> \<open>0 < B\<close> apply (simp add: divide_simps semiring_normalization_rules)
   136       done
   137     also have "... = fact k * B / cmod w ^ (k-n)"
   138       by simp
   139     finally have "fact k * B / cmod w < fact k * B / cmod w ^ (k - n)" .
   140     then have "1 / cmod w < 1 / cmod w ^ (k - n)"
   141       by (metis kB divide_inverse inverse_eq_divide mult_less_cancel_left_pos)
   142     then have "cmod w ^ (k - n) < cmod w"
   143       by (metis frac_le le_less_trans norm_ge_zero norm_one not_less order_refl wge1 zero_less_one)
   144     with self_le_power [OF wge1] have False
   145       by (meson diff_is_0_eq not_gr0 not_le that)
   146     then show ?thesis by blast
   147   qed
   148   then have "(deriv ^^ (k + Suc n)) f 0 / fact (k + Suc n) * \<xi> ^ (k + Suc n) = 0" for k
   149     using not_less_eq by blast
   150   then have "(\<lambda>i. (deriv ^^ (i + Suc n)) f 0 / fact (i + Suc n) * \<xi> ^ (i + Suc n)) sums 0"
   151     by (rule sums_0)
   152   with sums_split_initial_segment [OF sumsf, where n = "Suc n"]
   153   show ?thesis
   154     using atLeast0AtMost lessThan_Suc_atMost sums_unique2 by fastforce
   155 qed
   156 
   157 text\<open>Every bounded entire function is a constant function.\<close>
   158 theorem Liouville_theorem:
   159     assumes holf: "f holomorphic_on UNIV"
   160         and bf: "bounded (range f)"
   161     obtains c where "\<And>z. f z = c"
   162 proof -
   163   obtain B where "\<And>z. cmod (f z) \<le> B"
   164     by (meson bf bounded_pos rangeI)
   165   then show ?thesis
   166     using Liouville_polynomial [OF holf, of 0 B 0, simplified] that by blast
   167 qed
   168 
   169 text\<open>A holomorphic function f has only isolated zeros unless f is 0.\<close>
   170 
   171 lemma powser_0_nonzero:
   172   fixes a :: "nat \<Rightarrow> 'a::{real_normed_field,banach}"
   173   assumes r: "0 < r"
   174       and sm: "\<And>x. norm (x - \<xi>) < r \<Longrightarrow> (\<lambda>n. a n * (x - \<xi>) ^ n) sums (f x)"
   175       and [simp]: "f \<xi> = 0"
   176       and m0: "a m \<noteq> 0" and "m>0"
   177   obtains s where "0 < s" and "\<And>z. z \<in> cball \<xi> s - {\<xi>} \<Longrightarrow> f z \<noteq> 0"
   178 proof -
   179   have "r \<le> conv_radius a"
   180     using sm sums_summable by (auto simp: le_conv_radius_iff [where \<xi>=\<xi>])
   181   obtain m where am: "a m \<noteq> 0" and az [simp]: "(\<And>n. n<m \<Longrightarrow> a n = 0)"
   182     apply (rule_tac m = "LEAST n. a n \<noteq> 0" in that)
   183     using m0
   184     apply (rule LeastI2)
   185     apply (fastforce intro:  dest!: not_less_Least)+
   186     done
   187   define b where "b i = a (i+m) / a m" for i
   188   define g where "g x = suminf (\<lambda>i. b i * (x - \<xi>) ^ i)" for x
   189   have [simp]: "b 0 = 1"
   190     by (simp add: am b_def)
   191   { fix x::'a
   192     assume "norm (x - \<xi>) < r"
   193     then have "(\<lambda>n. (a m * (x - \<xi>)^m) * (b n * (x - \<xi>)^n)) sums (f x)"
   194       using am az sm sums_zero_iff_shift [of m "(\<lambda>n. a n * (x - \<xi>) ^ n)" "f x"]
   195       by (simp add: b_def monoid_mult_class.power_add algebra_simps)
   196     then have "x \<noteq> \<xi> \<Longrightarrow> (\<lambda>n. b n * (x - \<xi>)^n) sums (f x / (a m * (x - \<xi>)^m))"
   197       using am by (simp add: sums_mult_D)
   198   } note bsums = this
   199   then have  "norm (x - \<xi>) < r \<Longrightarrow> summable (\<lambda>n. b n * (x - \<xi>)^n)" for x
   200     using sums_summable by (cases "x=\<xi>") auto
   201   then have "r \<le> conv_radius b"
   202     by (simp add: le_conv_radius_iff [where \<xi>=\<xi>])
   203   then have "r/2 < conv_radius b"
   204     using not_le order_trans r by fastforce
   205   then have "continuous_on (cball \<xi> (r/2)) g"
   206     using powser_continuous_suminf [of "r/2" b \<xi>] by (simp add: g_def)
   207   then obtain s where "s>0"  "\<And>x. \<lbrakk>norm (x - \<xi>) \<le> s; norm (x - \<xi>) \<le> r/2\<rbrakk> \<Longrightarrow> dist (g x) (g \<xi>) < 1/2"
   208     apply (rule continuous_onE [where x=\<xi> and e = "1/2"])
   209     using r apply (auto simp: norm_minus_commute dist_norm)
   210     done
   211   moreover have "g \<xi> = 1"
   212     by (simp add: g_def)
   213   ultimately have gnz: "\<And>x. \<lbrakk>norm (x - \<xi>) \<le> s; norm (x - \<xi>) \<le> r/2\<rbrakk> \<Longrightarrow> (g x) \<noteq> 0"
   214     by fastforce
   215   have "f x \<noteq> 0" if "x \<noteq> \<xi>" "norm (x - \<xi>) \<le> s" "norm (x - \<xi>) \<le> r/2" for x
   216     using bsums [of x] that gnz [of x]
   217     apply (auto simp: g_def)
   218     using r sums_iff by fastforce
   219   then show ?thesis
   220     apply (rule_tac s="min s (r/2)" in that)
   221     using \<open>0 < r\<close> \<open>0 < s\<close> by (auto simp: dist_commute dist_norm)
   222 qed
   223 
   224 subsection \<open>Analytic continuation\<close>
   225 
   226 proposition isolated_zeros:
   227   assumes holf: "f holomorphic_on S"
   228       and "open S" "connected S" "\<xi> \<in> S" "f \<xi> = 0" "\<beta> \<in> S" "f \<beta> \<noteq> 0"
   229     obtains r where "0 < r" and "ball \<xi> r \<subseteq> S" and 
   230         "\<And>z. z \<in> ball \<xi> r - {\<xi>} \<Longrightarrow> f z \<noteq> 0"
   231 proof -
   232   obtain r where "0 < r" and r: "ball \<xi> r \<subseteq> S"
   233     using \<open>open S\<close> \<open>\<xi> \<in> S\<close> open_contains_ball_eq by blast
   234   have powf: "((\<lambda>n. (deriv ^^ n) f \<xi> / (fact n) * (z - \<xi>)^n) sums f z)" if "z \<in> ball \<xi> r" for z
   235     apply (rule holomorphic_power_series [OF _ that])
   236     apply (rule holomorphic_on_subset [OF holf r])
   237     done
   238   obtain m where m: "(deriv ^^ m) f \<xi> / (fact m) \<noteq> 0"
   239     using holomorphic_fun_eq_0_on_connected [OF holf \<open>open S\<close> \<open>connected S\<close> _ \<open>\<xi> \<in> S\<close> \<open>\<beta> \<in> S\<close>] \<open>f \<beta> \<noteq> 0\<close>
   240     by auto
   241   then have "m \<noteq> 0" using assms(5) funpow_0 by fastforce
   242   obtain s where "0 < s" and s: "\<And>z. z \<in> cball \<xi> s - {\<xi>} \<Longrightarrow> f z \<noteq> 0"
   243     apply (rule powser_0_nonzero [OF \<open>0 < r\<close> powf \<open>f \<xi> = 0\<close> m])
   244     using \<open>m \<noteq> 0\<close> by (auto simp: dist_commute dist_norm)
   245   have "0 < min r s"  by (simp add: \<open>0 < r\<close> \<open>0 < s\<close>)
   246   then show ?thesis
   247     apply (rule that)
   248     using r s by auto
   249 qed
   250 
   251 proposition analytic_continuation:
   252   assumes holf: "f holomorphic_on S"
   253       and "open S" and "connected S"
   254       and "U \<subseteq> S" and "\<xi> \<in> S"
   255       and "\<xi> islimpt U"
   256       and fU0 [simp]: "\<And>z. z \<in> U \<Longrightarrow> f z = 0"
   257       and "w \<in> S"
   258     shows "f w = 0"
   259 proof -
   260   obtain e where "0 < e" and e: "cball \<xi> e \<subseteq> S"
   261     using \<open>open S\<close> \<open>\<xi> \<in> S\<close> open_contains_cball_eq by blast
   262   define T where "T = cball \<xi> e \<inter> U"
   263   have contf: "continuous_on (closure T) f"
   264     by (metis T_def closed_cball closure_minimal e holf holomorphic_on_imp_continuous_on
   265               holomorphic_on_subset inf.cobounded1)
   266   have fT0 [simp]: "\<And>x. x \<in> T \<Longrightarrow> f x = 0"
   267     by (simp add: T_def)
   268   have "\<And>r. \<lbrakk>\<forall>e>0. \<exists>x'\<in>U. x' \<noteq> \<xi> \<and> dist x' \<xi> < e; 0 < r\<rbrakk> \<Longrightarrow> \<exists>x'\<in>cball \<xi> e \<inter> U. x' \<noteq> \<xi> \<and> dist x' \<xi> < r"
   269     by (metis \<open>0 < e\<close> IntI dist_commute less_eq_real_def mem_cball min_less_iff_conj)
   270   then have "\<xi> islimpt T" using \<open>\<xi> islimpt U\<close>
   271     by (auto simp: T_def islimpt_approachable)
   272   then have "\<xi> \<in> closure T"
   273     by (simp add: closure_def)
   274   then have "f \<xi> = 0"
   275     by (auto simp: continuous_constant_on_closure [OF contf])
   276   show ?thesis
   277     apply (rule ccontr)
   278     apply (rule isolated_zeros [OF holf \<open>open S\<close> \<open>connected S\<close> \<open>\<xi> \<in> S\<close> \<open>f \<xi> = 0\<close> \<open>w \<in> S\<close>], assumption)
   279     by (metis open_ball \<open>\<xi> islimpt T\<close> centre_in_ball fT0 insertE insert_Diff islimptE)
   280 qed
   281 
   282 corollary analytic_continuation_open:
   283   assumes "open s" and "open s'" and "s \<noteq> {}" and "connected s'" 
   284       and "s \<subseteq> s'"
   285   assumes "f holomorphic_on s'" and "g holomorphic_on s'" 
   286       and "\<And>z. z \<in> s \<Longrightarrow> f z = g z"
   287   assumes "z \<in> s'"
   288   shows   "f z = g z"
   289 proof -
   290   from \<open>s \<noteq> {}\<close> obtain \<xi> where "\<xi> \<in> s" by auto
   291   with \<open>open s\<close> have \<xi>: "\<xi> islimpt s" 
   292     by (intro interior_limit_point) (auto simp: interior_open)
   293   have "f z - g z = 0"
   294     by (rule analytic_continuation[of "\<lambda>z. f z - g z" s' s \<xi>])
   295        (insert assms \<open>\<xi> \<in> s\<close> \<xi>, auto intro: holomorphic_intros)
   296   thus ?thesis by simp
   297 qed
   298 
   299 subsection\<open>Open mapping theorem\<close>
   300 
   301 lemma holomorphic_contract_to_zero:
   302   assumes contf: "continuous_on (cball \<xi> r) f"
   303       and holf: "f holomorphic_on ball \<xi> r"
   304       and "0 < r"
   305       and norm_less: "\<And>z. norm(\<xi> - z) = r \<Longrightarrow> norm(f \<xi>) < norm(f z)"
   306   obtains z where "z \<in> ball \<xi> r" "f z = 0"
   307 proof -
   308   { assume fnz: "\<And>w. w \<in> ball \<xi> r \<Longrightarrow> f w \<noteq> 0"
   309     then have "0 < norm (f \<xi>)"
   310       by (simp add: \<open>0 < r\<close>)
   311     have fnz': "\<And>w. w \<in> cball \<xi> r \<Longrightarrow> f w \<noteq> 0"
   312       by (metis norm_less dist_norm fnz less_eq_real_def mem_ball mem_cball norm_not_less_zero norm_zero)
   313     have "frontier(cball \<xi> r) \<noteq> {}"
   314       using \<open>0 < r\<close> by simp
   315     define g where [abs_def]: "g z = inverse (f z)" for z
   316     have contg: "continuous_on (cball \<xi> r) g"
   317       unfolding g_def using contf continuous_on_inverse fnz' by blast
   318     have holg: "g holomorphic_on ball \<xi> r"
   319       unfolding g_def using fnz holf holomorphic_on_inverse by blast
   320     have "frontier (cball \<xi> r) \<subseteq> cball \<xi> r"
   321       by (simp add: subset_iff)
   322     then have contf': "continuous_on (frontier (cball \<xi> r)) f"
   323           and contg': "continuous_on (frontier (cball \<xi> r)) g"
   324       by (blast intro: contf contg continuous_on_subset)+
   325     have froc: "frontier(cball \<xi> r) \<noteq> {}"
   326       using \<open>0 < r\<close> by simp
   327     moreover have "continuous_on (frontier (cball \<xi> r)) (norm o f)"
   328       using contf' continuous_on_compose continuous_on_norm_id by blast
   329     ultimately obtain w where w: "w \<in> frontier(cball \<xi> r)"
   330                           and now: "\<And>x. x \<in> frontier(cball \<xi> r) \<Longrightarrow> norm (f w) \<le> norm (f x)"
   331       apply (rule bexE [OF continuous_attains_inf [OF compact_frontier [OF compact_cball]]])
   332       apply simp
   333       done
   334     then have fw: "0 < norm (f w)"
   335       by (simp add: fnz')
   336     have "continuous_on (frontier (cball \<xi> r)) (norm o g)"
   337       using contg' continuous_on_compose continuous_on_norm_id by blast
   338     then obtain v where v: "v \<in> frontier(cball \<xi> r)"
   339                and nov: "\<And>x. x \<in> frontier(cball \<xi> r) \<Longrightarrow> norm (g v) \<ge> norm (g x)"
   340       apply (rule bexE [OF continuous_attains_sup [OF compact_frontier [OF compact_cball] froc]])
   341       apply simp
   342       done
   343     then have fv: "0 < norm (f v)"
   344       by (simp add: fnz')
   345     have "norm ((deriv ^^ 0) g \<xi>) \<le> fact 0 * norm (g v) / r ^ 0"
   346       by (rule Cauchy_inequality [OF holg contg \<open>0 < r\<close>]) (simp add: dist_norm nov)
   347     then have "cmod (g \<xi>) \<le> norm (g v)"
   348       by simp
   349     with w have wr: "norm (\<xi> - w) = r" and nfw: "norm (f w) \<le> norm (f \<xi>)"
   350       apply (simp_all add: dist_norm)
   351       by (metis \<open>0 < cmod (f \<xi>)\<close> g_def less_imp_inverse_less norm_inverse not_le now order_trans v)
   352     with fw have False
   353       using norm_less by force
   354   }
   355   with that show ?thesis by blast
   356 qed
   357 
   358 theorem open_mapping_thm:
   359   assumes holf: "f holomorphic_on S"
   360       and S: "open S" and "connected S"
   361       and "open U" and "U \<subseteq> S"
   362       and fne: "\<not> f constant_on S"
   363     shows "open (f ` U)"
   364 proof -
   365   have *: "open (f ` U)"
   366           if "U \<noteq> {}" and U: "open U" "connected U" and "f holomorphic_on U" and fneU: "\<And>x. \<exists>y \<in> U. f y \<noteq> x"
   367           for U
   368   proof (clarsimp simp: open_contains_ball)
   369     fix \<xi> assume \<xi>: "\<xi> \<in> U"
   370     show "\<exists>e>0. ball (f \<xi>) e \<subseteq> f ` U"
   371     proof -
   372       have hol: "(\<lambda>z. f z - f \<xi>) holomorphic_on U"
   373         by (rule holomorphic_intros that)+
   374       obtain s where "0 < s" and sbU: "ball \<xi> s \<subseteq> U"
   375                  and sne: "\<And>z. z \<in> ball \<xi> s - {\<xi>} \<Longrightarrow> (\<lambda>z. f z - f \<xi>) z \<noteq> 0"
   376         using isolated_zeros [OF hol U \<xi>]  by (metis fneU right_minus_eq)
   377       obtain r where "0 < r" and r: "cball \<xi> r \<subseteq> ball \<xi> s"
   378         apply (rule_tac r="s/2" in that)
   379         using \<open>0 < s\<close> by auto
   380       have "cball \<xi> r \<subseteq> U"
   381         using sbU r by blast
   382       then have frsbU: "frontier (cball \<xi> r) \<subseteq> U"
   383         using Diff_subset frontier_def order_trans by fastforce
   384       then have cof: "compact (frontier(cball \<xi> r))"
   385         by blast
   386       have frne: "frontier (cball \<xi> r) \<noteq> {}"
   387         using \<open>0 < r\<close> by auto
   388       have contfr: "continuous_on (frontier (cball \<xi> r)) (\<lambda>z. norm (f z - f \<xi>))"
   389         apply (rule continuous_on_compose2 [OF Complex_Analysis_Basics.continuous_on_norm_id])
   390         using hol frsbU holomorphic_on_imp_continuous_on holomorphic_on_subset by blast+
   391       obtain w where "norm (\<xi> - w) = r"
   392                  and w: "(\<And>z. norm (\<xi> - z) = r \<Longrightarrow> norm (f w - f \<xi>) \<le> norm(f z - f \<xi>))"
   393         apply (rule bexE [OF continuous_attains_inf [OF cof frne contfr]])
   394         apply (simp add: dist_norm)
   395         done
   396       moreover define \<epsilon> where "\<epsilon> \<equiv> norm (f w - f \<xi>) / 3"
   397       ultimately have "0 < \<epsilon>"
   398         using \<open>0 < r\<close> dist_complex_def r sne by auto
   399       have "ball (f \<xi>) \<epsilon> \<subseteq> f ` U"
   400       proof
   401         fix \<gamma>
   402         assume \<gamma>: "\<gamma> \<in> ball (f \<xi>) \<epsilon>"
   403         have *: "cmod (\<gamma> - f \<xi>) < cmod (\<gamma> - f z)" if "cmod (\<xi> - z) = r" for z
   404         proof -
   405           have lt: "cmod (f w - f \<xi>) / 3 < cmod (\<gamma> - f z)"
   406             using w [OF that] \<gamma>
   407             using dist_triangle2 [of "f \<xi>" "\<gamma>"  "f z"] dist_triangle2 [of "f \<xi>" "f z" \<gamma>]
   408             by (simp add: \<epsilon>_def dist_norm norm_minus_commute)
   409           show ?thesis
   410             by (metis \<epsilon>_def dist_commute dist_norm less_trans lt mem_ball \<gamma>)
   411        qed
   412        have "continuous_on (cball \<xi> r) (\<lambda>z. \<gamma> - f z)"
   413           apply (rule continuous_intros)+
   414           using \<open>cball \<xi> r \<subseteq> U\<close> \<open>f holomorphic_on U\<close>
   415           apply (blast intro: continuous_on_subset holomorphic_on_imp_continuous_on)
   416           done
   417         moreover have "(\<lambda>z. \<gamma> - f z) holomorphic_on ball \<xi> r"
   418           apply (rule holomorphic_intros)+
   419           apply (metis \<open>cball \<xi> r \<subseteq> U\<close> \<open>f holomorphic_on U\<close> holomorphic_on_subset interior_cball interior_subset)
   420           done
   421         ultimately obtain z where "z \<in> ball \<xi> r" "\<gamma> - f z = 0"
   422           apply (rule holomorphic_contract_to_zero)
   423           apply (blast intro!: \<open>0 < r\<close> *)+
   424           done
   425         then show "\<gamma> \<in> f ` U"
   426           using \<open>cball \<xi> r \<subseteq> U\<close> by fastforce
   427       qed
   428       then show ?thesis using  \<open>0 < \<epsilon>\<close> by blast
   429     qed
   430   qed
   431   have "open (f ` X)" if "X \<in> components U" for X
   432   proof -
   433     have holfU: "f holomorphic_on U"
   434       using \<open>U \<subseteq> S\<close> holf holomorphic_on_subset by blast
   435     have "X \<noteq> {}"
   436       using that by (simp add: in_components_nonempty)
   437     moreover have "open X"
   438       using that \<open>open U\<close> open_components by auto
   439     moreover have "connected X"
   440       using that in_components_maximal by blast
   441     moreover have "f holomorphic_on X"
   442       by (meson that holfU holomorphic_on_subset in_components_maximal)
   443     moreover have "\<exists>y\<in>X. f y \<noteq> x" for x
   444     proof (rule ccontr)
   445       assume not: "\<not> (\<exists>y\<in>X. f y \<noteq> x)"
   446       have "X \<subseteq> S"
   447         using \<open>U \<subseteq> S\<close> in_components_subset that by blast
   448       obtain w where w: "w \<in> X" using \<open>X \<noteq> {}\<close> by blast
   449       have wis: "w islimpt X"
   450         using w \<open>open X\<close> interior_eq by auto
   451       have hol: "(\<lambda>z. f z - x) holomorphic_on S"
   452         by (simp add: holf holomorphic_on_diff)
   453       with fne [unfolded constant_on_def] 
   454            analytic_continuation[OF hol S \<open>connected S\<close> \<open>X \<subseteq> S\<close> _ wis] not \<open>X \<subseteq> S\<close> w
   455       show False by auto
   456     qed
   457     ultimately show ?thesis
   458       by (rule *)
   459   qed
   460   then have "open (f ` \<Union>(components U))"
   461     by (metis (no_types, lifting) imageE image_Union open_Union)
   462   then show ?thesis
   463     by force
   464 qed
   465 
   466 text\<open>No need for \<^term>\<open>S\<close> to be connected. But the nonconstant condition is stronger.\<close>
   467 corollary%unimportant open_mapping_thm2:
   468   assumes holf: "f holomorphic_on S"
   469       and S: "open S"
   470       and "open U" "U \<subseteq> S"
   471       and fnc: "\<And>X. \<lbrakk>open X; X \<subseteq> S; X \<noteq> {}\<rbrakk> \<Longrightarrow> \<not> f constant_on X"
   472     shows "open (f ` U)"
   473 proof -
   474   have "S = \<Union>(components S)" by simp
   475   with \<open>U \<subseteq> S\<close> have "U = (\<Union>C \<in> components S. C \<inter> U)" by auto
   476   then have "f ` U = (\<Union>C \<in> components S. f ` (C \<inter> U))"
   477     using image_UN by fastforce
   478   moreover
   479   { fix C assume "C \<in> components S"
   480     with S \<open>C \<in> components S\<close> open_components in_components_connected
   481     have C: "open C" "connected C" by auto
   482     have "C \<subseteq> S"
   483       by (metis \<open>C \<in> components S\<close> in_components_maximal)
   484     have nf: "\<not> f constant_on C"
   485       apply (rule fnc)
   486       using C \<open>C \<subseteq> S\<close> \<open>C \<in> components S\<close> in_components_nonempty by auto
   487     have "f holomorphic_on C"
   488       by (metis holf holomorphic_on_subset \<open>C \<subseteq> S\<close>)
   489     then have "open (f ` (C \<inter> U))"
   490       apply (rule open_mapping_thm [OF _ C _ _ nf])
   491       apply (simp add: C \<open>open U\<close> open_Int, blast)
   492       done
   493   } ultimately show ?thesis
   494     by force
   495 qed
   496 
   497 corollary%unimportant open_mapping_thm3:
   498   assumes holf: "f holomorphic_on S"
   499       and "open S" and injf: "inj_on f S"
   500     shows  "open (f ` S)"
   501 apply (rule open_mapping_thm2 [OF holf])
   502 using assms
   503 apply (simp_all add:)
   504 using injective_not_constant subset_inj_on by blast
   505 
   506 subsection\<open>Maximum modulus principle\<close>
   507 
   508 text\<open>If \<^term>\<open>f\<close> is holomorphic, then its norm (modulus) cannot exhibit a true local maximum that is
   509    properly within the domain of \<^term>\<open>f\<close>.\<close>
   510 
   511 proposition maximum_modulus_principle:
   512   assumes holf: "f holomorphic_on S"
   513       and S: "open S" and "connected S"
   514       and "open U" and "U \<subseteq> S" and "\<xi> \<in> U"
   515       and no: "\<And>z. z \<in> U \<Longrightarrow> norm(f z) \<le> norm(f \<xi>)"
   516     shows "f constant_on S"
   517 proof (rule ccontr)
   518   assume "\<not> f constant_on S"
   519   then have "open (f ` U)"
   520     using open_mapping_thm assms by blast
   521   moreover have "\<not> open (f ` U)"
   522   proof -
   523     have "\<exists>t. cmod (f \<xi> - t) < e \<and> t \<notin> f ` U" if "0 < e" for e
   524       apply (rule_tac x="if 0 < Re(f \<xi>) then f \<xi> + (e/2) else f \<xi> - (e/2)" in exI)
   525       using that
   526       apply (simp add: dist_norm)
   527       apply (fastforce simp: cmod_Re_le_iff dest!: no dest: sym)
   528       done
   529     then show ?thesis
   530       unfolding open_contains_ball by (metis \<open>\<xi> \<in> U\<close> contra_subsetD dist_norm imageI mem_ball)
   531   qed
   532   ultimately show False
   533     by blast
   534 qed
   535 
   536 proposition maximum_modulus_frontier:
   537   assumes holf: "f holomorphic_on (interior S)"
   538       and contf: "continuous_on (closure S) f"
   539       and bos: "bounded S"
   540       and leB: "\<And>z. z \<in> frontier S \<Longrightarrow> norm(f z) \<le> B"
   541       and "\<xi> \<in> S"
   542     shows "norm(f \<xi>) \<le> B"
   543 proof -
   544   have "compact (closure S)" using bos
   545     by (simp add: bounded_closure compact_eq_bounded_closed)
   546   moreover have "continuous_on (closure S) (cmod \<circ> f)"
   547     using contf continuous_on_compose continuous_on_norm_id by blast
   548   ultimately obtain z where zin: "z \<in> closure S" and z: "\<And>y. y \<in> closure S \<Longrightarrow> (cmod \<circ> f) y \<le> (cmod \<circ> f) z"
   549     using continuous_attains_sup [of "closure S" "norm o f"] \<open>\<xi> \<in> S\<close> by auto
   550   then consider "z \<in> frontier S" | "z \<in> interior S" using frontier_def by auto
   551   then have "norm(f z) \<le> B"
   552   proof cases
   553     case 1 then show ?thesis using leB by blast
   554   next
   555     case 2
   556     have zin: "z \<in> connected_component_set (interior S) z"
   557       by (simp add: 2)
   558     have "f constant_on (connected_component_set (interior S) z)"
   559       apply (rule maximum_modulus_principle [OF _ _ _ _ _ zin])
   560       apply (metis connected_component_subset holf holomorphic_on_subset)
   561       apply (simp_all add: open_connected_component)
   562       by (metis closure_subset comp_eq_dest_lhs  interior_subset subsetCE z connected_component_in)
   563     then obtain c where c: "\<And>w. w \<in> connected_component_set (interior S) z \<Longrightarrow> f w = c"
   564       by (auto simp: constant_on_def)
   565     have "f ` closure(connected_component_set (interior S) z) \<subseteq> {c}"
   566       apply (rule image_closure_subset)
   567       apply (meson closure_mono connected_component_subset contf continuous_on_subset interior_subset)
   568       using c
   569       apply auto
   570       done
   571     then have cc: "\<And>w. w \<in> closure(connected_component_set (interior S) z) \<Longrightarrow> f w = c" by blast
   572     have "frontier(connected_component_set (interior S) z) \<noteq> {}"
   573       apply (simp add: frontier_eq_empty)
   574       by (metis "2" bos bounded_interior connected_component_eq_UNIV connected_component_refl not_bounded_UNIV)
   575     then obtain w where w: "w \<in> frontier(connected_component_set (interior S) z)"
   576        by auto
   577     then have "norm (f z) = norm (f w)"  by (simp add: "2" c cc frontier_def)
   578     also have "... \<le> B"
   579       apply (rule leB)
   580       using w
   581 using frontier_interior_subset frontier_of_connected_component_subset by blast
   582     finally show ?thesis .
   583   qed
   584   then show ?thesis
   585     using z \<open>\<xi> \<in> S\<close> closure_subset by fastforce
   586 qed
   587 
   588 corollary%unimportant maximum_real_frontier:
   589   assumes holf: "f holomorphic_on (interior S)"
   590       and contf: "continuous_on (closure S) f"
   591       and bos: "bounded S"
   592       and leB: "\<And>z. z \<in> frontier S \<Longrightarrow> Re(f z) \<le> B"
   593       and "\<xi> \<in> S"
   594     shows "Re(f \<xi>) \<le> B"
   595 using maximum_modulus_frontier [of "exp o f" S "exp B"]
   596       Transcendental.continuous_on_exp holomorphic_on_compose holomorphic_on_exp assms
   597 by auto
   598 
   599 subsection%unimportant \<open>Factoring out a zero according to its order\<close>
   600 
   601 lemma holomorphic_factor_order_of_zero:
   602   assumes holf: "f holomorphic_on S"
   603       and os: "open S"
   604       and "\<xi> \<in> S" "0 < n"
   605       and dnz: "(deriv ^^ n) f \<xi> \<noteq> 0"
   606       and dfz: "\<And>i. \<lbrakk>0 < i; i < n\<rbrakk> \<Longrightarrow> (deriv ^^ i) f \<xi> = 0"
   607    obtains g r where "0 < r"
   608                 "g holomorphic_on ball \<xi> r"
   609                 "\<And>w. w \<in> ball \<xi> r \<Longrightarrow> f w - f \<xi> = (w - \<xi>)^n * g w"
   610                 "\<And>w. w \<in> ball \<xi> r \<Longrightarrow> g w \<noteq> 0"
   611 proof -
   612   obtain r where "r>0" and r: "ball \<xi> r \<subseteq> S" using assms by (blast elim!: openE)
   613   then have holfb: "f holomorphic_on ball \<xi> r"
   614     using holf holomorphic_on_subset by blast
   615   define g where "g w = suminf (\<lambda>i. (deriv ^^ (i + n)) f \<xi> / (fact(i + n)) * (w - \<xi>)^i)" for w
   616   have sumsg: "(\<lambda>i. (deriv ^^ (i + n)) f \<xi> / (fact(i + n)) * (w - \<xi>)^i) sums g w"
   617    and feq: "f w - f \<xi> = (w - \<xi>)^n * g w"
   618        if w: "w \<in> ball \<xi> r" for w
   619   proof -
   620     define powf where "powf = (\<lambda>i. (deriv ^^ i) f \<xi>/(fact i) * (w - \<xi>)^i)"
   621     have sing: "{..<n} - {i. powf i = 0} = (if f \<xi> = 0 then {} else {0})"
   622       unfolding powf_def using \<open>0 < n\<close> dfz by (auto simp: dfz; metis funpow_0 not_gr0)
   623     have "powf sums f w"
   624       unfolding powf_def by (rule holomorphic_power_series [OF holfb w])
   625     moreover have "(\<Sum>i<n. powf i) = f \<xi>"
   626       apply (subst Groups_Big.comm_monoid_add_class.sum.setdiff_irrelevant [symmetric])
   627       apply simp
   628       apply (simp only: dfz sing)
   629       apply (simp add: powf_def)
   630       done
   631     ultimately have fsums: "(\<lambda>i. powf (i+n)) sums (f w - f \<xi>)"
   632       using w sums_iff_shift' by metis
   633     then have *: "summable (\<lambda>i. (w - \<xi>) ^ n * ((deriv ^^ (i + n)) f \<xi> * (w - \<xi>) ^ i / fact (i + n)))"
   634       unfolding powf_def using sums_summable
   635       by (auto simp: power_add mult_ac)
   636     have "summable (\<lambda>i. (deriv ^^ (i + n)) f \<xi> * (w - \<xi>) ^ i / fact (i + n))"
   637     proof (cases "w=\<xi>")
   638       case False then show ?thesis
   639         using summable_mult [OF *, of "1 / (w - \<xi>) ^ n"] by simp
   640     next
   641       case True then show ?thesis
   642         by (auto simp: Power.semiring_1_class.power_0_left intro!: summable_finite [of "{0}"]
   643                  split: if_split_asm)
   644     qed
   645     then show sumsg: "(\<lambda>i. (deriv ^^ (i + n)) f \<xi> / (fact(i + n)) * (w - \<xi>)^i) sums g w"
   646       by (simp add: summable_sums_iff g_def)
   647     show "f w - f \<xi> = (w - \<xi>)^n * g w"
   648       apply (rule sums_unique2)
   649       apply (rule fsums [unfolded powf_def])
   650       using sums_mult [OF sumsg, of "(w - \<xi>) ^ n"]
   651       by (auto simp: power_add mult_ac)
   652   qed
   653   then have holg: "g holomorphic_on ball \<xi> r"
   654     by (meson sumsg power_series_holomorphic)
   655   then have contg: "continuous_on (ball \<xi> r) g"
   656     by (blast intro: holomorphic_on_imp_continuous_on)
   657   have "g \<xi> \<noteq> 0"
   658     using dnz unfolding g_def
   659     by (subst suminf_finite [of "{0}"]) auto
   660   obtain d where "0 < d" and d: "\<And>w. w \<in> ball \<xi> d \<Longrightarrow> g w \<noteq> 0"
   661     apply (rule exE [OF continuous_on_avoid [OF contg _ \<open>g \<xi> \<noteq> 0\<close>]])
   662     using \<open>0 < r\<close>
   663     apply force
   664     by (metis \<open>0 < r\<close> less_trans mem_ball not_less_iff_gr_or_eq)
   665   show ?thesis
   666     apply (rule that [where g=g and r ="min r d"])
   667     using \<open>0 < r\<close> \<open>0 < d\<close> holg
   668     apply (auto simp: feq holomorphic_on_subset subset_ball d)
   669     done
   670 qed
   671 
   672 
   673 lemma holomorphic_factor_order_of_zero_strong:
   674   assumes holf: "f holomorphic_on S" "open S"  "\<xi> \<in> S" "0 < n"
   675       and "(deriv ^^ n) f \<xi> \<noteq> 0"
   676       and "\<And>i. \<lbrakk>0 < i; i < n\<rbrakk> \<Longrightarrow> (deriv ^^ i) f \<xi> = 0"
   677    obtains g r where "0 < r"
   678                 "g holomorphic_on ball \<xi> r"
   679                 "\<And>w. w \<in> ball \<xi> r \<Longrightarrow> f w - f \<xi> = ((w - \<xi>) * g w) ^ n"
   680                 "\<And>w. w \<in> ball \<xi> r \<Longrightarrow> g w \<noteq> 0"
   681 proof -
   682   obtain g r where "0 < r"
   683                and holg: "g holomorphic_on ball \<xi> r"
   684                and feq: "\<And>w. w \<in> ball \<xi> r \<Longrightarrow> f w - f \<xi> = (w - \<xi>)^n * g w"
   685                and gne: "\<And>w. w \<in> ball \<xi> r \<Longrightarrow> g w \<noteq> 0"
   686     by (auto intro: holomorphic_factor_order_of_zero [OF assms])
   687   have con: "continuous_on (ball \<xi> r) (\<lambda>z. deriv g z / g z)"
   688     by (rule continuous_intros) (auto simp: gne holg holomorphic_deriv holomorphic_on_imp_continuous_on)
   689   have cd: "\<And>x. dist \<xi> x < r \<Longrightarrow> (\<lambda>z. deriv g z / g z) field_differentiable at x"
   690     apply (rule derivative_intros)+
   691     using holg mem_ball apply (blast intro: holomorphic_deriv holomorphic_on_imp_differentiable_at)
   692     apply (metis open_ball at_within_open holg holomorphic_on_def mem_ball)
   693     using gne mem_ball by blast
   694   obtain h where h: "\<And>x. x \<in> ball \<xi> r \<Longrightarrow> (h has_field_derivative deriv g x / g x) (at x)"
   695     apply (rule exE [OF holomorphic_convex_primitive [of "ball \<xi> r" "{}" "\<lambda>z. deriv g z / g z"]])
   696     apply (auto simp: con cd)
   697     apply (metis open_ball at_within_open mem_ball)
   698     done
   699   then have "continuous_on (ball \<xi> r) h"
   700     by (metis open_ball holomorphic_on_imp_continuous_on holomorphic_on_open)
   701   then have con: "continuous_on (ball \<xi> r) (\<lambda>x. exp (h x) / g x)"
   702     by (auto intro!: continuous_intros simp add: holg holomorphic_on_imp_continuous_on gne)
   703   have 0: "dist \<xi> x < r \<Longrightarrow> ((\<lambda>x. exp (h x) / g x) has_field_derivative 0) (at x)" for x
   704     apply (rule h derivative_eq_intros | simp)+
   705     apply (rule DERIV_deriv_iff_field_differentiable [THEN iffD2])
   706     using holg apply (auto simp: holomorphic_on_imp_differentiable_at gne h)
   707     done
   708   obtain c where c: "\<And>x. x \<in> ball \<xi> r \<Longrightarrow> exp (h x) / g x = c"
   709     by (rule DERIV_zero_connected_constant [of "ball \<xi> r" "{}" "\<lambda>x. exp(h x) / g x"]) (auto simp: con 0)
   710   have hol: "(\<lambda>z. exp ((Ln (inverse c) + h z) / of_nat n)) holomorphic_on ball \<xi> r"
   711     apply (rule holomorphic_on_compose [unfolded o_def, where g = exp])
   712     apply (rule holomorphic_intros)+
   713     using h holomorphic_on_open apply blast
   714     apply (rule holomorphic_intros)+
   715     using \<open>0 < n\<close> apply simp
   716     apply (rule holomorphic_intros)+
   717     done
   718   show ?thesis
   719     apply (rule that [where g="\<lambda>z. exp((Ln(inverse c) + h z)/n)" and r =r])
   720     using \<open>0 < r\<close> \<open>0 < n\<close>
   721     apply (auto simp: feq power_mult_distrib exp_divide_power_eq c [symmetric])
   722     apply (rule hol)
   723     apply (simp add: Transcendental.exp_add gne)
   724     done
   725 qed
   726 
   727 
   728 lemma
   729   fixes k :: "'a::wellorder"
   730   assumes a_def: "a == LEAST x. P x" and P: "P k"
   731   shows def_LeastI: "P a" and def_Least_le: "a \<le> k"
   732 unfolding a_def
   733 by (rule LeastI Least_le; rule P)+
   734 
   735 lemma holomorphic_factor_zero_nonconstant:
   736   assumes holf: "f holomorphic_on S" and S: "open S" "connected S"
   737       and "\<xi> \<in> S" "f \<xi> = 0"
   738       and nonconst: "\<not> f constant_on S"
   739    obtains g r n
   740       where "0 < n"  "0 < r"  "ball \<xi> r \<subseteq> S"
   741             "g holomorphic_on ball \<xi> r"
   742             "\<And>w. w \<in> ball \<xi> r \<Longrightarrow> f w = (w - \<xi>)^n * g w"
   743             "\<And>w. w \<in> ball \<xi> r \<Longrightarrow> g w \<noteq> 0"
   744 proof (cases "\<forall>n>0. (deriv ^^ n) f \<xi> = 0")
   745   case True then show ?thesis
   746     using holomorphic_fun_eq_const_on_connected [OF holf S _ \<open>\<xi> \<in> S\<close>] nonconst by (simp add: constant_on_def)
   747 next
   748   case False
   749   then obtain n0 where "n0 > 0" and n0: "(deriv ^^ n0) f \<xi> \<noteq> 0" by blast
   750   obtain r0 where "r0 > 0" "ball \<xi> r0 \<subseteq> S" using S openE \<open>\<xi> \<in> S\<close> by auto
   751   define n where "n \<equiv> LEAST n. (deriv ^^ n) f \<xi> \<noteq> 0"
   752   have n_ne: "(deriv ^^ n) f \<xi> \<noteq> 0"
   753     by (rule def_LeastI [OF n_def]) (rule n0)
   754   then have "0 < n" using \<open>f \<xi> = 0\<close>
   755     using funpow_0 by fastforce
   756   have n_min: "\<And>k. k < n \<Longrightarrow> (deriv ^^ k) f \<xi> = 0"
   757     using def_Least_le [OF n_def] not_le by blast
   758   then obtain g r1
   759     where  "0 < r1" "g holomorphic_on ball \<xi> r1"
   760            "\<And>w. w \<in> ball \<xi> r1 \<Longrightarrow> f w = (w - \<xi>) ^ n * g w"
   761            "\<And>w. w \<in> ball \<xi> r1 \<Longrightarrow> g w \<noteq> 0"
   762     by (auto intro: holomorphic_factor_order_of_zero [OF holf \<open>open S\<close> \<open>\<xi> \<in> S\<close> \<open>n > 0\<close> n_ne] simp: \<open>f \<xi> = 0\<close>)
   763   then show ?thesis
   764     apply (rule_tac g=g and r="min r0 r1" and n=n in that)
   765     using \<open>0 < n\<close> \<open>0 < r0\<close> \<open>0 < r1\<close> \<open>ball \<xi> r0 \<subseteq> S\<close>
   766     apply (auto simp: subset_ball intro: holomorphic_on_subset)
   767     done
   768 qed
   769 
   770 
   771 lemma holomorphic_lower_bound_difference:
   772   assumes holf: "f holomorphic_on S" and S: "open S" "connected S"
   773       and "\<xi> \<in> S" and "\<phi> \<in> S"
   774       and fne: "f \<phi> \<noteq> f \<xi>"
   775    obtains k n r
   776       where "0 < k"  "0 < r"
   777             "ball \<xi> r \<subseteq> S"
   778             "\<And>w. w \<in> ball \<xi> r \<Longrightarrow> k * norm(w - \<xi>)^n \<le> norm(f w - f \<xi>)"
   779 proof -
   780   define n where "n = (LEAST n. 0 < n \<and> (deriv ^^ n) f \<xi> \<noteq> 0)"
   781   obtain n0 where "0 < n0" and n0: "(deriv ^^ n0) f \<xi> \<noteq> 0"
   782     using fne holomorphic_fun_eq_const_on_connected [OF holf S] \<open>\<xi> \<in> S\<close> \<open>\<phi> \<in> S\<close> by blast
   783   then have "0 < n" and n_ne: "(deriv ^^ n) f \<xi> \<noteq> 0"
   784     unfolding n_def by (metis (mono_tags, lifting) LeastI)+
   785   have n_min: "\<And>k. \<lbrakk>0 < k; k < n\<rbrakk> \<Longrightarrow> (deriv ^^ k) f \<xi> = 0"
   786     unfolding n_def by (blast dest: not_less_Least)
   787   then obtain g r
   788     where "0 < r" and holg: "g holomorphic_on ball \<xi> r"
   789       and fne: "\<And>w. w \<in> ball \<xi> r \<Longrightarrow> f w - f \<xi> = (w - \<xi>) ^ n * g w"
   790       and gnz: "\<And>w. w \<in> ball \<xi> r \<Longrightarrow> g w \<noteq> 0"
   791       by (auto intro: holomorphic_factor_order_of_zero  [OF holf \<open>open S\<close> \<open>\<xi> \<in> S\<close> \<open>n > 0\<close> n_ne])
   792   obtain e where "e>0" and e: "ball \<xi> e \<subseteq> S" using assms by (blast elim!: openE)
   793   then have holfb: "f holomorphic_on ball \<xi> e"
   794     using holf holomorphic_on_subset by blast
   795   define d where "d = (min e r) / 2"
   796   have "0 < d" using \<open>0 < r\<close> \<open>0 < e\<close> by (simp add: d_def)
   797   have "d < r"
   798     using \<open>0 < r\<close> by (auto simp: d_def)
   799   then have cbb: "cball \<xi> d \<subseteq> ball \<xi> r"
   800     by (auto simp: cball_subset_ball_iff)
   801   then have "g holomorphic_on cball \<xi> d"
   802     by (rule holomorphic_on_subset [OF holg])
   803   then have "closed (g ` cball \<xi> d)"
   804     by (simp add: compact_imp_closed compact_continuous_image holomorphic_on_imp_continuous_on)
   805   moreover have "g ` cball \<xi> d \<noteq> {}"
   806     using \<open>0 < d\<close> by auto
   807   ultimately obtain x where x: "x \<in> g ` cball \<xi> d" and "\<And>y. y \<in> g ` cball \<xi> d \<Longrightarrow> dist 0 x \<le> dist 0 y"
   808     by (rule distance_attains_inf) blast
   809   then have leg: "\<And>w. w \<in> cball \<xi> d \<Longrightarrow> norm x \<le> norm (g w)"
   810     by auto
   811   have "ball \<xi> d \<subseteq> cball \<xi> d" by auto
   812   also have "... \<subseteq> ball \<xi> e" using \<open>0 < d\<close> d_def by auto
   813   also have "... \<subseteq> S" by (rule e)
   814   finally have dS: "ball \<xi> d \<subseteq> S" .
   815   moreover have "x \<noteq> 0" using gnz x \<open>d < r\<close> by auto
   816   ultimately show ?thesis
   817     apply (rule_tac k="norm x" and n=n and r=d in that)
   818     using \<open>d < r\<close> leg
   819     apply (auto simp: \<open>0 < d\<close> fne norm_mult norm_power algebra_simps mult_right_mono)
   820     done
   821 qed
   822 
   823 lemma
   824   assumes holf: "f holomorphic_on (S - {\<xi>})" and \<xi>: "\<xi> \<in> interior S"
   825     shows holomorphic_on_extend_lim:
   826           "(\<exists>g. g holomorphic_on S \<and> (\<forall>z \<in> S - {\<xi>}. g z = f z)) \<longleftrightarrow>
   827            ((\<lambda>z. (z - \<xi>) * f z) \<longlongrightarrow> 0) (at \<xi>)"
   828           (is "?P = ?Q")
   829      and holomorphic_on_extend_bounded:
   830           "(\<exists>g. g holomorphic_on S \<and> (\<forall>z \<in> S - {\<xi>}. g z = f z)) \<longleftrightarrow>
   831            (\<exists>B. eventually (\<lambda>z. norm(f z) \<le> B) (at \<xi>))"
   832           (is "?P = ?R")
   833 proof -
   834   obtain \<delta> where "0 < \<delta>" and \<delta>: "ball \<xi> \<delta> \<subseteq> S"
   835     using \<xi> mem_interior by blast
   836   have "?R" if holg: "g holomorphic_on S" and gf: "\<And>z. z \<in> S - {\<xi>} \<Longrightarrow> g z = f z" for g
   837   proof -
   838     have *: "\<forall>\<^sub>F z in at \<xi>. dist (g z) (g \<xi>) < 1 \<longrightarrow> cmod (f z) \<le> cmod (g \<xi>) + 1"
   839       apply (simp add: eventually_at)
   840       apply (rule_tac x="\<delta>" in exI)
   841       using \<delta> \<open>0 < \<delta>\<close>
   842       apply (clarsimp simp:)
   843       apply (drule_tac c=x in subsetD)
   844       apply (simp add: dist_commute)
   845       by (metis DiffI add.commute diff_le_eq dist_norm gf le_less_trans less_eq_real_def norm_triangle_ineq2 singletonD)
   846     have "continuous_on (interior S) g"
   847       by (meson continuous_on_subset holg holomorphic_on_imp_continuous_on interior_subset)
   848     then have "\<And>x. x \<in> interior S \<Longrightarrow> (g \<longlongrightarrow> g x) (at x)"
   849       using continuous_on_interior continuous_within holg holomorphic_on_imp_continuous_on by blast
   850     then have "(g \<longlongrightarrow> g \<xi>) (at \<xi>)"
   851       by (simp add: \<xi>)
   852     then show ?thesis
   853       apply (rule_tac x="norm(g \<xi>) + 1" in exI)
   854       apply (rule eventually_mp [OF * tendstoD [where e=1]], auto)
   855       done
   856   qed
   857   moreover have "?Q" if "\<forall>\<^sub>F z in at \<xi>. cmod (f z) \<le> B" for B
   858     by (rule lim_null_mult_right_bounded [OF _ that]) (simp add: LIM_zero)
   859   moreover have "?P" if "(\<lambda>z. (z - \<xi>) * f z) \<midarrow>\<xi>\<rightarrow> 0"
   860   proof -
   861     define h where [abs_def]: "h z = (z - \<xi>)^2 * f z" for z
   862     have h0: "(h has_field_derivative 0) (at \<xi>)"
   863       apply (simp add: h_def has_field_derivative_iff)
   864       apply (rule Lim_transform_within [OF that, of 1])
   865       apply (auto simp: divide_simps power2_eq_square)
   866       done
   867     have holh: "h holomorphic_on S"
   868     proof (simp add: holomorphic_on_def, clarify)
   869       fix z assume "z \<in> S"
   870       show "h field_differentiable at z within S"
   871       proof (cases "z = \<xi>")
   872         case True then show ?thesis
   873           using field_differentiable_at_within field_differentiable_def h0 by blast
   874       next
   875         case False
   876         then have "f field_differentiable at z within S"
   877           using holomorphic_onD [OF holf, of z] \<open>z \<in> S\<close>
   878           unfolding field_differentiable_def has_field_derivative_iff
   879           by (force intro: exI [where x="dist \<xi> z"] elim: Lim_transform_within_set [unfolded eventually_at])
   880         then show ?thesis
   881           by (simp add: h_def power2_eq_square derivative_intros)
   882       qed
   883     qed
   884     define g where [abs_def]: "g z = (if z = \<xi> then deriv h \<xi> else (h z - h \<xi>) / (z - \<xi>))" for z
   885     have holg: "g holomorphic_on S"
   886       unfolding g_def by (rule pole_lemma [OF holh \<xi>])
   887     show ?thesis
   888       apply (rule_tac x="\<lambda>z. if z = \<xi> then deriv g \<xi> else (g z - g \<xi>)/(z - \<xi>)" in exI)
   889       apply (rule conjI)
   890       apply (rule pole_lemma [OF holg \<xi>])
   891       apply (auto simp: g_def power2_eq_square divide_simps)
   892       using h0 apply (simp add: h0 DERIV_imp_deriv h_def power2_eq_square)
   893       done
   894   qed
   895   ultimately show "?P = ?Q" and "?P = ?R"
   896     by meson+
   897 qed
   898 
   899 lemma pole_at_infinity:
   900   assumes holf: "f holomorphic_on UNIV" and lim: "((inverse o f) \<longlongrightarrow> l) at_infinity"
   901   obtains a n where "\<And>z. f z = (\<Sum>i\<le>n. a i * z^i)"
   902 proof (cases "l = 0")
   903   case False
   904   with tendsto_inverse [OF lim] show ?thesis
   905     apply (rule_tac a="(\<lambda>n. inverse l)" and n=0 in that)
   906     apply (simp add: Liouville_weak [OF holf, of "inverse l"])
   907     done
   908 next
   909   case True
   910   then have [simp]: "l = 0" .
   911   show ?thesis
   912   proof (cases "\<exists>r. 0 < r \<and> (\<forall>z \<in> ball 0 r - {0}. f(inverse z) \<noteq> 0)")
   913     case True
   914       then obtain r where "0 < r" and r: "\<And>z. z \<in> ball 0 r - {0} \<Longrightarrow> f(inverse z) \<noteq> 0"
   915              by auto
   916       have 1: "inverse \<circ> f \<circ> inverse holomorphic_on ball 0 r - {0}"
   917         by (rule holomorphic_on_compose holomorphic_intros holomorphic_on_subset [OF holf] | force simp: r)+
   918       have 2: "0 \<in> interior (ball 0 r)"
   919         using \<open>0 < r\<close> by simp
   920       have "\<exists>B. 0<B \<and> eventually (\<lambda>z. cmod ((inverse \<circ> f \<circ> inverse) z) \<le> B) (at 0)"
   921         apply (rule exI [where x=1])
   922         apply simp
   923         using tendstoD [OF lim [unfolded lim_at_infinity_0] zero_less_one]
   924         apply (rule eventually_mono)
   925         apply (simp add: dist_norm)
   926         done
   927       with holomorphic_on_extend_bounded [OF 1 2]
   928       obtain g where holg: "g holomorphic_on ball 0 r"
   929                  and geq: "\<And>z. z \<in> ball 0 r - {0} \<Longrightarrow> g z = (inverse \<circ> f \<circ> inverse) z"
   930         by meson
   931       have ifi0: "(inverse \<circ> f \<circ> inverse) \<midarrow>0\<rightarrow> 0"
   932         using \<open>l = 0\<close> lim lim_at_infinity_0 by blast
   933       have g2g0: "g \<midarrow>0\<rightarrow> g 0"
   934         using \<open>0 < r\<close> centre_in_ball continuous_at continuous_on_eq_continuous_at holg
   935         by (blast intro: holomorphic_on_imp_continuous_on)
   936       have g2g1: "g \<midarrow>0\<rightarrow> 0"
   937         apply (rule Lim_transform_within_open [OF ifi0 open_ball [of 0 r]])
   938         using \<open>0 < r\<close> by (auto simp: geq)
   939       have [simp]: "g 0 = 0"
   940         by (rule tendsto_unique [OF _ g2g0 g2g1]) simp
   941       have "ball 0 r - {0::complex} \<noteq> {}"
   942         using \<open>0 < r\<close>
   943         apply (clarsimp simp: ball_def dist_norm)
   944         apply (drule_tac c="of_real r/2" in subsetD, auto)
   945         done
   946       then obtain w::complex where "w \<noteq> 0" and w: "norm w < r" by force
   947       then have "g w \<noteq> 0" by (simp add: geq r)
   948       obtain B n e where "0 < B" "0 < e" "e \<le> r"
   949                      and leg: "\<And>w. norm w < e \<Longrightarrow> B * cmod w ^ n \<le> cmod (g w)"
   950         apply (rule holomorphic_lower_bound_difference [OF holg open_ball connected_ball, of 0 w])
   951         using \<open>0 < r\<close> w \<open>g w \<noteq> 0\<close> by (auto simp: ball_subset_ball_iff)
   952       have "cmod (f z) \<le> cmod z ^ n / B" if "2/e \<le> cmod z" for z
   953       proof -
   954         have ize: "inverse z \<in> ball 0 e - {0}" using that \<open>0 < e\<close>
   955           by (auto simp: norm_divide divide_simps algebra_simps)
   956         then have [simp]: "z \<noteq> 0" and izr: "inverse z \<in> ball 0 r - {0}" using  \<open>e \<le> r\<close>
   957           by auto
   958         then have [simp]: "f z \<noteq> 0"
   959           using r [of "inverse z"] by simp
   960         have [simp]: "f z = inverse (g (inverse z))"
   961           using izr geq [of "inverse z"] by simp
   962         show ?thesis using ize leg [of "inverse z"]  \<open>0 < B\<close>  \<open>0 < e\<close>
   963           by (simp add: divide_simps norm_divide algebra_simps)
   964       qed
   965       then show ?thesis
   966         apply (rule_tac a = "\<lambda>k. (deriv ^^ k) f 0 / (fact k)" and n=n in that)
   967         apply (rule_tac A = "2/e" and B = "1/B" in Liouville_polynomial [OF holf], simp)
   968         done
   969   next
   970     case False
   971     then have fi0: "\<And>r. r > 0 \<Longrightarrow> \<exists>z\<in>ball 0 r - {0}. f (inverse z) = 0"
   972       by simp
   973     have fz0: "f z = 0" if "0 < r" and lt1: "\<And>x. x \<noteq> 0 \<Longrightarrow> cmod x < r \<Longrightarrow> inverse (cmod (f (inverse x))) < 1"
   974               for z r
   975     proof -
   976       have f0: "(f \<longlongrightarrow> 0) at_infinity"
   977       proof -
   978         have DIM_complex[intro]: "2 \<le> DIM(complex)"  \<comment> \<open>should not be necessary!\<close>
   979           by simp
   980         from lt1 have "f (inverse x) \<noteq> 0 \<Longrightarrow> x \<noteq> 0 \<Longrightarrow> cmod x < r \<Longrightarrow> 1 < cmod (f (inverse x))" for x
   981           using one_less_inverse by force
   982         then have **: "cmod (f (inverse x)) \<le> 1 \<Longrightarrow> x \<noteq> 0 \<Longrightarrow> cmod x < r \<Longrightarrow> f (inverse x) = 0" for x
   983           by force
   984         then have *: "(f \<circ> inverse) ` (ball 0 r - {0}) \<subseteq> {0} \<union> - ball 0 1"
   985           by force
   986         have "continuous_on (inverse ` (ball 0 r - {0})) f"
   987           using continuous_on_subset holf holomorphic_on_imp_continuous_on by blast
   988         then have "connected ((f \<circ> inverse) ` (ball 0 r - {0}))"
   989           apply (intro connected_continuous_image continuous_intros)
   990           apply (force intro: connected_punctured_ball)+
   991           done
   992         then have "{0} \<inter> (f \<circ> inverse) ` (ball 0 r - {0}) = {} \<or> - ball 0 1 \<inter> (f \<circ> inverse) ` (ball 0 r - {0}) = {}"
   993           by (rule connected_closedD) (use * in auto)
   994         then have "w \<noteq> 0 \<Longrightarrow> cmod w < r \<Longrightarrow> f (inverse w) = 0" for w
   995           using fi0 **[of w] \<open>0 < r\<close>
   996           apply (auto simp add: inf.commute [of "- ball 0 1"] Diff_eq [symmetric] image_subset_iff dest: less_imp_le)
   997            apply fastforce
   998           apply (drule bspec [of _ _ w])
   999            apply (auto dest: less_imp_le)
  1000           done
  1001         then show ?thesis
  1002           apply (simp add: lim_at_infinity_0)
  1003           apply (rule Lim_eventually)
  1004           apply (simp add: eventually_at)
  1005           apply (rule_tac x=r in exI)
  1006           apply (simp add: \<open>0 < r\<close> dist_norm)
  1007           done
  1008       qed
  1009       obtain w where "w \<in> ball 0 r - {0}" and "f (inverse w) = 0"
  1010         using False \<open>0 < r\<close> by blast
  1011       then show ?thesis
  1012         by (auto simp: f0 Liouville_weak [OF holf, of 0])
  1013     qed
  1014     show ?thesis
  1015       apply (rule that [of "\<lambda>n. 0" 0])
  1016       using lim [unfolded lim_at_infinity_0]
  1017       apply (simp add: Lim_at dist_norm norm_inverse)
  1018       apply (drule_tac x=1 in spec)
  1019       using fz0 apply auto
  1020       done
  1021     qed
  1022 qed
  1023 
  1024 subsection%unimportant \<open>Entire proper functions are precisely the non-trivial polynomials\<close>
  1025 
  1026 lemma proper_map_polyfun:
  1027     fixes c :: "nat \<Rightarrow> 'a::{real_normed_div_algebra,heine_borel}"
  1028   assumes "closed S" and "compact K" and c: "c i \<noteq> 0" "1 \<le> i" "i \<le> n"
  1029     shows "compact (S \<inter> {z. (\<Sum>i\<le>n. c i * z^i) \<in> K})"
  1030 proof -
  1031   obtain B where "B > 0" and B: "\<And>x. x \<in> K \<Longrightarrow> norm x \<le> B"
  1032     by (metis compact_imp_bounded \<open>compact K\<close> bounded_pos)
  1033   have *: "norm x \<le> b"
  1034             if "\<And>x. b \<le> norm x \<Longrightarrow> B + 1 \<le> norm (\<Sum>i\<le>n. c i * x ^ i)"
  1035                "(\<Sum>i\<le>n. c i * x ^ i) \<in> K"  for b x
  1036   proof -
  1037     have "norm (\<Sum>i\<le>n. c i * x ^ i) \<le> B"
  1038       using B that by blast
  1039     moreover have "\<not> B + 1 \<le> B"
  1040       by simp
  1041     ultimately show "norm x \<le> b"
  1042       using that by (metis (no_types) less_eq_real_def not_less order_trans)
  1043   qed
  1044   have "bounded {z. (\<Sum>i\<le>n. c i * z ^ i) \<in> K}"
  1045     using polyfun_extremal [where c=c and B="B+1", OF c]
  1046     by (auto simp: bounded_pos eventually_at_infinity_pos *)
  1047   moreover have "closed ((\<lambda>z. (\<Sum>i\<le>n. c i * z ^ i)) -` K)"
  1048     apply (intro allI continuous_closed_vimage continuous_intros)
  1049     using \<open>compact K\<close> compact_eq_bounded_closed by blast
  1050   ultimately show ?thesis
  1051     using closed_Int_compact [OF \<open>closed S\<close>] compact_eq_bounded_closed
  1052     by (auto simp add: vimage_def)
  1053 qed
  1054 
  1055 lemma proper_map_polyfun_univ:
  1056     fixes c :: "nat \<Rightarrow> 'a::{real_normed_div_algebra,heine_borel}"
  1057   assumes "compact K" "c i \<noteq> 0" "1 \<le> i" "i \<le> n"
  1058     shows "compact ({z. (\<Sum>i\<le>n. c i * z^i) \<in> K})"
  1059   using proper_map_polyfun [of UNIV K c i n] assms by simp
  1060 
  1061 lemma proper_map_polyfun_eq:
  1062   assumes "f holomorphic_on UNIV"
  1063     shows "(\<forall>k. compact k \<longrightarrow> compact {z. f z \<in> k}) \<longleftrightarrow>
  1064            (\<exists>c n. 0 < n \<and> (c n \<noteq> 0) \<and> f = (\<lambda>z. \<Sum>i\<le>n. c i * z^i))"
  1065           (is "?lhs = ?rhs")
  1066 proof
  1067   assume compf [rule_format]: ?lhs
  1068   have 2: "\<exists>k. 0 < k \<and> a k \<noteq> 0 \<and> f = (\<lambda>z. \<Sum>i \<le> k. a i * z ^ i)"
  1069         if "\<And>z. f z = (\<Sum>i\<le>n. a i * z ^ i)" for a n
  1070   proof (cases "\<forall>i\<le>n. 0<i \<longrightarrow> a i = 0")
  1071     case True
  1072     then have [simp]: "\<And>z. f z = a 0"
  1073       by (simp add: that sum_atMost_shift)
  1074     have False using compf [of "{a 0}"] by simp
  1075     then show ?thesis ..
  1076   next
  1077     case False
  1078     then obtain k where k: "0 < k" "k\<le>n" "a k \<noteq> 0" by force
  1079     define m where "m = (GREATEST k. k\<le>n \<and> a k \<noteq> 0)"
  1080     have m: "m\<le>n \<and> a m \<noteq> 0"
  1081       unfolding m_def
  1082       apply (rule GreatestI_nat [where b = n])
  1083       using k apply auto
  1084       done
  1085     have [simp]: "a i = 0" if "m < i" "i \<le> n" for i
  1086       using Greatest_le_nat [where b = "n" and P = "\<lambda>k. k\<le>n \<and> a k \<noteq> 0"]
  1087       using m_def not_le that by auto
  1088     have "k \<le> m"
  1089       unfolding m_def
  1090       apply (rule Greatest_le_nat [where b = "n"])
  1091       using k apply auto
  1092       done
  1093     with k m show ?thesis
  1094       by (rule_tac x=m in exI) (auto simp: that comm_monoid_add_class.sum.mono_neutral_right)
  1095   qed
  1096   have "((inverse \<circ> f) \<longlongrightarrow> 0) at_infinity"
  1097   proof (rule Lim_at_infinityI)
  1098     fix e::real assume "0 < e"
  1099     with compf [of "cball 0 (inverse e)"]
  1100     show "\<exists>B. \<forall>x. B \<le> cmod x \<longrightarrow> dist ((inverse \<circ> f) x) 0 \<le> e"
  1101       apply simp
  1102       apply (clarsimp simp add: compact_eq_bounded_closed bounded_pos norm_inverse)
  1103       apply (rule_tac x="b+1" in exI)
  1104       apply (metis inverse_inverse_eq less_add_same_cancel2 less_imp_inverse_less add.commute not_le not_less_iff_gr_or_eq order_trans zero_less_one)
  1105       done
  1106   qed
  1107   then show ?rhs
  1108     apply (rule pole_at_infinity [OF assms])
  1109     using 2 apply blast
  1110     done
  1111 next
  1112   assume ?rhs
  1113   then obtain c n where "0 < n" "c n \<noteq> 0" "f = (\<lambda>z. \<Sum>i\<le>n. c i * z ^ i)" by blast
  1114   then have "compact {z. f z \<in> k}" if "compact k" for k
  1115     by (auto intro: proper_map_polyfun_univ [OF that])
  1116   then show ?lhs by blast
  1117 qed
  1118 
  1119 subsection \<open>Relating invertibility and nonvanishing of derivative\<close>
  1120 
  1121 lemma has_complex_derivative_locally_injective:
  1122   assumes holf: "f holomorphic_on S"
  1123       and S: "\<xi> \<in> S" "open S"
  1124       and dnz: "deriv f \<xi> \<noteq> 0"
  1125   obtains r where "r > 0" "ball \<xi> r \<subseteq> S" "inj_on f (ball \<xi> r)"
  1126 proof -
  1127   have *: "\<exists>d>0. \<forall>x. dist \<xi> x < d \<longrightarrow> onorm (\<lambda>v. deriv f x * v - deriv f \<xi> * v) < e" if "e > 0" for e
  1128   proof -
  1129     have contdf: "continuous_on S (deriv f)"
  1130       by (simp add: holf holomorphic_deriv holomorphic_on_imp_continuous_on \<open>open S\<close>)
  1131     obtain \<delta> where "\<delta>>0" and \<delta>: "\<And>x. \<lbrakk>x \<in> S; dist x \<xi> \<le> \<delta>\<rbrakk> \<Longrightarrow> cmod (deriv f x - deriv f \<xi>) \<le> e/2"
  1132       using continuous_onE [OF contdf \<open>\<xi> \<in> S\<close>, of "e/2"] \<open>0 < e\<close>
  1133       by (metis dist_complex_def half_gt_zero less_imp_le)
  1134     obtain \<epsilon> where "\<epsilon>>0" "ball \<xi> \<epsilon> \<subseteq> S"
  1135       by (metis openE [OF \<open>open S\<close> \<open>\<xi> \<in> S\<close>])
  1136     with \<open>\<delta>>0\<close> have "\<exists>\<delta>>0. \<forall>x. dist \<xi> x < \<delta> \<longrightarrow> onorm (\<lambda>v. deriv f x * v - deriv f \<xi> * v) \<le> e/2"
  1137       apply (rule_tac x="min \<delta> \<epsilon>" in exI)
  1138       apply (intro conjI allI impI Operator_Norm.onorm_le)
  1139       apply simp
  1140       apply (simp only: Rings.ring_class.left_diff_distrib [symmetric] norm_mult)
  1141       apply (rule mult_right_mono [OF \<delta>])
  1142       apply (auto simp: dist_commute Rings.ordered_semiring_class.mult_right_mono \<delta>)
  1143       done
  1144     with \<open>e>0\<close> show ?thesis by force
  1145   qed
  1146   have "inj ((*) (deriv f \<xi>))"
  1147     using dnz by simp
  1148   then obtain g' where g': "linear g'" "g' \<circ> (*) (deriv f \<xi>) = id"
  1149     using linear_injective_left_inverse [of "(*) (deriv f \<xi>)"]
  1150     by (auto simp: linear_times)
  1151   show ?thesis
  1152     apply (rule has_derivative_locally_injective [OF S, where f=f and f' = "\<lambda>z h. deriv f z * h" and g' = g'])
  1153     using g' *
  1154     apply (simp_all add: linear_conv_bounded_linear that)
  1155     using DERIV_deriv_iff_field_differentiable has_field_derivative_imp_has_derivative holf
  1156         holomorphic_on_imp_differentiable_at \<open>open S\<close> apply blast
  1157     done
  1158 qed
  1159 
  1160 lemma has_complex_derivative_locally_invertible:
  1161   assumes holf: "f holomorphic_on S"
  1162       and S: "\<xi> \<in> S" "open S"
  1163       and dnz: "deriv f \<xi> \<noteq> 0"
  1164   obtains r where "r > 0" "ball \<xi> r \<subseteq> S" "open (f `  (ball \<xi> r))" "inj_on f (ball \<xi> r)"
  1165 proof -
  1166   obtain r where "r > 0" "ball \<xi> r \<subseteq> S" "inj_on f (ball \<xi> r)"
  1167     by (blast intro: that has_complex_derivative_locally_injective [OF assms])
  1168   then have \<xi>: "\<xi> \<in> ball \<xi> r" by simp
  1169   then have nc: "\<not> f constant_on ball \<xi> r"
  1170     using \<open>inj_on f (ball \<xi> r)\<close> injective_not_constant by fastforce
  1171   have holf': "f holomorphic_on ball \<xi> r"
  1172     using \<open>ball \<xi> r \<subseteq> S\<close> holf holomorphic_on_subset by blast
  1173   have "open (f ` ball \<xi> r)"
  1174     apply (rule open_mapping_thm [OF holf'])
  1175     using nc apply auto
  1176     done
  1177   then show ?thesis
  1178     using \<open>0 < r\<close> \<open>ball \<xi> r \<subseteq> S\<close> \<open>inj_on f (ball \<xi> r)\<close> that  by blast
  1179 qed
  1180 
  1181 lemma holomorphic_injective_imp_regular:
  1182   assumes holf: "f holomorphic_on S"
  1183       and "open S" and injf: "inj_on f S"
  1184       and "\<xi> \<in> S"
  1185     shows "deriv f \<xi> \<noteq> 0"
  1186 proof -
  1187   obtain r where "r>0" and r: "ball \<xi> r \<subseteq> S" using assms by (blast elim!: openE)
  1188   have holf': "f holomorphic_on ball \<xi> r"
  1189     using \<open>ball \<xi> r \<subseteq> S\<close> holf holomorphic_on_subset by blast
  1190   show ?thesis
  1191   proof (cases "\<forall>n>0. (deriv ^^ n) f \<xi> = 0")
  1192     case True
  1193     have fcon: "f w = f \<xi>" if "w \<in> ball \<xi> r" for w
  1194       apply (rule holomorphic_fun_eq_const_on_connected [OF holf'])
  1195       using True \<open>0 < r\<close> that by auto
  1196     have False
  1197       using fcon [of "\<xi> + r/2"] \<open>0 < r\<close> r injf unfolding inj_on_def
  1198       by (metis \<open>\<xi> \<in> S\<close> contra_subsetD dist_commute fcon mem_ball perfect_choose_dist)
  1199     then show ?thesis ..
  1200   next
  1201     case False
  1202     then obtain n0 where n0: "n0 > 0 \<and> (deriv ^^ n0) f \<xi> \<noteq> 0" by blast
  1203     define n where [abs_def]: "n = (LEAST n. n > 0 \<and> (deriv ^^ n) f \<xi> \<noteq> 0)"
  1204     have n_ne: "n > 0" "(deriv ^^ n) f \<xi> \<noteq> 0"
  1205       using def_LeastI [OF n_def n0] by auto
  1206     have n_min: "\<And>k. 0 < k \<Longrightarrow> k < n \<Longrightarrow> (deriv ^^ k) f \<xi> = 0"
  1207       using def_Least_le [OF n_def] not_le by auto
  1208     obtain g \<delta> where "0 < \<delta>"
  1209              and holg: "g holomorphic_on ball \<xi> \<delta>"
  1210              and fd: "\<And>w. w \<in> ball \<xi> \<delta> \<Longrightarrow> f w - f \<xi> = ((w - \<xi>) * g w) ^ n"
  1211              and gnz: "\<And>w. w \<in> ball \<xi> \<delta> \<Longrightarrow> g w \<noteq> 0"
  1212       apply (rule holomorphic_factor_order_of_zero_strong [OF holf \<open>open S\<close> \<open>\<xi> \<in> S\<close> n_ne])
  1213       apply (blast intro: n_min)+
  1214       done
  1215     show ?thesis
  1216     proof (cases "n=1")
  1217       case True
  1218       with n_ne show ?thesis by auto
  1219     next
  1220       case False
  1221       have holgw: "(\<lambda>w. (w - \<xi>) * g w) holomorphic_on ball \<xi> (min r \<delta>)"
  1222         apply (rule holomorphic_intros)+
  1223         using holg by (simp add: holomorphic_on_subset subset_ball)
  1224       have gd: "\<And>w. dist \<xi> w < \<delta> \<Longrightarrow> (g has_field_derivative deriv g w) (at w)"
  1225         using holg
  1226         by (simp add: DERIV_deriv_iff_field_differentiable holomorphic_on_def at_within_open_NO_MATCH)
  1227       have *: "\<And>w. w \<in> ball \<xi> (min r \<delta>)
  1228             \<Longrightarrow> ((\<lambda>w. (w - \<xi>) * g w) has_field_derivative ((w - \<xi>) * deriv g w + g w))
  1229                 (at w)"
  1230         by (rule gd derivative_eq_intros | simp)+
  1231       have [simp]: "deriv (\<lambda>w. (w - \<xi>) * g w) \<xi> \<noteq> 0"
  1232         using * [of \<xi>] \<open>0 < \<delta>\<close> \<open>0 < r\<close> by (simp add: DERIV_imp_deriv gnz)
  1233       obtain T where "\<xi> \<in> T" "open T" and Tsb: "T \<subseteq> ball \<xi> (min r \<delta>)" and oimT: "open ((\<lambda>w. (w - \<xi>) * g w) ` T)"
  1234         apply (rule has_complex_derivative_locally_invertible [OF holgw, of \<xi>])
  1235         using \<open>0 < r\<close> \<open>0 < \<delta>\<close>
  1236         apply (simp_all add:)
  1237         by (meson open_ball centre_in_ball)
  1238       define U where "U = (\<lambda>w. (w - \<xi>) * g w) ` T"
  1239       have "open U" by (metis oimT U_def)
  1240       have "0 \<in> U"
  1241         apply (auto simp: U_def)
  1242         apply (rule image_eqI [where x = \<xi>])
  1243         apply (auto simp: \<open>\<xi> \<in> T\<close>)
  1244         done
  1245       then obtain \<epsilon> where "\<epsilon>>0" and \<epsilon>: "cball 0 \<epsilon> \<subseteq> U"
  1246         using \<open>open U\<close> open_contains_cball by blast
  1247       then have "\<epsilon> * exp(2 * of_real pi * \<i> * (0/n)) \<in> cball 0 \<epsilon>"
  1248                 "\<epsilon> * exp(2 * of_real pi * \<i> * (1/n)) \<in> cball 0 \<epsilon>"
  1249         by (auto simp: norm_mult)
  1250       with \<epsilon> have "\<epsilon> * exp(2 * of_real pi * \<i> * (0/n)) \<in> U"
  1251                   "\<epsilon> * exp(2 * of_real pi * \<i> * (1/n)) \<in> U" by blast+
  1252       then obtain y0 y1 where "y0 \<in> T" and y0: "(y0 - \<xi>) * g y0 = \<epsilon> * exp(2 * of_real pi * \<i> * (0/n))"
  1253                           and "y1 \<in> T" and y1: "(y1 - \<xi>) * g y1 = \<epsilon> * exp(2 * of_real pi * \<i> * (1/n))"
  1254         by (auto simp: U_def)
  1255       then have "y0 \<in> ball \<xi> \<delta>" "y1 \<in> ball \<xi> \<delta>" using Tsb by auto
  1256       moreover have "y0 \<noteq> y1"
  1257         using y0 y1 \<open>\<epsilon> > 0\<close> complex_root_unity_eq_1 [of n 1] \<open>n > 0\<close> False by auto
  1258       moreover have "T \<subseteq> S"
  1259         by (meson Tsb min.cobounded1 order_trans r subset_ball)
  1260       ultimately have False
  1261         using inj_onD [OF injf, of y0 y1] \<open>y0 \<in> T\<close> \<open>y1 \<in> T\<close>
  1262         using fd [of y0] fd [of y1] complex_root_unity [of n 1] n_ne
  1263         apply (simp add: y0 y1 power_mult_distrib)
  1264         apply (force simp: algebra_simps)
  1265         done
  1266       then show ?thesis ..
  1267     qed
  1268   qed
  1269 qed
  1270 
  1271 text\<open>Hence a nice clean inverse function theorem\<close>
  1272 
  1273 proposition holomorphic_has_inverse:
  1274   assumes holf: "f holomorphic_on S"
  1275       and "open S" and injf: "inj_on f S"
  1276   obtains g where "g holomorphic_on (f ` S)"
  1277                   "\<And>z. z \<in> S \<Longrightarrow> deriv f z * deriv g (f z) = 1"
  1278                   "\<And>z. z \<in> S \<Longrightarrow> g(f z) = z"
  1279 proof -
  1280   have ofs: "open (f ` S)"
  1281     by (rule open_mapping_thm3 [OF assms])
  1282   have contf: "continuous_on S f"
  1283     by (simp add: holf holomorphic_on_imp_continuous_on)
  1284   have *: "(the_inv_into S f has_field_derivative inverse (deriv f z)) (at (f z))" if "z \<in> S" for z
  1285   proof -
  1286     have 1: "(f has_field_derivative deriv f z) (at z)"
  1287       using DERIV_deriv_iff_field_differentiable \<open>z \<in> S\<close> \<open>open S\<close> holf holomorphic_on_imp_differentiable_at
  1288       by blast
  1289     have 2: "deriv f z \<noteq> 0"
  1290       using \<open>z \<in> S\<close> \<open>open S\<close> holf holomorphic_injective_imp_regular injf by blast
  1291     show ?thesis
  1292       apply (rule has_field_derivative_inverse_strong [OF 1 2 \<open>open S\<close> \<open>z \<in> S\<close>])
  1293        apply (simp add: holf holomorphic_on_imp_continuous_on)
  1294       by (simp add: injf the_inv_into_f_f)
  1295   qed
  1296   show ?thesis
  1297     proof
  1298       show "the_inv_into S f holomorphic_on f ` S"
  1299         by (simp add: holomorphic_on_open ofs) (blast intro: *)
  1300     next
  1301       fix z assume "z \<in> S"
  1302       have "deriv f z \<noteq> 0"
  1303         using \<open>z \<in> S\<close> \<open>open S\<close> holf holomorphic_injective_imp_regular injf by blast
  1304       then show "deriv f z * deriv (the_inv_into S f) (f z) = 1"
  1305         using * [OF \<open>z \<in> S\<close>]  by (simp add: DERIV_imp_deriv)
  1306     next
  1307       fix z assume "z \<in> S"
  1308       show "the_inv_into S f (f z) = z"
  1309         by (simp add: \<open>z \<in> S\<close> injf the_inv_into_f_f)
  1310   qed
  1311 qed
  1312 
  1313 subsection\<open>The Schwarz Lemma\<close>
  1314 
  1315 lemma Schwarz1:
  1316   assumes holf: "f holomorphic_on S"
  1317       and contf: "continuous_on (closure S) f"
  1318       and S: "open S" "connected S"
  1319       and boS: "bounded S"
  1320       and "S \<noteq> {}"
  1321   obtains w where "w \<in> frontier S"
  1322        "\<And>z. z \<in> closure S \<Longrightarrow> norm (f z) \<le> norm (f w)"
  1323 proof -
  1324   have connf: "continuous_on (closure S) (norm o f)"
  1325     using contf continuous_on_compose continuous_on_norm_id by blast
  1326   have coc: "compact (closure S)"
  1327     by (simp add: \<open>bounded S\<close> bounded_closure compact_eq_bounded_closed)
  1328   then obtain x where x: "x \<in> closure S" and xmax: "\<And>z. z \<in> closure S \<Longrightarrow> norm(f z) \<le> norm(f x)"
  1329     apply (rule bexE [OF continuous_attains_sup [OF _ _ connf]])
  1330     using \<open>S \<noteq> {}\<close> apply auto
  1331     done
  1332   then show ?thesis
  1333   proof (cases "x \<in> frontier S")
  1334     case True
  1335     then show ?thesis using that xmax by blast
  1336   next
  1337     case False
  1338     then have "x \<in> S"
  1339       using \<open>open S\<close> frontier_def interior_eq x by auto
  1340     then have "f constant_on S"
  1341       apply (rule maximum_modulus_principle [OF holf S \<open>open S\<close> order_refl])
  1342       using closure_subset apply (blast intro: xmax)
  1343       done
  1344     then have "f constant_on (closure S)"
  1345       by (rule constant_on_closureI [OF _ contf])
  1346     then obtain c where c: "\<And>x. x \<in> closure S \<Longrightarrow> f x = c"
  1347       by (meson constant_on_def)
  1348     obtain w where "w \<in> frontier S"
  1349       by (metis coc all_not_in_conv assms(6) closure_UNIV frontier_eq_empty not_compact_UNIV)
  1350     then show ?thesis
  1351       by (simp add: c frontier_def that)
  1352   qed
  1353 qed
  1354 
  1355 lemma Schwarz2:
  1356  "\<lbrakk>f holomorphic_on ball 0 r;
  1357     0 < s; ball w s \<subseteq> ball 0 r;
  1358     \<And>z. norm (w-z) < s \<Longrightarrow> norm(f z) \<le> norm(f w)\<rbrakk>
  1359     \<Longrightarrow> f constant_on ball 0 r"
  1360 by (rule maximum_modulus_principle [where U = "ball w s" and \<xi> = w]) (simp_all add: dist_norm)
  1361 
  1362 lemma Schwarz3:
  1363   assumes holf: "f holomorphic_on (ball 0 r)" and [simp]: "f 0 = 0"
  1364   obtains h where "h holomorphic_on (ball 0 r)" and "\<And>z. norm z < r \<Longrightarrow> f z = z * (h z)" and "deriv f 0 = h 0"
  1365 proof -
  1366   define h where "h z = (if z = 0 then deriv f 0 else f z / z)" for z
  1367   have d0: "deriv f 0 = h 0"
  1368     by (simp add: h_def)
  1369   moreover have "h holomorphic_on (ball 0 r)"
  1370     by (rule pole_theorem_open_0 [OF holf, of 0]) (auto simp: h_def)
  1371   moreover have "norm z < r \<Longrightarrow> f z = z * h z" for z
  1372     by (simp add: h_def)
  1373   ultimately show ?thesis
  1374     using that by blast
  1375 qed
  1376 
  1377 proposition Schwarz_Lemma:
  1378   assumes holf: "f holomorphic_on (ball 0 1)" and [simp]: "f 0 = 0"
  1379       and no: "\<And>z. norm z < 1 \<Longrightarrow> norm (f z) < 1"
  1380       and \<xi>: "norm \<xi> < 1"
  1381     shows "norm (f \<xi>) \<le> norm \<xi>" and "norm(deriv f 0) \<le> 1"
  1382       and "((\<exists>z. norm z < 1 \<and> z \<noteq> 0 \<and> norm(f z) = norm z) 
  1383             \<or> norm(deriv f 0) = 1)
  1384            \<Longrightarrow> \<exists>\<alpha>. (\<forall>z. norm z < 1 \<longrightarrow> f z = \<alpha> * z) \<and> norm \<alpha> = 1" 
  1385       (is "?P \<Longrightarrow> ?Q")
  1386 proof -
  1387   obtain h where holh: "h holomorphic_on (ball 0 1)"
  1388              and fz_eq: "\<And>z. norm z < 1 \<Longrightarrow> f z = z * (h z)" and df0: "deriv f 0 = h 0"
  1389     by (rule Schwarz3 [OF holf]) auto
  1390   have noh_le: "norm (h z) \<le> 1" if z: "norm z < 1" for z
  1391   proof -
  1392     have "norm (h z) < a" if a: "1 < a" for a
  1393     proof -
  1394       have "max (inverse a) (norm z) < 1"
  1395         using z a by (simp_all add: inverse_less_1_iff)
  1396       then obtain r where r: "max (inverse a) (norm z) < r" and "r < 1"
  1397         using Rats_dense_in_real by blast
  1398       then have nzr: "norm z < r" and ira: "inverse r < a"
  1399         using z a less_imp_inverse_less by force+
  1400       then have "0 < r"
  1401         by (meson norm_not_less_zero not_le order.strict_trans2)
  1402       have holh': "h holomorphic_on ball 0 r"
  1403         by (meson holh \<open>r < 1\<close> holomorphic_on_subset less_eq_real_def subset_ball)
  1404       have conth': "continuous_on (cball 0 r) h"
  1405         by (meson \<open>r < 1\<close> dual_order.trans holh holomorphic_on_imp_continuous_on holomorphic_on_subset mem_ball_0 mem_cball_0 not_less subsetI)
  1406       obtain w where w: "norm w = r" and lenw: "\<And>z. norm z < r \<Longrightarrow> norm(h z) \<le> norm(h w)"
  1407         apply (rule Schwarz1 [OF holh']) using conth' \<open>0 < r\<close> by auto
  1408       have "h w = f w / w" using fz_eq \<open>r < 1\<close> nzr w by auto
  1409       then have "cmod (h z) < inverse r"
  1410         by (metis \<open>0 < r\<close> \<open>r < 1\<close> divide_strict_right_mono inverse_eq_divide
  1411                   le_less_trans lenw no norm_divide nzr w)
  1412       then show ?thesis using ira by linarith
  1413     qed
  1414     then show "norm (h z) \<le> 1"
  1415       using not_le by blast
  1416   qed
  1417   show "cmod (f \<xi>) \<le> cmod \<xi>"
  1418   proof (cases "\<xi> = 0")
  1419     case True then show ?thesis by auto
  1420   next
  1421     case False
  1422     then show ?thesis
  1423       by (simp add: noh_le fz_eq \<xi> mult_left_le norm_mult)
  1424   qed
  1425   show no_df0: "norm(deriv f 0) \<le> 1"
  1426     by (simp add: \<open>\<And>z. cmod z < 1 \<Longrightarrow> cmod (h z) \<le> 1\<close> df0)
  1427   show "?Q" if "?P"
  1428     using that
  1429   proof
  1430     assume "\<exists>z. cmod z < 1 \<and> z \<noteq> 0 \<and> cmod (f z) = cmod z"
  1431     then obtain \<gamma> where \<gamma>: "cmod \<gamma> < 1" "\<gamma> \<noteq> 0" "cmod (f \<gamma>) = cmod \<gamma>" by blast
  1432     then have [simp]: "norm (h \<gamma>) = 1"
  1433       by (simp add: fz_eq norm_mult)
  1434     have "ball \<gamma> (1 - cmod \<gamma>) \<subseteq> ball 0 1"
  1435       by (simp add: ball_subset_ball_iff)
  1436     moreover have "\<And>z. cmod (\<gamma> - z) < 1 - cmod \<gamma> \<Longrightarrow> cmod (h z) \<le> cmod (h \<gamma>)"
  1437       apply (simp add: algebra_simps)
  1438       by (metis add_diff_cancel_left' diff_diff_eq2 le_less_trans noh_le norm_triangle_ineq4)
  1439     ultimately obtain c where c: "\<And>z. norm z < 1 \<Longrightarrow> h z = c"
  1440       using Schwarz2 [OF holh, of "1 - norm \<gamma>" \<gamma>, unfolded constant_on_def] \<gamma> by auto
  1441     then have "norm c = 1"
  1442       using \<gamma> by force
  1443     with c show ?thesis
  1444       using fz_eq by auto
  1445   next
  1446     assume [simp]: "cmod (deriv f 0) = 1"
  1447     then obtain c where c: "\<And>z. norm z < 1 \<Longrightarrow> h z = c"
  1448       using Schwarz2 [OF holh zero_less_one, of 0, unfolded constant_on_def] df0 noh_le
  1449       by auto
  1450     moreover have "norm c = 1"  using df0 c by auto
  1451     ultimately show ?thesis
  1452       using fz_eq by auto
  1453   qed
  1454 qed
  1455 
  1456 corollary Schwarz_Lemma':
  1457   assumes holf: "f holomorphic_on (ball 0 1)" and [simp]: "f 0 = 0"
  1458       and no: "\<And>z. norm z < 1 \<Longrightarrow> norm (f z) < 1"
  1459     shows "((\<forall>\<xi>. norm \<xi> < 1 \<longrightarrow> norm (f \<xi>) \<le> norm \<xi>) 
  1460             \<and> norm(deriv f 0) \<le> 1) 
  1461             \<and> (((\<exists>z. norm z < 1 \<and> z \<noteq> 0 \<and> norm(f z) = norm z) 
  1462               \<or> norm(deriv f 0) = 1)
  1463               \<longrightarrow> (\<exists>\<alpha>. (\<forall>z. norm z < 1 \<longrightarrow> f z = \<alpha> * z) \<and> norm \<alpha> = 1))"
  1464   using Schwarz_Lemma [OF assms]
  1465   by (metis (no_types) norm_eq_zero zero_less_one)
  1466 
  1467 subsection\<open>The Schwarz reflection principle\<close>
  1468 
  1469 lemma hol_pal_lem0:
  1470   assumes "d \<bullet> a \<le> k" "k \<le> d \<bullet> b"
  1471   obtains c where
  1472      "c \<in> closed_segment a b" "d \<bullet> c = k"
  1473      "\<And>z. z \<in> closed_segment a c \<Longrightarrow> d \<bullet> z \<le> k"
  1474      "\<And>z. z \<in> closed_segment c b \<Longrightarrow> k \<le> d \<bullet> z"
  1475 proof -
  1476   obtain c where cin: "c \<in> closed_segment a b" and keq: "k = d \<bullet> c"
  1477     using connected_ivt_hyperplane [of "closed_segment a b" a b d k]
  1478     by (auto simp: assms)
  1479   have "closed_segment a c \<subseteq> {z. d \<bullet> z \<le> k}"  "closed_segment c b \<subseteq> {z. k \<le> d \<bullet> z}"
  1480     unfolding segment_convex_hull using assms keq
  1481     by (auto simp: convex_halfspace_le convex_halfspace_ge hull_minimal)
  1482   then show ?thesis using cin that by fastforce
  1483 qed
  1484 
  1485 lemma hol_pal_lem1:
  1486   assumes "convex S" "open S"
  1487       and abc: "a \<in> S" "b \<in> S" "c \<in> S"
  1488           "d \<noteq> 0" and lek: "d \<bullet> a \<le> k" "d \<bullet> b \<le> k" "d \<bullet> c \<le> k"
  1489       and holf1: "f holomorphic_on {z. z \<in> S \<and> d \<bullet> z < k}"
  1490       and contf: "continuous_on S f"
  1491     shows "contour_integral (linepath a b) f +
  1492            contour_integral (linepath b c) f +
  1493            contour_integral (linepath c a) f = 0"
  1494 proof -
  1495   have "interior (convex hull {a, b, c}) \<subseteq> interior(S \<inter> {x. d \<bullet> x \<le> k})"
  1496     apply (rule interior_mono)
  1497     apply (rule hull_minimal)
  1498      apply (simp add: abc lek)
  1499     apply (rule convex_Int [OF \<open>convex S\<close> convex_halfspace_le])
  1500     done
  1501   also have "... \<subseteq> {z \<in> S. d \<bullet> z < k}"
  1502     by (force simp: interior_open [OF \<open>open S\<close>] \<open>d \<noteq> 0\<close>)
  1503   finally have *: "interior (convex hull {a, b, c}) \<subseteq> {z \<in> S. d \<bullet> z < k}" .
  1504   have "continuous_on (convex hull {a,b,c}) f"
  1505     using \<open>convex S\<close> contf abc continuous_on_subset subset_hull
  1506     by fastforce
  1507   moreover have "f holomorphic_on interior (convex hull {a,b,c})"
  1508     by (rule holomorphic_on_subset [OF holf1 *])
  1509   ultimately show ?thesis
  1510     using Cauchy_theorem_triangle_interior has_chain_integral_chain_integral3
  1511       by blast
  1512 qed
  1513 
  1514 lemma hol_pal_lem2:
  1515   assumes S: "convex S" "open S"
  1516       and abc: "a \<in> S" "b \<in> S" "c \<in> S"
  1517       and "d \<noteq> 0" and lek: "d \<bullet> a \<le> k" "d \<bullet> b \<le> k"
  1518       and holf1: "f holomorphic_on {z. z \<in> S \<and> d \<bullet> z < k}"
  1519       and holf2: "f holomorphic_on {z. z \<in> S \<and> k < d \<bullet> z}"
  1520       and contf: "continuous_on S f"
  1521     shows "contour_integral (linepath a b) f +
  1522            contour_integral (linepath b c) f +
  1523            contour_integral (linepath c a) f = 0"
  1524 proof (cases "d \<bullet> c \<le> k")
  1525   case True show ?thesis
  1526     by (rule hol_pal_lem1 [OF S abc \<open>d \<noteq> 0\<close> lek True holf1 contf])
  1527 next
  1528   case False
  1529   then have "d \<bullet> c > k" by force
  1530   obtain a' where a': "a' \<in> closed_segment b c" and "d \<bullet> a' = k"
  1531      and ba': "\<And>z. z \<in> closed_segment b a' \<Longrightarrow> d \<bullet> z \<le> k"
  1532      and a'c: "\<And>z. z \<in> closed_segment a' c \<Longrightarrow> k \<le> d \<bullet> z"
  1533     apply (rule hol_pal_lem0 [of d b k c, OF \<open>d \<bullet> b \<le> k\<close>])
  1534     using False by auto
  1535   obtain b' where b': "b' \<in> closed_segment a c" and "d \<bullet> b' = k"
  1536      and ab': "\<And>z. z \<in> closed_segment a b' \<Longrightarrow> d \<bullet> z \<le> k"
  1537      and b'c: "\<And>z. z \<in> closed_segment b' c \<Longrightarrow> k \<le> d \<bullet> z"
  1538     apply (rule hol_pal_lem0 [of d a k c, OF \<open>d \<bullet> a \<le> k\<close>])
  1539     using False by auto
  1540   have a'b': "a' \<in> S \<and> b' \<in> S"
  1541     using a' abc b' convex_contains_segment \<open>convex S\<close> by auto
  1542   have "continuous_on (closed_segment c a) f"
  1543     by (meson abc contf continuous_on_subset convex_contains_segment \<open>convex S\<close>)
  1544   then have 1: "contour_integral (linepath c a) f =
  1545                 contour_integral (linepath c b') f + contour_integral (linepath b' a) f"
  1546     apply (rule contour_integral_split_linepath)
  1547     using b' by (simp add: closed_segment_commute)
  1548   have "continuous_on (closed_segment b c) f"
  1549     by (meson abc contf continuous_on_subset convex_contains_segment \<open>convex S\<close>)
  1550   then have 2: "contour_integral (linepath b c) f =
  1551                 contour_integral (linepath b a') f + contour_integral (linepath a' c) f"
  1552     by (rule contour_integral_split_linepath [OF _ a'])
  1553   have 3: "contour_integral (reversepath (linepath b' a')) f =
  1554                 - contour_integral (linepath b' a') f"
  1555     by (rule contour_integral_reversepath [OF valid_path_linepath])
  1556   have fcd_le: "f field_differentiable at x"
  1557                if "x \<in> interior S \<and> x \<in> interior {x. d \<bullet> x \<le> k}" for x
  1558   proof -
  1559     have "f holomorphic_on S \<inter> {c. d \<bullet> c < k}"
  1560       by (metis (no_types) Collect_conj_eq Collect_mem_eq holf1)
  1561     then have "\<exists>C D. x \<in> interior C \<inter> interior D \<and> f holomorphic_on interior C \<inter> interior D"
  1562       using that
  1563       by (metis Collect_mem_eq Int_Collect \<open>d \<noteq> 0\<close> interior_halfspace_le interior_open \<open>open S\<close>)
  1564     then show "f field_differentiable at x"
  1565       by (metis at_within_interior holomorphic_on_def interior_Int interior_interior)
  1566   qed
  1567   have ab_le: "\<And>x. x \<in> closed_segment a b \<Longrightarrow> d \<bullet> x \<le> k"
  1568   proof -
  1569     fix x :: complex
  1570     assume "x \<in> closed_segment a b"
  1571     then have "\<And>C. x \<in> C \<or> b \<notin> C \<or> a \<notin> C \<or> \<not> convex C"
  1572       by (meson contra_subsetD convex_contains_segment)
  1573     then show "d \<bullet> x \<le> k"
  1574       by (metis lek convex_halfspace_le mem_Collect_eq)
  1575   qed
  1576   have "continuous_on (S \<inter> {x. d \<bullet> x \<le> k}) f" using contf
  1577     by (simp add: continuous_on_subset)
  1578   then have "(f has_contour_integral 0)
  1579          (linepath a b +++ linepath b a' +++ linepath a' b' +++ linepath b' a)"
  1580     apply (rule Cauchy_theorem_convex [where K = "{}"])
  1581     apply (simp_all add: path_image_join convex_Int convex_halfspace_le \<open>convex S\<close> fcd_le ab_le
  1582                 closed_segment_subset abc a'b' ba')
  1583     by (metis \<open>d \<bullet> a' = k\<close> \<open>d \<bullet> b' = k\<close> convex_contains_segment convex_halfspace_le lek(1) mem_Collect_eq order_refl)
  1584   then have 4: "contour_integral (linepath a b) f +
  1585                 contour_integral (linepath b a') f +
  1586                 contour_integral (linepath a' b') f +
  1587                 contour_integral (linepath b' a) f = 0"
  1588     by (rule has_chain_integral_chain_integral4)
  1589   have fcd_ge: "f field_differentiable at x"
  1590                if "x \<in> interior S \<and> x \<in> interior {x. k \<le> d \<bullet> x}" for x
  1591   proof -
  1592     have f2: "f holomorphic_on S \<inter> {c. k < d \<bullet> c}"
  1593       by (metis (full_types) Collect_conj_eq Collect_mem_eq holf2)
  1594     have f3: "interior S = S"
  1595       by (simp add: interior_open \<open>open S\<close>)
  1596     then have "x \<in> S \<inter> interior {c. k \<le> d \<bullet> c}"
  1597       using that by simp
  1598     then show "f field_differentiable at x"
  1599       using f3 f2 unfolding holomorphic_on_def
  1600       by (metis (no_types) \<open>d \<noteq> 0\<close> at_within_interior interior_Int interior_halfspace_ge interior_interior)
  1601   qed
  1602   have "continuous_on (S \<inter> {x. k \<le> d \<bullet> x}) f" using contf
  1603     by (simp add: continuous_on_subset)
  1604   then have "(f has_contour_integral 0) (linepath a' c +++ linepath c b' +++ linepath b' a')"
  1605     apply (rule Cauchy_theorem_convex [where K = "{}"])
  1606     apply (simp_all add: path_image_join convex_Int convex_halfspace_ge \<open>convex S\<close>
  1607                       fcd_ge closed_segment_subset abc a'b' a'c)
  1608     by (metis \<open>d \<bullet> a' = k\<close> b'c closed_segment_commute convex_contains_segment
  1609               convex_halfspace_ge ends_in_segment(2) mem_Collect_eq order_refl)
  1610   then have 5: "contour_integral (linepath a' c) f + contour_integral (linepath c b') f + contour_integral (linepath b' a') f = 0"
  1611     by (rule has_chain_integral_chain_integral3)
  1612   show ?thesis
  1613     using 1 2 3 4 5 by (metis add.assoc eq_neg_iff_add_eq_0 reversepath_linepath)
  1614 qed
  1615 
  1616 lemma hol_pal_lem3:
  1617   assumes S: "convex S" "open S"
  1618       and abc: "a \<in> S" "b \<in> S" "c \<in> S"
  1619       and "d \<noteq> 0" and lek: "d \<bullet> a \<le> k"
  1620       and holf1: "f holomorphic_on {z. z \<in> S \<and> d \<bullet> z < k}"
  1621       and holf2: "f holomorphic_on {z. z \<in> S \<and> k < d \<bullet> z}"
  1622       and contf: "continuous_on S f"
  1623     shows "contour_integral (linepath a b) f +
  1624            contour_integral (linepath b c) f +
  1625            contour_integral (linepath c a) f = 0"
  1626 proof (cases "d \<bullet> b \<le> k")
  1627   case True show ?thesis
  1628     by (rule hol_pal_lem2 [OF S abc \<open>d \<noteq> 0\<close> lek True holf1 holf2 contf])
  1629 next
  1630   case False
  1631   show ?thesis
  1632   proof (cases "d \<bullet> c \<le> k")
  1633     case True
  1634     have "contour_integral (linepath c a) f +
  1635           contour_integral (linepath a b) f +
  1636           contour_integral (linepath b c) f = 0"
  1637       by (rule hol_pal_lem2 [OF S \<open>c \<in> S\<close> \<open>a \<in> S\<close> \<open>b \<in> S\<close> \<open>d \<noteq> 0\<close> \<open>d \<bullet> c \<le> k\<close> lek holf1 holf2 contf])
  1638     then show ?thesis
  1639       by (simp add: algebra_simps)
  1640   next
  1641     case False
  1642     have "contour_integral (linepath b c) f +
  1643           contour_integral (linepath c a) f +
  1644           contour_integral (linepath a b) f = 0"
  1645       apply (rule hol_pal_lem2 [OF S \<open>b \<in> S\<close> \<open>c \<in> S\<close> \<open>a \<in> S\<close>, of "-d" "-k"])
  1646       using \<open>d \<noteq> 0\<close> \<open>\<not> d \<bullet> b \<le> k\<close> False by (simp_all add: holf1 holf2 contf)
  1647     then show ?thesis
  1648       by (simp add: algebra_simps)
  1649   qed
  1650 qed
  1651 
  1652 lemma hol_pal_lem4:
  1653   assumes S: "convex S" "open S"
  1654       and abc: "a \<in> S" "b \<in> S" "c \<in> S" and "d \<noteq> 0"
  1655       and holf1: "f holomorphic_on {z. z \<in> S \<and> d \<bullet> z < k}"
  1656       and holf2: "f holomorphic_on {z. z \<in> S \<and> k < d \<bullet> z}"
  1657       and contf: "continuous_on S f"
  1658     shows "contour_integral (linepath a b) f +
  1659            contour_integral (linepath b c) f +
  1660            contour_integral (linepath c a) f = 0"
  1661 proof (cases "d \<bullet> a \<le> k")
  1662   case True show ?thesis
  1663     by (rule hol_pal_lem3 [OF S abc \<open>d \<noteq> 0\<close> True holf1 holf2 contf])
  1664 next
  1665   case False
  1666   show ?thesis
  1667     apply (rule hol_pal_lem3 [OF S abc, of "-d" "-k"])
  1668     using \<open>d \<noteq> 0\<close> False by (simp_all add: holf1 holf2 contf)
  1669 qed
  1670 
  1671 lemma holomorphic_on_paste_across_line:
  1672   assumes S: "open S" and "d \<noteq> 0"
  1673       and holf1: "f holomorphic_on (S \<inter> {z. d \<bullet> z < k})"
  1674       and holf2: "f holomorphic_on (S \<inter> {z. k < d \<bullet> z})"
  1675       and contf: "continuous_on S f"
  1676     shows "f holomorphic_on S"
  1677 proof -
  1678   have *: "\<exists>t. open t \<and> p \<in> t \<and> continuous_on t f \<and>
  1679                (\<forall>a b c. convex hull {a, b, c} \<subseteq> t \<longrightarrow>
  1680                          contour_integral (linepath a b) f +
  1681                          contour_integral (linepath b c) f +
  1682                          contour_integral (linepath c a) f = 0)"
  1683           if "p \<in> S" for p
  1684   proof -
  1685     obtain e where "e>0" and e: "ball p e \<subseteq> S"
  1686       using \<open>p \<in> S\<close> openE S by blast
  1687     then have "continuous_on (ball p e) f"
  1688       using contf continuous_on_subset by blast
  1689     moreover have "f holomorphic_on {z. dist p z < e \<and> d \<bullet> z < k}"
  1690       apply (rule holomorphic_on_subset [OF holf1])
  1691       using e by auto
  1692     moreover have "f holomorphic_on {z. dist p z < e \<and> k < d \<bullet> z}"
  1693       apply (rule holomorphic_on_subset [OF holf2])
  1694       using e by auto
  1695     ultimately show ?thesis
  1696       apply (rule_tac x="ball p e" in exI)
  1697       using \<open>e > 0\<close> e \<open>d \<noteq> 0\<close>
  1698       apply (simp add:, clarify)
  1699       apply (rule hol_pal_lem4 [of "ball p e" _ _ _ d _ k])
  1700       apply (auto simp: subset_hull)
  1701       done
  1702   qed
  1703   show ?thesis
  1704     by (blast intro: * Morera_local_triangle analytic_imp_holomorphic)
  1705 qed
  1706 
  1707 proposition Schwarz_reflection:
  1708   assumes "open S" and cnjs: "cnj ` S \<subseteq> S"
  1709       and  holf: "f holomorphic_on (S \<inter> {z. 0 < Im z})"
  1710       and contf: "continuous_on (S \<inter> {z. 0 \<le> Im z}) f"
  1711       and f: "\<And>z. \<lbrakk>z \<in> S; z \<in> \<real>\<rbrakk> \<Longrightarrow> (f z) \<in> \<real>"
  1712     shows "(\<lambda>z. if 0 \<le> Im z then f z else cnj(f(cnj z))) holomorphic_on S"
  1713 proof -
  1714   have 1: "(\<lambda>z. if 0 \<le> Im z then f z else cnj (f (cnj z))) holomorphic_on (S \<inter> {z. 0 < Im z})"
  1715     by (force intro: iffD1 [OF holomorphic_cong [OF refl] holf])
  1716   have cont_cfc: "continuous_on (S \<inter> {z. Im z \<le> 0}) (cnj o f o cnj)"
  1717     apply (intro continuous_intros continuous_on_compose continuous_on_subset [OF contf])
  1718     using cnjs apply auto
  1719     done
  1720   have "cnj \<circ> f \<circ> cnj field_differentiable at x within S \<inter> {z. Im z < 0}"
  1721         if "x \<in> S" "Im x < 0" "f field_differentiable at (cnj x) within S \<inter> {z. 0 < Im z}" for x
  1722     using that
  1723     apply (simp add: field_differentiable_def has_field_derivative_iff Lim_within dist_norm, clarify)
  1724     apply (rule_tac x="cnj f'" in exI)
  1725     apply (elim all_forward ex_forward conj_forward imp_forward asm_rl, clarify)
  1726     apply (drule_tac x="cnj xa" in bspec)
  1727     using cnjs apply force
  1728     apply (metis complex_cnj_cnj complex_cnj_diff complex_cnj_divide complex_mod_cnj)
  1729     done
  1730   then have hol_cfc: "(cnj o f o cnj) holomorphic_on (S \<inter> {z. Im z < 0})"
  1731     using holf cnjs
  1732     by (force simp: holomorphic_on_def)
  1733   have 2: "(\<lambda>z. if 0 \<le> Im z then f z else cnj (f (cnj z))) holomorphic_on (S \<inter> {z. Im z < 0})"
  1734     apply (rule iffD1 [OF holomorphic_cong [OF refl]])
  1735     using hol_cfc by auto
  1736   have [simp]: "(S \<inter> {z. 0 \<le> Im z}) \<union> (S \<inter> {z. Im z \<le> 0}) = S"
  1737     by force
  1738   have "continuous_on ((S \<inter> {z. 0 \<le> Im z}) \<union> (S \<inter> {z. Im z \<le> 0}))
  1739                        (\<lambda>z. if 0 \<le> Im z then f z else cnj (f (cnj z)))"
  1740     apply (rule continuous_on_cases_local)
  1741     using cont_cfc contf
  1742     apply (simp_all add: closedin_closed_Int closed_halfspace_Im_le closed_halfspace_Im_ge)
  1743     using f Reals_cnj_iff complex_is_Real_iff apply auto
  1744     done
  1745   then have 3: "continuous_on S (\<lambda>z. if 0 \<le> Im z then f z else cnj (f (cnj z)))"
  1746     by force
  1747   show ?thesis
  1748     apply (rule holomorphic_on_paste_across_line [OF \<open>open S\<close>, of "- \<i>" _ 0])
  1749     using 1 2 3
  1750     apply auto
  1751     done
  1752 qed
  1753 
  1754 subsection\<open>Bloch's theorem\<close>
  1755 
  1756 lemma Bloch_lemma_0:
  1757   assumes holf: "f holomorphic_on cball 0 r" and "0 < r"
  1758       and [simp]: "f 0 = 0"
  1759       and le: "\<And>z. norm z < r \<Longrightarrow> norm(deriv f z) \<le> 2 * norm(deriv f 0)"
  1760     shows "ball 0 ((3 - 2 * sqrt 2) * r * norm(deriv f 0)) \<subseteq> f ` ball 0 r"
  1761 proof -
  1762   have "sqrt 2 < 3/2"
  1763     by (rule real_less_lsqrt) (auto simp: power2_eq_square)
  1764   then have sq3: "0 < 3 - 2 * sqrt 2" by simp
  1765   show ?thesis
  1766   proof (cases "deriv f 0 = 0")
  1767     case True then show ?thesis by simp
  1768   next
  1769     case False
  1770     define C where "C = 2 * norm(deriv f 0)"
  1771     have "0 < C" using False by (simp add: C_def)
  1772     have holf': "f holomorphic_on ball 0 r" using holf
  1773       using ball_subset_cball holomorphic_on_subset by blast
  1774     then have holdf': "deriv f holomorphic_on ball 0 r"
  1775       by (rule holomorphic_deriv [OF _ open_ball])
  1776     have "Le1": "norm(deriv f z - deriv f 0) \<le> norm z / (r - norm z) * C"
  1777                 if "norm z < r" for z
  1778     proof -
  1779       have T1: "norm(deriv f z - deriv f 0) \<le> norm z / (R - norm z) * C"
  1780               if R: "norm z < R" "R < r" for R
  1781       proof -
  1782         have "0 < R" using R
  1783           by (metis less_trans norm_zero zero_less_norm_iff)
  1784         have df_le: "\<And>x. norm x < r \<Longrightarrow> norm (deriv f x) \<le> C"
  1785           using le by (simp add: C_def)
  1786         have hol_df: "deriv f holomorphic_on cball 0 R"
  1787           apply (rule holomorphic_on_subset) using R holdf' by auto
  1788         have *: "((\<lambda>w. deriv f w / (w - z)) has_contour_integral 2 * pi * \<i> * deriv f z) (circlepath 0 R)"
  1789                  if "norm z < R" for z
  1790           using \<open>0 < R\<close> that Cauchy_integral_formula_convex_simple [OF convex_cball hol_df, of _ "circlepath 0 R"]
  1791           by (force simp: winding_number_circlepath)
  1792         have **: "((\<lambda>x. deriv f x / (x - z) - deriv f x / x) has_contour_integral
  1793                    of_real (2 * pi) * \<i> * (deriv f z - deriv f 0))
  1794                   (circlepath 0 R)"
  1795            using has_contour_integral_diff [OF * [of z] * [of 0]] \<open>0 < R\<close> that
  1796            by (simp add: algebra_simps)
  1797         have [simp]: "\<And>x. norm x = R \<Longrightarrow> x \<noteq> z"  using that(1) by blast
  1798         have "norm (deriv f x / (x - z) - deriv f x / x)
  1799                      \<le> C * norm z / (R * (R - norm z))"
  1800                   if "norm x = R" for x
  1801         proof -
  1802           have [simp]: "norm (deriv f x * x - deriv f x * (x - z)) =
  1803                         norm (deriv f x) * norm z"
  1804             by (simp add: norm_mult right_diff_distrib')
  1805           show ?thesis
  1806             using  \<open>0 < R\<close> \<open>0 < C\<close> R that
  1807             apply (simp add: norm_mult norm_divide divide_simps)
  1808             using df_le norm_triangle_ineq2 \<open>0 < C\<close> apply (auto intro!: mult_mono)
  1809             done
  1810         qed
  1811         then show ?thesis
  1812           using has_contour_integral_bound_circlepath
  1813                   [OF **, of "C * norm z/(R*(R - norm z))"]
  1814                 \<open>0 < R\<close> \<open>0 < C\<close> R
  1815           apply (simp add: norm_mult norm_divide)
  1816           apply (simp add: divide_simps mult.commute)
  1817           done
  1818       qed
  1819       obtain r' where r': "norm z < r'" "r' < r"
  1820         using Rats_dense_in_real [of "norm z" r] \<open>norm z < r\<close> by blast
  1821       then have [simp]: "closure {r'<..<r} = {r'..r}" by simp
  1822       show ?thesis
  1823         apply (rule continuous_ge_on_closure
  1824                  [where f = "\<lambda>r. norm z / (r - norm z) * C" and s = "{r'<..<r}",
  1825                   OF _ _ T1])
  1826         apply (intro continuous_intros)
  1827         using that r'
  1828         apply (auto simp: not_le)
  1829         done
  1830     qed
  1831     have "*": "(norm z - norm z^2/(r - norm z)) * norm(deriv f 0) \<le> norm(f z)"
  1832               if r: "norm z < r" for z
  1833     proof -
  1834       have 1: "\<And>x. x \<in> ball 0 r \<Longrightarrow>
  1835               ((\<lambda>z. f z - deriv f 0 * z) has_field_derivative deriv f x - deriv f 0)
  1836                (at x within ball 0 r)"
  1837         by (rule derivative_eq_intros holomorphic_derivI holf' | simp)+
  1838       have 2: "closed_segment 0 z \<subseteq> ball 0 r"
  1839         by (metis \<open>0 < r\<close> convex_ball convex_contains_segment dist_self mem_ball mem_ball_0 that)
  1840       have 3: "(\<lambda>t. (norm z)\<^sup>2 * t / (r - norm z) * C) integrable_on {0..1}"
  1841         apply (rule integrable_on_cmult_right [where 'b=real, simplified])
  1842         apply (rule integrable_on_cdivide [where 'b=real, simplified])
  1843         apply (rule integrable_on_cmult_left [where 'b=real, simplified])
  1844         apply (rule ident_integrable_on)
  1845         done
  1846       have 4: "norm (deriv f (x *\<^sub>R z) - deriv f 0) * norm z \<le> norm z * norm z * x * C / (r - norm z)"
  1847               if x: "0 \<le> x" "x \<le> 1" for x
  1848       proof -
  1849         have [simp]: "x * norm z < r"
  1850           using r x by (meson le_less_trans mult_le_cancel_right2 norm_not_less_zero)
  1851         have "norm (deriv f (x *\<^sub>R z) - deriv f 0) \<le> norm (x *\<^sub>R z) / (r - norm (x *\<^sub>R z)) * C"
  1852           apply (rule Le1) using r x \<open>0 < r\<close> by simp
  1853         also have "... \<le> norm (x *\<^sub>R z) / (r - norm z) * C"
  1854           using r x \<open>0 < r\<close>
  1855           apply (simp add: divide_simps)
  1856           by (simp add: \<open>0 < C\<close> mult.assoc mult_left_le_one_le ordered_comm_semiring_class.comm_mult_left_mono)
  1857         finally have "norm (deriv f (x *\<^sub>R z) - deriv f 0) * norm z \<le> norm (x *\<^sub>R z)  / (r - norm z) * C * norm z"
  1858           by (rule mult_right_mono) simp
  1859         with x show ?thesis by (simp add: algebra_simps)
  1860       qed
  1861       have le_norm: "abc \<le> norm d - e \<Longrightarrow> norm(f - d) \<le> e \<Longrightarrow> abc \<le> norm f" for abc d e and f::complex
  1862         by (metis add_diff_cancel_left' add_diff_eq diff_left_mono norm_diff_ineq order_trans)
  1863       have "norm (integral {0..1} (\<lambda>x. (deriv f (x *\<^sub>R z) - deriv f 0) * z))
  1864             \<le> integral {0..1} (\<lambda>t. (norm z)\<^sup>2 * t / (r - norm z) * C)"
  1865         apply (rule integral_norm_bound_integral)
  1866         using contour_integral_primitive [OF 1, of "linepath 0 z"] 2
  1867         apply (simp add: has_contour_integral_linepath has_integral_integrable_integral)
  1868         apply (rule 3)
  1869         apply (simp add: norm_mult power2_eq_square 4)
  1870         done
  1871       then have int_le: "norm (f z - deriv f 0 * z) \<le> (norm z)\<^sup>2 * norm(deriv f 0) / ((r - norm z))"
  1872         using contour_integral_primitive [OF 1, of "linepath 0 z"] 2
  1873         apply (simp add: has_contour_integral_linepath has_integral_integrable_integral C_def)
  1874         done
  1875       show ?thesis
  1876         apply (rule le_norm [OF _ int_le])
  1877         using \<open>norm z < r\<close>
  1878         apply (simp add: power2_eq_square divide_simps C_def norm_mult)
  1879         proof -
  1880           have "norm z * (norm (deriv f 0) * (r - norm z - norm z)) \<le> norm z * (norm (deriv f 0) * (r - norm z) - norm (deriv f 0) * norm z)"
  1881             by (simp add: linordered_field_class.sign_simps(38))
  1882           then show "(norm z * (r - norm z) - norm z * norm z) * norm (deriv f 0) \<le> norm (deriv f 0) * norm z * (r - norm z) - norm z * norm z * norm (deriv f 0)"
  1883             by (simp add: linordered_field_class.sign_simps(38) mult.commute mult.left_commute)
  1884         qed
  1885     qed
  1886     have sq201 [simp]: "0 < (1 - sqrt 2 / 2)" "(1 - sqrt 2 / 2)  < 1"
  1887       by (auto simp:  sqrt2_less_2)
  1888     have 1: "continuous_on (closure (ball 0 ((1 - sqrt 2 / 2) * r))) f"
  1889       apply (rule continuous_on_subset [OF holomorphic_on_imp_continuous_on [OF holf]])
  1890       apply (subst closure_ball)
  1891       using \<open>0 < r\<close> mult_pos_pos sq201
  1892       apply (auto simp: cball_subset_cball_iff)
  1893       done
  1894     have 2: "open (f ` interior (ball 0 ((1 - sqrt 2 / 2) * r)))"
  1895       apply (rule open_mapping_thm [OF holf' open_ball connected_ball], force)
  1896       using \<open>0 < r\<close> mult_pos_pos sq201 apply (simp add: ball_subset_ball_iff)
  1897       using False \<open>0 < r\<close> centre_in_ball holf' holomorphic_nonconstant by blast
  1898     have "ball 0 ((3 - 2 * sqrt 2) * r * norm (deriv f 0)) =
  1899           ball (f 0) ((3 - 2 * sqrt 2) * r * norm (deriv f 0))"
  1900       by simp
  1901     also have "...  \<subseteq> f ` ball 0 ((1 - sqrt 2 / 2) * r)"
  1902     proof -
  1903       have 3: "(3 - 2 * sqrt 2) * r * norm (deriv f 0) \<le> norm (f z)"
  1904            if "norm z = (1 - sqrt 2 / 2) * r" for z
  1905         apply (rule order_trans [OF _ *])
  1906         using  \<open>0 < r\<close>
  1907         apply (simp_all add: field_simps  power2_eq_square that)
  1908         apply (simp add: mult.assoc [symmetric])
  1909         done
  1910       show ?thesis
  1911         apply (rule ball_subset_open_map_image [OF 1 2 _ bounded_ball])
  1912         using \<open>0 < r\<close> sq201 3 apply simp_all
  1913         using C_def \<open>0 < C\<close> sq3 apply force
  1914         done
  1915      qed
  1916     also have "...  \<subseteq> f ` ball 0 r"
  1917       apply (rule image_subsetI [OF imageI], simp)
  1918       apply (erule less_le_trans)
  1919       using \<open>0 < r\<close> apply (auto simp: field_simps)
  1920       done
  1921     finally show ?thesis .
  1922   qed
  1923 qed
  1924 
  1925 lemma Bloch_lemma:
  1926   assumes holf: "f holomorphic_on cball a r" and "0 < r"
  1927       and le: "\<And>z. z \<in> ball a r \<Longrightarrow> norm(deriv f z) \<le> 2 * norm(deriv f a)"
  1928     shows "ball (f a) ((3 - 2 * sqrt 2) * r * norm(deriv f a)) \<subseteq> f ` ball a r"
  1929 proof -
  1930   have fz: "(\<lambda>z. f (a + z)) = f o (\<lambda>z. (a + z))"
  1931     by (simp add: o_def)
  1932   have hol0: "(\<lambda>z. f (a + z)) holomorphic_on cball 0 r"
  1933     unfolding fz by (intro holomorphic_intros holf holomorphic_on_compose | simp)+
  1934   then have [simp]: "\<And>x. norm x < r \<Longrightarrow> (\<lambda>z. f (a + z)) field_differentiable at x"
  1935     by (metis open_ball at_within_open ball_subset_cball diff_0 dist_norm holomorphic_on_def holomorphic_on_subset mem_ball norm_minus_cancel)
  1936   have [simp]: "\<And>z. norm z < r \<Longrightarrow> f field_differentiable at (a + z)"
  1937     by (metis holf open_ball add_diff_cancel_left' dist_complex_def holomorphic_on_imp_differentiable_at holomorphic_on_subset interior_cball interior_subset mem_ball norm_minus_commute)
  1938   then have [simp]: "f field_differentiable at a"
  1939     by (metis add.comm_neutral \<open>0 < r\<close> norm_eq_zero)
  1940   have hol1: "(\<lambda>z. f (a + z) - f a) holomorphic_on cball 0 r"
  1941     by (intro holomorphic_intros hol0)
  1942   then have "ball 0 ((3 - 2 * sqrt 2) * r * norm (deriv (\<lambda>z. f (a + z) - f a) 0))
  1943              \<subseteq> (\<lambda>z. f (a + z) - f a) ` ball 0 r"
  1944     apply (rule Bloch_lemma_0)
  1945     apply (simp_all add: \<open>0 < r\<close>)
  1946     apply (simp add: fz complex_derivative_chain)
  1947     apply (simp add: dist_norm le)
  1948     done
  1949   then show ?thesis
  1950     apply clarify
  1951     apply (drule_tac c="x - f a" in subsetD)
  1952      apply (force simp: fz \<open>0 < r\<close> dist_norm complex_derivative_chain field_differentiable_compose)+
  1953     done
  1954 qed
  1955 
  1956 proposition Bloch_unit:
  1957   assumes holf: "f holomorphic_on ball a 1" and [simp]: "deriv f a = 1"
  1958   obtains b r where "1/12 < r" and "ball b r \<subseteq> f ` (ball a 1)"
  1959 proof -
  1960   define r :: real where "r = 249/256"
  1961   have "0 < r" "r < 1" by (auto simp: r_def)
  1962   define g where "g z = deriv f z * of_real(r - norm(z - a))" for z
  1963   have "deriv f holomorphic_on ball a 1"
  1964     by (rule holomorphic_deriv [OF holf open_ball])
  1965   then have "continuous_on (ball a 1) (deriv f)"
  1966     using holomorphic_on_imp_continuous_on by blast
  1967   then have "continuous_on (cball a r) (deriv f)"
  1968     by (rule continuous_on_subset) (simp add: cball_subset_ball_iff \<open>r < 1\<close>)
  1969   then have "continuous_on (cball a r) g"
  1970     by (simp add: g_def continuous_intros)
  1971   then have 1: "compact (g ` cball a r)"
  1972     by (rule compact_continuous_image [OF _ compact_cball])
  1973   have 2: "g ` cball a r \<noteq> {}"
  1974     using \<open>r > 0\<close> by auto
  1975   obtain p where pr: "p \<in> cball a r"
  1976              and pge: "\<And>y. y \<in> cball a r \<Longrightarrow> norm (g y) \<le> norm (g p)"
  1977     using distance_attains_sup [OF 1 2, of 0] by force
  1978   define t where "t = (r - norm(p - a)) / 2"
  1979   have "norm (p - a) \<noteq> r"
  1980     using pge [of a] \<open>r > 0\<close> by (auto simp: g_def norm_mult)
  1981   then have "norm (p - a) < r" using pr
  1982     by (simp add: norm_minus_commute dist_norm)
  1983   then have "0 < t"
  1984     by (simp add: t_def)
  1985   have cpt: "cball p t \<subseteq> ball a r"
  1986     using \<open>0 < t\<close> by (simp add: cball_subset_ball_iff dist_norm t_def field_simps)
  1987   have gen_le_dfp: "norm (deriv f y) * (r - norm (y - a)) / (r - norm (p - a)) \<le> norm (deriv f p)"
  1988             if "y \<in> cball a r" for y
  1989   proof -
  1990     have [simp]: "norm (y - a) \<le> r"
  1991       using that by (simp add: dist_norm norm_minus_commute)
  1992     have "norm (g y) \<le> norm (g p)"
  1993       using pge [OF that] by simp
  1994     then have "norm (deriv f y) * abs (r - norm (y - a)) \<le> norm (deriv f p) * abs (r - norm (p - a))"
  1995       by (simp only: dist_norm g_def norm_mult norm_of_real)
  1996     with that \<open>norm (p - a) < r\<close> show ?thesis
  1997       by (simp add: dist_norm divide_simps)
  1998   qed
  1999   have le_norm_dfp: "r / (r - norm (p - a)) \<le> norm (deriv f p)"
  2000     using gen_le_dfp [of a] \<open>r > 0\<close> by auto
  2001   have 1: "f holomorphic_on cball p t"
  2002     apply (rule holomorphic_on_subset [OF holf])
  2003     using cpt \<open>r < 1\<close> order_subst1 subset_ball by auto
  2004   have 2: "norm (deriv f z) \<le> 2 * norm (deriv f p)" if "z \<in> ball p t" for z
  2005   proof -
  2006     have z: "z \<in> cball a r"
  2007       by (meson ball_subset_cball subsetD cpt that)
  2008     then have "norm(z - a) < r"
  2009       by (metis ball_subset_cball contra_subsetD cpt dist_norm mem_ball norm_minus_commute that)
  2010     have "norm (deriv f z) * (r - norm (z - a)) / (r - norm (p - a)) \<le> norm (deriv f p)"
  2011       using gen_le_dfp [OF z] by simp
  2012     with \<open>norm (z - a) < r\<close> \<open>norm (p - a) < r\<close>
  2013     have "norm (deriv f z) \<le> (r - norm (p - a)) / (r - norm (z - a)) * norm (deriv f p)"
  2014        by (simp add: field_simps)
  2015     also have "... \<le> 2 * norm (deriv f p)"
  2016       apply (rule mult_right_mono)
  2017       using that \<open>norm (p - a) < r\<close> \<open>norm(z - a) < r\<close>
  2018       apply (simp_all add: field_simps t_def dist_norm [symmetric])
  2019       using dist_triangle3 [of z a p] by linarith
  2020     finally show ?thesis .
  2021   qed
  2022   have sqrt2: "sqrt 2 < 2113/1494"
  2023     by (rule real_less_lsqrt) (auto simp: power2_eq_square)
  2024   then have sq3: "0 < 3 - 2 * sqrt 2" by simp
  2025   have "1 / 12 / ((3 - 2 * sqrt 2) / 2) < r"
  2026     using sq3 sqrt2 by (auto simp: field_simps r_def)
  2027   also have "... \<le> cmod (deriv f p) * (r - cmod (p - a))"
  2028     using \<open>norm (p - a) < r\<close> le_norm_dfp   by (simp add: pos_divide_le_eq)
  2029   finally have "1 / 12 < cmod (deriv f p) * (r - cmod (p - a)) * ((3 - 2 * sqrt 2) / 2)"
  2030     using pos_divide_less_eq half_gt_zero_iff sq3 by blast
  2031   then have **: "1 / 12 < (3 - 2 * sqrt 2) * t * norm (deriv f p)"
  2032     using sq3 by (simp add: mult.commute t_def)
  2033   have "ball (f p) ((3 - 2 * sqrt 2) * t * norm (deriv f p)) \<subseteq> f ` ball p t"
  2034     by (rule Bloch_lemma [OF 1 \<open>0 < t\<close> 2])
  2035   also have "... \<subseteq> f ` ball a 1"
  2036     apply (rule image_mono)
  2037     apply (rule order_trans [OF ball_subset_cball])
  2038     apply (rule order_trans [OF cpt])
  2039     using \<open>0 < t\<close> \<open>r < 1\<close> apply (simp add: ball_subset_ball_iff dist_norm)
  2040     done
  2041   finally have "ball (f p) ((3 - 2 * sqrt 2) * t * norm (deriv f p)) \<subseteq> f ` ball a 1" .
  2042   with ** show ?thesis
  2043     by (rule that)
  2044 qed
  2045 
  2046 theorem Bloch:
  2047   assumes holf: "f holomorphic_on ball a r" and "0 < r"
  2048       and r': "r' \<le> r * norm (deriv f a) / 12"
  2049   obtains b where "ball b r' \<subseteq> f ` (ball a r)"
  2050 proof (cases "deriv f a = 0")
  2051   case True with r' show ?thesis
  2052     using ball_eq_empty that by fastforce
  2053 next
  2054   case False
  2055   define C where "C = deriv f a"
  2056   have "0 < norm C" using False by (simp add: C_def)
  2057   have dfa: "f field_differentiable at a"
  2058     apply (rule holomorphic_on_imp_differentiable_at [OF holf])
  2059     using \<open>0 < r\<close> by auto
  2060   have fo: "(\<lambda>z. f (a + of_real r * z)) = f o (\<lambda>z. (a + of_real r * z))"
  2061     by (simp add: o_def)
  2062   have holf': "f holomorphic_on (\<lambda>z. a + complex_of_real r * z) ` ball 0 1"
  2063     apply (rule holomorphic_on_subset [OF holf])
  2064     using \<open>0 < r\<close> apply (force simp: dist_norm norm_mult)
  2065     done
  2066   have 1: "(\<lambda>z. f (a + r * z) / (C * r)) holomorphic_on ball 0 1"
  2067     apply (rule holomorphic_intros holomorphic_on_compose holf' | simp add: fo)+
  2068     using \<open>0 < r\<close> by (simp add: C_def False)
  2069   have "((\<lambda>z. f (a + of_real r * z) / (C * of_real r)) has_field_derivative
  2070         (deriv f (a + of_real r * z) / C)) (at z)"
  2071        if "norm z < 1" for z
  2072   proof -
  2073     have *: "((\<lambda>x. f (a + of_real r * x)) has_field_derivative
  2074            (deriv f (a + of_real r * z) * of_real r)) (at z)"
  2075       apply (simp add: fo)
  2076       apply (rule DERIV_chain [OF field_differentiable_derivI])
  2077       apply (rule holomorphic_on_imp_differentiable_at [OF holf], simp)
  2078       using \<open>0 < r\<close> apply (simp add: dist_norm norm_mult that)
  2079       apply (rule derivative_eq_intros | simp)+
  2080       done
  2081     show ?thesis
  2082       apply (rule derivative_eq_intros * | simp)+
  2083       using \<open>0 < r\<close> by (auto simp: C_def False)
  2084   qed
  2085   have 2: "deriv (\<lambda>z. f (a + of_real r * z) / (C * of_real r)) 0 = 1"
  2086     apply (subst deriv_cdivide_right)
  2087     apply (simp add: field_differentiable_def fo)
  2088     apply (rule exI)
  2089     apply (rule DERIV_chain [OF field_differentiable_derivI])
  2090     apply (simp add: dfa)
  2091     apply (rule derivative_eq_intros | simp add: C_def False fo)+
  2092     using \<open>0 < r\<close>
  2093     apply (simp add: C_def False fo)
  2094     apply (simp add: derivative_intros dfa complex_derivative_chain)
  2095     done
  2096   have sb1: "(*) (C * r) ` (\<lambda>z. f (a + of_real r * z) / (C * r)) ` ball 0 1
  2097              \<subseteq> f ` ball a r"
  2098     using \<open>0 < r\<close> by (auto simp: dist_norm norm_mult C_def False)
  2099   have sb2: "ball (C * r * b) r' \<subseteq> (*) (C * r) ` ball b t"
  2100              if "1 / 12 < t" for b t
  2101   proof -
  2102     have *: "r * cmod (deriv f a) / 12 \<le> r * (t * cmod (deriv f a))"
  2103       using that \<open>0 < r\<close> less_eq_real_def mult.commute mult.right_neutral mult_left_mono norm_ge_zero times_divide_eq_right
  2104       by auto
  2105     show ?thesis
  2106       apply clarify
  2107       apply (rule_tac x="x / (C * r)" in image_eqI)
  2108       using \<open>0 < r\<close>
  2109       apply (simp_all add: dist_norm norm_mult norm_divide C_def False field_simps)
  2110       apply (erule less_le_trans)
  2111       apply (rule order_trans [OF r' *])
  2112       done
  2113   qed
  2114   show ?thesis
  2115     apply (rule Bloch_unit [OF 1 2])
  2116     apply (rename_tac t)
  2117     apply (rule_tac b="(C * of_real r) * b" in that)
  2118     apply (drule image_mono [where f = "\<lambda>z. (C * of_real r) * z"])
  2119     using sb1 sb2
  2120     apply force
  2121     done
  2122 qed
  2123 
  2124 corollary Bloch_general:
  2125   assumes holf: "f holomorphic_on s" and "a \<in> s"
  2126       and tle: "\<And>z. z \<in> frontier s \<Longrightarrow> t \<le> dist a z"
  2127       and rle: "r \<le> t * norm(deriv f a) / 12"
  2128   obtains b where "ball b r \<subseteq> f ` s"
  2129 proof -
  2130   consider "r \<le> 0" | "0 < t * norm(deriv f a) / 12" using rle by force
  2131   then show ?thesis
  2132   proof cases
  2133     case 1 then show ?thesis
  2134       by (simp add: ball_empty that)
  2135   next
  2136     case 2
  2137     show ?thesis
  2138     proof (cases "deriv f a = 0")
  2139       case True then show ?thesis
  2140         using rle by (simp add: ball_empty that)
  2141     next
  2142       case False
  2143       then have "t > 0"
  2144         using 2 by (force simp: zero_less_mult_iff)
  2145       have "\<not> ball a t \<subseteq> s \<Longrightarrow> ball a t \<inter> frontier s \<noteq> {}"
  2146         apply (rule connected_Int_frontier [of "ball a t" s], simp_all)
  2147         using \<open>0 < t\<close> \<open>a \<in> s\<close> centre_in_ball apply blast
  2148         done
  2149       with tle have *: "ball a t \<subseteq> s" by fastforce
  2150       then have 1: "f holomorphic_on ball a t"
  2151         using holf using holomorphic_on_subset by blast
  2152       show ?thesis
  2153         apply (rule Bloch [OF 1 \<open>t > 0\<close> rle])
  2154         apply (rule_tac b=b in that)
  2155         using * apply force
  2156         done
  2157     qed
  2158   qed
  2159 qed
  2160 
  2161 subsection \<open>Cauchy's residue theorem\<close>
  2162 
  2163 text\<open>Wenda Li and LC Paulson (2016). A Formal Proof of Cauchy's Residue Theorem.
  2164     Interactive Theorem Proving\<close>
  2165 
  2166 definition%important residue :: "(complex \<Rightarrow> complex) \<Rightarrow> complex \<Rightarrow> complex" where
  2167   "residue f z = (SOME int. \<exists>e>0. \<forall>\<epsilon>>0. \<epsilon><e
  2168     \<longrightarrow> (f has_contour_integral 2*pi* \<i> *int) (circlepath z \<epsilon>))"
  2169 
  2170 lemma Eps_cong:
  2171   assumes "\<And>x. P x = Q x"
  2172   shows   "Eps P = Eps Q"
  2173   using ext[of P Q, OF assms] by simp
  2174 
  2175 lemma residue_cong:
  2176   assumes eq: "eventually (\<lambda>z. f z = g z) (at z)" and "z = z'"
  2177   shows   "residue f z = residue g z'"
  2178 proof -
  2179   from assms have eq': "eventually (\<lambda>z. g z = f z) (at z)"
  2180     by (simp add: eq_commute)
  2181   let ?P = "\<lambda>f c e. (\<forall>\<epsilon>>0. \<epsilon> < e \<longrightarrow>
  2182    (f has_contour_integral of_real (2 * pi) * \<i> * c) (circlepath z \<epsilon>))"
  2183   have "residue f z = residue g z" unfolding residue_def
  2184   proof (rule Eps_cong)
  2185     fix c :: complex
  2186     have "\<exists>e>0. ?P g c e" 
  2187       if "\<exists>e>0. ?P f c e" and "eventually (\<lambda>z. f z = g z) (at z)" for f g 
  2188     proof -
  2189       from that(1) obtain e where e: "e > 0" "?P f c e"
  2190         by blast
  2191       from that(2) obtain e' where e': "e' > 0" "\<And>z'. z' \<noteq> z \<Longrightarrow> dist z' z < e' \<Longrightarrow> f z' = g z'"
  2192         unfolding eventually_at by blast
  2193       have "?P g c (min e e')"
  2194       proof (intro allI exI impI, goal_cases)
  2195         case (1 \<epsilon>)
  2196         hence "(f has_contour_integral of_real (2 * pi) * \<i> * c) (circlepath z \<epsilon>)" 
  2197           using e(2) by auto
  2198         thus ?case
  2199         proof (rule has_contour_integral_eq)
  2200           fix z' assume "z' \<in> path_image (circlepath z \<epsilon>)"
  2201           hence "dist z' z < e'" and "z' \<noteq> z"
  2202             using 1 by (auto simp: dist_commute)
  2203           with e'(2)[of z'] show "f z' = g z'" by simp
  2204         qed
  2205       qed
  2206       moreover from e and e' have "min e e' > 0" by auto
  2207       ultimately show ?thesis by blast
  2208     qed
  2209     from this[OF _ eq] and this[OF _ eq']
  2210       show "(\<exists>e>0. ?P f c e) \<longleftrightarrow> (\<exists>e>0. ?P g c e)"
  2211       by blast
  2212   qed
  2213   with assms show ?thesis by simp
  2214 qed
  2215 
  2216 lemma contour_integral_circlepath_eq:
  2217   assumes "open s" and f_holo:"f holomorphic_on (s-{z})" and "0<e1" "e1\<le>e2"
  2218     and e2_cball:"cball z e2 \<subseteq> s"
  2219   shows
  2220     "f contour_integrable_on circlepath z e1"
  2221     "f contour_integrable_on circlepath z e2"
  2222     "contour_integral (circlepath z e2) f = contour_integral (circlepath z e1) f"
  2223 proof -
  2224   define l where "l \<equiv> linepath (z+e2) (z+e1)"
  2225   have [simp]:"valid_path l" "pathstart l=z+e2" "pathfinish l=z+e1" unfolding l_def by auto
  2226   have "e2>0" using \<open>e1>0\<close> \<open>e1\<le>e2\<close> by auto
  2227   have zl_img:"z\<notin>path_image l"
  2228     proof
  2229       assume "z \<in> path_image l"
  2230       then have "e2 \<le> cmod (e2 - e1)"
  2231         using segment_furthest_le[of z "z+e2" "z+e1" "z+e2",simplified] \<open>e1>0\<close> \<open>e2>0\<close> unfolding l_def
  2232         by (auto simp add:closed_segment_commute)
  2233       thus False using \<open>e2>0\<close> \<open>e1>0\<close> \<open>e1\<le>e2\<close>
  2234         apply (subst (asm) norm_of_real)
  2235         by auto
  2236     qed
  2237   define g where "g \<equiv> circlepath z e2 +++ l +++ reversepath (circlepath z e1) +++ reversepath l"
  2238   show [simp]: "f contour_integrable_on circlepath z e2" "f contour_integrable_on (circlepath z e1)"
  2239     proof -
  2240       show "f contour_integrable_on circlepath z e2"
  2241         apply (intro contour_integrable_continuous_circlepath[OF
  2242                 continuous_on_subset[OF holomorphic_on_imp_continuous_on[OF f_holo]]])
  2243         using \<open>e2>0\<close> e2_cball by auto
  2244       show "f contour_integrable_on (circlepath z e1)"
  2245         apply (intro contour_integrable_continuous_circlepath[OF
  2246                       continuous_on_subset[OF holomorphic_on_imp_continuous_on[OF f_holo]]])
  2247         using \<open>e1>0\<close> \<open>e1\<le>e2\<close> e2_cball by auto
  2248     qed
  2249   have [simp]:"f contour_integrable_on l"
  2250     proof -
  2251       have "closed_segment (z + e2) (z + e1) \<subseteq> cball z e2" using \<open>e2>0\<close> \<open>e1>0\<close> \<open>e1\<le>e2\<close>
  2252         by (intro closed_segment_subset,auto simp add:dist_norm)
  2253       hence "closed_segment (z + e2) (z + e1) \<subseteq> s - {z}" using zl_img e2_cball unfolding l_def
  2254         by auto
  2255       then show "f contour_integrable_on l" unfolding l_def
  2256         apply (intro contour_integrable_continuous_linepath[OF
  2257                       continuous_on_subset[OF holomorphic_on_imp_continuous_on[OF f_holo]]])
  2258         by auto
  2259     qed
  2260   let ?ig="\<lambda>g. contour_integral g f"
  2261   have "(f has_contour_integral 0) g"
  2262     proof (rule Cauchy_theorem_global[OF _ f_holo])
  2263       show "open (s - {z})" using \<open>open s\<close> by auto
  2264       show "valid_path g" unfolding g_def l_def by auto
  2265       show "pathfinish g = pathstart g" unfolding g_def l_def by auto
  2266     next
  2267       have path_img:"path_image g \<subseteq> cball z e2"
  2268         proof -
  2269           have "closed_segment (z + e2) (z + e1) \<subseteq> cball z e2" using \<open>e2>0\<close> \<open>e1>0\<close> \<open>e1\<le>e2\<close>
  2270             by (intro closed_segment_subset,auto simp add:dist_norm)
  2271           moreover have "sphere z \<bar>e1\<bar> \<subseteq> cball z e2" using \<open>e2>0\<close> \<open>e1\<le>e2\<close> \<open>e1>0\<close> by auto
  2272           ultimately show ?thesis unfolding g_def l_def using \<open>e2>0\<close>
  2273             by (simp add: path_image_join closed_segment_commute)
  2274         qed
  2275       show "path_image g \<subseteq> s - {z}"
  2276         proof -
  2277           have "z\<notin>path_image g" using zl_img
  2278             unfolding g_def l_def by (auto simp add: path_image_join closed_segment_commute)
  2279           moreover note \<open>cball z e2 \<subseteq> s\<close> and path_img
  2280           ultimately show ?thesis by auto
  2281         qed
  2282       show "winding_number g w = 0" when"w \<notin> s - {z}" for w
  2283         proof -
  2284           have "winding_number g w = 0" when "w\<notin>s" using that e2_cball
  2285             apply (intro winding_number_zero_outside[OF _ _ _ _ path_img])
  2286             by (auto simp add:g_def l_def)
  2287           moreover have "winding_number g z=0"
  2288             proof -
  2289               let ?Wz="\<lambda>g. winding_number g z"
  2290               have "?Wz g = ?Wz (circlepath z e2) + ?Wz l + ?Wz (reversepath (circlepath z e1))
  2291                   + ?Wz (reversepath l)"
  2292                 using \<open>e2>0\<close> \<open>e1>0\<close> zl_img unfolding g_def l_def
  2293                 by (subst winding_number_join,auto simp add:path_image_join closed_segment_commute)+
  2294               also have "... = ?Wz (circlepath z e2) + ?Wz (reversepath (circlepath z e1))"
  2295                 using zl_img
  2296                 apply (subst (2) winding_number_reversepath)
  2297                 by (auto simp add:l_def closed_segment_commute)
  2298               also have "... = 0"
  2299                 proof -
  2300                   have "?Wz (circlepath z e2) = 1" using \<open>e2>0\<close>
  2301                     by (auto intro: winding_number_circlepath_centre)
  2302                   moreover have "?Wz (reversepath (circlepath z e1)) = -1" using \<open>e1>0\<close>
  2303                     apply (subst winding_number_reversepath)
  2304                     by (auto intro: winding_number_circlepath_centre)
  2305                   ultimately show ?thesis by auto
  2306                 qed
  2307               finally show ?thesis .
  2308             qed
  2309           ultimately show ?thesis using that by auto
  2310         qed
  2311     qed
  2312   then have "0 = ?ig g" using contour_integral_unique by simp
  2313   also have "... = ?ig (circlepath z e2) + ?ig l + ?ig (reversepath (circlepath z e1))
  2314       + ?ig (reversepath l)"
  2315     unfolding g_def
  2316     by (auto simp add:contour_integrable_reversepath_eq)
  2317   also have "... = ?ig (circlepath z e2)  - ?ig (circlepath z e1)"
  2318     by (auto simp add:contour_integral_reversepath)
  2319   finally show "contour_integral (circlepath z e2) f = contour_integral (circlepath z e1) f"
  2320     by simp
  2321 qed
  2322 
  2323 lemma base_residue:
  2324   assumes "open s" "z\<in>s" "r>0" and f_holo:"f holomorphic_on (s - {z})"
  2325     and r_cball:"cball z r \<subseteq> s"
  2326   shows "(f has_contour_integral 2 * pi * \<i> * (residue f z)) (circlepath z r)"
  2327 proof -
  2328   obtain e where "e>0" and e_cball:"cball z e \<subseteq> s"
  2329     using open_contains_cball[of s] \<open>open s\<close> \<open>z\<in>s\<close> by auto
  2330   define c where "c \<equiv> 2 * pi * \<i>"
  2331   define i where "i \<equiv> contour_integral (circlepath z e) f / c"
  2332   have "(f has_contour_integral c*i) (circlepath z \<epsilon>)" when "\<epsilon>>0" "\<epsilon><e" for \<epsilon>
  2333     proof -
  2334       have "contour_integral (circlepath z e) f = contour_integral (circlepath z \<epsilon>) f"
  2335           "f contour_integrable_on circlepath z \<epsilon>"
  2336           "f contour_integrable_on circlepath z e"
  2337         using \<open>\<epsilon><e\<close>
  2338         by (intro contour_integral_circlepath_eq[OF \<open>open s\<close> f_holo \<open>\<epsilon>>0\<close> _ e_cball],auto)+
  2339       then show ?thesis unfolding i_def c_def
  2340         by (auto intro:has_contour_integral_integral)
  2341     qed
  2342   then have "\<exists>e>0. \<forall>\<epsilon>>0. \<epsilon><e \<longrightarrow> (f has_contour_integral c * (residue f z)) (circlepath z \<epsilon>)"
  2343     unfolding residue_def c_def
  2344     apply (rule_tac someI[of _ i],intro  exI[where x=e])
  2345     by (auto simp add:\<open>e>0\<close> c_def)
  2346   then obtain e' where "e'>0"
  2347       and e'_def:"\<forall>\<epsilon>>0. \<epsilon><e' \<longrightarrow> (f has_contour_integral c * (residue f z)) (circlepath z \<epsilon>)"
  2348     by auto
  2349   let ?int="\<lambda>e. contour_integral (circlepath z e) f"
  2350   define  \<epsilon> where "\<epsilon> \<equiv> Min {r,e'} / 2"
  2351   have "\<epsilon>>0" "\<epsilon>\<le>r" "\<epsilon><e'" using \<open>r>0\<close> \<open>e'>0\<close> unfolding \<epsilon>_def by auto
  2352   have "(f has_contour_integral c * (residue f z)) (circlepath z \<epsilon>)"
  2353     using e'_def[rule_format,OF \<open>\<epsilon>>0\<close> \<open>\<epsilon><e'\<close>] .
  2354   then show ?thesis unfolding c_def
  2355     using contour_integral_circlepath_eq[OF \<open>open s\<close> f_holo \<open>\<epsilon>>0\<close> \<open>\<epsilon>\<le>r\<close> r_cball]
  2356     by (auto elim: has_contour_integral_eqpath[of _ _ "circlepath z \<epsilon>" "circlepath z r"])
  2357 qed
  2358 
  2359 lemma residue_holo:
  2360   assumes "open s" "z \<in> s" and f_holo: "f holomorphic_on s"
  2361   shows "residue f z = 0"
  2362 proof -
  2363   define c where "c \<equiv> 2 * pi * \<i>"
  2364   obtain e where "e>0" and e_cball:"cball z e \<subseteq> s" using \<open>open s\<close> \<open>z\<in>s\<close>
  2365     using open_contains_cball_eq by blast
  2366   have "(f has_contour_integral c*residue f z) (circlepath z e)"
  2367     using f_holo
  2368     by (auto intro: base_residue[OF \<open>open s\<close> \<open>z\<in>s\<close> \<open>e>0\<close> _ e_cball,folded c_def])
  2369   moreover have "(f has_contour_integral 0) (circlepath z e)"
  2370     using f_holo e_cball \<open>e>0\<close>
  2371     by (auto intro: Cauchy_theorem_convex_simple[of _ "cball z e"])
  2372   ultimately have "c*residue f z =0"
  2373     using has_contour_integral_unique by blast
  2374   thus ?thesis unfolding c_def  by auto
  2375 qed
  2376 
  2377 lemma residue_const:"residue (\<lambda>_. c) z = 0"
  2378   by (intro residue_holo[of "UNIV::complex set"],auto intro:holomorphic_intros)
  2379 
  2380 lemma residue_add:
  2381   assumes "open s" "z \<in> s" and f_holo: "f holomorphic_on s - {z}"
  2382       and g_holo:"g holomorphic_on s - {z}"
  2383   shows "residue (\<lambda>z. f z + g z) z= residue f z + residue g z"
  2384 proof -
  2385   define c where "c \<equiv> 2 * pi * \<i>"
  2386   define fg where "fg \<equiv> (\<lambda>z. f z+g z)"
  2387   obtain e where "e>0" and e_cball:"cball z e \<subseteq> s" using \<open>open s\<close> \<open>z\<in>s\<close>
  2388     using open_contains_cball_eq by blast
  2389   have "(fg has_contour_integral c * residue fg z) (circlepath z e)"
  2390     unfolding fg_def using f_holo g_holo
  2391     apply (intro base_residue[OF \<open>open s\<close> \<open>z\<in>s\<close> \<open>e>0\<close> _ e_cball,folded c_def])
  2392     by (auto intro:holomorphic_intros)
  2393   moreover have "(fg has_contour_integral c*residue f z + c* residue g z) (circlepath z e)"
  2394     unfolding fg_def using f_holo g_holo
  2395     by (auto intro: has_contour_integral_add base_residue[OF \<open>open s\<close> \<open>z\<in>s\<close> \<open>e>0\<close> _ e_cball,folded c_def])
  2396   ultimately have "c*(residue f z + residue g z) = c * residue fg z"
  2397     using has_contour_integral_unique by (auto simp add:distrib_left)
  2398   thus ?thesis unfolding fg_def
  2399     by (auto simp add:c_def)
  2400 qed
  2401 
  2402 lemma residue_lmul:
  2403   assumes "open s" "z \<in> s" and f_holo: "f holomorphic_on s - {z}"
  2404   shows "residue (\<lambda>z. c * (f z)) z= c * residue f z"
  2405 proof (cases "c=0")
  2406   case True
  2407   thus ?thesis using residue_const by auto
  2408 next
  2409   case False
  2410   define c' where "c' \<equiv> 2 * pi * \<i>"
  2411   define f' where "f' \<equiv> (\<lambda>z. c * (f z))"
  2412   obtain e where "e>0" and e_cball:"cball z e \<subseteq> s" using \<open>open s\<close> \<open>z\<in>s\<close>
  2413     using open_contains_cball_eq by blast
  2414   have "(f' has_contour_integral c' * residue f' z) (circlepath z e)"
  2415     unfolding f'_def using f_holo
  2416     apply (intro base_residue[OF \<open>open s\<close> \<open>z\<in>s\<close> \<open>e>0\<close> _ e_cball,folded c'_def])
  2417     by (auto intro:holomorphic_intros)
  2418   moreover have "(f' has_contour_integral c * (c' * residue f z)) (circlepath z e)"
  2419     unfolding f'_def using f_holo
  2420     by (auto intro: has_contour_integral_lmul
  2421       base_residue[OF \<open>open s\<close> \<open>z\<in>s\<close> \<open>e>0\<close> _ e_cball,folded c'_def])
  2422   ultimately have "c' * residue f' z  = c * (c' * residue f z)"
  2423     using has_contour_integral_unique by auto
  2424   thus ?thesis unfolding f'_def c'_def using False
  2425     by (auto simp add:field_simps)
  2426 qed
  2427 
  2428 lemma residue_rmul:
  2429   assumes "open s" "z \<in> s" and f_holo: "f holomorphic_on s - {z}"
  2430   shows "residue (\<lambda>z. (f z) * c) z= residue f z * c"
  2431 using residue_lmul[OF assms,of c] by (auto simp add:algebra_simps)
  2432 
  2433 lemma residue_div:
  2434   assumes "open s" "z \<in> s" and f_holo: "f holomorphic_on s - {z}"
  2435   shows "residue (\<lambda>z. (f z) / c) z= residue f z / c "
  2436 using residue_lmul[OF assms,of "1/c"] by (auto simp add:algebra_simps)
  2437 
  2438 lemma residue_neg:
  2439   assumes "open s" "z \<in> s" and f_holo: "f holomorphic_on s - {z}"
  2440   shows "residue (\<lambda>z. - (f z)) z= - residue f z"
  2441 using residue_lmul[OF assms,of "-1"] by auto
  2442 
  2443 lemma residue_diff:
  2444   assumes "open s" "z \<in> s" and f_holo: "f holomorphic_on s - {z}"
  2445       and g_holo:"g holomorphic_on s - {z}"
  2446   shows "residue (\<lambda>z. f z - g z) z= residue f z - residue g z"
  2447 using residue_add[OF assms(1,2,3),of "\<lambda>z. - g z"] residue_neg[OF assms(1,2,4)]
  2448 by (auto intro:holomorphic_intros g_holo)
  2449 
  2450 lemma residue_simple:
  2451   assumes "open s" "z\<in>s" and f_holo:"f holomorphic_on s"
  2452   shows "residue (\<lambda>w. f w / (w - z)) z = f z"
  2453 proof -
  2454   define c where "c \<equiv> 2 * pi * \<i>"
  2455   define f' where "f' \<equiv> \<lambda>w. f w / (w - z)"
  2456   obtain e where "e>0" and e_cball:"cball z e \<subseteq> s" using \<open>open s\<close> \<open>z\<in>s\<close>
  2457     using open_contains_cball_eq by blast
  2458   have "(f' has_contour_integral c * f z) (circlepath z e)"
  2459     unfolding f'_def c_def using \<open>e>0\<close> f_holo e_cball
  2460     by (auto intro!: Cauchy_integral_circlepath_simple holomorphic_intros)
  2461   moreover have "(f' has_contour_integral c * residue f' z) (circlepath z e)"
  2462     unfolding f'_def using f_holo
  2463     apply (intro base_residue[OF \<open>open s\<close> \<open>z\<in>s\<close> \<open>e>0\<close> _ e_cball,folded c_def])
  2464     by (auto intro!:holomorphic_intros)
  2465   ultimately have "c * f z = c * residue f' z"
  2466     using has_contour_integral_unique by blast
  2467   thus ?thesis unfolding c_def f'_def  by auto
  2468 qed
  2469 
  2470 lemma residue_simple':
  2471   assumes s: "open s" "z \<in> s" and holo: "f holomorphic_on (s - {z})" 
  2472       and lim: "((\<lambda>w. f w * (w - z)) \<longlongrightarrow> c) (at z)"
  2473   shows   "residue f z = c"
  2474 proof -
  2475   define g where "g = (\<lambda>w. if w = z then c else f w * (w - z))"
  2476   from holo have "(\<lambda>w. f w * (w - z)) holomorphic_on (s - {z})" (is "?P")
  2477     by (force intro: holomorphic_intros)
  2478   also have "?P \<longleftrightarrow> g holomorphic_on (s - {z})"
  2479     by (intro holomorphic_cong refl) (simp_all add: g_def)
  2480   finally have *: "g holomorphic_on (s - {z})" .
  2481 
  2482   note lim
  2483   also have "(\<lambda>w. f w * (w - z)) \<midarrow>z\<rightarrow> c \<longleftrightarrow> g \<midarrow>z\<rightarrow> g z"
  2484     by (intro filterlim_cong refl) (simp_all add: g_def [abs_def] eventually_at_filter)
  2485   finally have **: "g \<midarrow>z\<rightarrow> g z" .
  2486 
  2487   have g_holo: "g holomorphic_on s"
  2488     by (rule no_isolated_singularity'[where K = "{z}"])
  2489        (insert assms * **, simp_all add: at_within_open_NO_MATCH)
  2490   from s and this have "residue (\<lambda>w. g w / (w - z)) z = g z"
  2491     by (rule residue_simple)
  2492   also have "\<forall>\<^sub>F za in at z. g za / (za - z) = f za"
  2493     unfolding eventually_at by (auto intro!: exI[of _ 1] simp: field_simps g_def)
  2494   hence "residue (\<lambda>w. g w / (w - z)) z = residue f z"
  2495     by (intro residue_cong refl)
  2496   finally show ?thesis
  2497     by (simp add: g_def)
  2498 qed
  2499 
  2500 lemma residue_holomorphic_over_power:
  2501   assumes "open A" "z0 \<in> A" "f holomorphic_on A"
  2502   shows   "residue (\<lambda>z. f z / (z - z0) ^ Suc n) z0 = (deriv ^^ n) f z0 / fact n"
  2503 proof -
  2504   let ?f = "\<lambda>z. f z / (z - z0) ^ Suc n"
  2505   from assms(1,2) obtain r where r: "r > 0" "cball z0 r \<subseteq> A"
  2506     by (auto simp: open_contains_cball)
  2507   have "(?f has_contour_integral 2 * pi * \<i> * residue ?f z0) (circlepath z0 r)"
  2508     using r assms by (intro base_residue[of A]) (auto intro!: holomorphic_intros)
  2509   moreover have "(?f has_contour_integral 2 * pi * \<i> / fact n * (deriv ^^ n) f z0) (circlepath z0 r)"
  2510     using assms r
  2511     by (intro Cauchy_has_contour_integral_higher_derivative_circlepath)
  2512        (auto intro!: holomorphic_on_subset[OF assms(3)] holomorphic_on_imp_continuous_on)
  2513   ultimately have "2 * pi * \<i> * residue ?f z0 = 2 * pi * \<i> / fact n * (deriv ^^ n) f z0"  
  2514     by (rule has_contour_integral_unique)
  2515   thus ?thesis by (simp add: field_simps)
  2516 qed
  2517 
  2518 lemma residue_holomorphic_over_power':
  2519   assumes "open A" "0 \<in> A" "f holomorphic_on A"
  2520   shows   "residue (\<lambda>z. f z / z ^ Suc n) 0 = (deriv ^^ n) f 0 / fact n"
  2521   using residue_holomorphic_over_power[OF assms] by simp
  2522 
  2523 lemma get_integrable_path:
  2524   assumes "open s" "connected (s-pts)" "finite pts" "f holomorphic_on (s-pts) " "a\<in>s-pts" "b\<in>s-pts"
  2525   obtains g where "valid_path g" "pathstart g = a" "pathfinish g = b"
  2526     "path_image g \<subseteq> s-pts" "f contour_integrable_on g" using assms
  2527 proof (induct arbitrary:s thesis a rule:finite_induct[OF \<open>finite pts\<close>])
  2528   case 1
  2529   obtain g where "valid_path g" "path_image g \<subseteq> s" "pathstart g = a" "pathfinish g = b"
  2530     using connected_open_polynomial_connected[OF \<open>open s\<close>,of a b ] \<open>connected (s - {})\<close>
  2531       valid_path_polynomial_function "1.prems"(6) "1.prems"(7) by auto
  2532   moreover have "f contour_integrable_on g"
  2533     using contour_integrable_holomorphic_simple[OF _ \<open>open s\<close> \<open>valid_path g\<close> \<open>path_image g \<subseteq> s\<close>,of f]
  2534       \<open>f holomorphic_on s - {}\<close>
  2535     by auto
  2536   ultimately show ?case using "1"(1)[of g] by auto
  2537 next
  2538   case idt:(2 p pts)
  2539   obtain e where "e>0" and e:"\<forall>w\<in>ball a e. w \<in> s \<and> (w \<noteq> a \<longrightarrow> w \<notin> insert p pts)"
  2540     using finite_ball_avoid[OF \<open>open s\<close> \<open>finite (insert p pts)\<close>, of a]
  2541       \<open>a \<in> s - insert p pts\<close>
  2542     by auto
  2543   define a' where "a' \<equiv> a+e/2"
  2544   have "a'\<in>s-{p} -pts"  using e[rule_format,of "a+e/2"] \<open>e>0\<close>
  2545     by (auto simp add:dist_complex_def a'_def)
  2546   then obtain g' where g'[simp]:"valid_path g'" "pathstart g' = a'" "pathfinish g' = b"
  2547     "path_image g' \<subseteq> s - {p} - pts" "f contour_integrable_on g'"
  2548     using idt.hyps(3)[of a' "s-{p}"] idt.prems idt.hyps(1)
  2549     by (metis Diff_insert2 open_delete)
  2550   define g where "g \<equiv> linepath a a' +++ g'"
  2551   have "valid_path g" unfolding g_def by (auto intro: valid_path_join)
  2552   moreover have "pathstart g = a" and  "pathfinish g = b" unfolding g_def by auto
  2553   moreover have "path_image g \<subseteq> s - insert p pts" unfolding g_def
  2554     proof (rule subset_path_image_join)
  2555       have "closed_segment a a' \<subseteq> ball a e" using \<open>e>0\<close>
  2556         by (auto dest!:segment_bound1 simp:a'_def dist_complex_def norm_minus_commute)
  2557       then show "path_image (linepath a a') \<subseteq> s - insert p pts" using e idt(9)
  2558         by auto
  2559     next
  2560       show "path_image g' \<subseteq> s - insert p pts" using g'(4) by blast
  2561     qed
  2562   moreover have "f contour_integrable_on g"
  2563     proof -
  2564       have "closed_segment a a' \<subseteq> ball a e" using \<open>e>0\<close>
  2565         by (auto dest!:segment_bound1 simp:a'_def dist_complex_def norm_minus_commute)
  2566       then have "continuous_on (closed_segment a a') f"
  2567         using e idt.prems(6) holomorphic_on_imp_continuous_on[OF idt.prems(5)]
  2568         apply (elim continuous_on_subset)
  2569         by auto
  2570       then have "f contour_integrable_on linepath a a'"
  2571         using contour_integrable_continuous_linepath by auto
  2572       then show ?thesis unfolding g_def
  2573         apply (rule contour_integrable_joinI)
  2574         by (auto simp add: \<open>e>0\<close>)
  2575     qed
  2576   ultimately show ?case using idt.prems(1)[of g] by auto
  2577 qed
  2578 
  2579 lemma Cauchy_theorem_aux:
  2580   assumes "open s" "connected (s-pts)" "finite pts" "pts \<subseteq> s" "f holomorphic_on s-pts"
  2581           "valid_path g" "pathfinish g = pathstart g" "path_image g \<subseteq> s-pts"
  2582           "\<forall>z. (z \<notin> s) \<longrightarrow> winding_number g z  = 0"
  2583           "\<forall>p\<in>s. h p>0 \<and> (\<forall>w\<in>cball p (h p). w\<in>s \<and> (w\<noteq>p \<longrightarrow> w \<notin> pts))"
  2584   shows "contour_integral g f = (\<Sum>p\<in>pts. winding_number g p * contour_integral (circlepath p (h p)) f)"
  2585     using assms
  2586 proof (induct arbitrary:s g rule:finite_induct[OF \<open>finite pts\<close>])
  2587   case 1
  2588   then show ?case by (simp add: Cauchy_theorem_global contour_integral_unique)
  2589 next
  2590   case (2 p pts)
  2591   note fin[simp] = \<open>finite (insert p pts)\<close>
  2592     and connected = \<open>connected (s - insert p pts)\<close>
  2593     and valid[simp] = \<open>valid_path g\<close>
  2594     and g_loop[simp] = \<open>pathfinish g = pathstart g\<close>
  2595     and holo[simp]= \<open>f holomorphic_on s - insert p pts\<close>
  2596     and path_img = \<open>path_image g \<subseteq> s - insert p pts\<close>
  2597     and winding = \<open>\<forall>z. z \<notin> s \<longrightarrow> winding_number g z = 0\<close>
  2598     and h = \<open>\<forall>pa\<in>s. 0 < h pa \<and> (\<forall>w\<in>cball pa (h pa). w \<in> s \<and> (w \<noteq> pa \<longrightarrow> w \<notin> insert p pts))\<close>
  2599   have "h p>0" and "p\<in>s"
  2600     and h_p: "\<forall>w\<in>cball p (h p). w \<in> s \<and> (w \<noteq> p \<longrightarrow> w \<notin> insert p pts)"
  2601     using h \<open>insert p pts \<subseteq> s\<close> by auto
  2602   obtain pg where pg[simp]: "valid_path pg" "pathstart pg = pathstart g" "pathfinish pg=p+h p"
  2603       "path_image pg \<subseteq> s-insert p pts" "f contour_integrable_on pg"
  2604     proof -
  2605       have "p + h p\<in>cball p (h p)" using h[rule_format,of p]
  2606         by (simp add: \<open>p \<in> s\<close> dist_norm)
  2607       then have "p + h p \<in> s - insert p pts" using h[rule_format,of p] \<open>insert p pts \<subseteq> s\<close>
  2608         by fastforce
  2609       moreover have "pathstart g \<in> s - insert p pts " using path_img by auto
  2610       ultimately show ?thesis
  2611         using get_integrable_path[OF \<open>open s\<close> connected fin holo,of "pathstart g" "p+h p"] that
  2612         by blast
  2613     qed
  2614   obtain n::int where "n=winding_number g p"
  2615     using integer_winding_number[OF _ g_loop,of p] valid path_img
  2616     by (metis DiffD2 Ints_cases insertI1 subset_eq valid_path_imp_path)
  2617   define p_circ where "p_circ \<equiv> circlepath p (h p)"
  2618   define p_circ_pt where "p_circ_pt \<equiv> linepath (p+h p) (p+h p)"
  2619   define n_circ where "n_circ \<equiv> \<lambda>n. ((+++) p_circ ^^ n) p_circ_pt"
  2620   define cp where "cp \<equiv> if n\<ge>0 then reversepath (n_circ (nat n)) else n_circ (nat (- n))"
  2621   have n_circ:"valid_path (n_circ k)"
  2622       "winding_number (n_circ k) p = k"
  2623       "pathstart (n_circ k) = p + h p" "pathfinish (n_circ k) = p + h p"
  2624       "path_image (n_circ k) =  (if k=0 then {p + h p} else sphere p (h p))"
  2625       "p \<notin> path_image (n_circ k)"
  2626       "\<And>p'. p'\<notin>s - pts \<Longrightarrow> winding_number (n_circ k) p'=0 \<and> p'\<notin>path_image (n_circ k)"
  2627       "f contour_integrable_on (n_circ k)"
  2628       "contour_integral (n_circ k) f = k *  contour_integral p_circ f"
  2629       for k
  2630     proof (induct k)
  2631       case 0
  2632       show "valid_path (n_circ 0)"
  2633         and "path_image (n_circ 0) =  (if 0=0 then {p + h p} else sphere p (h p))"
  2634         and "winding_number (n_circ 0) p = of_nat 0"
  2635         and "pathstart (n_circ 0) = p + h p"
  2636         and "pathfinish (n_circ 0) = p + h p"
  2637         and "p \<notin> path_image (n_circ 0)"
  2638         unfolding n_circ_def p_circ_pt_def using \<open>h p > 0\<close>
  2639         by (auto simp add: dist_norm)
  2640       show "winding_number (n_circ 0) p'=0 \<and> p'\<notin>path_image (n_circ 0)" when "p'\<notin>s- pts" for p'
  2641         unfolding n_circ_def p_circ_pt_def
  2642         apply (auto intro!:winding_number_trivial)
  2643         by (metis Diff_iff pathfinish_in_path_image pg(3) pg(4) subsetCE subset_insertI that)+
  2644       show "f contour_integrable_on (n_circ 0)"
  2645         unfolding n_circ_def p_circ_pt_def
  2646         by (auto intro!:contour_integrable_continuous_linepath simp add:continuous_on_sing)
  2647       show "contour_integral (n_circ 0) f = of_nat 0  *  contour_integral p_circ f"
  2648         unfolding n_circ_def p_circ_pt_def by auto
  2649     next
  2650       case (Suc k)
  2651       have n_Suc:"n_circ (Suc k) = p_circ +++ n_circ k" unfolding n_circ_def by auto
  2652       have pcirc:"p \<notin> path_image p_circ" "valid_path p_circ" "pathfinish p_circ = pathstart (n_circ k)"
  2653         using Suc(3) unfolding p_circ_def using \<open>h p > 0\<close> by (auto simp add: p_circ_def)
  2654       have pcirc_image:"path_image p_circ \<subseteq> s - insert p pts"
  2655         proof -
  2656           have "path_image p_circ \<subseteq> cball p (h p)" using \<open>0 < h p\<close> p_circ_def by auto
  2657           then show ?thesis using h_p pcirc(1) by auto
  2658         qed
  2659       have pcirc_integrable:"f contour_integrable_on p_circ"
  2660         by (auto simp add:p_circ_def intro!: pcirc_image[unfolded p_circ_def]
  2661           contour_integrable_continuous_circlepath holomorphic_on_imp_continuous_on
  2662           holomorphic_on_subset[OF holo])
  2663       show "valid_path (n_circ (Suc k))"
  2664         using valid_path_join[OF pcirc(2) Suc(1) pcirc(3)] unfolding n_circ_def by auto
  2665       show "path_image (n_circ (Suc k))
  2666           = (if Suc k = 0 then {p + complex_of_real (h p)} else sphere p (h p))"
  2667         proof -
  2668           have "path_image p_circ = sphere p (h p)"
  2669             unfolding p_circ_def using \<open>0 < h p\<close> by auto
  2670           then show ?thesis unfolding n_Suc  using Suc.hyps(5)  \<open>h p>0\<close>
  2671             by (auto simp add:  path_image_join[OF pcirc(3)]  dist_norm)
  2672         qed
  2673       then show "p \<notin> path_image (n_circ (Suc k))" using \<open>h p>0\<close> by auto
  2674       show "winding_number (n_circ (Suc k)) p = of_nat (Suc k)"
  2675         proof -
  2676           have "winding_number p_circ p = 1"
  2677             by (simp add: \<open>h p > 0\<close> p_circ_def winding_number_circlepath_centre)
  2678           moreover have "p \<notin> path_image (n_circ k)" using Suc(5) \<open>h p>0\<close> by auto
  2679           then have "winding_number (p_circ +++ n_circ k) p
  2680               = winding_number p_circ p + winding_number (n_circ k) p"
  2681             using  valid_path_imp_path Suc.hyps(1) Suc.hyps(2) pcirc
  2682             apply (intro winding_number_join)
  2683             by auto
  2684           ultimately show ?thesis using Suc(2) unfolding n_circ_def
  2685             by auto
  2686         qed
  2687       show "pathstart (n_circ (Suc k)) = p + h p"
  2688         by (simp add: n_circ_def p_circ_def)
  2689       show "pathfinish (n_circ (Suc k)) = p + h p"
  2690         using Suc(4) unfolding n_circ_def by auto
  2691       show "winding_number (n_circ (Suc k)) p'=0 \<and>  p'\<notin>path_image (n_circ (Suc k))" when "p'\<notin>s-pts" for p'
  2692         proof -
  2693           have " p' \<notin> path_image p_circ" using \<open>p \<in> s\<close> h p_circ_def that using pcirc_image by blast
  2694           moreover have "p' \<notin> path_image (n_circ k)"
  2695             using Suc.hyps(7) that by blast
  2696           moreover have "winding_number p_circ p' = 0"
  2697             proof -
  2698               have "path_image p_circ \<subseteq> cball p (h p)"
  2699                 using h unfolding p_circ_def using \<open>p \<in> s\<close> by fastforce
  2700               moreover have "p'\<notin>cball p (h p)" using \<open>p \<in> s\<close> h that "2.hyps"(2) by fastforce
  2701               ultimately show ?thesis unfolding p_circ_def
  2702                 apply (intro winding_number_zero_outside)
  2703                 by auto
  2704             qed
  2705           ultimately show ?thesis
  2706             unfolding n_Suc
  2707             apply (subst winding_number_join)
  2708             by (auto simp: valid_path_imp_path pcirc Suc that not_in_path_image_join Suc.hyps(7)[OF that])
  2709         qed
  2710       show "f contour_integrable_on (n_circ (Suc k))"
  2711         unfolding n_Suc
  2712         by (rule contour_integrable_joinI[OF pcirc_integrable Suc(8) pcirc(2) Suc(1)])
  2713       show "contour_integral (n_circ (Suc k)) f = (Suc k) *  contour_integral p_circ f"
  2714         unfolding n_Suc
  2715         by (auto simp add:contour_integral_join[OF pcirc_integrable Suc(8) pcirc(2) Suc(1)]
  2716           Suc(9) algebra_simps)
  2717     qed
  2718   have cp[simp]:"pathstart cp = p + h p"  "pathfinish cp = p + h p"
  2719          "valid_path cp" "path_image cp \<subseteq> s - insert p pts"
  2720          "winding_number cp p = - n"
  2721          "\<And>p'. p'\<notin>s - pts \<Longrightarrow> winding_number cp p'=0 \<and> p' \<notin> path_image cp"
  2722          "f contour_integrable_on cp"
  2723          "contour_integral cp f = - n * contour_integral p_circ f"
  2724     proof -
  2725       show "pathstart cp = p + h p" and "pathfinish cp = p + h p" and "valid_path cp"
  2726         using n_circ unfolding cp_def by auto
  2727     next
  2728       have "sphere p (h p) \<subseteq>  s - insert p pts"
  2729         using h[rule_format,of p] \<open>insert p pts \<subseteq> s\<close> by force
  2730       moreover  have "p + complex_of_real (h p) \<in> s - insert p pts"
  2731         using pg(3) pg(4) by (metis pathfinish_in_path_image subsetCE)
  2732       ultimately show "path_image cp \<subseteq>  s - insert p pts" unfolding cp_def
  2733         using n_circ(5)  by auto
  2734     next
  2735       show "winding_number cp p = - n"
  2736         unfolding cp_def using winding_number_reversepath n_circ \<open>h p>0\<close>
  2737         by (auto simp: valid_path_imp_path)
  2738     next
  2739       show "winding_number cp p'=0 \<and> p' \<notin> path_image cp" when "p'\<notin>s - pts" for p'
  2740         unfolding cp_def
  2741         apply (auto)
  2742         apply (subst winding_number_reversepath)
  2743         by (auto simp add: valid_path_imp_path n_circ(7)[OF that] n_circ(1))
  2744     next
  2745       show "f contour_integrable_on cp" unfolding cp_def
  2746         using contour_integrable_reversepath_eq n_circ(1,8) by auto
  2747     next
  2748       show "contour_integral cp f = - n * contour_integral p_circ f"
  2749         unfolding cp_def using contour_integral_reversepath[OF n_circ(1)] n_circ(9)
  2750         by auto
  2751     qed
  2752   define g' where "g' \<equiv> g +++ pg +++ cp +++ (reversepath pg)"
  2753   have "contour_integral g' f = (\<Sum>p\<in>pts. winding_number g' p * contour_integral (circlepath p (h p)) f)"
  2754     proof (rule "2.hyps"(3)[of "s-{p}" "g'",OF _ _ \<open>finite pts\<close> ])
  2755       show "connected (s - {p} - pts)" using connected by (metis Diff_insert2)
  2756       show "open (s - {p})" using \<open>open s\<close> by auto
  2757       show " pts \<subseteq> s - {p}" using \<open>insert p pts \<subseteq> s\<close> \<open> p \<notin> pts\<close>  by blast
  2758       show "f holomorphic_on s - {p} - pts" using holo \<open>p \<notin> pts\<close> by (metis Diff_insert2)
  2759       show "valid_path g'"
  2760         unfolding g'_def cp_def using n_circ valid pg g_loop
  2761         by (auto intro!:valid_path_join )
  2762       show "pathfinish g' = pathstart g'"
  2763         unfolding g'_def cp_def using pg(2) by simp
  2764       show "path_image g' \<subseteq> s - {p} - pts"
  2765         proof -
  2766           define s' where "s' \<equiv> s - {p} - pts"
  2767           have s':"s' = s-insert p pts " unfolding s'_def by auto
  2768           then show ?thesis using path_img pg(4) cp(4)
  2769             unfolding g'_def
  2770             apply (fold s'_def s')
  2771             apply (intro subset_path_image_join)
  2772             by auto
  2773         qed
  2774       note path_join_imp[simp]
  2775       show "\<forall>z. z \<notin> s - {p} \<longrightarrow> winding_number g' z = 0"
  2776         proof clarify
  2777           fix z assume z:"z\<notin>s - {p}"
  2778           have "winding_number (g +++ pg +++ cp +++ reversepath pg) z = winding_number g z
  2779               + winding_number (pg +++ cp +++ (reversepath pg)) z"
  2780             proof (rule winding_number_join)
  2781               show "path g" using \<open>valid_path g\<close> by (simp add: valid_path_imp_path)
  2782               show "z \<notin> path_image g" using z path_img by auto
  2783               show "path (pg +++ cp +++ reversepath pg)" using pg(3) cp
  2784                 by (simp add: valid_path_imp_path)
  2785             next
  2786               have "path_image (pg +++ cp +++ reversepath pg) \<subseteq> s - insert p pts"
  2787                 using pg(4) cp(4) by (auto simp:subset_path_image_join)
  2788               then show "z \<notin> path_image (pg +++ cp +++ reversepath pg)" using z by auto
  2789             next
  2790               show "pathfinish g = pathstart (pg +++ cp +++ reversepath pg)" using g_loop by auto
  2791             qed
  2792           also have "... = winding_number g z + (winding_number pg z
  2793               + winding_number (cp +++ (reversepath pg)) z)"
  2794             proof (subst add_left_cancel,rule winding_number_join)
  2795               show "path pg" and "path (cp +++ reversepath pg)"
  2796                and "pathfinish pg = pathstart (cp +++ reversepath pg)"
  2797                 by (auto simp add: valid_path_imp_path)
  2798               show "z \<notin> path_image pg" using pg(4) z by blast
  2799               show "z \<notin> path_image (cp +++ reversepath pg)" using z
  2800                 by (metis Diff_iff \<open>z \<notin> path_image pg\<close> contra_subsetD cp(4) insertI1
  2801                   not_in_path_image_join path_image_reversepath singletonD)
  2802             qed
  2803           also have "... = winding_number g z + (winding_number pg z
  2804               + (winding_number cp z + winding_number (reversepath pg) z))"
  2805             apply (auto intro!:winding_number_join simp: valid_path_imp_path)
  2806             apply (metis Diff_iff contra_subsetD cp(4) insertI1 singletonD z)
  2807             by (metis Diff_insert2 Diff_subset contra_subsetD pg(4) z)
  2808           also have "... = winding_number g z + winding_number cp z"
  2809             apply (subst winding_number_reversepath)
  2810             apply (auto simp: valid_path_imp_path)
  2811             by (metis Diff_iff contra_subsetD insertI1 pg(4) singletonD z)
  2812           finally have "winding_number g' z = winding_number g z + winding_number cp z"
  2813             unfolding g'_def .
  2814           moreover have "winding_number g z + winding_number cp z = 0"
  2815             using winding z \<open>n=winding_number g p\<close> by auto
  2816           ultimately show "winding_number g' z = 0" unfolding g'_def by auto
  2817         qed
  2818       show "\<forall>pa\<in>s - {p}. 0 < h pa \<and> (\<forall>w\<in>cball pa (h pa). w \<in> s - {p} \<and> (w \<noteq> pa \<longrightarrow> w \<notin> pts))"
  2819         using h by fastforce
  2820     qed
  2821   moreover have "contour_integral g' f = contour_integral g f
  2822       - winding_number g p * contour_integral p_circ f"
  2823     proof -
  2824       have "contour_integral g' f =  contour_integral g f
  2825         + contour_integral (pg +++ cp +++ reversepath pg) f"
  2826         unfolding g'_def
  2827         apply (subst contour_integral_join)
  2828         by (auto simp add:open_Diff[OF \<open>open s\<close>,OF finite_imp_closed[OF fin]]
  2829           intro!: contour_integrable_holomorphic_simple[OF holo _ _ path_img]
  2830           contour_integrable_reversepath)
  2831       also have "... = contour_integral g f + contour_integral pg f
  2832           + contour_integral (cp +++ reversepath pg) f"
  2833         apply (subst contour_integral_join)
  2834         by (auto simp add:contour_integrable_reversepath)
  2835       also have "... = contour_integral g f + contour_integral pg f
  2836           + contour_integral cp f + contour_integral (reversepath pg) f"
  2837         apply (subst contour_integral_join)
  2838         by (auto simp add:contour_integrable_reversepath)
  2839       also have "... = contour_integral g f + contour_integral cp f"
  2840         using contour_integral_reversepath
  2841         by (auto simp add:contour_integrable_reversepath)
  2842       also have "... = contour_integral g f - winding_number g p * contour_integral p_circ f"
  2843         using \<open>n=winding_number g p\<close> by auto
  2844       finally show ?thesis .
  2845     qed
  2846   moreover have "winding_number g' p' = winding_number g p'" when "p'\<in>pts" for p'
  2847     proof -
  2848       have [simp]: "p' \<notin> path_image g" "p' \<notin> path_image pg" "p'\<notin>path_image cp"
  2849         using "2.prems"(8) that
  2850         apply blast
  2851         apply (metis Diff_iff Diff_insert2 contra_subsetD pg(4) that)
  2852         by (meson DiffD2 cp(4) rev_subsetD subset_insertI that)
  2853       have "winding_number g' p' = winding_number g p'
  2854           + winding_number (pg +++ cp +++ reversepath pg) p'" unfolding g'_def
  2855         apply (subst winding_number_join)
  2856         apply (simp_all add: valid_path_imp_path)
  2857         apply (intro not_in_path_image_join)
  2858         by auto
  2859       also have "... = winding_number g p' + winding_number pg p'
  2860           + winding_number (cp +++ reversepath pg) p'"
  2861         apply (subst winding_number_join)
  2862         apply (simp_all add: valid_path_imp_path)
  2863         apply (intro not_in_path_image_join)
  2864         by auto
  2865       also have "... = winding_number g p' + winding_number pg p'+ winding_number cp p'
  2866           + winding_number (reversepath pg) p'"
  2867         apply (subst winding_number_join)
  2868         by (simp_all add: valid_path_imp_path)
  2869       also have "... = winding_number g p' + winding_number cp p'"
  2870         apply (subst winding_number_reversepath)
  2871         by (simp_all add: valid_path_imp_path)
  2872       also have "... = winding_number g p'" using that by auto
  2873       finally show ?thesis .
  2874     qed
  2875   ultimately show ?case unfolding p_circ_def
  2876     apply (subst (asm) sum.cong[OF refl,
  2877         of pts _ "\<lambda>p. winding_number g p * contour_integral (circlepath p (h p)) f"])
  2878     by (auto simp add:sum.insert[OF \<open>finite pts\<close> \<open>p\<notin>pts\<close>] algebra_simps)
  2879 qed
  2880 
  2881 lemma Cauchy_theorem_singularities:
  2882   assumes "open s" "connected s" "finite pts" and
  2883           holo:"f holomorphic_on s-pts" and
  2884           "valid_path g" and
  2885           loop:"pathfinish g = pathstart g" and
  2886           "path_image g \<subseteq> s-pts" and
  2887           homo:"\<forall>z. (z \<notin> s) \<longrightarrow> winding_number g z  = 0" and
  2888           avoid:"\<forall>p\<in>s. h p>0 \<and> (\<forall>w\<in>cball p (h p). w\<in>s \<and> (w\<noteq>p \<longrightarrow> w \<notin> pts))"
  2889   shows "contour_integral g f = (\<Sum>p\<in>pts. winding_number g p * contour_integral (circlepath p (h p)) f)"
  2890     (is "?L=?R")
  2891 proof -
  2892   define circ where "circ \<equiv> \<lambda>p. winding_number g p * contour_integral (circlepath p (h p)) f"
  2893   define pts1 where "pts1 \<equiv> pts \<inter> s"
  2894   define pts2 where "pts2 \<equiv> pts - pts1"
  2895   have "pts=pts1 \<union> pts2" "pts1 \<inter> pts2 = {}" "pts2 \<inter> s={}" "pts1\<subseteq>s"
  2896     unfolding pts1_def pts2_def by auto
  2897   have "contour_integral g f =  (\<Sum>p\<in>pts1. circ p)" unfolding circ_def
  2898     proof (rule Cauchy_theorem_aux[OF \<open>open s\<close> _ _ \<open>pts1\<subseteq>s\<close> _ \<open>valid_path g\<close> loop _ homo])
  2899       have "finite pts1" unfolding pts1_def using \<open>finite pts\<close> by auto
  2900       then show "connected (s - pts1)"
  2901         using \<open>open s\<close> \<open>connected s\<close> connected_open_delete_finite[of s] by auto
  2902     next
  2903       show "finite pts1" using \<open>pts = pts1 \<union> pts2\<close> assms(3) by auto
  2904       show "f holomorphic_on s - pts1" by (metis Diff_Int2 Int_absorb holo pts1_def)
  2905       show "path_image g \<subseteq> s - pts1" using assms(7) pts1_def by auto
  2906       show "\<forall>p\<in>s. 0 < h p \<and> (\<forall>w\<in>cball p (h p). w \<in> s \<and> (w \<noteq> p \<longrightarrow> w \<notin> pts1))"
  2907         by (simp add: avoid pts1_def)
  2908     qed
  2909   moreover have "sum circ pts2=0"
  2910     proof -
  2911       have "winding_number g p=0" when "p\<in>pts2" for p
  2912         using  \<open>pts2 \<inter> s={}\<close> that homo[rule_format,of p] by auto
  2913       thus ?thesis unfolding circ_def
  2914         apply (intro sum.neutral)
  2915         by auto
  2916     qed
  2917   moreover have "?R=sum circ pts1 + sum circ pts2"
  2918     unfolding circ_def
  2919     using sum.union_disjoint[OF _ _ \<open>pts1 \<inter> pts2 = {}\<close>] \<open>finite pts\<close> \<open>pts=pts1 \<union> pts2\<close>
  2920     by blast
  2921   ultimately show ?thesis
  2922     apply (fold circ_def)
  2923     by auto
  2924 qed
  2925 
  2926 theorem Residue_theorem:
  2927   fixes s pts::"complex set" and f::"complex \<Rightarrow> complex"
  2928     and g::"real \<Rightarrow> complex"
  2929   assumes "open s" "connected s" "finite pts" and
  2930           holo:"f holomorphic_on s-pts" and
  2931           "valid_path g" and
  2932           loop:"pathfinish g = pathstart g" and
  2933           "path_image g \<subseteq> s-pts" and
  2934           homo:"\<forall>z. (z \<notin> s) \<longrightarrow> winding_number g z  = 0"
  2935   shows "contour_integral g f = 2 * pi * \<i> *(\<Sum>p\<in>pts. winding_number g p * residue f p)"
  2936 proof -
  2937   define c where "c \<equiv>  2 * pi * \<i>"
  2938   obtain h where avoid:"\<forall>p\<in>s. h p>0 \<and> (\<forall>w\<in>cball p (h p). w\<in>s \<and> (w\<noteq>p \<longrightarrow> w \<notin> pts))"
  2939     using finite_cball_avoid[OF \<open>open s\<close> \<open>finite pts\<close>] by metis
  2940   have "contour_integral g f
  2941       = (\<Sum>p\<in>pts. winding_number g p * contour_integral (circlepath p (h p)) f)"
  2942     using Cauchy_theorem_singularities[OF assms avoid] .
  2943   also have "... = (\<Sum>p\<in>pts.  c * winding_number g p * residue f p)"
  2944     proof (intro sum.cong)
  2945       show "pts = pts" by simp
  2946     next
  2947       fix x assume "x \<in> pts"
  2948       show "winding_number g x * contour_integral (circlepath x (h x)) f
  2949           = c * winding_number g x * residue f x"
  2950         proof (cases "x\<in>s")
  2951           case False
  2952           then have "winding_number g x=0" using homo by auto
  2953           thus ?thesis by auto
  2954         next
  2955           case True
  2956           have "contour_integral (circlepath x (h x)) f = c* residue f x"
  2957             using \<open>x\<in>pts\<close> \<open>finite pts\<close> avoid[rule_format,OF True]
  2958             apply (intro base_residue[of "s-(pts-{x})",THEN contour_integral_unique,folded c_def])
  2959             by (auto intro:holomorphic_on_subset[OF holo] open_Diff[OF \<open>open s\<close> finite_imp_closed])
  2960           then show ?thesis by auto
  2961         qed
  2962     qed
  2963   also have "... = c * (\<Sum>p\<in>pts. winding_number g p * residue f p)"
  2964     by (simp add: sum_distrib_left algebra_simps)
  2965   finally show ?thesis unfolding c_def .
  2966 qed
  2967 
  2968 subsection \<open>Non-essential singular points\<close>
  2969 
  2970 definition%important is_pole :: 
  2971   "('a::topological_space \<Rightarrow> 'b::real_normed_vector) \<Rightarrow> 'a \<Rightarrow> bool" where
  2972   "is_pole f a =  (LIM x (at a). f x :> at_infinity)"
  2973 
  2974 lemma is_pole_cong:
  2975   assumes "eventually (\<lambda>x. f x = g x) (at a)" "a=b"
  2976   shows "is_pole f a \<longleftrightarrow> is_pole g b"
  2977   unfolding is_pole_def using assms by (intro filterlim_cong,auto)
  2978 
  2979 lemma is_pole_transform:
  2980   assumes "is_pole f a" "eventually (\<lambda>x. f x = g x) (at a)" "a=b"
  2981   shows "is_pole g b"
  2982   using is_pole_cong assms by auto
  2983 
  2984 lemma is_pole_tendsto:
  2985   fixes f::"('a::topological_space \<Rightarrow> 'b::real_normed_div_algebra)"
  2986   shows "is_pole f x \<Longrightarrow> ((inverse o f) \<longlongrightarrow> 0) (at x)"
  2987 unfolding is_pole_def
  2988 by (auto simp add:filterlim_inverse_at_iff[symmetric] comp_def filterlim_at)
  2989 
  2990 lemma is_pole_inverse_holomorphic:
  2991   assumes "open s"
  2992     and f_holo:"f holomorphic_on (s-{z})"
  2993     and pole:"is_pole f z"
  2994     and non_z:"\<forall>x\<in>s-{z}. f x\<noteq>0"
  2995   shows "(\<lambda>x. if x=z then 0 else inverse (f x)) holomorphic_on s"
  2996 proof -
  2997   define g where "g \<equiv> \<lambda>x. if x=z then 0 else inverse (f x)"
  2998   have "isCont g z" unfolding isCont_def  using is_pole_tendsto[OF pole]
  2999     apply (subst Lim_cong_at[where b=z and y=0 and g="inverse \<circ> f"])
  3000     by (simp_all add:g_def)
  3001   moreover have "continuous_on (s-{z}) f" using f_holo holomorphic_on_imp_continuous_on by auto
  3002   hence "continuous_on (s-{z}) (inverse o f)" unfolding comp_def
  3003     by (auto elim!:continuous_on_inverse simp add:non_z)
  3004   hence "continuous_on (s-{z}) g" unfolding g_def
  3005     apply (subst continuous_on_cong[where t="s-{z}" and g="inverse o f"])
  3006     by auto
  3007   ultimately have "continuous_on s g" using open_delete[OF \<open>open s\<close>] \<open>open s\<close>
  3008     by (auto simp add:continuous_on_eq_continuous_at)
  3009   moreover have "(inverse o f) holomorphic_on (s-{z})"
  3010     unfolding comp_def using f_holo
  3011     by (auto elim!:holomorphic_on_inverse simp add:non_z)
  3012   hence "g holomorphic_on (s-{z})"
  3013     apply (subst holomorphic_cong[where t="s-{z}" and g="inverse o f"])
  3014     by (auto simp add:g_def)
  3015   ultimately show ?thesis unfolding g_def using \<open>open s\<close>
  3016     by (auto elim!: no_isolated_singularity)
  3017 qed
  3018 
  3019 lemma not_is_pole_holomorphic:
  3020   assumes "open A" "x \<in> A" "f holomorphic_on A"
  3021   shows   "\<not>is_pole f x"
  3022 proof -
  3023   have "continuous_on A f" by (intro holomorphic_on_imp_continuous_on) fact
  3024   with assms have "isCont f x" by (simp add: continuous_on_eq_continuous_at)
  3025   hence "f \<midarrow>x\<rightarrow> f x" by (simp add: isCont_def)
  3026   thus "\<not>is_pole f x" unfolding is_pole_def
  3027     using not_tendsto_and_filterlim_at_infinity[of "at x" f "f x"] by auto
  3028 qed
  3029 
  3030 lemma is_pole_inverse_power: "n > 0 \<Longrightarrow> is_pole (\<lambda>z::complex. 1 / (z - a) ^ n) a"
  3031   unfolding is_pole_def inverse_eq_divide [symmetric]
  3032   by (intro filterlim_compose[OF filterlim_inverse_at_infinity] tendsto_intros)
  3033      (auto simp: filterlim_at eventually_at intro!: exI[of _ 1] tendsto_eq_intros)
  3034 
  3035 lemma is_pole_inverse: "is_pole (\<lambda>z::complex. 1 / (z - a)) a"
  3036   using is_pole_inverse_power[of 1 a] by simp
  3037 
  3038 lemma is_pole_divide:
  3039   fixes f :: "'a :: t2_space \<Rightarrow> 'b :: real_normed_field"
  3040   assumes "isCont f z" "filterlim g (at 0) (at z)" "f z \<noteq> 0"
  3041   shows   "is_pole (\<lambda>z. f z / g z) z"
  3042 proof -
  3043   have "filterlim (\<lambda>z. f z * inverse (g z)) at_infinity (at z)"
  3044     by (intro tendsto_mult_filterlim_at_infinity[of _ "f z"]
  3045                  filterlim_compose[OF filterlim_inverse_at_infinity])+
  3046        (insert assms, auto simp: isCont_def)
  3047   thus ?thesis by (simp add: divide_simps is_pole_def)
  3048 qed
  3049 
  3050 lemma is_pole_basic:
  3051   assumes "f holomorphic_on A" "open A" "z \<in> A" "f z \<noteq> 0" "n > 0"
  3052   shows   "is_pole (\<lambda>w. f w / (w - z) ^ n) z"
  3053 proof (rule is_pole_divide)
  3054   have "continuous_on A f" by (rule holomorphic_on_imp_continuous_on) fact
  3055   with assms show "isCont f z" by (auto simp: continuous_on_eq_continuous_at)
  3056   have "filterlim (\<lambda>w. (w - z) ^ n) (nhds 0) (at z)"
  3057     using assms by (auto intro!: tendsto_eq_intros)
  3058   thus "filterlim (\<lambda>w. (w - z) ^ n) (at 0) (at z)"
  3059     by (intro filterlim_atI tendsto_eq_intros)
  3060        (insert assms, auto simp: eventually_at_filter)
  3061 qed fact+
  3062 
  3063 lemma is_pole_basic':
  3064   assumes "f holomorphic_on A" "open A" "0 \<in> A" "f 0 \<noteq> 0" "n > 0"
  3065   shows   "is_pole (\<lambda>w. f w / w ^ n) 0"
  3066   using is_pole_basic[of f A 0] assms by simp
  3067 
  3068 text \<open>The proposition 
  3069               \<^term>\<open>\<exists>x. ((f::complex\<Rightarrow>complex) \<longlongrightarrow> x) (at z) \<or> is_pole f z\<close> 
  3070 can be interpreted as the complex function \<^term>\<open>f\<close> has a non-essential singularity at \<^term>\<open>z\<close> 
  3071 (i.e. the singularity is either removable or a pole).\<close> 
  3072 definition not_essential::"[complex \<Rightarrow> complex, complex] \<Rightarrow> bool" where
  3073   "not_essential f z = (\<exists>x. f\<midarrow>z\<rightarrow>x \<or> is_pole f z)"
  3074 
  3075 definition isolated_singularity_at::"[complex \<Rightarrow> complex, complex] \<Rightarrow> bool" where
  3076   "isolated_singularity_at f z = (\<exists>r>0. f analytic_on ball z r-{z})"
  3077 
  3078 named_theorems singularity_intros "introduction rules for singularities"
  3079 
  3080 lemma holomorphic_factor_unique:
  3081   fixes f::"complex \<Rightarrow> complex" and z::complex and r::real and m n::int
  3082   assumes "r>0" "g z\<noteq>0" "h z\<noteq>0"
  3083     and asm:"\<forall>w\<in>ball z r-{z}. f w = g w * (w-z) powr n \<and> g w\<noteq>0 \<and> f w =  h w * (w - z) powr m \<and> h w\<noteq>0"
  3084     and g_holo:"g holomorphic_on ball z r" and h_holo:"h holomorphic_on ball z r"
  3085   shows "n=m"
  3086 proof -
  3087   have [simp]:"at z within ball z r \<noteq> bot" using \<open>r>0\<close>
  3088       by (auto simp add:at_within_ball_bot_iff)
  3089   have False when "n>m"
  3090   proof -
  3091     have "(h \<longlongrightarrow> 0) (at z within ball z r)"
  3092     proof (rule Lim_transform_within[OF _ \<open>r>0\<close>, where f="\<lambda>w. (w - z) powr (n - m) * g w"])
  3093       have "\<forall>w\<in>ball z r-{z}. h w = (w-z)powr(n-m) * g w"
  3094         using \<open>n>m\<close> asm \<open>r>0\<close>
  3095         apply (auto simp add:field_simps powr_diff)
  3096         by force
  3097       then show "\<lbrakk>x' \<in> ball z r; 0 < dist x' z;dist x' z < r\<rbrakk>
  3098             \<Longrightarrow> (x' - z) powr (n - m) * g x' = h x'" for x' by auto
  3099     next
  3100       define F where "F \<equiv> at z within ball z r"
  3101       define f' where "f' \<equiv> \<lambda>x. (x - z) powr (n-m)"
  3102       have "f' z=0" using \<open>n>m\<close> unfolding f'_def by auto
  3103       moreover have "continuous F f'" unfolding f'_def F_def continuous_def
  3104         apply (subst netlimit_within)
  3105         using \<open>n>m\<close> by (auto intro!:tendsto_powr_complex_0 tendsto_eq_intros)  
  3106       ultimately have "(f' \<longlongrightarrow> 0) F" unfolding F_def
  3107         by (simp add: continuous_within)
  3108       moreover have "(g \<longlongrightarrow> g z) F"
  3109         using holomorphic_on_imp_continuous_on[OF g_holo,unfolded continuous_on_def] \<open>r>0\<close>
  3110         unfolding F_def by auto
  3111       ultimately show " ((\<lambda>w. f' w * g w) \<longlongrightarrow> 0) F" using tendsto_mult by fastforce
  3112     qed
  3113     moreover have "(h \<longlongrightarrow> h z) (at z within ball z r)"
  3114       using holomorphic_on_imp_continuous_on[OF h_holo]
  3115       by (auto simp add:continuous_on_def \<open>r>0\<close>)
  3116     ultimately have "h z=0" by (auto intro!: tendsto_unique)
  3117     thus False using \<open>h z\<noteq>0\<close> by auto
  3118   qed
  3119   moreover have False when "m>n"
  3120   proof -
  3121     have "(g \<longlongrightarrow> 0) (at z within ball z r)"
  3122     proof (rule Lim_transform_within[OF _ \<open>r>0\<close>, where f="\<lambda>w. (w - z) powr (m - n) * h w"])
  3123       have "\<forall>w\<in>ball z r -{z}. g w = (w-z) powr (m-n) * h w" using \<open>m>n\<close> asm
  3124         apply (auto simp add:field_simps powr_diff)
  3125         by force
  3126       then show "\<lbrakk>x' \<in> ball z r; 0 < dist x' z;dist x' z < r\<rbrakk>
  3127             \<Longrightarrow> (x' - z) powr (m - n) * h x' = g x'" for x' by auto
  3128     next
  3129       define F where "F \<equiv> at z within ball z r"
  3130       define f' where "f' \<equiv>\<lambda>x. (x - z) powr (m-n)"
  3131       have "f' z=0" using \<open>m>n\<close> unfolding f'_def by auto
  3132       moreover have "continuous F f'" unfolding f'_def F_def continuous_def
  3133         apply (subst netlimit_within)
  3134         using \<open>m>n\<close> by (auto intro!:tendsto_powr_complex_0 tendsto_eq_intros)
  3135       ultimately have "(f' \<longlongrightarrow> 0) F" unfolding F_def
  3136         by (simp add: continuous_within)
  3137       moreover have "(h \<longlongrightarrow> h z) F"
  3138         using holomorphic_on_imp_continuous_on[OF h_holo,unfolded continuous_on_def] \<open>r>0\<close>
  3139         unfolding F_def by auto
  3140       ultimately show " ((\<lambda>w. f' w * h w) \<longlongrightarrow> 0) F" using tendsto_mult by fastforce
  3141     qed
  3142     moreover have "(g \<longlongrightarrow> g z) (at z within ball z r)"
  3143       using holomorphic_on_imp_continuous_on[OF g_holo]
  3144       by (auto simp add:continuous_on_def \<open>r>0\<close>)
  3145     ultimately have "g z=0" by (auto intro!: tendsto_unique)
  3146     thus False using \<open>g z\<noteq>0\<close> by auto
  3147   qed
  3148   ultimately show "n=m" by fastforce
  3149 qed
  3150 
  3151 lemma holomorphic_factor_puncture:
  3152   assumes f_iso:"isolated_singularity_at f z"   
  3153       and "not_essential f z" \<comment> \<open>\<^term>\<open>f\<close> has either a removable singularity or a pole at \<^term>\<open>z\<close>\<close>
  3154       and non_zero:"\<exists>\<^sub>Fw in (at z). f w\<noteq>0" \<comment> \<open>\<^term>\<open>f\<close> will not be constantly zero in a neighbour of \<^term>\<open>z\<close>\<close>
  3155   shows "\<exists>!n::int. \<exists>g r. 0 < r \<and> g holomorphic_on cball z r \<and> g z\<noteq>0
  3156           \<and> (\<forall>w\<in>cball z r-{z}. f w = g w * (w-z) powr n \<and> g w\<noteq>0)"
  3157 proof -
  3158   define P where "P = (\<lambda>f n g r. 0 < r \<and> g holomorphic_on cball z r \<and> g z\<noteq>0
  3159           \<and> (\<forall>w\<in>cball z r - {z}. f w = g w * (w-z) powr (of_int n)  \<and> g w\<noteq>0))"
  3160   have imp_unique:"\<exists>!n::int. \<exists>g r. P f n g r" when "\<exists>n g r. P f n g r" 
  3161   proof (rule ex_ex1I[OF that])
  3162     fix n1 n2 :: int
  3163     assume g1_asm:"\<exists>g1 r1. P f n1 g1 r1" and g2_asm:"\<exists>g2 r2. P f n2 g2 r2"
  3164     define fac where "fac \<equiv> \<lambda>n g r. \<forall>w\<in>cball z r-{z}. f w = g w * (w - z) powr (of_int n) \<and> g w \<noteq> 0"
  3165     obtain g1 r1 where "0 < r1" and g1_holo: "g1 holomorphic_on cball z r1" and "g1 z\<noteq>0"
  3166         and "fac n1 g1 r1" using g1_asm unfolding P_def fac_def by auto
  3167     obtain g2 r2 where "0 < r2" and g2_holo: "g2 holomorphic_on cball z r2" and "g2 z\<noteq>0"
  3168         and "fac n2 g2 r2" using g2_asm unfolding P_def fac_def by auto
  3169     define r where "r \<equiv> min r1 r2"
  3170     have "r>0" using \<open>r1>0\<close> \<open>r2>0\<close> unfolding r_def by auto
  3171     moreover have "\<forall>w\<in>ball z r-{z}. f w = g1 w * (w-z) powr n1 \<and> g1 w\<noteq>0 
  3172         \<and> f w = g2 w * (w - z) powr n2  \<and> g2 w\<noteq>0"
  3173       using \<open>fac n1 g1 r1\<close> \<open>fac n2 g2 r2\<close>   unfolding fac_def r_def
  3174       by fastforce
  3175     ultimately show "n1=n2" using g1_holo g2_holo \<open>g1 z\<noteq>0\<close> \<open>g2 z\<noteq>0\<close>
  3176       apply (elim holomorphic_factor_unique)
  3177       by (auto simp add:r_def) 
  3178   qed
  3179 
  3180   have P_exist:"\<exists> n g r. P h n g r" when 
  3181       "\<exists>z'. (h \<longlongrightarrow> z') (at z)" "isolated_singularity_at h z"  "\<exists>\<^sub>Fw in (at z). h w\<noteq>0" 
  3182     for h
  3183   proof -
  3184     from that(2) obtain r where "r>0" "h analytic_on ball z r - {z}"
  3185       unfolding isolated_singularity_at_def by auto
  3186     obtain z' where "(h \<longlongrightarrow> z') (at z)" using \<open>\<exists>z'. (h \<longlongrightarrow> z') (at z)\<close> by auto
  3187     define h' where "h'=(\<lambda>x. if x=z then z' else h x)"
  3188     have "h' holomorphic_on ball z r"
  3189       apply (rule no_isolated_singularity'[of "{z}"]) 
  3190       subgoal by (metis LIM_equal Lim_at_imp_Lim_at_within \<open>h \<midarrow>z\<rightarrow> z'\<close> empty_iff h'_def insert_iff)
  3191       subgoal using \<open>h analytic_on ball z r - {z}\<close> analytic_imp_holomorphic h'_def holomorphic_transform 
  3192         by fastforce
  3193       by auto
  3194     have ?thesis when "z'=0"
  3195     proof - 
  3196       have "h' z=0" using that unfolding h'_def by auto
  3197       moreover have "\<not> h' constant_on ball z r" 
  3198         using \<open>\<exists>\<^sub>Fw in (at z). h w\<noteq>0\<close> unfolding constant_on_def frequently_def eventually_at h'_def
  3199         apply simp
  3200         by (metis \<open>0 < r\<close> centre_in_ball dist_commute mem_ball that)
  3201       moreover note \<open>h' holomorphic_on ball z r\<close>
  3202       ultimately obtain g r1 n where "0 < n" "0 < r1" "ball z r1 \<subseteq> ball z r" and
  3203           g:"g holomorphic_on ball z r1"
  3204           "\<And>w. w \<in> ball z r1 \<Longrightarrow> h' w = (w - z) ^ n * g w"
  3205           "\<And>w. w \<in> ball z r1 \<Longrightarrow> g w \<noteq> 0" 
  3206         using holomorphic_factor_zero_nonconstant[of _ "ball z r" z thesis,simplified,
  3207                 OF \<open>h' holomorphic_on ball z r\<close> \<open>r>0\<close> \<open>h' z=0\<close> \<open>\<not> h' constant_on ball z r\<close>] 
  3208         by (auto simp add:dist_commute)
  3209       define rr where "rr=r1/2"
  3210       have "P h' n g rr"
  3211         unfolding P_def rr_def
  3212         using \<open>n>0\<close> \<open>r1>0\<close> g by (auto simp add:powr_nat)
  3213       then have "P h n g rr"
  3214         unfolding h'_def P_def by auto
  3215       then show ?thesis unfolding P_def by blast
  3216     qed
  3217     moreover have ?thesis when "z'\<noteq>0"
  3218     proof -
  3219       have "h' z\<noteq>0" using that unfolding h'_def by auto
  3220       obtain r1 where "r1>0" "cball z r1 \<subseteq> ball z r" "\<forall>x\<in>cball z r1. h' x\<noteq>0"
  3221       proof -
  3222         have "isCont h' z" "h' z\<noteq>0"
  3223           by (auto simp add: Lim_cong_within \<open>h \<midarrow>z\<rightarrow> z'\<close> \<open>z'\<noteq>0\<close> continuous_at h'_def)
  3224         then obtain r2 where r2:"r2>0" "\<forall>x\<in>ball z r2. h' x\<noteq>0"
  3225           using continuous_at_avoid[of z h' 0 ] unfolding ball_def by auto
  3226         define r1 where "r1=min r2 r / 2"
  3227         have "0 < r1" "cball z r1 \<subseteq> ball z r" 
  3228           using \<open>r2>0\<close> \<open>r>0\<close> unfolding r1_def by auto
  3229         moreover have "\<forall>x\<in>cball z r1. h' x \<noteq> 0" 
  3230           using r2 unfolding r1_def by simp
  3231         ultimately show ?thesis using that by auto
  3232       qed
  3233       then have "P h' 0 h' r1" using \<open>h' holomorphic_on ball z r\<close> unfolding P_def by auto
  3234       then have "P h 0 h' r1" unfolding P_def h'_def by auto
  3235       then show ?thesis unfolding P_def by blast
  3236     qed
  3237     ultimately show ?thesis by auto
  3238   qed
  3239 
  3240   have ?thesis when "\<exists>x. (f \<longlongrightarrow> x) (at z)"
  3241     apply (rule_tac imp_unique[unfolded P_def])
  3242     using P_exist[OF that(1) f_iso non_zero] unfolding P_def .
  3243   moreover have ?thesis when "is_pole f z"
  3244   proof (rule imp_unique[unfolded P_def])
  3245     obtain e where [simp]:"e>0" and e_holo:"f holomorphic_on ball z e - {z}" and e_nz: "\<forall>x\<in>ball z e-{z}. f x\<noteq>0"
  3246     proof -
  3247       have "\<forall>\<^sub>F z in at z. f z \<noteq> 0"
  3248         using \<open>is_pole f z\<close> filterlim_at_infinity_imp_eventually_ne unfolding is_pole_def
  3249         by auto
  3250       then obtain e1 where e1:"e1>0" "\<forall>x\<in>ball z e1-{z}. f x\<noteq>0"
  3251         using that eventually_at[of "\<lambda>x. f x\<noteq>0" z UNIV,simplified] by (auto simp add:dist_commute)
  3252       obtain e2 where e2:"e2>0" "f holomorphic_on ball z e2 - {z}"
  3253         using f_iso analytic_imp_holomorphic unfolding isolated_singularity_at_def by auto
  3254       define e where "e=min e1 e2"
  3255       show ?thesis
  3256         apply (rule that[of e])
  3257         using  e1 e2 unfolding e_def by auto
  3258     qed
  3259     
  3260     define h where "h \<equiv> \<lambda>x. inverse (f x)"
  3261 
  3262     have "\<exists>n g r. P h n g r"
  3263     proof -
  3264       have "h \<midarrow>z\<rightarrow> 0" 
  3265         using Lim_transform_within_open assms(2) h_def is_pole_tendsto that by fastforce
  3266       moreover have "\<exists>\<^sub>Fw in (at z). h w\<noteq>0"
  3267         using non_zero 
  3268         apply (elim frequently_rev_mp)
  3269         unfolding h_def eventually_at by (auto intro:exI[where x=1])
  3270       moreover have "isolated_singularity_at h z"
  3271         unfolding isolated_singularity_at_def h_def
  3272         apply (rule exI[where x=e])
  3273         using e_holo e_nz \<open>e>0\<close> by (metis open_ball analytic_on_open 
  3274             holomorphic_on_inverse open_delete)
  3275       ultimately show ?thesis
  3276         using P_exist[of h] by auto
  3277     qed
  3278     then obtain n g r
  3279       where "0 < r" and
  3280             g_holo:"g holomorphic_on cball z r" and "g z\<noteq>0" and
  3281             g_fac:"(\<forall>w\<in>cball z r-{z}. h w = g w * (w - z) powr of_int n  \<and> g w \<noteq> 0)"
  3282       unfolding P_def by auto
  3283     have "P f (-n) (inverse o g) r"
  3284     proof -
  3285       have "f w = inverse (g w) * (w - z) powr of_int (- n)" when "w\<in>cball z r - {z}" for w
  3286         using g_fac[rule_format,of w] that unfolding h_def 
  3287         apply (auto simp add:powr_minus )
  3288         by (metis inverse_inverse_eq inverse_mult_distrib)
  3289       then show ?thesis 
  3290         unfolding P_def comp_def
  3291         using \<open>r>0\<close> g_holo g_fac \<open>g z\<noteq>0\<close> by (auto intro:holomorphic_intros)
  3292     qed
  3293     then show "\<exists>x g r. 0 < r \<and> g holomorphic_on cball z r \<and> g z \<noteq> 0 
  3294                   \<and> (\<forall>w\<in>cball z r - {z}. f w = g w * (w - z) powr of_int x  \<and> g w \<noteq> 0)"
  3295       unfolding P_def by blast
  3296   qed
  3297   ultimately show ?thesis using \<open>not_essential f z\<close> unfolding not_essential_def  by presburger
  3298 qed
  3299 
  3300 lemma not_essential_transform:
  3301   assumes "not_essential g z"
  3302   assumes "\<forall>\<^sub>F w in (at z). g w = f w"
  3303   shows "not_essential f z" 
  3304   using assms unfolding not_essential_def
  3305   by (simp add: filterlim_cong is_pole_cong)
  3306 
  3307 lemma isolated_singularity_at_transform:
  3308   assumes "isolated_singularity_at g z"
  3309   assumes "\<forall>\<^sub>F w in (at z). g w = f w"
  3310   shows "isolated_singularity_at f z" 
  3311 proof -
  3312   obtain r1 where "r1>0" and r1:"g analytic_on ball z r1 - {z}"
  3313     using assms(1) unfolding isolated_singularity_at_def by auto
  3314   obtain r2 where "r2>0" and r2:" \<forall>x. x \<noteq> z \<and> dist x z < r2 \<longrightarrow> g x = f x"
  3315     using assms(2) unfolding eventually_at by auto
  3316   define r3 where "r3=min r1 r2"
  3317   have "r3>0" unfolding r3_def using \<open>r1>0\<close> \<open>r2>0\<close> by auto
  3318   moreover have "f analytic_on ball z r3 - {z}"
  3319   proof -
  3320     have "g holomorphic_on ball z r3 - {z}"
  3321       using r1 unfolding r3_def by (subst (asm) analytic_on_open,auto)
  3322     then have "f holomorphic_on ball z r3 - {z}"
  3323       using r2 unfolding r3_def 
  3324       by (auto simp add:dist_commute elim!:holomorphic_transform)
  3325     then show ?thesis by (subst analytic_on_open,auto)  
  3326   qed
  3327   ultimately show ?thesis unfolding isolated_singularity_at_def by auto
  3328 qed
  3329 
  3330 lemma not_essential_powr[singularity_intros]:
  3331   assumes "LIM w (at z). f w :> (at x)"
  3332   shows "not_essential (\<lambda>w. (f w) powr (of_int n)) z"
  3333 proof -
  3334   define fp where "fp=(\<lambda>w. (f w) powr (of_int n))"
  3335   have ?thesis when "n>0"
  3336   proof -
  3337     have "(\<lambda>w.  (f w) ^ (nat n)) \<midarrow>z\<rightarrow> x ^ nat n" 
  3338       using that assms unfolding filterlim_at by (auto intro!:tendsto_eq_intros)
  3339     then have "fp \<midarrow>z\<rightarrow> x ^ nat n" unfolding fp_def      
  3340       apply (elim Lim_transform_within[where d=1],simp)
  3341       by (metis less_le powr_0 powr_of_int that zero_less_nat_eq zero_power)
  3342     then show ?thesis unfolding not_essential_def fp_def by auto
  3343   qed
  3344   moreover have ?thesis when "n=0"
  3345   proof -
  3346     have "fp \<midarrow>z\<rightarrow> 1 " 
  3347       apply (subst tendsto_cong[where g="\<lambda>_.1"])
  3348       using that filterlim_at_within_not_equal[OF assms,of 0] unfolding fp_def by auto
  3349     then show ?thesis unfolding fp_def not_essential_def by auto
  3350   qed
  3351   moreover have ?thesis when "n<0"
  3352   proof (cases "x=0")
  3353     case True
  3354     have "LIM w (at z). inverse ((f w) ^ (nat (-n))) :> at_infinity"
  3355       apply (subst filterlim_inverse_at_iff[symmetric],simp)
  3356       apply (rule filterlim_atI)
  3357       subgoal using assms True that unfolding filterlim_at by (auto intro!:tendsto_eq_intros)
  3358       subgoal using filterlim_at_within_not_equal[OF assms,of 0] 
  3359         by (eventually_elim,insert that,auto)
  3360       done
  3361     then have "LIM w (at z). fp w :> at_infinity"
  3362     proof (elim filterlim_mono_eventually)
  3363       show "\<forall>\<^sub>F x in at z. inverse (f x ^ nat (- n)) = fp x"
  3364         using filterlim_at_within_not_equal[OF assms,of 0] unfolding fp_def
  3365         apply eventually_elim
  3366         using powr_of_int that by auto
  3367     qed auto
  3368     then show ?thesis unfolding fp_def not_essential_def is_pole_def by auto
  3369   next
  3370     case False
  3371     let ?xx= "inverse (x ^ (nat (-n)))"
  3372     have "(\<lambda>w. inverse ((f w) ^ (nat (-n)))) \<midarrow>z\<rightarrow>?xx"
  3373       using assms False unfolding filterlim_at by (auto intro!:tendsto_eq_intros)
  3374     then have "fp \<midarrow>z\<rightarrow>?xx"
  3375       apply (elim Lim_transform_within[where d=1],simp)
  3376       unfolding fp_def by (metis inverse_zero nat_mono_iff nat_zero_as_int neg_0_less_iff_less 
  3377           not_le power_eq_0_iff powr_0 powr_of_int that)
  3378     then show ?thesis unfolding fp_def not_essential_def by auto
  3379   qed
  3380   ultimately show ?thesis by linarith
  3381 qed
  3382 
  3383 lemma isolated_singularity_at_powr[singularity_intros]:
  3384   assumes "isolated_singularity_at f z" "\<forall>\<^sub>F w in (at z). f w\<noteq>0"
  3385   shows "isolated_singularity_at (\<lambda>w. (f w) powr (of_int n)) z"
  3386 proof -
  3387   obtain r1 where "r1>0" "f analytic_on ball z r1 - {z}"
  3388     using assms(1) unfolding isolated_singularity_at_def by auto
  3389   then have r1:"f holomorphic_on ball z r1 - {z}"
  3390     using analytic_on_open[of "ball z r1-{z}" f] by blast
  3391   obtain r2 where "r2>0" and r2:"\<forall>w. w \<noteq> z \<and> dist w z < r2 \<longrightarrow> f w \<noteq> 0"
  3392     using assms(2) unfolding eventually_at by auto
  3393   define r3 where "r3=min r1 r2"
  3394   have "(\<lambda>w. (f w) powr of_int n) holomorphic_on ball z r3 - {z}"
  3395     apply (rule holomorphic_on_powr_of_int)
  3396     subgoal unfolding r3_def using r1 by auto
  3397     subgoal unfolding r3_def using r2 by (auto simp add:dist_commute)
  3398     done
  3399   moreover have "r3>0" unfolding r3_def using \<open>0 < r1\<close> \<open>0 < r2\<close> by linarith
  3400   ultimately show ?thesis unfolding isolated_singularity_at_def
  3401     apply (subst (asm) analytic_on_open[symmetric])
  3402     by auto
  3403 qed
  3404 
  3405 lemma non_zero_neighbour:
  3406   assumes f_iso:"isolated_singularity_at f z"   
  3407       and f_ness:"not_essential f z" 
  3408       and f_nconst:"\<exists>\<^sub>Fw in (at z). f w\<noteq>0"
  3409     shows "\<forall>\<^sub>F w in (at z). f w\<noteq>0"
  3410 proof -
  3411   obtain fn fp fr where [simp]:"fp z \<noteq> 0" and "fr > 0"
  3412           and fr: "fp holomorphic_on cball z fr" 
  3413                   "\<forall>w\<in>cball z fr - {z}. f w = fp w * (w - z) powr of_int fn \<and> fp w \<noteq> 0"
  3414     using holomorphic_factor_puncture[OF f_iso f_ness f_nconst,THEN ex1_implies_ex] by auto
  3415   have "f w \<noteq> 0" when " w \<noteq> z" "dist w z < fr" for w
  3416   proof -
  3417     have "f w = fp w * (w - z) powr of_int fn" "fp w \<noteq> 0"
  3418       using fr(2)[rule_format, of w] using that by (auto simp add:dist_commute)
  3419     moreover have "(w - z) powr of_int fn \<noteq>0"
  3420       unfolding powr_eq_0_iff using \<open>w\<noteq>z\<close> by auto
  3421     ultimately show ?thesis by auto
  3422   qed
  3423   then show ?thesis using \<open>fr>0\<close> unfolding eventually_at by auto
  3424 qed
  3425 
  3426 lemma non_zero_neighbour_pole:
  3427   assumes "is_pole f z"
  3428   shows "\<forall>\<^sub>F w in (at z). f w\<noteq>0"
  3429   using assms filterlim_at_infinity_imp_eventually_ne[of f "at z" 0]  
  3430   unfolding is_pole_def by auto
  3431 
  3432 lemma non_zero_neighbour_alt:
  3433   assumes holo: "f holomorphic_on S"
  3434       and "open S" "connected S" "z \<in> S"  "\<beta> \<in> S" "f \<beta> \<noteq> 0"
  3435     shows "\<forall>\<^sub>F w in (at z). f w\<noteq>0 \<and> w\<in>S"
  3436 proof (cases "f z = 0")
  3437   case True
  3438   from isolated_zeros[OF holo \<open>open S\<close> \<open>connected S\<close> \<open>z \<in> S\<close> True \<open>\<beta> \<in> S\<close> \<open>f \<beta> \<noteq> 0\<close>] 
  3439   obtain r where "0 < r" "ball z r \<subseteq> S" "\<forall>w \<in> ball z r - {z}.f w \<noteq> 0" by metis 
  3440   then show ?thesis unfolding eventually_at 
  3441     apply (rule_tac x=r in exI)
  3442     by (auto simp add:dist_commute)
  3443 next
  3444   case False
  3445   obtain r1 where r1:"r1>0" "\<forall>y. dist z y < r1 \<longrightarrow> f y \<noteq> 0"
  3446     using continuous_at_avoid[of z f, OF _ False] assms(2,4) continuous_on_eq_continuous_at 
  3447       holo holomorphic_on_imp_continuous_on by blast
  3448   obtain r2 where r2:"r2>0" "ball z r2 \<subseteq> S" 
  3449     using assms(2) assms(4) openE by blast
  3450   show ?thesis unfolding eventually_at 
  3451     apply (rule_tac x="min r1 r2" in exI)
  3452     using r1 r2 by (auto simp add:dist_commute)
  3453 qed
  3454 
  3455 lemma not_essential_times[singularity_intros]:
  3456   assumes f_ness:"not_essential f z" and g_ness:"not_essential g z"
  3457   assumes f_iso:"isolated_singularity_at f z" and g_iso:"isolated_singularity_at g z"
  3458   shows "not_essential (\<lambda>w. f w * g w) z"
  3459 proof -
  3460   define fg where "fg = (\<lambda>w. f w * g w)"
  3461   have ?thesis when "\<not> ((\<exists>\<^sub>Fw in (at z). f w\<noteq>0) \<and> (\<exists>\<^sub>Fw in (at z). g w\<noteq>0))"
  3462   proof -
  3463     have "\<forall>\<^sub>Fw in (at z). fg w=0" 
  3464       using that[unfolded frequently_def, simplified] unfolding fg_def
  3465       by (auto elim: eventually_rev_mp)
  3466     from tendsto_cong[OF this] have "fg \<midarrow>z\<rightarrow>0" by auto
  3467     then show ?thesis unfolding not_essential_def fg_def by auto
  3468   qed
  3469   moreover have ?thesis when f_nconst:"\<exists>\<^sub>Fw in (at z). f w\<noteq>0" and g_nconst:"\<exists>\<^sub>Fw in (at z). g w\<noteq>0"
  3470   proof -
  3471     obtain fn fp fr where [simp]:"fp z \<noteq> 0" and "fr > 0"
  3472           and fr: "fp holomorphic_on cball z fr" 
  3473                   "\<forall>w\<in>cball z fr - {z}. f w = fp w * (w - z) powr of_int fn \<and> fp w \<noteq> 0"
  3474       using holomorphic_factor_puncture[OF f_iso f_ness f_nconst,THEN ex1_implies_ex] by auto
  3475     obtain gn gp gr where [simp]:"gp z \<noteq> 0" and "gr > 0"
  3476           and gr: "gp holomorphic_on cball z gr" 
  3477                   "\<forall>w\<in>cball z gr - {z}. g w = gp w * (w - z) powr of_int gn \<and> gp w \<noteq> 0"
  3478       using holomorphic_factor_puncture[OF g_iso g_ness g_nconst,THEN ex1_implies_ex] by auto
  3479   
  3480     define r1 where "r1=(min fr gr)"
  3481     have "r1>0" unfolding r1_def using  \<open>fr>0\<close> \<open>gr>0\<close> by auto
  3482     have fg_times:"fg w = (fp w * gp w) * (w - z) powr (of_int (fn+gn))" and fgp_nz:"fp w*gp w\<noteq>0"
  3483       when "w\<in>ball z r1 - {z}" for w
  3484     proof -
  3485       have "f w = fp w * (w - z) powr of_int fn" "fp w\<noteq>0"
  3486         using fr(2)[rule_format,of w] that unfolding r1_def by auto
  3487       moreover have "g w = gp w * (w - z) powr of_int gn" "gp w \<noteq> 0"
  3488         using gr(2)[rule_format, of w] that unfolding r1_def by auto
  3489       ultimately show "fg w = (fp w * gp w) * (w - z) powr (of_int (fn+gn))" "fp w*gp w\<noteq>0"
  3490         unfolding fg_def by (auto simp add:powr_add)
  3491     qed
  3492 
  3493     have [intro]: "fp \<midarrow>z\<rightarrow>fp z" "gp \<midarrow>z\<rightarrow>gp z"
  3494         using fr(1) \<open>fr>0\<close> gr(1) \<open>gr>0\<close>
  3495         by (meson open_ball ball_subset_cball centre_in_ball 
  3496             continuous_on_eq_continuous_at continuous_within holomorphic_on_imp_continuous_on 
  3497             holomorphic_on_subset)+
  3498     have ?thesis when "fn+gn>0" 
  3499     proof -
  3500       have "(\<lambda>w. (fp w * gp w) * (w - z) ^ (nat (fn+gn))) \<midarrow>z\<rightarrow>0" 
  3501         using that by (auto intro!:tendsto_eq_intros)
  3502       then have "fg \<midarrow>z\<rightarrow> 0"
  3503         apply (elim Lim_transform_within[OF _ \<open>r1>0\<close>])
  3504         by (metis (no_types, hide_lams) Diff_iff cball_trivial dist_commute dist_self 
  3505               eq_iff_diff_eq_0 fg_times less_le linorder_not_le mem_ball mem_cball powr_of_int 
  3506               that)
  3507       then show ?thesis unfolding not_essential_def fg_def by auto
  3508     qed
  3509     moreover have ?thesis when "fn+gn=0" 
  3510     proof -
  3511       have "(\<lambda>w. fp w * gp w) \<midarrow>z\<rightarrow>fp z*gp z" 
  3512         using that by (auto intro!:tendsto_eq_intros)
  3513       then have "fg \<midarrow>z\<rightarrow> fp z*gp z"
  3514         apply (elim Lim_transform_within[OF _ \<open>r1>0\<close>])
  3515         apply (subst fg_times)
  3516         by (auto simp add:dist_commute that)
  3517       then show ?thesis unfolding not_essential_def fg_def by auto
  3518     qed
  3519     moreover have ?thesis when "fn+gn<0" 
  3520     proof -
  3521       have "LIM w (at z). fp w * gp w / (w-z)^nat (-(fn+gn)) :> at_infinity"
  3522         apply (rule filterlim_divide_at_infinity)
  3523         apply (insert that, auto intro!:tendsto_eq_intros filterlim_atI)
  3524         using eventually_at_topological by blast
  3525       then have "is_pole fg z" unfolding is_pole_def
  3526         apply (elim filterlim_transform_within[OF _ _ \<open>r1>0\<close>],simp)
  3527         apply (subst fg_times,simp add:dist_commute)
  3528         apply (subst powr_of_int)
  3529         using that by (auto simp add:divide_simps)
  3530       then show ?thesis unfolding not_essential_def fg_def by auto
  3531     qed
  3532     ultimately show ?thesis unfolding not_essential_def fg_def by fastforce
  3533   qed
  3534   ultimately show ?thesis by auto
  3535 qed
  3536 
  3537 lemma not_essential_inverse[singularity_intros]:
  3538   assumes f_ness:"not_essential f z"
  3539   assumes f_iso:"isolated_singularity_at f z"
  3540   shows "not_essential (\<lambda>w. inverse (f w)) z"
  3541 proof -
  3542   define vf where "vf = (\<lambda>w. inverse (f w))"
  3543   have ?thesis when "\<not>(\<exists>\<^sub>Fw in (at z). f w\<noteq>0)"
  3544   proof -
  3545     have "\<forall>\<^sub>Fw in (at z). f w=0" 
  3546       using that[unfolded frequently_def, simplified] by (auto elim: eventually_rev_mp)
  3547     then have "\<forall>\<^sub>Fw in (at z). vf w=0"
  3548       unfolding vf_def by auto
  3549     from tendsto_cong[OF this] have "vf \<midarrow>z\<rightarrow>0" unfolding vf_def by auto
  3550     then show ?thesis unfolding not_essential_def vf_def by auto
  3551   qed
  3552   moreover have ?thesis when "is_pole f z"
  3553   proof -
  3554     have "vf \<midarrow>z\<rightarrow>0"
  3555       using that filterlim_at filterlim_inverse_at_iff unfolding is_pole_def vf_def by blast
  3556     then show ?thesis unfolding not_essential_def vf_def by auto
  3557   qed
  3558   moreover have ?thesis when "\<exists>x. f\<midarrow>z\<rightarrow>x " and f_nconst:"\<exists>\<^sub>Fw in (at z). f w\<noteq>0"
  3559   proof -
  3560     from that obtain fz where fz:"f\<midarrow>z\<rightarrow>fz" by auto
  3561     have ?thesis when "fz=0"
  3562     proof -
  3563       have "(\<lambda>w. inverse (vf w)) \<midarrow>z\<rightarrow>0"
  3564         using fz that unfolding vf_def by auto
  3565       moreover have "\<forall>\<^sub>F w in at z. inverse (vf w) \<noteq> 0"
  3566         using non_zero_neighbour[OF f_iso f_ness f_nconst]
  3567         unfolding vf_def by auto
  3568       ultimately have "is_pole vf z"
  3569         using filterlim_inverse_at_iff[of vf "at z"] unfolding filterlim_at is_pole_def by auto
  3570       then show ?thesis unfolding not_essential_def vf_def by auto
  3571     qed
  3572     moreover have ?thesis when "fz\<noteq>0"
  3573     proof -
  3574       have "vf \<midarrow>z\<rightarrow>inverse fz"
  3575         using fz that unfolding vf_def by (auto intro:tendsto_eq_intros)
  3576       then show ?thesis unfolding not_essential_def vf_def by auto
  3577     qed
  3578     ultimately show ?thesis by auto
  3579   qed
  3580   ultimately show ?thesis using f_ness unfolding not_essential_def by auto
  3581 qed
  3582 
  3583 lemma isolated_singularity_at_inverse[singularity_intros]:
  3584   assumes f_iso:"isolated_singularity_at f z"
  3585       and f_ness:"not_essential f z"
  3586   shows "isolated_singularity_at (\<lambda>w. inverse (f w)) z"
  3587 proof -
  3588   define vf where "vf = (\<lambda>w. inverse (f w))"
  3589   have ?thesis when "\<not>(\<exists>\<^sub>Fw in (at z). f w\<noteq>0)"
  3590   proof -
  3591     have "\<forall>\<^sub>Fw in (at z). f w=0" 
  3592       using that[unfolded frequently_def, simplified] by (auto elim: eventually_rev_mp)
  3593     then have "\<forall>\<^sub>Fw in (at z). vf w=0"
  3594       unfolding vf_def by auto
  3595     then obtain d1 where "d1>0" and d1:"\<forall>x. x \<noteq> z \<and> dist x z < d1 \<longrightarrow> vf x = 0"
  3596       unfolding eventually_at by auto
  3597     then have "vf holomorphic_on ball z d1-{z}"
  3598       apply (rule_tac holomorphic_transform[of "\<lambda>_. 0"])
  3599       by (auto simp add:dist_commute)
  3600     then have "vf analytic_on ball z d1 - {z}"
  3601       by (simp add: analytic_on_open open_delete)
  3602     then show ?thesis using \<open>d1>0\<close> unfolding isolated_singularity_at_def vf_def by auto
  3603   qed
  3604   moreover have ?thesis when f_nconst:"\<exists>\<^sub>Fw in (at z). f w\<noteq>0"
  3605   proof -
  3606     have "\<forall>\<^sub>F w in at z. f w \<noteq> 0" using non_zero_neighbour[OF f_iso f_ness f_nconst] .
  3607     then obtain d1 where d1:"d1>0" "\<forall>x. x \<noteq> z \<and> dist x z < d1 \<longrightarrow> f x \<noteq> 0"
  3608       unfolding eventually_at by auto
  3609     obtain d2 where "d2>0" and d2:"f analytic_on ball z d2 - {z}"
  3610       using f_iso unfolding isolated_singularity_at_def by auto
  3611     define d3 where "d3=min d1 d2"
  3612     have "d3>0" unfolding d3_def using \<open>d1>0\<close> \<open>d2>0\<close> by auto
  3613     moreover have "vf analytic_on ball z d3 - {z}"
  3614       unfolding vf_def
  3615       apply (rule analytic_on_inverse)
  3616       subgoal using d2 unfolding d3_def by (elim analytic_on_subset) auto
  3617       subgoal for w using d1 unfolding d3_def by (auto simp add:dist_commute)
  3618       done
  3619     ultimately show ?thesis unfolding isolated_singularity_at_def vf_def by auto
  3620   qed
  3621   ultimately show ?thesis by auto
  3622 qed
  3623 
  3624 lemma not_essential_divide[singularity_intros]:
  3625   assumes f_ness:"not_essential f z" and g_ness:"not_essential g z"
  3626   assumes f_iso:"isolated_singularity_at f z" and g_iso:"isolated_singularity_at g z"
  3627   shows "not_essential (\<lambda>w. f w / g w) z"
  3628 proof -
  3629   have "not_essential (\<lambda>w. f w * inverse (g w)) z"
  3630     apply (rule not_essential_times[where g="\<lambda>w. inverse (g w)"])
  3631     using assms by (auto intro: isolated_singularity_at_inverse not_essential_inverse)
  3632   then show ?thesis by (simp add:field_simps)
  3633 qed
  3634 
  3635 lemma 
  3636   assumes f_iso:"isolated_singularity_at f z"
  3637       and g_iso:"isolated_singularity_at g z"
  3638     shows isolated_singularity_at_times[singularity_intros]:
  3639               "isolated_singularity_at (\<lambda>w. f w * g w) z" and
  3640           isolated_singularity_at_add[singularity_intros]:
  3641               "isolated_singularity_at (\<lambda>w. f w + g w) z"
  3642 proof -
  3643   obtain d1 d2 where "d1>0" "d2>0" 
  3644       and d1:"f analytic_on ball z d1 - {z}" and d2:"g analytic_on ball z d2 - {z}"
  3645     using f_iso g_iso unfolding isolated_singularity_at_def by auto
  3646   define d3 where "d3=min d1 d2"
  3647   have "d3>0" unfolding d3_def using \<open>d1>0\<close> \<open>d2>0\<close> by auto
  3648   
  3649   have "(\<lambda>w. f w * g w) analytic_on ball z d3 - {z}"
  3650     apply (rule analytic_on_mult)
  3651     using d1 d2 unfolding d3_def by (auto elim:analytic_on_subset)
  3652   then show "isolated_singularity_at (\<lambda>w. f w * g w) z" 
  3653     using \<open>d3>0\<close> unfolding isolated_singularity_at_def by auto
  3654   have "(\<lambda>w. f w + g w) analytic_on ball z d3 - {z}"
  3655     apply (rule analytic_on_add)
  3656     using d1 d2 unfolding d3_def by (auto elim:analytic_on_subset)
  3657   then show "isolated_singularity_at (\<lambda>w. f w + g w) z" 
  3658     using \<open>d3>0\<close> unfolding isolated_singularity_at_def by auto
  3659 qed
  3660 
  3661 lemma isolated_singularity_at_uminus[singularity_intros]:
  3662   assumes f_iso:"isolated_singularity_at f z"
  3663   shows "isolated_singularity_at (\<lambda>w. - f w) z"
  3664   using assms unfolding isolated_singularity_at_def using analytic_on_neg by blast
  3665 
  3666 lemma isolated_singularity_at_id[singularity_intros]:
  3667      "isolated_singularity_at (\<lambda>w. w) z"
  3668   unfolding isolated_singularity_at_def by (simp add: gt_ex)
  3669 
  3670 lemma isolated_singularity_at_minus[singularity_intros]:
  3671   assumes f_iso:"isolated_singularity_at f z"
  3672       and g_iso:"isolated_singularity_at g z"
  3673     shows "isolated_singularity_at (\<lambda>w. f w - g w) z"
  3674   using isolated_singularity_at_uminus[THEN isolated_singularity_at_add[OF f_iso,of "\<lambda>w. - g w"]
  3675         ,OF g_iso] by simp
  3676 
  3677 lemma isolated_singularity_at_divide[singularity_intros]:
  3678   assumes f_iso:"isolated_singularity_at f z"
  3679       and g_iso:"isolated_singularity_at g z"
  3680       and g_ness:"not_essential g z"
  3681     shows "isolated_singularity_at (\<lambda>w. f w / g w) z"
  3682   using isolated_singularity_at_inverse[THEN isolated_singularity_at_times[OF f_iso,
  3683           of "\<lambda>w. inverse (g w)"],OF g_iso g_ness] by (simp add:field_simps)
  3684 
  3685 lemma isolated_singularity_at_const[singularity_intros]:
  3686     "isolated_singularity_at (\<lambda>w. c) z"
  3687   unfolding isolated_singularity_at_def by (simp add: gt_ex)
  3688 
  3689 lemma isolated_singularity_at_holomorphic:
  3690   assumes "f holomorphic_on s-{z}" "open s" "z\<in>s"
  3691   shows "isolated_singularity_at f z"
  3692   using assms unfolding isolated_singularity_at_def 
  3693   by (metis analytic_on_holomorphic centre_in_ball insert_Diff openE open_delete subset_insert_iff)
  3694 
  3695 subsubsection \<open>The order of non-essential singularities (i.e. removable singularities or poles)\<close>
  3696 
  3697 
  3698 definition%important zorder :: "(complex \<Rightarrow> complex) \<Rightarrow> complex \<Rightarrow> int" where
  3699   "zorder f z = (THE n. (\<exists>h r. r>0 \<and> h holomorphic_on cball z r \<and> h z\<noteq>0
  3700                    \<and> (\<forall>w\<in>cball z r - {z}. f w =  h w * (w-z) powr (of_int n)
  3701                    \<and> h w \<noteq>0)))"
  3702 
  3703 definition%important zor_poly
  3704     ::"[complex \<Rightarrow> complex, complex] \<Rightarrow> complex \<Rightarrow> complex" where
  3705   "zor_poly f z = (SOME h. \<exists>r. r > 0 \<and> h holomorphic_on cball z r \<and> h z \<noteq> 0
  3706                    \<and> (\<forall>w\<in>cball z r - {z}. f w =  h w * (w - z) powr (zorder f z)
  3707                    \<and> h w \<noteq>0))"
  3708 
  3709 lemma zorder_exist:
  3710   fixes f::"complex \<Rightarrow> complex" and z::complex
  3711   defines "n\<equiv>zorder f z" and "g\<equiv>zor_poly f z"
  3712   assumes f_iso:"isolated_singularity_at f z" 
  3713       and f_ness:"not_essential f z" 
  3714       and f_nconst:"\<exists>\<^sub>Fw in (at z). f w\<noteq>0"
  3715   shows "g z\<noteq>0 \<and> (\<exists>r. r>0 \<and> g holomorphic_on cball z r
  3716     \<and> (\<forall>w\<in>cball z r - {z}. f w  = g w * (w-z) powr n  \<and> g w \<noteq>0))"
  3717 proof -
  3718   define P where "P = (\<lambda>n g r. 0 < r \<and> g holomorphic_on cball z r \<and> g z\<noteq>0
  3719           \<and> (\<forall>w\<in>cball z r - {z}. f w = g w * (w-z) powr (of_int n) \<and> g w\<noteq>0))"
  3720   have "\<exists>!n. \<exists>g r. P n g r" 
  3721     using holomorphic_factor_puncture[OF assms(3-)] unfolding P_def by auto
  3722   then have "\<exists>g r. P n g r"
  3723     unfolding n_def P_def zorder_def
  3724     by (drule_tac theI',argo)
  3725   then have "\<exists>r. P n g r"
  3726     unfolding P_def zor_poly_def g_def n_def
  3727     by (drule_tac someI_ex,argo)
  3728   then obtain r1 where "P n g r1" by auto
  3729   then show ?thesis unfolding P_def by auto
  3730 qed
  3731 
  3732 lemma 
  3733   fixes f::"complex \<Rightarrow> complex" and z::complex
  3734   assumes f_iso:"isolated_singularity_at f z" 
  3735       and f_ness:"not_essential f z"  
  3736       and f_nconst:"\<exists>\<^sub>Fw in (at z). f w\<noteq>0"
  3737     shows zorder_inverse: "zorder (\<lambda>w. inverse (f w)) z = - zorder f z"
  3738       and zor_poly_inverse: "\<forall>\<^sub>Fw in (at z). zor_poly (\<lambda>w. inverse (f w)) z w 
  3739                                                 = inverse (zor_poly f z w)"
  3740 proof -
  3741   define vf where "vf = (\<lambda>w. inverse (f w))"
  3742   define fn vfn where 
  3743     "fn = zorder f z"  and "vfn = zorder vf z"
  3744   define fp vfp where 
  3745     "fp = zor_poly f z" and "vfp = zor_poly vf z"
  3746 
  3747   obtain fr where [simp]:"fp z \<noteq> 0" and "fr > 0"
  3748           and fr: "fp holomorphic_on cball z fr" 
  3749                   "\<forall>w\<in>cball z fr - {z}. f w = fp w * (w - z) powr of_int fn \<and> fp w \<noteq> 0"
  3750     using zorder_exist[OF f_iso f_ness f_nconst,folded fn_def fp_def]
  3751     by auto
  3752   have fr_inverse: "vf w = (inverse (fp w)) * (w - z) powr (of_int (-fn))" 
  3753         and fr_nz: "inverse (fp w)\<noteq>0"
  3754     when "w\<in>ball z fr - {z}" for w
  3755   proof -
  3756     have "f w = fp w * (w - z) powr of_int fn" "fp w\<noteq>0"
  3757       using fr(2)[rule_format,of w] that by auto
  3758     then show "vf w = (inverse (fp w)) * (w - z) powr (of_int (-fn))" "inverse (fp w)\<noteq>0"
  3759       unfolding vf_def by (auto simp add:powr_minus)
  3760   qed
  3761   obtain vfr where [simp]:"vfp z \<noteq> 0" and "vfr>0" and vfr:"vfp holomorphic_on cball z vfr" 
  3762       "(\<forall>w\<in>cball z vfr - {z}. vf w = vfp w * (w - z) powr of_int vfn \<and> vfp w \<noteq> 0)"
  3763   proof -
  3764     have "isolated_singularity_at vf z" 
  3765       using isolated_singularity_at_inverse[OF f_iso f_ness] unfolding vf_def . 
  3766     moreover have "not_essential vf z" 
  3767       using not_essential_inverse[OF f_ness f_iso] unfolding vf_def .
  3768     moreover have "\<exists>\<^sub>F w in at z. vf w \<noteq> 0" 
  3769       using f_nconst unfolding vf_def by (auto elim:frequently_elim1)
  3770     ultimately show ?thesis using zorder_exist[of vf z, folded vfn_def vfp_def] that by auto
  3771   qed
  3772 
  3773 
  3774   define r1 where "r1 = min fr vfr"
  3775   have "r1>0" using \<open>fr>0\<close> \<open>vfr>0\<close> unfolding r1_def by simp
  3776   show "vfn = - fn"
  3777     apply (rule holomorphic_factor_unique[of r1 vfp z "\<lambda>w. inverse (fp w)" vf])
  3778     subgoal using \<open>r1>0\<close> by simp
  3779     subgoal by simp
  3780     subgoal by simp
  3781     subgoal
  3782     proof (rule ballI)
  3783       fix w assume "w \<in> ball z r1 - {z}"
  3784       then have "w \<in> ball z fr - {z}" "w \<in> cball z vfr - {z}"  unfolding r1_def by auto
  3785       from fr_inverse[OF this(1)] fr_nz[OF this(1)] vfr(2)[rule_format,OF this(2)] 
  3786       show "vf w = vfp w * (w - z) powr of_int vfn \<and> vfp w \<noteq> 0 
  3787               \<and> vf w = inverse (fp w) * (w - z) powr of_int (- fn) \<and> inverse (fp w) \<noteq> 0" by auto
  3788     qed
  3789     subgoal using vfr(1) unfolding r1_def by (auto intro!:holomorphic_intros) 
  3790     subgoal using fr unfolding r1_def by (auto intro!:holomorphic_intros)
  3791     done
  3792 
  3793   have "vfp w = inverse (fp w)" when "w\<in>ball z r1-{z}" for w
  3794   proof -
  3795     have "w \<in> ball z fr - {z}" "w \<in> cball z vfr - {z}"  "w\<noteq>z" using that unfolding r1_def by auto
  3796     from fr_inverse[OF this(1)] fr_nz[OF this(1)] vfr(2)[rule_format,OF this(2)] \<open>vfn = - fn\<close> \<open>w\<noteq>z\<close>
  3797     show ?thesis by auto
  3798   qed
  3799   then show "\<forall>\<^sub>Fw in (at z). vfp w = inverse (fp w)"
  3800     unfolding eventually_at using \<open>r1>0\<close>
  3801     apply (rule_tac x=r1 in exI)
  3802     by (auto simp add:dist_commute)
  3803 qed
  3804 
  3805 lemma 
  3806   fixes f g::"complex \<Rightarrow> complex" and z::complex
  3807   assumes f_iso:"isolated_singularity_at f z" and g_iso:"isolated_singularity_at g z"  
  3808       and f_ness:"not_essential f z" and g_ness:"not_essential g z" 
  3809       and fg_nconst: "\<exists>\<^sub>Fw in (at z). f w * g w\<noteq> 0"
  3810   shows zorder_times:"zorder (\<lambda>w. f w * g w) z = zorder f z + zorder g z" and
  3811         zor_poly_times:"\<forall>\<^sub>Fw in (at z). zor_poly (\<lambda>w. f w * g w) z w 
  3812                                                   = zor_poly f z w *zor_poly g z w"
  3813 proof -
  3814   define fg where "fg = (\<lambda>w. f w * g w)"
  3815   define fn gn fgn where 
  3816     "fn = zorder f z" and "gn = zorder g z" and "fgn = zorder fg z"
  3817   define fp gp fgp where 
  3818     "fp = zor_poly f z" and "gp = zor_poly g z" and "fgp = zor_poly fg z"
  3819   have f_nconst:"\<exists>\<^sub>Fw in (at z). f w \<noteq> 0" and g_nconst:"\<exists>\<^sub>Fw in (at z).g w\<noteq> 0"
  3820     using fg_nconst by (auto elim!:frequently_elim1)
  3821   obtain fr where [simp]:"fp z \<noteq> 0" and "fr > 0" 
  3822           and fr: "fp holomorphic_on cball z fr" 
  3823                   "\<forall>w\<in>cball z fr - {z}. f w = fp w * (w - z) powr of_int fn \<and> fp w \<noteq> 0"
  3824     using zorder_exist[OF f_iso f_ness f_nconst,folded fp_def fn_def] by auto
  3825   obtain gr where [simp]:"gp z \<noteq> 0" and "gr > 0"  
  3826           and gr: "gp holomorphic_on cball z gr" 
  3827                   "\<forall>w\<in>cball z gr - {z}. g w = gp w * (w - z) powr of_int gn \<and> gp w \<noteq> 0"
  3828     using zorder_exist[OF g_iso g_ness g_nconst,folded gn_def gp_def] by auto
  3829   define r1 where "r1=min fr gr"
  3830   have "r1>0" unfolding r1_def using \<open>fr>0\<close> \<open>gr>0\<close> by auto
  3831   have fg_times:"fg w = (fp w * gp w) * (w - z) powr (of_int (fn+gn))" and fgp_nz:"fp w*gp w\<noteq>0"
  3832     when "w\<in>ball z r1 - {z}" for w
  3833   proof -
  3834     have "f w = fp w * (w - z) powr of_int fn" "fp w\<noteq>0"
  3835       using fr(2)[rule_format,of w] that unfolding r1_def by auto
  3836     moreover have "g w = gp w * (w - z) powr of_int gn" "gp w \<noteq> 0"
  3837       using gr(2)[rule_format, of w] that unfolding r1_def by auto
  3838     ultimately show "fg w = (fp w * gp w) * (w - z) powr (of_int (fn+gn))" "fp w*gp w\<noteq>0"
  3839       unfolding fg_def by (auto simp add:powr_add)
  3840   qed
  3841 
  3842   obtain fgr where [simp]:"fgp z \<noteq> 0" and "fgr > 0"
  3843           and fgr: "fgp holomorphic_on cball z fgr" 
  3844                   "\<forall>w\<in>cball z fgr - {z}. fg w = fgp w * (w - z) powr of_int fgn \<and> fgp w \<noteq> 0"
  3845   proof -
  3846     have "fgp z \<noteq> 0 \<and> (\<exists>r>0. fgp holomorphic_on cball z r 
  3847             \<and> (\<forall>w\<in>cball z r - {z}. fg w = fgp w * (w - z) powr of_int fgn \<and> fgp w \<noteq> 0))"
  3848       apply (rule zorder_exist[of fg z, folded fgn_def fgp_def])
  3849       subgoal unfolding fg_def using isolated_singularity_at_times[OF f_iso g_iso] .
  3850       subgoal unfolding fg_def using not_essential_times[OF f_ness g_ness f_iso g_iso] .
  3851       subgoal unfolding fg_def using fg_nconst .
  3852       done
  3853     then show ?thesis using that by blast
  3854   qed
  3855   define r2 where "r2 = min fgr r1"
  3856   have "r2>0" using \<open>r1>0\<close> \<open>fgr>0\<close> unfolding r2_def by simp
  3857   show "fgn = fn + gn "
  3858     apply (rule holomorphic_factor_unique[of r2 fgp z "\<lambda>w. fp w * gp w" fg])
  3859     subgoal using \<open>r2>0\<close> by simp
  3860     subgoal by simp
  3861     subgoal by simp
  3862     subgoal
  3863     proof (rule ballI)
  3864       fix w assume "w \<in> ball z r2 - {z}"
  3865       then have "w \<in> ball z r1 - {z}" "w \<in> cball z fgr - {z}"  unfolding r2_def by auto
  3866       from fg_times[OF this(1)] fgp_nz[OF this(1)] fgr(2)[rule_format,OF this(2)] 
  3867       show "fg w = fgp w * (w - z) powr of_int fgn \<and> fgp w \<noteq> 0 
  3868               \<and> fg w = fp w * gp w * (w - z) powr of_int (fn + gn) \<and> fp w * gp w \<noteq> 0" by auto
  3869     qed
  3870     subgoal using fgr(1) unfolding r2_def r1_def by (auto intro!:holomorphic_intros) 
  3871     subgoal using fr(1) gr(1) unfolding r2_def r1_def by (auto intro!:holomorphic_intros)
  3872     done
  3873 
  3874   have "fgp w = fp w *gp w" when "w\<in>ball z r2-{z}" for w
  3875   proof -
  3876     have "w \<in> ball z r1 - {z}" "w \<in> cball z fgr - {z}" "w\<noteq>z" using that  unfolding r2_def by auto
  3877     from fg_times[OF this(1)] fgp_nz[OF this(1)] fgr(2)[rule_format,OF this(2)] \<open>fgn = fn + gn\<close> \<open>w\<noteq>z\<close>
  3878     show ?thesis by auto
  3879   qed
  3880   then show "\<forall>\<^sub>Fw in (at z). fgp w = fp w * gp w" 
  3881     using \<open>r2>0\<close> unfolding eventually_at by (auto simp add:dist_commute)
  3882 qed
  3883 
  3884 lemma 
  3885   fixes f g::"complex \<Rightarrow> complex" and z::complex
  3886   assumes f_iso:"isolated_singularity_at f z" and g_iso:"isolated_singularity_at g z"  
  3887       and f_ness:"not_essential f z" and g_ness:"not_essential g z" 
  3888       and fg_nconst: "\<exists>\<^sub>Fw in (at z). f w * g w\<noteq> 0"
  3889   shows zorder_divide:"zorder (\<lambda>w. f w / g w) z = zorder f z - zorder g z" and
  3890         zor_poly_divide:"\<forall>\<^sub>Fw in (at z). zor_poly (\<lambda>w. f w / g w) z w 
  3891                                                   = zor_poly f z w  / zor_poly g z w"
  3892 proof -
  3893   have f_nconst:"\<exists>\<^sub>Fw in (at z). f w \<noteq> 0" and g_nconst:"\<exists>\<^sub>Fw in (at z).g w\<noteq> 0"
  3894     using fg_nconst by (auto elim!:frequently_elim1)
  3895   define vg where "vg=(\<lambda>w. inverse (g w))"
  3896   have "zorder (\<lambda>w. f w * vg w) z = zorder f z + zorder vg z"
  3897     apply (rule zorder_times[OF f_iso _ f_ness,of vg])
  3898     subgoal unfolding vg_def using isolated_singularity_at_inverse[OF g_iso g_ness] .
  3899     subgoal unfolding vg_def using not_essential_inverse[OF g_ness g_iso] .
  3900     subgoal unfolding vg_def using fg_nconst by (auto elim!:frequently_elim1)
  3901     done
  3902   then show "zorder (\<lambda>w. f w / g w) z = zorder f z - zorder g z"
  3903     using zorder_inverse[OF g_iso g_ness g_nconst,folded vg_def] unfolding vg_def 
  3904     by (auto simp add:field_simps)
  3905 
  3906   have "\<forall>\<^sub>F w in at z. zor_poly (\<lambda>w. f w * vg w) z w = zor_poly f z w * zor_poly vg z w"
  3907     apply (rule zor_poly_times[OF f_iso _ f_ness,of vg])
  3908     subgoal unfolding vg_def using isolated_singularity_at_inverse[OF g_iso g_ness] .
  3909     subgoal unfolding vg_def using not_essential_inverse[OF g_ness g_iso] .
  3910     subgoal unfolding vg_def using fg_nconst by (auto elim!:frequently_elim1)
  3911     done
  3912   then show "\<forall>\<^sub>Fw in (at z). zor_poly (\<lambda>w. f w / g w) z w = zor_poly f z w  / zor_poly g z w"
  3913     using zor_poly_inverse[OF g_iso g_ness g_nconst,folded vg_def] unfolding vg_def
  3914     apply eventually_elim
  3915     by (auto simp add:field_simps)
  3916 qed
  3917 
  3918 lemma zorder_exist_zero:
  3919   fixes f::"complex \<Rightarrow> complex" and z::complex
  3920   defines "n\<equiv>zorder f z" and "g\<equiv>zor_poly f z"
  3921   assumes  holo: "f holomorphic_on s" and 
  3922           "open s" "connected s" "z\<in>s"
  3923       and non_const: "\<exists>w\<in>s. f w \<noteq> 0"
  3924   shows "(if f z=0 then n > 0 else n=0) \<and> (\<exists>r. r>0 \<and> cball z r \<subseteq> s \<and> g holomorphic_on cball z r
  3925     \<and> (\<forall>w\<in>cball z r. f w  = g w * (w-z) ^ nat n  \<and> g w \<noteq>0))"
  3926 proof -
  3927   obtain r where "g z \<noteq> 0" and r: "r>0" "cball z r \<subseteq> s" "g holomorphic_on cball z r" 
  3928             "(\<forall>w\<in>cball z r - {z}. f w = g w * (w - z) powr of_int n \<and> g w \<noteq> 0)"
  3929   proof -
  3930     have "g z \<noteq> 0 \<and> (\<exists>r>0. g holomorphic_on cball z r 
  3931             \<and> (\<forall>w\<in>cball z r - {z}. f w = g w * (w - z) powr of_int n \<and> g w \<noteq> 0))"
  3932     proof (rule zorder_exist[of f z,folded g_def n_def])
  3933       show "isolated_singularity_at f z" unfolding isolated_singularity_at_def
  3934         using holo assms(4,6)
  3935         by (meson Diff_subset open_ball analytic_on_holomorphic holomorphic_on_subset openE)
  3936       show "not_essential f z" unfolding not_essential_def 
  3937         using assms(4,6) at_within_open continuous_on holo holomorphic_on_imp_continuous_on 
  3938         by fastforce
  3939       have "\<forall>\<^sub>F w in at z. f w \<noteq> 0 \<and> w\<in>s"
  3940       proof -
  3941         obtain w where "w\<in>s" "f w\<noteq>0" using non_const by auto
  3942         then show ?thesis 
  3943           by (rule non_zero_neighbour_alt[OF holo \<open>open s\<close> \<open>connected s\<close> \<open>z\<in>s\<close>])
  3944       qed
  3945       then show "\<exists>\<^sub>F w in at z. f w \<noteq> 0"
  3946         apply (elim eventually_frequentlyE)
  3947         by auto
  3948     qed
  3949     then obtain r1 where "g z \<noteq> 0" "r1>0" and r1:"g holomorphic_on cball z r1"
  3950             "(\<forall>w\<in>cball z r1 - {z}. f w = g w * (w - z) powr of_int n \<and> g w \<noteq> 0)"
  3951       by auto
  3952     obtain r2 where r2: "r2>0" "cball z r2 \<subseteq> s" 
  3953       using assms(4,6) open_contains_cball_eq by blast
  3954     define r3 where "r3=min r1 r2"
  3955     have "r3>0" "cball z r3 \<subseteq> s" using \<open>r1>0\<close> r2 unfolding r3_def by auto
  3956     moreover have "g holomorphic_on cball z r3" 
  3957       using r1(1) unfolding r3_def by auto
  3958     moreover have "(\<forall>w\<in>cball z r3 - {z}. f w = g w * (w - z) powr of_int n \<and> g w \<noteq> 0)" 
  3959       using r1(2) unfolding r3_def by auto
  3960     ultimately show ?thesis using that[of r3] \<open>g z\<noteq>0\<close> by auto 
  3961   qed
  3962 
  3963   have if_0:"if f z=0 then n > 0 else n=0" 
  3964   proof -
  3965     have "f\<midarrow> z \<rightarrow> f z"
  3966       by (metis assms(4,6,7) at_within_open continuous_on holo holomorphic_on_imp_continuous_on)
  3967     then have "(\<lambda>w. g w * (w - z) powr of_int n) \<midarrow>z\<rightarrow> f z"
  3968       apply (elim Lim_transform_within_open[where s="ball z r"])
  3969       using r by auto
  3970     moreover have "g \<midarrow>z\<rightarrow>g z"
  3971       by (metis (mono_tags, lifting) open_ball at_within_open_subset 
  3972           ball_subset_cball centre_in_ball continuous_on holomorphic_on_imp_continuous_on r(1,3) subsetCE)
  3973     ultimately have "(\<lambda>w. (g w * (w - z) powr of_int n) / g w) \<midarrow>z\<rightarrow> f z/g z"
  3974       apply (rule_tac tendsto_divide)
  3975       using \<open>g z\<noteq>0\<close> by auto
  3976     then have powr_tendsto:"(\<lambda>w. (w - z) powr of_int n) \<midarrow>z\<rightarrow> f z/g z"
  3977       apply (elim Lim_transform_within_open[where s="ball z r"])
  3978       using r by auto
  3979 
  3980     have ?thesis when "n\<ge>0" "f z=0" 
  3981     proof -
  3982       have "(\<lambda>w. (w - z) ^ nat n) \<midarrow>z\<rightarrow> f z/g z"
  3983         using powr_tendsto 
  3984         apply (elim Lim_transform_within[where d=r])
  3985         by (auto simp add: powr_of_int \<open>n\<ge>0\<close> \<open>r>0\<close>)
  3986       then have *:"(\<lambda>w. (w - z) ^ nat n) \<midarrow>z\<rightarrow> 0" using \<open>f z=0\<close> by simp
  3987       moreover have False when "n=0"
  3988       proof -
  3989         have "(\<lambda>w. (w - z) ^ nat n) \<midarrow>z\<rightarrow> 1"
  3990           using \<open>n=0\<close> by auto
  3991         then show False using * using LIM_unique zero_neq_one by blast
  3992       qed
  3993       ultimately show ?thesis using that by fastforce
  3994     qed
  3995     moreover have ?thesis when "n\<ge>0" "f z\<noteq>0" 
  3996     proof -
  3997       have False when "n>0"
  3998       proof -
  3999         have "(\<lambda>w. (w - z) ^ nat n) \<midarrow>z\<rightarrow> f z/g z"
  4000           using powr_tendsto 
  4001           apply (elim Lim_transform_within[where d=r])
  4002           by (auto simp add: powr_of_int \<open>n\<ge>0\<close> \<open>r>0\<close>)
  4003         moreover have "(\<lambda>w. (w - z) ^ nat n) \<midarrow>z\<rightarrow> 0"
  4004           using \<open>n>0\<close> by (auto intro!:tendsto_eq_intros)
  4005         ultimately show False using \<open>f z\<noteq>0\<close> \<open>g z\<noteq>0\<close> using LIM_unique divide_eq_0_iff by blast
  4006       qed
  4007       then show ?thesis using that by force
  4008     qed
  4009     moreover have False when "n<0"
  4010     proof -
  4011       have "(\<lambda>w. inverse ((w - z) ^ nat (- n))) \<midarrow>z\<rightarrow> f z/g z"
  4012            "(\<lambda>w.((w - z) ^ nat (- n))) \<midarrow>z\<rightarrow> 0"
  4013         subgoal  using powr_tendsto powr_of_int that
  4014           by (elim Lim_transform_within_open[where s=UNIV],auto)
  4015         subgoal using that by (auto intro!:tendsto_eq_intros)
  4016         done
  4017       from tendsto_mult[OF this,simplified] 
  4018       have "(\<lambda>x. inverse ((x - z) ^ nat (- n)) * (x - z) ^ nat (- n)) \<midarrow>z\<rightarrow> 0" .
  4019       then have "(\<lambda>x. 1::complex) \<midarrow>z\<rightarrow> 0" 
  4020         by (elim Lim_transform_within_open[where s=UNIV],auto)
  4021       then show False using LIM_const_eq by fastforce
  4022     qed
  4023     ultimately show ?thesis by fastforce
  4024   qed
  4025   moreover have "f w  = g w * (w-z) ^ nat n  \<and> g w \<noteq>0" when "w\<in>cball z r" for w
  4026   proof (cases "w=z")
  4027     case True
  4028     then have "f \<midarrow>z\<rightarrow>f w" 
  4029       using assms(4,6) at_within_open continuous_on holo holomorphic_on_imp_continuous_on by fastforce
  4030     then have "(\<lambda>w. g w * (w-z) ^ nat n) \<midarrow>z\<rightarrow>f w"
  4031     proof (elim Lim_transform_within[OF _ \<open>r>0\<close>])
  4032       fix x assume "0 < dist x z" "dist x z < r"
  4033       then have "x \<in> cball z r - {z}" "x\<noteq>z"
  4034         unfolding cball_def by (auto simp add: dist_commute)
  4035       then have "f x = g x * (x - z) powr of_int n"
  4036         using r(4)[rule_format,of x] by simp
  4037       also have "... = g x * (x - z) ^ nat n"
  4038         apply (subst powr_of_int)
  4039         using if_0 \<open>x\<noteq>z\<close> by (auto split:if_splits)
  4040       finally show "f x = g x * (x - z) ^ nat n" .
  4041     qed
  4042     moreover have "(\<lambda>w. g w * (w-z) ^ nat n) \<midarrow>z\<rightarrow> g w * (w-z) ^ nat n"
  4043       using True apply (auto intro!:tendsto_eq_intros)
  4044       by (metis open_ball at_within_open_subset ball_subset_cball centre_in_ball 
  4045           continuous_on holomorphic_on_imp_continuous_on r(1) r(3) that)
  4046     ultimately have "f w = g w * (w-z) ^ nat n" using LIM_unique by blast
  4047     then show ?thesis using \<open>g z\<noteq>0\<close> True by auto
  4048   next
  4049     case False
  4050     then have "f w = g w * (w - z) powr of_int n \<and> g w \<noteq> 0"
  4051       using r(4) that by auto
  4052     then show ?thesis using False if_0 powr_of_int by (auto split:if_splits)
  4053   qed
  4054   ultimately show ?thesis using r by auto
  4055 qed
  4056 
  4057 lemma zorder_exist_pole:
  4058   fixes f::"complex \<Rightarrow> complex" and z::complex
  4059   defines "n\<equiv>zorder f z" and "g\<equiv>zor_poly f z"
  4060   assumes  holo: "f holomorphic_on s-{z}" and 
  4061           "open s" "z\<in>s"
  4062       and "is_pole f z"
  4063   shows "n < 0 \<and> g z\<noteq>0 \<and> (\<exists>r. r>0 \<and> cball z r \<subseteq> s \<and> g holomorphic_on cball z r
  4064     \<and> (\<forall>w\<in>cball z r - {z}. f w  = g w / (w-z) ^ nat (- n) \<and> g w \<noteq>0))"
  4065 proof -
  4066   obtain r where "g z \<noteq> 0" and r: "r>0" "cball z r \<subseteq> s" "g holomorphic_on cball z r" 
  4067             "(\<forall>w\<in>cball z r - {z}. f w = g w * (w - z) powr of_int n \<and> g w \<noteq> 0)"
  4068   proof -
  4069     have "g z \<noteq> 0 \<and> (\<exists>r>0. g holomorphic_on cball z r 
  4070             \<and> (\<forall>w\<in>cball z r - {z}. f w = g w * (w - z) powr of_int n \<and> g w \<noteq> 0))"
  4071     proof (rule zorder_exist[of f z,folded g_def n_def])
  4072       show "isolated_singularity_at f z" unfolding isolated_singularity_at_def
  4073         using holo assms(4,5)
  4074         by (metis analytic_on_holomorphic centre_in_ball insert_Diff openE open_delete subset_insert_iff)
  4075       show "not_essential f z" unfolding not_essential_def 
  4076         using assms(4,6) at_within_open continuous_on holo holomorphic_on_imp_continuous_on 
  4077         by fastforce
  4078       from non_zero_neighbour_pole[OF \<open>is_pole f z\<close>] show "\<exists>\<^sub>F w in at z. f w \<noteq> 0"
  4079         apply (elim eventually_frequentlyE)
  4080         by auto
  4081     qed
  4082     then obtain r1 where "g z \<noteq> 0" "r1>0" and r1:"g holomorphic_on cball z r1"
  4083             "(\<forall>w\<in>cball z r1 - {z}. f w = g w * (w - z) powr of_int n \<and> g w \<noteq> 0)"
  4084       by auto
  4085     obtain r2 where r2: "r2>0" "cball z r2 \<subseteq> s" 
  4086       using assms(4,5) open_contains_cball_eq by metis
  4087     define r3 where "r3=min r1 r2"
  4088     have "r3>0" "cball z r3 \<subseteq> s" using \<open>r1>0\<close> r2 unfolding r3_def by auto
  4089     moreover have "g holomorphic_on cball z r3" 
  4090       using r1(1) unfolding r3_def by auto
  4091     moreover have "(\<forall>w\<in>cball z r3 - {z}. f w = g w * (w - z) powr of_int n \<and> g w \<noteq> 0)" 
  4092       using r1(2) unfolding r3_def by auto
  4093     ultimately show ?thesis using that[of r3] \<open>g z\<noteq>0\<close> by auto 
  4094   qed
  4095 
  4096   have "n<0"
  4097   proof (rule ccontr)
  4098     assume " \<not> n < 0"
  4099     define c where "c=(if n=0 then g z else 0)"
  4100     have [simp]:"g \<midarrow>z\<rightarrow> g z" 
  4101       by (metis open_ball at_within_open ball_subset_cball centre_in_ball 
  4102             continuous_on holomorphic_on_imp_continuous_on holomorphic_on_subset r(1) r(3) )
  4103     have "\<forall>\<^sub>F x in at z. f x = g x * (x - z) ^ nat n"
  4104       unfolding eventually_at_topological
  4105       apply (rule_tac exI[where x="ball z r"])
  4106       using r powr_of_int \<open>\<not> n < 0\<close> by auto
  4107     moreover have "(\<lambda>x. g x * (x - z) ^ nat n) \<midarrow>z\<rightarrow>c"
  4108     proof (cases "n=0")
  4109       case True
  4110       then show ?thesis unfolding c_def by simp
  4111     next
  4112       case False
  4113       then have "(\<lambda>x. (x - z) ^ nat n) \<midarrow>z\<rightarrow> 0" using \<open>\<not> n < 0\<close>
  4114         by (auto intro!:tendsto_eq_intros)
  4115       from tendsto_mult[OF _ this,of g "g z",simplified] 
  4116       show ?thesis unfolding c_def using False by simp
  4117     qed
  4118     ultimately have "f \<midarrow>z\<rightarrow>c" using tendsto_cong by fast
  4119     then show False using \<open>is_pole f z\<close> at_neq_bot not_tendsto_and_filterlim_at_infinity 
  4120       unfolding is_pole_def by blast
  4121   qed
  4122   moreover have "\<forall>w\<in>cball z r - {z}. f w  = g w / (w-z) ^ nat (- n) \<and> g w \<noteq>0"
  4123     using r(4) \<open>n<0\<close> powr_of_int 
  4124     by (metis Diff_iff divide_inverse eq_iff_diff_eq_0 insert_iff linorder_not_le)
  4125   ultimately show ?thesis using r(1-3) \<open>g z\<noteq>0\<close> by auto
  4126 qed
  4127 
  4128 lemma zorder_eqI:
  4129   assumes "open s" "z \<in> s" "g holomorphic_on s" "g z \<noteq> 0"
  4130   assumes fg_eq:"\<And>w. \<lbrakk>w \<in> s;w\<noteq>z\<rbrakk> \<Longrightarrow> f w = g w * (w - z) powr n"
  4131   shows   "zorder f z = n"
  4132 proof -
  4133   have "continuous_on s g" by (rule holomorphic_on_imp_continuous_on) fact
  4134   moreover have "open (-{0::complex})" by auto
  4135   ultimately have "open ((g -` (-{0})) \<inter> s)"
  4136     unfolding continuous_on_open_vimage[OF \<open>open s\<close>] by blast
  4137   moreover from assms have "z \<in> (g -` (-{0})) \<inter> s" by auto
  4138   ultimately obtain r where r: "r > 0" "cball z r \<subseteq>  s \<inter> (g -` (-{0}))"
  4139     unfolding open_contains_cball by blast
  4140 
  4141   let ?gg= "(\<lambda>w. g w * (w - z) powr n)"
  4142   define P where "P = (\<lambda>n g r. 0 < r \<and> g holomorphic_on cball z r \<and> g z\<noteq>0
  4143           \<and> (\<forall>w\<in>cball z r - {z}. f w = g w * (w-z) powr (of_int n) \<and> g w\<noteq>0))"
  4144   have "P n g r"
  4145     unfolding P_def using r assms(3,4,5) by auto
  4146   then have "\<exists>g r. P n g r" by auto
  4147   moreover have unique: "\<exists>!n. \<exists>g r. P n g r" unfolding P_def
  4148   proof (rule holomorphic_factor_puncture)
  4149     have "ball z r-{z} \<subseteq> s" using r using ball_subset_cball by blast
  4150     then have "?gg holomorphic_on ball z r-{z}"
  4151       using \<open>g holomorphic_on s\<close> r by (auto intro!: holomorphic_intros)
  4152     then have "f holomorphic_on ball z r - {z}"
  4153       apply (elim holomorphic_transform)
  4154       using fg_eq \<open>ball z r-{z} \<subseteq> s\<close> by auto
  4155     then show "isolated_singularity_at f z" unfolding isolated_singularity_at_def
  4156       using analytic_on_open open_delete r(1) by blast
  4157   next
  4158     have "not_essential ?gg z"
  4159     proof (intro singularity_intros)
  4160       show "not_essential g z" 
  4161         by (meson \<open>continuous_on s g\<close> assms(1) assms(2) continuous_on_eq_continuous_at 
  4162             isCont_def not_essential_def)
  4163       show " \<forall>\<^sub>F w in at z. w - z \<noteq> 0" by (simp add: eventually_at_filter)
  4164       then show "LIM w at z. w - z :> at 0" 
  4165         unfolding filterlim_at by (auto intro:tendsto_eq_intros)
  4166       show "isolated_singularity_at g z" 
  4167         by (meson Diff_subset open_ball analytic_on_holomorphic 
  4168             assms(1,2,3) holomorphic_on_subset isolated_singularity_at_def openE)
  4169     qed
  4170     then show "not_essential f z"
  4171       apply (elim not_essential_transform)
  4172       unfolding eventually_at using assms(1,2) assms(5)[symmetric] 
  4173       by (metis dist_commute mem_ball openE subsetCE)
  4174     show "\<exists>\<^sub>F w in at z. f w \<noteq> 0" unfolding frequently_at 
  4175     proof (rule,rule)
  4176       fix d::real assume "0 < d"
  4177       define z' where "z'=z+min d r / 2"
  4178       have "z' \<noteq> z" " dist z' z < d "
  4179         unfolding z'_def using \<open>d>0\<close> \<open>r>0\<close> 
  4180         by (auto simp add:dist_norm)
  4181       moreover have "f z' \<noteq> 0"  
  4182       proof (subst fg_eq[OF _ \<open>z'\<noteq>z\<close>])
  4183         have "z' \<in> cball z r" unfolding z'_def using \<open>r>0\<close> \<open>d>0\<close> by (auto simp add:dist_norm)
  4184         then show " z' \<in> s" using r(2) by blast
  4185         show "g z' * (z' - z) powr of_int n \<noteq> 0" 
  4186           using P_def \<open>P n g r\<close> \<open>z' \<in> cball z r\<close> calculation(1) by auto
  4187       qed
  4188       ultimately show "\<exists>x\<in>UNIV. x \<noteq> z \<and> dist x z < d \<and> f x \<noteq> 0" by auto
  4189     qed
  4190   qed
  4191   ultimately have "(THE n. \<exists>g r. P n g r) = n"
  4192     by (rule_tac the1_equality)
  4193   then show ?thesis unfolding zorder_def P_def by blast
  4194 qed
  4195 
  4196 lemma residue_pole_order:
  4197   fixes f::"complex \<Rightarrow> complex" and z::complex
  4198   defines "n \<equiv> nat (- zorder f z)" and "h \<equiv> zor_poly f z"
  4199   assumes f_iso:"isolated_singularity_at f z"
  4200     and pole:"is_pole f z"
  4201   shows "residue f z = ((deriv ^^ (n - 1)) h z / fact (n-1))"
  4202 proof -
  4203   define g where "g \<equiv> \<lambda>x. if x=z then 0 else inverse (f x)"
  4204   obtain e where [simp]:"e>0" and f_holo:"f holomorphic_on ball z e - {z}"
  4205     using f_iso analytic_imp_holomorphic unfolding isolated_singularity_at_def by blast
  4206   obtain r where "0 < n" "0 < r" and r_cball:"cball z r \<subseteq> ball z e" and h_holo: "h holomorphic_on cball z r"
  4207       and h_divide:"(\<forall>w\<in>cball z r. (w\<noteq>z \<longrightarrow> f w = h w / (w - z) ^ n) \<and> h w \<noteq> 0)"
  4208   proof -
  4209     obtain r where r:"zorder f z < 0" "h z \<noteq> 0" "r>0" "cball z r \<subseteq> ball z e" "h holomorphic_on cball z r" 
  4210         "(\<forall>w\<in>cball z r - {z}. f w = h w / (w - z) ^ n \<and> h w \<noteq> 0)"
  4211       using zorder_exist_pole[OF f_holo,simplified,OF \<open>is_pole f z\<close>,folded n_def h_def] by auto
  4212     have "n>0" using \<open>zorder f z < 0\<close> unfolding n_def by simp
  4213     moreover have "(\<forall>w\<in>cball z r. (w\<noteq>z \<longrightarrow> f w = h w / (w - z) ^ n) \<and> h w \<noteq> 0)"
  4214       using \<open>h z\<noteq>0\<close> r(6) by blast
  4215     ultimately show ?thesis using r(3,4,5) that by blast
  4216   qed
  4217   have r_nonzero:"\<And>w. w \<in> ball z r - {z} \<Longrightarrow> f w \<noteq> 0"
  4218     using h_divide by simp
  4219   define c where "c \<equiv> 2 * pi * \<i>"
  4220   define der_f where "der_f \<equiv> ((deriv ^^ (n - 1)) h z / fact (n-1))"
  4221   define h' where "h' \<equiv> \<lambda>u. h u / (u - z) ^ n"
  4222   have "(h' has_contour_integral c / fact (n - 1) * (deriv ^^ (n - 1)) h z) (circlepath z r)"
  4223     unfolding h'_def
  4224     proof (rule Cauchy_has_contour_integral_higher_derivative_circlepath[of z r h z "n-1",
  4225         folded c_def Suc_pred'[OF \<open>n>0\<close>]])
  4226       show "continuous_on (cball z r) h" using holomorphic_on_imp_continuous_on h_holo by simp
  4227       show "h holomorphic_on ball z r" using h_holo by auto
  4228       show " z \<in> ball z r" using \<open>r>0\<close> by auto
  4229     qed
  4230   then have "(h' has_contour_integral c * der_f) (circlepath z r)" unfolding der_f_def by auto
  4231   then have "(f has_contour_integral c * der_f) (circlepath z r)"
  4232     proof (elim has_contour_integral_eq)
  4233       fix x assume "x \<in> path_image (circlepath z r)"
  4234       hence "x\<in>cball z r - {z}" using \<open>r>0\<close> by auto
  4235       then show "h' x = f x" using h_divide unfolding h'_def by auto
  4236     qed
  4237   moreover have "(f has_contour_integral c * residue f z) (circlepath z r)"
  4238     using base_residue[of \<open>ball z e\<close> z,simplified,OF \<open>r>0\<close> f_holo r_cball,folded c_def] 
  4239     unfolding c_def by simp
  4240   ultimately have "c * der_f =  c * residue f z" using has_contour_integral_unique by blast
  4241   hence "der_f = residue f z" unfolding c_def by auto
  4242   thus ?thesis unfolding der_f_def by auto
  4243 qed
  4244 
  4245 lemma simple_zeroI:
  4246   assumes "open s" "z \<in> s" "g holomorphic_on s" "g z \<noteq> 0"
  4247   assumes "\<And>w. w \<in> s \<Longrightarrow> f w = g w * (w - z)"
  4248   shows   "zorder f z = 1"
  4249   using assms(1-4) by (rule zorder_eqI) (use assms(5) in auto)
  4250 
  4251 lemma higher_deriv_power:
  4252   shows   "(deriv ^^ j) (\<lambda>w. (w - z) ^ n) w = 
  4253              pochhammer (of_nat (Suc n - j)) j * (w - z) ^ (n - j)"
  4254 proof (induction j arbitrary: w)
  4255   case 0
  4256   thus ?case by auto
  4257 next
  4258   case (Suc j w)
  4259   have "(deriv ^^ Suc j) (\<lambda>w. (w - z) ^ n) w = deriv ((deriv ^^ j) (\<lambda>w. (w - z) ^ n)) w"
  4260     by simp
  4261   also have "(deriv ^^ j) (\<lambda>w. (w - z) ^ n) = 
  4262                (\<lambda>w. pochhammer (of_nat (Suc n - j)) j * (w - z) ^ (n - j))"
  4263     using Suc by (intro Suc.IH ext)
  4264   also {
  4265     have "(\<dots> has_field_derivative of_nat (n - j) *
  4266                pochhammer (of_nat (Suc n - j)) j * (w - z) ^ (n - Suc j)) (at w)"
  4267       using Suc.prems by (auto intro!: derivative_eq_intros)
  4268     also have "of_nat (n - j) * pochhammer (of_nat (Suc n - j)) j = 
  4269                  pochhammer (of_nat (Suc n - Suc j)) (Suc j)"
  4270       by (cases "Suc j \<le> n", subst pochhammer_rec) 
  4271          (insert Suc.prems, simp_all add: algebra_simps Suc_diff_le pochhammer_0_left)
  4272     finally have "deriv (\<lambda>w. pochhammer (of_nat (Suc n - j)) j * (w - z) ^ (n - j)) w =
  4273                     \<dots> * (w - z) ^ (n - Suc j)"
  4274       by (rule DERIV_imp_deriv)
  4275   }
  4276   finally show ?case .
  4277 qed
  4278 
  4279 lemma zorder_zero_eqI:
  4280   assumes  f_holo:"f holomorphic_on s" and "open s" "z \<in> s"
  4281   assumes zero: "\<And>i. i < nat n \<Longrightarrow> (deriv ^^ i) f z = 0"
  4282   assumes nz: "(deriv ^^ nat n) f z \<noteq> 0" and "n\<ge>0"
  4283   shows   "zorder f z = n"
  4284 proof -
  4285   obtain r where [simp]:"r>0" and "ball z r \<subseteq> s"
  4286     using \<open>open s\<close> \<open>z\<in>s\<close> openE by blast
  4287   have nz':"\<exists>w\<in>ball z r. f w \<noteq> 0"
  4288   proof (rule ccontr)
  4289     assume "\<not> (\<exists>w\<in>ball z r. f w \<noteq> 0)"
  4290     then have "eventually (\<lambda>u. f u = 0) (nhds z)"
  4291       using \<open>r>0\<close> unfolding eventually_nhds 
  4292       apply (rule_tac x="ball z r" in exI)
  4293       by auto
  4294     then have "(deriv ^^ nat n) f z = (deriv ^^ nat n) (\<lambda>_. 0) z"
  4295       by (intro higher_deriv_cong_ev) auto
  4296     also have "(deriv ^^ nat n) (\<lambda>_. 0) z = 0"
  4297       by (induction n) simp_all
  4298     finally show False using nz by contradiction
  4299   qed
  4300 
  4301   define zn g where "zn = zorder f z" and "g = zor_poly f z"
  4302   obtain e where e_if:"if f z = 0 then 0 < zn else zn = 0" and
  4303             [simp]:"e>0" and "cball z e \<subseteq> ball z r" and
  4304             g_holo:"g holomorphic_on cball z e" and
  4305             e_fac:"(\<forall>w\<in>cball z e. f w = g w * (w - z) ^ nat zn \<and> g w \<noteq> 0)"
  4306   proof -
  4307     have "f holomorphic_on ball z r"
  4308       using f_holo \<open>ball z r \<subseteq> s\<close> by auto
  4309     from that zorder_exist_zero[of f "ball z r" z,simplified,OF this nz',folded zn_def g_def]
  4310     show ?thesis by blast
  4311   qed
  4312   from this(1,2,5) have "zn\<ge>0" "g z\<noteq>0"
  4313     subgoal by (auto split:if_splits) 
  4314     subgoal using \<open>0 < e\<close> ball_subset_cball centre_in_ball e_fac by blast
  4315     done
  4316 
  4317   define A where "A = (\<lambda>i. of_nat (i choose (nat zn)) * fact (nat zn) * (deriv ^^ (i - nat zn)) g z)"
  4318   have deriv_A:"(deriv ^^ i) f z = (if zn \<le> int i then A i else 0)" for i
  4319   proof -
  4320     have "eventually (\<lambda>w. w \<in> ball z e) (nhds z)"
  4321       using \<open>cball z e \<subseteq> ball z r\<close> \<open>e>0\<close> by (intro eventually_nhds_in_open) auto
  4322     hence "eventually (\<lambda>w. f w = (w - z) ^ (nat zn) * g w) (nhds z)"
  4323       apply eventually_elim 
  4324       by (use e_fac in auto)
  4325     hence "(deriv ^^ i) f z = (deriv ^^ i) (\<lambda>w. (w - z) ^ nat zn * g w) z"
  4326       by (intro higher_deriv_cong_ev) auto
  4327     also have "\<dots> = (\<Sum>j=0..i. of_nat (i choose j) *
  4328                        (deriv ^^ j) (\<lambda>w. (w - z) ^ nat zn) z * (deriv ^^ (i - j)) g z)"
  4329       using g_holo \<open>e>0\<close> 
  4330       by (intro higher_deriv_mult[of _ "ball z e"]) (auto intro!: holomorphic_intros)
  4331     also have "\<dots> = (\<Sum>j=0..i. if j = nat zn then 
  4332                     of_nat (i choose nat zn) * fact (nat zn) * (deriv ^^ (i - nat zn)) g z else 0)"
  4333     proof (intro sum.cong refl, goal_cases)
  4334       case (1 j)
  4335       have "(deriv ^^ j) (\<lambda>w. (w - z) ^ nat zn) z = 
  4336               pochhammer (of_nat (Suc (nat zn) - j)) j * 0 ^ (nat zn - j)"
  4337         by (subst higher_deriv_power) auto
  4338       also have "\<dots> = (if j = nat zn then fact j else 0)"
  4339         by (auto simp: not_less pochhammer_0_left pochhammer_fact)
  4340       also have "of_nat (i choose j) * \<dots> * (deriv ^^ (i - j)) g z = 
  4341                    (if j = nat zn then of_nat (i choose (nat zn)) * fact (nat zn) 
  4342                         * (deriv ^^ (i - nat zn)) g z else 0)"
  4343         by simp
  4344       finally show ?case .
  4345     qed
  4346     also have "\<dots> = (if i \<ge> zn then A i else 0)"
  4347       by (auto simp: A_def)
  4348     finally show "(deriv ^^ i) f z = \<dots>" .
  4349   qed
  4350 
  4351   have False when "n<zn"
  4352   proof -
  4353     have "(deriv ^^ nat n) f z = 0"
  4354       using deriv_A[of "nat n"] that \<open>n\<ge>0\<close> by auto 
  4355     with nz show False by auto
  4356   qed
  4357   moreover have "n\<le>zn"
  4358   proof -
  4359     have "g z \<noteq> 0" using e_fac[rule_format,of z] \<open>e>0\<close> by simp 
  4360     then have "(deriv ^^ nat zn) f z \<noteq> 0"
  4361       using deriv_A[of "nat zn"] by(auto simp add:A_def)
  4362     then have "nat zn \<ge> nat n" using zero[of "nat zn"] by linarith
  4363     moreover have "zn\<ge>0" using e_if by (auto split:if_splits)
  4364     ultimately show ?thesis using nat_le_eq_zle by blast
  4365   qed
  4366   ultimately show ?thesis unfolding zn_def by fastforce
  4367 qed
  4368 
  4369 lemma 
  4370   assumes "eventually (\<lambda>z. f z = g z) (at z)" "z = z'"
  4371   shows zorder_cong:"zorder f z = zorder g z'" and zor_poly_cong:"zor_poly f z = zor_poly g z'"
  4372 proof -
  4373   define P where "P = (\<lambda>ff n h r. 0 < r \<and> h holomorphic_on cball z r \<and> h z\<noteq>0
  4374                     \<and> (\<forall>w\<in>cball z r - {z}. ff w = h w * (w-z) powr (of_int n) \<and> h w\<noteq>0))"
  4375   have "(\<exists>r. P f n h r) = (\<exists>r. P g n h r)" for n h 
  4376   proof -
  4377     have *: "\<exists>r. P g n h r" if "\<exists>r. P f n h r" and "eventually (\<lambda>x. f x = g x) (at z)" for f g 
  4378     proof -
  4379       from that(1) obtain r1 where r1_P:"P f n h r1" by auto
  4380       from that(2) obtain r2 where "r2>0" and r2_dist:"\<forall>x. x \<noteq> z \<and> dist x z \<le> r2 \<longrightarrow> f x = g x"
  4381         unfolding eventually_at_le by auto
  4382       define r where "r=min r1 r2"
  4383       have "r>0" "h z\<noteq>0" using r1_P \<open>r2>0\<close> unfolding r_def P_def by auto
  4384       moreover have "h holomorphic_on cball z r"
  4385         using r1_P unfolding P_def r_def by auto
  4386       moreover have "g w = h w * (w - z) powr of_int n \<and> h w \<noteq> 0" when "w\<in>cball z r - {z}" for w
  4387       proof -
  4388         have "f w = h w * (w - z) powr of_int n \<and> h w \<noteq> 0"
  4389           using r1_P that unfolding P_def r_def by auto
  4390         moreover have "f w=g w" using r2_dist[rule_format,of w] that unfolding r_def 
  4391           by (simp add: dist_commute) 
  4392         ultimately show ?thesis by simp
  4393       qed
  4394       ultimately show ?thesis unfolding P_def by auto
  4395     qed
  4396     from assms have eq': "eventually (\<lambda>z. g z = f z) (at z)"
  4397       by (simp add: eq_commute)
  4398     show ?thesis
  4399       by (rule iffI[OF *[OF _ assms(1)] *[OF _ eq']])
  4400   qed
  4401   then show "zorder f z = zorder g z'" "zor_poly f z = zor_poly g z'"  
  4402       using \<open>z=z'\<close> unfolding P_def zorder_def zor_poly_def by auto
  4403 qed
  4404 
  4405 lemma zorder_nonzero_div_power:
  4406   assumes "open s" "z \<in> s" "f holomorphic_on s" "f z \<noteq> 0" "n > 0"
  4407   shows  "zorder (\<lambda>w. f w / (w - z) ^ n) z = - n"
  4408   apply (rule zorder_eqI[OF \<open>open s\<close> \<open>z\<in>s\<close> \<open>f holomorphic_on s\<close> \<open>f z\<noteq>0\<close>])
  4409   apply (subst powr_of_int)
  4410   using \<open>n>0\<close> by (auto simp add:field_simps)
  4411 
  4412 lemma zor_poly_eq:
  4413   assumes "isolated_singularity_at f z" "not_essential f z" "\<exists>\<^sub>F w in at z. f w \<noteq> 0"
  4414   shows "eventually (\<lambda>w. zor_poly f z w = f w * (w - z) powr - zorder f z) (at z)"
  4415 proof -
  4416   obtain r where r:"r>0" 
  4417        "(\<forall>w\<in>cball z r - {z}. f w = zor_poly f z w * (w - z) powr of_int (zorder f z))"
  4418     using zorder_exist[OF assms] by blast
  4419   then have *: "\<forall>w\<in>ball z r - {z}. zor_poly f z w = f w * (w - z) powr - zorder f z" 
  4420     by (auto simp: field_simps powr_minus)
  4421   have "eventually (\<lambda>w. w \<in> ball z r - {z}) (at z)"
  4422     using r eventually_at_ball'[of r z UNIV] by auto
  4423   thus ?thesis by eventually_elim (insert *, auto)
  4424 qed
  4425 
  4426 lemma zor_poly_zero_eq:
  4427   assumes "f holomorphic_on s" "open s" "connected s" "z \<in> s" "\<exists>w\<in>s. f w \<noteq> 0"
  4428   shows "eventually (\<lambda>w. zor_poly f z w = f w / (w - z) ^ nat (zorder f z)) (at z)"
  4429 proof -
  4430   obtain r where r:"r>0" 
  4431        "(\<forall>w\<in>cball z r - {z}. f w = zor_poly f z w * (w - z) ^ nat (zorder f z))"
  4432     using zorder_exist_zero[OF assms] by auto
  4433   then have *: "\<forall>w\<in>ball z r - {z}. zor_poly f z w = f w / (w - z) ^ nat (zorder f z)" 
  4434     by (auto simp: field_simps powr_minus)
  4435   have "eventually (\<lambda>w. w \<in> ball z r - {z}) (at z)"
  4436     using r eventually_at_ball'[of r z UNIV] by auto
  4437   thus ?thesis by eventually_elim (insert *, auto)
  4438 qed
  4439 
  4440 lemma zor_poly_pole_eq:
  4441   assumes f_iso:"isolated_singularity_at f z" "is_pole f z"
  4442   shows "eventually (\<lambda>w. zor_poly f z w = f w * (w - z) ^ nat (- zorder f z)) (at z)"
  4443 proof -
  4444   obtain e where [simp]:"e>0" and f_holo:"f holomorphic_on ball z e - {z}"
  4445     using f_iso analytic_imp_holomorphic unfolding isolated_singularity_at_def by blast
  4446   obtain r where r:"r>0" 
  4447        "(\<forall>w\<in>cball z r - {z}. f w = zor_poly f z w / (w - z) ^ nat (- zorder f z))"
  4448     using zorder_exist_pole[OF f_holo,simplified,OF \<open>is_pole f z\<close>] by auto
  4449   then have *: "\<forall>w\<in>ball z r - {z}. zor_poly f z w = f w * (w - z) ^ nat (- zorder f z)" 
  4450     by (auto simp: field_simps)
  4451   have "eventually (\<lambda>w. w \<in> ball z r - {z}) (at z)"
  4452     using r eventually_at_ball'[of r z UNIV] by auto
  4453   thus ?thesis by eventually_elim (insert *, auto)
  4454 qed
  4455 
  4456 lemma zor_poly_eqI:
  4457   fixes f :: "complex \<Rightarrow> complex" and z0 :: complex
  4458   defines "n \<equiv> zorder f z0"
  4459   assumes "isolated_singularity_at f z0" "not_essential f z0" "\<exists>\<^sub>F w in at z0. f w \<noteq> 0"
  4460   assumes lim: "((\<lambda>x. f (g x) * (g x - z0) powr - n) \<longlongrightarrow> c) F"
  4461   assumes g: "filterlim g (at z0) F" and "F \<noteq> bot"
  4462   shows   "zor_poly f z0 z0 = c"
  4463 proof -
  4464   from zorder_exist[OF assms(2-4)] obtain r where
  4465     r: "r > 0" "zor_poly f z0 holomorphic_on cball z0 r"
  4466        "\<And>w. w \<in> cball z0 r - {z0} \<Longrightarrow> f w = zor_poly f z0 w * (w - z0) powr n"
  4467     unfolding n_def by blast
  4468   from r(1) have "eventually (\<lambda>w. w \<in> ball z0 r \<and> w \<noteq> z0) (at z0)"
  4469     using eventually_at_ball'[of r z0 UNIV] by auto
  4470   hence "eventually (\<lambda>w. zor_poly f z0 w = f w * (w - z0) powr - n) (at z0)"
  4471     by eventually_elim (insert r, auto simp: field_simps powr_minus)
  4472   moreover have "continuous_on (ball z0 r) (zor_poly f z0)"
  4473     using r by (intro holomorphic_on_imp_continuous_on) auto
  4474   with r(1,2) have "isCont (zor_poly f z0) z0"
  4475     by (auto simp: continuous_on_eq_continuous_at)
  4476   hence "(zor_poly f z0 \<longlongrightarrow> zor_poly f z0 z0) (at z0)"
  4477     unfolding isCont_def .
  4478   ultimately have "((\<lambda>w. f w * (w - z0) powr - n) \<longlongrightarrow> zor_poly f z0 z0) (at z0)"
  4479     by (rule Lim_transform_eventually)
  4480   hence "((\<lambda>x. f (g x) * (g x - z0) powr - n) \<longlongrightarrow> zor_poly f z0 z0) F"
  4481     by (rule filterlim_compose[OF _ g])
  4482   from tendsto_unique[OF \<open>F \<noteq> bot\<close> this lim] show ?thesis .
  4483 qed
  4484 
  4485 lemma zor_poly_zero_eqI:
  4486   fixes f :: "complex \<Rightarrow> complex" and z0 :: complex
  4487   defines "n \<equiv> zorder f z0"
  4488   assumes "f holomorphic_on A" "open A" "connected A" "z0 \<in> A" "\<exists>z\<in>A. f z \<noteq> 0"
  4489   assumes lim: "((\<lambda>x. f (g x) / (g x - z0) ^ nat n) \<longlongrightarrow> c) F"
  4490   assumes g: "filterlim g (at z0) F" and "F \<noteq> bot"
  4491   shows   "zor_poly f z0 z0 = c"
  4492 proof -
  4493   from zorder_exist_zero[OF assms(2-6)] obtain r where
  4494     r: "r > 0" "cball z0 r \<subseteq> A" "zor_poly f z0 holomorphic_on cball z0 r"
  4495        "\<And>w. w \<in> cball z0 r \<Longrightarrow> f w = zor_poly f z0 w * (w - z0) ^ nat n"
  4496     unfolding n_def by blast
  4497   from r(1) have "eventually (\<lambda>w. w \<in> ball z0 r \<and> w \<noteq> z0) (at z0)"
  4498     using eventually_at_ball'[of r z0 UNIV] by auto
  4499   hence "eventually (\<lambda>w. zor_poly f z0 w = f w / (w - z0) ^ nat n) (at z0)"
  4500     by eventually_elim (insert r, auto simp: field_simps)
  4501   moreover have "continuous_on (ball z0 r) (zor_poly f z0)"
  4502     using r by (intro holomorphic_on_imp_continuous_on) auto
  4503   with r(1,2) have "isCont (zor_poly f z0) z0"
  4504     by (auto simp: continuous_on_eq_continuous_at)
  4505   hence "(zor_poly f z0 \<longlongrightarrow> zor_poly f z0 z0) (at z0)"
  4506     unfolding isCont_def .
  4507   ultimately have "((\<lambda>w. f w / (w - z0) ^ nat n) \<longlongrightarrow> zor_poly f z0 z0) (at z0)"
  4508     by (rule Lim_transform_eventually)
  4509   hence "((\<lambda>x. f (g x) / (g x - z0) ^ nat n) \<longlongrightarrow> zor_poly f z0 z0) F"
  4510     by (rule filterlim_compose[OF _ g])
  4511   from tendsto_unique[OF \<open>F \<noteq> bot\<close> this lim] show ?thesis .
  4512 qed
  4513 
  4514 lemma zor_poly_pole_eqI:
  4515   fixes f :: "complex \<Rightarrow> complex" and z0 :: complex
  4516   defines "n \<equiv> zorder f z0"
  4517   assumes f_iso:"isolated_singularity_at f z0" and "is_pole f z0"
  4518   assumes lim: "((\<lambda>x. f (g x) * (g x - z0) ^ nat (-n)) \<longlongrightarrow> c) F"
  4519   assumes g: "filterlim g (at z0) F" and "F \<noteq> bot"
  4520   shows   "zor_poly f z0 z0 = c"
  4521 proof -
  4522   obtain r where r: "r > 0"  "zor_poly f z0 holomorphic_on cball z0 r"
  4523   proof -   
  4524     have "\<exists>\<^sub>F w in at z0. f w \<noteq> 0" 
  4525       using non_zero_neighbour_pole[OF \<open>is_pole f z0\<close>] by (auto elim:eventually_frequentlyE)
  4526     moreover have "not_essential f z0" unfolding not_essential_def using \<open>is_pole f z0\<close> by simp
  4527     ultimately show ?thesis using that zorder_exist[OF f_iso,folded n_def] by auto
  4528   qed
  4529   from r(1) have "eventually (\<lambda>w. w \<in> ball z0 r \<and> w \<noteq> z0) (at z0)"
  4530     using eventually_at_ball'[of r z0 UNIV] by auto
  4531   have "eventually (\<lambda>w. zor_poly f z0 w = f w * (w - z0) ^ nat (-n)) (at z0)"
  4532     using zor_poly_pole_eq[OF f_iso \<open>is_pole f z0\<close>] unfolding n_def .
  4533   moreover have "continuous_on (ball z0 r) (zor_poly f z0)"
  4534     using r by (intro holomorphic_on_imp_continuous_on) auto
  4535   with r(1,2) have "isCont (zor_poly f z0) z0"
  4536     by (auto simp: continuous_on_eq_continuous_at)
  4537   hence "(zor_poly f z0 \<longlongrightarrow> zor_poly f z0 z0) (at z0)"
  4538     unfolding isCont_def .
  4539   ultimately have "((\<lambda>w. f w * (w - z0) ^ nat (-n)) \<longlongrightarrow> zor_poly f z0 z0) (at z0)"
  4540     by (rule Lim_transform_eventually)
  4541   hence "((\<lambda>x. f (g x) * (g x - z0) ^ nat (-n)) \<longlongrightarrow> zor_poly f z0 z0) F"
  4542     by (rule filterlim_compose[OF _ g])
  4543   from tendsto_unique[OF \<open>F \<noteq> bot\<close> this lim] show ?thesis .
  4544 qed
  4545 
  4546 lemma residue_simple_pole:
  4547   assumes "isolated_singularity_at f z0" 
  4548   assumes "is_pole f z0" "zorder f z0 = - 1"
  4549   shows   "residue f z0 = zor_poly f z0 z0"
  4550   using assms by (subst residue_pole_order) simp_all
  4551 
  4552 lemma residue_simple_pole_limit:
  4553   assumes "isolated_singularity_at f z0" 
  4554   assumes "is_pole f z0" "zorder f z0 = - 1"
  4555   assumes "((\<lambda>x. f (g x) * (g x - z0)) \<longlongrightarrow> c) F"
  4556   assumes "filterlim g (at z0) F" "F \<noteq> bot"
  4557   shows   "residue f z0 = c"
  4558 proof -
  4559   have "residue f z0 = zor_poly f z0 z0"
  4560     by (rule residue_simple_pole assms)+
  4561   also have "\<dots> = c"
  4562     apply (rule zor_poly_pole_eqI)
  4563     using assms by auto
  4564   finally show ?thesis .
  4565 qed
  4566 
  4567 lemma lhopital_complex_simple:
  4568   assumes "(f has_field_derivative f') (at z)" 
  4569   assumes "(g has_field_derivative g') (at z)"
  4570   assumes "f z = 0" "g z = 0" "g' \<noteq> 0" "f' / g' = c"
  4571   shows   "((\<lambda>w. f w / g w) \<longlongrightarrow> c) (at z)"
  4572 proof -
  4573   have "eventually (\<lambda>w. w \<noteq> z) (at z)"
  4574     by (auto simp: eventually_at_filter)
  4575   hence "eventually (\<lambda>w. ((f w - f z) / (w - z)) / ((g w - g z) / (w - z)) = f w / g w) (at z)"
  4576     by eventually_elim (simp add: assms divide_simps)
  4577   moreover have "((\<lambda>w. ((f w - f z) / (w - z)) / ((g w - g z) / (w - z))) \<longlongrightarrow> f' / g') (at z)"
  4578     by (intro tendsto_divide has_field_derivativeD assms)
  4579   ultimately have "((\<lambda>w. f w / g w) \<longlongrightarrow> f' / g') (at z)"
  4580     by (rule Lim_transform_eventually)
  4581   with assms show ?thesis by simp
  4582 qed
  4583 
  4584 lemma
  4585   assumes f_holo:"f holomorphic_on s" and g_holo:"g holomorphic_on s" 
  4586           and "open s" "connected s" "z \<in> s" 
  4587   assumes g_deriv:"(g has_field_derivative g') (at z)"
  4588   assumes "f z \<noteq> 0" "g z = 0" "g' \<noteq> 0"
  4589   shows   porder_simple_pole_deriv: "zorder (\<lambda>w. f w / g w) z = - 1"
  4590     and   residue_simple_pole_deriv: "residue (\<lambda>w. f w / g w) z = f z / g'"
  4591 proof -
  4592   have [simp]:"isolated_singularity_at f z" "isolated_singularity_at g z"
  4593     using isolated_singularity_at_holomorphic[OF _ \<open>open s\<close> \<open>z\<in>s\<close>] f_holo g_holo
  4594     by (meson Diff_subset holomorphic_on_subset)+
  4595   have [simp]:"not_essential f z" "not_essential g z"
  4596     unfolding not_essential_def using f_holo g_holo assms(3,5)
  4597     by (meson continuous_on_eq_continuous_at continuous_within holomorphic_on_imp_continuous_on)+
  4598   have g_nconst:"\<exists>\<^sub>F w in at z. g w \<noteq>0 " 
  4599   proof (rule ccontr)
  4600     assume "\<not> (\<exists>\<^sub>F w in at z. g w \<noteq> 0)"
  4601     then have "\<forall>\<^sub>F w in nhds z. g w = 0"
  4602       unfolding eventually_at eventually_nhds frequently_at using \<open>g z = 0\<close> 
  4603       by (metis open_ball UNIV_I centre_in_ball dist_commute mem_ball)
  4604     then have "deriv g z = deriv (\<lambda>_. 0) z"
  4605       by (intro deriv_cong_ev) auto
  4606     then have "deriv g z = 0" by auto
  4607     then have "g' = 0" using g_deriv DERIV_imp_deriv by blast
  4608     then show False using \<open>g'\<noteq>0\<close> by auto
  4609   qed
  4610   
  4611   have "zorder (\<lambda>w. f w / g w) z = zorder f z - zorder g z"
  4612   proof -
  4613     have "\<forall>\<^sub>F w in at z. f w \<noteq>0 \<and> w\<in>s" 
  4614       apply (rule non_zero_neighbour_alt)
  4615       using assms by auto
  4616     with g_nconst have "\<exists>\<^sub>F w in at z. f w * g w \<noteq> 0" 
  4617       by (elim frequently_rev_mp eventually_rev_mp,auto)
  4618     then show ?thesis using zorder_divide[of f z g] by auto
  4619   qed
  4620   moreover have "zorder f z=0"
  4621     apply (rule zorder_zero_eqI[OF f_holo \<open>open s\<close> \<open>z\<in>s\<close>])
  4622     using \<open>f z\<noteq>0\<close> by auto
  4623   moreover have "zorder g z=1"
  4624     apply (rule zorder_zero_eqI[OF g_holo \<open>open s\<close> \<open>z\<in>s\<close>])
  4625     subgoal using assms(8) by auto
  4626     subgoal using DERIV_imp_deriv assms(9) g_deriv by auto
  4627     subgoal by simp
  4628     done
  4629   ultimately show "zorder (\<lambda>w. f w / g w) z = - 1" by auto
  4630   
  4631   show "residue (\<lambda>w. f w / g w) z = f z / g'"
  4632   proof (rule residue_simple_pole_limit[where g=id and F="at z",simplified])
  4633     show "zorder (\<lambda>w. f w / g w) z = - 1" by fact
  4634     show "isolated_singularity_at (\<lambda>w. f w / g w) z" 
  4635       by (auto intro: singularity_intros)
  4636     show "is_pole (\<lambda>w. f w / g w) z" 
  4637     proof (rule is_pole_divide)
  4638       have "\<forall>\<^sub>F x in at z. g x \<noteq> 0" 
  4639         apply (rule non_zero_neighbour)
  4640         using g_nconst by auto
  4641       moreover have "g \<midarrow>z\<rightarrow> 0" 
  4642         using DERIV_isCont assms(8) continuous_at g_deriv by force
  4643       ultimately show "filterlim g (at 0) (at z)" unfolding filterlim_at by simp
  4644       show "isCont f z" 
  4645         using assms(3,5) continuous_on_eq_continuous_at f_holo holomorphic_on_imp_continuous_on 
  4646         by auto
  4647       show "f z \<noteq> 0" by fact
  4648     qed
  4649     show "filterlim id (at z) (at z)" by (simp add: filterlim_iff)
  4650     have "((\<lambda>w. (f w * (w - z)) / g w) \<longlongrightarrow> f z / g') (at z)"
  4651     proof (rule lhopital_complex_simple)
  4652       show "((\<lambda>w. f w * (w - z)) has_field_derivative f z) (at z)"
  4653         using assms by (auto intro!: derivative_eq_intros holomorphic_derivI[OF f_holo])
  4654       show "(g has_field_derivative g') (at z)" by fact
  4655     qed (insert assms, auto)
  4656     then show "((\<lambda>w. (f w / g w) * (w - z)) \<longlongrightarrow> f z / g') (at z)"
  4657       by (simp add: divide_simps)
  4658   qed
  4659 qed
  4660 
  4661 subsection \<open>The argument principle\<close>
  4662 
  4663 theorem argument_principle:
  4664   fixes f::"complex \<Rightarrow> complex" and poles s:: "complex set"
  4665   defines "pz \<equiv> {w. f w = 0 \<or> w \<in> poles}" \<comment> \<open>\<^term>\<open>pz\<close> is the set of poles and zeros\<close>
  4666   assumes "open s" and
  4667           "connected s" and
  4668           f_holo:"f holomorphic_on s-poles" and
  4669           h_holo:"h holomorphic_on s" and
  4670           "valid_path g" and
  4671           loop:"pathfinish g = pathstart g" and
  4672           path_img:"path_image g \<subseteq> s - pz" and
  4673           homo:"\<forall>z. (z \<notin> s) \<longrightarrow> winding_number g z = 0" and
  4674           finite:"finite pz" and
  4675           poles:"\<forall>p\<in>poles. is_pole f p"
  4676   shows "contour_integral g (\<lambda>x. deriv f x * h x / f x) = 2 * pi * \<i> *
  4677           (\<Sum>p\<in>pz. winding_number g p * h p * zorder f p)"
  4678     (is "?L=?R")
  4679 proof -
  4680   define c where "c \<equiv> 2 * complex_of_real pi * \<i> "
  4681   define ff where "ff \<equiv> (\<lambda>x. deriv f x * h x / f x)"
  4682   define cont where "cont \<equiv> \<lambda>ff p e. (ff has_contour_integral c * zorder f p * h p ) (circlepath p e)"
  4683   define avoid where "avoid \<equiv> \<lambda>p e. \<forall>w\<in>cball p e. w \<in> s \<and> (w \<noteq> p \<longrightarrow> w \<notin> pz)"
  4684 
  4685   have "\<exists>e>0. avoid p e \<and> (p\<in>pz \<longrightarrow> cont ff p e)" when "p\<in>s" for p
  4686   proof -
  4687     obtain e1 where "e1>0" and e1_avoid:"avoid p e1"
  4688       using finite_cball_avoid[OF \<open>open s\<close> finite] \<open>p\<in>s\<close> unfolding avoid_def by auto
  4689     have "\<exists>e2>0. cball p e2 \<subseteq> ball p e1 \<and> cont ff p e2" when "p\<in>pz"
  4690     proof -
  4691       define po where "po \<equiv> zorder f p"
  4692       define pp where "pp \<equiv> zor_poly f p"
  4693       define f' where "f' \<equiv> \<lambda>w. pp w * (w - p) powr po"
  4694       define ff' where "ff' \<equiv> (\<lambda>x. deriv f' x * h x / f' x)"
  4695       obtain r where "pp p\<noteq>0" "r>0" and
  4696           "r<e1" and
  4697           pp_holo:"pp holomorphic_on cball p r" and
  4698           pp_po:"(\<forall>w\<in>cball p r-{p}. f w = pp w * (w - p) powr po \<and> pp w \<noteq> 0)"
  4699       proof -
  4700         have "isolated_singularity_at f p"
  4701         proof -
  4702           have "f holomorphic_on ball p e1 - {p}"
  4703             apply (intro holomorphic_on_subset[OF f_holo])
  4704             using e1_avoid \<open>p\<in>pz\<close> unfolding avoid_def pz_def by force
  4705           then show ?thesis unfolding isolated_singularity_at_def 
  4706             using \<open>e1>0\<close> analytic_on_open open_delete by blast
  4707         qed
  4708         moreover have "not_essential f p"  
  4709         proof (cases "is_pole f p")
  4710           case True
  4711           then show ?thesis unfolding not_essential_def by auto
  4712         next
  4713           case False
  4714           then have "p\<in>s-poles" using \<open>p\<in>s\<close> poles unfolding pz_def by auto
  4715           moreover have "open (s-poles)"
  4716             using \<open>open s\<close> 
  4717             apply (elim open_Diff)
  4718             apply (rule finite_imp_closed)
  4719             using finite unfolding pz_def by simp
  4720           ultimately have "isCont f p"
  4721             using holomorphic_on_imp_continuous_on[OF f_holo] continuous_on_eq_continuous_at
  4722             by auto
  4723           then show ?thesis unfolding isCont_def not_essential_def by auto
  4724         qed  
  4725         moreover have "\<exists>\<^sub>F w in at p. f w \<noteq> 0 "
  4726         proof (rule ccontr)
  4727           assume "\<not> (\<exists>\<^sub>F w in at p. f w \<noteq> 0)"
  4728           then have "\<forall>\<^sub>F w in at p. f w= 0" unfolding frequently_def by auto
  4729           then obtain rr where "rr>0" "\<forall>w\<in>ball p rr - {p}. f w =0"
  4730             unfolding eventually_at by (auto simp add:dist_commute)
  4731           then have "ball p rr - {p} \<subseteq> {w\<in>ball p rr-{p}. f w=0}" by blast
  4732           moreover have "infinite (ball p rr - {p})" using \<open>rr>0\<close> using finite_imp_not_open by fastforce
  4733           ultimately have "infinite {w\<in>ball p rr-{p}. f w=0}" using infinite_super by blast
  4734           then have "infinite pz"
  4735             unfolding pz_def infinite_super by auto
  4736           then show False using \<open>finite pz\<close> by auto
  4737         qed
  4738         ultimately obtain r where "pp p \<noteq> 0" and r:"r>0" "pp holomorphic_on cball p r" 
  4739                   "(\<forall>w\<in>cball p r - {p}. f w = pp w * (w - p) powr of_int po \<and> pp w \<noteq> 0)"
  4740           using zorder_exist[of f p,folded po_def pp_def] by auto
  4741         define r1 where "r1=min r e1 / 2"
  4742         have "r1<e1" unfolding r1_def using \<open>e1>0\<close> \<open>r>0\<close> by auto
  4743         moreover have "r1>0" "pp holomorphic_on cball p r1" 
  4744                   "(\<forall>w\<in>cball p r1 - {p}. f w = pp w * (w - p) powr of_int po \<and> pp w \<noteq> 0)"
  4745           unfolding r1_def using \<open>e1>0\<close> r by auto
  4746         ultimately show ?thesis using that \<open>pp p\<noteq>0\<close> by auto
  4747       qed
  4748       
  4749       define e2 where "e2 \<equiv> r/2"
  4750       have "e2>0" using \<open>r>0\<close> unfolding e2_def by auto
  4751       define anal where "anal \<equiv> \<lambda>w. deriv pp w * h w / pp w"
  4752       define prin where "prin \<equiv> \<lambda>w. po * h w / (w - p)"
  4753       have "((\<lambda>w.  prin w + anal w) has_contour_integral c * po * h p) (circlepath p e2)"
  4754       proof (rule has_contour_integral_add[of _ _ _ _ 0,simplified])
  4755         have "ball p r \<subseteq> s"
  4756           using \<open>r<e1\<close> avoid_def ball_subset_cball e1_avoid by (simp add: subset_eq)
  4757         then have "cball p e2 \<subseteq> s"
  4758           using \<open>r>0\<close> unfolding e2_def by auto
  4759         then have "(\<lambda>w. po * h w) holomorphic_on cball p e2"
  4760           using h_holo by (auto intro!: holomorphic_intros)
  4761         then show "(prin has_contour_integral c * po * h p ) (circlepath p e2)"
  4762           using Cauchy_integral_circlepath_simple[folded c_def, of "\<lambda>w. po * h w"] \<open>e2>0\<close>
  4763           unfolding prin_def by (auto simp add: mult.assoc)
  4764         have "anal holomorphic_on ball p r" unfolding anal_def
  4765           using pp_holo h_holo pp_po \<open>ball p r \<subseteq> s\<close> \<open>pp p\<noteq>0\<close>
  4766           by (auto intro!: holomorphic_intros)
  4767         then show "(anal has_contour_integral 0) (circlepath p e2)"
  4768           using e2_def \<open>r>0\<close>
  4769           by (auto elim!: Cauchy_theorem_disc_simple)
  4770       qed
  4771       then have "cont ff' p e2" unfolding cont_def po_def
  4772       proof (elim has_contour_integral_eq)
  4773         fix w assume "w \<in> path_image (circlepath p e2)"
  4774         then have "w\<in>ball p r" and "w\<noteq>p" unfolding e2_def using \<open>r>0\<close> by auto
  4775         define wp where "wp \<equiv> w-p"
  4776         have "wp\<noteq>0" and "pp w \<noteq>0"
  4777           unfolding wp_def using \<open>w\<noteq>p\<close> \<open>w\<in>ball p r\<close> pp_po by auto
  4778         moreover have der_f':"deriv f' w = po * pp w * (w-p) powr (po - 1) + deriv pp w * (w-p) powr po"
  4779         proof (rule DERIV_imp_deriv)
  4780           have "(pp has_field_derivative (deriv pp w)) (at w)"
  4781             using DERIV_deriv_iff_has_field_derivative pp_holo \<open>w\<noteq>p\<close>
  4782             by (meson open_ball \<open>w \<in> ball p r\<close> ball_subset_cball holomorphic_derivI holomorphic_on_subset)
  4783           then show " (f' has_field_derivative of_int po * pp w * (w - p) powr of_int (po - 1) 
  4784                   + deriv pp w * (w - p) powr of_int po) (at w)"
  4785             unfolding f'_def using \<open>w\<noteq>p\<close>
  4786             apply (auto intro!: derivative_eq_intros(35) DERIV_cong[OF has_field_derivative_powr_of_int])
  4787             by (auto intro: derivative_eq_intros)
  4788         qed
  4789         ultimately show "prin w + anal w = ff' w"
  4790           unfolding ff'_def prin_def anal_def
  4791           apply simp
  4792           apply (unfold f'_def)
  4793           apply (fold wp_def)
  4794           apply (auto simp add:field_simps)
  4795           by (metis (no_types, lifting) diff_add_cancel mult.commute powr_add powr_to_1)
  4796       qed
  4797       then have "cont ff p e2" unfolding cont_def
  4798       proof (elim has_contour_integral_eq)
  4799         fix w assume "w \<in> path_image (circlepath p e2)"
  4800         then have "w\<in>ball p r" and "w\<noteq>p" unfolding e2_def using \<open>r>0\<close> by auto
  4801         have "deriv f' w =  deriv f w"
  4802         proof (rule complex_derivative_transform_within_open[where s="ball p r - {p}"])
  4803           show "f' holomorphic_on ball p r - {p}" unfolding f'_def using pp_holo
  4804             by (auto intro!: holomorphic_intros)
  4805         next
  4806           have "ball p e1 - {p} \<subseteq> s - poles"
  4807             using ball_subset_cball e1_avoid[unfolded avoid_def] unfolding pz_def
  4808             by auto
  4809           then have "ball p r - {p} \<subseteq> s - poles" 
  4810             apply (elim dual_order.trans)
  4811             using \<open>r<e1\<close> by auto
  4812           then show "f holomorphic_on ball p r - {p}" using f_holo
  4813             by auto
  4814         next
  4815           show "open (ball p r - {p})" by auto
  4816           show "w \<in> ball p r - {p}" using \<open>w\<in>ball p r\<close> \<open>w\<noteq>p\<close> by auto
  4817         next
  4818           fix x assume "x \<in> ball p r - {p}"
  4819           then show "f' x = f x"
  4820             using pp_po unfolding f'_def by auto
  4821         qed
  4822         moreover have " f' w  =  f w "
  4823           using \<open>w \<in> ball p r\<close> ball_subset_cball subset_iff pp_po \<open>w\<noteq>p\<close>
  4824           unfolding f'_def by auto
  4825         ultimately show "ff' w = ff w"
  4826           unfolding ff'_def ff_def by simp
  4827       qed
  4828       moreover have "cball p e2 \<subseteq> ball p e1"
  4829         using \<open>0 < r\<close> \<open>r<e1\<close> e2_def by auto
  4830       ultimately show ?thesis using \<open>e2>0\<close> by auto
  4831     qed
  4832     then obtain e2 where e2:"p\<in>pz \<longrightarrow> e2>0 \<and> cball p e2 \<subseteq> ball p e1 \<and> cont ff p e2"
  4833       by auto
  4834     define e4 where "e4 \<equiv> if p\<in>pz then e2 else  e1"
  4835     have "e4>0" using e2 \<open>e1>0\<close> unfolding e4_def by auto
  4836     moreover have "avoid p e4" using e2 \<open>e1>0\<close> e1_avoid unfolding e4_def avoid_def by auto
  4837     moreover have "p\<in>pz \<longrightarrow> cont ff p e4"
  4838       by (auto simp add: e2 e4_def)
  4839     ultimately show ?thesis by auto
  4840   qed
  4841   then obtain get_e where get_e:"\<forall>p\<in>s. get_e p>0 \<and> avoid p (get_e p)
  4842       \<and> (p\<in>pz \<longrightarrow> cont ff p (get_e p))"
  4843     by metis
  4844   define ci where "ci \<equiv> \<lambda>p. contour_integral (circlepath p (get_e p)) ff"
  4845   define w where "w \<equiv> \<lambda>p. winding_number g p"
  4846   have "contour_integral g ff = (\<Sum>p\<in>pz. w p * ci p)" unfolding ci_def w_def
  4847   proof (rule Cauchy_theorem_singularities[OF \<open>open s\<close> \<open>connected s\<close> finite _ \<open>valid_path g\<close> loop
  4848         path_img homo])
  4849     have "open (s - pz)" using open_Diff[OF _ finite_imp_closed[OF finite]] \<open>open s\<close> by auto
  4850     then show "ff holomorphic_on s - pz" unfolding ff_def using f_holo h_holo
  4851       by (auto intro!: holomorphic_intros simp add:pz_def)
  4852   next
  4853     show "\<forall>p\<in>s. 0 < get_e p \<and> (\<forall>w\<in>cball p (get_e p). w \<in> s \<and> (w \<noteq> p \<longrightarrow> w \<notin> pz))"
  4854       using get_e using avoid_def by blast
  4855   qed
  4856   also have "... = (\<Sum>p\<in>pz. c * w p * h p * zorder f p)"
  4857   proof (rule sum.cong[of pz pz,simplified])
  4858     fix p assume "p \<in> pz"
  4859     show "w p * ci p = c * w p * h p * (zorder f p)"
  4860     proof (cases "p\<in>s")
  4861       assume "p \<in> s"
  4862       have "ci p = c * h p * (zorder f p)" unfolding ci_def
  4863         apply (rule contour_integral_unique)
  4864         using get_e \<open>p\<in>s\<close> \<open>p\<in>pz\<close> unfolding cont_def by (metis mult.assoc mult.commute)
  4865       thus ?thesis by auto
  4866     next
  4867       assume "p\<notin>s"
  4868       then have "w p=0" using homo unfolding w_def by auto
  4869       then show ?thesis by auto
  4870     qed
  4871   qed
  4872   also have "... = c*(\<Sum>p\<in>pz. w p * h p * zorder f p)"
  4873     unfolding sum_distrib_left by (simp add:algebra_simps)
  4874   finally have "contour_integral g ff = c * (\<Sum>p\<in>pz. w p * h p * of_int (zorder f p))" .
  4875   then show ?thesis unfolding ff_def c_def w_def by simp
  4876 qed
  4877 
  4878 subsection \<open>Rouche's theorem \<close>
  4879 
  4880 theorem Rouche_theorem:
  4881   fixes f g::"complex \<Rightarrow> complex" and s:: "complex set"
  4882   defines "fg\<equiv>(\<lambda>p. f p + g p)"
  4883   defines "zeros_fg\<equiv>{p. fg p = 0}" and "zeros_f\<equiv>{p. f p = 0}"
  4884   assumes
  4885     "open s" and "connected s" and
  4886     "finite zeros_fg" and
  4887     "finite zeros_f" and
  4888     f_holo:"f holomorphic_on s" and
  4889     g_holo:"g holomorphic_on s" and
  4890     "valid_path \<gamma>" and
  4891     loop:"pathfinish \<gamma> = pathstart \<gamma>" and
  4892     path_img:"path_image \<gamma> \<subseteq> s " and
  4893     path_less:"\<forall>z\<in>path_image \<gamma>. cmod(f z) > cmod(g z)" and
  4894     homo:"\<forall>z. (z \<notin> s) \<longrightarrow> winding_number \<gamma> z = 0"
  4895   shows "(\<Sum>p\<in>zeros_fg. winding_number \<gamma> p * zorder fg p)
  4896           = (\<Sum>p\<in>zeros_f. winding_number \<gamma> p * zorder f p)"
  4897 proof -
  4898   have path_fg:"path_image \<gamma> \<subseteq> s - zeros_fg"
  4899   proof -
  4900     have False when "z\<in>path_image \<gamma>" and "f z + g z=0" for z
  4901     proof -
  4902       have "cmod (f z) > cmod (g z)" using \<open>z\<in>path_image \<gamma>\<close> path_less by auto
  4903       moreover have "f z = - g z"  using \<open>f z + g z =0\<close> by (simp add: eq_neg_iff_add_eq_0)
  4904       then have "cmod (f z) = cmod (g z)" by auto
  4905       ultimately show False by auto
  4906     qed
  4907     then show ?thesis unfolding zeros_fg_def fg_def using path_img by auto
  4908   qed
  4909   have path_f:"path_image \<gamma> \<subseteq> s - zeros_f"
  4910   proof -
  4911     have False when "z\<in>path_image \<gamma>" and "f z =0" for z
  4912     proof -
  4913       have "cmod (g z) < cmod (f z) " using \<open>z\<in>path_image \<gamma>\<close> path_less by auto
  4914       then have "cmod (g z) < 0" using \<open>f z=0\<close> by auto
  4915       then show False by auto
  4916     qed
  4917     then show ?thesis unfolding zeros_f_def using path_img by auto
  4918   qed
  4919   define w where "w \<equiv> \<lambda>p. winding_number \<gamma> p"
  4920   define c where "c \<equiv> 2 * complex_of_real pi * \<i>"
  4921   define h where "h \<equiv> \<lambda>p. g p / f p + 1"
  4922   obtain spikes
  4923     where "finite spikes" and spikes: "\<forall>x\<in>{0..1} - spikes. \<gamma> differentiable at x"
  4924     using \<open>valid_path \<gamma>\<close>
  4925     by (auto simp: valid_path_def piecewise_C1_differentiable_on_def C1_differentiable_on_eq)
  4926   have h_contour:"((\<lambda>x. deriv h x / h x) has_contour_integral 0) \<gamma>"
  4927   proof -
  4928     have outside_img:"0 \<in> outside (path_image (h o \<gamma>))"
  4929     proof -
  4930       have "h p \<in> ball 1 1" when "p\<in>path_image \<gamma>" for p
  4931       proof -
  4932         have "cmod (g p/f p) <1" using path_less[rule_format,OF that]
  4933           apply (cases "cmod (f p) = 0")
  4934           by (auto simp add: norm_divide)
  4935         then show ?thesis unfolding h_def by (auto simp add:dist_complex_def)
  4936       qed
  4937       then have "path_image (h o \<gamma>) \<subseteq> ball 1 1"
  4938         by (simp add: image_subset_iff path_image_compose)
  4939       moreover have " (0::complex) \<notin> ball 1 1" by (simp add: dist_norm)
  4940       ultimately show "?thesis"
  4941         using  convex_in_outside[of "ball 1 1" 0] outside_mono by blast
  4942     qed
  4943     have valid_h:"valid_path (h \<circ> \<gamma>)"
  4944     proof (rule valid_path_compose_holomorphic[OF \<open>valid_path \<gamma>\<close> _ _ path_f])
  4945       show "h holomorphic_on s - zeros_f"
  4946         unfolding h_def using f_holo g_holo
  4947         by (auto intro!: holomorphic_intros simp add:zeros_f_def)
  4948     next
  4949       show "open (s - zeros_f)" using \<open>finite zeros_f\<close> \<open>open s\<close> finite_imp_closed
  4950         by auto
  4951     qed
  4952     have "((\<lambda>z. 1/z) has_contour_integral 0) (h \<circ> \<gamma>)"
  4953     proof -
  4954       have "0 \<notin> path_image (h \<circ> \<gamma>)" using outside_img by (simp add: outside_def)
  4955       then have "((\<lambda>z. 1/z) has_contour_integral c * winding_number (h \<circ> \<gamma>) 0) (h \<circ> \<gamma>)"
  4956         using has_contour_integral_winding_number[of "h o \<gamma>" 0,simplified] valid_h
  4957         unfolding c_def by auto
  4958       moreover have "winding_number (h o \<gamma>) 0 = 0"
  4959       proof -
  4960         have "0 \<in> outside (path_image (h \<circ> \<gamma>))" using outside_img .
  4961         moreover have "path (h o \<gamma>)"
  4962           using valid_h  by (simp add: valid_path_imp_path)
  4963         moreover have "pathfinish (h o \<gamma>) = pathstart (h o \<gamma>)"
  4964           by (simp add: loop pathfinish_compose pathstart_compose)
  4965         ultimately show ?thesis using winding_number_zero_in_outside by auto
  4966       qed
  4967       ultimately show ?thesis by auto
  4968     qed
  4969     moreover have "vector_derivative (h \<circ> \<gamma>) (at x) = vector_derivative \<gamma> (at x) * deriv h (\<gamma> x)"
  4970       when "x\<in>{0..1} - spikes" for x
  4971     proof (rule vector_derivative_chain_at_general)
  4972       show "\<gamma> differentiable at x" using that \<open>valid_path \<gamma>\<close> spikes by auto
  4973     next
  4974       define der where "der \<equiv> \<lambda>p. (deriv g p * f p - g p * deriv f p)/(f p * f p)"
  4975       define t where "t \<equiv> \<gamma> x"
  4976       have "f t\<noteq>0" unfolding zeros_f_def t_def
  4977         by (metis DiffD1 image_eqI norm_not_less_zero norm_zero path_defs(4) path_less that)
  4978       moreover have "t\<in>s"
  4979         using contra_subsetD path_image_def path_fg t_def that by fastforce
  4980       ultimately have "(h has_field_derivative der t) (at t)"
  4981         unfolding h_def der_def using g_holo f_holo \<open>open s\<close>
  4982         by (auto intro!: holomorphic_derivI derivative_eq_intros)
  4983       then show "h field_differentiable at (\<gamma> x)" 
  4984         unfolding t_def field_differentiable_def by blast
  4985     qed
  4986     then have " ((/) 1 has_contour_integral 0) (h \<circ> \<gamma>)
  4987                   = ((\<lambda>x. deriv h x / h x) has_contour_integral 0) \<gamma>"
  4988       unfolding has_contour_integral
  4989       apply (intro has_integral_spike_eq[OF negligible_finite, OF \<open>finite spikes\<close>])
  4990       by auto
  4991     ultimately show ?thesis by auto
  4992   qed
  4993   then have "contour_integral \<gamma> (\<lambda>x. deriv h x / h x) = 0"
  4994     using  contour_integral_unique by simp
  4995   moreover have "contour_integral \<gamma> (\<lambda>x. deriv fg x / fg x) = contour_integral \<gamma> (\<lambda>x. deriv f x / f x)
  4996       + contour_integral \<gamma> (\<lambda>p. deriv h p / h p)"
  4997   proof -
  4998     have "(\<lambda>p. deriv f p / f p) contour_integrable_on \<gamma>"
  4999     proof (rule contour_integrable_holomorphic_simple[OF _ _ \<open>valid_path \<gamma>\<close> path_f])
  5000       show "open (s - zeros_f)" using finite_imp_closed[OF \<open>finite zeros_f\<close>] \<open>open s\<close>
  5001         by auto
  5002       then show "(\<lambda>p. deriv f p / f p) holomorphic_on s - zeros_f"
  5003         using f_holo
  5004         by (auto intro!: holomorphic_intros simp add:zeros_f_def)
  5005     qed
  5006     moreover have "(\<lambda>p. deriv h p / h p) contour_integrable_on \<gamma>"
  5007       using h_contour
  5008       by (simp add: has_contour_integral_integrable)
  5009     ultimately have "contour_integral \<gamma> (\<lambda>x. deriv f x / f x + deriv h x / h x) =
  5010                         contour_integral \<gamma> (\<lambda>p. deriv f p / f p) + contour_integral \<gamma> (\<lambda>p. deriv h p / h p)"
  5011       using contour_integral_add[of "(\<lambda>p. deriv f p / f p)" \<gamma> "(\<lambda>p. deriv h p / h p)" ]
  5012       by auto
  5013     moreover have "deriv fg p / fg p =  deriv f p / f p + deriv h p / h p"
  5014                       when "p\<in> path_image \<gamma>" for p
  5015     proof -
  5016       have "fg p\<noteq>0" and "f p\<noteq>0" using path_f path_fg that unfolding zeros_f_def zeros_fg_def
  5017         by auto
  5018       have "h p\<noteq>0"
  5019       proof (rule ccontr)
  5020         assume "\<not> h p \<noteq> 0"
  5021         then have "g p / f p= -1" unfolding h_def by (simp add: add_eq_0_iff2)
  5022         then have "cmod (g p/f p) = 1" by auto
  5023         moreover have "cmod (g p/f p) <1" using path_less[rule_format,OF that]
  5024           apply (cases "cmod (f p) = 0")
  5025           by (auto simp add: norm_divide)
  5026         ultimately show False by auto
  5027       qed
  5028       have der_fg:"deriv fg p =  deriv f p + deriv g p" unfolding fg_def
  5029         using f_holo g_holo holomorphic_on_imp_differentiable_at[OF _  \<open>open s\<close>] path_img that
  5030         by auto
  5031       have der_h:"deriv h p = (deriv g p * f p - g p * deriv f p)/(f p * f p)"
  5032       proof -
  5033         define der where "der \<equiv> \<lambda>p. (deriv g p * f p - g p * deriv f p)/(f p * f p)"
  5034         have "p\<in>s" using path_img that by auto
  5035         then have "(h has_field_derivative der p) (at p)"
  5036           unfolding h_def der_def using g_holo f_holo \<open>open s\<close> \<open>f p\<noteq>0\<close>
  5037           by (auto intro!: derivative_eq_intros holomorphic_derivI)
  5038         then show ?thesis unfolding der_def using DERIV_imp_deriv by auto
  5039       qed
  5040       show ?thesis
  5041         apply (simp only:der_fg der_h)
  5042         apply (auto simp add:field_simps \<open>h p\<noteq>0\<close> \<open>f p\<noteq>0\<close> \<open>fg p\<noteq>0\<close>)
  5043         by (auto simp add:field_simps h_def \<open>f p\<noteq>0\<close> fg_def)
  5044     qed
  5045     then have "contour_integral \<gamma> (\<lambda>p. deriv fg p / fg p)
  5046                   = contour_integral \<gamma> (\<lambda>p. deriv f p / f p + deriv h p / h p)"
  5047       by (elim contour_integral_eq)
  5048     ultimately show ?thesis by auto
  5049   qed
  5050   moreover have "contour_integral \<gamma> (\<lambda>x. deriv fg x / fg x) = c * (\<Sum>p\<in>zeros_fg. w p * zorder fg p)"
  5051     unfolding c_def zeros_fg_def w_def
  5052   proof (rule argument_principle[OF \<open>open s\<close> \<open>connected s\<close> _ _ \<open>valid_path \<gamma>\<close> loop _ homo
  5053         , of _ "{}" "\<lambda>_. 1",simplified])
  5054     show "fg holomorphic_on s" unfolding fg_def using f_holo g_holo holomorphic_on_add by auto
  5055     show "path_image \<gamma> \<subseteq> s - {p. fg p = 0}" using path_fg unfolding zeros_fg_def .
  5056     show " finite {p. fg p = 0}" using \<open>finite zeros_fg\<close> unfolding zeros_fg_def .
  5057   qed
  5058   moreover have "contour_integral \<gamma> (\<lambda>x. deriv f x / f x) = c * (\<Sum>p\<in>zeros_f. w p * zorder f p)"
  5059     unfolding c_def zeros_f_def w_def
  5060   proof (rule argument_principle[OF \<open>open s\<close> \<open>connected s\<close> _ _ \<open>valid_path \<gamma>\<close> loop _ homo
  5061         , of _ "{}" "\<lambda>_. 1",simplified])
  5062     show "f holomorphic_on s" using f_holo g_holo holomorphic_on_add by auto
  5063     show "path_image \<gamma> \<subseteq> s - {p. f p = 0}" using path_f unfolding zeros_f_def .
  5064     show " finite {p. f p = 0}" using \<open>finite zeros_f\<close> unfolding zeros_f_def .
  5065   qed
  5066   ultimately have " c* (\<Sum>p\<in>zeros_fg. w p * (zorder fg p)) = c* (\<Sum>p\<in>zeros_f. w p * (zorder f p))"
  5067     by auto
  5068   then show ?thesis unfolding c_def using w_def by auto
  5069 qed
  5070 
  5071 end