src/HOL/Analysis/Complex_Analysis_Basics.thy
changeset 63627 6ddb43c6b711
parent 63367 6c731c8b7f03
child 63886 685fb01256af
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/HOL/Analysis/Complex_Analysis_Basics.thy	Mon Aug 08 14:13:14 2016 +0200
     1.3 @@ -0,0 +1,1328 @@
     1.4 +(*  Author: John Harrison, Marco Maggesi, Graziano Gentili, Gianni Ciolli, Valentina Bruno
     1.5 +    Ported from "hol_light/Multivariate/canal.ml" by L C Paulson (2014)
     1.6 +*)
     1.7 +
     1.8 +section \<open>Complex Analysis Basics\<close>
     1.9 +
    1.10 +theory Complex_Analysis_Basics
    1.11 +imports Cartesian_Euclidean_Space "~~/src/HOL/Library/Nonpos_Ints"
    1.12 +begin
    1.13 +
    1.14 +
    1.15 +subsection\<open>General lemmas\<close>
    1.16 +
    1.17 +lemma nonneg_Reals_cmod_eq_Re: "z \<in> \<real>\<^sub>\<ge>\<^sub>0 \<Longrightarrow> norm z = Re z"
    1.18 +  by (simp add: complex_nonneg_Reals_iff cmod_eq_Re)
    1.19 +
    1.20 +lemma has_derivative_mult_right:
    1.21 +  fixes c:: "'a :: real_normed_algebra"
    1.22 +  shows "((op * c) has_derivative (op * c)) F"
    1.23 +by (rule has_derivative_mult_right [OF has_derivative_id])
    1.24 +
    1.25 +lemma has_derivative_of_real[derivative_intros, simp]:
    1.26 +  "(f has_derivative f') F \<Longrightarrow> ((\<lambda>x. of_real (f x)) has_derivative (\<lambda>x. of_real (f' x))) F"
    1.27 +  using bounded_linear.has_derivative[OF bounded_linear_of_real] .
    1.28 +
    1.29 +lemma has_vector_derivative_real_complex:
    1.30 +  "DERIV f (of_real a) :> f' \<Longrightarrow> ((\<lambda>x. f (of_real x)) has_vector_derivative f') (at a within s)"
    1.31 +  using has_derivative_compose[of of_real of_real a _ f "op * f'"]
    1.32 +  by (simp add: scaleR_conv_of_real ac_simps has_vector_derivative_def has_field_derivative_def)
    1.33 +
    1.34 +lemma fact_cancel:
    1.35 +  fixes c :: "'a::real_field"
    1.36 +  shows "of_nat (Suc n) * c / (fact (Suc n)) = c / (fact n)"
    1.37 +  by (simp add: of_nat_mult del: of_nat_Suc times_nat.simps)
    1.38 +
    1.39 +lemma bilinear_times:
    1.40 +  fixes c::"'a::real_algebra" shows "bilinear (\<lambda>x y::'a. x*y)"
    1.41 +  by (auto simp: bilinear_def distrib_left distrib_right intro!: linearI)
    1.42 +
    1.43 +lemma linear_cnj: "linear cnj"
    1.44 +  using bounded_linear.linear[OF bounded_linear_cnj] .
    1.45 +
    1.46 +lemma tendsto_Re_upper:
    1.47 +  assumes "~ (trivial_limit F)"
    1.48 +          "(f \<longlongrightarrow> l) F"
    1.49 +          "eventually (\<lambda>x. Re(f x) \<le> b) F"
    1.50 +    shows  "Re(l) \<le> b"
    1.51 +  by (metis assms tendsto_le [OF _ tendsto_const]  tendsto_Re)
    1.52 +
    1.53 +lemma tendsto_Re_lower:
    1.54 +  assumes "~ (trivial_limit F)"
    1.55 +          "(f \<longlongrightarrow> l) F"
    1.56 +          "eventually (\<lambda>x. b \<le> Re(f x)) F"
    1.57 +    shows  "b \<le> Re(l)"
    1.58 +  by (metis assms tendsto_le [OF _ _ tendsto_const]  tendsto_Re)
    1.59 +
    1.60 +lemma tendsto_Im_upper:
    1.61 +  assumes "~ (trivial_limit F)"
    1.62 +          "(f \<longlongrightarrow> l) F"
    1.63 +          "eventually (\<lambda>x. Im(f x) \<le> b) F"
    1.64 +    shows  "Im(l) \<le> b"
    1.65 +  by (metis assms tendsto_le [OF _ tendsto_const]  tendsto_Im)
    1.66 +
    1.67 +lemma tendsto_Im_lower:
    1.68 +  assumes "~ (trivial_limit F)"
    1.69 +          "(f \<longlongrightarrow> l) F"
    1.70 +          "eventually (\<lambda>x. b \<le> Im(f x)) F"
    1.71 +    shows  "b \<le> Im(l)"
    1.72 +  by (metis assms tendsto_le [OF _ _ tendsto_const]  tendsto_Im)
    1.73 +
    1.74 +lemma lambda_zero: "(\<lambda>h::'a::mult_zero. 0) = op * 0"
    1.75 +  by auto
    1.76 +
    1.77 +lemma lambda_one: "(\<lambda>x::'a::monoid_mult. x) = op * 1"
    1.78 +  by auto
    1.79 +
    1.80 +lemma continuous_mult_left:
    1.81 +  fixes c::"'a::real_normed_algebra"
    1.82 +  shows "continuous F f \<Longrightarrow> continuous F (\<lambda>x. c * f x)"
    1.83 +by (rule continuous_mult [OF continuous_const])
    1.84 +
    1.85 +lemma continuous_mult_right:
    1.86 +  fixes c::"'a::real_normed_algebra"
    1.87 +  shows "continuous F f \<Longrightarrow> continuous F (\<lambda>x. f x * c)"
    1.88 +by (rule continuous_mult [OF _ continuous_const])
    1.89 +
    1.90 +lemma continuous_on_mult_left:
    1.91 +  fixes c::"'a::real_normed_algebra"
    1.92 +  shows "continuous_on s f \<Longrightarrow> continuous_on s (\<lambda>x. c * f x)"
    1.93 +by (rule continuous_on_mult [OF continuous_on_const])
    1.94 +
    1.95 +lemma continuous_on_mult_right:
    1.96 +  fixes c::"'a::real_normed_algebra"
    1.97 +  shows "continuous_on s f \<Longrightarrow> continuous_on s (\<lambda>x. f x * c)"
    1.98 +by (rule continuous_on_mult [OF _ continuous_on_const])
    1.99 +
   1.100 +lemma uniformly_continuous_on_cmul_right [continuous_intros]:
   1.101 +  fixes f :: "'a::real_normed_vector \<Rightarrow> 'b::real_normed_algebra"
   1.102 +  shows "uniformly_continuous_on s f \<Longrightarrow> uniformly_continuous_on s (\<lambda>x. f x * c)"
   1.103 +  using bounded_linear.uniformly_continuous_on[OF bounded_linear_mult_left] .
   1.104 +
   1.105 +lemma uniformly_continuous_on_cmul_left[continuous_intros]:
   1.106 +  fixes f :: "'a::real_normed_vector \<Rightarrow> 'b::real_normed_algebra"
   1.107 +  assumes "uniformly_continuous_on s f"
   1.108 +    shows "uniformly_continuous_on s (\<lambda>x. c * f x)"
   1.109 +by (metis assms bounded_linear.uniformly_continuous_on bounded_linear_mult_right)
   1.110 +
   1.111 +lemma continuous_within_norm_id [continuous_intros]: "continuous (at x within S) norm"
   1.112 +  by (rule continuous_norm [OF continuous_ident])
   1.113 +
   1.114 +lemma continuous_on_norm_id [continuous_intros]: "continuous_on S norm"
   1.115 +  by (intro continuous_on_id continuous_on_norm)
   1.116 +
   1.117 +subsection\<open>DERIV stuff\<close>
   1.118 +
   1.119 +lemma DERIV_zero_connected_constant:
   1.120 +  fixes f :: "'a::{real_normed_field,euclidean_space} \<Rightarrow> 'a"
   1.121 +  assumes "connected s"
   1.122 +      and "open s"
   1.123 +      and "finite k"
   1.124 +      and "continuous_on s f"
   1.125 +      and "\<forall>x\<in>(s - k). DERIV f x :> 0"
   1.126 +    obtains c where "\<And>x. x \<in> s \<Longrightarrow> f(x) = c"
   1.127 +using has_derivative_zero_connected_constant [OF assms(1-4)] assms
   1.128 +by (metis DERIV_const has_derivative_const Diff_iff at_within_open frechet_derivative_at has_field_derivative_def)
   1.129 +
   1.130 +lemma DERIV_zero_constant:
   1.131 +  fixes f :: "'a::{real_normed_field, real_inner} \<Rightarrow> 'a"
   1.132 +  shows    "\<lbrakk>convex s;
   1.133 +             \<And>x. x\<in>s \<Longrightarrow> (f has_field_derivative 0) (at x within s)\<rbrakk>
   1.134 +             \<Longrightarrow> \<exists>c. \<forall>x \<in> s. f(x) = c"
   1.135 +  by (auto simp: has_field_derivative_def lambda_zero intro: has_derivative_zero_constant)
   1.136 +
   1.137 +lemma DERIV_zero_unique:
   1.138 +  fixes f :: "'a::{real_normed_field, real_inner} \<Rightarrow> 'a"
   1.139 +  assumes "convex s"
   1.140 +      and d0: "\<And>x. x\<in>s \<Longrightarrow> (f has_field_derivative 0) (at x within s)"
   1.141 +      and "a \<in> s"
   1.142 +      and "x \<in> s"
   1.143 +    shows "f x = f a"
   1.144 +  by (rule has_derivative_zero_unique [OF assms(1) _ assms(4,3)])
   1.145 +     (metis d0 has_field_derivative_imp_has_derivative lambda_zero)
   1.146 +
   1.147 +lemma DERIV_zero_connected_unique:
   1.148 +  fixes f :: "'a::{real_normed_field, real_inner} \<Rightarrow> 'a"
   1.149 +  assumes "connected s"
   1.150 +      and "open s"
   1.151 +      and d0: "\<And>x. x\<in>s \<Longrightarrow> DERIV f x :> 0"
   1.152 +      and "a \<in> s"
   1.153 +      and "x \<in> s"
   1.154 +    shows "f x = f a"
   1.155 +    by (rule has_derivative_zero_unique_connected [OF assms(2,1) _ assms(5,4)])
   1.156 +       (metis has_field_derivative_def lambda_zero d0)
   1.157 +
   1.158 +lemma DERIV_transform_within:
   1.159 +  assumes "(f has_field_derivative f') (at a within s)"
   1.160 +      and "0 < d" "a \<in> s"
   1.161 +      and "\<And>x. x\<in>s \<Longrightarrow> dist x a < d \<Longrightarrow> f x = g x"
   1.162 +    shows "(g has_field_derivative f') (at a within s)"
   1.163 +  using assms unfolding has_field_derivative_def
   1.164 +  by (blast intro: has_derivative_transform_within)
   1.165 +
   1.166 +lemma DERIV_transform_within_open:
   1.167 +  assumes "DERIV f a :> f'"
   1.168 +      and "open s" "a \<in> s"
   1.169 +      and "\<And>x. x\<in>s \<Longrightarrow> f x = g x"
   1.170 +    shows "DERIV g a :> f'"
   1.171 +  using assms unfolding has_field_derivative_def
   1.172 +by (metis has_derivative_transform_within_open)
   1.173 +
   1.174 +lemma DERIV_transform_at:
   1.175 +  assumes "DERIV f a :> f'"
   1.176 +      and "0 < d"
   1.177 +      and "\<And>x. dist x a < d \<Longrightarrow> f x = g x"
   1.178 +    shows "DERIV g a :> f'"
   1.179 +  by (blast intro: assms DERIV_transform_within)
   1.180 +
   1.181 +(*generalising DERIV_isconst_all, which requires type real (using the ordering)*)
   1.182 +lemma DERIV_zero_UNIV_unique:
   1.183 +  fixes f :: "'a::{real_normed_field, real_inner} \<Rightarrow> 'a"
   1.184 +  shows "(\<And>x. DERIV f x :> 0) \<Longrightarrow> f x = f a"
   1.185 +by (metis DERIV_zero_unique UNIV_I convex_UNIV)
   1.186 +
   1.187 +subsection \<open>Some limit theorems about real part of real series etc.\<close>
   1.188 +
   1.189 +(*MOVE? But not to Finite_Cartesian_Product*)
   1.190 +lemma sums_vec_nth :
   1.191 +  assumes "f sums a"
   1.192 +  shows "(\<lambda>x. f x $ i) sums a $ i"
   1.193 +using assms unfolding sums_def
   1.194 +by (auto dest: tendsto_vec_nth [where i=i])
   1.195 +
   1.196 +lemma summable_vec_nth :
   1.197 +  assumes "summable f"
   1.198 +  shows "summable (\<lambda>x. f x $ i)"
   1.199 +using assms unfolding summable_def
   1.200 +by (blast intro: sums_vec_nth)
   1.201 +
   1.202 +subsection \<open>Complex number lemmas\<close>
   1.203 +
   1.204 +lemma
   1.205 +  shows open_halfspace_Re_lt: "open {z. Re(z) < b}"
   1.206 +    and open_halfspace_Re_gt: "open {z. Re(z) > b}"
   1.207 +    and closed_halfspace_Re_ge: "closed {z. Re(z) \<ge> b}"
   1.208 +    and closed_halfspace_Re_le: "closed {z. Re(z) \<le> b}"
   1.209 +    and closed_halfspace_Re_eq: "closed {z. Re(z) = b}"
   1.210 +    and open_halfspace_Im_lt: "open {z. Im(z) < b}"
   1.211 +    and open_halfspace_Im_gt: "open {z. Im(z) > b}"
   1.212 +    and closed_halfspace_Im_ge: "closed {z. Im(z) \<ge> b}"
   1.213 +    and closed_halfspace_Im_le: "closed {z. Im(z) \<le> b}"
   1.214 +    and closed_halfspace_Im_eq: "closed {z. Im(z) = b}"
   1.215 +  by (intro open_Collect_less closed_Collect_le closed_Collect_eq continuous_on_Re
   1.216 +            continuous_on_Im continuous_on_id continuous_on_const)+
   1.217 +
   1.218 +lemma closed_complex_Reals: "closed (\<real> :: complex set)"
   1.219 +proof -
   1.220 +  have "(\<real> :: complex set) = {z. Im z = 0}"
   1.221 +    by (auto simp: complex_is_Real_iff)
   1.222 +  then show ?thesis
   1.223 +    by (metis closed_halfspace_Im_eq)
   1.224 +qed
   1.225 +
   1.226 +lemma closed_Real_halfspace_Re_le: "closed (\<real> \<inter> {w. Re w \<le> x})"
   1.227 +  by (simp add: closed_Int closed_complex_Reals closed_halfspace_Re_le)
   1.228 +
   1.229 +corollary closed_nonpos_Reals_complex [simp]: "closed (\<real>\<^sub>\<le>\<^sub>0 :: complex set)"
   1.230 +proof -
   1.231 +  have "\<real>\<^sub>\<le>\<^sub>0 = \<real> \<inter> {z. Re(z) \<le> 0}"
   1.232 +    using complex_nonpos_Reals_iff complex_is_Real_iff by auto
   1.233 +  then show ?thesis
   1.234 +    by (metis closed_Real_halfspace_Re_le)
   1.235 +qed
   1.236 +
   1.237 +lemma closed_Real_halfspace_Re_ge: "closed (\<real> \<inter> {w. x \<le> Re(w)})"
   1.238 +  using closed_halfspace_Re_ge
   1.239 +  by (simp add: closed_Int closed_complex_Reals)
   1.240 +
   1.241 +corollary closed_nonneg_Reals_complex [simp]: "closed (\<real>\<^sub>\<ge>\<^sub>0 :: complex set)"
   1.242 +proof -
   1.243 +  have "\<real>\<^sub>\<ge>\<^sub>0 = \<real> \<inter> {z. Re(z) \<ge> 0}"
   1.244 +    using complex_nonneg_Reals_iff complex_is_Real_iff by auto
   1.245 +  then show ?thesis
   1.246 +    by (metis closed_Real_halfspace_Re_ge)
   1.247 +qed
   1.248 +
   1.249 +lemma closed_real_abs_le: "closed {w \<in> \<real>. \<bar>Re w\<bar> \<le> r}"
   1.250 +proof -
   1.251 +  have "{w \<in> \<real>. \<bar>Re w\<bar> \<le> r} = (\<real> \<inter> {w. Re w \<le> r}) \<inter> (\<real> \<inter> {w. Re w \<ge> -r})"
   1.252 +    by auto
   1.253 +  then show "closed {w \<in> \<real>. \<bar>Re w\<bar> \<le> r}"
   1.254 +    by (simp add: closed_Int closed_Real_halfspace_Re_ge closed_Real_halfspace_Re_le)
   1.255 +qed
   1.256 +
   1.257 +lemma real_lim:
   1.258 +  fixes l::complex
   1.259 +  assumes "(f \<longlongrightarrow> l) F" and "~(trivial_limit F)" and "eventually P F" and "\<And>a. P a \<Longrightarrow> f a \<in> \<real>"
   1.260 +  shows  "l \<in> \<real>"
   1.261 +proof (rule Lim_in_closed_set[OF closed_complex_Reals _ assms(2,1)])
   1.262 +  show "eventually (\<lambda>x. f x \<in> \<real>) F"
   1.263 +    using assms(3, 4) by (auto intro: eventually_mono)
   1.264 +qed
   1.265 +
   1.266 +lemma real_lim_sequentially:
   1.267 +  fixes l::complex
   1.268 +  shows "(f \<longlongrightarrow> l) sequentially \<Longrightarrow> (\<exists>N. \<forall>n\<ge>N. f n \<in> \<real>) \<Longrightarrow> l \<in> \<real>"
   1.269 +by (rule real_lim [where F=sequentially]) (auto simp: eventually_sequentially)
   1.270 +
   1.271 +lemma real_series:
   1.272 +  fixes l::complex
   1.273 +  shows "f sums l \<Longrightarrow> (\<And>n. f n \<in> \<real>) \<Longrightarrow> l \<in> \<real>"
   1.274 +unfolding sums_def
   1.275 +by (metis real_lim_sequentially setsum_in_Reals)
   1.276 +
   1.277 +lemma Lim_null_comparison_Re:
   1.278 +  assumes "eventually (\<lambda>x. norm(f x) \<le> Re(g x)) F" "(g \<longlongrightarrow> 0) F" shows "(f \<longlongrightarrow> 0) F"
   1.279 +  by (rule Lim_null_comparison[OF assms(1)] tendsto_eq_intros assms(2))+ simp
   1.280 +
   1.281 +subsection\<open>Holomorphic functions\<close>
   1.282 +
   1.283 +definition field_differentiable :: "['a \<Rightarrow> 'a::real_normed_field, 'a filter] \<Rightarrow> bool"
   1.284 +           (infixr "(field'_differentiable)" 50)
   1.285 +  where "f field_differentiable F \<equiv> \<exists>f'. (f has_field_derivative f') F"
   1.286 +
   1.287 +lemma field_differentiable_derivI:
   1.288 +    "f field_differentiable (at x) \<Longrightarrow> (f has_field_derivative deriv f x) (at x)"
   1.289 +by (simp add: field_differentiable_def DERIV_deriv_iff_has_field_derivative)
   1.290 +
   1.291 +lemma field_differentiable_imp_continuous_at:
   1.292 +    "f field_differentiable (at x within s) \<Longrightarrow> continuous (at x within s) f"
   1.293 +  by (metis DERIV_continuous field_differentiable_def)
   1.294 +
   1.295 +lemma field_differentiable_within_subset:
   1.296 +    "\<lbrakk>f field_differentiable (at x within s); t \<subseteq> s\<rbrakk>
   1.297 +     \<Longrightarrow> f field_differentiable (at x within t)"
   1.298 +  by (metis DERIV_subset field_differentiable_def)
   1.299 +
   1.300 +lemma field_differentiable_at_within:
   1.301 +    "\<lbrakk>f field_differentiable (at x)\<rbrakk>
   1.302 +     \<Longrightarrow> f field_differentiable (at x within s)"
   1.303 +  unfolding field_differentiable_def
   1.304 +  by (metis DERIV_subset top_greatest)
   1.305 +
   1.306 +lemma field_differentiable_linear [simp,derivative_intros]: "(op * c) field_differentiable F"
   1.307 +proof -
   1.308 +  show ?thesis
   1.309 +    unfolding field_differentiable_def has_field_derivative_def mult_commute_abs
   1.310 +    by (force intro: has_derivative_mult_right)
   1.311 +qed
   1.312 +
   1.313 +lemma field_differentiable_const [simp,derivative_intros]: "(\<lambda>z. c) field_differentiable F"
   1.314 +  unfolding field_differentiable_def has_field_derivative_def
   1.315 +  by (rule exI [where x=0])
   1.316 +     (metis has_derivative_const lambda_zero)
   1.317 +
   1.318 +lemma field_differentiable_ident [simp,derivative_intros]: "(\<lambda>z. z) field_differentiable F"
   1.319 +  unfolding field_differentiable_def has_field_derivative_def
   1.320 +  by (rule exI [where x=1])
   1.321 +     (simp add: lambda_one [symmetric])
   1.322 +
   1.323 +lemma field_differentiable_id [simp,derivative_intros]: "id field_differentiable F"
   1.324 +  unfolding id_def by (rule field_differentiable_ident)
   1.325 +
   1.326 +lemma field_differentiable_minus [derivative_intros]:
   1.327 +  "f field_differentiable F \<Longrightarrow> (\<lambda>z. - (f z)) field_differentiable F"
   1.328 +  unfolding field_differentiable_def
   1.329 +  by (metis field_differentiable_minus)
   1.330 +
   1.331 +lemma field_differentiable_add [derivative_intros]:
   1.332 +  assumes "f field_differentiable F" "g field_differentiable F"
   1.333 +    shows "(\<lambda>z. f z + g z) field_differentiable F"
   1.334 +  using assms unfolding field_differentiable_def
   1.335 +  by (metis field_differentiable_add)
   1.336 +
   1.337 +lemma field_differentiable_add_const [simp,derivative_intros]:
   1.338 +     "op + c field_differentiable F"
   1.339 +  by (simp add: field_differentiable_add)
   1.340 +
   1.341 +lemma field_differentiable_setsum [derivative_intros]:
   1.342 +  "(\<And>i. i \<in> I \<Longrightarrow> (f i) field_differentiable F) \<Longrightarrow> (\<lambda>z. \<Sum>i\<in>I. f i z) field_differentiable F"
   1.343 +  by (induct I rule: infinite_finite_induct)
   1.344 +     (auto intro: field_differentiable_add field_differentiable_const)
   1.345 +
   1.346 +lemma field_differentiable_diff [derivative_intros]:
   1.347 +  assumes "f field_differentiable F" "g field_differentiable F"
   1.348 +    shows "(\<lambda>z. f z - g z) field_differentiable F"
   1.349 +  using assms unfolding field_differentiable_def
   1.350 +  by (metis field_differentiable_diff)
   1.351 +
   1.352 +lemma field_differentiable_inverse [derivative_intros]:
   1.353 +  assumes "f field_differentiable (at a within s)" "f a \<noteq> 0"
   1.354 +  shows "(\<lambda>z. inverse (f z)) field_differentiable (at a within s)"
   1.355 +  using assms unfolding field_differentiable_def
   1.356 +  by (metis DERIV_inverse_fun)
   1.357 +
   1.358 +lemma field_differentiable_mult [derivative_intros]:
   1.359 +  assumes "f field_differentiable (at a within s)"
   1.360 +          "g field_differentiable (at a within s)"
   1.361 +    shows "(\<lambda>z. f z * g z) field_differentiable (at a within s)"
   1.362 +  using assms unfolding field_differentiable_def
   1.363 +  by (metis DERIV_mult [of f _ a s g])
   1.364 +
   1.365 +lemma field_differentiable_divide [derivative_intros]:
   1.366 +  assumes "f field_differentiable (at a within s)"
   1.367 +          "g field_differentiable (at a within s)"
   1.368 +          "g a \<noteq> 0"
   1.369 +    shows "(\<lambda>z. f z / g z) field_differentiable (at a within s)"
   1.370 +  using assms unfolding field_differentiable_def
   1.371 +  by (metis DERIV_divide [of f _ a s g])
   1.372 +
   1.373 +lemma field_differentiable_power [derivative_intros]:
   1.374 +  assumes "f field_differentiable (at a within s)"
   1.375 +    shows "(\<lambda>z. f z ^ n) field_differentiable (at a within s)"
   1.376 +  using assms unfolding field_differentiable_def
   1.377 +  by (metis DERIV_power)
   1.378 +
   1.379 +lemma field_differentiable_transform_within:
   1.380 +  "0 < d \<Longrightarrow>
   1.381 +        x \<in> s \<Longrightarrow>
   1.382 +        (\<And>x'. x' \<in> s \<Longrightarrow> dist x' x < d \<Longrightarrow> f x' = g x') \<Longrightarrow>
   1.383 +        f field_differentiable (at x within s)
   1.384 +        \<Longrightarrow> g field_differentiable (at x within s)"
   1.385 +  unfolding field_differentiable_def has_field_derivative_def
   1.386 +  by (blast intro: has_derivative_transform_within)
   1.387 +
   1.388 +lemma field_differentiable_compose_within:
   1.389 +  assumes "f field_differentiable (at a within s)"
   1.390 +          "g field_differentiable (at (f a) within f`s)"
   1.391 +    shows "(g o f) field_differentiable (at a within s)"
   1.392 +  using assms unfolding field_differentiable_def
   1.393 +  by (metis DERIV_image_chain)
   1.394 +
   1.395 +lemma field_differentiable_compose:
   1.396 +  "f field_differentiable at z \<Longrightarrow> g field_differentiable at (f z)
   1.397 +          \<Longrightarrow> (g o f) field_differentiable at z"
   1.398 +by (metis field_differentiable_at_within field_differentiable_compose_within)
   1.399 +
   1.400 +lemma field_differentiable_within_open:
   1.401 +     "\<lbrakk>a \<in> s; open s\<rbrakk> \<Longrightarrow> f field_differentiable at a within s \<longleftrightarrow>
   1.402 +                          f field_differentiable at a"
   1.403 +  unfolding field_differentiable_def
   1.404 +  by (metis at_within_open)
   1.405 +
   1.406 +subsection\<open>Caratheodory characterization\<close>
   1.407 +
   1.408 +lemma field_differentiable_caratheodory_at:
   1.409 +  "f field_differentiable (at z) \<longleftrightarrow>
   1.410 +         (\<exists>g. (\<forall>w. f(w) - f(z) = g(w) * (w - z)) \<and> continuous (at z) g)"
   1.411 +  using CARAT_DERIV [of f]
   1.412 +  by (simp add: field_differentiable_def has_field_derivative_def)
   1.413 +
   1.414 +lemma field_differentiable_caratheodory_within:
   1.415 +  "f field_differentiable (at z within s) \<longleftrightarrow>
   1.416 +         (\<exists>g. (\<forall>w. f(w) - f(z) = g(w) * (w - z)) \<and> continuous (at z within s) g)"
   1.417 +  using DERIV_caratheodory_within [of f]
   1.418 +  by (simp add: field_differentiable_def has_field_derivative_def)
   1.419 +
   1.420 +subsection\<open>Holomorphic\<close>
   1.421 +
   1.422 +definition holomorphic_on :: "[complex \<Rightarrow> complex, complex set] \<Rightarrow> bool"
   1.423 +           (infixl "(holomorphic'_on)" 50)
   1.424 +  where "f holomorphic_on s \<equiv> \<forall>x\<in>s. f field_differentiable (at x within s)"
   1.425 +
   1.426 +named_theorems holomorphic_intros "structural introduction rules for holomorphic_on"
   1.427 +
   1.428 +lemma holomorphic_onI [intro?]: "(\<And>x. x \<in> s \<Longrightarrow> f field_differentiable (at x within s)) \<Longrightarrow> f holomorphic_on s"
   1.429 +  by (simp add: holomorphic_on_def)
   1.430 +
   1.431 +lemma holomorphic_onD [dest?]: "\<lbrakk>f holomorphic_on s; x \<in> s\<rbrakk> \<Longrightarrow> f field_differentiable (at x within s)"
   1.432 +  by (simp add: holomorphic_on_def)
   1.433 +
   1.434 +lemma holomorphic_on_imp_differentiable_at:
   1.435 +   "\<lbrakk>f holomorphic_on s; open s; x \<in> s\<rbrakk> \<Longrightarrow> f field_differentiable (at x)"
   1.436 +using at_within_open holomorphic_on_def by fastforce
   1.437 +
   1.438 +lemma holomorphic_on_empty [holomorphic_intros]: "f holomorphic_on {}"
   1.439 +  by (simp add: holomorphic_on_def)
   1.440 +
   1.441 +lemma holomorphic_on_open:
   1.442 +    "open s \<Longrightarrow> f holomorphic_on s \<longleftrightarrow> (\<forall>x \<in> s. \<exists>f'. DERIV f x :> f')"
   1.443 +  by (auto simp: holomorphic_on_def field_differentiable_def has_field_derivative_def at_within_open [of _ s])
   1.444 +
   1.445 +lemma holomorphic_on_imp_continuous_on:
   1.446 +    "f holomorphic_on s \<Longrightarrow> continuous_on s f"
   1.447 +  by (metis field_differentiable_imp_continuous_at continuous_on_eq_continuous_within holomorphic_on_def)
   1.448 +
   1.449 +lemma holomorphic_on_subset [elim]:
   1.450 +    "f holomorphic_on s \<Longrightarrow> t \<subseteq> s \<Longrightarrow> f holomorphic_on t"
   1.451 +  unfolding holomorphic_on_def
   1.452 +  by (metis field_differentiable_within_subset subsetD)
   1.453 +
   1.454 +lemma holomorphic_transform: "\<lbrakk>f holomorphic_on s; \<And>x. x \<in> s \<Longrightarrow> f x = g x\<rbrakk> \<Longrightarrow> g holomorphic_on s"
   1.455 +  by (metis field_differentiable_transform_within linordered_field_no_ub holomorphic_on_def)
   1.456 +
   1.457 +lemma holomorphic_cong: "s = t ==> (\<And>x. x \<in> s \<Longrightarrow> f x = g x) \<Longrightarrow> f holomorphic_on s \<longleftrightarrow> g holomorphic_on t"
   1.458 +  by (metis holomorphic_transform)
   1.459 +
   1.460 +lemma holomorphic_on_linear [simp, holomorphic_intros]: "(op * c) holomorphic_on s"
   1.461 +  unfolding holomorphic_on_def by (metis field_differentiable_linear)
   1.462 +
   1.463 +lemma holomorphic_on_const [simp, holomorphic_intros]: "(\<lambda>z. c) holomorphic_on s"
   1.464 +  unfolding holomorphic_on_def by (metis field_differentiable_const)
   1.465 +
   1.466 +lemma holomorphic_on_ident [simp, holomorphic_intros]: "(\<lambda>x. x) holomorphic_on s"
   1.467 +  unfolding holomorphic_on_def by (metis field_differentiable_ident)
   1.468 +
   1.469 +lemma holomorphic_on_id [simp, holomorphic_intros]: "id holomorphic_on s"
   1.470 +  unfolding id_def by (rule holomorphic_on_ident)
   1.471 +
   1.472 +lemma holomorphic_on_compose:
   1.473 +  "f holomorphic_on s \<Longrightarrow> g holomorphic_on (f ` s) \<Longrightarrow> (g o f) holomorphic_on s"
   1.474 +  using field_differentiable_compose_within[of f _ s g]
   1.475 +  by (auto simp: holomorphic_on_def)
   1.476 +
   1.477 +lemma holomorphic_on_compose_gen:
   1.478 +  "f holomorphic_on s \<Longrightarrow> g holomorphic_on t \<Longrightarrow> f ` s \<subseteq> t \<Longrightarrow> (g o f) holomorphic_on s"
   1.479 +  by (metis holomorphic_on_compose holomorphic_on_subset)
   1.480 +
   1.481 +lemma holomorphic_on_minus [holomorphic_intros]: "f holomorphic_on s \<Longrightarrow> (\<lambda>z. -(f z)) holomorphic_on s"
   1.482 +  by (metis field_differentiable_minus holomorphic_on_def)
   1.483 +
   1.484 +lemma holomorphic_on_add [holomorphic_intros]:
   1.485 +  "\<lbrakk>f holomorphic_on s; g holomorphic_on s\<rbrakk> \<Longrightarrow> (\<lambda>z. f z + g z) holomorphic_on s"
   1.486 +  unfolding holomorphic_on_def by (metis field_differentiable_add)
   1.487 +
   1.488 +lemma holomorphic_on_diff [holomorphic_intros]:
   1.489 +  "\<lbrakk>f holomorphic_on s; g holomorphic_on s\<rbrakk> \<Longrightarrow> (\<lambda>z. f z - g z) holomorphic_on s"
   1.490 +  unfolding holomorphic_on_def by (metis field_differentiable_diff)
   1.491 +
   1.492 +lemma holomorphic_on_mult [holomorphic_intros]:
   1.493 +  "\<lbrakk>f holomorphic_on s; g holomorphic_on s\<rbrakk> \<Longrightarrow> (\<lambda>z. f z * g z) holomorphic_on s"
   1.494 +  unfolding holomorphic_on_def by (metis field_differentiable_mult)
   1.495 +
   1.496 +lemma holomorphic_on_inverse [holomorphic_intros]:
   1.497 +  "\<lbrakk>f holomorphic_on s; \<And>z. z \<in> s \<Longrightarrow> f z \<noteq> 0\<rbrakk> \<Longrightarrow> (\<lambda>z. inverse (f z)) holomorphic_on s"
   1.498 +  unfolding holomorphic_on_def by (metis field_differentiable_inverse)
   1.499 +
   1.500 +lemma holomorphic_on_divide [holomorphic_intros]:
   1.501 +  "\<lbrakk>f holomorphic_on s; g holomorphic_on s; \<And>z. z \<in> s \<Longrightarrow> g z \<noteq> 0\<rbrakk> \<Longrightarrow> (\<lambda>z. f z / g z) holomorphic_on s"
   1.502 +  unfolding holomorphic_on_def by (metis field_differentiable_divide)
   1.503 +
   1.504 +lemma holomorphic_on_power [holomorphic_intros]:
   1.505 +  "f holomorphic_on s \<Longrightarrow> (\<lambda>z. (f z)^n) holomorphic_on s"
   1.506 +  unfolding holomorphic_on_def by (metis field_differentiable_power)
   1.507 +
   1.508 +lemma holomorphic_on_setsum [holomorphic_intros]:
   1.509 +  "(\<And>i. i \<in> I \<Longrightarrow> (f i) holomorphic_on s) \<Longrightarrow> (\<lambda>x. setsum (\<lambda>i. f i x) I) holomorphic_on s"
   1.510 +  unfolding holomorphic_on_def by (metis field_differentiable_setsum)
   1.511 +
   1.512 +lemma DERIV_deriv_iff_field_differentiable:
   1.513 +  "DERIV f x :> deriv f x \<longleftrightarrow> f field_differentiable at x"
   1.514 +  unfolding field_differentiable_def by (metis DERIV_imp_deriv)
   1.515 +
   1.516 +lemma holomorphic_derivI:
   1.517 +     "\<lbrakk>f holomorphic_on S; open S; x \<in> S\<rbrakk>
   1.518 +      \<Longrightarrow> (f has_field_derivative deriv f x) (at x within T)"
   1.519 +by (metis DERIV_deriv_iff_field_differentiable at_within_open  holomorphic_on_def has_field_derivative_at_within)
   1.520 +
   1.521 +lemma complex_derivative_chain:
   1.522 +  "f field_differentiable at x \<Longrightarrow> g field_differentiable at (f x)
   1.523 +    \<Longrightarrow> deriv (g o f) x = deriv g (f x) * deriv f x"
   1.524 +  by (metis DERIV_deriv_iff_field_differentiable DERIV_chain DERIV_imp_deriv)
   1.525 +
   1.526 +lemma deriv_linear [simp]: "deriv (\<lambda>w. c * w) = (\<lambda>z. c)"
   1.527 +  by (metis DERIV_imp_deriv DERIV_cmult_Id)
   1.528 +
   1.529 +lemma deriv_ident [simp]: "deriv (\<lambda>w. w) = (\<lambda>z. 1)"
   1.530 +  by (metis DERIV_imp_deriv DERIV_ident)
   1.531 +
   1.532 +lemma deriv_id [simp]: "deriv id = (\<lambda>z. 1)"
   1.533 +  by (simp add: id_def)
   1.534 +
   1.535 +lemma deriv_const [simp]: "deriv (\<lambda>w. c) = (\<lambda>z. 0)"
   1.536 +  by (metis DERIV_imp_deriv DERIV_const)
   1.537 +
   1.538 +lemma deriv_add [simp]:
   1.539 +  "\<lbrakk>f field_differentiable at z; g field_differentiable at z\<rbrakk>
   1.540 +   \<Longrightarrow> deriv (\<lambda>w. f w + g w) z = deriv f z + deriv g z"
   1.541 +  unfolding DERIV_deriv_iff_field_differentiable[symmetric]
   1.542 +  by (auto intro!: DERIV_imp_deriv derivative_intros)
   1.543 +
   1.544 +lemma deriv_diff [simp]:
   1.545 +  "\<lbrakk>f field_differentiable at z; g field_differentiable at z\<rbrakk>
   1.546 +   \<Longrightarrow> deriv (\<lambda>w. f w - g w) z = deriv f z - deriv g z"
   1.547 +  unfolding DERIV_deriv_iff_field_differentiable[symmetric]
   1.548 +  by (auto intro!: DERIV_imp_deriv derivative_intros)
   1.549 +
   1.550 +lemma deriv_mult [simp]:
   1.551 +  "\<lbrakk>f field_differentiable at z; g field_differentiable at z\<rbrakk>
   1.552 +   \<Longrightarrow> deriv (\<lambda>w. f w * g w) z = f z * deriv g z + deriv f z * g z"
   1.553 +  unfolding DERIV_deriv_iff_field_differentiable[symmetric]
   1.554 +  by (auto intro!: DERIV_imp_deriv derivative_eq_intros)
   1.555 +
   1.556 +lemma deriv_cmult [simp]:
   1.557 +  "f field_differentiable at z \<Longrightarrow> deriv (\<lambda>w. c * f w) z = c * deriv f z"
   1.558 +  unfolding DERIV_deriv_iff_field_differentiable[symmetric]
   1.559 +  by (auto intro!: DERIV_imp_deriv derivative_eq_intros)
   1.560 +
   1.561 +lemma deriv_cmult_right [simp]:
   1.562 +  "f field_differentiable at z \<Longrightarrow> deriv (\<lambda>w. f w * c) z = deriv f z * c"
   1.563 +  unfolding DERIV_deriv_iff_field_differentiable[symmetric]
   1.564 +  by (auto intro!: DERIV_imp_deriv derivative_eq_intros)
   1.565 +
   1.566 +lemma deriv_cdivide_right [simp]:
   1.567 +  "f field_differentiable at z \<Longrightarrow> deriv (\<lambda>w. f w / c) z = deriv f z / c"
   1.568 +  unfolding Fields.field_class.field_divide_inverse
   1.569 +  by (blast intro: deriv_cmult_right)
   1.570 +
   1.571 +lemma complex_derivative_transform_within_open:
   1.572 +  "\<lbrakk>f holomorphic_on s; g holomorphic_on s; open s; z \<in> s; \<And>w. w \<in> s \<Longrightarrow> f w = g w\<rbrakk>
   1.573 +   \<Longrightarrow> deriv f z = deriv g z"
   1.574 +  unfolding holomorphic_on_def
   1.575 +  by (rule DERIV_imp_deriv)
   1.576 +     (metis DERIV_deriv_iff_field_differentiable DERIV_transform_within_open at_within_open)
   1.577 +
   1.578 +lemma deriv_compose_linear:
   1.579 +  "f field_differentiable at (c * z) \<Longrightarrow> deriv (\<lambda>w. f (c * w)) z = c * deriv f (c * z)"
   1.580 +apply (rule DERIV_imp_deriv)
   1.581 +apply (simp add: DERIV_deriv_iff_field_differentiable [symmetric])
   1.582 +apply (drule DERIV_chain' [of "times c" c z UNIV f "deriv f (c * z)", OF DERIV_cmult_Id])
   1.583 +apply (simp add: algebra_simps)
   1.584 +done
   1.585 +
   1.586 +lemma nonzero_deriv_nonconstant:
   1.587 +  assumes df: "DERIV f \<xi> :> df" and S: "open S" "\<xi> \<in> S" and "df \<noteq> 0"
   1.588 +    shows "\<not> f constant_on S"
   1.589 +unfolding constant_on_def
   1.590 +by (metis \<open>df \<noteq> 0\<close> DERIV_transform_within_open [OF df S] DERIV_const DERIV_unique)
   1.591 +
   1.592 +lemma holomorphic_nonconstant:
   1.593 +  assumes holf: "f holomorphic_on S" and "open S" "\<xi> \<in> S" "deriv f \<xi> \<noteq> 0"
   1.594 +    shows "\<not> f constant_on S"
   1.595 +    apply (rule nonzero_deriv_nonconstant [of f "deriv f \<xi>" \<xi> S])
   1.596 +    using assms
   1.597 +    apply (auto simp: holomorphic_derivI)
   1.598 +    done
   1.599 +
   1.600 +subsection\<open>Analyticity on a set\<close>
   1.601 +
   1.602 +definition analytic_on (infixl "(analytic'_on)" 50)
   1.603 +  where
   1.604 +   "f analytic_on s \<equiv> \<forall>x \<in> s. \<exists>e. 0 < e \<and> f holomorphic_on (ball x e)"
   1.605 +
   1.606 +lemma analytic_imp_holomorphic: "f analytic_on s \<Longrightarrow> f holomorphic_on s"
   1.607 +  by (simp add: at_within_open [OF _ open_ball] analytic_on_def holomorphic_on_def)
   1.608 +     (metis centre_in_ball field_differentiable_at_within)
   1.609 +
   1.610 +lemma analytic_on_open: "open s \<Longrightarrow> f analytic_on s \<longleftrightarrow> f holomorphic_on s"
   1.611 +apply (auto simp: analytic_imp_holomorphic)
   1.612 +apply (auto simp: analytic_on_def holomorphic_on_def)
   1.613 +by (metis holomorphic_on_def holomorphic_on_subset open_contains_ball)
   1.614 +
   1.615 +lemma analytic_on_imp_differentiable_at:
   1.616 +  "f analytic_on s \<Longrightarrow> x \<in> s \<Longrightarrow> f field_differentiable (at x)"
   1.617 + apply (auto simp: analytic_on_def holomorphic_on_def)
   1.618 +by (metis Topology_Euclidean_Space.open_ball centre_in_ball field_differentiable_within_open)
   1.619 +
   1.620 +lemma analytic_on_subset: "f analytic_on s \<Longrightarrow> t \<subseteq> s \<Longrightarrow> f analytic_on t"
   1.621 +  by (auto simp: analytic_on_def)
   1.622 +
   1.623 +lemma analytic_on_Un: "f analytic_on (s \<union> t) \<longleftrightarrow> f analytic_on s \<and> f analytic_on t"
   1.624 +  by (auto simp: analytic_on_def)
   1.625 +
   1.626 +lemma analytic_on_Union: "f analytic_on (\<Union>s) \<longleftrightarrow> (\<forall>t \<in> s. f analytic_on t)"
   1.627 +  by (auto simp: analytic_on_def)
   1.628 +
   1.629 +lemma analytic_on_UN: "f analytic_on (\<Union>i\<in>I. s i) \<longleftrightarrow> (\<forall>i\<in>I. f analytic_on (s i))"
   1.630 +  by (auto simp: analytic_on_def)
   1.631 +
   1.632 +lemma analytic_on_holomorphic:
   1.633 +  "f analytic_on s \<longleftrightarrow> (\<exists>t. open t \<and> s \<subseteq> t \<and> f holomorphic_on t)"
   1.634 +  (is "?lhs = ?rhs")
   1.635 +proof -
   1.636 +  have "?lhs \<longleftrightarrow> (\<exists>t. open t \<and> s \<subseteq> t \<and> f analytic_on t)"
   1.637 +  proof safe
   1.638 +    assume "f analytic_on s"
   1.639 +    then show "\<exists>t. open t \<and> s \<subseteq> t \<and> f analytic_on t"
   1.640 +      apply (simp add: analytic_on_def)
   1.641 +      apply (rule exI [where x="\<Union>{u. open u \<and> f analytic_on u}"], auto)
   1.642 +      apply (metis Topology_Euclidean_Space.open_ball analytic_on_open centre_in_ball)
   1.643 +      by (metis analytic_on_def)
   1.644 +  next
   1.645 +    fix t
   1.646 +    assume "open t" "s \<subseteq> t" "f analytic_on t"
   1.647 +    then show "f analytic_on s"
   1.648 +        by (metis analytic_on_subset)
   1.649 +  qed
   1.650 +  also have "... \<longleftrightarrow> ?rhs"
   1.651 +    by (auto simp: analytic_on_open)
   1.652 +  finally show ?thesis .
   1.653 +qed
   1.654 +
   1.655 +lemma analytic_on_linear: "(op * c) analytic_on s"
   1.656 +  by (auto simp add: analytic_on_holomorphic holomorphic_on_linear)
   1.657 +
   1.658 +lemma analytic_on_const: "(\<lambda>z. c) analytic_on s"
   1.659 +  by (metis analytic_on_def holomorphic_on_const zero_less_one)
   1.660 +
   1.661 +lemma analytic_on_ident: "(\<lambda>x. x) analytic_on s"
   1.662 +  by (simp add: analytic_on_def holomorphic_on_ident gt_ex)
   1.663 +
   1.664 +lemma analytic_on_id: "id analytic_on s"
   1.665 +  unfolding id_def by (rule analytic_on_ident)
   1.666 +
   1.667 +lemma analytic_on_compose:
   1.668 +  assumes f: "f analytic_on s"
   1.669 +      and g: "g analytic_on (f ` s)"
   1.670 +    shows "(g o f) analytic_on s"
   1.671 +unfolding analytic_on_def
   1.672 +proof (intro ballI)
   1.673 +  fix x
   1.674 +  assume x: "x \<in> s"
   1.675 +  then obtain e where e: "0 < e" and fh: "f holomorphic_on ball x e" using f
   1.676 +    by (metis analytic_on_def)
   1.677 +  obtain e' where e': "0 < e'" and gh: "g holomorphic_on ball (f x) e'" using g
   1.678 +    by (metis analytic_on_def g image_eqI x)
   1.679 +  have "isCont f x"
   1.680 +    by (metis analytic_on_imp_differentiable_at field_differentiable_imp_continuous_at f x)
   1.681 +  with e' obtain d where d: "0 < d" and fd: "f ` ball x d \<subseteq> ball (f x) e'"
   1.682 +     by (auto simp: continuous_at_ball)
   1.683 +  have "g \<circ> f holomorphic_on ball x (min d e)"
   1.684 +    apply (rule holomorphic_on_compose)
   1.685 +    apply (metis fh holomorphic_on_subset min.bounded_iff order_refl subset_ball)
   1.686 +    by (metis fd gh holomorphic_on_subset image_mono min.cobounded1 subset_ball)
   1.687 +  then show "\<exists>e>0. g \<circ> f holomorphic_on ball x e"
   1.688 +    by (metis d e min_less_iff_conj)
   1.689 +qed
   1.690 +
   1.691 +lemma analytic_on_compose_gen:
   1.692 +  "f analytic_on s \<Longrightarrow> g analytic_on t \<Longrightarrow> (\<And>z. z \<in> s \<Longrightarrow> f z \<in> t)
   1.693 +             \<Longrightarrow> g o f analytic_on s"
   1.694 +by (metis analytic_on_compose analytic_on_subset image_subset_iff)
   1.695 +
   1.696 +lemma analytic_on_neg:
   1.697 +  "f analytic_on s \<Longrightarrow> (\<lambda>z. -(f z)) analytic_on s"
   1.698 +by (metis analytic_on_holomorphic holomorphic_on_minus)
   1.699 +
   1.700 +lemma analytic_on_add:
   1.701 +  assumes f: "f analytic_on s"
   1.702 +      and g: "g analytic_on s"
   1.703 +    shows "(\<lambda>z. f z + g z) analytic_on s"
   1.704 +unfolding analytic_on_def
   1.705 +proof (intro ballI)
   1.706 +  fix z
   1.707 +  assume z: "z \<in> s"
   1.708 +  then obtain e where e: "0 < e" and fh: "f holomorphic_on ball z e" using f
   1.709 +    by (metis analytic_on_def)
   1.710 +  obtain e' where e': "0 < e'" and gh: "g holomorphic_on ball z e'" using g
   1.711 +    by (metis analytic_on_def g z)
   1.712 +  have "(\<lambda>z. f z + g z) holomorphic_on ball z (min e e')"
   1.713 +    apply (rule holomorphic_on_add)
   1.714 +    apply (metis fh holomorphic_on_subset min.bounded_iff order_refl subset_ball)
   1.715 +    by (metis gh holomorphic_on_subset min.bounded_iff order_refl subset_ball)
   1.716 +  then show "\<exists>e>0. (\<lambda>z. f z + g z) holomorphic_on ball z e"
   1.717 +    by (metis e e' min_less_iff_conj)
   1.718 +qed
   1.719 +
   1.720 +lemma analytic_on_diff:
   1.721 +  assumes f: "f analytic_on s"
   1.722 +      and g: "g analytic_on s"
   1.723 +    shows "(\<lambda>z. f z - g z) analytic_on s"
   1.724 +unfolding analytic_on_def
   1.725 +proof (intro ballI)
   1.726 +  fix z
   1.727 +  assume z: "z \<in> s"
   1.728 +  then obtain e where e: "0 < e" and fh: "f holomorphic_on ball z e" using f
   1.729 +    by (metis analytic_on_def)
   1.730 +  obtain e' where e': "0 < e'" and gh: "g holomorphic_on ball z e'" using g
   1.731 +    by (metis analytic_on_def g z)
   1.732 +  have "(\<lambda>z. f z - g z) holomorphic_on ball z (min e e')"
   1.733 +    apply (rule holomorphic_on_diff)
   1.734 +    apply (metis fh holomorphic_on_subset min.bounded_iff order_refl subset_ball)
   1.735 +    by (metis gh holomorphic_on_subset min.bounded_iff order_refl subset_ball)
   1.736 +  then show "\<exists>e>0. (\<lambda>z. f z - g z) holomorphic_on ball z e"
   1.737 +    by (metis e e' min_less_iff_conj)
   1.738 +qed
   1.739 +
   1.740 +lemma analytic_on_mult:
   1.741 +  assumes f: "f analytic_on s"
   1.742 +      and g: "g analytic_on s"
   1.743 +    shows "(\<lambda>z. f z * g z) analytic_on s"
   1.744 +unfolding analytic_on_def
   1.745 +proof (intro ballI)
   1.746 +  fix z
   1.747 +  assume z: "z \<in> s"
   1.748 +  then obtain e where e: "0 < e" and fh: "f holomorphic_on ball z e" using f
   1.749 +    by (metis analytic_on_def)
   1.750 +  obtain e' where e': "0 < e'" and gh: "g holomorphic_on ball z e'" using g
   1.751 +    by (metis analytic_on_def g z)
   1.752 +  have "(\<lambda>z. f z * g z) holomorphic_on ball z (min e e')"
   1.753 +    apply (rule holomorphic_on_mult)
   1.754 +    apply (metis fh holomorphic_on_subset min.bounded_iff order_refl subset_ball)
   1.755 +    by (metis gh holomorphic_on_subset min.bounded_iff order_refl subset_ball)
   1.756 +  then show "\<exists>e>0. (\<lambda>z. f z * g z) holomorphic_on ball z e"
   1.757 +    by (metis e e' min_less_iff_conj)
   1.758 +qed
   1.759 +
   1.760 +lemma analytic_on_inverse:
   1.761 +  assumes f: "f analytic_on s"
   1.762 +      and nz: "(\<And>z. z \<in> s \<Longrightarrow> f z \<noteq> 0)"
   1.763 +    shows "(\<lambda>z. inverse (f z)) analytic_on s"
   1.764 +unfolding analytic_on_def
   1.765 +proof (intro ballI)
   1.766 +  fix z
   1.767 +  assume z: "z \<in> s"
   1.768 +  then obtain e where e: "0 < e" and fh: "f holomorphic_on ball z e" using f
   1.769 +    by (metis analytic_on_def)
   1.770 +  have "continuous_on (ball z e) f"
   1.771 +    by (metis fh holomorphic_on_imp_continuous_on)
   1.772 +  then obtain e' where e': "0 < e'" and nz': "\<And>y. dist z y < e' \<Longrightarrow> f y \<noteq> 0"
   1.773 +    by (metis Topology_Euclidean_Space.open_ball centre_in_ball continuous_on_open_avoid e z nz)
   1.774 +  have "(\<lambda>z. inverse (f z)) holomorphic_on ball z (min e e')"
   1.775 +    apply (rule holomorphic_on_inverse)
   1.776 +    apply (metis fh holomorphic_on_subset min.cobounded2 min.commute subset_ball)
   1.777 +    by (metis nz' mem_ball min_less_iff_conj)
   1.778 +  then show "\<exists>e>0. (\<lambda>z. inverse (f z)) holomorphic_on ball z e"
   1.779 +    by (metis e e' min_less_iff_conj)
   1.780 +qed
   1.781 +
   1.782 +lemma analytic_on_divide:
   1.783 +  assumes f: "f analytic_on s"
   1.784 +      and g: "g analytic_on s"
   1.785 +      and nz: "(\<And>z. z \<in> s \<Longrightarrow> g z \<noteq> 0)"
   1.786 +    shows "(\<lambda>z. f z / g z) analytic_on s"
   1.787 +unfolding divide_inverse
   1.788 +by (metis analytic_on_inverse analytic_on_mult f g nz)
   1.789 +
   1.790 +lemma analytic_on_power:
   1.791 +  "f analytic_on s \<Longrightarrow> (\<lambda>z. (f z) ^ n) analytic_on s"
   1.792 +by (induct n) (auto simp: analytic_on_const analytic_on_mult)
   1.793 +
   1.794 +lemma analytic_on_setsum:
   1.795 +  "(\<And>i. i \<in> I \<Longrightarrow> (f i) analytic_on s) \<Longrightarrow> (\<lambda>x. setsum (\<lambda>i. f i x) I) analytic_on s"
   1.796 +  by (induct I rule: infinite_finite_induct) (auto simp: analytic_on_const analytic_on_add)
   1.797 +
   1.798 +lemma deriv_left_inverse:
   1.799 +  assumes "f holomorphic_on S" and "g holomorphic_on T"
   1.800 +      and "open S" and "open T"
   1.801 +      and "f ` S \<subseteq> T"
   1.802 +      and [simp]: "\<And>z. z \<in> S \<Longrightarrow> g (f z) = z"
   1.803 +      and "w \<in> S"
   1.804 +    shows "deriv f w * deriv g (f w) = 1"
   1.805 +proof -
   1.806 +  have "deriv f w * deriv g (f w) = deriv g (f w) * deriv f w"
   1.807 +    by (simp add: algebra_simps)
   1.808 +  also have "... = deriv (g o f) w"
   1.809 +    using assms
   1.810 +    by (metis analytic_on_imp_differentiable_at analytic_on_open complex_derivative_chain image_subset_iff)
   1.811 +  also have "... = deriv id w"
   1.812 +    apply (rule complex_derivative_transform_within_open [where s=S])
   1.813 +    apply (rule assms holomorphic_on_compose_gen holomorphic_intros)+
   1.814 +    apply simp
   1.815 +    done
   1.816 +  also have "... = 1"
   1.817 +    by simp
   1.818 +  finally show ?thesis .
   1.819 +qed
   1.820 +
   1.821 +subsection\<open>analyticity at a point\<close>
   1.822 +
   1.823 +lemma analytic_at_ball:
   1.824 +  "f analytic_on {z} \<longleftrightarrow> (\<exists>e. 0<e \<and> f holomorphic_on ball z e)"
   1.825 +by (metis analytic_on_def singleton_iff)
   1.826 +
   1.827 +lemma analytic_at:
   1.828 +    "f analytic_on {z} \<longleftrightarrow> (\<exists>s. open s \<and> z \<in> s \<and> f holomorphic_on s)"
   1.829 +by (metis analytic_on_holomorphic empty_subsetI insert_subset)
   1.830 +
   1.831 +lemma analytic_on_analytic_at:
   1.832 +    "f analytic_on s \<longleftrightarrow> (\<forall>z \<in> s. f analytic_on {z})"
   1.833 +by (metis analytic_at_ball analytic_on_def)
   1.834 +
   1.835 +lemma analytic_at_two:
   1.836 +  "f analytic_on {z} \<and> g analytic_on {z} \<longleftrightarrow>
   1.837 +   (\<exists>s. open s \<and> z \<in> s \<and> f holomorphic_on s \<and> g holomorphic_on s)"
   1.838 +  (is "?lhs = ?rhs")
   1.839 +proof
   1.840 +  assume ?lhs
   1.841 +  then obtain s t
   1.842 +    where st: "open s" "z \<in> s" "f holomorphic_on s"
   1.843 +              "open t" "z \<in> t" "g holomorphic_on t"
   1.844 +    by (auto simp: analytic_at)
   1.845 +  show ?rhs
   1.846 +    apply (rule_tac x="s \<inter> t" in exI)
   1.847 +    using st
   1.848 +    apply (auto simp: Diff_subset holomorphic_on_subset)
   1.849 +    done
   1.850 +next
   1.851 +  assume ?rhs
   1.852 +  then show ?lhs
   1.853 +    by (force simp add: analytic_at)
   1.854 +qed
   1.855 +
   1.856 +subsection\<open>Combining theorems for derivative with ``analytic at'' hypotheses\<close>
   1.857 +
   1.858 +lemma
   1.859 +  assumes "f analytic_on {z}" "g analytic_on {z}"
   1.860 +  shows complex_derivative_add_at: "deriv (\<lambda>w. f w + g w) z = deriv f z + deriv g z"
   1.861 +    and complex_derivative_diff_at: "deriv (\<lambda>w. f w - g w) z = deriv f z - deriv g z"
   1.862 +    and complex_derivative_mult_at: "deriv (\<lambda>w. f w * g w) z =
   1.863 +           f z * deriv g z + deriv f z * g z"
   1.864 +proof -
   1.865 +  obtain s where s: "open s" "z \<in> s" "f holomorphic_on s" "g holomorphic_on s"
   1.866 +    using assms by (metis analytic_at_two)
   1.867 +  show "deriv (\<lambda>w. f w + g w) z = deriv f z + deriv g z"
   1.868 +    apply (rule DERIV_imp_deriv [OF DERIV_add])
   1.869 +    using s
   1.870 +    apply (auto simp: holomorphic_on_open field_differentiable_def DERIV_deriv_iff_field_differentiable)
   1.871 +    done
   1.872 +  show "deriv (\<lambda>w. f w - g w) z = deriv f z - deriv g z"
   1.873 +    apply (rule DERIV_imp_deriv [OF DERIV_diff])
   1.874 +    using s
   1.875 +    apply (auto simp: holomorphic_on_open field_differentiable_def DERIV_deriv_iff_field_differentiable)
   1.876 +    done
   1.877 +  show "deriv (\<lambda>w. f w * g w) z = f z * deriv g z + deriv f z * g z"
   1.878 +    apply (rule DERIV_imp_deriv [OF DERIV_mult'])
   1.879 +    using s
   1.880 +    apply (auto simp: holomorphic_on_open field_differentiable_def DERIV_deriv_iff_field_differentiable)
   1.881 +    done
   1.882 +qed
   1.883 +
   1.884 +lemma deriv_cmult_at:
   1.885 +  "f analytic_on {z} \<Longrightarrow>  deriv (\<lambda>w. c * f w) z = c * deriv f z"
   1.886 +by (auto simp: complex_derivative_mult_at deriv_const analytic_on_const)
   1.887 +
   1.888 +lemma deriv_cmult_right_at:
   1.889 +  "f analytic_on {z} \<Longrightarrow>  deriv (\<lambda>w. f w * c) z = deriv f z * c"
   1.890 +by (auto simp: complex_derivative_mult_at deriv_const analytic_on_const)
   1.891 +
   1.892 +subsection\<open>Complex differentiation of sequences and series\<close>
   1.893 +
   1.894 +(* TODO: Could probably be simplified using Uniform_Limit *)
   1.895 +lemma has_complex_derivative_sequence:
   1.896 +  fixes s :: "complex set"
   1.897 +  assumes cvs: "convex s"
   1.898 +      and df:  "\<And>n x. x \<in> s \<Longrightarrow> (f n has_field_derivative f' n x) (at x within s)"
   1.899 +      and conv: "\<And>e. 0 < e \<Longrightarrow> \<exists>N. \<forall>n x. n \<ge> N \<longrightarrow> x \<in> s \<longrightarrow> norm (f' n x - g' x) \<le> e"
   1.900 +      and "\<exists>x l. x \<in> s \<and> ((\<lambda>n. f n x) \<longlongrightarrow> l) sequentially"
   1.901 +    shows "\<exists>g. \<forall>x \<in> s. ((\<lambda>n. f n x) \<longlongrightarrow> g x) sequentially \<and>
   1.902 +                       (g has_field_derivative (g' x)) (at x within s)"
   1.903 +proof -
   1.904 +  from assms obtain x l where x: "x \<in> s" and tf: "((\<lambda>n. f n x) \<longlongrightarrow> l) sequentially"
   1.905 +    by blast
   1.906 +  { fix e::real assume e: "e > 0"
   1.907 +    then obtain N where N: "\<forall>n\<ge>N. \<forall>x. x \<in> s \<longrightarrow> cmod (f' n x - g' x) \<le> e"
   1.908 +      by (metis conv)
   1.909 +    have "\<exists>N. \<forall>n\<ge>N. \<forall>x\<in>s. \<forall>h. cmod (f' n x * h - g' x * h) \<le> e * cmod h"
   1.910 +    proof (rule exI [of _ N], clarify)
   1.911 +      fix n y h
   1.912 +      assume "N \<le> n" "y \<in> s"
   1.913 +      then have "cmod (f' n y - g' y) \<le> e"
   1.914 +        by (metis N)
   1.915 +      then have "cmod h * cmod (f' n y - g' y) \<le> cmod h * e"
   1.916 +        by (auto simp: antisym_conv2 mult_le_cancel_left norm_triangle_ineq2)
   1.917 +      then show "cmod (f' n y * h - g' y * h) \<le> e * cmod h"
   1.918 +        by (simp add: norm_mult [symmetric] field_simps)
   1.919 +    qed
   1.920 +  } note ** = this
   1.921 +  show ?thesis
   1.922 +  unfolding has_field_derivative_def
   1.923 +  proof (rule has_derivative_sequence [OF cvs _ _ x])
   1.924 +    show "\<forall>n. \<forall>x\<in>s. (f n has_derivative (op * (f' n x))) (at x within s)"
   1.925 +      by (metis has_field_derivative_def df)
   1.926 +  next show "(\<lambda>n. f n x) \<longlonglongrightarrow> l"
   1.927 +    by (rule tf)
   1.928 +  next show "\<forall>e>0. \<exists>N. \<forall>n\<ge>N. \<forall>x\<in>s. \<forall>h. cmod (f' n x * h - g' x * h) \<le> e * cmod h"
   1.929 +    by (blast intro: **)
   1.930 +  qed
   1.931 +qed
   1.932 +
   1.933 +lemma has_complex_derivative_series:
   1.934 +  fixes s :: "complex set"
   1.935 +  assumes cvs: "convex s"
   1.936 +      and df:  "\<And>n x. x \<in> s \<Longrightarrow> (f n has_field_derivative f' n x) (at x within s)"
   1.937 +      and conv: "\<And>e. 0 < e \<Longrightarrow> \<exists>N. \<forall>n x. n \<ge> N \<longrightarrow> x \<in> s
   1.938 +                \<longrightarrow> cmod ((\<Sum>i<n. f' i x) - g' x) \<le> e"
   1.939 +      and "\<exists>x l. x \<in> s \<and> ((\<lambda>n. f n x) sums l)"
   1.940 +    shows "\<exists>g. \<forall>x \<in> s. ((\<lambda>n. f n x) sums g x) \<and> ((g has_field_derivative g' x) (at x within s))"
   1.941 +proof -
   1.942 +  from assms obtain x l where x: "x \<in> s" and sf: "((\<lambda>n. f n x) sums l)"
   1.943 +    by blast
   1.944 +  { fix e::real assume e: "e > 0"
   1.945 +    then obtain N where N: "\<forall>n x. n \<ge> N \<longrightarrow> x \<in> s
   1.946 +            \<longrightarrow> cmod ((\<Sum>i<n. f' i x) - g' x) \<le> e"
   1.947 +      by (metis conv)
   1.948 +    have "\<exists>N. \<forall>n\<ge>N. \<forall>x\<in>s. \<forall>h. cmod ((\<Sum>i<n. h * f' i x) - g' x * h) \<le> e * cmod h"
   1.949 +    proof (rule exI [of _ N], clarify)
   1.950 +      fix n y h
   1.951 +      assume "N \<le> n" "y \<in> s"
   1.952 +      then have "cmod ((\<Sum>i<n. f' i y) - g' y) \<le> e"
   1.953 +        by (metis N)
   1.954 +      then have "cmod h * cmod ((\<Sum>i<n. f' i y) - g' y) \<le> cmod h * e"
   1.955 +        by (auto simp: antisym_conv2 mult_le_cancel_left norm_triangle_ineq2)
   1.956 +      then show "cmod ((\<Sum>i<n. h * f' i y) - g' y * h) \<le> e * cmod h"
   1.957 +        by (simp add: norm_mult [symmetric] field_simps setsum_right_distrib)
   1.958 +    qed
   1.959 +  } note ** = this
   1.960 +  show ?thesis
   1.961 +  unfolding has_field_derivative_def
   1.962 +  proof (rule has_derivative_series [OF cvs _ _ x])
   1.963 +    fix n x
   1.964 +    assume "x \<in> s"
   1.965 +    then show "((f n) has_derivative (\<lambda>z. z * f' n x)) (at x within s)"
   1.966 +      by (metis df has_field_derivative_def mult_commute_abs)
   1.967 +  next show " ((\<lambda>n. f n x) sums l)"
   1.968 +    by (rule sf)
   1.969 +  next show "\<forall>e>0. \<exists>N. \<forall>n\<ge>N. \<forall>x\<in>s. \<forall>h. cmod ((\<Sum>i<n. h * f' i x) - g' x * h) \<le> e * cmod h"
   1.970 +    by (blast intro: **)
   1.971 +  qed
   1.972 +qed
   1.973 +
   1.974 +
   1.975 +lemma field_differentiable_series:
   1.976 +  fixes f :: "nat \<Rightarrow> complex \<Rightarrow> complex"
   1.977 +  assumes "convex s" "open s"
   1.978 +  assumes "\<And>n x. x \<in> s \<Longrightarrow> (f n has_field_derivative f' n x) (at x)"
   1.979 +  assumes "uniformly_convergent_on s (\<lambda>n x. \<Sum>i<n. f' i x)"
   1.980 +  assumes "x0 \<in> s" "summable (\<lambda>n. f n x0)" and x: "x \<in> s"
   1.981 +  shows   "summable (\<lambda>n. f n x)" and "(\<lambda>x. \<Sum>n. f n x) field_differentiable (at x)"
   1.982 +proof -
   1.983 +  from assms(4) obtain g' where A: "uniform_limit s (\<lambda>n x. \<Sum>i<n. f' i x) g' sequentially"
   1.984 +    unfolding uniformly_convergent_on_def by blast
   1.985 +  from x and \<open>open s\<close> have s: "at x within s = at x" by (rule at_within_open)
   1.986 +  have "\<exists>g. \<forall>x\<in>s. (\<lambda>n. f n x) sums g x \<and> (g has_field_derivative g' x) (at x within s)"
   1.987 +    by (intro has_field_derivative_series[of s f f' g' x0] assms A has_field_derivative_at_within)
   1.988 +  then obtain g where g: "\<And>x. x \<in> s \<Longrightarrow> (\<lambda>n. f n x) sums g x"
   1.989 +    "\<And>x. x \<in> s \<Longrightarrow> (g has_field_derivative g' x) (at x within s)" by blast
   1.990 +  from g[OF x] show "summable (\<lambda>n. f n x)" by (auto simp: summable_def)
   1.991 +  from g(2)[OF x] have g': "(g has_derivative op * (g' x)) (at x)"
   1.992 +    by (simp add: has_field_derivative_def s)
   1.993 +  have "((\<lambda>x. \<Sum>n. f n x) has_derivative op * (g' x)) (at x)"
   1.994 +    by (rule has_derivative_transform_within_open[OF g' \<open>open s\<close> x])
   1.995 +       (insert g, auto simp: sums_iff)
   1.996 +  thus "(\<lambda>x. \<Sum>n. f n x) field_differentiable (at x)" unfolding differentiable_def
   1.997 +    by (auto simp: summable_def field_differentiable_def has_field_derivative_def)
   1.998 +qed
   1.999 +
  1.1000 +lemma field_differentiable_series':
  1.1001 +  fixes f :: "nat \<Rightarrow> complex \<Rightarrow> complex"
  1.1002 +  assumes "convex s" "open s"
  1.1003 +  assumes "\<And>n x. x \<in> s \<Longrightarrow> (f n has_field_derivative f' n x) (at x)"
  1.1004 +  assumes "uniformly_convergent_on s (\<lambda>n x. \<Sum>i<n. f' i x)"
  1.1005 +  assumes "x0 \<in> s" "summable (\<lambda>n. f n x0)"
  1.1006 +  shows   "(\<lambda>x. \<Sum>n. f n x) field_differentiable (at x0)"
  1.1007 +  using field_differentiable_series[OF assms, of x0] \<open>x0 \<in> s\<close> by blast+
  1.1008 +
  1.1009 +subsection\<open>Bound theorem\<close>
  1.1010 +
  1.1011 +lemma field_differentiable_bound:
  1.1012 +  fixes s :: "complex set"
  1.1013 +  assumes cvs: "convex s"
  1.1014 +      and df:  "\<And>z. z \<in> s \<Longrightarrow> (f has_field_derivative f' z) (at z within s)"
  1.1015 +      and dn:  "\<And>z. z \<in> s \<Longrightarrow> norm (f' z) \<le> B"
  1.1016 +      and "x \<in> s"  "y \<in> s"
  1.1017 +    shows "norm(f x - f y) \<le> B * norm(x - y)"
  1.1018 +  apply (rule differentiable_bound [OF cvs])
  1.1019 +  apply (rule ballI, erule df [unfolded has_field_derivative_def])
  1.1020 +  apply (rule ballI, rule onorm_le, simp add: norm_mult mult_right_mono dn)
  1.1021 +  apply fact
  1.1022 +  apply fact
  1.1023 +  done
  1.1024 +
  1.1025 +subsection\<open>Inverse function theorem for complex derivatives\<close>
  1.1026 +
  1.1027 +lemma has_complex_derivative_inverse_basic:
  1.1028 +  fixes f :: "complex \<Rightarrow> complex"
  1.1029 +  shows "DERIV f (g y) :> f' \<Longrightarrow>
  1.1030 +        f' \<noteq> 0 \<Longrightarrow>
  1.1031 +        continuous (at y) g \<Longrightarrow>
  1.1032 +        open t \<Longrightarrow>
  1.1033 +        y \<in> t \<Longrightarrow>
  1.1034 +        (\<And>z. z \<in> t \<Longrightarrow> f (g z) = z)
  1.1035 +        \<Longrightarrow> DERIV g y :> inverse (f')"
  1.1036 +  unfolding has_field_derivative_def
  1.1037 +  apply (rule has_derivative_inverse_basic)
  1.1038 +  apply (auto simp:  bounded_linear_mult_right)
  1.1039 +  done
  1.1040 +
  1.1041 +(*Used only once, in Multivariate/cauchy.ml. *)
  1.1042 +lemma has_complex_derivative_inverse_strong:
  1.1043 +  fixes f :: "complex \<Rightarrow> complex"
  1.1044 +  shows "DERIV f x :> f' \<Longrightarrow>
  1.1045 +         f' \<noteq> 0 \<Longrightarrow>
  1.1046 +         open s \<Longrightarrow>
  1.1047 +         x \<in> s \<Longrightarrow>
  1.1048 +         continuous_on s f \<Longrightarrow>
  1.1049 +         (\<And>z. z \<in> s \<Longrightarrow> g (f z) = z)
  1.1050 +         \<Longrightarrow> DERIV g (f x) :> inverse (f')"
  1.1051 +  unfolding has_field_derivative_def
  1.1052 +  apply (rule has_derivative_inverse_strong [of s x f g ])
  1.1053 +  by auto
  1.1054 +
  1.1055 +lemma has_complex_derivative_inverse_strong_x:
  1.1056 +  fixes f :: "complex \<Rightarrow> complex"
  1.1057 +  shows  "DERIV f (g y) :> f' \<Longrightarrow>
  1.1058 +          f' \<noteq> 0 \<Longrightarrow>
  1.1059 +          open s \<Longrightarrow>
  1.1060 +          continuous_on s f \<Longrightarrow>
  1.1061 +          g y \<in> s \<Longrightarrow> f(g y) = y \<Longrightarrow>
  1.1062 +          (\<And>z. z \<in> s \<Longrightarrow> g (f z) = z)
  1.1063 +          \<Longrightarrow> DERIV g y :> inverse (f')"
  1.1064 +  unfolding has_field_derivative_def
  1.1065 +  apply (rule has_derivative_inverse_strong_x [of s g y f])
  1.1066 +  by auto
  1.1067 +
  1.1068 +subsection \<open>Taylor on Complex Numbers\<close>
  1.1069 +
  1.1070 +lemma setsum_Suc_reindex:
  1.1071 +  fixes f :: "nat \<Rightarrow> 'a::ab_group_add"
  1.1072 +    shows  "setsum f {0..n} = f 0 - f (Suc n) + setsum (\<lambda>i. f (Suc i)) {0..n}"
  1.1073 +by (induct n) auto
  1.1074 +
  1.1075 +lemma complex_taylor:
  1.1076 +  assumes s: "convex s"
  1.1077 +      and f: "\<And>i x. x \<in> s \<Longrightarrow> i \<le> n \<Longrightarrow> (f i has_field_derivative f (Suc i) x) (at x within s)"
  1.1078 +      and B: "\<And>x. x \<in> s \<Longrightarrow> cmod (f (Suc n) x) \<le> B"
  1.1079 +      and w: "w \<in> s"
  1.1080 +      and z: "z \<in> s"
  1.1081 +    shows "cmod(f 0 z - (\<Sum>i\<le>n. f i w * (z-w) ^ i / (fact i)))
  1.1082 +          \<le> B * cmod(z - w)^(Suc n) / fact n"
  1.1083 +proof -
  1.1084 +  have wzs: "closed_segment w z \<subseteq> s" using assms
  1.1085 +    by (metis convex_contains_segment)
  1.1086 +  { fix u
  1.1087 +    assume "u \<in> closed_segment w z"
  1.1088 +    then have "u \<in> s"
  1.1089 +      by (metis wzs subsetD)
  1.1090 +    have "(\<Sum>i\<le>n. f i u * (- of_nat i * (z-u)^(i - 1)) / (fact i) +
  1.1091 +                      f (Suc i) u * (z-u)^i / (fact i)) =
  1.1092 +              f (Suc n) u * (z-u) ^ n / (fact n)"
  1.1093 +    proof (induction n)
  1.1094 +      case 0 show ?case by simp
  1.1095 +    next
  1.1096 +      case (Suc n)
  1.1097 +      have "(\<Sum>i\<le>Suc n. f i u * (- of_nat i * (z-u) ^ (i - 1)) / (fact i) +
  1.1098 +                             f (Suc i) u * (z-u) ^ i / (fact i)) =
  1.1099 +           f (Suc n) u * (z-u) ^ n / (fact n) +
  1.1100 +           f (Suc (Suc n)) u * ((z-u) * (z-u) ^ n) / (fact (Suc n)) -
  1.1101 +           f (Suc n) u * ((1 + of_nat n) * (z-u) ^ n) / (fact (Suc n))"
  1.1102 +        using Suc by simp
  1.1103 +      also have "... = f (Suc (Suc n)) u * (z-u) ^ Suc n / (fact (Suc n))"
  1.1104 +      proof -
  1.1105 +        have "(fact(Suc n)) *
  1.1106 +             (f(Suc n) u *(z-u) ^ n / (fact n) +
  1.1107 +               f(Suc(Suc n)) u *((z-u) *(z-u) ^ n) / (fact(Suc n)) -
  1.1108 +               f(Suc n) u *((1 + of_nat n) *(z-u) ^ n) / (fact(Suc n))) =
  1.1109 +            ((fact(Suc n)) *(f(Suc n) u *(z-u) ^ n)) / (fact n) +
  1.1110 +            ((fact(Suc n)) *(f(Suc(Suc n)) u *((z-u) *(z-u) ^ n)) / (fact(Suc n))) -
  1.1111 +            ((fact(Suc n)) *(f(Suc n) u *(of_nat(Suc n) *(z-u) ^ n))) / (fact(Suc n))"
  1.1112 +          by (simp add: algebra_simps del: fact_Suc)
  1.1113 +        also have "... = ((fact (Suc n)) * (f (Suc n) u * (z-u) ^ n)) / (fact n) +
  1.1114 +                         (f (Suc (Suc n)) u * ((z-u) * (z-u) ^ n)) -
  1.1115 +                         (f (Suc n) u * ((1 + of_nat n) * (z-u) ^ n))"
  1.1116 +          by (simp del: fact_Suc)
  1.1117 +        also have "... = (of_nat (Suc n) * (f (Suc n) u * (z-u) ^ n)) +
  1.1118 +                         (f (Suc (Suc n)) u * ((z-u) * (z-u) ^ n)) -
  1.1119 +                         (f (Suc n) u * ((1 + of_nat n) * (z-u) ^ n))"
  1.1120 +          by (simp only: fact_Suc of_nat_mult ac_simps) simp
  1.1121 +        also have "... = f (Suc (Suc n)) u * ((z-u) * (z-u) ^ n)"
  1.1122 +          by (simp add: algebra_simps)
  1.1123 +        finally show ?thesis
  1.1124 +        by (simp add: mult_left_cancel [where c = "(fact (Suc n))", THEN iffD1] del: fact_Suc)
  1.1125 +      qed
  1.1126 +      finally show ?case .
  1.1127 +    qed
  1.1128 +    then have "((\<lambda>v. (\<Sum>i\<le>n. f i v * (z - v)^i / (fact i)))
  1.1129 +                has_field_derivative f (Suc n) u * (z-u) ^ n / (fact n))
  1.1130 +               (at u within s)"
  1.1131 +      apply (intro derivative_eq_intros)
  1.1132 +      apply (blast intro: assms \<open>u \<in> s\<close>)
  1.1133 +      apply (rule refl)+
  1.1134 +      apply (auto simp: field_simps)
  1.1135 +      done
  1.1136 +  } note sum_deriv = this
  1.1137 +  { fix u
  1.1138 +    assume u: "u \<in> closed_segment w z"
  1.1139 +    then have us: "u \<in> s"
  1.1140 +      by (metis wzs subsetD)
  1.1141 +    have "cmod (f (Suc n) u) * cmod (z - u) ^ n \<le> cmod (f (Suc n) u) * cmod (u - z) ^ n"
  1.1142 +      by (metis norm_minus_commute order_refl)
  1.1143 +    also have "... \<le> cmod (f (Suc n) u) * cmod (z - w) ^ n"
  1.1144 +      by (metis mult_left_mono norm_ge_zero power_mono segment_bound [OF u])
  1.1145 +    also have "... \<le> B * cmod (z - w) ^ n"
  1.1146 +      by (metis norm_ge_zero zero_le_power mult_right_mono  B [OF us])
  1.1147 +    finally have "cmod (f (Suc n) u) * cmod (z - u) ^ n \<le> B * cmod (z - w) ^ n" .
  1.1148 +  } note cmod_bound = this
  1.1149 +  have "(\<Sum>i\<le>n. f i z * (z - z) ^ i / (fact i)) = (\<Sum>i\<le>n. (f i z / (fact i)) * 0 ^ i)"
  1.1150 +    by simp
  1.1151 +  also have "\<dots> = f 0 z / (fact 0)"
  1.1152 +    by (subst setsum_zero_power) simp
  1.1153 +  finally have "cmod (f 0 z - (\<Sum>i\<le>n. f i w * (z - w) ^ i / (fact i)))
  1.1154 +                \<le> cmod ((\<Sum>i\<le>n. f i w * (z - w) ^ i / (fact i)) -
  1.1155 +                        (\<Sum>i\<le>n. f i z * (z - z) ^ i / (fact i)))"
  1.1156 +    by (simp add: norm_minus_commute)
  1.1157 +  also have "... \<le> B * cmod (z - w) ^ n / (fact n) * cmod (w - z)"
  1.1158 +    apply (rule field_differentiable_bound
  1.1159 +      [where f' = "\<lambda>w. f (Suc n) w * (z - w)^n / (fact n)"
  1.1160 +         and s = "closed_segment w z", OF convex_closed_segment])
  1.1161 +    apply (auto simp: ends_in_segment DERIV_subset [OF sum_deriv wzs]
  1.1162 +                  norm_divide norm_mult norm_power divide_le_cancel cmod_bound)
  1.1163 +    done
  1.1164 +  also have "...  \<le> B * cmod (z - w) ^ Suc n / (fact n)"
  1.1165 +    by (simp add: algebra_simps norm_minus_commute)
  1.1166 +  finally show ?thesis .
  1.1167 +qed
  1.1168 +
  1.1169 +text\<open>Something more like the traditional MVT for real components\<close>
  1.1170 +
  1.1171 +lemma complex_mvt_line:
  1.1172 +  assumes "\<And>u. u \<in> closed_segment w z \<Longrightarrow> (f has_field_derivative f'(u)) (at u)"
  1.1173 +    shows "\<exists>u. u \<in> closed_segment w z \<and> Re(f z) - Re(f w) = Re(f'(u) * (z - w))"
  1.1174 +proof -
  1.1175 +  have twz: "\<And>t. (1 - t) *\<^sub>R w + t *\<^sub>R z = w + t *\<^sub>R (z - w)"
  1.1176 +    by (simp add: real_vector.scale_left_diff_distrib real_vector.scale_right_diff_distrib)
  1.1177 +  note assms[unfolded has_field_derivative_def, derivative_intros]
  1.1178 +  show ?thesis
  1.1179 +    apply (cut_tac mvt_simple
  1.1180 +                     [of 0 1 "Re o f o (\<lambda>t. (1 - t) *\<^sub>R w +  t *\<^sub>R z)"
  1.1181 +                      "\<lambda>u. Re o (\<lambda>h. f'((1 - u) *\<^sub>R w + u *\<^sub>R z) * h) o (\<lambda>t. t *\<^sub>R (z - w))"])
  1.1182 +    apply auto
  1.1183 +    apply (rule_tac x="(1 - x) *\<^sub>R w + x *\<^sub>R z" in exI)
  1.1184 +    apply (auto simp: closed_segment_def twz) []
  1.1185 +    apply (intro derivative_eq_intros has_derivative_at_within, simp_all)
  1.1186 +    apply (simp add: fun_eq_iff real_vector.scale_right_diff_distrib)
  1.1187 +    apply (force simp: twz closed_segment_def)
  1.1188 +    done
  1.1189 +qed
  1.1190 +
  1.1191 +lemma complex_taylor_mvt:
  1.1192 +  assumes "\<And>i x. \<lbrakk>x \<in> closed_segment w z; i \<le> n\<rbrakk> \<Longrightarrow> ((f i) has_field_derivative f (Suc i) x) (at x)"
  1.1193 +    shows "\<exists>u. u \<in> closed_segment w z \<and>
  1.1194 +            Re (f 0 z) =
  1.1195 +            Re ((\<Sum>i = 0..n. f i w * (z - w) ^ i / (fact i)) +
  1.1196 +                (f (Suc n) u * (z-u)^n / (fact n)) * (z - w))"
  1.1197 +proof -
  1.1198 +  { fix u
  1.1199 +    assume u: "u \<in> closed_segment w z"
  1.1200 +    have "(\<Sum>i = 0..n.
  1.1201 +               (f (Suc i) u * (z-u) ^ i - of_nat i * (f i u * (z-u) ^ (i - Suc 0))) /
  1.1202 +               (fact i)) =
  1.1203 +          f (Suc 0) u -
  1.1204 +             (f (Suc (Suc n)) u * ((z-u) ^ Suc n) - (of_nat (Suc n)) * (z-u) ^ n * f (Suc n) u) /
  1.1205 +             (fact (Suc n)) +
  1.1206 +             (\<Sum>i = 0..n.
  1.1207 +                 (f (Suc (Suc i)) u * ((z-u) ^ Suc i) - of_nat (Suc i) * (f (Suc i) u * (z-u) ^ i)) /
  1.1208 +                 (fact (Suc i)))"
  1.1209 +       by (subst setsum_Suc_reindex) simp
  1.1210 +    also have "... = f (Suc 0) u -
  1.1211 +             (f (Suc (Suc n)) u * ((z-u) ^ Suc n) - (of_nat (Suc n)) * (z-u) ^ n * f (Suc n) u) /
  1.1212 +             (fact (Suc n)) +
  1.1213 +             (\<Sum>i = 0..n.
  1.1214 +                 f (Suc (Suc i)) u * ((z-u) ^ Suc i) / (fact (Suc i))  -
  1.1215 +                 f (Suc i) u * (z-u) ^ i / (fact i))"
  1.1216 +      by (simp only: diff_divide_distrib fact_cancel ac_simps)
  1.1217 +    also have "... = f (Suc 0) u -
  1.1218 +             (f (Suc (Suc n)) u * (z-u) ^ Suc n - of_nat (Suc n) * (z-u) ^ n * f (Suc n) u) /
  1.1219 +             (fact (Suc n)) +
  1.1220 +             f (Suc (Suc n)) u * (z-u) ^ Suc n / (fact (Suc n)) - f (Suc 0) u"
  1.1221 +      by (subst setsum_Suc_diff) auto
  1.1222 +    also have "... = f (Suc n) u * (z-u) ^ n / (fact n)"
  1.1223 +      by (simp only: algebra_simps diff_divide_distrib fact_cancel)
  1.1224 +    finally have "(\<Sum>i = 0..n. (f (Suc i) u * (z - u) ^ i
  1.1225 +                             - of_nat i * (f i u * (z-u) ^ (i - Suc 0))) / (fact i)) =
  1.1226 +                  f (Suc n) u * (z - u) ^ n / (fact n)" .
  1.1227 +    then have "((\<lambda>u. \<Sum>i = 0..n. f i u * (z - u) ^ i / (fact i)) has_field_derivative
  1.1228 +                f (Suc n) u * (z - u) ^ n / (fact n))  (at u)"
  1.1229 +      apply (intro derivative_eq_intros)+
  1.1230 +      apply (force intro: u assms)
  1.1231 +      apply (rule refl)+
  1.1232 +      apply (auto simp: ac_simps)
  1.1233 +      done
  1.1234 +  }
  1.1235 +  then show ?thesis
  1.1236 +    apply (cut_tac complex_mvt_line [of w z "\<lambda>u. \<Sum>i = 0..n. f i u * (z-u) ^ i / (fact i)"
  1.1237 +               "\<lambda>u. (f (Suc n) u * (z-u)^n / (fact n))"])
  1.1238 +    apply (auto simp add: intro: open_closed_segment)
  1.1239 +    done
  1.1240 +qed
  1.1241 +
  1.1242 +
  1.1243 +subsection \<open>Polynomal function extremal theorem, from HOL Light\<close>
  1.1244 +
  1.1245 +lemma polyfun_extremal_lemma: (*COMPLEX_POLYFUN_EXTREMAL_LEMMA in HOL Light*)
  1.1246 +    fixes c :: "nat \<Rightarrow> 'a::real_normed_div_algebra"
  1.1247 +  assumes "0 < e"
  1.1248 +    shows "\<exists>M. \<forall>z. M \<le> norm(z) \<longrightarrow> norm (\<Sum>i\<le>n. c(i) * z^i) \<le> e * norm(z) ^ (Suc n)"
  1.1249 +proof (induct n)
  1.1250 +  case 0 with assms
  1.1251 +  show ?case
  1.1252 +    apply (rule_tac x="norm (c 0) / e" in exI)
  1.1253 +    apply (auto simp: field_simps)
  1.1254 +    done
  1.1255 +next
  1.1256 +  case (Suc n)
  1.1257 +  obtain M where M: "\<And>z. M \<le> norm z \<Longrightarrow> norm (\<Sum>i\<le>n. c i * z^i) \<le> e * norm z ^ Suc n"
  1.1258 +    using Suc assms by blast
  1.1259 +  show ?case
  1.1260 +  proof (rule exI [where x= "max M (1 + norm(c(Suc n)) / e)"], clarsimp simp del: power_Suc)
  1.1261 +    fix z::'a
  1.1262 +    assume z1: "M \<le> norm z" and "1 + norm (c (Suc n)) / e \<le> norm z"
  1.1263 +    then have z2: "e + norm (c (Suc n)) \<le> e * norm z"
  1.1264 +      using assms by (simp add: field_simps)
  1.1265 +    have "norm (\<Sum>i\<le>n. c i * z^i) \<le> e * norm z ^ Suc n"
  1.1266 +      using M [OF z1] by simp
  1.1267 +    then have "norm (\<Sum>i\<le>n. c i * z^i) + norm (c (Suc n) * z ^ Suc n) \<le> e * norm z ^ Suc n + norm (c (Suc n) * z ^ Suc n)"
  1.1268 +      by simp
  1.1269 +    then have "norm ((\<Sum>i\<le>n. c i * z^i) + c (Suc n) * z ^ Suc n) \<le> e * norm z ^ Suc n + norm (c (Suc n) * z ^ Suc n)"
  1.1270 +      by (blast intro: norm_triangle_le elim: )
  1.1271 +    also have "... \<le> (e + norm (c (Suc n))) * norm z ^ Suc n"
  1.1272 +      by (simp add: norm_power norm_mult algebra_simps)
  1.1273 +    also have "... \<le> (e * norm z) * norm z ^ Suc n"
  1.1274 +      by (metis z2 mult.commute mult_left_mono norm_ge_zero norm_power)
  1.1275 +    finally show "norm ((\<Sum>i\<le>n. c i * z^i) + c (Suc n) * z ^ Suc n) \<le> e * norm z ^ Suc (Suc n)"
  1.1276 +      by simp
  1.1277 +  qed
  1.1278 +qed
  1.1279 +
  1.1280 +lemma polyfun_extremal: (*COMPLEX_POLYFUN_EXTREMAL in HOL Light*)
  1.1281 +    fixes c :: "nat \<Rightarrow> 'a::real_normed_div_algebra"
  1.1282 +  assumes k: "c k \<noteq> 0" "1\<le>k" and kn: "k\<le>n"
  1.1283 +    shows "eventually (\<lambda>z. norm (\<Sum>i\<le>n. c(i) * z^i) \<ge> B) at_infinity"
  1.1284 +using kn
  1.1285 +proof (induction n)
  1.1286 +  case 0
  1.1287 +  then show ?case
  1.1288 +    using k  by simp
  1.1289 +next
  1.1290 +  case (Suc m)
  1.1291 +  let ?even = ?case
  1.1292 +  show ?even
  1.1293 +  proof (cases "c (Suc m) = 0")
  1.1294 +    case True
  1.1295 +    then show ?even using Suc k
  1.1296 +      by auto (metis antisym_conv less_eq_Suc_le not_le)
  1.1297 +  next
  1.1298 +    case False
  1.1299 +    then obtain M where M:
  1.1300 +          "\<And>z. M \<le> norm z \<Longrightarrow> norm (\<Sum>i\<le>m. c i * z^i) \<le> norm (c (Suc m)) / 2 * norm z ^ Suc m"
  1.1301 +      using polyfun_extremal_lemma [of "norm(c (Suc m)) / 2" c m] Suc
  1.1302 +      by auto
  1.1303 +    have "\<exists>b. \<forall>z. b \<le> norm z \<longrightarrow> B \<le> norm (\<Sum>i\<le>Suc m. c i * z^i)"
  1.1304 +    proof (rule exI [where x="max M (max 1 (\<bar>B\<bar> / (norm(c (Suc m)) / 2)))"], clarsimp simp del: power_Suc)
  1.1305 +      fix z::'a
  1.1306 +      assume z1: "M \<le> norm z" "1 \<le> norm z"
  1.1307 +         and "\<bar>B\<bar> * 2 / norm (c (Suc m)) \<le> norm z"
  1.1308 +      then have z2: "\<bar>B\<bar> \<le> norm (c (Suc m)) * norm z / 2"
  1.1309 +        using False by (simp add: field_simps)
  1.1310 +      have nz: "norm z \<le> norm z ^ Suc m"
  1.1311 +        by (metis \<open>1 \<le> norm z\<close> One_nat_def less_eq_Suc_le power_increasing power_one_right zero_less_Suc)
  1.1312 +      have *: "\<And>y x. norm (c (Suc m)) * norm z / 2 \<le> norm y - norm x \<Longrightarrow> B \<le> norm (x + y)"
  1.1313 +        by (metis abs_le_iff add.commute norm_diff_ineq order_trans z2)
  1.1314 +      have "norm z * norm (c (Suc m)) + 2 * norm (\<Sum>i\<le>m. c i * z^i)
  1.1315 +            \<le> norm (c (Suc m)) * norm z + norm (c (Suc m)) * norm z ^ Suc m"
  1.1316 +        using M [of z] Suc z1  by auto
  1.1317 +      also have "... \<le> 2 * (norm (c (Suc m)) * norm z ^ Suc m)"
  1.1318 +        using nz by (simp add: mult_mono del: power_Suc)
  1.1319 +      finally show "B \<le> norm ((\<Sum>i\<le>m. c i * z^i) + c (Suc m) * z ^ Suc m)"
  1.1320 +        using Suc.IH
  1.1321 +        apply (auto simp: eventually_at_infinity)
  1.1322 +        apply (rule *)
  1.1323 +        apply (simp add: field_simps norm_mult norm_power)
  1.1324 +        done
  1.1325 +    qed
  1.1326 +    then show ?even
  1.1327 +      by (simp add: eventually_at_infinity)
  1.1328 +  qed
  1.1329 +qed
  1.1330 +
  1.1331 +end