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