src/HOL/Analysis/Cauchy_Integral_Theorem.thy
 changeset 63627 6ddb43c6b711 parent 63594 bd218a9320b5 child 63918 6bf55e6e0b75
```     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/HOL/Analysis/Cauchy_Integral_Theorem.thy	Mon Aug 08 14:13:14 2016 +0200
1.3 @@ -0,0 +1,7539 @@
1.4 +section \<open>Complex path integrals and Cauchy's integral theorem\<close>
1.5 +
1.6 +text\<open>By John Harrison et al.  Ported from HOL Light by L C Paulson (2015)\<close>
1.7 +
1.8 +theory Cauchy_Integral_Theorem
1.9 +imports Complex_Transcendental Weierstrass_Theorems Ordered_Euclidean_Space
1.10 +begin
1.11 +
1.12 +subsection\<open>Homeomorphisms of arc images\<close>
1.13 +
1.14 +lemma homeomorphism_arc:
1.15 +  fixes g :: "real \<Rightarrow> 'a::t2_space"
1.16 +  assumes "arc g"
1.17 +  obtains h where "homeomorphism {0..1} (path_image g) g h"
1.18 +using assms by (force simp add: arc_def homeomorphism_compact path_def path_image_def)
1.19 +
1.20 +lemma homeomorphic_arc_image_interval:
1.21 +  fixes g :: "real \<Rightarrow> 'a::t2_space" and a::real
1.22 +  assumes "arc g" "a < b"
1.23 +  shows "(path_image g) homeomorphic {a..b}"
1.24 +proof -
1.25 +  have "(path_image g) homeomorphic {0..1::real}"
1.26 +    by (meson assms(1) homeomorphic_def homeomorphic_sym homeomorphism_arc)
1.27 +  also have "... homeomorphic {a..b}"
1.28 +    using assms by (force intro: homeomorphic_closed_intervals_real)
1.29 +  finally show ?thesis .
1.30 +qed
1.31 +
1.32 +lemma homeomorphic_arc_images:
1.33 +  fixes g :: "real \<Rightarrow> 'a::t2_space" and h :: "real \<Rightarrow> 'b::t2_space"
1.34 +  assumes "arc g" "arc h"
1.35 +  shows "(path_image g) homeomorphic (path_image h)"
1.36 +proof -
1.37 +  have "(path_image g) homeomorphic {0..1::real}"
1.38 +    by (meson assms homeomorphic_def homeomorphic_sym homeomorphism_arc)
1.39 +  also have "... homeomorphic (path_image h)"
1.40 +    by (meson assms homeomorphic_def homeomorphism_arc)
1.41 +  finally show ?thesis .
1.42 +qed
1.43 +
1.44 +subsection \<open>Piecewise differentiable functions\<close>
1.45 +
1.46 +definition piecewise_differentiable_on
1.47 +           (infixr "piecewise'_differentiable'_on" 50)
1.48 +  where "f piecewise_differentiable_on i  \<equiv>
1.49 +           continuous_on i f \<and>
1.50 +           (\<exists>s. finite s \<and> (\<forall>x \<in> i - s. f differentiable (at x within i)))"
1.51 +
1.52 +lemma piecewise_differentiable_on_imp_continuous_on:
1.53 +    "f piecewise_differentiable_on s \<Longrightarrow> continuous_on s f"
1.55 +
1.56 +lemma piecewise_differentiable_on_subset:
1.57 +    "f piecewise_differentiable_on s \<Longrightarrow> t \<le> s \<Longrightarrow> f piecewise_differentiable_on t"
1.58 +  using continuous_on_subset
1.59 +  unfolding piecewise_differentiable_on_def
1.60 +  apply safe
1.61 +  apply (blast intro: elim: continuous_on_subset)
1.62 +  by (meson Diff_iff differentiable_within_subset subsetCE)
1.63 +
1.64 +lemma differentiable_on_imp_piecewise_differentiable:
1.65 +  fixes a:: "'a::{linorder_topology,real_normed_vector}"
1.66 +  shows "f differentiable_on {a..b} \<Longrightarrow> f piecewise_differentiable_on {a..b}"
1.67 +  apply (simp add: piecewise_differentiable_on_def differentiable_imp_continuous_on)
1.68 +  apply (rule_tac x="{a,b}" in exI, simp add: differentiable_on_def)
1.69 +  done
1.70 +
1.71 +lemma differentiable_imp_piecewise_differentiable:
1.72 +    "(\<And>x. x \<in> s \<Longrightarrow> f differentiable (at x within s))
1.73 +         \<Longrightarrow> f piecewise_differentiable_on s"
1.74 +by (auto simp: piecewise_differentiable_on_def differentiable_imp_continuous_on differentiable_on_def
1.75 +         intro: differentiable_within_subset)
1.76 +
1.77 +lemma piecewise_differentiable_const [iff]: "(\<lambda>x. z) piecewise_differentiable_on s"
1.78 +  by (simp add: differentiable_imp_piecewise_differentiable)
1.79 +
1.80 +lemma piecewise_differentiable_compose:
1.81 +    "\<lbrakk>f piecewise_differentiable_on s; g piecewise_differentiable_on (f ` s);
1.82 +      \<And>x. finite (s \<inter> f-`{x})\<rbrakk>
1.83 +      \<Longrightarrow> (g o f) piecewise_differentiable_on s"
1.84 +  apply (simp add: piecewise_differentiable_on_def, safe)
1.85 +  apply (blast intro: continuous_on_compose2)
1.86 +  apply (rename_tac A B)
1.87 +  apply (rule_tac x="A \<union> (\<Union>x\<in>B. s \<inter> f-`{x})" in exI)
1.88 +  apply (blast intro: differentiable_chain_within)
1.89 +  done
1.90 +
1.91 +lemma piecewise_differentiable_affine:
1.92 +  fixes m::real
1.93 +  assumes "f piecewise_differentiable_on ((\<lambda>x. m *\<^sub>R x + c) ` s)"
1.94 +  shows "(f o (\<lambda>x. m *\<^sub>R x + c)) piecewise_differentiable_on s"
1.95 +proof (cases "m = 0")
1.96 +  case True
1.97 +  then show ?thesis
1.98 +    unfolding o_def
1.99 +    by (force intro: differentiable_imp_piecewise_differentiable differentiable_const)
1.100 +next
1.101 +  case False
1.102 +  show ?thesis
1.103 +    apply (rule piecewise_differentiable_compose [OF differentiable_imp_piecewise_differentiable])
1.104 +    apply (rule assms derivative_intros | simp add: False vimage_def real_vector_affinity_eq)+
1.105 +    done
1.106 +qed
1.107 +
1.108 +lemma piecewise_differentiable_cases:
1.109 +  fixes c::real
1.110 +  assumes "f piecewise_differentiable_on {a..c}"
1.111 +          "g piecewise_differentiable_on {c..b}"
1.112 +           "a \<le> c" "c \<le> b" "f c = g c"
1.113 +  shows "(\<lambda>x. if x \<le> c then f x else g x) piecewise_differentiable_on {a..b}"
1.114 +proof -
1.115 +  obtain s t where st: "finite s" "finite t"
1.116 +                       "\<forall>x\<in>{a..c} - s. f differentiable at x within {a..c}"
1.117 +                       "\<forall>x\<in>{c..b} - t. g differentiable at x within {c..b}"
1.118 +    using assms
1.119 +    by (auto simp: piecewise_differentiable_on_def)
1.120 +  have finabc: "finite ({a,b,c} \<union> (s \<union> t))"
1.121 +    by (metis \<open>finite s\<close> \<open>finite t\<close> finite_Un finite_insert finite.emptyI)
1.122 +  have "continuous_on {a..c} f" "continuous_on {c..b} g"
1.123 +    using assms piecewise_differentiable_on_def by auto
1.124 +  then have "continuous_on {a..b} (\<lambda>x. if x \<le> c then f x else g x)"
1.125 +    using continuous_on_cases [OF closed_real_atLeastAtMost [of a c],
1.126 +                               OF closed_real_atLeastAtMost [of c b],
1.127 +                               of f g "\<lambda>x. x\<le>c"]  assms
1.128 +    by (force simp: ivl_disj_un_two_touch)
1.129 +  moreover
1.130 +  { fix x
1.131 +    assume x: "x \<in> {a..b} - ({a,b,c} \<union> (s \<union> t))"
1.132 +    have "(\<lambda>x. if x \<le> c then f x else g x) differentiable at x within {a..b}" (is "?diff_fg")
1.133 +    proof (cases x c rule: le_cases)
1.134 +      case le show ?diff_fg
1.135 +        apply (rule differentiable_transform_within [where d = "dist x c" and f = f])
1.136 +        using x le st
1.137 +        apply (simp_all add: dist_real_def)
1.138 +        apply (rule differentiable_at_withinI)
1.139 +        apply (rule differentiable_within_open [where s = "{a<..<c} - s", THEN iffD1], simp_all)
1.140 +        apply (blast intro: open_greaterThanLessThan finite_imp_closed)
1.141 +        apply (force elim!: differentiable_subset)+
1.142 +        done
1.143 +    next
1.144 +      case ge show ?diff_fg
1.145 +        apply (rule differentiable_transform_within [where d = "dist x c" and f = g])
1.146 +        using x ge st
1.147 +        apply (simp_all add: dist_real_def)
1.148 +        apply (rule differentiable_at_withinI)
1.149 +        apply (rule differentiable_within_open [where s = "{c<..<b} - t", THEN iffD1], simp_all)
1.150 +        apply (blast intro: open_greaterThanLessThan finite_imp_closed)
1.151 +        apply (force elim!: differentiable_subset)+
1.152 +        done
1.153 +    qed
1.154 +  }
1.155 +  then have "\<exists>s. finite s \<and>
1.156 +                 (\<forall>x\<in>{a..b} - s. (\<lambda>x. if x \<le> c then f x else g x) differentiable at x within {a..b})"
1.157 +    by (meson finabc)
1.158 +  ultimately show ?thesis
1.159 +    by (simp add: piecewise_differentiable_on_def)
1.160 +qed
1.161 +
1.162 +lemma piecewise_differentiable_neg:
1.163 +    "f piecewise_differentiable_on s \<Longrightarrow> (\<lambda>x. -(f x)) piecewise_differentiable_on s"
1.164 +  by (auto simp: piecewise_differentiable_on_def continuous_on_minus)
1.165 +
1.167 +  assumes "f piecewise_differentiable_on i"
1.168 +          "g piecewise_differentiable_on i"
1.169 +    shows "(\<lambda>x. f x + g x) piecewise_differentiable_on i"
1.170 +proof -
1.171 +  obtain s t where st: "finite s" "finite t"
1.172 +                       "\<forall>x\<in>i - s. f differentiable at x within i"
1.173 +                       "\<forall>x\<in>i - t. g differentiable at x within i"
1.174 +    using assms by (auto simp: piecewise_differentiable_on_def)
1.175 +  then have "finite (s \<union> t) \<and> (\<forall>x\<in>i - (s \<union> t). (\<lambda>x. f x + g x) differentiable at x within i)"
1.176 +    by auto
1.177 +  moreover have "continuous_on i f" "continuous_on i g"
1.178 +    using assms piecewise_differentiable_on_def by auto
1.179 +  ultimately show ?thesis
1.180 +    by (auto simp: piecewise_differentiable_on_def continuous_on_add)
1.181 +qed
1.182 +
1.183 +lemma piecewise_differentiable_diff:
1.184 +    "\<lbrakk>f piecewise_differentiable_on s;  g piecewise_differentiable_on s\<rbrakk>
1.185 +     \<Longrightarrow> (\<lambda>x. f x - g x) piecewise_differentiable_on s"
1.187 +  by (metis piecewise_differentiable_add piecewise_differentiable_neg)
1.188 +
1.189 +lemma continuous_on_joinpaths_D1:
1.190 +    "continuous_on {0..1} (g1 +++ g2) \<Longrightarrow> continuous_on {0..1} g1"
1.191 +  apply (rule continuous_on_eq [of _ "(g1 +++ g2) o (op*(inverse 2))"])
1.192 +  apply (rule continuous_intros | simp)+
1.193 +  apply (auto elim!: continuous_on_subset simp: joinpaths_def)
1.194 +  done
1.195 +
1.196 +lemma continuous_on_joinpaths_D2:
1.197 +    "\<lbrakk>continuous_on {0..1} (g1 +++ g2); pathfinish g1 = pathstart g2\<rbrakk> \<Longrightarrow> continuous_on {0..1} g2"
1.198 +  apply (rule continuous_on_eq [of _ "(g1 +++ g2) o (\<lambda>x. inverse 2*x + 1/2)"])
1.199 +  apply (rule continuous_intros | simp)+
1.200 +  apply (auto elim!: continuous_on_subset simp add: joinpaths_def pathfinish_def pathstart_def Ball_def)
1.201 +  done
1.202 +
1.203 +lemma piecewise_differentiable_D1:
1.204 +    "(g1 +++ g2) piecewise_differentiable_on {0..1} \<Longrightarrow> g1 piecewise_differentiable_on {0..1}"
1.205 +  apply (clarsimp simp add: piecewise_differentiable_on_def dest!: continuous_on_joinpaths_D1)
1.206 +  apply (rule_tac x="insert 1 ((op*2)`s)" in exI)
1.207 +  apply simp
1.208 +  apply (intro ballI)
1.209 +  apply (rule_tac d="dist (x/2) (1/2)" and f = "(g1 +++ g2) o (op*(inverse 2))"
1.210 +       in differentiable_transform_within)
1.211 +  apply (auto simp: dist_real_def joinpaths_def)
1.212 +  apply (rule differentiable_chain_within derivative_intros | simp)+
1.213 +  apply (rule differentiable_subset)
1.214 +  apply (force simp:)+
1.215 +  done
1.216 +
1.217 +lemma piecewise_differentiable_D2:
1.218 +    "\<lbrakk>(g1 +++ g2) piecewise_differentiable_on {0..1}; pathfinish g1 = pathstart g2\<rbrakk>
1.219 +    \<Longrightarrow> g2 piecewise_differentiable_on {0..1}"
1.220 +  apply (clarsimp simp add: piecewise_differentiable_on_def dest!: continuous_on_joinpaths_D2)
1.221 +  apply (rule_tac x="insert 0 ((\<lambda>x. 2*x-1)`s)" in exI)
1.222 +  apply simp
1.223 +  apply (intro ballI)
1.224 +  apply (rule_tac d="dist ((x+1)/2) (1/2)" and f = "(g1 +++ g2) o (\<lambda>x. (x+1)/2)"
1.225 +          in differentiable_transform_within)
1.226 +  apply (auto simp: dist_real_def joinpaths_def abs_if field_simps split: if_split_asm)
1.227 +  apply (rule differentiable_chain_within derivative_intros | simp)+
1.228 +  apply (rule differentiable_subset)
1.229 +  apply (force simp: divide_simps)+
1.230 +  done
1.231 +
1.232 +
1.233 +subsubsection\<open>The concept of continuously differentiable\<close>
1.234 +
1.235 +text \<open>
1.236 +John Harrison writes as follows:
1.237 +
1.238 +``The usual assumption in complex analysis texts is that a path \<open>\<gamma>\<close> should be piecewise
1.239 +continuously differentiable, which ensures that the path integral exists at least for any continuous
1.240 +f, since all piecewise continuous functions are integrable. However, our notion of validity is
1.241 +weaker, just piecewise differentiability... [namely] continuity plus differentiability except on a
1.242 +ﬁnite set ... [Our] underlying theory of integration is the Kurzweil-Henstock theory. In contrast to
1.243 +the Riemann or Lebesgue theory (but in common with a simple notion based on antiderivatives), this
1.244 +can integrate all derivatives.''
1.245 +
1.246 +"Formalizing basic complex analysis." From Insight to Proof: Festschrift in Honour of Andrzej Trybulec.
1.247 +Studies in Logic, Grammar and Rhetoric 10.23 (2007): 151-165.
1.248 +
1.249 +And indeed he does not assume that his derivatives are continuous, but the penalty is unreasonably
1.250 +difficult proofs concerning winding numbers. We need a self-contained and straightforward theorem
1.251 +asserting that all derivatives can be integrated before we can adopt Harrison's choice.\<close>
1.252 +
1.253 +definition C1_differentiable_on :: "(real \<Rightarrow> 'a::real_normed_vector) \<Rightarrow> real set \<Rightarrow> bool"
1.254 +           (infix "C1'_differentiable'_on" 50)
1.255 +  where
1.256 +  "f C1_differentiable_on s \<longleftrightarrow>
1.257 +   (\<exists>D. (\<forall>x \<in> s. (f has_vector_derivative (D x)) (at x)) \<and> continuous_on s D)"
1.258 +
1.259 +lemma C1_differentiable_on_eq:
1.260 +    "f C1_differentiable_on s \<longleftrightarrow>
1.261 +     (\<forall>x \<in> s. f differentiable at x) \<and> continuous_on s (\<lambda>x. vector_derivative f (at x))"
1.262 +  unfolding C1_differentiable_on_def
1.263 +  apply safe
1.264 +  using differentiable_def has_vector_derivative_def apply blast
1.265 +  apply (erule continuous_on_eq)
1.266 +  using vector_derivative_at apply fastforce
1.267 +  using vector_derivative_works apply fastforce
1.268 +  done
1.269 +
1.270 +lemma C1_differentiable_on_subset:
1.271 +  "f C1_differentiable_on t \<Longrightarrow> s \<subseteq> t \<Longrightarrow> f C1_differentiable_on s"
1.272 +  unfolding C1_differentiable_on_def  continuous_on_eq_continuous_within
1.273 +  by (blast intro:  continuous_within_subset)
1.274 +
1.275 +lemma C1_differentiable_compose:
1.276 +    "\<lbrakk>f C1_differentiable_on s; g C1_differentiable_on (f ` s);
1.277 +      \<And>x. finite (s \<inter> f-`{x})\<rbrakk>
1.278 +      \<Longrightarrow> (g o f) C1_differentiable_on s"
1.279 +  apply (simp add: C1_differentiable_on_eq, safe)
1.280 +   using differentiable_chain_at apply blast
1.281 +  apply (rule continuous_on_eq [of _ "\<lambda>x. vector_derivative f (at x) *\<^sub>R vector_derivative g (at (f x))"])
1.282 +   apply (rule Limits.continuous_on_scaleR, assumption)
1.283 +   apply (metis (mono_tags, lifting) continuous_on_eq continuous_at_imp_continuous_on continuous_on_compose differentiable_imp_continuous_within o_def)
1.284 +  by (simp add: vector_derivative_chain_at)
1.285 +
1.286 +lemma C1_diff_imp_diff: "f C1_differentiable_on s \<Longrightarrow> f differentiable_on s"
1.287 +  by (simp add: C1_differentiable_on_eq differentiable_at_imp_differentiable_on)
1.288 +
1.289 +lemma C1_differentiable_on_ident [simp, derivative_intros]: "(\<lambda>x. x) C1_differentiable_on s"
1.290 +  by (auto simp: C1_differentiable_on_eq continuous_on_const)
1.291 +
1.292 +lemma C1_differentiable_on_const [simp, derivative_intros]: "(\<lambda>z. a) C1_differentiable_on s"
1.293 +  by (auto simp: C1_differentiable_on_eq continuous_on_const)
1.294 +
1.296 +  "f C1_differentiable_on s \<Longrightarrow> g C1_differentiable_on s \<Longrightarrow> (\<lambda>x. f x + g x) C1_differentiable_on s"
1.297 +  unfolding C1_differentiable_on_eq  by (auto intro: continuous_intros)
1.298 +
1.299 +lemma C1_differentiable_on_minus [simp, derivative_intros]:
1.300 +  "f C1_differentiable_on s \<Longrightarrow> (\<lambda>x. - f x) C1_differentiable_on s"
1.301 +  unfolding C1_differentiable_on_eq  by (auto intro: continuous_intros)
1.302 +
1.303 +lemma C1_differentiable_on_diff [simp, derivative_intros]:
1.304 +  "f C1_differentiable_on s \<Longrightarrow> g C1_differentiable_on s \<Longrightarrow> (\<lambda>x. f x - g x) C1_differentiable_on s"
1.305 +  unfolding C1_differentiable_on_eq  by (auto intro: continuous_intros)
1.306 +
1.307 +lemma C1_differentiable_on_mult [simp, derivative_intros]:
1.308 +  fixes f g :: "real \<Rightarrow> 'a :: real_normed_algebra"
1.309 +  shows "f C1_differentiable_on s \<Longrightarrow> g C1_differentiable_on s \<Longrightarrow> (\<lambda>x. f x * g x) C1_differentiable_on s"
1.310 +  unfolding C1_differentiable_on_eq
1.311 +  by (auto simp: continuous_on_add continuous_on_mult continuous_at_imp_continuous_on differentiable_imp_continuous_within)
1.312 +
1.313 +lemma C1_differentiable_on_scaleR [simp, derivative_intros]:
1.314 +  "f C1_differentiable_on s \<Longrightarrow> g C1_differentiable_on s \<Longrightarrow> (\<lambda>x. f x *\<^sub>R g x) C1_differentiable_on s"
1.315 +  unfolding C1_differentiable_on_eq
1.316 +  by (rule continuous_intros | simp add: continuous_at_imp_continuous_on differentiable_imp_continuous_within)+
1.317 +
1.318 +
1.319 +definition piecewise_C1_differentiable_on
1.320 +           (infixr "piecewise'_C1'_differentiable'_on" 50)
1.321 +  where "f piecewise_C1_differentiable_on i  \<equiv>
1.322 +           continuous_on i f \<and>
1.323 +           (\<exists>s. finite s \<and> (f C1_differentiable_on (i - s)))"
1.324 +
1.325 +lemma C1_differentiable_imp_piecewise:
1.326 +    "f C1_differentiable_on s \<Longrightarrow> f piecewise_C1_differentiable_on s"
1.327 +  by (auto simp: piecewise_C1_differentiable_on_def C1_differentiable_on_eq continuous_at_imp_continuous_on differentiable_imp_continuous_within)
1.328 +
1.329 +lemma piecewise_C1_imp_differentiable:
1.330 +    "f piecewise_C1_differentiable_on i \<Longrightarrow> f piecewise_differentiable_on i"
1.331 +  by (auto simp: piecewise_C1_differentiable_on_def piecewise_differentiable_on_def
1.332 +           C1_differentiable_on_def differentiable_def has_vector_derivative_def
1.333 +           intro: has_derivative_at_within)
1.334 +
1.335 +lemma piecewise_C1_differentiable_compose:
1.336 +    "\<lbrakk>f piecewise_C1_differentiable_on s; g piecewise_C1_differentiable_on (f ` s);
1.337 +      \<And>x. finite (s \<inter> f-`{x})\<rbrakk>
1.338 +      \<Longrightarrow> (g o f) piecewise_C1_differentiable_on s"
1.339 +  apply (simp add: piecewise_C1_differentiable_on_def, safe)
1.340 +  apply (blast intro: continuous_on_compose2)
1.341 +  apply (rename_tac A B)
1.342 +  apply (rule_tac x="A \<union> (\<Union>x\<in>B. s \<inter> f-`{x})" in exI)
1.343 +  apply (rule conjI, blast)
1.344 +  apply (rule C1_differentiable_compose)
1.345 +  apply (blast intro: C1_differentiable_on_subset)
1.346 +  apply (blast intro: C1_differentiable_on_subset)
1.347 +  by (simp add: Diff_Int_distrib2)
1.348 +
1.349 +lemma piecewise_C1_differentiable_on_subset:
1.350 +    "f piecewise_C1_differentiable_on s \<Longrightarrow> t \<le> s \<Longrightarrow> f piecewise_C1_differentiable_on t"
1.351 +  by (auto simp: piecewise_C1_differentiable_on_def elim!: continuous_on_subset C1_differentiable_on_subset)
1.352 +
1.353 +lemma C1_differentiable_imp_continuous_on:
1.354 +  "f C1_differentiable_on s \<Longrightarrow> continuous_on s f"
1.355 +  unfolding C1_differentiable_on_eq continuous_on_eq_continuous_within
1.356 +  using differentiable_at_withinI differentiable_imp_continuous_within by blast
1.357 +
1.358 +lemma C1_differentiable_on_empty [iff]: "f C1_differentiable_on {}"
1.359 +  unfolding C1_differentiable_on_def
1.360 +  by auto
1.361 +
1.362 +lemma piecewise_C1_differentiable_affine:
1.363 +  fixes m::real
1.364 +  assumes "f piecewise_C1_differentiable_on ((\<lambda>x. m * x + c) ` s)"
1.365 +  shows "(f o (\<lambda>x. m *\<^sub>R x + c)) piecewise_C1_differentiable_on s"
1.366 +proof (cases "m = 0")
1.367 +  case True
1.368 +  then show ?thesis
1.369 +    unfolding o_def by (auto simp: piecewise_C1_differentiable_on_def continuous_on_const)
1.370 +next
1.371 +  case False
1.372 +  show ?thesis
1.373 +    apply (rule piecewise_C1_differentiable_compose [OF C1_differentiable_imp_piecewise])
1.374 +    apply (rule assms derivative_intros | simp add: False vimage_def)+
1.375 +    using real_vector_affinity_eq [OF False, where c=c, unfolded scaleR_conv_of_real]
1.376 +    apply simp
1.377 +    done
1.378 +qed
1.379 +
1.380 +lemma piecewise_C1_differentiable_cases:
1.381 +  fixes c::real
1.382 +  assumes "f piecewise_C1_differentiable_on {a..c}"
1.383 +          "g piecewise_C1_differentiable_on {c..b}"
1.384 +           "a \<le> c" "c \<le> b" "f c = g c"
1.385 +  shows "(\<lambda>x. if x \<le> c then f x else g x) piecewise_C1_differentiable_on {a..b}"
1.386 +proof -
1.387 +  obtain s t where st: "f C1_differentiable_on ({a..c} - s)"
1.388 +                       "g C1_differentiable_on ({c..b} - t)"
1.389 +                       "finite s" "finite t"
1.390 +    using assms
1.391 +    by (force simp: piecewise_C1_differentiable_on_def)
1.392 +  then have f_diff: "f differentiable_on {a..<c} - s"
1.393 +        and g_diff: "g differentiable_on {c<..b} - t"
1.394 +    by (simp_all add: C1_differentiable_on_eq differentiable_at_withinI differentiable_on_def)
1.395 +  have "continuous_on {a..c} f" "continuous_on {c..b} g"
1.396 +    using assms piecewise_C1_differentiable_on_def by auto
1.397 +  then have cab: "continuous_on {a..b} (\<lambda>x. if x \<le> c then f x else g x)"
1.398 +    using continuous_on_cases [OF closed_real_atLeastAtMost [of a c],
1.399 +                               OF closed_real_atLeastAtMost [of c b],
1.400 +                               of f g "\<lambda>x. x\<le>c"]  assms
1.401 +    by (force simp: ivl_disj_un_two_touch)
1.402 +  { fix x
1.403 +    assume x: "x \<in> {a..b} - insert c (s \<union> t)"
1.404 +    have "(\<lambda>x. if x \<le> c then f x else g x) differentiable at x" (is "?diff_fg")
1.405 +    proof (cases x c rule: le_cases)
1.406 +      case le show ?diff_fg
1.407 +        apply (rule differentiable_transform_within [where f=f and d = "dist x c"])
1.408 +        using x dist_real_def le st by (auto simp: C1_differentiable_on_eq)
1.409 +    next
1.410 +      case ge show ?diff_fg
1.411 +        apply (rule differentiable_transform_within [where f=g and d = "dist x c"])
1.412 +        using dist_nz x dist_real_def ge st x by (auto simp: C1_differentiable_on_eq)
1.413 +    qed
1.414 +  }
1.415 +  then have "(\<forall>x \<in> {a..b} - insert c (s \<union> t). (\<lambda>x. if x \<le> c then f x else g x) differentiable at x)"
1.416 +    by auto
1.417 +  moreover
1.418 +  { assume fcon: "continuous_on ({a<..<c} - s) (\<lambda>x. vector_derivative f (at x))"
1.419 +       and gcon: "continuous_on ({c<..<b} - t) (\<lambda>x. vector_derivative g (at x))"
1.420 +    have "open ({a<..<c} - s)"  "open ({c<..<b} - t)"
1.421 +      using st by (simp_all add: open_Diff finite_imp_closed)
1.422 +    moreover have "continuous_on ({a<..<c} - s) (\<lambda>x. vector_derivative (\<lambda>x. if x \<le> c then f x else g x) (at x))"
1.423 +      apply (rule continuous_on_eq [OF fcon])
1.425 +      apply (rule vector_derivative_at [symmetric])
1.426 +      apply (rule_tac f=f and d="dist x c" in has_vector_derivative_transform_within)
1.427 +      apply (simp_all add: dist_norm vector_derivative_works [symmetric])
1.428 +      apply (metis (full_types) C1_differentiable_on_eq Diff_iff Groups.add_ac(2) add_mono_thms_linordered_field(5) atLeastAtMost_iff linorder_not_le order_less_irrefl st(1))
1.429 +      apply auto
1.430 +      done
1.431 +    moreover have "continuous_on ({c<..<b} - t) (\<lambda>x. vector_derivative (\<lambda>x. if x \<le> c then f x else g x) (at x))"
1.432 +      apply (rule continuous_on_eq [OF gcon])
1.434 +      apply (rule vector_derivative_at [symmetric])
1.435 +      apply (rule_tac f=g and d="dist x c" in has_vector_derivative_transform_within)
1.436 +      apply (simp_all add: dist_norm vector_derivative_works [symmetric])
1.437 +      apply (metis (full_types) C1_differentiable_on_eq Diff_iff Groups.add_ac(2) add_mono_thms_linordered_field(5) atLeastAtMost_iff less_irrefl not_le st(2))
1.438 +      apply auto
1.439 +      done
1.440 +    ultimately have "continuous_on ({a<..<b} - insert c (s \<union> t))
1.441 +        (\<lambda>x. vector_derivative (\<lambda>x. if x \<le> c then f x else g x) (at x))"
1.442 +      apply (rule continuous_on_subset [OF continuous_on_open_Un], auto)
1.443 +      done
1.444 +  } note * = this
1.445 +  have "continuous_on ({a<..<b} - insert c (s \<union> t)) (\<lambda>x. vector_derivative (\<lambda>x. if x \<le> c then f x else g x) (at x))"
1.446 +    using st
1.447 +    by (auto simp: C1_differentiable_on_eq elim!: continuous_on_subset intro: *)
1.448 +  ultimately have "\<exists>s. finite s \<and> ((\<lambda>x. if x \<le> c then f x else g x) C1_differentiable_on {a..b} - s)"
1.449 +    apply (rule_tac x="{a,b,c} \<union> s \<union> t" in exI)
1.450 +    using st  by (auto simp: C1_differentiable_on_eq elim!: continuous_on_subset)
1.451 +  with cab show ?thesis
1.452 +    by (simp add: piecewise_C1_differentiable_on_def)
1.453 +qed
1.454 +
1.455 +lemma piecewise_C1_differentiable_neg:
1.456 +    "f piecewise_C1_differentiable_on s \<Longrightarrow> (\<lambda>x. -(f x)) piecewise_C1_differentiable_on s"
1.457 +  unfolding piecewise_C1_differentiable_on_def
1.458 +  by (auto intro!: continuous_on_minus C1_differentiable_on_minus)
1.459 +
1.461 +  assumes "f piecewise_C1_differentiable_on i"
1.462 +          "g piecewise_C1_differentiable_on i"
1.463 +    shows "(\<lambda>x. f x + g x) piecewise_C1_differentiable_on i"
1.464 +proof -
1.465 +  obtain s t where st: "finite s" "finite t"
1.466 +                       "f C1_differentiable_on (i-s)"
1.467 +                       "g C1_differentiable_on (i-t)"
1.468 +    using assms by (auto simp: piecewise_C1_differentiable_on_def)
1.469 +  then have "finite (s \<union> t) \<and> (\<lambda>x. f x + g x) C1_differentiable_on i - (s \<union> t)"
1.470 +    by (auto intro: C1_differentiable_on_add elim!: C1_differentiable_on_subset)
1.471 +  moreover have "continuous_on i f" "continuous_on i g"
1.472 +    using assms piecewise_C1_differentiable_on_def by auto
1.473 +  ultimately show ?thesis
1.474 +    by (auto simp: piecewise_C1_differentiable_on_def continuous_on_add)
1.475 +qed
1.476 +
1.477 +lemma piecewise_C1_differentiable_diff:
1.478 +    "\<lbrakk>f piecewise_C1_differentiable_on s;  g piecewise_C1_differentiable_on s\<rbrakk>
1.479 +     \<Longrightarrow> (\<lambda>x. f x - g x) piecewise_C1_differentiable_on s"
1.481 +  by (metis piecewise_C1_differentiable_add piecewise_C1_differentiable_neg)
1.482 +
1.483 +lemma piecewise_C1_differentiable_D1:
1.484 +  fixes g1 :: "real \<Rightarrow> 'a::real_normed_field"
1.485 +  assumes "(g1 +++ g2) piecewise_C1_differentiable_on {0..1}"
1.486 +    shows "g1 piecewise_C1_differentiable_on {0..1}"
1.487 +proof -
1.488 +  obtain s where "finite s"
1.489 +             and co12: "continuous_on ({0..1} - s) (\<lambda>x. vector_derivative (g1 +++ g2) (at x))"
1.490 +             and g12D: "\<forall>x\<in>{0..1} - s. g1 +++ g2 differentiable at x"
1.491 +    using assms  by (auto simp: piecewise_C1_differentiable_on_def C1_differentiable_on_eq)
1.492 +  then have g1D: "g1 differentiable at x" if "x \<in> {0..1} - insert 1 (op * 2 ` s)" for x
1.493 +    apply (rule_tac d="dist (x/2) (1/2)" and f = "(g1 +++ g2) o (op*(inverse 2))" in differentiable_transform_within)
1.494 +    using that
1.495 +    apply (simp_all add: dist_real_def joinpaths_def)
1.496 +    apply (rule differentiable_chain_at derivative_intros | force)+
1.497 +    done
1.498 +  have [simp]: "vector_derivative (g1 \<circ> op * 2) (at (x/2)) = 2 *\<^sub>R vector_derivative g1 (at x)"
1.499 +               if "x \<in> {0..1} - insert 1 (op * 2 ` s)" for x
1.500 +    apply (subst vector_derivative_chain_at)
1.501 +    using that
1.502 +    apply (rule derivative_eq_intros g1D | simp)+
1.503 +    done
1.504 +  have "continuous_on ({0..1/2} - insert (1/2) s) (\<lambda>x. vector_derivative (g1 +++ g2) (at x))"
1.505 +    using co12 by (rule continuous_on_subset) force
1.506 +  then have coDhalf: "continuous_on ({0..1/2} - insert (1/2) s) (\<lambda>x. vector_derivative (g1 o op*2) (at x))"
1.507 +    apply (rule continuous_on_eq [OF _ vector_derivative_at])
1.508 +    apply (rule_tac f="g1 o op*2" and d="dist x (1/2)" in has_vector_derivative_transform_within)
1.509 +    apply (simp_all add: dist_norm joinpaths_def vector_derivative_works [symmetric])
1.510 +    apply (force intro: g1D differentiable_chain_at)
1.511 +    apply auto
1.512 +    done
1.513 +  have "continuous_on ({0..1} - insert 1 (op * 2 ` s))
1.514 +                      ((\<lambda>x. 1/2 * vector_derivative (g1 o op*2) (at x)) o op*(1/2))"
1.515 +    apply (rule continuous_intros)+
1.516 +    using coDhalf
1.517 +    apply (simp add: scaleR_conv_of_real image_set_diff image_image)
1.518 +    done
1.519 +  then have con_g1: "continuous_on ({0..1} - insert 1 (op * 2 ` s)) (\<lambda>x. vector_derivative g1 (at x))"
1.520 +    by (rule continuous_on_eq) (simp add: scaleR_conv_of_real)
1.521 +  have "continuous_on {0..1} g1"
1.522 +    using continuous_on_joinpaths_D1 assms piecewise_C1_differentiable_on_def by blast
1.523 +  with \<open>finite s\<close> show ?thesis
1.524 +    apply (clarsimp simp add: piecewise_C1_differentiable_on_def C1_differentiable_on_eq)
1.525 +    apply (rule_tac x="insert 1 ((op*2)`s)" in exI)
1.526 +    apply (simp add: g1D con_g1)
1.527 +  done
1.528 +qed
1.529 +
1.530 +lemma piecewise_C1_differentiable_D2:
1.531 +  fixes g2 :: "real \<Rightarrow> 'a::real_normed_field"
1.532 +  assumes "(g1 +++ g2) piecewise_C1_differentiable_on {0..1}" "pathfinish g1 = pathstart g2"
1.533 +    shows "g2 piecewise_C1_differentiable_on {0..1}"
1.534 +proof -
1.535 +  obtain s where "finite s"
1.536 +             and co12: "continuous_on ({0..1} - s) (\<lambda>x. vector_derivative (g1 +++ g2) (at x))"
1.537 +             and g12D: "\<forall>x\<in>{0..1} - s. g1 +++ g2 differentiable at x"
1.538 +    using assms  by (auto simp: piecewise_C1_differentiable_on_def C1_differentiable_on_eq)
1.539 +  then have g2D: "g2 differentiable at x" if "x \<in> {0..1} - insert 0 ((\<lambda>x. 2*x-1) ` s)" for x
1.540 +    apply (rule_tac d="dist ((x+1)/2) (1/2)" and f = "(g1 +++ g2) o (\<lambda>x. (x+1)/2)" in differentiable_transform_within)
1.541 +    using that
1.542 +    apply (simp_all add: dist_real_def joinpaths_def)
1.543 +    apply (auto simp: dist_real_def joinpaths_def field_simps)
1.544 +    apply (rule differentiable_chain_at derivative_intros | force)+
1.545 +    apply (drule_tac x= "(x + 1) / 2" in bspec, force simp: divide_simps)
1.546 +    apply assumption
1.547 +    done
1.548 +  have [simp]: "vector_derivative (g2 \<circ> (\<lambda>x. 2*x-1)) (at ((x+1)/2)) = 2 *\<^sub>R vector_derivative g2 (at x)"
1.549 +               if "x \<in> {0..1} - insert 0 ((\<lambda>x. 2*x-1) ` s)" for x
1.550 +    using that  by (auto simp: vector_derivative_chain_at divide_simps g2D)
1.551 +  have "continuous_on ({1/2..1} - insert (1/2) s) (\<lambda>x. vector_derivative (g1 +++ g2) (at x))"
1.552 +    using co12 by (rule continuous_on_subset) force
1.553 +  then have coDhalf: "continuous_on ({1/2..1} - insert (1/2) s) (\<lambda>x. vector_derivative (g2 o (\<lambda>x. 2*x-1)) (at x))"
1.554 +    apply (rule continuous_on_eq [OF _ vector_derivative_at])
1.555 +    apply (rule_tac f="g2 o (\<lambda>x. 2*x-1)" and d="dist (3/4) ((x+1)/2)" in has_vector_derivative_transform_within)
1.556 +    apply (auto simp: dist_real_def field_simps joinpaths_def vector_derivative_works [symmetric]
1.557 +                intro!: g2D differentiable_chain_at)
1.558 +    done
1.559 +  have [simp]: "((\<lambda>x. (x + 1) / 2) ` ({0..1} - insert 0 ((\<lambda>x. 2 * x - 1) ` s))) = ({1/2..1} - insert (1/2) s)"
1.560 +    apply (simp add: image_set_diff inj_on_def image_image)
1.561 +    apply (auto simp: image_affinity_atLeastAtMost_div add_divide_distrib)
1.562 +    done
1.563 +  have "continuous_on ({0..1} - insert 0 ((\<lambda>x. 2*x-1) ` s))
1.564 +                      ((\<lambda>x. 1/2 * vector_derivative (g2 \<circ> (\<lambda>x. 2*x-1)) (at x)) o (\<lambda>x. (x+1)/2))"
1.565 +    by (rule continuous_intros | simp add:  coDhalf)+
1.566 +  then have con_g2: "continuous_on ({0..1} - insert 0 ((\<lambda>x. 2*x-1) ` s)) (\<lambda>x. vector_derivative g2 (at x))"
1.567 +    by (rule continuous_on_eq) (simp add: scaleR_conv_of_real)
1.568 +  have "continuous_on {0..1} g2"
1.569 +    using continuous_on_joinpaths_D2 assms piecewise_C1_differentiable_on_def by blast
1.570 +  with \<open>finite s\<close> show ?thesis
1.571 +    apply (clarsimp simp add: piecewise_C1_differentiable_on_def C1_differentiable_on_eq)
1.572 +    apply (rule_tac x="insert 0 ((\<lambda>x. 2 * x - 1) ` s)" in exI)
1.573 +    apply (simp add: g2D con_g2)
1.574 +  done
1.575 +qed
1.576 +
1.577 +subsection \<open>Valid paths, and their start and finish\<close>
1.578 +
1.579 +lemma Diff_Un_eq: "A - (B \<union> C) = A - B - C"
1.580 +  by blast
1.581 +
1.582 +definition valid_path :: "(real \<Rightarrow> 'a :: real_normed_vector) \<Rightarrow> bool"
1.583 +  where "valid_path f \<equiv> f piecewise_C1_differentiable_on {0..1::real}"
1.584 +
1.585 +definition closed_path :: "(real \<Rightarrow> 'a :: real_normed_vector) \<Rightarrow> bool"
1.586 +  where "closed_path g \<equiv> g 0 = g 1"
1.587 +
1.588 +subsubsection\<open>In particular, all results for paths apply\<close>
1.589 +
1.590 +lemma valid_path_imp_path: "valid_path g \<Longrightarrow> path g"
1.591 +by (simp add: path_def piecewise_C1_differentiable_on_def valid_path_def)
1.592 +
1.593 +lemma connected_valid_path_image: "valid_path g \<Longrightarrow> connected(path_image g)"
1.594 +  by (metis connected_path_image valid_path_imp_path)
1.595 +
1.596 +lemma compact_valid_path_image: "valid_path g \<Longrightarrow> compact(path_image g)"
1.597 +  by (metis compact_path_image valid_path_imp_path)
1.598 +
1.599 +lemma bounded_valid_path_image: "valid_path g \<Longrightarrow> bounded(path_image g)"
1.600 +  by (metis bounded_path_image valid_path_imp_path)
1.601 +
1.602 +lemma closed_valid_path_image: "valid_path g \<Longrightarrow> closed(path_image g)"
1.603 +  by (metis closed_path_image valid_path_imp_path)
1.604 +
1.605 +proposition valid_path_compose:
1.606 +  assumes "valid_path g"
1.607 +      and der: "\<And>x. x \<in> path_image g \<Longrightarrow> \<exists>f'. (f has_field_derivative f') (at x)"
1.608 +      and con: "continuous_on (path_image g) (deriv f)"
1.609 +    shows "valid_path (f o g)"
1.610 +proof -
1.611 +  obtain s where "finite s" and g_diff: "g C1_differentiable_on {0..1} - s"
1.612 +    using \<open>valid_path g\<close> unfolding valid_path_def piecewise_C1_differentiable_on_def by auto
1.613 +  have "f \<circ> g differentiable at t" when "t\<in>{0..1} - s" for t
1.614 +    proof (rule differentiable_chain_at)
1.615 +      show "g differentiable at t" using \<open>valid_path g\<close>
1.616 +        by (meson C1_differentiable_on_eq \<open>g C1_differentiable_on {0..1} - s\<close> that)
1.617 +    next
1.618 +      have "g t\<in>path_image g" using that DiffD1 image_eqI path_image_def by metis
1.619 +      then obtain f' where "(f has_field_derivative f') (at (g t))"
1.620 +        using der by auto
1.621 +      then have " (f has_derivative op * f') (at (g t))"
1.622 +        using has_field_derivative_imp_has_derivative[of f f' "at (g t)"] by auto
1.623 +      then show "f differentiable at (g t)" using differentiableI by auto
1.624 +    qed
1.625 +  moreover have "continuous_on ({0..1} - s) (\<lambda>x. vector_derivative (f \<circ> g) (at x))"
1.626 +    proof (rule continuous_on_eq [where f = "\<lambda>x. vector_derivative g (at x) * deriv f (g x)"],
1.627 +        rule continuous_intros)
1.628 +      show "continuous_on ({0..1} - s) (\<lambda>x. vector_derivative g (at x))"
1.629 +        using g_diff C1_differentiable_on_eq by auto
1.630 +    next
1.631 +      have "continuous_on {0..1} (\<lambda>x. deriv f (g x))"
1.632 +        using continuous_on_compose[OF _ con[unfolded path_image_def],unfolded comp_def]
1.633 +          \<open>valid_path g\<close> piecewise_C1_differentiable_on_def valid_path_def
1.634 +        by blast
1.635 +      then show "continuous_on ({0..1} - s) (\<lambda>x. deriv f (g x))"
1.636 +        using continuous_on_subset by blast
1.637 +    next
1.638 +      show "vector_derivative g (at t) * deriv f (g t) = vector_derivative (f \<circ> g) (at t)"
1.639 +          when "t \<in> {0..1} - s" for t
1.640 +        proof (rule vector_derivative_chain_at_general[symmetric])
1.641 +          show "g differentiable at t" by (meson C1_differentiable_on_eq g_diff that)
1.642 +        next
1.643 +          have "g t\<in>path_image g" using that DiffD1 image_eqI path_image_def by metis
1.644 +          then obtain f' where "(f has_field_derivative f') (at (g t))"
1.645 +            using der by auto
1.646 +          then show "\<exists>g'. (f has_field_derivative g') (at (g t))" by auto
1.647 +        qed
1.648 +    qed
1.649 +  ultimately have "f o g C1_differentiable_on {0..1} - s"
1.650 +    using C1_differentiable_on_eq by blast
1.651 +  moreover have "path (f o g)"
1.652 +    proof -
1.653 +      have "isCont f x" when "x\<in>path_image g" for x
1.654 +        proof -
1.655 +          obtain f' where "(f has_field_derivative f') (at x)"
1.656 +            using der[rule_format] \<open>x\<in>path_image g\<close> by auto
1.657 +          thus ?thesis using DERIV_isCont by auto
1.658 +        qed
1.659 +      then have "continuous_on (path_image g) f" using continuous_at_imp_continuous_on by auto
1.660 +      then show ?thesis using path_continuous_image \<open>valid_path g\<close> valid_path_imp_path by auto
1.661 +    qed
1.662 +  ultimately show ?thesis unfolding valid_path_def piecewise_C1_differentiable_on_def path_def
1.663 +    using \<open>finite s\<close> by auto
1.664 +qed
1.665 +
1.666 +
1.667 +subsection\<open>Contour Integrals along a path\<close>
1.668 +
1.669 +text\<open>This definition is for complex numbers only, and does not generalise to line integrals in a vector field\<close>
1.670 +
1.671 +text\<open>piecewise differentiable function on [0,1]\<close>
1.672 +
1.673 +definition has_contour_integral :: "(complex \<Rightarrow> complex) \<Rightarrow> complex \<Rightarrow> (real \<Rightarrow> complex) \<Rightarrow> bool"
1.674 +           (infixr "has'_contour'_integral" 50)
1.675 +  where "(f has_contour_integral i) g \<equiv>
1.676 +           ((\<lambda>x. f(g x) * vector_derivative g (at x within {0..1}))
1.677 +            has_integral i) {0..1}"
1.678 +
1.679 +definition contour_integrable_on
1.680 +           (infixr "contour'_integrable'_on" 50)
1.681 +  where "f contour_integrable_on g \<equiv> \<exists>i. (f has_contour_integral i) g"
1.682 +
1.683 +definition contour_integral
1.684 +  where "contour_integral g f \<equiv> @i. (f has_contour_integral i) g \<or> ~ f contour_integrable_on g \<and> i=0"
1.685 +
1.686 +lemma not_integrable_contour_integral: "~ f contour_integrable_on g \<Longrightarrow> contour_integral g f = 0"
1.687 +  unfolding contour_integrable_on_def contour_integral_def by blast
1.688 +
1.689 +lemma contour_integral_unique: "(f has_contour_integral i) g \<Longrightarrow> contour_integral g f = i"
1.690 +  apply (simp add: contour_integral_def has_contour_integral_def contour_integrable_on_def)
1.691 +  using has_integral_unique by blast
1.692 +
1.693 +corollary has_contour_integral_eqpath:
1.694 +     "\<lbrakk>(f has_contour_integral y) p; f contour_integrable_on \<gamma>;
1.695 +       contour_integral p f = contour_integral \<gamma> f\<rbrakk>
1.696 +      \<Longrightarrow> (f has_contour_integral y) \<gamma>"
1.697 +using contour_integrable_on_def contour_integral_unique by auto
1.698 +
1.699 +lemma has_contour_integral_integral:
1.700 +    "f contour_integrable_on i \<Longrightarrow> (f has_contour_integral (contour_integral i f)) i"
1.701 +  by (metis contour_integral_unique contour_integrable_on_def)
1.702 +
1.703 +lemma has_contour_integral_unique:
1.704 +    "(f has_contour_integral i) g \<Longrightarrow> (f has_contour_integral j) g \<Longrightarrow> i = j"
1.705 +  using has_integral_unique
1.706 +  by (auto simp: has_contour_integral_def)
1.707 +
1.708 +lemma has_contour_integral_integrable: "(f has_contour_integral i) g \<Longrightarrow> f contour_integrable_on g"
1.709 +  using contour_integrable_on_def by blast
1.710 +
1.711 +(* Show that we can forget about the localized derivative.*)
1.712 +
1.713 +lemma vector_derivative_within_interior:
1.714 +     "\<lbrakk>x \<in> interior s; NO_MATCH UNIV s\<rbrakk>
1.715 +      \<Longrightarrow> vector_derivative f (at x within s) = vector_derivative f (at x)"
1.716 +  apply (simp add: vector_derivative_def has_vector_derivative_def has_derivative_def netlimit_within_interior)
1.717 +  apply (subst lim_within_interior, auto)
1.718 +  done
1.719 +
1.720 +lemma has_integral_localized_vector_derivative:
1.721 +    "((\<lambda>x. f (g x) * vector_derivative g (at x within {a..b})) has_integral i) {a..b} \<longleftrightarrow>
1.722 +     ((\<lambda>x. f (g x) * vector_derivative g (at x)) has_integral i) {a..b}"
1.723 +proof -
1.724 +  have "{a..b} - {a,b} = interior {a..b}"
1.725 +    by (simp add: atLeastAtMost_diff_ends)
1.726 +  show ?thesis
1.727 +    apply (rule has_integral_spike_eq [of "{a,b}"])
1.728 +    apply (auto simp: vector_derivative_within_interior)
1.729 +    done
1.730 +qed
1.731 +
1.732 +lemma integrable_on_localized_vector_derivative:
1.733 +    "(\<lambda>x. f (g x) * vector_derivative g (at x within {a..b})) integrable_on {a..b} \<longleftrightarrow>
1.734 +     (\<lambda>x. f (g x) * vector_derivative g (at x)) integrable_on {a..b}"
1.735 +  by (simp add: integrable_on_def has_integral_localized_vector_derivative)
1.736 +
1.737 +lemma has_contour_integral:
1.738 +     "(f has_contour_integral i) g \<longleftrightarrow>
1.739 +      ((\<lambda>x. f (g x) * vector_derivative g (at x)) has_integral i) {0..1}"
1.740 +  by (simp add: has_integral_localized_vector_derivative has_contour_integral_def)
1.741 +
1.742 +lemma contour_integrable_on:
1.743 +     "f contour_integrable_on g \<longleftrightarrow>
1.744 +      (\<lambda>t. f(g t) * vector_derivative g (at t)) integrable_on {0..1}"
1.745 +  by (simp add: has_contour_integral integrable_on_def contour_integrable_on_def)
1.746 +
1.747 +subsection\<open>Reversing a path\<close>
1.748 +
1.749 +lemma valid_path_imp_reverse:
1.750 +  assumes "valid_path g"
1.751 +    shows "valid_path(reversepath g)"
1.752 +proof -
1.753 +  obtain s where "finite s" "g C1_differentiable_on ({0..1} - s)"
1.754 +    using assms by (auto simp: valid_path_def piecewise_C1_differentiable_on_def)
1.755 +  then have "finite (op - 1 ` s)" "(reversepath g C1_differentiable_on ({0..1} - op - 1 ` s))"
1.756 +    apply (auto simp: reversepath_def)
1.757 +    apply (rule C1_differentiable_compose [of "\<lambda>x::real. 1-x" _ g, unfolded o_def])
1.758 +    apply (auto simp: C1_differentiable_on_eq)
1.759 +    apply (rule continuous_intros, force)
1.760 +    apply (force elim!: continuous_on_subset)
1.761 +    apply (simp add: finite_vimageI inj_on_def)
1.762 +    done
1.763 +  then show ?thesis using assms
1.764 +    by (auto simp: valid_path_def piecewise_C1_differentiable_on_def path_def [symmetric])
1.765 +qed
1.766 +
1.767 +lemma valid_path_reversepath [simp]: "valid_path(reversepath g) \<longleftrightarrow> valid_path g"
1.768 +  using valid_path_imp_reverse by force
1.769 +
1.770 +lemma has_contour_integral_reversepath:
1.771 +  assumes "valid_path g" "(f has_contour_integral i) g"
1.772 +    shows "(f has_contour_integral (-i)) (reversepath g)"
1.773 +proof -
1.774 +  { fix s x
1.775 +    assume xs: "g C1_differentiable_on ({0..1} - s)" "x \<notin> op - 1 ` s" "0 \<le> x" "x \<le> 1"
1.776 +      have "vector_derivative (\<lambda>x. g (1 - x)) (at x within {0..1}) =
1.777 +            - vector_derivative g (at (1 - x) within {0..1})"
1.778 +      proof -
1.779 +        obtain f' where f': "(g has_vector_derivative f') (at (1 - x))"
1.780 +          using xs
1.781 +          by (force simp: has_vector_derivative_def C1_differentiable_on_def)
1.782 +        have "(g o (\<lambda>x. 1 - x) has_vector_derivative -1 *\<^sub>R f') (at x)"
1.783 +          apply (rule vector_diff_chain_within)
1.784 +          apply (intro vector_diff_chain_within derivative_eq_intros | simp)+
1.785 +          apply (rule has_vector_derivative_at_within [OF f'])
1.786 +          done
1.787 +        then have mf': "((\<lambda>x. g (1 - x)) has_vector_derivative -f') (at x)"
1.788 +          by (simp add: o_def)
1.789 +        show ?thesis
1.790 +          using xs
1.791 +          by (auto simp: vector_derivative_at_within_ivl [OF mf'] vector_derivative_at_within_ivl [OF f'])
1.792 +      qed
1.793 +  } note * = this
1.794 +  have 01: "{0..1::real} = cbox 0 1"
1.795 +    by simp
1.796 +  show ?thesis using assms
1.797 +    apply (auto simp: has_contour_integral_def)
1.798 +    apply (drule has_integral_affinity01 [where m= "-1" and c=1])
1.799 +    apply (auto simp: reversepath_def valid_path_def piecewise_C1_differentiable_on_def)
1.800 +    apply (drule has_integral_neg)
1.801 +    apply (rule_tac s = "(\<lambda>x. 1 - x) ` s" in has_integral_spike_finite)
1.802 +    apply (auto simp: *)
1.803 +    done
1.804 +qed
1.805 +
1.806 +lemma contour_integrable_reversepath:
1.807 +    "valid_path g \<Longrightarrow> f contour_integrable_on g \<Longrightarrow> f contour_integrable_on (reversepath g)"
1.808 +  using has_contour_integral_reversepath contour_integrable_on_def by blast
1.809 +
1.810 +lemma contour_integrable_reversepath_eq:
1.811 +    "valid_path g \<Longrightarrow> (f contour_integrable_on (reversepath g) \<longleftrightarrow> f contour_integrable_on g)"
1.812 +  using contour_integrable_reversepath valid_path_reversepath by fastforce
1.813 +
1.814 +lemma contour_integral_reversepath:
1.815 +  assumes "valid_path g"
1.816 +    shows "contour_integral (reversepath g) f = - (contour_integral g f)"
1.817 +proof (cases "f contour_integrable_on g")
1.818 +  case True then show ?thesis
1.819 +    by (simp add: assms contour_integral_unique has_contour_integral_integral has_contour_integral_reversepath)
1.820 +next
1.821 +  case False then have "~ f contour_integrable_on (reversepath g)"
1.822 +    by (simp add: assms contour_integrable_reversepath_eq)
1.823 +  with False show ?thesis by (simp add: not_integrable_contour_integral)
1.824 +qed
1.825 +
1.826 +
1.827 +subsection\<open>Joining two paths together\<close>
1.828 +
1.829 +lemma valid_path_join:
1.830 +  assumes "valid_path g1" "valid_path g2" "pathfinish g1 = pathstart g2"
1.831 +    shows "valid_path(g1 +++ g2)"
1.832 +proof -
1.833 +  have "g1 1 = g2 0"
1.834 +    using assms by (auto simp: pathfinish_def pathstart_def)
1.835 +  moreover have "(g1 o (\<lambda>x. 2*x)) piecewise_C1_differentiable_on {0..1/2}"
1.836 +    apply (rule piecewise_C1_differentiable_compose)
1.837 +    using assms
1.838 +    apply (auto simp: valid_path_def piecewise_C1_differentiable_on_def continuous_on_joinpaths)
1.839 +    apply (rule continuous_intros | simp)+
1.840 +    apply (force intro: finite_vimageI [where h = "op*2"] inj_onI)
1.841 +    done
1.842 +  moreover have "(g2 o (\<lambda>x. 2*x-1)) piecewise_C1_differentiable_on {1/2..1}"
1.843 +    apply (rule piecewise_C1_differentiable_compose)
1.844 +    using assms unfolding valid_path_def piecewise_C1_differentiable_on_def
1.845 +    by (auto intro!: continuous_intros finite_vimageI [where h = "(\<lambda>x. 2*x - 1)"] inj_onI
1.846 +             simp: image_affinity_atLeastAtMost_diff continuous_on_joinpaths)
1.847 +  ultimately show ?thesis
1.848 +    apply (simp only: valid_path_def continuous_on_joinpaths joinpaths_def)
1.849 +    apply (rule piecewise_C1_differentiable_cases)
1.850 +    apply (auto simp: o_def)
1.851 +    done
1.852 +qed
1.853 +
1.854 +lemma valid_path_join_D1:
1.855 +  fixes g1 :: "real \<Rightarrow> 'a::real_normed_field"
1.856 +  shows "valid_path (g1 +++ g2) \<Longrightarrow> valid_path g1"
1.857 +  unfolding valid_path_def
1.858 +  by (rule piecewise_C1_differentiable_D1)
1.859 +
1.860 +lemma valid_path_join_D2:
1.861 +  fixes g2 :: "real \<Rightarrow> 'a::real_normed_field"
1.862 +  shows "\<lbrakk>valid_path (g1 +++ g2); pathfinish g1 = pathstart g2\<rbrakk> \<Longrightarrow> valid_path g2"
1.863 +  unfolding valid_path_def
1.864 +  by (rule piecewise_C1_differentiable_D2)
1.865 +
1.866 +lemma valid_path_join_eq [simp]:
1.867 +  fixes g2 :: "real \<Rightarrow> 'a::real_normed_field"
1.868 +  shows "pathfinish g1 = pathstart g2 \<Longrightarrow> (valid_path(g1 +++ g2) \<longleftrightarrow> valid_path g1 \<and> valid_path g2)"
1.869 +  using valid_path_join_D1 valid_path_join_D2 valid_path_join by blast
1.870 +
1.871 +lemma has_contour_integral_join:
1.872 +  assumes "(f has_contour_integral i1) g1" "(f has_contour_integral i2) g2"
1.873 +          "valid_path g1" "valid_path g2"
1.874 +    shows "(f has_contour_integral (i1 + i2)) (g1 +++ g2)"
1.875 +proof -
1.876 +  obtain s1 s2
1.877 +    where s1: "finite s1" "\<forall>x\<in>{0..1} - s1. g1 differentiable at x"
1.878 +      and s2: "finite s2" "\<forall>x\<in>{0..1} - s2. g2 differentiable at x"
1.879 +    using assms
1.880 +    by (auto simp: valid_path_def piecewise_C1_differentiable_on_def C1_differentiable_on_eq)
1.881 +  have 1: "((\<lambda>x. f (g1 x) * vector_derivative g1 (at x)) has_integral i1) {0..1}"
1.882 +   and 2: "((\<lambda>x. f (g2 x) * vector_derivative g2 (at x)) has_integral i2) {0..1}"
1.883 +    using assms
1.884 +    by (auto simp: has_contour_integral)
1.885 +  have i1: "((\<lambda>x. (2*f (g1 (2*x))) * vector_derivative g1 (at (2*x))) has_integral i1) {0..1/2}"
1.886 +   and i2: "((\<lambda>x. (2*f (g2 (2*x - 1))) * vector_derivative g2 (at (2*x - 1))) has_integral i2) {1/2..1}"
1.887 +    using has_integral_affinity01 [OF 1, where m= 2 and c=0, THEN has_integral_cmul [where c=2]]
1.888 +          has_integral_affinity01 [OF 2, where m= 2 and c="-1", THEN has_integral_cmul [where c=2]]
1.889 +    by (simp_all only: image_affinity_atLeastAtMost_div_diff, simp_all add: scaleR_conv_of_real mult_ac)
1.890 +  have g1: "\<lbrakk>0 \<le> z; z*2 < 1; z*2 \<notin> s1\<rbrakk> \<Longrightarrow>
1.891 +            vector_derivative (\<lambda>x. if x*2 \<le> 1 then g1 (2*x) else g2 (2*x - 1)) (at z) =
1.892 +            2 *\<^sub>R vector_derivative g1 (at (z*2))" for z
1.893 +    apply (rule vector_derivative_at [OF has_vector_derivative_transform_within [where f = "(\<lambda>x. g1(2*x))" and d = "\<bar>z - 1/2\<bar>"]])
1.894 +    apply (simp_all add: dist_real_def abs_if split: if_split_asm)
1.895 +    apply (rule vector_diff_chain_at [of "\<lambda>x. 2*x" 2 _ g1, simplified o_def])
1.896 +    apply (simp add: has_vector_derivative_def has_derivative_def bounded_linear_mult_left)
1.897 +    using s1
1.898 +    apply (auto simp: algebra_simps vector_derivative_works)
1.899 +    done
1.900 +  have g2: "\<lbrakk>1 < z*2; z \<le> 1; z*2 - 1 \<notin> s2\<rbrakk> \<Longrightarrow>
1.901 +            vector_derivative (\<lambda>x. if x*2 \<le> 1 then g1 (2*x) else g2 (2*x - 1)) (at z) =
1.902 +            2 *\<^sub>R vector_derivative g2 (at (z*2 - 1))" for z
1.903 +    apply (rule vector_derivative_at [OF has_vector_derivative_transform_within [where f = "(\<lambda>x. g2 (2*x - 1))" and d = "\<bar>z - 1/2\<bar>"]])
1.904 +    apply (simp_all add: dist_real_def abs_if split: if_split_asm)
1.905 +    apply (rule vector_diff_chain_at [of "\<lambda>x. 2*x - 1" 2 _ g2, simplified o_def])
1.906 +    apply (simp add: has_vector_derivative_def has_derivative_def bounded_linear_mult_left)
1.907 +    using s2
1.908 +    apply (auto simp: algebra_simps vector_derivative_works)
1.909 +    done
1.910 +  have "((\<lambda>x. f ((g1 +++ g2) x) * vector_derivative (g1 +++ g2) (at x)) has_integral i1) {0..1/2}"
1.911 +    apply (rule has_integral_spike_finite [OF _ _ i1, of "insert (1/2) (op*2 -` s1)"])
1.912 +    using s1
1.913 +    apply (force intro: finite_vimageI [where h = "op*2"] inj_onI)
1.914 +    apply (clarsimp simp add: joinpaths_def scaleR_conv_of_real mult_ac g1)
1.915 +    done
1.916 +  moreover have "((\<lambda>x. f ((g1 +++ g2) x) * vector_derivative (g1 +++ g2) (at x)) has_integral i2) {1/2..1}"
1.917 +    apply (rule has_integral_spike_finite [OF _ _ i2, of "insert (1/2) ((\<lambda>x. 2*x-1) -` s2)"])
1.918 +    using s2
1.919 +    apply (force intro: finite_vimageI [where h = "\<lambda>x. 2*x-1"] inj_onI)
1.920 +    apply (clarsimp simp add: joinpaths_def scaleR_conv_of_real mult_ac g2)
1.921 +    done
1.922 +  ultimately
1.923 +  show ?thesis
1.924 +    apply (simp add: has_contour_integral)
1.925 +    apply (rule has_integral_combine [where c = "1/2"], auto)
1.926 +    done
1.927 +qed
1.928 +
1.929 +lemma contour_integrable_joinI:
1.930 +  assumes "f contour_integrable_on g1" "f contour_integrable_on g2"
1.931 +          "valid_path g1" "valid_path g2"
1.932 +    shows "f contour_integrable_on (g1 +++ g2)"
1.933 +  using assms
1.934 +  by (meson has_contour_integral_join contour_integrable_on_def)
1.935 +
1.936 +lemma contour_integrable_joinD1:
1.937 +  assumes "f contour_integrable_on (g1 +++ g2)" "valid_path g1"
1.938 +    shows "f contour_integrable_on g1"
1.939 +proof -
1.940 +  obtain s1
1.941 +    where s1: "finite s1" "\<forall>x\<in>{0..1} - s1. g1 differentiable at x"
1.942 +    using assms by (auto simp: valid_path_def piecewise_C1_differentiable_on_def C1_differentiable_on_eq)
1.943 +  have "(\<lambda>x. f ((g1 +++ g2) (x/2)) * vector_derivative (g1 +++ g2) (at (x/2))) integrable_on {0..1}"
1.944 +    using assms
1.945 +    apply (auto simp: contour_integrable_on)
1.946 +    apply (drule integrable_on_subcbox [where a=0 and b="1/2"])
1.947 +    apply (auto intro: integrable_affinity [of _ 0 "1/2::real" "1/2" 0, simplified])
1.948 +    done
1.949 +  then have *: "(\<lambda>x. (f ((g1 +++ g2) (x/2))/2) * vector_derivative (g1 +++ g2) (at (x/2))) integrable_on {0..1}"
1.950 +    by (auto dest: integrable_cmul [where c="1/2"] simp: scaleR_conv_of_real)
1.951 +  have g1: "\<lbrakk>0 < z; z < 1; z \<notin> s1\<rbrakk> \<Longrightarrow>
1.952 +            vector_derivative (\<lambda>x. if x*2 \<le> 1 then g1 (2*x) else g2 (2*x - 1)) (at (z/2)) =
1.953 +            2 *\<^sub>R vector_derivative g1 (at z)"  for z
1.954 +    apply (rule vector_derivative_at [OF has_vector_derivative_transform_within [where f = "(\<lambda>x. g1(2*x))" and d = "\<bar>(z-1)/2\<bar>"]])
1.955 +    apply (simp_all add: field_simps dist_real_def abs_if split: if_split_asm)
1.956 +    apply (rule vector_diff_chain_at [of "\<lambda>x. x*2" 2 _ g1, simplified o_def])
1.957 +    using s1
1.958 +    apply (auto simp: vector_derivative_works has_vector_derivative_def has_derivative_def bounded_linear_mult_left)
1.959 +    done
1.960 +  show ?thesis
1.961 +    using s1
1.962 +    apply (auto simp: contour_integrable_on)
1.963 +    apply (rule integrable_spike_finite [of "{0,1} \<union> s1", OF _ _ *])
1.964 +    apply (auto simp: joinpaths_def scaleR_conv_of_real g1)
1.965 +    done
1.966 +qed
1.967 +
1.968 +lemma contour_integrable_joinD2:
1.969 +  assumes "f contour_integrable_on (g1 +++ g2)" "valid_path g2"
1.970 +    shows "f contour_integrable_on g2"
1.971 +proof -
1.972 +  obtain s2
1.973 +    where s2: "finite s2" "\<forall>x\<in>{0..1} - s2. g2 differentiable at x"
1.974 +    using assms by (auto simp: valid_path_def piecewise_C1_differentiable_on_def C1_differentiable_on_eq)
1.975 +  have "(\<lambda>x. f ((g1 +++ g2) (x/2 + 1/2)) * vector_derivative (g1 +++ g2) (at (x/2 + 1/2))) integrable_on {0..1}"
1.976 +    using assms
1.977 +    apply (auto simp: contour_integrable_on)
1.978 +    apply (drule integrable_on_subcbox [where a="1/2" and b=1], auto)
1.979 +    apply (drule integrable_affinity [of _ "1/2::real" 1 "1/2" "1/2", simplified])
1.980 +    apply (simp add: image_affinity_atLeastAtMost_diff)
1.981 +    done
1.982 +  then have *: "(\<lambda>x. (f ((g1 +++ g2) (x/2 + 1/2))/2) * vector_derivative (g1 +++ g2) (at (x/2 + 1/2)))
1.983 +                integrable_on {0..1}"
1.984 +    by (auto dest: integrable_cmul [where c="1/2"] simp: scaleR_conv_of_real)
1.985 +  have g2: "\<lbrakk>0 < z; z < 1; z \<notin> s2\<rbrakk> \<Longrightarrow>
1.986 +            vector_derivative (\<lambda>x. if x*2 \<le> 1 then g1 (2*x) else g2 (2*x - 1)) (at (z/2+1/2)) =
1.987 +            2 *\<^sub>R vector_derivative g2 (at z)" for z
1.988 +    apply (rule vector_derivative_at [OF has_vector_derivative_transform_within [where f = "(\<lambda>x. g2(2*x-1))" and d = "\<bar>z/2\<bar>"]])
1.989 +    apply (simp_all add: field_simps dist_real_def abs_if split: if_split_asm)
1.990 +    apply (rule vector_diff_chain_at [of "\<lambda>x. x*2-1" 2 _ g2, simplified o_def])
1.991 +    using s2
1.992 +    apply (auto simp: has_vector_derivative_def has_derivative_def bounded_linear_mult_left
1.994 +    done
1.995 +  show ?thesis
1.996 +    using s2
1.997 +    apply (auto simp: contour_integrable_on)
1.998 +    apply (rule integrable_spike_finite [of "{0,1} \<union> s2", OF _ _ *])
1.999 +    apply (auto simp: joinpaths_def scaleR_conv_of_real g2)
1.1000 +    done
1.1001 +qed
1.1002 +
1.1003 +lemma contour_integrable_join [simp]:
1.1004 +  shows
1.1005 +    "\<lbrakk>valid_path g1; valid_path g2\<rbrakk>
1.1006 +     \<Longrightarrow> f contour_integrable_on (g1 +++ g2) \<longleftrightarrow> f contour_integrable_on g1 \<and> f contour_integrable_on g2"
1.1007 +using contour_integrable_joinD1 contour_integrable_joinD2 contour_integrable_joinI by blast
1.1008 +
1.1009 +lemma contour_integral_join [simp]:
1.1010 +  shows
1.1011 +    "\<lbrakk>f contour_integrable_on g1; f contour_integrable_on g2; valid_path g1; valid_path g2\<rbrakk>
1.1012 +        \<Longrightarrow> contour_integral (g1 +++ g2) f = contour_integral g1 f + contour_integral g2 f"
1.1013 +  by (simp add: has_contour_integral_integral has_contour_integral_join contour_integral_unique)
1.1014 +
1.1015 +
1.1016 +subsection\<open>Shifting the starting point of a (closed) path\<close>
1.1017 +
1.1018 +lemma shiftpath_alt_def: "shiftpath a f = (\<lambda>x. if x \<le> 1-a then f (a + x) else f (a + x - 1))"
1.1019 +  by (auto simp: shiftpath_def)
1.1020 +
1.1021 +lemma valid_path_shiftpath [intro]:
1.1022 +  assumes "valid_path g" "pathfinish g = pathstart g" "a \<in> {0..1}"
1.1023 +    shows "valid_path(shiftpath a g)"
1.1024 +  using assms
1.1025 +  apply (auto simp: valid_path_def shiftpath_alt_def)
1.1026 +  apply (rule piecewise_C1_differentiable_cases)
1.1027 +  apply (auto simp: algebra_simps)
1.1028 +  apply (rule piecewise_C1_differentiable_affine [of g 1 a, simplified o_def scaleR_one])
1.1029 +  apply (auto simp: pathfinish_def pathstart_def elim: piecewise_C1_differentiable_on_subset)
1.1030 +  apply (rule piecewise_C1_differentiable_affine [of g 1 "a-1", simplified o_def scaleR_one algebra_simps])
1.1031 +  apply (auto simp: pathfinish_def pathstart_def elim: piecewise_C1_differentiable_on_subset)
1.1032 +  done
1.1033 +
1.1034 +lemma has_contour_integral_shiftpath:
1.1035 +  assumes f: "(f has_contour_integral i) g" "valid_path g"
1.1036 +      and a: "a \<in> {0..1}"
1.1037 +    shows "(f has_contour_integral i) (shiftpath a g)"
1.1038 +proof -
1.1039 +  obtain s
1.1040 +    where s: "finite s" and g: "\<forall>x\<in>{0..1} - s. g differentiable at x"
1.1041 +    using assms by (auto simp: valid_path_def piecewise_C1_differentiable_on_def C1_differentiable_on_eq)
1.1042 +  have *: "((\<lambda>x. f (g x) * vector_derivative g (at x)) has_integral i) {0..1}"
1.1043 +    using assms by (auto simp: has_contour_integral)
1.1044 +  then have i: "i = integral {a..1} (\<lambda>x. f (g x) * vector_derivative g (at x)) +
1.1045 +                    integral {0..a} (\<lambda>x. f (g x) * vector_derivative g (at x))"
1.1046 +    apply (rule has_integral_unique)
1.1048 +    apply (subst integral_combine)
1.1049 +    using assms * integral_unique by auto
1.1050 +  { fix x
1.1051 +    have "0 \<le> x \<Longrightarrow> x + a < 1 \<Longrightarrow> x \<notin> (\<lambda>x. x - a) ` s \<Longrightarrow>
1.1052 +         vector_derivative (shiftpath a g) (at x) = vector_derivative g (at (x + a))"
1.1053 +      unfolding shiftpath_def
1.1054 +      apply (rule vector_derivative_at [OF has_vector_derivative_transform_within [where f = "(\<lambda>x. g(a+x))" and d = "dist(1-a) x"]])
1.1055 +        apply (auto simp: field_simps dist_real_def abs_if split: if_split_asm)
1.1056 +      apply (rule vector_diff_chain_at [of "\<lambda>x. x+a" 1 _ g, simplified o_def scaleR_one])
1.1057 +       apply (intro derivative_eq_intros | simp)+
1.1058 +      using g
1.1059 +       apply (drule_tac x="x+a" in bspec)
1.1060 +      using a apply (auto simp: has_vector_derivative_def vector_derivative_works image_def add.commute)
1.1061 +      done
1.1062 +  } note vd1 = this
1.1063 +  { fix x
1.1064 +    have "1 < x + a \<Longrightarrow> x \<le> 1 \<Longrightarrow> x \<notin> (\<lambda>x. x - a + 1) ` s \<Longrightarrow>
1.1065 +          vector_derivative (shiftpath a g) (at x) = vector_derivative g (at (x + a - 1))"
1.1066 +      unfolding shiftpath_def
1.1067 +      apply (rule vector_derivative_at [OF has_vector_derivative_transform_within [where f = "(\<lambda>x. g(a+x-1))" and d = "dist (1-a) x"]])
1.1068 +        apply (auto simp: field_simps dist_real_def abs_if split: if_split_asm)
1.1069 +      apply (rule vector_diff_chain_at [of "\<lambda>x. x+a-1" 1 _ g, simplified o_def scaleR_one])
1.1070 +       apply (intro derivative_eq_intros | simp)+
1.1071 +      using g
1.1072 +      apply (drule_tac x="x+a-1" in bspec)
1.1073 +      using a apply (auto simp: has_vector_derivative_def vector_derivative_works image_def add.commute)
1.1074 +      done
1.1075 +  } note vd2 = this
1.1076 +  have va1: "(\<lambda>x. f (g x) * vector_derivative g (at x)) integrable_on ({a..1})"
1.1077 +    using * a   by (fastforce intro: integrable_subinterval_real)
1.1078 +  have v0a: "(\<lambda>x. f (g x) * vector_derivative g (at x)) integrable_on ({0..a})"
1.1079 +    apply (rule integrable_subinterval_real)
1.1080 +    using * a by auto
1.1081 +  have "((\<lambda>x. f (shiftpath a g x) * vector_derivative (shiftpath a g) (at x))
1.1082 +        has_integral  integral {a..1} (\<lambda>x. f (g x) * vector_derivative g (at x)))  {0..1 - a}"
1.1083 +    apply (rule has_integral_spike_finite
1.1084 +             [where s = "{1-a} \<union> (\<lambda>x. x-a) ` s" and f = "\<lambda>x. f(g(a+x)) * vector_derivative g (at(a+x))"])
1.1085 +      using s apply blast
1.1086 +     using a apply (auto simp: algebra_simps vd1)
1.1087 +     apply (force simp: shiftpath_def add.commute)
1.1088 +    using has_integral_affinity [where m=1 and c=a, simplified, OF integrable_integral [OF va1]]
1.1089 +    apply (simp add: image_affinity_atLeastAtMost_diff [where m=1 and c=a, simplified] add.commute)
1.1090 +    done
1.1091 +  moreover
1.1092 +  have "((\<lambda>x. f (shiftpath a g x) * vector_derivative (shiftpath a g) (at x))
1.1093 +        has_integral  integral {0..a} (\<lambda>x. f (g x) * vector_derivative g (at x)))  {1 - a..1}"
1.1094 +    apply (rule has_integral_spike_finite
1.1095 +             [where s = "{1-a} \<union> (\<lambda>x. x-a+1) ` s" and f = "\<lambda>x. f(g(a+x-1)) * vector_derivative g (at(a+x-1))"])
1.1096 +      using s apply blast
1.1097 +     using a apply (auto simp: algebra_simps vd2)
1.1098 +     apply (force simp: shiftpath_def add.commute)
1.1099 +    using has_integral_affinity [where m=1 and c="a-1", simplified, OF integrable_integral [OF v0a]]
1.1100 +    apply (simp add: image_affinity_atLeastAtMost [where m=1 and c="1-a", simplified])
1.1101 +    apply (simp add: algebra_simps)
1.1102 +    done
1.1103 +  ultimately show ?thesis
1.1104 +    using a
1.1105 +    by (auto simp: i has_contour_integral intro: has_integral_combine [where c = "1-a"])
1.1106 +qed
1.1107 +
1.1108 +lemma has_contour_integral_shiftpath_D:
1.1109 +  assumes "(f has_contour_integral i) (shiftpath a g)"
1.1110 +          "valid_path g" "pathfinish g = pathstart g" "a \<in> {0..1}"
1.1111 +    shows "(f has_contour_integral i) g"
1.1112 +proof -
1.1113 +  obtain s
1.1114 +    where s: "finite s" and g: "\<forall>x\<in>{0..1} - s. g differentiable at x"
1.1115 +    using assms by (auto simp: valid_path_def piecewise_C1_differentiable_on_def C1_differentiable_on_eq)
1.1116 +  { fix x
1.1117 +    assume x: "0 < x" "x < 1" "x \<notin> s"
1.1118 +    then have gx: "g differentiable at x"
1.1119 +      using g by auto
1.1120 +    have "vector_derivative g (at x within {0..1}) =
1.1121 +          vector_derivative (shiftpath (1 - a) (shiftpath a g)) (at x within {0..1})"
1.1122 +      apply (rule vector_derivative_at_within_ivl
1.1123 +                  [OF has_vector_derivative_transform_within_open
1.1124 +                      [where f = "(shiftpath (1 - a) (shiftpath a g))" and s = "{0<..<1}-s"]])
1.1125 +      using s g assms x
1.1126 +      apply (auto simp: finite_imp_closed open_Diff shiftpath_shiftpath
1.1127 +                        vector_derivative_within_interior vector_derivative_works [symmetric])
1.1128 +      apply (rule differentiable_transform_within [OF gx, of "min x (1-x)"])
1.1129 +      apply (auto simp: dist_real_def shiftpath_shiftpath abs_if split: if_split_asm)
1.1130 +      done
1.1131 +  } note vd = this
1.1132 +  have fi: "(f has_contour_integral i) (shiftpath (1 - a) (shiftpath a g))"
1.1133 +    using assms  by (auto intro!: has_contour_integral_shiftpath)
1.1134 +  show ?thesis
1.1135 +    apply (simp add: has_contour_integral_def)
1.1136 +    apply (rule has_integral_spike_finite [of "{0,1} \<union> s", OF _ _  fi [unfolded has_contour_integral_def]])
1.1137 +    using s assms vd
1.1138 +    apply (auto simp: Path_Connected.shiftpath_shiftpath)
1.1139 +    done
1.1140 +qed
1.1141 +
1.1142 +lemma has_contour_integral_shiftpath_eq:
1.1143 +  assumes "valid_path g" "pathfinish g = pathstart g" "a \<in> {0..1}"
1.1144 +    shows "(f has_contour_integral i) (shiftpath a g) \<longleftrightarrow> (f has_contour_integral i) g"
1.1145 +  using assms has_contour_integral_shiftpath has_contour_integral_shiftpath_D by blast
1.1146 +
1.1147 +lemma contour_integrable_on_shiftpath_eq:
1.1148 +  assumes "valid_path g" "pathfinish g = pathstart g" "a \<in> {0..1}"
1.1149 +    shows "f contour_integrable_on (shiftpath a g) \<longleftrightarrow> f contour_integrable_on g"
1.1150 +using assms contour_integrable_on_def has_contour_integral_shiftpath_eq by auto
1.1151 +
1.1152 +lemma contour_integral_shiftpath:
1.1153 +  assumes "valid_path g" "pathfinish g = pathstart g" "a \<in> {0..1}"
1.1154 +    shows "contour_integral (shiftpath a g) f = contour_integral g f"
1.1155 +   using assms
1.1156 +   by (simp add: contour_integral_def contour_integrable_on_def has_contour_integral_shiftpath_eq)
1.1157 +
1.1158 +
1.1160 +
1.1161 +lemma has_vector_derivative_linepath_within:
1.1162 +    "(linepath a b has_vector_derivative (b - a)) (at x within s)"
1.1163 +apply (simp add: linepath_def has_vector_derivative_def algebra_simps)
1.1164 +apply (rule derivative_eq_intros | simp)+
1.1165 +done
1.1166 +
1.1167 +lemma vector_derivative_linepath_within:
1.1168 +    "x \<in> {0..1} \<Longrightarrow> vector_derivative (linepath a b) (at x within {0..1}) = b - a"
1.1169 +  apply (rule vector_derivative_within_closed_interval [of 0 "1::real", simplified])
1.1170 +  apply (auto simp: has_vector_derivative_linepath_within)
1.1171 +  done
1.1172 +
1.1173 +lemma vector_derivative_linepath_at [simp]: "vector_derivative (linepath a b) (at x) = b - a"
1.1174 +  by (simp add: has_vector_derivative_linepath_within vector_derivative_at)
1.1175 +
1.1176 +lemma valid_path_linepath [iff]: "valid_path (linepath a b)"
1.1177 +  apply (simp add: valid_path_def piecewise_C1_differentiable_on_def C1_differentiable_on_eq continuous_on_linepath)
1.1178 +  apply (rule_tac x="{}" in exI)
1.1179 +  apply (simp add: differentiable_on_def differentiable_def)
1.1180 +  using has_vector_derivative_def has_vector_derivative_linepath_within
1.1181 +  apply (fastforce simp add: continuous_on_eq_continuous_within)
1.1182 +  done
1.1183 +
1.1184 +lemma has_contour_integral_linepath:
1.1185 +  shows "(f has_contour_integral i) (linepath a b) \<longleftrightarrow>
1.1186 +         ((\<lambda>x. f(linepath a b x) * (b - a)) has_integral i) {0..1}"
1.1187 +  by (simp add: has_contour_integral vector_derivative_linepath_at)
1.1188 +
1.1189 +lemma linepath_in_path:
1.1190 +  shows "x \<in> {0..1} \<Longrightarrow> linepath a b x \<in> closed_segment a b"
1.1191 +  by (auto simp: segment linepath_def)
1.1192 +
1.1193 +lemma linepath_image_01: "linepath a b ` {0..1} = closed_segment a b"
1.1194 +  by (auto simp: segment linepath_def)
1.1195 +
1.1196 +lemma linepath_in_convex_hull:
1.1197 +    fixes x::real
1.1198 +    assumes a: "a \<in> convex hull s"
1.1199 +        and b: "b \<in> convex hull s"
1.1200 +        and x: "0\<le>x" "x\<le>1"
1.1201 +       shows "linepath a b x \<in> convex hull s"
1.1202 +  apply (rule closed_segment_subset_convex_hull [OF a b, THEN subsetD])
1.1203 +  using x
1.1204 +  apply (auto simp: linepath_image_01 [symmetric])
1.1205 +  done
1.1206 +
1.1207 +lemma Re_linepath: "Re(linepath (of_real a) (of_real b) x) = (1 - x)*a + x*b"
1.1208 +  by (simp add: linepath_def)
1.1209 +
1.1210 +lemma Im_linepath: "Im(linepath (of_real a) (of_real b) x) = 0"
1.1211 +  by (simp add: linepath_def)
1.1212 +
1.1213 +lemma has_contour_integral_trivial [iff]: "(f has_contour_integral 0) (linepath a a)"
1.1214 +  by (simp add: has_contour_integral_linepath)
1.1215 +
1.1216 +lemma contour_integral_trivial [simp]: "contour_integral (linepath a a) f = 0"
1.1217 +  using has_contour_integral_trivial contour_integral_unique by blast
1.1218 +
1.1219 +
1.1220 +subsection\<open>Relation to subpath construction\<close>
1.1221 +
1.1222 +lemma valid_path_subpath:
1.1223 +  fixes g :: "real \<Rightarrow> 'a :: real_normed_vector"
1.1224 +  assumes "valid_path g" "u \<in> {0..1}" "v \<in> {0..1}"
1.1225 +    shows "valid_path(subpath u v g)"
1.1226 +proof (cases "v=u")
1.1227 +  case True
1.1228 +  then show ?thesis
1.1229 +    unfolding valid_path_def subpath_def
1.1230 +    by (force intro: C1_differentiable_on_const C1_differentiable_imp_piecewise)
1.1231 +next
1.1232 +  case False
1.1233 +  have "(g o (\<lambda>x. ((v-u) * x + u))) piecewise_C1_differentiable_on {0..1}"
1.1234 +    apply (rule piecewise_C1_differentiable_compose)
1.1235 +    apply (simp add: C1_differentiable_imp_piecewise)
1.1236 +     apply (simp add: image_affinity_atLeastAtMost)
1.1237 +    using assms False
1.1238 +    apply (auto simp: algebra_simps valid_path_def piecewise_C1_differentiable_on_subset)
1.1239 +    apply (subst Int_commute)
1.1240 +    apply (auto simp: inj_on_def algebra_simps crossproduct_eq finite_vimage_IntI)
1.1241 +    done
1.1242 +  then show ?thesis
1.1243 +    by (auto simp: o_def valid_path_def subpath_def)
1.1244 +qed
1.1245 +
1.1246 +lemma has_contour_integral_subpath_refl [iff]: "(f has_contour_integral 0) (subpath u u g)"
1.1247 +  by (simp add: has_contour_integral subpath_def)
1.1248 +
1.1249 +lemma contour_integrable_subpath_refl [iff]: "f contour_integrable_on (subpath u u g)"
1.1250 +  using has_contour_integral_subpath_refl contour_integrable_on_def by blast
1.1251 +
1.1252 +lemma contour_integral_subpath_refl [simp]: "contour_integral (subpath u u g) f = 0"
1.1253 +  by (simp add: has_contour_integral_subpath_refl contour_integral_unique)
1.1254 +
1.1255 +lemma has_contour_integral_subpath:
1.1256 +  assumes f: "f contour_integrable_on g" and g: "valid_path g"
1.1257 +      and uv: "u \<in> {0..1}" "v \<in> {0..1}" "u \<le> v"
1.1258 +    shows "(f has_contour_integral  integral {u..v} (\<lambda>x. f(g x) * vector_derivative g (at x)))
1.1259 +           (subpath u v g)"
1.1260 +proof (cases "v=u")
1.1261 +  case True
1.1262 +  then show ?thesis
1.1263 +    using f   by (simp add: contour_integrable_on_def subpath_def has_contour_integral)
1.1264 +next
1.1265 +  case False
1.1266 +  obtain s where s: "\<And>x. x \<in> {0..1} - s \<Longrightarrow> g differentiable at x" and fs: "finite s"
1.1267 +    using g unfolding piecewise_C1_differentiable_on_def C1_differentiable_on_eq valid_path_def by blast
1.1268 +  have *: "((\<lambda>x. f (g ((v - u) * x + u)) * vector_derivative g (at ((v - u) * x + u)))
1.1269 +            has_integral (1 / (v - u)) * integral {u..v} (\<lambda>t. f (g t) * vector_derivative g (at t)))
1.1270 +           {0..1}"
1.1271 +    using f uv
1.1272 +    apply (simp add: contour_integrable_on subpath_def has_contour_integral)
1.1273 +    apply (drule integrable_on_subcbox [where a=u and b=v, simplified])
1.1274 +    apply (simp_all add: has_integral_integral)
1.1275 +    apply (drule has_integral_affinity [where m="v-u" and c=u, simplified])
1.1276 +    apply (simp_all add: False image_affinity_atLeastAtMost_div_diff scaleR_conv_of_real)
1.1277 +    apply (simp add: divide_simps False)
1.1278 +    done
1.1279 +  { fix x
1.1280 +    have "x \<in> {0..1} \<Longrightarrow>
1.1281 +           x \<notin> (\<lambda>t. (v-u) *\<^sub>R t + u) -` s \<Longrightarrow>
1.1282 +           vector_derivative (\<lambda>x. g ((v-u) * x + u)) (at x) = (v-u) *\<^sub>R vector_derivative g (at ((v-u) * x + u))"
1.1283 +      apply (rule vector_derivative_at [OF vector_diff_chain_at [simplified o_def]])
1.1284 +      apply (intro derivative_eq_intros | simp)+
1.1285 +      apply (cut_tac s [of "(v - u) * x + u"])
1.1286 +      using uv mult_left_le [of x "v-u"]
1.1287 +      apply (auto simp:  vector_derivative_works)
1.1288 +      done
1.1289 +  } note vd = this
1.1290 +  show ?thesis
1.1291 +    apply (cut_tac has_integral_cmul [OF *, where c = "v-u"])
1.1292 +    using fs assms
1.1293 +    apply (simp add: False subpath_def has_contour_integral)
1.1294 +    apply (rule_tac s = "(\<lambda>t. ((v-u) *\<^sub>R t + u)) -` s" in has_integral_spike_finite)
1.1295 +    apply (auto simp: inj_on_def False finite_vimageI vd scaleR_conv_of_real)
1.1296 +    done
1.1297 +qed
1.1298 +
1.1299 +lemma contour_integrable_subpath:
1.1300 +  assumes "f contour_integrable_on g" "valid_path g" "u \<in> {0..1}" "v \<in> {0..1}"
1.1301 +    shows "f contour_integrable_on (subpath u v g)"
1.1302 +  apply (cases u v rule: linorder_class.le_cases)
1.1303 +   apply (metis contour_integrable_on_def has_contour_integral_subpath [OF assms])
1.1304 +  apply (subst reversepath_subpath [symmetric])
1.1305 +  apply (rule contour_integrable_reversepath)
1.1306 +   using assms apply (blast intro: valid_path_subpath)
1.1307 +  apply (simp add: contour_integrable_on_def)
1.1308 +  using assms apply (blast intro: has_contour_integral_subpath)
1.1309 +  done
1.1310 +
1.1311 +lemma has_integral_integrable_integral: "(f has_integral i) s \<longleftrightarrow> f integrable_on s \<and> integral s f = i"
1.1312 +  by blast
1.1313 +
1.1314 +lemma has_integral_contour_integral_subpath:
1.1315 +  assumes "f contour_integrable_on g" "valid_path g" "u \<in> {0..1}" "v \<in> {0..1}" "u \<le> v"
1.1316 +    shows "(((\<lambda>x. f(g x) * vector_derivative g (at x)))
1.1317 +            has_integral  contour_integral (subpath u v g) f) {u..v}"
1.1318 +  using assms
1.1319 +  apply (auto simp: has_integral_integrable_integral)
1.1320 +  apply (rule integrable_on_subcbox [where a=u and b=v and s = "{0..1}", simplified])
1.1321 +  apply (auto simp: contour_integral_unique [OF has_contour_integral_subpath] contour_integrable_on)
1.1322 +  done
1.1323 +
1.1324 +lemma contour_integral_subcontour_integral:
1.1325 +  assumes "f contour_integrable_on g" "valid_path g" "u \<in> {0..1}" "v \<in> {0..1}" "u \<le> v"
1.1326 +    shows "contour_integral (subpath u v g) f =
1.1327 +           integral {u..v} (\<lambda>x. f(g x) * vector_derivative g (at x))"
1.1328 +  using assms has_contour_integral_subpath contour_integral_unique by blast
1.1329 +
1.1330 +lemma contour_integral_subpath_combine_less:
1.1331 +  assumes "f contour_integrable_on g" "valid_path g" "u \<in> {0..1}" "v \<in> {0..1}" "w \<in> {0..1}"
1.1332 +          "u<v" "v<w"
1.1333 +    shows "contour_integral (subpath u v g) f + contour_integral (subpath v w g) f =
1.1334 +           contour_integral (subpath u w g) f"
1.1335 +  using assms apply (auto simp: contour_integral_subcontour_integral)
1.1336 +  apply (rule integral_combine, auto)
1.1337 +  apply (rule integrable_on_subcbox [where a=u and b=w and s = "{0..1}", simplified])
1.1338 +  apply (auto simp: contour_integrable_on)
1.1339 +  done
1.1340 +
1.1341 +lemma contour_integral_subpath_combine:
1.1342 +  assumes "f contour_integrable_on g" "valid_path g" "u \<in> {0..1}" "v \<in> {0..1}" "w \<in> {0..1}"
1.1343 +    shows "contour_integral (subpath u v g) f + contour_integral (subpath v w g) f =
1.1344 +           contour_integral (subpath u w g) f"
1.1345 +proof (cases "u\<noteq>v \<and> v\<noteq>w \<and> u\<noteq>w")
1.1346 +  case True
1.1347 +    have *: "subpath v u g = reversepath(subpath u v g) \<and>
1.1348 +             subpath w u g = reversepath(subpath u w g) \<and>
1.1349 +             subpath w v g = reversepath(subpath v w g)"
1.1350 +      by (auto simp: reversepath_subpath)
1.1351 +    have "u < v \<and> v < w \<or>
1.1352 +          u < w \<and> w < v \<or>
1.1353 +          v < u \<and> u < w \<or>
1.1354 +          v < w \<and> w < u \<or>
1.1355 +          w < u \<and> u < v \<or>
1.1356 +          w < v \<and> v < u"
1.1357 +      using True assms by linarith
1.1358 +    with assms show ?thesis
1.1359 +      using contour_integral_subpath_combine_less [of f g u v w]
1.1360 +            contour_integral_subpath_combine_less [of f g u w v]
1.1361 +            contour_integral_subpath_combine_less [of f g v u w]
1.1362 +            contour_integral_subpath_combine_less [of f g v w u]
1.1363 +            contour_integral_subpath_combine_less [of f g w u v]
1.1364 +            contour_integral_subpath_combine_less [of f g w v u]
1.1365 +      apply simp
1.1366 +      apply (elim disjE)
1.1367 +      apply (auto simp: * contour_integral_reversepath contour_integrable_subpath
1.1368 +                   valid_path_reversepath valid_path_subpath algebra_simps)
1.1369 +      done
1.1370 +next
1.1371 +  case False
1.1372 +  then show ?thesis
1.1373 +    apply (auto simp: contour_integral_subpath_refl)
1.1374 +    using assms
1.1375 +    by (metis eq_neg_iff_add_eq_0 contour_integrable_subpath contour_integral_reversepath reversepath_subpath valid_path_subpath)
1.1376 +qed
1.1377 +
1.1378 +lemma contour_integral_integral:
1.1379 +     "contour_integral g f = integral {0..1} (\<lambda>x. f (g x) * vector_derivative g (at x))"
1.1380 +  by (simp add: contour_integral_def integral_def has_contour_integral contour_integrable_on)
1.1381 +
1.1382 +
1.1383 +text\<open>Cauchy's theorem where there's a primitive\<close>
1.1384 +
1.1385 +lemma contour_integral_primitive_lemma:
1.1386 +  fixes f :: "complex \<Rightarrow> complex" and g :: "real \<Rightarrow> complex"
1.1387 +  assumes "a \<le> b"
1.1388 +      and "\<And>x. x \<in> s \<Longrightarrow> (f has_field_derivative f' x) (at x within s)"
1.1389 +      and "g piecewise_differentiable_on {a..b}"  "\<And>x. x \<in> {a..b} \<Longrightarrow> g x \<in> s"
1.1390 +    shows "((\<lambda>x. f'(g x) * vector_derivative g (at x within {a..b}))
1.1391 +             has_integral (f(g b) - f(g a))) {a..b}"
1.1392 +proof -
1.1393 +  obtain k where k: "finite k" "\<forall>x\<in>{a..b} - k. g differentiable (at x within {a..b})" and cg: "continuous_on {a..b} g"
1.1394 +    using assms by (auto simp: piecewise_differentiable_on_def)
1.1395 +  have cfg: "continuous_on {a..b} (\<lambda>x. f (g x))"
1.1396 +    apply (rule continuous_on_compose [OF cg, unfolded o_def])
1.1397 +    using assms
1.1398 +    apply (metis field_differentiable_def field_differentiable_imp_continuous_at continuous_on_eq_continuous_within continuous_on_subset image_subset_iff)
1.1399 +    done
1.1400 +  { fix x::real
1.1401 +    assume a: "a < x" and b: "x < b" and xk: "x \<notin> k"
1.1402 +    then have "g differentiable at x within {a..b}"
1.1403 +      using k by (simp add: differentiable_at_withinI)
1.1404 +    then have "(g has_vector_derivative vector_derivative g (at x within {a..b})) (at x within {a..b})"
1.1405 +      by (simp add: vector_derivative_works has_field_derivative_def scaleR_conv_of_real)
1.1406 +    then have gdiff: "(g has_derivative (\<lambda>u. u * vector_derivative g (at x within {a..b}))) (at x within {a..b})"
1.1407 +      by (simp add: has_vector_derivative_def scaleR_conv_of_real)
1.1408 +    have "(f has_field_derivative (f' (g x))) (at (g x) within g ` {a..b})"
1.1409 +      using assms by (metis a atLeastAtMost_iff b DERIV_subset image_subset_iff less_eq_real_def)
1.1410 +    then have fdiff: "(f has_derivative op * (f' (g x))) (at (g x) within g ` {a..b})"
1.1411 +      by (simp add: has_field_derivative_def)
1.1412 +    have "((\<lambda>x. f (g x)) has_vector_derivative f' (g x) * vector_derivative g (at x within {a..b})) (at x within {a..b})"
1.1413 +      using diff_chain_within [OF gdiff fdiff]
1.1414 +      by (simp add: has_vector_derivative_def scaleR_conv_of_real o_def mult_ac)
1.1415 +  } note * = this
1.1416 +  show ?thesis
1.1417 +    apply (rule fundamental_theorem_of_calculus_interior_strong)
1.1418 +    using k assms cfg *
1.1419 +    apply (auto simp: at_within_closed_interval)
1.1420 +    done
1.1421 +qed
1.1422 +
1.1423 +lemma contour_integral_primitive:
1.1424 +  assumes "\<And>x. x \<in> s \<Longrightarrow> (f has_field_derivative f' x) (at x within s)"
1.1425 +      and "valid_path g" "path_image g \<subseteq> s"
1.1426 +    shows "(f' has_contour_integral (f(pathfinish g) - f(pathstart g))) g"
1.1427 +  using assms
1.1428 +  apply (simp add: valid_path_def path_image_def pathfinish_def pathstart_def has_contour_integral_def)
1.1429 +  apply (auto intro!: piecewise_C1_imp_differentiable contour_integral_primitive_lemma [of 0 1 s])
1.1430 +  done
1.1431 +
1.1432 +corollary Cauchy_theorem_primitive:
1.1433 +  assumes "\<And>x. x \<in> s \<Longrightarrow> (f has_field_derivative f' x) (at x within s)"
1.1434 +      and "valid_path g"  "path_image g \<subseteq> s" "pathfinish g = pathstart g"
1.1435 +    shows "(f' has_contour_integral 0) g"
1.1436 +  using assms
1.1437 +  by (metis diff_self contour_integral_primitive)
1.1438 +
1.1439 +
1.1440 +text\<open>Existence of path integral for continuous function\<close>
1.1441 +lemma contour_integrable_continuous_linepath:
1.1442 +  assumes "continuous_on (closed_segment a b) f"
1.1443 +  shows "f contour_integrable_on (linepath a b)"
1.1444 +proof -
1.1445 +  have "continuous_on {0..1} ((\<lambda>x. f x * (b - a)) o linepath a b)"
1.1446 +    apply (rule continuous_on_compose [OF continuous_on_linepath], simp add: linepath_image_01)
1.1447 +    apply (rule continuous_intros | simp add: assms)+
1.1448 +    done
1.1449 +  then show ?thesis
1.1450 +    apply (simp add: contour_integrable_on_def has_contour_integral_def integrable_on_def [symmetric])
1.1451 +    apply (rule integrable_continuous [of 0 "1::real", simplified])
1.1452 +    apply (rule continuous_on_eq [where f = "\<lambda>x. f(linepath a b x)*(b - a)"])
1.1453 +    apply (auto simp: vector_derivative_linepath_within)
1.1454 +    done
1.1455 +qed
1.1456 +
1.1457 +lemma has_field_der_id: "((\<lambda>x. x\<^sup>2 / 2) has_field_derivative x) (at x)"
1.1458 +  by (rule has_derivative_imp_has_field_derivative)
1.1459 +     (rule derivative_intros | simp)+
1.1460 +
1.1461 +lemma contour_integral_id [simp]: "contour_integral (linepath a b) (\<lambda>y. y) = (b^2 - a^2)/2"
1.1462 +  apply (rule contour_integral_unique)
1.1463 +  using contour_integral_primitive [of UNIV "\<lambda>x. x^2/2" "\<lambda>x. x" "linepath a b"]
1.1464 +  apply (auto simp: field_simps has_field_der_id)
1.1465 +  done
1.1466 +
1.1467 +lemma contour_integrable_on_const [iff]: "(\<lambda>x. c) contour_integrable_on (linepath a b)"
1.1468 +  by (simp add: continuous_on_const contour_integrable_continuous_linepath)
1.1469 +
1.1470 +lemma contour_integrable_on_id [iff]: "(\<lambda>x. x) contour_integrable_on (linepath a b)"
1.1471 +  by (simp add: continuous_on_id contour_integrable_continuous_linepath)
1.1472 +
1.1473 +
1.1474 +subsection\<open>Arithmetical combining theorems\<close>
1.1475 +
1.1476 +lemma has_contour_integral_neg:
1.1477 +    "(f has_contour_integral i) g \<Longrightarrow> ((\<lambda>x. -(f x)) has_contour_integral (-i)) g"
1.1478 +  by (simp add: has_integral_neg has_contour_integral_def)
1.1479 +
1.1481 +    "\<lbrakk>(f1 has_contour_integral i1) g; (f2 has_contour_integral i2) g\<rbrakk>
1.1482 +     \<Longrightarrow> ((\<lambda>x. f1 x + f2 x) has_contour_integral (i1 + i2)) g"
1.1484 +
1.1485 +lemma has_contour_integral_diff:
1.1486 +  "\<lbrakk>(f1 has_contour_integral i1) g; (f2 has_contour_integral i2) g\<rbrakk>
1.1487 +         \<Longrightarrow> ((\<lambda>x. f1 x - f2 x) has_contour_integral (i1 - i2)) g"
1.1488 +  by (simp add: has_integral_sub has_contour_integral_def algebra_simps)
1.1489 +
1.1490 +lemma has_contour_integral_lmul:
1.1491 +  "(f has_contour_integral i) g \<Longrightarrow> ((\<lambda>x. c * (f x)) has_contour_integral (c*i)) g"
1.1493 +apply (drule has_integral_mult_right)
1.1495 +done
1.1496 +
1.1497 +lemma has_contour_integral_rmul:
1.1498 +  "(f has_contour_integral i) g \<Longrightarrow> ((\<lambda>x. (f x) * c) has_contour_integral (i*c)) g"
1.1499 +apply (drule has_contour_integral_lmul)
1.1501 +done
1.1502 +
1.1503 +lemma has_contour_integral_div:
1.1504 +  "(f has_contour_integral i) g \<Longrightarrow> ((\<lambda>x. f x/c) has_contour_integral (i/c)) g"
1.1505 +  by (simp add: field_class.field_divide_inverse) (metis has_contour_integral_rmul)
1.1506 +
1.1507 +lemma has_contour_integral_eq:
1.1508 +    "\<lbrakk>(f has_contour_integral y) p; \<And>x. x \<in> path_image p \<Longrightarrow> f x = g x\<rbrakk> \<Longrightarrow> (g has_contour_integral y) p"
1.1509 +apply (simp add: path_image_def has_contour_integral_def)
1.1510 +by (metis (no_types, lifting) image_eqI has_integral_eq)
1.1511 +
1.1512 +lemma has_contour_integral_bound_linepath:
1.1513 +  assumes "(f has_contour_integral i) (linepath a b)"
1.1514 +          "0 \<le> B" "\<And>x. x \<in> closed_segment a b \<Longrightarrow> norm(f x) \<le> B"
1.1515 +    shows "norm i \<le> B * norm(b - a)"
1.1516 +proof -
1.1517 +  { fix x::real
1.1518 +    assume x: "0 \<le> x" "x \<le> 1"
1.1519 +  have "norm (f (linepath a b x)) *
1.1520 +        norm (vector_derivative (linepath a b) (at x within {0..1})) \<le> B * norm (b - a)"
1.1521 +    by (auto intro: mult_mono simp: assms linepath_in_path of_real_linepath vector_derivative_linepath_within x)
1.1522 +  } note * = this
1.1523 +  have "norm i \<le> (B * norm (b - a)) * content (cbox 0 (1::real))"
1.1524 +    apply (rule has_integral_bound
1.1525 +       [of _ "\<lambda>x. f (linepath a b x) * vector_derivative (linepath a b) (at x within {0..1})"])
1.1526 +    using assms * unfolding has_contour_integral_def
1.1527 +    apply (auto simp: norm_mult)
1.1528 +    done
1.1529 +  then show ?thesis
1.1530 +    by (auto simp: content_real)
1.1531 +qed
1.1532 +
1.1533 +(*UNUSED
1.1534 +lemma has_contour_integral_bound_linepath_strong:
1.1535 +  fixes a :: real and f :: "complex \<Rightarrow> real"
1.1536 +  assumes "(f has_contour_integral i) (linepath a b)"
1.1537 +          "finite k"
1.1538 +          "0 \<le> B" "\<And>x::real. x \<in> closed_segment a b - k \<Longrightarrow> norm(f x) \<le> B"
1.1539 +    shows "norm i \<le> B*norm(b - a)"
1.1540 +*)
1.1541 +
1.1542 +lemma has_contour_integral_const_linepath: "((\<lambda>x. c) has_contour_integral c*(b - a))(linepath a b)"
1.1543 +  unfolding has_contour_integral_linepath
1.1544 +  by (metis content_real diff_0_right has_integral_const_real lambda_one of_real_1 scaleR_conv_of_real zero_le_one)
1.1545 +
1.1546 +lemma has_contour_integral_0: "((\<lambda>x. 0) has_contour_integral 0) g"
1.1547 +  by (simp add: has_contour_integral_def)
1.1548 +
1.1549 +lemma has_contour_integral_is_0:
1.1550 +    "(\<And>z. z \<in> path_image g \<Longrightarrow> f z = 0) \<Longrightarrow> (f has_contour_integral 0) g"
1.1551 +  by (rule has_contour_integral_eq [OF has_contour_integral_0]) auto
1.1552 +
1.1553 +lemma has_contour_integral_setsum:
1.1554 +    "\<lbrakk>finite s; \<And>a. a \<in> s \<Longrightarrow> (f a has_contour_integral i a) p\<rbrakk>
1.1555 +     \<Longrightarrow> ((\<lambda>x. setsum (\<lambda>a. f a x) s) has_contour_integral setsum i s) p"
1.1556 +  by (induction s rule: finite_induct) (auto simp: has_contour_integral_0 has_contour_integral_add)
1.1557 +
1.1558 +
1.1559 +subsection \<open>Operations on path integrals\<close>
1.1560 +
1.1561 +lemma contour_integral_const_linepath [simp]: "contour_integral (linepath a b) (\<lambda>x. c) = c*(b - a)"
1.1562 +  by (rule contour_integral_unique [OF has_contour_integral_const_linepath])
1.1563 +
1.1564 +lemma contour_integral_neg:
1.1565 +    "f contour_integrable_on g \<Longrightarrow> contour_integral g (\<lambda>x. -(f x)) = -(contour_integral g f)"
1.1566 +  by (simp add: contour_integral_unique has_contour_integral_integral has_contour_integral_neg)
1.1567 +
1.1569 +    "f1 contour_integrable_on g \<Longrightarrow> f2 contour_integrable_on g \<Longrightarrow> contour_integral g (\<lambda>x. f1 x + f2 x) =
1.1570 +                contour_integral g f1 + contour_integral g f2"
1.1572 +
1.1573 +lemma contour_integral_diff:
1.1574 +    "f1 contour_integrable_on g \<Longrightarrow> f2 contour_integrable_on g \<Longrightarrow> contour_integral g (\<lambda>x. f1 x - f2 x) =
1.1575 +                contour_integral g f1 - contour_integral g f2"
1.1576 +  by (simp add: contour_integral_unique has_contour_integral_integral has_contour_integral_diff)
1.1577 +
1.1578 +lemma contour_integral_lmul:
1.1579 +  shows "f contour_integrable_on g
1.1580 +           \<Longrightarrow> contour_integral g (\<lambda>x. c * f x) = c*contour_integral g f"
1.1581 +  by (simp add: contour_integral_unique has_contour_integral_integral has_contour_integral_lmul)
1.1582 +
1.1583 +lemma contour_integral_rmul:
1.1584 +  shows "f contour_integrable_on g
1.1585 +        \<Longrightarrow> contour_integral g (\<lambda>x. f x * c) = contour_integral g f * c"
1.1586 +  by (simp add: contour_integral_unique has_contour_integral_integral has_contour_integral_rmul)
1.1587 +
1.1588 +lemma contour_integral_div:
1.1589 +  shows "f contour_integrable_on g
1.1590 +        \<Longrightarrow> contour_integral g (\<lambda>x. f x / c) = contour_integral g f / c"
1.1591 +  by (simp add: contour_integral_unique has_contour_integral_integral has_contour_integral_div)
1.1592 +
1.1593 +lemma contour_integral_eq:
1.1594 +    "(\<And>x. x \<in> path_image p \<Longrightarrow> f x = g x) \<Longrightarrow> contour_integral p f = contour_integral p g"
1.1595 +  apply (simp add: contour_integral_def)
1.1596 +  using has_contour_integral_eq
1.1597 +  by (metis contour_integral_unique has_contour_integral_integrable has_contour_integral_integral)
1.1598 +
1.1599 +lemma contour_integral_eq_0:
1.1600 +    "(\<And>z. z \<in> path_image g \<Longrightarrow> f z = 0) \<Longrightarrow> contour_integral g f = 0"
1.1601 +  by (simp add: has_contour_integral_is_0 contour_integral_unique)
1.1602 +
1.1603 +lemma contour_integral_bound_linepath:
1.1604 +  shows
1.1605 +    "\<lbrakk>f contour_integrable_on (linepath a b);
1.1606 +      0 \<le> B; \<And>x. x \<in> closed_segment a b \<Longrightarrow> norm(f x) \<le> B\<rbrakk>
1.1607 +     \<Longrightarrow> norm(contour_integral (linepath a b) f) \<le> B*norm(b - a)"
1.1608 +  apply (rule has_contour_integral_bound_linepath [of f])
1.1609 +  apply (auto simp: has_contour_integral_integral)
1.1610 +  done
1.1611 +
1.1612 +lemma contour_integral_0 [simp]: "contour_integral g (\<lambda>x. 0) = 0"
1.1613 +  by (simp add: contour_integral_unique has_contour_integral_0)
1.1614 +
1.1615 +lemma contour_integral_setsum:
1.1616 +    "\<lbrakk>finite s; \<And>a. a \<in> s \<Longrightarrow> (f a) contour_integrable_on p\<rbrakk>
1.1617 +     \<Longrightarrow> contour_integral p (\<lambda>x. setsum (\<lambda>a. f a x) s) = setsum (\<lambda>a. contour_integral p (f a)) s"
1.1618 +  by (auto simp: contour_integral_unique has_contour_integral_setsum has_contour_integral_integral)
1.1619 +
1.1620 +lemma contour_integrable_eq:
1.1621 +    "\<lbrakk>f contour_integrable_on p; \<And>x. x \<in> path_image p \<Longrightarrow> f x = g x\<rbrakk> \<Longrightarrow> g contour_integrable_on p"
1.1622 +  unfolding contour_integrable_on_def
1.1623 +  by (metis has_contour_integral_eq)
1.1624 +
1.1625 +
1.1626 +subsection \<open>Arithmetic theorems for path integrability\<close>
1.1627 +
1.1628 +lemma contour_integrable_neg:
1.1629 +    "f contour_integrable_on g \<Longrightarrow> (\<lambda>x. -(f x)) contour_integrable_on g"
1.1630 +  using has_contour_integral_neg contour_integrable_on_def by blast
1.1631 +
1.1633 +    "\<lbrakk>f1 contour_integrable_on g; f2 contour_integrable_on g\<rbrakk> \<Longrightarrow> (\<lambda>x. f1 x + f2 x) contour_integrable_on g"
1.1635 +  by fastforce
1.1636 +
1.1637 +lemma contour_integrable_diff:
1.1638 +    "\<lbrakk>f1 contour_integrable_on g; f2 contour_integrable_on g\<rbrakk> \<Longrightarrow> (\<lambda>x. f1 x - f2 x) contour_integrable_on g"
1.1639 +  using has_contour_integral_diff contour_integrable_on_def
1.1640 +  by fastforce
1.1641 +
1.1642 +lemma contour_integrable_lmul:
1.1643 +    "f contour_integrable_on g \<Longrightarrow> (\<lambda>x. c * f x) contour_integrable_on g"
1.1644 +  using has_contour_integral_lmul contour_integrable_on_def
1.1645 +  by fastforce
1.1646 +
1.1647 +lemma contour_integrable_rmul:
1.1648 +    "f contour_integrable_on g \<Longrightarrow> (\<lambda>x. f x * c) contour_integrable_on g"
1.1649 +  using has_contour_integral_rmul contour_integrable_on_def
1.1650 +  by fastforce
1.1651 +
1.1652 +lemma contour_integrable_div:
1.1653 +    "f contour_integrable_on g \<Longrightarrow> (\<lambda>x. f x / c) contour_integrable_on g"
1.1654 +  using has_contour_integral_div contour_integrable_on_def
1.1655 +  by fastforce
1.1656 +
1.1657 +lemma contour_integrable_setsum:
1.1658 +    "\<lbrakk>finite s; \<And>a. a \<in> s \<Longrightarrow> (f a) contour_integrable_on p\<rbrakk>
1.1659 +     \<Longrightarrow> (\<lambda>x. setsum (\<lambda>a. f a x) s) contour_integrable_on p"
1.1660 +   unfolding contour_integrable_on_def
1.1661 +   by (metis has_contour_integral_setsum)
1.1662 +
1.1663 +
1.1664 +subsection\<open>Reversing a path integral\<close>
1.1665 +
1.1666 +lemma has_contour_integral_reverse_linepath:
1.1667 +    "(f has_contour_integral i) (linepath a b)
1.1668 +     \<Longrightarrow> (f has_contour_integral (-i)) (linepath b a)"
1.1669 +  using has_contour_integral_reversepath valid_path_linepath by fastforce
1.1670 +
1.1671 +lemma contour_integral_reverse_linepath:
1.1672 +    "continuous_on (closed_segment a b) f
1.1673 +     \<Longrightarrow> contour_integral (linepath a b) f = - (contour_integral(linepath b a) f)"
1.1674 +apply (rule contour_integral_unique)
1.1675 +apply (rule has_contour_integral_reverse_linepath)
1.1676 +by (simp add: closed_segment_commute contour_integrable_continuous_linepath has_contour_integral_integral)
1.1677 +
1.1678 +
1.1679 +(* Splitting a path integral in a flat way.*)
1.1680 +
1.1681 +lemma has_contour_integral_split:
1.1682 +  assumes f: "(f has_contour_integral i) (linepath a c)" "(f has_contour_integral j) (linepath c b)"
1.1683 +      and k: "0 \<le> k" "k \<le> 1"
1.1684 +      and c: "c - a = k *\<^sub>R (b - a)"
1.1685 +    shows "(f has_contour_integral (i + j)) (linepath a b)"
1.1686 +proof (cases "k = 0 \<or> k = 1")
1.1687 +  case True
1.1688 +  then show ?thesis
1.1689 +    using assms
1.1690 +    apply auto
1.1691 +    apply (metis add.left_neutral has_contour_integral_trivial has_contour_integral_unique)
1.1692 +    apply (metis add.right_neutral has_contour_integral_trivial has_contour_integral_unique)
1.1693 +    done
1.1694 +next
1.1695 +  case False
1.1696 +  then have k: "0 < k" "k < 1" "complex_of_real k \<noteq> 1"
1.1697 +    using assms apply auto
1.1698 +    using of_real_eq_iff by fastforce
1.1699 +  have c': "c = k *\<^sub>R (b - a) + a"
1.1700 +    by (metis diff_add_cancel c)
1.1701 +  have bc: "(b - c) = (1 - k) *\<^sub>R (b - a)"
1.1702 +    by (simp add: algebra_simps c')
1.1703 +  { assume *: "((\<lambda>x. f ((1 - x) *\<^sub>R a + x *\<^sub>R c) * (c - a)) has_integral i) {0..1}"
1.1704 +    have **: "\<And>x. ((k - x) / k) *\<^sub>R a + (x / k) *\<^sub>R c = (1 - x) *\<^sub>R a + x *\<^sub>R b"
1.1705 +      using False
1.1706 +      apply (simp add: c' algebra_simps)
1.1707 +      apply (simp add: real_vector.scale_left_distrib [symmetric] divide_simps)
1.1708 +      done
1.1709 +    have "((\<lambda>x. f ((1 - x) *\<^sub>R a + x *\<^sub>R b) * (b - a)) has_integral i) {0..k}"
1.1710 +      using * k
1.1711 +      apply -
1.1712 +      apply (drule has_integral_affinity [of _ _ 0 "1::real" "inverse k" "0", simplified])
1.1713 +      apply (simp_all add: divide_simps mult.commute [of _ "k"] image_affinity_atLeastAtMost ** c)
1.1714 +      apply (drule has_integral_cmul [where c = "inverse k"])
1.1715 +      apply (simp add: has_integral_cmul)
1.1716 +      done
1.1717 +  } note fi = this
1.1718 +  { assume *: "((\<lambda>x. f ((1 - x) *\<^sub>R c + x *\<^sub>R b) * (b - c)) has_integral j) {0..1}"
1.1719 +    have **: "\<And>x. (((1 - x) / (1 - k)) *\<^sub>R c + ((x - k) / (1 - k)) *\<^sub>R b) = ((1 - x) *\<^sub>R a + x *\<^sub>R b)"
1.1720 +      using k
1.1721 +      apply (simp add: c' field_simps)
1.1722 +      apply (simp add: scaleR_conv_of_real divide_simps)
1.1723 +      apply (simp add: field_simps)
1.1724 +      done
1.1725 +    have "((\<lambda>x. f ((1 - x) *\<^sub>R a + x *\<^sub>R b) * (b - a)) has_integral j) {k..1}"
1.1726 +      using * k
1.1727 +      apply -
1.1728 +      apply (drule has_integral_affinity [of _ _ 0 "1::real" "inverse(1 - k)" "-(k/(1 - k))", simplified])
1.1729 +      apply (simp_all add: divide_simps mult.commute [of _ "1-k"] image_affinity_atLeastAtMost ** bc)
1.1730 +      apply (drule has_integral_cmul [where k = "(1 - k) *\<^sub>R j" and c = "inverse (1 - k)"])
1.1731 +      apply (simp add: has_integral_cmul)
1.1732 +      done
1.1733 +  } note fj = this
1.1734 +  show ?thesis
1.1735 +    using f k
1.1736 +    apply (simp add: has_contour_integral_linepath)
1.1737 +    apply (simp add: linepath_def)
1.1738 +    apply (rule has_integral_combine [OF _ _ fi fj], simp_all)
1.1739 +    done
1.1740 +qed
1.1741 +
1.1742 +lemma continuous_on_closed_segment_transform:
1.1743 +  assumes f: "continuous_on (closed_segment a b) f"
1.1744 +      and k: "0 \<le> k" "k \<le> 1"
1.1745 +      and c: "c - a = k *\<^sub>R (b - a)"
1.1746 +    shows "continuous_on (closed_segment a c) f"
1.1747 +proof -
1.1748 +  have c': "c = (1 - k) *\<^sub>R a + k *\<^sub>R b"
1.1749 +    using c by (simp add: algebra_simps)
1.1750 +  show "continuous_on (closed_segment a c) f"
1.1751 +    apply (rule continuous_on_subset [OF f])
1.1752 +    apply (simp add: segment_convex_hull)
1.1753 +    apply (rule convex_hull_subset)
1.1754 +    using assms
1.1755 +    apply (auto simp: hull_inc c' Convex.convexD_alt)
1.1756 +    done
1.1757 +qed
1.1758 +
1.1759 +lemma contour_integral_split:
1.1760 +  assumes f: "continuous_on (closed_segment a b) f"
1.1761 +      and k: "0 \<le> k" "k \<le> 1"
1.1762 +      and c: "c - a = k *\<^sub>R (b - a)"
1.1763 +    shows "contour_integral(linepath a b) f = contour_integral(linepath a c) f + contour_integral(linepath c b) f"
1.1764 +proof -
1.1765 +  have c': "c = (1 - k) *\<^sub>R a + k *\<^sub>R b"
1.1766 +    using c by (simp add: algebra_simps)
1.1767 +  have *: "continuous_on (closed_segment a c) f" "continuous_on (closed_segment c b) f"
1.1768 +    apply (rule_tac [!] continuous_on_subset [OF f])
1.1769 +    apply (simp_all add: segment_convex_hull)
1.1770 +    apply (rule_tac [!] convex_hull_subset)
1.1771 +    using assms
1.1772 +    apply (auto simp: hull_inc c' Convex.convexD_alt)
1.1773 +    done
1.1774 +  show ?thesis
1.1775 +    apply (rule contour_integral_unique)
1.1776 +    apply (rule has_contour_integral_split [OF has_contour_integral_integral has_contour_integral_integral k c])
1.1777 +    apply (rule contour_integrable_continuous_linepath *)+
1.1778 +    done
1.1779 +qed
1.1780 +
1.1781 +lemma contour_integral_split_linepath:
1.1782 +  assumes f: "continuous_on (closed_segment a b) f"
1.1783 +      and c: "c \<in> closed_segment a b"
1.1784 +    shows "contour_integral(linepath a b) f = contour_integral(linepath a c) f + contour_integral(linepath c b) f"
1.1785 +  using c
1.1786 +  by (auto simp: closed_segment_def algebra_simps intro!: contour_integral_split [OF f])
1.1787 +
1.1788 +(* The special case of midpoints used in the main quadrisection.*)
1.1789 +
1.1790 +lemma has_contour_integral_midpoint:
1.1791 +  assumes "(f has_contour_integral i) (linepath a (midpoint a b))"
1.1792 +          "(f has_contour_integral j) (linepath (midpoint a b) b)"
1.1793 +    shows "(f has_contour_integral (i + j)) (linepath a b)"
1.1794 +  apply (rule has_contour_integral_split [where c = "midpoint a b" and k = "1/2"])
1.1795 +  using assms
1.1796 +  apply (auto simp: midpoint_def algebra_simps scaleR_conv_of_real)
1.1797 +  done
1.1798 +
1.1799 +lemma contour_integral_midpoint:
1.1800 +   "continuous_on (closed_segment a b) f
1.1801 +    \<Longrightarrow> contour_integral (linepath a b) f =
1.1802 +        contour_integral (linepath a (midpoint a b)) f + contour_integral (linepath (midpoint a b) b) f"
1.1803 +  apply (rule contour_integral_split [where c = "midpoint a b" and k = "1/2"])
1.1804 +  apply (auto simp: midpoint_def algebra_simps scaleR_conv_of_real)
1.1805 +  done
1.1806 +
1.1807 +
1.1808 +text\<open>A couple of special case lemmas that are useful below\<close>
1.1809 +
1.1810 +lemma triangle_linear_has_chain_integral:
1.1811 +    "((\<lambda>x. m*x + d) has_contour_integral 0) (linepath a b +++ linepath b c +++ linepath c a)"
1.1812 +  apply (rule Cauchy_theorem_primitive [of UNIV "\<lambda>x. m/2 * x^2 + d*x"])
1.1813 +  apply (auto intro!: derivative_eq_intros)
1.1814 +  done
1.1815 +
1.1816 +lemma has_chain_integral_chain_integral3:
1.1817 +     "(f has_contour_integral i) (linepath a b +++ linepath b c +++ linepath c d)
1.1818 +      \<Longrightarrow> contour_integral (linepath a b) f + contour_integral (linepath b c) f + contour_integral (linepath c d) f = i"
1.1819 +  apply (subst contour_integral_unique [symmetric], assumption)
1.1820 +  apply (drule has_contour_integral_integrable)
1.1821 +  apply (simp add: valid_path_join)
1.1822 +  done
1.1823 +
1.1824 +lemma has_chain_integral_chain_integral4:
1.1825 +     "(f has_contour_integral i) (linepath a b +++ linepath b c +++ linepath c d +++ linepath d e)
1.1826 +      \<Longrightarrow> contour_integral (linepath a b) f + contour_integral (linepath b c) f + contour_integral (linepath c d) f + contour_integral (linepath d e) f = i"
1.1827 +  apply (subst contour_integral_unique [symmetric], assumption)
1.1828 +  apply (drule has_contour_integral_integrable)
1.1829 +  apply (simp add: valid_path_join)
1.1830 +  done
1.1831 +
1.1832 +subsection\<open>Reversing the order in a double path integral\<close>
1.1833 +
1.1834 +text\<open>The condition is stronger than needed but it's often true in typical situations\<close>
1.1835 +
1.1836 +lemma fst_im_cbox [simp]: "cbox c d \<noteq> {} \<Longrightarrow> (fst ` cbox (a,c) (b,d)) = cbox a b"
1.1837 +  by (auto simp: cbox_Pair_eq)
1.1838 +
1.1839 +lemma snd_im_cbox [simp]: "cbox a b \<noteq> {} \<Longrightarrow> (snd ` cbox (a,c) (b,d)) = cbox c d"
1.1840 +  by (auto simp: cbox_Pair_eq)
1.1841 +
1.1842 +lemma contour_integral_swap:
1.1843 +  assumes fcon:  "continuous_on (path_image g \<times> path_image h) (\<lambda>(y1,y2). f y1 y2)"
1.1844 +      and vp:    "valid_path g" "valid_path h"
1.1845 +      and gvcon: "continuous_on {0..1} (\<lambda>t. vector_derivative g (at t))"
1.1846 +      and hvcon: "continuous_on {0..1} (\<lambda>t. vector_derivative h (at t))"
1.1847 +  shows "contour_integral g (\<lambda>w. contour_integral h (f w)) =
1.1848 +         contour_integral h (\<lambda>z. contour_integral g (\<lambda>w. f w z))"
1.1849 +proof -
1.1850 +  have gcon: "continuous_on {0..1} g" and hcon: "continuous_on {0..1} h"
1.1851 +    using assms by (auto simp: valid_path_def piecewise_C1_differentiable_on_def)
1.1852 +  have fgh1: "\<And>x. (\<lambda>t. f (g x) (h t)) = (\<lambda>(y1,y2). f y1 y2) o (\<lambda>t. (g x, h t))"
1.1853 +    by (rule ext) simp
1.1854 +  have fgh2: "\<And>x. (\<lambda>t. f (g t) (h x)) = (\<lambda>(y1,y2). f y1 y2) o (\<lambda>t. (g t, h x))"
1.1855 +    by (rule ext) simp
1.1856 +  have fcon_im1: "\<And>x. 0 \<le> x \<Longrightarrow> x \<le> 1 \<Longrightarrow> continuous_on ((\<lambda>t. (g x, h t)) ` {0..1}) (\<lambda>(x, y). f x y)"
1.1857 +    by (rule continuous_on_subset [OF fcon]) (auto simp: path_image_def)
1.1858 +  have fcon_im2: "\<And>x. 0 \<le> x \<Longrightarrow> x \<le> 1 \<Longrightarrow> continuous_on ((\<lambda>t. (g t, h x)) ` {0..1}) (\<lambda>(x, y). f x y)"
1.1859 +    by (rule continuous_on_subset [OF fcon]) (auto simp: path_image_def)
1.1860 +  have vdg: "\<And>y. y \<in> {0..1} \<Longrightarrow> (\<lambda>x. f (g x) (h y) * vector_derivative g (at x)) integrable_on {0..1}"
1.1861 +    apply (rule integrable_continuous_real)
1.1862 +    apply (rule continuous_on_mult [OF _ gvcon])
1.1863 +    apply (subst fgh2)
1.1864 +    apply (rule fcon_im2 gcon continuous_intros | simp)+
1.1865 +    done
1.1866 +  have "(\<lambda>z. vector_derivative g (at (fst z))) = (\<lambda>x. vector_derivative g (at x)) o fst"
1.1867 +    by auto
1.1868 +  then have gvcon': "continuous_on (cbox (0, 0) (1, 1::real)) (\<lambda>x. vector_derivative g (at (fst x)))"
1.1869 +    apply (rule ssubst)
1.1870 +    apply (rule continuous_intros | simp add: gvcon)+
1.1871 +    done
1.1872 +  have "(\<lambda>z. vector_derivative h (at (snd z))) = (\<lambda>x. vector_derivative h (at x)) o snd"
1.1873 +    by auto
1.1874 +  then have hvcon': "continuous_on (cbox (0, 0) (1::real, 1)) (\<lambda>x. vector_derivative h (at (snd x)))"
1.1875 +    apply (rule ssubst)
1.1876 +    apply (rule continuous_intros | simp add: hvcon)+
1.1877 +    done
1.1878 +  have "(\<lambda>x. f (g (fst x)) (h (snd x))) = (\<lambda>(y1,y2). f y1 y2) o (\<lambda>w. ((g o fst) w, (h o snd) w))"
1.1879 +    by auto
1.1880 +  then have fgh: "continuous_on (cbox (0, 0) (1, 1)) (\<lambda>x. f (g (fst x)) (h (snd x)))"
1.1881 +    apply (rule ssubst)
1.1882 +    apply (rule gcon hcon continuous_intros | simp)+
1.1883 +    apply (auto simp: path_image_def intro: continuous_on_subset [OF fcon])
1.1884 +    done
1.1885 +  have "integral {0..1} (\<lambda>x. contour_integral h (f (g x)) * vector_derivative g (at x)) =
1.1886 +        integral {0..1} (\<lambda>x. contour_integral h (\<lambda>y. f (g x) y * vector_derivative g (at x)))"
1.1887 +    apply (rule integral_cong [OF contour_integral_rmul [symmetric]])
1.1888 +    apply (clarsimp simp: contour_integrable_on)
1.1889 +    apply (rule integrable_continuous_real)
1.1890 +    apply (rule continuous_on_mult [OF _ hvcon])
1.1891 +    apply (subst fgh1)
1.1892 +    apply (rule fcon_im1 hcon continuous_intros | simp)+
1.1893 +    done
1.1894 +  also have "... = integral {0..1}
1.1895 +                     (\<lambda>y. contour_integral g (\<lambda>x. f x (h y) * vector_derivative h (at y)))"
1.1896 +    apply (simp only: contour_integral_integral)
1.1897 +    apply (subst integral_swap_continuous [where 'a = real and 'b = real, of 0 0 1 1, simplified])
1.1898 +     apply (rule fgh gvcon' hvcon' continuous_intros | simp add: split_def)+
1.1899 +    unfolding integral_mult_left [symmetric]
1.1900 +    apply (simp only: mult_ac)
1.1901 +    done
1.1902 +  also have "... = contour_integral h (\<lambda>z. contour_integral g (\<lambda>w. f w z))"
1.1903 +    apply (simp add: contour_integral_integral)
1.1904 +    apply (rule integral_cong)
1.1905 +    unfolding integral_mult_left [symmetric]
1.1906 +    apply (simp add: algebra_simps)
1.1907 +    done
1.1908 +  finally show ?thesis
1.1909 +    by (simp add: contour_integral_integral)
1.1910 +qed
1.1911 +
1.1912 +
1.1914 +
1.1915 +lemma norm_sum_half:
1.1916 +  assumes "norm(a + b) >= e"
1.1917 +    shows "norm a >= e/2 \<or> norm b >= e/2"
1.1918 +proof -
1.1919 +  have "e \<le> norm (- a - b)"
1.1921 +  thus ?thesis
1.1922 +    using norm_triangle_ineq4 order_trans by fastforce
1.1923 +qed
1.1924 +
1.1925 +lemma norm_sum_lemma:
1.1926 +  assumes "e \<le> norm (a + b + c + d)"
1.1927 +    shows "e / 4 \<le> norm a \<or> e / 4 \<le> norm b \<or> e / 4 \<le> norm c \<or> e / 4 \<le> norm d"
1.1928 +proof -
1.1929 +  have "e \<le> norm ((a + b) + (c + d))" using assms
1.1930 +    by (simp add: algebra_simps)
1.1931 +  then show ?thesis
1.1932 +    by (auto dest!: norm_sum_half)
1.1933 +qed
1.1934 +
1.1936 +  assumes f: "continuous_on (convex hull {a,b,c}) f"
1.1937 +      and dist: "dist a b \<le> K" "dist b c \<le> K" "dist c a \<le> K"
1.1938 +      and e: "e * K^2 \<le>
1.1939 +              norm (contour_integral(linepath a b) f + contour_integral(linepath b c) f + contour_integral(linepath c a) f)"
1.1940 +  shows "\<exists>a' b' c'.
1.1941 +           a' \<in> convex hull {a,b,c} \<and> b' \<in> convex hull {a,b,c} \<and> c' \<in> convex hull {a,b,c} \<and>
1.1942 +           dist a' b' \<le> K/2  \<and>  dist b' c' \<le> K/2  \<and>  dist c' a' \<le> K/2  \<and>
1.1943 +           e * (K/2)^2 \<le> norm(contour_integral(linepath a' b') f + contour_integral(linepath b' c') f + contour_integral(linepath c' a') f)"
1.1944 +proof -
1.1945 +  note divide_le_eq_numeral1 [simp del]
1.1946 +  define a' where "a' = midpoint b c"
1.1947 +  define b' where "b' = midpoint c a"
1.1948 +  define c' where "c' = midpoint a b"
1.1949 +  have fabc: "continuous_on (closed_segment a b) f" "continuous_on (closed_segment b c) f" "continuous_on (closed_segment c a) f"
1.1950 +    using f continuous_on_subset segments_subset_convex_hull by metis+
1.1951 +  have fcont': "continuous_on (closed_segment c' b') f"
1.1952 +               "continuous_on (closed_segment a' c') f"
1.1953 +               "continuous_on (closed_segment b' a') f"
1.1954 +    unfolding a'_def b'_def c'_def
1.1955 +    apply (rule continuous_on_subset [OF f],
1.1956 +           metis midpoints_in_convex_hull convex_hull_subset hull_subset insert_subset segment_convex_hull)+
1.1957 +    done
1.1958 +  let ?pathint = "\<lambda>x y. contour_integral(linepath x y) f"
1.1959 +  have *: "?pathint a b + ?pathint b c + ?pathint c a =
1.1960 +          (?pathint a c' + ?pathint c' b' + ?pathint b' a) +
1.1961 +          (?pathint a' c' + ?pathint c' b + ?pathint b a') +
1.1962 +          (?pathint a' c + ?pathint c b' + ?pathint b' a') +
1.1963 +          (?pathint a' b' + ?pathint b' c' + ?pathint c' a')"
1.1964 +    apply (simp add: fcont' contour_integral_reverse_linepath)
1.1965 +    apply (simp add: a'_def b'_def c'_def contour_integral_midpoint fabc)
1.1966 +    done
1.1967 +  have [simp]: "\<And>x y. cmod (x * 2 - y * 2) = cmod (x - y) * 2"
1.1968 +    by (metis left_diff_distrib mult.commute norm_mult_numeral1)
1.1969 +  have [simp]: "\<And>x y. cmod (x - y) = cmod (y - x)"
1.1970 +    by (simp add: norm_minus_commute)
1.1971 +  consider "e * K\<^sup>2 / 4 \<le> cmod (?pathint a c' + ?pathint c' b' + ?pathint b' a)" |
1.1972 +           "e * K\<^sup>2 / 4 \<le> cmod (?pathint a' c' + ?pathint c' b + ?pathint b a')" |
1.1973 +           "e * K\<^sup>2 / 4 \<le> cmod (?pathint a' c + ?pathint c b' + ?pathint b' a')" |
1.1974 +           "e * K\<^sup>2 / 4 \<le> cmod (?pathint a' b' + ?pathint b' c' + ?pathint c' a')"
1.1975 +    using assms
1.1976 +    apply (simp only: *)
1.1977 +    apply (blast intro: that dest!: norm_sum_lemma)
1.1978 +    done
1.1979 +  then show ?thesis
1.1980 +  proof cases
1.1981 +    case 1 then show ?thesis
1.1982 +      apply (rule_tac x=a in exI)
1.1983 +      apply (rule exI [where x=c'])
1.1984 +      apply (rule exI [where x=b'])
1.1985 +      using assms
1.1986 +      apply (auto simp: a'_def c'_def b'_def midpoints_in_convex_hull hull_subset [THEN subsetD])
1.1987 +      apply (auto simp: midpoint_def dist_norm scaleR_conv_of_real divide_simps)
1.1988 +      done
1.1989 +  next
1.1990 +    case 2 then show ?thesis
1.1991 +      apply (rule_tac x=a' in exI)
1.1992 +      apply (rule exI [where x=c'])
1.1993 +      apply (rule exI [where x=b])
1.1994 +      using assms
1.1995 +      apply (auto simp: a'_def c'_def b'_def midpoints_in_convex_hull hull_subset [THEN subsetD])
1.1996 +      apply (auto simp: midpoint_def dist_norm scaleR_conv_of_real divide_simps)
1.1997 +      done
1.1998 +  next
1.1999 +    case 3 then show ?thesis
1.2000 +      apply (rule_tac x=a' in exI)
1.2001 +      apply (rule exI [where x=c])
1.2002 +      apply (rule exI [where x=b'])
1.2003 +      using assms
1.2004 +      apply (auto simp: a'_def c'_def b'_def midpoints_in_convex_hull hull_subset [THEN subsetD])
1.2005 +      apply (auto simp: midpoint_def dist_norm scaleR_conv_of_real divide_simps)
1.2006 +      done
1.2007 +  next
1.2008 +    case 4 then show ?thesis
1.2009 +      apply (rule_tac x=a' in exI)
1.2010 +      apply (rule exI [where x=b'])
1.2011 +      apply (rule exI [where x=c'])
1.2012 +      using assms
1.2013 +      apply (auto simp: a'_def c'_def b'_def midpoints_in_convex_hull hull_subset [THEN subsetD])
1.2014 +      apply (auto simp: midpoint_def dist_norm scaleR_conv_of_real divide_simps)
1.2015 +      done
1.2016 +  qed
1.2017 +qed
1.2018 +
1.2019 +subsection\<open>Cauchy's theorem for triangles\<close>
1.2020 +
1.2021 +lemma triangle_points_closer:
1.2022 +  fixes a::complex
1.2023 +  shows "\<lbrakk>x \<in> convex hull {a,b,c};  y \<in> convex hull {a,b,c}\<rbrakk>
1.2024 +         \<Longrightarrow> norm(x - y) \<le> norm(a - b) \<or>
1.2025 +             norm(x - y) \<le> norm(b - c) \<or>
1.2026 +             norm(x - y) \<le> norm(c - a)"
1.2027 +  using simplex_extremal_le [of "{a,b,c}"]
1.2028 +  by (auto simp: norm_minus_commute)
1.2029 +
1.2030 +lemma holomorphic_point_small_triangle:
1.2031 +  assumes x: "x \<in> s"
1.2032 +      and f: "continuous_on s f"
1.2033 +      and cd: "f field_differentiable (at x within s)"
1.2034 +      and e: "0 < e"
1.2035 +    shows "\<exists>k>0. \<forall>a b c. dist a b \<le> k \<and> dist b c \<le> k \<and> dist c a \<le> k \<and>
1.2036 +              x \<in> convex hull {a,b,c} \<and> convex hull {a,b,c} \<subseteq> s
1.2037 +              \<longrightarrow> norm(contour_integral(linepath a b) f + contour_integral(linepath b c) f +
1.2038 +                       contour_integral(linepath c a) f)
1.2039 +                  \<le> e*(dist a b + dist b c + dist c a)^2"
1.2040 +           (is "\<exists>k>0. \<forall>a b c. _ \<longrightarrow> ?normle a b c")
1.2041 +proof -
1.2042 +  have le_of_3: "\<And>a x y z. \<lbrakk>0 \<le> x*y; 0 \<le> x*z; 0 \<le> y*z; a \<le> (e*(x + y + z))*x + (e*(x + y + z))*y + (e*(x + y + z))*z\<rbrakk>
1.2043 +                     \<Longrightarrow> a \<le> e*(x + y + z)^2"
1.2044 +    by (simp add: algebra_simps power2_eq_square)
1.2045 +  have disj_le: "\<lbrakk>x \<le> a \<or> x \<le> b \<or> x \<le> c; 0 \<le> a; 0 \<le> b; 0 \<le> c\<rbrakk> \<Longrightarrow> x \<le> a + b + c"
1.2046 +             for x::real and a b c
1.2047 +    by linarith
1.2048 +  have fabc: "f contour_integrable_on linepath a b" "f contour_integrable_on linepath b c" "f contour_integrable_on linepath c a"
1.2049 +              if "convex hull {a, b, c} \<subseteq> s" for a b c
1.2050 +    using segments_subset_convex_hull that
1.2051 +    by (metis continuous_on_subset f contour_integrable_continuous_linepath)+
1.2052 +  note path_bound = has_contour_integral_bound_linepath [simplified norm_minus_commute, OF has_contour_integral_integral]
1.2053 +  { fix f' a b c d
1.2054 +    assume d: "0 < d"
1.2055 +       and f': "\<And>y. \<lbrakk>cmod (y - x) \<le> d; y \<in> s\<rbrakk> \<Longrightarrow> cmod (f y - f x - f' * (y - x)) \<le> e * cmod (y - x)"
1.2056 +       and le: "cmod (a - b) \<le> d" "cmod (b - c) \<le> d" "cmod (c - a) \<le> d"
1.2057 +       and xc: "x \<in> convex hull {a, b, c}"
1.2058 +       and s: "convex hull {a, b, c} \<subseteq> s"
1.2059 +    have pa: "contour_integral (linepath a b) f + contour_integral (linepath b c) f + contour_integral (linepath c a) f =
1.2060 +              contour_integral (linepath a b) (\<lambda>y. f y - f x - f'*(y - x)) +
1.2061 +              contour_integral (linepath b c) (\<lambda>y. f y - f x - f'*(y - x)) +
1.2062 +              contour_integral (linepath c a) (\<lambda>y. f y - f x - f'*(y - x))"
1.2063 +      apply (simp add: contour_integral_diff contour_integral_lmul contour_integrable_lmul contour_integrable_diff fabc [OF s])
1.2064 +      apply (simp add: field_simps)
1.2065 +      done
1.2066 +    { fix y
1.2067 +      assume yc: "y \<in> convex hull {a,b,c}"
1.2068 +      have "cmod (f y - f x - f' * (y - x)) \<le> e*norm(y - x)"
1.2069 +        apply (rule f')
1.2070 +        apply (metis triangle_points_closer [OF xc yc] le norm_minus_commute order_trans)
1.2071 +        using s yc by blast
1.2072 +      also have "... \<le> e * (cmod (a - b) + cmod (b - c) + cmod (c - a))"
1.2073 +        by (simp add: yc e xc disj_le [OF triangle_points_closer])
1.2074 +      finally have "cmod (f y - f x - f' * (y - x)) \<le> e * (cmod (a - b) + cmod (b - c) + cmod (c - a))" .
1.2075 +    } note cm_le = this
1.2076 +    have "?normle a b c"
1.2077 +      apply (simp add: dist_norm pa)
1.2078 +      apply (rule le_of_3)
1.2079 +      using f' xc s e
1.2080 +      apply simp_all
1.2081 +      apply (intro norm_triangle_le add_mono path_bound)
1.2082 +      apply (simp_all add: contour_integral_diff contour_integral_lmul contour_integrable_lmul contour_integrable_diff fabc)
1.2083 +      apply (blast intro: cm_le elim: dest: segments_subset_convex_hull [THEN subsetD])+
1.2084 +      done
1.2085 +  } note * = this
1.2086 +  show ?thesis
1.2087 +    using cd e
1.2088 +    apply (simp add: field_differentiable_def has_field_derivative_def has_derivative_within_alt approachable_lt_le2 Ball_def)
1.2089 +    apply (clarify dest!: spec mp)
1.2090 +    using *
1.2091 +    apply (simp add: dist_norm, blast)
1.2092 +    done
1.2093 +qed
1.2094 +
1.2095 +
1.2096 +(* Hence the most basic theorem for a triangle.*)
1.2097 +locale Chain =
1.2098 +  fixes x0 At Follows
1.2099 +  assumes At0: "At x0 0"
1.2100 +      and AtSuc: "\<And>x n. At x n \<Longrightarrow> \<exists>x'. At x' (Suc n) \<and> Follows x' x"
1.2101 +begin
1.2102 +  primrec f where
1.2103 +    "f 0 = x0"
1.2104 +  | "f (Suc n) = (SOME x. At x (Suc n) \<and> Follows x (f n))"
1.2105 +
1.2106 +  lemma At: "At (f n) n"
1.2107 +  proof (induct n)
1.2108 +    case 0 show ?case
1.2109 +      by (simp add: At0)
1.2110 +  next
1.2111 +    case (Suc n) show ?case
1.2112 +      by (metis (no_types, lifting) AtSuc [OF Suc] f.simps(2) someI_ex)
1.2113 +  qed
1.2114 +
1.2115 +  lemma Follows: "Follows (f(Suc n)) (f n)"
1.2116 +    by (metis (no_types, lifting) AtSuc [OF At [of n]] f.simps(2) someI_ex)
1.2117 +
1.2118 +  declare f.simps(2) [simp del]
1.2119 +end
1.2120 +
1.2121 +lemma Chain3:
1.2122 +  assumes At0: "At x0 y0 z0 0"
1.2123 +      and AtSuc: "\<And>x y z n. At x y z n \<Longrightarrow> \<exists>x' y' z'. At x' y' z' (Suc n) \<and> Follows x' y' z' x y z"
1.2124 +  obtains f g h where
1.2125 +    "f 0 = x0" "g 0 = y0" "h 0 = z0"
1.2126 +                      "\<And>n. At (f n) (g n) (h n) n"
1.2127 +                       "\<And>n. Follows (f(Suc n)) (g(Suc n)) (h(Suc n)) (f n) (g n) (h n)"
1.2128 +proof -
1.2129 +  interpret three: Chain "(x0,y0,z0)" "\<lambda>(x,y,z). At x y z" "\<lambda>(x',y',z'). \<lambda>(x,y,z). Follows x' y' z' x y z"
1.2130 +    apply unfold_locales
1.2131 +    using At0 AtSuc by auto
1.2132 +  show ?thesis
1.2133 +  apply (rule that [of "\<lambda>n. fst (three.f n)"  "\<lambda>n. fst (snd (three.f n))" "\<lambda>n. snd (snd (three.f n))"])
1.2134 +  apply simp_all
1.2135 +  using three.At three.Follows
1.2136 +  apply (simp_all add: split_beta')
1.2137 +  done
1.2138 +qed
1.2139 +
1.2140 +lemma Cauchy_theorem_triangle:
1.2141 +  assumes "f holomorphic_on (convex hull {a,b,c})"
1.2142 +    shows "(f has_contour_integral 0) (linepath a b +++ linepath b c +++ linepath c a)"
1.2143 +proof -
1.2144 +  have contf: "continuous_on (convex hull {a,b,c}) f"
1.2145 +    by (metis assms holomorphic_on_imp_continuous_on)
1.2146 +  let ?pathint = "\<lambda>x y. contour_integral(linepath x y) f"
1.2147 +  { fix y::complex
1.2148 +    assume fy: "(f has_contour_integral y) (linepath a b +++ linepath b c +++ linepath c a)"
1.2149 +       and ynz: "y \<noteq> 0"
1.2150 +    define K where "K = 1 + max (dist a b) (max (dist b c) (dist c a))"
1.2151 +    define e where "e = norm y / K^2"
1.2152 +    have K1: "K \<ge> 1"  by (simp add: K_def max.coboundedI1)
1.2153 +    then have K: "K > 0" by linarith
1.2154 +    have [iff]: "dist a b \<le> K" "dist b c \<le> K" "dist c a \<le> K"
1.2155 +      by (simp_all add: K_def)
1.2156 +    have e: "e > 0"
1.2157 +      unfolding e_def using ynz K1 by simp
1.2158 +    define At where "At x y z n \<longleftrightarrow>
1.2159 +        convex hull {x,y,z} \<subseteq> convex hull {a,b,c} \<and>
1.2160 +        dist x y \<le> K/2^n \<and> dist y z \<le> K/2^n \<and> dist z x \<le> K/2^n \<and>
1.2161 +        norm(?pathint x y + ?pathint y z + ?pathint z x) \<ge> e*(K/2^n)^2"
1.2162 +      for x y z n
1.2163 +    have At0: "At a b c 0"
1.2164 +      using fy
1.2165 +      by (simp add: At_def e_def has_chain_integral_chain_integral3)
1.2166 +    { fix x y z n
1.2167 +      assume At: "At x y z n"
1.2168 +      then have contf': "continuous_on (convex hull {x,y,z}) f"
1.2169 +        using contf At_def continuous_on_subset by blast
1.2170 +      have "\<exists>x' y' z'. At x' y' z' (Suc n) \<and> convex hull {x',y',z'} \<subseteq> convex hull {x,y,z}"
1.2171 +        using At
1.2172 +        apply (simp add: At_def)
1.2173 +        using  Cauchy_theorem_quadrisection [OF contf', of "K/2^n" e]
1.2174 +        apply clarsimp
1.2175 +        apply (rule_tac x="a'" in exI)
1.2176 +        apply (rule_tac x="b'" in exI)
1.2177 +        apply (rule_tac x="c'" in exI)
1.2178 +        apply (simp add: algebra_simps)
1.2179 +        apply (meson convex_hull_subset empty_subsetI insert_subset subsetCE)
1.2180 +        done
1.2181 +    } note AtSuc = this
1.2182 +    obtain fa fb fc
1.2183 +      where f0 [simp]: "fa 0 = a" "fb 0 = b" "fc 0 = c"
1.2184 +        and cosb: "\<And>n. convex hull {fa n, fb n, fc n} \<subseteq> convex hull {a,b,c}"
1.2185 +        and dist: "\<And>n. dist (fa n) (fb n) \<le> K/2^n"
1.2186 +                  "\<And>n. dist (fb n) (fc n) \<le> K/2^n"
1.2187 +                  "\<And>n. dist (fc n) (fa n) \<le> K/2^n"
1.2188 +        and no: "\<And>n. norm(?pathint (fa n) (fb n) +
1.2189 +                           ?pathint (fb n) (fc n) +
1.2190 +                           ?pathint (fc n) (fa n)) \<ge> e * (K/2^n)^2"
1.2191 +        and conv_le: "\<And>n. convex hull {fa(Suc n), fb(Suc n), fc(Suc n)} \<subseteq> convex hull {fa n, fb n, fc n}"
1.2192 +      apply (rule Chain3 [of At, OF At0 AtSuc])
1.2193 +      apply (auto simp: At_def)
1.2194 +      done
1.2195 +    have "\<exists>x. \<forall>n. x \<in> convex hull {fa n, fb n, fc n}"
1.2196 +      apply (rule bounded_closed_nest)
1.2197 +      apply (simp_all add: compact_imp_closed finite_imp_compact_convex_hull finite_imp_bounded_convex_hull)
1.2198 +      apply (rule allI)
1.2199 +      apply (rule transitive_stepwise_le)
1.2200 +      apply (auto simp: conv_le)
1.2201 +      done
1.2202 +    then obtain x where x: "\<And>n. x \<in> convex hull {fa n, fb n, fc n}" by auto
1.2203 +    then have xin: "x \<in> convex hull {a,b,c}"
1.2204 +      using assms f0 by blast
1.2205 +    then have fx: "f field_differentiable at x within (convex hull {a,b,c})"
1.2206 +      using assms holomorphic_on_def by blast
1.2207 +    { fix k n
1.2208 +      assume k: "0 < k"
1.2209 +         and le:
1.2210 +            "\<And>x' y' z'.
1.2211 +               \<lbrakk>dist x' y' \<le> k; dist y' z' \<le> k; dist z' x' \<le> k;
1.2212 +                x \<in> convex hull {x',y',z'};
1.2213 +                convex hull {x',y',z'} \<subseteq> convex hull {a,b,c}\<rbrakk>
1.2214 +               \<Longrightarrow>
1.2215 +               cmod (?pathint x' y' + ?pathint y' z' + ?pathint z' x') * 10
1.2216 +                     \<le> e * (dist x' y' + dist y' z' + dist z' x')\<^sup>2"
1.2217 +         and Kk: "K / k < 2 ^ n"
1.2218 +      have "K / 2 ^ n < k" using Kk k
1.2219 +        by (auto simp: field_simps)
1.2220 +      then have DD: "dist (fa n) (fb n) \<le> k" "dist (fb n) (fc n) \<le> k" "dist (fc n) (fa n) \<le> k"
1.2221 +        using dist [of n]  k
1.2222 +        by linarith+
1.2223 +      have dle: "(dist (fa n) (fb n) + dist (fb n) (fc n) + dist (fc n) (fa n))\<^sup>2
1.2224 +               \<le> (3 * K / 2 ^ n)\<^sup>2"
1.2225 +        using dist [of n] e K
1.2226 +        by (simp add: abs_le_square_iff [symmetric])
1.2227 +      have less10: "\<And>x y::real. 0 < x \<Longrightarrow> y \<le> 9*x \<Longrightarrow> y < x*10"
1.2228 +        by linarith
1.2229 +      have "e * (dist (fa n) (fb n) + dist (fb n) (fc n) + dist (fc n) (fa n))\<^sup>2 \<le> e * (3 * K / 2 ^ n)\<^sup>2"
1.2230 +        using ynz dle e mult_le_cancel_left_pos by blast
1.2231 +      also have "... <
1.2232 +          cmod (?pathint (fa n) (fb n) + ?pathint (fb n) (fc n) + ?pathint (fc n) (fa n)) * 10"
1.2233 +        using no [of n] e K
1.2234 +        apply (simp add: e_def field_simps)
1.2235 +        apply (simp only: zero_less_norm_iff [symmetric])
1.2236 +        done
1.2237 +      finally have False
1.2238 +        using le [OF DD x cosb] by auto
1.2239 +    } then
1.2240 +    have ?thesis
1.2241 +      using holomorphic_point_small_triangle [OF xin contf fx, of "e/10"] e
1.2242 +      apply clarsimp
1.2243 +      apply (rule_tac y1="K/k" in exE [OF real_arch_pow[of 2]])
1.2244 +      apply force+
1.2245 +      done
1.2246 +  }
1.2247 +  moreover have "f contour_integrable_on (linepath a b +++ linepath b c +++ linepath c a)"
1.2248 +    by simp (meson contf continuous_on_subset contour_integrable_continuous_linepath segments_subset_convex_hull(1)
1.2249 +                   segments_subset_convex_hull(3) segments_subset_convex_hull(5))
1.2250 +  ultimately show ?thesis
1.2251 +    using has_contour_integral_integral by fastforce
1.2252 +qed
1.2253 +
1.2254 +
1.2255 +subsection\<open>Version needing function holomorphic in interior only\<close>
1.2256 +
1.2257 +lemma Cauchy_theorem_flat_lemma:
1.2258 +  assumes f: "continuous_on (convex hull {a,b,c}) f"
1.2259 +      and c: "c - a = k *\<^sub>R (b - a)"
1.2260 +      and k: "0 \<le> k"
1.2261 +    shows "contour_integral (linepath a b) f + contour_integral (linepath b c) f +
1.2262 +          contour_integral (linepath c a) f = 0"
1.2263 +proof -
1.2264 +  have fabc: "continuous_on (closed_segment a b) f" "continuous_on (closed_segment b c) f" "continuous_on (closed_segment c a) f"
1.2265 +    using f continuous_on_subset segments_subset_convex_hull by metis+
1.2266 +  show ?thesis
1.2267 +  proof (cases "k \<le> 1")
1.2268 +    case True show ?thesis
1.2269 +      by (simp add: contour_integral_split [OF fabc(1) k True c] contour_integral_reverse_linepath fabc)
1.2270 +  next
1.2271 +    case False then show ?thesis
1.2272 +      using fabc c
1.2273 +      apply (subst contour_integral_split [of a c f "1/k" b, symmetric])
1.2274 +      apply (metis closed_segment_commute fabc(3))
1.2275 +      apply (auto simp: k contour_integral_reverse_linepath)
1.2276 +      done
1.2277 +  qed
1.2278 +qed
1.2279 +
1.2280 +lemma Cauchy_theorem_flat:
1.2281 +  assumes f: "continuous_on (convex hull {a,b,c}) f"
1.2282 +      and c: "c - a = k *\<^sub>R (b - a)"
1.2283 +    shows "contour_integral (linepath a b) f +
1.2284 +           contour_integral (linepath b c) f +
1.2285 +           contour_integral (linepath c a) f = 0"
1.2286 +proof (cases "0 \<le> k")
1.2287 +  case True with assms show ?thesis
1.2288 +    by (blast intro: Cauchy_theorem_flat_lemma)
1.2289 +next
1.2290 +  case False
1.2291 +  have "continuous_on (closed_segment a b) f" "continuous_on (closed_segment b c) f" "continuous_on (closed_segment c a) f"
1.2292 +    using f continuous_on_subset segments_subset_convex_hull by metis+
1.2293 +  moreover have "contour_integral (linepath b a) f + contour_integral (linepath a c) f +
1.2294 +        contour_integral (linepath c b) f = 0"
1.2295 +    apply (rule Cauchy_theorem_flat_lemma [of b a c f "1-k"])
1.2296 +    using False c
1.2297 +    apply (auto simp: f insert_commute scaleR_conv_of_real algebra_simps)
1.2298 +    done
1.2299 +  ultimately show ?thesis
1.2300 +    apply (auto simp: contour_integral_reverse_linepath)
1.2301 +    using add_eq_0_iff by force
1.2302 +qed
1.2303 +
1.2304 +
1.2305 +lemma Cauchy_theorem_triangle_interior:
1.2306 +  assumes contf: "continuous_on (convex hull {a,b,c}) f"
1.2307 +      and holf:  "f holomorphic_on interior (convex hull {a,b,c})"
1.2308 +     shows "(f has_contour_integral 0) (linepath a b +++ linepath b c +++ linepath c a)"
1.2309 +proof -
1.2310 +  have fabc: "continuous_on (closed_segment a b) f" "continuous_on (closed_segment b c) f" "continuous_on (closed_segment c a) f"
1.2311 +    using contf continuous_on_subset segments_subset_convex_hull by metis+
1.2312 +  have "bounded (f ` (convex hull {a,b,c}))"
1.2313 +    by (simp add: compact_continuous_image compact_convex_hull compact_imp_bounded contf)
1.2314 +  then obtain B where "0 < B" and Bnf: "\<And>x. x \<in> convex hull {a,b,c} \<Longrightarrow> norm (f x) \<le> B"
1.2315 +     by (auto simp: dest!: bounded_pos [THEN iffD1])
1.2316 +  have "bounded (convex hull {a,b,c})"
1.2317 +    by (simp add: bounded_convex_hull)
1.2318 +  then obtain C where C: "0 < C" and Cno: "\<And>y. y \<in> convex hull {a,b,c} \<Longrightarrow> norm y < C"
1.2319 +    using bounded_pos_less by blast
1.2320 +  then have diff_2C: "norm(x - y) \<le> 2*C"
1.2321 +           if x: "x \<in> convex hull {a, b, c}" and y: "y \<in> convex hull {a, b, c}" for x y
1.2322 +  proof -
1.2323 +    have "cmod x \<le> C"
1.2324 +      using x by (meson Cno not_le not_less_iff_gr_or_eq)
1.2325 +    hence "cmod (x - y) \<le> C + C"
1.2326 +      using y by (meson Cno add_mono_thms_linordered_field(4) less_eq_real_def norm_triangle_ineq4 order_trans)
1.2327 +    thus "cmod (x - y) \<le> 2 * C"
1.2328 +      by (metis mult_2)
1.2329 +  qed
1.2330 +  have contf': "continuous_on (convex hull {b,a,c}) f"
1.2331 +    using contf by (simp add: insert_commute)
1.2332 +  { fix y::complex
1.2333 +    assume fy: "(f has_contour_integral y) (linepath a b +++ linepath b c +++ linepath c a)"
1.2334 +       and ynz: "y \<noteq> 0"
1.2335 +    have pi_eq_y: "contour_integral (linepath a b) f + contour_integral (linepath b c) f + contour_integral (linepath c a) f = y"
1.2336 +      by (rule has_chain_integral_chain_integral3 [OF fy])
1.2337 +    have ?thesis
1.2338 +    proof (cases "c=a \<or> a=b \<or> b=c")
1.2339 +      case True then show ?thesis
1.2340 +        using Cauchy_theorem_flat [OF contf, of 0]
1.2341 +        using has_chain_integral_chain_integral3 [OF fy] ynz
1.2342 +        by (force simp: fabc contour_integral_reverse_linepath)
1.2343 +    next
1.2344 +      case False
1.2345 +      then have car3: "card {a, b, c} = Suc (DIM(complex))"
1.2346 +        by auto
1.2347 +      { assume "interior(convex hull {a,b,c}) = {}"
1.2348 +        then have "collinear{a,b,c}"
1.2349 +          using interior_convex_hull_eq_empty [OF car3]
1.2350 +          by (simp add: collinear_3_eq_affine_dependent)
1.2351 +        then have "False"
1.2352 +          using False
1.2353 +          apply (clarsimp simp add: collinear_3 collinear_lemma)
1.2354 +          apply (drule Cauchy_theorem_flat [OF contf'])
1.2355 +          using pi_eq_y ynz
1.2357 +          done
1.2358 +      }
1.2359 +      then obtain d where d: "d \<in> interior (convex hull {a, b, c})"
1.2360 +        by blast
1.2361 +      { fix d1
1.2362 +        assume d1_pos: "0 < d1"
1.2363 +           and d1: "\<And>x x'. \<lbrakk>x\<in>convex hull {a, b, c}; x'\<in>convex hull {a, b, c}; cmod (x' - x) < d1\<rbrakk>
1.2364 +                           \<Longrightarrow> cmod (f x' - f x) < cmod y / (24 * C)"
1.2365 +        define e where "e = min 1 (min (d1/(4*C)) ((norm y / 24 / C) / B))"
1.2366 +        define shrink where "shrink x = x - e *\<^sub>R (x - d)" for x
1.2367 +        let ?pathint = "\<lambda>x y. contour_integral(linepath x y) f"
1.2368 +        have e: "0 < e" "e \<le> 1" "e \<le> d1 / (4 * C)" "e \<le> cmod y / 24 / C / B"
1.2369 +          using d1_pos \<open>C>0\<close> \<open>B>0\<close> ynz by (simp_all add: e_def)
1.2370 +        then have eCB: "24 * e * C * B \<le> cmod y"
1.2371 +          using \<open>C>0\<close> \<open>B>0\<close>  by (simp add: field_simps)
1.2372 +        have e_le_d1: "e * (4 * C) \<le> d1"
1.2373 +          using e \<open>C>0\<close> by (simp add: field_simps)
1.2374 +        have "shrink a \<in> interior(convex hull {a,b,c})"
1.2375 +             "shrink b \<in> interior(convex hull {a,b,c})"
1.2376 +             "shrink c \<in> interior(convex hull {a,b,c})"
1.2377 +          using d e by (auto simp: hull_inc mem_interior_convex_shrink shrink_def)
1.2378 +        then have fhp0: "(f has_contour_integral 0)
1.2379 +                (linepath (shrink a) (shrink b) +++ linepath (shrink b) (shrink c) +++ linepath (shrink c) (shrink a))"
1.2380 +          by (simp add: Cauchy_theorem_triangle holomorphic_on_subset [OF holf] hull_minimal convex_interior)
1.2381 +        then have f_0_shrink: "?pathint (shrink a) (shrink b) + ?pathint (shrink b) (shrink c) + ?pathint (shrink c) (shrink a) = 0"
1.2382 +          by (simp add: has_chain_integral_chain_integral3)
1.2383 +        have fpi_abc: "f contour_integrable_on linepath (shrink a) (shrink b)"
1.2384 +                      "f contour_integrable_on linepath (shrink b) (shrink c)"
1.2385 +                      "f contour_integrable_on linepath (shrink c) (shrink a)"
1.2386 +          using fhp0  by (auto simp: valid_path_join dest: has_contour_integral_integrable)
1.2387 +        have cmod_shr: "\<And>x y. cmod (shrink y - shrink x - (y - x)) = e * cmod (x - y)"
1.2388 +          using e by (simp add: shrink_def real_vector.scale_right_diff_distrib [symmetric])
1.2389 +        have sh_eq: "\<And>a b d::complex. (b - e *\<^sub>R (b - d)) - (a - e *\<^sub>R (a - d)) - (b - a) = e *\<^sub>R (a - b)"
1.2390 +          by (simp add: algebra_simps)
1.2391 +        have "cmod y / (24 * C) \<le> cmod y / cmod (b - a) / 12"
1.2392 +          using False \<open>C>0\<close> diff_2C [of b a] ynz
1.2393 +          by (auto simp: divide_simps hull_inc)
1.2394 +        have less_C: "\<lbrakk>u \<in> convex hull {a, b, c}; 0 \<le> x; x \<le> 1\<rbrakk> \<Longrightarrow> x * cmod u < C" for x u
1.2395 +          apply (cases "x=0", simp add: \<open>0<C\<close>)
1.2396 +          using Cno [of u] mult_left_le_one_le [of "cmod u" x] le_less_trans norm_ge_zero by blast
1.2397 +        { fix u v
1.2398 +          assume uv: "u \<in> convex hull {a, b, c}" "v \<in> convex hull {a, b, c}" "u\<noteq>v"
1.2399 +             and fpi_uv: "f contour_integrable_on linepath (shrink u) (shrink v)"
1.2400 +          have shr_uv: "shrink u \<in> interior(convex hull {a,b,c})"
1.2401 +                       "shrink v \<in> interior(convex hull {a,b,c})"
1.2402 +            using d e uv
1.2403 +            by (auto simp: hull_inc mem_interior_convex_shrink shrink_def)
1.2404 +          have cmod_fuv: "\<And>x. 0\<le>x \<Longrightarrow> x\<le>1 \<Longrightarrow> cmod (f (linepath (shrink u) (shrink v) x)) \<le> B"
1.2405 +            using shr_uv by (blast intro: Bnf linepath_in_convex_hull interior_subset [THEN subsetD])
1.2406 +          have By_uv: "B * (12 * (e * cmod (u - v))) \<le> cmod y"
1.2407 +            apply (rule order_trans [OF _ eCB])
1.2408 +            using e \<open>B>0\<close> diff_2C [of u v] uv
1.2409 +            by (auto simp: field_simps)
1.2410 +          { fix x::real   assume x: "0\<le>x" "x\<le>1"
1.2411 +            have cmod_less_4C: "cmod ((1 - x) *\<^sub>R u - (1 - x) *\<^sub>R d) + cmod (x *\<^sub>R v - x *\<^sub>R d) < (C+C) + (C+C)"
1.2412 +              apply (rule add_strict_mono; rule norm_triangle_half_l [of _ 0])
1.2413 +              using uv x d interior_subset
1.2414 +              apply (auto simp: hull_inc intro!: less_C)
1.2415 +              done
1.2416 +            have ll: "linepath (shrink u) (shrink v) x - linepath u v x = -e * ((1 - x) *\<^sub>R (u - d) + x *\<^sub>R (v - d))"
1.2417 +              by (simp add: linepath_def shrink_def algebra_simps scaleR_conv_of_real)
1.2418 +            have cmod_less_dt: "cmod (linepath (shrink u) (shrink v) x - linepath u v x) < d1"
1.2419 +              using \<open>e>0\<close>
1.2420 +              apply (simp add: ll norm_mult scaleR_diff_right)
1.2421 +              apply (rule less_le_trans [OF _ e_le_d1])
1.2422 +              using cmod_less_4C
1.2423 +              apply (force intro: norm_triangle_lt)
1.2424 +              done
1.2425 +            have "cmod (f (linepath (shrink u) (shrink v) x) - f (linepath u v x)) < cmod y / (24 * C)"
1.2426 +              using x uv shr_uv cmod_less_dt
1.2427 +              by (auto simp: hull_inc intro: d1 interior_subset [THEN subsetD] linepath_in_convex_hull)
1.2428 +            also have "... \<le> cmod y / cmod (v - u) / 12"
1.2429 +              using False uv \<open>C>0\<close> diff_2C [of v u] ynz
1.2430 +              by (auto simp: divide_simps hull_inc)
1.2431 +            finally have "cmod (f (linepath (shrink u) (shrink v) x) - f (linepath u v x)) \<le> cmod y / cmod (v - u) / 12"
1.2432 +              by simp
1.2433 +            then have cmod_12_le: "cmod (v - u) * cmod (f (linepath (shrink u) (shrink v) x) - f (linepath u v x)) * 12 \<le> cmod y"
1.2434 +              using uv False by (auto simp: field_simps)
1.2435 +            have "cmod (f (linepath (shrink u) (shrink v) x)) * cmod (shrink v - shrink u - (v - u)) +
1.2436 +                  cmod (v - u) * cmod (f (linepath (shrink u) (shrink v) x) - f (linepath u v x))
1.2437 +                  \<le> cmod y / 6"
1.2438 +              apply (rule order_trans [of _ "B*((norm y / 24 / C / B)*2*C) + (2*C)*(norm y /24 / C)"])
1.2439 +              apply (rule add_mono [OF mult_mono])
1.2440 +              using By_uv e \<open>0 < B\<close> \<open>0 < C\<close> x ynz
1.2441 +              apply (simp_all add: cmod_fuv cmod_shr cmod_12_le hull_inc)
1.2442 +              apply (simp add: field_simps)
1.2443 +              done
1.2444 +          } note cmod_diff_le = this
1.2445 +          have f_uv: "continuous_on (closed_segment u v) f"
1.2446 +            by (blast intro: uv continuous_on_subset [OF contf closed_segment_subset_convex_hull])
1.2447 +          have **: "\<And>f' x' f x::complex. f'*x' - f*x = f'*(x' - x) + x*(f' - f)"
1.2448 +            by (simp add: algebra_simps)
1.2449 +          have "norm (?pathint (shrink u) (shrink v) - ?pathint u v) \<le> norm y / 6"
1.2450 +            apply (rule order_trans)
1.2451 +            apply (rule has_integral_bound
1.2452 +                    [of "B*(norm y /24/C/B)*2*C + (2*C)*(norm y/24/C)"
1.2453 +                        "\<lambda>x. f(linepath (shrink u) (shrink v) x) * (shrink v - shrink u) - f(linepath u v x)*(v - u)"
1.2454 +                        _ 0 1 ])
1.2455 +            using ynz \<open>0 < B\<close> \<open>0 < C\<close>
1.2456 +            apply (simp_all del: le_divide_eq_numeral1)
1.2457 +            apply (simp add: has_integral_sub has_contour_integral_linepath [symmetric] has_contour_integral_integral
1.2458 +                             fpi_uv f_uv contour_integrable_continuous_linepath, clarify)
1.2459 +            apply (simp only: **)
1.2460 +            apply (simp add: norm_triangle_le norm_mult cmod_diff_le del: le_divide_eq_numeral1)
1.2461 +            done
1.2462 +          } note * = this
1.2463 +          have "norm (?pathint (shrink a) (shrink b) - ?pathint a b) \<le> norm y / 6"
1.2464 +            using False fpi_abc by (rule_tac *) (auto simp: hull_inc)
1.2465 +          moreover
1.2466 +          have "norm (?pathint (shrink b) (shrink c) - ?pathint b c) \<le> norm y / 6"
1.2467 +            using False fpi_abc by (rule_tac *) (auto simp: hull_inc)
1.2468 +          moreover
1.2469 +          have "norm (?pathint (shrink c) (shrink a) - ?pathint c a) \<le> norm y / 6"
1.2470 +            using False fpi_abc by (rule_tac *) (auto simp: hull_inc)
1.2471 +          ultimately
1.2472 +          have "norm((?pathint (shrink a) (shrink b) - ?pathint a b) +
1.2473 +                     (?pathint (shrink b) (shrink c) - ?pathint b c) + (?pathint (shrink c) (shrink a) - ?pathint c a))
1.2474 +                \<le> norm y / 6 + norm y / 6 + norm y / 6"
1.2475 +            by (metis norm_triangle_le add_mono)
1.2476 +          also have "... = norm y / 2"
1.2477 +            by simp
1.2478 +          finally have "norm((?pathint (shrink a) (shrink b) + ?pathint (shrink b) (shrink c) + ?pathint (shrink c) (shrink a)) -
1.2479 +                          (?pathint a b + ?pathint b c + ?pathint c a))
1.2480 +                \<le> norm y / 2"
1.2481 +            by (simp add: algebra_simps)
1.2482 +          then
1.2483 +          have "norm(?pathint a b + ?pathint b c + ?pathint c a) \<le> norm y / 2"
1.2485 +          then have "False"
1.2486 +            using pi_eq_y ynz by auto
1.2487 +        }
1.2488 +        moreover have "uniformly_continuous_on (convex hull {a,b,c}) f"
1.2489 +          by (simp add: contf compact_convex_hull compact_uniformly_continuous)
1.2490 +        ultimately have "False"
1.2491 +          unfolding uniformly_continuous_on_def
1.2492 +          by (force simp: ynz \<open>0 < C\<close> dist_norm)
1.2493 +        then show ?thesis ..
1.2494 +      qed
1.2495 +  }
1.2496 +  moreover have "f contour_integrable_on (linepath a b +++ linepath b c +++ linepath c a)"
1.2497 +    using fabc contour_integrable_continuous_linepath by auto
1.2498 +  ultimately show ?thesis
1.2499 +    using has_contour_integral_integral by fastforce
1.2500 +qed
1.2501 +
1.2502 +
1.2503 +subsection\<open>Version allowing finite number of exceptional points\<close>
1.2504 +
1.2505 +lemma Cauchy_theorem_triangle_cofinite:
1.2506 +  assumes "continuous_on (convex hull {a,b,c}) f"
1.2507 +      and "finite s"
1.2508 +      and "(\<And>x. x \<in> interior(convex hull {a,b,c}) - s \<Longrightarrow> f field_differentiable (at x))"
1.2509 +     shows "(f has_contour_integral 0) (linepath a b +++ linepath b c +++ linepath c a)"
1.2510 +using assms
1.2511 +proof (induction "card s" arbitrary: a b c s rule: less_induct)
1.2512 +  case (less s a b c)
1.2513 +  show ?case
1.2514 +  proof (cases "s={}")
1.2515 +    case True with less show ?thesis
1.2516 +      by (fastforce simp: holomorphic_on_def field_differentiable_at_within
1.2517 +                    Cauchy_theorem_triangle_interior)
1.2518 +  next
1.2519 +    case False
1.2520 +    then obtain d s' where d: "s = insert d s'" "d \<notin> s'"
1.2521 +      by (meson Set.set_insert all_not_in_conv)
1.2522 +    then show ?thesis
1.2523 +    proof (cases "d \<in> convex hull {a,b,c}")
1.2524 +      case False
1.2525 +      show "(f has_contour_integral 0) (linepath a b +++ linepath b c +++ linepath c a)"
1.2526 +        apply (rule less.hyps [of "s'"])
1.2527 +        using False d \<open>finite s\<close> interior_subset
1.2528 +        apply (auto intro!: less.prems)
1.2529 +        done
1.2530 +    next
1.2531 +      case True
1.2532 +      have *: "convex hull {a, b, d} \<subseteq> convex hull {a, b, c}"
1.2533 +        by (meson True hull_subset insert_subset convex_hull_subset)
1.2534 +      have abd: "(f has_contour_integral 0) (linepath a b +++ linepath b d +++ linepath d a)"
1.2535 +        apply (rule less.hyps [of "s'"])
1.2536 +        using True d  \<open>finite s\<close> not_in_interior_convex_hull_3
1.2537 +        apply (auto intro!: less.prems continuous_on_subset [OF  _ *])
1.2538 +        apply (metis * insert_absorb insert_subset interior_mono)
1.2539 +        done
1.2540 +      have *: "convex hull {b, c, d} \<subseteq> convex hull {a, b, c}"
1.2541 +        by (meson True hull_subset insert_subset convex_hull_subset)
1.2542 +      have bcd: "(f has_contour_integral 0) (linepath b c +++ linepath c d +++ linepath d b)"
1.2543 +        apply (rule less.hyps [of "s'"])
1.2544 +        using True d  \<open>finite s\<close> not_in_interior_convex_hull_3
1.2545 +        apply (auto intro!: less.prems continuous_on_subset [OF _ *])
1.2546 +        apply (metis * insert_absorb insert_subset interior_mono)
1.2547 +        done
1.2548 +      have *: "convex hull {c, a, d} \<subseteq> convex hull {a, b, c}"
1.2549 +        by (meson True hull_subset insert_subset convex_hull_subset)
1.2550 +      have cad: "(f has_contour_integral 0) (linepath c a +++ linepath a d +++ linepath d c)"
1.2551 +        apply (rule less.hyps [of "s'"])
1.2552 +        using True d  \<open>finite s\<close> not_in_interior_convex_hull_3
1.2553 +        apply (auto intro!: less.prems continuous_on_subset [OF _ *])
1.2554 +        apply (metis * insert_absorb insert_subset interior_mono)
1.2555 +        done
1.2556 +      have "f contour_integrable_on linepath a b"
1.2557 +        using less.prems
1.2558 +        by (metis continuous_on_subset insert_commute contour_integrable_continuous_linepath segments_subset_convex_hull(3))
1.2559 +      moreover have "f contour_integrable_on linepath b c"
1.2560 +        using less.prems
1.2561 +        by (metis continuous_on_subset contour_integrable_continuous_linepath segments_subset_convex_hull(3))
1.2562 +      moreover have "f contour_integrable_on linepath c a"
1.2563 +        using less.prems
1.2564 +        by (metis continuous_on_subset insert_commute contour_integrable_continuous_linepath segments_subset_convex_hull(3))
1.2565 +      ultimately have fpi: "f contour_integrable_on (linepath a b +++ linepath b c +++ linepath c a)"
1.2566 +        by auto
1.2567 +      { fix y::complex
1.2568 +        assume fy: "(f has_contour_integral y) (linepath a b +++ linepath b c +++ linepath c a)"
1.2569 +           and ynz: "y \<noteq> 0"
1.2570 +        have cont_ad: "continuous_on (closed_segment a d) f"
1.2571 +          by (meson "*" continuous_on_subset less.prems(1) segments_subset_convex_hull(3))
1.2572 +        have cont_bd: "continuous_on (closed_segment b d) f"
1.2573 +          by (meson True closed_segment_subset_convex_hull continuous_on_subset hull_subset insert_subset less.prems(1))
1.2574 +        have cont_cd: "continuous_on (closed_segment c d) f"
1.2575 +          by (meson "*" continuous_on_subset less.prems(1) segments_subset_convex_hull(2))
1.2576 +        have "contour_integral  (linepath a b) f = - (contour_integral (linepath b d) f + (contour_integral (linepath d a) f))"
1.2577 +                "contour_integral  (linepath b c) f = - (contour_integral (linepath c d) f + (contour_integral (linepath d b) f))"
1.2578 +                "contour_integral  (linepath c a) f = - (contour_integral (linepath a d) f + contour_integral (linepath d c) f)"
1.2579 +            using has_chain_integral_chain_integral3 [OF abd]
1.2580 +                  has_chain_integral_chain_integral3 [OF bcd]
1.2583 +        then have ?thesis
1.2584 +          using cont_ad cont_bd cont_cd fy has_chain_integral_chain_integral3 contour_integral_reverse_linepath by fastforce
1.2585 +      }
1.2586 +      then show ?thesis
1.2587 +        using fpi contour_integrable_on_def by blast
1.2588 +    qed
1.2589 +  qed
1.2590 +qed
1.2591 +
1.2592 +
1.2593 +subsection\<open>Cauchy's theorem for an open starlike set\<close>
1.2594 +
1.2595 +lemma starlike_convex_subset:
1.2596 +  assumes s: "a \<in> s" "closed_segment b c \<subseteq> s" and subs: "\<And>x. x \<in> s \<Longrightarrow> closed_segment a x \<subseteq> s"
1.2597 +    shows "convex hull {a,b,c} \<subseteq> s"
1.2598 +      using s
1.2599 +      apply (clarsimp simp add: convex_hull_insert [of "{b,c}" a] segment_convex_hull)
1.2600 +      apply (meson subs convexD convex_closed_segment ends_in_segment(1) ends_in_segment(2) subsetCE)
1.2601 +      done
1.2602 +
1.2603 +lemma triangle_contour_integrals_starlike_primitive:
1.2604 +  assumes contf: "continuous_on s f"
1.2605 +      and s: "a \<in> s" "open s"
1.2606 +      and x: "x \<in> s"
1.2607 +      and subs: "\<And>y. y \<in> s \<Longrightarrow> closed_segment a y \<subseteq> s"
1.2608 +      and zer: "\<And>b c. closed_segment b c \<subseteq> s
1.2609 +                   \<Longrightarrow> contour_integral (linepath a b) f + contour_integral (linepath b c) f +
1.2610 +                       contour_integral (linepath c a) f = 0"
1.2611 +    shows "((\<lambda>x. contour_integral(linepath a x) f) has_field_derivative f x) (at x)"
1.2612 +proof -
1.2613 +  let ?pathint = "\<lambda>x y. contour_integral(linepath x y) f"
1.2614 +  { fix e y
1.2615 +    assume e: "0 < e" and bxe: "ball x e \<subseteq> s" and close: "cmod (y - x) < e"
1.2616 +    have y: "y \<in> s"
1.2617 +      using bxe close  by (force simp: dist_norm norm_minus_commute)
1.2618 +    have cont_ayf: "continuous_on (closed_segment a y) f"
1.2619 +      using contf continuous_on_subset subs y by blast
1.2620 +    have xys: "closed_segment x y \<subseteq> s"
1.2621 +      apply (rule order_trans [OF _ bxe])
1.2622 +      using close
1.2623 +      by (auto simp: dist_norm ball_def norm_minus_commute dest: segment_bound)
1.2624 +    have "?pathint a y - ?pathint a x = ?pathint x y"
1.2625 +      using zer [OF xys]  contour_integral_reverse_linepath [OF cont_ayf]  add_eq_0_iff by force
1.2626 +  } note [simp] = this
1.2627 +  { fix e::real
1.2628 +    assume e: "0 < e"
1.2629 +    have cont_atx: "continuous (at x) f"
1.2630 +      using x s contf continuous_on_eq_continuous_at by blast
1.2631 +    then obtain d1 where d1: "d1>0" and d1_less: "\<And>y. cmod (y - x) < d1 \<Longrightarrow> cmod (f y - f x) < e/2"
1.2632 +      unfolding continuous_at Lim_at dist_norm  using e
1.2633 +      by (drule_tac x="e/2" in spec) force
1.2634 +    obtain d2 where d2: "d2>0" "ball x d2 \<subseteq> s" using  \<open>open s\<close> x
1.2635 +      by (auto simp: open_contains_ball)
1.2636 +    have dpos: "min d1 d2 > 0" using d1 d2 by simp
1.2637 +    { fix y
1.2638 +      assume yx: "y \<noteq> x" and close: "cmod (y - x) < min d1 d2"
1.2639 +      have y: "y \<in> s"
1.2640 +        using d2 close  by (force simp: dist_norm norm_minus_commute)
1.2641 +      have fxy: "f contour_integrable_on linepath x y"
1.2642 +        apply (rule contour_integrable_continuous_linepath)
1.2643 +        apply (rule continuous_on_subset [OF contf])
1.2644 +        using close d2
1.2645 +        apply (auto simp: dist_norm norm_minus_commute dest!: segment_bound(1))
1.2646 +        done
1.2647 +      then obtain i where i: "(f has_contour_integral i) (linepath x y)"
1.2648 +        by (auto simp: contour_integrable_on_def)
1.2649 +      then have "((\<lambda>w. f w - f x) has_contour_integral (i - f x * (y - x))) (linepath x y)"
1.2650 +        by (rule has_contour_integral_diff [OF _ has_contour_integral_const_linepath])
1.2651 +      then have "cmod (i - f x * (y - x)) \<le> e / 2 * cmod (y - x)"
1.2652 +        apply (rule has_contour_integral_bound_linepath [where B = "e/2"])
1.2653 +        using e apply simp
1.2654 +        apply (rule d1_less [THEN less_imp_le])
1.2655 +        using close segment_bound
1.2656 +        apply force
1.2657 +        done
1.2658 +      also have "... < e * cmod (y - x)"
1.2659 +        by (simp add: e yx)
1.2660 +      finally have "cmod (?pathint x y - f x * (y-x)) / cmod (y-x) < e"
1.2661 +        using i yx  by (simp add: contour_integral_unique divide_less_eq)
1.2662 +    }
1.2663 +    then have "\<exists>d>0. \<forall>y. y \<noteq> x \<and> cmod (y-x) < d \<longrightarrow> cmod (?pathint x y - f x * (y-x)) / cmod (y-x) < e"
1.2664 +      using dpos by blast
1.2665 +  }
1.2666 +  then have *: "(\<lambda>y. (?pathint x y - f x * (y - x)) /\<^sub>R cmod (y - x)) \<midarrow>x\<rightarrow> 0"
1.2667 +    by (simp add: Lim_at dist_norm inverse_eq_divide)
1.2668 +  show ?thesis
1.2669 +    apply (simp add: has_field_derivative_def has_derivative_at bounded_linear_mult_right)
1.2670 +    apply (rule Lim_transform [OF * Lim_eventually])
1.2671 +    apply (simp add: inverse_eq_divide [symmetric] eventually_at)
1.2672 +    using \<open>open s\<close> x
1.2673 +    apply (force simp: dist_norm open_contains_ball)
1.2674 +    done
1.2675 +qed
1.2676 +
1.2677 +(** Existence of a primitive.*)
1.2678 +
1.2679 +lemma holomorphic_starlike_primitive:
1.2680 +  fixes f :: "complex \<Rightarrow> complex"
1.2681 +  assumes contf: "continuous_on s f"
1.2682 +      and s: "starlike s" and os: "open s"
1.2683 +      and k: "finite k"
1.2684 +      and fcd: "\<And>x. x \<in> s - k \<Longrightarrow> f field_differentiable at x"
1.2685 +    shows "\<exists>g. \<forall>x \<in> s. (g has_field_derivative f x) (at x)"
1.2686 +proof -
1.2687 +  obtain a where a: "a\<in>s" and a_cs: "\<And>x. x\<in>s \<Longrightarrow> closed_segment a x \<subseteq> s"
1.2688 +    using s by (auto simp: starlike_def)
1.2689 +  { fix x b c
1.2690 +    assume "x \<in> s" "closed_segment b c \<subseteq> s"
1.2691 +    then have abcs: "convex hull {a, b, c} \<subseteq> s"
1.2692 +      by (simp add: a a_cs starlike_convex_subset)
1.2693 +    then have *: "continuous_on (convex hull {a, b, c}) f"
1.2694 +      by (simp add: continuous_on_subset [OF contf])
1.2695 +    have "(f has_contour_integral 0) (linepath a b +++ linepath b c +++ linepath c a)"
1.2696 +      apply (rule Cauchy_theorem_triangle_cofinite [OF _ k])
1.2697 +      using abcs apply (simp add: continuous_on_subset [OF contf])
1.2698 +      using * abcs interior_subset apply (auto intro: fcd)
1.2699 +      done
1.2700 +  } note 0 = this
1.2701 +  show ?thesis
1.2702 +    apply (intro exI ballI)
1.2703 +    apply (rule triangle_contour_integrals_starlike_primitive [OF contf a os], assumption)
1.2704 +    apply (metis a_cs)
1.2705 +    apply (metis has_chain_integral_chain_integral3 0)
1.2706 +    done
1.2707 +qed
1.2708 +
1.2709 +lemma Cauchy_theorem_starlike:
1.2710 + "\<lbrakk>open s; starlike s; finite k; continuous_on s f;
1.2711 +   \<And>x. x \<in> s - k \<Longrightarrow> f field_differentiable at x;
1.2712 +   valid_path g; path_image g \<subseteq> s; pathfinish g = pathstart g\<rbrakk>
1.2713 +   \<Longrightarrow> (f has_contour_integral 0)  g"
1.2714 +  by (metis holomorphic_starlike_primitive Cauchy_theorem_primitive at_within_open)
1.2715 +
1.2716 +lemma Cauchy_theorem_starlike_simple:
1.2717 +  "\<lbrakk>open s; starlike s; f holomorphic_on s; valid_path g; path_image g \<subseteq> s; pathfinish g = pathstart g\<rbrakk>
1.2718 +   \<Longrightarrow> (f has_contour_integral 0) g"
1.2719 +apply (rule Cauchy_theorem_starlike [OF _ _ finite.emptyI])
1.2721 +apply (metis at_within_open holomorphic_on_def)
1.2722 +done
1.2723 +
1.2724 +
1.2725 +subsection\<open>Cauchy's theorem for a convex set\<close>
1.2726 +
1.2727 +text\<open>For a convex set we can avoid assuming openness and boundary analyticity\<close>
1.2728 +
1.2729 +lemma triangle_contour_integrals_convex_primitive:
1.2730 +  assumes contf: "continuous_on s f"
1.2731 +      and s: "a \<in> s" "convex s"
1.2732 +      and x: "x \<in> s"
1.2733 +      and zer: "\<And>b c. \<lbrakk>b \<in> s; c \<in> s\<rbrakk>
1.2734 +                   \<Longrightarrow> contour_integral (linepath a b) f + contour_integral (linepath b c) f +
1.2735 +                       contour_integral (linepath c a) f = 0"
1.2736 +    shows "((\<lambda>x. contour_integral(linepath a x) f) has_field_derivative f x) (at x within s)"
1.2737 +proof -
1.2738 +  let ?pathint = "\<lambda>x y. contour_integral(linepath x y) f"
1.2739 +  { fix y
1.2740 +    assume y: "y \<in> s"
1.2741 +    have cont_ayf: "continuous_on (closed_segment a y) f"
1.2742 +      using s y  by (meson contf continuous_on_subset convex_contains_segment)
1.2743 +    have xys: "closed_segment x y \<subseteq> s"  (*?*)
1.2744 +      using convex_contains_segment s x y by auto
1.2745 +    have "?pathint a y - ?pathint a x = ?pathint x y"
1.2746 +      using zer [OF x y]  contour_integral_reverse_linepath [OF cont_ayf]  add_eq_0_iff by force
1.2747 +  } note [simp] = this
1.2748 +  { fix e::real
1.2749 +    assume e: "0 < e"
1.2750 +    have cont_atx: "continuous (at x within s) f"
1.2751 +      using x s contf  by (simp add: continuous_on_eq_continuous_within)
1.2752 +    then obtain d1 where d1: "d1>0" and d1_less: "\<And>y. \<lbrakk>y \<in> s; cmod (y - x) < d1\<rbrakk> \<Longrightarrow> cmod (f y - f x) < e/2"
1.2753 +      unfolding continuous_within Lim_within dist_norm using e
1.2754 +      by (drule_tac x="e/2" in spec) force
1.2755 +    { fix y
1.2756 +      assume yx: "y \<noteq> x" and close: "cmod (y - x) < d1" and y: "y \<in> s"
1.2757 +      have fxy: "f contour_integrable_on linepath x y"
1.2758 +        using convex_contains_segment s x y
1.2759 +        by (blast intro!: contour_integrable_continuous_linepath continuous_on_subset [OF contf])
1.2760 +      then obtain i where i: "(f has_contour_integral i) (linepath x y)"
1.2761 +        by (auto simp: contour_integrable_on_def)
1.2762 +      then have "((\<lambda>w. f w - f x) has_contour_integral (i - f x * (y - x))) (linepath x y)"
1.2763 +        by (rule has_contour_integral_diff [OF _ has_contour_integral_const_linepath])
1.2764 +      then have "cmod (i - f x * (y - x)) \<le> e / 2 * cmod (y - x)"
1.2765 +        apply (rule has_contour_integral_bound_linepath [where B = "e/2"])
1.2766 +        using e apply simp
1.2767 +        apply (rule d1_less [THEN less_imp_le])
1.2768 +        using convex_contains_segment s(2) x y apply blast
1.2769 +        using close segment_bound(1) apply fastforce
1.2770 +        done
1.2771 +      also have "... < e * cmod (y - x)"
1.2772 +        by (simp add: e yx)
1.2773 +      finally have "cmod (?pathint x y - f x * (y-x)) / cmod (y-x) < e"
1.2774 +        using i yx  by (simp add: contour_integral_unique divide_less_eq)
1.2775 +    }
1.2776 +    then have "\<exists>d>0. \<forall>y\<in>s. y \<noteq> x \<and> cmod (y-x) < d \<longrightarrow> cmod (?pathint x y - f x * (y-x)) / cmod (y-x) < e"
1.2777 +      using d1 by blast
1.2778 +  }
1.2779 +  then have *: "((\<lambda>y. (contour_integral (linepath x y) f - f x * (y - x)) /\<^sub>R cmod (y - x)) \<longlongrightarrow> 0) (at x within s)"
1.2780 +    by (simp add: Lim_within dist_norm inverse_eq_divide)
1.2781 +  show ?thesis
1.2782 +    apply (simp add: has_field_derivative_def has_derivative_within bounded_linear_mult_right)
1.2783 +    apply (rule Lim_transform [OF * Lim_eventually])
1.2784 +    using linordered_field_no_ub
1.2785 +    apply (force simp: inverse_eq_divide [symmetric] eventually_at)
1.2786 +    done
1.2787 +qed
1.2788 +
1.2789 +lemma contour_integral_convex_primitive:
1.2790 +  "\<lbrakk>convex s; continuous_on s f;
1.2791 +    \<And>a b c. \<lbrakk>a \<in> s; b \<in> s; c \<in> s\<rbrakk> \<Longrightarrow> (f has_contour_integral 0) (linepath a b +++ linepath b c +++ linepath c a)\<rbrakk>
1.2792 +         \<Longrightarrow> \<exists>g. \<forall>x \<in> s. (g has_field_derivative f x) (at x within s)"
1.2793 +  apply (cases "s={}")
1.2794 +  apply (simp_all add: ex_in_conv [symmetric])
1.2795 +  apply (blast intro: triangle_contour_integrals_convex_primitive has_chain_integral_chain_integral3)
1.2796 +  done
1.2797 +
1.2798 +lemma holomorphic_convex_primitive:
1.2799 +  fixes f :: "complex \<Rightarrow> complex"
1.2800 +  shows
1.2801 +  "\<lbrakk>convex s; finite k; continuous_on s f;
1.2802 +    \<And>x. x \<in> interior s - k \<Longrightarrow> f field_differentiable at x\<rbrakk>
1.2803 +   \<Longrightarrow> \<exists>g. \<forall>x \<in> s. (g has_field_derivative f x) (at x within s)"
1.2804 +apply (rule contour_integral_convex_primitive [OF _ _ Cauchy_theorem_triangle_cofinite])
1.2805 +prefer 3
1.2806 +apply (erule continuous_on_subset)
1.2807 +apply (simp add: subset_hull continuous_on_subset, assumption+)
1.2808 +by (metis Diff_iff convex_contains_segment insert_absorb insert_subset interior_mono segment_convex_hull subset_hull)
1.2809 +
1.2810 +lemma Cauchy_theorem_convex:
1.2811 +    "\<lbrakk>continuous_on s f; convex s; finite k;
1.2812 +      \<And>x. x \<in> interior s - k \<Longrightarrow> f field_differentiable at x;
1.2813 +     valid_path g; path_image g \<subseteq> s;
1.2814 +     pathfinish g = pathstart g\<rbrakk> \<Longrightarrow> (f has_contour_integral 0) g"
1.2815 +  by (metis holomorphic_convex_primitive Cauchy_theorem_primitive)
1.2816 +
1.2817 +lemma Cauchy_theorem_convex_simple:
1.2818 +    "\<lbrakk>f holomorphic_on s; convex s;
1.2819 +     valid_path g; path_image g \<subseteq> s;
1.2820 +     pathfinish g = pathstart g\<rbrakk> \<Longrightarrow> (f has_contour_integral 0) g"
1.2821 +  apply (rule Cauchy_theorem_convex)
1.2822 +  apply (simp_all add: holomorphic_on_imp_continuous_on)
1.2823 +  apply (rule finite.emptyI)
1.2824 +  using at_within_interior holomorphic_on_def interior_subset by fastforce
1.2825 +
1.2826 +
1.2827 +text\<open>In particular for a disc\<close>
1.2828 +lemma Cauchy_theorem_disc:
1.2829 +    "\<lbrakk>finite k; continuous_on (cball a e) f;
1.2830 +      \<And>x. x \<in> ball a e - k \<Longrightarrow> f field_differentiable at x;
1.2831 +     valid_path g; path_image g \<subseteq> cball a e;
1.2832 +     pathfinish g = pathstart g\<rbrakk> \<Longrightarrow> (f has_contour_integral 0) g"
1.2833 +  apply (rule Cauchy_theorem_convex)
1.2834 +  apply (auto simp: convex_cball interior_cball)
1.2835 +  done
1.2836 +
1.2837 +lemma Cauchy_theorem_disc_simple:
1.2838 +    "\<lbrakk>f holomorphic_on (ball a e); valid_path g; path_image g \<subseteq> ball a e;
1.2839 +     pathfinish g = pathstart g\<rbrakk> \<Longrightarrow> (f has_contour_integral 0) g"
1.2841 +
1.2842 +
1.2843 +subsection\<open>Generalize integrability to local primitives\<close>
1.2844 +
1.2845 +lemma contour_integral_local_primitive_lemma:
1.2846 +  fixes f :: "complex\<Rightarrow>complex"
1.2847 +  shows
1.2848 +    "\<lbrakk>g piecewise_differentiable_on {a..b};
1.2849 +      \<And>x. x \<in> s \<Longrightarrow> (f has_field_derivative f' x) (at x within s);
1.2850 +      \<And>x. x \<in> {a..b} \<Longrightarrow> g x \<in> s\<rbrakk>
1.2851 +     \<Longrightarrow> (\<lambda>x. f' (g x) * vector_derivative g (at x within {a..b}))
1.2852 +            integrable_on {a..b}"
1.2853 +  apply (cases "cbox a b = {}", force)
1.2854 +  apply (simp add: integrable_on_def)
1.2855 +  apply (rule exI)
1.2856 +  apply (rule contour_integral_primitive_lemma, assumption+)
1.2857 +  using atLeastAtMost_iff by blast
1.2858 +
1.2859 +lemma contour_integral_local_primitive_any:
1.2860 +  fixes f :: "complex \<Rightarrow> complex"
1.2861 +  assumes gpd: "g piecewise_differentiable_on {a..b}"
1.2862 +      and dh: "\<And>x. x \<in> s
1.2863 +               \<Longrightarrow> \<exists>d h. 0 < d \<and>
1.2864 +                         (\<forall>y. norm(y - x) < d \<longrightarrow> (h has_field_derivative f y) (at y within s))"
1.2865 +      and gs: "\<And>x. x \<in> {a..b} \<Longrightarrow> g x \<in> s"
1.2866 +  shows "(\<lambda>x. f(g x) * vector_derivative g (at x)) integrable_on {a..b}"
1.2867 +proof -
1.2868 +  { fix x
1.2869 +    assume x: "a \<le> x" "x \<le> b"
1.2870 +    obtain d h where d: "0 < d"
1.2871 +               and h: "(\<And>y. norm(y - g x) < d \<Longrightarrow> (h has_field_derivative f y) (at y within s))"
1.2872 +      using x gs dh by (metis atLeastAtMost_iff)
1.2873 +    have "continuous_on {a..b} g" using gpd piecewise_differentiable_on_def by blast
1.2874 +    then obtain e where e: "e>0" and lessd: "\<And>x'. x' \<in> {a..b} \<Longrightarrow> \<bar>x' - x\<bar> < e \<Longrightarrow> cmod (g x' - g x) < d"
1.2875 +      using x d
1.2876 +      apply (auto simp: dist_norm continuous_on_iff)
1.2877 +      apply (drule_tac x=x in bspec)
1.2878 +      using x apply simp
1.2879 +      apply (drule_tac x=d in spec, auto)
1.2880 +      done
1.2881 +    have "\<exists>d>0. \<forall>u v. u \<le> x \<and> x \<le> v \<and> {u..v} \<subseteq> ball x d \<and> (u \<le> v \<longrightarrow> a \<le> u \<and> v \<le> b) \<longrightarrow>
1.2882 +                          (\<lambda>x. f (g x) * vector_derivative g (at x)) integrable_on {u..v}"
1.2883 +      apply (rule_tac x=e in exI)
1.2884 +      using e
1.2885 +      apply (simp add: integrable_on_localized_vector_derivative [symmetric], clarify)
1.2886 +      apply (rule_tac f = h and s = "g ` {u..v}" in contour_integral_local_primitive_lemma)
1.2887 +        apply (meson atLeastatMost_subset_iff gpd piecewise_differentiable_on_subset)
1.2888 +       apply (force simp: ball_def dist_norm intro: lessd gs DERIV_subset [OF h], force)
1.2889 +      done
1.2890 +  } then
1.2891 +  show ?thesis
1.2892 +    by (force simp: intro!: integrable_on_little_subintervals [of a b, simplified])
1.2893 +qed
1.2894 +
1.2895 +lemma contour_integral_local_primitive:
1.2896 +  fixes f :: "complex \<Rightarrow> complex"
1.2897 +  assumes g: "valid_path g" "path_image g \<subseteq> s"
1.2898 +      and dh: "\<And>x. x \<in> s
1.2899 +               \<Longrightarrow> \<exists>d h. 0 < d \<and>
1.2900 +                         (\<forall>y. norm(y - x) < d \<longrightarrow> (h has_field_derivative f y) (at y within s))"
1.2901 +  shows "f contour_integrable_on g"
1.2902 +  using g
1.2903 +  apply (simp add: valid_path_def path_image_def contour_integrable_on_def has_contour_integral_def
1.2904 +            has_integral_localized_vector_derivative integrable_on_def [symmetric])
1.2905 +  using contour_integral_local_primitive_any [OF _ dh]
1.2906 +  by (meson image_subset_iff piecewise_C1_imp_differentiable)
1.2907 +
1.2908 +
1.2909 +text\<open>In particular if a function is holomorphic\<close>
1.2910 +
1.2911 +lemma contour_integrable_holomorphic:
1.2912 +  assumes contf: "continuous_on s f"
1.2913 +      and os: "open s"
1.2914 +      and k: "finite k"
1.2915 +      and g: "valid_path g" "path_image g \<subseteq> s"
1.2916 +      and fcd: "\<And>x. x \<in> s - k \<Longrightarrow> f field_differentiable at x"
1.2917 +    shows "f contour_integrable_on g"
1.2918 +proof -
1.2919 +  { fix z
1.2920 +    assume z: "z \<in> s"
1.2921 +    obtain d where d: "d>0" "ball z d \<subseteq> s" using  \<open>open s\<close> z
1.2922 +      by (auto simp: open_contains_ball)
1.2923 +    then have contfb: "continuous_on (ball z d) f"
1.2924 +      using contf continuous_on_subset by blast
1.2925 +    obtain h where "\<forall>y\<in>ball z d. (h has_field_derivative f y) (at y within ball z d)"
1.2926 +      using holomorphic_convex_primitive [OF convex_ball k contfb fcd] d
1.2927 +            interior_subset by force
1.2928 +    then have "\<forall>y\<in>ball z d. (h has_field_derivative f y) (at y within s)"
1.2929 +      by (metis Topology_Euclidean_Space.open_ball at_within_open d(2) os subsetCE)
1.2930 +    then have "\<exists>h. (\<forall>y. cmod (y - z) < d \<longrightarrow> (h has_field_derivative f y) (at y within s))"
1.2931 +      by (force simp: dist_norm norm_minus_commute)
1.2932 +    then have "\<exists>d h. 0 < d \<and> (\<forall>y. cmod (y - z) < d \<longrightarrow> (h has_field_derivative f y) (at y within s))"
1.2933 +      using d by blast
1.2934 +  }
1.2935 +  then show ?thesis
1.2936 +    by (rule contour_integral_local_primitive [OF g])
1.2937 +qed
1.2938 +
1.2939 +lemma contour_integrable_holomorphic_simple:
1.2940 +  assumes fh: "f holomorphic_on s"
1.2941 +      and os: "open s"
1.2942 +      and g: "valid_path g" "path_image g \<subseteq> s"
1.2943 +    shows "f contour_integrable_on g"
1.2944 +  apply (rule contour_integrable_holomorphic [OF _ os Finite_Set.finite.emptyI g])
1.2945 +  apply (simp add: fh holomorphic_on_imp_continuous_on)
1.2946 +  using fh  by (simp add: field_differentiable_def holomorphic_on_open os)
1.2947 +
1.2948 +lemma continuous_on_inversediff:
1.2949 +  fixes z:: "'a::real_normed_field" shows "z \<notin> s \<Longrightarrow> continuous_on s (\<lambda>w. 1 / (w - z))"
1.2950 +  by (rule continuous_intros | force)+
1.2951 +
1.2952 +corollary contour_integrable_inversediff:
1.2953 +    "\<lbrakk>valid_path g; z \<notin> path_image g\<rbrakk> \<Longrightarrow> (\<lambda>w. 1 / (w-z)) contour_integrable_on g"
1.2954 +apply (rule contour_integrable_holomorphic_simple [of _ "UNIV-{z}"])
1.2955 +apply (auto simp: holomorphic_on_open open_delete intro!: derivative_eq_intros)
1.2956 +done
1.2957 +
1.2958 +text\<open>Key fact that path integral is the same for a "nearby" path. This is the
1.2959 + main lemma for the homotopy form of Cauchy's theorem and is also useful
1.2960 + if we want "without loss of generality" to assume some nice properties of a
1.2961 + path (e.g. smoothness). It can also be used to define the integrals of
1.2962 + analytic functions over arbitrary continuous paths. This is just done for
1.2963 + winding numbers now.
1.2964 +\<close>
1.2965 +
1.2966 +text\<open>A technical definition to avoid duplication of similar proofs,
1.2967 +     for paths joined at the ends versus looping paths\<close>
1.2968 +definition linked_paths :: "bool \<Rightarrow> (real \<Rightarrow> 'a) \<Rightarrow> (real \<Rightarrow> 'a::topological_space) \<Rightarrow> bool"
1.2969 +  where "linked_paths atends g h ==
1.2970 +        (if atends then pathstart h = pathstart g \<and> pathfinish h = pathfinish g
1.2971 +                   else pathfinish g = pathstart g \<and> pathfinish h = pathstart h)"
1.2972 +
1.2973 +text\<open>This formulation covers two cases: @{term g} and @{term h} share their
1.2974 +      start and end points; @{term g} and @{term h} both loop upon themselves.\<close>
1.2975 +lemma contour_integral_nearby:
1.2976 +  assumes os: "open s" and p: "path p" "path_image p \<subseteq> s"
1.2977 +    shows
1.2978 +       "\<exists>d. 0 < d \<and>
1.2979 +            (\<forall>g h. valid_path g \<and> valid_path h \<and>
1.2980 +                  (\<forall>t \<in> {0..1}. norm(g t - p t) < d \<and> norm(h t - p t) < d) \<and>
1.2981 +                  linked_paths atends g h
1.2982 +                  \<longrightarrow> path_image g \<subseteq> s \<and> path_image h \<subseteq> s \<and>
1.2983 +                      (\<forall>f. f holomorphic_on s \<longrightarrow> contour_integral h f = contour_integral g f))"
1.2984 +proof -
1.2985 +  have "\<forall>z. \<exists>e. z \<in> path_image p \<longrightarrow> 0 < e \<and> ball z e \<subseteq> s"
1.2986 +    using open_contains_ball os p(2) by blast
1.2987 +  then obtain ee where ee: "\<And>z. z \<in> path_image p \<Longrightarrow> 0 < ee z \<and> ball z (ee z) \<subseteq> s"
1.2988 +    by metis
1.2989 +  define cover where "cover = (\<lambda>z. ball z (ee z/3)) ` (path_image p)"
1.2990 +  have "compact (path_image p)"
1.2991 +    by (metis p(1) compact_path_image)
1.2992 +  moreover have "path_image p \<subseteq> (\<Union>c\<in>path_image p. ball c (ee c / 3))"
1.2993 +    using ee by auto
1.2994 +  ultimately have "\<exists>D \<subseteq> cover. finite D \<and> path_image p \<subseteq> \<Union>D"
1.2995 +    by (simp add: compact_eq_heine_borel cover_def)
1.2996 +  then obtain D where D: "D \<subseteq> cover" "finite D" "path_image p \<subseteq> \<Union>D"
1.2997 +    by blast
1.2998 +  then obtain k where k: "k \<subseteq> {0..1}" "finite k" and D_eq: "D = ((\<lambda>z. ball z (ee z / 3)) \<circ> p) ` k"
1.2999 +    apply (simp add: cover_def path_image_def image_comp)
1.3000 +    apply (blast dest!: finite_subset_image [OF \<open>finite D\<close>])
1.3001 +    done
1.3002 +  then have kne: "k \<noteq> {}"
1.3003 +    using D by auto
1.3004 +  have pi: "\<And>i. i \<in> k \<Longrightarrow> p i \<in> path_image p"
1.3005 +    using k  by (auto simp: path_image_def)
1.3006 +  then have eepi: "\<And>i. i \<in> k \<Longrightarrow> 0 < ee((p i))"
1.3007 +    by (metis ee)
1.3008 +  define e where "e = Min((ee o p) ` k)"
1.3009 +  have fin_eep: "finite ((ee o p) ` k)"
1.3010 +    using k  by blast
1.3011 +  have enz: "0 < e"
1.3012 +    using ee k  by (simp add: kne e_def Min_gr_iff [OF fin_eep] eepi)
1.3013 +  have "uniformly_continuous_on {0..1} p"
1.3014 +    using p  by (simp add: path_def compact_uniformly_continuous)
1.3015 +  then obtain d::real where d: "d>0"
1.3016 +          and de: "\<And>x x'. \<bar>x' - x\<bar> < d \<Longrightarrow> x\<in>{0..1} \<Longrightarrow> x'\<in>{0..1} \<Longrightarrow> cmod (p x' - p x) < e/3"
1.3017 +    unfolding uniformly_continuous_on_def dist_norm real_norm_def
1.3018 +    by (metis divide_pos_pos enz zero_less_numeral)
1.3019 +  then obtain N::nat where N: "N>0" "inverse N < d"
1.3020 +    using real_arch_inverse [of d]   by auto
1.3021 +  { fix g h
1.3022 +    assume g: "valid_path g" and gp: "\<forall>t\<in>{0..1}. cmod (g t - p t) < e / 3"
1.3023 +       and h: "valid_path h" and hp: "\<forall>t\<in>{0..1}. cmod (h t - p t) < e / 3"
1.3024 +       and joins: "linked_paths atends g h"
1.3025 +    { fix t::real
1.3026 +      assume t: "0 \<le> t" "t \<le> 1"
1.3027 +      then obtain u where u: "u \<in> k" and ptu: "p t \<in> ball(p u) (ee(p u) / 3)"
1.3028 +        using \<open>path_image p \<subseteq> \<Union>D\<close> D_eq by (force simp: path_image_def)
1.3029 +      then have ele: "e \<le> ee (p u)" using fin_eep
1.3030 +        by (simp add: e_def)
1.3031 +      have "cmod (g t - p t) < e / 3" "cmod (h t - p t) < e / 3"
1.3032 +        using gp hp t by auto
1.3033 +      with ele have "cmod (g t - p t) < ee (p u) / 3"
1.3034 +                    "cmod (h t - p t) < ee (p u) / 3"
1.3035 +        by linarith+
1.3036 +      then have "g t \<in> ball(p u) (ee(p u))"  "h t \<in> ball(p u) (ee(p u))"
1.3037 +        using norm_diff_triangle_ineq [of "g t" "p t" "p t" "p u"]
1.3038 +              norm_diff_triangle_ineq [of "h t" "p t" "p t" "p u"] ptu eepi u
1.3039 +        by (force simp: dist_norm ball_def norm_minus_commute)+
1.3040 +      then have "g t \<in> s" "h t \<in> s" using ee u k
1.3041 +        by (auto simp: path_image_def ball_def)
1.3042 +    }
1.3043 +    then have ghs: "path_image g \<subseteq> s" "path_image h \<subseteq> s"
1.3044 +      by (auto simp: path_image_def)
1.3045 +    moreover
1.3046 +    { fix f
1.3047 +      assume fhols: "f holomorphic_on s"
1.3048 +      then have fpa: "f contour_integrable_on g"  "f contour_integrable_on h"
1.3049 +        using g ghs h holomorphic_on_imp_continuous_on os contour_integrable_holomorphic_simple
1.3050 +        by blast+
1.3051 +      have contf: "continuous_on s f"
1.3052 +        by (simp add: fhols holomorphic_on_imp_continuous_on)
1.3053 +      { fix z
1.3054 +        assume z: "z \<in> path_image p"
1.3055 +        have "f holomorphic_on ball z (ee z)"
1.3056 +          using fhols ee z holomorphic_on_subset by blast
1.3057 +        then have "\<exists>ff. (\<forall>w \<in> ball z (ee z). (ff has_field_derivative f w) (at w))"
1.3058 +          using holomorphic_convex_primitive [of "ball z (ee z)" "{}" f, simplified]
1.3059 +          by (metis open_ball at_within_open holomorphic_on_def holomorphic_on_imp_continuous_on mem_ball)
1.3060 +      }
1.3061 +      then obtain ff where ff:
1.3062 +            "\<And>z w. \<lbrakk>z \<in> path_image p; w \<in> ball z (ee z)\<rbrakk> \<Longrightarrow> (ff z has_field_derivative f w) (at w)"
1.3063 +        by metis
1.3064 +      { fix n
1.3065 +        assume n: "n \<le> N"
1.3066 +        then have "contour_integral(subpath 0 (n/N) h) f - contour_integral(subpath 0 (n/N) g) f =
1.3067 +                   contour_integral(linepath (g(n/N)) (h(n/N))) f - contour_integral(linepath (g 0) (h 0)) f"
1.3068 +        proof (induct n)
1.3069 +          case 0 show ?case by simp
1.3070 +        next
1.3071 +          case (Suc n)
1.3072 +          obtain t where t: "t \<in> k" and "p (n/N) \<in> ball(p t) (ee(p t) / 3)"
1.3073 +            using \<open>path_image p \<subseteq> \<Union>D\<close> [THEN subsetD, where c="p (n/N)"] D_eq N Suc.prems
1.3074 +            by (force simp: path_image_def)
1.3075 +          then have ptu: "cmod (p t - p (n/N)) < ee (p t) / 3"
1.3076 +            by (simp add: dist_norm)
1.3077 +          have e3le: "e/3 \<le> ee (p t) / 3"  using fin_eep t
1.3078 +            by (simp add: e_def)
1.3079 +          { fix x
1.3080 +            assume x: "n/N \<le> x" "x \<le> (1 + n)/N"
1.3081 +            then have nN01: "0 \<le> n/N" "(1 + n)/N \<le> 1"
1.3082 +              using Suc.prems by auto
1.3083 +            then have x01: "0 \<le> x" "x \<le> 1"
1.3084 +              using x by linarith+
1.3085 +            have "cmod (p t - p x)  < ee (p t) / 3 + e/3"
1.3086 +              apply (rule norm_diff_triangle_less [OF ptu de])
1.3087 +              using x N x01 Suc.prems
1.3088 +              apply (auto simp: field_simps)
1.3089 +              done
1.3090 +            then have ptx: "cmod (p t - p x) < 2*ee (p t)/3"
1.3091 +              using e3le eepi [OF t] by simp
1.3092 +            have "cmod (p t - g x) < 2*ee (p t)/3 + e/3 "
1.3093 +              apply (rule norm_diff_triangle_less [OF ptx])
1.3094 +              using gp x01 by (simp add: norm_minus_commute)
1.3095 +            also have "... \<le> ee (p t)"
1.3096 +              using e3le eepi [OF t] by simp
1.3097 +            finally have gg: "cmod (p t - g x) < ee (p t)" .
1.3098 +            have "cmod (p t - h x) < 2*ee (p t)/3 + e/3 "
1.3099 +              apply (rule norm_diff_triangle_less [OF ptx])
1.3100 +              using hp x01 by (simp add: norm_minus_commute)
1.3101 +            also have "... \<le> ee (p t)"
1.3102 +              using e3le eepi [OF t] by simp
1.3103 +            finally have "cmod (p t - g x) < ee (p t)"
1.3104 +                         "cmod (p t - h x) < ee (p t)"
1.3105 +              using gg by auto
1.3106 +          } note ptgh_ee = this
1.3107 +          have pi_hgn: "path_image (linepath (h (n/N)) (g (n/N))) \<subseteq> ball (p t) (ee (p t))"
1.3108 +            using ptgh_ee [of "n/N"] Suc.prems
1.3109 +            by (auto simp: field_simps dist_norm dest: segment_furthest_le [where y="p t"])
1.3110 +          then have gh_ns: "closed_segment (g (n/N)) (h (n/N)) \<subseteq> s"
1.3111 +            using \<open>N>0\<close> Suc.prems
1.3112 +            apply (simp add: path_image_join field_simps closed_segment_commute)
1.3113 +            apply (erule order_trans)
1.3114 +            apply (simp add: ee pi t)
1.3115 +            done
1.3116 +          have pi_ghn': "path_image (linepath (g ((1 + n) / N)) (h ((1 + n) / N)))
1.3117 +                  \<subseteq> ball (p t) (ee (p t))"
1.3118 +            using ptgh_ee [of "(1+n)/N"] Suc.prems
1.3119 +            by (auto simp: field_simps dist_norm dest: segment_furthest_le [where y="p t"])
1.3120 +          then have gh_n's: "closed_segment (g ((1 + n) / N)) (h ((1 + n) / N)) \<subseteq> s"
1.3121 +            using \<open>N>0\<close> Suc.prems ee pi t
1.3122 +            by (auto simp: Path_Connected.path_image_join field_simps)
1.3123 +          have pi_subset_ball:
1.3124 +                "path_image (subpath (n/N) ((1+n) / N) g +++ linepath (g ((1+n) / N)) (h ((1+n) / N)) +++
1.3125 +                             subpath ((1+n) / N) (n/N) h +++ linepath (h (n/N)) (g (n/N)))
1.3126 +                 \<subseteq> ball (p t) (ee (p t))"
1.3127 +            apply (intro subset_path_image_join pi_hgn pi_ghn')
1.3128 +            using \<open>N>0\<close> Suc.prems
1.3129 +            apply (auto simp: path_image_subpath dist_norm field_simps closed_segment_eq_real_ivl ptgh_ee)
1.3130 +            done
1.3131 +          have pi0: "(f has_contour_integral 0)
1.3132 +                       (subpath (n/ N) ((Suc n)/N) g +++ linepath(g ((Suc n) / N)) (h((Suc n) / N)) +++
1.3133 +                        subpath ((Suc n) / N) (n/N) h +++ linepath(h (n/N)) (g (n/N)))"
1.3134 +            apply (rule Cauchy_theorem_primitive [of "ball(p t) (ee(p t))" "ff (p t)" "f"])
1.3135 +            apply (metis ff open_ball at_within_open pi t)
1.3136 +            apply (intro valid_path_join)
1.3137 +            using Suc.prems pi_subset_ball apply (simp_all add: valid_path_subpath g h)
1.3138 +            done
1.3139 +          have fpa1: "f contour_integrable_on subpath (real n / real N) (real (Suc n) / real N) g"
1.3140 +            using Suc.prems by (simp add: contour_integrable_subpath g fpa)
1.3141 +          have fpa2: "f contour_integrable_on linepath (g (real (Suc n) / real N)) (h (real (Suc n) / real N))"
1.3142 +            using gh_n's
1.3143 +            by (auto intro!: contour_integrable_continuous_linepath continuous_on_subset [OF contf])
1.3144 +          have fpa3: "f contour_integrable_on linepath (h (real n / real N)) (g (real n / real N))"
1.3145 +            using gh_ns
1.3146 +            by (auto simp: closed_segment_commute intro!: contour_integrable_continuous_linepath continuous_on_subset [OF contf])
1.3147 +          have eq0: "contour_integral (subpath (n/N) ((Suc n) / real N) g) f +
1.3148 +                     contour_integral (linepath (g ((Suc n) / N)) (h ((Suc n) / N))) f +
1.3149 +                     contour_integral (subpath ((Suc n) / N) (n/N) h) f +
1.3150 +                     contour_integral (linepath (h (n/N)) (g (n/N))) f = 0"
1.3151 +            using contour_integral_unique [OF pi0] Suc.prems
1.3152 +            by (simp add: g h fpa valid_path_subpath contour_integrable_subpath
1.3153 +                          fpa1 fpa2 fpa3 algebra_simps del: of_nat_Suc)
1.3154 +          have *: "\<And>hn he hn' gn gd gn' hgn ghn gh0 ghn'.
1.3155 +                    \<lbrakk>hn - gn = ghn - gh0;
1.3156 +                     gd + ghn' + he + hgn = (0::complex);
1.3157 +                     hn - he = hn'; gn + gd = gn'; hgn = -ghn\<rbrakk> \<Longrightarrow> hn' - gn' = ghn' - gh0"
1.3158 +            by (auto simp: algebra_simps)
1.3159 +          have "contour_integral (subpath 0 (n/N) h) f - contour_integral (subpath ((Suc n) / N) (n/N) h) f =
1.3160 +                contour_integral (subpath 0 (n/N) h) f + contour_integral (subpath (n/N) ((Suc n) / N) h) f"
1.3161 +            unfolding reversepath_subpath [symmetric, of "((Suc n) / N)"]
1.3162 +            using Suc.prems by (simp add: h fpa contour_integral_reversepath valid_path_subpath contour_integrable_subpath)
1.3163 +          also have "... = contour_integral (subpath 0 ((Suc n) / N) h) f"
1.3164 +            using Suc.prems by (simp add: contour_integral_subpath_combine h fpa)
1.3165 +          finally have pi0_eq:
1.3166 +               "contour_integral (subpath 0 (n/N) h) f - contour_integral (subpath ((Suc n) / N) (n/N) h) f =
1.3167 +                contour_integral (subpath 0 ((Suc n) / N) h) f" .
1.3168 +          show ?case
1.3169 +            apply (rule * [OF Suc.hyps eq0 pi0_eq])
1.3170 +            using Suc.prems
1.3171 +            apply (simp_all add: g h fpa contour_integral_subpath_combine
1.3172 +                     contour_integral_reversepath [symmetric] contour_integrable_continuous_linepath
1.3173 +                     continuous_on_subset [OF contf gh_ns])
1.3174 +            done
1.3175 +      qed
1.3176 +      } note ind = this
1.3177 +      have "contour_integral h f = contour_integral g f"
1.3178 +        using ind [OF order_refl] N joins
1.3180 +    }
1.3181 +    ultimately
1.3182 +    have "path_image g \<subseteq> s \<and> path_image h \<subseteq> s \<and> (\<forall>f. f holomorphic_on s \<longrightarrow> contour_integral h f = contour_integral g f)"
1.3183 +      by metis
1.3184 +  } note * = this
1.3185 +  show ?thesis
1.3186 +    apply (rule_tac x="e/3" in exI)
1.3187 +    apply (rule conjI)
1.3188 +    using enz apply simp
1.3189 +    apply (clarsimp simp only: ball_conj_distrib)
1.3190 +    apply (rule *; assumption)
1.3191 +    done
1.3192 +qed
1.3193 +
1.3194 +
1.3195 +lemma
1.3196 +  assumes "open s" "path p" "path_image p \<subseteq> s"
1.3197 +    shows contour_integral_nearby_ends:
1.3198 +      "\<exists>d. 0 < d \<and>
1.3199 +              (\<forall>g h. valid_path g \<and> valid_path h \<and>
1.3200 +                    (\<forall>t \<in> {0..1}. norm(g t - p t) < d \<and> norm(h t - p t) < d) \<and>
1.3201 +                    pathstart h = pathstart g \<and> pathfinish h = pathfinish g
1.3202 +                    \<longrightarrow> path_image g \<subseteq> s \<and>
1.3203 +                        path_image h \<subseteq> s \<and>
1.3204 +                        (\<forall>f. f holomorphic_on s
1.3205 +                            \<longrightarrow> contour_integral h f = contour_integral g f))"
1.3206 +    and contour_integral_nearby_loops:
1.3207 +      "\<exists>d. 0 < d \<and>
1.3208 +              (\<forall>g h. valid_path g \<and> valid_path h \<and>
1.3209 +                    (\<forall>t \<in> {0..1}. norm(g t - p t) < d \<and> norm(h t - p t) < d) \<and>
1.3210 +                    pathfinish g = pathstart g \<and> pathfinish h = pathstart h
1.3211 +                    \<longrightarrow> path_image g \<subseteq> s \<and>
1.3212 +                        path_image h \<subseteq> s \<and>
1.3213 +                        (\<forall>f. f holomorphic_on s
1.3214 +                            \<longrightarrow> contour_integral h f = contour_integral g f))"
1.3215 +  using contour_integral_nearby [OF assms, where atends=True]
1.3216 +  using contour_integral_nearby [OF assms, where atends=False]
1.3217 +  unfolding linked_paths_def by simp_all
1.3218 +
1.3219 +corollary differentiable_polynomial_function:
1.3220 +  fixes p :: "real \<Rightarrow> 'a::euclidean_space"
1.3221 +  shows "polynomial_function p \<Longrightarrow> p differentiable_on s"
1.3222 +by (meson has_vector_derivative_polynomial_function differentiable_at_imp_differentiable_on differentiable_def has_vector_derivative_def)
1.3223 +
1.3224 +lemma C1_differentiable_polynomial_function:
1.3225 +  fixes p :: "real \<Rightarrow> 'a::euclidean_space"
1.3226 +  shows "polynomial_function p \<Longrightarrow> p C1_differentiable_on s"
1.3227 +  by (metis continuous_on_polymonial_function C1_differentiable_on_def  has_vector_derivative_polynomial_function)
1.3228 +
1.3229 +lemma valid_path_polynomial_function:
1.3230 +  fixes p :: "real \<Rightarrow> 'a::euclidean_space"
1.3231 +  shows "polynomial_function p \<Longrightarrow> valid_path p"
1.3232 +by (force simp: valid_path_def piecewise_C1_differentiable_on_def continuous_on_polymonial_function C1_differentiable_polynomial_function)
1.3233 +
1.3234 +lemma valid_path_subpath_trivial [simp]:
1.3235 +    fixes g :: "real \<Rightarrow> 'a::euclidean_space"
1.3236 +    shows "z \<noteq> g x \<Longrightarrow> valid_path (subpath x x g)"
1.3237 +  by (simp add: subpath_def valid_path_polynomial_function)
1.3238 +
1.3239 +lemma contour_integral_bound_exists:
1.3240 +assumes s: "open s"
1.3241 +    and g: "valid_path g"
1.3242 +    and pag: "path_image g \<subseteq> s"
1.3243 +  shows "\<exists>L. 0 < L \<and>
1.3244 +       (\<forall>f B. f holomorphic_on s \<and> (\<forall>z \<in> s. norm(f z) \<le> B)
1.3245 +         \<longrightarrow> norm(contour_integral g f) \<le> L*B)"
1.3246 +proof -
1.3247 +have "path g" using g
1.3248 +  by (simp add: valid_path_imp_path)
1.3249 +then obtain d::real and p
1.3250 +  where d: "0 < d"
1.3251 +    and p: "polynomial_function p" "path_image p \<subseteq> s"
1.3252 +    and pi: "\<And>f. f holomorphic_on s \<Longrightarrow> contour_integral g f = contour_integral p f"
1.3253 +  using contour_integral_nearby_ends [OF s \<open>path g\<close> pag]
1.3254 +  apply clarify
1.3255 +  apply (drule_tac x=g in spec)
1.3256 +  apply (simp only: assms)
1.3257 +  apply (force simp: valid_path_polynomial_function dest: path_approx_polynomial_function)
1.3258 +  done
1.3259 +then obtain p' where p': "polynomial_function p'"
1.3260 +         "\<And>x. (p has_vector_derivative (p' x)) (at x)"
1.3261 +  using has_vector_derivative_polynomial_function by force
1.3262 +then have "bounded(p' ` {0..1})"
1.3263 +  using continuous_on_polymonial_function
1.3264 +  by (force simp: intro!: compact_imp_bounded compact_continuous_image)
1.3265 +then obtain L where L: "L>0" and nop': "\<And>x. x \<in> {0..1} \<Longrightarrow> norm (p' x) \<le> L"
1.3266 +  by (force simp: bounded_pos)
1.3267 +{ fix f B
1.3268 +  assume f: "f holomorphic_on s"
1.3269 +     and B: "\<And>z. z\<in>s \<Longrightarrow> cmod (f z) \<le> B"
1.3270 +  then have "f contour_integrable_on p \<and> valid_path p"
1.3271 +    using p s
1.3272 +    by (blast intro: valid_path_polynomial_function contour_integrable_holomorphic_simple holomorphic_on_imp_continuous_on)
1.3273 +  moreover have "\<And>x. x \<in> {0..1} \<Longrightarrow> cmod (vector_derivative p (at x)) * cmod (f (p x)) \<le> L * B"
1.3274 +    apply (rule mult_mono)
1.3275 +    apply (subst Derivative.vector_derivative_at; force intro: p' nop')
1.3276 +    using L B p
1.3277 +    apply (auto simp: path_image_def image_subset_iff)
1.3278 +    done
1.3279 +  ultimately have "cmod (contour_integral g f) \<le> L * B"
1.3280 +    apply (simp add: pi [OF f])
1.3281 +    apply (simp add: contour_integral_integral)
1.3282 +    apply (rule order_trans [OF integral_norm_bound_integral])
1.3283 +    apply (auto simp: mult.commute integral_norm_bound_integral contour_integrable_on [symmetric] norm_mult)
1.3284 +    done
1.3285 +} then
1.3286 +show ?thesis
1.3287 +  by (force simp: L contour_integral_integral)
1.3288 +qed
1.3289 +
1.3290 +subsection\<open>Constancy of a function from a connected set into a finite, disconnected or discrete set\<close>
1.3291 +
1.3292 +text\<open>Still missing: versions for a set that is smaller than R, or countable.\<close>
1.3293 +
1.3294 +lemma continuous_disconnected_range_constant:
1.3295 +  assumes s: "connected s"
1.3296 +      and conf: "continuous_on s f"
1.3297 +      and fim: "f ` s \<subseteq> t"
1.3298 +      and cct: "\<And>y. y \<in> t \<Longrightarrow> connected_component_set t y = {y}"
1.3299 +    shows "\<exists>a. \<forall>x \<in> s. f x = a"
1.3300 +proof (cases "s = {}")
1.3301 +  case True then show ?thesis by force
1.3302 +next
1.3303 +  case False
1.3304 +  { fix x assume "x \<in> s"
1.3305 +    then have "f ` s \<subseteq> {f x}"
1.3306 +    by (metis connected_continuous_image conf connected_component_maximal fim image_subset_iff rev_image_eqI s cct)
1.3307 +  }
1.3308 +  with False show ?thesis
1.3309 +    by blast
1.3310 +qed
1.3311 +
1.3312 +lemma discrete_subset_disconnected:
1.3313 +  fixes s :: "'a::topological_space set"
1.3314 +  fixes t :: "'b::real_normed_vector set"
1.3315 +  assumes conf: "continuous_on s f"
1.3316 +      and no: "\<And>x. x \<in> s \<Longrightarrow> \<exists>e>0. \<forall>y. y \<in> s \<and> f y \<noteq> f x \<longrightarrow> e \<le> norm (f y - f x)"
1.3317 +   shows "f ` s \<subseteq> {y. connected_component_set (f ` s) y = {y}}"
1.3318 +proof -
1.3319 +  { fix x assume x: "x \<in> s"
1.3320 +    then obtain e where "e>0" and ele: "\<And>y. \<lbrakk>y \<in> s; f y \<noteq> f x\<rbrakk> \<Longrightarrow> e \<le> norm (f y - f x)"
1.3321 +      using conf no [OF x] by auto
1.3322 +    then have e2: "0 \<le> e / 2"
1.3323 +      by simp
1.3324 +    have "f y = f x" if "y \<in> s" and ccs: "f y \<in> connected_component_set (f ` s) (f x)" for y
1.3325 +      apply (rule ccontr)
1.3326 +      using connected_closed [of "connected_component_set (f ` s) (f x)"] \<open>e>0\<close>
1.3327 +      apply (simp add: del: ex_simps)
1.3328 +      apply (drule spec [where x="cball (f x) (e / 2)"])
1.3329 +      apply (drule spec [where x="- ball(f x) e"])
1.3330 +      apply (auto simp: dist_norm open_closed [symmetric] simp del: le_divide_eq_numeral1 dest!: connected_component_in)
1.3331 +        apply (metis diff_self e2 ele norm_minus_commute norm_zero not_less)
1.3332 +       using centre_in_cball connected_component_refl_eq e2 x apply blast
1.3333 +      using ccs
1.3334 +      apply (force simp: cball_def dist_norm norm_minus_commute dest: ele [OF \<open>y \<in> s\<close>])
1.3335 +      done
1.3336 +    moreover have "connected_component_set (f ` s) (f x) \<subseteq> f ` s"
1.3337 +      by (auto simp: connected_component_in)
1.3338 +    ultimately have "connected_component_set (f ` s) (f x) = {f x}"
1.3339 +      by (auto simp: x)
1.3340 +  }
1.3341 +  with assms show ?thesis
1.3342 +    by blast
1.3343 +qed
1.3344 +
1.3345 +lemma finite_implies_discrete:
1.3346 +  fixes s :: "'a::topological_space set"
1.3347 +  assumes "finite (f ` s)"
1.3348 +  shows "(\<forall>x \<in> s. \<exists>e>0. \<forall>y. y \<in> s \<and> f y \<noteq> f x \<longrightarrow> e \<le> norm (f y - f x))"
1.3349 +proof -
1.3350 +  have "\<exists>e>0. \<forall>y. y \<in> s \<and> f y \<noteq> f x \<longrightarrow> e \<le> norm (f y - f x)" if "x \<in> s" for x
1.3351 +  proof (cases "f ` s - {f x} = {}")
1.3352 +    case True
1.3353 +    with zero_less_numeral show ?thesis
1.3354 +      by (fastforce simp add: Set.image_subset_iff cong: conj_cong)
1.3355 +  next
1.3356 +    case False
1.3357 +    then obtain z where z: "z \<in> s" "f z \<noteq> f x"
1.3358 +      by blast
1.3359 +    have finn: "finite {norm (z - f x) |z. z \<in> f ` s - {f x}}"
1.3360 +      using assms by simp
1.3361 +    then have *: "0 < Inf{norm(z - f x) | z. z \<in> f ` s - {f x}}"
1.3362 +      apply (rule finite_imp_less_Inf)
1.3363 +      using z apply force+
1.3364 +      done
1.3365 +    show ?thesis
1.3366 +      by (force intro!: * cInf_le_finite [OF finn])
1.3367 +  qed
1.3368 +  with assms show ?thesis
1.3369 +    by blast
1.3370 +qed
1.3371 +
1.3372 +text\<open>This proof requires the existence of two separate values of the range type.\<close>
1.3373 +lemma finite_range_constant_imp_connected:
1.3374 +  assumes "\<And>f::'a::topological_space \<Rightarrow> 'b::real_normed_algebra_1.
1.3375 +              \<lbrakk>continuous_on s f; finite(f ` s)\<rbrakk> \<Longrightarrow> \<exists>a. \<forall>x \<in> s. f x = a"
1.3376 +    shows "connected s"
1.3377 +proof -
1.3378 +  { fix t u
1.3379 +    assume clt: "closedin (subtopology euclidean s) t"
1.3380 +       and clu: "closedin (subtopology euclidean s) u"
1.3381 +       and tue: "t \<inter> u = {}" and tus: "t \<union> u = s"
1.3382 +    have conif: "continuous_on s (\<lambda>x. if x \<in> t then 0 else 1)"
1.3383 +      apply (subst tus [symmetric])
1.3384 +      apply (rule continuous_on_cases_local)
1.3385 +      using clt clu tue
1.3386 +      apply (auto simp: tus continuous_on_const)
1.3387 +      done
1.3388 +    have fi: "finite ((\<lambda>x. if x \<in> t then 0 else 1) ` s)"
1.3389 +      by (rule finite_subset [of _ "{0,1}"]) auto
1.3390 +    have "t = {} \<or> u = {}"
1.3391 +      using assms [OF conif fi] tus [symmetric]
1.3392 +      by (auto simp: Ball_def) (metis IntI empty_iff one_neq_zero tue)
1.3393 +  }
1.3394 +  then show ?thesis
1.3395 +    by (simp add: connected_closedin_eq)
1.3396 +qed
1.3397 +
1.3398 +lemma continuous_disconnected_range_constant_eq:
1.3399 +      "(connected s \<longleftrightarrow>
1.3400 +           (\<forall>f::'a::topological_space \<Rightarrow> 'b::real_normed_algebra_1.
1.3401 +            \<forall>t. continuous_on s f \<and> f ` s \<subseteq> t \<and> (\<forall>y \<in> t. connected_component_set t y = {y})
1.3402 +            \<longrightarrow> (\<exists>a::'b. \<forall>x \<in> s. f x = a)))" (is ?thesis1)
1.3403 +  and continuous_discrete_range_constant_eq:
1.3404 +      "(connected s \<longleftrightarrow>
1.3405 +         (\<forall>f::'a::topological_space \<Rightarrow> 'b::real_normed_algebra_1.
1.3406 +          continuous_on s f \<and>
1.3407 +          (\<forall>x \<in> s. \<exists>e. 0 < e \<and> (\<forall>y. y \<in> s \<and> (f y \<noteq> f x) \<longrightarrow> e \<le> norm(f y - f x)))
1.3408 +          \<longrightarrow> (\<exists>a::'b. \<forall>x \<in> s. f x = a)))" (is ?thesis2)
1.3409 +  and continuous_finite_range_constant_eq:
1.3410 +      "(connected s \<longleftrightarrow>
1.3411 +         (\<forall>f::'a::topological_space \<Rightarrow> 'b::real_normed_algebra_1.
1.3412 +          continuous_on s f \<and> finite (f ` s)
1.3413 +          \<longrightarrow> (\<exists>a::'b. \<forall>x \<in> s. f x = a)))" (is ?thesis3)
1.3414 +proof -
1.3415 +  have *: "\<And>s t u v. \<lbrakk>s \<Longrightarrow> t; t \<Longrightarrow> u; u \<Longrightarrow> v; v \<Longrightarrow> s\<rbrakk>
1.3416 +    \<Longrightarrow> (s \<longleftrightarrow> t) \<and> (s \<longleftrightarrow> u) \<and> (s \<longleftrightarrow> v)"
1.3417 +    by blast
1.3418 +  have "?thesis1 \<and> ?thesis2 \<and> ?thesis3"
1.3419 +    apply (rule *)
1.3420 +    using continuous_disconnected_range_constant apply metis
1.3421 +    apply clarify
1.3422 +    apply (frule discrete_subset_disconnected; blast)
1.3423 +    apply (blast dest: finite_implies_discrete)
1.3424 +    apply (blast intro!: finite_range_constant_imp_connected)
1.3425 +    done
1.3426 +  then show ?thesis1 ?thesis2 ?thesis3
1.3427 +    by blast+
1.3428 +qed
1.3429 +
1.3430 +lemma continuous_discrete_range_constant:
1.3431 +  fixes f :: "'a::topological_space \<Rightarrow> 'b::real_normed_algebra_1"
1.3432 +  assumes s: "connected s"
1.3433 +      and "continuous_on s f"
1.3434 +      and "\<And>x. x \<in> s \<Longrightarrow> \<exists>e>0. \<forall>y. y \<in> s \<and> f y \<noteq> f x \<longrightarrow> e \<le> norm (f y - f x)"
1.3435 +    shows "\<exists>a. \<forall>x \<in> s. f x = a"
1.3436 +  using continuous_discrete_range_constant_eq [THEN iffD1, OF s] assms
1.3437 +  by blast
1.3438 +
1.3439 +lemma continuous_finite_range_constant:
1.3440 +  fixes f :: "'a::topological_space \<Rightarrow> 'b::real_normed_algebra_1"
1.3441 +  assumes "connected s"
1.3442 +      and "continuous_on s f"
1.3443 +      and "finite (f ` s)"
1.3444 +    shows "\<exists>a. \<forall>x \<in> s. f x = a"
1.3445 +  using assms continuous_finite_range_constant_eq
1.3446 +  by blast
1.3447 +
1.3448 +
1.3449 +text\<open>We can treat even non-rectifiable paths as having a "length" for bounds on analytic functions in open sets.\<close>
1.3450 +
1.3451 +subsection\<open>Winding Numbers\<close>
1.3452 +
1.3453 +definition winding_number:: "[real \<Rightarrow> complex, complex] \<Rightarrow> complex" where
1.3454 +  "winding_number \<gamma> z \<equiv>
1.3455 +    @n. \<forall>e > 0. \<exists>p. valid_path p \<and> z \<notin> path_image p \<and>
1.3456 +                    pathstart p = pathstart \<gamma> \<and>
1.3457 +                    pathfinish p = pathfinish \<gamma> \<and>
1.3458 +                    (\<forall>t \<in> {0..1}. norm(\<gamma> t - p t) < e) \<and>
1.3459 +                    contour_integral p (\<lambda>w. 1/(w - z)) = 2 * pi * \<i> * n"
1.3460 +
1.3461 +lemma winding_number:
1.3462 +  assumes "path \<gamma>" "z \<notin> path_image \<gamma>" "0 < e"
1.3463 +    shows "\<exists>p. valid_path p \<and> z \<notin> path_image p \<and>
1.3464 +               pathstart p = pathstart \<gamma> \<and>
1.3465 +               pathfinish p = pathfinish \<gamma> \<and>
1.3466 +               (\<forall>t \<in> {0..1}. norm (\<gamma> t - p t) < e) \<and>
1.3467 +               contour_integral p (\<lambda>w. 1/(w - z)) = 2 * pi * \<i> * winding_number \<gamma> z"
1.3468 +proof -
1.3469 +  have "path_image \<gamma> \<subseteq> UNIV - {z}"
1.3470 +    using assms by blast
1.3471 +  then obtain d
1.3472 +    where d: "d>0"
1.3473 +      and pi_eq: "\<And>h1 h2. valid_path h1 \<and> valid_path h2 \<and>
1.3474 +                    (\<forall>t\<in>{0..1}. cmod (h1 t - \<gamma> t) < d \<and> cmod (h2 t - \<gamma> t) < d) \<and>
1.3475 +                    pathstart h2 = pathstart h1 \<and> pathfinish h2 = pathfinish h1 \<longrightarrow>
1.3476 +                      path_image h1 \<subseteq> UNIV - {z} \<and> path_image h2 \<subseteq> UNIV - {z} \<and>
1.3477 +                      (\<forall>f. f holomorphic_on UNIV - {z} \<longrightarrow> contour_integral h2 f = contour_integral h1 f)"
1.3478 +    using contour_integral_nearby_ends [of "UNIV - {z}" \<gamma>] assms by (auto simp: open_delete)
1.3479 +  then obtain h where h: "polynomial_function h \<and> pathstart h = pathstart \<gamma> \<and> pathfinish h = pathfinish \<gamma> \<and>
1.3480 +                          (\<forall>t \<in> {0..1}. norm(h t - \<gamma> t) < d/2)"
1.3481 +    using path_approx_polynomial_function [OF \<open>path \<gamma>\<close>, of "d/2"] d by auto
1.3482 +  define nn where "nn = 1/(2* pi*\<i>) * contour_integral h (\<lambda>w. 1/(w - z))"
1.3483 +  have "\<exists>n. \<forall>e > 0. \<exists>p. valid_path p \<and> z \<notin> path_image p \<and>
1.3484 +                        pathstart p = pathstart \<gamma> \<and>  pathfinish p = pathfinish \<gamma> \<and>
1.3485 +                        (\<forall>t \<in> {0..1}. norm(\<gamma> t - p t) < e) \<and>
1.3486 +                        contour_integral p (\<lambda>w. 1/(w - z)) = 2 * pi * \<i> * n"
1.3487 +                    (is "\<exists>n. \<forall>e > 0. ?PP e n")
1.3488 +    proof (rule_tac x=nn in exI, clarify)
1.3489 +      fix e::real
1.3490 +      assume e: "e>0"
1.3491 +      obtain p where p: "polynomial_function p \<and>
1.3492 +            pathstart p = pathstart \<gamma> \<and> pathfinish p = pathfinish \<gamma> \<and> (\<forall>t\<in>{0..1}. cmod (p t - \<gamma> t) < min e (d / 2))"
1.3493 +        using path_approx_polynomial_function [OF \<open>path \<gamma>\<close>, of "min e (d/2)"] d \<open>0<e\<close> by auto
1.3494 +      have "(\<lambda>w. 1 / (w - z)) holomorphic_on UNIV - {z}"
1.3495 +        by (auto simp: intro!: holomorphic_intros)
1.3496 +      then show "?PP e nn"
1.3497 +        apply (rule_tac x=p in exI)
1.3498 +        using pi_eq [of h p] h p d
1.3499 +        apply (auto simp: valid_path_polynomial_function norm_minus_commute nn_def)
1.3500 +        done
1.3501 +    qed
1.3502 +  then show ?thesis
1.3503 +    unfolding winding_number_def
1.3504 +    apply (rule someI2_ex)
1.3505 +    apply (blast intro: \<open>0<e\<close>)
1.3506 +    done
1.3507 +qed
1.3508 +
1.3509 +lemma winding_number_unique:
1.3510 +  assumes \<gamma>: "path \<gamma>" "z \<notin> path_image \<gamma>"
1.3511 +      and pi:
1.3512 +        "\<And>e. e>0 \<Longrightarrow> \<exists>p. valid_path p \<and> z \<notin> path_image p \<and>
1.3513 +                          pathstart p = pathstart \<gamma> \<and> pathfinish p = pathfinish \<gamma> \<and>
1.3514 +                          (\<forall>t \<in> {0..1}. norm (\<gamma> t - p t) < e) \<and>
1.3515 +                          contour_integral p (\<lambda>w. 1/(w - z)) = 2 * pi * \<i> * n"
1.3516 +   shows "winding_number \<gamma> z = n"
1.3517 +proof -
1.3518 +  have "path_image \<gamma> \<subseteq> UNIV - {z}"
1.3519 +    using assms by blast
1.3520 +  then obtain e
1.3521 +    where e: "e>0"
1.3522 +      and pi_eq: "\<And>h1 h2 f. \<lbrakk>valid_path h1; valid_path h2;
1.3523 +                    (\<forall>t\<in>{0..1}. cmod (h1 t - \<gamma> t) < e \<and> cmod (h2 t - \<gamma> t) < e);
1.3524 +                    pathstart h2 = pathstart h1; pathfinish h2 = pathfinish h1; f holomorphic_on UNIV - {z}\<rbrakk> \<Longrightarrow>
1.3525 +                    contour_integral h2 f = contour_integral h1 f"
1.3526 +    using contour_integral_nearby_ends [of "UNIV - {z}" \<gamma>] assms  by (auto simp: open_delete)
1.3527 +  obtain p where p:
1.3528 +     "valid_path p \<and> z \<notin> path_image p \<and>
1.3529 +      pathstart p = pathstart \<gamma> \<and> pathfinish p = pathfinish \<gamma> \<and>
1.3530 +      (\<forall>t \<in> {0..1}. norm (\<gamma> t - p t) < e) \<and>
1.3531 +      contour_integral p (\<lambda>w. 1/(w - z)) = 2 * pi * \<i> * n"
1.3532 +    using pi [OF e] by blast
1.3533 +  obtain q where q:
1.3534 +     "valid_path q \<and> z \<notin> path_image q \<and>
1.3535 +      pathstart q = pathstart \<gamma> \<and> pathfinish q = pathfinish \<gamma> \<and>
1.3536 +      (\<forall>t\<in>{0..1}. cmod (\<gamma> t - q t) < e) \<and> contour_integral q (\<lambda>w. 1 / (w - z)) = complex_of_real (2 * pi) * \<i> * winding_number \<gamma> z"
1.3537 +    using winding_number [OF \<gamma> e] by blast
1.3538 +  have "2 * complex_of_real pi * \<i> * n = contour_integral p (\<lambda>w. 1 / (w - z))"
1.3539 +    using p by auto
1.3540 +  also have "... = contour_integral q (\<lambda>w. 1 / (w - z))"
1.3541 +    apply (rule pi_eq)
1.3542 +    using p q
1.3543 +    by (auto simp: valid_path_polynomial_function norm_minus_commute intro!: holomorphic_intros)
1.3544 +  also have "... = 2 * complex_of_real pi * \<i> * winding_number \<gamma> z"
1.3545 +    using q by auto
1.3546 +  finally have "2 * complex_of_real pi * \<i> * n = 2 * complex_of_real pi * \<i> * winding_number \<gamma> z" .
1.3547 +  then show ?thesis
1.3548 +    by simp
1.3549 +qed
1.3550 +
1.3551 +lemma winding_number_unique_loop:
1.3552 +  assumes \<gamma>: "path \<gamma>" "z \<notin> path_image \<gamma>"
1.3553 +      and loop: "pathfinish \<gamma> = pathstart \<gamma>"
1.3554 +      and pi:
1.3555 +        "\<And>e. e>0 \<Longrightarrow> \<exists>p. valid_path p \<and> z \<notin> path_image p \<and>
1.3556 +                           pathfinish p = pathstart p \<and>
1.3557 +                           (\<forall>t \<in> {0..1}. norm (\<gamma> t - p t) < e) \<and>
1.3558 +                           contour_integral p (\<lambda>w. 1/(w - z)) = 2 * pi * \<i> * n"
1.3559 +   shows "winding_number \<gamma> z = n"
1.3560 +proof -
1.3561 +  have "path_image \<gamma> \<subseteq> UNIV - {z}"
1.3562 +    using assms by blast
1.3563 +  then obtain e
1.3564 +    where e: "e>0"
1.3565 +      and pi_eq: "\<And>h1 h2 f. \<lbrakk>valid_path h1; valid_path h2;
1.3566 +                    (\<forall>t\<in>{0..1}. cmod (h1 t - \<gamma> t) < e \<and> cmod (h2 t - \<gamma> t) < e);
1.3567 +                    pathfinish h1 = pathstart h1; pathfinish h2 = pathstart h2; f holomorphic_on UNIV - {z}\<rbrakk> \<Longrightarrow>
1.3568 +                    contour_integral h2 f = contour_integral h1 f"
1.3569 +    using contour_integral_nearby_loops [of "UNIV - {z}" \<gamma>] assms  by (auto simp: open_delete)
1.3570 +  obtain p where p:
1.3571 +     "valid_path p \<and> z \<notin> path_image p \<and>
1.3572 +      pathfinish p = pathstart p \<and>
1.3573 +      (\<forall>t \<in> {0..1}. norm (\<gamma> t - p t) < e) \<and>
1.3574 +      contour_integral p (\<lambda>w. 1/(w - z)) = 2 * pi * \<i> * n"
1.3575 +    using pi [OF e] by blast
1.3576 +  obtain q where q:
1.3577 +     "valid_path q \<and> z \<notin> path_image q \<and>
1.3578 +      pathstart q = pathstart \<gamma> \<and> pathfinish q = pathfinish \<gamma> \<and>
1.3579 +      (\<forall>t\<in>{0..1}. cmod (\<gamma> t - q t) < e) \<and> contour_integral q (\<lambda>w. 1 / (w - z)) = complex_of_real (2 * pi) * \<i> * winding_number \<gamma> z"
1.3580 +    using winding_number [OF \<gamma> e] by blast
1.3581 +  have "2 * complex_of_real pi * \<i> * n = contour_integral p (\<lambda>w. 1 / (w - z))"
1.3582 +    using p by auto
1.3583 +  also have "... = contour_integral q (\<lambda>w. 1 / (w - z))"
1.3584 +    apply (rule pi_eq)
1.3585 +    using p q loop
1.3586 +    by (auto simp: valid_path_polynomial_function norm_minus_commute intro!: holomorphic_intros)
1.3587 +  also have "... = 2 * complex_of_real pi * \<i> * winding_number \<gamma> z"
1.3588 +    using q by auto
1.3589 +  finally have "2 * complex_of_real pi * \<i> * n = 2 * complex_of_real pi * \<i> * winding_number \<gamma> z" .
1.3590 +  then show ?thesis
1.3591 +    by simp
1.3592 +qed
1.3593 +
1.3594 +lemma winding_number_valid_path:
1.3595 +  assumes "valid_path \<gamma>" "z \<notin> path_image \<gamma>"
1.3596 +    shows "winding_number \<gamma> z = 1/(2*pi*\<i>) * contour_integral \<gamma> (\<lambda>w. 1/(w - z))"
1.3597 +  using assms by (auto simp: valid_path_imp_path intro!: winding_number_unique)
1.3598 +
1.3599 +lemma has_contour_integral_winding_number:
1.3600 +  assumes \<gamma>: "valid_path \<gamma>" "z \<notin> path_image \<gamma>"
1.3601 +    shows "((\<lambda>w. 1/(w - z)) has_contour_integral (2*pi*\<i>*winding_number \<gamma> z)) \<gamma>"
1.3602 +by (simp add: winding_number_valid_path has_contour_integral_integral contour_integrable_inversediff assms)
1.3603 +
1.3604 +lemma winding_number_trivial [simp]: "z \<noteq> a \<Longrightarrow> winding_number(linepath a a) z = 0"
1.3605 +  by (simp add: winding_number_valid_path)
1.3606 +
1.3607 +lemma winding_number_subpath_trivial [simp]: "z \<noteq> g x \<Longrightarrow> winding_number (subpath x x g) z = 0"
1.3608 +  by (simp add: path_image_subpath winding_number_valid_path)
1.3609 +
1.3610 +lemma winding_number_join:
1.3611 +  assumes g1: "path g1" "z \<notin> path_image g1"
1.3612 +      and g2: "path g2" "z \<notin> path_image g2"
1.3613 +      and "pathfinish g1 = pathstart g2"
1.3614 +    shows "winding_number(g1 +++ g2) z = winding_number g1 z + winding_number g2 z"
1.3615 +  apply (rule winding_number_unique)
1.3616 +  using assms apply (simp_all add: not_in_path_image_join)
1.3617 +  apply (frule winding_number [OF g2])
1.3618 +  apply (frule winding_number [OF g1], clarify)
1.3619 +  apply (rename_tac p2 p1)
1.3620 +  apply (rule_tac x="p1+++p2" in exI)
1.3621 +  apply (simp add: not_in_path_image_join contour_integrable_inversediff algebra_simps)
1.3622 +  apply (auto simp: joinpaths_def)
1.3623 +  done
1.3624 +
1.3625 +lemma winding_number_reversepath:
1.3626 +  assumes "path \<gamma>" "z \<notin> path_image \<gamma>"
1.3627 +    shows "winding_number(reversepath \<gamma>) z = - (winding_number \<gamma> z)"
1.3628 +  apply (rule winding_number_unique)
1.3629 +  using assms
1.3630 +  apply simp_all
1.3631 +  apply (frule winding_number [OF assms], clarify)
1.3632 +  apply (rule_tac x="reversepath p" in exI)
1.3633 +  apply (simp add: contour_integral_reversepath contour_integrable_inversediff valid_path_imp_reverse)
1.3634 +  apply (auto simp: reversepath_def)
1.3635 +  done
1.3636 +
1.3637 +lemma winding_number_shiftpath:
1.3638 +  assumes \<gamma>: "path \<gamma>" "z \<notin> path_image \<gamma>"
1.3639 +      and "pathfinish \<gamma> = pathstart \<gamma>" "a \<in> {0..1}"
1.3640 +    shows "winding_number(shiftpath a \<gamma>) z = winding_number \<gamma> z"
1.3641 +  apply (rule winding_number_unique_loop)
1.3642 +  using assms
1.3643 +  apply (simp_all add: path_shiftpath path_image_shiftpath pathfinish_shiftpath pathstart_shiftpath)
1.3644 +  apply (frule winding_number [OF \<gamma>], clarify)
1.3645 +  apply (rule_tac x="shiftpath a p" in exI)
1.3646 +  apply (simp add: contour_integral_shiftpath path_image_shiftpath pathfinish_shiftpath pathstart_shiftpath valid_path_shiftpath)
1.3647 +  apply (auto simp: shiftpath_def)
1.3648 +  done
1.3649 +
1.3650 +lemma winding_number_split_linepath:
1.3651 +  assumes "c \<in> closed_segment a b" "z \<notin> closed_segment a b"
1.3652 +    shows "winding_number(linepath a b) z = winding_number(linepath a c) z + winding_number(linepath c b) z"
1.3653 +proof -
1.3654 +  have "z \<notin> closed_segment a c" "z \<notin> closed_segment c b"
1.3655 +    using assms  apply (meson convex_contains_segment convex_segment ends_in_segment(1) subsetCE)
1.3656 +    using assms  by (meson convex_contains_segment convex_segment ends_in_segment(2) subsetCE)
1.3657 +  then show ?thesis
1.3658 +    using assms
1.3659 +    by (simp add: winding_number_valid_path contour_integral_split_linepath [symmetric] continuous_on_inversediff field_simps)
1.3660 +qed
1.3661 +
1.3662 +lemma winding_number_cong:
1.3663 +   "(\<And>t. \<lbrakk>0 \<le> t; t \<le> 1\<rbrakk> \<Longrightarrow> p t = q t) \<Longrightarrow> winding_number p z = winding_number q z"
1.3664 +  by (simp add: winding_number_def pathstart_def pathfinish_def)
1.3665 +
1.3666 +lemma winding_number_offset: "winding_number p z = winding_number (\<lambda>w. p w - z) 0"
1.3667 +  apply (simp add: winding_number_def contour_integral_integral path_image_def valid_path_def pathstart_def pathfinish_def)
1.3668 +  apply (intro ext arg_cong [where f = Eps] arg_cong [where f = All] imp_cong refl, safe)
1.3669 +  apply (rename_tac g)
1.3670 +  apply (rule_tac x="\<lambda>t. g t - z" in exI)
1.3671 +  apply (force simp: vector_derivative_def has_vector_derivative_diff_const piecewise_C1_differentiable_diff C1_differentiable_imp_piecewise)
1.3672 +  apply (rename_tac g)
1.3673 +  apply (rule_tac x="\<lambda>t. g t + z" in exI)
1.3675 +  apply (force simp: algebra_simps)
1.3676 +  done
1.3677 +
1.3678 +(* A combined theorem deducing several things piecewise.*)
1.3679 +lemma winding_number_join_pos_combined:
1.3680 +     "\<lbrakk>valid_path \<gamma>1; z \<notin> path_image \<gamma>1; 0 < Re(winding_number \<gamma>1 z);
1.3681 +       valid_path \<gamma>2; z \<notin> path_image \<gamma>2; 0 < Re(winding_number \<gamma>2 z); pathfinish \<gamma>1 = pathstart \<gamma>2\<rbrakk>
1.3682 +      \<Longrightarrow> valid_path(\<gamma>1 +++ \<gamma>2) \<and> z \<notin> path_image(\<gamma>1 +++ \<gamma>2) \<and> 0 < Re(winding_number(\<gamma>1 +++ \<gamma>2) z)"
1.3683 +  by (simp add: valid_path_join path_image_join winding_number_join valid_path_imp_path)
1.3684 +
1.3685 +
1.3686 +(* Useful sufficient conditions for the winding number to be positive etc.*)
1.3687 +
1.3688 +lemma Re_winding_number:
1.3689 +    "\<lbrakk>valid_path \<gamma>; z \<notin> path_image \<gamma>\<rbrakk>
1.3690 +     \<Longrightarrow> Re(winding_number \<gamma> z) = Im(contour_integral \<gamma> (\<lambda>w. 1/(w - z))) / (2*pi)"
1.3691 +by (simp add: winding_number_valid_path field_simps Re_divide power2_eq_square)
1.3692 +
1.3693 +lemma winding_number_pos_le:
1.3694 +  assumes \<gamma>: "valid_path \<gamma>" "z \<notin> path_image \<gamma>"
1.3695 +      and ge: "\<And>x. \<lbrakk>0 < x; x < 1\<rbrakk> \<Longrightarrow> 0 \<le> Im (vector_derivative \<gamma> (at x) * cnj(\<gamma> x - z))"
1.3696 +    shows "0 \<le> Re(winding_number \<gamma> z)"
1.3697 +proof -
1.3698 +  have *: "0 \<le> Im (vector_derivative \<gamma> (at x) / (\<gamma> x - z))" if x: "0 < x" "x < 1" for x
1.3699 +    using ge by (simp add: Complex.Im_divide algebra_simps x)
1.3700 +  show ?thesis
1.3701 +    apply (simp add: Re_winding_number [OF \<gamma>] field_simps)
1.3702 +    apply (rule has_integral_component_nonneg
1.3703 +             [of \<i> "\<lambda>x. if x \<in> {0<..<1}
1.3704 +                         then 1/(\<gamma> x - z) * vector_derivative \<gamma> (at x) else 0", simplified])
1.3705 +      prefer 3 apply (force simp: *)
1.3706 +     apply (simp add: Basis_complex_def)
1.3707 +    apply (rule has_integral_spike_interior [of 0 1 _ "\<lambda>x. 1/(\<gamma> x - z) * vector_derivative \<gamma> (at x)"])
1.3708 +    apply simp
1.3709 +    apply (simp only: box_real)
1.3710 +    apply (subst has_contour_integral [symmetric])
1.3711 +    using \<gamma>
1.3712 +    apply (simp add: contour_integrable_inversediff has_contour_integral_integral)
1.3713 +    done
1.3714 +qed
1.3715 +
1.3716 +lemma winding_number_pos_lt_lemma:
1.3717 +  assumes \<gamma>: "valid_path \<gamma>" "z \<notin> path_image \<gamma>"
1.3718 +      and e: "0 < e"
1.3719 +      and ge: "\<And>x. \<lbrakk>0 < x; x < 1\<rbrakk> \<Longrightarrow> e \<le> Im (vector_derivative \<gamma> (at x) / (\<gamma> x - z))"
1.3720 +    shows "0 < Re(winding_number \<gamma> z)"
1.3721 +proof -
1.3722 +  have "e \<le> Im (contour_integral \<gamma> (\<lambda>w. 1 / (w - z)))"
1.3723 +    apply (rule has_integral_component_le
1.3724 +             [of \<i> "\<lambda>x. \<i>*e" "\<i>*e" "{0..1}"
1.3725 +                    "\<lambda>x. if x \<in> {0<..<1} then 1/(\<gamma> x - z) * vector_derivative \<gamma> (at x) else \<i>*e"
1.3726 +                    "contour_integral \<gamma> (\<lambda>w. 1/(w - z))", simplified])
1.3727 +    using e
1.3728 +    apply (simp_all add: Basis_complex_def)
1.3729 +    using has_integral_const_real [of _ 0 1] apply force
1.3730 +    apply (rule has_integral_spike_interior [of 0 1 _ "\<lambda>x. 1/(\<gamma> x - z) * vector_derivative \<gamma> (at x)", simplified box_real])
1.3731 +    apply simp
1.3732 +    apply (subst has_contour_integral [symmetric])
1.3733 +    using \<gamma>
1.3734 +    apply (simp_all add: contour_integrable_inversediff has_contour_integral_integral ge)
1.3735 +    done
1.3736 +  with e show ?thesis
1.3737 +    by (simp add: Re_winding_number [OF \<gamma>] field_simps)
1.3738 +qed
1.3739 +
1.3740 +lemma winding_number_pos_lt:
1.3741 +  assumes \<gamma>: "valid_path \<gamma>" "z \<notin> path_image \<gamma>"
1.3742 +      and e: "0 < e"
1.3743 +      and ge: "\<And>x. \<lbrakk>0 < x; x < 1\<rbrakk> \<Longrightarrow> e \<le> Im (vector_derivative \<gamma> (at x) * cnj(\<gamma> x - z))"
1.3744 +    shows "0 < Re (winding_number \<gamma> z)"
1.3745 +proof -
1.3746 +  have bm: "bounded ((\<lambda>w. w - z) ` (path_image \<gamma>))"
1.3747 +    using bounded_translation [of _ "-z"] \<gamma> by (simp add: bounded_valid_path_image)
1.3748 +  then obtain B where B: "B > 0" and Bno: "\<And>x. x \<in> (\<lambda>w. w - z) ` (path_image \<gamma>) \<Longrightarrow> norm x \<le> B"
1.3749 +    using bounded_pos [THEN iffD1, OF bm] by blast
1.3750 +  { fix x::real  assume x: "0 < x" "x < 1"
1.3751 +    then have B2: "cmod (\<gamma> x - z)^2 \<le> B^2" using Bno [of "\<gamma> x - z"]
1.3752 +      by (simp add: path_image_def power2_eq_square mult_mono')
1.3753 +    with x have "\<gamma> x \<noteq> z" using \<gamma>
1.3754 +      using path_image_def by fastforce
1.3755 +    then have "e / B\<^sup>2 \<le> Im (vector_derivative \<gamma> (at x) * cnj (\<gamma> x - z)) / (cmod (\<gamma> x - z))\<^sup>2"
1.3756 +      using B ge [OF x] B2 e
1.3757 +      apply (rule_tac y="e / (cmod (\<gamma> x - z))\<^sup>2" in order_trans)
1.3758 +      apply (auto simp: divide_left_mono divide_right_mono)
1.3759 +      done
1.3760 +    then have "e / B\<^sup>2 \<le> Im (vector_derivative \<gamma> (at x) / (\<gamma> x - z))"
1.3761 +      by (simp add: Im_divide_Reals complex_div_cnj [of _ "\<gamma> x - z" for x] del: complex_cnj_diff times_complex.sel)
1.3762 +  } note * = this
1.3763 +  show ?thesis
1.3764 +    using e B by (simp add: * winding_number_pos_lt_lemma [OF \<gamma>, of "e/B^2"])
1.3765 +qed
1.3766 +
1.3767 +subsection\<open>The winding number is an integer\<close>
1.3768 +
1.3769 +text\<open>Proof from the book Complex Analysis by Lars V. Ahlfors, Chapter 4, section 2.1,
1.3770 +     Also on page 134 of Serge Lang's book with the name title, etc.\<close>
1.3771 +
1.3772 +lemma exp_fg:
1.3773 +  fixes z::complex
1.3774 +  assumes g: "(g has_vector_derivative g') (at x within s)"
1.3775 +      and f: "(f has_vector_derivative (g' / (g x - z))) (at x within s)"
1.3776 +      and z: "g x \<noteq> z"
1.3777 +    shows "((\<lambda>x. exp(-f x) * (g x - z)) has_vector_derivative 0) (at x within s)"
1.3778 +proof -
1.3779 +  have *: "(exp o (\<lambda>x. (- f x)) has_vector_derivative - (g' / (g x - z)) * exp (- f x)) (at x within s)"
1.3780 +    using assms unfolding has_vector_derivative_def scaleR_conv_of_real
1.3781 +    by (auto intro!: derivative_eq_intros)
1.3782 +  show ?thesis
1.3783 +    apply (rule has_vector_derivative_eq_rhs)
1.3784 +    apply (rule bounded_bilinear.has_vector_derivative [OF bounded_bilinear_mult])
1.3785 +    using z
1.3786 +    apply (auto simp: intro!: derivative_eq_intros * [unfolded o_def] g)
1.3787 +    done
1.3788 +qed
1.3789 +
1.3790 +lemma winding_number_exp_integral:
1.3791 +  fixes z::complex
1.3792 +  assumes \<gamma>: "\<gamma> piecewise_C1_differentiable_on {a..b}"
1.3793 +      and ab: "a \<le> b"
1.3794 +      and z: "z \<notin> \<gamma> ` {a..b}"
1.3795 +    shows "(\<lambda>x. vector_derivative \<gamma> (at x) / (\<gamma> x - z)) integrable_on {a..b}"
1.3796 +          (is "?thesis1")
1.3797 +          "exp (- (integral {a..b} (\<lambda>x. vector_derivative \<gamma> (at x) / (\<gamma> x - z)))) * (\<gamma> b - z) = \<gamma> a - z"
1.3798 +          (is "?thesis2")
1.3799 +proof -
1.3800 +  let ?D\<gamma> = "\<lambda>x. vector_derivative \<gamma> (at x)"
1.3801 +  have [simp]: "\<And>x. a \<le> x \<Longrightarrow> x \<le> b \<Longrightarrow> \<gamma> x \<noteq> z"
1.3802 +    using z by force
1.3803 +  have cong: "continuous_on {a..b} \<gamma>"
1.3804 +    using \<gamma> by (simp add: piecewise_C1_differentiable_on_def)
1.3805 +  obtain k where fink: "finite k" and g_C1_diff: "\<gamma> C1_differentiable_on ({a..b} - k)"
1.3806 +    using \<gamma> by (force simp: piecewise_C1_differentiable_on_def)
1.3807 +  have o: "open ({a<..<b} - k)"
1.3808 +    using \<open>finite k\<close> by (simp add: finite_imp_closed open_Diff)
1.3809 +  moreover have "{a<..<b} - k \<subseteq> {a..b} - k"
1.3810 +    by force
1.3811 +  ultimately have g_diff_at: "\<And>x. \<lbrakk>x \<notin> k; x \<in> {a<..<b}\<rbrakk> \<Longrightarrow> \<gamma> differentiable at x"
1.3812 +    by (metis Diff_iff differentiable_on_subset C1_diff_imp_diff [OF g_C1_diff] differentiable_on_def differentiable_within_open)
1.3813 +  { fix w
1.3814 +    assume "w \<noteq> z"
1.3815 +    have "continuous_on (ball w (cmod (w - z))) (\<lambda>w. 1 / (w - z))"
1.3816 +      by (auto simp: dist_norm intro!: continuous_intros)
1.3817 +    moreover have "\<And>x. cmod (w - x) < cmod (w - z) \<Longrightarrow> \<exists>f'. ((\<lambda>w. 1 / (w - z)) has_field_derivative f') (at x)"
1.3818 +      by (auto simp: intro!: derivative_eq_intros)
1.3819 +    ultimately have "\<exists>h. \<forall>y. norm(y - w) < norm(w - z) \<longrightarrow> (h has_field_derivative 1/(y - z)) (at y)"
1.3820 +      using holomorphic_convex_primitive [of "ball w (norm(w - z))" "{}" "\<lambda>w. 1/(w - z)"]
1.3821 +      by (simp add: field_differentiable_def Ball_def dist_norm at_within_open_NO_MATCH norm_minus_commute)
1.3822 +  }
1.3823 +  then obtain h where h: "\<And>w y. w \<noteq> z \<Longrightarrow> norm(y - w) < norm(w - z) \<Longrightarrow> (h w has_field_derivative 1/(y - z)) (at y)"
1.3824 +    by meson
1.3825 +  have exy: "\<exists>y. ((\<lambda>x. inverse (\<gamma> x - z) * ?D\<gamma> x) has_integral y) {a..b}"
1.3826 +    unfolding integrable_on_def [symmetric]
1.3827 +    apply (rule contour_integral_local_primitive_any [OF piecewise_C1_imp_differentiable [OF \<gamma>], of "-{z}"])
1.3828 +    apply (rename_tac w)
1.3829 +    apply (rule_tac x="norm(w - z)" in exI)
1.3830 +    apply (simp_all add: inverse_eq_divide)
1.3831 +    apply (metis has_field_derivative_at_within h)
1.3832 +    done
1.3833 +  have vg_int: "(\<lambda>x. ?D\<gamma> x / (\<gamma> x - z)) integrable_on {a..b}"
1.3834 +    unfolding box_real [symmetric] divide_inverse_commute
1.3835 +    by (auto intro!: exy integrable_subinterval simp add: integrable_on_def ab)
1.3836 +  with ab show ?thesis1
1.3837 +    by (simp add: divide_inverse_commute integral_def integrable_on_def)
1.3838 +  { fix t
1.3839 +    assume t: "t \<in> {a..b}"
1.3840 +    have cball: "continuous_on (ball (\<gamma> t) (dist (\<gamma> t) z)) (\<lambda>x. inverse (x - z))"
1.3841 +        using z by (auto intro!: continuous_intros simp: dist_norm)
1.3842 +    have icd: "\<And>x. cmod (\<gamma> t - x) < cmod (\<gamma> t - z) \<Longrightarrow> (\<lambda>w. inverse (w - z)) field_differentiable at x"
1.3843 +      unfolding field_differentiable_def by (force simp: intro!: derivative_eq_intros)
1.3844 +    obtain h where h: "\<And>x. cmod (\<gamma> t - x) < cmod (\<gamma> t - z) \<Longrightarrow>
1.3845 +                       (h has_field_derivative inverse (x - z)) (at x within {y. cmod (\<gamma> t - y) < cmod (\<gamma> t - z)})"
1.3846 +      using holomorphic_convex_primitive [where f = "\<lambda>w. inverse(w - z)", OF convex_ball finite.emptyI cball icd]
1.3847 +      by simp (auto simp: ball_def dist_norm that)
1.3848 +    { fix x D
1.3849 +      assume x: "x \<notin> k" "a < x" "x < b"
1.3850 +      then have "x \<in> interior ({a..b} - k)"
1.3851 +        using open_subset_interior [OF o] by fastforce
1.3852 +      then have con: "isCont (\<lambda>x. ?D\<gamma> x) x"
1.3853 +        using g_C1_diff x by (auto simp: C1_differentiable_on_eq intro: continuous_on_interior)
1.3854 +      then have con_vd: "continuous (at x within {a..b}) (\<lambda>x. ?D\<gamma> x)"
1.3855 +        by (rule continuous_at_imp_continuous_within)
1.3856 +      have gdx: "\<gamma> differentiable at x"
1.3857 +        using x by (simp add: g_diff_at)
1.3858 +      have "((\<lambda>c. exp (- integral {a..c} (\<lambda>x. vector_derivative \<gamma> (at x) / (\<gamma> x - z))) * (\<gamma> c - z)) has_derivative (\<lambda>h. 0))
1.3859 +          (at x within {a..b})"
1.3860 +        using x gdx t
1.3861 +        apply (clarsimp simp add: differentiable_iff_scaleR)
1.3862 +        apply (rule exp_fg [unfolded has_vector_derivative_def, simplified], blast intro: has_derivative_at_within)
1.3863 +        apply (simp_all add: has_vector_derivative_def [symmetric])
1.3864 +        apply (rule has_vector_derivative_eq_rhs [OF integral_has_vector_derivative_continuous_at])
1.3865 +        apply (rule con_vd continuous_intros cong vg_int | simp add: continuous_at_imp_continuous_within has_vector_derivative_continuous vector_derivative_at)+
1.3866 +        done
1.3867 +      } note * = this
1.3868 +    have "exp (- (integral {a..t} (\<lambda>x. ?D\<gamma> x / (\<gamma> x - z)))) * (\<gamma> t - z) =\<gamma> a - z"
1.3869 +      apply (rule has_derivative_zero_unique_strong_interval [of "{a,b} \<union> k" a b])
1.3870 +      using t
1.3871 +      apply (auto intro!: * continuous_intros fink cong indefinite_integral_continuous [OF vg_int]  simp add: ab)+
1.3872 +      done
1.3873 +   }
1.3874 +  with ab show ?thesis2
1.3875 +    by (simp add: divide_inverse_commute integral_def)
1.3876 +qed
1.3877 +
1.3878 +corollary winding_number_exp_2pi:
1.3879 +    "\<lbrakk>path p; z \<notin> path_image p\<rbrakk>
1.3880 +     \<Longrightarrow> pathfinish p - z = exp (2 * pi * \<i> * winding_number p z) * (pathstart p - z)"
1.3881 +using winding_number [of p z 1] unfolding valid_path_def path_image_def pathstart_def pathfinish_def
1.3882 +  by (force dest: winding_number_exp_integral(2) [of _ 0 1 z] simp: field_simps contour_integral_integral exp_minus)
1.3883 +
1.3884 +
1.3885 +subsection\<open>The version with complex integers and equality\<close>
1.3886 +
1.3887 +lemma integer_winding_number_eq:
1.3888 +  assumes \<gamma>: "path \<gamma>" and z: "z \<notin> path_image \<gamma>"
1.3889 +  shows "winding_number \<gamma> z \<in> \<int> \<longleftrightarrow> pathfinish \<gamma> = pathstart \<gamma>"
1.3890 +proof -
1.3891 +  have *: "\<And>i::complex. \<And>g0 g1. \<lbrakk>i \<noteq> 0; g0 \<noteq> z; (g1 - z) / i = g0 - z\<rbrakk> \<Longrightarrow> (i = 1 \<longleftrightarrow> g1 = g0)"
1.3892 +      by (simp add: field_simps) algebra
1.3893 +  obtain p where p: "valid_path p" "z \<notin> path_image p"
1.3894 +                    "pathstart p = pathstart \<gamma>" "pathfinish p = pathfinish \<gamma>"
1.3895 +                    "contour_integral p (\<lambda>w. 1 / (w - z)) = complex_of_real (2 * pi) * \<i> * winding_number \<gamma> z"
1.3896 +    using winding_number [OF assms, of 1] by auto
1.3897 +  have [simp]: "(winding_number \<gamma> z \<in> \<int>) = (exp (contour_integral p (\<lambda>w. 1 / (w - z))) = 1)"
1.3898 +      using p by (simp add: exp_eq_1 complex_is_Int_iff)
1.3899 +  have "winding_number p z \<in> \<int> \<longleftrightarrow> pathfinish p = pathstart p"
1.3900 +    using p z
1.3901 +    apply (simp add: winding_number_valid_path valid_path_def path_image_def pathstart_def pathfinish_def)
1.3902 +    using winding_number_exp_integral(2) [of p 0 1 z]
1.3903 +    apply (simp add: field_simps contour_integral_integral exp_minus)
1.3904 +    apply (rule *)
1.3905 +    apply (auto simp: path_image_def field_simps)
1.3906 +    done
1.3907 +  then show ?thesis using p
1.3908 +    by (auto simp: winding_number_valid_path)
1.3909 +qed
1.3910 +
1.3911 +theorem integer_winding_number:
1.3912 +  "\<lbrakk>path \<gamma>; pathfinish \<gamma> = pathstart \<gamma>; z \<notin> path_image \<gamma>\<rbrakk> \<Longrightarrow> winding_number \<gamma> z \<in> \<int>"
1.3913 +by (metis integer_winding_number_eq)
1.3914 +
1.3915 +
1.3916 +text\<open>If the winding number's magnitude is at least one, then the path must contain points in every direction.*)
1.3917 +   We can thus bound the winding number of a path that doesn't intersect a given ray. \<close>
1.3918 +
1.3919 +lemma winding_number_pos_meets:
1.3920 +  fixes z::complex
1.3921 +  assumes \<gamma>: "valid_path \<gamma>" and z: "z \<notin> path_image \<gamma>" and 1: "Re (winding_number \<gamma> z) \<ge> 1"
1.3922 +      and w: "w \<noteq> z"
1.3923 +  shows "\<exists>a::real. 0 < a \<and> z + a*(w - z) \<in> path_image \<gamma>"
1.3924 +proof -
1.3925 +  have [simp]: "\<And>x. 0 \<le> x \<Longrightarrow> x \<le> 1 \<Longrightarrow> \<gamma> x \<noteq> z"
1.3926 +    using z by (auto simp: path_image_def)
1.3927 +  have [simp]: "z \<notin> \<gamma> ` {0..1}"
1.3928 +    using path_image_def z by auto
1.3929 +  have gpd: "\<gamma> piecewise_C1_differentiable_on {0..1}"
1.3930 +    using \<gamma> valid_path_def by blast
1.3931 +  define r where "r = (w - z) / (\<gamma> 0 - z)"
1.3932 +  have [simp]: "r \<noteq> 0"
1.3933 +    using w z by (auto simp: r_def)
1.3934 +  have "Arg r \<le> 2*pi"
1.3935 +    by (simp add: Arg less_eq_real_def)
1.3936 +  also have "... \<le> Im (integral {0..1} (\<lambda>x. vector_derivative \<gamma> (at x) / (\<gamma> x - z)))"
1.3937 +    using 1
1.3938 +    apply (simp add: winding_number_valid_path [OF \<gamma> z] contour_integral_integral)
1.3939 +    apply (simp add: Complex.Re_divide field_simps power2_eq_square)
1.3940 +    done
1.3941 +  finally have "Arg r \<le> Im (integral {0..1} (\<lambda>x. vector_derivative \<gamma> (at x) / (\<gamma> x - z)))" .
1.3942 +  then have "\<exists>t. t \<in> {0..1} \<and> Im(integral {0..t} (\<lambda>x. vector_derivative \<gamma> (at x)/(\<gamma> x - z))) = Arg r"
1.3944 +    apply (rule IVT')
1.3945 +    apply (simp_all add: Arg_ge_0)
1.3946 +    apply (intro continuous_intros indefinite_integral_continuous winding_number_exp_integral [OF gpd]; simp)
1.3947 +    done
1.3948 +  then obtain t where t:     "t \<in> {0..1}"
1.3949 +                  and eqArg: "Im (integral {0..t} (\<lambda>x. vector_derivative \<gamma> (at x)/(\<gamma> x - z))) = Arg r"
1.3950 +    by blast
1.3951 +  define i where "i = integral {0..t} (\<lambda>x. vector_derivative \<gamma> (at x) / (\<gamma> x - z))"
1.3952 +  have iArg: "Arg r = Im i"
1.3953 +    using eqArg by (simp add: i_def)
1.3954 +  have gpdt: "\<gamma> piecewise_C1_differentiable_on {0..t}"
1.3955 +    by (metis atLeastAtMost_iff atLeastatMost_subset_iff order_refl piecewise_C1_differentiable_on_subset gpd t)
1.3956 +  have "exp (- i) * (\<gamma> t - z) = \<gamma> 0 - z"
1.3957 +    unfolding i_def
1.3958 +    apply (rule winding_number_exp_integral [OF gpdt])
1.3959 +    using t z unfolding path_image_def
1.3960 +    apply force+
1.3961 +    done
1.3962 +  then have *: "\<gamma> t - z = exp i * (\<gamma> 0 - z)"
1.3963 +    by (simp add: exp_minus field_simps)
1.3964 +  then have "(w - z) = r * (\<gamma> 0 - z)"
1.3965 +    by (simp add: r_def)
1.3966 +  then have "z + complex_of_real (exp (Re i)) * (w - z) / complex_of_real (cmod r) = \<gamma> t"
1.3968 +    apply (subst Complex_Transcendental.Arg_eq [of r])
1.3969 +    apply (simp add: iArg)
1.3970 +    using *
1.3971 +    apply (simp add: exp_eq_polar field_simps)
1.3972 +    done
1.3973 +  with t show ?thesis
1.3974 +    by (rule_tac x="exp(Re i) / norm r" in exI) (auto simp: path_image_def)
1.3975 +qed
1.3976 +
1.3977 +lemma winding_number_big_meets:
1.3978 +  fixes z::complex
1.3979 +  assumes \<gamma>: "valid_path \<gamma>" and z: "z \<notin> path_image \<gamma>" and "\<bar>Re (winding_number \<gamma> z)\<bar> \<ge> 1"
1.3980 +      and w: "w \<noteq> z"
1.3981 +  shows "\<exists>a::real. 0 < a \<and> z + a*(w - z) \<in> path_image \<gamma>"
1.3982 +proof -
1.3983 +  { assume "Re (winding_number \<gamma> z) \<le> - 1"
1.3984 +    then have "Re (winding_number (reversepath \<gamma>) z) \<ge> 1"
1.3985 +      by (simp add: \<gamma> valid_path_imp_path winding_number_reversepath z)
1.3986 +    moreover have "valid_path (reversepath \<gamma>)"
1.3987 +      using \<gamma> valid_path_imp_reverse by auto
1.3988 +    moreover have "z \<notin> path_image (reversepath \<gamma>)"
1.3989 +      by (simp add: z)
1.3990 +    ultimately have "\<exists>a::real. 0 < a \<and> z + a*(w - z) \<in> path_image (reversepath \<gamma>)"
1.3991 +      using winding_number_pos_meets w by blast
1.3992 +    then have ?thesis
1.3993 +      by simp
1.3994 +  }
1.3995 +  then show ?thesis
1.3996 +    using assms
1.3997 +    by (simp add: abs_if winding_number_pos_meets split: if_split_asm)
1.3998 +qed
1.3999 +
1.4000 +lemma winding_number_less_1:
1.4001 +  fixes z::complex
1.4002 +  shows
1.4003 +  "\<lbrakk>valid_path \<gamma>; z \<notin> path_image \<gamma>; w \<noteq> z;
1.4004 +    \<And>a::real. 0 < a \<Longrightarrow> z + a*(w - z) \<notin> path_image \<gamma>\<rbrakk>
1.4005 +   \<Longrightarrow> \<bar>Re(winding_number \<gamma> z)\<bar> < 1"
1.4006 +   by (auto simp: not_less dest: winding_number_big_meets)
1.4007 +
1.4008 +text\<open>One way of proving that WN=1 for a loop.\<close>
1.4009 +lemma winding_number_eq_1:
1.4010 +  fixes z::complex
1.4011 +  assumes \<gamma>: "valid_path \<gamma>" and z: "z \<notin> path_image \<gamma>" and loop: "pathfinish \<gamma> = pathstart \<gamma>"
1.4012 +      and 0: "0 < Re(winding_number \<gamma> z)" and 2: "Re(winding_number \<gamma> z) < 2"
1.4013 +  shows "winding_number \<gamma> z = 1"
1.4014 +proof -
1.4015 +  have "winding_number \<gamma> z \<in> Ints"
1.4016 +    by (simp add: \<gamma> integer_winding_number loop valid_path_imp_path z)
1.4017 +  then show ?thesis
1.4018 +    using 0 2 by (auto simp: Ints_def)
1.4019 +qed
1.4020 +
1.4021 +
1.4022 +subsection\<open>Continuity of winding number and invariance on connected sets.\<close>
1.4023 +
1.4024 +lemma continuous_at_winding_number:
1.4025 +  fixes z::complex
1.4026 +  assumes \<gamma>: "path \<gamma>" and z: "z \<notin> path_image \<gamma>"
1.4027 +  shows "continuous (at z) (winding_number \<gamma>)"
1.4028 +proof -
1.4029 +  obtain e where "e>0" and cbg: "cball z e \<subseteq> - path_image \<gamma>"
1.4030 +    using open_contains_cball [of "- path_image \<gamma>"]  z
1.4031 +    by (force simp: closed_def [symmetric] closed_path_image [OF \<gamma>])
1.4032 +  then have ppag: "path_image \<gamma> \<subseteq> - cball z (e/2)"
1.4033 +    by (force simp: cball_def dist_norm)
1.4034 +  have oc: "open (- cball z (e / 2))"
1.4035 +    by (simp add: closed_def [symmetric])
1.4036 +  obtain d where "d>0" and pi_eq:
1.4037 +    "\<And>h1 h2. \<lbrakk>valid_path h1; valid_path h2;
1.4038 +              (\<forall>t\<in>{0..1}. cmod (h1 t - \<gamma> t) < d \<and> cmod (h2 t - \<gamma> t) < d);
1.4039 +              pathstart h2 = pathstart h1; pathfinish h2 = pathfinish h1\<rbrakk>
1.4040 +             \<Longrightarrow>
1.4041 +               path_image h1 \<subseteq> - cball z (e / 2) \<and>
1.4042 +               path_image h2 \<subseteq> - cball z (e / 2) \<and>
1.4043 +               (\<forall>f. f holomorphic_on - cball z (e / 2) \<longrightarrow> contour_integral h2 f = contour_integral h1 f)"
1.4044 +    using contour_integral_nearby_ends [OF oc \<gamma> ppag] by metis
1.4045 +  obtain p where p: "valid_path p" "z \<notin> path_image p"
1.4046 +                    "pathstart p = pathstart \<gamma> \<and> pathfinish p = pathfinish \<gamma>"
1.4047 +              and pg: "\<And>t. t\<in>{0..1} \<Longrightarrow> cmod (\<gamma> t - p t) < min d e / 2"
1.4048 +              and pi: "contour_integral p (\<lambda>x. 1 / (x - z)) = complex_of_real (2 * pi) * \<i> * winding_number \<gamma> z"
1.4049 +    using winding_number [OF \<gamma> z, of "min d e / 2"] \<open>d>0\<close> \<open>e>0\<close> by auto
1.4050 +  { fix w
1.4051 +    assume d2: "cmod (w - z) < d/2" and e2: "cmod (w - z) < e/2"
1.4052 +    then have wnotp: "w \<notin> path_image p"
1.4053 +      using cbg \<open>d>0\<close> \<open>e>0\<close>
1.4054 +      apply (simp add: path_image_def cball_def dist_norm, clarify)
1.4055 +      apply (frule pg)
1.4056 +      apply (drule_tac c="\<gamma> x" in subsetD)
1.4057 +      apply (auto simp: less_eq_real_def norm_minus_commute norm_triangle_half_l)
1.4058 +      done
1.4059 +    have wnotg: "w \<notin> path_image \<gamma>"
1.4060 +      using cbg e2 \<open>e>0\<close> by (force simp: dist_norm norm_minus_commute)
1.4061 +    { fix k::real
1.4062 +      assume k: "k>0"
1.4063 +      then obtain q where q: "valid_path q" "w \<notin> path_image q"
1.4064 +                             "pathstart q = pathstart \<gamma> \<and> pathfinish q = pathfinish \<gamma>"
1.4065 +                    and qg: "\<And>t. t \<in> {0..1} \<Longrightarrow> cmod (\<gamma> t - q t) < min k (min d e) / 2"
1.4066 +                    and qi: "contour_integral q (\<lambda>u. 1 / (u - w)) = complex_of_real (2 * pi) * \<i> * winding_number \<gamma> w"
1.4067 +        using winding_number [OF \<gamma> wnotg, of "min k (min d e) / 2"] \<open>d>0\<close> \<open>e>0\<close> k
1.4068 +        by (force simp: min_divide_distrib_right)
1.4069 +      have "contour_integral p (\<lambda>u. 1 / (u - w)) = contour_integral q (\<lambda>u. 1 / (u - w))"
1.4070 +        apply (rule pi_eq [OF \<open>valid_path q\<close> \<open>valid_path p\<close>, THEN conjunct2, THEN conjunct2, rule_format])
1.4071 +        apply (frule pg)
1.4072 +        apply (frule qg)
1.4073 +        using p q \<open>d>0\<close> e2
1.4074 +        apply (auto simp: dist_norm norm_minus_commute intro!: holomorphic_intros)
1.4075 +        done
1.4076 +      then have "contour_integral p (\<lambda>x. 1 / (x - w)) = complex_of_real (2 * pi) * \<i> * winding_number \<gamma> w"
1.4077 +        by (simp add: pi qi)
1.4078 +    } note pip = this
1.4079 +    have "path p"
1.4080 +      using p by (simp add: valid_path_imp_path)
1.4081 +    then have "winding_number p w = winding_number \<gamma> w"
1.4082 +      apply (rule winding_number_unique [OF _ wnotp])
1.4083 +      apply (rule_tac x=p in exI)
1.4084 +      apply (simp add: p wnotp min_divide_distrib_right pip)
1.4085 +      done
1.4086 +  } note wnwn = this
1.4087 +  obtain pe where "pe>0" and cbp: "cball z (3 / 4 * pe) \<subseteq> - path_image p"
1.4088 +    using p open_contains_cball [of "- path_image p"]
1.4089 +    by (force simp: closed_def [symmetric] closed_path_image [OF valid_path_imp_path])
1.4090 +  obtain L
1.4091 +    where "L>0"
1.4092 +      and L: "\<And>f B. \<lbrakk>f holomorphic_on - cball z (3 / 4 * pe);
1.4093 +                      \<forall>z \<in> - cball z (3 / 4 * pe). cmod (f z) \<le> B\<rbrakk> \<Longrightarrow>
1.4094 +                      cmod (contour_integral p f) \<le> L * B"
1.4095 +    using contour_integral_bound_exists [of "- cball z (3/4*pe)" p] cbp \<open>valid_path p\<close> by force
1.4096 +  { fix e::real and w::complex
1.4097 +    assume e: "0 < e" and w: "cmod (w - z) < pe/4" "cmod (w - z) < e * pe\<^sup>2 / (8 * L)"
1.4098 +    then have [simp]: "w \<notin> path_image p"
1.4099 +      using cbp p(2) \<open>0 < pe\<close>
1.4100 +      by (force simp: dist_norm norm_minus_commute path_image_def cball_def)
1.4101 +    have [simp]: "contour_integral p (\<lambda>x. 1/(x - w)) - contour_integral p (\<lambda>x. 1/(x - z)) =
1.4102 +                  contour_integral p (\<lambda>x. 1/(x - w) - 1/(x - z))"
1.4103 +      by (simp add: p contour_integrable_inversediff contour_integral_diff)
1.4104 +    { fix x
1.4105 +      assume pe: "3/4 * pe < cmod (z - x)"
1.4106 +      have "cmod (w - x) < pe/4 + cmod (z - x)"
1.4107 +        by (meson add_less_cancel_right norm_diff_triangle_le order_refl order_trans_rules(21) w(1))
1.4108 +      then have wx: "cmod (w - x) < 4/3 * cmod (z - x)" using pe by simp
1.4109 +      have "cmod (z - x) \<le> cmod (z - w) + cmod (w - x)"
1.4110 +        using norm_diff_triangle_le by blast
1.4111 +      also have "... < pe/4 + cmod (w - x)"
1.4112 +        using w by (simp add: norm_minus_commute)
1.4113 +      finally have "pe/2 < cmod (w - x)"
1.4114 +        using pe by auto
1.4115 +      then have "(pe/2)^2 < cmod (w - x) ^ 2"
1.4116 +        apply (rule power_strict_mono)
1.4117 +        using \<open>pe>0\<close> by auto
1.4118 +      then have pe2: "pe^2 < 4 * cmod (w - x) ^ 2"
1.4119 +        by (simp add: power_divide)
1.4120 +      have "8 * L * cmod (w - z) < e * pe\<^sup>2"
1.4121 +        using w \<open>L>0\<close> by (simp add: field_simps)
1.4122 +      also have "... < e * 4 * cmod (w - x) * cmod (w - x)"
1.4123 +        using pe2 \<open>e>0\<close> by (simp add: power2_eq_square)
1.4124 +      also have "... < e * 4 * cmod (w - x) * (4/3 * cmod (z - x))"
1.4125 +        using wx
1.4126 +        apply (rule mult_strict_left_mono)
1.4127 +        using pe2 e not_less_iff_gr_or_eq by fastforce
1.4128 +      finally have "L * cmod (w - z) < 2/3 * e * cmod (w - x) * cmod (z - x)"
1.4129 +        by simp
1.4130 +      also have "... \<le> e * cmod (w - x) * cmod (z - x)"
1.4131 +         using e by simp
1.4132 +      finally have Lwz: "L * cmod (w - z) < e * cmod (w - x) * cmod (z - x)" .
1.4133 +      have "L * cmod (1 / (x - w) - 1 / (x - z)) \<le> e"
1.4134 +        apply (cases "x=z \<or> x=w")
1.4135 +        using pe \<open>pe>0\<close> w \<open>L>0\<close>
1.4136 +        apply (force simp: norm_minus_commute)
1.4137 +        using wx w(2) \<open>L>0\<close> pe pe2 Lwz
1.4138 +        apply (auto simp: divide_simps mult_less_0_iff norm_minus_commute norm_divide norm_mult power2_eq_square)
1.4139 +        done
1.4140 +    } note L_cmod_le = this
1.4141 +    have *: "cmod (contour_integral p (\<lambda>x. 1 / (x - w) - 1 / (x - z))) \<le> L * (e * pe\<^sup>2 / L / 4 * (inverse (pe / 2))\<^sup>2)"
1.4142 +      apply (rule L)
1.4143 +      using \<open>pe>0\<close> w
1.4144 +      apply (force simp: dist_norm norm_minus_commute intro!: holomorphic_intros)
1.4145 +      using \<open>pe>0\<close> w \<open>L>0\<close>
1.4146 +      apply (auto simp: cball_def dist_norm field_simps L_cmod_le  simp del: less_divide_eq_numeral1 le_divide_eq_numeral1)
1.4147 +      done
1.4148 +    have "cmod (contour_integral p (\<lambda>x. 1 / (x - w)) - contour_integral p (\<lambda>x. 1 / (x - z))) < 2*e"
1.4150 +      apply (rule le_less_trans [OF *])
1.4151 +      using \<open>L>0\<close> e
1.4152 +      apply (force simp: field_simps)
1.4153 +      done
1.4154 +    then have "cmod (winding_number p w - winding_number p z) < e"
1.4155 +      using pi_ge_two e
1.4156 +      by (force simp: winding_number_valid_path p field_simps norm_divide norm_mult intro: less_le_trans)
1.4157 +  } note cmod_wn_diff = this
1.4158 +  then have "isCont (winding_number p) z"
1.4159 +    apply (simp add: continuous_at_eps_delta, clarify)
1.4160 +    apply (rule_tac x="min (pe/4) (e/2*pe^2/L/4)" in exI)
1.4161 +    using \<open>pe>0\<close> \<open>L>0\<close>
1.4162 +    apply (simp add: dist_norm cmod_wn_diff)
1.4163 +    done
1.4164 +  then show ?thesis
1.4165 +    apply (rule continuous_transform_within [where d = "min d e / 2"])
1.4166 +    apply (auto simp: \<open>d>0\<close> \<open>e>0\<close> dist_norm wnwn)
1.4167 +    done
1.4168 +qed
1.4169 +
1.4170 +corollary continuous_on_winding_number:
1.4171 +    "path \<gamma> \<Longrightarrow> continuous_on (- path_image \<gamma>) (\<lambda>w. winding_number \<gamma> w)"
1.4172 +  by (simp add: continuous_at_imp_continuous_on continuous_at_winding_number)
1.4173 +
1.4174 +
1.4175 +subsection\<open>The winding number is constant on a connected region\<close>
1.4176 +
1.4177 +lemma winding_number_constant:
1.4178 +  assumes \<gamma>: "path \<gamma>" and loop: "pathfinish \<gamma> = pathstart \<gamma>" and cs: "connected s" and sg: "s \<inter> path_image \<gamma> = {}"
1.4179 +    shows "\<exists>k. \<forall>z \<in> s. winding_number \<gamma> z = k"
1.4180 +proof -
1.4181 +  { fix y z
1.4182 +    assume ne: "winding_number \<gamma> y \<noteq> winding_number \<gamma> z"
1.4183 +    assume "y \<in> s" "z \<in> s"
1.4184 +    then have "winding_number \<gamma> y \<in> \<int>"  "winding_number \<gamma> z \<in>  \<int>"
1.4185 +      using integer_winding_number [OF \<gamma> loop] sg \<open>y \<in> s\<close> by auto
1.4186 +    with ne have "1 \<le> cmod (winding_number \<gamma> y - winding_number \<gamma> z)"
1.4187 +      by (auto simp: Ints_def of_int_diff [symmetric] simp del: of_int_diff)
1.4188 +  } note * = this
1.4189 +  show ?thesis
1.4190 +    apply (rule continuous_discrete_range_constant [OF cs])
1.4191 +    using continuous_on_winding_number [OF \<gamma>] sg
1.4192 +    apply (metis Diff_Compl Diff_eq_empty_iff continuous_on_subset)
1.4193 +    apply (rule_tac x=1 in exI)
1.4194 +    apply (auto simp: *)
1.4195 +    done
1.4196 +qed
1.4197 +
1.4198 +lemma winding_number_eq:
1.4199 +     "\<lbrakk>path \<gamma>; pathfinish \<gamma> = pathstart \<gamma>; w \<in> s; z \<in> s; connected s; s \<inter> path_image \<gamma> = {}\<rbrakk>
1.4200 +      \<Longrightarrow> winding_number \<gamma> w = winding_number \<gamma> z"
1.4201 +using winding_number_constant by fastforce
1.4202 +
1.4203 +lemma open_winding_number_levelsets:
1.4204 +  assumes \<gamma>: "path \<gamma>" and loop: "pathfinish \<gamma> = pathstart \<gamma>"
1.4205 +    shows "open {z. z \<notin> path_image \<gamma> \<and> winding_number \<gamma> z = k}"
1.4206 +proof -
1.4207 +  have op: "open (- path_image \<gamma>)"
1.4208 +    by (simp add: closed_path_image \<gamma> open_Compl)
1.4209 +  { fix z assume z: "z \<notin> path_image \<gamma>" and k: "k = winding_number \<gamma> z"
1.4210 +    obtain e where e: "e>0" "ball z e \<subseteq> - path_image \<gamma>"
1.4211 +      using open_contains_ball [of "- path_image \<gamma>"] op z
1.4212 +      by blast
1.4213 +    have "\<exists>e>0. \<forall>y. dist y z < e \<longrightarrow> y \<notin> path_image \<gamma> \<and> winding_number \<gamma> y = winding_number \<gamma> z"
1.4214 +      apply (rule_tac x=e in exI)
1.4215 +      using e apply (simp add: dist_norm ball_def norm_minus_commute)
1.4216 +      apply (auto simp: dist_norm norm_minus_commute intro!: winding_number_eq [OF assms, where s = "ball z e"])
1.4217 +      done
1.4218 +  } then
1.4219 +  show ?thesis
1.4220 +    by (auto simp: open_dist)
1.4221 +qed
1.4222 +
1.4223 +subsection\<open>Winding number is zero "outside" a curve, in various senses\<close>
1.4224 +
1.4225 +lemma winding_number_zero_in_outside:
1.4226 +  assumes \<gamma>: "path \<gamma>" and loop: "pathfinish \<gamma> = pathstart \<gamma>" and z: "z \<in> outside (path_image \<gamma>)"
1.4227 +    shows "winding_number \<gamma> z = 0"
1.4228 +proof -
1.4229 +  obtain B::real where "0 < B" and B: "path_image \<gamma> \<subseteq> ball 0 B"
1.4230 +    using bounded_subset_ballD [OF bounded_path_image [OF \<gamma>]] by auto
1.4231 +  obtain w::complex where w: "w \<notin> ball 0 (B + 1)"
1.4232 +    by (metis abs_of_nonneg le_less less_irrefl mem_ball_0 norm_of_real)
1.4233 +  have "- ball 0 (B + 1) \<subseteq> outside (path_image \<gamma>)"
1.4234 +    apply (rule outside_subset_convex)
1.4235 +    using B subset_ball by auto
1.4236 +  then have wout: "w \<in> outside (path_image \<gamma>)"
1.4237 +    using w by blast
1.4238 +  moreover obtain k where "\<And>z. z \<in> outside (path_image \<gamma>) \<Longrightarrow> winding_number \<gamma> z = k"
1.4239 +    using winding_number_constant [OF \<gamma> loop, of "outside(path_image \<gamma>)"] connected_outside
1.4240 +    by (metis DIM_complex bounded_path_image dual_order.refl \<gamma> outside_no_overlap)
1.4241 +  ultimately have "winding_number \<gamma> z = winding_number \<gamma> w"
1.4242 +    using z by blast
1.4243 +  also have "... = 0"
1.4244 +  proof -
1.4245 +    have wnot: "w \<notin> path_image \<gamma>"  using wout by (simp add: outside_def)
1.4246 +    { fix e::real assume "0<e"
1.4247 +      obtain p where p: "polynomial_function p" "pathstart p = pathstart \<gamma>" "pathfinish p = pathfinish \<gamma>"
1.4248 +                 and pg1: "(\<And>t. \<lbrakk>0 \<le> t; t \<le> 1\<rbrakk> \<Longrightarrow> cmod (p t - \<gamma> t) < 1)"
1.4249 +                 and pge: "(\<And>t. \<lbrakk>0 \<le> t; t \<le> 1\<rbrakk> \<Longrightarrow> cmod (p t - \<gamma> t) < e)"
1.4250 +        using path_approx_polynomial_function [OF \<gamma>, of "min 1 e"] \<open>e>0\<close> by force
1.4251 +      have pip: "path_image p \<subseteq> ball 0 (B + 1)"
1.4252 +        using B
1.4253 +        apply (clarsimp simp add: path_image_def dist_norm ball_def)
1.4254 +        apply (frule (1) pg1)
1.4255 +        apply (fastforce dest: norm_add_less)
1.4256 +        done
1.4257 +      then have "w \<notin> path_image p"  using w by blast
1.4258 +      then have "\<exists>p. valid_path p \<and> w \<notin> path_image p \<and>
1.4259 +                     pathstart p = pathstart \<gamma> \<and> pathfinish p = pathfinish \<gamma> \<and>
1.4260 +                     (\<forall>t\<in>{0..1}. cmod (\<gamma> t - p t) < e) \<and> contour_integral p (\<lambda>wa. 1 / (wa - w)) = 0"
1.4261 +        apply (rule_tac x=p in exI)
1.4262 +        apply (simp add: p valid_path_polynomial_function)
1.4263 +        apply (intro conjI)
1.4264 +        using pge apply (simp add: norm_minus_commute)
1.4265 +        apply (rule contour_integral_unique [OF Cauchy_theorem_convex_simple [OF _ convex_ball [of 0 "B+1"]]])
1.4266 +        apply (rule holomorphic_intros | simp add: dist_norm)+
1.4267 +        using mem_ball_0 w apply blast
1.4268 +        using p apply (simp_all add: valid_path_polynomial_function loop pip)
1.4269 +        done
1.4270 +    }
1.4271 +    then show ?thesis
1.4272 +      by (auto intro: winding_number_unique [OF \<gamma>] simp add: wnot)
1.4273 +  qed
1.4274 +  finally show ?thesis .
1.4275 +qed
1.4276 +
1.4277 +lemma winding_number_zero_outside:
1.4278 +    "\<lbrakk>path \<gamma>; convex s; pathfinish \<gamma> = pathstart \<gamma>; z \<notin> s; path_image \<gamma> \<subseteq> s\<rbrakk> \<Longrightarrow> winding_number \<gamma> z = 0"
1.4279 +  by (meson convex_in_outside outside_mono subsetCE winding_number_zero_in_outside)
1.4280 +
1.4281 +lemma winding_number_zero_at_infinity:
1.4282 +  assumes \<gamma>: "path \<gamma>" and loop: "pathfinish \<gamma> = pathstart \<gamma>"
1.4283 +    shows "\<exists>B. \<forall>z. B \<le> norm z \<longrightarrow> winding_number \<gamma> z = 0"
1.4284 +proof -
1.4285 +  obtain B::real where "0 < B" and B: "path_image \<gamma> \<subseteq> ball 0 B"
1.4286 +    using bounded_subset_ballD [OF bounded_path_image [OF \<gamma>]] by auto
1.4287 +  then show ?thesis
1.4288 +    apply (rule_tac x="B+1" in exI, clarify)
1.4289 +    apply (rule winding_number_zero_outside [OF \<gamma> convex_cball [of 0 B] loop])
1.4290 +    apply (meson less_add_one mem_cball_0 not_le order_trans)
1.4291 +    using ball_subset_cball by blast
1.4292 +qed
1.4293 +
1.4294 +lemma winding_number_zero_point:
1.4295 +    "\<lbrakk>path \<gamma>; convex s; pathfinish \<gamma> = pathstart \<gamma>; open s; path_image \<gamma> \<subseteq> s\<rbrakk>
1.4296 +     \<Longrightarrow> \<exists>z. z \<in> s \<and> winding_number \<gamma> z = 0"
1.4297 +  using outside_compact_in_open [of "path_image \<gamma>" s] path_image_nonempty winding_number_zero_in_outside
1.4298 +  by (fastforce simp add: compact_path_image)
1.4299 +
1.4300 +
1.4301 +text\<open>If a path winds round a set, it winds rounds its inside.\<close>
1.4302 +lemma winding_number_around_inside:
1.4303 +  assumes \<gamma>: "path \<gamma>" and loop: "pathfinish \<gamma> = pathstart \<gamma>"
1.4304 +      and cls: "closed s" and cos: "connected s" and s_disj: "s \<inter> path_image \<gamma> = {}"
1.4305 +      and z: "z \<in> s" and wn_nz: "winding_number \<gamma> z \<noteq> 0" and w: "w \<in> s \<union> inside s"
1.4306 +    shows "winding_number \<gamma> w = winding_number \<gamma> z"
1.4307 +proof -
1.4308 +  have ssb: "s \<subseteq> inside(path_image \<gamma>)"
1.4309 +  proof
1.4310 +    fix x :: complex
1.4311 +    assume "x \<in> s"
1.4312 +    hence "x \<notin> path_image \<gamma>"
1.4313 +      by (meson disjoint_iff_not_equal s_disj)
1.4314 +    thus "x \<in> inside (path_image \<gamma>)"
1.4315 +      using \<open>x \<in> s\<close> by (metis (no_types) ComplI UnE cos \<gamma> loop s_disj union_with_outside winding_number_eq winding_number_zero_in_outside wn_nz z)
1.4316 +qed
1.4317 +  show ?thesis
1.4318 +    apply (rule winding_number_eq [OF \<gamma> loop w])
1.4319 +    using z apply blast
1.4320 +    apply (simp add: cls connected_with_inside cos)
1.4321 +    apply (simp add: Int_Un_distrib2 s_disj, safe)
1.4322 +    by (meson ssb inside_inside_compact_connected [OF cls, of "path_image \<gamma>"] compact_path_image connected_path_image contra_subsetD disjoint_iff_not_equal \<gamma> inside_no_overlap)
1.4323 + qed
1.4324 +
1.4325 +
1.4326 +text\<open>Bounding a WN by 1/2 for a path and point in opposite halfspaces.\<close>
1.4327 +lemma winding_number_subpath_continuous:
1.4328 +  assumes \<gamma>: "valid_path \<gamma>" and z: "z \<notin> path_image \<gamma>"
1.4329 +    shows "continuous_on {0..1} (\<lambda>x. winding_number(subpath 0 x \<gamma>) z)"
1.4330 +proof -
1.4331 +  have *: "integral {0..x} (\<lambda>t. vector_derivative \<gamma> (at t) / (\<gamma> t - z)) / (2 * of_real pi * \<i>) =
1.4332 +         winding_number (subpath 0 x \<gamma>) z"
1.4333 +         if x: "0 \<le> x" "x \<le> 1" for x
1.4334 +  proof -
1.4335 +    have "integral {0..x} (\<lambda>t. vector_derivative \<gamma> (at t) / (\<gamma> t - z)) / (2 * of_real pi * \<i>) =
1.4336 +          1 / (2*pi*\<i>) * contour_integral (subpath 0 x \<gamma>) (\<lambda>w. 1/(w - z))"
1.4337 +      using assms x
1.4338 +      apply (simp add: contour_integral_subcontour_integral [OF contour_integrable_inversediff])
1.4339 +      done
1.4340 +    also have "... = winding_number (subpath 0 x \<gamma>) z"
1.4341 +      apply (subst winding_number_valid_path)
1.4342 +      using assms x
1.4343 +      apply (simp_all add: path_image_subpath valid_path_subpath)
1.4344 +      by (force simp: path_image_def)
1.4345 +    finally show ?thesis .
1.4346 +  qed
1.4347 +  show ?thesis
1.4348 +    apply (rule continuous_on_eq
1.4349 +                 [where f = "\<lambda>x. 1 / (2*pi*\<i>) *
1.4350 +                                 integral {0..x} (\<lambda>t. 1/(\<gamma> t - z) * vector_derivative \<gamma> (at t))"])
1.4351 +    apply (rule continuous_intros)+
1.4352 +    apply (rule indefinite_integral_continuous)
1.4353 +    apply (rule contour_integrable_inversediff [OF assms, unfolded contour_integrable_on])
1.4354 +      using assms
1.4355 +    apply (simp add: *)
1.4356 +    done
1.4357 +qed
1.4358 +
1.4359 +lemma winding_number_ivt_pos:
1.4360 +    assumes \<gamma>: "valid_path \<gamma>" and z: "z \<notin> path_image \<gamma>" and "0 \<le> w" "w \<le> Re(winding_number \<gamma> z)"
1.4361 +      shows "\<exists>t \<in> {0..1}. Re(winding_number(subpath 0 t \<gamma>) z) = w"
1.4362 +  apply (rule ivt_increasing_component_on_1 [of 0 1, where ?k = "1::complex", simplified complex_inner_1_right])
1.4364 +  apply (rule winding_number_subpath_continuous [OF \<gamma> z])
1.4365 +  using assms
1.4366 +  apply (auto simp: path_image_def image_def)
1.4367 +  done
1.4368 +
1.4369 +lemma winding_number_ivt_neg:
1.4370 +    assumes \<gamma>: "valid_path \<gamma>" and z: "z \<notin> path_image \<gamma>" and "Re(winding_number \<gamma> z) \<le> w" "w \<le> 0"
1.4371 +      shows "\<exists>t \<in> {0..1}. Re(winding_number(subpath 0 t \<gamma>) z) = w"
1.4372 +  apply (rule ivt_decreasing_component_on_1 [of 0 1, where ?k = "1::complex", simplified complex_inner_1_right])
1.4374 +  apply (rule winding_number_subpath_continuous [OF \<gamma> z])
1.4375 +  using assms
1.4376 +  apply (auto simp: path_image_def image_def)
1.4377 +  done
1.4378 +
1.4379 +lemma winding_number_ivt_abs:
1.4380 +    assumes \<gamma>: "valid_path \<gamma>" and z: "z \<notin> path_image \<gamma>" and "0 \<le> w" "w \<le> \<bar>Re(winding_number \<gamma> z)\<bar>"
1.4381 +      shows "\<exists>t \<in> {0..1}. \<bar>Re (winding_number (subpath 0 t \<gamma>) z)\<bar> = w"
1.4382 +  using assms winding_number_ivt_pos [of \<gamma> z w] winding_number_ivt_neg [of \<gamma> z "-w"]
1.4383 +  by force
1.4384 +
1.4385 +lemma winding_number_lt_half_lemma:
1.4386 +  assumes \<gamma>: "valid_path \<gamma>" and z: "z \<notin> path_image \<gamma>" and az: "a \<bullet> z \<le> b" and pag: "path_image \<gamma> \<subseteq> {w. a \<bullet> w > b}"
1.4387 +    shows "Re(winding_number \<gamma> z) < 1/2"
1.4388 +proof -
1.4389 +  { assume "Re(winding_number \<gamma> z) \<ge> 1/2"
1.4390 +    then obtain t::real where t: "0 \<le> t" "t \<le> 1" and sub12: "Re (winding_number (subpath 0 t \<gamma>) z) = 1/2"
1.4391 +      using winding_number_ivt_pos [OF \<gamma> z, of "1/2"] by auto
1.4392 +    have gt: "\<gamma> t - z = - (of_real (exp (- (2 * pi * Im (winding_number (subpath 0 t \<gamma>) z)))) * (\<gamma> 0 - z))"
1.4393 +      using winding_number_exp_2pi [of "subpath 0 t \<gamma>" z]
1.4394 +      apply (simp add: t \<gamma> valid_path_imp_path)
1.4395 +      using closed_segment_eq_real_ivl path_image_def t z by (fastforce simp: path_image_subpath Euler sub12)
1.4396 +    have "b < a \<bullet> \<gamma> 0"
1.4397 +    proof -
1.4398 +      have "\<gamma> 0 \<in> {c. b < a \<bullet> c}"
1.4399 +        by (metis (no_types) pag atLeastAtMost_iff image_subset_iff order_refl path_image_def zero_le_one)
1.4400 +      thus ?thesis
1.4401 +        by blast
1.4402 +    qed
1.4403 +    moreover have "b < a \<bullet> \<gamma> t"
1.4404 +    proof -
1.4405 +      have "\<gamma> t \<in> {c. b < a \<bullet> c}"
1.4406 +        by (metis (no_types) pag atLeastAtMost_iff image_subset_iff path_image_def t)
1.4407 +      thus ?thesis
1.4408 +        by blast
1.4409 +    qed
1.4410 +    ultimately have "0 < a \<bullet> (\<gamma> 0 - z)" "0 < a \<bullet> (\<gamma> t - z)" using az
1.4411 +      by (simp add: inner_diff_right)+
1.4412 +    then have False
1.4413 +      by (simp add: gt inner_mult_right mult_less_0_iff)
1.4414 +  }
1.4415 +  then show ?thesis by force
1.4416 +qed
1.4417 +
1.4418 +lemma winding_number_lt_half:
1.4419 +  assumes "valid_path \<gamma>" "a \<bullet> z \<le> b" "path_image \<gamma> \<subseteq> {w. a \<bullet> w > b}"
1.4420 +    shows "\<bar>Re (winding_number \<gamma> z)\<bar> < 1/2"
1.4421 +proof -
1.4422 +  have "z \<notin> path_image \<gamma>" using assms by auto
1.4423 +  with assms show ?thesis
1.4424 +    apply (simp add: winding_number_lt_half_lemma abs_if del: less_divide_eq_numeral1)
1.4425 +    apply (metis complex_inner_1_right winding_number_lt_half_lemma [OF valid_path_imp_reverse, of \<gamma> z a b]
1.4426 +                 winding_number_reversepath valid_path_imp_path inner_minus_left path_image_reversepath)
1.4427 +    done
1.4428 +qed
1.4429 +
1.4430 +lemma winding_number_le_half:
1.4431 +  assumes \<gamma>: "valid_path \<gamma>" and z: "z \<notin> path_image \<gamma>"
1.4432 +      and anz: "a \<noteq> 0" and azb: "a \<bullet> z \<le> b" and pag: "path_image \<gamma> \<subseteq> {w. a \<bullet> w \<ge> b}"
1.4433 +    shows "\<bar>Re (winding_number \<gamma> z)\<bar> \<le> 1/2"
1.4434 +proof -
1.4435 +  { assume wnz_12: "\<bar>Re (winding_number \<gamma> z)\<bar> > 1/2"
1.4436 +    have "isCont (winding_number \<gamma>) z"
1.4437 +      by (metis continuous_at_winding_number valid_path_imp_path \<gamma> z)
1.4438 +    then obtain d where "d>0" and d: "\<And>x'. dist x' z < d \<Longrightarrow> dist (winding_number \<gamma> x') (winding_number \<gamma> z) < \<bar>Re(winding_number \<gamma> z)\<bar> - 1/2"
1.4439 +      using continuous_at_eps_delta wnz_12 diff_gt_0_iff_gt by blast
1.4440 +    define z' where "z' = z - (d / (2 * cmod a)) *\<^sub>R a"
1.4441 +    have *: "a \<bullet> z' \<le> b - d / 3 * cmod a"
1.4442 +      unfolding z'_def inner_mult_right' divide_inverse
1.4443 +      apply (simp add: divide_simps algebra_simps dot_square_norm power2_eq_square anz)
1.4444 +      apply (metis \<open>0 < d\<close> add_increasing azb less_eq_real_def mult_nonneg_nonneg mult_right_mono norm_ge_zero norm_numeral)
1.4445 +      done
1.4446 +    have "cmod (winding_number \<gamma> z' - winding_number \<gamma> z) < \<bar>Re (winding_number \<gamma> z)\<bar> - 1/2"
1.4447 +      using d [of z'] anz \<open>d>0\<close> by (simp add: dist_norm z'_def)
1.4448 +    then have "1/2 < \<bar>Re (winding_number \<gamma> z)\<bar> - cmod (winding_number \<gamma> z' - winding_number \<gamma> z)"
1.4449 +      by simp
1.4450 +    then have "1/2 < \<bar>Re (winding_number \<gamma> z)\<bar> - \<bar>Re (winding_number \<gamma> z') - Re (winding_number \<gamma> z)\<bar>"
1.4451 +      using abs_Re_le_cmod [of "winding_number \<gamma> z' - winding_number \<gamma> z"] by simp
1.4452 +    then have wnz_12': "\<bar>Re (winding_number \<gamma> z')\<bar> > 1/2"
1.4453 +      by linarith
1.4454 +    moreover have "\<bar>Re (winding_number \<gamma> z')\<bar> < 1/2"
1.4455 +      apply (rule winding_number_lt_half [OF \<gamma> *])
1.4456 +      using azb \<open>d>0\<close> pag
1.4457 +      apply (auto simp: add_strict_increasing anz divide_simps algebra_simps dest!: subsetD)
1.4458 +      done
1.4459 +    ultimately have False
1.4460 +      by simp
1.4461 +  }
1.4462 +  then show ?thesis by force
1.4463 +qed
1.4464 +
1.4465 +lemma winding_number_lt_half_linepath: "z \<notin> closed_segment a b \<Longrightarrow> \<bar>Re (winding_number (linepath a b) z)\<bar> < 1/2"
1.4466 +  using separating_hyperplane_closed_point [of "closed_segment a b" z]
1.4467 +  apply auto
1.4468 +  apply (simp add: closed_segment_def)
1.4469 +  apply (drule less_imp_le)
1.4470 +  apply (frule winding_number_lt_half [OF valid_path_linepath [of a b]])
1.4471 +  apply (auto simp: segment)
1.4472 +  done
1.4473 +
1.4474 +
1.4475 +text\<open> Positivity of WN for a linepath.\<close>
1.4476 +lemma winding_number_linepath_pos_lt:
1.4477 +    assumes "0 < Im ((b - a) * cnj (b - z))"
1.4478 +      shows "0 < Re(winding_number(linepath a b) z)"
1.4479 +proof -
1.4480 +  have z: "z \<notin> path_image (linepath a b)"
1.4481 +    using assms
1.4482 +    by (simp add: closed_segment_def) (force simp: algebra_simps)
1.4483 +  show ?thesis
1.4484 +    apply (rule winding_number_pos_lt [OF valid_path_linepath z assms])
1.4485 +    apply (simp add: linepath_def algebra_simps)
1.4486 +    done
1.4487 +qed
1.4488 +
1.4489 +
1.4490 +subsection\<open>Cauchy's integral formula, again for a convex enclosing set.\<close>
1.4491 +
1.4492 +lemma Cauchy_integral_formula_weak:
1.4493 +    assumes s: "convex s" and "finite k" and conf: "continuous_on s f"
1.4494 +        and fcd: "(\<And>x. x \<in> interior s - k \<Longrightarrow> f field_differentiable at x)"
1.4495 +        and z: "z \<in> interior s - k" and vpg: "valid_path \<gamma>"
1.4496 +        and pasz: "path_image \<gamma> \<subseteq> s - {z}" and loop: "pathfinish \<gamma> = pathstart \<gamma>"
1.4497 +      shows "((\<lambda>w. f w / (w - z)) has_contour_integral (2*pi * \<i> * winding_number \<gamma> z * f z)) \<gamma>"
1.4498 +proof -
1.4499 +  obtain f' where f': "(f has_field_derivative f') (at z)"
1.4500 +    using fcd [OF z] by (auto simp: field_differentiable_def)
1.4501 +  have pas: "path_image \<gamma> \<subseteq> s" and znotin: "z \<notin> path_image \<gamma>" using pasz by blast+
1.4502 +  have c: "continuous (at x within s) (\<lambda>w. if w = z then f' else (f w - f z) / (w - z))" if "x \<in> s" for x
1.4503 +  proof (cases "x = z")
1.4504 +    case True then show ?thesis
1.4505 +      apply (simp add: continuous_within)
1.4506 +      apply (rule Lim_transform_away_within [of _ "z+1" _ "\<lambda>w::complex. (f w - f z)/(w - z)"])
1.4507 +      using has_field_derivative_at_within DERIV_within_iff f'
1.4508 +      apply (fastforce simp add:)+
1.4509 +      done
1.4510 +  next
1.4511 +    case False
1.4512 +    then have dxz: "dist x z > 0" by auto
1.4513 +    have cf: "continuous (at x within s) f"
1.4514 +      using conf continuous_on_eq_continuous_within that by blast
1.4515 +    have "continuous (at x within s) (\<lambda>w. (f w - f z) / (w - z))"
1.4516 +      by (rule cf continuous_intros | simp add: False)+
1.4517 +    then show ?thesis
1.4518 +      apply (rule continuous_transform_within [OF _ dxz that, of "\<lambda>w::complex. (f w - f z)/(w - z)"])
1.4519 +      apply (force simp: dist_commute)
1.4520 +      done
1.4521 +  qed
1.4522 +  have fink': "finite (insert z k)" using \<open>finite k\<close> by blast
1.4523 +  have *: "((\<lambda>w. if w = z then f' else (f w - f z) / (w - z)) has_contour_integral 0) \<gamma>"
1.4524 +    apply (rule Cauchy_theorem_convex [OF _ s fink' _ vpg pas loop])
1.4525 +    using c apply (force simp: continuous_on_eq_continuous_within)
1.4526 +    apply (rename_tac w)
1.4527 +    apply (rule_tac d="dist w z" and f = "\<lambda>w. (f w - f z)/(w - z)" in field_differentiable_transform_within)
1.4528 +    apply (simp_all add: dist_pos_lt dist_commute)
1.4529 +    apply (metis less_irrefl)
1.4530 +    apply (rule derivative_intros fcd | simp)+
1.4531 +    done
1.4532 +  show ?thesis
1.4533 +    apply (rule has_contour_integral_eq)
1.4534 +    using znotin has_contour_integral_add [OF has_contour_integral_lmul [OF has_contour_integral_winding_number [OF vpg znotin], of "f z"] *]
1.4535 +    apply (auto simp: mult_ac divide_simps)
1.4536 +    done
1.4537 +qed
1.4538 +
1.4539 +theorem Cauchy_integral_formula_convex_simple:
1.4540 +    "\<lbrakk>convex s; f holomorphic_on s; z \<in> interior s; valid_path \<gamma>; path_image \<gamma> \<subseteq> s - {z};
1.4541 +      pathfinish \<gamma> = pathstart \<gamma>\<rbrakk>
1.4542 +     \<Longrightarrow> ((\<lambda>w. f w / (w - z)) has_contour_integral (2*pi * \<i> * winding_number \<gamma> z * f z)) \<gamma>"
1.4543 +  apply (rule Cauchy_integral_formula_weak [where k = "{}"])
1.4544 +  using holomorphic_on_imp_continuous_on
1.4545 +  by auto (metis at_within_interior holomorphic_on_def interiorE subsetCE)
1.4546 +
1.4547 +
1.4548 +subsection\<open>Homotopy forms of Cauchy's theorem\<close>
1.4549 +
1.4550 +proposition Cauchy_theorem_homotopic:
1.4551 +    assumes hom: "if atends then homotopic_paths s g h else homotopic_loops s g h"
1.4552 +        and "open s" and f: "f holomorphic_on s"
1.4553 +        and vpg: "valid_path g" and vph: "valid_path h"
1.4554 +    shows "contour_integral g f = contour_integral h f"
1.4555 +proof -
1.4556 +  have pathsf: "linked_paths atends g h"
1.4557 +    using hom  by (auto simp: linked_paths_def homotopic_paths_imp_pathstart homotopic_paths_imp_pathfinish homotopic_loops_imp_loop)
1.4558 +  obtain k :: "real \<times> real \<Rightarrow> complex"
1.4559 +    where contk: "continuous_on ({0..1} \<times> {0..1}) k"
1.4560 +      and ks: "k ` ({0..1} \<times> {0..1}) \<subseteq> s"
1.4561 +      and k [simp]: "\<forall>x. k (0, x) = g x" "\<forall>x. k (1, x) = h x"
1.4562 +      and ksf: "\<forall>t\<in>{0..1}. linked_paths atends g (\<lambda>x. k (t, x))"
1.4563 +      using hom pathsf by (auto simp: linked_paths_def homotopic_paths_def homotopic_loops_def homotopic_with_def split: if_split_asm)
1.4564 +  have ucontk: "uniformly_continuous_on ({0..1} \<times> {0..1}) k"
1.4565 +    by (blast intro: compact_Times compact_uniformly_continuous [OF contk])
1.4566 +  { fix t::real assume t: "t \<in> {0..1}"
1.4567 +    have pak: "path (k o (\<lambda>u. (t, u)))"
1.4568 +      unfolding path_def
1.4569 +      apply (rule continuous_intros continuous_on_subset [OF contk])+
1.4570 +      using t by force
1.4571 +    have pik: "path_image (k \<circ> Pair t) \<subseteq> s"
1.4572 +      using ks t by (auto simp: path_image_def)
1.4573 +    obtain e where "e>0" and e:
1.4574 +         "\<And>g h. \<lbrakk>valid_path g; valid_path h;
1.4575 +                  \<forall>u\<in>{0..1}. cmod (g u - (k \<circ> Pair t) u) < e \<and> cmod (h u - (k \<circ> Pair t) u) < e;
1.4576 +                  linked_paths atends g h\<rbrakk>
1.4577 +                 \<Longrightarrow> contour_integral h f = contour_integral g f"
1.4578 +      using contour_integral_nearby [OF \<open>open s\<close> pak pik, of atends] f by metis
1.4579 +    obtain d where "d>0" and d:
1.4580 +        "\<And>x x'. \<lbrakk>x \<in> {0..1} \<times> {0..1}; x' \<in> {0..1} \<times> {0..1}; norm (x'-x) < d\<rbrakk> \<Longrightarrow> norm (k x' - k x) < e/4"
1.4581 +      by (rule uniformly_continuous_onE [OF ucontk, of "e/4"]) (auto simp: dist_norm \<open>e>0\<close>)
1.4582 +    { fix t1 t2
1.4583 +      assume t1: "0 \<le> t1" "t1 \<le> 1" and t2: "0 \<le> t2" "t2 \<le> 1" and ltd: "\<bar>t1 - t\<bar> < d" "\<bar>t2 - t\<bar> < d"
1.4584 +      have no2: "\<And>g1 k1 kt. \<lbrakk>norm(g1 - k1) < e/4; norm(k1 - kt) < e/4\<rbrakk> \<Longrightarrow> norm(g1 - kt) < e"
1.4585 +        using \<open>e > 0\<close>
1.4586 +        apply (rule_tac y = k1 in norm_triangle_half_l)
1.4587 +        apply (auto simp: norm_minus_commute intro: order_less_trans)
1.4588 +        done
1.4589 +      have "\<exists>d>0. \<forall>g1 g2. valid_path g1 \<and> valid_path g2 \<and>
1.4590 +                          (\<forall>u\<in>{0..1}. cmod (g1 u - k (t1, u)) < d \<and> cmod (g2 u - k (t2, u)) < d) \<and>
1.4591 +                          linked_paths atends g1 g2 \<longrightarrow>
1.4592 +                          contour_integral g2 f = contour_integral g1 f"
1.4593 +        apply (rule_tac x="e/4" in exI)
1.4594 +        using t t1 t2 ltd \<open>e > 0\<close>
1.4595 +        apply (auto intro!: e simp: d no2 simp del: less_divide_eq_numeral1)
1.4596 +        done
1.4597 +    }
1.4598 +    then have "\<exists>e. 0 < e \<and>
1.4599 +              (\<forall>t1 t2. t1 \<in> {0..1} \<and> t2 \<in> {0..1} \<and> \<bar>t1 - t\<bar> < e \<and> \<bar>t2 - t\<bar> < e
1.4600 +                \<longrightarrow> (\<exists>d. 0 < d \<and>
1.4601 +                     (\<forall>g1 g2. valid_path g1 \<and> valid_path g2 \<and>
1.4602 +                       (\<forall>u \<in> {0..1}.
1.4603 +                          norm(g1 u - k((t1,u))) < d \<and> norm(g2 u - k((t2,u))) < d) \<and>
1.4604 +                          linked_paths atends g1 g2
1.4605 +                          \<longrightarrow> contour_integral g2 f = contour_integral g1 f)))"
1.4606 +      by (rule_tac x=d in exI) (simp add: \<open>d > 0\<close>)
1.4607 +  }
1.4608 +  then obtain ee where ee:
1.4609 +       "\<And>t. t \<in> {0..1} \<Longrightarrow> ee t > 0 \<and>
1.4610 +          (\<forall>t1 t2. t1 \<in> {0..1} \<longrightarrow> t2 \<in> {0..1} \<longrightarrow> \<bar>t1 - t\<bar> < ee t \<longrightarrow> \<bar>t2 - t\<bar> < ee t
1.4611 +            \<longrightarrow> (\<exists>d. 0 < d \<and>
1.4612 +                 (\<forall>g1 g2. valid_path g1 \<and> valid_path g2 \<and>
1.4613 +                   (\<forall>u \<in> {0..1}.
1.4614 +                      norm(g1 u - k((t1,u))) < d \<and> norm(g2 u - k((t2,u))) < d) \<and>
1.4615 +                      linked_paths atends g1 g2
1.4616 +                      \<longrightarrow> contour_integral g2 f = contour_integral g1 f)))"
1.4617 +    by metis
1.4618 +  note ee_rule = ee [THEN conjunct2, rule_format]
1.4619 +  define C where "C = (\<lambda>t. ball t (ee t / 3)) ` {0..1}"
1.4620 +  have "\<forall>t \<in> C. open t" by (simp add: C_def)
1.4621 +  moreover have "{0..1} \<subseteq> \<Union>C"
1.4622 +    using ee [THEN conjunct1] by (auto simp: C_def dist_norm)
1.4623 +  ultimately obtain C' where C': "C' \<subseteq> C" "finite C'" and C'01: "{0..1} \<subseteq> \<Union>C'"
1.4624 +    by (rule compactE [OF compact_interval])
1.4625 +  define kk where "kk = {t \<in> {0..1}. ball t (ee t / 3) \<in> C'}"
1.4626 +  have kk01: "kk \<subseteq> {0..1}" by (auto simp: kk_def)
1.4627 +  define e where "e = Min (ee ` kk)"
1.4628 +  have C'_eq: "C' = (\<lambda>t. ball t (ee t / 3)) ` kk"
1.4629 +    using C' by (auto simp: kk_def C_def)
1.4630 +  have ee_pos[simp]: "\<And>t. t \<in> {0..1} \<Longrightarrow> ee t > 0"
1.4631 +    by (simp add: kk_def ee)
1.4632 +  moreover have "finite kk"
1.4633 +    using \<open>finite C'\<close> kk01 by (force simp: C'_eq inj_on_def ball_eq_ball_iff dest: ee_pos finite_imageD)
1.4634 +  moreover have "kk \<noteq> {}" using \<open>{0..1} \<subseteq> \<Union>C'\<close> C'_eq by force
1.4635 +  ultimately have "e > 0"
1.4636 +    using finite_less_Inf_iff [of "ee ` kk" 0] kk01 by (force simp: e_def)
1.4637 +  then obtain N::nat where "N > 0" and N: "1/N < e/3"
1.4638 +    by (meson divide_pos_pos nat_approx_posE zero_less_Suc zero_less_numeral)
1.4639 +  have e_le_ee: "\<And>i. i \<in> kk \<Longrightarrow> e \<le> ee i"
1.4640 +    using \<open>finite kk\<close> by (simp add: e_def Min_le_iff [of "ee ` kk"])
1.4641 +  have plus: "\<exists>t \<in> kk. x \<in> ball t (ee t / 3)" if "x \<in> {0..1}" for x
1.4642 +    using C' subsetD [OF C'01 that]  unfolding C'_eq by blast
1.4643 +  have [OF order_refl]:
1.4644 +      "\<exists>d. 0 < d \<and> (\<forall>j. valid_path j \<and> (\<forall>u \<in> {0..1}. norm(j u - k (n/N, u)) < d) \<and> linked_paths atends g j
1.4645 +                        \<longrightarrow> contour_integral j f = contour_integral g f)"
1.4646 +       if "n \<le> N" for n
1.4647 +  using that
1.4648 +  proof (induct n)
1.4649 +    case 0 show ?case using ee_rule [of 0 0 0]
1.4650 +      apply clarsimp
1.4651 +      apply (rule_tac x=d in exI, safe)
1.4652 +      by (metis diff_self vpg norm_zero)
1.4653 +  next
1.4654 +    case (Suc n)
1.4655 +    then have N01: "n/N \<in> {0..1}" "(Suc n)/N \<in> {0..1}"  by auto
1.4656 +    then obtain t where t: "t \<in> kk" "n/N \<in> ball t (ee t / 3)"
1.4657 +      using plus [of "n/N"] by blast
1.4658 +    then have nN_less: "\<bar>n/N - t\<bar> < ee t"
1.4659 +      by (simp add: dist_norm del: less_divide_eq_numeral1)
1.4660 +    have n'N_less: "\<bar>real (Suc n) / real N - t\<bar> < ee t"
1.4661 +      using t N \<open>N > 0\<close> e_le_ee [of t]
1.4663 +    have t01: "t \<in> {0..1}" using \<open>kk \<subseteq> {0..1}\<close> \<open>t \<in> kk\<close> by blast
1.4664 +    obtain d1 where "d1 > 0" and d1:
1.4665 +        "\<And>g1 g2. \<lbrakk>valid_path g1; valid_path g2;
1.4666 +                   \<forall>u\<in>{0..1}. cmod (g1 u - k (n/N, u)) < d1 \<and> cmod (g2 u - k ((Suc n) / N, u)) < d1;
1.4667 +                   linked_paths atends g1 g2\<rbrakk>
1.4668 +                   \<Longrightarrow> contour_integral g2 f = contour_integral g1 f"
1.4669 +      using ee [THEN conjunct2, rule_format, OF t01 N01 nN_less n'N_less] by fastforce
1.4670 +    have "n \<le> N" using Suc.prems by auto
1.4671 +    with Suc.hyps
1.4672 +    obtain d2 where "d2 > 0"
1.4673 +      and d2: "\<And>j. \<lbrakk>valid_path j; \<forall>u\<in>{0..1}. cmod (j u - k (n/N, u)) < d2; linked_paths atends g j\<rbrakk>
1.4674 +                     \<Longrightarrow> contour_integral j f = contour_integral g f"
1.4675 +        by auto
1.4676 +    have "continuous_on {0..1} (k o (\<lambda>u. (n/N, u)))"
1.4677 +      apply (rule continuous_intros continuous_on_subset [OF contk])+
1.4678 +      using N01 by auto
1.4679 +    then have pkn: "path (\<lambda>u. k (n/N, u))"
1.4680 +      by (simp add: path_def)
1.4681 +    have min12: "min d1 d2 > 0" by (simp add: \<open>0 < d1\<close> \<open>0 < d2\<close>)
1.4682 +    obtain p where "polynomial_function p"
1.4683 +        and psf: "pathstart p = pathstart (\<lambda>u. k (n/N, u))"
1.4684 +                 "pathfinish p = pathfinish (\<lambda>u. k (n/N, u))"
1.4685 +        and pk_le:  "\<And>t. t\<in>{0..1} \<Longrightarrow> cmod (p t - k (n/N, t)) < min d1 d2"
1.4686 +      using path_approx_polynomial_function [OF pkn min12] by blast
1.4687 +    then have vpp: "valid_path p" using valid_path_polynomial_function by blast
1.4688 +    have lpa: "linked_paths atends g p"
1.4689 +      by (metis (mono_tags, lifting) N01(1) ksf linked_paths_def pathfinish_def pathstart_def psf)
1.4690 +    show ?case
1.4691 +      apply (rule_tac x="min d1 d2" in exI)
1.4692 +      apply (simp add: \<open>0 < d1\<close> \<open>0 < d2\<close>, clarify)
1.4693 +      apply (rule_tac s="contour_integral p f" in trans)
1.4694 +      using pk_le N01(1) ksf pathfinish_def pathstart_def
1.4695 +      apply (force intro!: vpp d1 simp add: linked_paths_def psf ksf)
1.4696 +      using pk_le N01 apply (force intro!: vpp d2 lpa simp add: linked_paths_def psf ksf)
1.4697 +      done
1.4698 +  qed
1.4699 +  then obtain d where "0 < d"
1.4700 +                       "\<And>j. valid_path j \<and> (\<forall>u \<in> {0..1}. norm(j u - k (1,u)) < d) \<and>
1.4701 +                            linked_paths atends g j
1.4702 +                            \<Longrightarrow> contour_integral j f = contour_integral g f"
1.4703 +    using \<open>N>0\<close> by auto
1.4704 +  then have "linked_paths atends g h \<Longrightarrow> contour_integral h f = contour_integral g f"
1.4705 +    using \<open>N>0\<close> vph by fastforce
1.4706 +  then show ?thesis
1.4707 +    by (simp add: pathsf)
1.4708 +qed
1.4709 +
1.4710 +proposition Cauchy_theorem_homotopic_paths:
1.4711 +    assumes hom: "homotopic_paths s g h"
1.4712 +        and "open s" and f: "f holomorphic_on s"
1.4713 +        and vpg: "valid_path g" and vph: "valid_path h"
1.4714 +    shows "contour_integral g f = contour_integral h f"
1.4715 +  using Cauchy_theorem_homotopic [of True s g h] assms by simp
1.4716 +
1.4717 +proposition Cauchy_theorem_homotopic_loops:
1.4718 +    assumes hom: "homotopic_loops s g h"
1.4719 +        and "open s" and f: "f holomorphic_on s"
1.4720 +        and vpg: "valid_path g" and vph: "valid_path h"
1.4721 +    shows "contour_integral g f = contour_integral h f"
1.4722 +  using Cauchy_theorem_homotopic [of False s g h] assms by simp
1.4723 +
1.4724 +lemma has_contour_integral_newpath:
1.4725 +    "\<lbrakk>(f has_contour_integral y) h; f contour_integrable_on g; contour_integral g f = contour_integral h f\<rbrakk>
1.4726 +     \<Longrightarrow> (f has_contour_integral y) g"
1.4727 +  using has_contour_integral_integral contour_integral_unique by auto
1.4728 +
1.4729 +lemma Cauchy_theorem_null_homotopic:
1.4730 +     "\<lbrakk>f holomorphic_on s; open s; valid_path g; homotopic_loops s g (linepath a a)\<rbrakk> \<Longrightarrow> (f has_contour_integral 0) g"
1.4731 +  apply (rule has_contour_integral_newpath [where h = "linepath a a"], simp)
1.4732 +  using contour_integrable_holomorphic_simple
1.4733 +    apply (blast dest: holomorphic_on_imp_continuous_on homotopic_loops_imp_subset)
1.4734 +  by (simp add: Cauchy_theorem_homotopic_loops)
1.4735 +
1.4736 +
1.4737 +
1.4738 +subsection\<open>More winding number properties\<close>
1.4739 +
1.4740 +text\<open>including the fact that it's +-1 inside a simple closed curve.\<close>
1.4741 +
1.4742 +lemma winding_number_homotopic_paths:
1.4743 +    assumes "homotopic_paths (-{z}) g h"
1.4744 +      shows "winding_number g z = winding_number h z"
1.4745 +proof -
1.4746 +  have "path g" "path h" using homotopic_paths_imp_path [OF assms] by auto
1.4747 +  moreover have pag: "z \<notin> path_image g" and pah: "z \<notin> path_image h"
1.4748 +    using homotopic_paths_imp_subset [OF assms] by auto
1.4749 +  ultimately obtain d e where "d > 0" "e > 0"
1.4750 +      and d: "\<And>p. \<lbrakk>path p; pathstart p = pathstart g; pathfinish p = pathfinish g; \<forall>t\<in>{0..1}. norm (p t - g t) < d\<rbrakk>
1.4751 +            \<Longrightarrow> homotopic_paths (-{z}) g p"
1.4752 +      and e: "\<And>q. \<lbrakk>path q; pathstart q = pathstart h; pathfinish q = pathfinish h; \<forall>t\<in>{0..1}. norm (q t - h t) < e\<rbrakk>
1.4753 +            \<Longrightarrow> homotopic_paths (-{z}) h q"
1.4754 +    using homotopic_nearby_paths [of g "-{z}"] homotopic_nearby_paths [of h "-{z}"] by force
1.4755 +  obtain p where p:
1.4756 +       "valid_path p" "z \<notin> path_image p"
1.4757 +       "pathstart p = pathstart g" "pathfinish p = pathfinish g"
1.4758 +       and gp_less:"\<forall>t\<in>{0..1}. cmod (g t - p t) < d"
1.4759 +       and pap: "contour_integral p (\<lambda>w. 1 / (w - z)) = complex_of_real (2 * pi) * \<i> * winding_number g z"
1.4760 +    using winding_number [OF \<open>path g\<close> pag \<open>0 < d\<close>] by blast
1.4761 +  obtain q where q:
1.4762 +       "valid_path q" "z \<notin> path_image q"
1.4763 +       "pathstart q = pathstart h" "pathfinish q = pathfinish h"
1.4764 +       and hq_less: "\<forall>t\<in>{0..1}. cmod (h t - q t) < e"
1.4765 +       and paq:  "contour_integral q (\<lambda>w. 1 / (w - z)) = complex_of_real (2 * pi) * \<i> * winding_number h z"
1.4766 +    using winding_number [OF \<open>path h\<close> pah \<open>0 < e\<close>] by blast
1.4767 +  have gp: "homotopic_paths (- {z}) g p"
1.4768 +    by (simp add: d p valid_path_imp_path norm_minus_commute gp_less)
1.4769 +  have hq: "homotopic_paths (- {z}) h q"
1.4770 +    by (simp add: e q valid_path_imp_path norm_minus_commute hq_less)
1.4771 +  have "contour_integral p (\<lambda>w. 1/(w - z)) = contour_integral q (\<lambda>w. 1/(w - z))"
1.4772 +    apply (rule Cauchy_theorem_homotopic_paths [of "-{z}"])
1.4773 +    apply (blast intro: homotopic_paths_trans homotopic_paths_sym gp hq assms)
1.4774 +    apply (auto intro!: holomorphic_intros simp: p q)
1.4775 +    done
1.4776 +  then show ?thesis
1.4777 +    by (simp add: pap paq)
1.4778 +qed
1.4779 +
1.4780 +lemma winding_number_homotopic_loops:
1.4781 +    assumes "homotopic_loops (-{z}) g h"
1.4782 +      shows "winding_number g z = winding_number h z"
1.4783 +proof -
1.4784 +  have "path g" "path h" using homotopic_loops_imp_path [OF assms] by auto
1.4785 +  moreover have pag: "z \<notin> path_image g" and pah: "z \<notin> path_image h"
1.4786 +    using homotopic_loops_imp_subset [OF assms] by auto
1.4787 +  moreover have gloop: "pathfinish g = pathstart g" and hloop: "pathfinish h = pathstart h"
1.4788 +    using homotopic_loops_imp_loop [OF assms] by auto
1.4789 +  ultimately obtain d e where "d > 0" "e > 0"
1.4790 +      and d: "\<And>p. \<lbrakk>path p; pathfinish p = pathstart p; \<forall>t\<in>{0..1}. norm (p t - g t) < d\<rbrakk>
1.4791 +            \<Longrightarrow> homotopic_loops (-{z}) g p"
1.4792 +      and e: "\<And>q. \<lbrakk>path q; pathfinish q = pathstart q; \<forall>t\<in>{0..1}. norm (q t - h t) < e\<rbrakk>
1.4793 +            \<Longrightarrow> homotopic_loops (-{z}) h q"
1.4794 +    using homotopic_nearby_loops [of g "-{z}"] homotopic_nearby_loops [of h "-{z}"] by force
1.4795 +  obtain p where p:
1.4796 +       "valid_path p" "z \<notin> path_image p"
1.4797 +       "pathstart p = pathstart g" "pathfinish p = pathfinish g"
1.4798 +       and gp_less:"\<forall>t\<in>{0..1}. cmod (g t - p t) < d"
1.4799 +       and pap: "contour_integral p (\<lambda>w. 1 / (w - z)) = complex_of_real (2 * pi) * \<i> * winding_number g z"
1.4800 +    using winding_number [OF \<open>path g\<close> pag \<open>0 < d\<close>] by blast
1.4801 +  obtain q where q:
1.4802 +       "valid_path q" "z \<notin> path_image q"
1.4803 +       "pathstart q = pathstart h" "pathfinish q = pathfinish h"
1.4804 +       and hq_less: "\<forall>t\<in>{0..1}. cmod (h t - q t) < e"
1.4805 +       and paq:  "contour_integral q (\<lambda>w. 1 / (w - z)) = complex_of_real (2 * pi) * \<i> * winding_number h z"
1.4806 +    using winding_number [OF \<open>path h\<close> pah \<open>0 < e\<close>] by blast
1.4807 +  have gp: "homotopic_loops (- {z}) g p"
1.4808 +    by (simp add: gloop d gp_less norm_minus_commute p valid_path_imp_path)
1.4809 +  have hq: "homotopic_loops (- {z}) h q"
1.4810 +    by (simp add: e hloop hq_less norm_minus_commute q valid_path_imp_path)
1.4811 +  have "contour_integral p (\<lambda>w. 1/(w - z)) = contour_integral q (\<lambda>w. 1/(w - z))"
1.4812 +    apply (rule Cauchy_theorem_homotopic_loops [of "-{z}"])
1.4813 +    apply (blast intro: homotopic_loops_trans homotopic_loops_sym gp hq assms)
1.4814 +    apply (auto intro!: holomorphic_intros simp: p q)
1.4815 +    done
1.4816 +  then show ?thesis
1.4817 +    by (simp add: pap paq)
1.4818 +qed
1.4819 +
1.4820 +lemma winding_number_paths_linear_eq:
1.4821 +  "\<lbrakk>path g; path h; pathstart h = pathstart g; pathfinish h = pathfinish g;
1.4822 +    \<And>t. t \<in> {0..1} \<Longrightarrow> z \<notin> closed_segment (g t) (h t)\<rbrakk>
1.4823 +        \<Longrightarrow> winding_number h z = winding_number g z"
1.4824 +  by (blast intro: sym homotopic_paths_linear winding_number_homotopic_paths elim: )
1.4825 +
1.4826 +lemma winding_number_loops_linear_eq:
1.4827 +  "\<lbrakk>path g; path h; pathfinish g = pathstart g; pathfinish h = pathstart h;
1.4828 +    \<And>t. t \<in> {0..1} \<Longrightarrow> z \<notin> closed_segment (g t) (h t)\<rbrakk>
1.4829 +        \<Longrightarrow> winding_number h z = winding_number g z"
1.4830 +  by (blast intro: sym homotopic_loops_linear winding_number_homotopic_loops elim: )
1.4831 +
1.4832 +lemma winding_number_nearby_paths_eq:
1.4833 +     "\<lbrakk>path g; path h;
1.4834 +      pathstart h = pathstart g; pathfinish h = pathfinish g;
1.4835 +      \<And>t. t \<in> {0..1} \<Longrightarrow> norm(h t - g t) < norm(g t - z)\<rbrakk>
1.4836 +      \<Longrightarrow> winding_number h z = winding_number g z"
1.4837 +  by (metis segment_bound(2) norm_minus_commute not_le winding_number_paths_linear_eq)
1.4838 +
1.4839 +lemma winding_number_nearby_loops_eq:
1.4840 +     "\<lbrakk>path g; path h;
1.4841 +      pathfinish g = pathstart g;
1.4842 +        pathfinish h = pathstart h;
1.4843 +      \<And>t. t \<in> {0..1} \<Longrightarrow> norm(h t - g t) < norm(g t - z)\<rbrakk>
1.4844 +      \<Longrightarrow> winding_number h z = winding_number g z"
1.4845 +  by (metis segment_bound(2) norm_minus_commute not_le winding_number_loops_linear_eq)
1.4846 +
1.4847 +
1.4848 +proposition winding_number_subpath_combine:
1.4849 +    "\<lbrakk>path g; z \<notin> path_image g;
1.4850 +      u \<in> {0..1}; v \<in> {0..1}; w \<in> {0..1}\<rbrakk>
1.4851 +      \<Longrightarrow> winding_number (subpath u v g) z + winding_number (subpath v w g) z =
1.4852 +          winding_number (subpath u w g) z"
1.4853 +apply (rule trans [OF winding_number_join [THEN sym]
1.4854 +                      winding_number_homotopic_paths [OF homotopic_join_subpaths]])
1.4855 +apply (auto dest: path_image_subpath_subset)
1.4856 +done
1.4857 +
1.4858 +
1.4859 +subsection\<open>Partial circle path\<close>
1.4860 +
1.4861 +definition part_circlepath :: "[complex, real, real, real, real] \<Rightarrow> complex"
1.4862 +  where "part_circlepath z r s t \<equiv> \<lambda>x. z + of_real r * exp (\<i> * of_real (linepath s t x))"
1.4863 +
1.4864 +lemma pathstart_part_circlepath [simp]:
1.4865 +     "pathstart(part_circlepath z r s t) = z + r*exp(\<i> * s)"
1.4866 +by (metis part_circlepath_def pathstart_def pathstart_linepath)
1.4867 +
1.4868 +lemma pathfinish_part_circlepath [simp]:
1.4869 +     "pathfinish(part_circlepath z r s t) = z + r*exp(\<i>*t)"
1.4870 +by (metis part_circlepath_def pathfinish_def pathfinish_linepath)
1.4871 +
1.4872 +proposition has_vector_derivative_part_circlepath [derivative_intros]:
1.4873 +    "((part_circlepath z r s t) has_vector_derivative
1.4874 +      (\<i> * r * (of_real t - of_real s) * exp(\<i> * linepath s t x)))
1.4875 +     (at x within X)"
1.4876 +  apply (simp add: part_circlepath_def linepath_def scaleR_conv_of_real)
1.4877 +  apply (rule has_vector_derivative_real_complex)
1.4878 +  apply (rule derivative_eq_intros | simp)+
1.4879 +  done
1.4880 +
1.4881 +corollary vector_derivative_part_circlepath:
1.4882 +    "vector_derivative (part_circlepath z r s t) (at x) =
1.4883 +       \<i> * r * (of_real t - of_real s) * exp(\<i> * linepath s t x)"
1.4884 +  using has_vector_derivative_part_circlepath vector_derivative_at by blast
1.4885 +
1.4886 +corollary vector_derivative_part_circlepath01:
1.4887 +    "\<lbrakk>0 \<le> x; x \<le> 1\<rbrakk>
1.4888 +     \<Longrightarrow> vector_derivative (part_circlepath z r s t) (at x within {0..1}) =
1.4889 +          \<i> * r * (of_real t - of_real s) * exp(\<i> * linepath s t x)"
1.4890 +  using has_vector_derivative_part_circlepath
1.4891 +  by (auto simp: vector_derivative_at_within_ivl)
1.4892 +
1.4893 +lemma valid_path_part_circlepath [simp]: "valid_path (part_circlepath z r s t)"
1.4894 +  apply (simp add: valid_path_def)
1.4895 +  apply (rule C1_differentiable_imp_piecewise)
1.4896 +  apply (auto simp: C1_differentiable_on_eq vector_derivative_works vector_derivative_part_circlepath has_vector_derivative_part_circlepath
1.4897 +              intro!: continuous_intros)
1.4898 +  done
1.4899 +
1.4900 +lemma path_part_circlepath [simp]: "path (part_circlepath z r s t)"
1.4901 +  by (simp add: valid_path_imp_path)
1.4902 +
1.4903 +proposition path_image_part_circlepath:
1.4904 +  assumes "s \<le> t"
1.4905 +    shows "path_image (part_circlepath z r s t) = {z + r * exp(\<i> * of_real x) | x. s \<le> x \<and> x \<le> t}"
1.4906 +proof -
1.4907 +  { fix z::real
1.4908 +    assume "0 \<le> z" "z \<le> 1"
1.4909 +    with \<open>s \<le> t\<close> have "\<exists>x. (exp (\<i> * linepath s t z) = exp (\<i> * of_real x)) \<and> s \<le> x \<and> x \<le> t"
1.4910 +      apply (rule_tac x="(1 - z) * s + z * t" in exI)
1.4911 +      apply (simp add: linepath_def scaleR_conv_of_real algebra_simps)
1.4912 +      apply (rule conjI)
1.4913 +      using mult_right_mono apply blast
1.4914 +      using affine_ineq  by (metis "mult.commute")
1.4915 +  }
1.4916 +  moreover
1.4917 +  { fix z
1.4918 +    assume "s \<le> z" "z \<le> t"
1.4919 +    then have "z + of_real r * exp (\<i> * of_real z) \<in> (\<lambda>x. z + of_real r * exp (\<i> * linepath s t x)) ` {0..1}"
1.4920 +      apply (rule_tac x="(z - s)/(t - s)" in image_eqI)
1.4921 +      apply (simp add: linepath_def scaleR_conv_of_real divide_simps exp_eq)
1.4922 +      apply (auto simp: algebra_simps divide_simps)
1.4923 +      done
1.4924 +  }
1.4925 +  ultimately show ?thesis
1.4926 +    by (fastforce simp add: path_image_def part_circlepath_def)
1.4927 +qed
1.4928 +
1.4929 +corollary path_image_part_circlepath_subset:
1.4930 +    "\<lbrakk>s \<le> t; 0 \<le> r\<rbrakk> \<Longrightarrow> path_image(part_circlepath z r s t) \<subseteq> sphere z r"
1.4931 +by (auto simp: path_image_part_circlepath sphere_def dist_norm algebra_simps norm_mult)
1.4932 +
1.4933 +proposition in_path_image_part_circlepath:
1.4934 +  assumes "w \<in> path_image(part_circlepath z r s t)" "s \<le> t" "0 \<le> r"
1.4935 +    shows "norm(w - z) = r"
1.4936 +proof -
1.4937 +  have "w \<in> {c. dist z c = r}"
1.4938 +    by (metis (no_types) path_image_part_circlepath_subset sphere_def subset_eq assms)
1.4939 +  thus ?thesis
1.4940 +    by (simp add: dist_norm norm_minus_commute)
1.4941 +qed
1.4942 +
1.4943 +proposition finite_bounded_log: "finite {z::complex. norm z \<le> b \<and> exp z = w}"
1.4944 +proof (cases "w = 0")
1.4945 +  case True then show ?thesis by auto
1.4946 +next
1.4947 +  case False
1.4948 +  have *: "finite {x. cmod (complex_of_real (2 * real_of_int x * pi) * \<i>) \<le> b + cmod (Ln w)}"
1.4949 +    apply (simp add: norm_mult finite_int_iff_bounded_le)
1.4950 +    apply (rule_tac x="\<lfloor>(b + cmod (Ln w)) / (2*pi)\<rfloor>" in exI)
1.4951 +    apply (auto simp: divide_simps le_floor_iff)
1.4952 +    done
1.4953 +  have [simp]: "\<And>P f. {z. P z \<and> (\<exists>n. z = f n)} = f ` {n. P (f n)}"
1.4954 +    by blast
1.4955 +  show ?thesis
1.4956 +    apply (subst exp_Ln [OF False, symmetric])
1.4957 +    apply (simp add: exp_eq)
1.4958 +    using norm_add_leD apply (fastforce intro: finite_subset [OF _ *])
1.4959 +    done
1.4960 +qed
1.4961 +
1.4962 +lemma finite_bounded_log2:
1.4963 +  fixes a::complex
1.4964 +    assumes "a \<noteq> 0"
1.4965 +    shows "finite {z. norm z \<le> b \<and> exp(a*z) = w}"
1.4966 +proof -
1.4967 +  have *: "finite ((\<lambda>z. z / a) ` {z. cmod z \<le> b * cmod a \<and> exp z = w})"
1.4968 +    by (rule finite_imageI [OF finite_bounded_log])
1.4969 +  show ?thesis
1.4970 +    by (rule finite_subset [OF _ *]) (force simp: assms norm_mult)
1.4971 +qed
1.4972 +
1.4973 +proposition has_contour_integral_bound_part_circlepath_strong:
1.4974 +  assumes fi: "(f has_contour_integral i) (part_circlepath z r s t)"
1.4975 +      and "finite k" and le: "0 \<le> B" "0 < r" "s \<le> t"
1.4976 +      and B: "\<And>x. x \<in> path_image(part_circlepath z r s t) - k \<Longrightarrow> norm(f x) \<le> B"
1.4977 +    shows "cmod i \<le> B * r * (t - s)"
1.4978 +proof -
1.4979 +  consider "s = t" | "s < t" using \<open>s \<le> t\<close> by linarith
1.4980 +  then show ?thesis
1.4981 +  proof cases
1.4982 +    case 1 with fi [unfolded has_contour_integral]
1.4983 +    have "i = 0"  by (simp add: vector_derivative_part_circlepath)
1.4984 +    with assms show ?thesis by simp
1.4985 +  next
1.4986 +    case 2
1.4987 +    have [simp]: "\<bar>r\<bar> = r" using \<open>r > 0\<close> by linarith
1.4988 +    have [simp]: "cmod (complex_of_real t - complex_of_real s) = t-s"
1.4989 +      by (metis "2" abs_of_pos diff_gt_0_iff_gt norm_of_real of_real_diff)
1.4990 +    have "finite (part_circlepath z r s t -` {y} \<inter> {0..1})" if "y \<in> k" for y
1.4991 +    proof -
1.4992 +      define w where "w = (y - z)/of_real r / exp(\<i> * of_real s)"
1.4993 +      have fin: "finite (of_real -` {z. cmod z \<le> 1 \<and> exp (\<i> * complex_of_real (t - s) * z) = w})"
1.4994 +        apply (rule finite_vimageI [OF finite_bounded_log2])
1.4995 +        using \<open>s < t\<close> apply (auto simp: inj_of_real)
1.4996 +        done
1.4997 +      show ?thesis
1.4998 +        apply (simp add: part_circlepath_def linepath_def vimage_def)
1.4999 +        apply (rule finite_subset [OF _ fin])
1.5000 +        using le
1.5001 +        apply (auto simp: w_def algebra_simps scaleR_conv_of_real exp_add exp_diff)
1.5002 +        done
1.5003 +    qed
1.5004 +    then have fin01: "finite ((part_circlepath z r s t) -` k \<inter> {0..1})"
1.5005 +      by (rule finite_finite_vimage_IntI [OF \<open>finite k\<close>])
1.5006 +    have **: "((\<lambda>x. if (part_circlepath z r s t x) \<in> k then 0
1.5007 +                    else f(part_circlepath z r s t x) *
1.5008 +                       vector_derivative (part_circlepath z r s t) (at x)) has_integral i)  {0..1}"
1.5009 +      apply (rule has_integral_spike
1.5010 +              [where f = "\<lambda>x. f(part_circlepath z r s t x) * vector_derivative (part_circlepath z r s t) (at x)"])
1.5011 +      apply (rule negligible_finite [OF fin01])
1.5012 +      using fi has_contour_integral
1.5013 +      apply auto
1.5014 +      done
1.5015 +    have *: "\<And>x. \<lbrakk>0 \<le> x; x \<le> 1; part_circlepath z r s t x \<notin> k\<rbrakk> \<Longrightarrow> cmod (f (part_circlepath z r s t x)) \<le> B"
1.5016 +      by (auto intro!: B [unfolded path_image_def image_def, simplified])
1.5017 +    show ?thesis
1.5018 +      apply (rule has_integral_bound [where 'a=real, simplified, OF _ **, simplified])
1.5019 +      using assms apply force
1.5020 +      apply (simp add: norm_mult vector_derivative_part_circlepath)
1.5021 +      using le * "2" \<open>r > 0\<close> by auto
1.5022 +  qed
1.5023 +qed
1.5024 +
1.5025 +corollary has_contour_integral_bound_part_circlepath:
1.5026 +      "\<lbrakk>(f has_contour_integral i) (part_circlepath z r s t);
1.5027 +        0 \<le> B; 0 < r; s \<le> t;
1.5028 +        \<And>x. x \<in> path_image(part_circlepath z r s t) \<Longrightarrow> norm(f x) \<le> B\<rbrakk>
1.5029 +       \<Longrightarrow> norm i \<le> B*r*(t - s)"
1.5030 +  by (auto intro: has_contour_integral_bound_part_circlepath_strong)
1.5031 +
1.5032 +proposition contour_integrable_continuous_part_circlepath:
1.5033 +     "continuous_on (path_image (part_circlepath z r s t)) f
1.5034 +      \<Longrightarrow> f contour_integrable_on (part_circlepath z r s t)"
1.5035 +  apply (simp add: contour_integrable_on has_contour_integral_def vector_derivative_part_circlepath path_image_def)
1.5036 +  apply (rule integrable_continuous_real)
1.5037 +  apply (fast intro: path_part_circlepath [unfolded path_def] continuous_intros continuous_on_compose2 [where g=f, OF _ _ order_refl])
1.5038 +  done
1.5039 +
1.5040 +proposition winding_number_part_circlepath_pos_less:
1.5041 +  assumes "s < t" and no: "norm(w - z) < r"
1.5042 +    shows "0 < Re (winding_number(part_circlepath z r s t) w)"
1.5043 +proof -
1.5044 +  have "0 < r" by (meson no norm_not_less_zero not_le order.strict_trans2)
1.5045 +  note valid_path_part_circlepath
1.5046 +  moreover have " w \<notin> path_image (part_circlepath z r s t)"
1.5047 +    using assms by (auto simp: path_image_def image_def part_circlepath_def norm_mult linepath_def)
1.5048 +  moreover have "0 < r * (t - s) * (r - cmod (w - z))"
1.5049 +    using assms by (metis \<open>0 < r\<close> diff_gt_0_iff_gt mult_pos_pos)
1.5050 +  ultimately show ?thesis
1.5051 +    apply (rule winding_number_pos_lt [where e = "r*(t - s)*(r - norm(w - z))"])
1.5052 +    apply (simp add: vector_derivative_part_circlepath right_diff_distrib [symmetric] mult_ac)
1.5053 +    apply (rule mult_left_mono)+
1.5054 +    using Re_Im_le_cmod [of "w-z" "linepath s t x" for x]
1.5055 +    apply (simp add: exp_Euler cos_of_real sin_of_real part_circlepath_def algebra_simps cos_squared_eq [unfolded power2_eq_square])
1.5056 +    using assms \<open>0 < r\<close> by auto
1.5057 +qed
1.5058 +
1.5059 +proposition simple_path_part_circlepath:
1.5060 +    "simple_path(part_circlepath z r s t) \<longleftrightarrow> (r \<noteq> 0 \<and> s \<noteq> t \<and> \<bar>s - t\<bar> \<le> 2*pi)"
1.5061 +proof (cases "r = 0 \<or> s = t")
1.5062 +  case True
1.5063 +  then show ?thesis
1.5064 +    apply (rule disjE)
1.5065 +    apply (force simp: part_circlepath_def simple_path_def intro: bexI [where x = "1/4"] bexI [where x = "1/3"])+
1.5066 +    done
1.5067 +next
1.5068 +  case False then have "r \<noteq> 0" "s \<noteq> t" by auto
1.5069 +  have *: "\<And>x y z s t. \<i>*((1 - x) * s + x * t) = \<i>*(((1 - y) * s + y * t)) + z  \<longleftrightarrow> \<i>*(x - y) * (t - s) = z"
1.5070 +    by (simp add: algebra_simps)
1.5071 +  have abs01: "\<And>x y::real. 0 \<le> x \<and> x \<le> 1 \<and> 0 \<le> y \<and> y \<le> 1
1.5072 +                      \<Longrightarrow> (x = y \<or> x = 0 \<and> y = 1 \<or> x = 1 \<and> y = 0 \<longleftrightarrow> \<bar>x - y\<bar> \<in> {0,1})"
1.5073 +    by auto
1.5074 +  have abs_away: "\<And>P. (\<forall>x\<in>{0..1}. \<forall>y\<in>{0..1}. P \<bar>x - y\<bar>) \<longleftrightarrow> (\<forall>x::real. 0 \<le> x \<and> x \<le> 1 \<longrightarrow> P x)"
1.5075 +    by force
1.5076 +  have **: "\<And>x y. (\<exists>n. (complex_of_real x - of_real y) * (of_real t - of_real s) = 2 * (of_int n * of_real pi)) \<longleftrightarrow>
1.5077 +                  (\<exists>n. \<bar>x - y\<bar> * (t - s) = 2 * (of_int n * pi))"
1.5078 +    by (force simp: algebra_simps abs_if dest: arg_cong [where f=Re] arg_cong [where f=complex_of_real]
1.5079 +                    intro: exI [where x = "-n" for n])
1.5080 +  have 1: "\<forall>x. 0 \<le> x \<and> x \<le> 1 \<longrightarrow> (\<exists>n. x * (t - s) = 2 * (real_of_int n * pi)) \<longrightarrow> x = 0 \<or> x = 1 \<Longrightarrow> \<bar>s - t\<bar> \<le> 2 * pi"
1.5081 +    apply (rule ccontr)
1.5082 +    apply (drule_tac x="2*pi / \<bar>t - s\<bar>" in spec)
1.5083 +    using False
1.5084 +    apply (simp add: abs_minus_commute divide_simps)
1.5085 +    apply (frule_tac x=1 in spec)
1.5086 +    apply (drule_tac x="-1" in spec)
1.5088 +    done
1.5089 +  have 2: "\<bar>s - t\<bar> = \<bar>2 * (real_of_int n * pi) / x\<bar>" if "x \<noteq> 0" "x * (t - s) = 2 * (real_of_int n * pi)" for x n
1.5090 +  proof -
1.5091 +    have "t-s = 2 * (real_of_int n * pi)/x"
1.5092 +      using that by (simp add: field_simps)
1.5093 +    then show ?thesis by (metis abs_minus_commute)
1.5094 +  qed
1.5095 +  show ?thesis using False
1.5096 +    apply (simp add: simple_path_def path_part_circlepath)
1.5097 +    apply (simp add: part_circlepath_def linepath_def exp_eq  * ** abs01  del: Set.insert_iff)
1.5098 +    apply (subst abs_away)
1.5099 +    apply (auto simp: 1)
1.5100 +    apply (rule ccontr)
1.5101 +    apply (auto simp: 2 divide_simps abs_mult dest: of_int_leD)
1.5102 +    done
1.5103 +qed
1.5104 +
1.5105 +proposition arc_part_circlepath:
1.5106 +  assumes "r \<noteq> 0" "s \<noteq> t" "\<bar>s - t\<bar> < 2*pi"
1.5107 +    shows "arc (part_circlepath z r s t)"
1.5108 +proof -
1.5109 +  have *: "x = y" if eq: "\<i> * (linepath s t x) = \<i> * (linepath s t y) + 2 * of_int n * complex_of_real pi * \<i>"
1.5110 +                  and x: "x \<in> {0..1}" and y: "y \<in> {0..1}" for x y n
1.5111 +    proof -
1.5112 +      have "(linepath s t x) = (linepath s t y) + 2 * of_int n * complex_of_real pi"
1.5113 +        by (metis add_divide_eq_iff complex_i_not_zero mult.commute nonzero_mult_divide_cancel_left eq)
1.5114 +      then have "s*y + t*x = s*x + (t*y + of_int n * (pi * 2))"
1.5115 +        by (force simp: algebra_simps linepath_def dest: arg_cong [where f=Re])
1.5116 +      then have st: "x \<noteq> y \<Longrightarrow> (s-t) = (of_int n * (pi * 2) / (y-x))"
1.5117 +        by (force simp: field_simps)
1.5118 +      show ?thesis
1.5119 +        apply (rule ccontr)
1.5120 +        using assms x y
1.5121 +        apply (simp add: st abs_mult field_simps)
1.5122 +        using st
1.5123 +        apply (auto simp: dest: of_int_lessD)
1.5124 +        done
1.5125 +    qed
1.5126 +  show ?thesis
1.5127 +    using assms
1.5128 +    apply (simp add: arc_def)
1.5129 +    apply (simp add: part_circlepath_def inj_on_def exp_eq)
1.5130 +    apply (blast intro: *)
1.5131 +    done
1.5132 +qed
1.5133 +
1.5134 +
1.5135 +subsection\<open>Special case of one complete circle\<close>
1.5136 +
1.5137 +definition circlepath :: "[complex, real, real] \<Rightarrow> complex"
1.5138 +  where "circlepath z r \<equiv> part_circlepath z r 0 (2*pi)"
1.5139 +
1.5140 +lemma circlepath: "circlepath z r = (\<lambda>x. z + r * exp(2 * of_real pi * \<i> * of_real x))"
1.5141 +  by (simp add: circlepath_def part_circlepath_def linepath_def algebra_simps)
1.5142 +
1.5143 +lemma pathstart_circlepath [simp]: "pathstart (circlepath z r) = z + r"
1.5144 +  by (simp add: circlepath_def)
1.5145 +
1.5146 +lemma pathfinish_circlepath [simp]: "pathfinish (circlepath z r) = z + r"
1.5147 +  by (simp add: circlepath_def) (metis exp_two_pi_i mult.commute)
1.5148 +
1.5149 +lemma circlepath_minus: "circlepath z (-r) x = circlepath z r (x + 1/2)"
1.5150 +proof -
1.5151 +  have "z + of_real r * exp (2 * pi * \<i> * (x + 1 / 2)) =
1.5152 +        z + of_real r * exp (2 * pi * \<i> * x + pi * \<i>)"
1.5154 +  also have "... = z - r * exp (2 * pi * \<i> * x)"
1.5156 +  finally show ?thesis
1.5157 +    by (simp add: circlepath path_image_def sphere_def dist_norm)
1.5158 +qed
1.5159 +
1.5160 +lemma circlepath_add1: "circlepath z r (x+1) = circlepath z r x"
1.5161 +  using circlepath_minus [of z r "x+1/2"] circlepath_minus [of z "-r" x]
1.5163 +
1.5164 +lemma circlepath_add_half: "circlepath z r (x + 1/2) = circlepath z r (x - 1/2)"
1.5165 +  using circlepath_add1 [of z r "x-1/2"]
1.5167 +
1.5168 +lemma path_image_circlepath_minus_subset:
1.5169 +     "path_image (circlepath z (-r)) \<subseteq> path_image (circlepath z r)"
1.5170 +  apply (simp add: path_image_def image_def circlepath_minus, clarify)
1.5171 +  apply (case_tac "xa \<le> 1/2", force)
1.5173 +  done
1.5174 +
1.5175 +lemma path_image_circlepath_minus: "path_image (circlepath z (-r)) = path_image (circlepath z r)"
1.5176 +  using path_image_circlepath_minus_subset by fastforce
1.5177 +
1.5178 +proposition has_vector_derivative_circlepath [derivative_intros]:
1.5179 + "((circlepath z r) has_vector_derivative (2 * pi * \<i> * r * exp (2 * of_real pi * \<i> * of_real x)))
1.5180 +   (at x within X)"
1.5181 +  apply (simp add: circlepath_def scaleR_conv_of_real)
1.5182 +  apply (rule derivative_eq_intros)
1.5183 +  apply (simp add: algebra_simps)
1.5184 +  done
1.5185 +
1.5186 +corollary vector_derivative_circlepath:
1.5187 +   "vector_derivative (circlepath z r) (at x) =
1.5188 +    2 * pi * \<i> * r * exp(2 * of_real pi * \<i> * x)"
1.5189 +using has_vector_derivative_circlepath vector_derivative_at by blast
1.5190 +
1.5191 +corollary vector_derivative_circlepath01:
1.5192 +    "\<lbrakk>0 \<le> x; x \<le> 1\<rbrakk>
1.5193 +     \<Longrightarrow> vector_derivative (circlepath z r) (at x within {0..1}) =
1.5194 +          2 * pi * \<i> * r * exp(2 * of_real pi * \<i> * x)"
1.5195 +  using has_vector_derivative_circlepath
1.5196 +  by (auto simp: vector_derivative_at_within_ivl)
1.5197 +
1.5198 +lemma valid_path_circlepath [simp]: "valid_path (circlepath z r)"
1.5199 +  by (simp add: circlepath_def)
1.5200 +
1.5201 +lemma path_circlepath [simp]: "path (circlepath z r)"
1.5202 +  by (simp add: valid_path_imp_path)
1.5203 +
1.5204 +lemma path_image_circlepath_nonneg:
1.5205 +  assumes "0 \<le> r" shows "path_image (circlepath z r) = sphere z r"
1.5206 +proof -
1.5207 +  have *: "x \<in> (\<lambda>u. z + (cmod (x - z)) * exp (\<i> * (of_real u * (of_real pi * 2)))) ` {0..1}" for x
1.5208 +  proof (cases "x = z")
1.5209 +    case True then show ?thesis by force
1.5210 +  next
1.5211 +    case False
1.5212 +    define w where "w = x - z"
1.5213 +    then have "w \<noteq> 0" by (simp add: False)
1.5214 +    have **: "\<And>t. \<lbrakk>Re w = cos t * cmod w; Im w = sin t * cmod w\<rbrakk> \<Longrightarrow> w = of_real (cmod w) * exp (\<i> * t)"
1.5215 +      using cis_conv_exp complex_eq_iff by auto
1.5216 +    show ?thesis
1.5217 +      apply (rule sincos_total_2pi [of "Re(w/of_real(norm w))" "Im(w/of_real(norm w))"])
1.5218 +      apply (simp add: divide_simps \<open>w \<noteq> 0\<close> cmod_power2 [symmetric])
1.5219 +      apply (rule_tac x="t / (2*pi)" in image_eqI)
1.5220 +      apply (simp add: divide_simps \<open>w \<noteq> 0\<close>)
1.5221 +      using False **
1.5222 +      apply (auto simp: w_def)
1.5223 +      done
1.5224 +  qed
1.5225 +  show ?thesis
1.5226 +    unfolding circlepath path_image_def sphere_def dist_norm
1.5227 +    by (force simp: assms algebra_simps norm_mult norm_minus_commute intro: *)
1.5228 +qed
1.5229 +
1.5230 +proposition path_image_circlepath [simp]:
1.5231 +    "path_image (circlepath z r) = sphere z \<bar>r\<bar>"
1.5232 +  using path_image_circlepath_minus
1.5233 +  by (force simp add: path_image_circlepath_nonneg abs_if)
1.5234 +
1.5235 +lemma has_contour_integral_bound_circlepath_strong:
1.5236 +      "\<lbrakk>(f has_contour_integral i) (circlepath z r);
1.5237 +        finite k; 0 \<le> B; 0 < r;
1.5238 +        \<And>x. \<lbrakk>norm(x - z) = r; x \<notin> k\<rbrakk> \<Longrightarrow> norm(f x) \<le> B\<rbrakk>
1.5239 +        \<Longrightarrow> norm i \<le> B*(2*pi*r)"
1.5240 +  unfolding circlepath_def
1.5241 +  by (auto simp: algebra_simps in_path_image_part_circlepath dest!: has_contour_integral_bound_part_circlepath_strong)
1.5242 +
1.5243 +corollary has_contour_integral_bound_circlepath:
1.5244 +      "\<lbrakk>(f has_contour_integral i) (circlepath z r);
1.5245 +        0 \<le> B; 0 < r; \<And>x. norm(x - z) = r \<Longrightarrow> norm(f x) \<le> B\<rbrakk>
1.5246 +        \<Longrightarrow> norm i \<le> B*(2*pi*r)"
1.5247 +  by (auto intro: has_contour_integral_bound_circlepath_strong)
1.5248 +
1.5249 +proposition contour_integrable_continuous_circlepath:
1.5250 +    "continuous_on (path_image (circlepath z r)) f
1.5251 +     \<Longrightarrow> f contour_integrable_on (circlepath z r)"
1.5252 +  by (simp add: circlepath_def contour_integrable_continuous_part_circlepath)
1.5253 +
1.5254 +lemma simple_path_circlepath: "simple_path(circlepath z r) \<longleftrightarrow> (r \<noteq> 0)"
1.5255 +  by (simp add: circlepath_def simple_path_part_circlepath)
1.5256 +
1.5257 +lemma notin_path_image_circlepath [simp]: "cmod (w - z) < r \<Longrightarrow> w \<notin> path_image (circlepath z r)"
1.5258 +  by (simp add: sphere_def dist_norm norm_minus_commute)
1.5259 +
1.5260 +proposition contour_integral_circlepath:
1.5261 +     "0 < r \<Longrightarrow> contour_integral (circlepath z r) (\<lambda>w. 1 / (w - z)) = 2 * complex_of_real pi * \<i>"
1.5262 +  apply (rule contour_integral_unique)
1.5263 +  apply (simp add: has_contour_integral_def)
1.5264 +  apply (subst has_integral_cong)
1.5265 +  apply (simp add: vector_derivative_circlepath01)
1.5266 +  using has_integral_const_real [of _ 0 1]
1.5267 +  apply (force simp: circlepath)
1.5268 +  done
1.5269 +
1.5270 +lemma winding_number_circlepath_centre: "0 < r \<Longrightarrow> winding_number (circlepath z r) z = 1"
1.5271 +  apply (rule winding_number_unique_loop)
1.5272 +  apply (simp_all add: sphere_def valid_path_imp_path)
1.5273 +  apply (rule_tac x="circlepath z r" in exI)
1.5274 +  apply (simp add: sphere_def contour_integral_circlepath)
1.5275 +  done
1.5276 +
1.5277 +proposition winding_number_circlepath:
1.5278 +  assumes "norm(w - z) < r" shows "winding_number(circlepath z r) w = 1"
1.5279 +proof (cases "w = z")
1.5280 +  case True then show ?thesis
1.5281 +    using assms winding_number_circlepath_centre by auto
1.5282 +next
1.5283 +  case False
1.5284 +  have [simp]: "r > 0"
1.5285 +    using assms le_less_trans norm_ge_zero by blast
1.5286 +  define r' where "r' = norm(w - z)"
1.5287 +  have "r' < r"
1.5288 +    by (simp add: assms r'_def)
1.5289 +  have disjo: "cball z r' \<inter> sphere z r = {}"
1.5290 +    using \<open>r' < r\<close> by (force simp: cball_def sphere_def)
1.5291 +  have "winding_number(circlepath z r) w = winding_number(circlepath z r) z"
1.5292 +    apply (rule winding_number_around_inside [where s = "cball z r'"])
1.5293 +    apply (simp_all add: disjo order.strict_implies_order winding_number_circlepath_centre)
1.5294 +    apply (simp_all add: False r'_def dist_norm norm_minus_commute)
1.5295 +    done
1.5296 +  also have "... = 1"
1.5297 +    by (simp add: winding_number_circlepath_centre)
1.5298 +  finally show ?thesis .
1.5299 +qed
1.5300 +
1.5301 +
1.5302 +text\<open> Hence the Cauchy formula for points inside a circle.\<close>
1.5303 +
1.5304 +theorem Cauchy_integral_circlepath:
1.5305 +  assumes "continuous_on (cball z r) f" "f holomorphic_on (ball z r)" "norm(w - z) < r"
1.5306 +  shows "((\<lambda>u. f u/(u - w)) has_contour_integral (2 * of_real pi * \<i> * f w))
1.5307 +         (circlepath z r)"
1.5308 +proof -
1.5309 +  have "r > 0"
1.5310 +    using assms le_less_trans norm_ge_zero by blast
1.5311 +  have "((\<lambda>u. f u / (u - w)) has_contour_integral (2 * pi) * \<i> * winding_number (circlepath z r) w * f w)
1.5312 +        (circlepath z r)"
1.5313 +    apply (rule Cauchy_integral_formula_weak [where s = "cball z r" and k = "{}"])
1.5314 +    using assms  \<open>r > 0\<close>
1.5315 +    apply (simp_all add: dist_norm norm_minus_commute)
1.5316 +    apply (metis at_within_interior dist_norm holomorphic_on_def interior_ball mem_ball norm_minus_commute)
1.5317 +    apply (simp add: cball_def sphere_def dist_norm, clarify)
1.5319 +    by (metis dist_commute dist_norm less_irrefl)
1.5320 +  then show ?thesis
1.5321 +    by (simp add: winding_number_circlepath assms)
1.5322 +qed
1.5323 +
1.5324 +corollary Cauchy_integral_circlepath_simple:
1.5325 +  assumes "f holomorphic_on cball z r" "norm(w - z) < r"
1.5326 +  shows "((\<lambda>u. f u/(u - w)) has_contour_integral (2 * of_real pi * \<i> * f w))
1.5327 +         (circlepath z r)"
1.5328 +using assms by (force simp: holomorphic_on_imp_continuous_on holomorphic_on_subset Cauchy_integral_circlepath)
1.5329 +
1.5330 +
1.5331 +lemma no_bounded_connected_component_imp_winding_number_zero:
1.5332 +  assumes g: "path g" "path_image g \<subseteq> s" "pathfinish g = pathstart g" "z \<notin> s"
1.5333 +      and nb: "\<And>z. bounded (connected_component_set (- s) z) \<longrightarrow> z \<in> s"
1.5334 +  shows "winding_number g z = 0"
1.5335 +apply (rule winding_number_zero_in_outside)
1.5337 +by (metis nb [of z] \<open>path_image g \<subseteq> s\<close> \<open>z \<notin> s\<close> contra_subsetD mem_Collect_eq outside outside_mono)
1.5338 +
1.5339 +lemma no_bounded_path_component_imp_winding_number_zero:
1.5340 +  assumes g: "path g" "path_image g \<subseteq> s" "pathfinish g = pathstart g" "z \<notin> s"
1.5341 +      and nb: "\<And>z. bounded (path_component_set (- s) z) \<longrightarrow> z \<in> s"
1.5342 +  shows "winding_number g z = 0"
1.5343 +apply (rule no_bounded_connected_component_imp_winding_number_zero [OF g])
1.5344 +by (simp add: bounded_subset nb path_component_subset_connected_component)
1.5345 +
1.5346 +
1.5347 +subsection\<open> Uniform convergence of path integral\<close>
1.5348 +
1.5349 +text\<open>Uniform convergence when the derivative of the path is bounded, and in particular for the special case of a circle.\<close>
1.5350 +
1.5351 +proposition contour_integral_uniform_limit:
1.5352 +  assumes ev_fint: "eventually (\<lambda>n::'a. (f n) contour_integrable_on \<gamma>) F"
1.5353 +      and ev_no: "\<And>e. 0 < e \<Longrightarrow> eventually (\<lambda>n. \<forall>x \<in> path_image \<gamma>. norm(f n x - l x) < e) F"
1.5354 +      and noleB: "\<And>t. t \<in> {0..1} \<Longrightarrow> norm (vector_derivative \<gamma> (at t)) \<le> B"
1.5355 +      and \<gamma>: "valid_path \<gamma>"
1.5356 +      and [simp]: "~ (trivial_limit F)"
1.5357 +  shows "l contour_integrable_on \<gamma>" "((\<lambda>n. contour_integral \<gamma> (f n)) \<longlongrightarrow> contour_integral \<gamma> l) F"
1.5358 +proof -
1.5359 +  have "0 \<le> B" by (meson noleB [of 0] atLeastAtMost_iff norm_ge_zero order_refl order_trans zero_le_one)
1.5360 +  { fix e::real
1.5361 +    assume "0 < e"
1.5362 +    then have eB: "0 < e / (\<bar>B\<bar> + 1)" by simp
1.5363 +    obtain a where fga: "\<And>x. x \<in> {0..1} \<Longrightarrow> cmod (f a (\<gamma> x) - l (\<gamma> x)) < e / (\<bar>B\<bar> + 1)"
1.5364 +               and inta: "(\<lambda>t. f a (\<gamma> t) * vector_derivative \<gamma> (at t)) integrable_on {0..1}"
1.5365 +      using eventually_happens [OF eventually_conj [OF ev_no [OF eB] ev_fint]]
1.5366 +      by (fastforce simp: contour_integrable_on path_image_def)
1.5367 +    have Ble: "B * e / (\<bar>B\<bar> + 1) \<le> e"
1.5368 +      using \<open>0 \<le> B\<close>  \<open>0 < e\<close> by (simp add: divide_simps)
1.5369 +    have "\<exists>h. (\<forall>x\<in>{0..1}. cmod (l (\<gamma> x) * vector_derivative \<gamma> (at x) - h x) \<le> e) \<and> h integrable_on {0..1}"
1.5370 +      apply (rule_tac x="\<lambda>x. f (a::'a) (\<gamma> x) * vector_derivative \<gamma> (at x)" in exI)
1.5371 +      apply (intro inta conjI ballI)
1.5372 +      apply (rule order_trans [OF _ Ble])
1.5373 +      apply (frule noleB)
1.5374 +      apply (frule fga)
1.5375 +      using \<open>0 \<le> B\<close>  \<open>0 < e\<close>
1.5376 +      apply (simp add: norm_mult left_diff_distrib [symmetric] norm_minus_commute divide_simps)
1.5377 +      apply (drule (1) mult_mono [OF less_imp_le])
1.5378 +      apply (simp_all add: mult_ac)
1.5379 +      done
1.5380 +  }
1.5381 +  then show lintg: "l contour_integrable_on \<gamma>"
1.5382 +    apply (simp add: contour_integrable_on)
1.5383 +    apply (blast intro: integrable_uniform_limit_real)
1.5384 +    done
1.5385 +  { fix e::real
1.5386 +    define B' where "B' = B + 1"
1.5387 +    have B': "B' > 0" "B' > B" using  \<open>0 \<le> B\<close> by (auto simp: B'_def)
1.5388 +    assume "0 < e"
1.5389 +    then have ev_no': "\<forall>\<^sub>F n in F. \<forall>x\<in>path_image \<gamma>. 2 * cmod (f n x - l x) < e / B'"
1.5390 +      using ev_no [of "e / B' / 2"] B' by (simp add: field_simps)
1.5391 +    have ie: "integral {0..1::real} (\<lambda>x. e / 2) < e" using \<open>0 < e\<close> by simp
1.5392 +    have *: "cmod (f x (\<gamma> t) * vector_derivative \<gamma> (at t) - l (\<gamma> t) * vector_derivative \<gamma> (at t)) \<le> e / 2"
1.5393 +             if t: "t\<in>{0..1}" and leB': "2 * cmod (f x (\<gamma> t) - l (\<gamma> t)) < e / B'" for x t
1.5394 +    proof -
1.5395 +      have "2 * cmod (f x (\<gamma> t) - l (\<gamma> t)) * cmod (vector_derivative \<gamma> (at t)) \<le> e * (B/ B')"
1.5396 +        using mult_mono [OF less_imp_le [OF leB'] noleB] B' \<open>0 < e\<close> t by auto
1.5397 +      also have "... < e"
1.5398 +        by (simp add: B' \<open>0 < e\<close> mult_imp_div_pos_less)
1.5399 +      finally have "2 * cmod (f x (\<gamma> t) - l (\<gamma> t)) * cmod (vector_derivative \<gamma> (at t)) < e" .
1.5400 +      then show ?thesis
1.5401 +        by (simp add: left_diff_distrib [symmetric] norm_mult)
1.5402 +    qed
1.5403 +    have "\<forall>\<^sub>F x in F. dist (contour_integral \<gamma> (f x)) (contour_integral \<gamma> l) < e"
1.5404 +      apply (rule eventually_mono [OF eventually_conj [OF ev_no' ev_fint]])
1.5405 +      apply (simp add: dist_norm contour_integrable_on path_image_def contour_integral_integral)
1.5406 +      apply (simp add: lintg integral_diff [symmetric] contour_integrable_on [symmetric], clarify)
1.5407 +      apply (rule le_less_trans [OF integral_norm_bound_integral ie])
1.5408 +      apply (simp add: lintg integrable_diff contour_integrable_on [symmetric])
1.5409 +      apply (blast intro: *)+
1.5410 +      done
1.5411 +  }
1.5412 +  then show "((\<lambda>n. contour_integral \<gamma> (f n)) \<longlongrightarrow> contour_integral \<gamma> l) F"
1.5413 +    by (rule tendstoI)
1.5414 +qed
1.5415 +
1.5416 +proposition contour_integral_uniform_limit_circlepath:
1.5417 +  assumes ev_fint: "eventually (\<lambda>n::'a. (f n) contour_integrable_on (circlepath z r)) F"
1.5418 +      and ev_no: "\<And>e. 0 < e \<Longrightarrow> eventually (\<lambda>n. \<forall>x \<in> path_image (circlepath z r). norm(f n x - l x) < e) F"
1.5419 +      and [simp]: "~ (trivial_limit F)" "0 < r"
1.5420 +  shows "l contour_integrable_on (circlepath z r)" "((\<lambda>n. contour_integral (circlepath z r) (f n)) \<longlongrightarrow> contour_integral (circlepath z r) l) F"
1.5421 +by (auto simp: vector_derivative_circlepath norm_mult intro: contour_integral_uniform_limit assms)
1.5422 +
1.5423 +
1.5424 +subsection\<open> General stepping result for derivative formulas.\<close>
1.5425 +
1.5426 +lemma sum_sqs_eq:
1.5427 +  fixes x::"'a::idom" shows "x * x + y * y = x * (y * 2) \<Longrightarrow> y = x"
1.5428 +  by algebra
1.5429 +
1.5430 +proposition Cauchy_next_derivative:
1.5431 +  assumes "continuous_on (path_image \<gamma>) f'"
1.5432 +      and leB: "\<And>t. t \<in> {0..1} \<Longrightarrow> norm (vector_derivative \<gamma> (at t)) \<le> B"
1.5433 +      and int: "\<And>w. w \<in> s - path_image \<gamma> \<Longrightarrow> ((\<lambda>u. f' u / (u - w)^k) has_contour_integral f w) \<gamma>"
1.5434 +      and k: "k \<noteq> 0"
1.5435 +      and "open s"
1.5436 +      and \<gamma>: "valid_path \<gamma>"
1.5437 +      and w: "w \<in> s - path_image \<gamma>"
1.5438 +    shows "(\<lambda>u. f' u / (u - w)^(Suc k)) contour_integrable_on \<gamma>"
1.5439 +      and "(f has_field_derivative (k * contour_integral \<gamma> (\<lambda>u. f' u/(u - w)^(Suc k))))
1.5440 +           (at w)"  (is "?thes2")
1.5441 +proof -
1.5442 +  have "open (s - path_image \<gamma>)" using \<open>open s\<close> closed_valid_path_image \<gamma> by blast
1.5443 +  then obtain d where "d>0" and d: "ball w d \<subseteq> s - path_image \<gamma>" using w
1.5444 +    using open_contains_ball by blast
1.5445 +  have [simp]: "\<And>n. cmod (1 + of_nat n) = 1 + of_nat n"
1.5446 +    by (metis norm_of_nat of_nat_Suc)
1.5447 +  have 1: "\<forall>\<^sub>F n in at w. (\<lambda>x. f' x * (inverse (x - n) ^ k - inverse (x - w) ^ k) / (n - w) / of_nat k)
1.5448 +                         contour_integrable_on \<gamma>"
1.5449 +    apply (simp add: eventually_at)
1.5450 +    apply (rule_tac x=d in exI)
1.5451 +    apply (simp add: \<open>d > 0\<close> dist_norm field_simps, clarify)
1.5452 +    apply (rule contour_integrable_div [OF contour_integrable_diff])
1.5453 +    using int w d
1.5454 +    apply (force simp:  dist_norm norm_minus_commute intro!: has_contour_integral_integrable)+
1.5455 +    done
1.5456 +  have bim_g: "bounded (image f' (path_image \<gamma>))"
1.5457 +    by (simp add: compact_imp_bounded compact_continuous_image compact_valid_path_image assms)
1.5458 +  then obtain C where "C > 0" and C: "\<And>x. \<lbrakk>0 \<le> x; x \<le> 1\<rbrakk> \<Longrightarrow> cmod (f' (\<gamma> x)) \<le> C"
1.5459 +    by (force simp: bounded_pos path_image_def)
1.5460 +  have twom: "\<forall>\<^sub>F n in at w.
1.5461 +               \<forall>x\<in>path_image \<gamma>.
1.5462 +                cmod ((inverse (x - n) ^ k - inverse (x - w) ^ k) / (n - w) / k - inverse (x - w) ^ Suc k) < e"
1.5463 +         if "0 < e" for e
1.5464 +  proof -
1.5465 +    have *: "cmod ((inverse (x - u) ^ k - inverse (x - w) ^ k) / ((u - w) * k) - inverse (x - w) ^ Suc k)   < e"
1.5466 +            if x: "x \<in> path_image \<gamma>" and "u \<noteq> w" and uwd: "cmod (u - w) < d/2"
1.5467 +                and uw_less: "cmod (u - w) < e * (d / 2) ^ (k+2) / (1 + real k)"
1.5468 +            for u x
1.5469 +    proof -
1.5470 +      define ff where [abs_def]:
1.5471 +        "ff n w =
1.5472 +          (if n = 0 then inverse(x - w)^k
1.5473 +           else if n = 1 then k / (x - w)^(Suc k)
1.5474 +           else (k * of_real(Suc k)) / (x - w)^(k + 2))" for n :: nat and w
1.5475 +      have km1: "\<And>z::complex. z \<noteq> 0 \<Longrightarrow> z ^ (k - Suc 0) = z ^ k / z"
1.5476 +        by (simp add: field_simps) (metis Suc_pred \<open>k \<noteq> 0\<close> neq0_conv power_Suc)
1.5477 +      have ff1: "(ff i has_field_derivative ff (Suc i) z) (at z within ball w (d / 2))"
1.5478 +              if "z \<in> ball w (d / 2)" "i \<le> 1" for i z
1.5479 +      proof -
1.5480 +        have "z \<notin> path_image \<gamma>"
1.5481 +          using \<open>x \<in> path_image \<gamma>\<close> d that ball_divide_subset_numeral by blast
1.5482 +        then have xz[simp]: "x \<noteq> z" using \<open>x \<in> path_image \<gamma>\<close> by blast
1.5483 +        then have neq: "x * x + z * z \<noteq> x * (z * 2)"
1.5484 +          by (blast intro: dest!: sum_sqs_eq)
1.5485 +        with xz have "\<And>v. v \<noteq> 0 \<Longrightarrow> (x * x + z * z) * v \<noteq> (x * (z * 2) * v)" by auto
1.5486 +        then have neqq: "\<And>v. v \<noteq> 0 \<Longrightarrow> x * (x * v) + z * (z * v) \<noteq> x * (z * (2 * v))"
1.5487 +          by (simp add: algebra_simps)
1.5488 +        show ?thesis using \<open>i \<le> 1\<close>
1.5489 +          apply (simp add: ff_def dist_norm Nat.le_Suc_eq km1, safe)
1.5490 +          apply (rule derivative_eq_intros | simp add: km1 | simp add: field_simps neq neqq)+
1.5491 +          done
1.5492 +      qed
1.5493 +      { fix a::real and b::real assume ab: "a > 0" "b > 0"
1.5494 +        then have "k * (1 + real k) * (1 / a) \<le> k * (1 + real k) * (4 / b) \<longleftrightarrow> b \<le> 4 * a"
1.5495 +          apply (subst mult_le_cancel_left_pos)
1.5496 +          using \<open>k \<noteq> 0\<close>
1.5497 +          apply (auto simp: divide_simps)
1.5498 +          done
1.5499 +        with ab have "real k * (1 + real k) / a \<le> (real k * 4 + real k * real k * 4) / b \<longleftrightarrow> b \<le> 4 * a"
1.5500 +          by (simp add: field_simps)
1.5501 +      } note canc = this
1.5502 +      have ff2: "cmod (ff (Suc 1) v) \<le> real (k * (k + 1)) / (d / 2) ^ (k + 2)"
1.5503 +                if "v \<in> ball w (d / 2)" for v
1.5504 +      proof -
1.5505 +        have "d/2 \<le> cmod (x - v)" using d x that
1.5506 +          apply (simp add: dist_norm path_image_def ball_def not_less [symmetric] del: divide_const_simps, clarify)
1.5507 +          apply (drule subsetD)
1.5508 +           prefer 2 apply blast
1.5509 +          apply (metis norm_minus_commute norm_triangle_half_r CollectI)
1.5510 +          done
1.5511 +        then have "d \<le> cmod (x - v) * 2"
1.5512 +          by (simp add: divide_simps)
1.5513 +        then have dpow_le: "d ^ (k+2) \<le> (cmod (x - v) * 2) ^ (k+2)"
1.5514 +          using \<open>0 < d\<close> order_less_imp_le power_mono by blast
1.5515 +        have "x \<noteq> v" using that
1.5516 +          using \<open>x \<in> path_image \<gamma>\<close> ball_divide_subset_numeral d by fastforce
1.5517 +        then show ?thesis
1.5518 +        using \<open>d > 0\<close>
1.5519 +        apply (simp add: ff_def norm_mult norm_divide norm_power dist_norm canc)
1.5520 +        using dpow_le
1.5521 +        apply (simp add: algebra_simps divide_simps mult_less_0_iff)
1.5522 +        done
1.5523 +      qed
1.5524 +      have ub: "u \<in> ball w (d / 2)"
1.5525 +        using uwd by (simp add: dist_commute dist_norm)
1.5526 +      have "cmod (inverse (x - u) ^ k - (inverse (x - w) ^ k + of_nat k * (u - w) / ((x - w) * (x - w) ^ k)))
1.5527 +                  \<le> (real k * 4 + real k * real k * 4) * (cmod (u - w) * cmod (u - w)) / (d * (d * (d / 2) ^ k))"
1.5528 +        using complex_taylor [OF _ ff1 ff2 _ ub, of w, simplified]
1.5529 +        by (simp add: ff_def \<open>0 < d\<close>)
1.5530 +      then have "cmod (inverse (x - u) ^ k - (inverse (x - w) ^ k + of_nat k * (u - w) / ((x - w) * (x - w) ^ k)))
1.5531 +                  \<le> (cmod (u - w) * real k) * (1 + real k) * cmod (u - w) / (d / 2) ^ (k+2)"
1.5532 +        by (simp add: field_simps)
1.5533 +      then have "cmod (inverse (x - u) ^ k - (inverse (x - w) ^ k + of_nat k * (u - w) / ((x - w) * (x - w) ^ k)))
1.5534 +                 / (cmod (u - w) * real k)
1.5535 +                  \<le> (1 + real k) * cmod (u - w) / (d / 2) ^ (k+2)"
1.5536 +        using \<open>k \<noteq> 0\<close> \<open>u \<noteq> w\<close> by (simp add: mult_ac zero_less_mult_iff pos_divide_le_eq)
1.5537 +      also have "... < e"
1.5538 +        using uw_less \<open>0 < d\<close> by (simp add: mult_ac divide_simps)
1.5539 +      finally have e: "cmod (inverse (x-u)^k - (inverse (x-w)^k + of_nat k * (u-w) / ((x-w) * (x-w)^k)))
1.5540 +                        / cmod ((u - w) * real k)   <   e"
1.5541 +        by (simp add: norm_mult)
1.5542 +      have "x \<noteq> u"
1.5543 +        using uwd \<open>0 < d\<close> x d by (force simp: dist_norm ball_def norm_minus_commute)
1.5544 +      show ?thesis
1.5545 +        apply (rule le_less_trans [OF _ e])
1.5546 +        using \<open>k \<noteq> 0\<close> \<open>x \<noteq> u\<close>  \<open>u \<noteq> w\<close>
1.5547 +        apply (simp add: field_simps norm_divide [symmetric])
1.5548 +        done
1.5549 +    qed
1.5550 +    show ?thesis
1.5551 +      unfolding eventually_at
1.5552 +      apply (rule_tac x = "min (d/2) ((e*(d/2)^(k + 2))/(Suc k))" in exI)
1.5553 +      apply (force simp: \<open>d > 0\<close> dist_norm that simp del: power_Suc intro: *)
1.5554 +      done
1.5555 +  qed
1.5556 +  have 2: "\<forall>\<^sub>F n in at w.
1.5557 +              \<forall>x\<in>path_image \<gamma>.
1.5558 +               cmod (f' x * (inverse (x - n) ^ k - inverse (x - w) ^ k) / (n - w) / of_nat k - f' x / (x - w) ^ Suc k) < e"
1.5559 +          if "0 < e" for e
1.5560 +  proof -
1.5561 +    have *: "cmod (f' (\<gamma> x) * (inverse (\<gamma> x - u) ^ k - inverse (\<gamma> x - w) ^ k) / ((u - w) * k) -
1.5562 +                        f' (\<gamma> x) / ((\<gamma> x - w) * (\<gamma> x - w) ^ k)) < e"
1.5563 +              if ec: "cmod ((inverse (\<gamma> x - u) ^ k - inverse (\<gamma> x - w) ^ k) / ((u - w) * k) -
1.5564 +                      inverse (\<gamma> x - w) * inverse (\<gamma> x - w) ^ k) < e / C"
1.5565 +                 and x: "0 \<le> x" "x \<le> 1"
1.5566 +              for u x
1.5567 +    proof (cases "(f' (\<gamma> x)) = 0")
1.5568 +      case True then show ?thesis by (simp add: \<open>0 < e\<close>)
1.5569 +    next
1.5570 +      case False
1.5571 +      have "cmod (f' (\<gamma> x) * (inverse (\<gamma> x - u) ^ k - inverse (\<gamma> x - w) ^ k) / ((u - w) * k) -
1.5572 +                        f' (\<gamma> x) / ((\<gamma> x - w) * (\<gamma> x - w) ^ k)) =
1.5573 +            cmod (f' (\<gamma> x) * ((inverse (\<gamma> x - u) ^ k - inverse (\<gamma> x - w) ^ k) / ((u - w) * k) -
1.5574 +                             inverse (\<gamma> x - w) * inverse (\<gamma> x - w) ^ k))"
1.5575 +        by (simp add: field_simps)
1.5576 +      also have "... = cmod (f' (\<gamma> x)) *
1.5577 +                       cmod ((inverse (\<gamma> x - u) ^ k - inverse (\<gamma> x - w) ^ k) / ((u - w) * k) -
1.5578 +                             inverse (\<gamma> x - w) * inverse (\<gamma> x - w) ^ k)"
1.5579 +        by (simp add: norm_mult)
1.5580 +      also have "... < cmod (f' (\<gamma> x)) * (e/C)"
1.5581 +        apply (rule mult_strict_left_mono [OF ec])
1.5582 +        using False by simp
1.5583 +      also have "... \<le> e" using C
1.5584 +        by (metis False \<open>0 < e\<close> frac_le less_eq_real_def mult.commute pos_le_divide_eq x zero_less_norm_iff)
1.5585 +      finally show ?thesis .
1.5586 +    qed
1.5587 +    show ?thesis
1.5588 +      using twom [OF divide_pos_pos [OF that \<open>C > 0\<close>]]   unfolding path_image_def
1.5589 +      by (force intro: * elim: eventually_mono)
1.5590 +  qed
1.5591 +  show "(\<lambda>u. f' u / (u - w) ^ (Suc k)) contour_integrable_on \<gamma>"
1.5592 +    by (rule contour_integral_uniform_limit [OF 1 2 leB \<gamma>]) auto
1.5593 +  have *: "(\<lambda>n. contour_integral \<gamma> (\<lambda>x. f' x * (inverse (x - n) ^ k - inverse (x - w) ^ k) / (n - w) / k))
1.5594 +           \<midarrow>w\<rightarrow> contour_integral \<gamma> (\<lambda>u. f' u / (u - w) ^ (Suc k))"
1.5595 +    by (rule contour_integral_uniform_limit [OF 1 2 leB \<gamma>]) auto
1.5596 +  have **: "contour_integral \<gamma> (\<lambda>x. f' x * (inverse (x - u) ^ k - inverse (x - w) ^ k) / ((u - w) * k)) =
1.5597 +              (f u - f w) / (u - w) / k"
1.5598 +           if "dist u w < d" for u
1.5599 +    apply (rule contour_integral_unique)
1.5600 +    apply (simp add: diff_divide_distrib algebra_simps)
1.5601 +    apply (rule has_contour_integral_diff; rule has_contour_integral_div; simp add: field_simps; rule int)
1.5602 +    apply (metis contra_subsetD d dist_commute mem_ball that)
1.5603 +    apply (rule w)
1.5604 +    done
1.5605 +  show ?thes2
1.5606 +    apply (simp add: DERIV_within_iff del: power_Suc)
1.5607 +    apply (rule Lim_transform_within [OF tendsto_mult_left [OF *] \<open>0 < d\<close> ])
1.5608 +    apply (simp add: \<open>k \<noteq> 0\<close> **)
1.5609 +    done
1.5610 +qed
1.5611 +
1.5612 +corollary Cauchy_next_derivative_circlepath:
1.5613 +  assumes contf: "continuous_on (path_image (circlepath z r)) f"
1.5614 +      and int: "\<And>w. w \<in> ball z r \<Longrightarrow> ((\<lambda>u. f u / (u - w)^k) has_contour_integral g w) (circlepath z r)"
1.5615 +      and k: "k \<noteq> 0"
1.5616 +      and w: "w \<in> ball z r"
1.5617 +    shows "(\<lambda>u. f u / (u - w)^(Suc k)) contour_integrable_on (circlepath z r)"
1.5618 +           (is "?thes1")
1.5619 +      and "(g has_field_derivative (k * contour_integral (circlepath z r) (\<lambda>u. f u/(u - w)^(Suc k)))) (at w)"
1.5620 +           (is "?thes2")
1.5621 +proof -
1.5622 +  have "r > 0" using w
1.5623 +    using ball_eq_empty by fastforce
1.5624 +  have wim: "w \<in> ball z r - path_image (circlepath z r)"
1.5625 +    using w by (auto simp: dist_norm)
1.5626 +  show ?thes1 ?thes2
1.5627 +    by (rule Cauchy_next_derivative [OF contf _ int k open_ball valid_path_circlepath wim, where B = "2 * pi * \<bar>r\<bar>"];
1.5628 +        auto simp: vector_derivative_circlepath norm_mult)+
1.5629 +qed
1.5630 +
1.5631 +
1.5632 +text\<open> In particular, the first derivative formula.\<close>
1.5633 +
1.5634 +proposition Cauchy_derivative_integral_circlepath:
1.5635 +  assumes contf: "continuous_on (cball z r) f"
1.5636 +      and holf: "f holomorphic_on ball z r"
1.5637 +      and w: "w \<in> ball z r"
1.5638 +    shows "(\<lambda>u. f u/(u - w)^2) contour_integrable_on (circlepath z r)"
1.5639 +           (is "?thes1")
1.5640 +      and "(f has_field_derivative (1 / (2 * of_real pi * \<i>) * contour_integral(circlepath z r) (\<lambda>u. f u / (u - w)^2))) (at w)"
1.5641 +           (is "?thes2")
1.5642 +proof -
1.5643 +  have [simp]: "r \<ge> 0" using w
1.5644 +    using ball_eq_empty by fastforce
1.5645 +  have f: "continuous_on (path_image (circlepath z r)) f"
1.5646 +    by (rule continuous_on_subset [OF contf]) (force simp add: cball_def sphere_def)
1.5647 +  have int: "\<And>w. dist z w < r \<Longrightarrow>
1.5648 +                 ((\<lambda>u. f u / (u - w)) has_contour_integral (\<lambda>x. 2 * of_real pi * \<i> * f x) w) (circlepath z r)"
1.5649 +    by (rule Cauchy_integral_circlepath [OF contf holf]) (simp add: dist_norm norm_minus_commute)
1.5650 +  show ?thes1
1.5651 +    apply (simp add: power2_eq_square)
1.5652 +    apply (rule Cauchy_next_derivative_circlepath [OF f _ _ w, where k=1, simplified])
1.5653 +    apply (blast intro: int)
1.5654 +    done
1.5655 +  have "((\<lambda>x. 2 * of_real pi * \<i> * f x) has_field_derivative contour_integral (circlepath z r) (\<lambda>u. f u / (u - w)^2)) (at w)"
1.5656 +    apply (simp add: power2_eq_square)
1.5657 +    apply (rule Cauchy_next_derivative_circlepath [OF f _ _ w, where k=1 and g = "\<lambda>x. 2 * of_real pi * \<i> * f x", simplified])
1.5658 +    apply (blast intro: int)
1.5659 +    done
1.5660 +  then have fder: "(f has_field_derivative contour_integral (circlepath z r) (\<lambda>u. f u / (u - w)^2) / (2 * of_real pi * \<i>)) (at w)"
1.5661 +    by (rule DERIV_cdivide [where f = "\<lambda>x. 2 * of_real pi * \<i> * f x" and c = "2 * of_real pi * \<i>", simplified])
1.5662 +  show ?thes2
1.5663 +    by simp (rule fder)
1.5664 +qed
1.5665 +
1.5666 +subsection\<open> Existence of all higher derivatives.\<close>
1.5667 +
1.5668 +proposition derivative_is_holomorphic:
1.5669 +  assumes "open s"
1.5670 +      and fder: "\<And>z. z \<in> s \<Longrightarrow> (f has_field_derivative f' z) (at z)"
1.5671 +    shows "f' holomorphic_on s"
1.5672 +proof -
1.5673 +  have *: "\<exists>h. (f' has_field_derivative h) (at z)" if "z \<in> s" for z
1.5674 +  proof -
1.5675 +    obtain r where "r > 0" and r: "cball z r \<subseteq> s"
1.5676 +      using open_contains_cball \<open>z \<in> s\<close> \<open>open s\<close> by blast
1.5677 +    then have holf_cball: "f holomorphic_on cball z r"
1.5678 +      apply (simp add: holomorphic_on_def)
1.5679 +      using field_differentiable_at_within field_differentiable_def fder by blast
1.5680 +    then have "continuous_on (path_image (circlepath z r)) f"
1.5681 +      using \<open>r > 0\<close> by (force elim: holomorphic_on_subset [THEN holomorphic_on_imp_continuous_on])
1.5682 +    then have contfpi: "continuous_on (path_image (circlepath z r)) (\<lambda>x. 1/(2 * of_real pi*\<i>) * f x)"
1.5683 +      by (auto intro: continuous_intros)+
1.5684 +    have contf_cball: "continuous_on (cball z r) f" using holf_cball
1.5685 +      by (simp add: holomorphic_on_imp_continuous_on holomorphic_on_subset)
1.5686 +    have holf_ball: "f holomorphic_on ball z r" using holf_cball
1.5687 +      using ball_subset_cball holomorphic_on_subset by blast
1.5688 +    { fix w  assume w: "w \<in> ball z r"
1.5689 +      have intf: "(\<lambda>u. f u / (u - w)\<^sup>2) contour_integrable_on circlepath z r"
1.5690 +        by (blast intro: w Cauchy_derivative_integral_circlepath [OF contf_cball holf_ball])
1.5691 +      have fder': "(f has_field_derivative 1 / (2 * of_real pi * \<i>) * contour_integral (circlepath z r) (\<lambda>u. f u / (u - w)\<^sup>2))
1.5692 +                  (at w)"
1.5693 +        by (blast intro: w Cauchy_derivative_integral_circlepath [OF contf_cball holf_ball])
1.5694 +      have f'_eq: "f' w = contour_integral (circlepath z r) (\<lambda>u. f u / (u - w)\<^sup>2) / (2 * of_real pi * \<i>)"
1.5695 +        using fder' ball_subset_cball r w by (force intro: DERIV_unique [OF fder])
1.5696 +      have "((\<lambda>u. f u / (u - w)\<^sup>2 / (2 * of_real pi * \<i>)) has_contour_integral
1.5697 +                contour_integral (circlepath z r) (\<lambda>u. f u / (u - w)\<^sup>2) / (2 * of_real pi * \<i>))
1.5698 +                (circlepath z r)"
1.5699 +        by (rule has_contour_integral_div [OF has_contour_integral_integral [OF intf]])
1.5700 +      then have "((\<lambda>u. f u / (2 * of_real pi * \<i> * (u - w)\<^sup>2)) has_contour_integral
1.5701 +                contour_integral (circlepath z r) (\<lambda>u. f u / (u - w)\<^sup>2) / (2 * of_real pi * \<i>))
1.5702 +                (circlepath z r)"
1.5703 +        by (simp add: algebra_simps)
1.5704 +      then have "((\<lambda>u. f u / (2 * of_real pi * \<i> * (u - w)\<^sup>2)) has_contour_integral f' w) (circlepath z r)"
1.5705 +        by (simp add: f'_eq)
1.5706 +    } note * = this
1.5707 +    show ?thesis
1.5708 +      apply (rule exI)
1.5709 +      apply (rule Cauchy_next_derivative_circlepath [OF contfpi, of 2 f', simplified])
1.5710 +      apply (simp_all add: \<open>0 < r\<close> * dist_norm)
1.5711 +      done
1.5712 +  qed
1.5713 +  show ?thesis
1.5714 +    by (simp add: holomorphic_on_open [OF \<open>open s\<close>] *)
1.5715 +qed
1.5716 +
1.5717 +lemma holomorphic_deriv [holomorphic_intros]:
1.5718 +    "\<lbrakk>f holomorphic_on s; open s\<rbrakk> \<Longrightarrow> (deriv f) holomorphic_on s"
1.5719 +by (metis DERIV_deriv_iff_field_differentiable at_within_open derivative_is_holomorphic holomorphic_on_def)
1.5720 +
1.5721 +lemma analytic_deriv: "f analytic_on s \<Longrightarrow> (deriv f) analytic_on s"
1.5722 +  using analytic_on_holomorphic holomorphic_deriv by auto
1.5723 +
1.5724 +lemma holomorphic_higher_deriv [holomorphic_intros]: "\<lbrakk>f holomorphic_on s; open s\<rbrakk> \<Longrightarrow> (deriv ^^ n) f holomorphic_on s"
1.5725 +  by (induction n) (auto simp: holomorphic_deriv)
1.5726 +
1.5727 +lemma analytic_higher_deriv: "f analytic_on s \<Longrightarrow> (deriv ^^ n) f analytic_on s"
1.5728 +  unfolding analytic_on_def using holomorphic_higher_deriv by blast
1.5729 +
1.5730 +lemma has_field_derivative_higher_deriv:
1.5731 +     "\<lbrakk>f holomorphic_on s; open s; x \<in> s\<rbrakk>
1.5732 +      \<Longrightarrow> ((deriv ^^ n) f has_field_derivative (deriv ^^ (Suc n)) f x) (at x)"
1.5733 +by (metis (no_types, hide_lams) DERIV_deriv_iff_field_differentiable at_within_open comp_apply
1.5734 +         funpow.simps(2) holomorphic_higher_deriv holomorphic_on_def)
1.5735 +
1.5736 +lemma valid_path_compose_holomorphic:
1.5737 +  assumes "valid_path g" and holo:"f holomorphic_on s" and "open s" "path_image g \<subseteq> s"
1.5738 +  shows "valid_path (f o g)"
1.5739 +proof (rule valid_path_compose[OF \<open>valid_path g\<close>])
1.5740 +  fix x assume "x \<in> path_image g"
1.5741 +  then show "\<exists>f'. (f has_field_derivative f') (at x)"
1.5742 +    using holo holomorphic_on_open[OF \<open>open s\<close>] \<open>path_image g \<subseteq> s\<close> by auto
1.5743 +next
1.5744 +  have "deriv f holomorphic_on s"
1.5745 +    using holomorphic_deriv holo \<open>open s\<close> by auto
1.5746 +  then show "continuous_on (path_image g) (deriv f)"
1.5747 +    using assms(4) holomorphic_on_imp_continuous_on holomorphic_on_subset by auto
1.5748 +qed
1.5749 +
1.5750 +
1.5751 +subsection\<open> Morera's theorem.\<close>
1.5752 +
1.5753 +lemma Morera_local_triangle_ball:
1.5754 +  assumes "\<And>z. z \<in> s
1.5755 +          \<Longrightarrow> \<exists>e a. 0 < e \<and> z \<in> ball a e \<and> continuous_on (ball a e) f \<and>
1.5756 +                    (\<forall>b c. closed_segment b c \<subseteq> ball a e
1.5757 +                           \<longrightarrow> contour_integral (linepath a b) f +
1.5758 +                               contour_integral (linepath b c) f +
1.5759 +                               contour_integral (linepath c a) f = 0)"
1.5760 +  shows "f analytic_on s"
1.5761 +proof -
1.5762 +  { fix z  assume "z \<in> s"
1.5763 +    with assms obtain e a where
1.5764 +            "0 < e" and z: "z \<in> ball a e" and contf: "continuous_on (ball a e) f"
1.5765 +        and 0: "\<And>b c. closed_segment b c \<subseteq> ball a e
1.5766 +                      \<Longrightarrow> contour_integral (linepath a b) f +
1.5767 +                          contour_integral (linepath b c) f +
1.5768 +                          contour_integral (linepath c a) f = 0"
1.5769 +      by fastforce
1.5770 +    have az: "dist a z < e" using mem_ball z by blast
1.5771 +    have sb_ball: "ball z (e - dist a z) \<subseteq> ball a e"
1.5772 +      by (simp add: dist_commute ball_subset_ball_iff)
1.5773 +    have "\<exists>e>0. f holomorphic_on ball z e"
1.5774 +      apply (rule_tac x="e - dist a z" in exI)
1.5775 +      apply (simp add: az)
1.5776 +      apply (rule holomorphic_on_subset [OF _ sb_ball])
1.5777 +      apply (rule derivative_is_holomorphic[OF open_ball])
1.5778 +      apply (rule triangle_contour_integrals_starlike_primitive [OF contf _ open_ball, of a])
1.5779 +         apply (simp_all add: 0 \<open>0 < e\<close>)
1.5780 +      apply (meson \<open>0 < e\<close> centre_in_ball convex_ball convex_contains_segment mem_ball)
1.5781 +      done
1.5782 +  }
1.5783 +  then show ?thesis
1.5784 +    by (simp add: analytic_on_def)
1.5785 +qed
1.5786 +
1.5787 +lemma Morera_local_triangle:
1.5788 +  assumes "\<And>z. z \<in> s
1.5789 +          \<Longrightarrow> \<exists>t. open t \<and> z \<in> t \<and> continuous_on t f \<and>
1.5790 +                  (\<forall>a b c. convex hull {a,b,c} \<subseteq> t
1.5791 +                              \<longrightarrow> contour_integral (linepath a b) f +
1.5792 +                                  contour_integral (linepath b c) f +
1.5793 +                                  contour_integral (linepath c a) f = 0)"
1.5794 +  shows "f analytic_on s"
1.5795 +proof -
1.5796 +  { fix z  assume "z \<in> s"
1.5797 +    with assms obtain t where
1.5798 +            "open t" and z: "z \<in> t" and contf: "continuous_on t f"
1.5799 +        and 0: "\<And>a b c. convex hull {a,b,c} \<subseteq> t
1.5800 +                      \<Longrightarrow> contour_integral (linepath a b) f +
1.5801 +                          contour_integral (linepath b c) f +
1.5802 +                          contour_integral (linepath c a) f = 0"
1.5803 +      by force
1.5804 +    then obtain e where "e>0" and e: "ball z e \<subseteq> t"
1.5805 +      using open_contains_ball by blast
1.5806 +    have [simp]: "continuous_on (ball z e) f" using contf
1.5807 +      using continuous_on_subset e by blast
1.5808 +    have "\<exists>e a. 0 < e \<and>
1.5809 +               z \<in> ball a e \<and>
1.5810 +               continuous_on (ball a e) f \<and>
1.5811 +               (\<forall>b c. closed_segment b c \<subseteq> ball a e \<longrightarrow>
1.5812 +                      contour_integral (linepath a b) f + contour_integral (linepath b c) f + contour_integral (linepath c a) f = 0)"
1.5813 +      apply (rule_tac x=e in exI)
1.5814 +      apply (rule_tac x=z in exI)
1.5815 +      apply (simp add: \<open>e > 0\<close>, clarify)
1.5816 +      apply (rule 0)
1.5817 +      apply (meson z \<open>0 < e\<close> centre_in_ball closed_segment_subset convex_ball dual_order.trans e starlike_convex_subset)
1.5818 +      done
1.5819 +  }
1.5820 +  then show ?thesis
1.5821 +    by (simp add: Morera_local_triangle_ball)
1.5822 +qed
1.5823 +
1.5824 +proposition Morera_triangle:
1.5825 +    "\<lbrakk>continuous_on s f; open s;
1.5826 +      \<And>a b c. convex hull {a,b,c} \<subseteq> s
1.5827 +              \<longrightarrow> contour_integral (linepath a b) f +
1.5828 +                  contour_integral (linepath b c) f +
1.5829 +                  contour_integral (linepath c a) f = 0\<rbrakk>
1.5830 +     \<Longrightarrow> f analytic_on s"
1.5831 +  using Morera_local_triangle by blast
1.5832 +
1.5833 +
1.5834 +
1.5835 +subsection\<open> Combining theorems for higher derivatives including Leibniz rule.\<close>
1.5836 +
1.5837 +lemma higher_deriv_linear [simp]:
1.5838 +    "(deriv ^^ n) (\<lambda>w. c*w) = (\<lambda>z. if n = 0 then c*z else if n = 1 then c else 0)"
1.5839 +  by (induction n) (auto simp: deriv_const deriv_linear)
1.5840 +
1.5841 +lemma higher_deriv_const [simp]: "(deriv ^^ n) (\<lambda>w. c) = (\<lambda>w. if n=0 then c else 0)"
1.5842 +  by (induction n) (auto simp: deriv_const)
1.5843 +
1.5844 +lemma higher_deriv_ident [simp]:
1.5845 +     "(deriv ^^ n) (\<lambda>w. w) z = (if n = 0 then z else if n = 1 then 1 else 0)"
1.5846 +  apply (induction n, simp)
1.5847 +  apply (metis higher_deriv_linear lambda_one)
1.5848 +  done
1.5849 +
1.5850 +corollary higher_deriv_id [simp]:
1.5851 +     "(deriv ^^ n) id z = (if n = 0 then z else if n = 1 then 1 else 0)"
1.5852 +  by (simp add: id_def)
1.5853 +
1.5854 +lemma has_complex_derivative_funpow_1:
1.5855 +     "\<lbrakk>(f has_field_derivative 1) (at z); f z = z\<rbrakk> \<Longrightarrow> (f^^n has_field_derivative 1) (at z)"
1.5856 +  apply (induction n)
1.5857 +  apply auto
1.5858 +  apply (metis DERIV_ident DERIV_transform_at id_apply zero_less_one)
1.5859 +  by (metis DERIV_chain comp_funpow comp_id funpow_swap1 mult.right_neutral)
1.5860 +
1.5861 +proposition higher_deriv_uminus:
1.5862 +  assumes "f holomorphic_on s" "open s" and z: "z \<in> s"
1.5863 +    shows "(deriv ^^ n) (\<lambda>w. -(f w)) z = - ((deriv ^^ n) f z)"
1.5864 +using z
1.5865 +proof (induction n arbitrary: z)
1.5866 +  case 0 then show ?case by simp
1.5867 +next
1.5868 +  case (Suc n z)
1.5869 +  have *: "((deriv ^^ n) f has_field_derivative deriv ((deriv ^^ n) f) z) (at z)"
1.5870 +    using Suc.prems assms has_field_derivative_higher_deriv by auto
1.5871 +  show ?case
1.5872 +    apply simp
1.5873 +    apply (rule DERIV_imp_deriv)
1.5874 +    apply (rule DERIV_transform_within_open [of "\<lambda>w. -((deriv ^^ n) f w)"])
1.5875 +    apply (rule derivative_eq_intros | rule * refl assms Suc)+
1.5876 +    apply (simp add: Suc)
1.5877 +    done
1.5878 +qed
1.5879 +
1.5881 +  fixes z::complex
1.5882 +  assumes "f holomorphic_on s" "g holomorphic_on s" "open s" and z: "z \<in> s"
1.5883 +    shows "(deriv ^^ n) (\<lambda>w. f w + g w) z = (deriv ^^ n) f z + (deriv ^^ n) g z"
1.5884 +using z
1.5885 +proof (induction n arbitrary: z)
1.5886 +  case 0 then show ?case by simp
1.5887 +next
1.5888 +  case (Suc n z)
1.5889 +  have *: "((deriv ^^ n) f has_field_derivative deriv ((deriv ^^ n) f) z) (at z)"
1.5890 +          "((deriv ^^ n) g has_field_derivative deriv ((deriv ^^ n) g) z) (at z)"
1.5891 +    using Suc.prems assms has_field_derivative_higher_deriv by auto
1.5892 +  show ?case
1.5893 +    apply simp
1.5894 +    apply (rule DERIV_imp_deriv)
1.5895 +    apply (rule DERIV_transform_within_open [of "\<lambda>w. (deriv ^^ n) f w + (deriv ^^ n) g w"])
1.5896 +    apply (rule derivative_eq_intros | rule * refl assms Suc)+
1.5897 +    apply (simp add: Suc)
1.5898 +    done
1.5899 +qed
1.5900 +
1.5901 +corollary higher_deriv_diff:
1.5902 +  fixes z::complex
1.5903 +  assumes "f holomorphic_on s" "g holomorphic_on s" "open s" and z: "z \<in> s"
1.5904 +    shows "(deriv ^^ n) (\<lambda>w. f w - g w) z = (deriv ^^ n) f z - (deriv ^^ n) g z"
1.5907 +  using assms holomorphic_on_minus apply (auto simp: higher_deriv_uminus)
1.5908 +  done
1.5909 +
1.5910 +
1.5911 +lemma bb: "Suc n choose k = (n choose k) + (if k = 0 then 0 else (n choose (k - 1)))"
1.5912 +  by (cases k) simp_all
1.5913 +
1.5914 +proposition higher_deriv_mult:
1.5915 +  fixes z::complex
1.5916 +  assumes "f holomorphic_on s" "g holomorphic_on s" "open s" and z: "z \<in> s"
1.5917 +    shows "(deriv ^^ n) (\<lambda>w. f w * g w) z =
1.5918 +           (\<Sum>i = 0..n. of_nat (n choose i) * (deriv ^^ i) f z * (deriv ^^ (n - i)) g z)"
1.5919 +using z
1.5920 +proof (induction n arbitrary: z)
1.5921 +  case 0 then show ?case by simp
1.5922 +next
1.5923 +  case (Suc n z)
1.5924 +  have *: "\<And>n. ((deriv ^^ n) f has_field_derivative deriv ((deriv ^^ n) f) z) (at z)"
1.5925 +          "\<And>n. ((deriv ^^ n) g has_field_derivative deriv ((deriv ^^ n) g) z) (at z)"
1.5926 +    using Suc.prems assms has_field_derivative_higher_deriv by auto
1.5927 +  have sumeq: "(\<Sum>i = 0..n.
1.5928 +               of_nat (n choose i) * (deriv ((deriv ^^ i) f) z * (deriv ^^ (n - i)) g z + deriv ((deriv ^^ (n - i)) g) z * (deriv ^^ i) f z)) =
1.5929 +            g z * deriv ((deriv ^^ n) f) z + (\<Sum>i = 0..n. (deriv ^^ i) f z * (of_nat (Suc n choose i) * (deriv ^^ (Suc n - i)) g z))"
1.5930 +    apply (simp add: bb algebra_simps setsum.distrib)
1.5931 +    apply (subst (4) setsum_Suc_reindex)
1.5932 +    apply (auto simp: algebra_simps Suc_diff_le intro: setsum.cong)
1.5933 +    done
1.5934 +  show ?case
1.5935 +    apply (simp only: funpow.simps o_apply)
1.5936 +    apply (rule DERIV_imp_deriv)
1.5937 +    apply (rule DERIV_transform_within_open
1.5938 +             [of "\<lambda>w. (\<Sum>i = 0..n. of_nat (n choose i) * (deriv ^^ i) f w * (deriv ^^ (n - i)) g w)"])
1.5939 +    apply (simp add: algebra_simps)
1.5940 +    apply (rule DERIV_cong [OF DERIV_setsum])
1.5941 +    apply (rule DERIV_cmult)
1.5942 +    apply (auto simp: intro: DERIV_mult * sumeq \<open>open s\<close> Suc.prems Suc.IH [symmetric])
1.5943 +    done
1.5944 +qed
1.5945 +
1.5946 +
1.5947 +proposition higher_deriv_transform_within_open:
1.5948 +  fixes z::complex
1.5949 +  assumes "f holomorphic_on s" "g holomorphic_on s" "open s" and z: "z \<in> s"
1.5950 +      and fg: "\<And>w. w \<in> s \<Longrightarrow> f w = g w"
1.5951 +    shows "(deriv ^^ i) f z = (deriv ^^ i) g z"
1.5952 +using z
1.5953 +by (induction i arbitrary: z)
1.5954 +   (auto simp: fg intro: complex_derivative_transform_within_open holomorphic_higher_deriv assms)
1.5955 +
1.5956 +proposition higher_deriv_compose_linear:
1.5957 +  fixes z::complex
1.5958 +  assumes f: "f holomorphic_on t" and s: "open s" and t: "open t" and z: "z \<in> s"
1.5959 +      and fg: "\<And>w. w \<in> s \<Longrightarrow> u * w \<in> t"
1.5960 +    shows "(deriv ^^ n) (\<lambda>w. f (u * w)) z = u^n * (deriv ^^ n) f (u * z)"
1.5961 +using z
1.5962 +proof (induction n arbitrary: z)
1.5963 +  case 0 then show ?case by simp
1.5964 +next
1.5965 +  case (Suc n z)
1.5966 +  have holo0: "f holomorphic_on op * u ` s"
1.5967 +    by (meson fg f holomorphic_on_subset image_subset_iff)
1.5968 +  have holo1: "(\<lambda>w. f (u * w)) holomorphic_on s"
1.5969 +    apply (rule holomorphic_on_compose [where g=f, unfolded o_def])
1.5970 +    apply (rule holo0 holomorphic_intros)+
1.5971 +    done
1.5972 +  have holo2: "(\<lambda>z. u ^ n * (deriv ^^ n) f (u * z)) holomorphic_on s"
1.5973 +    apply (rule holomorphic_intros)+
1.5974 +    apply (rule holomorphic_on_compose [where g="(deriv ^^ n) f", unfolded o_def])
1.5975 +    apply (rule holomorphic_intros)
1.5976 +    apply (rule holomorphic_on_subset [where s=t])
1.5977 +    apply (rule holomorphic_intros assms)+
1.5978 +    apply (blast intro: fg)
1.5979 +    done
1.5980 +  have "deriv ((deriv ^^ n) (\<lambda>w. f (u * w))) z = deriv (\<lambda>z. u^n * (deriv ^^ n) f (u*z)) z"
1.5981 +    apply (rule complex_derivative_transform_within_open [OF _ holo2 s Suc.prems])
1.5982 +    apply (rule holomorphic_higher_deriv [OF holo1 s])
1.5983 +    apply (simp add: Suc.IH)
1.5984 +    done
1.5985 +  also have "... = u^n * deriv (\<lambda>z. (deriv ^^ n) f (u * z)) z"
1.5986 +    apply (rule deriv_cmult)
1.5987 +    apply (rule analytic_on_imp_differentiable_at [OF _ Suc.prems])
1.5988 +    apply (rule analytic_on_compose_gen [where g="(deriv ^^ n) f" and t=t, unfolded o_def])
1.5989 +    apply (simp add: analytic_on_linear)
1.5990 +    apply (simp add: analytic_on_open f holomorphic_higher_deriv t)
1.5991 +    apply (blast intro: fg)
1.5992 +    done
1.5993 +  also have "... = u * u ^ n * deriv ((deriv ^^ n) f) (u * z)"
1.5994 +      apply (subst complex_derivative_chain [where g = "(deriv ^^ n) f" and f = "op*u", unfolded o_def])
1.5995 +      apply (rule derivative_intros)
1.5996 +      using Suc.prems field_differentiable_def f fg has_field_derivative_higher_deriv t apply blast
1.5997 +      apply (simp add: deriv_linear)
1.5998 +      done
1.5999 +  finally show ?case
1.6000 +    by simp
1.6001 +qed
1.6002 +
1.6004 +  assumes "f analytic_on {z}" "g analytic_on {z}"
1.6005 +    shows "(deriv ^^ n) (\<lambda>w. f w + g w) z = (deriv ^^ n) f z + (deriv ^^ n) g z"
1.6006 +proof -
1.6007 +  have "f analytic_on {z} \<and> g analytic_on {z}"
1.6008 +    using assms by blast
1.6009 +  with higher_deriv_add show ?thesis
1.6010 +    by (auto simp: analytic_at_two)
1.6011 +qed
1.6012 +
1.6013 +lemma higher_deriv_diff_at:
1.6014 +  assumes "f analytic_on {z}" "g analytic_on {z}"
1.6015 +    shows "(deriv ^^ n) (\<lambda>w. f w - g w) z = (deriv ^^ n) f z - (deriv ^^ n) g z"
1.6016 +proof -
1.6017 +  have "f analytic_on {z} \<and> g analytic_on {z}"
1.6018 +    using assms by blast
1.6019 +  with higher_deriv_diff show ?thesis
1.6020 +    by (auto simp: analytic_at_two)
1.6021 +qed
1.6022 +
1.6023 +lemma higher_deriv_uminus_at:
1.6024 +   "f analytic_on {z}  \<Longrightarrow> (deriv ^^ n) (\<lambda>w. -(f w)) z = - ((deriv ^^ n) f z)"
1.6025 +  using higher_deriv_uminus
1.6026 +    by (auto simp: analytic_at)
1.6027 +
1.6028 +lemma higher_deriv_mult_at:
1.6029 +  assumes "f analytic_on {z}" "g analytic_on {z}"
1.6030 +    shows "(deriv ^^ n) (\<lambda>w. f w * g w) z =
1.6031 +           (\<Sum>i = 0..n. of_nat (n choose i) * (deriv ^^ i) f z * (deriv ^^ (n - i)) g z)"
1.6032 +proof -
1.6033 +  have "f analytic_on {z} \<and> g analytic_on {z}"
1.6034 +    using assms by blast
1.6035 +  with higher_deriv_mult show ?thesis
1.6036 +    by (auto simp: analytic_at_two)
1.6037 +qed
1.6038 +
1.6039 +
1.6040 +text\<open> Nonexistence of isolated singularities and a stronger integral formula.\<close>
1.6041 +
1.6042 +proposition no_isolated_singularity:
1.6043 +  fixes z::complex
1.6044 +  assumes f: "continuous_on s f" and holf: "f holomorphic_on (s - k)" and s: "open s" and k: "finite k"
1.6045 +    shows "f holomorphic_on s"
1.6046 +proof -
1.6047 +  { fix z
1.6048 +    assume "z \<in> s" and cdf: "\<And>x. x\<in>s - k \<Longrightarrow> f field_differentiable at x"
1.6049 +    have "f field_differentiable at z"
1.6050 +    proof (cases "z \<in> k")
1.6051 +      case False then show ?thesis by (blast intro: cdf \<open>z \<in> s\<close>)
1.6052 +    next
1.6053 +      case True
1.6054 +      with finite_set_avoid [OF k, of z]
1.6055 +      obtain d where "d>0" and d: "\<And>x. \<lbrakk>x\<in>k; x \<noteq> z\<rbrakk> \<Longrightarrow> d \<le> dist z x"
1.6056 +        by blast
1.6057 +      obtain e where "e>0" and e: "ball z e \<subseteq> s"
1.6058 +        using  s \<open>z \<in> s\<close> by (force simp add: open_contains_ball)
1.6059 +      have fde: "continuous_on (ball z (min d e)) f"
1.6060 +        by (metis Int_iff ball_min_Int continuous_on_subset e f subsetI)
1.6061 +      have "\<exists>g. \<forall>w \<in> ball z (min d e). (g has_field_derivative f w) (at w within ball z (min d e))"
1.6062 +        apply (rule contour_integral_convex_primitive [OF convex_ball fde])
1.6063 +        apply (rule Cauchy_theorem_triangle_cofinite [OF _ k])
1.6064 +         apply (metis continuous_on_subset [OF fde] closed_segment_subset convex_ball starlike_convex_subset)
1.6065 +        apply (rule cdf)
1.6066 +        apply (metis Diff_iff Int_iff ball_min_Int bot_least contra_subsetD convex_ball e insert_subset
1.6067 +               interior_mono interior_subset subset_hull)
1.6068 +        done
1.6069 +      then have "f holomorphic_on ball z (min d e)"
1.6070 +        by (metis open_ball at_within_open derivative_is_holomorphic)
1.6071 +      then show ?thesis
1.6072 +        unfolding holomorphic_on_def
1.6073 +        by (metis open_ball \<open>0 < d\<close> \<open>0 < e\<close> at_within_open centre_in_ball min_less_iff_conj)
1.6074 +    qed
1.6075 +  }
1.6076 +  with holf s k show ?thesis
1.6077 +    by (simp add: holomorphic_on_open open_Diff finite_imp_closed field_differentiable_def [symmetric])
1.6078 +qed
1.6079 +
1.6080 +proposition Cauchy_integral_formula_convex:
1.6081 +    assumes s: "convex s" and k: "finite k" and contf: "continuous_on s f"
1.6082 +        and fcd: "(\<And>x. x \<in> interior s - k \<Longrightarrow> f field_differentiable at x)"
1.6083 +        and z: "z \<in> interior s" and vpg: "valid_path \<gamma>"
1.6084 +        and pasz: "path_image \<gamma> \<subseteq> s - {z}" and loop: "pathfinish \<gamma> = pathstart \<gamma>"
1.6085 +      shows "((\<lambda>w. f w / (w - z)) has_contour_integral (2*pi * \<i> * winding_number \<gamma> z * f z)) \<gamma>"
1.6086 +  apply (rule Cauchy_integral_formula_weak [OF s finite.emptyI contf])
1.6087 +  apply (simp add: holomorphic_on_open [symmetric] field_differentiable_def)
1.6088 +  using no_isolated_singularity [where s = "interior s"]
1.6089 +  apply (metis k contf fcd holomorphic_on_open field_differentiable_def continuous_on_subset
1.6090 +               has_field_derivative_at_within holomorphic_on_def interior_subset open_interior)
1.6091 +  using assms
1.6092 +  apply auto
1.6093 +  done
1.6094 +
1.6095 +
1.6096 +text\<open> Formula for higher derivatives.\<close>
1.6097 +
1.6098 +proposition Cauchy_has_contour_integral_higher_derivative_circlepath:
1.6099 +  assumes contf: "continuous_on (cball z r) f"
1.6100 +      and holf: "f holomorphic_on ball z r"
1.6101 +      and w: "w \<in> ball z r"
1.6102 +    shows "((\<lambda>u. f u / (u - w) ^ (Suc k)) has_contour_integral ((2 * pi * \<i>) / (fact k) * (deriv ^^ k) f w))
1.6103 +           (circlepath z r)"
1.6104 +using w
1.6105 +proof (induction k arbitrary: w)
1.6106 +  case 0 then show ?case
1.6107 +    using assms by (auto simp: Cauchy_integral_circlepath dist_commute dist_norm)
1.6108 +next
1.6109 +  case (Suc k)
1.6110 +  have [simp]: "r > 0" using w
1.6111 +    using ball_eq_empty by fastforce
1.6112 +  have f: "continuous_on (path_image (circlepath z r)) f"
1.6113 +    by (rule continuous_on_subset [OF contf]) (force simp add: cball_def sphere_def less_imp_le)
1.6114 +  obtain X where X: "((\<lambda>u. f u / (u - w) ^ Suc (Suc k)) has_contour_integral X) (circlepath z r)"
1.6115 +    using Cauchy_next_derivative_circlepath(1) [OF f Suc.IH _ Suc.prems]
1.6116 +    by (auto simp: contour_integrable_on_def)
1.6117 +  then have con: "contour_integral (circlepath z r) ((\<lambda>u. f u / (u - w) ^ Suc (Suc k))) = X"
1.6118 +    by (rule contour_integral_unique)
1.6119 +  have "\<And>n. ((deriv ^^ n) f has_field_derivative deriv ((deriv ^^ n) f) w) (at w)"
1.6120 +    using Suc.prems assms has_field_derivative_higher_deriv by auto
1.6121 +  then have dnf_diff: "\<And>n. (deriv ^^ n) f field_differentiable (at w)"
1.6122 +    by (force simp add: field_differentiable_def)
1.6123 +  have "deriv (\<lambda>w. complex_of_real (2 * pi) * \<i> / (fact k) * (deriv ^^ k) f w) w =
1.6124 +          of_nat (Suc k) * contour_integral (circlepath z r) (\<lambda>u. f u / (u - w) ^ Suc (Suc k))"
1.6125 +    by (force intro!: DERIV_imp_deriv Cauchy_next_derivative_circlepath [OF f Suc.IH _ Suc.prems])
1.6126 +  also have "... = of_nat (Suc k) * X"
1.6127 +    by (simp only: con)
1.6128 +  finally have "deriv (\<lambda>w. ((2 * pi) * \<i> / (fact k)) * (deriv ^^ k) f w) w = of_nat (Suc k) * X" .
1.6129 +  then have "((2 * pi) * \<i> / (fact k)) * deriv (\<lambda>w. (deriv ^^ k) f w) w = of_nat (Suc k) * X"
1.6130 +    by (metis deriv_cmult dnf_diff)
1.6131 +  then have "deriv (\<lambda>w. (deriv ^^ k) f w) w = of_nat (Suc k) * X / ((2 * pi) * \<i> / (fact k))"
1.6132 +    by (simp add: field_simps)
1.6133 +  then show ?case
1.6134 +  using of_nat_eq_0_iff X by fastforce
1.6135 +qed
1.6136 +
1.6137 +proposition Cauchy_higher_derivative_integral_circlepath:
1.6138 +  assumes contf: "continuous_on (cball z r) f"
1.6139 +      and holf: "f holomorphic_on ball z r"
1.6140 +      and w: "w \<in> ball z r"
1.6141 +    shows "(\<lambda>u. f u / (u - w)^(Suc k)) contour_integrable_on (circlepath z r)"
1.6142 +           (is "?thes1")
1.6143 +      and "(deriv ^^ k) f w = (fact k) / (2 * pi * \<i>) * contour_integral(circlepath z r) (\<lambda>u. f u/(u - w)^(Suc k))"
1.6144 +           (is "?thes2")
1.6145 +proof -
1.6146 +  have *: "((\<lambda>u. f u / (u - w) ^ Suc k) has_contour_integral (2 * pi) * \<i> / (fact k) * (deriv ^^ k) f w)
1.6147 +           (circlepath z r)"
1.6148 +    using Cauchy_has_contour_integral_higher_derivative_circlepath [OF assms]
1.6149 +    by simp
1.6150 +  show ?thes1 using *
1.6151 +    using contour_integrable_on_def by blast
1.6152 +  show ?thes2
1.6153 +    unfolding contour_integral_unique [OF *] by (simp add: divide_simps)
1.6154 +qed
1.6155 +
1.6156 +corollary Cauchy_contour_integral_circlepath:
1.6157 +  assumes "continuous_on (cball z r) f" "f holomorphic_on ball z r" "w \<in> ball z r"
1.6158 +    shows "contour_integral(circlepath z r) (\<lambda>u. f u/(u - w)^(Suc k)) = (2 * pi * \<i>) * (deriv ^^ k) f w / (fact k)"
1.6159 +by (simp add: Cauchy_higher_derivative_integral_circlepath [OF assms])
1.6160 +
1.6161 +corollary Cauchy_contour_integral_circlepath_2:
1.6162 +  assumes "continuous_on (cball z r) f" "f holomorphic_on ball z r" "w \<in> ball z r"
1.6163 +    shows "contour_integral(circlepath z r) (\<lambda>u. f u/(u - w)^2) = (2 * pi * \<i>) * deriv f w"
1.6164 +  using Cauchy_contour_integral_circlepath [OF assms, of 1]
1.6165 +  by (simp add: power2_eq_square)
1.6166 +
1.6167 +
1.6168 +subsection\<open>A holomorphic function is analytic, i.e. has local power series.\<close>
1.6169 +
1.6170 +theorem holomorphic_power_series:
1.6171 +  assumes holf: "f holomorphic_on ball z r"
1.6172 +      and w: "w \<in> ball z r"
1.6173 +    shows "((\<lambda>n. (deriv ^^ n) f z / (fact n) * (w - z)^n) sums f w)"
1.6174 +proof -
1.6175 +  have fh': "f holomorphic_on cball z ((r + dist w z) / 2)"
1.6176 +     apply (rule holomorphic_on_subset [OF holf])
1.6177 +     apply (clarsimp simp del: divide_const_simps)
1.6178 +     apply (metis add.commute dist_commute le_less_trans mem_ball real_gt_half_sum w)
1.6179 +     done
1.6180 +  \<comment>\<open>Replacing @{term r} and the original (weak) premises\<close>
1.6181 +  obtain r where "0 < r" and holfc: "f holomorphic_on cball z r" and w: "w \<in> ball z r"
1.6182 +    apply (rule that [of "(r + dist w z) / 2"])
1.6183 +      apply (simp_all add: fh')
1.6184 +     apply (metis add_0_iff ball_eq_empty dist_nz dist_self empty_iff not_less pos_add_strict w)
1.6185 +    apply (metis add_less_cancel_right dist_commute mem_ball mult_2_right w)
1.6186 +    done
1.6187 +  then have holf: "f holomorphic_on ball z r" and contf: "continuous_on (cball z r) f"
1.6188 +    using ball_subset_cball holomorphic_on_subset apply blast
1.6189 +    by (simp add: holfc holomorphic_on_imp_continuous_on)
1.6190 +  have cint: "\<And>k. (\<lambda>u. f u / (u - z) ^ Suc k) contour_integrable_on circlepath z r"
1.6191 +    apply (rule Cauchy_higher_derivative_integral_circlepath [OF contf holf])
1.6192 +    apply (simp add: \<open>0 < r\<close>)
1.6193 +    done
1.6194 +  obtain B where "0 < B" and B: "\<And>u. u \<in> cball z r \<Longrightarrow> norm(f u) \<le> B"
1.6195 +    by (metis (no_types) bounded_pos compact_cball compact_continuous_image compact_imp_bounded contf image_eqI)
1.6196 +  obtain k where k: "0 < k" "k \<le> r" and wz_eq: "norm(w - z) = r - k"
1.6197 +             and kle: "\<And>u. norm(u - z) = r \<Longrightarrow> k \<le> norm(u - w)"
1.6198 +    apply (rule_tac k = "r - dist z w" in that)
1.6199 +    using w
1.6200 +    apply (auto simp: dist_norm norm_minus_commute)
1.6202 +  have *: "\<forall>\<^sub>F n in sequentially. \<forall>x\<in>path_image (circlepath z r).
1.6203 +                norm ((\<Sum>k<n. (w - z) ^ k * (f x / (x - z) ^ Suc k)) - f x / (x - w)) < e"
1.6204 +          if "0 < e" for e
1.6205 +  proof -
1.6206 +    have rr: "0 \<le> (r - k) / r" "(r - k) / r < 1" using  k by auto
1.6207 +    obtain n where n: "((r - k) / r) ^ n < e / B * k"
1.6208 +      using real_arch_pow_inv [of "e/B*k" "(r - k)/r"] \<open>0 < e\<close> \<open>0 < B\<close> k by force
1.6209 +    have "norm ((\<Sum>k<N. (w - z) ^ k * f u / (u - z) ^ Suc k) - f u / (u - w)) < e"
1.6210 +         if "n \<le> N" and r: "r = dist z u"  for N u
1.6211 +    proof -
1.6212 +      have N: "((r - k) / r) ^ N < e / B * k"
1.6213 +        apply (rule le_less_trans [OF power_decreasing n])
1.6214 +        using  \<open>n \<le> N\<close> k by auto
1.6215 +      have u [simp]: "(u \<noteq> z) \<and> (u \<noteq> w)"
1.6216 +        using \<open>0 < r\<close> r w by auto
1.6217 +      have wzu_not1: "(w - z) / (u - z) \<noteq> 1"
1.6218 +        by (metis (no_types) dist_norm divide_eq_1_iff less_irrefl mem_ball norm_minus_commute r w)
1.6219 +      have "norm ((\<Sum>k<N. (w - z) ^ k * f u / (u - z) ^ Suc k) * (u - w) - f u)
1.6220 +            = norm ((\<Sum>k<N. (((w - z) / (u - z)) ^ k)) * f u * (u - w) / (u - z) - f u)"
1.6221 +        unfolding setsum_left_distrib setsum_divide_distrib power_divide by (simp add: algebra_simps)
1.6222 +      also have "... = norm ((((w - z) / (u - z)) ^ N - 1) * (u - w) / (((w - z) / (u - z) - 1) * (u - z)) - 1) * norm (f u)"
1.6223 +        using \<open>0 < B\<close>
1.6224 +        apply (auto simp: geometric_sum [OF wzu_not1])
1.6225 +        apply (simp add: field_simps norm_mult [symmetric])
1.6226 +        done
1.6227 +      also have "... = norm ((u-z) ^ N * (w - u) - ((w - z) ^ N - (u-z) ^ N) * (u-w)) / (r ^ N * norm (u-w)) * norm (f u)"
1.6228 +        using \<open>0 < r\<close> r by (simp add: divide_simps norm_mult norm_divide norm_power dist_norm norm_minus_commute)
1.6229 +      also have "... = norm ((w - z) ^ N * (w - u)) / (r ^ N * norm (u - w)) * norm (f u)"
1.6230 +        by (simp add: algebra_simps)
1.6231 +      also have "... = norm (w - z) ^ N * norm (f u) / r ^ N"
1.6232 +        by (simp add: norm_mult norm_power norm_minus_commute)
1.6233 +      also have "... \<le> (((r - k)/r)^N) * B"
1.6234 +        using \<open>0 < r\<close> w k
1.6235 +        apply (simp add: divide_simps)
1.6236 +        apply (rule mult_mono [OF power_mono])
1.6237 +        apply (auto simp: norm_divide wz_eq norm_power dist_norm norm_minus_commute B r)
1.6238 +        done
1.6239 +      also have "... < e * k"
1.6240 +        using \<open>0 < B\<close> N by (simp add: divide_simps)
1.6241 +      also have "... \<le> e * norm (u - w)"
1.6242 +        using r kle \<open>0 < e\<close> by (simp add: dist_commute dist_norm)
1.6243 +      finally show ?thesis
1.6244 +        by (simp add: divide_simps norm_divide del: power_Suc)
1.6245 +    qed
1.6246 +    with \<open>0 < r\<close> show ?thesis
1.6247 +      by (auto simp: mult_ac less_imp_le eventually_sequentially Ball_def)
1.6248 +  qed
1.6249 +  have eq: "\<forall>\<^sub>F x in sequentially.
1.6250 +             contour_integral (circlepath z r) (\<lambda>u. \<Sum>k<x. (w - z) ^ k * (f u / (u - z) ^ Suc k)) =
1.6251 +             (\<Sum>k<x. contour_integral (circlepath z r) (\<lambda>u. f u / (u - z) ^ Suc k) * (w - z) ^ k)"
1.6252 +    apply (rule eventuallyI)
1.6253 +    apply (subst contour_integral_setsum, simp)
1.6254 +    using contour_integrable_lmul [OF cint, of "(w - z) ^ a" for a] apply (simp add: field_simps)
1.6255 +    apply (simp only: contour_integral_lmul cint algebra_simps)
1.6256 +    done
1.6257 +  have "(\<lambda>k. contour_integral (circlepath z r) (\<lambda>u. f u/(u - z)^(Suc k)) * (w - z)^k)
1.6258 +        sums contour_integral (circlepath z r) (\<lambda>u. f u/(u - w))"
1.6259 +    unfolding sums_def
1.6260 +    apply (rule Lim_transform_eventually [OF eq])
1.6261 +    apply (rule contour_integral_uniform_limit_circlepath [OF eventuallyI *])
1.6262 +    apply (rule contour_integrable_setsum, simp)
1.6263 +    apply (rule contour_integrable_lmul)
1.6264 +    apply (rule Cauchy_higher_derivative_integral_circlepath [OF contf holf])
1.6265 +    using \<open>0 < r\<close>
1.6266 +    apply auto
1.6267 +    done
1.6268 +  then have "(\<lambda>k. contour_integral (circlepath z r) (\<lambda>u. f u/(u - z)^(Suc k)) * (w - z)^k)
1.6269 +             sums (2 * of_real pi * \<i> * f w)"
1.6270 +    using w by (auto simp: dist_commute dist_norm contour_integral_unique [OF Cauchy_integral_circlepath_simple [OF holfc]])
1.6271 +  then have "(\<lambda>k. contour_integral (circlepath z r) (\<lambda>u. f u / (u - z) ^ Suc k) * (w - z)^k / (\<i> * (of_real pi * 2)))
1.6272 +            sums ((2 * of_real pi * \<i> * f w) / (\<i> * (complex_of_real pi * 2)))"
1.6273 +    by (rule sums_divide)
1.6274 +  then have "(\<lambda>n. (w - z) ^ n * contour_integral (circlepath z r) (\<lambda>u. f u / (u - z) ^ Suc n) / (\<i> * (of_real pi * 2)))
1.6275 +            sums f w"
1.6276 +    by (simp add: field_simps)
1.6277 +  then show ?thesis
1.6278 +    by (simp add: field_simps \<open>0 < r\<close> Cauchy_higher_derivative_integral_circlepath [OF contf holf])
1.6279 +qed
1.6280 +
1.6281 +
1.6282 +subsection\<open>The Liouville theorem and the Fundamental Theorem of Algebra.\<close>
1.6283 +
1.6284 +text\<open> These weak Liouville versions don't even need the derivative formula.\<close>
1.6285 +
1.6286 +lemma Liouville_weak_0:
1.6287 +  assumes holf: "f holomorphic_on UNIV" and inf: "(f \<longlongrightarrow> 0) at_infinity"
1.6288 +    shows "f z = 0"
1.6289 +proof (rule ccontr)
1.6290 +  assume fz: "f z \<noteq> 0"
1.6291 +  with inf [unfolded Lim_at_infinity, rule_format, of "norm(f z)/2"]
1.6292 +  obtain B where B: "\<And>x. B \<le> cmod x \<Longrightarrow> norm (f x) * 2 < cmod (f z)"
1.6293 +    by (auto simp: dist_norm)
1.6294 +  define R where "R = 1 + \<bar>B\<bar> + norm z"
1.6295 +  have "R > 0" unfolding R_def
1.6296 +  proof -
1.6297 +    have "0 \<le> cmod z + \<bar>B\<bar>"
1.6298 +      by (metis (full_types) add_nonneg_nonneg norm_ge_zero real_norm_def)
1.6299 +    then show "0 < 1 + \<bar>B\<bar> + cmod z"
1.6300 +      by linarith
1.6301 +  qed
1.6302 +  have *: "((\<lambda>u. f u / (u - z)) has_contour_integral 2 * complex_of_real pi * \<i> * f z) (circlepath z R)"
1.6303 +    apply (rule Cauchy_integral_circlepath)
1.6304 +    using \<open>R > 0\<close> apply (auto intro: holomorphic_on_subset [OF holf] holomorphic_on_imp_continuous_on)+
1.6305 +    done
1.6306 +  have "cmod (x - z) = R \<Longrightarrow> cmod (f x) * 2 \<le> cmod (f z)" for x
1.6307 +    apply (simp add: R_def)
1.6308 +    apply (rule less_imp_le)
1.6309 +    apply (rule B)
1.6310 +    using norm_triangle_ineq4 [of x z]
1.6311 +    apply (auto simp:)
1.6312 +    done
1.6313 +  with  \<open>R > 0\<close> fz show False
1.6314 +    using has_contour_integral_bound_circlepath [OF *, of "norm(f z)/2/R"]
1.6315 +    by (auto simp: norm_mult norm_divide divide_simps)
1.6316 +qed
1.6317 +
1.6318 +proposition Liouville_weak:
1.6319 +  assumes "f holomorphic_on UNIV" and "(f \<longlongrightarrow> l) at_infinity"
1.6320 +    shows "f z = l"
1.6321 +  using Liouville_weak_0 [of "\<lambda>z. f z - l"]
1.6322 +  by (simp add: assms holomorphic_on_const holomorphic_on_diff LIM_zero)
1.6323 +
1.6324 +
1.6325 +proposition Liouville_weak_inverse:
1.6326 +  assumes "f holomorphic_on UNIV" and unbounded: "\<And>B. eventually (\<lambda>x. norm (f x) \<ge> B) at_infinity"
1.6327 +    obtains z where "f z = 0"
1.6328 +proof -
1.6329 +  { assume f: "\<And>z. f z \<noteq> 0"
1.6330 +    have 1: "(\<lambda>x. 1 / f x) holomorphic_on UNIV"
1.6331 +      by (simp add: holomorphic_on_divide holomorphic_on_const assms f)
1.6332 +    have 2: "((\<lambda>x. 1 / f x) \<longlongrightarrow> 0) at_infinity"
1.6333 +      apply (rule tendstoI [OF eventually_mono])
1.6334 +      apply (rule_tac B="2/e" in unbounded)
1.6335 +      apply (simp add: dist_norm norm_divide divide_simps mult_ac)
1.6336 +      done
1.6337 +    have False
1.6338 +      using Liouville_weak_0 [OF 1 2] f by simp
1.6339 +  }
1.6340 +  then show ?thesis
1.6341 +    using that by blast
1.6342 +qed
1.6343 +
1.6344 +
1.6345 +text\<open> In particular we get the Fundamental Theorem of Algebra.\<close>
1.6346 +
1.6347 +theorem fundamental_theorem_of_algebra:
1.6348 +    fixes a :: "nat \<Rightarrow> complex"
1.6349 +  assumes "a 0 = 0 \<or> (\<exists>i \<in> {1..n}. a i \<noteq> 0)"
1.6350 +  obtains z where "(\<Sum>i\<le>n. a i * z^i) = 0"
1.6351 +using assms
1.6352 +proof (elim disjE bexE)
1.6353 +  assume "a 0 = 0" then show ?thesis
1.6354 +    by (auto simp: that [of 0])
1.6355 +next
1.6356 +  fix i
1.6357 +  assume i: "i \<in> {1..n}" and nz: "a i \<noteq> 0"
1.6358 +  have 1: "(\<lambda>z. \<Sum>i\<le>n. a i * z^i) holomorphic_on UNIV"
1.6359 +    by (rule holomorphic_intros)+
1.6360 +  show ?thesis
1.6361 +    apply (rule Liouville_weak_inverse [OF 1])
1.6362 +    apply (rule polyfun_extremal)
1.6363 +    apply (rule nz)
1.6364 +    using i that
1.6365 +    apply (auto simp:)
1.6366 +    done
1.6367 +qed
1.6368 +
1.6369 +
1.6370 +subsection\<open> Weierstrass convergence theorem.\<close>
1.6371 +
1.6372 +proposition holomorphic_uniform_limit:
1.6373 +  assumes cont: "eventually (\<lambda>n. continuous_on (cball z r) (f n) \<and> (f n) holomorphic_on ball z r) F"
1.6374 +      and lim: "\<And>e. 0 < e \<Longrightarrow> eventually (\<lambda>n. \<forall>x \<in> cball z r. norm(f n x - g x) < e) F"
1.6375 +      and F:  "~ trivial_limit F"
1.6376 +  obtains "continuous_on (cball z r) g" "g holomorphic_on ball z r"
1.6377 +proof (cases r "0::real" rule: linorder_cases)
1.6378 +  case less then show ?thesis by (force simp add: ball_empty less_imp_le continuous_on_def holomorphic_on_def intro: that)
1.6379 +next
1.6380 +  case equal then show ?thesis
1.6381 +    by (force simp add: holomorphic_on_def continuous_on_sing intro: that)
1.6382 +next
1.6383 +  case greater
1.6384 +  have contg: "continuous_on (cball z r) g"
1.6385 +    using cont
1.6386 +    by (fastforce simp: eventually_conj_iff dist_norm intro: eventually_mono [OF lim] continuous_uniform_limit [OF F])
1.6387 +  have 1: "continuous_on (path_image (circlepath z r)) (\<lambda>x. 1 / (2 * complex_of_real pi * \<i>) * g x)"
1.6388 +    apply (rule continuous_intros continuous_on_subset [OF contg])+
1.6389 +    using \<open>0 < r\<close> by auto
1.6390 +  have 2: "((\<lambda>u. 1 / (2 * of_real pi * \<i>) * g u / (u - w) ^ 1) has_contour_integral g w) (circlepath z r)"
1.6391 +       if w: "w \<in> ball z r" for w
1.6392 +  proof -
1.6393 +    define d where "d = (r - norm(w - z))"
1.6394 +    have "0 < d"  "d \<le> r" using w by (auto simp: norm_minus_commute d_def dist_norm)
1.6395 +    have dle: "\<And>u. cmod (z - u) = r \<Longrightarrow> d \<le> cmod (u - w)"
1.6397 +    have ev_int: "\<forall>\<^sub>F n in F. (\<lambda>u. f n u / (u - w)) contour_integrable_on circlepath z r"
1.6398 +      apply (rule eventually_mono [OF cont])
1.6399 +      using w
1.6400 +      apply (auto intro: Cauchy_higher_derivative_integral_circlepath [where k=0, simplified])
1.6401 +      done
1.6402 +    have ev_less: "\<forall>\<^sub>F n in F. \<forall>x\<in>path_image (circlepath z r). cmod (f n x / (x - w) - g x / (x - w)) < e"
1.6403 +         if "e > 0" for e
1.6404 +      using greater \<open>0 < d\<close> \<open>0 < e\<close>
1.6405 +      apply (simp add: norm_divide diff_divide_distrib [symmetric] divide_simps)
1.6406 +      apply (rule_tac e1="e * d" in eventually_mono [OF lim])
1.6407 +      apply (force simp: dist_norm intro: dle mult_left_mono less_le_trans)+
1.6408 +      done
1.6409 +    have g_cint: "(\<lambda>u. g u/(u - w)) contour_integrable_on circlepath z r"
1.6410 +      by (rule contour_integral_uniform_limit_circlepath [OF ev_int ev_less F \<open>0 < r\<close>])
1.6411 +    have cif_tends_cig: "((\<lambda>n. contour_integral(circlepath z r) (\<lambda>u. f n u / (u - w))) \<longlongrightarrow> contour_integral(circlepath z r) (\<lambda>u. g u/(u - w))) F"
1.6412 +      by (rule contour_integral_uniform_limit_circlepath [OF ev_int ev_less F \<open>0 < r\<close>])
1.6413 +    have f_tends_cig: "((\<lambda>n. 2 * of_real pi * \<i> * f n w) \<longlongrightarrow> contour_integral (circlepath z r) (\<lambda>u. g u / (u - w))) F"
1.6414 +      apply (rule Lim_transform_eventually [where f = "\<lambda>n. contour_integral (circlepath z r) (\<lambda>u. f n u/(u - w))"])
1.6415 +      apply (rule eventually_mono [OF cont])
1.6416 +      apply (rule contour_integral_unique [OF Cauchy_integral_circlepath])
1.6417 +      using w
1.6418 +      apply (auto simp: norm_minus_commute dist_norm cif_tends_cig)
1.6419 +      done
1.6420 +    have "((\<lambda>n. 2 * of_real pi * \<i> * f n w) \<longlongrightarrow> 2 * of_real pi * \<i> * g w) F"
1.6421 +      apply (rule tendsto_mult_left [OF tendstoI])
1.6422 +      apply (rule eventually_mono [OF lim], assumption)
1.6423 +      using w
1.6424 +      apply (force simp add: dist_norm)
1.6425 +      done
1.6426 +    then have "((\<lambda>u. g u / (u - w)) has_contour_integral 2 * of_real pi * \<i> * g w) (circlepath z r)"
1.6427 +      using has_contour_integral_integral [OF g_cint] tendsto_unique [OF F f_tends_cig] w
1.6428 +      by (force simp add: dist_norm)
1.6429 +    then have "((\<lambda>u. g u / (2 * of_real pi * \<i> * (u - w))) has_contour_integral g w) (circlepath z r)"
1.6430 +      using has_contour_integral_div [where c = "2 * of_real pi * \<i>"]
1.6431 +      by (force simp add: field_simps)
1.6432 +    then show ?thesis
1.6433 +      by (simp add: dist_norm)
1.6434 +  qed
1.6435 +  show ?thesis
1.6436 +    using Cauchy_next_derivative_circlepath(2) [OF 1 2, simplified]
1.6437 +    by (fastforce simp add: holomorphic_on_open contg intro: that)
1.6438 +qed
1.6439 +
1.6440 +
1.6441 +text\<open> Version showing that the limit is the limit of the derivatives.\<close>
1.6442 +
1.6443 +proposition has_complex_derivative_uniform_limit:
1.6444 +  fixes z::complex
1.6445 +  assumes cont: "eventually (\<lambda>n. continuous_on (cball z r) (f n) \<and>
1.6446 +                               (\<forall>w \<in> ball z r. ((f n) has_field_derivative (f' n w)) (at w))) F"
1.6447 +      and lim: "\<And>e. 0 < e \<Longrightarrow> eventually (\<lambda>n. \<forall>x \<in> cball z r. norm(f n x - g x) < e) F"
1.6448 +      and F:  "~ trivial_limit F" and "0 < r"
1.6449 +  obtains g' where
1.6450 +      "continuous_on (cball z r) g"
1.6451 +      "\<And>w. w \<in> ball z r \<Longrightarrow> (g has_field_derivative (g' w)) (at w) \<and> ((\<lambda>n. f' n w) \<longlongrightarrow> g' w) F"
1.6452 +proof -
1.6453 +  let ?conint = "contour_integral (circlepath z r)"
1.6454 +  have g: "continuous_on (cball z r) g" "g holomorphic_on ball z r"
1.6455 +    by (rule holomorphic_uniform_limit [OF eventually_mono [OF cont] lim F];
1.6456 +             auto simp: holomorphic_on_open field_differentiable_def)+
1.6457 +  then obtain g' where g': "\<And>x. x \<in> ball z r \<Longrightarrow> (g has_field_derivative g' x) (at x)"
1.6458 +    using DERIV_deriv_iff_has_field_derivative
1.6459 +    by (fastforce simp add: holomorphic_on_open)
1.6460 +  then have derg: "\<And>x. x \<in> ball z r \<Longrightarrow> deriv g x = g' x"
1.6461 +    by (simp add: DERIV_imp_deriv)
1.6462 +  have tends_f'n_g': "((\<lambda>n. f' n w) \<longlongrightarrow> g' w) F" if w: "w \<in> ball z r" for w
1.6463 +  proof -
1.6464 +    have eq_f': "?conint (\<lambda>x. f n x / (x - w)\<^sup>2) - ?conint (\<lambda>x. g x / (x - w)\<^sup>2) = (f' n w - g' w) * (2 * of_real pi * \<i>)"
1.6465 +             if cont_fn: "continuous_on (cball z r) (f n)"
1.6466 +             and fnd: "\<And>w. w \<in> ball z r \<Longrightarrow> (f n has_field_derivative f' n w) (at w)" for n
1.6467 +    proof -
1.6468 +      have hol_fn: "f n holomorphic_on ball z r"
1.6469 +        using fnd by (force simp add: holomorphic_on_open)
1.6470 +      have "(f n has_field_derivative 1 / (2 * of_real pi * \<i>) * ?conint (\<lambda>u. f n u / (u - w)\<^sup>2)) (at w)"
1.6471 +        by (rule Cauchy_derivative_integral_circlepath [OF cont_fn hol_fn w])
1.6472 +      then have f': "f' n w = 1 / (2 * of_real pi * \<i>) * ?conint (\<lambda>u. f n u / (u - w)\<^sup>2)"
1.6473 +        using DERIV_unique [OF fnd] w by blast
1.6474 +      show ?thesis
1.6475 +        by (simp add: f' Cauchy_contour_integral_circlepath_2 [OF g w] derg [OF w] divide_simps)
1.6476 +    qed
1.6477 +    define d where "d = (r - norm(w - z))^2"
1.6478 +    have "d > 0"
1.6479 +      using w by (simp add: dist_commute dist_norm d_def)
1.6480 +    have dle: "\<And>y. r = cmod (z - y) \<Longrightarrow> d \<le> cmod ((y - w)\<^sup>2)"
1.6481 +      apply (simp add: d_def norm_power)
1.6482 +      apply (rule power_mono)
1.6484 +      apply (metis diff_ge_0_iff_ge dist_commute dist_norm less_eq_real_def mem_ball w)
1.6485 +      done
1.6486 +    have 1: "\<forall>\<^sub>F n in F. (\<lambda>x. f n x / (x - w)\<^sup>2) contour_integrable_on circlepath z r"
1.6487 +      by (force simp add: holomorphic_on_open intro: w Cauchy_derivative_integral_circlepath eventually_mono [OF cont])
1.6488 +    have 2: "0 < e \<Longrightarrow> \<forall>\<^sub>F n in F. \<forall>x \<in> path_image (circlepath z r). cmod (f n x / (x - w)\<^sup>2 - g x / (x - w)\<^sup>2) < e" for e
1.6489 +      using \<open>r > 0\<close>
1.6490 +      apply (simp add: diff_divide_distrib [symmetric] norm_divide divide_simps sphere_def)
1.6491 +      apply (rule eventually_mono [OF lim, of "e*d"])
1.6492 +      apply (simp add: \<open>0 < d\<close>)
1.6493 +      apply (force simp add: dist_norm dle intro: less_le_trans)
1.6494 +      done
1.6495 +    have "((\<lambda>n. contour_integral (circlepath z r) (\<lambda>x. f n x / (x - w)\<^sup>2))
1.6496 +             \<longlongrightarrow> contour_integral (circlepath z r) ((\<lambda>x. g x / (x - w)\<^sup>2))) F"
1.6497 +      by (rule contour_integral_uniform_limit_circlepath [OF 1 2 F \<open>0 < r\<close>])
1.6498 +    then have tendsto_0: "((\<lambda>n. 1 / (2 * of_real pi * \<i>) * (?conint (\<lambda>x. f n x / (x - w)\<^sup>2) - ?conint (\<lambda>x. g x / (x - w)\<^sup>2))) \<longlongrightarrow> 0) F"
1.6499 +      using Lim_null by (force intro!: tendsto_mult_right_zero)
1.6500 +    have "((\<lambda>n. f' n w - g' w) \<longlongrightarrow> 0) F"
1.6501 +      apply (rule Lim_transform_eventually [OF _ tendsto_0])
1.6502 +      apply (force simp add: divide_simps intro: eq_f' eventually_mono [OF cont])
1.6503 +      done
1.6504 +    then show ?thesis using Lim_null by blast
1.6505 +  qed
1.6506 +  obtain g' where "\<And>w. w \<in> ball z r \<Longrightarrow> (g has_field_derivative (g' w)) (at w) \<and> ((\<lambda>n. f' n w) \<longlongrightarrow> g' w) F"
1.6507 +      by (blast intro: tends_f'n_g' g' )
1.6508 +  then show ?thesis using g
1.6509 +    using that by blast
1.6510 +qed
1.6511 +
1.6512 +
1.6513 +subsection\<open>Some more simple/convenient versions for applications.\<close>
1.6514 +
1.6515 +lemma holomorphic_uniform_sequence:
1.6516 +  assumes s: "open s"
1.6517 +      and hol_fn: "\<And>n. (f n) holomorphic_on s"
1.6518 +      and to_g: "\<And>x. x \<in> s
1.6519 +                     \<Longrightarrow> \<exists>d. 0 < d \<and> cball x d \<subseteq> s \<and>
1.6520 +                             (\<forall>e. 0 < e \<longrightarrow> eventually (\<lambda>n. \<forall>y \<in> cball x d. norm(f n y - g y) < e) sequentially)"
1.6521 +  shows "g holomorphic_on s"
1.6522 +proof -
1.6523 +  have "\<exists>f'. (g has_field_derivative f') (at z)" if "z \<in> s" for z
1.6524 +  proof -
1.6525 +    obtain r where "0 < r" and r: "cball z r \<subseteq> s"
1.6526 +               and fg: "\<forall>e. 0 < e \<longrightarrow> eventually (\<lambda>n. \<forall>y \<in> cball z r. norm(f n y - g y) < e) sequentially"
1.6527 +      using to_g [OF \<open>z \<in> s\<close>] by blast
1.6528 +    have *: "\<forall>\<^sub>F n in sequentially. continuous_on (cball z r) (f n) \<and> f n holomorphic_on ball z r"
1.6529 +      apply (intro eventuallyI conjI)
1.6530 +      using hol_fn holomorphic_on_imp_continuous_on holomorphic_on_subset r apply blast
1.6531 +      apply (metis hol_fn holomorphic_on_subset interior_cball interior_subset r)
1.6532 +      done
1.6533 +    show ?thesis
1.6534 +      apply (rule holomorphic_uniform_limit [OF *])
1.6535 +      using \<open>0 < r\<close> centre_in_ball fg
1.6536 +      apply (auto simp: holomorphic_on_open)
1.6537 +      done
1.6538 +  qed
1.6539 +  with s show ?thesis
1.6540 +    by (simp add: holomorphic_on_open)
1.6541 +qed
1.6542 +
1.6543 +lemma has_complex_derivative_uniform_sequence:
1.6544 +  fixes s :: "complex set"
1.6545 +  assumes s: "open s"
1.6546 +      and hfd: "\<And>n x. x \<in> s \<Longrightarrow> ((f n) has_field_derivative f' n x) (at x)"
1.6547 +      and to_g: "\<And>x. x \<in> s
1.6548 +             \<Longrightarrow> \<exists>d. 0 < d \<and> cball x d \<subseteq> s \<and>
1.6549 +                     (\<forall>e. 0 < e \<longrightarrow> eventually (\<lambda>n. \<forall>y \<in> cball x d. norm(f n y - g y) < e) sequentially)"
1.6550 +  shows "\<exists>g'. \<forall>x \<in> s. (g has_field_derivative g' x) (at x) \<and> ((\<lambda>n. f' n x) \<longlongrightarrow> g' x) sequentially"
1.6551 +proof -
1.6552 +  have y: "\<exists>y. (g has_field_derivative y) (at z) \<and> (\<lambda>n. f' n z) \<longlonglongrightarrow> y" if "z \<in> s" for z
1.6553 +  proof -
1.6554 +    obtain r where "0 < r" and r: "cball z r \<subseteq> s"
1.6555 +               and fg: "\<forall>e. 0 < e \<longrightarrow> eventually (\<lambda>n. \<forall>y \<in> cball z r. norm(f n y - g y) < e) sequentially"
1.6556 +      using to_g [OF \<open>z \<in> s\<close>] by blast
1.6557 +    have *: "\<forall>\<^sub>F n in sequentially. continuous_on (cball z r) (f n) \<and>
1.6558 +                                   (\<forall>w \<in> ball z r. ((f n) has_field_derivative (f' n w)) (at w))"
1.6559 +      apply (intro eventuallyI conjI)
1.6560 +      apply (meson hfd holomorphic_on_imp_continuous_on holomorphic_on_open holomorphic_on_subset r s)
1.6561 +      using ball_subset_cball hfd r apply blast
1.6562 +      done
1.6563 +    show ?thesis
1.6564 +      apply (rule has_complex_derivative_uniform_limit [OF *, of g])
1.6565 +      using \<open>0 < r\<close> centre_in_ball fg
1.6566 +      apply force+
1.6567 +      done
1.6568 +  qed
1.6569 +  show ?thesis
1.6570 +    by (rule bchoice) (blast intro: y)
1.6571 +qed
1.6572 +
1.6573 +
1.6574 +subsection\<open>On analytic functions defined by a series.\<close>
1.6575 +
1.6576 +lemma series_and_derivative_comparison:
1.6577 +  fixes s :: "complex set"
1.6578 +  assumes s: "open s"
1.6579 +      and h: "summable h"
1.6580 +      and hfd: "\<And>n x. x \<in> s \<Longrightarrow> (f n has_field_derivative f' n x) (at x)"
1.6581 +      and to_g: "\<And>n x. \<lbrakk>N \<le> n; x \<in> s\<rbrakk> \<Longrightarrow> norm(f n x) \<le> h n"
1.6582 +  obtains g g' where "\<forall>x \<in> s. ((\<lambda>n. f n x) sums g x) \<and> ((\<lambda>n. f' n x) sums g' x) \<and> (g has_field_derivative g' x) (at x)"
1.6583 +proof -
1.6584 +  obtain g where g: "\<And>e. e>0 \<Longrightarrow> \<exists>N. \<forall>n x. N \<le> n \<and> x \<in> s \<longrightarrow> dist (\<Sum>n<n. f n x) (g x) < e"
1.6585 +    using series_comparison_uniform [OF h to_g, of N s] by force
1.6586 +  have *: "\<exists>d>0. cball x d \<subseteq> s \<and> (\<forall>e>0. \<forall>\<^sub>F n in sequentially. \<forall>y\<in>cball x d. cmod ((\<Sum>a<n. f a y) - g y) < e)"
1.6587 +         if "x \<in> s" for x
1.6588 +  proof -
1.6589 +    obtain d where "d>0" and d: "cball x d \<subseteq> s"
1.6590 +      using open_contains_cball [of "s"] \<open>x \<in> s\<close> s by blast
1.6591 +    then show ?thesis
1.6592 +      apply (rule_tac x=d in exI)
1.6593 +      apply (auto simp: dist_norm eventually_sequentially)
1.6594 +      apply (metis g contra_subsetD dist_norm)
1.6595 +      done
1.6596 +  qed
1.6597 +  have "(\<forall>x\<in>s. (\<lambda>n. \<Sum>i<n. f i x) \<longlonglongrightarrow> g x)"
1.6598 +    using g by (force simp add: lim_sequentially)
1.6599 +  moreover have "\<exists>g'. \<forall>x\<in>s. (g has_field_derivative g' x) (at x) \<and> (\<lambda>n. \<Sum>i<n. f' i x) \<longlonglongrightarrow> g' x"
1.6600 +    by (rule has_complex_derivative_uniform_sequence [OF s]) (auto intro: * hfd DERIV_setsum)+
1.6601 +  ultimately show ?thesis
1.6602 +    by (force simp add: sums_def  conj_commute intro: that)
1.6603 +qed
1.6604 +
1.6605 +text\<open>A version where we only have local uniform/comparative convergence.\<close>
1.6606 +
1.6607 +lemma series_and_derivative_comparison_local:
1.6608 +  fixes s :: "complex set"
1.6609 +  assumes s: "open s"
1.6610 +      and hfd: "\<And>n x. x \<in> s \<Longrightarrow> (f n has_field_derivative f' n x) (at x)"
1.6611 +      and to_g: "\<And>x. x \<in> s \<Longrightarrow>
1.6612 +                      \<exists>d h N. 0 < d \<and> summable h \<and> (\<forall>n y. N \<le> n \<and> y \<in> ball x d \<longrightarrow> norm(f n y) \<le> h n)"
1.6613 +  shows "\<exists>g g'. \<forall>x \<in> s. ((\<lambda>n. f n x) sums g x) \<and> ((\<lambda>n. f' n x) sums g' x) \<and> (g has_field_derivative g' x) (at x)"
1.6614 +proof -
1.6615 +  have "\<exists>y. (\<lambda>n. f n z) sums (\<Sum>n. f n z) \<and> (\<lambda>n. f' n z) sums y \<and> ((\<lambda>x. \<Sum>n. f n x) has_field_derivative y) (at z)"
1.6616 +       if "z \<in> s" for z
1.6617 +  proof -
1.6618 +    obtain d h N where "0 < d" "summable h" and le_h: "\<And>n y. \<lbrakk>N \<le> n; y \<in> ball z d\<rbrakk> \<Longrightarrow> norm(f n y) \<le> h n"
1.6619 +      using to_g \<open>z \<in> s\<close> by meson
1.6620 +    then obtain r where "r>0" and r: "ball z r \<subseteq> ball z d \<inter> s" using \<open>z \<in> s\<close> s
1.6621 +      by (metis Int_iff open_ball centre_in_ball open_Int open_contains_ball_eq)
1.6622 +    have 1: "open (ball z d \<inter> s)"
1.6623 +      by (simp add: open_Int s)
1.6624 +    have 2: "\<And>n x. x \<in> ball z d \<inter> s \<Longrightarrow> (f n has_field_derivative f' n x) (at x)"
1.6625 +      by (auto simp: hfd)
1.6626 +    obtain g g' where gg': "\<forall>x \<in> ball z d \<inter> s. ((\<lambda>n. f n x) sums g x) \<and>
1.6627 +                                    ((\<lambda>n. f' n x) sums g' x) \<and> (g has_field_derivative g' x) (at x)"
1.6628 +      by (auto intro: le_h series_and_derivative_comparison [OF 1 \<open>summable h\<close> hfd])
1.6629 +    then have "(\<lambda>n. f' n z) sums g' z"
1.6630 +      by (meson \<open>0 < r\<close> centre_in_ball contra_subsetD r)
1.6631 +    moreover have "(\<lambda>n. f n z) sums (\<Sum>n. f n z)"
1.6632 +      by (metis summable_comparison_test' summable_sums centre_in_ball \<open>0 < d\<close> \<open>summable h\<close> le_h)
1.6633 +    moreover have "((\<lambda>x. \<Sum>n. f n x) has_field_derivative g' z) (at z)"
1.6634 +      apply (rule_tac f=g in DERIV_transform_at [OF _ \<open>0 < r\<close>])
1.6635 +      apply (simp add: gg' \<open>z \<in> s\<close> \<open>0 < d\<close>)
1.6636 +      apply (metis (full_types) contra_subsetD dist_commute gg' mem_ball r sums_unique)
1.6637 +      done
1.6638 +    ultimately show ?thesis by auto
1.6639 +  qed
1.6640 +  then show ?thesis
1.6641 +    by (rule_tac x="\<lambda>x. suminf  (\<lambda>n. f n x)" in exI) meson
1.6642 +qed
1.6643 +
1.6644 +
1.6645 +text\<open>Sometimes convenient to compare with a complex series of positive reals. (?)\<close>
1.6646 +
1.6647 +lemma series_and_derivative_comparison_complex:
1.6648 +  fixes s :: "complex set"
1.6649 +  assumes s: "open s"
1.6650 +      and hfd: "\<And>n x. x \<in> s \<Longrightarrow> (f n has_field_derivative f' n x) (at x)"
1.6651 +      and to_g: "\<And>x. x \<in> s \<Longrightarrow>
1.6652 +                      \<exists>d h N. 0 < d \<and> summable h \<and> range h \<subseteq> nonneg_Reals \<and> (\<forall>n y. N \<le> n \<and> y \<in> ball x d \<longrightarrow> cmod(f n y) \<le> cmod (h n))"
1.6653 +  shows "\<exists>g g'. \<forall>x \<in> s. ((\<lambda>n. f n x) sums g x) \<and> ((\<lambda>n. f' n x) sums g' x) \<and> (g has_field_derivative g' x) (at x)"
1.6654 +apply (rule series_and_derivative_comparison_local [OF s hfd], assumption)
1.6655 +apply (frule to_g)
1.6656 +apply (erule ex_forward)
1.6657 +apply (erule exE)
1.6658 +apply (rule_tac x="Re o h" in exI)
1.6659 +apply (erule ex_forward)
1.6660 +apply (simp add: summable_Re o_def )
1.6661 +apply (elim conjE all_forward)
1.6662 +apply (simp add: nonneg_Reals_cmod_eq_Re image_subset_iff)
1.6663 +done
```