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