src/HOL/Analysis/Cauchy_Integral_Theorem.thy
author wenzelm
Mon Mar 25 17:21:26 2019 +0100 (4 weeks ago)
changeset 69981 3dced198b9ec
parent 69922 4a9167f377b0
child 70136 f03a01a18c6e
permissions -rw-r--r--
more strict AFP properties;
     1 section \<open>Complex Path Integrals and Cauchy's Integral Theorem\<close>
     2 
     3 text\<open>By John Harrison et al.  Ported from HOL Light by L C Paulson (2015)\<close>
     4 
     5 theory Cauchy_Integral_Theorem
     6 imports Complex_Transcendental Weierstrass_Theorems Ordered_Euclidean_Space
     7 begin
     8 
     9 subsection%unimportant \<open>Homeomorphisms of arc images\<close>
    10 
    11 lemma homeomorphism_arc:
    12   fixes g :: "real \<Rightarrow> 'a::t2_space"
    13   assumes "arc g"
    14   obtains h where "homeomorphism {0..1} (path_image g) g h"
    15 using assms by (force simp: arc_def homeomorphism_compact path_def path_image_def)
    16 
    17 lemma homeomorphic_arc_image_interval:
    18   fixes g :: "real \<Rightarrow> 'a::t2_space" and a::real
    19   assumes "arc g" "a < b"
    20   shows "(path_image g) homeomorphic {a..b}"
    21 proof -
    22   have "(path_image g) homeomorphic {0..1::real}"
    23     by (meson assms(1) homeomorphic_def homeomorphic_sym homeomorphism_arc)
    24   also have "\<dots> homeomorphic {a..b}"
    25     using assms by (force intro: homeomorphic_closed_intervals_real)
    26   finally show ?thesis .
    27 qed
    28 
    29 lemma homeomorphic_arc_images:
    30   fixes g :: "real \<Rightarrow> 'a::t2_space" and h :: "real \<Rightarrow> 'b::t2_space"
    31   assumes "arc g" "arc h"
    32   shows "(path_image g) homeomorphic (path_image h)"
    33 proof -
    34   have "(path_image g) homeomorphic {0..1::real}"
    35     by (meson assms homeomorphic_def homeomorphic_sym homeomorphism_arc)
    36   also have "\<dots> homeomorphic (path_image h)"
    37     by (meson assms homeomorphic_def homeomorphism_arc)
    38   finally show ?thesis .
    39 qed
    40 
    41 lemma path_connected_arc_complement:
    42   fixes \<gamma> :: "real \<Rightarrow> 'a::euclidean_space"
    43   assumes "arc \<gamma>" "2 \<le> DIM('a)"
    44   shows "path_connected(- path_image \<gamma>)"
    45 proof -
    46   have "path_image \<gamma> homeomorphic {0..1::real}"
    47     by (simp add: assms homeomorphic_arc_image_interval)
    48   then
    49   show ?thesis
    50     apply (rule path_connected_complement_homeomorphic_convex_compact)
    51       apply (auto simp: assms)
    52     done
    53 qed
    54 
    55 lemma connected_arc_complement:
    56   fixes \<gamma> :: "real \<Rightarrow> 'a::euclidean_space"
    57   assumes "arc \<gamma>" "2 \<le> DIM('a)"
    58   shows "connected(- path_image \<gamma>)"
    59   by (simp add: assms path_connected_arc_complement path_connected_imp_connected)
    60 
    61 lemma inside_arc_empty:
    62   fixes \<gamma> :: "real \<Rightarrow> 'a::euclidean_space"
    63   assumes "arc \<gamma>"
    64     shows "inside(path_image \<gamma>) = {}"
    65 proof (cases "DIM('a) = 1")
    66   case True
    67   then show ?thesis
    68     using assms connected_arc_image connected_convex_1_gen inside_convex by blast
    69 next
    70   case False
    71   show ?thesis
    72   proof (rule inside_bounded_complement_connected_empty)
    73     show "connected (- path_image \<gamma>)"
    74       apply (rule connected_arc_complement [OF assms])
    75       using False
    76       by (metis DIM_ge_Suc0 One_nat_def Suc_1 not_less_eq_eq order_class.order.antisym)
    77     show "bounded (path_image \<gamma>)"
    78       by (simp add: assms bounded_arc_image)
    79   qed
    80 qed
    81 
    82 lemma inside_simple_curve_imp_closed:
    83   fixes \<gamma> :: "real \<Rightarrow> 'a::euclidean_space"
    84     shows "\<lbrakk>simple_path \<gamma>; x \<in> inside(path_image \<gamma>)\<rbrakk> \<Longrightarrow> pathfinish \<gamma> = pathstart \<gamma>"
    85   using arc_simple_path  inside_arc_empty by blast
    86 
    87 
    88 subsection%unimportant \<open>Piecewise differentiable functions\<close>
    89 
    90 definition piecewise_differentiable_on
    91            (infixr "piecewise'_differentiable'_on" 50)
    92   where "f piecewise_differentiable_on i  \<equiv>
    93            continuous_on i f \<and>
    94            (\<exists>S. finite S \<and> (\<forall>x \<in> i - S. f differentiable (at x within i)))"
    95 
    96 lemma piecewise_differentiable_on_imp_continuous_on:
    97     "f piecewise_differentiable_on S \<Longrightarrow> continuous_on S f"
    98 by (simp add: piecewise_differentiable_on_def)
    99 
   100 lemma piecewise_differentiable_on_subset:
   101     "f piecewise_differentiable_on S \<Longrightarrow> T \<le> S \<Longrightarrow> f piecewise_differentiable_on T"
   102   using continuous_on_subset
   103   unfolding piecewise_differentiable_on_def
   104   apply safe
   105   apply (blast elim: continuous_on_subset)
   106   by (meson Diff_iff differentiable_within_subset subsetCE)
   107 
   108 lemma differentiable_on_imp_piecewise_differentiable:
   109   fixes a:: "'a::{linorder_topology,real_normed_vector}"
   110   shows "f differentiable_on {a..b} \<Longrightarrow> f piecewise_differentiable_on {a..b}"
   111   apply (simp add: piecewise_differentiable_on_def differentiable_imp_continuous_on)
   112   apply (rule_tac x="{a,b}" in exI, simp add: differentiable_on_def)
   113   done
   114 
   115 lemma differentiable_imp_piecewise_differentiable:
   116     "(\<And>x. x \<in> S \<Longrightarrow> f differentiable (at x within S))
   117          \<Longrightarrow> f piecewise_differentiable_on S"
   118 by (auto simp: piecewise_differentiable_on_def differentiable_imp_continuous_on differentiable_on_def
   119          intro: differentiable_within_subset)
   120 
   121 lemma piecewise_differentiable_const [iff]: "(\<lambda>x. z) piecewise_differentiable_on S"
   122   by (simp add: differentiable_imp_piecewise_differentiable)
   123 
   124 lemma piecewise_differentiable_compose:
   125     "\<lbrakk>f piecewise_differentiable_on S; g piecewise_differentiable_on (f ` S);
   126       \<And>x. finite (S \<inter> f-`{x})\<rbrakk>
   127       \<Longrightarrow> (g \<circ> f) piecewise_differentiable_on S"
   128   apply (simp add: piecewise_differentiable_on_def, safe)
   129   apply (blast intro: continuous_on_compose2)
   130   apply (rename_tac A B)
   131   apply (rule_tac x="A \<union> (\<Union>x\<in>B. S \<inter> f-`{x})" in exI)
   132   apply (blast intro!: differentiable_chain_within)
   133   done
   134 
   135 lemma piecewise_differentiable_affine:
   136   fixes m::real
   137   assumes "f piecewise_differentiable_on ((\<lambda>x. m *\<^sub>R x + c) ` S)"
   138   shows "(f \<circ> (\<lambda>x. m *\<^sub>R x + c)) piecewise_differentiable_on S"
   139 proof (cases "m = 0")
   140   case True
   141   then show ?thesis
   142     unfolding o_def
   143     by (force intro: differentiable_imp_piecewise_differentiable differentiable_const)
   144 next
   145   case False
   146   show ?thesis
   147     apply (rule piecewise_differentiable_compose [OF differentiable_imp_piecewise_differentiable])
   148     apply (rule assms derivative_intros | simp add: False vimage_def real_vector_affinity_eq)+
   149     done
   150 qed
   151 
   152 lemma piecewise_differentiable_cases:
   153   fixes c::real
   154   assumes "f piecewise_differentiable_on {a..c}"
   155           "g piecewise_differentiable_on {c..b}"
   156            "a \<le> c" "c \<le> b" "f c = g c"
   157   shows "(\<lambda>x. if x \<le> c then f x else g x) piecewise_differentiable_on {a..b}"
   158 proof -
   159   obtain S T where st: "finite S" "finite T"
   160                and fd: "\<And>x. x \<in> {a..c} - S \<Longrightarrow> f differentiable at x within {a..c}"
   161                and gd: "\<And>x. x \<in> {c..b} - T \<Longrightarrow> g differentiable at x within {c..b}"
   162     using assms
   163     by (auto simp: piecewise_differentiable_on_def)
   164   have finabc: "finite ({a,b,c} \<union> (S \<union> T))"
   165     by (metis \<open>finite S\<close> \<open>finite T\<close> finite_Un finite_insert finite.emptyI)
   166   have "continuous_on {a..c} f" "continuous_on {c..b} g"
   167     using assms piecewise_differentiable_on_def by auto
   168   then have "continuous_on {a..b} (\<lambda>x. if x \<le> c then f x else g x)"
   169     using continuous_on_cases [OF closed_real_atLeastAtMost [of a c],
   170                                OF closed_real_atLeastAtMost [of c b],
   171                                of f g "\<lambda>x. x\<le>c"]  assms
   172     by (force simp: ivl_disj_un_two_touch)
   173   moreover
   174   { fix x
   175     assume x: "x \<in> {a..b} - ({a,b,c} \<union> (S \<union> T))"
   176     have "(\<lambda>x. if x \<le> c then f x else g x) differentiable at x within {a..b}" (is "?diff_fg")
   177     proof (cases x c rule: le_cases)
   178       case le show ?diff_fg
   179       proof (rule differentiable_transform_within [where d = "dist x c"])
   180         have "f differentiable at x"
   181           using x le fd [of x] at_within_interior [of x "{a..c}"] by simp
   182         then show "f differentiable at x within {a..b}"
   183           by (simp add: differentiable_at_withinI)
   184       qed (use x le st dist_real_def in auto)
   185     next
   186       case ge show ?diff_fg
   187       proof (rule differentiable_transform_within [where d = "dist x c"])
   188         have "g differentiable at x"
   189           using x ge gd [of x] at_within_interior [of x "{c..b}"] by simp
   190         then show "g differentiable at x within {a..b}"
   191           by (simp add: differentiable_at_withinI)
   192       qed (use x ge st dist_real_def in auto)
   193     qed
   194   }
   195   then have "\<exists>S. finite S \<and>
   196                  (\<forall>x\<in>{a..b} - S. (\<lambda>x. if x \<le> c then f x else g x) differentiable at x within {a..b})"
   197     by (meson finabc)
   198   ultimately show ?thesis
   199     by (simp add: piecewise_differentiable_on_def)
   200 qed
   201 
   202 lemma piecewise_differentiable_neg:
   203     "f piecewise_differentiable_on S \<Longrightarrow> (\<lambda>x. -(f x)) piecewise_differentiable_on S"
   204   by (auto simp: piecewise_differentiable_on_def continuous_on_minus)
   205 
   206 lemma piecewise_differentiable_add:
   207   assumes "f piecewise_differentiable_on i"
   208           "g piecewise_differentiable_on i"
   209     shows "(\<lambda>x. f x + g x) piecewise_differentiable_on i"
   210 proof -
   211   obtain S T where st: "finite S" "finite T"
   212                        "\<forall>x\<in>i - S. f differentiable at x within i"
   213                        "\<forall>x\<in>i - T. g differentiable at x within i"
   214     using assms by (auto simp: piecewise_differentiable_on_def)
   215   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)"
   216     by auto
   217   moreover have "continuous_on i f" "continuous_on i g"
   218     using assms piecewise_differentiable_on_def by auto
   219   ultimately show ?thesis
   220     by (auto simp: piecewise_differentiable_on_def continuous_on_add)
   221 qed
   222 
   223 lemma piecewise_differentiable_diff:
   224     "\<lbrakk>f piecewise_differentiable_on S;  g piecewise_differentiable_on S\<rbrakk>
   225      \<Longrightarrow> (\<lambda>x. f x - g x) piecewise_differentiable_on S"
   226   unfolding diff_conv_add_uminus
   227   by (metis piecewise_differentiable_add piecewise_differentiable_neg)
   228 
   229 lemma continuous_on_joinpaths_D1:
   230     "continuous_on {0..1} (g1 +++ g2) \<Longrightarrow> continuous_on {0..1} g1"
   231   apply (rule continuous_on_eq [of _ "(g1 +++ g2) \<circ> ((*)(inverse 2))"])
   232   apply (rule continuous_intros | simp)+
   233   apply (auto elim!: continuous_on_subset simp: joinpaths_def)
   234   done
   235 
   236 lemma continuous_on_joinpaths_D2:
   237     "\<lbrakk>continuous_on {0..1} (g1 +++ g2); pathfinish g1 = pathstart g2\<rbrakk> \<Longrightarrow> continuous_on {0..1} g2"
   238   apply (rule continuous_on_eq [of _ "(g1 +++ g2) \<circ> (\<lambda>x. inverse 2*x + 1/2)"])
   239   apply (rule continuous_intros | simp)+
   240   apply (auto elim!: continuous_on_subset simp add: joinpaths_def pathfinish_def pathstart_def Ball_def)
   241   done
   242 
   243 lemma piecewise_differentiable_D1:
   244   assumes "(g1 +++ g2) piecewise_differentiable_on {0..1}"
   245   shows "g1 piecewise_differentiable_on {0..1}"
   246 proof -
   247   obtain S where cont: "continuous_on {0..1} g1" and "finite S"
   248     and S: "\<And>x. x \<in> {0..1} - S \<Longrightarrow> g1 +++ g2 differentiable at x within {0..1}"
   249     using assms unfolding piecewise_differentiable_on_def
   250     by (blast dest!: continuous_on_joinpaths_D1)
   251   show ?thesis
   252     unfolding piecewise_differentiable_on_def
   253   proof (intro exI conjI ballI cont)
   254     show "finite (insert 1 (((*)2) ` S))"
   255       by (simp add: \<open>finite S\<close>)
   256     show "g1 differentiable at x within {0..1}" if "x \<in> {0..1} - insert 1 ((*) 2 ` S)" for x
   257     proof (rule_tac d="dist (x/2) (1/2)" in differentiable_transform_within)
   258       have "g1 +++ g2 differentiable at (x / 2) within {0..1/2}"
   259         by (rule differentiable_subset [OF S [of "x/2"]] | use that in force)+
   260       then show "g1 +++ g2 \<circ> (*) (inverse 2) differentiable at x within {0..1}"
   261         using image_affinity_atLeastAtMost_div [of 2 0 "0::real" 1]
   262         by (auto intro: differentiable_chain_within)
   263     qed (use that in \<open>auto simp: joinpaths_def\<close>)
   264   qed
   265 qed
   266 
   267 lemma piecewise_differentiable_D2:
   268   assumes "(g1 +++ g2) piecewise_differentiable_on {0..1}" and eq: "pathfinish g1 = pathstart g2"
   269   shows "g2 piecewise_differentiable_on {0..1}"
   270 proof -
   271   have [simp]: "g1 1 = g2 0"
   272     using eq by (simp add: pathfinish_def pathstart_def)
   273   obtain S where cont: "continuous_on {0..1} g2" and "finite S"
   274     and S: "\<And>x. x \<in> {0..1} - S \<Longrightarrow> g1 +++ g2 differentiable at x within {0..1}"
   275     using assms unfolding piecewise_differentiable_on_def
   276     by (blast dest!: continuous_on_joinpaths_D2)
   277   show ?thesis
   278     unfolding piecewise_differentiable_on_def
   279   proof (intro exI conjI ballI cont)
   280     show "finite (insert 0 ((\<lambda>x. 2*x-1)`S))"
   281       by (simp add: \<open>finite S\<close>)
   282     show "g2 differentiable at x within {0..1}" if "x \<in> {0..1} - insert 0 ((\<lambda>x. 2*x-1)`S)" for x
   283     proof (rule_tac d="dist ((x+1)/2) (1/2)" in differentiable_transform_within)
   284       have x2: "(x + 1) / 2 \<notin> S"
   285         using that
   286         apply (clarsimp simp: image_iff)
   287         by (metis add.commute add_diff_cancel_left' mult_2 field_sum_of_halves)
   288       have "g1 +++ g2 \<circ> (\<lambda>x. (x+1) / 2) differentiable at x within {0..1}"
   289         by (rule differentiable_chain_within differentiable_subset [OF S [of "(x+1)/2"]] | use x2 that in force)+
   290       then show "g1 +++ g2 \<circ> (\<lambda>x. (x+1) / 2) differentiable at x within {0..1}"
   291         by (auto intro: differentiable_chain_within)
   292       show "(g1 +++ g2 \<circ> (\<lambda>x. (x + 1) / 2)) x' = g2 x'" if "x' \<in> {0..1}" "dist x' x < dist ((x + 1) / 2) (1/2)" for x'
   293       proof -
   294         have [simp]: "(2*x'+2)/2 = x'+1"
   295           by (simp add: divide_simps)
   296         show ?thesis
   297           using that by (auto simp: joinpaths_def)
   298       qed
   299     qed (use that in \<open>auto simp: joinpaths_def\<close>)
   300   qed
   301 qed
   302 
   303 
   304 subsection\<open>The concept of continuously differentiable\<close>
   305 
   306 text \<open>
   307 John Harrison writes as follows:
   308 
   309 ``The usual assumption in complex analysis texts is that a path \<open>\<gamma>\<close> should be piecewise
   310 continuously differentiable, which ensures that the path integral exists at least for any continuous
   311 f, since all piecewise continuous functions are integrable. However, our notion of validity is
   312 weaker, just piecewise differentiability\ldots{} [namely] continuity plus differentiability except on a
   313 finite set\ldots{} [Our] underlying theory of integration is the Kurzweil-Henstock theory. In contrast to
   314 the Riemann or Lebesgue theory (but in common with a simple notion based on antiderivatives), this
   315 can integrate all derivatives.''
   316 
   317 "Formalizing basic complex analysis." From Insight to Proof: Festschrift in Honour of Andrzej Trybulec.
   318 Studies in Logic, Grammar and Rhetoric 10.23 (2007): 151-165.
   319 
   320 And indeed he does not assume that his derivatives are continuous, but the penalty is unreasonably
   321 difficult proofs concerning winding numbers. We need a self-contained and straightforward theorem
   322 asserting that all derivatives can be integrated before we can adopt Harrison's choice.\<close>
   323 
   324 definition%important C1_differentiable_on :: "(real \<Rightarrow> 'a::real_normed_vector) \<Rightarrow> real set \<Rightarrow> bool"
   325            (infix "C1'_differentiable'_on" 50)
   326   where
   327   "f C1_differentiable_on S \<longleftrightarrow>
   328    (\<exists>D. (\<forall>x \<in> S. (f has_vector_derivative (D x)) (at x)) \<and> continuous_on S D)"
   329 
   330 lemma C1_differentiable_on_eq:
   331     "f C1_differentiable_on S \<longleftrightarrow>
   332      (\<forall>x \<in> S. f differentiable at x) \<and> continuous_on S (\<lambda>x. vector_derivative f (at x))"
   333      (is "?lhs = ?rhs")
   334 proof
   335   assume ?lhs
   336   then show ?rhs
   337     unfolding C1_differentiable_on_def
   338     by (metis (no_types, lifting) continuous_on_eq  differentiableI_vector vector_derivative_at)
   339 next
   340   assume ?rhs
   341   then show ?lhs
   342     using C1_differentiable_on_def vector_derivative_works by fastforce
   343 qed
   344 
   345 lemma C1_differentiable_on_subset:
   346   "f C1_differentiable_on T \<Longrightarrow> S \<subseteq> T \<Longrightarrow> f C1_differentiable_on S"
   347   unfolding C1_differentiable_on_def  continuous_on_eq_continuous_within
   348   by (blast intro:  continuous_within_subset)
   349 
   350 lemma C1_differentiable_compose:
   351   assumes fg: "f C1_differentiable_on S" "g C1_differentiable_on (f ` S)" and fin: "\<And>x. finite (S \<inter> f-`{x})"
   352   shows "(g \<circ> f) C1_differentiable_on S"
   353 proof -
   354   have "\<And>x. x \<in> S \<Longrightarrow> g \<circ> f differentiable at x"
   355     by (meson C1_differentiable_on_eq assms differentiable_chain_at imageI)
   356   moreover have "continuous_on S (\<lambda>x. vector_derivative (g \<circ> f) (at x))"
   357   proof (rule continuous_on_eq [of _ "\<lambda>x. vector_derivative f (at x) *\<^sub>R vector_derivative g (at (f x))"])
   358     show "continuous_on S (\<lambda>x. vector_derivative f (at x) *\<^sub>R vector_derivative g (at (f x)))"
   359       using fg
   360       apply (clarsimp simp add: C1_differentiable_on_eq)
   361       apply (rule Limits.continuous_on_scaleR, assumption)
   362       by (metis (mono_tags, lifting) continuous_at_imp_continuous_on continuous_on_compose continuous_on_cong differentiable_imp_continuous_within o_def)
   363     show "\<And>x. x \<in> S \<Longrightarrow> vector_derivative f (at x) *\<^sub>R vector_derivative g (at (f x)) = vector_derivative (g \<circ> f) (at x)"
   364       by (metis (mono_tags, hide_lams) C1_differentiable_on_eq fg imageI vector_derivative_chain_at)
   365   qed
   366   ultimately show ?thesis
   367     by (simp add: C1_differentiable_on_eq)
   368 qed
   369 
   370 lemma C1_diff_imp_diff: "f C1_differentiable_on S \<Longrightarrow> f differentiable_on S"
   371   by (simp add: C1_differentiable_on_eq differentiable_at_imp_differentiable_on)
   372 
   373 lemma C1_differentiable_on_ident [simp, derivative_intros]: "(\<lambda>x. x) C1_differentiable_on S"
   374   by (auto simp: C1_differentiable_on_eq continuous_on_const)
   375 
   376 lemma C1_differentiable_on_const [simp, derivative_intros]: "(\<lambda>z. a) C1_differentiable_on S"
   377   by (auto simp: C1_differentiable_on_eq continuous_on_const)
   378 
   379 lemma C1_differentiable_on_add [simp, derivative_intros]:
   380   "f C1_differentiable_on S \<Longrightarrow> g C1_differentiable_on S \<Longrightarrow> (\<lambda>x. f x + g x) C1_differentiable_on S"
   381   unfolding C1_differentiable_on_eq  by (auto intro: continuous_intros)
   382 
   383 lemma C1_differentiable_on_minus [simp, derivative_intros]:
   384   "f C1_differentiable_on S \<Longrightarrow> (\<lambda>x. - f x) C1_differentiable_on S"
   385   unfolding C1_differentiable_on_eq  by (auto intro: continuous_intros)
   386 
   387 lemma C1_differentiable_on_diff [simp, derivative_intros]:
   388   "f C1_differentiable_on S \<Longrightarrow> g C1_differentiable_on S \<Longrightarrow> (\<lambda>x. f x - g x) C1_differentiable_on S"
   389   unfolding C1_differentiable_on_eq  by (auto intro: continuous_intros)
   390 
   391 lemma C1_differentiable_on_mult [simp, derivative_intros]:
   392   fixes f g :: "real \<Rightarrow> 'a :: real_normed_algebra"
   393   shows "f C1_differentiable_on S \<Longrightarrow> g C1_differentiable_on S \<Longrightarrow> (\<lambda>x. f x * g x) C1_differentiable_on S"
   394   unfolding C1_differentiable_on_eq
   395   by (auto simp: continuous_on_add continuous_on_mult continuous_at_imp_continuous_on differentiable_imp_continuous_within)
   396 
   397 lemma C1_differentiable_on_scaleR [simp, derivative_intros]:
   398   "f C1_differentiable_on S \<Longrightarrow> g C1_differentiable_on S \<Longrightarrow> (\<lambda>x. f x *\<^sub>R g x) C1_differentiable_on S"
   399   unfolding C1_differentiable_on_eq
   400   by (rule continuous_intros | simp add: continuous_at_imp_continuous_on differentiable_imp_continuous_within)+
   401 
   402 
   403 definition%important piecewise_C1_differentiable_on
   404            (infixr "piecewise'_C1'_differentiable'_on" 50)
   405   where "f piecewise_C1_differentiable_on i  \<equiv>
   406            continuous_on i f \<and>
   407            (\<exists>S. finite S \<and> (f C1_differentiable_on (i - S)))"
   408 
   409 lemma C1_differentiable_imp_piecewise:
   410     "f C1_differentiable_on S \<Longrightarrow> f piecewise_C1_differentiable_on S"
   411   by (auto simp: piecewise_C1_differentiable_on_def C1_differentiable_on_eq continuous_at_imp_continuous_on differentiable_imp_continuous_within)
   412 
   413 lemma piecewise_C1_imp_differentiable:
   414     "f piecewise_C1_differentiable_on i \<Longrightarrow> f piecewise_differentiable_on i"
   415   by (auto simp: piecewise_C1_differentiable_on_def piecewise_differentiable_on_def
   416            C1_differentiable_on_def differentiable_def has_vector_derivative_def
   417            intro: has_derivative_at_withinI)
   418 
   419 lemma piecewise_C1_differentiable_compose:
   420   assumes fg: "f piecewise_C1_differentiable_on S" "g piecewise_C1_differentiable_on (f ` S)" and fin: "\<And>x. finite (S \<inter> f-`{x})"
   421   shows "(g \<circ> f) piecewise_C1_differentiable_on S"
   422 proof -
   423   have "continuous_on S (\<lambda>x. g (f x))"
   424     by (metis continuous_on_compose2 fg order_refl piecewise_C1_differentiable_on_def)
   425   moreover have "\<exists>T. finite T \<and> g \<circ> f C1_differentiable_on S - T"
   426   proof -
   427     obtain F where "finite F" and F: "f C1_differentiable_on S - F" and f: "f piecewise_C1_differentiable_on S"
   428       using fg by (auto simp: piecewise_C1_differentiable_on_def)
   429     obtain G where "finite G" and G: "g C1_differentiable_on f ` S - G" and g: "g piecewise_C1_differentiable_on f ` S"
   430       using fg by (auto simp: piecewise_C1_differentiable_on_def)
   431     show ?thesis
   432     proof (intro exI conjI)
   433       show "finite (F \<union> (\<Union>x\<in>G. S \<inter> f-`{x}))"
   434         using fin by (auto simp only: Int_Union \<open>finite F\<close> \<open>finite G\<close> finite_UN finite_imageI)
   435       show "g \<circ> f C1_differentiable_on S - (F \<union> (\<Union>x\<in>G. S \<inter> f -` {x}))"
   436         apply (rule C1_differentiable_compose)
   437           apply (blast intro: C1_differentiable_on_subset [OF F])
   438           apply (blast intro: C1_differentiable_on_subset [OF G])
   439         by (simp add:  C1_differentiable_on_subset G Diff_Int_distrib2 fin)
   440     qed
   441   qed
   442   ultimately show ?thesis
   443     by (simp add: piecewise_C1_differentiable_on_def)
   444 qed
   445 
   446 lemma piecewise_C1_differentiable_on_subset:
   447     "f piecewise_C1_differentiable_on S \<Longrightarrow> T \<le> S \<Longrightarrow> f piecewise_C1_differentiable_on T"
   448   by (auto simp: piecewise_C1_differentiable_on_def elim!: continuous_on_subset C1_differentiable_on_subset)
   449 
   450 lemma C1_differentiable_imp_continuous_on:
   451   "f C1_differentiable_on S \<Longrightarrow> continuous_on S f"
   452   unfolding C1_differentiable_on_eq continuous_on_eq_continuous_within
   453   using differentiable_at_withinI differentiable_imp_continuous_within by blast
   454 
   455 lemma C1_differentiable_on_empty [iff]: "f C1_differentiable_on {}"
   456   unfolding C1_differentiable_on_def
   457   by auto
   458 
   459 lemma piecewise_C1_differentiable_affine:
   460   fixes m::real
   461   assumes "f piecewise_C1_differentiable_on ((\<lambda>x. m * x + c) ` S)"
   462   shows "(f \<circ> (\<lambda>x. m *\<^sub>R x + c)) piecewise_C1_differentiable_on S"
   463 proof (cases "m = 0")
   464   case True
   465   then show ?thesis
   466     unfolding o_def by (auto simp: piecewise_C1_differentiable_on_def continuous_on_const)
   467 next
   468   case False
   469   have *: "\<And>x. finite (S \<inter> {y. m * y + c = x})"
   470     using False not_finite_existsD by fastforce
   471   show ?thesis
   472     apply (rule piecewise_C1_differentiable_compose [OF C1_differentiable_imp_piecewise])
   473     apply (rule * assms derivative_intros | simp add: False vimage_def)+
   474     done
   475 qed
   476 
   477 lemma piecewise_C1_differentiable_cases:
   478   fixes c::real
   479   assumes "f piecewise_C1_differentiable_on {a..c}"
   480           "g piecewise_C1_differentiable_on {c..b}"
   481            "a \<le> c" "c \<le> b" "f c = g c"
   482   shows "(\<lambda>x. if x \<le> c then f x else g x) piecewise_C1_differentiable_on {a..b}"
   483 proof -
   484   obtain S T where st: "f C1_differentiable_on ({a..c} - S)"
   485                        "g C1_differentiable_on ({c..b} - T)"
   486                        "finite S" "finite T"
   487     using assms
   488     by (force simp: piecewise_C1_differentiable_on_def)
   489   then have f_diff: "f differentiable_on {a..<c} - S"
   490         and g_diff: "g differentiable_on {c<..b} - T"
   491     by (simp_all add: C1_differentiable_on_eq differentiable_at_withinI differentiable_on_def)
   492   have "continuous_on {a..c} f" "continuous_on {c..b} g"
   493     using assms piecewise_C1_differentiable_on_def by auto
   494   then have cab: "continuous_on {a..b} (\<lambda>x. if x \<le> c then f x else g x)"
   495     using continuous_on_cases [OF closed_real_atLeastAtMost [of a c],
   496                                OF closed_real_atLeastAtMost [of c b],
   497                                of f g "\<lambda>x. x\<le>c"]  assms
   498     by (force simp: ivl_disj_un_two_touch)
   499   { fix x
   500     assume x: "x \<in> {a..b} - insert c (S \<union> T)"
   501     have "(\<lambda>x. if x \<le> c then f x else g x) differentiable at x" (is "?diff_fg")
   502     proof (cases x c rule: le_cases)
   503       case le show ?diff_fg
   504         apply (rule differentiable_transform_within [where f=f and d = "dist x c"])
   505         using x dist_real_def le st by (auto simp: C1_differentiable_on_eq)
   506     next
   507       case ge show ?diff_fg
   508         apply (rule differentiable_transform_within [where f=g and d = "dist x c"])
   509         using dist_nz x dist_real_def ge st x by (auto simp: C1_differentiable_on_eq)
   510     qed
   511   }
   512   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)"
   513     by auto
   514   moreover
   515   { assume fcon: "continuous_on ({a<..<c} - S) (\<lambda>x. vector_derivative f (at x))"
   516        and gcon: "continuous_on ({c<..<b} - T) (\<lambda>x. vector_derivative g (at x))"
   517     have "open ({a<..<c} - S)"  "open ({c<..<b} - T)"
   518       using st by (simp_all add: open_Diff finite_imp_closed)
   519     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))"
   520     proof -
   521       have "((\<lambda>x. if x \<le> c then f x else g x) has_vector_derivative vector_derivative f (at x))            (at x)"
   522         if "a < x" "x < c" "x \<notin> S" for x
   523       proof -
   524         have f: "f differentiable at x"
   525           by (meson C1_differentiable_on_eq Diff_iff atLeastAtMost_iff less_eq_real_def st(1) that)
   526         show ?thesis
   527           using that
   528           apply (rule_tac f=f and d="dist x c" in has_vector_derivative_transform_within)
   529              apply (auto simp: dist_norm vector_derivative_works [symmetric] f)
   530           done
   531       qed
   532       then show ?thesis
   533         by (metis (no_types, lifting) continuous_on_eq [OF fcon] DiffE greaterThanLessThan_iff vector_derivative_at)
   534     qed
   535     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))"
   536     proof -
   537       have "((\<lambda>x. if x \<le> c then f x else g x) has_vector_derivative vector_derivative g (at x))            (at x)"
   538         if "c < x" "x < b" "x \<notin> T" for x
   539       proof -
   540         have g: "g differentiable at x"
   541           by (metis C1_differentiable_on_eq DiffD1 DiffI atLeastAtMost_diff_ends greaterThanLessThan_iff st(2) that)
   542         show ?thesis
   543           using that
   544           apply (rule_tac f=g and d="dist x c" in has_vector_derivative_transform_within)
   545              apply (auto simp: dist_norm vector_derivative_works [symmetric] g)
   546           done
   547       qed
   548       then show ?thesis
   549         by (metis (no_types, lifting) continuous_on_eq [OF gcon] DiffE greaterThanLessThan_iff vector_derivative_at)
   550     qed
   551     ultimately have "continuous_on ({a<..<b} - insert c (S \<union> T))
   552         (\<lambda>x. vector_derivative (\<lambda>x. if x \<le> c then f x else g x) (at x))"
   553       by (rule continuous_on_subset [OF continuous_on_open_Un], auto)
   554   } note * = this
   555   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))"
   556     using st
   557     by (auto simp: C1_differentiable_on_eq elim!: continuous_on_subset intro: *)
   558   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)"
   559     apply (rule_tac x="{a,b,c} \<union> S \<union> T" in exI)
   560     using st  by (auto simp: C1_differentiable_on_eq elim!: continuous_on_subset)
   561   with cab show ?thesis
   562     by (simp add: piecewise_C1_differentiable_on_def)
   563 qed
   564 
   565 lemma piecewise_C1_differentiable_neg:
   566     "f piecewise_C1_differentiable_on S \<Longrightarrow> (\<lambda>x. -(f x)) piecewise_C1_differentiable_on S"
   567   unfolding piecewise_C1_differentiable_on_def
   568   by (auto intro!: continuous_on_minus C1_differentiable_on_minus)
   569 
   570 lemma piecewise_C1_differentiable_add:
   571   assumes "f piecewise_C1_differentiable_on i"
   572           "g piecewise_C1_differentiable_on i"
   573     shows "(\<lambda>x. f x + g x) piecewise_C1_differentiable_on i"
   574 proof -
   575   obtain S t where st: "finite S" "finite t"
   576                        "f C1_differentiable_on (i-S)"
   577                        "g C1_differentiable_on (i-t)"
   578     using assms by (auto simp: piecewise_C1_differentiable_on_def)
   579   then have "finite (S \<union> t) \<and> (\<lambda>x. f x + g x) C1_differentiable_on i - (S \<union> t)"
   580     by (auto intro: C1_differentiable_on_add elim!: C1_differentiable_on_subset)
   581   moreover have "continuous_on i f" "continuous_on i g"
   582     using assms piecewise_C1_differentiable_on_def by auto
   583   ultimately show ?thesis
   584     by (auto simp: piecewise_C1_differentiable_on_def continuous_on_add)
   585 qed
   586 
   587 lemma piecewise_C1_differentiable_diff:
   588     "\<lbrakk>f piecewise_C1_differentiable_on S;  g piecewise_C1_differentiable_on S\<rbrakk>
   589      \<Longrightarrow> (\<lambda>x. f x - g x) piecewise_C1_differentiable_on S"
   590   unfolding diff_conv_add_uminus
   591   by (metis piecewise_C1_differentiable_add piecewise_C1_differentiable_neg)
   592 
   593 lemma piecewise_C1_differentiable_D1:
   594   fixes g1 :: "real \<Rightarrow> 'a::real_normed_field"
   595   assumes "(g1 +++ g2) piecewise_C1_differentiable_on {0..1}"
   596     shows "g1 piecewise_C1_differentiable_on {0..1}"
   597 proof -
   598   obtain S where "finite S"
   599              and co12: "continuous_on ({0..1} - S) (\<lambda>x. vector_derivative (g1 +++ g2) (at x))"
   600              and g12D: "\<forall>x\<in>{0..1} - S. g1 +++ g2 differentiable at x"
   601     using assms  by (auto simp: piecewise_C1_differentiable_on_def C1_differentiable_on_eq)
   602   have g1D: "g1 differentiable at x" if "x \<in> {0..1} - insert 1 ((*) 2 ` S)" for x
   603   proof (rule differentiable_transform_within)
   604     show "g1 +++ g2 \<circ> (*) (inverse 2) differentiable at x"
   605       using that g12D
   606       apply (simp only: joinpaths_def)
   607       by (rule differentiable_chain_at derivative_intros | force)+
   608     show "\<And>x'. \<lbrakk>dist x' x < dist (x/2) (1/2)\<rbrakk>
   609           \<Longrightarrow> (g1 +++ g2 \<circ> (*) (inverse 2)) x' = g1 x'"
   610       using that by (auto simp: dist_real_def joinpaths_def)
   611   qed (use that in \<open>auto simp: dist_real_def\<close>)
   612   have [simp]: "vector_derivative (g1 \<circ> (*) 2) (at (x/2)) = 2 *\<^sub>R vector_derivative g1 (at x)"
   613                if "x \<in> {0..1} - insert 1 ((*) 2 ` S)" for x
   614     apply (subst vector_derivative_chain_at)
   615     using that
   616     apply (rule derivative_eq_intros g1D | simp)+
   617     done
   618   have "continuous_on ({0..1/2} - insert (1/2) S) (\<lambda>x. vector_derivative (g1 +++ g2) (at x))"
   619     using co12 by (rule continuous_on_subset) force
   620   then have coDhalf: "continuous_on ({0..1/2} - insert (1/2) S) (\<lambda>x. vector_derivative (g1 \<circ> (*)2) (at x))"
   621   proof (rule continuous_on_eq [OF _ vector_derivative_at])
   622     show "(g1 +++ g2 has_vector_derivative vector_derivative (g1 \<circ> (*) 2) (at x)) (at x)"
   623       if "x \<in> {0..1/2} - insert (1/2) S" for x
   624     proof (rule has_vector_derivative_transform_within)
   625       show "(g1 \<circ> (*) 2 has_vector_derivative vector_derivative (g1 \<circ> (*) 2) (at x)) (at x)"
   626         using that
   627         by (force intro: g1D differentiable_chain_at simp: vector_derivative_works [symmetric])
   628       show "\<And>x'. \<lbrakk>dist x' x < dist x (1/2)\<rbrakk> \<Longrightarrow> (g1 \<circ> (*) 2) x' = (g1 +++ g2) x'"
   629         using that by (auto simp: dist_norm joinpaths_def)
   630     qed (use that in \<open>auto simp: dist_norm\<close>)
   631   qed
   632   have "continuous_on ({0..1} - insert 1 ((*) 2 ` S))
   633                       ((\<lambda>x. 1/2 * vector_derivative (g1 \<circ> (*)2) (at x)) \<circ> (*)(1/2))"
   634     apply (rule continuous_intros)+
   635     using coDhalf
   636     apply (simp add: scaleR_conv_of_real image_set_diff image_image)
   637     done
   638   then have con_g1: "continuous_on ({0..1} - insert 1 ((*) 2 ` S)) (\<lambda>x. vector_derivative g1 (at x))"
   639     by (rule continuous_on_eq) (simp add: scaleR_conv_of_real)
   640   have "continuous_on {0..1} g1"
   641     using continuous_on_joinpaths_D1 assms piecewise_C1_differentiable_on_def by blast
   642   with \<open>finite S\<close> show ?thesis
   643     apply (clarsimp simp add: piecewise_C1_differentiable_on_def C1_differentiable_on_eq)
   644     apply (rule_tac x="insert 1 (((*)2)`S)" in exI)
   645     apply (simp add: g1D con_g1)
   646   done
   647 qed
   648 
   649 lemma piecewise_C1_differentiable_D2:
   650   fixes g2 :: "real \<Rightarrow> 'a::real_normed_field"
   651   assumes "(g1 +++ g2) piecewise_C1_differentiable_on {0..1}" "pathfinish g1 = pathstart g2"
   652     shows "g2 piecewise_C1_differentiable_on {0..1}"
   653 proof -
   654   obtain S where "finite S"
   655              and co12: "continuous_on ({0..1} - S) (\<lambda>x. vector_derivative (g1 +++ g2) (at x))"
   656              and g12D: "\<forall>x\<in>{0..1} - S. g1 +++ g2 differentiable at x"
   657     using assms  by (auto simp: piecewise_C1_differentiable_on_def C1_differentiable_on_eq)
   658   have g2D: "g2 differentiable at x" if "x \<in> {0..1} - insert 0 ((\<lambda>x. 2*x-1) ` S)" for x
   659   proof (rule differentiable_transform_within)
   660     show "g1 +++ g2 \<circ> (\<lambda>x. (x + 1) / 2) differentiable at x"
   661       using g12D that
   662       apply (simp only: joinpaths_def)
   663       apply (drule_tac x= "(x+1) / 2" in bspec, force simp: divide_simps)
   664       apply (rule differentiable_chain_at derivative_intros | force)+
   665       done
   666     show "\<And>x'. dist x' x < dist ((x + 1) / 2) (1/2) \<Longrightarrow> (g1 +++ g2 \<circ> (\<lambda>x. (x + 1) / 2)) x' = g2 x'"
   667       using that by (auto simp: dist_real_def joinpaths_def field_simps)
   668     qed (use that in \<open>auto simp: dist_norm\<close>)
   669   have [simp]: "vector_derivative (g2 \<circ> (\<lambda>x. 2*x-1)) (at ((x+1)/2)) = 2 *\<^sub>R vector_derivative g2 (at x)"
   670                if "x \<in> {0..1} - insert 0 ((\<lambda>x. 2*x-1) ` S)" for x
   671     using that  by (auto simp: vector_derivative_chain_at divide_simps g2D)
   672   have "continuous_on ({1/2..1} - insert (1/2) S) (\<lambda>x. vector_derivative (g1 +++ g2) (at x))"
   673     using co12 by (rule continuous_on_subset) force
   674   then have coDhalf: "continuous_on ({1/2..1} - insert (1/2) S) (\<lambda>x. vector_derivative (g2 \<circ> (\<lambda>x. 2*x-1)) (at x))"
   675   proof (rule continuous_on_eq [OF _ vector_derivative_at])
   676     show "(g1 +++ g2 has_vector_derivative vector_derivative (g2 \<circ> (\<lambda>x. 2 * x - 1)) (at x))
   677           (at x)"
   678       if "x \<in> {1 / 2..1} - insert (1 / 2) S" for x
   679     proof (rule_tac f="g2 \<circ> (\<lambda>x. 2*x-1)" and d="dist (3/4) ((x+1)/2)" in has_vector_derivative_transform_within)
   680       show "(g2 \<circ> (\<lambda>x. 2 * x - 1) has_vector_derivative vector_derivative (g2 \<circ> (\<lambda>x. 2 * x - 1)) (at x))
   681             (at x)"
   682         using that by (force intro: g2D differentiable_chain_at simp: vector_derivative_works [symmetric])
   683       show "\<And>x'. \<lbrakk>dist x' x < dist (3 / 4) ((x + 1) / 2)\<rbrakk> \<Longrightarrow> (g2 \<circ> (\<lambda>x. 2 * x - 1)) x' = (g1 +++ g2) x'"
   684         using that by (auto simp: dist_norm joinpaths_def add_divide_distrib)
   685     qed (use that in \<open>auto simp: dist_norm\<close>)
   686   qed
   687   have [simp]: "((\<lambda>x. (x+1) / 2) ` ({0..1} - insert 0 ((\<lambda>x. 2 * x - 1) ` S))) = ({1/2..1} - insert (1/2) S)"
   688     apply (simp add: image_set_diff inj_on_def image_image)
   689     apply (auto simp: image_affinity_atLeastAtMost_div add_divide_distrib)
   690     done
   691   have "continuous_on ({0..1} - insert 0 ((\<lambda>x. 2*x-1) ` S))
   692                       ((\<lambda>x. 1/2 * vector_derivative (g2 \<circ> (\<lambda>x. 2*x-1)) (at x)) \<circ> (\<lambda>x. (x+1)/2))"
   693     by (rule continuous_intros | simp add:  coDhalf)+
   694   then have con_g2: "continuous_on ({0..1} - insert 0 ((\<lambda>x. 2*x-1) ` S)) (\<lambda>x. vector_derivative g2 (at x))"
   695     by (rule continuous_on_eq) (simp add: scaleR_conv_of_real)
   696   have "continuous_on {0..1} g2"
   697     using continuous_on_joinpaths_D2 assms piecewise_C1_differentiable_on_def by blast
   698   with \<open>finite S\<close> show ?thesis
   699     apply (clarsimp simp add: piecewise_C1_differentiable_on_def C1_differentiable_on_eq)
   700     apply (rule_tac x="insert 0 ((\<lambda>x. 2 * x - 1) ` S)" in exI)
   701     apply (simp add: g2D con_g2)
   702   done
   703 qed
   704 
   705 subsection \<open>Valid paths, and their start and finish\<close>
   706 
   707 definition%important valid_path :: "(real \<Rightarrow> 'a :: real_normed_vector) \<Rightarrow> bool"
   708   where "valid_path f \<equiv> f piecewise_C1_differentiable_on {0..1::real}"
   709 
   710 definition closed_path :: "(real \<Rightarrow> 'a :: real_normed_vector) \<Rightarrow> bool"
   711   where "closed_path g \<equiv> g 0 = g 1"
   712 
   713 text\<open>In particular, all results for paths apply\<close>
   714 
   715 lemma valid_path_imp_path: "valid_path g \<Longrightarrow> path g"
   716   by (simp add: path_def piecewise_C1_differentiable_on_def valid_path_def)
   717 
   718 lemma connected_valid_path_image: "valid_path g \<Longrightarrow> connected(path_image g)"
   719   by (metis connected_path_image valid_path_imp_path)
   720 
   721 lemma compact_valid_path_image: "valid_path g \<Longrightarrow> compact(path_image g)"
   722   by (metis compact_path_image valid_path_imp_path)
   723 
   724 lemma bounded_valid_path_image: "valid_path g \<Longrightarrow> bounded(path_image g)"
   725   by (metis bounded_path_image valid_path_imp_path)
   726 
   727 lemma closed_valid_path_image: "valid_path g \<Longrightarrow> closed(path_image g)"
   728   by (metis closed_path_image valid_path_imp_path)
   729 
   730 lemma valid_path_compose:
   731   assumes "valid_path g"
   732       and der: "\<And>x. x \<in> path_image g \<Longrightarrow> f field_differentiable (at x)"
   733       and con: "continuous_on (path_image g) (deriv f)"
   734     shows "valid_path (f \<circ> g)"
   735 proof -
   736   obtain S where "finite S" and g_diff: "g C1_differentiable_on {0..1} - S"
   737     using \<open>valid_path g\<close> unfolding valid_path_def piecewise_C1_differentiable_on_def by auto
   738   have "f \<circ> g differentiable at t" when "t\<in>{0..1} - S" for t
   739     proof (rule differentiable_chain_at)
   740       show "g differentiable at t" using \<open>valid_path g\<close>
   741         by (meson C1_differentiable_on_eq \<open>g C1_differentiable_on {0..1} - S\<close> that)
   742     next
   743       have "g t\<in>path_image g" using that DiffD1 image_eqI path_image_def by metis
   744       then show "f differentiable at (g t)"
   745         using der[THEN field_differentiable_imp_differentiable] by auto
   746     qed
   747   moreover have "continuous_on ({0..1} - S) (\<lambda>x. vector_derivative (f \<circ> g) (at x))"
   748     proof (rule continuous_on_eq [where f = "\<lambda>x. vector_derivative g (at x) * deriv f (g x)"],
   749         rule continuous_intros)
   750       show "continuous_on ({0..1} - S) (\<lambda>x. vector_derivative g (at x))"
   751         using g_diff C1_differentiable_on_eq by auto
   752     next
   753       have "continuous_on {0..1} (\<lambda>x. deriv f (g x))"
   754         using continuous_on_compose[OF _ con[unfolded path_image_def],unfolded comp_def]
   755           \<open>valid_path g\<close> piecewise_C1_differentiable_on_def valid_path_def
   756         by blast
   757       then show "continuous_on ({0..1} - S) (\<lambda>x. deriv f (g x))"
   758         using continuous_on_subset by blast
   759     next
   760       show "vector_derivative g (at t) * deriv f (g t) = vector_derivative (f \<circ> g) (at t)"
   761           when "t \<in> {0..1} - S" for t
   762         proof (rule vector_derivative_chain_at_general[symmetric])
   763           show "g differentiable at t" by (meson C1_differentiable_on_eq g_diff that)
   764         next
   765           have "g t\<in>path_image g" using that DiffD1 image_eqI path_image_def by metis
   766           then show "f field_differentiable at (g t)" using der by auto
   767         qed
   768     qed
   769   ultimately have "f \<circ> g C1_differentiable_on {0..1} - S"
   770     using C1_differentiable_on_eq by blast
   771   moreover have "path (f \<circ> g)"
   772     apply (rule path_continuous_image[OF valid_path_imp_path[OF \<open>valid_path g\<close>]])
   773     using der
   774     by (simp add: continuous_at_imp_continuous_on field_differentiable_imp_continuous_at)
   775   ultimately show ?thesis unfolding valid_path_def piecewise_C1_differentiable_on_def path_def
   776     using \<open>finite S\<close> by auto
   777 qed
   778   
   779 lemma valid_path_uminus_comp[simp]:
   780   fixes g::"real \<Rightarrow> 'a ::real_normed_field"
   781   shows "valid_path (uminus \<circ> g) \<longleftrightarrow> valid_path g"
   782 proof 
   783   show "valid_path g \<Longrightarrow> valid_path (uminus \<circ> g)" for g::"real \<Rightarrow> 'a"
   784     by (auto intro!: valid_path_compose derivative_intros simp add: deriv_linear[of "-1",simplified])  
   785   then show "valid_path g" when "valid_path (uminus \<circ> g)"
   786     by (metis fun.map_comp group_add_class.minus_comp_minus id_comp that)
   787 qed
   788 
   789 lemma valid_path_offset[simp]:
   790   shows "valid_path (\<lambda>t. g t - z) \<longleftrightarrow> valid_path g"  
   791 proof 
   792   show *: "valid_path (g::real\<Rightarrow>'a) \<Longrightarrow> valid_path (\<lambda>t. g t - z)" for g z
   793     unfolding valid_path_def
   794     by (fastforce intro:derivative_intros C1_differentiable_imp_piecewise piecewise_C1_differentiable_diff)
   795   show "valid_path (\<lambda>t. g t - z) \<Longrightarrow> valid_path g"
   796     using *[of "\<lambda>t. g t - z" "-z",simplified] .
   797 qed
   798   
   799 
   800 subsection\<open>Contour Integrals along a path\<close>
   801 
   802 text\<open>This definition is for complex numbers only, and does not generalise to line integrals in a vector field\<close>
   803 
   804 text\<open>piecewise differentiable function on [0,1]\<close>
   805 
   806 definition%important has_contour_integral :: "(complex \<Rightarrow> complex) \<Rightarrow> complex \<Rightarrow> (real \<Rightarrow> complex) \<Rightarrow> bool"
   807            (infixr "has'_contour'_integral" 50)
   808   where "(f has_contour_integral i) g \<equiv>
   809            ((\<lambda>x. f(g x) * vector_derivative g (at x within {0..1}))
   810             has_integral i) {0..1}"
   811 
   812 definition%important contour_integrable_on
   813            (infixr "contour'_integrable'_on" 50)
   814   where "f contour_integrable_on g \<equiv> \<exists>i. (f has_contour_integral i) g"
   815 
   816 definition%important contour_integral
   817   where "contour_integral g f \<equiv> SOME i. (f has_contour_integral i) g \<or> \<not> f contour_integrable_on g \<and> i=0"
   818 
   819 lemma not_integrable_contour_integral: "\<not> f contour_integrable_on g \<Longrightarrow> contour_integral g f = 0"
   820   unfolding contour_integrable_on_def contour_integral_def by blast
   821 
   822 lemma contour_integral_unique: "(f has_contour_integral i) g \<Longrightarrow> contour_integral g f = i"
   823   apply (simp add: contour_integral_def has_contour_integral_def contour_integrable_on_def)
   824   using has_integral_unique by blast
   825 
   826 lemma has_contour_integral_eqpath:
   827      "\<lbrakk>(f has_contour_integral y) p; f contour_integrable_on \<gamma>;
   828        contour_integral p f = contour_integral \<gamma> f\<rbrakk>
   829       \<Longrightarrow> (f has_contour_integral y) \<gamma>"
   830 using contour_integrable_on_def contour_integral_unique by auto
   831 
   832 lemma has_contour_integral_integral:
   833     "f contour_integrable_on i \<Longrightarrow> (f has_contour_integral (contour_integral i f)) i"
   834   by (metis contour_integral_unique contour_integrable_on_def)
   835 
   836 lemma has_contour_integral_unique:
   837     "(f has_contour_integral i) g \<Longrightarrow> (f has_contour_integral j) g \<Longrightarrow> i = j"
   838   using has_integral_unique
   839   by (auto simp: has_contour_integral_def)
   840 
   841 lemma has_contour_integral_integrable: "(f has_contour_integral i) g \<Longrightarrow> f contour_integrable_on g"
   842   using contour_integrable_on_def by blast
   843 
   844 text\<open>Show that we can forget about the localized derivative.\<close>
   845 
   846 lemma has_integral_localized_vector_derivative:
   847     "((\<lambda>x. f (g x) * vector_derivative g (at x within {a..b})) has_integral i) {a..b} \<longleftrightarrow>
   848      ((\<lambda>x. f (g x) * vector_derivative g (at x)) has_integral i) {a..b}"
   849 proof -
   850   have *: "{a..b} - {a,b} = interior {a..b}"
   851     by (simp add: atLeastAtMost_diff_ends)
   852   show ?thesis
   853     apply (rule has_integral_spike_eq [of "{a,b}"])
   854     apply (auto simp: at_within_interior [of _ "{a..b}"])
   855     done
   856 qed
   857 
   858 lemma integrable_on_localized_vector_derivative:
   859     "(\<lambda>x. f (g x) * vector_derivative g (at x within {a..b})) integrable_on {a..b} \<longleftrightarrow>
   860      (\<lambda>x. f (g x) * vector_derivative g (at x)) integrable_on {a..b}"
   861   by (simp add: integrable_on_def has_integral_localized_vector_derivative)
   862 
   863 lemma has_contour_integral:
   864      "(f has_contour_integral i) g \<longleftrightarrow>
   865       ((\<lambda>x. f (g x) * vector_derivative g (at x)) has_integral i) {0..1}"
   866   by (simp add: has_integral_localized_vector_derivative has_contour_integral_def)
   867 
   868 lemma contour_integrable_on:
   869      "f contour_integrable_on g \<longleftrightarrow>
   870       (\<lambda>t. f(g t) * vector_derivative g (at t)) integrable_on {0..1}"
   871   by (simp add: has_contour_integral integrable_on_def contour_integrable_on_def)
   872 
   873 subsection%unimportant \<open>Reversing a path\<close>
   874 
   875 lemma valid_path_imp_reverse:
   876   assumes "valid_path g"
   877     shows "valid_path(reversepath g)"
   878 proof -
   879   obtain S where "finite S" and S: "g C1_differentiable_on ({0..1} - S)"
   880     using assms by (auto simp: valid_path_def piecewise_C1_differentiable_on_def)
   881   then have "finite ((-) 1 ` S)"
   882     by auto
   883   moreover have "(reversepath g C1_differentiable_on ({0..1} - (-) 1 ` S))"
   884     unfolding reversepath_def
   885     apply (rule C1_differentiable_compose [of "\<lambda>x::real. 1-x" _ g, unfolded o_def])
   886     using S
   887     by (force simp: finite_vimageI inj_on_def C1_differentiable_on_eq continuous_on_const elim!: continuous_on_subset)+
   888   ultimately show ?thesis using assms
   889     by (auto simp: valid_path_def piecewise_C1_differentiable_on_def path_def [symmetric])
   890 qed
   891 
   892 lemma valid_path_reversepath [simp]: "valid_path(reversepath g) \<longleftrightarrow> valid_path g"
   893   using valid_path_imp_reverse by force
   894 
   895 lemma has_contour_integral_reversepath:
   896   assumes "valid_path g" and f: "(f has_contour_integral i) g"
   897     shows "(f has_contour_integral (-i)) (reversepath g)"
   898 proof -
   899   { fix S x
   900     assume xs: "g C1_differentiable_on ({0..1} - S)" "x \<notin> (-) 1 ` S" "0 \<le> x" "x \<le> 1"
   901     have "vector_derivative (\<lambda>x. g (1 - x)) (at x within {0..1}) =
   902             - vector_derivative g (at (1 - x) within {0..1})"
   903     proof -
   904       obtain f' where f': "(g has_vector_derivative f') (at (1 - x))"
   905         using xs
   906         by (force simp: has_vector_derivative_def C1_differentiable_on_def)
   907       have "(g \<circ> (\<lambda>x. 1 - x) has_vector_derivative -1 *\<^sub>R f') (at x)"
   908         by (intro vector_diff_chain_within has_vector_derivative_at_within [OF f'] derivative_eq_intros | simp)+
   909       then have mf': "((\<lambda>x. g (1 - x)) has_vector_derivative -f') (at x)"
   910         by (simp add: o_def)
   911       show ?thesis
   912         using xs
   913         by (auto simp: vector_derivative_at_within_ivl [OF mf'] vector_derivative_at_within_ivl [OF f'])
   914     qed
   915   } note * = this
   916   obtain S where S: "continuous_on {0..1} g" "finite S" "g C1_differentiable_on {0..1} - S"
   917     using assms by (auto simp: valid_path_def piecewise_C1_differentiable_on_def)
   918   have "((\<lambda>x. - (f (g (1 - x)) * vector_derivative g (at (1 - x) within {0..1}))) has_integral -i)
   919        {0..1}"
   920     using has_integral_affinity01 [where m= "-1" and c=1, OF f [unfolded has_contour_integral_def]]
   921     by (simp add: has_integral_neg)
   922   then show ?thesis
   923     using S
   924     apply (clarsimp simp: reversepath_def has_contour_integral_def)
   925     apply (rule_tac S = "(\<lambda>x. 1 - x) ` S" in has_integral_spike_finite)
   926       apply (auto simp: *)
   927     done
   928 qed
   929 
   930 lemma contour_integrable_reversepath:
   931     "valid_path g \<Longrightarrow> f contour_integrable_on g \<Longrightarrow> f contour_integrable_on (reversepath g)"
   932   using has_contour_integral_reversepath contour_integrable_on_def by blast
   933 
   934 lemma contour_integrable_reversepath_eq:
   935     "valid_path g \<Longrightarrow> (f contour_integrable_on (reversepath g) \<longleftrightarrow> f contour_integrable_on g)"
   936   using contour_integrable_reversepath valid_path_reversepath by fastforce
   937 
   938 lemma contour_integral_reversepath:
   939   assumes "valid_path g"
   940     shows "contour_integral (reversepath g) f = - (contour_integral g f)"
   941 proof (cases "f contour_integrable_on g")
   942   case True then show ?thesis
   943     by (simp add: assms contour_integral_unique has_contour_integral_integral has_contour_integral_reversepath)
   944 next
   945   case False then have "\<not> f contour_integrable_on (reversepath g)"
   946     by (simp add: assms contour_integrable_reversepath_eq)
   947   with False show ?thesis by (simp add: not_integrable_contour_integral)
   948 qed
   949 
   950 
   951 subsection%unimportant \<open>Joining two paths together\<close>
   952 
   953 lemma valid_path_join:
   954   assumes "valid_path g1" "valid_path g2" "pathfinish g1 = pathstart g2"
   955     shows "valid_path(g1 +++ g2)"
   956 proof -
   957   have "g1 1 = g2 0"
   958     using assms by (auto simp: pathfinish_def pathstart_def)
   959   moreover have "(g1 \<circ> (\<lambda>x. 2*x)) piecewise_C1_differentiable_on {0..1/2}"
   960     apply (rule piecewise_C1_differentiable_compose)
   961     using assms
   962     apply (auto simp: valid_path_def piecewise_C1_differentiable_on_def continuous_on_joinpaths)
   963     apply (force intro: finite_vimageI [where h = "(*)2"] inj_onI)
   964     done
   965   moreover have "(g2 \<circ> (\<lambda>x. 2*x-1)) piecewise_C1_differentiable_on {1/2..1}"
   966     apply (rule piecewise_C1_differentiable_compose)
   967     using assms unfolding valid_path_def piecewise_C1_differentiable_on_def
   968     by (auto intro!: continuous_intros finite_vimageI [where h = "(\<lambda>x. 2*x - 1)"] inj_onI
   969              simp: image_affinity_atLeastAtMost_diff continuous_on_joinpaths)
   970   ultimately show ?thesis
   971     apply (simp only: valid_path_def continuous_on_joinpaths joinpaths_def)
   972     apply (rule piecewise_C1_differentiable_cases)
   973     apply (auto simp: o_def)
   974     done
   975 qed
   976 
   977 lemma valid_path_join_D1:
   978   fixes g1 :: "real \<Rightarrow> 'a::real_normed_field"
   979   shows "valid_path (g1 +++ g2) \<Longrightarrow> valid_path g1"
   980   unfolding valid_path_def
   981   by (rule piecewise_C1_differentiable_D1)
   982 
   983 lemma valid_path_join_D2:
   984   fixes g2 :: "real \<Rightarrow> 'a::real_normed_field"
   985   shows "\<lbrakk>valid_path (g1 +++ g2); pathfinish g1 = pathstart g2\<rbrakk> \<Longrightarrow> valid_path g2"
   986   unfolding valid_path_def
   987   by (rule piecewise_C1_differentiable_D2)
   988 
   989 lemma valid_path_join_eq [simp]:
   990   fixes g2 :: "real \<Rightarrow> 'a::real_normed_field"
   991   shows "pathfinish g1 = pathstart g2 \<Longrightarrow> (valid_path(g1 +++ g2) \<longleftrightarrow> valid_path g1 \<and> valid_path g2)"
   992   using valid_path_join_D1 valid_path_join_D2 valid_path_join by blast
   993 
   994 lemma has_contour_integral_join:
   995   assumes "(f has_contour_integral i1) g1" "(f has_contour_integral i2) g2"
   996           "valid_path g1" "valid_path g2"
   997     shows "(f has_contour_integral (i1 + i2)) (g1 +++ g2)"
   998 proof -
   999   obtain s1 s2
  1000     where s1: "finite s1" "\<forall>x\<in>{0..1} - s1. g1 differentiable at x"
  1001       and s2: "finite s2" "\<forall>x\<in>{0..1} - s2. g2 differentiable at x"
  1002     using assms
  1003     by (auto simp: valid_path_def piecewise_C1_differentiable_on_def C1_differentiable_on_eq)
  1004   have 1: "((\<lambda>x. f (g1 x) * vector_derivative g1 (at x)) has_integral i1) {0..1}"
  1005    and 2: "((\<lambda>x. f (g2 x) * vector_derivative g2 (at x)) has_integral i2) {0..1}"
  1006     using assms
  1007     by (auto simp: has_contour_integral)
  1008   have i1: "((\<lambda>x. (2*f (g1 (2*x))) * vector_derivative g1 (at (2*x))) has_integral i1) {0..1/2}"
  1009    and i2: "((\<lambda>x. (2*f (g2 (2*x - 1))) * vector_derivative g2 (at (2*x - 1))) has_integral i2) {1/2..1}"
  1010     using has_integral_affinity01 [OF 1, where m= 2 and c=0, THEN has_integral_cmul [where c=2]]
  1011           has_integral_affinity01 [OF 2, where m= 2 and c="-1", THEN has_integral_cmul [where c=2]]
  1012     by (simp_all only: image_affinity_atLeastAtMost_div_diff, simp_all add: scaleR_conv_of_real mult_ac)
  1013   have g1: "\<lbrakk>0 \<le> z; z*2 < 1; z*2 \<notin> s1\<rbrakk> \<Longrightarrow>
  1014             vector_derivative (\<lambda>x. if x*2 \<le> 1 then g1 (2*x) else g2 (2*x - 1)) (at z) =
  1015             2 *\<^sub>R vector_derivative g1 (at (z*2))" for z
  1016     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>"]])
  1017     apply (simp_all add: dist_real_def abs_if split: if_split_asm)
  1018     apply (rule vector_diff_chain_at [of "\<lambda>x. 2*x" 2 _ g1, simplified o_def])
  1019     apply (simp add: has_vector_derivative_def has_derivative_def bounded_linear_mult_left)
  1020     using s1
  1021     apply (auto simp: algebra_simps vector_derivative_works)
  1022     done
  1023   have g2: "\<lbrakk>1 < z*2; z \<le> 1; z*2 - 1 \<notin> s2\<rbrakk> \<Longrightarrow>
  1024             vector_derivative (\<lambda>x. if x*2 \<le> 1 then g1 (2*x) else g2 (2*x - 1)) (at z) =
  1025             2 *\<^sub>R vector_derivative g2 (at (z*2 - 1))" for z
  1026     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>"]])
  1027     apply (simp_all add: dist_real_def abs_if split: if_split_asm)
  1028     apply (rule vector_diff_chain_at [of "\<lambda>x. 2*x - 1" 2 _ g2, simplified o_def])
  1029     apply (simp add: has_vector_derivative_def has_derivative_def bounded_linear_mult_left)
  1030     using s2
  1031     apply (auto simp: algebra_simps vector_derivative_works)
  1032     done
  1033   have "((\<lambda>x. f ((g1 +++ g2) x) * vector_derivative (g1 +++ g2) (at x)) has_integral i1) {0..1/2}"
  1034     apply (rule has_integral_spike_finite [OF _ _ i1, of "insert (1/2) ((*)2 -` s1)"])
  1035     using s1
  1036     apply (force intro: finite_vimageI [where h = "(*)2"] inj_onI)
  1037     apply (clarsimp simp add: joinpaths_def scaleR_conv_of_real mult_ac g1)
  1038     done
  1039   moreover have "((\<lambda>x. f ((g1 +++ g2) x) * vector_derivative (g1 +++ g2) (at x)) has_integral i2) {1/2..1}"
  1040     apply (rule has_integral_spike_finite [OF _ _ i2, of "insert (1/2) ((\<lambda>x. 2*x-1) -` s2)"])
  1041     using s2
  1042     apply (force intro: finite_vimageI [where h = "\<lambda>x. 2*x-1"] inj_onI)
  1043     apply (clarsimp simp add: joinpaths_def scaleR_conv_of_real mult_ac g2)
  1044     done
  1045   ultimately
  1046   show ?thesis
  1047     apply (simp add: has_contour_integral)
  1048     apply (rule has_integral_combine [where c = "1/2"], auto)
  1049     done
  1050 qed
  1051 
  1052 lemma contour_integrable_joinI:
  1053   assumes "f contour_integrable_on g1" "f contour_integrable_on g2"
  1054           "valid_path g1" "valid_path g2"
  1055     shows "f contour_integrable_on (g1 +++ g2)"
  1056   using assms
  1057   by (meson has_contour_integral_join contour_integrable_on_def)
  1058 
  1059 lemma contour_integrable_joinD1:
  1060   assumes "f contour_integrable_on (g1 +++ g2)" "valid_path g1"
  1061     shows "f contour_integrable_on g1"
  1062 proof -
  1063   obtain s1
  1064     where s1: "finite s1" "\<forall>x\<in>{0..1} - s1. g1 differentiable at x"
  1065     using assms by (auto simp: valid_path_def piecewise_C1_differentiable_on_def C1_differentiable_on_eq)
  1066   have "(\<lambda>x. f ((g1 +++ g2) (x/2)) * vector_derivative (g1 +++ g2) (at (x/2))) integrable_on {0..1}"
  1067     using assms
  1068     apply (auto simp: contour_integrable_on)
  1069     apply (drule integrable_on_subcbox [where a=0 and b="1/2"])
  1070     apply (auto intro: integrable_affinity [of _ 0 "1/2::real" "1/2" 0, simplified])
  1071     done
  1072   then have *: "(\<lambda>x. (f ((g1 +++ g2) (x/2))/2) * vector_derivative (g1 +++ g2) (at (x/2))) integrable_on {0..1}"
  1073     by (auto dest: integrable_cmul [where c="1/2"] simp: scaleR_conv_of_real)
  1074   have g1: "\<lbrakk>0 < z; z < 1; z \<notin> s1\<rbrakk> \<Longrightarrow>
  1075             vector_derivative (\<lambda>x. if x*2 \<le> 1 then g1 (2*x) else g2 (2*x - 1)) (at (z/2)) =
  1076             2 *\<^sub>R vector_derivative g1 (at z)"  for z
  1077     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>"]])
  1078     apply (simp_all add: field_simps dist_real_def abs_if split: if_split_asm)
  1079     apply (rule vector_diff_chain_at [of "\<lambda>x. x*2" 2 _ g1, simplified o_def])
  1080     using s1
  1081     apply (auto simp: vector_derivative_works has_vector_derivative_def has_derivative_def bounded_linear_mult_left)
  1082     done
  1083   show ?thesis
  1084     using s1
  1085     apply (auto simp: contour_integrable_on)
  1086     apply (rule integrable_spike_finite [of "{0,1} \<union> s1", OF _ _ *])
  1087     apply (auto simp: joinpaths_def scaleR_conv_of_real g1)
  1088     done
  1089 qed
  1090 
  1091 lemma contour_integrable_joinD2:
  1092   assumes "f contour_integrable_on (g1 +++ g2)" "valid_path g2"
  1093     shows "f contour_integrable_on g2"
  1094 proof -
  1095   obtain s2
  1096     where s2: "finite s2" "\<forall>x\<in>{0..1} - s2. g2 differentiable at x"
  1097     using assms by (auto simp: valid_path_def piecewise_C1_differentiable_on_def C1_differentiable_on_eq)
  1098   have "(\<lambda>x. f ((g1 +++ g2) (x/2 + 1/2)) * vector_derivative (g1 +++ g2) (at (x/2 + 1/2))) integrable_on {0..1}"
  1099     using assms
  1100     apply (auto simp: contour_integrable_on)
  1101     apply (drule integrable_on_subcbox [where a="1/2" and b=1], auto)
  1102     apply (drule integrable_affinity [of _ "1/2::real" 1 "1/2" "1/2", simplified])
  1103     apply (simp add: image_affinity_atLeastAtMost_diff)
  1104     done
  1105   then have *: "(\<lambda>x. (f ((g1 +++ g2) (x/2 + 1/2))/2) * vector_derivative (g1 +++ g2) (at (x/2 + 1/2)))
  1106                 integrable_on {0..1}"
  1107     by (auto dest: integrable_cmul [where c="1/2"] simp: scaleR_conv_of_real)
  1108   have g2: "\<lbrakk>0 < z; z < 1; z \<notin> s2\<rbrakk> \<Longrightarrow>
  1109             vector_derivative (\<lambda>x. if x*2 \<le> 1 then g1 (2*x) else g2 (2*x - 1)) (at (z/2+1/2)) =
  1110             2 *\<^sub>R vector_derivative g2 (at z)" for z
  1111     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>"]])
  1112     apply (simp_all add: field_simps dist_real_def abs_if split: if_split_asm)
  1113     apply (rule vector_diff_chain_at [of "\<lambda>x. x*2-1" 2 _ g2, simplified o_def])
  1114     using s2
  1115     apply (auto simp: has_vector_derivative_def has_derivative_def bounded_linear_mult_left
  1116                       vector_derivative_works add_divide_distrib)
  1117     done
  1118   show ?thesis
  1119     using s2
  1120     apply (auto simp: contour_integrable_on)
  1121     apply (rule integrable_spike_finite [of "{0,1} \<union> s2", OF _ _ *])
  1122     apply (auto simp: joinpaths_def scaleR_conv_of_real g2)
  1123     done
  1124 qed
  1125 
  1126 lemma contour_integrable_join [simp]:
  1127   shows
  1128     "\<lbrakk>valid_path g1; valid_path g2\<rbrakk>
  1129      \<Longrightarrow> f contour_integrable_on (g1 +++ g2) \<longleftrightarrow> f contour_integrable_on g1 \<and> f contour_integrable_on g2"
  1130 using contour_integrable_joinD1 contour_integrable_joinD2 contour_integrable_joinI by blast
  1131 
  1132 lemma contour_integral_join [simp]:
  1133   shows
  1134     "\<lbrakk>f contour_integrable_on g1; f contour_integrable_on g2; valid_path g1; valid_path g2\<rbrakk>
  1135         \<Longrightarrow> contour_integral (g1 +++ g2) f = contour_integral g1 f + contour_integral g2 f"
  1136   by (simp add: has_contour_integral_integral has_contour_integral_join contour_integral_unique)
  1137 
  1138 
  1139 subsection%unimportant \<open>Shifting the starting point of a (closed) path\<close>
  1140 
  1141 lemma shiftpath_alt_def: "shiftpath a f = (\<lambda>x. if x \<le> 1-a then f (a + x) else f (a + x - 1))"
  1142   by (auto simp: shiftpath_def)
  1143 
  1144 lemma valid_path_shiftpath [intro]:
  1145   assumes "valid_path g" "pathfinish g = pathstart g" "a \<in> {0..1}"
  1146     shows "valid_path(shiftpath a g)"
  1147   using assms
  1148   apply (auto simp: valid_path_def shiftpath_alt_def)
  1149   apply (rule piecewise_C1_differentiable_cases)
  1150   apply (auto simp: algebra_simps)
  1151   apply (rule piecewise_C1_differentiable_affine [of g 1 a, simplified o_def scaleR_one])
  1152   apply (auto simp: pathfinish_def pathstart_def elim: piecewise_C1_differentiable_on_subset)
  1153   apply (rule piecewise_C1_differentiable_affine [of g 1 "a-1", simplified o_def scaleR_one algebra_simps])
  1154   apply (auto simp: pathfinish_def pathstart_def elim: piecewise_C1_differentiable_on_subset)
  1155   done
  1156 
  1157 lemma has_contour_integral_shiftpath:
  1158   assumes f: "(f has_contour_integral i) g" "valid_path g"
  1159       and a: "a \<in> {0..1}"
  1160     shows "(f has_contour_integral i) (shiftpath a g)"
  1161 proof -
  1162   obtain s
  1163     where s: "finite s" and g: "\<forall>x\<in>{0..1} - s. g differentiable at x"
  1164     using assms by (auto simp: valid_path_def piecewise_C1_differentiable_on_def C1_differentiable_on_eq)
  1165   have *: "((\<lambda>x. f (g x) * vector_derivative g (at x)) has_integral i) {0..1}"
  1166     using assms by (auto simp: has_contour_integral)
  1167   then have i: "i = integral {a..1} (\<lambda>x. f (g x) * vector_derivative g (at x)) +
  1168                     integral {0..a} (\<lambda>x. f (g x) * vector_derivative g (at x))"
  1169     apply (rule has_integral_unique)
  1170     apply (subst add.commute)
  1171     apply (subst integral_combine)
  1172     using assms * integral_unique by auto
  1173   { fix x
  1174     have "0 \<le> x \<Longrightarrow> x + a < 1 \<Longrightarrow> x \<notin> (\<lambda>x. x - a) ` s \<Longrightarrow>
  1175          vector_derivative (shiftpath a g) (at x) = vector_derivative g (at (x + a))"
  1176       unfolding shiftpath_def
  1177       apply (rule vector_derivative_at [OF has_vector_derivative_transform_within [where f = "(\<lambda>x. g(a+x))" and d = "dist(1-a) x"]])
  1178         apply (auto simp: field_simps dist_real_def abs_if split: if_split_asm)
  1179       apply (rule vector_diff_chain_at [of "\<lambda>x. x+a" 1 _ g, simplified o_def scaleR_one])
  1180        apply (intro derivative_eq_intros | simp)+
  1181       using g
  1182        apply (drule_tac x="x+a" in bspec)
  1183       using a apply (auto simp: has_vector_derivative_def vector_derivative_works image_def add.commute)
  1184       done
  1185   } note vd1 = this
  1186   { fix x
  1187     have "1 < x + a \<Longrightarrow> x \<le> 1 \<Longrightarrow> x \<notin> (\<lambda>x. x - a + 1) ` s \<Longrightarrow>
  1188           vector_derivative (shiftpath a g) (at x) = vector_derivative g (at (x + a - 1))"
  1189       unfolding shiftpath_def
  1190       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"]])
  1191         apply (auto simp: field_simps dist_real_def abs_if split: if_split_asm)
  1192       apply (rule vector_diff_chain_at [of "\<lambda>x. x+a-1" 1 _ g, simplified o_def scaleR_one])
  1193        apply (intro derivative_eq_intros | simp)+
  1194       using g
  1195       apply (drule_tac x="x+a-1" in bspec)
  1196       using a apply (auto simp: has_vector_derivative_def vector_derivative_works image_def add.commute)
  1197       done
  1198   } note vd2 = this
  1199   have va1: "(\<lambda>x. f (g x) * vector_derivative g (at x)) integrable_on ({a..1})"
  1200     using * a   by (fastforce intro: integrable_subinterval_real)
  1201   have v0a: "(\<lambda>x. f (g x) * vector_derivative g (at x)) integrable_on ({0..a})"
  1202     apply (rule integrable_subinterval_real)
  1203     using * a by auto
  1204   have "((\<lambda>x. f (shiftpath a g x) * vector_derivative (shiftpath a g) (at x))
  1205         has_integral  integral {a..1} (\<lambda>x. f (g x) * vector_derivative g (at x)))  {0..1 - a}"
  1206     apply (rule has_integral_spike_finite
  1207              [where S = "{1-a} \<union> (\<lambda>x. x-a) ` s" and f = "\<lambda>x. f(g(a+x)) * vector_derivative g (at(a+x))"])
  1208       using s apply blast
  1209      using a apply (auto simp: algebra_simps vd1)
  1210      apply (force simp: shiftpath_def add.commute)
  1211     using has_integral_affinity [where m=1 and c=a, simplified, OF integrable_integral [OF va1]]
  1212     apply (simp add: image_affinity_atLeastAtMost_diff [where m=1 and c=a, simplified] add.commute)
  1213     done
  1214   moreover
  1215   have "((\<lambda>x. f (shiftpath a g x) * vector_derivative (shiftpath a g) (at x))
  1216         has_integral  integral {0..a} (\<lambda>x. f (g x) * vector_derivative g (at x)))  {1 - a..1}"
  1217     apply (rule has_integral_spike_finite
  1218              [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))"])
  1219       using s apply blast
  1220      using a apply (auto simp: algebra_simps vd2)
  1221      apply (force simp: shiftpath_def add.commute)
  1222     using has_integral_affinity [where m=1 and c="a-1", simplified, OF integrable_integral [OF v0a]]
  1223     apply (simp add: image_affinity_atLeastAtMost [where m=1 and c="1-a", simplified])
  1224     apply (simp add: algebra_simps)
  1225     done
  1226   ultimately show ?thesis
  1227     using a
  1228     by (auto simp: i has_contour_integral intro: has_integral_combine [where c = "1-a"])
  1229 qed
  1230 
  1231 lemma has_contour_integral_shiftpath_D:
  1232   assumes "(f has_contour_integral i) (shiftpath a g)"
  1233           "valid_path g" "pathfinish g = pathstart g" "a \<in> {0..1}"
  1234     shows "(f has_contour_integral i) g"
  1235 proof -
  1236   obtain s
  1237     where s: "finite s" and g: "\<forall>x\<in>{0..1} - s. g differentiable at x"
  1238     using assms by (auto simp: valid_path_def piecewise_C1_differentiable_on_def C1_differentiable_on_eq)
  1239   { fix x
  1240     assume x: "0 < x" "x < 1" "x \<notin> s"
  1241     then have gx: "g differentiable at x"
  1242       using g by auto
  1243     have "vector_derivative g (at x within {0..1}) =
  1244           vector_derivative (shiftpath (1 - a) (shiftpath a g)) (at x within {0..1})"
  1245       apply (rule vector_derivative_at_within_ivl
  1246                   [OF has_vector_derivative_transform_within_open
  1247                       [where f = "(shiftpath (1 - a) (shiftpath a g))" and S = "{0<..<1}-s"]])
  1248       using s g assms x
  1249       apply (auto simp: finite_imp_closed open_Diff shiftpath_shiftpath
  1250                         at_within_interior [of _ "{0..1}"] vector_derivative_works [symmetric])
  1251       apply (rule differentiable_transform_within [OF gx, of "min x (1-x)"])
  1252       apply (auto simp: dist_real_def shiftpath_shiftpath abs_if split: if_split_asm)
  1253       done
  1254   } note vd = this
  1255   have fi: "(f has_contour_integral i) (shiftpath (1 - a) (shiftpath a g))"
  1256     using assms  by (auto intro!: has_contour_integral_shiftpath)
  1257   show ?thesis
  1258     apply (simp add: has_contour_integral_def)
  1259     apply (rule has_integral_spike_finite [of "{0,1} \<union> s", OF _ _  fi [unfolded has_contour_integral_def]])
  1260     using s assms vd
  1261     apply (auto simp: Path_Connected.shiftpath_shiftpath)
  1262     done
  1263 qed
  1264 
  1265 lemma has_contour_integral_shiftpath_eq:
  1266   assumes "valid_path g" "pathfinish g = pathstart g" "a \<in> {0..1}"
  1267     shows "(f has_contour_integral i) (shiftpath a g) \<longleftrightarrow> (f has_contour_integral i) g"
  1268   using assms has_contour_integral_shiftpath has_contour_integral_shiftpath_D by blast
  1269 
  1270 lemma contour_integrable_on_shiftpath_eq:
  1271   assumes "valid_path g" "pathfinish g = pathstart g" "a \<in> {0..1}"
  1272     shows "f contour_integrable_on (shiftpath a g) \<longleftrightarrow> f contour_integrable_on g"
  1273 using assms contour_integrable_on_def has_contour_integral_shiftpath_eq by auto
  1274 
  1275 lemma contour_integral_shiftpath:
  1276   assumes "valid_path g" "pathfinish g = pathstart g" "a \<in> {0..1}"
  1277     shows "contour_integral (shiftpath a g) f = contour_integral g f"
  1278    using assms
  1279    by (simp add: contour_integral_def contour_integrable_on_def has_contour_integral_shiftpath_eq)
  1280 
  1281 
  1282 subsection%unimportant \<open>More about straight-line paths\<close>
  1283 
  1284 lemma has_vector_derivative_linepath_within:
  1285     "(linepath a b has_vector_derivative (b - a)) (at x within s)"
  1286 apply (simp add: linepath_def has_vector_derivative_def algebra_simps)
  1287 apply (rule derivative_eq_intros | simp)+
  1288 done
  1289 
  1290 lemma vector_derivative_linepath_within:
  1291     "x \<in> {0..1} \<Longrightarrow> vector_derivative (linepath a b) (at x within {0..1}) = b - a"
  1292   apply (rule vector_derivative_within_cbox [of 0 "1::real", simplified])
  1293   apply (auto simp: has_vector_derivative_linepath_within)
  1294   done
  1295 
  1296 lemma vector_derivative_linepath_at [simp]: "vector_derivative (linepath a b) (at x) = b - a"
  1297   by (simp add: has_vector_derivative_linepath_within vector_derivative_at)
  1298 
  1299 lemma valid_path_linepath [iff]: "valid_path (linepath a b)"
  1300   apply (simp add: valid_path_def piecewise_C1_differentiable_on_def C1_differentiable_on_eq continuous_on_linepath)
  1301   apply (rule_tac x="{}" in exI)
  1302   apply (simp add: differentiable_on_def differentiable_def)
  1303   using has_vector_derivative_def has_vector_derivative_linepath_within
  1304   apply (fastforce simp add: continuous_on_eq_continuous_within)
  1305   done
  1306 
  1307 lemma has_contour_integral_linepath:
  1308   shows "(f has_contour_integral i) (linepath a b) \<longleftrightarrow>
  1309          ((\<lambda>x. f(linepath a b x) * (b - a)) has_integral i) {0..1}"
  1310   by (simp add: has_contour_integral vector_derivative_linepath_at)
  1311 
  1312 lemma linepath_in_path:
  1313   shows "x \<in> {0..1} \<Longrightarrow> linepath a b x \<in> closed_segment a b"
  1314   by (auto simp: segment linepath_def)
  1315 
  1316 lemma linepath_image_01: "linepath a b ` {0..1} = closed_segment a b"
  1317   by (auto simp: segment linepath_def)
  1318 
  1319 lemma linepath_in_convex_hull:
  1320     fixes x::real
  1321     assumes a: "a \<in> convex hull s"
  1322         and b: "b \<in> convex hull s"
  1323         and x: "0\<le>x" "x\<le>1"
  1324        shows "linepath a b x \<in> convex hull s"
  1325   apply (rule closed_segment_subset_convex_hull [OF a b, THEN subsetD])
  1326   using x
  1327   apply (auto simp: linepath_image_01 [symmetric])
  1328   done
  1329 
  1330 lemma Re_linepath: "Re(linepath (of_real a) (of_real b) x) = (1 - x)*a + x*b"
  1331   by (simp add: linepath_def)
  1332 
  1333 lemma Im_linepath: "Im(linepath (of_real a) (of_real b) x) = 0"
  1334   by (simp add: linepath_def)
  1335 
  1336 lemma has_contour_integral_trivial [iff]: "(f has_contour_integral 0) (linepath a a)"
  1337   by (simp add: has_contour_integral_linepath)
  1338 
  1339 lemma has_contour_integral_trivial_iff [simp]: "(f has_contour_integral i) (linepath a a) \<longleftrightarrow> i=0"
  1340   using has_contour_integral_unique by blast
  1341 
  1342 lemma contour_integral_trivial [simp]: "contour_integral (linepath a a) f = 0"
  1343   using has_contour_integral_trivial contour_integral_unique by blast
  1344 
  1345 lemma differentiable_linepath [intro]: "linepath a b differentiable at x within A"
  1346   by (auto simp: linepath_def)
  1347 
  1348 lemma bounded_linear_linepath:
  1349   assumes "bounded_linear f"
  1350   shows   "f (linepath a b x) = linepath (f a) (f b) x"
  1351 proof -
  1352   interpret f: bounded_linear f by fact
  1353   show ?thesis by (simp add: linepath_def f.add f.scale)
  1354 qed
  1355 
  1356 lemma bounded_linear_linepath':
  1357   assumes "bounded_linear f"
  1358   shows   "f \<circ> linepath a b = linepath (f a) (f b)"
  1359   using bounded_linear_linepath[OF assms] by (simp add: fun_eq_iff)
  1360 
  1361 lemma cnj_linepath: "cnj (linepath a b x) = linepath (cnj a) (cnj b) x"
  1362   by (simp add: linepath_def)
  1363 
  1364 lemma cnj_linepath': "cnj \<circ> linepath a b = linepath (cnj a) (cnj b)"
  1365   by (simp add: linepath_def fun_eq_iff)
  1366 
  1367 subsection\<open>Relation to subpath construction\<close>
  1368 
  1369 lemma valid_path_subpath:
  1370   fixes g :: "real \<Rightarrow> 'a :: real_normed_vector"
  1371   assumes "valid_path g" "u \<in> {0..1}" "v \<in> {0..1}"
  1372     shows "valid_path(subpath u v g)"
  1373 proof (cases "v=u")
  1374   case True
  1375   then show ?thesis
  1376     unfolding valid_path_def subpath_def
  1377     by (force intro: C1_differentiable_on_const C1_differentiable_imp_piecewise)
  1378 next
  1379   case False
  1380   have "(g \<circ> (\<lambda>x. ((v-u) * x + u))) piecewise_C1_differentiable_on {0..1}"
  1381     apply (rule piecewise_C1_differentiable_compose)
  1382     apply (simp add: C1_differentiable_imp_piecewise)
  1383      apply (simp add: image_affinity_atLeastAtMost)
  1384     using assms False
  1385     apply (auto simp: algebra_simps valid_path_def piecewise_C1_differentiable_on_subset)
  1386     apply (subst Int_commute)
  1387     apply (auto simp: inj_on_def algebra_simps crossproduct_eq finite_vimage_IntI)
  1388     done
  1389   then show ?thesis
  1390     by (auto simp: o_def valid_path_def subpath_def)
  1391 qed
  1392 
  1393 lemma has_contour_integral_subpath_refl [iff]: "(f has_contour_integral 0) (subpath u u g)"
  1394   by (simp add: has_contour_integral subpath_def)
  1395 
  1396 lemma contour_integrable_subpath_refl [iff]: "f contour_integrable_on (subpath u u g)"
  1397   using has_contour_integral_subpath_refl contour_integrable_on_def by blast
  1398 
  1399 lemma contour_integral_subpath_refl [simp]: "contour_integral (subpath u u g) f = 0"
  1400   by (simp add: has_contour_integral_subpath_refl contour_integral_unique)
  1401 
  1402 lemma has_contour_integral_subpath:
  1403   assumes f: "f contour_integrable_on g" and g: "valid_path g"
  1404       and uv: "u \<in> {0..1}" "v \<in> {0..1}" "u \<le> v"
  1405     shows "(f has_contour_integral  integral {u..v} (\<lambda>x. f(g x) * vector_derivative g (at x)))
  1406            (subpath u v g)"
  1407 proof (cases "v=u")
  1408   case True
  1409   then show ?thesis
  1410     using f   by (simp add: contour_integrable_on_def subpath_def has_contour_integral)
  1411 next
  1412   case False
  1413   obtain s where s: "\<And>x. x \<in> {0..1} - s \<Longrightarrow> g differentiable at x" and fs: "finite s"
  1414     using g unfolding piecewise_C1_differentiable_on_def C1_differentiable_on_eq valid_path_def by blast
  1415   have *: "((\<lambda>x. f (g ((v - u) * x + u)) * vector_derivative g (at ((v - u) * x + u)))
  1416             has_integral (1 / (v - u)) * integral {u..v} (\<lambda>t. f (g t) * vector_derivative g (at t)))
  1417            {0..1}"
  1418     using f uv
  1419     apply (simp add: contour_integrable_on subpath_def has_contour_integral)
  1420     apply (drule integrable_on_subcbox [where a=u and b=v, simplified])
  1421     apply (simp_all add: has_integral_integral)
  1422     apply (drule has_integral_affinity [where m="v-u" and c=u, simplified])
  1423     apply (simp_all add: False image_affinity_atLeastAtMost_div_diff scaleR_conv_of_real)
  1424     apply (simp add: divide_simps False)
  1425     done
  1426   { fix x
  1427     have "x \<in> {0..1} \<Longrightarrow>
  1428            x \<notin> (\<lambda>t. (v-u) *\<^sub>R t + u) -` s \<Longrightarrow>
  1429            vector_derivative (\<lambda>x. g ((v-u) * x + u)) (at x) = (v-u) *\<^sub>R vector_derivative g (at ((v-u) * x + u))"
  1430       apply (rule vector_derivative_at [OF vector_diff_chain_at [simplified o_def]])
  1431       apply (intro derivative_eq_intros | simp)+
  1432       apply (cut_tac s [of "(v - u) * x + u"])
  1433       using uv mult_left_le [of x "v-u"]
  1434       apply (auto simp:  vector_derivative_works)
  1435       done
  1436   } note vd = this
  1437   show ?thesis
  1438     apply (cut_tac has_integral_cmul [OF *, where c = "v-u"])
  1439     using fs assms
  1440     apply (simp add: False subpath_def has_contour_integral)
  1441     apply (rule_tac S = "(\<lambda>t. ((v-u) *\<^sub>R t + u)) -` s" in has_integral_spike_finite)
  1442     apply (auto simp: inj_on_def False finite_vimageI vd scaleR_conv_of_real)
  1443     done
  1444 qed
  1445 
  1446 lemma contour_integrable_subpath:
  1447   assumes "f contour_integrable_on g" "valid_path g" "u \<in> {0..1}" "v \<in> {0..1}"
  1448     shows "f contour_integrable_on (subpath u v g)"
  1449   apply (cases u v rule: linorder_class.le_cases)
  1450    apply (metis contour_integrable_on_def has_contour_integral_subpath [OF assms])
  1451   apply (subst reversepath_subpath [symmetric])
  1452   apply (rule contour_integrable_reversepath)
  1453    using assms apply (blast intro: valid_path_subpath)
  1454   apply (simp add: contour_integrable_on_def)
  1455   using assms apply (blast intro: has_contour_integral_subpath)
  1456   done
  1457 
  1458 lemma has_integral_contour_integral_subpath:
  1459   assumes "f contour_integrable_on g" "valid_path g" "u \<in> {0..1}" "v \<in> {0..1}" "u \<le> v"
  1460     shows "(((\<lambda>x. f(g x) * vector_derivative g (at x)))
  1461             has_integral  contour_integral (subpath u v g) f) {u..v}"
  1462   using assms
  1463   apply (auto simp: has_integral_integrable_integral)
  1464   apply (rule integrable_on_subcbox [where a=u and b=v and S = "{0..1}", simplified])
  1465   apply (auto simp: contour_integral_unique [OF has_contour_integral_subpath] contour_integrable_on)
  1466   done
  1467 
  1468 lemma contour_integral_subcontour_integral:
  1469   assumes "f contour_integrable_on g" "valid_path g" "u \<in> {0..1}" "v \<in> {0..1}" "u \<le> v"
  1470     shows "contour_integral (subpath u v g) f =
  1471            integral {u..v} (\<lambda>x. f(g x) * vector_derivative g (at x))"
  1472   using assms has_contour_integral_subpath contour_integral_unique by blast
  1473 
  1474 lemma contour_integral_subpath_combine_less:
  1475   assumes "f contour_integrable_on g" "valid_path g" "u \<in> {0..1}" "v \<in> {0..1}" "w \<in> {0..1}"
  1476           "u<v" "v<w"
  1477     shows "contour_integral (subpath u v g) f + contour_integral (subpath v w g) f =
  1478            contour_integral (subpath u w g) f"
  1479   using assms apply (auto simp: contour_integral_subcontour_integral)
  1480   apply (rule integral_combine, auto)
  1481   apply (rule integrable_on_subcbox [where a=u and b=w and S = "{0..1}", simplified])
  1482   apply (auto simp: contour_integrable_on)
  1483   done
  1484 
  1485 lemma contour_integral_subpath_combine:
  1486   assumes "f contour_integrable_on g" "valid_path g" "u \<in> {0..1}" "v \<in> {0..1}" "w \<in> {0..1}"
  1487     shows "contour_integral (subpath u v g) f + contour_integral (subpath v w g) f =
  1488            contour_integral (subpath u w g) f"
  1489 proof (cases "u\<noteq>v \<and> v\<noteq>w \<and> u\<noteq>w")
  1490   case True
  1491     have *: "subpath v u g = reversepath(subpath u v g) \<and>
  1492              subpath w u g = reversepath(subpath u w g) \<and>
  1493              subpath w v g = reversepath(subpath v w g)"
  1494       by (auto simp: reversepath_subpath)
  1495     have "u < v \<and> v < w \<or>
  1496           u < w \<and> w < v \<or>
  1497           v < u \<and> u < w \<or>
  1498           v < w \<and> w < u \<or>
  1499           w < u \<and> u < v \<or>
  1500           w < v \<and> v < u"
  1501       using True assms by linarith
  1502     with assms show ?thesis
  1503       using contour_integral_subpath_combine_less [of f g u v w]
  1504             contour_integral_subpath_combine_less [of f g u w v]
  1505             contour_integral_subpath_combine_less [of f g v u w]
  1506             contour_integral_subpath_combine_less [of f g v w u]
  1507             contour_integral_subpath_combine_less [of f g w u v]
  1508             contour_integral_subpath_combine_less [of f g w v u]
  1509       apply simp
  1510       apply (elim disjE)
  1511       apply (auto simp: * contour_integral_reversepath contour_integrable_subpath
  1512                    valid_path_reversepath valid_path_subpath algebra_simps)
  1513       done
  1514 next
  1515   case False
  1516   then show ?thesis
  1517     apply (auto simp: contour_integral_subpath_refl)
  1518     using assms
  1519     by (metis eq_neg_iff_add_eq_0 contour_integrable_subpath contour_integral_reversepath reversepath_subpath valid_path_subpath)
  1520 qed
  1521 
  1522 lemma contour_integral_integral:
  1523      "contour_integral g f = integral {0..1} (\<lambda>x. f (g x) * vector_derivative g (at x))"
  1524   by (simp add: contour_integral_def integral_def has_contour_integral contour_integrable_on)
  1525 
  1526 lemma contour_integral_cong:
  1527   assumes "g = g'" "\<And>x. x \<in> path_image g \<Longrightarrow> f x = f' x"
  1528   shows   "contour_integral g f = contour_integral g' f'"
  1529   unfolding contour_integral_integral using assms
  1530   by (intro integral_cong) (auto simp: path_image_def)
  1531 
  1532 
  1533 text \<open>Contour integral along a segment on the real axis\<close>
  1534 
  1535 lemma has_contour_integral_linepath_Reals_iff:
  1536   fixes a b :: complex and f :: "complex \<Rightarrow> complex"
  1537   assumes "a \<in> Reals" "b \<in> Reals" "Re a < Re b"
  1538   shows   "(f has_contour_integral I) (linepath a b) \<longleftrightarrow>
  1539              ((\<lambda>x. f (of_real x)) has_integral I) {Re a..Re b}"
  1540 proof -
  1541   from assms have [simp]: "of_real (Re a) = a" "of_real (Re b) = b"
  1542     by (simp_all add: complex_eq_iff)
  1543   from assms have "a \<noteq> b" by auto
  1544   have "((\<lambda>x. f (of_real x)) has_integral I) (cbox (Re a) (Re b)) \<longleftrightarrow>
  1545           ((\<lambda>x. f (a + b * of_real x - a * of_real x)) has_integral I /\<^sub>R (Re b - Re a)) {0..1}"
  1546     by (subst has_integral_affinity_iff [of "Re b - Re a" _ "Re a", symmetric])
  1547        (insert assms, simp_all add: field_simps scaleR_conv_of_real)
  1548   also have "(\<lambda>x. f (a + b * of_real x - a * of_real x)) =
  1549                (\<lambda>x. (f (a + b * of_real x - a * of_real x) * (b - a)) /\<^sub>R (Re b - Re a))"
  1550     using \<open>a \<noteq> b\<close> by (auto simp: field_simps fun_eq_iff scaleR_conv_of_real)
  1551   also have "(\<dots> has_integral I /\<^sub>R (Re b - Re a)) {0..1} \<longleftrightarrow> 
  1552                ((\<lambda>x. f (linepath a b x) * (b - a)) has_integral I) {0..1}" using assms
  1553     by (subst has_integral_cmul_iff) (auto simp: linepath_def scaleR_conv_of_real algebra_simps)
  1554   also have "\<dots> \<longleftrightarrow> (f has_contour_integral I) (linepath a b)" unfolding has_contour_integral_def
  1555     by (intro has_integral_cong) (simp add: vector_derivative_linepath_within)
  1556   finally show ?thesis by simp
  1557 qed
  1558 
  1559 lemma contour_integrable_linepath_Reals_iff:
  1560   fixes a b :: complex and f :: "complex \<Rightarrow> complex"
  1561   assumes "a \<in> Reals" "b \<in> Reals" "Re a < Re b"
  1562   shows   "(f contour_integrable_on linepath a b) \<longleftrightarrow>
  1563              (\<lambda>x. f (of_real x)) integrable_on {Re a..Re b}"
  1564   using has_contour_integral_linepath_Reals_iff[OF assms, of f]
  1565   by (auto simp: contour_integrable_on_def integrable_on_def)
  1566 
  1567 lemma contour_integral_linepath_Reals_eq:
  1568   fixes a b :: complex and f :: "complex \<Rightarrow> complex"
  1569   assumes "a \<in> Reals" "b \<in> Reals" "Re a < Re b"
  1570   shows   "contour_integral (linepath a b) f = integral {Re a..Re b} (\<lambda>x. f (of_real x))"
  1571 proof (cases "f contour_integrable_on linepath a b")
  1572   case True
  1573   thus ?thesis using has_contour_integral_linepath_Reals_iff[OF assms, of f]
  1574     using has_contour_integral_integral has_contour_integral_unique by blast
  1575 next
  1576   case False
  1577   thus ?thesis using contour_integrable_linepath_Reals_iff[OF assms, of f]
  1578     by (simp add: not_integrable_contour_integral not_integrable_integral)
  1579 qed
  1580 
  1581 
  1582 
  1583 text\<open>Cauchy's theorem where there's a primitive\<close>
  1584 
  1585 lemma contour_integral_primitive_lemma:
  1586   fixes f :: "complex \<Rightarrow> complex" and g :: "real \<Rightarrow> complex"
  1587   assumes "a \<le> b"
  1588       and "\<And>x. x \<in> s \<Longrightarrow> (f has_field_derivative f' x) (at x within s)"
  1589       and "g piecewise_differentiable_on {a..b}"  "\<And>x. x \<in> {a..b} \<Longrightarrow> g x \<in> s"
  1590     shows "((\<lambda>x. f'(g x) * vector_derivative g (at x within {a..b}))
  1591              has_integral (f(g b) - f(g a))) {a..b}"
  1592 proof -
  1593   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"
  1594     using assms by (auto simp: piecewise_differentiable_on_def)
  1595   have cfg: "continuous_on {a..b} (\<lambda>x. f (g x))"
  1596     apply (rule continuous_on_compose [OF cg, unfolded o_def])
  1597     using assms
  1598     apply (metis field_differentiable_def field_differentiable_imp_continuous_at continuous_on_eq_continuous_within continuous_on_subset image_subset_iff)
  1599     done
  1600   { fix x::real
  1601     assume a: "a < x" and b: "x < b" and xk: "x \<notin> k"
  1602     then have "g differentiable at x within {a..b}"
  1603       using k by (simp add: differentiable_at_withinI)
  1604     then have "(g has_vector_derivative vector_derivative g (at x within {a..b})) (at x within {a..b})"
  1605       by (simp add: vector_derivative_works has_field_derivative_def scaleR_conv_of_real)
  1606     then have gdiff: "(g has_derivative (\<lambda>u. u * vector_derivative g (at x within {a..b}))) (at x within {a..b})"
  1607       by (simp add: has_vector_derivative_def scaleR_conv_of_real)
  1608     have "(f has_field_derivative (f' (g x))) (at (g x) within g ` {a..b})"
  1609       using assms by (metis a atLeastAtMost_iff b DERIV_subset image_subset_iff less_eq_real_def)
  1610     then have fdiff: "(f has_derivative (*) (f' (g x))) (at (g x) within g ` {a..b})"
  1611       by (simp add: has_field_derivative_def)
  1612     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})"
  1613       using diff_chain_within [OF gdiff fdiff]
  1614       by (simp add: has_vector_derivative_def scaleR_conv_of_real o_def mult_ac)
  1615   } note * = this
  1616   show ?thesis
  1617     apply (rule fundamental_theorem_of_calculus_interior_strong)
  1618     using k assms cfg *
  1619     apply (auto simp: at_within_Icc_at)
  1620     done
  1621 qed
  1622 
  1623 lemma contour_integral_primitive:
  1624   assumes "\<And>x. x \<in> s \<Longrightarrow> (f has_field_derivative f' x) (at x within s)"
  1625       and "valid_path g" "path_image g \<subseteq> s"
  1626     shows "(f' has_contour_integral (f(pathfinish g) - f(pathstart g))) g"
  1627   using assms
  1628   apply (simp add: valid_path_def path_image_def pathfinish_def pathstart_def has_contour_integral_def)
  1629   apply (auto intro!: piecewise_C1_imp_differentiable contour_integral_primitive_lemma [of 0 1 s])
  1630   done
  1631 
  1632 corollary Cauchy_theorem_primitive:
  1633   assumes "\<And>x. x \<in> s \<Longrightarrow> (f has_field_derivative f' x) (at x within s)"
  1634       and "valid_path g"  "path_image g \<subseteq> s" "pathfinish g = pathstart g"
  1635     shows "(f' has_contour_integral 0) g"
  1636   using assms
  1637   by (metis diff_self contour_integral_primitive)
  1638 
  1639 text\<open>Existence of path integral for continuous function\<close>
  1640 lemma contour_integrable_continuous_linepath:
  1641   assumes "continuous_on (closed_segment a b) f"
  1642   shows "f contour_integrable_on (linepath a b)"
  1643 proof -
  1644   have "continuous_on {0..1} ((\<lambda>x. f x * (b - a)) \<circ> linepath a b)"
  1645     apply (rule continuous_on_compose [OF continuous_on_linepath], simp add: linepath_image_01)
  1646     apply (rule continuous_intros | simp add: assms)+
  1647     done
  1648   then show ?thesis
  1649     apply (simp add: contour_integrable_on_def has_contour_integral_def integrable_on_def [symmetric])
  1650     apply (rule integrable_continuous [of 0 "1::real", simplified])
  1651     apply (rule continuous_on_eq [where f = "\<lambda>x. f(linepath a b x)*(b - a)"])
  1652     apply (auto simp: vector_derivative_linepath_within)
  1653     done
  1654 qed
  1655 
  1656 lemma has_field_der_id: "((\<lambda>x. x\<^sup>2 / 2) has_field_derivative x) (at x)"
  1657   by (rule has_derivative_imp_has_field_derivative)
  1658      (rule derivative_intros | simp)+
  1659 
  1660 lemma contour_integral_id [simp]: "contour_integral (linepath a b) (\<lambda>y. y) = (b^2 - a^2)/2"
  1661   apply (rule contour_integral_unique)
  1662   using contour_integral_primitive [of UNIV "\<lambda>x. x^2/2" "\<lambda>x. x" "linepath a b"]
  1663   apply (auto simp: field_simps has_field_der_id)
  1664   done
  1665 
  1666 lemma contour_integrable_on_const [iff]: "(\<lambda>x. c) contour_integrable_on (linepath a b)"
  1667   by (simp add: continuous_on_const contour_integrable_continuous_linepath)
  1668 
  1669 lemma contour_integrable_on_id [iff]: "(\<lambda>x. x) contour_integrable_on (linepath a b)"
  1670   by (simp add: continuous_on_id contour_integrable_continuous_linepath)
  1671 
  1672 subsection%unimportant \<open>Arithmetical combining theorems\<close>
  1673 
  1674 lemma has_contour_integral_neg:
  1675     "(f has_contour_integral i) g \<Longrightarrow> ((\<lambda>x. -(f x)) has_contour_integral (-i)) g"
  1676   by (simp add: has_integral_neg has_contour_integral_def)
  1677 
  1678 lemma has_contour_integral_add:
  1679     "\<lbrakk>(f1 has_contour_integral i1) g; (f2 has_contour_integral i2) g\<rbrakk>
  1680      \<Longrightarrow> ((\<lambda>x. f1 x + f2 x) has_contour_integral (i1 + i2)) g"
  1681   by (simp add: has_integral_add has_contour_integral_def algebra_simps)
  1682 
  1683 lemma has_contour_integral_diff:
  1684   "\<lbrakk>(f1 has_contour_integral i1) g; (f2 has_contour_integral i2) g\<rbrakk>
  1685          \<Longrightarrow> ((\<lambda>x. f1 x - f2 x) has_contour_integral (i1 - i2)) g"
  1686   by (simp add: has_integral_diff has_contour_integral_def algebra_simps)
  1687 
  1688 lemma has_contour_integral_lmul:
  1689   "(f has_contour_integral i) g \<Longrightarrow> ((\<lambda>x. c * (f x)) has_contour_integral (c*i)) g"
  1690 apply (simp add: has_contour_integral_def)
  1691 apply (drule has_integral_mult_right)
  1692 apply (simp add: algebra_simps)
  1693 done
  1694 
  1695 lemma has_contour_integral_rmul:
  1696   "(f has_contour_integral i) g \<Longrightarrow> ((\<lambda>x. (f x) * c) has_contour_integral (i*c)) g"
  1697 apply (drule has_contour_integral_lmul)
  1698 apply (simp add: mult.commute)
  1699 done
  1700 
  1701 lemma has_contour_integral_div:
  1702   "(f has_contour_integral i) g \<Longrightarrow> ((\<lambda>x. f x/c) has_contour_integral (i/c)) g"
  1703   by (simp add: field_class.field_divide_inverse) (metis has_contour_integral_rmul)
  1704 
  1705 lemma has_contour_integral_eq:
  1706     "\<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"
  1707 apply (simp add: path_image_def has_contour_integral_def)
  1708 by (metis (no_types, lifting) image_eqI has_integral_eq)
  1709 
  1710 lemma has_contour_integral_bound_linepath:
  1711   assumes "(f has_contour_integral i) (linepath a b)"
  1712           "0 \<le> B" "\<And>x. x \<in> closed_segment a b \<Longrightarrow> norm(f x) \<le> B"
  1713     shows "norm i \<le> B * norm(b - a)"
  1714 proof -
  1715   { fix x::real
  1716     assume x: "0 \<le> x" "x \<le> 1"
  1717   have "norm (f (linepath a b x)) *
  1718         norm (vector_derivative (linepath a b) (at x within {0..1})) \<le> B * norm (b - a)"
  1719     by (auto intro: mult_mono simp: assms linepath_in_path of_real_linepath vector_derivative_linepath_within x)
  1720   } note * = this
  1721   have "norm i \<le> (B * norm (b - a)) * content (cbox 0 (1::real))"
  1722     apply (rule has_integral_bound
  1723        [of _ "\<lambda>x. f (linepath a b x) * vector_derivative (linepath a b) (at x within {0..1})"])
  1724     using assms * unfolding has_contour_integral_def
  1725     apply (auto simp: norm_mult)
  1726     done
  1727   then show ?thesis
  1728     by (auto simp: content_real)
  1729 qed
  1730 
  1731 (*UNUSED
  1732 lemma has_contour_integral_bound_linepath_strong:
  1733   fixes a :: real and f :: "complex \<Rightarrow> real"
  1734   assumes "(f has_contour_integral i) (linepath a b)"
  1735           "finite k"
  1736           "0 \<le> B" "\<And>x::real. x \<in> closed_segment a b - k \<Longrightarrow> norm(f x) \<le> B"
  1737     shows "norm i \<le> B*norm(b - a)"
  1738 *)
  1739 
  1740 lemma has_contour_integral_const_linepath: "((\<lambda>x. c) has_contour_integral c*(b - a))(linepath a b)"
  1741   unfolding has_contour_integral_linepath
  1742   by (metis content_real diff_0_right has_integral_const_real lambda_one of_real_1 scaleR_conv_of_real zero_le_one)
  1743 
  1744 lemma has_contour_integral_0: "((\<lambda>x. 0) has_contour_integral 0) g"
  1745   by (simp add: has_contour_integral_def)
  1746 
  1747 lemma has_contour_integral_is_0:
  1748     "(\<And>z. z \<in> path_image g \<Longrightarrow> f z = 0) \<Longrightarrow> (f has_contour_integral 0) g"
  1749   by (rule has_contour_integral_eq [OF has_contour_integral_0]) auto
  1750 
  1751 lemma has_contour_integral_sum:
  1752     "\<lbrakk>finite s; \<And>a. a \<in> s \<Longrightarrow> (f a has_contour_integral i a) p\<rbrakk>
  1753      \<Longrightarrow> ((\<lambda>x. sum (\<lambda>a. f a x) s) has_contour_integral sum i s) p"
  1754   by (induction s rule: finite_induct) (auto simp: has_contour_integral_0 has_contour_integral_add)
  1755 
  1756 subsection%unimportant \<open>Operations on path integrals\<close>
  1757 
  1758 lemma contour_integral_const_linepath [simp]: "contour_integral (linepath a b) (\<lambda>x. c) = c*(b - a)"
  1759   by (rule contour_integral_unique [OF has_contour_integral_const_linepath])
  1760 
  1761 lemma contour_integral_neg:
  1762     "f contour_integrable_on g \<Longrightarrow> contour_integral g (\<lambda>x. -(f x)) = -(contour_integral g f)"
  1763   by (simp add: contour_integral_unique has_contour_integral_integral has_contour_integral_neg)
  1764 
  1765 lemma contour_integral_add:
  1766     "f1 contour_integrable_on g \<Longrightarrow> f2 contour_integrable_on g \<Longrightarrow> contour_integral g (\<lambda>x. f1 x + f2 x) =
  1767                 contour_integral g f1 + contour_integral g f2"
  1768   by (simp add: contour_integral_unique has_contour_integral_integral has_contour_integral_add)
  1769 
  1770 lemma contour_integral_diff:
  1771     "f1 contour_integrable_on g \<Longrightarrow> f2 contour_integrable_on g \<Longrightarrow> contour_integral g (\<lambda>x. f1 x - f2 x) =
  1772                 contour_integral g f1 - contour_integral g f2"
  1773   by (simp add: contour_integral_unique has_contour_integral_integral has_contour_integral_diff)
  1774 
  1775 lemma contour_integral_lmul:
  1776   shows "f contour_integrable_on g
  1777            \<Longrightarrow> contour_integral g (\<lambda>x. c * f x) = c*contour_integral g f"
  1778   by (simp add: contour_integral_unique has_contour_integral_integral has_contour_integral_lmul)
  1779 
  1780 lemma contour_integral_rmul:
  1781   shows "f contour_integrable_on g
  1782         \<Longrightarrow> contour_integral g (\<lambda>x. f x * c) = contour_integral g f * c"
  1783   by (simp add: contour_integral_unique has_contour_integral_integral has_contour_integral_rmul)
  1784 
  1785 lemma contour_integral_div:
  1786   shows "f contour_integrable_on g
  1787         \<Longrightarrow> contour_integral g (\<lambda>x. f x / c) = contour_integral g f / c"
  1788   by (simp add: contour_integral_unique has_contour_integral_integral has_contour_integral_div)
  1789 
  1790 lemma contour_integral_eq:
  1791     "(\<And>x. x \<in> path_image p \<Longrightarrow> f x = g x) \<Longrightarrow> contour_integral p f = contour_integral p g"
  1792   apply (simp add: contour_integral_def)
  1793   using has_contour_integral_eq
  1794   by (metis contour_integral_unique has_contour_integral_integrable has_contour_integral_integral)
  1795 
  1796 lemma contour_integral_eq_0:
  1797     "(\<And>z. z \<in> path_image g \<Longrightarrow> f z = 0) \<Longrightarrow> contour_integral g f = 0"
  1798   by (simp add: has_contour_integral_is_0 contour_integral_unique)
  1799 
  1800 lemma contour_integral_bound_linepath:
  1801   shows
  1802     "\<lbrakk>f contour_integrable_on (linepath a b);
  1803       0 \<le> B; \<And>x. x \<in> closed_segment a b \<Longrightarrow> norm(f x) \<le> B\<rbrakk>
  1804      \<Longrightarrow> norm(contour_integral (linepath a b) f) \<le> B*norm(b - a)"
  1805   apply (rule has_contour_integral_bound_linepath [of f])
  1806   apply (auto simp: has_contour_integral_integral)
  1807   done
  1808 
  1809 lemma contour_integral_0 [simp]: "contour_integral g (\<lambda>x. 0) = 0"
  1810   by (simp add: contour_integral_unique has_contour_integral_0)
  1811 
  1812 lemma contour_integral_sum:
  1813     "\<lbrakk>finite s; \<And>a. a \<in> s \<Longrightarrow> (f a) contour_integrable_on p\<rbrakk>
  1814      \<Longrightarrow> contour_integral p (\<lambda>x. sum (\<lambda>a. f a x) s) = sum (\<lambda>a. contour_integral p (f a)) s"
  1815   by (auto simp: contour_integral_unique has_contour_integral_sum has_contour_integral_integral)
  1816 
  1817 lemma contour_integrable_eq:
  1818     "\<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"
  1819   unfolding contour_integrable_on_def
  1820   by (metis has_contour_integral_eq)
  1821 
  1822 
  1823 subsection%unimportant \<open>Arithmetic theorems for path integrability\<close>
  1824 
  1825 lemma contour_integrable_neg:
  1826     "f contour_integrable_on g \<Longrightarrow> (\<lambda>x. -(f x)) contour_integrable_on g"
  1827   using has_contour_integral_neg contour_integrable_on_def by blast
  1828 
  1829 lemma contour_integrable_add:
  1830     "\<lbrakk>f1 contour_integrable_on g; f2 contour_integrable_on g\<rbrakk> \<Longrightarrow> (\<lambda>x. f1 x + f2 x) contour_integrable_on g"
  1831   using has_contour_integral_add contour_integrable_on_def
  1832   by fastforce
  1833 
  1834 lemma contour_integrable_diff:
  1835     "\<lbrakk>f1 contour_integrable_on g; f2 contour_integrable_on g\<rbrakk> \<Longrightarrow> (\<lambda>x. f1 x - f2 x) contour_integrable_on g"
  1836   using has_contour_integral_diff contour_integrable_on_def
  1837   by fastforce
  1838 
  1839 lemma contour_integrable_lmul:
  1840     "f contour_integrable_on g \<Longrightarrow> (\<lambda>x. c * f x) contour_integrable_on g"
  1841   using has_contour_integral_lmul contour_integrable_on_def
  1842   by fastforce
  1843 
  1844 lemma contour_integrable_rmul:
  1845     "f contour_integrable_on g \<Longrightarrow> (\<lambda>x. f x * c) contour_integrable_on g"
  1846   using has_contour_integral_rmul contour_integrable_on_def
  1847   by fastforce
  1848 
  1849 lemma contour_integrable_div:
  1850     "f contour_integrable_on g \<Longrightarrow> (\<lambda>x. f x / c) contour_integrable_on g"
  1851   using has_contour_integral_div contour_integrable_on_def
  1852   by fastforce
  1853 
  1854 lemma contour_integrable_sum:
  1855     "\<lbrakk>finite s; \<And>a. a \<in> s \<Longrightarrow> (f a) contour_integrable_on p\<rbrakk>
  1856      \<Longrightarrow> (\<lambda>x. sum (\<lambda>a. f a x) s) contour_integrable_on p"
  1857    unfolding contour_integrable_on_def
  1858    by (metis has_contour_integral_sum)
  1859 
  1860 
  1861 subsection%unimportant \<open>Reversing a path integral\<close>
  1862 
  1863 lemma has_contour_integral_reverse_linepath:
  1864     "(f has_contour_integral i) (linepath a b)
  1865      \<Longrightarrow> (f has_contour_integral (-i)) (linepath b a)"
  1866   using has_contour_integral_reversepath valid_path_linepath by fastforce
  1867 
  1868 lemma contour_integral_reverse_linepath:
  1869     "continuous_on (closed_segment a b) f
  1870      \<Longrightarrow> contour_integral (linepath a b) f = - (contour_integral(linepath b a) f)"
  1871 apply (rule contour_integral_unique)
  1872 apply (rule has_contour_integral_reverse_linepath)
  1873 by (simp add: closed_segment_commute contour_integrable_continuous_linepath has_contour_integral_integral)
  1874 
  1875 
  1876 (* Splitting a path integral in a flat way.*)
  1877 
  1878 lemma has_contour_integral_split:
  1879   assumes f: "(f has_contour_integral i) (linepath a c)" "(f has_contour_integral j) (linepath c b)"
  1880       and k: "0 \<le> k" "k \<le> 1"
  1881       and c: "c - a = k *\<^sub>R (b - a)"
  1882     shows "(f has_contour_integral (i + j)) (linepath a b)"
  1883 proof (cases "k = 0 \<or> k = 1")
  1884   case True
  1885   then show ?thesis
  1886     using assms by auto
  1887 next
  1888   case False
  1889   then have k: "0 < k" "k < 1" "complex_of_real k \<noteq> 1"
  1890     using assms by auto
  1891   have c': "c = k *\<^sub>R (b - a) + a"
  1892     by (metis diff_add_cancel c)
  1893   have bc: "(b - c) = (1 - k) *\<^sub>R (b - a)"
  1894     by (simp add: algebra_simps c')
  1895   { assume *: "((\<lambda>x. f ((1 - x) *\<^sub>R a + x *\<^sub>R c) * (c - a)) has_integral i) {0..1}"
  1896     have **: "\<And>x. ((k - x) / k) *\<^sub>R a + (x / k) *\<^sub>R c = (1 - x) *\<^sub>R a + x *\<^sub>R b"
  1897       using False apply (simp add: c' algebra_simps)
  1898       apply (simp add: real_vector.scale_left_distrib [symmetric] divide_simps)
  1899       done
  1900     have "((\<lambda>x. f ((1 - x) *\<^sub>R a + x *\<^sub>R b) * (b - a)) has_integral i) {0..k}"
  1901       using k has_integral_affinity01 [OF *, of "inverse k" "0"]
  1902       apply (simp add: divide_simps mult.commute [of _ "k"] image_affinity_atLeastAtMost ** c)
  1903       apply (auto dest: has_integral_cmul [where c = "inverse k"])
  1904       done
  1905   } note fi = this
  1906   { assume *: "((\<lambda>x. f ((1 - x) *\<^sub>R c + x *\<^sub>R b) * (b - c)) has_integral j) {0..1}"
  1907     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)"
  1908       using k
  1909       apply (simp add: c' field_simps)
  1910       apply (simp add: scaleR_conv_of_real divide_simps)
  1911       apply (simp add: field_simps)
  1912       done
  1913     have "((\<lambda>x. f ((1 - x) *\<^sub>R a + x *\<^sub>R b) * (b - a)) has_integral j) {k..1}"
  1914       using k has_integral_affinity01 [OF *, of "inverse(1 - k)" "-(k/(1 - k))"]
  1915       apply (simp add: divide_simps mult.commute [of _ "1-k"] image_affinity_atLeastAtMost ** bc)
  1916       apply (auto dest: has_integral_cmul [where k = "(1 - k) *\<^sub>R j" and c = "inverse (1 - k)"])
  1917       done
  1918   } note fj = this
  1919   show ?thesis
  1920     using f k
  1921     apply (simp add: has_contour_integral_linepath)
  1922     apply (simp add: linepath_def)
  1923     apply (rule has_integral_combine [OF _ _ fi fj], simp_all)
  1924     done
  1925 qed
  1926 
  1927 lemma continuous_on_closed_segment_transform:
  1928   assumes f: "continuous_on (closed_segment a b) f"
  1929       and k: "0 \<le> k" "k \<le> 1"
  1930       and c: "c - a = k *\<^sub>R (b - a)"
  1931     shows "continuous_on (closed_segment a c) f"
  1932 proof -
  1933   have c': "c = (1 - k) *\<^sub>R a + k *\<^sub>R b"
  1934     using c by (simp add: algebra_simps)
  1935   have "closed_segment a c \<subseteq> closed_segment a b"
  1936     by (metis c' ends_in_segment(1) in_segment(1) k subset_closed_segment)
  1937   then show "continuous_on (closed_segment a c) f"
  1938     by (rule continuous_on_subset [OF f])
  1939 qed
  1940 
  1941 lemma contour_integral_split:
  1942   assumes f: "continuous_on (closed_segment a b) f"
  1943       and k: "0 \<le> k" "k \<le> 1"
  1944       and c: "c - a = k *\<^sub>R (b - a)"
  1945     shows "contour_integral(linepath a b) f = contour_integral(linepath a c) f + contour_integral(linepath c b) f"
  1946 proof -
  1947   have c': "c = (1 - k) *\<^sub>R a + k *\<^sub>R b"
  1948     using c by (simp add: algebra_simps)
  1949   have "closed_segment a c \<subseteq> closed_segment a b"
  1950     by (metis c' ends_in_segment(1) in_segment(1) k subset_closed_segment)
  1951   moreover have "closed_segment c b \<subseteq> closed_segment a b"
  1952     by (metis c' ends_in_segment(2) in_segment(1) k subset_closed_segment)
  1953   ultimately
  1954   have *: "continuous_on (closed_segment a c) f" "continuous_on (closed_segment c b) f"
  1955     by (auto intro: continuous_on_subset [OF f])
  1956   show ?thesis
  1957     by (rule contour_integral_unique) (meson "*" c contour_integrable_continuous_linepath has_contour_integral_integral has_contour_integral_split k)
  1958 qed
  1959 
  1960 lemma contour_integral_split_linepath:
  1961   assumes f: "continuous_on (closed_segment a b) f"
  1962       and c: "c \<in> closed_segment a b"
  1963     shows "contour_integral(linepath a b) f = contour_integral(linepath a c) f + contour_integral(linepath c b) f"
  1964   using c by (auto simp: closed_segment_def algebra_simps intro!: contour_integral_split [OF f])
  1965 
  1966 text\<open>The special case of midpoints used in the main quadrisection\<close>
  1967 
  1968 lemma has_contour_integral_midpoint:
  1969   assumes "(f has_contour_integral i) (linepath a (midpoint a b))"
  1970           "(f has_contour_integral j) (linepath (midpoint a b) b)"
  1971     shows "(f has_contour_integral (i + j)) (linepath a b)"
  1972   apply (rule has_contour_integral_split [where c = "midpoint a b" and k = "1/2"])
  1973   using assms
  1974   apply (auto simp: midpoint_def algebra_simps scaleR_conv_of_real)
  1975   done
  1976 
  1977 lemma contour_integral_midpoint:
  1978    "continuous_on (closed_segment a b) f
  1979     \<Longrightarrow> contour_integral (linepath a b) f =
  1980         contour_integral (linepath a (midpoint a b)) f + contour_integral (linepath (midpoint a b) b) f"
  1981   apply (rule contour_integral_split [where c = "midpoint a b" and k = "1/2"])
  1982   apply (auto simp: midpoint_def algebra_simps scaleR_conv_of_real)
  1983   done
  1984 
  1985 
  1986 text\<open>A couple of special case lemmas that are useful below\<close>
  1987 
  1988 lemma triangle_linear_has_chain_integral:
  1989     "((\<lambda>x. m*x + d) has_contour_integral 0) (linepath a b +++ linepath b c +++ linepath c a)"
  1990   apply (rule Cauchy_theorem_primitive [of UNIV "\<lambda>x. m/2 * x^2 + d*x"])
  1991   apply (auto intro!: derivative_eq_intros)
  1992   done
  1993 
  1994 lemma has_chain_integral_chain_integral3:
  1995      "(f has_contour_integral i) (linepath a b +++ linepath b c +++ linepath c d)
  1996       \<Longrightarrow> contour_integral (linepath a b) f + contour_integral (linepath b c) f + contour_integral (linepath c d) f = i"
  1997   apply (subst contour_integral_unique [symmetric], assumption)
  1998   apply (drule has_contour_integral_integrable)
  1999   apply (simp add: valid_path_join)
  2000   done
  2001 
  2002 lemma has_chain_integral_chain_integral4:
  2003      "(f has_contour_integral i) (linepath a b +++ linepath b c +++ linepath c d +++ linepath d e)
  2004       \<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"
  2005   apply (subst contour_integral_unique [symmetric], assumption)
  2006   apply (drule has_contour_integral_integrable)
  2007   apply (simp add: valid_path_join)
  2008   done
  2009 
  2010 subsection\<open>Reversing the order in a double path integral\<close>
  2011 
  2012 text\<open>The condition is stronger than needed but it's often true in typical situations\<close>
  2013 
  2014 lemma fst_im_cbox [simp]: "cbox c d \<noteq> {} \<Longrightarrow> (fst ` cbox (a,c) (b,d)) = cbox a b"
  2015   by (auto simp: cbox_Pair_eq)
  2016 
  2017 lemma snd_im_cbox [simp]: "cbox a b \<noteq> {} \<Longrightarrow> (snd ` cbox (a,c) (b,d)) = cbox c d"
  2018   by (auto simp: cbox_Pair_eq)
  2019 
  2020 proposition contour_integral_swap:
  2021   assumes fcon:  "continuous_on (path_image g \<times> path_image h) (\<lambda>(y1,y2). f y1 y2)"
  2022       and vp:    "valid_path g" "valid_path h"
  2023       and gvcon: "continuous_on {0..1} (\<lambda>t. vector_derivative g (at t))"
  2024       and hvcon: "continuous_on {0..1} (\<lambda>t. vector_derivative h (at t))"
  2025   shows "contour_integral g (\<lambda>w. contour_integral h (f w)) =
  2026          contour_integral h (\<lambda>z. contour_integral g (\<lambda>w. f w z))"
  2027 proof -
  2028   have gcon: "continuous_on {0..1} g" and hcon: "continuous_on {0..1} h"
  2029     using assms by (auto simp: valid_path_def piecewise_C1_differentiable_on_def)
  2030   have fgh1: "\<And>x. (\<lambda>t. f (g x) (h t)) = (\<lambda>(y1,y2). f y1 y2) \<circ> (\<lambda>t. (g x, h t))"
  2031     by (rule ext) simp
  2032   have fgh2: "\<And>x. (\<lambda>t. f (g t) (h x)) = (\<lambda>(y1,y2). f y1 y2) \<circ> (\<lambda>t. (g t, h x))"
  2033     by (rule ext) simp
  2034   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)"
  2035     by (rule continuous_on_subset [OF fcon]) (auto simp: path_image_def)
  2036   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)"
  2037     by (rule continuous_on_subset [OF fcon]) (auto simp: path_image_def)
  2038   have "\<And>y. y \<in> {0..1} \<Longrightarrow> continuous_on {0..1} (\<lambda>x. f (g x) (h y))"
  2039     by (subst fgh2) (rule fcon_im2 gcon continuous_intros | simp)+
  2040   then 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}"
  2041     using continuous_on_mult gvcon integrable_continuous_real by blast
  2042   have "(\<lambda>z. vector_derivative g (at (fst z))) = (\<lambda>x. vector_derivative g (at x)) \<circ> fst"
  2043     by auto
  2044   then have gvcon': "continuous_on (cbox (0, 0) (1, 1::real)) (\<lambda>x. vector_derivative g (at (fst x)))"
  2045     apply (rule ssubst)
  2046     apply (rule continuous_intros | simp add: gvcon)+
  2047     done
  2048   have "(\<lambda>z. vector_derivative h (at (snd z))) = (\<lambda>x. vector_derivative h (at x)) \<circ> snd"
  2049     by auto
  2050   then have hvcon': "continuous_on (cbox (0, 0) (1::real, 1)) (\<lambda>x. vector_derivative h (at (snd x)))"
  2051     apply (rule ssubst)
  2052     apply (rule continuous_intros | simp add: hvcon)+
  2053     done
  2054   have "(\<lambda>x. f (g (fst x)) (h (snd x))) = (\<lambda>(y1,y2). f y1 y2) \<circ> (\<lambda>w. ((g \<circ> fst) w, (h \<circ> snd) w))"
  2055     by auto
  2056   then have fgh: "continuous_on (cbox (0, 0) (1, 1)) (\<lambda>x. f (g (fst x)) (h (snd x)))"
  2057     apply (rule ssubst)
  2058     apply (rule gcon hcon continuous_intros | simp)+
  2059     apply (auto simp: path_image_def intro: continuous_on_subset [OF fcon])
  2060     done
  2061   have "integral {0..1} (\<lambda>x. contour_integral h (f (g x)) * vector_derivative g (at x)) =
  2062         integral {0..1} (\<lambda>x. contour_integral h (\<lambda>y. f (g x) y * vector_derivative g (at x)))"
  2063   proof (rule integral_cong [OF contour_integral_rmul [symmetric]])
  2064     show "\<And>x. x \<in> {0..1} \<Longrightarrow> f (g x) contour_integrable_on h"
  2065       unfolding contour_integrable_on
  2066     apply (rule integrable_continuous_real)
  2067     apply (rule continuous_on_mult [OF _ hvcon])
  2068     apply (subst fgh1)
  2069     apply (rule fcon_im1 hcon continuous_intros | simp)+
  2070       done
  2071   qed
  2072   also have "\<dots> = integral {0..1}
  2073                      (\<lambda>y. contour_integral g (\<lambda>x. f x (h y) * vector_derivative h (at y)))"
  2074     unfolding contour_integral_integral
  2075     apply (subst integral_swap_continuous [where 'a = real and 'b = real, of 0 0 1 1, simplified])
  2076      apply (rule fgh gvcon' hvcon' continuous_intros | simp add: split_def)+
  2077     unfolding integral_mult_left [symmetric]
  2078     apply (simp only: mult_ac)
  2079     done
  2080   also have "\<dots> = contour_integral h (\<lambda>z. contour_integral g (\<lambda>w. f w z))"
  2081     unfolding contour_integral_integral
  2082     apply (rule integral_cong)
  2083     unfolding integral_mult_left [symmetric]
  2084     apply (simp add: algebra_simps)
  2085     done
  2086   finally show ?thesis
  2087     by (simp add: contour_integral_integral)
  2088 qed
  2089 
  2090 
  2091 subsection%unimportant \<open>The key quadrisection step\<close>
  2092 
  2093 lemma norm_sum_half:
  2094   assumes "norm(a + b) \<ge> e"
  2095     shows "norm a \<ge> e/2 \<or> norm b \<ge> e/2"
  2096 proof -
  2097   have "e \<le> norm (- a - b)"
  2098     by (simp add: add.commute assms norm_minus_commute)
  2099   thus ?thesis
  2100     using norm_triangle_ineq4 order_trans by fastforce
  2101 qed
  2102 
  2103 lemma norm_sum_lemma:
  2104   assumes "e \<le> norm (a + b + c + d)"
  2105     shows "e / 4 \<le> norm a \<or> e / 4 \<le> norm b \<or> e / 4 \<le> norm c \<or> e / 4 \<le> norm d"
  2106 proof -
  2107   have "e \<le> norm ((a + b) + (c + d))" using assms
  2108     by (simp add: algebra_simps)
  2109   then show ?thesis
  2110     by (auto dest!: norm_sum_half)
  2111 qed
  2112 
  2113 lemma Cauchy_theorem_quadrisection:
  2114   assumes f: "continuous_on (convex hull {a,b,c}) f"
  2115       and dist: "dist a b \<le> K" "dist b c \<le> K" "dist c a \<le> K"
  2116       and e: "e * K^2 \<le>
  2117               norm (contour_integral(linepath a b) f + contour_integral(linepath b c) f + contour_integral(linepath c a) f)"
  2118   shows "\<exists>a' b' c'.
  2119            a' \<in> convex hull {a,b,c} \<and> b' \<in> convex hull {a,b,c} \<and> c' \<in> convex hull {a,b,c} \<and>
  2120            dist a' b' \<le> K/2  \<and>  dist b' c' \<le> K/2  \<and>  dist c' a' \<le> K/2  \<and>
  2121            e * (K/2)^2 \<le> norm(contour_integral(linepath a' b') f + contour_integral(linepath b' c') f + contour_integral(linepath c' a') f)"
  2122          (is "\<exists>x y z. ?\<Phi> x y z")
  2123 proof -
  2124   note divide_le_eq_numeral1 [simp del]
  2125   define a' where "a' = midpoint b c"
  2126   define b' where "b' = midpoint c a"
  2127   define c' where "c' = midpoint a b"
  2128   have fabc: "continuous_on (closed_segment a b) f" "continuous_on (closed_segment b c) f" "continuous_on (closed_segment c a) f"
  2129     using f continuous_on_subset segments_subset_convex_hull by metis+
  2130   have fcont': "continuous_on (closed_segment c' b') f"
  2131                "continuous_on (closed_segment a' c') f"
  2132                "continuous_on (closed_segment b' a') f"
  2133     unfolding a'_def b'_def c'_def
  2134     by (rule continuous_on_subset [OF f],
  2135            metis midpoints_in_convex_hull convex_hull_subset hull_subset insert_subset segment_convex_hull)+
  2136   let ?pathint = "\<lambda>x y. contour_integral(linepath x y) f"
  2137   have *: "?pathint a b + ?pathint b c + ?pathint c a =
  2138           (?pathint a c' + ?pathint c' b' + ?pathint b' a) +
  2139           (?pathint a' c' + ?pathint c' b + ?pathint b a') +
  2140           (?pathint a' c + ?pathint c b' + ?pathint b' a') +
  2141           (?pathint a' b' + ?pathint b' c' + ?pathint c' a')"
  2142     by (simp add: fcont' contour_integral_reverse_linepath) (simp add: a'_def b'_def c'_def contour_integral_midpoint fabc)
  2143   have [simp]: "\<And>x y. cmod (x * 2 - y * 2) = cmod (x - y) * 2"
  2144     by (metis left_diff_distrib mult.commute norm_mult_numeral1)
  2145   have [simp]: "\<And>x y. cmod (x - y) = cmod (y - x)"
  2146     by (simp add: norm_minus_commute)
  2147   consider "e * K\<^sup>2 / 4 \<le> cmod (?pathint a c' + ?pathint c' b' + ?pathint b' a)" |
  2148            "e * K\<^sup>2 / 4 \<le> cmod (?pathint a' c' + ?pathint c' b + ?pathint b a')" |
  2149            "e * K\<^sup>2 / 4 \<le> cmod (?pathint a' c + ?pathint c b' + ?pathint b' a')" |
  2150            "e * K\<^sup>2 / 4 \<le> cmod (?pathint a' b' + ?pathint b' c' + ?pathint c' a')"
  2151     using assms unfolding * by (blast intro: that dest!: norm_sum_lemma)
  2152   then show ?thesis
  2153   proof cases
  2154     case 1 then have "?\<Phi> a c' b'"
  2155       using assms
  2156       apply (clarsimp simp: c'_def b'_def midpoints_in_convex_hull hull_subset [THEN subsetD])
  2157       apply (auto simp: midpoint_def dist_norm scaleR_conv_of_real divide_simps)
  2158       done
  2159     then show ?thesis by blast
  2160   next
  2161     case 2 then  have "?\<Phi> a' c' b"
  2162       using assms
  2163       apply (clarsimp simp: a'_def c'_def midpoints_in_convex_hull hull_subset [THEN subsetD])
  2164       apply (auto simp: midpoint_def dist_norm scaleR_conv_of_real divide_simps)
  2165       done
  2166     then show ?thesis by blast
  2167   next
  2168     case 3 then have "?\<Phi> a' c b'"
  2169       using assms
  2170       apply (clarsimp simp: a'_def b'_def midpoints_in_convex_hull hull_subset [THEN subsetD])
  2171       apply (auto simp: midpoint_def dist_norm scaleR_conv_of_real divide_simps)
  2172       done
  2173     then show ?thesis by blast
  2174   next
  2175     case 4 then have "?\<Phi> a' b' c'"
  2176       using assms
  2177       apply (clarsimp simp: a'_def c'_def b'_def midpoints_in_convex_hull hull_subset [THEN subsetD])
  2178       apply (auto simp: midpoint_def dist_norm scaleR_conv_of_real divide_simps)
  2179       done
  2180     then show ?thesis by blast
  2181   qed
  2182 qed
  2183 
  2184 subsection%unimportant \<open>Cauchy's theorem for triangles\<close>
  2185 
  2186 lemma triangle_points_closer:
  2187   fixes a::complex
  2188   shows "\<lbrakk>x \<in> convex hull {a,b,c};  y \<in> convex hull {a,b,c}\<rbrakk>
  2189          \<Longrightarrow> norm(x - y) \<le> norm(a - b) \<or>
  2190              norm(x - y) \<le> norm(b - c) \<or>
  2191              norm(x - y) \<le> norm(c - a)"
  2192   using simplex_extremal_le [of "{a,b,c}"]
  2193   by (auto simp: norm_minus_commute)
  2194 
  2195 lemma holomorphic_point_small_triangle:
  2196   assumes x: "x \<in> S"
  2197       and f: "continuous_on S f"
  2198       and cd: "f field_differentiable (at x within S)"
  2199       and e: "0 < e"
  2200     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>
  2201               x \<in> convex hull {a,b,c} \<and> convex hull {a,b,c} \<subseteq> S
  2202               \<longrightarrow> norm(contour_integral(linepath a b) f + contour_integral(linepath b c) f +
  2203                        contour_integral(linepath c a) f)
  2204                   \<le> e*(dist a b + dist b c + dist c a)^2"
  2205            (is "\<exists>k>0. \<forall>a b c. _ \<longrightarrow> ?normle a b c")
  2206 proof -
  2207   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>
  2208                      \<Longrightarrow> a \<le> e*(x + y + z)^2"
  2209     by (simp add: algebra_simps power2_eq_square)
  2210   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"
  2211              for x::real and a b c
  2212     by linarith
  2213   have fabc: "f contour_integrable_on linepath a b" "f contour_integrable_on linepath b c" "f contour_integrable_on linepath c a"
  2214               if "convex hull {a, b, c} \<subseteq> S" for a b c
  2215     using segments_subset_convex_hull that
  2216     by (metis continuous_on_subset f contour_integrable_continuous_linepath)+
  2217   note path_bound = has_contour_integral_bound_linepath [simplified norm_minus_commute, OF has_contour_integral_integral]
  2218   { fix f' a b c d
  2219     assume d: "0 < d"
  2220        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)"
  2221        and le: "cmod (a - b) \<le> d" "cmod (b - c) \<le> d" "cmod (c - a) \<le> d"
  2222        and xc: "x \<in> convex hull {a, b, c}"
  2223        and S: "convex hull {a, b, c} \<subseteq> S"
  2224     have pa: "contour_integral (linepath a b) f + contour_integral (linepath b c) f + contour_integral (linepath c a) f =
  2225               contour_integral (linepath a b) (\<lambda>y. f y - f x - f'*(y - x)) +
  2226               contour_integral (linepath b c) (\<lambda>y. f y - f x - f'*(y - x)) +
  2227               contour_integral (linepath c a) (\<lambda>y. f y - f x - f'*(y - x))"
  2228       apply (simp add: contour_integral_diff contour_integral_lmul contour_integrable_lmul contour_integrable_diff fabc [OF S])
  2229       apply (simp add: field_simps)
  2230       done
  2231     { fix y
  2232       assume yc: "y \<in> convex hull {a,b,c}"
  2233       have "cmod (f y - f x - f' * (y - x)) \<le> e*norm(y - x)"
  2234       proof (rule f')
  2235         show "cmod (y - x) \<le> d"
  2236           by (metis triangle_points_closer [OF xc yc] le norm_minus_commute order_trans)
  2237       qed (use S yc in blast)
  2238       also have "\<dots> \<le> e * (cmod (a - b) + cmod (b - c) + cmod (c - a))"
  2239         by (simp add: yc e xc disj_le [OF triangle_points_closer])
  2240       finally have "cmod (f y - f x - f' * (y - x)) \<le> e * (cmod (a - b) + cmod (b - c) + cmod (c - a))" .
  2241     } note cm_le = this
  2242     have "?normle a b c"
  2243       unfolding dist_norm pa
  2244       apply (rule le_of_3)
  2245       using f' xc S e
  2246       apply simp_all
  2247       apply (intro norm_triangle_le add_mono path_bound)
  2248       apply (simp_all add: contour_integral_diff contour_integral_lmul contour_integrable_lmul contour_integrable_diff fabc)
  2249       apply (blast intro: cm_le elim: dest: segments_subset_convex_hull [THEN subsetD])+
  2250       done
  2251   } note * = this
  2252   show ?thesis
  2253     using cd e
  2254     apply (simp add: field_differentiable_def has_field_derivative_def has_derivative_within_alt approachable_lt_le2 Ball_def)
  2255     apply (clarify dest!: spec mp)
  2256     using * unfolding dist_norm
  2257     apply blast
  2258     done
  2259 qed
  2260 
  2261 
  2262 text\<open>Hence the most basic theorem for a triangle.\<close>
  2263 
  2264 locale Chain =
  2265   fixes x0 At Follows
  2266   assumes At0: "At x0 0"
  2267       and AtSuc: "\<And>x n. At x n \<Longrightarrow> \<exists>x'. At x' (Suc n) \<and> Follows x' x"
  2268 begin
  2269   primrec f where
  2270     "f 0 = x0"
  2271   | "f (Suc n) = (SOME x. At x (Suc n) \<and> Follows x (f n))"
  2272 
  2273   lemma At: "At (f n) n"
  2274   proof (induct n)
  2275     case 0 show ?case
  2276       by (simp add: At0)
  2277   next
  2278     case (Suc n) show ?case
  2279       by (metis (no_types, lifting) AtSuc [OF Suc] f.simps(2) someI_ex)
  2280   qed
  2281 
  2282   lemma Follows: "Follows (f(Suc n)) (f n)"
  2283     by (metis (no_types, lifting) AtSuc [OF At [of n]] f.simps(2) someI_ex)
  2284 
  2285   declare f.simps(2) [simp del]
  2286 end
  2287 
  2288 lemma Chain3:
  2289   assumes At0: "At x0 y0 z0 0"
  2290       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"
  2291   obtains f g h where
  2292     "f 0 = x0" "g 0 = y0" "h 0 = z0"
  2293                       "\<And>n. At (f n) (g n) (h n) n"
  2294                        "\<And>n. Follows (f(Suc n)) (g(Suc n)) (h(Suc n)) (f n) (g n) (h n)"
  2295 proof -
  2296   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"
  2297     apply unfold_locales
  2298     using At0 AtSuc by auto
  2299   show ?thesis
  2300   apply (rule that [of "\<lambda>n. fst (three.f n)"  "\<lambda>n. fst (snd (three.f n))" "\<lambda>n. snd (snd (three.f n))"])
  2301   using three.At three.Follows
  2302   apply simp_all
  2303   apply (simp_all add: split_beta')
  2304   done
  2305 qed
  2306 
  2307 proposition%unimportant Cauchy_theorem_triangle:
  2308   assumes "f holomorphic_on (convex hull {a,b,c})"
  2309     shows "(f has_contour_integral 0) (linepath a b +++ linepath b c +++ linepath c a)"
  2310 proof -
  2311   have contf: "continuous_on (convex hull {a,b,c}) f"
  2312     by (metis assms holomorphic_on_imp_continuous_on)
  2313   let ?pathint = "\<lambda>x y. contour_integral(linepath x y) f"
  2314   { fix y::complex
  2315     assume fy: "(f has_contour_integral y) (linepath a b +++ linepath b c +++ linepath c a)"
  2316        and ynz: "y \<noteq> 0"
  2317     define K where "K = 1 + max (dist a b) (max (dist b c) (dist c a))"
  2318     define e where "e = norm y / K^2"
  2319     have K1: "K \<ge> 1"  by (simp add: K_def max.coboundedI1)
  2320     then have K: "K > 0" by linarith
  2321     have [iff]: "dist a b \<le> K" "dist b c \<le> K" "dist c a \<le> K"
  2322       by (simp_all add: K_def)
  2323     have e: "e > 0"
  2324       unfolding e_def using ynz K1 by simp
  2325     define At where "At x y z n \<longleftrightarrow>
  2326         convex hull {x,y,z} \<subseteq> convex hull {a,b,c} \<and>
  2327         dist x y \<le> K/2^n \<and> dist y z \<le> K/2^n \<and> dist z x \<le> K/2^n \<and>
  2328         norm(?pathint x y + ?pathint y z + ?pathint z x) \<ge> e*(K/2^n)^2"
  2329       for x y z n
  2330     have At0: "At a b c 0"
  2331       using fy
  2332       by (simp add: At_def e_def has_chain_integral_chain_integral3)
  2333     { fix x y z n
  2334       assume At: "At x y z n"
  2335       then have contf': "continuous_on (convex hull {x,y,z}) f"
  2336         using contf At_def continuous_on_subset by metis
  2337       have "\<exists>x' y' z'. At x' y' z' (Suc n) \<and> convex hull {x',y',z'} \<subseteq> convex hull {x,y,z}"
  2338         using At Cauchy_theorem_quadrisection [OF contf', of "K/2^n" e]
  2339         apply (simp add: At_def algebra_simps)
  2340         apply (meson convex_hull_subset empty_subsetI insert_subset subsetCE)
  2341         done
  2342     } note AtSuc = this
  2343     obtain fa fb fc
  2344       where f0 [simp]: "fa 0 = a" "fb 0 = b" "fc 0 = c"
  2345         and cosb: "\<And>n. convex hull {fa n, fb n, fc n} \<subseteq> convex hull {a,b,c}"
  2346         and dist: "\<And>n. dist (fa n) (fb n) \<le> K/2^n"
  2347                   "\<And>n. dist (fb n) (fc n) \<le> K/2^n"
  2348                   "\<And>n. dist (fc n) (fa n) \<le> K/2^n"
  2349         and no: "\<And>n. norm(?pathint (fa n) (fb n) +
  2350                            ?pathint (fb n) (fc n) +
  2351                            ?pathint (fc n) (fa n)) \<ge> e * (K/2^n)^2"
  2352         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}"
  2353       apply (rule Chain3 [of At, OF At0 AtSuc])
  2354       apply (auto simp: At_def)
  2355       done
  2356     obtain x where x: "\<And>n. x \<in> convex hull {fa n, fb n, fc n}"
  2357     proof (rule bounded_closed_nest)
  2358       show "\<And>n. closed (convex hull {fa n, fb n, fc n})"
  2359         by (simp add: compact_imp_closed finite_imp_compact_convex_hull)
  2360       show "\<And>m n. m \<le> n \<Longrightarrow> convex hull {fa n, fb n, fc n} \<subseteq> convex hull {fa m, fb m, fc m}"
  2361         by (erule transitive_stepwise_le) (auto simp: conv_le)
  2362     qed (fastforce intro: finite_imp_bounded_convex_hull)+
  2363     then have xin: "x \<in> convex hull {a,b,c}"
  2364       using assms f0 by blast
  2365     then have fx: "f field_differentiable at x within (convex hull {a,b,c})"
  2366       using assms holomorphic_on_def by blast
  2367     { fix k n
  2368       assume k: "0 < k"
  2369          and le:
  2370             "\<And>x' y' z'.
  2371                \<lbrakk>dist x' y' \<le> k; dist y' z' \<le> k; dist z' x' \<le> k;
  2372                 x \<in> convex hull {x',y',z'};
  2373                 convex hull {x',y',z'} \<subseteq> convex hull {a,b,c}\<rbrakk>
  2374                \<Longrightarrow>
  2375                cmod (?pathint x' y' + ?pathint y' z' + ?pathint z' x') * 10
  2376                      \<le> e * (dist x' y' + dist y' z' + dist z' x')\<^sup>2"
  2377          and Kk: "K / k < 2 ^ n"
  2378       have "K / 2 ^ n < k" using Kk k
  2379         by (auto simp: field_simps)
  2380       then have DD: "dist (fa n) (fb n) \<le> k" "dist (fb n) (fc n) \<le> k" "dist (fc n) (fa n) \<le> k"
  2381         using dist [of n]  k
  2382         by linarith+
  2383       have dle: "(dist (fa n) (fb n) + dist (fb n) (fc n) + dist (fc n) (fa n))\<^sup>2
  2384                \<le> (3 * K / 2 ^ n)\<^sup>2"
  2385         using dist [of n] e K
  2386         by (simp add: abs_le_square_iff [symmetric])
  2387       have less10: "\<And>x y::real. 0 < x \<Longrightarrow> y \<le> 9*x \<Longrightarrow> y < x*10"
  2388         by linarith
  2389       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"
  2390         using ynz dle e mult_le_cancel_left_pos by blast
  2391       also have "\<dots> <
  2392           cmod (?pathint (fa n) (fb n) + ?pathint (fb n) (fc n) + ?pathint (fc n) (fa n)) * 10"
  2393         using no [of n] e K
  2394         apply (simp add: e_def field_simps)
  2395         apply (simp only: zero_less_norm_iff [symmetric])
  2396         done
  2397       finally have False
  2398         using le [OF DD x cosb] by auto
  2399     } then
  2400     have ?thesis
  2401       using holomorphic_point_small_triangle [OF xin contf fx, of "e/10"] e
  2402       apply clarsimp
  2403       apply (rule_tac y1="K/k" in exE [OF real_arch_pow[of 2]], force+)
  2404       done
  2405   }
  2406   moreover have "f contour_integrable_on (linepath a b +++ linepath b c +++ linepath c a)"
  2407     by simp (meson contf continuous_on_subset contour_integrable_continuous_linepath segments_subset_convex_hull(1)
  2408                    segments_subset_convex_hull(3) segments_subset_convex_hull(5))
  2409   ultimately show ?thesis
  2410     using has_contour_integral_integral by fastforce
  2411 qed
  2412 
  2413 subsection%unimportant \<open>Version needing function holomorphic in interior only\<close>
  2414 
  2415 lemma Cauchy_theorem_flat_lemma:
  2416   assumes f: "continuous_on (convex hull {a,b,c}) f"
  2417       and c: "c - a = k *\<^sub>R (b - a)"
  2418       and k: "0 \<le> k"
  2419     shows "contour_integral (linepath a b) f + contour_integral (linepath b c) f +
  2420           contour_integral (linepath c a) f = 0"
  2421 proof -
  2422   have fabc: "continuous_on (closed_segment a b) f" "continuous_on (closed_segment b c) f" "continuous_on (closed_segment c a) f"
  2423     using f continuous_on_subset segments_subset_convex_hull by metis+
  2424   show ?thesis
  2425   proof (cases "k \<le> 1")
  2426     case True show ?thesis
  2427       by (simp add: contour_integral_split [OF fabc(1) k True c] contour_integral_reverse_linepath fabc)
  2428   next
  2429     case False then show ?thesis
  2430       using fabc c
  2431       apply (subst contour_integral_split [of a c f "1/k" b, symmetric])
  2432       apply (metis closed_segment_commute fabc(3))
  2433       apply (auto simp: k contour_integral_reverse_linepath)
  2434       done
  2435   qed
  2436 qed
  2437 
  2438 lemma Cauchy_theorem_flat:
  2439   assumes f: "continuous_on (convex hull {a,b,c}) f"
  2440       and c: "c - a = k *\<^sub>R (b - a)"
  2441     shows "contour_integral (linepath a b) f +
  2442            contour_integral (linepath b c) f +
  2443            contour_integral (linepath c a) f = 0"
  2444 proof (cases "0 \<le> k")
  2445   case True with assms show ?thesis
  2446     by (blast intro: Cauchy_theorem_flat_lemma)
  2447 next
  2448   case False
  2449   have "continuous_on (closed_segment a b) f" "continuous_on (closed_segment b c) f" "continuous_on (closed_segment c a) f"
  2450     using f continuous_on_subset segments_subset_convex_hull by metis+
  2451   moreover have "contour_integral (linepath b a) f + contour_integral (linepath a c) f +
  2452         contour_integral (linepath c b) f = 0"
  2453     apply (rule Cauchy_theorem_flat_lemma [of b a c f "1-k"])
  2454     using False c
  2455     apply (auto simp: f insert_commute scaleR_conv_of_real algebra_simps)
  2456     done
  2457   ultimately show ?thesis
  2458     apply (auto simp: contour_integral_reverse_linepath)
  2459     using add_eq_0_iff by force
  2460 qed
  2461 
  2462 lemma Cauchy_theorem_triangle_interior:
  2463   assumes contf: "continuous_on (convex hull {a,b,c}) f"
  2464       and holf:  "f holomorphic_on interior (convex hull {a,b,c})"
  2465      shows "(f has_contour_integral 0) (linepath a b +++ linepath b c +++ linepath c a)"
  2466 proof -
  2467   have fabc: "continuous_on (closed_segment a b) f" "continuous_on (closed_segment b c) f" "continuous_on (closed_segment c a) f"
  2468     using contf continuous_on_subset segments_subset_convex_hull by metis+
  2469   have "bounded (f ` (convex hull {a,b,c}))"
  2470     by (simp add: compact_continuous_image compact_convex_hull compact_imp_bounded contf)
  2471   then obtain B where "0 < B" and Bnf: "\<And>x. x \<in> convex hull {a,b,c} \<Longrightarrow> norm (f x) \<le> B"
  2472      by (auto simp: dest!: bounded_pos [THEN iffD1])
  2473   have "bounded (convex hull {a,b,c})"
  2474     by (simp add: bounded_convex_hull)
  2475   then obtain C where C: "0 < C" and Cno: "\<And>y. y \<in> convex hull {a,b,c} \<Longrightarrow> norm y < C"
  2476     using bounded_pos_less by blast
  2477   then have diff_2C: "norm(x - y) \<le> 2*C"
  2478            if x: "x \<in> convex hull {a, b, c}" and y: "y \<in> convex hull {a, b, c}" for x y
  2479   proof -
  2480     have "cmod x \<le> C"
  2481       using x by (meson Cno not_le not_less_iff_gr_or_eq)
  2482     hence "cmod (x - y) \<le> C + C"
  2483       using y by (meson Cno add_mono_thms_linordered_field(4) less_eq_real_def norm_triangle_ineq4 order_trans)
  2484     thus "cmod (x - y) \<le> 2 * C"
  2485       by (metis mult_2)
  2486   qed
  2487   have contf': "continuous_on (convex hull {b,a,c}) f"
  2488     using contf by (simp add: insert_commute)
  2489   { fix y::complex
  2490     assume fy: "(f has_contour_integral y) (linepath a b +++ linepath b c +++ linepath c a)"
  2491        and ynz: "y \<noteq> 0"
  2492     have pi_eq_y: "contour_integral (linepath a b) f + contour_integral (linepath b c) f + contour_integral (linepath c a) f = y"
  2493       by (rule has_chain_integral_chain_integral3 [OF fy])
  2494     have ?thesis
  2495     proof (cases "c=a \<or> a=b \<or> b=c")
  2496       case True then show ?thesis
  2497         using Cauchy_theorem_flat [OF contf, of 0]
  2498         using has_chain_integral_chain_integral3 [OF fy] ynz
  2499         by (force simp: fabc contour_integral_reverse_linepath)
  2500     next
  2501       case False
  2502       then have car3: "card {a, b, c} = Suc (DIM(complex))"
  2503         by auto
  2504       { assume "interior(convex hull {a,b,c}) = {}"
  2505         then have "collinear{a,b,c}"
  2506           using interior_convex_hull_eq_empty [OF car3]
  2507           by (simp add: collinear_3_eq_affine_dependent)
  2508         with False obtain d where "c \<noteq> a" "a \<noteq> b" "b \<noteq> c" "c - b = d *\<^sub>R (a - b)"
  2509           by (auto simp: collinear_3 collinear_lemma)
  2510         then have "False"
  2511           using False Cauchy_theorem_flat [OF contf'] pi_eq_y ynz
  2512           by (simp add: fabc add_eq_0_iff contour_integral_reverse_linepath)
  2513       }
  2514       then obtain d where d: "d \<in> interior (convex hull {a, b, c})"
  2515         by blast
  2516       { fix d1
  2517         assume d1_pos: "0 < d1"
  2518            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>
  2519                            \<Longrightarrow> cmod (f x' - f x) < cmod y / (24 * C)"
  2520         define e where "e = min 1 (min (d1/(4*C)) ((norm y / 24 / C) / B))"
  2521         define shrink where "shrink x = x - e *\<^sub>R (x - d)" for x
  2522         let ?pathint = "\<lambda>x y. contour_integral(linepath x y) f"
  2523         have e: "0 < e" "e \<le> 1" "e \<le> d1 / (4 * C)" "e \<le> cmod y / 24 / C / B"
  2524           using d1_pos \<open>C>0\<close> \<open>B>0\<close> ynz by (simp_all add: e_def)
  2525         then have eCB: "24 * e * C * B \<le> cmod y"
  2526           using \<open>C>0\<close> \<open>B>0\<close>  by (simp add: field_simps)
  2527         have e_le_d1: "e * (4 * C) \<le> d1"
  2528           using e \<open>C>0\<close> by (simp add: field_simps)
  2529         have "shrink a \<in> interior(convex hull {a,b,c})"
  2530              "shrink b \<in> interior(convex hull {a,b,c})"
  2531              "shrink c \<in> interior(convex hull {a,b,c})"
  2532           using d e by (auto simp: hull_inc mem_interior_convex_shrink shrink_def)
  2533         then have fhp0: "(f has_contour_integral 0)
  2534                 (linepath (shrink a) (shrink b) +++ linepath (shrink b) (shrink c) +++ linepath (shrink c) (shrink a))"
  2535           by (simp add: Cauchy_theorem_triangle holomorphic_on_subset [OF holf] hull_minimal)
  2536         then have f_0_shrink: "?pathint (shrink a) (shrink b) + ?pathint (shrink b) (shrink c) + ?pathint (shrink c) (shrink a) = 0"
  2537           by (simp add: has_chain_integral_chain_integral3)
  2538         have fpi_abc: "f contour_integrable_on linepath (shrink a) (shrink b)"
  2539                       "f contour_integrable_on linepath (shrink b) (shrink c)"
  2540                       "f contour_integrable_on linepath (shrink c) (shrink a)"
  2541           using fhp0  by (auto simp: valid_path_join dest: has_contour_integral_integrable)
  2542         have cmod_shr: "\<And>x y. cmod (shrink y - shrink x - (y - x)) = e * cmod (x - y)"
  2543           using e by (simp add: shrink_def real_vector.scale_right_diff_distrib [symmetric])
  2544         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)"
  2545           by (simp add: algebra_simps)
  2546         have "cmod y / (24 * C) \<le> cmod y / cmod (b - a) / 12"
  2547           using False \<open>C>0\<close> diff_2C [of b a] ynz
  2548           by (auto simp: divide_simps hull_inc)
  2549         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
  2550           apply (cases "x=0", simp add: \<open>0<C\<close>)
  2551           using Cno [of u] mult_left_le_one_le [of "cmod u" x] le_less_trans norm_ge_zero by blast
  2552         { fix u v
  2553           assume uv: "u \<in> convex hull {a, b, c}" "v \<in> convex hull {a, b, c}" "u\<noteq>v"
  2554              and fpi_uv: "f contour_integrable_on linepath (shrink u) (shrink v)"
  2555           have shr_uv: "shrink u \<in> interior(convex hull {a,b,c})"
  2556                        "shrink v \<in> interior(convex hull {a,b,c})"
  2557             using d e uv
  2558             by (auto simp: hull_inc mem_interior_convex_shrink shrink_def)
  2559           have cmod_fuv: "\<And>x. 0\<le>x \<Longrightarrow> x\<le>1 \<Longrightarrow> cmod (f (linepath (shrink u) (shrink v) x)) \<le> B"
  2560             using shr_uv by (blast intro: Bnf linepath_in_convex_hull interior_subset [THEN subsetD])
  2561           have By_uv: "B * (12 * (e * cmod (u - v))) \<le> cmod y"
  2562             apply (rule order_trans [OF _ eCB])
  2563             using e \<open>B>0\<close> diff_2C [of u v] uv
  2564             by (auto simp: field_simps)
  2565           { fix x::real   assume x: "0\<le>x" "x\<le>1"
  2566             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)"
  2567               apply (rule add_strict_mono; rule norm_triangle_half_l [of _ 0])
  2568               using uv x d interior_subset
  2569               apply (auto simp: hull_inc intro!: less_C)
  2570               done
  2571             have ll: "linepath (shrink u) (shrink v) x - linepath u v x = -e * ((1 - x) *\<^sub>R (u - d) + x *\<^sub>R (v - d))"
  2572               by (simp add: linepath_def shrink_def algebra_simps scaleR_conv_of_real)
  2573             have cmod_less_dt: "cmod (linepath (shrink u) (shrink v) x - linepath u v x) < d1"
  2574               apply (simp only: ll norm_mult scaleR_diff_right)
  2575               using \<open>e>0\<close> cmod_less_4C apply (force intro: norm_triangle_lt less_le_trans [OF _ e_le_d1])
  2576               done
  2577             have "cmod (f (linepath (shrink u) (shrink v) x) - f (linepath u v x)) < cmod y / (24 * C)"
  2578               using x uv shr_uv cmod_less_dt
  2579               by (auto simp: hull_inc intro: d1 interior_subset [THEN subsetD] linepath_in_convex_hull)
  2580             also have "\<dots> \<le> cmod y / cmod (v - u) / 12"
  2581               using False uv \<open>C>0\<close> diff_2C [of v u] ynz
  2582               by (auto simp: divide_simps hull_inc)
  2583             finally have "cmod (f (linepath (shrink u) (shrink v) x) - f (linepath u v x)) \<le> cmod y / cmod (v - u) / 12"
  2584               by simp
  2585             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"
  2586               using uv False by (auto simp: field_simps)
  2587             have "cmod (f (linepath (shrink u) (shrink v) x)) * cmod (shrink v - shrink u - (v - u)) +
  2588                           cmod (v - u) * cmod (f (linepath (shrink u) (shrink v) x) - f (linepath u v x))
  2589                           \<le> B * (cmod y / 24 / C / B * 2 * C) + 2 * C * (cmod y / 24 / C)"
  2590               apply (rule add_mono [OF mult_mono])
  2591               using By_uv e \<open>0 < B\<close> \<open>0 < C\<close> x apply (simp_all add: cmod_fuv cmod_shr cmod_12_le)
  2592               apply (simp add: field_simps)
  2593               done
  2594             also have "\<dots> \<le> cmod y / 6"
  2595               by simp
  2596             finally have "cmod (f (linepath (shrink u) (shrink v) x)) * cmod (shrink v - shrink u - (v - u)) +
  2597                           cmod (v - u) * cmod (f (linepath (shrink u) (shrink v) x) - f (linepath u v x))
  2598                           \<le> cmod y / 6" .
  2599           } note cmod_diff_le = this
  2600           have f_uv: "continuous_on (closed_segment u v) f"
  2601             by (blast intro: uv continuous_on_subset [OF contf closed_segment_subset_convex_hull])
  2602           have **: "\<And>f' x' f x::complex. f'*x' - f*x = f'*(x' - x) + x*(f' - f)"
  2603             by (simp add: algebra_simps)
  2604           have "norm (?pathint (shrink u) (shrink v) - ?pathint u v)
  2605                 \<le> (B*(norm y /24/C/B)*2*C + (2*C)*(norm y/24/C)) * content (cbox 0 (1::real))"
  2606             apply (rule has_integral_bound
  2607                     [of _ "\<lambda>x. f(linepath (shrink u) (shrink v) x) * (shrink v - shrink u) - f(linepath u v x)*(v - u)"
  2608                         _ 0 1])
  2609             using ynz \<open>0 < B\<close> \<open>0 < C\<close>
  2610               apply (simp_all del: le_divide_eq_numeral1)
  2611             apply (simp add: has_integral_diff has_contour_integral_linepath [symmetric] has_contour_integral_integral
  2612                 fpi_uv f_uv contour_integrable_continuous_linepath)
  2613             apply (auto simp: ** norm_triangle_le norm_mult cmod_diff_le simp del: le_divide_eq_numeral1)
  2614             done
  2615           also have "\<dots> \<le> norm y / 6"
  2616             by simp
  2617           finally have "norm (?pathint (shrink u) (shrink v) - ?pathint u v) \<le> norm y / 6" .
  2618           } note * = this
  2619           have "norm (?pathint (shrink a) (shrink b) - ?pathint a b) \<le> norm y / 6"
  2620             using False fpi_abc by (rule_tac *) (auto simp: hull_inc)
  2621           moreover
  2622           have "norm (?pathint (shrink b) (shrink c) - ?pathint b c) \<le> norm y / 6"
  2623             using False fpi_abc by (rule_tac *) (auto simp: hull_inc)
  2624           moreover
  2625           have "norm (?pathint (shrink c) (shrink a) - ?pathint c a) \<le> norm y / 6"
  2626             using False fpi_abc by (rule_tac *) (auto simp: hull_inc)
  2627           ultimately
  2628           have "norm((?pathint (shrink a) (shrink b) - ?pathint a b) +
  2629                      (?pathint (shrink b) (shrink c) - ?pathint b c) + (?pathint (shrink c) (shrink a) - ?pathint c a))
  2630                 \<le> norm y / 6 + norm y / 6 + norm y / 6"
  2631             by (metis norm_triangle_le add_mono)
  2632           also have "\<dots> = norm y / 2"
  2633             by simp
  2634           finally have "norm((?pathint (shrink a) (shrink b) + ?pathint (shrink b) (shrink c) + ?pathint (shrink c) (shrink a)) -
  2635                           (?pathint a b + ?pathint b c + ?pathint c a))
  2636                 \<le> norm y / 2"
  2637             by (simp add: algebra_simps)
  2638           then
  2639           have "norm(?pathint a b + ?pathint b c + ?pathint c a) \<le> norm y / 2"
  2640             by (simp add: f_0_shrink) (metis (mono_tags) add.commute minus_add_distrib norm_minus_cancel uminus_add_conv_diff)
  2641           then have "False"
  2642             using pi_eq_y ynz by auto
  2643         }
  2644         moreover have "uniformly_continuous_on (convex hull {a,b,c}) f"
  2645           by (simp add: contf compact_convex_hull compact_uniformly_continuous)
  2646         ultimately have "False"
  2647           unfolding uniformly_continuous_on_def
  2648           by (force simp: ynz \<open>0 < C\<close> dist_norm)
  2649         then show ?thesis ..
  2650       qed
  2651   }
  2652   moreover have "f contour_integrable_on (linepath a b +++ linepath b c +++ linepath c a)"
  2653     using fabc contour_integrable_continuous_linepath by auto
  2654   ultimately show ?thesis
  2655     using has_contour_integral_integral by fastforce
  2656 qed
  2657 
  2658 subsection%unimportant \<open>Version allowing finite number of exceptional points\<close>
  2659 
  2660 proposition%unimportant Cauchy_theorem_triangle_cofinite:
  2661   assumes "continuous_on (convex hull {a,b,c}) f"
  2662       and "finite S"
  2663       and "(\<And>x. x \<in> interior(convex hull {a,b,c}) - S \<Longrightarrow> f field_differentiable (at x))"
  2664      shows "(f has_contour_integral 0) (linepath a b +++ linepath b c +++ linepath c a)"
  2665 using assms
  2666 proof (induction "card S" arbitrary: a b c S rule: less_induct)
  2667   case (less S a b c)
  2668   show ?case
  2669   proof (cases "S={}")
  2670     case True with less show ?thesis
  2671       by (fastforce simp: holomorphic_on_def field_differentiable_at_within Cauchy_theorem_triangle_interior)
  2672   next
  2673     case False
  2674     then obtain d S' where d: "S = insert d S'" "d \<notin> S'"
  2675       by (meson Set.set_insert all_not_in_conv)
  2676     then show ?thesis
  2677     proof (cases "d \<in> convex hull {a,b,c}")
  2678       case False
  2679       show "(f has_contour_integral 0) (linepath a b +++ linepath b c +++ linepath c a)"
  2680       proof (rule less.hyps)
  2681         show "\<And>x. x \<in> interior (convex hull {a, b, c}) - S' \<Longrightarrow> f field_differentiable at x"
  2682         using False d interior_subset by (auto intro!: less.prems)
  2683     qed (use d less.prems in auto)
  2684     next
  2685       case True
  2686       have *: "convex hull {a, b, d} \<subseteq> convex hull {a, b, c}"
  2687         by (meson True hull_subset insert_subset convex_hull_subset)
  2688       have abd: "(f has_contour_integral 0) (linepath a b +++ linepath b d +++ linepath d a)"
  2689       proof (rule less.hyps)
  2690         show "\<And>x. x \<in> interior (convex hull {a, b, d}) - S' \<Longrightarrow> f field_differentiable at x"
  2691           using d not_in_interior_convex_hull_3
  2692           by (clarsimp intro!: less.prems) (metis * insert_absorb insert_subset interior_mono)
  2693       qed (use d continuous_on_subset [OF  _ *] less.prems in auto)
  2694       have *: "convex hull {b, c, d} \<subseteq> convex hull {a, b, c}"
  2695         by (meson True hull_subset insert_subset convex_hull_subset)
  2696       have bcd: "(f has_contour_integral 0) (linepath b c +++ linepath c d +++ linepath d b)"
  2697       proof (rule less.hyps)
  2698         show "\<And>x. x \<in> interior (convex hull {b, c, d}) - S' \<Longrightarrow> f field_differentiable at x"
  2699           using d not_in_interior_convex_hull_3
  2700           by (clarsimp intro!: less.prems) (metis * insert_absorb insert_subset interior_mono)
  2701       qed (use d continuous_on_subset [OF  _ *] less.prems in auto)
  2702       have *: "convex hull {c, a, d} \<subseteq> convex hull {a, b, c}"
  2703         by (meson True hull_subset insert_subset convex_hull_subset)
  2704       have cad: "(f has_contour_integral 0) (linepath c a +++ linepath a d +++ linepath d c)"
  2705       proof (rule less.hyps)
  2706         show "\<And>x. x \<in> interior (convex hull {c, a, d}) - S' \<Longrightarrow> f field_differentiable at x"
  2707           using d not_in_interior_convex_hull_3
  2708           by (clarsimp intro!: less.prems) (metis * insert_absorb insert_subset interior_mono)
  2709       qed (use d continuous_on_subset [OF  _ *] less.prems in auto)
  2710       have "f contour_integrable_on linepath a b"
  2711         using less.prems abd contour_integrable_joinD1 contour_integrable_on_def by blast
  2712       moreover have "f contour_integrable_on linepath b c"
  2713         using less.prems bcd contour_integrable_joinD1 contour_integrable_on_def by blast
  2714       moreover have "f contour_integrable_on linepath c a"
  2715         using less.prems cad contour_integrable_joinD1 contour_integrable_on_def by blast
  2716       ultimately have fpi: "f contour_integrable_on (linepath a b +++ linepath b c +++ linepath c a)"
  2717         by auto
  2718       { fix y::complex
  2719         assume fy: "(f has_contour_integral y) (linepath a b +++ linepath b c +++ linepath c a)"
  2720            and ynz: "y \<noteq> 0"
  2721         have cont_ad: "continuous_on (closed_segment a d) f"
  2722           by (meson "*" continuous_on_subset less.prems(1) segments_subset_convex_hull(3))
  2723         have cont_bd: "continuous_on (closed_segment b d) f"
  2724           by (meson True closed_segment_subset_convex_hull continuous_on_subset hull_subset insert_subset less.prems(1))
  2725         have cont_cd: "continuous_on (closed_segment c d) f"
  2726           by (meson "*" continuous_on_subset less.prems(1) segments_subset_convex_hull(2))
  2727         have "contour_integral  (linepath a b) f = - (contour_integral (linepath b d) f + (contour_integral (linepath d a) f))"
  2728              "contour_integral  (linepath b c) f = - (contour_integral (linepath c d) f + (contour_integral (linepath d b) f))"
  2729              "contour_integral  (linepath c a) f = - (contour_integral (linepath a d) f + contour_integral (linepath d c) f)"
  2730             using has_chain_integral_chain_integral3 [OF abd]
  2731                   has_chain_integral_chain_integral3 [OF bcd]
  2732                   has_chain_integral_chain_integral3 [OF cad]
  2733             by (simp_all add: algebra_simps add_eq_0_iff)
  2734         then have ?thesis
  2735           using cont_ad cont_bd cont_cd fy has_chain_integral_chain_integral3 contour_integral_reverse_linepath by fastforce
  2736       }
  2737       then show ?thesis
  2738         using fpi contour_integrable_on_def by blast
  2739     qed
  2740   qed
  2741 qed
  2742 
  2743 subsection%unimportant \<open>Cauchy's theorem for an open starlike set\<close>
  2744 
  2745 lemma starlike_convex_subset:
  2746   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"
  2747     shows "convex hull {a,b,c} \<subseteq> S"
  2748       using S
  2749       apply (clarsimp simp add: convex_hull_insert [of "{b,c}" a] segment_convex_hull)
  2750       apply (meson subs convexD convex_closed_segment ends_in_segment(1) ends_in_segment(2) subsetCE)
  2751       done
  2752 
  2753 lemma triangle_contour_integrals_starlike_primitive:
  2754   assumes contf: "continuous_on S f"
  2755       and S: "a \<in> S" "open S"
  2756       and x: "x \<in> S"
  2757       and subs: "\<And>y. y \<in> S \<Longrightarrow> closed_segment a y \<subseteq> S"
  2758       and zer: "\<And>b c. closed_segment b c \<subseteq> S
  2759                    \<Longrightarrow> contour_integral (linepath a b) f + contour_integral (linepath b c) f +
  2760                        contour_integral (linepath c a) f = 0"
  2761     shows "((\<lambda>x. contour_integral(linepath a x) f) has_field_derivative f x) (at x)"
  2762 proof -
  2763   let ?pathint = "\<lambda>x y. contour_integral(linepath x y) f"
  2764   { fix e y
  2765     assume e: "0 < e" and bxe: "ball x e \<subseteq> S" and close: "cmod (y - x) < e"
  2766     have y: "y \<in> S"
  2767       using bxe close  by (force simp: dist_norm norm_minus_commute)
  2768     have cont_ayf: "continuous_on (closed_segment a y) f"
  2769       using contf continuous_on_subset subs y by blast
  2770     have xys: "closed_segment x y \<subseteq> S"
  2771       apply (rule order_trans [OF _ bxe])
  2772       using close
  2773       by (auto simp: dist_norm ball_def norm_minus_commute dest: segment_bound)
  2774     have "?pathint a y - ?pathint a x = ?pathint x y"
  2775       using zer [OF xys]  contour_integral_reverse_linepath [OF cont_ayf]  add_eq_0_iff by force
  2776   } note [simp] = this
  2777   { fix e::real
  2778     assume e: "0 < e"
  2779     have cont_atx: "continuous (at x) f"
  2780       using x S contf continuous_on_eq_continuous_at by blast
  2781     then obtain d1 where d1: "d1>0" and d1_less: "\<And>y. cmod (y - x) < d1 \<Longrightarrow> cmod (f y - f x) < e/2"
  2782       unfolding continuous_at Lim_at dist_norm  using e
  2783       by (drule_tac x="e/2" in spec) force
  2784     obtain d2 where d2: "d2>0" "ball x d2 \<subseteq> S" using  \<open>open S\<close> x
  2785       by (auto simp: open_contains_ball)
  2786     have dpos: "min d1 d2 > 0" using d1 d2 by simp
  2787     { fix y
  2788       assume yx: "y \<noteq> x" and close: "cmod (y - x) < min d1 d2"
  2789       have y: "y \<in> S"
  2790         using d2 close  by (force simp: dist_norm norm_minus_commute)
  2791       have "closed_segment x y \<subseteq> S"
  2792         using close d2  by (auto simp: dist_norm norm_minus_commute dest!: segment_bound(1))
  2793       then have fxy: "f contour_integrable_on linepath x y"
  2794         by (metis contour_integrable_continuous_linepath continuous_on_subset [OF contf])
  2795       then obtain i where i: "(f has_contour_integral i) (linepath x y)"
  2796         by (auto simp: contour_integrable_on_def)
  2797       then have "((\<lambda>w. f w - f x) has_contour_integral (i - f x * (y - x))) (linepath x y)"
  2798         by (rule has_contour_integral_diff [OF _ has_contour_integral_const_linepath])
  2799       then have "cmod (i - f x * (y - x)) \<le> e / 2 * cmod (y - x)"
  2800       proof (rule has_contour_integral_bound_linepath)
  2801         show "\<And>u. u \<in> closed_segment x y \<Longrightarrow> cmod (f u - f x) \<le> e / 2"
  2802           by (meson close d1_less le_less_trans less_imp_le min.strict_boundedE segment_bound1)
  2803       qed (use e in simp)
  2804       also have "\<dots> < e * cmod (y - x)"
  2805         by (simp add: e yx)
  2806       finally have "cmod (?pathint x y - f x * (y-x)) / cmod (y-x) < e"
  2807         using i yx  by (simp add: contour_integral_unique divide_less_eq)
  2808     }
  2809     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"
  2810       using dpos by blast
  2811   }
  2812   then have *: "(\<lambda>y. (?pathint x y - f x * (y - x)) /\<^sub>R cmod (y - x)) \<midarrow>x\<rightarrow> 0"
  2813     by (simp add: Lim_at dist_norm inverse_eq_divide)
  2814   show ?thesis
  2815     apply (simp add: has_field_derivative_def has_derivative_at2 bounded_linear_mult_right)
  2816     apply (rule Lim_transform [OF * Lim_eventually])
  2817     using \<open>open S\<close> x apply (force simp: dist_norm open_contains_ball inverse_eq_divide [symmetric] eventually_at)
  2818     done
  2819 qed
  2820 
  2821 (** Existence of a primitive.*)
  2822 lemma holomorphic_starlike_primitive:
  2823   fixes f :: "complex \<Rightarrow> complex"
  2824   assumes contf: "continuous_on S f"
  2825       and S: "starlike S" and os: "open S"
  2826       and k: "finite k"
  2827       and fcd: "\<And>x. x \<in> S - k \<Longrightarrow> f field_differentiable at x"
  2828     shows "\<exists>g. \<forall>x \<in> S. (g has_field_derivative f x) (at x)"
  2829 proof -
  2830   obtain a where a: "a\<in>S" and a_cs: "\<And>x. x\<in>S \<Longrightarrow> closed_segment a x \<subseteq> S"
  2831     using S by (auto simp: starlike_def)
  2832   { fix x b c
  2833     assume "x \<in> S" "closed_segment b c \<subseteq> S"
  2834     then have abcs: "convex hull {a, b, c} \<subseteq> S"
  2835       by (simp add: a a_cs starlike_convex_subset)
  2836     then have "continuous_on (convex hull {a, b, c}) f"
  2837       by (simp add: continuous_on_subset [OF contf])
  2838     then have "(f has_contour_integral 0) (linepath a b +++ linepath b c +++ linepath c a)"
  2839       using abcs interior_subset by (force intro: fcd Cauchy_theorem_triangle_cofinite [OF _ k])
  2840   } note 0 = this
  2841   show ?thesis
  2842     apply (intro exI ballI)
  2843     apply (rule triangle_contour_integrals_starlike_primitive [OF contf a os], assumption)
  2844     apply (metis a_cs)
  2845     apply (metis has_chain_integral_chain_integral3 0)
  2846     done
  2847 qed
  2848 
  2849 lemma Cauchy_theorem_starlike:
  2850  "\<lbrakk>open S; starlike S; finite k; continuous_on S f;
  2851    \<And>x. x \<in> S - k \<Longrightarrow> f field_differentiable at x;
  2852    valid_path g; path_image g \<subseteq> S; pathfinish g = pathstart g\<rbrakk>
  2853    \<Longrightarrow> (f has_contour_integral 0)  g"
  2854   by (metis holomorphic_starlike_primitive Cauchy_theorem_primitive at_within_open)
  2855 
  2856 lemma Cauchy_theorem_starlike_simple:
  2857   "\<lbrakk>open S; starlike S; f holomorphic_on S; valid_path g; path_image g \<subseteq> S; pathfinish g = pathstart g\<rbrakk>
  2858    \<Longrightarrow> (f has_contour_integral 0) g"
  2859 apply (rule Cauchy_theorem_starlike [OF _ _ finite.emptyI])
  2860 apply (simp_all add: holomorphic_on_imp_continuous_on)
  2861 apply (metis at_within_open holomorphic_on_def)
  2862 done
  2863 
  2864 subsection\<open>Cauchy's theorem for a convex set\<close>
  2865 
  2866 text\<open>For a convex set we can avoid assuming openness and boundary analyticity\<close>
  2867 
  2868 lemma triangle_contour_integrals_convex_primitive:
  2869   assumes contf: "continuous_on S f"
  2870       and S: "a \<in> S" "convex S"
  2871       and x: "x \<in> S"
  2872       and zer: "\<And>b c. \<lbrakk>b \<in> S; c \<in> S\<rbrakk>
  2873                    \<Longrightarrow> contour_integral (linepath a b) f + contour_integral (linepath b c) f +
  2874                        contour_integral (linepath c a) f = 0"
  2875     shows "((\<lambda>x. contour_integral(linepath a x) f) has_field_derivative f x) (at x within S)"
  2876 proof -
  2877   let ?pathint = "\<lambda>x y. contour_integral(linepath x y) f"
  2878   { fix y
  2879     assume y: "y \<in> S"
  2880     have cont_ayf: "continuous_on (closed_segment a y) f"
  2881       using S y  by (meson contf continuous_on_subset convex_contains_segment)
  2882     have xys: "closed_segment x y \<subseteq> S"  (*?*)
  2883       using convex_contains_segment S x y by auto
  2884     have "?pathint a y - ?pathint a x = ?pathint x y"
  2885       using zer [OF x y]  contour_integral_reverse_linepath [OF cont_ayf]  add_eq_0_iff by force
  2886   } note [simp] = this
  2887   { fix e::real
  2888     assume e: "0 < e"
  2889     have cont_atx: "continuous (at x within S) f"
  2890       using x S contf  by (simp add: continuous_on_eq_continuous_within)
  2891     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"
  2892       unfolding continuous_within Lim_within dist_norm using e
  2893       by (drule_tac x="e/2" in spec) force
  2894     { fix y
  2895       assume yx: "y \<noteq> x" and close: "cmod (y - x) < d1" and y: "y \<in> S"
  2896       have fxy: "f contour_integrable_on linepath x y"
  2897         using convex_contains_segment S x y
  2898         by (blast intro!: contour_integrable_continuous_linepath continuous_on_subset [OF contf])
  2899       then obtain i where i: "(f has_contour_integral i) (linepath x y)"
  2900         by (auto simp: contour_integrable_on_def)
  2901       then have "((\<lambda>w. f w - f x) has_contour_integral (i - f x * (y - x))) (linepath x y)"
  2902         by (rule has_contour_integral_diff [OF _ has_contour_integral_const_linepath])
  2903       then have "cmod (i - f x * (y - x)) \<le> e / 2 * cmod (y - x)"
  2904       proof (rule has_contour_integral_bound_linepath)
  2905         show "\<And>u. u \<in> closed_segment x y \<Longrightarrow> cmod (f u - f x) \<le> e / 2"
  2906           by (meson assms(3) close convex_contains_segment d1_less le_less_trans less_imp_le segment_bound1 subset_iff x y)
  2907       qed (use e in simp)
  2908       also have "\<dots> < e * cmod (y - x)"
  2909         by (simp add: e yx)
  2910       finally have "cmod (?pathint x y - f x * (y-x)) / cmod (y-x) < e"
  2911         using i yx  by (simp add: contour_integral_unique divide_less_eq)
  2912     }
  2913     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"
  2914       using d1 by blast
  2915   }
  2916   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)"
  2917     by (simp add: Lim_within dist_norm inverse_eq_divide)
  2918   show ?thesis
  2919     apply (simp add: has_field_derivative_def has_derivative_within bounded_linear_mult_right)
  2920     apply (rule Lim_transform [OF * Lim_eventually])
  2921     using linordered_field_no_ub
  2922     apply (force simp: inverse_eq_divide [symmetric] eventually_at)
  2923     done
  2924 qed
  2925 
  2926 lemma contour_integral_convex_primitive:
  2927   assumes "convex S" "continuous_on S f"
  2928           "\<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)"
  2929   obtains g where "\<And>x. x \<in> S \<Longrightarrow> (g has_field_derivative f x) (at x within S)"
  2930 proof (cases "S={}")
  2931   case False
  2932   with assms that show ?thesis
  2933     by (blast intro: triangle_contour_integrals_convex_primitive has_chain_integral_chain_integral3)
  2934 qed auto
  2935 
  2936 lemma holomorphic_convex_primitive:
  2937   fixes f :: "complex \<Rightarrow> complex"
  2938   assumes "convex S" "finite K" and contf: "continuous_on S f"
  2939     and fd: "\<And>x. x \<in> interior S - K \<Longrightarrow> f field_differentiable at x"
  2940   obtains g where "\<And>x. x \<in> S \<Longrightarrow> (g has_field_derivative f x) (at x within S)"
  2941 proof (rule contour_integral_convex_primitive [OF \<open>convex S\<close> contf Cauchy_theorem_triangle_cofinite])
  2942   have *: "convex hull {a, b, c} \<subseteq> S" if "a \<in> S" "b \<in> S" "c \<in> S" for a b c
  2943     by (simp add: \<open>convex S\<close> hull_minimal that)
  2944   show "continuous_on (convex hull {a, b, c}) f" if "a \<in> S" "b \<in> S" "c \<in> S" for a b c
  2945     by (meson "*" contf continuous_on_subset that)
  2946   show "f field_differentiable at x" if "a \<in> S" "b \<in> S" "c \<in> S" "x \<in> interior (convex hull {a, b, c}) - K" for a b c x
  2947     by (metis "*" DiffD1 DiffD2 DiffI fd interior_mono subsetCE that)
  2948 qed (use assms in \<open>force+\<close>)
  2949 
  2950 lemma holomorphic_convex_primitive':
  2951   fixes f :: "complex \<Rightarrow> complex"
  2952   assumes "convex S" and "open S" and "f holomorphic_on S"
  2953   obtains g where "\<And>x. x \<in> S \<Longrightarrow> (g has_field_derivative f x) (at x within S)"
  2954 proof (rule holomorphic_convex_primitive)
  2955   fix x assume "x \<in> interior S - {}"
  2956   with assms show "f field_differentiable at x"
  2957     by (auto intro!: holomorphic_on_imp_differentiable_at simp: interior_open)
  2958 qed (use assms in \<open>auto intro: holomorphic_on_imp_continuous_on\<close>)
  2959 
  2960 corollary%unimportant Cauchy_theorem_convex:
  2961     "\<lbrakk>continuous_on S f; convex S; finite K;
  2962       \<And>x. x \<in> interior S - K \<Longrightarrow> f field_differentiable at x;
  2963       valid_path g; path_image g \<subseteq> S; pathfinish g = pathstart g\<rbrakk>
  2964      \<Longrightarrow> (f has_contour_integral 0) g"
  2965   by (metis holomorphic_convex_primitive Cauchy_theorem_primitive)
  2966 
  2967 corollary Cauchy_theorem_convex_simple:
  2968     "\<lbrakk>f holomorphic_on S; convex S;
  2969      valid_path g; path_image g \<subseteq> S;
  2970      pathfinish g = pathstart g\<rbrakk> \<Longrightarrow> (f has_contour_integral 0) g"
  2971   apply (rule Cauchy_theorem_convex [where K = "{}"])
  2972   apply (simp_all add: holomorphic_on_imp_continuous_on)
  2973   using at_within_interior holomorphic_on_def interior_subset by fastforce
  2974 
  2975 text\<open>In particular for a disc\<close>
  2976 corollary%unimportant Cauchy_theorem_disc:
  2977     "\<lbrakk>finite K; continuous_on (cball a e) f;
  2978       \<And>x. x \<in> ball a e - K \<Longrightarrow> f field_differentiable at x;
  2979      valid_path g; path_image g \<subseteq> cball a e;
  2980      pathfinish g = pathstart g\<rbrakk> \<Longrightarrow> (f has_contour_integral 0) g"
  2981   by (auto intro: Cauchy_theorem_convex)
  2982 
  2983 corollary%unimportant Cauchy_theorem_disc_simple:
  2984     "\<lbrakk>f holomorphic_on (ball a e); valid_path g; path_image g \<subseteq> ball a e;
  2985      pathfinish g = pathstart g\<rbrakk> \<Longrightarrow> (f has_contour_integral 0) g"
  2986 by (simp add: Cauchy_theorem_convex_simple)
  2987 
  2988 subsection%unimportant \<open>Generalize integrability to local primitives\<close>
  2989 
  2990 lemma contour_integral_local_primitive_lemma:
  2991   fixes f :: "complex\<Rightarrow>complex"
  2992   shows
  2993     "\<lbrakk>g piecewise_differentiable_on {a..b};
  2994       \<And>x. x \<in> s \<Longrightarrow> (f has_field_derivative f' x) (at x within s);
  2995       \<And>x. x \<in> {a..b} \<Longrightarrow> g x \<in> s\<rbrakk>
  2996      \<Longrightarrow> (\<lambda>x. f' (g x) * vector_derivative g (at x within {a..b}))
  2997             integrable_on {a..b}"
  2998   apply (cases "cbox a b = {}", force)
  2999   apply (simp add: integrable_on_def)
  3000   apply (rule exI)
  3001   apply (rule contour_integral_primitive_lemma, assumption+)
  3002   using atLeastAtMost_iff by blast
  3003 
  3004 lemma contour_integral_local_primitive_any:
  3005   fixes f :: "complex \<Rightarrow> complex"
  3006   assumes gpd: "g piecewise_differentiable_on {a..b}"
  3007       and dh: "\<And>x. x \<in> s
  3008                \<Longrightarrow> \<exists>d h. 0 < d \<and>
  3009                          (\<forall>y. norm(y - x) < d \<longrightarrow> (h has_field_derivative f y) (at y within s))"
  3010       and gs: "\<And>x. x \<in> {a..b} \<Longrightarrow> g x \<in> s"
  3011   shows "(\<lambda>x. f(g x) * vector_derivative g (at x)) integrable_on {a..b}"
  3012 proof -
  3013   { fix x
  3014     assume x: "a \<le> x" "x \<le> b"
  3015     obtain d h where d: "0 < d"
  3016                and h: "(\<And>y. norm(y - g x) < d \<Longrightarrow> (h has_field_derivative f y) (at y within s))"
  3017       using x gs dh by (metis atLeastAtMost_iff)
  3018     have "continuous_on {a..b} g" using gpd piecewise_differentiable_on_def by blast
  3019     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"
  3020       using x d
  3021       apply (auto simp: dist_norm continuous_on_iff)
  3022       apply (drule_tac x=x in bspec)
  3023       using x apply simp
  3024       apply (drule_tac x=d in spec, auto)
  3025       done
  3026     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>
  3027                           (\<lambda>x. f (g x) * vector_derivative g (at x)) integrable_on {u..v}"
  3028       apply (rule_tac x=e in exI)
  3029       using e
  3030       apply (simp add: integrable_on_localized_vector_derivative [symmetric], clarify)
  3031       apply (rule_tac f = h and s = "g ` {u..v}" in contour_integral_local_primitive_lemma)
  3032         apply (meson atLeastatMost_subset_iff gpd piecewise_differentiable_on_subset)
  3033        apply (force simp: ball_def dist_norm intro: lessd gs DERIV_subset [OF h], force)
  3034       done
  3035   } then
  3036   show ?thesis
  3037     by (force simp: intro!: integrable_on_little_subintervals [of a b, simplified])
  3038 qed
  3039 
  3040 lemma contour_integral_local_primitive:
  3041   fixes f :: "complex \<Rightarrow> complex"
  3042   assumes g: "valid_path g" "path_image g \<subseteq> s"
  3043       and dh: "\<And>x. x \<in> s
  3044                \<Longrightarrow> \<exists>d h. 0 < d \<and>
  3045                          (\<forall>y. norm(y - x) < d \<longrightarrow> (h has_field_derivative f y) (at y within s))"
  3046   shows "f contour_integrable_on g"
  3047   using g
  3048   apply (simp add: valid_path_def path_image_def contour_integrable_on_def has_contour_integral_def
  3049             has_integral_localized_vector_derivative integrable_on_def [symmetric])
  3050   using contour_integral_local_primitive_any [OF _ dh]
  3051   by (meson image_subset_iff piecewise_C1_imp_differentiable)
  3052 
  3053 
  3054 text\<open>In particular if a function is holomorphic\<close>
  3055 
  3056 lemma contour_integrable_holomorphic:
  3057   assumes contf: "continuous_on s f"
  3058       and os: "open s"
  3059       and k: "finite k"
  3060       and g: "valid_path g" "path_image g \<subseteq> s"
  3061       and fcd: "\<And>x. x \<in> s - k \<Longrightarrow> f field_differentiable at x"
  3062     shows "f contour_integrable_on g"
  3063 proof -
  3064   { fix z
  3065     assume z: "z \<in> s"
  3066     obtain d where "d>0" and d: "ball z d \<subseteq> s" using  \<open>open s\<close> z
  3067       by (auto simp: open_contains_ball)
  3068     then have contfb: "continuous_on (ball z d) f"
  3069       using contf continuous_on_subset by blast
  3070     obtain h where "\<forall>y\<in>ball z d. (h has_field_derivative f y) (at y within ball z d)"
  3071       by (metis holomorphic_convex_primitive [OF convex_ball k contfb fcd] d interior_subset Diff_iff subsetD)
  3072     then have "\<forall>y\<in>ball z d. (h has_field_derivative f y) (at y within s)"
  3073       by (metis open_ball at_within_open d os subsetCE)
  3074     then have "\<exists>h. (\<forall>y. cmod (y - z) < d \<longrightarrow> (h has_field_derivative f y) (at y within s))"
  3075       by (force simp: dist_norm norm_minus_commute)
  3076     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))"
  3077       using \<open>0 < d\<close> by blast
  3078   }
  3079   then show ?thesis
  3080     by (rule contour_integral_local_primitive [OF g])
  3081 qed
  3082 
  3083 lemma contour_integrable_holomorphic_simple:
  3084   assumes fh: "f holomorphic_on S"
  3085       and os: "open S"
  3086       and g: "valid_path g" "path_image g \<subseteq> S"
  3087     shows "f contour_integrable_on g"
  3088   apply (rule contour_integrable_holomorphic [OF _ os Finite_Set.finite.emptyI g])
  3089   apply (simp add: fh holomorphic_on_imp_continuous_on)
  3090   using fh  by (simp add: field_differentiable_def holomorphic_on_open os)
  3091 
  3092 lemma continuous_on_inversediff:
  3093   fixes z:: "'a::real_normed_field" shows "z \<notin> S \<Longrightarrow> continuous_on S (\<lambda>w. 1 / (w - z))"
  3094   by (rule continuous_intros | force)+
  3095 
  3096 lemma contour_integrable_inversediff:
  3097     "\<lbrakk>valid_path g; z \<notin> path_image g\<rbrakk> \<Longrightarrow> (\<lambda>w. 1 / (w-z)) contour_integrable_on g"
  3098 apply (rule contour_integrable_holomorphic_simple [of _ "UNIV-{z}"])
  3099 apply (auto simp: holomorphic_on_open open_delete intro!: derivative_eq_intros)
  3100 done
  3101 
  3102 text\<open>Key fact that path integral is the same for a "nearby" path. This is the
  3103  main lemma for the homotopy form of Cauchy's theorem and is also useful
  3104  if we want "without loss of generality" to assume some nice properties of a
  3105  path (e.g. smoothness). It can also be used to define the integrals of
  3106  analytic functions over arbitrary continuous paths. This is just done for
  3107  winding numbers now.
  3108 \<close>
  3109 
  3110 text\<open>A technical definition to avoid duplication of similar proofs,
  3111      for paths joined at the ends versus looping paths\<close>
  3112 definition linked_paths :: "bool \<Rightarrow> (real \<Rightarrow> 'a) \<Rightarrow> (real \<Rightarrow> 'a::topological_space) \<Rightarrow> bool"
  3113   where "linked_paths atends g h ==
  3114         (if atends then pathstart h = pathstart g \<and> pathfinish h = pathfinish g
  3115                    else pathfinish g = pathstart g \<and> pathfinish h = pathstart h)"
  3116 
  3117 text\<open>This formulation covers two cases: \<^term>\<open>g\<close> and \<^term>\<open>h\<close> share their
  3118       start and end points; \<^term>\<open>g\<close> and \<^term>\<open>h\<close> both loop upon themselves.\<close>
  3119 lemma contour_integral_nearby:
  3120   assumes os: "open S" and p: "path p" "path_image p \<subseteq> S"
  3121   shows "\<exists>d. 0 < d \<and>
  3122             (\<forall>g h. valid_path g \<and> valid_path h \<and>
  3123                   (\<forall>t \<in> {0..1}. norm(g t - p t) < d \<and> norm(h t - p t) < d) \<and>
  3124                   linked_paths atends g h
  3125                   \<longrightarrow> path_image g \<subseteq> S \<and> path_image h \<subseteq> S \<and>
  3126                       (\<forall>f. f holomorphic_on S \<longrightarrow> contour_integral h f = contour_integral g f))"
  3127 proof -
  3128   have "\<forall>z. \<exists>e. z \<in> path_image p \<longrightarrow> 0 < e \<and> ball z e \<subseteq> S"
  3129     using open_contains_ball os p(2) by blast
  3130   then obtain ee where ee: "\<And>z. z \<in> path_image p \<Longrightarrow> 0 < ee z \<and> ball z (ee z) \<subseteq> S"
  3131     by metis
  3132   define cover where "cover = (\<lambda>z. ball z (ee z/3)) ` (path_image p)"
  3133   have "compact (path_image p)"
  3134     by (metis p(1) compact_path_image)
  3135   moreover have "path_image p \<subseteq> (\<Union>c\<in>path_image p. ball c (ee c / 3))"
  3136     using ee by auto
  3137   ultimately have "\<exists>D \<subseteq> cover. finite D \<and> path_image p \<subseteq> \<Union>D"
  3138     by (simp add: compact_eq_Heine_Borel cover_def)
  3139   then obtain D where D: "D \<subseteq> cover" "finite D" "path_image p \<subseteq> \<Union>D"
  3140     by blast
  3141   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"
  3142     apply (simp add: cover_def path_image_def image_comp)
  3143     apply (blast dest!: finite_subset_image [OF \<open>finite D\<close>])
  3144     done
  3145   then have kne: "k \<noteq> {}"
  3146     using D by auto
  3147   have pi: "\<And>i. i \<in> k \<Longrightarrow> p i \<in> path_image p"
  3148     using k  by (auto simp: path_image_def)
  3149   then have eepi: "\<And>i. i \<in> k \<Longrightarrow> 0 < ee((p i))"
  3150     by (metis ee)
  3151   define e where "e = Min((ee \<circ> p) ` k)"
  3152   have fin_eep: "finite ((ee \<circ> p) ` k)"
  3153     using k  by blast
  3154   have "0 < e"
  3155     using ee k  by (simp add: kne e_def Min_gr_iff [OF fin_eep] eepi)
  3156   have "uniformly_continuous_on {0..1} p"
  3157     using p  by (simp add: path_def compact_uniformly_continuous)
  3158   then obtain d::real where d: "d>0"
  3159           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"
  3160     unfolding uniformly_continuous_on_def dist_norm real_norm_def
  3161     by (metis divide_pos_pos \<open>0 < e\<close> zero_less_numeral)
  3162   then obtain N::nat where N: "N>0" "inverse N < d"
  3163     using real_arch_inverse [of d]   by auto
  3164   show ?thesis
  3165   proof (intro exI conjI allI; clarify?)
  3166     show "e/3 > 0"
  3167       using \<open>0 < e\<close> by simp
  3168     fix g h
  3169     assume g: "valid_path g" and ghp: "\<forall>t\<in>{0..1}. cmod (g t - p t) < e / 3 \<and>  cmod (h t - p t) < e / 3"
  3170        and h: "valid_path h"
  3171        and joins: "linked_paths atends g h"
  3172     { fix t::real
  3173       assume t: "0 \<le> t" "t \<le> 1"
  3174       then obtain u where u: "u \<in> k" and ptu: "p t \<in> ball(p u) (ee(p u) / 3)"
  3175         using \<open>path_image p \<subseteq> \<Union>D\<close> D_eq by (force simp: path_image_def)
  3176       then have ele: "e \<le> ee (p u)" using fin_eep
  3177         by (simp add: e_def)
  3178       have "cmod (g t - p t) < e / 3" "cmod (h t - p t) < e / 3"
  3179         using ghp t by auto
  3180       with ele have "cmod (g t - p t) < ee (p u) / 3"
  3181                     "cmod (h t - p t) < ee (p u) / 3"
  3182         by linarith+
  3183       then have "g t \<in> ball(p u) (ee(p u))"  "h t \<in> ball(p u) (ee(p u))"
  3184         using norm_diff_triangle_ineq [of "g t" "p t" "p t" "p u"]
  3185               norm_diff_triangle_ineq [of "h t" "p t" "p t" "p u"] ptu eepi u
  3186         by (force simp: dist_norm ball_def norm_minus_commute)+
  3187       then have "g t \<in> S" "h t \<in> S" using ee u k
  3188         by (auto simp: path_image_def ball_def)
  3189     }
  3190     then have ghs: "path_image g \<subseteq> S" "path_image h \<subseteq> S"
  3191       by (auto simp: path_image_def)
  3192     moreover
  3193     { fix f
  3194       assume fhols: "f holomorphic_on S"
  3195       then have fpa: "f contour_integrable_on g"  "f contour_integrable_on h"
  3196         using g ghs h holomorphic_on_imp_continuous_on os contour_integrable_holomorphic_simple
  3197         by blast+
  3198       have contf: "continuous_on S f"
  3199         by (simp add: fhols holomorphic_on_imp_continuous_on)
  3200       { fix z
  3201         assume z: "z \<in> path_image p"
  3202         have "f holomorphic_on ball z (ee z)"
  3203           using fhols ee z holomorphic_on_subset by blast
  3204         then have "\<exists>ff. (\<forall>w \<in> ball z (ee z). (ff has_field_derivative f w) (at w))"
  3205           using holomorphic_convex_primitive [of "ball z (ee z)" "{}" f, simplified]
  3206           by (metis open_ball at_within_open holomorphic_on_def holomorphic_on_imp_continuous_on mem_ball)
  3207       }
  3208       then obtain ff where ff:
  3209             "\<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)"
  3210         by metis
  3211       { fix n
  3212         assume n: "n \<le> N"
  3213         then have "contour_integral(subpath 0 (n/N) h) f - contour_integral(subpath 0 (n/N) g) f =
  3214                    contour_integral(linepath (g(n/N)) (h(n/N))) f - contour_integral(linepath (g 0) (h 0)) f"
  3215         proof (induct n)
  3216           case 0 show ?case by simp
  3217         next
  3218           case (Suc n)
  3219           obtain t where t: "t \<in> k" and "p (n/N) \<in> ball(p t) (ee(p t) / 3)"
  3220             using \<open>path_image p \<subseteq> \<Union>D\<close> [THEN subsetD, where c="p (n/N)"] D_eq N Suc.prems
  3221             by (force simp: path_image_def)
  3222           then have ptu: "cmod (p t - p (n/N)) < ee (p t) / 3"
  3223             by (simp add: dist_norm)
  3224           have e3le: "e/3 \<le> ee (p t) / 3"  using fin_eep t
  3225             by (simp add: e_def)
  3226           { fix x
  3227             assume x: "n/N \<le> x" "x \<le> (1 + n)/N"
  3228             then have nN01: "0 \<le> n/N" "(1 + n)/N \<le> 1"
  3229               using Suc.prems by auto
  3230             then have x01: "0 \<le> x" "x \<le> 1"
  3231               using x by linarith+
  3232             have "cmod (p t - p x)  < ee (p t) / 3 + e/3"
  3233             proof (rule norm_diff_triangle_less [OF ptu de])
  3234               show "\<bar>real n / real N - x\<bar> < d"
  3235                 using x N by (auto simp: field_simps)
  3236             qed (use x01 Suc.prems in auto)
  3237             then have ptx: "cmod (p t - p x) < 2*ee (p t)/3"
  3238               using e3le eepi [OF t] by simp
  3239             have "cmod (p t - g x) < 2*ee (p t)/3 + e/3 "
  3240               apply (rule norm_diff_triangle_less [OF ptx])
  3241               using ghp x01 by (simp add: norm_minus_commute)
  3242             also have "\<dots> \<le> ee (p t)"
  3243               using e3le eepi [OF t] by simp
  3244             finally have gg: "cmod (p t - g x) < ee (p t)" .
  3245             have "cmod (p t - h x) < 2*ee (p t)/3 + e/3 "
  3246               apply (rule norm_diff_triangle_less [OF ptx])
  3247               using ghp x01 by (simp add: norm_minus_commute)
  3248             also have "\<dots> \<le> ee (p t)"
  3249               using e3le eepi [OF t] by simp
  3250             finally have "cmod (p t - g x) < ee (p t)"
  3251                          "cmod (p t - h x) < ee (p t)"
  3252               using gg by auto
  3253           } note ptgh_ee = this
  3254           have "closed_segment (g (real n / real N)) (h (real n / real N)) = path_image (linepath (h (n/N)) (g (n/N)))"
  3255             by (simp add: closed_segment_commute)
  3256           also have pi_hgn: "\<dots> \<subseteq> ball (p t) (ee (p t))"
  3257             using ptgh_ee [of "n/N"] Suc.prems
  3258             by (auto simp: field_simps dist_norm dest: segment_furthest_le [where y="p t"])
  3259           finally have gh_ns: "closed_segment (g (n/N)) (h (n/N)) \<subseteq> S"
  3260             using ee pi t by blast
  3261           have pi_ghn': "path_image (linepath (g ((1 + n) / N)) (h ((1 + n) / N))) \<subseteq> ball (p t) (ee (p t))"
  3262             using ptgh_ee [of "(1+n)/N"] Suc.prems
  3263             by (auto simp: field_simps dist_norm dest: segment_furthest_le [where y="p t"])
  3264           then have gh_n's: "closed_segment (g ((1 + n) / N)) (h ((1 + n) / N)) \<subseteq> S"
  3265             using \<open>N>0\<close> Suc.prems ee pi t
  3266             by (auto simp: Path_Connected.path_image_join field_simps)
  3267           have pi_subset_ball:
  3268                 "path_image (subpath (n/N) ((1+n) / N) g +++ linepath (g ((1+n) / N)) (h ((1+n) / N)) +++
  3269                              subpath ((1+n) / N) (n/N) h +++ linepath (h (n/N)) (g (n/N)))
  3270                  \<subseteq> ball (p t) (ee (p t))"
  3271             apply (intro subset_path_image_join pi_hgn pi_ghn')
  3272             using \<open>N>0\<close> Suc.prems
  3273             apply (auto simp: path_image_subpath dist_norm field_simps closed_segment_eq_real_ivl ptgh_ee)
  3274             done
  3275           have pi0: "(f has_contour_integral 0)
  3276                        (subpath (n/ N) ((Suc n)/N) g +++ linepath(g ((Suc n) / N)) (h((Suc n) / N)) +++
  3277                         subpath ((Suc n) / N) (n/N) h +++ linepath(h (n/N)) (g (n/N)))"
  3278             apply (rule Cauchy_theorem_primitive [of "ball(p t) (ee(p t))" "ff (p t)" "f"])
  3279             apply (metis ff open_ball at_within_open pi t)
  3280             using Suc.prems pi_subset_ball apply (simp_all add: valid_path_join valid_path_subpath g h)
  3281             done
  3282           have fpa1: "f contour_integrable_on subpath (real n / real N) (real (Suc n) / real N) g"
  3283             using Suc.prems by (simp add: contour_integrable_subpath g fpa)
  3284           have fpa2: "f contour_integrable_on linepath (g (real (Suc n) / real N)) (h (real (Suc n) / real N))"
  3285             using gh_n's
  3286             by (auto intro!: contour_integrable_continuous_linepath continuous_on_subset [OF contf])
  3287           have fpa3: "f contour_integrable_on linepath (h (real n / real N)) (g (real n / real N))"
  3288             using gh_ns
  3289             by (auto simp: closed_segment_commute intro!: contour_integrable_continuous_linepath continuous_on_subset [OF contf])
  3290           have eq0: "contour_integral (subpath (n/N) ((Suc n) / real N) g) f +
  3291                      contour_integral (linepath (g ((Suc n) / N)) (h ((Suc n) / N))) f +
  3292                      contour_integral (subpath ((Suc n) / N) (n/N) h) f +
  3293                      contour_integral (linepath (h (n/N)) (g (n/N))) f = 0"
  3294             using contour_integral_unique [OF pi0] Suc.prems
  3295             by (simp add: g h fpa valid_path_subpath contour_integrable_subpath
  3296                           fpa1 fpa2 fpa3 algebra_simps del: of_nat_Suc)
  3297           have *: "\<And>hn he hn' gn gd gn' hgn ghn gh0 ghn'.
  3298                     \<lbrakk>hn - gn = ghn - gh0;
  3299                      gd + ghn' + he + hgn = (0::complex);
  3300                      hn - he = hn'; gn + gd = gn'; hgn = -ghn\<rbrakk> \<Longrightarrow> hn' - gn' = ghn' - gh0"
  3301             by (auto simp: algebra_simps)
  3302           have "contour_integral (subpath 0 (n/N) h) f - contour_integral (subpath ((Suc n) / N) (n/N) h) f =
  3303                 contour_integral (subpath 0 (n/N) h) f + contour_integral (subpath (n/N) ((Suc n) / N) h) f"
  3304             unfolding reversepath_subpath [symmetric, of "((Suc n) / N)"]
  3305             using Suc.prems by (simp add: h fpa contour_integral_reversepath valid_path_subpath contour_integrable_subpath)
  3306           also have "\<dots> = contour_integral (subpath 0 ((Suc n) / N) h) f"
  3307             using Suc.prems by (simp add: contour_integral_subpath_combine h fpa)
  3308           finally have pi0_eq:
  3309                "contour_integral (subpath 0 (n/N) h) f - contour_integral (subpath ((Suc n) / N) (n/N) h) f =
  3310                 contour_integral (subpath 0 ((Suc n) / N) h) f" .
  3311           show ?case
  3312             apply (rule * [OF Suc.hyps eq0 pi0_eq])
  3313             using Suc.prems
  3314             apply (simp_all add: g h fpa contour_integral_subpath_combine
  3315                      contour_integral_reversepath [symmetric] contour_integrable_continuous_linepath
  3316                      continuous_on_subset [OF contf gh_ns])
  3317             done
  3318       qed
  3319       } note ind = this
  3320       have "contour_integral h f = contour_integral g f"
  3321         using ind [OF order_refl] N joins
  3322         by (simp add: linked_paths_def pathstart_def pathfinish_def split: if_split_asm)
  3323     }
  3324     ultimately
  3325     show "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)"
  3326       by metis
  3327   qed
  3328 qed
  3329 
  3330 
  3331 lemma
  3332   assumes "open S" "path p" "path_image p \<subseteq> S"
  3333     shows contour_integral_nearby_ends:
  3334       "\<exists>d. 0 < d \<and>
  3335               (\<forall>g h. valid_path g \<and> valid_path h \<and>
  3336                     (\<forall>t \<in> {0..1}. norm(g t - p t) < d \<and> norm(h t - p t) < d) \<and>
  3337                     pathstart h = pathstart g \<and> pathfinish h = pathfinish g
  3338                     \<longrightarrow> path_image g \<subseteq> S \<and>
  3339                         path_image h \<subseteq> S \<and>
  3340                         (\<forall>f. f holomorphic_on S
  3341                             \<longrightarrow> contour_integral h f = contour_integral g f))"
  3342     and contour_integral_nearby_loops:
  3343       "\<exists>d. 0 < d \<and>
  3344               (\<forall>g h. valid_path g \<and> valid_path h \<and>
  3345                     (\<forall>t \<in> {0..1}. norm(g t - p t) < d \<and> norm(h t - p t) < d) \<and>
  3346                     pathfinish g = pathstart g \<and> pathfinish h = pathstart h
  3347                     \<longrightarrow> path_image g \<subseteq> S \<and>
  3348                         path_image h \<subseteq> S \<and>
  3349                         (\<forall>f. f holomorphic_on S
  3350                             \<longrightarrow> contour_integral h f = contour_integral g f))"
  3351   using contour_integral_nearby [OF assms, where atends=True]
  3352   using contour_integral_nearby [OF assms, where atends=False]
  3353   unfolding linked_paths_def by simp_all
  3354 
  3355 lemma C1_differentiable_polynomial_function:
  3356   fixes p :: "real \<Rightarrow> 'a::euclidean_space"
  3357   shows "polynomial_function p \<Longrightarrow> p C1_differentiable_on S"
  3358   by (metis continuous_on_polymonial_function C1_differentiable_on_def  has_vector_derivative_polynomial_function)
  3359 
  3360 lemma valid_path_polynomial_function:
  3361   fixes p :: "real \<Rightarrow> 'a::euclidean_space"
  3362   shows "polynomial_function p \<Longrightarrow> valid_path p"
  3363 by (force simp: valid_path_def piecewise_C1_differentiable_on_def continuous_on_polymonial_function C1_differentiable_polynomial_function)
  3364 
  3365 lemma valid_path_subpath_trivial [simp]:
  3366     fixes g :: "real \<Rightarrow> 'a::euclidean_space"
  3367     shows "z \<noteq> g x \<Longrightarrow> valid_path (subpath x x g)"
  3368   by (simp add: subpath_def valid_path_polynomial_function)
  3369 
  3370 lemma contour_integral_bound_exists:
  3371 assumes S: "open S"
  3372     and g: "valid_path g"
  3373     and pag: "path_image g \<subseteq> S"
  3374   shows "\<exists>L. 0 < L \<and>
  3375              (\<forall>f B. f holomorphic_on S \<and> (\<forall>z \<in> S. norm(f z) \<le> B)
  3376                \<longrightarrow> norm(contour_integral g f) \<le> L*B)"
  3377 proof -
  3378   have "path g" using g
  3379     by (simp add: valid_path_imp_path)
  3380   then obtain d::real and p
  3381     where d: "0 < d"
  3382       and p: "polynomial_function p" "path_image p \<subseteq> S"
  3383       and pi: "\<And>f. f holomorphic_on S \<Longrightarrow> contour_integral g f = contour_integral p f"
  3384     using contour_integral_nearby_ends [OF S \<open>path g\<close> pag]
  3385     apply clarify
  3386     apply (drule_tac x=g in spec)
  3387     apply (simp only: assms)
  3388     apply (force simp: valid_path_polynomial_function dest: path_approx_polynomial_function)
  3389     done
  3390   then obtain p' where p': "polynomial_function p'"
  3391     "\<And>x. (p has_vector_derivative (p' x)) (at x)"
  3392     by (blast intro: has_vector_derivative_polynomial_function that)
  3393   then have "bounded(p' ` {0..1})"
  3394     using continuous_on_polymonial_function
  3395     by (force simp: intro!: compact_imp_bounded compact_continuous_image)
  3396   then obtain L where L: "L>0" and nop': "\<And>x. \<lbrakk>0 \<le> x; x \<le> 1\<rbrakk> \<Longrightarrow> norm (p' x) \<le> L"
  3397     by (force simp: bounded_pos)
  3398   { fix f B
  3399     assume f: "f holomorphic_on S" and B: "\<And>z. z\<in>S \<Longrightarrow> cmod (f z) \<le> B"
  3400     then have "f contour_integrable_on p \<and> valid_path p"
  3401       using p S
  3402       by (blast intro: valid_path_polynomial_function contour_integrable_holomorphic_simple holomorphic_on_imp_continuous_on)
  3403     moreover have "cmod (vector_derivative p (at x)) * cmod (f (p x)) \<le> L * B" if "0 \<le> x" "x \<le> 1" for x
  3404     proof (rule mult_mono)
  3405       show "cmod (vector_derivative p (at x)) \<le> L"
  3406         by (metis nop' p'(2) that vector_derivative_at)
  3407       show "cmod (f (p x)) \<le> B"
  3408         by (metis B atLeastAtMost_iff imageI p(2) path_defs(4) subset_eq that)
  3409     qed (use \<open>L>0\<close> in auto)
  3410     ultimately have "cmod (contour_integral g f) \<le> L * B"
  3411       apply (simp only: pi [OF f])
  3412       apply (simp only: contour_integral_integral)
  3413       apply (rule order_trans [OF integral_norm_bound_integral])
  3414          apply (auto simp: mult.commute integral_norm_bound_integral contour_integrable_on [symmetric] norm_mult)
  3415       done
  3416   } then
  3417   show ?thesis
  3418     by (force simp: L contour_integral_integral)
  3419 qed
  3420 
  3421 text\<open>We can treat even non-rectifiable paths as having a "length" for bounds on analytic functions in open sets.\<close>
  3422 
  3423 subsection \<open>Winding Numbers\<close>
  3424 
  3425 definition%important winding_number_prop :: "[real \<Rightarrow> complex, complex, real, real \<Rightarrow> complex, complex] \<Rightarrow> bool" where
  3426   "winding_number_prop \<gamma> z e p n \<equiv>
  3427       valid_path p \<and> z \<notin> path_image p \<and>
  3428       pathstart p = pathstart \<gamma> \<and>
  3429       pathfinish p = pathfinish \<gamma> \<and>
  3430       (\<forall>t \<in> {0..1}. norm(\<gamma> t - p t) < e) \<and>
  3431       contour_integral p (\<lambda>w. 1/(w - z)) = 2 * pi * \<i> * n"
  3432 
  3433 definition%important winding_number:: "[real \<Rightarrow> complex, complex] \<Rightarrow> complex" where
  3434   "winding_number \<gamma> z \<equiv> SOME n. \<forall>e > 0. \<exists>p. winding_number_prop \<gamma> z e p n"
  3435 
  3436 
  3437 lemma winding_number:
  3438   assumes "path \<gamma>" "z \<notin> path_image \<gamma>" "0 < e"
  3439     shows "\<exists>p. winding_number_prop \<gamma> z e p (winding_number \<gamma> z)"
  3440 proof -
  3441   have "path_image \<gamma> \<subseteq> UNIV - {z}"
  3442     using assms by blast
  3443   then obtain d
  3444     where d: "d>0"
  3445       and pi_eq: "\<And>h1 h2. valid_path h1 \<and> valid_path h2 \<and>
  3446                     (\<forall>t\<in>{0..1}. cmod (h1 t - \<gamma> t) < d \<and> cmod (h2 t - \<gamma> t) < d) \<and>
  3447                     pathstart h2 = pathstart h1 \<and> pathfinish h2 = pathfinish h1 \<longrightarrow>
  3448                       path_image h1 \<subseteq> UNIV - {z} \<and> path_image h2 \<subseteq> UNIV - {z} \<and>
  3449                       (\<forall>f. f holomorphic_on UNIV - {z} \<longrightarrow> contour_integral h2 f = contour_integral h1 f)"
  3450     using contour_integral_nearby_ends [of "UNIV - {z}" \<gamma>] assms by (auto simp: open_delete)
  3451   then obtain h where h: "polynomial_function h \<and> pathstart h = pathstart \<gamma> \<and> pathfinish h = pathfinish \<gamma> \<and>
  3452                           (\<forall>t \<in> {0..1}. norm(h t - \<gamma> t) < d/2)"
  3453     using path_approx_polynomial_function [OF \<open>path \<gamma>\<close>, of "d/2"] d by auto
  3454   define nn where "nn = 1/(2* pi*\<i>) * contour_integral h (\<lambda>w. 1/(w - z))"
  3455   have "\<exists>n. \<forall>e > 0. \<exists>p. winding_number_prop \<gamma> z e p n"
  3456     proof (rule_tac x=nn in exI, clarify)
  3457       fix e::real
  3458       assume e: "e>0"
  3459       obtain p where p: "polynomial_function p \<and>
  3460             pathstart p = pathstart \<gamma> \<and> pathfinish p = pathfinish \<gamma> \<and> (\<forall>t\<in>{0..1}. cmod (p t - \<gamma> t) < min e (d/2))"
  3461         using path_approx_polynomial_function [OF \<open>path \<gamma>\<close>, of "min e (d/2)"] d \<open>0<e\<close> by auto
  3462       have "(\<lambda>w. 1 / (w - z)) holomorphic_on UNIV - {z}"
  3463         by (auto simp: intro!: holomorphic_intros)
  3464       then show "\<exists>p. winding_number_prop \<gamma> z e p nn"
  3465         apply (rule_tac x=p in exI)
  3466         using pi_eq [of h p] h p d
  3467         apply (auto simp: valid_path_polynomial_function norm_minus_commute nn_def winding_number_prop_def)
  3468         done
  3469     qed
  3470   then show ?thesis
  3471     unfolding winding_number_def by (rule someI2_ex) (blast intro: \<open>0<e\<close>)
  3472 qed
  3473 
  3474 lemma winding_number_unique:
  3475   assumes \<gamma>: "path \<gamma>" "z \<notin> path_image \<gamma>"
  3476       and pi: "\<And>e. e>0 \<Longrightarrow> \<exists>p. winding_number_prop \<gamma> z e p n"
  3477    shows "winding_number \<gamma> z = n"
  3478 proof -
  3479   have "path_image \<gamma> \<subseteq> UNIV - {z}"
  3480     using assms by blast
  3481   then obtain e
  3482     where e: "e>0"
  3483       and pi_eq: "\<And>h1 h2 f. \<lbrakk>valid_path h1; valid_path h2;
  3484                     (\<forall>t\<in>{0..1}. cmod (h1 t - \<gamma> t) < e \<and> cmod (h2 t - \<gamma> t) < e);
  3485                     pathstart h2 = pathstart h1; pathfinish h2 = pathfinish h1; f holomorphic_on UNIV - {z}\<rbrakk> \<Longrightarrow>
  3486                     contour_integral h2 f = contour_integral h1 f"
  3487     using contour_integral_nearby_ends [of "UNIV - {z}" \<gamma>] assms  by (auto simp: open_delete)
  3488   obtain p where p: "winding_number_prop \<gamma> z e p n"
  3489     using pi [OF e] by blast
  3490   obtain q where q: "winding_number_prop \<gamma> z e q (winding_number \<gamma> z)"
  3491     using winding_number [OF \<gamma> e] by blast
  3492   have "2 * complex_of_real pi * \<i> * n = contour_integral p (\<lambda>w. 1 / (w - z))"
  3493     using p by (auto simp: winding_number_prop_def)
  3494   also have "\<dots> = contour_integral q (\<lambda>w. 1 / (w - z))"
  3495   proof (rule pi_eq)
  3496     show "(\<lambda>w. 1 / (w - z)) holomorphic_on UNIV - {z}"
  3497       by (auto intro!: holomorphic_intros)
  3498   qed (use p q in \<open>auto simp: winding_number_prop_def norm_minus_commute\<close>)
  3499   also have "\<dots> = 2 * complex_of_real pi * \<i> * winding_number \<gamma> z"
  3500     using q by (auto simp: winding_number_prop_def)
  3501   finally have "2 * complex_of_real pi * \<i> * n = 2 * complex_of_real pi * \<i> * winding_number \<gamma> z" .
  3502   then show ?thesis
  3503     by simp
  3504 qed
  3505 
  3506 (*NB not winding_number_prop here due to the loop in p*)
  3507 lemma winding_number_unique_loop:
  3508   assumes \<gamma>: "path \<gamma>" "z \<notin> path_image \<gamma>"
  3509       and loop: "pathfinish \<gamma> = pathstart \<gamma>"
  3510       and pi:
  3511         "\<And>e. e>0 \<Longrightarrow> \<exists>p. valid_path p \<and> z \<notin> path_image p \<and>
  3512                            pathfinish p = pathstart p \<and>
  3513                            (\<forall>t \<in> {0..1}. norm (\<gamma> t - p t) < e) \<and>
  3514                            contour_integral p (\<lambda>w. 1/(w - z)) = 2 * pi * \<i> * n"
  3515    shows "winding_number \<gamma> z = n"
  3516 proof -
  3517   have "path_image \<gamma> \<subseteq> UNIV - {z}"
  3518     using assms by blast
  3519   then obtain e
  3520     where e: "e>0"
  3521       and pi_eq: "\<And>h1 h2 f. \<lbrakk>valid_path h1; valid_path h2;
  3522                     (\<forall>t\<in>{0..1}. cmod (h1 t - \<gamma> t) < e \<and> cmod (h2 t - \<gamma> t) < e);
  3523                     pathfinish h1 = pathstart h1; pathfinish h2 = pathstart h2; f holomorphic_on UNIV - {z}\<rbrakk> \<Longrightarrow>
  3524                     contour_integral h2 f = contour_integral h1 f"
  3525     using contour_integral_nearby_loops [of "UNIV - {z}" \<gamma>] assms  by (auto simp: open_delete)
  3526   obtain p where p:
  3527      "valid_path p \<and> z \<notin> path_image p \<and> pathfinish p = pathstart p \<and>
  3528       (\<forall>t \<in> {0..1}. norm (\<gamma> t - p t) < e) \<and>
  3529       contour_integral p (\<lambda>w. 1/(w - z)) = 2 * pi * \<i> * n"
  3530     using pi [OF e] by blast
  3531   obtain q where q: "winding_number_prop \<gamma> z e q (winding_number \<gamma> z)"
  3532     using winding_number [OF \<gamma> e] by blast
  3533   have "2 * complex_of_real pi * \<i> * n = contour_integral p (\<lambda>w. 1 / (w - z))"
  3534     using p by auto
  3535   also have "\<dots> = contour_integral q (\<lambda>w. 1 / (w - z))"
  3536   proof (rule pi_eq)
  3537     show "(\<lambda>w. 1 / (w - z)) holomorphic_on UNIV - {z}"
  3538       by (auto intro!: holomorphic_intros)
  3539   qed (use p q loop in \<open>auto simp: winding_number_prop_def norm_minus_commute\<close>)
  3540   also have "\<dots> = 2 * complex_of_real pi * \<i> * winding_number \<gamma> z"
  3541     using q by (auto simp: winding_number_prop_def)
  3542   finally have "2 * complex_of_real pi * \<i> * n = 2 * complex_of_real pi * \<i> * winding_number \<gamma> z" .
  3543   then show ?thesis
  3544     by simp
  3545 qed
  3546 
  3547 proposition winding_number_valid_path:
  3548   assumes "valid_path \<gamma>" "z \<notin> path_image \<gamma>"
  3549   shows "winding_number \<gamma> z = 1/(2*pi*\<i>) * contour_integral \<gamma> (\<lambda>w. 1/(w - z))"
  3550   by (rule winding_number_unique)
  3551   (use assms in \<open>auto simp: valid_path_imp_path winding_number_prop_def\<close>)
  3552 
  3553 proposition has_contour_integral_winding_number:
  3554   assumes \<gamma>: "valid_path \<gamma>" "z \<notin> path_image \<gamma>"
  3555     shows "((\<lambda>w. 1/(w - z)) has_contour_integral (2*pi*\<i>*winding_number \<gamma> z)) \<gamma>"
  3556 by (simp add: winding_number_valid_path has_contour_integral_integral contour_integrable_inversediff assms)
  3557 
  3558 lemma winding_number_trivial [simp]: "z \<noteq> a \<Longrightarrow> winding_number(linepath a a) z = 0"
  3559   by (simp add: winding_number_valid_path)
  3560 
  3561 lemma winding_number_subpath_trivial [simp]: "z \<noteq> g x \<Longrightarrow> winding_number (subpath x x g) z = 0"
  3562   by (simp add: path_image_subpath winding_number_valid_path)
  3563 
  3564 lemma winding_number_join:
  3565   assumes \<gamma>1: "path \<gamma>1" "z \<notin> path_image \<gamma>1"
  3566       and \<gamma>2: "path \<gamma>2" "z \<notin> path_image \<gamma>2"
  3567       and "pathfinish \<gamma>1 = pathstart \<gamma>2"
  3568     shows "winding_number(\<gamma>1 +++ \<gamma>2) z = winding_number \<gamma>1 z + winding_number \<gamma>2 z"
  3569 proof (rule winding_number_unique)
  3570   show "\<exists>p. winding_number_prop (\<gamma>1 +++ \<gamma>2) z e p
  3571               (winding_number \<gamma>1 z + winding_number \<gamma>2 z)" if "e > 0" for e
  3572   proof -
  3573     obtain p1 where "winding_number_prop \<gamma>1 z e p1 (winding_number \<gamma>1 z)"
  3574       using \<open>0 < e\<close> \<gamma>1 winding_number by blast
  3575     moreover
  3576     obtain p2 where "winding_number_prop \<gamma>2 z e p2 (winding_number \<gamma>2 z)"
  3577       using \<open>0 < e\<close> \<gamma>2 winding_number by blast
  3578     ultimately
  3579     have "winding_number_prop (\<gamma>1+++\<gamma>2) z e (p1+++p2) (winding_number \<gamma>1 z + winding_number \<gamma>2 z)"
  3580       using assms
  3581       apply (simp add: winding_number_prop_def not_in_path_image_join contour_integrable_inversediff algebra_simps)
  3582       apply (auto simp: joinpaths_def)
  3583       done
  3584     then show ?thesis
  3585       by blast
  3586   qed
  3587 qed (use assms in \<open>auto simp: not_in_path_image_join\<close>)
  3588 
  3589 lemma winding_number_reversepath:
  3590   assumes "path \<gamma>" "z \<notin> path_image \<gamma>"
  3591     shows "winding_number(reversepath \<gamma>) z = - (winding_number \<gamma> z)"
  3592 proof (rule winding_number_unique)
  3593   show "\<exists>p. winding_number_prop (reversepath \<gamma>) z e p (- winding_number \<gamma> z)" if "e > 0" for e
  3594   proof -
  3595     obtain p where "winding_number_prop \<gamma> z e p (winding_number \<gamma> z)"
  3596       using \<open>0 < e\<close> assms winding_number by blast
  3597     then have "winding_number_prop (reversepath \<gamma>) z e (reversepath p) (- winding_number \<gamma> z)"
  3598       using assms
  3599       apply (simp add: winding_number_prop_def contour_integral_reversepath contour_integrable_inversediff valid_path_imp_reverse)
  3600       apply (auto simp: reversepath_def)
  3601       done
  3602     then show ?thesis
  3603       by blast
  3604   qed
  3605 qed (use assms in auto)
  3606 
  3607 lemma winding_number_shiftpath:
  3608   assumes \<gamma>: "path \<gamma>" "z \<notin> path_image \<gamma>"
  3609       and "pathfinish \<gamma> = pathstart \<gamma>" "a \<in> {0..1}"
  3610     shows "winding_number(shiftpath a \<gamma>) z = winding_number \<gamma> z"
  3611 proof (rule winding_number_unique_loop)
  3612   show "\<exists>p. valid_path p \<and> z \<notin> path_image p \<and> pathfinish p = pathstart p \<and>
  3613             (\<forall>t\<in>{0..1}. cmod (shiftpath a \<gamma> t - p t) < e) \<and>
  3614             contour_integral p (\<lambda>w. 1 / (w - z)) =
  3615             complex_of_real (2 * pi) * \<i> * winding_number \<gamma> z"
  3616     if "e > 0" for e
  3617   proof -
  3618     obtain p where "winding_number_prop \<gamma> z e p (winding_number \<gamma> z)"
  3619       using \<open>0 < e\<close> assms winding_number by blast
  3620     then show ?thesis
  3621       apply (rule_tac x="shiftpath a p" in exI)
  3622       using assms that
  3623       apply (auto simp: winding_number_prop_def path_image_shiftpath pathfinish_shiftpath pathstart_shiftpath contour_integral_shiftpath)
  3624       apply (simp add: shiftpath_def)
  3625       done
  3626   qed
  3627 qed (use assms in \<open>auto simp: path_shiftpath path_image_shiftpath pathfinish_shiftpath pathstart_shiftpath\<close>)
  3628 
  3629 lemma winding_number_split_linepath:
  3630   assumes "c \<in> closed_segment a b" "z \<notin> closed_segment a b"
  3631     shows "winding_number(linepath a b) z = winding_number(linepath a c) z + winding_number(linepath c b) z"
  3632 proof -
  3633   have "z \<notin> closed_segment a c" "z \<notin> closed_segment c b"
  3634     using assms  by (meson convex_contains_segment convex_segment ends_in_segment subsetCE)+
  3635   then show ?thesis
  3636     using assms
  3637     by (simp add: winding_number_valid_path contour_integral_split_linepath [symmetric] continuous_on_inversediff field_simps)
  3638 qed
  3639 
  3640 lemma winding_number_cong:
  3641    "(\<And>t. \<lbrakk>0 \<le> t; t \<le> 1\<rbrakk> \<Longrightarrow> p t = q t) \<Longrightarrow> winding_number p z = winding_number q z"
  3642   by (simp add: winding_number_def winding_number_prop_def pathstart_def pathfinish_def)
  3643 
  3644 lemma winding_number_constI:
  3645   assumes "c\<noteq>z" "\<And>t. \<lbrakk>0\<le>t; t\<le>1\<rbrakk> \<Longrightarrow> g t = c" 
  3646   shows "winding_number g z = 0"
  3647 proof -
  3648   have "winding_number g z = winding_number (linepath c c) z"
  3649     apply (rule winding_number_cong)
  3650     using assms unfolding linepath_def by auto
  3651   moreover have "winding_number (linepath c c) z =0"
  3652     apply (rule winding_number_trivial)
  3653     using assms by auto
  3654   ultimately show ?thesis by auto
  3655 qed
  3656 
  3657 lemma winding_number_offset: "winding_number p z = winding_number (\<lambda>w. p w - z) 0"
  3658   unfolding winding_number_def
  3659 proof (intro ext arg_cong [where f = Eps] arg_cong [where f = All] imp_cong refl, safe)
  3660   fix n e g
  3661   assume "0 < e" and g: "winding_number_prop p z e g n"
  3662   then show "\<exists>r. winding_number_prop (\<lambda>w. p w - z) 0 e r n"
  3663     by (rule_tac x="\<lambda>t. g t - z" in exI)
  3664        (force simp: winding_number_prop_def contour_integral_integral valid_path_def path_defs
  3665                 vector_derivative_def has_vector_derivative_diff_const piecewise_C1_differentiable_diff C1_differentiable_imp_piecewise)
  3666 next
  3667   fix n e g
  3668   assume "0 < e" and g: "winding_number_prop (\<lambda>w. p w - z) 0 e g n"
  3669   then show "\<exists>r. winding_number_prop p z e r n"
  3670     apply (rule_tac x="\<lambda>t. g t + z" in exI)
  3671     apply (simp add: winding_number_prop_def contour_integral_integral valid_path_def path_defs
  3672         piecewise_C1_differentiable_add vector_derivative_def has_vector_derivative_add_const C1_differentiable_imp_piecewise)
  3673     apply (force simp: algebra_simps)
  3674     done
  3675 qed
  3676 
  3677 subsubsection%unimportant \<open>Some lemmas about negating a path\<close>
  3678 
  3679 lemma valid_path_negatepath: "valid_path \<gamma> \<Longrightarrow> valid_path (uminus \<circ> \<gamma>)"
  3680    unfolding o_def using piecewise_C1_differentiable_neg valid_path_def by blast
  3681 
  3682 lemma has_contour_integral_negatepath:
  3683   assumes \<gamma>: "valid_path \<gamma>" and cint: "((\<lambda>z. f (- z)) has_contour_integral - i) \<gamma>"
  3684   shows "(f has_contour_integral i) (uminus \<circ> \<gamma>)"
  3685 proof -
  3686   obtain S where cont: "continuous_on {0..1} \<gamma>" and "finite S" and diff: "\<gamma> C1_differentiable_on {0..1} - S"
  3687     using \<gamma> by (auto simp: valid_path_def piecewise_C1_differentiable_on_def)
  3688   have "((\<lambda>x. - (f (- \<gamma> x) * vector_derivative \<gamma> (at x within {0..1}))) has_integral i) {0..1}"
  3689     using cint by (auto simp: has_contour_integral_def dest: has_integral_neg)
  3690   then
  3691   have "((\<lambda>x. f (- \<gamma> x) * vector_derivative (uminus \<circ> \<gamma>) (at x within {0..1})) has_integral i) {0..1}"
  3692   proof (rule rev_iffD1 [OF _ has_integral_spike_eq])
  3693     show "negligible S"
  3694       by (simp add: \<open>finite S\<close> negligible_finite)
  3695     show "f (- \<gamma> x) * vector_derivative (uminus \<circ> \<gamma>) (at x within {0..1}) =
  3696          - (f (- \<gamma> x) * vector_derivative \<gamma> (at x within {0..1}))"
  3697       if "x \<in> {0..1} - S" for x
  3698     proof -
  3699       have "vector_derivative (uminus \<circ> \<gamma>) (at x within cbox 0 1) = - vector_derivative \<gamma> (at x within cbox 0 1)"
  3700       proof (rule vector_derivative_within_cbox)
  3701         show "(uminus \<circ> \<gamma> has_vector_derivative - vector_derivative \<gamma> (at x within cbox 0 1)) (at x within cbox 0 1)"
  3702           using that unfolding o_def
  3703           by (metis C1_differentiable_on_eq UNIV_I diff differentiable_subset has_vector_derivative_minus subsetI that vector_derivative_works)
  3704       qed (use that in auto)
  3705       then show ?thesis
  3706         by simp
  3707     qed
  3708   qed
  3709   then show ?thesis by (simp add: has_contour_integral_def)
  3710 qed
  3711 
  3712 lemma winding_number_negatepath:
  3713   assumes \<gamma>: "valid_path \<gamma>" and 0: "0 \<notin> path_image \<gamma>"
  3714   shows "winding_number(uminus \<circ> \<gamma>) 0 = winding_number \<gamma> 0"
  3715 proof -
  3716   have "(/) 1 contour_integrable_on \<gamma>"
  3717     using "0" \<gamma> contour_integrable_inversediff by fastforce
  3718   then have "((\<lambda>z. 1/z) has_contour_integral contour_integral \<gamma> ((/) 1)) \<gamma>"
  3719     by (rule has_contour_integral_integral)
  3720   then have "((\<lambda>z. 1 / - z) has_contour_integral - contour_integral \<gamma> ((/) 1)) \<gamma>"
  3721     using has_contour_integral_neg by auto
  3722   then show ?thesis
  3723     using assms
  3724     apply (simp add: winding_number_valid_path valid_path_negatepath image_def path_defs)
  3725     apply (simp add: contour_integral_unique has_contour_integral_negatepath)
  3726     done
  3727 qed
  3728 
  3729 lemma contour_integrable_negatepath:
  3730   assumes \<gamma>: "valid_path \<gamma>" and pi: "(\<lambda>z. f (- z)) contour_integrable_on \<gamma>"
  3731   shows "f contour_integrable_on (uminus \<circ> \<gamma>)"
  3732   by (metis \<gamma> add.inverse_inverse contour_integrable_on_def has_contour_integral_negatepath pi)
  3733 
  3734 (* A combined theorem deducing several things piecewise.*)
  3735 lemma winding_number_join_pos_combined:
  3736      "\<lbrakk>valid_path \<gamma>1; z \<notin> path_image \<gamma>1; 0 < Re(winding_number \<gamma>1 z);
  3737        valid_path \<gamma>2; z \<notin> path_image \<gamma>2; 0 < Re(winding_number \<gamma>2 z); pathfinish \<gamma>1 = pathstart \<gamma>2\<rbrakk>
  3738       \<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)"
  3739   by (simp add: valid_path_join path_image_join winding_number_join valid_path_imp_path)
  3740 
  3741 
  3742 subsubsection%unimportant \<open>Useful sufficient conditions for the winding number to be positive\<close>
  3743 
  3744 lemma Re_winding_number:
  3745     "\<lbrakk>valid_path \<gamma>; z \<notin> path_image \<gamma>\<rbrakk>
  3746      \<Longrightarrow> Re(winding_number \<gamma> z) = Im(contour_integral \<gamma> (\<lambda>w. 1/(w - z))) / (2*pi)"
  3747 by (simp add: winding_number_valid_path field_simps Re_divide power2_eq_square)
  3748 
  3749 lemma winding_number_pos_le:
  3750   assumes \<gamma>: "valid_path \<gamma>" "z \<notin> path_image \<gamma>"
  3751       and ge: "\<And>x. \<lbrakk>0 < x; x < 1\<rbrakk> \<Longrightarrow> 0 \<le> Im (vector_derivative \<gamma> (at x) * cnj(\<gamma> x - z))"
  3752     shows "0 \<le> Re(winding_number \<gamma> z)"
  3753 proof -
  3754   have ge0: "0 \<le> Im (vector_derivative \<gamma> (at x) / (\<gamma> x - z))" if x: "0 < x" "x < 1" for x
  3755     using ge by (simp add: Complex.Im_divide algebra_simps x)
  3756   let ?vd = "\<lambda>x. 1 / (\<gamma> x - z) * vector_derivative \<gamma> (at x)"
  3757   let ?int = "\<lambda>z. contour_integral \<gamma> (\<lambda>w. 1 / (w - z))"
  3758   have hi: "(?vd has_integral ?int z) (cbox 0 1)"
  3759     unfolding box_real
  3760     apply (subst has_contour_integral [symmetric])
  3761     using \<gamma> by (simp add: contour_integrable_inversediff has_contour_integral_integral)
  3762   have "0 \<le> Im (?int z)"
  3763   proof (rule has_integral_component_nonneg [of \<i>, simplified])
  3764     show "\<And>x. x \<in> cbox 0 1 \<Longrightarrow> 0 \<le> Im (if 0 < x \<and> x < 1 then ?vd x else 0)"
  3765       by (force simp: ge0)
  3766     show "((\<lambda>x. if 0 < x \<and> x < 1 then ?vd x else 0) has_integral ?int z) (cbox 0 1)"
  3767       by (rule has_integral_spike_interior [OF hi]) simp
  3768   qed
  3769   then show ?thesis
  3770     by (simp add: Re_winding_number [OF \<gamma>] field_simps)
  3771 qed
  3772 
  3773 lemma winding_number_pos_lt_lemma:
  3774   assumes \<gamma>: "valid_path \<gamma>" "z \<notin> path_image \<gamma>"
  3775       and e: "0 < e"
  3776       and ge: "\<And>x. \<lbrakk>0 < x; x < 1\<rbrakk> \<Longrightarrow> e \<le> Im (vector_derivative \<gamma> (at x) / (\<gamma> x - z))"
  3777     shows "0 < Re(winding_number \<gamma> z)"
  3778 proof -
  3779   let ?vd = "\<lambda>x. 1 / (\<gamma> x - z) * vector_derivative \<gamma> (at x)"
  3780   let ?int = "\<lambda>z. contour_integral \<gamma> (\<lambda>w. 1 / (w - z))"
  3781   have hi: "(?vd has_integral ?int z) (cbox 0 1)"
  3782     unfolding box_real
  3783     apply (subst has_contour_integral [symmetric])
  3784     using \<gamma> by (simp add: contour_integrable_inversediff has_contour_integral_integral)
  3785   have "e \<le> Im (contour_integral \<gamma> (\<lambda>w. 1 / (w - z)))"
  3786   proof (rule has_integral_component_le [of \<i> "\<lambda>x. \<i>*e" "\<i>*e" "{0..1}", simplified])
  3787     show "((\<lambda>x. if 0 < x \<and> x < 1 then ?vd x else \<i> * complex_of_real e) has_integral ?int z) {0..1}"
  3788       by (rule has_integral_spike_interior [OF hi, simplified box_real]) (use e in simp)
  3789     show "\<And>x. 0 \<le> x \<and> x \<le> 1 \<Longrightarrow>
  3790               e \<le> Im (if 0 < x \<and> x < 1 then ?vd x else \<i> * complex_of_real e)"
  3791       by (simp add: ge)
  3792   qed (use has_integral_const_real [of _ 0 1] in auto)
  3793   with e show ?thesis
  3794     by (simp add: Re_winding_number [OF \<gamma>] field_simps)
  3795 qed
  3796 
  3797 lemma winding_number_pos_lt:
  3798   assumes \<gamma>: "valid_path \<gamma>" "z \<notin> path_image \<gamma>"
  3799       and e: "0 < e"
  3800       and ge: "\<And>x. \<lbrakk>0 < x; x < 1\<rbrakk> \<Longrightarrow> e \<le> Im (vector_derivative \<gamma> (at x) * cnj(\<gamma> x - z))"
  3801     shows "0 < Re (winding_number \<gamma> z)"
  3802 proof -
  3803   have bm: "bounded ((\<lambda>w. w - z) ` (path_image \<gamma>))"
  3804     using bounded_translation [of _ "-z"] \<gamma> by (simp add: bounded_valid_path_image)
  3805   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"
  3806     using bounded_pos [THEN iffD1, OF bm] by blast
  3807   { fix x::real  assume x: "0 < x" "x < 1"
  3808     then have B2: "cmod (\<gamma> x - z)^2 \<le> B^2" using Bno [of "\<gamma> x - z"]
  3809       by (simp add: path_image_def power2_eq_square mult_mono')
  3810     with x have "\<gamma> x \<noteq> z" using \<gamma>
  3811       using path_image_def by fastforce
  3812     then have "e / B\<^sup>2 \<le> Im (vector_derivative \<gamma> (at x) * cnj (\<gamma> x - z)) / (cmod (\<gamma> x - z))\<^sup>2"
  3813       using B ge [OF x] B2 e
  3814       apply (rule_tac y="e / (cmod (\<gamma> x - z))\<^sup>2" in order_trans)
  3815       apply (auto simp: divide_left_mono divide_right_mono)
  3816       done
  3817     then have "e / B\<^sup>2 \<le> Im (vector_derivative \<gamma> (at x) / (\<gamma> x - z))"
  3818       by (simp add: complex_div_cnj [of _ "\<gamma> x - z" for x] del: complex_cnj_diff times_complex.sel)
  3819   } note * = this
  3820   show ?thesis
  3821     using e B by (simp add: * winding_number_pos_lt_lemma [OF \<gamma>, of "e/B^2"])
  3822 qed
  3823 
  3824 subsection\<open>The winding number is an integer\<close>
  3825 
  3826 text\<open>Proof from the book Complex Analysis by Lars V. Ahlfors, Chapter 4, section 2.1,
  3827      Also on page 134 of Serge Lang's book with the name title, etc.\<close>
  3828 
  3829 lemma exp_fg:
  3830   fixes z::complex
  3831   assumes g: "(g has_vector_derivative g') (at x within s)"
  3832       and f: "(f has_vector_derivative (g' / (g x - z))) (at x within s)"
  3833       and z: "g x \<noteq> z"
  3834     shows "((\<lambda>x. exp(-f x) * (g x - z)) has_vector_derivative 0) (at x within s)"
  3835 proof -
  3836   have *: "(exp \<circ> (\<lambda>x. (- f x)) has_vector_derivative - (g' / (g x - z)) * exp (- f x)) (at x within s)"
  3837     using assms unfolding has_vector_derivative_def scaleR_conv_of_real
  3838     by (auto intro!: derivative_eq_intros)
  3839   show ?thesis
  3840     apply (rule has_vector_derivative_eq_rhs)
  3841     using z
  3842     apply (auto intro!: derivative_eq_intros * [unfolded o_def] g)
  3843     done
  3844 qed
  3845 
  3846 lemma winding_number_exp_integral:
  3847   fixes z::complex
  3848   assumes \<gamma>: "\<gamma> piecewise_C1_differentiable_on {a..b}"
  3849       and ab: "a \<le> b"
  3850       and z: "z \<notin> \<gamma> ` {a..b}"
  3851     shows "(\<lambda>x. vector_derivative \<gamma> (at x) / (\<gamma> x - z)) integrable_on {a..b}"
  3852           (is "?thesis1")
  3853           "exp (- (integral {a..b} (\<lambda>x. vector_derivative \<gamma> (at x) / (\<gamma> x - z)))) * (\<gamma> b - z) = \<gamma> a - z"
  3854           (is "?thesis2")
  3855 proof -
  3856   let ?D\<gamma> = "\<lambda>x. vector_derivative \<gamma> (at x)"
  3857   have [simp]: "\<And>x. a \<le> x \<Longrightarrow> x \<le> b \<Longrightarrow> \<gamma> x \<noteq> z"
  3858     using z by force
  3859   have cong: "continuous_on {a..b} \<gamma>"
  3860     using \<gamma> by (simp add: piecewise_C1_differentiable_on_def)
  3861   obtain k where fink: "finite k" and g_C1_diff: "\<gamma> C1_differentiable_on ({a..b} - k)"
  3862     using \<gamma> by (force simp: piecewise_C1_differentiable_on_def)
  3863   have \<circ>: "open ({a<..<b} - k)"
  3864     using \<open>finite k\<close> by (simp add: finite_imp_closed open_Diff)
  3865   moreover have "{a<..<b} - k \<subseteq> {a..b} - k"
  3866     by force
  3867   ultimately have g_diff_at: "\<And>x. \<lbrakk>x \<notin> k; x \<in> {a<..<b}\<rbrakk> \<Longrightarrow> \<gamma> differentiable at x"
  3868     by (metis Diff_iff differentiable_on_subset C1_diff_imp_diff [OF g_C1_diff] differentiable_on_def at_within_open)
  3869   { fix w
  3870     assume "w \<noteq> z"
  3871     have "continuous_on (ball w (cmod (w - z))) (\<lambda>w. 1 / (w - z))"
  3872       by (auto simp: dist_norm intro!: continuous_intros)
  3873     moreover have "\<And>x. cmod (w - x) < cmod (w - z) \<Longrightarrow> \<exists>f'. ((\<lambda>w. 1 / (w - z)) has_field_derivative f') (at x)"
  3874       by (auto simp: intro!: derivative_eq_intros)
  3875     ultimately have "\<exists>h. \<forall>y. norm(y - w) < norm(w - z) \<longrightarrow> (h has_field_derivative 1/(y - z)) (at y)"
  3876       using holomorphic_convex_primitive [of "ball w (norm(w - z))" "{}" "\<lambda>w. 1/(w - z)"]
  3877       by (force simp: field_differentiable_def Ball_def dist_norm at_within_open_NO_MATCH norm_minus_commute)
  3878   }
  3879   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)"
  3880     by meson
  3881   have exy: "\<exists>y. ((\<lambda>x. inverse (\<gamma> x - z) * ?D\<gamma> x) has_integral y) {a..b}"
  3882     unfolding integrable_on_def [symmetric]
  3883   proof (rule contour_integral_local_primitive_any [OF piecewise_C1_imp_differentiable [OF \<gamma>]])
  3884     show "\<exists>d h. 0 < d \<and>
  3885                (\<forall>y. cmod (y - w) < d \<longrightarrow> (h has_field_derivative inverse (y - z))(at y within - {z}))"
  3886           if "w \<in> - {z}" for w
  3887       apply (rule_tac x="norm(w - z)" in exI)
  3888       using that inverse_eq_divide has_field_derivative_at_within h
  3889       by (metis Compl_insert DiffD2 insertCI right_minus_eq zero_less_norm_iff)
  3890   qed simp
  3891   have vg_int: "(\<lambda>x. ?D\<gamma> x / (\<gamma> x - z)) integrable_on {a..b}"
  3892     unfolding box_real [symmetric] divide_inverse_commute
  3893     by (auto intro!: exy integrable_subinterval simp add: integrable_on_def ab)
  3894   with ab show ?thesis1
  3895     by (simp add: divide_inverse_commute integral_def integrable_on_def)
  3896   { fix t
  3897     assume t: "t \<in> {a..b}"
  3898     have cball: "continuous_on (ball (\<gamma> t) (dist (\<gamma> t) z)) (\<lambda>x. inverse (x - z))"
  3899         using z by (auto intro!: continuous_intros simp: dist_norm)
  3900     have icd: "\<And>x. cmod (\<gamma> t - x) < cmod (\<gamma> t - z) \<Longrightarrow> (\<lambda>w. inverse (w - z)) field_differentiable at x"
  3901       unfolding field_differentiable_def by (force simp: intro!: derivative_eq_intros)
  3902     obtain h where h: "\<And>x. cmod (\<gamma> t - x) < cmod (\<gamma> t - z) \<Longrightarrow>
  3903                        (h has_field_derivative inverse (x - z)) (at x within {y. cmod (\<gamma> t - y) < cmod (\<gamma> t - z)})"
  3904       using holomorphic_convex_primitive [where f = "\<lambda>w. inverse(w - z)", OF convex_ball finite.emptyI cball icd]
  3905       by simp (auto simp: ball_def dist_norm that)
  3906     { fix x D
  3907       assume x: "x \<notin> k" "a < x" "x < b"
  3908       then have "x \<in> interior ({a..b} - k)"
  3909         using open_subset_interior [OF \<circ>] by fastforce
  3910       then have con: "isCont ?D\<gamma> x"
  3911         using g_C1_diff x by (auto simp: C1_differentiable_on_eq intro: continuous_on_interior)
  3912       then have con_vd: "continuous (at x within {a..b}) (\<lambda>x. ?D\<gamma> x)"
  3913         by (rule continuous_at_imp_continuous_within)
  3914       have gdx: "\<gamma> differentiable at x"
  3915         using x by (simp add: g_diff_at)
  3916       have "\<And>d. \<lbrakk>x \<notin> k; a < x; x < b;
  3917           (\<gamma> has_vector_derivative d) (at x); a \<le> t; t \<le> b\<rbrakk>
  3918          \<Longrightarrow> ((\<lambda>x. integral {a..x}
  3919                      (\<lambda>x. ?D\<gamma> x /
  3920                            (\<gamma> x - z))) has_vector_derivative
  3921               d / (\<gamma> x - z))
  3922               (at x within {a..b})"
  3923         apply (rule has_vector_derivative_eq_rhs)
  3924          apply (rule integral_has_vector_derivative_continuous_at [where S = "{}", simplified])
  3925         apply (rule con_vd continuous_intros cong vg_int | simp add: continuous_at_imp_continuous_within has_vector_derivative_continuous vector_derivative_at)+
  3926         done
  3927       then have "((\<lambda>c. exp (- integral {a..c} (\<lambda>x. ?D\<gamma> x / (\<gamma> x - z))) * (\<gamma> c - z)) has_derivative (\<lambda>h. 0))
  3928           (at x within {a..b})"
  3929         using x gdx t
  3930         apply (clarsimp simp add: differentiable_iff_scaleR)
  3931         apply (rule exp_fg [unfolded has_vector_derivative_def, simplified], blast intro: has_derivative_at_withinI)
  3932         apply (simp_all add: has_vector_derivative_def [symmetric])
  3933         done
  3934       } note * = this
  3935     have "exp (- (integral {a..t} (\<lambda>x. ?D\<gamma> x / (\<gamma> x - z)))) * (\<gamma> t - z) =\<gamma> a - z"
  3936       apply (rule has_derivative_zero_unique_strong_interval [of "{a,b} \<union> k" a b])
  3937       using t
  3938       apply (auto intro!: * continuous_intros fink cong indefinite_integral_continuous_1 [OF vg_int]  simp add: ab)+
  3939       done
  3940    }
  3941   with ab show ?thesis2
  3942     by (simp add: divide_inverse_commute integral_def)
  3943 qed
  3944 
  3945 lemma winding_number_exp_2pi:
  3946     "\<lbrakk>path p; z \<notin> path_image p\<rbrakk>
  3947      \<Longrightarrow> pathfinish p - z = exp (2 * pi * \<i> * winding_number p z) * (pathstart p - z)"
  3948 using winding_number [of p z 1] unfolding valid_path_def path_image_def pathstart_def pathfinish_def winding_number_prop_def
  3949   by (force dest: winding_number_exp_integral(2) [of _ 0 1 z] simp: field_simps contour_integral_integral exp_minus)
  3950 
  3951 lemma integer_winding_number_eq:
  3952   assumes \<gamma>: "path \<gamma>" and z: "z \<notin> path_image \<gamma>"
  3953   shows "winding_number \<gamma> z \<in> \<int> \<longleftrightarrow> pathfinish \<gamma> = pathstart \<gamma>"
  3954 proof -
  3955   obtain p where p: "valid_path p" "z \<notin> path_image p"
  3956                     "pathstart p = pathstart \<gamma>" "pathfinish p = pathfinish \<gamma>"
  3957            and eq: "contour_integral p (\<lambda>w. 1 / (w - z)) = complex_of_real (2 * pi) * \<i> * winding_number \<gamma> z"
  3958     using winding_number [OF assms, of 1] unfolding winding_number_prop_def by auto
  3959   then have wneq: "winding_number \<gamma> z = winding_number p z"
  3960       using eq winding_number_valid_path by force
  3961   have iff: "(winding_number \<gamma> z \<in> \<int>) \<longleftrightarrow> (exp (contour_integral p (\<lambda>w. 1 / (w - z))) = 1)"
  3962     using eq by (simp add: exp_eq_1 complex_is_Int_iff)
  3963   have "exp (contour_integral p (\<lambda>w. 1 / (w - z))) = (\<gamma> 1 - z) / (\<gamma> 0 - z)"
  3964     using p winding_number_exp_integral(2) [of p 0 1 z]
  3965     apply (simp add: valid_path_def path_defs contour_integral_integral exp_minus divide_simps)
  3966     by (metis path_image_def pathstart_def pathstart_in_path_image)
  3967   then have "winding_number p z \<in> \<int> \<longleftrightarrow> pathfinish p = pathstart p"
  3968     using p wneq iff by (auto simp: path_defs)
  3969   then show ?thesis using p eq
  3970     by (auto simp: winding_number_valid_path)
  3971 qed
  3972 
  3973 theorem integer_winding_number:
  3974   "\<lbrakk>path \<gamma>; pathfinish \<gamma> = pathstart \<gamma>; z \<notin> path_image \<gamma>\<rbrakk> \<Longrightarrow> winding_number \<gamma> z \<in> \<int>"
  3975 by (metis integer_winding_number_eq)
  3976 
  3977 
  3978 text\<open>If the winding number's magnitude is at least one, then the path must contain points in every direction.*)
  3979    We can thus bound the winding number of a path that doesn't intersect a given ray. \<close>
  3980 
  3981 lemma winding_number_pos_meets:
  3982   fixes z::complex
  3983   assumes \<gamma>: "valid_path \<gamma>" and z: "z \<notin> path_image \<gamma>" and 1: "Re (winding_number \<gamma> z) \<ge> 1"
  3984       and w: "w \<noteq> z"
  3985   shows "\<exists>a::real. 0 < a \<and> z + a*(w - z) \<in> path_image \<gamma>"
  3986 proof -
  3987   have [simp]: "\<And>x. 0 \<le> x \<Longrightarrow> x \<le> 1 \<Longrightarrow> \<gamma> x \<noteq> z"
  3988     using z by (auto simp: path_image_def)
  3989   have [simp]: "z \<notin> \<gamma> ` {0..1}"
  3990     using path_image_def z by auto
  3991   have gpd: "\<gamma> piecewise_C1_differentiable_on {0..1}"
  3992     using \<gamma> valid_path_def by blast
  3993   define r where "r = (w - z) / (\<gamma> 0 - z)"
  3994   have [simp]: "r \<noteq> 0"
  3995     using w z by (auto simp: r_def)
  3996   have cont: "continuous_on {0..1}
  3997      (\<lambda>x. Im (integral {0..x} (\<lambda>x. vector_derivative \<gamma> (at x) / (\<gamma> x - z))))"
  3998     by (intro continuous_intros indefinite_integral_continuous_1 winding_number_exp_integral [OF gpd]; simp)
  3999   have "Arg2pi r \<le> 2*pi"
  4000     by (simp add: Arg2pi less_eq_real_def)
  4001   also have "\<dots> \<le> Im (integral {0..1} (\<lambda>x. vector_derivative \<gamma> (at x) / (\<gamma> x - z)))"
  4002     using 1
  4003     apply (simp add: winding_number_valid_path [OF \<gamma> z] contour_integral_integral)
  4004     apply (simp add: Complex.Re_divide field_simps power2_eq_square)
  4005     done
  4006   finally have "Arg2pi r \<le> Im (integral {0..1} (\<lambda>x. vector_derivative \<gamma> (at x) / (\<gamma> x - z)))" .
  4007   then have "\<exists>t. t \<in> {0..1} \<and> Im(integral {0..t} (\<lambda>x. vector_derivative \<gamma> (at x)/(\<gamma> x - z))) = Arg2pi r"
  4008     by (simp add: Arg2pi_ge_0 cont IVT')
  4009   then obtain t where t:     "t \<in> {0..1}"
  4010                   and eqArg: "Im (integral {0..t} (\<lambda>x. vector_derivative \<gamma> (at x)/(\<gamma> x - z))) = Arg2pi r"
  4011     by blast
  4012   define i where "i = integral {0..t} (\<lambda>x. vector_derivative \<gamma> (at x) / (\<gamma> x - z))"
  4013   have iArg: "Arg2pi r = Im i"
  4014     using eqArg by (simp add: i_def)
  4015   have gpdt: "\<gamma> piecewise_C1_differentiable_on {0..t}"
  4016     by (metis atLeastAtMost_iff atLeastatMost_subset_iff order_refl piecewise_C1_differentiable_on_subset gpd t)
  4017   have "exp (- i) * (\<gamma> t - z) = \<gamma> 0 - z"
  4018     unfolding i_def
  4019     apply (rule winding_number_exp_integral [OF gpdt])
  4020     using t z unfolding path_image_def by force+
  4021   then have *: "\<gamma> t - z = exp i * (\<gamma> 0 - z)"
  4022     by (simp add: exp_minus field_simps)
  4023   then have "(w - z) = r * (\<gamma> 0 - z)"
  4024     by (simp add: r_def)
  4025   then have "z + complex_of_real (exp (Re i)) * (w - z) / complex_of_real (cmod r) = \<gamma> t"
  4026     apply simp
  4027     apply (subst Complex_Transcendental.Arg2pi_eq [of r])
  4028     apply (simp add: iArg)
  4029     using * apply (simp add: exp_eq_polar field_simps)
  4030     done
  4031   with t show ?thesis
  4032     by (rule_tac x="exp(Re i) / norm r" in exI) (auto simp: path_image_def)
  4033 qed
  4034 
  4035 lemma winding_number_big_meets:
  4036   fixes z::complex
  4037   assumes \<gamma>: "valid_path \<gamma>" and z: "z \<notin> path_image \<gamma>" and "\<bar>Re (winding_number \<gamma> z)\<bar> \<ge> 1"
  4038       and w: "w \<noteq> z"
  4039   shows "\<exists>a::real. 0 < a \<and> z + a*(w - z) \<in> path_image \<gamma>"
  4040 proof -
  4041   { assume "Re (winding_number \<gamma> z) \<le> - 1"
  4042     then have "Re (winding_number (reversepath \<gamma>) z) \<ge> 1"
  4043       by (simp add: \<gamma> valid_path_imp_path winding_number_reversepath z)
  4044     moreover have "valid_path (reversepath \<gamma>)"
  4045       using \<gamma> valid_path_imp_reverse by auto
  4046     moreover have "z \<notin> path_image (reversepath \<gamma>)"
  4047       by (simp add: z)
  4048     ultimately have "\<exists>a::real. 0 < a \<and> z + a*(w - z) \<in> path_image (reversepath \<gamma>)"
  4049       using winding_number_pos_meets w by blast
  4050     then have ?thesis
  4051       by simp
  4052   }
  4053   then show ?thesis
  4054     using assms
  4055     by (simp add: abs_if winding_number_pos_meets split: if_split_asm)
  4056 qed
  4057 
  4058 lemma winding_number_less_1:
  4059   fixes z::complex
  4060   shows
  4061   "\<lbrakk>valid_path \<gamma>; z \<notin> path_image \<gamma>; w \<noteq> z;
  4062     \<And>a::real. 0 < a \<Longrightarrow> z + a*(w - z) \<notin> path_image \<gamma>\<rbrakk>
  4063    \<Longrightarrow> Re(winding_number \<gamma> z) < 1"
  4064    by (auto simp: not_less dest: winding_number_big_meets)
  4065 
  4066 text\<open>One way of proving that WN=1 for a loop.\<close>
  4067 lemma winding_number_eq_1:
  4068   fixes z::complex
  4069   assumes \<gamma>: "valid_path \<gamma>" and z: "z \<notin> path_image \<gamma>" and loop: "pathfinish \<gamma> = pathstart \<gamma>"
  4070       and 0: "0 < Re(winding_number \<gamma> z)" and 2: "Re(winding_number \<gamma> z) < 2"
  4071   shows "winding_number \<gamma> z = 1"
  4072 proof -
  4073   have "winding_number \<gamma> z \<in> Ints"
  4074     by (simp add: \<gamma> integer_winding_number loop valid_path_imp_path z)
  4075   then show ?thesis
  4076     using 0 2 by (auto simp: Ints_def)
  4077 qed
  4078 
  4079 subsection\<open>Continuity of winding number and invariance on connected sets\<close>
  4080 
  4081 lemma continuous_at_winding_number:
  4082   fixes z::complex
  4083   assumes \<gamma>: "path \<gamma>" and z: "z \<notin> path_image \<gamma>"
  4084   shows "continuous (at z) (winding_number \<gamma>)"
  4085 proof -
  4086   obtain e where "e>0" and cbg: "cball z e \<subseteq> - path_image \<gamma>"
  4087     using open_contains_cball [of "- path_image \<gamma>"]  z
  4088     by (force simp: closed_def [symmetric] closed_path_image [OF \<gamma>])
  4089   then have ppag: "path_image \<gamma> \<subseteq> - cball z (e/2)"
  4090     by (force simp: cball_def dist_norm)
  4091   have oc: "open (- cball z (e / 2))"
  4092     by (simp add: closed_def [symmetric])
  4093   obtain d where "d>0" and pi_eq:
  4094     "\<And>h1 h2. \<lbrakk>valid_path h1; valid_path h2;
  4095               (\<forall>t\<in>{0..1}. cmod (h1 t - \<gamma> t) < d \<and> cmod (h2 t - \<gamma> t) < d);
  4096               pathstart h2 = pathstart h1; pathfinish h2 = pathfinish h1\<rbrakk>
  4097              \<Longrightarrow>
  4098                path_image h1 \<subseteq> - cball z (e / 2) \<and>
  4099                path_image h2 \<subseteq> - cball z (e / 2) \<and>
  4100                (\<forall>f. f holomorphic_on - cball z (e / 2) \<longrightarrow> contour_integral h2 f = contour_integral h1 f)"
  4101     using contour_integral_nearby_ends [OF oc \<gamma> ppag] by metis
  4102   obtain p where p: "valid_path p" "z \<notin> path_image p"
  4103                     "pathstart p = pathstart \<gamma> \<and> pathfinish p = pathfinish \<gamma>"
  4104               and pg: "\<And>t. t\<in>{0..1} \<Longrightarrow> cmod (\<gamma> t - p t) < min d e / 2"
  4105               and pi: "contour_integral p (\<lambda>x. 1 / (x - z)) = complex_of_real (2 * pi) * \<i> * winding_number \<gamma> z"
  4106     using winding_number [OF \<gamma> z, of "min d e / 2"] \<open>d>0\<close> \<open>e>0\<close> by (auto simp: winding_number_prop_def)
  4107   { fix w
  4108     assume d2: "cmod (w - z) < d/2" and e2: "cmod (w - z) < e/2"
  4109     then have wnotp: "w \<notin> path_image p"
  4110       using cbg \<open>d>0\<close> \<open>e>0\<close>
  4111       apply (simp add: path_image_def cball_def dist_norm, clarify)
  4112       apply (frule pg)
  4113       apply (drule_tac c="\<gamma> x" in subsetD)
  4114       apply (auto simp: less_eq_real_def norm_minus_commute norm_triangle_half_l)
  4115       done
  4116     have wnotg: "w \<notin> path_image \<gamma>"
  4117       using cbg e2 \<open>e>0\<close> by (force simp: dist_norm norm_minus_commute)
  4118     { fix k::real
  4119       assume k: "k>0"
  4120       then obtain q where q: "valid_path q" "w \<notin> path_image q"
  4121                              "pathstart q = pathstart \<gamma> \<and> pathfinish q = pathfinish \<gamma>"
  4122                     and qg: "\<And>t. t \<in> {0..1} \<Longrightarrow> cmod (\<gamma> t - q t) < min k (min d e) / 2"
  4123                     and qi: "contour_integral q (\<lambda>u. 1 / (u - w)) = complex_of_real (2 * pi) * \<i> * winding_number \<gamma> w"
  4124         using winding_number [OF \<gamma> wnotg, of "min k (min d e) / 2"] \<open>d>0\<close> \<open>e>0\<close> k
  4125         by (force simp: min_divide_distrib_right winding_number_prop_def)
  4126       have "contour_integral p (\<lambda>u. 1 / (u - w)) = contour_integral q (\<lambda>u. 1 / (u - w))"
  4127         apply (rule pi_eq [OF \<open>valid_path q\<close> \<open>valid_path p\<close>, THEN conjunct2, THEN conjunct2, rule_format])
  4128         apply (frule pg)
  4129         apply (frule qg)
  4130         using p q \<open>d>0\<close> e2
  4131         apply (auto simp: dist_norm norm_minus_commute intro!: holomorphic_intros)
  4132         done
  4133       then have "contour_integral p (\<lambda>x. 1 / (x - w)) = complex_of_real (2 * pi) * \<i> * winding_number \<gamma> w"
  4134         by (simp add: pi qi)
  4135     } note pip = this
  4136     have "path p"
  4137       using p by (simp add: valid_path_imp_path)
  4138     then have "winding_number p w = winding_number \<gamma> w"
  4139       apply (rule winding_number_unique [OF _ wnotp])
  4140       apply (rule_tac x=p in exI)
  4141       apply (simp add: p wnotp min_divide_distrib_right pip winding_number_prop_def)
  4142       done
  4143   } note wnwn = this
  4144   obtain pe where "pe>0" and cbp: "cball z (3 / 4 * pe) \<subseteq> - path_image p"
  4145     using p open_contains_cball [of "- path_image p"]
  4146     by (force simp: closed_def [symmetric] closed_path_image [OF valid_path_imp_path])
  4147   obtain L
  4148     where "L>0"
  4149       and L: "\<And>f B. \<lbrakk>f holomorphic_on - cball z (3 / 4 * pe);
  4150                       \<forall>z \<in> - cball z (3 / 4 * pe). cmod (f z) \<le> B\<rbrakk> \<Longrightarrow>
  4151                       cmod (contour_integral p f) \<le> L * B"
  4152     using contour_integral_bound_exists [of "- cball z (3/4*pe)" p] cbp \<open>valid_path p\<close> by force
  4153   { fix e::real and w::complex
  4154     assume e: "0 < e" and w: "cmod (w - z) < pe/4" "cmod (w - z) < e * pe\<^sup>2 / (8 * L)"
  4155     then have [simp]: "w \<notin> path_image p"
  4156       using cbp p(2) \<open>0 < pe\<close>
  4157       by (force simp: dist_norm norm_minus_commute path_image_def cball_def)
  4158     have [simp]: "contour_integral p (\<lambda>x. 1/(x - w)) - contour_integral p (\<lambda>x. 1/(x - z)) =
  4159                   contour_integral p (\<lambda>x. 1/(x - w) - 1/(x - z))"
  4160       by (simp add: p contour_integrable_inversediff contour_integral_diff)
  4161     { fix x
  4162       assume pe: "3/4 * pe < cmod (z - x)"
  4163       have "cmod (w - x) < pe/4 + cmod (z - x)"
  4164         by (meson add_less_cancel_right norm_diff_triangle_le order_refl order_trans_rules(21) w(1))
  4165       then have wx: "cmod (w - x) < 4/3 * cmod (z - x)" using pe by simp
  4166       have "cmod (z - x) \<le> cmod (z - w) + cmod (w - x)"
  4167         using norm_diff_triangle_le by blast
  4168       also have "\<dots> < pe/4 + cmod (w - x)"
  4169         using w by (simp add: norm_minus_commute)
  4170       finally have "pe/2 < cmod (w - x)"
  4171         using pe by auto
  4172       then have "(pe/2)^2 < cmod (w - x) ^ 2"
  4173         apply (rule power_strict_mono)
  4174         using \<open>pe>0\<close> by auto
  4175       then have pe2: "pe^2 < 4 * cmod (w - x) ^ 2"
  4176         by (simp add: power_divide)
  4177       have "8 * L * cmod (w - z) < e * pe\<^sup>2"
  4178         using w \<open>L>0\<close> by (simp add: field_simps)
  4179       also have "\<dots> < e * 4 * cmod (w - x) * cmod (w - x)"
  4180         using pe2 \<open>e>0\<close> by (simp add: power2_eq_square)
  4181       also have "\<dots> < e * 4 * cmod (w - x) * (4/3 * cmod (z - x))"
  4182         using wx
  4183         apply (rule mult_strict_left_mono)
  4184         using pe2 e not_less_iff_gr_or_eq by fastforce
  4185       finally have "L * cmod (w - z) < 2/3 * e * cmod (w - x) * cmod (z - x)"
  4186         by simp
  4187       also have "\<dots> \<le> e * cmod (w - x) * cmod (z - x)"
  4188          using e by simp
  4189       finally have Lwz: "L * cmod (w - z) < e * cmod (w - x) * cmod (z - x)" .
  4190       have "L * cmod (1 / (x - w) - 1 / (x - z)) \<le> e"
  4191         apply (cases "x=z \<or> x=w")
  4192         using pe \<open>pe>0\<close> w \<open>L>0\<close>
  4193         apply (force simp: norm_minus_commute)
  4194         using wx w(2) \<open>L>0\<close> pe pe2 Lwz
  4195         apply (auto simp: divide_simps mult_less_0_iff norm_minus_commute norm_divide norm_mult power2_eq_square)
  4196         done
  4197     } note L_cmod_le = this
  4198     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)"
  4199       apply (rule L)
  4200       using \<open>pe>0\<close> w
  4201       apply (force simp: dist_norm norm_minus_commute intro!: holomorphic_intros)
  4202       using \<open>pe>0\<close> w \<open>L>0\<close>
  4203       apply (auto simp: cball_def dist_norm field_simps L_cmod_le  simp del: less_divide_eq_numeral1 le_divide_eq_numeral1)
  4204       done
  4205     have "cmod (contour_integral p (\<lambda>x. 1 / (x - w)) - contour_integral p (\<lambda>x. 1 / (x - z))) < 2*e"
  4206       apply simp
  4207       apply (rule le_less_trans [OF *])
  4208       using \<open>L>0\<close> e
  4209       apply (force simp: field_simps)
  4210       done
  4211     then have "cmod (winding_number p w - winding_number p z) < e"
  4212       using pi_ge_two e
  4213       by (force simp: winding_number_valid_path p field_simps norm_divide norm_mult intro: less_le_trans)
  4214   } note cmod_wn_diff = this
  4215   then have "isCont (winding_number p) z"
  4216     apply (simp add: continuous_at_eps_delta, clarify)
  4217     apply (rule_tac x="min (pe/4) (e/2*pe^2/L/4)" in exI)
  4218     using \<open>pe>0\<close> \<open>L>0\<close>
  4219     apply (simp add: dist_norm cmod_wn_diff)
  4220     done
  4221   then show ?thesis
  4222     apply (rule continuous_transform_within [where d = "min d e / 2"])
  4223     apply (auto simp: \<open>d>0\<close> \<open>e>0\<close> dist_norm wnwn)
  4224     done
  4225 qed
  4226 
  4227 corollary continuous_on_winding_number:
  4228     "path \<gamma> \<Longrightarrow> continuous_on (- path_image \<gamma>) (\<lambda>w. winding_number \<gamma> w)"
  4229   by (simp add: continuous_at_imp_continuous_on continuous_at_winding_number)
  4230 
  4231 subsection%unimportant \<open>The winding number is constant on a connected region\<close>
  4232 
  4233 lemma winding_number_constant:
  4234   assumes \<gamma>: "path \<gamma>" and loop: "pathfinish \<gamma> = pathstart \<gamma>" and cs: "connected S" and sg: "S \<inter> path_image \<gamma> = {}"
  4235   shows "winding_number \<gamma> constant_on S"
  4236 proof -
  4237   have *: "1 \<le> cmod (winding_number \<gamma> y - winding_number \<gamma> z)"
  4238       if ne: "winding_number \<gamma> y \<noteq> winding_number \<gamma> z" and "y \<in> S" "z \<in> S" for y z
  4239   proof -
  4240     have "winding_number \<gamma> y \<in> \<int>"  "winding_number \<gamma> z \<in>  \<int>"
  4241       using that integer_winding_number [OF \<gamma> loop] sg \<open>y \<in> S\<close> by auto
  4242     with ne show ?thesis
  4243       by (auto simp: Ints_def simp flip: of_int_diff)
  4244   qed
  4245   have cont: "continuous_on S (\<lambda>w. winding_number \<gamma> w)"
  4246     using continuous_on_winding_number [OF \<gamma>] sg
  4247     by (meson continuous_on_subset disjoint_eq_subset_Compl)
  4248   show ?thesis
  4249     using "*" zero_less_one
  4250     by (blast intro: continuous_discrete_range_constant [OF cs cont])
  4251 qed
  4252 
  4253 lemma winding_number_eq:
  4254      "\<lbrakk>path \<gamma>; pathfinish \<gamma> = pathstart \<gamma>; w \<in> S; z \<in> S; connected S; S \<inter> path_image \<gamma> = {}\<rbrakk>
  4255       \<Longrightarrow> winding_number \<gamma> w = winding_number \<gamma> z"
  4256   using winding_number_constant by (metis constant_on_def)
  4257 
  4258 lemma open_winding_number_levelsets:
  4259   assumes \<gamma>: "path \<gamma>" and loop: "pathfinish \<gamma> = pathstart \<gamma>"
  4260     shows "open {z. z \<notin> path_image \<gamma> \<and> winding_number \<gamma> z = k}"
  4261 proof -
  4262   have opn: "open (- path_image \<gamma>)"
  4263     by (simp add: closed_path_image \<gamma> open_Compl)
  4264   { fix z assume z: "z \<notin> path_image \<gamma>" and k: "k = winding_number \<gamma> z"
  4265     obtain e where e: "e>0" "ball z e \<subseteq> - path_image \<gamma>"
  4266       using open_contains_ball [of "- path_image \<gamma>"] opn z
  4267       by blast
  4268     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"
  4269       apply (rule_tac x=e in exI)
  4270       using e apply (simp add: dist_norm ball_def norm_minus_commute)
  4271       apply (auto simp: dist_norm norm_minus_commute intro!: winding_number_eq [OF assms, where S = "ball z e"])
  4272       done
  4273   } then
  4274   show ?thesis
  4275     by (auto simp: open_dist)
  4276 qed
  4277 
  4278 subsection\<open>Winding number is zero "outside" a curve, in various senses\<close>
  4279 
  4280 proposition winding_number_zero_in_outside:
  4281   assumes \<gamma>: "path \<gamma>" and loop: "pathfinish \<gamma> = pathstart \<gamma>" and z: "z \<in> outside (path_image \<gamma>)"
  4282     shows "winding_number \<gamma> z = 0"
  4283 proof -
  4284   obtain B::real where "0 < B" and B: "path_image \<gamma> \<subseteq> ball 0 B"
  4285     using bounded_subset_ballD [OF bounded_path_image [OF \<gamma>]] by auto
  4286   obtain w::complex where w: "w \<notin> ball 0 (B + 1)"
  4287     by (metis abs_of_nonneg le_less less_irrefl mem_ball_0 norm_of_real)
  4288   have "- ball 0 (B + 1) \<subseteq> outside (path_image \<gamma>)"
  4289     apply (rule outside_subset_convex)
  4290     using B subset_ball by auto
  4291   then have wout: "w \<in> outside (path_image \<gamma>)"
  4292     using w by blast
  4293   moreover have "winding_number \<gamma> constant_on outside (path_image \<gamma>)"
  4294     using winding_number_constant [OF \<gamma> loop, of "outside(path_image \<gamma>)"] connected_outside
  4295     by (metis DIM_complex bounded_path_image dual_order.refl \<gamma> outside_no_overlap)
  4296   ultimately have "winding_number \<gamma> z = winding_number \<gamma> w"
  4297     by (metis (no_types, hide_lams) constant_on_def z)
  4298   also have "\<dots> = 0"
  4299   proof -
  4300     have wnot: "w \<notin> path_image \<gamma>"  using wout by (simp add: outside_def)
  4301     { fix e::real assume "0<e"
  4302       obtain p where p: "polynomial_function p" "pathstart p = pathstart \<gamma>" "pathfinish p = pathfinish \<gamma>"
  4303                  and pg1: "(\<And>t. \<lbrakk>0 \<le> t; t \<le> 1\<rbrakk> \<Longrightarrow> cmod (p t - \<gamma> t) < 1)"
  4304                  and pge: "(\<And>t. \<lbrakk>0 \<le> t; t \<le> 1\<rbrakk> \<Longrightarrow> cmod (p t - \<gamma> t) < e)"
  4305         using path_approx_polynomial_function [OF \<gamma>, of "min 1 e"] \<open>e>0\<close> by force
  4306       have pip: "path_image p \<subseteq> ball 0 (B + 1)"
  4307         using B
  4308         apply (clarsimp simp add: path_image_def dist_norm ball_def)
  4309         apply (frule (1) pg1)
  4310         apply (fastforce dest: norm_add_less)
  4311         done
  4312       then have "w \<notin> path_image p"  using w by blast
  4313       then have "\<exists>p. valid_path p \<and> w \<notin> path_image p \<and>
  4314                      pathstart p = pathstart \<gamma> \<and> pathfinish p = pathfinish \<gamma> \<and>
  4315                      (\<forall>t\<in>{0..1}. cmod (\<gamma> t - p t) < e) \<and> contour_integral p (\<lambda>wa. 1 / (wa - w)) = 0"
  4316         apply (rule_tac x=p in exI)
  4317         apply (simp add: p valid_path_polynomial_function)
  4318         apply (intro conjI)
  4319         using pge apply (simp add: norm_minus_commute)
  4320         apply (rule contour_integral_unique [OF Cauchy_theorem_convex_simple [OF _ convex_ball [of 0 "B+1"]]])
  4321         apply (rule holomorphic_intros | simp add: dist_norm)+
  4322         using mem_ball_0 w apply blast
  4323         using p apply (simp_all add: valid_path_polynomial_function loop pip)
  4324         done
  4325     }
  4326     then show ?thesis
  4327       by (auto intro: winding_number_unique [OF \<gamma>] simp add: winding_number_prop_def wnot)
  4328   qed
  4329   finally show ?thesis .
  4330 qed
  4331 
  4332 corollary%unimportant winding_number_zero_const: "a \<noteq> z \<Longrightarrow> winding_number (\<lambda>t. a) z = 0"
  4333   by (rule winding_number_zero_in_outside)
  4334      (auto simp: pathfinish_def pathstart_def path_polynomial_function)
  4335 
  4336 corollary%unimportant winding_number_zero_outside:
  4337     "\<lbrakk>path \<gamma>; convex s; pathfinish \<gamma> = pathstart \<gamma>; z \<notin> s; path_image \<gamma> \<subseteq> s\<rbrakk> \<Longrightarrow> winding_number \<gamma> z = 0"
  4338   by (meson convex_in_outside outside_mono subsetCE winding_number_zero_in_outside)
  4339 
  4340 lemma winding_number_zero_at_infinity:
  4341   assumes \<gamma>: "path \<gamma>" and loop: "pathfinish \<gamma> = pathstart \<gamma>"
  4342     shows "\<exists>B. \<forall>z. B \<le> norm z \<longrightarrow> winding_number \<gamma> z = 0"
  4343 proof -
  4344   obtain B::real where "0 < B" and B: "path_image \<gamma> \<subseteq> ball 0 B"
  4345     using bounded_subset_ballD [OF bounded_path_image [OF \<gamma>]] by auto
  4346   then show ?thesis
  4347     apply (rule_tac x="B+1" in exI, clarify)
  4348     apply (rule winding_number_zero_outside [OF \<gamma> convex_cball [of 0 B] loop])
  4349     apply (meson less_add_one mem_cball_0 not_le order_trans)
  4350     using ball_subset_cball by blast
  4351 qed
  4352 
  4353 lemma winding_number_zero_point:
  4354     "\<lbrakk>path \<gamma>; convex s; pathfinish \<gamma> = pathstart \<gamma>; open s; path_image \<gamma> \<subseteq> s\<rbrakk>
  4355      \<Longrightarrow> \<exists>z. z \<in> s \<and> winding_number \<gamma> z = 0"
  4356   using outside_compact_in_open [of "path_image \<gamma>" s] path_image_nonempty winding_number_zero_in_outside
  4357   by (fastforce simp add: compact_path_image)
  4358 
  4359 
  4360 text\<open>If a path winds round a set, it winds rounds its inside.\<close>
  4361 lemma winding_number_around_inside:
  4362   assumes \<gamma>: "path \<gamma>" and loop: "pathfinish \<gamma> = pathstart \<gamma>"
  4363       and cls: "closed s" and cos: "connected s" and s_disj: "s \<inter> path_image \<gamma> = {}"
  4364       and z: "z \<in> s" and wn_nz: "winding_number \<gamma> z \<noteq> 0" and w: "w \<in> s \<union> inside s"
  4365     shows "winding_number \<gamma> w = winding_number \<gamma> z"
  4366 proof -
  4367   have ssb: "s \<subseteq> inside(path_image \<gamma>)"
  4368   proof
  4369     fix x :: complex
  4370     assume "x \<in> s"
  4371     hence "x \<notin> path_image \<gamma>"
  4372       by (meson disjoint_iff_not_equal s_disj)
  4373     thus "x \<in> inside (path_image \<gamma>)"
  4374       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)
  4375 qed
  4376   show ?thesis
  4377     apply (rule winding_number_eq [OF \<gamma> loop w])
  4378     using z apply blast
  4379     apply (simp add: cls connected_with_inside cos)
  4380     apply (simp add: Int_Un_distrib2 s_disj, safe)
  4381     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)
  4382  qed
  4383 
  4384 
  4385 text\<open>Bounding a WN by 1/2 for a path and point in opposite halfspaces.\<close>
  4386 lemma winding_number_subpath_continuous:
  4387   assumes \<gamma>: "valid_path \<gamma>" and z: "z \<notin> path_image \<gamma>"
  4388     shows "continuous_on {0..1} (\<lambda>x. winding_number(subpath 0 x \<gamma>) z)"
  4389 proof -
  4390   have *: "integral {0..x} (\<lambda>t. vector_derivative \<gamma> (at t) / (\<gamma> t - z)) / (2 * of_real pi * \<i>) =
  4391          winding_number (subpath 0 x \<gamma>) z"
  4392          if x: "0 \<le> x" "x \<le> 1" for x
  4393   proof -
  4394     have "integral {0..x} (\<lambda>t. vector_derivative \<gamma> (at t) / (\<gamma> t - z)) / (2 * of_real pi * \<i>) =
  4395           1 / (2*pi*\<i>) * contour_integral (subpath 0 x \<gamma>) (\<lambda>w. 1/(w - z))"
  4396       using assms x
  4397       apply (simp add: contour_integral_subcontour_integral [OF contour_integrable_inversediff])
  4398       done
  4399     also have "\<dots> = winding_number (subpath 0 x \<gamma>) z"
  4400       apply (subst winding_number_valid_path)
  4401       using assms x
  4402       apply (simp_all add: path_image_subpath valid_path_subpath)
  4403       by (force simp: path_image_def)
  4404     finally show ?thesis .
  4405   qed
  4406   show ?thesis
  4407     apply (rule continuous_on_eq
  4408                  [where f = "\<lambda>x. 1 / (2*pi*\<i>) *
  4409                                  integral {0..x} (\<lambda>t. 1/(\<gamma> t - z) * vector_derivative \<gamma> (at t))"])
  4410     apply (rule continuous_intros)+
  4411     apply (rule indefinite_integral_continuous_1)
  4412     apply (rule contour_integrable_inversediff [OF assms, unfolded contour_integrable_on])
  4413       using assms
  4414     apply (simp add: *)
  4415     done
  4416 qed
  4417 
  4418 lemma winding_number_ivt_pos:
  4419     assumes \<gamma>: "valid_path \<gamma>" and z: "z \<notin> path_image \<gamma>" and "0 \<le> w" "w \<le> Re(winding_number \<gamma> z)"
  4420       shows "\<exists>t \<in> {0..1}. Re(winding_number(subpath 0 t \<gamma>) z) = w"
  4421   apply (rule ivt_increasing_component_on_1 [of 0 1, where ?k = "1::complex", simplified complex_inner_1_right], simp)
  4422   apply (rule winding_number_subpath_continuous [OF \<gamma> z])
  4423   using assms
  4424   apply (auto simp: path_image_def image_def)
  4425   done
  4426 
  4427 lemma winding_number_ivt_neg:
  4428     assumes \<gamma>: "valid_path \<gamma>" and z: "z \<notin> path_image \<gamma>" and "Re(winding_number \<gamma> z) \<le> w" "w \<le> 0"
  4429       shows "\<exists>t \<in> {0..1}. Re(winding_number(subpath 0 t \<gamma>) z) = w"
  4430   apply (rule ivt_decreasing_component_on_1 [of 0 1, where ?k = "1::complex", simplified complex_inner_1_right], simp)
  4431   apply (rule winding_number_subpath_continuous [OF \<gamma> z])
  4432   using assms
  4433   apply (auto simp: path_image_def image_def)
  4434   done
  4435 
  4436 lemma winding_number_ivt_abs:
  4437     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>"
  4438       shows "\<exists>t \<in> {0..1}. \<bar>Re (winding_number (subpath 0 t \<gamma>) z)\<bar> = w"
  4439   using assms winding_number_ivt_pos [of \<gamma> z w] winding_number_ivt_neg [of \<gamma> z "-w"]
  4440   by force
  4441 
  4442 lemma winding_number_lt_half_lemma:
  4443   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}"
  4444     shows "Re(winding_number \<gamma> z) < 1/2"
  4445 proof -
  4446   { assume "Re(winding_number \<gamma> z) \<ge> 1/2"
  4447     then obtain t::real where t: "0 \<le> t" "t \<le> 1" and sub12: "Re (winding_number (subpath 0 t \<gamma>) z) = 1/2"
  4448       using winding_number_ivt_pos [OF \<gamma> z, of "1/2"] by auto
  4449     have gt: "\<gamma> t - z = - (of_real (exp (- (2 * pi * Im (winding_number (subpath 0 t \<gamma>) z)))) * (\<gamma> 0 - z))"
  4450       using winding_number_exp_2pi [of "subpath 0 t \<gamma>" z]
  4451       apply (simp add: t \<gamma> valid_path_imp_path)
  4452       using closed_segment_eq_real_ivl path_image_def t z by (fastforce simp: path_image_subpath Euler sub12)
  4453     have "b < a \<bullet> \<gamma> 0"
  4454     proof -
  4455       have "\<gamma> 0 \<in> {c. b < a \<bullet> c}"
  4456         by (metis (no_types) pag atLeastAtMost_iff image_subset_iff order_refl path_image_def zero_le_one)
  4457       thus ?thesis
  4458         by blast
  4459     qed
  4460     moreover have "b < a \<bullet> \<gamma> t"
  4461     proof -
  4462       have "\<gamma> t \<in> {c. b < a \<bullet> c}"
  4463         by (metis (no_types) pag atLeastAtMost_iff image_subset_iff path_image_def t)
  4464       thus ?thesis
  4465         by blast
  4466     qed
  4467     ultimately have "0 < a \<bullet> (\<gamma> 0 - z)" "0 < a \<bullet> (\<gamma> t - z)" using az
  4468       by (simp add: inner_diff_right)+
  4469     then have False
  4470       by (simp add: gt inner_mult_right mult_less_0_iff)
  4471   }
  4472   then show ?thesis by force
  4473 qed
  4474 
  4475 lemma winding_number_lt_half:
  4476   assumes "valid_path \<gamma>" "a \<bullet> z \<le> b" "path_image \<gamma> \<subseteq> {w. a \<bullet> w > b}"
  4477     shows "\<bar>Re (winding_number \<gamma> z)\<bar> < 1/2"
  4478 proof -
  4479   have "z \<notin> path_image \<gamma>" using assms by auto
  4480   with assms show ?thesis
  4481     apply (simp add: winding_number_lt_half_lemma abs_if del: less_divide_eq_numeral1)
  4482     apply (metis complex_inner_1_right winding_number_lt_half_lemma [OF valid_path_imp_reverse, of \<gamma> z a b]
  4483                  winding_number_reversepath valid_path_imp_path inner_minus_left path_image_reversepath)
  4484     done
  4485 qed
  4486 
  4487 lemma winding_number_le_half:
  4488   assumes \<gamma>: "valid_path \<gamma>" and z: "z \<notin> path_image \<gamma>"
  4489       and anz: "a \<noteq> 0" and azb: "a \<bullet> z \<le> b" and pag: "path_image \<gamma> \<subseteq> {w. a \<bullet> w \<ge> b}"
  4490     shows "\<bar>Re (winding_number \<gamma> z)\<bar> \<le> 1/2"
  4491 proof -
  4492   { assume wnz_12: "\<bar>Re (winding_number \<gamma> z)\<bar> > 1/2"
  4493     have "isCont (winding_number \<gamma>) z"
  4494       by (metis continuous_at_winding_number valid_path_imp_path \<gamma> z)
  4495     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"
  4496       using continuous_at_eps_delta wnz_12 diff_gt_0_iff_gt by blast
  4497     define z' where "z' = z - (d / (2 * cmod a)) *\<^sub>R a"
  4498     have *: "a \<bullet> z' \<le> b - d / 3 * cmod a"
  4499       unfolding z'_def inner_mult_right' divide_inverse
  4500       apply (simp add: divide_simps algebra_simps dot_square_norm power2_eq_square anz)
  4501       apply (metis \<open>0 < d\<close> add_increasing azb less_eq_real_def mult_nonneg_nonneg mult_right_mono norm_ge_zero norm_numeral)
  4502       done
  4503     have "cmod (winding_number \<gamma> z' - winding_number \<gamma> z) < \<bar>Re (winding_number \<gamma> z)\<bar> - 1/2"
  4504       using d [of z'] anz \<open>d>0\<close> by (simp add: dist_norm z'_def)
  4505     then have "1/2 < \<bar>Re (winding_number \<gamma> z)\<bar> - cmod (winding_number \<gamma> z' - winding_number \<gamma> z)"
  4506       by simp
  4507     then have "1/2 < \<bar>Re (winding_number \<gamma> z)\<bar> - \<bar>Re (winding_number \<gamma> z') - Re (winding_number \<gamma> z)\<bar>"
  4508       using abs_Re_le_cmod [of "winding_number \<gamma> z' - winding_number \<gamma> z"] by simp
  4509     then have wnz_12': "\<bar>Re (winding_number \<gamma> z')\<bar> > 1/2"
  4510       by linarith
  4511     moreover have "\<bar>Re (winding_number \<gamma> z')\<bar> < 1/2"
  4512       apply (rule winding_number_lt_half [OF \<gamma> *])
  4513       using azb \<open>d>0\<close> pag
  4514       apply (auto simp: add_strict_increasing anz divide_simps algebra_simps dest!: subsetD)
  4515       done
  4516     ultimately have False
  4517       by simp
  4518   }
  4519   then show ?thesis by force
  4520 qed
  4521 
  4522 lemma winding_number_lt_half_linepath: "z \<notin> closed_segment a b \<Longrightarrow> \<bar>Re (winding_number (linepath a b) z)\<bar> < 1/2"
  4523   using separating_hyperplane_closed_point [of "closed_segment a b" z]
  4524   apply auto
  4525   apply (simp add: closed_segment_def)
  4526   apply (drule less_imp_le)
  4527   apply (frule winding_number_lt_half [OF valid_path_linepath [of a b]])
  4528   apply (auto simp: segment)
  4529   done
  4530 
  4531 
  4532 text\<open> Positivity of WN for a linepath.\<close>
  4533 lemma winding_number_linepath_pos_lt:
  4534     assumes "0 < Im ((b - a) * cnj (b - z))"
  4535       shows "0 < Re(winding_number(linepath a b) z)"
  4536 proof -
  4537   have z: "z \<notin> path_image (linepath a b)"
  4538     using assms
  4539     by (simp add: closed_segment_def) (force simp: algebra_simps)
  4540   show ?thesis
  4541     apply (rule winding_number_pos_lt [OF valid_path_linepath z assms])
  4542     apply (simp add: linepath_def algebra_simps)
  4543     done
  4544 qed
  4545 
  4546 
  4547 subsection\<open>Cauchy's integral formula, again for a convex enclosing set\<close>
  4548 
  4549 lemma Cauchy_integral_formula_weak:
  4550     assumes s: "convex s" and "finite k" and conf: "continuous_on s f"
  4551         and fcd: "(\<And>x. x \<in> interior s - k \<Longrightarrow> f field_differentiable at x)"
  4552         and z: "z \<in> interior s - k" and vpg: "valid_path \<gamma>"
  4553         and pasz: "path_image \<gamma> \<subseteq> s - {z}" and loop: "pathfinish \<gamma> = pathstart \<gamma>"
  4554       shows "((\<lambda>w. f w / (w - z)) has_contour_integral (2*pi * \<i> * winding_number \<gamma> z * f z)) \<gamma>"
  4555 proof -
  4556   obtain f' where f': "(f has_field_derivative f') (at z)"
  4557     using fcd [OF z] by (auto simp: field_differentiable_def)
  4558   have pas: "path_image \<gamma> \<subseteq> s" and znotin: "z \<notin> path_image \<gamma>" using pasz by blast+
  4559   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
  4560   proof (cases "x = z")
  4561     case True then show ?thesis
  4562       apply (simp add: continuous_within)
  4563       apply (rule Lim_transform_away_within [of _ "z+1" _ "\<lambda>w::complex. (f w - f z)/(w - z)"])
  4564       using has_field_derivative_at_within has_field_derivative_iff f'
  4565       apply (fastforce simp add:)+
  4566       done
  4567   next
  4568     case False
  4569     then have dxz: "dist x z > 0" by auto
  4570     have cf: "continuous (at x within s) f"
  4571       using conf continuous_on_eq_continuous_within that by blast
  4572     have "continuous (at x within s) (\<lambda>w. (f w - f z) / (w - z))"
  4573       by (rule cf continuous_intros | simp add: False)+
  4574     then show ?thesis
  4575       apply (rule continuous_transform_within [OF _ dxz that, of "\<lambda>w::complex. (f w - f z)/(w - z)"])
  4576       apply (force simp: dist_commute)
  4577       done
  4578   qed
  4579   have fink': "finite (insert z k)" using \<open>finite k\<close> by blast
  4580   have *: "((\<lambda>w. if w = z then f' else (f w - f z) / (w - z)) has_contour_integral 0) \<gamma>"
  4581     apply (rule Cauchy_theorem_convex [OF _ s fink' _ vpg pas loop])
  4582     using c apply (force simp: continuous_on_eq_continuous_within)
  4583     apply (rename_tac w)
  4584     apply (rule_tac d="dist w z" and f = "\<lambda>w. (f w - f z)/(w - z)" in field_differentiable_transform_within)
  4585     apply (simp_all add: dist_pos_lt dist_commute)
  4586     apply (metis less_irrefl)
  4587     apply (rule derivative_intros fcd | simp)+
  4588     done
  4589   show ?thesis
  4590     apply (rule has_contour_integral_eq)
  4591     using znotin has_contour_integral_add [OF has_contour_integral_lmul [OF has_contour_integral_winding_number [OF vpg znotin], of "f z"] *]
  4592     apply (auto simp: mult_ac divide_simps)
  4593     done
  4594 qed
  4595 
  4596 theorem Cauchy_integral_formula_convex_simple:
  4597     "\<lbrakk>convex s; f holomorphic_on s; z \<in> interior s; valid_path \<gamma>; path_image \<gamma> \<subseteq> s - {z};
  4598       pathfinish \<gamma> = pathstart \<gamma>\<rbrakk>
  4599      \<Longrightarrow> ((\<lambda>w. f w / (w - z)) has_contour_integral (2*pi * \<i> * winding_number \<gamma> z * f z)) \<gamma>"
  4600   apply (rule Cauchy_integral_formula_weak [where k = "{}"])
  4601   using holomorphic_on_imp_continuous_on
  4602   by auto (metis at_within_interior holomorphic_on_def interiorE subsetCE)
  4603 
  4604 subsection\<open>Homotopy forms of Cauchy's theorem\<close>
  4605 
  4606 lemma Cauchy_theorem_homotopic:
  4607     assumes hom: "if atends then homotopic_paths s g h else homotopic_loops s g h"
  4608         and "open s" and f: "f holomorphic_on s"
  4609         and vpg: "valid_path g" and vph: "valid_path h"
  4610     shows "contour_integral g f = contour_integral h f"
  4611 proof -
  4612   have pathsf: "linked_paths atends g h"
  4613     using hom  by (auto simp: linked_paths_def homotopic_paths_imp_pathstart homotopic_paths_imp_pathfinish homotopic_loops_imp_loop)
  4614   obtain k :: "real \<times> real \<Rightarrow> complex"
  4615     where contk: "continuous_on ({0..1} \<times> {0..1}) k"
  4616       and ks: "k ` ({0..1} \<times> {0..1}) \<subseteq> s"
  4617       and k [simp]: "\<forall>x. k (0, x) = g x" "\<forall>x. k (1, x) = h x"
  4618       and ksf: "\<forall>t\<in>{0..1}. linked_paths atends g (\<lambda>x. k (t, x))"
  4619       using hom pathsf by (auto simp: linked_paths_def homotopic_paths_def homotopic_loops_def homotopic_with_def split: if_split_asm)
  4620   have ucontk: "uniformly_continuous_on ({0..1} \<times> {0..1}) k"
  4621     by (blast intro: compact_Times compact_uniformly_continuous [OF contk])
  4622   { fix t::real assume t: "t \<in> {0..1}"
  4623     have pak: "path (k \<circ> (\<lambda>u. (t, u)))"
  4624       unfolding path_def
  4625       apply (rule continuous_intros continuous_on_subset [OF contk])+
  4626       using t by force
  4627     have pik: "path_image (k \<circ> Pair t) \<subseteq> s"
  4628       using ks t by (auto simp: path_image_def)
  4629     obtain e where "e>0" and e:
  4630          "\<And>g h. \<lbrakk>valid_path g; valid_path h;
  4631                   \<forall>u\<in>{0..1}. cmod (g u - (k \<circ> Pair t) u) < e \<and> cmod (h u - (k \<circ> Pair t) u) < e;
  4632                   linked_paths atends g h\<rbrakk>
  4633                  \<Longrightarrow> contour_integral h f = contour_integral g f"
  4634       using contour_integral_nearby [OF \<open>open s\<close> pak pik, of atends] f by metis
  4635     obtain d where "d>0" and d:
  4636         "\<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"
  4637       by (rule uniformly_continuous_onE [OF ucontk, of "e/4"]) (auto simp: dist_norm \<open>e>0\<close>)
  4638     { fix t1 t2
  4639       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"
  4640       have no2: "\<And>g1 k1 kt. \<lbrakk>norm(g1 - k1) < e/4; norm(k1 - kt) < e/4\<rbrakk> \<Longrightarrow> norm(g1 - kt) < e"
  4641         using \<open>e > 0\<close>
  4642         apply (rule_tac y = k1 in norm_triangle_half_l)
  4643         apply (auto simp: norm_minus_commute intro: order_less_trans)
  4644         done
  4645       have "\<exists>d>0. \<forall>g1 g2. valid_path g1 \<and> valid_path g2 \<and>
  4646                           (\<forall>u\<in>{0..1}. cmod (g1 u - k (t1, u)) < d \<and> cmod (g2 u - k (t2, u)) < d) \<and>
  4647                           linked_paths atends g1 g2 \<longrightarrow>
  4648                           contour_integral g2 f = contour_integral g1 f"
  4649         apply (rule_tac x="e/4" in exI)
  4650         using t t1 t2 ltd \<open>e > 0\<close>
  4651         apply (auto intro!: e simp: d no2 simp del: less_divide_eq_numeral1)
  4652         done
  4653     }
  4654     then have "\<exists>e. 0 < e \<and>
  4655               (\<forall>t1 t2. t1 \<in> {0..1} \<and> t2 \<in> {0..1} \<and> \<bar>t1 - t\<bar> < e \<and> \<bar>t2 - t\<bar> < e
  4656                 \<longrightarrow> (\<exists>d. 0 < d \<and>
  4657                      (\<forall>g1 g2. valid_path g1 \<and> valid_path g2 \<and>
  4658                        (\<forall>u \<in> {0..1}.
  4659                           norm(g1 u - k((t1,u))) < d \<and> norm(g2 u - k((t2,u))) < d) \<and>
  4660                           linked_paths atends g1 g2
  4661                           \<longrightarrow> contour_integral g2 f = contour_integral g1 f)))"
  4662       by (rule_tac x=d in exI) (simp add: \<open>d > 0\<close>)
  4663   }
  4664   then obtain ee where ee:
  4665        "\<And>t. t \<in> {0..1} \<Longrightarrow> ee t > 0 \<and>
  4666           (\<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
  4667             \<longrightarrow> (\<exists>d. 0 < d \<and>
  4668                  (\<forall>g1 g2. valid_path g1 \<and> valid_path g2 \<and>
  4669                    (\<forall>u \<in> {0..1}.
  4670                       norm(g1 u - k((t1,u))) < d \<and> norm(g2 u - k((t2,u))) < d) \<and>
  4671                       linked_paths atends g1 g2
  4672                       \<longrightarrow> contour_integral g2 f = contour_integral g1 f)))"
  4673     by metis
  4674   note ee_rule = ee [THEN conjunct2, rule_format]
  4675   define C where "C = (\<lambda>t. ball t (ee t / 3)) ` {0..1}"
  4676   obtain C' where C': "C' \<subseteq> C" "finite C'" and C'01: "{0..1} \<subseteq> \<Union>C'"
  4677   proof (rule compactE [OF compact_interval])
  4678     show "{0..1} \<subseteq> \<Union>C"
  4679       using ee [THEN conjunct1] by (auto simp: C_def dist_norm)
  4680   qed (use C_def in auto)
  4681   define kk where "kk = {t \<in> {0..1}. ball t (ee t / 3) \<in> C'}"
  4682   have kk01: "kk \<subseteq> {0..1}" by (auto simp: kk_def)
  4683   define e where "e = Min (ee ` kk)"
  4684   have C'_eq: "C' = (\<lambda>t. ball t (ee t / 3)) ` kk"
  4685     using C' by (auto simp: kk_def C_def)
  4686   have ee_pos[simp]: "\<And>t. t \<in> {0..1} \<Longrightarrow> ee t > 0"
  4687     by (simp add: kk_def ee)
  4688   moreover have "finite kk"
  4689     using \<open>finite C'\<close> kk01 by (force simp: C'_eq inj_on_def ball_eq_ball_iff dest: ee_pos finite_imageD)
  4690   moreover have "kk \<noteq> {}" using \<open>{0..1} \<subseteq> \<Union>C'\<close> C'_eq by force
  4691   ultimately have "e > 0"
  4692     using finite_less_Inf_iff [of "ee ` kk" 0] kk01 by (force simp: e_def)
  4693   then obtain N::nat where "N > 0" and N: "1/N < e/3"
  4694     by (meson divide_pos_pos nat_approx_posE zero_less_Suc zero_less_numeral)
  4695   have e_le_ee: "\<And>i. i \<in> kk \<Longrightarrow> e \<le> ee i"
  4696     using \<open>finite kk\<close> by (simp add: e_def Min_le_iff [of "ee ` kk"])
  4697   have plus: "\<exists>t \<in> kk. x \<in> ball t (ee t / 3)" if "x \<in> {0..1}" for x
  4698     using C' subsetD [OF C'01 that]  unfolding C'_eq by blast
  4699   have [OF order_refl]:
  4700       "\<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
  4701                         \<longrightarrow> contour_integral j f = contour_integral g f)"
  4702        if "n \<le> N" for n
  4703   using that
  4704   proof (induct n)
  4705     case 0 show ?case using ee_rule [of 0 0 0]
  4706       apply clarsimp
  4707       apply (rule_tac x=d in exI, safe)
  4708       by (metis diff_self vpg norm_zero)
  4709   next
  4710     case (Suc n)
  4711     then have N01: "n/N \<in> {0..1}" "(Suc n)/N \<in> {0..1}"  by auto
  4712     then obtain t where t: "t \<in> kk" "n/N \<in> ball t (ee t / 3)"
  4713       using plus [of "n/N"] by blast
  4714     then have nN_less: "\<bar>n/N - t\<bar> < ee t"
  4715       by (simp add: dist_norm del: less_divide_eq_numeral1)
  4716     have n'N_less: "\<bar>real (Suc n) / real N - t\<bar> < ee t"
  4717       using t N \<open>N > 0\<close> e_le_ee [of t]
  4718       by (simp add: dist_norm add_divide_distrib abs_diff_less_iff del: less_divide_eq_numeral1) (simp add: field_simps)
  4719     have t01: "t \<in> {0..1}" using \<open>kk \<subseteq> {0..1}\<close> \<open>t \<in> kk\<close> by blast
  4720     obtain d1 where "d1 > 0" and d1:
  4721         "\<And>g1 g2. \<lbrakk>valid_path g1; valid_path g2;
  4722                    \<forall>u\<in>{0..1}. cmod (g1 u - k (n/N, u)) < d1 \<and> cmod (g2 u - k ((Suc n) / N, u)) < d1;
  4723                    linked_paths atends g1 g2\<rbrakk>
  4724                    \<Longrightarrow> contour_integral g2 f = contour_integral g1 f"
  4725       using ee [THEN conjunct2, rule_format, OF t01 N01 nN_less n'N_less] by fastforce
  4726     have "n \<le> N" using Suc.prems by auto
  4727     with Suc.hyps
  4728     obtain d2 where "d2 > 0"
  4729       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>
  4730                      \<Longrightarrow> contour_integral j f = contour_integral g f"
  4731         by auto
  4732     have "continuous_on {0..1} (k \<circ> (\<lambda>u. (n/N, u)))"
  4733       apply (rule continuous_intros continuous_on_subset [OF contk])+
  4734       using N01 by auto
  4735     then have pkn: "path (\<lambda>u. k (n/N, u))"
  4736       by (simp add: path_def)
  4737     have min12: "min d1 d2 > 0" by (simp add: \<open>0 < d1\<close> \<open>0 < d2\<close>)
  4738     obtain p where "polynomial_function p"
  4739         and psf: "pathstart p = pathstart (\<lambda>u. k (n/N, u))"
  4740                  "pathfinish p = pathfinish (\<lambda>u. k (n/N, u))"
  4741         and pk_le:  "\<And>t. t\<in>{0..1} \<Longrightarrow> cmod (p t - k (n/N, t)) < min d1 d2"
  4742       using path_approx_polynomial_function [OF pkn min12] by blast
  4743     then have vpp: "valid_path p" using valid_path_polynomial_function by blast
  4744     have lpa: "linked_paths atends g p"
  4745       by (metis (mono_tags, lifting) N01(1) ksf linked_paths_def pathfinish_def pathstart_def psf)
  4746     show ?case
  4747     proof (intro exI; safe)
  4748       fix j
  4749       assume "valid_path j" "linked_paths atends g j"
  4750         and "\<forall>u\<in>{0..1}. cmod (j u - k (real (Suc n) / real N, u)) < min d1 d2"
  4751       then have "contour_integral j f = contour_integral p f"
  4752         using pk_le N01(1) ksf by (force intro!: vpp d1 simp add: linked_paths_def psf)
  4753       also have "... = contour_integral g f"
  4754         using pk_le by (force intro!: vpp d2 lpa)
  4755       finally show "contour_integral j f = contour_integral g f" .
  4756     qed (simp add: \<open>0 < d1\<close> \<open>0 < d2\<close>)
  4757   qed
  4758   then obtain d where "0 < d"
  4759                        "\<And>j. valid_path j \<and> (\<forall>u \<in> {0..1}. norm(j u - k (1,u)) < d) \<and> linked_paths atends g j
  4760                             \<Longrightarrow> contour_integral j f = contour_integral g f"
  4761     using \<open>N>0\<close> by auto
  4762   then have "linked_paths atends g h \<Longrightarrow> contour_integral h f = contour_integral g f"
  4763     using \<open>N>0\<close> vph by fastforce
  4764   then show ?thesis
  4765     by (simp add: pathsf)
  4766 qed
  4767 
  4768 proposition Cauchy_theorem_homotopic_paths:
  4769     assumes hom: "homotopic_paths s g h"
  4770         and "open s" and f: "f holomorphic_on s"
  4771         and vpg: "valid_path g" and vph: "valid_path h"
  4772     shows "contour_integral g f = contour_integral h f"
  4773   using Cauchy_theorem_homotopic [of True s g h] assms by simp
  4774 
  4775 proposition Cauchy_theorem_homotopic_loops:
  4776     assumes hom: "homotopic_loops s g h"
  4777         and "open s" and f: "f holomorphic_on s"
  4778         and vpg: "valid_path g" and vph: "valid_path h"
  4779     shows "contour_integral g f = contour_integral h f"
  4780   using Cauchy_theorem_homotopic [of False s g h] assms by simp
  4781 
  4782 lemma has_contour_integral_newpath:
  4783     "\<lbrakk>(f has_contour_integral y) h; f contour_integrable_on g; contour_integral g f = contour_integral h f\<rbrakk>
  4784      \<Longrightarrow> (f has_contour_integral y) g"
  4785   using has_contour_integral_integral contour_integral_unique by auto
  4786 
  4787 lemma Cauchy_theorem_null_homotopic:
  4788      "\<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"
  4789   apply (rule has_contour_integral_newpath [where h = "linepath a a"], simp)
  4790   using contour_integrable_holomorphic_simple
  4791     apply (blast dest: holomorphic_on_imp_continuous_on homotopic_loops_imp_subset)
  4792   by (simp add: Cauchy_theorem_homotopic_loops)
  4793 
  4794 subsection%unimportant \<open>More winding number properties\<close>
  4795 
  4796 text\<open>including the fact that it's +-1 inside a simple closed curve.\<close>
  4797 
  4798 lemma winding_number_homotopic_paths:
  4799     assumes "homotopic_paths (-{z}) g h"
  4800       shows "winding_number g z = winding_number h z"
  4801 proof -
  4802   have "path g" "path h" using homotopic_paths_imp_path [OF assms] by auto
  4803   moreover have pag: "z \<notin> path_image g" and pah: "z \<notin> path_image h"
  4804     using homotopic_paths_imp_subset [OF assms] by auto
  4805   ultimately obtain d e where "d > 0" "e > 0"
  4806       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>
  4807             \<Longrightarrow> homotopic_paths (-{z}) g p"
  4808       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>
  4809             \<Longrightarrow> homotopic_paths (-{z}) h q"
  4810     using homotopic_nearby_paths [of g "-{z}"] homotopic_nearby_paths [of h "-{z}"] by force
  4811   obtain p where p:
  4812        "valid_path p" "z \<notin> path_image p"
  4813        "pathstart p = pathstart g" "pathfinish p = pathfinish g"
  4814        and gp_less:"\<forall>t\<in>{0..1}. cmod (g t - p t) < d"
  4815        and pap: "contour_integral p (\<lambda>w. 1 / (w - z)) = complex_of_real (2 * pi) * \<i> * winding_number g z"
  4816     using winding_number [OF \<open>path g\<close> pag \<open>0 < d\<close>] unfolding winding_number_prop_def by blast
  4817   obtain q where q:
  4818        "valid_path q" "z \<notin> path_image q"
  4819        "pathstart q = pathstart h" "pathfinish q = pathfinish h"
  4820        and hq_less: "\<forall>t\<in>{0..1}. cmod (h t - q t) < e"
  4821        and paq:  "contour_integral q (\<lambda>w. 1 / (w - z)) = complex_of_real (2 * pi) * \<i> * winding_number h z"
  4822     using winding_number [OF \<open>path h\<close> pah \<open>0 < e\<close>] unfolding winding_number_prop_def by blast
  4823   have "homotopic_paths (- {z}) g p"
  4824     by (simp add: d p valid_path_imp_path norm_minus_commute gp_less)
  4825   moreover have "homotopic_paths (- {z}) h q"
  4826     by (simp add: e q valid_path_imp_path norm_minus_commute hq_less)
  4827   ultimately have "homotopic_paths (- {z}) p q"
  4828     by (blast intro: homotopic_paths_trans homotopic_paths_sym assms)
  4829   then have "contour_integral p (\<lambda>w. 1/(w - z)) = contour_integral q (\<lambda>w. 1/(w - z))"
  4830     by (rule Cauchy_theorem_homotopic_paths) (auto intro!: holomorphic_intros simp: p q)
  4831   then show ?thesis
  4832     by (simp add: pap paq)
  4833 qed
  4834 
  4835 lemma winding_number_homotopic_loops:
  4836     assumes "homotopic_loops (-{z}) g h"
  4837       shows "winding_number g z = winding_number h z"
  4838 proof -
  4839   have "path g" "path h" using homotopic_loops_imp_path [OF assms] by auto
  4840   moreover have pag: "z \<notin> path_image g" and pah: "z \<notin> path_image h"
  4841     using homotopic_loops_imp_subset [OF assms] by auto
  4842   moreover have gloop: "pathfinish g = pathstart g" and hloop: "pathfinish h = pathstart h"
  4843     using homotopic_loops_imp_loop [OF assms] by auto
  4844   ultimately obtain d e where "d > 0" "e > 0"
  4845       and d: "\<And>p. \<lbrakk>path p; pathfinish p = pathstart p; \<forall>t\<in>{0..1}. norm (p t - g t) < d\<rbrakk>
  4846             \<Longrightarrow> homotopic_loops (-{z}) g p"
  4847       and e: "\<And>q. \<lbrakk>path q; pathfinish q = pathstart q; \<forall>t\<in>{0..1}. norm (q t - h t) < e\<rbrakk>
  4848             \<Longrightarrow> homotopic_loops (-{z}) h q"
  4849     using homotopic_nearby_loops [of g "-{z}"] homotopic_nearby_loops [of h "-{z}"] by force
  4850   obtain p where p:
  4851        "valid_path p" "z \<notin> path_image p"
  4852        "pathstart p = pathstart g" "pathfinish p = pathfinish g"
  4853        and gp_less:"\<forall>t\<in>{0..1}. cmod (g t - p t) < d"
  4854        and pap: "contour_integral p (\<lambda>w. 1 / (w - z)) = complex_of_real (2 * pi) * \<i> * winding_number g z"
  4855     using winding_number [OF \<open>path g\<close> pag \<open>0 < d\<close>] unfolding winding_number_prop_def by blast
  4856   obtain q where q:
  4857        "valid_path q" "z \<notin> path_image q"
  4858        "pathstart q = pathstart h" "pathfinish q = pathfinish h"
  4859        and hq_less: "\<forall>t\<in>{0..1}. cmod (h t - q t) < e"
  4860        and paq:  "contour_integral q (\<lambda>w. 1 / (w - z)) = complex_of_real (2 * pi) * \<i> * winding_number h z"
  4861     using winding_number [OF \<open>path h\<close> pah \<open>0 < e\<close>] unfolding winding_number_prop_def by blast
  4862   have gp: "homotopic_loops (- {z}) g p"
  4863     by (simp add: gloop d gp_less norm_minus_commute p valid_path_imp_path)
  4864   have hq: "homotopic_loops (- {z}) h q"
  4865     by (simp add: e hloop hq_less norm_minus_commute q valid_path_imp_path)
  4866   have "contour_integral p (\<lambda>w. 1/(w - z)) = contour_integral q (\<lambda>w. 1/(w - z))"
  4867   proof (rule Cauchy_theorem_homotopic_loops)
  4868     show "homotopic_loops (- {z}) p q"
  4869       by (blast intro: homotopic_loops_trans homotopic_loops_sym gp hq assms)
  4870   qed (auto intro!: holomorphic_intros simp: p q)
  4871   then show ?thesis
  4872     by (simp add: pap paq)
  4873 qed
  4874 
  4875 lemma winding_number_paths_linear_eq:
  4876   "\<lbrakk>path g; path h; pathstart h = pathstart g; pathfinish h = pathfinish g;
  4877     \<And>t. t \<in> {0..1} \<Longrightarrow> z \<notin> closed_segment (g t) (h t)\<rbrakk>
  4878         \<Longrightarrow> winding_number h z = winding_number g z"
  4879   by (blast intro: sym homotopic_paths_linear winding_number_homotopic_paths)
  4880 
  4881 lemma winding_number_loops_linear_eq:
  4882   "\<lbrakk>path g; path h; pathfinish g = pathstart g; pathfinish h = pathstart h;
  4883     \<And>t. t \<in> {0..1} \<Longrightarrow> z \<notin> closed_segment (g t) (h t)\<rbrakk>
  4884         \<Longrightarrow> winding_number h z = winding_number g z"
  4885   by (blast intro: sym homotopic_loops_linear winding_number_homotopic_loops)
  4886 
  4887 lemma winding_number_nearby_paths_eq:
  4888      "\<lbrakk>path g; path h; pathstart h = pathstart g; pathfinish h = pathfinish g;
  4889       \<And>t. t \<in> {0..1} \<Longrightarrow> norm(h t - g t) < norm(g t - z)\<rbrakk>
  4890       \<Longrightarrow> winding_number h z = winding_number g z"
  4891   by (metis segment_bound(2) norm_minus_commute not_le winding_number_paths_linear_eq)
  4892 
  4893 lemma winding_number_nearby_loops_eq:
  4894      "\<lbrakk>path g; path h; pathfinish g = pathstart g; pathfinish h = pathstart h;
  4895       \<And>t. t \<in> {0..1} \<Longrightarrow> norm(h t - g t) < norm(g t - z)\<rbrakk>
  4896       \<Longrightarrow> winding_number h z = winding_number g z"
  4897   by (metis segment_bound(2) norm_minus_commute not_le winding_number_loops_linear_eq)
  4898 
  4899 
  4900 lemma winding_number_subpath_combine:
  4901     "\<lbrakk>path g; z \<notin> path_image g;
  4902       u \<in> {0..1}; v \<in> {0..1}; w \<in> {0..1}\<rbrakk>
  4903       \<Longrightarrow> winding_number (subpath u v g) z + winding_number (subpath v w g) z =
  4904           winding_number (subpath u w g) z"
  4905 apply (rule trans [OF winding_number_join [THEN sym]
  4906                       winding_number_homotopic_paths [OF homotopic_join_subpaths]])
  4907   using path_image_subpath_subset by auto
  4908 
  4909 subsection\<open>Partial circle path\<close>
  4910 
  4911 definition%important part_circlepath :: "[complex, real, real, real, real] \<Rightarrow> complex"
  4912   where "part_circlepath z r s t \<equiv> \<lambda>x. z + of_real r * exp (\<i> * of_real (linepath s t x))"
  4913 
  4914 lemma pathstart_part_circlepath [simp]:
  4915      "pathstart(part_circlepath z r s t) = z + r*exp(\<i> * s)"
  4916 by (metis part_circlepath_def pathstart_def pathstart_linepath)
  4917 
  4918 lemma pathfinish_part_circlepath [simp]:
  4919      "pathfinish(part_circlepath z r s t) = z + r*exp(\<i>*t)"
  4920 by (metis part_circlepath_def pathfinish_def pathfinish_linepath)
  4921 
  4922 lemma reversepath_part_circlepath[simp]:
  4923     "reversepath (part_circlepath z r s t) = part_circlepath z r t s"
  4924   unfolding part_circlepath_def reversepath_def linepath_def 
  4925   by (auto simp:algebra_simps)
  4926     
  4927 lemma has_vector_derivative_part_circlepath [derivative_intros]:
  4928     "((part_circlepath z r s t) has_vector_derivative
  4929       (\<i> * r * (of_real t - of_real s) * exp(\<i> * linepath s t x)))
  4930      (at x within X)"
  4931   apply (simp add: part_circlepath_def linepath_def scaleR_conv_of_real)
  4932   apply (rule has_vector_derivative_real_complex)
  4933   apply (rule derivative_eq_intros | simp)+
  4934   done
  4935 
  4936 lemma differentiable_part_circlepath:
  4937   "part_circlepath c r a b differentiable at x within A"
  4938   using has_vector_derivative_part_circlepath[of c r a b x A] differentiableI_vector by blast
  4939 
  4940 lemma vector_derivative_part_circlepath:
  4941     "vector_derivative (part_circlepath z r s t) (at x) =
  4942        \<i> * r * (of_real t - of_real s) * exp(\<i> * linepath s t x)"
  4943   using has_vector_derivative_part_circlepath vector_derivative_at by blast
  4944 
  4945 lemma vector_derivative_part_circlepath01:
  4946     "\<lbrakk>0 \<le> x; x \<le> 1\<rbrakk>
  4947      \<Longrightarrow> vector_derivative (part_circlepath z r s t) (at x within {0..1}) =
  4948           \<i> * r * (of_real t - of_real s) * exp(\<i> * linepath s t x)"
  4949   using has_vector_derivative_part_circlepath
  4950   by (auto simp: vector_derivative_at_within_ivl)
  4951 
  4952 lemma valid_path_part_circlepath [simp]: "valid_path (part_circlepath z r s t)"
  4953   apply (simp add: valid_path_def)
  4954   apply (rule C1_differentiable_imp_piecewise)
  4955   apply (auto simp: C1_differentiable_on_eq vector_derivative_works vector_derivative_part_circlepath has_vector_derivative_part_circlepath
  4956               intro!: continuous_intros)
  4957   done
  4958 
  4959 lemma path_part_circlepath [simp]: "path (part_circlepath z r s t)"
  4960   by (simp add: valid_path_imp_path)
  4961 
  4962 proposition path_image_part_circlepath:
  4963   assumes "s \<le> t"
  4964     shows "path_image (part_circlepath z r s t) = {z + r * exp(\<i> * of_real x) | x. s \<le> x \<and> x \<le> t}"
  4965 proof -
  4966   { fix z::real
  4967     assume "0 \<le> z" "z \<le> 1"
  4968     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"
  4969       apply (rule_tac x="(1 - z) * s + z * t" in exI)
  4970       apply (simp add: linepath_def scaleR_conv_of_real algebra_simps)
  4971       apply (rule conjI)
  4972       using mult_right_mono apply blast
  4973       using affine_ineq  by (metis "mult.commute")
  4974   }
  4975   moreover
  4976   { fix z
  4977     assume "s \<le> z" "z \<le> t"
  4978     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}"
  4979       apply (rule_tac x="(z - s)/(t - s)" in image_eqI)
  4980       apply (simp add: linepath_def scaleR_conv_of_real divide_simps exp_eq)
  4981       apply (auto simp: algebra_simps divide_simps)
  4982       done
  4983   }
  4984   ultimately show ?thesis
  4985     by (fastforce simp add: path_image_def part_circlepath_def)
  4986 qed
  4987 
  4988 lemma path_image_part_circlepath':
  4989   "path_image (part_circlepath z r s t) = (\<lambda>x. z + r * cis x) ` closed_segment s t"
  4990 proof -
  4991   have "path_image (part_circlepath z r s t) = 
  4992           (\<lambda>x. z + r * exp(\<i> * of_real x)) ` linepath s t ` {0..1}"
  4993     by (simp add: image_image path_image_def part_circlepath_def)
  4994   also have "linepath s t ` {0..1} = closed_segment s t"
  4995     by (rule linepath_image_01)
  4996   finally show ?thesis by (simp add: cis_conv_exp)
  4997 qed
  4998 
  4999 lemma path_image_part_circlepath_subset:
  5000     "\<lbrakk>s \<le> t; 0 \<le> r\<rbrakk> \<Longrightarrow> path_image(part_circlepath z r s t) \<subseteq> sphere z r"
  5001 by (auto simp: path_image_part_circlepath sphere_def dist_norm algebra_simps norm_mult)
  5002 
  5003 lemma in_path_image_part_circlepath:
  5004   assumes "w \<in> path_image(part_circlepath z r s t)" "s \<le> t" "0 \<le> r"
  5005     shows "norm(w - z) = r"
  5006 proof -
  5007   have "w \<in> {c. dist z c = r}"
  5008     by (metis (no_types) path_image_part_circlepath_subset sphere_def subset_eq assms)
  5009   thus ?thesis
  5010     by (simp add: dist_norm norm_minus_commute)
  5011 qed
  5012 
  5013 lemma path_image_part_circlepath_subset':
  5014   assumes "r \<ge> 0"
  5015   shows   "path_image (part_circlepath z r s t) \<subseteq> sphere z r"
  5016 proof (cases "s \<le> t")
  5017   case True
  5018   thus ?thesis using path_image_part_circlepath_subset[of s t r z] assms by simp
  5019 next
  5020   case False
  5021   thus ?thesis using path_image_part_circlepath_subset[of t s r z] assms
  5022     by (subst reversepath_part_circlepath [symmetric], subst path_image_reversepath) simp_all
  5023 qed
  5024 
  5025 lemma part_circlepath_cnj: "cnj (part_circlepath c r a b x) = part_circlepath (cnj c) r (-a) (-b) x"
  5026   by (simp add: part_circlepath_def exp_cnj linepath_def algebra_simps)
  5027 
  5028 lemma contour_integral_bound_part_circlepath:
  5029   assumes "f contour_integrable_on part_circlepath c r a b"
  5030   assumes "B \<ge> 0" "r \<ge> 0" "\<And>x. x \<in> path_image (part_circlepath c r a b) \<Longrightarrow> norm (f x) \<le> B"
  5031   shows   "norm (contour_integral (part_circlepath c r a b) f) \<le> B * r * \<bar>b - a\<bar>"
  5032 proof -
  5033   let ?I = "integral {0..1} (\<lambda>x. f (part_circlepath c r a b x) * \<i> * of_real (r * (b - a)) *
  5034               exp (\<i> * linepath a b x))"
  5035   have "norm ?I \<le> integral {0..1} (\<lambda>x::real. B * 1 * (r * \<bar>b - a\<bar>) * 1)"
  5036   proof (rule integral_norm_bound_integral, goal_cases)
  5037     case 1
  5038     with assms(1) show ?case
  5039       by (simp add: contour_integrable_on vector_derivative_part_circlepath mult_ac)
  5040   next
  5041     case (3 x)
  5042     with assms(2-) show ?case unfolding norm_mult norm_of_real abs_mult
  5043       by (intro mult_mono) (auto simp: path_image_def)
  5044   qed auto
  5045   also have "?I = contour_integral (part_circlepath c r a b) f"
  5046     by (simp add: contour_integral_integral vector_derivative_part_circlepath mult_ac)
  5047   finally show ?thesis by simp
  5048 qed
  5049 
  5050 lemma has_contour_integral_part_circlepath_iff:
  5051   assumes "a < b"
  5052   shows "(f has_contour_integral I) (part_circlepath c r a b) \<longleftrightarrow>
  5053            ((\<lambda>t. f (c + r * cis t) * r * \<i> * cis t) has_integral I) {a..b}"
  5054 proof -
  5055   have "(f has_contour_integral I) (part_circlepath c r a b) \<longleftrightarrow>
  5056           ((\<lambda>x. f (part_circlepath c r a b x) * vector_derivative (part_circlepath c r a b)
  5057            (at x within {0..1})) has_integral I) {0..1}"
  5058     unfolding has_contour_integral_def ..
  5059   also have "\<dots> \<longleftrightarrow> ((\<lambda>x. f (part_circlepath c r a b x) * r * (b - a) * \<i> *
  5060                             cis (linepath a b x)) has_integral I) {0..1}"
  5061     by (intro has_integral_cong, subst vector_derivative_part_circlepath01)
  5062        (simp_all add: cis_conv_exp)
  5063   also have "\<dots> \<longleftrightarrow> ((\<lambda>x. f (c + r * exp (\<i> * linepath (of_real a) (of_real b) x)) *
  5064                        r * \<i> * exp (\<i> * linepath (of_real a) (of_real b) x) *
  5065                        vector_derivative (linepath (of_real a) (of_real b)) 
  5066                          (at x within {0..1})) has_integral I) {0..1}"
  5067     by (intro has_integral_cong, subst vector_derivative_linepath_within)
  5068        (auto simp: part_circlepath_def cis_conv_exp of_real_linepath [symmetric])
  5069   also have "\<dots> \<longleftrightarrow> ((\<lambda>z. f (c + r * exp (\<i> * z)) * r * \<i> * exp (\<i> * z)) has_contour_integral I)
  5070                       (linepath (of_real a) (of_real b))"
  5071     by (simp add: has_contour_integral_def)
  5072   also have "\<dots> \<longleftrightarrow> ((\<lambda>t. f (c + r * cis t) * r * \<i> * cis t) has_integral I) {a..b}" using assms
  5073     by (subst has_contour_integral_linepath_Reals_iff) (simp_all add: cis_conv_exp)
  5074   finally show ?thesis .
  5075 qed
  5076 
  5077 lemma contour_integrable_part_circlepath_iff:
  5078   assumes "a < b"
  5079   shows "f contour_integrable_on (part_circlepath c r a b) \<longleftrightarrow>
  5080            (\<lambda>t. f (c + r * cis t) * r * \<i> * cis t) integrable_on {a..b}"
  5081   using assms by (auto simp: contour_integrable_on_def integrable_on_def 
  5082                              has_contour_integral_part_circlepath_iff)
  5083 
  5084 lemma contour_integral_part_circlepath_eq:
  5085   assumes "a < b"
  5086   shows "contour_integral (part_circlepath c r a b) f =
  5087            integral {a..b} (\<lambda>t. f (c + r * cis t) * r * \<i> * cis t)"
  5088 proof (cases "f contour_integrable_on part_circlepath c r a b")
  5089   case True
  5090   hence "(\<lambda>t. f (c + r * cis t) * r * \<i> * cis t) integrable_on {a..b}" 
  5091     using assms by (simp add: contour_integrable_part_circlepath_iff)
  5092   with True show ?thesis
  5093     using has_contour_integral_part_circlepath_iff[OF assms]
  5094           contour_integral_unique has_integral_integrable_integral by blast
  5095 next
  5096   case False
  5097   hence "\<not>(\<lambda>t. f (c + r * cis t) * r * \<i> * cis t) integrable_on {a..b}" 
  5098     using assms by (simp add: contour_integrable_part_circlepath_iff)
  5099   with False show ?thesis
  5100     by (simp add: not_integrable_contour_integral not_integrable_integral)
  5101 qed
  5102 
  5103 lemma contour_integral_part_circlepath_reverse:
  5104   "contour_integral (part_circlepath c r a b) f = -contour_integral (part_circlepath c r b a) f"
  5105   by (subst reversepath_part_circlepath [symmetric], subst contour_integral_reversepath) simp_all
  5106 
  5107 lemma contour_integral_part_circlepath_reverse':
  5108   "b < a \<Longrightarrow> contour_integral (part_circlepath c r a b) f = 
  5109                -contour_integral (part_circlepath c r b a) f"
  5110   by (rule contour_integral_part_circlepath_reverse)
  5111 
  5112 lemma finite_bounded_log: "finite {z::complex. norm z \<le> b \<and> exp z = w}"
  5113 proof (cases "w = 0")
  5114   case True then show ?thesis by auto
  5115 next
  5116   case False
  5117   have *: "finite {x. cmod (complex_of_real (2 * real_of_int x * pi) * \<i>) \<le> b + cmod (Ln w)}"
  5118     apply (simp add: norm_mult finite_int_iff_bounded_le)
  5119     apply (rule_tac x="\<lfloor>(b + cmod (Ln w)) / (2*pi)\<rfloor>" in exI)
  5120     apply (auto simp: divide_simps le_floor_iff)
  5121     done
  5122   have [simp]: "\<And>P f. {z. P z \<and> (\<exists>n. z = f n)} = f ` {n. P (f n)}"
  5123     by blast
  5124   show ?thesis
  5125     apply (subst exp_Ln [OF False, symmetric])
  5126     apply (simp add: exp_eq)
  5127     using norm_add_leD apply (fastforce intro: finite_subset [OF _ *])
  5128     done
  5129 qed
  5130 
  5131 lemma finite_bounded_log2:
  5132   fixes a::complex
  5133     assumes "a \<noteq> 0"
  5134     shows "finite {z. norm z \<le> b \<and> exp(a*z) = w}"
  5135 proof -
  5136   have *: "finite ((\<lambda>z. z / a) ` {z. cmod z \<le> b * cmod a \<and> exp z = w})"
  5137     by (rule finite_imageI [OF finite_bounded_log])
  5138   show ?thesis
  5139     by (rule finite_subset [OF _ *]) (force simp: assms norm_mult)
  5140 qed
  5141 
  5142 lemma has_contour_integral_bound_part_circlepath_strong:
  5143   assumes fi: "(f has_contour_integral i) (part_circlepath z r s t)"
  5144       and "finite k" and le: "0 \<le> B" "0 < r" "s \<le> t"
  5145       and B: "\<And>x. x \<in> path_image(part_circlepath z r s t) - k \<Longrightarrow> norm(f x) \<le> B"
  5146     shows "cmod i \<le> B * r * (t - s)"
  5147 proof -
  5148   consider "s = t" | "s < t" using \<open>s \<le> t\<close> by linarith
  5149   then show ?thesis
  5150   proof cases
  5151     case 1 with fi [unfolded has_contour_integral]
  5152     have "i = 0"  by (simp add: vector_derivative_part_circlepath)
  5153     with assms show ?thesis by simp
  5154   next
  5155     case 2
  5156     have [simp]: "\<bar>r\<bar> = r" using \<open>r > 0\<close> by linarith
  5157     have [simp]: "cmod (complex_of_real t - complex_of_real s) = t-s"
  5158       by (metis "2" abs_of_pos diff_gt_0_iff_gt norm_of_real of_real_diff)
  5159     have "finite (part_circlepath z r s t -` {y} \<inter> {0..1})" if "y \<in> k" for y
  5160     proof -
  5161       define w where "w = (y - z)/of_real r / exp(\<i> * of_real s)"
  5162       have fin: "finite (of_real -` {z. cmod z \<le> 1 \<and> exp (\<i> * complex_of_real (t - s) * z) = w})"
  5163         apply (rule finite_vimageI [OF finite_bounded_log2])
  5164         using \<open>s < t\<close> apply (auto simp: inj_of_real)
  5165         done
  5166       show ?thesis
  5167         apply (simp add: part_circlepath_def linepath_def vimage_def)
  5168         apply (rule finite_subset [OF _ fin])
  5169         using le
  5170         apply (auto simp: w_def algebra_simps scaleR_conv_of_real exp_add exp_diff)
  5171         done
  5172     qed
  5173     then have fin01: "finite ((part_circlepath z r s t) -` k \<inter> {0..1})"
  5174       by (rule finite_finite_vimage_IntI [OF \<open>finite k\<close>])
  5175     have **: "((\<lambda>x. if (part_circlepath z r s t x) \<in> k then 0
  5176                     else f(part_circlepath z r s t x) *
  5177                        vector_derivative (part_circlepath z r s t) (at x)) has_integral i)  {0..1}"
  5178       by (rule has_integral_spike [OF negligible_finite [OF fin01]])  (use fi has_contour_integral in auto)
  5179     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"
  5180       by (auto intro!: B [unfolded path_image_def image_def, simplified])
  5181     show ?thesis
  5182       apply (rule has_integral_bound [where 'a=real, simplified, OF _ **, simplified])
  5183       using assms apply force
  5184       apply (simp add: norm_mult vector_derivative_part_circlepath)
  5185       using le * "2" \<open>r > 0\<close> by auto
  5186   qed
  5187 qed
  5188 
  5189 lemma has_contour_integral_bound_part_circlepath:
  5190       "\<lbrakk>(f has_contour_integral i) (part_circlepath z r s t);
  5191         0 \<le> B; 0 < r; s \<le> t;
  5192         \<And>x. x \<in> path_image(part_circlepath z r s t) \<Longrightarrow> norm(f x) \<le> B\<rbrakk>
  5193        \<Longrightarrow> norm i \<le> B*r*(t - s)"
  5194   by (auto intro: has_contour_integral_bound_part_circlepath_strong)
  5195 
  5196 lemma contour_integrable_continuous_part_circlepath:
  5197      "continuous_on (path_image (part_circlepath z r s t)) f
  5198       \<Longrightarrow> f contour_integrable_on (part_circlepath z r s t)"
  5199   apply (simp add: contour_integrable_on has_contour_integral_def vector_derivative_part_circlepath path_image_def)
  5200   apply (rule integrable_continuous_real)
  5201   apply (fast intro: path_part_circlepath [unfolded path_def] continuous_intros continuous_on_compose2 [where g=f, OF _ _ order_refl])
  5202   done
  5203 
  5204 proposition winding_number_part_circlepath_pos_less:
  5205   assumes "s < t" and no: "norm(w - z) < r"
  5206     shows "0 < Re (winding_number(part_circlepath z r s t) w)"
  5207 proof -
  5208   have "0 < r" by (meson no norm_not_less_zero not_le order.strict_trans2)
  5209   note valid_path_part_circlepath
  5210   moreover have " w \<notin> path_image (part_circlepath z r s t)"
  5211     using assms by (auto simp: path_image_def image_def part_circlepath_def norm_mult linepath_def)
  5212   moreover have "0 < r * (t - s) * (r - cmod (w - z))"
  5213     using assms by (metis \<open>0 < r\<close> diff_gt_0_iff_gt mult_pos_pos)
  5214   ultimately show ?thesis
  5215     apply (rule winding_number_pos_lt [where e = "r*(t - s)*(r - norm(w - z))"])
  5216     apply (simp add: vector_derivative_part_circlepath right_diff_distrib [symmetric] mult_ac)
  5217     apply (rule mult_left_mono)+
  5218     using Re_Im_le_cmod [of "w-z" "linepath s t x" for x]
  5219     apply (simp add: exp_Euler cos_of_real sin_of_real part_circlepath_def algebra_simps cos_squared_eq [unfolded power2_eq_square])
  5220     using assms \<open>0 < r\<close> by auto
  5221 qed
  5222 
  5223 lemma simple_path_part_circlepath:
  5224     "simple_path(part_circlepath z r s t) \<longleftrightarrow> (r \<noteq> 0 \<and> s \<noteq> t \<and> \<bar>s - t\<bar> \<le> 2*pi)"
  5225 proof (cases "r = 0 \<or> s = t")
  5226   case True
  5227   then show ?thesis
  5228     unfolding part_circlepath_def simple_path_def
  5229     by (rule disjE) (force intro: bexI [where x = "1/4"] bexI [where x = "1/3"])+
  5230 next
  5231   case False then have "r \<noteq> 0" "s \<noteq> t" by auto
  5232   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"
  5233     by (simp add: algebra_simps)
  5234   have abs01: "\<And>x y::real. 0 \<le> x \<and> x \<le> 1 \<and> 0 \<le> y \<and> y \<le> 1
  5235                       \<Longrightarrow> (x = y \<or> x = 0 \<and> y = 1 \<or> x = 1 \<and> y = 0 \<longleftrightarrow> \<bar>x - y\<bar> \<in> {0,1})"
  5236     by auto
  5237   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>
  5238                   (\<exists>n. \<bar>x - y\<bar> * (t - s) = 2 * (of_int n * pi))"
  5239     by (force simp: algebra_simps abs_if dest: arg_cong [where f=Re] arg_cong [where f=complex_of_real]
  5240                     intro: exI [where x = "-n" for n])
  5241   have 1: "\<bar>s - t\<bar> \<le> 2 * pi"
  5242     if "\<And>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"
  5243   proof (rule ccontr)
  5244     assume "\<not> \<bar>s - t\<bar> \<le> 2 * pi"
  5245     then have *: "\<And>n. t - s \<noteq> of_int n * \<bar>s - t\<bar>"
  5246       using False that [of "2*pi / \<bar>t - s\<bar>"] by (simp add: abs_minus_commute divide_simps)
  5247     show False
  5248       using * [of 1] * [of "-1"] by auto
  5249   qed
  5250   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
  5251   proof -
  5252     have "t-s = 2 * (real_of_int n * pi)/x"
  5253       using that by (simp add: field_simps)
  5254     then show ?thesis by (metis abs_minus_commute)
  5255   qed
  5256   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)"
  5257     by force
  5258   show ?thesis using False
  5259     apply (simp add: simple_path_def)
  5260     apply (simp add: part_circlepath_def linepath_def exp_eq  * ** abs01  del: Set.insert_iff)
  5261     apply (subst abs_away)
  5262     apply (auto simp: 1)
  5263     apply (rule ccontr)
  5264     apply (auto simp: 2 divide_simps abs_mult dest: of_int_leD)
  5265     done
  5266 qed
  5267 
  5268 lemma arc_part_circlepath:
  5269   assumes "r \<noteq> 0" "s \<noteq> t" "\<bar>s - t\<bar> < 2*pi"
  5270     shows "arc (part_circlepath z r s t)"
  5271 proof -
  5272   have *: "x = y" if eq: "\<i> * (linepath s t x) = \<i> * (linepath s t y) + 2 * of_int n * complex_of_real pi * \<i>"
  5273     and x: "x \<in> {0..1}" and y: "y \<in> {0..1}" for x y n
  5274   proof (rule ccontr)
  5275     assume "x \<noteq> y"
  5276     have "(linepath s t x) = (linepath s t y) + 2 * of_int n * complex_of_real pi"
  5277       by (metis add_divide_eq_iff complex_i_not_zero mult.commute nonzero_mult_div_cancel_left eq)
  5278     then have "s*y + t*x = s*x + (t*y + of_int n * (pi * 2))"
  5279       by (force simp: algebra_simps linepath_def dest: arg_cong [where f=Re])
  5280     with \<open>x \<noteq> y\<close> have st: "s-t = (of_int n * (pi * 2) / (y-x))"
  5281       by (force simp: field_simps)
  5282     have "\<bar>real_of_int n\<bar> < \<bar>y - x\<bar>"
  5283       using assms \<open>x \<noteq> y\<close> by (simp add: st abs_mult field_simps)
  5284     then show False
  5285       using assms x y st by (auto dest: of_int_lessD)
  5286   qed
  5287   show ?thesis
  5288     using assms
  5289     apply (simp add: arc_def)
  5290     apply (simp add: part_circlepath_def inj_on_def exp_eq)
  5291     apply (blast intro: *)
  5292     done
  5293 qed
  5294 
  5295 subsection\<open>Special case of one complete circle\<close>
  5296 
  5297 definition%important circlepath :: "[complex, real, real] \<Rightarrow> complex"
  5298   where "circlepath z r \<equiv> part_circlepath z r 0 (2*pi)"
  5299 
  5300 lemma circlepath: "circlepath z r = (\<lambda>x. z + r * exp(2 * of_real pi * \<i> * of_real x))"
  5301   by (simp add: circlepath_def part_circlepath_def linepath_def algebra_simps)
  5302 
  5303 lemma pathstart_circlepath [simp]: "pathstart (circlepath z r) = z + r"
  5304   by (simp add: circlepath_def)
  5305 
  5306 lemma pathfinish_circlepath [simp]: "pathfinish (circlepath z r) = z + r"
  5307   by (simp add: circlepath_def) (metis exp_two_pi_i mult.commute)
  5308 
  5309 lemma circlepath_minus: "circlepath z (-r) x = circlepath z r (x + 1/2)"
  5310 proof -
  5311   have "z + of_real r * exp (2 * pi * \<i> * (x + 1/2)) =
  5312         z + of_real r * exp (2 * pi * \<i> * x + pi * \<i>)"
  5313     by (simp add: divide_simps) (simp add: algebra_simps)
  5314   also have "\<dots> = z - r * exp (2 * pi * \<i> * x)"
  5315     by (simp add: exp_add)
  5316   finally show ?thesis
  5317     by (simp add: circlepath path_image_def sphere_def dist_norm)
  5318 qed
  5319 
  5320 lemma circlepath_add1: "circlepath z r (x+1) = circlepath z r x"
  5321   using circlepath_minus [of z r "x+1/2"] circlepath_minus [of z "-r" x]
  5322   by (simp add: add.commute)
  5323 
  5324 lemma circlepath_add_half: "circlepath z r (x + 1/2) = circlepath z r (x - 1/2)"
  5325   using circlepath_add1 [of z r "x-1/2"]
  5326   by (simp add: add.commute)
  5327 
  5328 lemma path_image_circlepath_minus_subset:
  5329      "path_image (circlepath z (-r)) \<subseteq> path_image (circlepath z r)"
  5330   apply (simp add: path_image_def image_def circlepath_minus, clarify)
  5331   apply (case_tac "xa \<le> 1/2", force)
  5332   apply (force simp: circlepath_add_half)+
  5333   done
  5334 
  5335 lemma path_image_circlepath_minus: "path_image (circlepath z (-r)) = path_image (circlepath z r)"
  5336   using path_image_circlepath_minus_subset by fastforce
  5337 
  5338 lemma has_vector_derivative_circlepath [derivative_intros]:
  5339  "((circlepath z r) has_vector_derivative (2 * pi * \<i> * r * exp (2 * of_real pi * \<i> * of_real x)))
  5340    (at x within X)"
  5341   apply (simp add: circlepath_def scaleR_conv_of_real)
  5342   apply (rule derivative_eq_intros)
  5343   apply (simp add: algebra_simps)
  5344   done
  5345 
  5346 lemma vector_derivative_circlepath:
  5347    "vector_derivative (circlepath z r) (at x) =
  5348     2 * pi * \<i> * r * exp(2 * of_real pi * \<i> * x)"
  5349 using has_vector_derivative_circlepath vector_derivative_at by blast
  5350 
  5351 lemma vector_derivative_circlepath01:
  5352     "\<lbrakk>0 \<le> x; x \<le> 1\<rbrakk>
  5353      \<Longrightarrow> vector_derivative (circlepath z r) (at x within {0..1}) =
  5354           2 * pi * \<i> * r * exp(2 * of_real pi * \<i> * x)"
  5355   using has_vector_derivative_circlepath
  5356   by (auto simp: vector_derivative_at_within_ivl)
  5357 
  5358 lemma valid_path_circlepath [simp]: "valid_path (circlepath z r)"
  5359   by (simp add: circlepath_def)
  5360 
  5361 lemma path_circlepath [simp]: "path (circlepath z r)"
  5362   by (simp add: valid_path_imp_path)
  5363 
  5364 lemma path_image_circlepath_nonneg:
  5365   assumes "0 \<le> r" shows "path_image (circlepath z r) = sphere z r"
  5366 proof -
  5367   have *: "x \<in> (\<lambda>u. z + (cmod (x - z)) * exp (\<i> * (of_real u * (of_real pi * 2)))) ` {0..1}" for x
  5368   proof (cases "x = z")
  5369     case True then show ?thesis by force
  5370   next
  5371     case False
  5372     define w where "w = x - z"
  5373     then have "w \<noteq> 0" by (simp add: False)
  5374     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)"
  5375       using cis_conv_exp complex_eq_iff by auto
  5376     show ?thesis
  5377       apply (rule sincos_total_2pi [of "Re(w/of_real(norm w))" "Im(w/of_real(norm w))"])
  5378       apply (simp add: divide_simps \<open>w \<noteq> 0\<close> cmod_power2 [symmetric])
  5379       apply (rule_tac x="t / (2*pi)" in image_eqI)
  5380       apply (simp add: divide_simps \<open>w \<noteq> 0\<close>)
  5381       using False **
  5382       apply (auto simp: w_def)
  5383       done
  5384   qed
  5385   show ?thesis
  5386     unfolding circlepath path_image_def sphere_def dist_norm
  5387     by (force simp: assms algebra_simps norm_mult norm_minus_commute intro: *)
  5388 qed
  5389 
  5390 lemma path_image_circlepath [simp]:
  5391     "path_image (circlepath z r) = sphere z \<bar>r\<bar>"
  5392   using path_image_circlepath_minus
  5393   by (force simp: path_image_circlepath_nonneg abs_if)
  5394 
  5395 lemma has_contour_integral_bound_circlepath_strong:
  5396       "\<lbrakk>(f has_contour_integral i) (circlepath z r);
  5397         finite k; 0 \<le> B; 0 < r;
  5398         \<And>x. \<lbrakk>norm(x - z) = r; x \<notin> k\<rbrakk> \<Longrightarrow> norm(f x) \<le> B\<rbrakk>
  5399         \<Longrightarrow> norm i \<le> B*(2*pi*r)"
  5400   unfolding circlepath_def
  5401   by (auto simp: algebra_simps in_path_image_part_circlepath dest!: has_contour_integral_bound_part_circlepath_strong)
  5402 
  5403 lemma has_contour_integral_bound_circlepath:
  5404       "\<lbrakk>(f has_contour_integral i) (circlepath z r);
  5405         0 \<le> B; 0 < r; \<And>x. norm(x - z) = r \<Longrightarrow> norm(f x) \<le> B\<rbrakk>
  5406         \<Longrightarrow> norm i \<le> B*(2*pi*r)"
  5407   by (auto intro: has_contour_integral_bound_circlepath_strong)
  5408 
  5409 lemma contour_integrable_continuous_circlepath:
  5410     "continuous_on (path_image (circlepath z r)) f
  5411      \<Longrightarrow> f contour_integrable_on (circlepath z r)"
  5412   by (simp add: circlepath_def contour_integrable_continuous_part_circlepath)
  5413 
  5414 lemma simple_path_circlepath: "simple_path(circlepath z r) \<longleftrightarrow> (r \<noteq> 0)"
  5415   by (simp add: circlepath_def simple_path_part_circlepath)
  5416 
  5417 lemma notin_path_image_circlepath [simp]: "cmod (w - z) < r \<Longrightarrow> w \<notin> path_image (circlepath z r)"
  5418   by (simp add: sphere_def dist_norm norm_minus_commute)
  5419 
  5420 lemma contour_integral_circlepath:
  5421   assumes "r > 0"
  5422   shows "contour_integral (circlepath z r) (\<lambda>w. 1 / (w - z)) = 2 * complex_of_real pi * \<i>"
  5423 proof (rule contour_integral_unique)
  5424   show "((\<lambda>w. 1 / (w - z)) has_contour_integral 2 * complex_of_real pi * \<i>) (circlepath z r)"
  5425     unfolding has_contour_integral_def using assms
  5426     apply (subst has_integral_cong)
  5427      apply (simp add: vector_derivative_circlepath01)
  5428     using has_integral_const_real [of _ 0 1] apply (force simp: circlepath)
  5429     done
  5430 qed
  5431 
  5432 lemma winding_number_circlepath_centre: "0 < r \<Longrightarrow> winding_number (circlepath z r) z = 1"
  5433   apply (rule winding_number_unique_loop)
  5434   apply (simp_all add: sphere_def valid_path_imp_path)
  5435   apply (rule_tac x="circlepath z r" in exI)
  5436   apply (simp add: sphere_def contour_integral_circlepath)
  5437   done
  5438 
  5439 proposition winding_number_circlepath:
  5440   assumes "norm(w - z) < r" shows "winding_number(circlepath z r) w = 1"
  5441 proof (cases "w = z")
  5442   case True then show ?thesis
  5443     using assms winding_number_circlepath_centre by auto
  5444 next
  5445   case False
  5446   have [simp]: "r > 0"
  5447     using assms le_less_trans norm_ge_zero by blast
  5448   define r' where "r' = norm(w - z)"
  5449   have "r' < r"
  5450     by (simp add: assms r'_def)
  5451   have disjo: "cball z r' \<inter> sphere z r = {}"
  5452     using \<open>r' < r\<close> by (force simp: cball_def sphere_def)
  5453   have "winding_number(circlepath z r) w = winding_number(circlepath z r) z"
  5454   proof (rule winding_number_around_inside [where s = "cball z r'"])
  5455     show "winding_number (circlepath z r) z \<noteq> 0"
  5456       by (simp add: winding_number_circlepath_centre)
  5457     show "cball z r' \<inter> path_image (circlepath z r) = {}"
  5458       by (simp add: disjo less_eq_real_def)
  5459   qed (auto simp: r'_def dist_norm norm_minus_commute)
  5460   also have "\<dots> = 1"
  5461     by (simp add: winding_number_circlepath_centre)
  5462   finally show ?thesis .
  5463 qed
  5464 
  5465 
  5466 text\<open> Hence the Cauchy formula for points inside a circle.\<close>
  5467 
  5468 theorem Cauchy_integral_circlepath:
  5469   assumes contf: "continuous_on (cball z r) f" and holf: "f holomorphic_on (ball z r)" and wz: "norm(w - z) < r"
  5470   shows "((\<lambda>u. f u/(u - w)) has_contour_integral (2 * of_real pi * \<i> * f w))
  5471          (circlepath z r)"
  5472 proof -
  5473   have "r > 0"
  5474     using assms le_less_trans norm_ge_zero by blast
  5475   have "((\<lambda>u. f u / (u - w)) has_contour_integral (2 * pi) * \<i> * winding_number (circlepath z r) w * f w)
  5476         (circlepath z r)"
  5477   proof (rule Cauchy_integral_formula_weak [where s = "cball z r" and k = "{}"])
  5478     show "\<And>x. x \<in> interior (cball z r) - {} \<Longrightarrow>
  5479          f field_differentiable at x"
  5480       using holf holomorphic_on_imp_differentiable_at by auto
  5481     have "w \<notin> sphere z r"
  5482       by simp (metis dist_commute dist_norm not_le order_refl wz)
  5483     then show "path_image (circlepath z r) \<subseteq> cball z r - {w}"
  5484       using \<open>r > 0\<close> by (auto simp add: cball_def sphere_def)
  5485   qed (use wz in \<open>simp_all add: dist_norm norm_minus_commute contf\<close>)
  5486   then show ?thesis
  5487     by (simp add: winding_number_circlepath assms)
  5488 qed
  5489 
  5490 corollary%unimportant Cauchy_integral_circlepath_simple:
  5491   assumes "f holomorphic_on cball z r" "norm(w - z) < r"
  5492   shows "((\<lambda>u. f u/(u - w)) has_contour_integral (2 * of_real pi * \<i> * f w))
  5493          (circlepath z r)"
  5494 using assms by (force simp: holomorphic_on_imp_continuous_on holomorphic_on_subset Cauchy_integral_circlepath)
  5495 
  5496 
  5497 lemma no_bounded_connected_component_imp_winding_number_zero:
  5498   assumes g: "path g" "path_image g \<subseteq> s" "pathfinish g = pathstart g" "z \<notin> s"
  5499       and nb: "\<And>z. bounded (connected_component_set (- s) z) \<longrightarrow> z \<in> s"
  5500   shows "winding_number g z = 0"
  5501 apply (rule winding_number_zero_in_outside)
  5502 apply (simp_all add: assms)
  5503 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)
  5504 
  5505 lemma no_bounded_path_component_imp_winding_number_zero:
  5506   assumes g: "path g" "path_image g \<subseteq> s" "pathfinish g = pathstart g" "z \<notin> s"
  5507       and nb: "\<And>z. bounded (path_component_set (- s) z) \<longrightarrow> z \<in> s"
  5508   shows "winding_number g z = 0"
  5509 apply (rule no_bounded_connected_component_imp_winding_number_zero [OF g])
  5510 by (simp add: bounded_subset nb path_component_subset_connected_component)
  5511 
  5512 
  5513 subsection\<open> Uniform convergence of path integral\<close>
  5514 
  5515 text\<open>Uniform convergence when the derivative of the path is bounded, and in particular for the special case of a circle.\<close>
  5516 
  5517 proposition contour_integral_uniform_limit:
  5518   assumes ev_fint: "eventually (\<lambda>n::'a. (f n) contour_integrable_on \<gamma>) F"
  5519       and ul_f: "uniform_limit (path_image \<gamma>) f l F"
  5520       and noleB: "\<And>t. t \<in> {0..1} \<Longrightarrow> norm (vector_derivative \<gamma> (at t)) \<le> B"
  5521       and \<gamma>: "valid_path \<gamma>"
  5522       and [simp]: "\<not> trivial_limit F"
  5523   shows "l contour_integrable_on \<gamma>" "((\<lambda>n. contour_integral \<gamma> (f n)) \<longlongrightarrow> contour_integral \<gamma> l) F"
  5524 proof -
  5525   have "0 \<le> B" by (meson noleB [of 0] atLeastAtMost_iff norm_ge_zero order_refl order_trans zero_le_one)
  5526   { fix e::real
  5527     assume "0 < e"
  5528     then have "0 < e / (\<bar>B\<bar> + 1)" by simp
  5529     then have "\<forall>\<^sub>F n in F. \<forall>x\<in>path_image \<gamma>. cmod (f n x - l x) < e / (\<bar>B\<bar> + 1)"
  5530       using ul_f [unfolded uniform_limit_iff dist_norm] by auto
  5531     with ev_fint
  5532     obtain a where fga: "\<And>x. x \<in> {0..1} \<Longrightarrow> cmod (f a (\<gamma> x) - l (\<gamma> x)) < e / (\<bar>B\<bar> + 1)"
  5533                and inta: "(\<lambda>t. f a (\<gamma> t) * vector_derivative \<gamma> (at t)) integrable_on {0..1}"
  5534       using eventually_happens [OF eventually_conj]
  5535       by (fastforce simp: contour_integrable_on path_image_def)
  5536     have Ble: "B * e / (\<bar>B\<bar> + 1) \<le> e"
  5537       using \<open>0 \<le> B\<close>  \<open>0 < e\<close> by (simp add: divide_simps)
  5538     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}"
  5539     proof (intro exI conjI ballI)
  5540       show "cmod (l (\<gamma> x) * vector_derivative \<gamma> (at x) - f a (\<gamma> x) * vector_derivative \<gamma> (at x)) \<le> e"
  5541         if "x \<in> {0..1}" for x
  5542         apply (rule order_trans [OF _ Ble])
  5543         using noleB [OF that] fga [OF that] \<open>0 \<le> B\<close> \<open>0 < e\<close>
  5544         apply (simp add: norm_mult left_diff_distrib [symmetric] norm_minus_commute divide_simps)
  5545         apply (fastforce simp: mult_ac dest: mult_mono [OF less_imp_le])
  5546         done
  5547     qed (rule inta)
  5548   }
  5549   then show lintg: "l contour_integrable_on \<gamma>"
  5550     unfolding contour_integrable_on by (metis (mono_tags, lifting)integrable_uniform_limit_real)
  5551   { fix e::real
  5552     define B' where "B' = B + 1"
  5553     have B': "B' > 0" "B' > B" using  \<open>0 \<le> B\<close> by (auto simp: B'_def)
  5554     assume "0 < e"
  5555     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'"
  5556       using ul_f [unfolded uniform_limit_iff dist_norm, rule_format, of "e / B' / 2"] B'
  5557         by (simp add: field_simps)
  5558     have ie: "integral {0..1::real} (\<lambda>x. e / 2) < e" using \<open>0 < e\<close> by simp
  5559     have *: "cmod (f x (\<gamma> t) * vector_derivative \<gamma> (at t) - l (\<gamma> t) * vector_derivative \<gamma> (at t)) \<le> e / 2"
  5560              if t: "t\<in>{0..1}" and leB': "2 * cmod (f x (\<gamma> t) - l (\<gamma> t)) < e / B'" for x t
  5561     proof -
  5562       have "2 * cmod (f x (\<gamma> t) - l (\<gamma> t)) * cmod (vector_derivative \<gamma> (at t)) \<le> e * (B/ B')"
  5563         using mult_mono [OF less_imp_le [OF leB'] noleB] B' \<open>0 < e\<close> t by auto
  5564       also have "\<dots> < e"
  5565         by (simp add: B' \<open>0 < e\<close> mult_imp_div_pos_less)
  5566       finally have "2 * cmod (f x (\<gamma> t) - l (\<gamma> t)) * cmod (vector_derivative \<gamma> (at t)) < e" .
  5567       then show ?thesis
  5568         by (simp add: left_diff_distrib [symmetric] norm_mult)
  5569     qed
  5570     have le_e: "\<And>x. \<lbrakk>\<forall>xa\<in>{0..1}. 2 * cmod (f x (\<gamma> xa) - l (\<gamma> xa)) < e / B'; f x contour_integrable_on \<gamma>\<rbrakk>
  5571          \<Longrightarrow> cmod (integral {0..1}
  5572                     (\<lambda>u. f x (\<gamma> u) * vector_derivative \<gamma> (at u) - l (\<gamma> u) * vector_derivative \<gamma> (at u))) < e"
  5573       apply (rule le_less_trans [OF integral_norm_bound_integral ie])
  5574         apply (simp add: lintg integrable_diff contour_integrable_on [symmetric])
  5575        apply (blast intro: *)+
  5576       done
  5577     have "\<forall>\<^sub>F x in F. dist (contour_integral \<gamma> (f x)) (contour_integral \<gamma> l) < e"
  5578       apply (rule eventually_mono [OF eventually_conj [OF ev_no' ev_fint]])
  5579       apply (simp add: dist_norm contour_integrable_on path_image_def contour_integral_integral)
  5580       apply (simp add: lintg integral_diff [symmetric] contour_integrable_on [symmetric] le_e)
  5581       done
  5582   }
  5583   then show "((\<lambda>n. contour_integral \<gamma> (f n)) \<longlongrightarrow> contour_integral \<gamma> l) F"
  5584     by (rule tendstoI)
  5585 qed
  5586 
  5587 corollary%unimportant contour_integral_uniform_limit_circlepath:
  5588   assumes "\<forall>\<^sub>F n::'a in F. (f n) contour_integrable_on (circlepath z r)"
  5589       and "uniform_limit (sphere z r) f l F"
  5590       and "\<not> trivial_limit F" "0 < r"
  5591     shows "l contour_integrable_on (circlepath z r)"
  5592           "((\<lambda>n. contour_integral (circlepath z r) (f n)) \<longlongrightarrow> contour_integral (circlepath z r) l) F"
  5593   using assms by (auto simp: vector_derivative_circlepath norm_mult intro!: contour_integral_uniform_limit)
  5594 
  5595 
  5596 subsection%unimportant \<open>General stepping result for derivative formulas\<close>
  5597 
  5598 lemma Cauchy_next_derivative:
  5599   assumes "continuous_on (path_image \<gamma>) f'"
  5600       and leB: "\<And>t. t \<in> {0..1} \<Longrightarrow> norm (vector_derivative \<gamma> (at t)) \<le> B"
  5601       and int: "\<And>w. w \<in> s - path_image \<gamma> \<Longrightarrow> ((\<lambda>u. f' u / (u - w)^k) has_contour_integral f w) \<gamma>"