# Theory Complex_Analysis_Basics

(*  Author: John Harrison, Marco Maggesi, Graziano Gentili, Gianni Ciolli, Valentina Bruno
Ported from "hol_light/Multivariate/canal.ml" by L C Paulson (2014)
*)

section ‹Complex Analysis Basics›
text ‹Definitions of analytic and holomorphic functions, limit theorems, complex differentiation›

theory Complex_Analysis_Basics
imports Derivative  Uncountable_Sets
begin

subsectiontag unimportant›‹General lemmas›

lemma nonneg_Reals_cmod_eq_Re: "z  0  norm z = Re z"

lemma fact_cancel:
fixes c :: "'a::real_field"
shows "of_nat (Suc n) * c / (fact (Suc n)) = c / (fact n)"
using of_nat_neq_0 by force

lemma vector_derivative_cnj_within:
assumes "at x within A  bot" and
shows   "vector_derivative (λz. cnj (f z)) (at x within A) =
cnj (vector_derivative f (at x within A))" (is "_ = cnj ?D")
proof -
let ?D = "vector_derivative f (at x within A)"
from assms have "(f has_vector_derivative ?D) (at x within A)"
by (subst (asm) vector_derivative_works)
hence "((λx. cnj (f x)) has_vector_derivative cnj ?D) (at x within A)"
by (rule has_vector_derivative_cnj)
thus ?thesis using assms by (auto dest: vector_derivative_within)
qed

lemma vector_derivative_cnj:
assumes "f differentiable at x"
shows   "vector_derivative (λz. cnj (f z)) (at x) = cnj (vector_derivative f (at x))"
using assms by (intro vector_derivative_cnj_within) auto

lemma
shows open_halfspace_Re_lt: "open {z. Re(z) < b}"
and open_halfspace_Re_gt: "open {z. Re(z) > b}"
and closed_halfspace_Re_ge: "closed {z. Re(z)  b}"
and closed_halfspace_Re_le: "closed {z. Re(z)  b}"
and closed_halfspace_Re_eq: "closed {z. Re(z) = b}"
and open_halfspace_Im_lt: "open {z. Im(z) < b}"
and open_halfspace_Im_gt: "open {z. Im(z) > b}"
and closed_halfspace_Im_ge: "closed {z. Im(z)  b}"
and closed_halfspace_Im_le: "closed {z. Im(z)  b}"
and closed_halfspace_Im_eq: "closed {z. Im(z) = b}"
by (intro open_Collect_less closed_Collect_le closed_Collect_eq continuous_on_Re
continuous_on_Im continuous_on_id continuous_on_const)+

lemma uncountable_halfspace_Im_gt: "uncountable {z. Im z > c}"
proof -
obtain r where r: "r > 0" "ball ((c + 1) *R 𝗂) r  {z. Im z > c}"
using open_halfspace_Im_gt[of c] unfolding open_contains_ball by force
then show ?thesis
using countable_subset uncountable_ball by blast
qed

lemma uncountable_halfspace_Im_lt: "uncountable {z. Im z < c}"
proof -
obtain r where r: "r > 0" "ball ((c - 1) *R 𝗂) r  {z. Im z < c}"
using open_halfspace_Im_lt[of c] unfolding open_contains_ball by force
then show ?thesis
using countable_subset uncountable_ball by blast
qed

lemma uncountable_halfspace_Re_gt: "uncountable {z. Re z > c}"
proof -
obtain r where r: "r > 0" "ball (of_real(c + 1)) r  {z. Re z > c}"
using open_halfspace_Re_gt[of c] unfolding open_contains_ball by force
then show ?thesis
using countable_subset uncountable_ball by blast
qed

lemma uncountable_halfspace_Re_lt: "uncountable {z. Re z < c}"
proof -
obtain r where r: "r > 0" "ball (of_real(c - 1)) r  {z. Re z < c}"
using open_halfspace_Re_lt[of c] unfolding open_contains_ball by force
then show ?thesis
using countable_subset uncountable_ball by blast
qed

lemma connected_halfspace_Im_gt [intro]: "connected {z. c < Im z}"
by (intro convex_connected convex_halfspace_Im_gt)

lemma connected_halfspace_Im_lt [intro]: "connected {z. c > Im z}"
by (intro convex_connected convex_halfspace_Im_lt)

lemma connected_halfspace_Re_gt [intro]: "connected {z. c < Re z}"
by (intro convex_connected convex_halfspace_Re_gt)

lemma connected_halfspace_Re_lt [intro]: "connected {z. c > Re z}"
by (intro convex_connected convex_halfspace_Re_lt)

lemma closed_complex_Reals: "closed ( :: complex set)"
proof -
have "( :: complex set) = {z. Im z = 0}"
by (auto simp: complex_is_Real_iff)
then show ?thesis
by (metis closed_halfspace_Im_eq)
qed

lemma closed_Real_halfspace_Re_le: "closed (  {w. Re w  x})"
by (simp add: closed_Int closed_complex_Reals closed_halfspace_Re_le)

lemma closed_nonpos_Reals_complex [simp]: "closed (0 :: complex set)"
proof -
have "0 =   {z. Re(z)  0}"
using complex_nonpos_Reals_iff complex_is_Real_iff by auto
then show ?thesis
by (metis closed_Real_halfspace_Re_le)
qed

lemma closed_Real_halfspace_Re_ge: "closed (  {w. x  Re(w)})"
using closed_halfspace_Re_ge

lemma closed_nonneg_Reals_complex [simp]: "closed (0 :: complex set)"
proof -
have "0 =   {z. Re(z)  0}"
using complex_nonneg_Reals_iff complex_is_Real_iff by auto
then show ?thesis
by (metis closed_Real_halfspace_Re_ge)
qed

lemma closed_real_abs_le: "closed {w  . ¦Re w¦  r}"
proof -
have "{w  . ¦Re w¦  r} = (  {w. Re w  r})  (  {w. Re w  -r})"
by auto
then show "closed {w  . ¦Re w¦  r}"
by (simp add: closed_Int closed_Real_halfspace_Re_ge closed_Real_halfspace_Re_le)
qed

lemma real_lim:
fixes l::complex
assumes "(f  l) F" and  and "eventually P F" and "a. P a  f a  "
shows  "l  "
proof (rule Lim_in_closed_set[OF closed_complex_Reals _ assms(2,1)])
show "eventually (λx. f x  ) F"
using assms(3, 4) by (auto intro: eventually_mono)
qed

lemma real_lim_sequentially:
fixes l::complex
shows "(f  l) sequentially  (N. nN. f n  )  l  "
by (rule real_lim [where F=]) (auto simp: eventually_sequentially)

lemma real_series:
fixes l::complex
shows "f sums l  (n. f n  )  l  "
unfolding sums_def
by (metis real_lim_sequentially sum_in_Reals)

lemma Lim_null_comparison_Re:
assumes "eventually (λx. norm(f x)  Re(g x)) F" "(g  0) F" shows "(f  0) F"
by (rule Lim_null_comparison[OF assms(1)] tendsto_eq_intros assms(2))+ simp

subsection‹Holomorphic functions›

definitiontag important› holomorphic_on :: "[complex  complex, complex set]  bool"
(infixl "(holomorphic'_on)" 50)
where "f holomorphic_on s  xs. f field_differentiable (at x within s)"

named_theoremstag important› holomorphic_intros "structural introduction rules for holomorphic_on"

lemma holomorphic_onI [intro?]: "(x. x  s  f field_differentiable (at x within s))  f holomorphic_on s"

lemma holomorphic_onD [dest?]: "f holomorphic_on s; x  s  f field_differentiable (at x within s)"

lemma holomorphic_on_imp_differentiable_on:

unfolding holomorphic_on_def differentiable_on_def

lemma holomorphic_on_imp_differentiable_at:
"f holomorphic_on s; open s; x  s  f field_differentiable (at x)"
using at_within_open holomorphic_on_def by fastforce

lemma holomorphic_on_empty [holomorphic_intros]:

lemma holomorphic_on_open:
"open s  f holomorphic_on s  (x  s. f'. DERIV f x :> f')"
by (auto simp: holomorphic_on_def field_differentiable_def has_field_derivative_def at_within_open [of _ s])

lemma holomorphic_on_UN_open:
assumes "n. n  I  f holomorphic_on A n" "n. n  I  open (A n)"
shows   "f holomorphic_on (nI. A n)"
proof -
have "f field_differentiable at z within (nI. A n)" if "z  (nI. A n)" for z
proof -
from that obtain n where "n  I" "z  A n"
by blast
hence "f holomorphic_on A n" "open (A n)"
with z  A n have
by (auto simp: holomorphic_on_open field_differentiable_def)
thus ?thesis
by (meson field_differentiable_at_within)
qed
thus ?thesis
by (auto simp: holomorphic_on_def)
qed

lemma holomorphic_on_imp_continuous_on:
"f holomorphic_on s  continuous_on s f"
by (metis field_differentiable_imp_continuous_at continuous_on_eq_continuous_within holomorphic_on_def)

lemma holomorphic_closedin_preimage_constant:
assumes "f holomorphic_on D"
shows "closedin (top_of_set D) {zD. f z = a}"
by (simp add: assms continuous_closedin_preimage_constant holomorphic_on_imp_continuous_on)

lemma holomorphic_closed_preimage_constant:
assumes
shows "closed {z. f z = a}"
using holomorphic_closedin_preimage_constant [OF assms] by simp

lemma holomorphic_on_subset [elim]:
"f holomorphic_on s  t  s  f holomorphic_on t"
unfolding holomorphic_on_def
by (metis field_differentiable_within_subset subsetD)

lemma holomorphic_transform: "f holomorphic_on s; x. x  s  f x = g x  g holomorphic_on s"
by (metis field_differentiable_transform_within linordered_field_no_ub holomorphic_on_def)

lemma holomorphic_cong: "s = t ==> (x. x  s  f x = g x)  f holomorphic_on s  g holomorphic_on t"
by (metis holomorphic_transform)

lemma holomorphic_on_linear [simp, holomorphic_intros]: "((*) c) holomorphic_on s"
unfolding holomorphic_on_def by (metis field_differentiable_linear)

lemma holomorphic_on_const [simp, holomorphic_intros]: "(λz. c) holomorphic_on s"
unfolding holomorphic_on_def by (metis field_differentiable_const)

lemma holomorphic_on_ident [simp, holomorphic_intros]: "(λx. x) holomorphic_on s"
unfolding holomorphic_on_def by (metis field_differentiable_ident)

lemma holomorphic_on_id [simp, holomorphic_intros]:
unfolding id_def by (rule holomorphic_on_ident)

lemma constant_on_imp_holomorphic_on:
assumes "f constant_on A"
shows   "f holomorphic_on A"
proof -
from assms obtain c where c: "xA. f x = c"
unfolding constant_on_def by blast
have "f holomorphic_on A  (λ_. c) holomorphic_on A"
by (intro holomorphic_cong) (use c in auto)
thus ?thesis
by simp
qed

lemma holomorphic_on_compose:
"f holomorphic_on s  g holomorphic_on (f ` s)  (g o f) holomorphic_on s"
using field_differentiable_compose_within[of f _ s g]
by (auto simp: holomorphic_on_def)

lemma holomorphic_on_compose_gen:
"f holomorphic_on s  g holomorphic_on t  f ` s  t  (g o f) holomorphic_on s"
by (metis holomorphic_on_compose holomorphic_on_subset)

lemma holomorphic_on_balls_imp_entire:
assumes "¬bdd_above A" "r. r  A  f holomorphic_on ball c r"
shows   "f holomorphic_on B"
proof (rule holomorphic_on_subset)
show  unfolding holomorphic_on_def
proof
fix z :: complex
from ¬bdd_above A obtain r where r: "r  A" "r > norm (z - c)"
by (meson bdd_aboveI not_le)
with assms(2) have "f holomorphic_on ball c r" by blast
moreover from r have "z  ball c r" by (auto simp: dist_norm norm_minus_commute)
ultimately show
by (auto simp: holomorphic_on_def at_within_open[of _ "ball c r"])
qed
qed auto

lemma holomorphic_on_balls_imp_entire':
assumes "r. r > 0  f holomorphic_on ball c r"
shows   "f holomorphic_on B"
proof (rule holomorphic_on_balls_imp_entire)
{
fix M :: real
have "x. x > max M 0" by (intro gt_ex)
hence "x>0. x > M" by auto
}
thus "¬bdd_above {(0::real)<..}" unfolding bdd_above_def
by (auto simp: not_le)
qed (insert assms, auto)

lemma holomorphic_on_minus [holomorphic_intros]: "f holomorphic_on A  (λz. -(f z)) holomorphic_on A"
by (metis field_differentiable_minus holomorphic_on_def)

"f holomorphic_on A; g holomorphic_on A  (λz. f z + g z) holomorphic_on A"

lemma holomorphic_on_diff [holomorphic_intros]:
"f holomorphic_on A; g holomorphic_on A  (λz. f z - g z) holomorphic_on A"
unfolding holomorphic_on_def by (metis field_differentiable_diff)

lemma holomorphic_on_mult [holomorphic_intros]:
"f holomorphic_on A; g holomorphic_on A  (λz. f z * g z) holomorphic_on A"
unfolding holomorphic_on_def by (metis field_differentiable_mult)

lemma holomorphic_on_inverse [holomorphic_intros]:
"f holomorphic_on A; z. z  A  f z  0  (λz. inverse (f z)) holomorphic_on A"
unfolding holomorphic_on_def by (metis field_differentiable_inverse)

lemma holomorphic_on_divide [holomorphic_intros]:
"f holomorphic_on A; g holomorphic_on A; z. z  A  g z  0  (λz. f z / g z) holomorphic_on A"
unfolding holomorphic_on_def by (metis field_differentiable_divide)

lemma holomorphic_on_power [holomorphic_intros]:
"f holomorphic_on A  (λz. (f z)^n) holomorphic_on A"
unfolding holomorphic_on_def by (metis field_differentiable_power)

lemma holomorphic_on_power_int [holomorphic_intros]:
assumes nz: "n  0  (xA. f x  0)" and f: "f holomorphic_on A"
shows   "(λx. f x powi n) holomorphic_on A"
proof (cases "n  0")
case True
have "(λx. f x ^ nat n) holomorphic_on A"
with True show ?thesis
next
case False
hence "(λx. inverse (f x ^ nat (-n))) holomorphic_on A"
using nz by (auto intro!: holomorphic_intros f)
with False show ?thesis
qed

lemma holomorphic_on_sum [holomorphic_intros]:
"(i. i  I  (f i) holomorphic_on A)  (λx. sum (λi. f i x) I) holomorphic_on A"
unfolding holomorphic_on_def by (metis field_differentiable_sum)

lemma holomorphic_on_prod [holomorphic_intros]:
"(i. i  I  (f i) holomorphic_on A)  (λx. prod (λi. f i x) I) holomorphic_on A"
by (induction I rule: infinite_finite_induct) (auto intro: holomorphic_intros)

lemma holomorphic_pochhammer [holomorphic_intros]:
"f holomorphic_on A  (λs. pochhammer (f s) n) holomorphic_on A"
by (induction n) (auto intro!: holomorphic_intros simp: pochhammer_Suc)

lemma holomorphic_on_scaleR [holomorphic_intros]:
"f holomorphic_on A  (λx. c *R f x) holomorphic_on A"
by (auto simp: scaleR_conv_of_real intro!: holomorphic_intros)

lemma holomorphic_on_Un [holomorphic_intros]:
assumes "f holomorphic_on A" "f holomorphic_on B" "open A" "open B"
shows   "f holomorphic_on (A  B)"
using assms by (auto simp: holomorphic_on_def  at_within_open[of _ A]
at_within_open[of _ B]  at_within_open[of _ "A  B"] open_Un)

lemma holomorphic_on_If_Un [holomorphic_intros]:
assumes "f holomorphic_on A" "g holomorphic_on B" "open A" "open B"
assumes "z. z  A  z  B  f z = g z"
shows   "(λz. if z  A then f z else g z) holomorphic_on (A  B)" (is "?h holomorphic_on _")
proof (intro holomorphic_on_Un)
note f holomorphic_on A
also have
by (intro holomorphic_cong) auto
finally show  .
next
note g holomorphic_on B
also have
using assms by (intro holomorphic_cong) auto
finally show  .
qed (insert assms, auto)

lemma holomorphic_derivI:
"f holomorphic_on S; open S; x  S
(f has_field_derivative deriv f x) (at x within T)"
by (metis DERIV_deriv_iff_field_differentiable at_within_open  holomorphic_on_def has_field_derivative_at_within)

lemma complex_derivative_transform_within_open:
"f holomorphic_on s; g holomorphic_on s; open s; z  s; w. w  s  f w = g w
deriv f z = deriv g z"
unfolding holomorphic_on_def
by (rule DERIV_imp_deriv)
(metis DERIV_deriv_iff_field_differentiable has_field_derivative_transform_within_open at_within_open)

lemma holomorphic_on_compose_cnj_cnj:
assumes  "open A"
shows
proof -
have [simp]: "open (cnj ` A)"
unfolding image_cnj_conv_vimage_cnj using assms by (intro open_vimage) auto
show ?thesis
using assms unfolding holomorphic_on_def
by (auto intro!: field_differentiable_cnj_cnj simp: at_within_open_NO_MATCH)
qed

lemma holomorphic_nonconstant:
assumes holf: "f holomorphic_on S" and "open S" "ξ  S" "deriv f ξ  0"
shows "¬ f constant_on S"
by (rule nonzero_deriv_nonconstant [of f "deriv f ξ" ξ S])
(use assms in auto simp: holomorphic_derivI)

subsection‹Analyticity on a set›

definitiontag important› analytic_on (infixl "(analytic'_on)" 50)
where "f analytic_on S  x  S. e. 0 < e  f holomorphic_on (ball x e)"

named_theoremstag important› analytic_intros "introduction rules for proving analyticity"

lemma analytic_imp_holomorphic: "f analytic_on S  f holomorphic_on S"
by (simp add: at_within_open [OF _ open_ball] analytic_on_def holomorphic_on_def)
(metis centre_in_ball field_differentiable_at_within)

lemma analytic_on_open: "open S  f analytic_on S  f holomorphic_on S"
by (meson analytic_imp_holomorphic analytic_on_def holomorphic_on_subset openE)

lemma analytic_on_imp_differentiable_at:
"f analytic_on S  x  S  f field_differentiable (at x)"
using analytic_on_def holomorphic_on_imp_differentiable_at by auto

lemma analytic_at_imp_isCont:
assumes "f analytic_on {z}"
shows   "isCont f z"
using assms by (meson analytic_on_imp_differentiable_at field_differentiable_imp_continuous_at insertI1)

lemma analytic_at_neq_imp_eventually_neq:
assumes "f analytic_on {x}" "f x  c"
shows   "eventually (λy. f y  c) (at x)"
proof (intro tendsto_imp_eventually_ne)
show "f x f x"
using assms by (simp add: analytic_at_imp_isCont isContD)
qed (use assms in auto)

lemma analytic_on_subset: "f analytic_on S  T  S  f analytic_on T"
by (auto simp: analytic_on_def)

lemma analytic_on_Un: "f analytic_on (S  T)  f analytic_on S  f analytic_on T"
by (auto simp: analytic_on_def)

lemma analytic_on_Union: "f analytic_on (𝒯)  (T  𝒯. f analytic_on T)"
by (auto simp: analytic_on_def)

lemma analytic_on_UN: "f analytic_on (iI. S i)  (iI. f analytic_on (S i))"
by (auto simp: analytic_on_def)

lemma analytic_on_holomorphic:
"f analytic_on S  (T. open T  S  T  f holomorphic_on T)"
(is "?lhs = ?rhs")
proof -
have "?lhs  (T. open T  S  T  f analytic_on T)"
proof safe
assume "f analytic_on S"
then show "T. open T  S  T  f analytic_on T"
apply (rule exI [where x="{U. open U  f  U}"], auto)
apply (metis open_ball analytic_on_open centre_in_ball)
by (metis analytic_on_def)
next
fix T
assume "open T" "S  T" "f analytic_on T"
then show "f analytic_on S"
by (metis analytic_on_subset)
qed
also have "...  ?rhs"
by (auto simp: analytic_on_open)
finally show ?thesis .
qed

lemma analytic_on_linear [analytic_intros,simp]: "((*) c) analytic_on S"

lemma analytic_on_const [analytic_intros,simp]: "(λz. c) analytic_on S"
by (metis analytic_on_def holomorphic_on_const zero_less_one)

lemma analytic_on_ident [analytic_intros,simp]: "(λx. x) analytic_on S"

lemma analytic_on_id [analytic_intros]:
unfolding id_def by (rule analytic_on_ident)

lemma analytic_on_compose:
assumes f: "f analytic_on S"
and g: "g analytic_on (f ` S)"
shows "(g o f) analytic_on S"
unfolding analytic_on_def
proof (intro ballI)
fix x
assume x: "x  S"
then obtain e where e: "0 < e" and fh: "f holomorphic_on ball x e" using f
by (metis analytic_on_def)
obtain e' where e': "0 < e'" and gh: "g holomorphic_on ball (f x) e'" using g
by (metis analytic_on_def g image_eqI x)
have "isCont f x"
by (metis analytic_on_imp_differentiable_at field_differentiable_imp_continuous_at f x)
with e' obtain d where d: "0 < d" and fd: "f ` ball x d  ball (f x) e'"
by (auto simp: continuous_at_ball)
have "g  f holomorphic_on ball x (min d e)"
apply (rule holomorphic_on_compose)
apply (metis fh holomorphic_on_subset min.bounded_iff order_refl subset_ball)
by (metis fd gh holomorphic_on_subset image_mono min.cobounded1 subset_ball)
then show "e>0. g  f holomorphic_on ball x e"
by (metis d e min_less_iff_conj)
qed

lemma analytic_on_compose_gen:
"f analytic_on S  g analytic_on T  (z. z  S  f z  T)
g o f analytic_on S"
by (metis analytic_on_compose analytic_on_subset image_subset_iff)

lemma analytic_on_neg [analytic_intros]:
"f analytic_on S  (λz. -(f z)) analytic_on S"
by (metis analytic_on_holomorphic holomorphic_on_minus)

assumes f: "f analytic_on S"
and g: "g analytic_on S"
shows "(λz. f z + g z) analytic_on S"
unfolding analytic_on_def
proof (intro ballI)
fix z
assume z: "z  S"
then obtain e where e: "0 < e" and fh: "f holomorphic_on ball z e" using f
by (metis analytic_on_def)
obtain e' where e': "0 < e'" and gh: "g holomorphic_on ball z e'" using g
by (metis analytic_on_def g z)
have "(λz. f z + g z) holomorphic_on ball z (min e e')"
apply (metis fh holomorphic_on_subset min.bounded_iff order_refl subset_ball)
by (metis gh holomorphic_on_subset min.bounded_iff order_refl subset_ball)
then show "e>0. (λz. f z + g z) holomorphic_on ball z e"
by (metis e e' min_less_iff_conj)
qed

lemma analytic_on_diff [analytic_intros]:
assumes f: "f analytic_on S"
and g: "g analytic_on S"
shows "(λz. f z - g z) analytic_on S"
unfolding analytic_on_def
proof (intro ballI)
fix z
assume z: "z  S"
then obtain e where e: "0 < e" and fh: "f holomorphic_on ball z e" using f
by (metis analytic_on_def)
obtain e' where e': "0 < e'" and gh: "g holomorphic_on ball z e'" using g
by (metis analytic_on_def g z)
have "(λz. f z - g z) holomorphic_on ball z (min e e')"
apply (rule holomorphic_on_diff)
apply (metis fh holomorphic_on_subset min.bounded_iff order_refl subset_ball)
by (metis gh holomorphic_on_subset min.bounded_iff order_refl subset_ball)
then show "e>0. (λz. f z - g z) holomorphic_on ball z e"
by (metis e e' min_less_iff_conj)
qed

lemma analytic_on_mult [analytic_intros]:
assumes f: "f analytic_on S"
and g: "g analytic_on S"
shows "(λz. f z * g z) analytic_on S"
unfolding analytic_on_def
proof (intro ballI)
fix z
assume z: "z  S"
then obtain e where e: "0 < e" and fh: "f holomorphic_on ball z e" using f
by (metis analytic_on_def)
obtain e' where e': "0 < e'" and gh: "g holomorphic_on ball z e'" using g
by (metis analytic_on_def g z)
have "(λz. f z * g z) holomorphic_on ball z (min e e')"
apply (rule holomorphic_on_mult)
apply (metis fh holomorphic_on_subset min.bounded_iff order_refl subset_ball)
by (metis gh holomorphic_on_subset min.bounded_iff order_refl subset_ball)
then show "e>0. (λz. f z * g z) holomorphic_on ball z e"
by (metis e e' min_less_iff_conj)
qed

lemma analytic_on_inverse [analytic_intros]:
assumes f: "f analytic_on S"
and nz: "(z. z  S  f z  0)"
shows "(λz. inverse (f z)) analytic_on S"
unfolding analytic_on_def
proof (intro ballI)
fix z
assume z: "z  S"
then obtain e where e: "0 < e" and fh: "f holomorphic_on ball z e" using f
by (metis analytic_on_def)
have "continuous_on (ball z e) f"
by (metis fh holomorphic_on_imp_continuous_on)
then obtain e' where e': "0 < e'" and nz': "y. dist z y < e'  f y  0"
by (metis open_ball centre_in_ball continuous_on_open_avoid e z nz)
have "(λz. inverse (f z)) holomorphic_on ball z (min e e')"
apply (rule holomorphic_on_inverse)
apply (metis fh holomorphic_on_subset min.cobounded2 min.commute subset_ball)
by (metis nz' mem_ball min_less_iff_conj)
then show "e>0. (λz. inverse (f z)) holomorphic_on ball z e"
by (metis e e' min_less_iff_conj)
qed

lemma analytic_on_divide [analytic_intros]:
assumes f: "f analytic_on S"
and g: "g analytic_on S"
and nz: "(z. z  S  g z  0)"
shows "(λz. f z / g z) analytic_on S"
unfolding divide_inverse
by (metis analytic_on_inverse analytic_on_mult f g nz)

lemma analytic_on_power [analytic_intros]:
"f analytic_on S  (λz. (f z) ^ n) analytic_on S"
by (induct n) (auto simp: analytic_on_mult)

lemma analytic_on_power_int [analytic_intros]:
assumes nz: "n  0  (xA. f x  0)" and f: "f analytic_on A"
shows   "(λx. f x powi n) analytic_on A"
proof (cases "n  0")
case True
have "(λx. f x ^ nat n) analytic_on A"
using analytic_on_power f by blast
with True show ?thesis
next
case False
hence "(λx. inverse (f x ^ nat (-n))) analytic_on A"
using nz by (auto intro!: analytic_intros f)
with False show ?thesis
qed

lemma analytic_on_sum [analytic_intros]:
"(i. i  I  (f i) analytic_on S)  (λx. sum (λi. f i x) I) analytic_on S"
by (induct I rule: infinite_finite_induct) (auto simp: analytic_on_add)

lemma analytic_on_prod [analytic_intros]:
"(i. i  I  (f i) analytic_on S)  (λx. prod (λi. f i x) I) analytic_on S"
by (induct I rule: infinite_finite_induct) (auto simp: analytic_on_mult)

lemma deriv_left_inverse:
assumes "f holomorphic_on S" and "g holomorphic_on T"
and "open S" and "open T"
and "f ` S  T"
and [simp]: "z. z  S  g (f z) = z"
and "w  S"
shows "deriv f w * deriv g (f w) = 1"
proof -
have "deriv f w * deriv g (f w) = deriv g (f w) * deriv f w"
also have "... = deriv (g o f) w"
using assms
by (metis analytic_on_imp_differentiable_at analytic_on_open deriv_chain image_subset_iff)
also have "... = deriv id w"
proof (rule complex_derivative_transform_within_open [where s=S])
show "g  f holomorphic_on S"
by (rule assms holomorphic_on_compose_gen holomorphic_intros)+
qed (use assms in auto)
also have "... = 1"
by simp
finally show ?thesis .
qed

subsectiontag unimportant›‹Analyticity at a point›

lemma analytic_at_ball:
"f analytic_on {z}  (e. 0<e  f holomorphic_on ball z e)"
by (metis analytic_on_def singleton_iff)

lemma analytic_at:
"f analytic_on {z}  (s. open s  z  s  f holomorphic_on s)"
by (metis analytic_on_holomorphic empty_subsetI insert_subset)

lemma holomorphic_on_imp_analytic_at:
assumes "f holomorphic_on A" "open A" "z  A"
shows   "f analytic_on {z}"
using assms by (meson analytic_at)

lemma analytic_on_analytic_at:
"f analytic_on s  (z  s. f analytic_on {z})"
by (metis analytic_at_ball analytic_on_def)

lemma analytic_at_two:
"f analytic_on {z}  g analytic_on {z}
(s. open s  z  s  f holomorphic_on s  g holomorphic_on s)"
(is "?lhs = ?rhs")
proof
assume ?lhs
then obtain s t
where st: "open s" "z  s" "f holomorphic_on s"
"open t" "z  t" "g holomorphic_on t"
by (auto simp: analytic_at)
show ?rhs
apply (rule_tac x="s  t" in exI)
using st
apply (auto simp: holomorphic_on_subset)
done
next
assume ?rhs
then show ?lhs
qed

subsectiontag unimportant›‹Combining theorems for derivative with ``analytic at'' hypotheses›

lemma
assumes "f analytic_on {z}" "g analytic_on {z}"
shows complex_derivative_add_at: "deriv (λw. f w + g w) z = deriv f z + deriv g z"
and complex_derivative_diff_at: "deriv (λw. f w - g w) z = deriv f z - deriv g z"
and complex_derivative_mult_at: "deriv (λw. f w * g w) z =
f z * deriv g z + deriv f z * g z"
proof -
obtain s where s: "open s" "z  s" "f holomorphic_on s" "g holomorphic_on s"
using assms by (metis analytic_at_two)
show "deriv (λw. f w + g w) z = deriv f z + deriv g z"
using s
apply (auto simp: holomorphic_on_open field_differentiable_def DERIV_deriv_iff_field_differentiable)
done
show "deriv (λw. f w - g w) z = deriv f z - deriv g z"
apply (rule DERIV_imp_deriv [OF DERIV_diff])
using s
apply (auto simp: holomorphic_on_open field_differentiable_def DERIV_deriv_iff_field_differentiable)
done
show "deriv (λw. f w * g w) z = f z * deriv g z + deriv f z * g z"
apply (rule DERIV_imp_deriv [OF DERIV_mult'])
using s
apply (auto simp: holomorphic_on_open field_differentiable_def DERIV_deriv_iff_field_differentiable)
done
qed

lemma deriv_cmult_at:
"f analytic_on {z}   deriv (λw. c * f w) z = c * deriv f z"
by (auto simp: complex_derivative_mult_at)

lemma deriv_cmult_right_at:
"f analytic_on {z}   deriv (λw. f w * c) z = deriv f z * c"
by (auto simp: complex_derivative_mult_at)

subsectiontag unimportant›‹Complex differentiation of sequences and series›

(* TODO: Could probably be simplified using Uniform_Limit *)
lemma has_complex_derivative_sequence:
fixes S ::
assumes cvs: "convex S"
and df:  "n x. x  S  (f n has_field_derivative f' n x) (at x within S)"
and conv: "e. 0 < e  N. n x. n  N  x  S  norm (f' n x - g' x)  e"
and "x l. x  S  ((λn. f n x)  l) sequentially"
shows "g. x  S. ((λn. f n x)  g x) sequentially
(g has_field_derivative (g' x)) (at x within S)"
proof -
from assms obtain x l where x: "x  S" and tf: "((λn. f n x)  l) sequentially"
by blast
{ fix e::real assume e: "e > 0"
then obtain N where N: "nN. x. x  S  cmod (f' n x - g' x)  e"
by (metis conv)
have "N. nN. xS. h. cmod (f' n x * h - g' x * h)  e * cmod h"
proof (rule exI [of _ N], clarify)
fix n y h
assume "N  n" "y  S"
then have "cmod (f' n y - g' y)  e"
by (metis N)
then have "cmod h * cmod (f' n y - g' y)  cmod h * e"
by (auto simp: antisym_conv2 mult_le_cancel_left norm_triangle_ineq2)
then show "cmod (f' n y * h - g' y * h)  e * cmod h"
by (simp add: norm_mult [symmetric] field_simps)
qed
} note ** = this
show ?thesis
unfolding has_field_derivative_def
proof (rule has_derivative_sequence [OF cvs _ _ x])
show "(λn. f n x)  l"
by (rule tf)
next show "e. e > 0  F n in sequentially. xS. h. cmod (f' n x * h - g' x * h)  e * cmod h"
unfolding eventually_sequentially by (blast intro: **)
qed (metis has_field_derivative_def df)
qed

lemma has_complex_derivative_series:
fixes S ::
assumes cvs: "convex S"
and df:  "n x. x  S  (f n has_field_derivative f' n x) (at x within S)"
and conv: "e. 0 < e  N. n x. n  N  x  S
cmod ((i<n. f' i x) - g' x)  e"
and "x l. x  S  ((λn. f n x) sums l)"
shows "g. x  S. ((λn. f n x) sums g x)  ((g has_field_derivative g' x) (at x within S))"
proof -
from assms obtain x l where x: "x  S" and sf: "((λn. f n x) sums l)"
by blast
{ fix e::real assume e: "e > 0"
then obtain N where N: "n x. n  N  x  S
cmod ((i<n. f' i x) - g' x)  e"
by (metis conv)
have "N. nN. xS. h. cmod ((i<n. h * f' i x) - g' x * h)  e * cmod h"
proof (rule exI [of _ N], clarify)
fix n y h
assume "N  n" "y  S"
then have "cmod ((i<n. f' i y) - g' y)  e"
by (metis N)
then have "cmod h * cmod ((i<n. f' i y) - g' y)  cmod h * e"
by (auto simp: antisym_conv2 mult_le_cancel_left norm_triangle_ineq2)
then show "cmod ((i<n. h * f' i y) - g' y * h)  e * cmod h"
by (simp add: norm_mult [symmetric] field_simps sum_distrib_left)
qed
} note ** = this
show ?thesis
unfolding has_field_derivative_def
proof (rule has_derivative_series [OF cvs _ _ x])
fix n x
assume "x  S"
then show "((f n) has_derivative (λz. z * f' n x)) (at x within S)"
by (metis df has_field_derivative_def mult_commute_abs)
next show " ((λn. f n x) sums l)"
by (rule sf)
next show "e. e>0  F n in sequentially. xS. h. cmod ((i<n. h * f' i x) - g' x * h)  e * cmod h"
unfolding eventually_sequentially by (blast intro: **)
qed
qed

subsectiontag unimportant› ‹Taylor on Complex Numbers›

lemma sum_Suc_reindex:
shows  "sum f {0..n} = f 0 - f (Suc n) + sum (λi. f (Suc i)) {0..n}"
by (induct n) auto

lemma field_Taylor:
assumes S: "convex S"
and f: "i x. x  S  i  n  (f i has_field_derivative f (Suc i) x) (at x within S)"
and B: "x. x  S  norm (f (Suc n) x)  B"
and w: "w  S"
and z: "z  S"
shows "norm(f 0 z - (in. f i w * (z-w) ^ i / (fact i)))
B * norm(z - w)^(Suc n) / fact n"
proof -
have wzs: "closed_segment w z  S" using assms
by (metis convex_contains_segment)
{ fix u
assume "u  closed_segment w z"
then have "u  S"
by (metis wzs subsetD)
have "(in. f i u * (- of_nat i * (z-u)^(i - 1)) / (fact i) +
f (Suc i) u * (z-u)^i / (fact i)) =
f (Suc n) u * (z-u) ^ n / (fact n)"
proof (induction n)
case 0 show ?case by simp
next
case (Suc n)
have "(iSuc n. f i u * (- of_nat i * (z-u) ^ (i - 1)) / (fact i) +
f (Suc i) u * (z-u) ^ i / (fact i)) =
f (Suc n) u * (z-u) ^ n / (fact n) +
f (Suc (Suc n)) u * ((z-u) * (z-u) ^ n) / (fact (Suc n)) -
f (Suc n) u * ((1 + of_nat n) * (z-u) ^ n) / (fact (Suc n))"
using Suc by simp
also have "... = f (Suc (Suc n)) u * (z-u) ^ Suc n / (fact (Suc n))"
proof -
have "(fact(Suc n)) *
(f(Suc n) u *(z-u) ^ n / (fact n) +
f(Suc(Suc n)) u *((z-u) *(z-u) ^ n) / (fact(Suc n)) -
f(Suc n) u *((1 + of_nat n) *(z-u) ^ n) / (fact(Suc n))) =
((fact(Suc n)) *(f(Suc n) u *(z-u) ^ n)) / (fact n) +
((fact(Suc n)) *(f(Suc(Suc n)) u *((z-u) *(z-u) ^ n)) / (fact(Suc n))) -
((fact(Suc n)) *(f(Suc n) u *(of_nat(Suc n) *(z-u) ^ n))) / (fact(Suc n))"
by (simp add: algebra_simps del: fact_Suc)
also have "... = ((fact (Suc n)) * (f (Suc n) u * (z-u) ^ n)) / (fact n) +
(f (Suc (Suc n)) u * ((z-u) * (z-u) ^ n)) -
(f (Suc n) u * ((1 + of_nat n) * (z-u) ^ n))"
by (simp del: fact_Suc)
also have "... = (of_nat (Suc n) * (f (Suc n) u * (z-u) ^ n)) +
(f (Suc (Suc n)) u * ((z-u) * (z-u) ^ n)) -
(f (Suc n) u * ((1 + of_nat n) * (z-u) ^ n))"
by (simp only: fact_Suc of_nat_mult ac_simps) simp
also have "... = f (Suc (Suc n)) u * ((z-u) * (z-u) ^ n)"
finally show ?thesis
by (simp add: mult_left_cancel [where c = "(fact (Suc n))", THEN iffD1] del: fact_Suc)
qed
finally show ?case .
qed
then have "((λv. (in. f i v * (z - v)^i / (fact i)))
has_field_derivative f (Suc n) u * (z-u) ^ n / (fact n))
(at u within S)"
apply (intro derivative_eq_intros)
apply (blast intro: assms u  S)
apply (rule refl)+
apply (auto simp: field_simps)
done
} note sum_deriv = this
{ fix u
assume u: "u  closed_segment w z"
then have us: "u  S"
by (metis wzs subsetD)
have "norm (f (Suc n) u) * norm (z - u) ^ n  norm (f (Suc n) u) * norm (u - z) ^ n"
by (metis norm_minus_commute order_refl)
also have "...  norm (f (Suc n) u) * norm (z - w) ^ n"
by (metis mult_left_mono norm_ge_zero power_mono segment_bound [OF u])
also have "...  B * norm (z - w) ^ n"
by (metis norm_ge_zero zero_le_power mult_right_mono  B [OF us])
finally have "norm (f (Suc n) u) * norm (z - u) ^ n  B * norm (z - w) ^ n" .
} note cmod_bound = this
have "(in. f i z * (z - z) ^ i / (fact i)) = (in. (f i z / (fact i)) * 0 ^ i)"
by simp
also have " = f 0 z / (fact 0)"
by (subst sum_zero_power) simp
finally have "norm (f 0 z - (in. f i w * (z - w) ^ i / (fact i)))
norm ((in. f i w * (z - w) ^ i / (fact i)) -
(in. f i z * (z - z) ^ i / (fact i)))"
also have "...  B * norm (z - w) ^ n / (fact n) * norm (w - z)"
apply (rule field_differentiable_bound
[where f' = "λw. f (Suc n) w  (z  w)n  (fact n)"
and S = "closed_segment w z", OF convex_closed_segment])
apply (auto simp: DERIV_subset [OF sum_deriv wzs]
norm_divide norm_mult norm_power divide_le_cancel cmod_bound)
done
also have "...   B * norm (z - w) ^ Suc n / (fact n)"
finally show ?thesis .
qed

lemma complex_Taylor:
assumes S: "convex S"
and f: "i x. x  S  i  n  (f i has_field_derivative f (Suc i) x) (at x within S)"
and B: "x. x  S  cmod (f (Suc n) x)  B"
and w: "w  S"
and z: "z  S"
shows "cmod(f 0 z - (in. f i w * (z-w) ^ i / (fact i)))
B * cmod(z - w)^(Suc n) / fact n"
using assms by (rule field_Taylor)

text‹Something more like the traditional MVT for real components›

lemma complex_mvt_line:
assumes "u. u  closed_segment w z  (f has_field_derivative f'(u)) (at u)"
shows "u. u  closed_segment w z  Re(f z) - Re(f w) = Re(f'(u) * (z - w))"
proof -
have twz: "t. (1 - t) *R w + t *R z = w + t *R (z - w)"
note assms[unfolded has_field_derivative_def, derivative_intros]
show ?thesis
apply (cut_tac mvt_simple
[of   "Re  f  (λt. (  t)  w   t  z)"
"λu. Re  (λh. f'((  u)  w  u  z)  h)  (λt. t  (z  w))"])
apply auto
apply (rule_tac x="(1 - x) *R w + x *R z" in exI)
apply (auto simp: closed_segment_def twz) []
apply (intro derivative_eq_intros has_derivative_at_withinI, simp_all)
apply (force simp: twz closed_segment_def)
done
qed

lemma complex_Taylor_mvt:
assumes "i x. x  closed_segment w z; i  n  ((f i) has_field_derivative f (Suc i) x) (at x)"
shows "u. u  closed_segment w z
Re (f 0 z) =
Re ((i = 0..n. f i w * (z - w) ^ i / (fact i)) +
(f (Suc n) u * (z-u)^n / (fact n)) * (z - w))"
proof -
{ fix u
assume u: "u  closed_segment w z"
have "(i = 0..n.
(f (Suc i) u * (z-u) ^ i - of_nat i * (f i u * (z-u) ^ (i - Suc 0))) /
(fact i)) =
f (Suc 0) u -
(f (Suc (Suc n)) u * ((z-u) ^ Suc n) - (of_nat (Suc n)) * (z-u) ^ n * f (Suc n) u) /
(fact (Suc n)) +
(i = 0..n.
(f (Suc (Suc i)) u * ((z-u) ^ Suc i) - of_nat (Suc i) * (f (Suc i) u * (z-u) ^ i)) /
(fact (Suc i)))"
by (subst sum_Suc_reindex) simp
also have "... = f (Suc 0) u -
(f (Suc (Suc n)) u * ((z-u) ^ Suc n) - (of_nat (Suc n)) * (z-u) ^ n * f (Suc n) u) /
(fact (Suc n)) +
(i = 0..n.
f (Suc (Suc i)) u * ((z-u) ^ Suc i) / (fact (Suc i))  -
f (Suc i) u * (z-u) ^ i / (fact i))"
by (simp only: diff_divide_distrib fact_cancel ac_simps)
also have "... = f (Suc 0) u -
(f (Suc (Suc n)) u * (z-u) ^ Suc n - of_nat (Suc n) * (z-u) ^ n * f (Suc n) u) /
(fact (Suc n)) +
f (Suc (Suc n)) u * (z-u) ^ Suc n / (fact (Suc n)) - f (Suc 0) u"
by (subst sum_Suc_diff) auto
also have "... = f (Suc n) u * (z-u) ^ n / (fact n)"
by (simp only: algebra_simps diff_divide_distrib fact_cancel)
finally have "(i = 0..n. (f (Suc i) u * (z - u) ^ i
- of_nat i * (f i u * (z-u) ^ (i - Suc 0))) / (fact i)) =
f (Suc n) u * (z - u) ^ n / (fact n)" .
then have "((λu. i = 0..n. f i u * (z - u) ^ i / (fact i)) has_field_derivative
f (Suc n) u * (z - u) ^ n / (fact n))  (at u)"
apply (intro derivative_eq_intros)+
apply (force intro: u assms)
apply (rule refl)+
apply (auto simp: ac_simps)
done
}
then show ?thesis
apply (cut_tac complex_mvt_line [of w z "λu. i = ..n. f i u  (zu)  i  (fact i)"
"λu. (f (Suc n) u  (zu)n  (fact n))"])
apply (auto simp add: intro: open_closed_segment)
done
qed

end