src/HOL/Multivariate_Analysis/Derivative.thy
changeset 33741 4c414d0835ab
child 33758 53078b0d21f5
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/HOL/Multivariate_Analysis/Derivative.thy	Tue Nov 17 18:52:30 2009 +0100
     1.3 @@ -0,0 +1,1332 @@
     1.4 +(*  Title:      HOL/Library/Convex_Euclidean_Space.thy
     1.5 +    Author:                       John Harrison
     1.6 +    Translated to from HOL light: Robert Himmelmann, TU Muenchen *)
     1.7 +
     1.8 +header {* Multivariate calculus in Euclidean space. *}
     1.9 +
    1.10 +theory Derivative
    1.11 +  imports Brouwer_Fixpoint RealVector
    1.12 +begin
    1.13 +
    1.14 +
    1.15 +(* Because I do not want to type this all the time *)
    1.16 +lemmas linear_linear = linear_conv_bounded_linear[THEN sym]
    1.17 +
    1.18 +subsection {* Derivatives *}
    1.19 +
    1.20 +text {* The definition is slightly tricky since we make it work over
    1.21 +  nets of a particular form. This lets us prove theorems generally and use 
    1.22 +  "at a" or "at a within s" for restriction to a set (1-sided on R etc.) *}
    1.23 +
    1.24 +definition has_derivative :: "('a::real_normed_vector \<Rightarrow> 'b::real_normed_vector) \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> ('a net \<Rightarrow> bool)"
    1.25 +(infixl "(has'_derivative)" 12) where
    1.26 + "(f has_derivative f') net \<equiv> bounded_linear f' \<and> ((\<lambda>y. (1 / (norm (y - netlimit net))) *\<^sub>R
    1.27 +   (f y - (f (netlimit net) + f'(y - netlimit net)))) ---> 0) net"
    1.28 +
    1.29 +lemma derivative_linear[dest]:"(f has_derivative f') net \<Longrightarrow> bounded_linear f'"
    1.30 +  unfolding has_derivative_def by auto
    1.31 +
    1.32 +lemma FDERIV_conv_has_derivative:"FDERIV f (x::'a::{real_normed_vector,perfect_space}) :> f' = (f has_derivative f') (at x)" (is "?l = ?r") proof 
    1.33 +  assume ?l note as = this[unfolded fderiv_def]
    1.34 +  show ?r unfolding has_derivative_def Lim_at apply-apply(rule,rule as[THEN conjunct1]) proof(rule,rule)
    1.35 +    fix e::real assume "e>0"
    1.36 +    guess d using as[THEN conjunct2,unfolded LIM_def,rule_format,OF`e>0`] ..
    1.37 +    thus "\<exists>d>0. \<forall>xa. 0 < dist xa x \<and> dist xa x < d \<longrightarrow>
    1.38 +      dist ((1 / norm (xa - netlimit (at x))) *\<^sub>R (f xa - (f (netlimit (at x)) + f' (xa - netlimit (at x))))) (0) < e"
    1.39 +      apply(rule_tac x=d in exI) apply(erule conjE,rule,assumption) apply rule apply(erule_tac x="xa - x" in allE)
    1.40 +      unfolding vector_dist_norm netlimit_at[of x] by(auto simp add:group_simps) qed next
    1.41 +  assume ?r note as = this[unfolded has_derivative_def]
    1.42 +  show ?l unfolding fderiv_def LIM_def apply-apply(rule,rule as[THEN conjunct1]) proof(rule,rule)
    1.43 +    fix e::real assume "e>0"
    1.44 +    guess d using as[THEN conjunct2,unfolded Lim_at,rule_format,OF`e>0`] ..
    1.45 +    thus "\<exists>s>0. \<forall>xa. xa \<noteq> 0 \<and> dist xa 0 < s \<longrightarrow> dist (norm (f (x + xa) - f x - f' xa) / norm xa) 0 < e" apply-
    1.46 +      apply(rule_tac x=d in exI) apply(erule conjE,rule,assumption) apply rule apply(erule_tac x="xa + x" in allE)
    1.47 +      unfolding vector_dist_norm netlimit_at[of x] by(auto simp add:group_simps) qed qed
    1.48 +
    1.49 +subsection {* These are the only cases we'll care about, probably. *}
    1.50 +
    1.51 +lemma has_derivative_within: "(f has_derivative f') (at x within s) \<longleftrightarrow>
    1.52 +         bounded_linear f' \<and> ((\<lambda>y. (1 / norm(y - x)) *\<^sub>R (f y - (f x + f' (y - x)))) ---> 0) (at x within s)"
    1.53 +  unfolding has_derivative_def and Lim by(auto simp add:netlimit_within)
    1.54 +
    1.55 +lemma has_derivative_at: "(f has_derivative f') (at x) \<longleftrightarrow>
    1.56 +         bounded_linear f' \<and> ((\<lambda>y. (1 / (norm(y - x))) *\<^sub>R (f y - (f x + f' (y - x)))) ---> 0) (at x)"
    1.57 +  apply(subst within_UNIV[THEN sym]) unfolding has_derivative_within unfolding within_UNIV by auto
    1.58 +
    1.59 +subsection {* More explicit epsilon-delta forms. *}
    1.60 +
    1.61 +lemma has_derivative_within':
    1.62 +  "(f has_derivative f')(at x within s) \<longleftrightarrow> bounded_linear f' \<and>
    1.63 +        (\<forall>e>0. \<exists>d>0. \<forall>x'\<in>s. 0 < norm(x' - x) \<and> norm(x' - x) < d
    1.64 +        \<longrightarrow> norm(f x' - f x - f'(x' - x)) / norm(x' - x) < e)"
    1.65 +  unfolding has_derivative_within Lim_within vector_dist_norm
    1.66 +  unfolding diff_0_right norm_mul by(simp add: group_simps)
    1.67 +
    1.68 +lemma has_derivative_at':
    1.69 + "(f has_derivative f') (at x) \<longleftrightarrow> bounded_linear f' \<and>
    1.70 +   (\<forall>e>0. \<exists>d>0. \<forall>x'. 0 < norm(x' - x) \<and> norm(x' - x) < d
    1.71 +        \<longrightarrow> norm(f x' - f x - f'(x' - x)) / norm(x' - x) < e)"
    1.72 +  apply(subst within_UNIV[THEN sym]) unfolding has_derivative_within' by auto
    1.73 +
    1.74 +lemma has_derivative_at_within: "(f has_derivative f') (at x) \<Longrightarrow> (f has_derivative f') (at x within s)"
    1.75 +  unfolding has_derivative_within' has_derivative_at' by meson
    1.76 +
    1.77 +lemma has_derivative_within_open:
    1.78 +  "a \<in> s \<Longrightarrow> open s \<Longrightarrow> ((f has_derivative f') (at a within s) \<longleftrightarrow> (f has_derivative f') (at a))"
    1.79 +  unfolding has_derivative_within has_derivative_at using Lim_within_open by auto
    1.80 +
    1.81 +subsection {* Derivatives on real = Derivatives on real^1 *}
    1.82 +
    1.83 +lemma dist_vec1_0[simp]: "dist(vec1 (x::real)) 0 = norm x" unfolding vector_dist_norm by(auto simp add:vec1_dest_vec1_simps)
    1.84 +
    1.85 +lemma bounded_linear_vec1_dest_vec1: fixes f::"real \<Rightarrow> real"
    1.86 +  shows "linear (vec1 \<circ> f \<circ> dest_vec1) = bounded_linear f" (is "?l = ?r") proof-
    1.87 +  { assume ?l guess K using linear_bounded[OF `?l`] ..
    1.88 +    hence "\<exists>K. \<forall>x. \<bar>f x\<bar> \<le> \<bar>x\<bar> * K" apply(rule_tac x=K in exI)
    1.89 +      unfolding vec1_dest_vec1_simps by (auto simp add:field_simps) }
    1.90 +  thus ?thesis unfolding linear_def bounded_linear_def additive_def bounded_linear_axioms_def o_def
    1.91 +    unfolding vec1_dest_vec1_simps by auto qed 
    1.92 +
    1.93 +lemma has_derivative_within_vec1_dest_vec1: fixes f::"real\<Rightarrow>real" shows
    1.94 +  "((vec1 \<circ> f \<circ> dest_vec1) has_derivative (vec1 \<circ> f' \<circ> dest_vec1)) (at (vec1 x) within vec1 ` s)
    1.95 +  = (f has_derivative f') (at x within s)"
    1.96 +  unfolding has_derivative_within unfolding bounded_linear_vec1_dest_vec1[unfolded linear_conv_bounded_linear]
    1.97 +  unfolding o_def Lim_within Ball_def unfolding forall_vec1 
    1.98 +  unfolding vec1_dest_vec1_simps dist_vec1_0 image_iff by auto  
    1.99 +
   1.100 +lemma has_derivative_at_vec1_dest_vec1: fixes f::"real\<Rightarrow>real" shows
   1.101 +  "((vec1 \<circ> f \<circ> dest_vec1) has_derivative (vec1 \<circ> f' \<circ> dest_vec1)) (at (vec1 x)) = (f has_derivative f') (at x)"
   1.102 +  using has_derivative_within_vec1_dest_vec1[where s=UNIV, unfolded range_vec1 within_UNIV] by auto
   1.103 +
   1.104 +lemma bounded_linear_vec1: fixes f::"'a::real_normed_vector\<Rightarrow>real"
   1.105 +  shows "bounded_linear f = bounded_linear (vec1 \<circ> f)"
   1.106 +  unfolding bounded_linear_def additive_def bounded_linear_axioms_def o_def
   1.107 +  unfolding vec1_dest_vec1_simps by auto
   1.108 +
   1.109 +lemma bounded_linear_dest_vec1: fixes f::"real\<Rightarrow>'a::real_normed_vector"
   1.110 +  shows "bounded_linear f = bounded_linear (f \<circ> dest_vec1)"
   1.111 +  unfolding bounded_linear_def additive_def bounded_linear_axioms_def o_def
   1.112 +  unfolding vec1_dest_vec1_simps by auto
   1.113 +
   1.114 +lemma has_derivative_at_vec1: fixes f::"'a::real_normed_vector\<Rightarrow>real" shows
   1.115 +  "(f has_derivative f') (at x) = ((vec1 \<circ> f) has_derivative (vec1 \<circ> f')) (at x)"
   1.116 +  unfolding has_derivative_at unfolding bounded_linear_vec1[unfolded linear_conv_bounded_linear]
   1.117 +  unfolding o_def Lim_at unfolding vec1_dest_vec1_simps dist_vec1_0 by auto
   1.118 +
   1.119 +lemma has_derivative_within_dest_vec1:fixes f::"real\<Rightarrow>'a::real_normed_vector" shows
   1.120 +  "((f \<circ> dest_vec1) has_derivative (f' \<circ> dest_vec1)) (at (vec1 x) within vec1 ` s) = (f has_derivative f') (at x within s)"
   1.121 +  unfolding has_derivative_within bounded_linear_dest_vec1 unfolding o_def Lim_within Ball_def
   1.122 +  unfolding vec1_dest_vec1_simps dist_vec1_0 image_iff by auto
   1.123 +
   1.124 +lemma has_derivative_at_dest_vec1:fixes f::"real\<Rightarrow>'a::real_normed_vector" shows
   1.125 +  "((f \<circ> dest_vec1) has_derivative (f' \<circ> dest_vec1)) (at (vec1 x)) = (f has_derivative f') (at x)"
   1.126 +  using has_derivative_within_dest_vec1[where s=UNIV] by(auto simp add:within_UNIV)
   1.127 +
   1.128 +lemma derivative_is_linear: fixes f::"real^'a::finite \<Rightarrow> real^'b::finite" shows
   1.129 +  "(f has_derivative f') net \<Longrightarrow> linear f'"
   1.130 +  unfolding has_derivative_def and linear_conv_bounded_linear by auto
   1.131 +
   1.132 +
   1.133 +subsection {* Combining theorems. *}
   1.134 +
   1.135 +lemma (in bounded_linear) has_derivative: "(f has_derivative f) net"
   1.136 +  unfolding has_derivative_def apply(rule,rule bounded_linear_axioms)
   1.137 +  unfolding diff by(simp add: Lim_const)
   1.138 +
   1.139 +lemma has_derivative_id: "((\<lambda>x. x) has_derivative (\<lambda>h. h)) net"
   1.140 +  apply(rule bounded_linear.has_derivative) using bounded_linear_ident[unfolded id_def] by simp
   1.141 +
   1.142 +lemma has_derivative_const: "((\<lambda>x. c) has_derivative (\<lambda>h. 0)) net"
   1.143 +  unfolding has_derivative_def apply(rule,rule bounded_linear_zero) by(simp add: Lim_const)
   1.144 +
   1.145 +lemma (in bounded_linear) cmul: shows "bounded_linear (\<lambda>x. (c::real) *\<^sub>R f x)" proof
   1.146 +  guess K using pos_bounded ..
   1.147 +  thus "\<exists>K. \<forall>x. norm ((c::real) *\<^sub>R f x) \<le> norm x * K" apply(rule_tac x="abs c * K" in exI) proof
   1.148 +    fix x case goal1
   1.149 +    hence "abs c * norm (f x) \<le> abs c * (norm x * K)" apply-apply(erule conjE,erule_tac x=x in allE)  
   1.150 +      apply(rule mult_left_mono) by auto
   1.151 +    thus ?case by(auto simp add:field_simps)
   1.152 +  qed qed(auto simp add: scaleR.add_right add scaleR)
   1.153 +
   1.154 +lemma has_derivative_cmul: assumes "(f has_derivative f') net" shows "((\<lambda>x. c *\<^sub>R f(x)) has_derivative (\<lambda>h. c *\<^sub>R f'(h))) net"
   1.155 +  unfolding has_derivative_def apply(rule,rule bounded_linear.cmul)
   1.156 +  using assms[unfolded has_derivative_def] using Lim_cmul[OF assms[unfolded has_derivative_def,THEN conjunct2]]
   1.157 +  unfolding scaleR_right_diff_distrib scaleR_right_distrib by auto 
   1.158 +
   1.159 +lemma has_derivative_cmul_eq: assumes "c \<noteq> 0" 
   1.160 +  shows "(((\<lambda>x. c *\<^sub>R f(x)) has_derivative (\<lambda>h. c *\<^sub>R f'(h))) net \<longleftrightarrow> (f has_derivative f') net)"
   1.161 +  apply(rule) defer apply(rule has_derivative_cmul,assumption) 
   1.162 +  apply(drule has_derivative_cmul[where c="1/c"]) using assms by auto
   1.163 +
   1.164 +lemma has_derivative_neg:
   1.165 + "(f has_derivative f') net \<Longrightarrow> ((\<lambda>x. -(f x)) has_derivative (\<lambda>h. -(f' h))) net"
   1.166 +  apply(drule has_derivative_cmul[where c="-1"]) by auto
   1.167 +
   1.168 +lemma has_derivative_neg_eq: "((\<lambda>x. -(f x)) has_derivative (\<lambda>h. -(f' h))) net \<longleftrightarrow> (f has_derivative f') net"
   1.169 +  apply(rule, drule_tac[!] has_derivative_neg) by auto
   1.170 +
   1.171 +lemma has_derivative_add: assumes "(f has_derivative f') net" "(g has_derivative g') net"
   1.172 +  shows "((\<lambda>x. f(x) + g(x)) has_derivative (\<lambda>h. f'(h) + g'(h))) net" proof-
   1.173 +  note as = assms[unfolded has_derivative_def]
   1.174 +  show ?thesis unfolding has_derivative_def apply(rule,rule bounded_linear_add)
   1.175 +    using Lim_add[OF as(1)[THEN conjunct2] as(2)[THEN conjunct2]] and as
   1.176 +    by(auto simp add:group_simps scaleR_right_diff_distrib scaleR_right_distrib) qed
   1.177 +
   1.178 +lemma has_derivative_add_const:"(f has_derivative f') net \<Longrightarrow> ((\<lambda>x. f x + c) has_derivative f') net"
   1.179 +  apply(drule has_derivative_add) apply(rule has_derivative_const) by auto
   1.180 +
   1.181 +lemma has_derivative_sub:
   1.182 + "(f has_derivative f') net \<Longrightarrow> (g has_derivative g') net \<Longrightarrow> ((\<lambda>x. f(x) - g(x)) has_derivative (\<lambda>h. f'(h) - g'(h))) net"
   1.183 +  apply(drule has_derivative_add) apply(drule has_derivative_neg,assumption) by(simp add:group_simps)
   1.184 +
   1.185 +lemma has_derivative_setsum: assumes "finite s" "\<forall>a\<in>s. ((f a) has_derivative (f' a)) net"
   1.186 +  shows "((\<lambda>x. setsum (\<lambda>a. f a x) s) has_derivative (\<lambda>h. setsum (\<lambda>a. f' a h) s)) net"
   1.187 +  apply(induct_tac s rule:finite_subset_induct[where A=s]) apply(rule assms(1)) 
   1.188 +proof- fix x F assume as:"finite F" "x \<notin> F" "x\<in>s" "((\<lambda>x. \<Sum>a\<in>F. f a x) has_derivative (\<lambda>h. \<Sum>a\<in>F. f' a h)) net" 
   1.189 +  thus "((\<lambda>xa. \<Sum>a\<in>insert x F. f a xa) has_derivative (\<lambda>h. \<Sum>a\<in>insert x F. f' a h)) net"
   1.190 +    unfolding setsum_insert[OF as(1,2)] apply-apply(rule has_derivative_add) apply(rule assms(2)[rule_format]) by auto
   1.191 +qed(auto intro!: has_derivative_const)
   1.192 +
   1.193 +lemma has_derivative_setsum_numseg:
   1.194 +  "\<forall>i. m \<le> i \<and> i \<le> n \<longrightarrow> ((f i) has_derivative (f' i)) net \<Longrightarrow>
   1.195 +  ((\<lambda>x. setsum (\<lambda>i. f i x) {m..n::nat}) has_derivative (\<lambda>h. setsum (\<lambda>i. f' i h) {m..n})) net"
   1.196 +  apply(rule has_derivative_setsum) by auto
   1.197 +
   1.198 +subsection {* somewhat different results for derivative of scalar multiplier. *}
   1.199 +
   1.200 +lemma has_derivative_vmul_component: fixes c::"real^'a::finite \<Rightarrow> real^'b::finite" and v::"real^'c::finite"
   1.201 +  assumes "(c has_derivative c') net"
   1.202 +  shows "((\<lambda>x. c(x)$k *\<^sub>R v) has_derivative (\<lambda>x. (c' x)$k *\<^sub>R v)) net" proof-
   1.203 +  have *:"\<And>y. (c y $ k *\<^sub>R v - (c (netlimit net) $ k *\<^sub>R v + c' (y - netlimit net) $ k *\<^sub>R v)) = 
   1.204 +        (c y $ k - (c (netlimit net) $ k + c' (y - netlimit net) $ k)) *\<^sub>R v" 
   1.205 +    unfolding scaleR_left_diff_distrib scaleR_left_distrib by auto
   1.206 +  show ?thesis unfolding has_derivative_def and * and linear_conv_bounded_linear[symmetric]
   1.207 +    apply(rule,rule linear_vmul_component[of c' k v, unfolded smult_conv_scaleR]) defer 
   1.208 +    apply(subst vector_smult_lzero[THEN sym, of v]) unfolding scaleR_scaleR smult_conv_scaleR apply(rule Lim_vmul)
   1.209 +    using assms[unfolded has_derivative_def] unfolding Lim o_def apply- apply(cases "trivial_limit net")
   1.210 +    apply(rule,assumption,rule disjI2,rule,rule) proof-
   1.211 +    have *:"\<And>x. x - vec 0 = (x::real^'n)" by auto 
   1.212 +    have **:"\<And>d x. d * (c x $ k - (c (netlimit net) $ k + c' (x - netlimit net) $ k)) = (d *\<^sub>R (c x - (c (netlimit net) + c' (x - netlimit net) ))) $k" by(auto simp add:field_simps)
   1.213 +    fix e assume "\<not> trivial_limit net" "0 < (e::real)"
   1.214 +    then obtain A where A:"A\<in>Rep_net net" "\<forall>x\<in>A. dist ((1 / norm (x - netlimit net)) *\<^sub>R (c x - (c (netlimit net) + c' (x - netlimit net)))) 0 < e"
   1.215 +      using assms[unfolded has_derivative_def Lim] unfolding eventually_def by auto
   1.216 +    show "eventually (\<lambda>x. dist (1 / norm (x - netlimit net) * (c x $ k - (c (netlimit net) $ k + c' (x - netlimit net) $ k))) 0 < e) net"
   1.217 +      unfolding eventually_def apply(rule_tac x=A in bexI) apply rule proof-
   1.218 +      case goal1 thus ?case apply -apply(drule A(2)[rule_format]) unfolding vector_dist_norm vec1_vec apply(rule le_less_trans) prefer 2 apply assumption unfolding * ** and norm_vec1[unfolded vec1_vec]
   1.219 +	using component_le_norm[of "(1 / norm (x - netlimit net)) *\<^sub>R (c x - (c (netlimit net) + c' (x - netlimit net))) - 0" k] by auto
   1.220 +      qed(insert A, auto) qed(insert assms[unfolded has_derivative_def], auto simp add:linear_conv_bounded_linear) qed 
   1.221 +
   1.222 +lemma has_derivative_vmul_within: fixes c::"real \<Rightarrow> real" and v::"real^'a::finite"
   1.223 +  assumes "(c has_derivative c') (at x within s)"
   1.224 +  shows "((\<lambda>x. (c x) *\<^sub>R v) has_derivative (\<lambda>x. (c' x) *\<^sub>R v)) (at x within s)" proof-
   1.225 +  have *:"\<And>c. (\<lambda>x. (vec1 \<circ> c \<circ> dest_vec1) x $ 1 *\<^sub>R v) = (\<lambda>x. (c x) *\<^sub>R v) \<circ> dest_vec1" unfolding o_def by auto
   1.226 +  show ?thesis using has_derivative_vmul_component[of "vec1 \<circ> c \<circ> dest_vec1" "vec1 \<circ> c' \<circ> dest_vec1" "at (vec1 x) within vec1 ` s" 1 v]
   1.227 +  unfolding * and has_derivative_within_vec1_dest_vec1 unfolding has_derivative_within_dest_vec1 using assms by auto qed
   1.228 +
   1.229 +lemma has_derivative_vmul_at: fixes c::"real \<Rightarrow> real" and v::"real^'a::finite"
   1.230 +  assumes "(c has_derivative c') (at x)"
   1.231 +  shows "((\<lambda>x. (c x) *\<^sub>R v) has_derivative (\<lambda>x. (c' x) *\<^sub>R v)) (at x)"
   1.232 +  using has_derivative_vmul_within[where s=UNIV] and assms by(auto simp add: within_UNIV)
   1.233 +
   1.234 +lemma has_derivative_lift_dot:
   1.235 +  assumes "(f has_derivative f') net"
   1.236 +  shows "((\<lambda>x. inner v (f x)) has_derivative (\<lambda>t. inner v (f' t))) net" proof-
   1.237 +  show ?thesis using assms unfolding has_derivative_def apply- apply(erule conjE,rule)
   1.238 +    apply(rule bounded_linear_compose[of _ f']) apply(rule inner.bounded_linear_right,assumption)
   1.239 +    apply(drule Lim_inner[where a=v]) unfolding o_def
   1.240 +    by(auto simp add:inner.scaleR_right inner.add_right inner.diff_right) qed
   1.241 +
   1.242 +lemmas has_derivative_intros = has_derivative_sub has_derivative_add has_derivative_cmul has_derivative_id has_derivative_const
   1.243 +   has_derivative_neg has_derivative_vmul_component has_derivative_vmul_at has_derivative_vmul_within has_derivative_cmul 
   1.244 +   bounded_linear.has_derivative has_derivative_lift_dot
   1.245 +
   1.246 +subsection {* limit transformation for derivatives. *}
   1.247 +
   1.248 +lemma has_derivative_transform_within:
   1.249 +  assumes "0 < d" "x \<in> s" "\<forall>x'\<in>s. dist x' x < d \<longrightarrow> f x' = g x'" "(f has_derivative f') (at x within s)"
   1.250 +  shows "(g has_derivative f') (at x within s)"
   1.251 +  using assms(4) unfolding has_derivative_within apply- apply(erule conjE,rule,assumption)
   1.252 +  apply(rule Lim_transform_within[OF assms(1)]) defer apply assumption
   1.253 +  apply(rule,rule) apply(drule assms(3)[rule_format]) using assms(3)[rule_format, OF assms(2)] by auto
   1.254 +
   1.255 +lemma has_derivative_transform_at:
   1.256 +  assumes "0 < d" "\<forall>x'. dist x' x < d \<longrightarrow> f x' = g x'" "(f has_derivative f') (at x)"
   1.257 +  shows "(g has_derivative f') (at x)"
   1.258 +  apply(subst within_UNIV[THEN sym]) apply(rule has_derivative_transform_within[OF assms(1)])
   1.259 +  using assms(2-3) unfolding within_UNIV by auto
   1.260 +
   1.261 +lemma has_derivative_transform_within_open:
   1.262 +  assumes "open s" "x \<in> s" "\<forall>y\<in>s. f y = g y" "(f has_derivative f') (at x)"
   1.263 +  shows "(g has_derivative f') (at x)"
   1.264 +  using assms(4) unfolding has_derivative_at apply- apply(erule conjE,rule,assumption)
   1.265 +  apply(rule Lim_transform_within_open[OF assms(1,2)]) defer apply assumption
   1.266 +  apply(rule,rule) apply(drule assms(3)[rule_format]) using assms(3)[rule_format, OF assms(2)] by auto
   1.267 +
   1.268 +subsection {* differentiability. *}
   1.269 +
   1.270 +definition differentiable :: "('a::real_normed_vector \<Rightarrow> 'b::real_normed_vector) \<Rightarrow> 'a net \<Rightarrow> bool" (infixr "differentiable" 30) where
   1.271 +  "f differentiable net \<equiv> (\<exists>f'. (f has_derivative f') net)"
   1.272 +
   1.273 +definition differentiable_on :: "('a::real_normed_vector \<Rightarrow> 'b::real_normed_vector) \<Rightarrow> 'a set \<Rightarrow> bool" (infixr "differentiable'_on" 30) where
   1.274 +  "f differentiable_on s \<equiv> (\<forall>x\<in>s. f differentiable (at x within s))"
   1.275 +
   1.276 +lemma differentiableI: "(f has_derivative f') net \<Longrightarrow> f differentiable net"
   1.277 +  unfolding differentiable_def by auto
   1.278 +
   1.279 +lemma differentiable_at_withinI: "f differentiable (at x) \<Longrightarrow> f differentiable (at x within s)"
   1.280 +  unfolding differentiable_def using has_derivative_at_within by blast
   1.281 +
   1.282 +lemma differentiable_within_open: assumes "a \<in> s" "open s" shows 
   1.283 +  "f differentiable (at a within s) \<longleftrightarrow> (f differentiable (at a))"
   1.284 +  unfolding differentiable_def has_derivative_within_open[OF assms] by auto
   1.285 +
   1.286 +lemma differentiable_at_imp_differentiable_on: "(\<forall>x\<in>(s::(real^'n::finite) set). f differentiable at x) \<Longrightarrow> f differentiable_on s"
   1.287 +  unfolding differentiable_on_def by(auto intro!: differentiable_at_withinI)
   1.288 +
   1.289 +lemma differentiable_on_eq_differentiable_at: "open s \<Longrightarrow> (f differentiable_on s \<longleftrightarrow> (\<forall>x\<in>s. f differentiable at x))"
   1.290 +  unfolding differentiable_on_def by(auto simp add: differentiable_within_open)
   1.291 +
   1.292 +lemma differentiable_transform_within:
   1.293 +  assumes "0 < d" "x \<in> s" "\<forall>x'\<in>s. dist x' x < d \<longrightarrow> f x' = g x'" "f differentiable (at x within s)"
   1.294 +  shows "g differentiable (at x within s)"
   1.295 +  using assms(4) unfolding differentiable_def by(auto intro!: has_derivative_transform_within[OF assms(1-3)])
   1.296 +
   1.297 +lemma differentiable_transform_at:
   1.298 +  assumes "0 < d" "\<forall>x'. dist x' x < d \<longrightarrow> f x' = g x'" "f differentiable at x"
   1.299 +  shows "g differentiable at x"
   1.300 +  using assms(3) unfolding differentiable_def using has_derivative_transform_at[OF assms(1-2)] by auto
   1.301 +
   1.302 +subsection {* Frechet derivative and Jacobian matrix. *}
   1.303 +
   1.304 +definition "frechet_derivative f net = (SOME f'. (f has_derivative f') net)"
   1.305 +
   1.306 +lemma frechet_derivative_works:
   1.307 + "f differentiable net \<longleftrightarrow> (f has_derivative (frechet_derivative f net)) net"
   1.308 +  unfolding frechet_derivative_def differentiable_def and some_eq_ex[of "\<lambda> f' . (f has_derivative f') net"] ..
   1.309 +
   1.310 +lemma linear_frechet_derivative: fixes f::"real^'a::finite \<Rightarrow> real^'b::finite"
   1.311 +  shows "f differentiable net \<Longrightarrow> linear(frechet_derivative f net)"
   1.312 +  unfolding frechet_derivative_works has_derivative_def unfolding linear_conv_bounded_linear by auto
   1.313 +
   1.314 +definition "jacobian f net = matrix(frechet_derivative f net)"
   1.315 +
   1.316 +lemma jacobian_works: "(f::(real^'a::finite) \<Rightarrow> (real^'b::finite)) differentiable net \<longleftrightarrow> (f has_derivative (\<lambda>h. (jacobian f net) *v h)) net"
   1.317 +  apply rule unfolding jacobian_def apply(simp only: matrix_works[OF linear_frechet_derivative]) defer
   1.318 +  apply(rule differentiableI) apply assumption unfolding frechet_derivative_works by assumption
   1.319 +
   1.320 +subsection {* Differentiability implies continuity. *}
   1.321 +
   1.322 +lemma Lim_mul_norm_within: fixes f::"'a::real_normed_vector \<Rightarrow> 'b::real_normed_vector"
   1.323 +  shows "(f ---> 0) (at a within s) \<Longrightarrow> ((\<lambda>x. norm(x - a) *\<^sub>R f(x)) ---> 0) (at a within s)"
   1.324 +  unfolding Lim_within apply(rule,rule) apply(erule_tac x=e in allE,erule impE,assumption,erule exE,erule conjE)
   1.325 +  apply(rule_tac x="min d 1" in exI) apply rule defer apply(rule,erule_tac x=x in ballE) unfolding vector_dist_norm diff_0_right norm_mul
   1.326 +  by(auto intro!: mult_strict_mono[of _ "1::real", unfolded mult_1_left])
   1.327 +
   1.328 +lemma differentiable_imp_continuous_within: assumes "f differentiable (at x within s)" 
   1.329 +  shows "continuous (at x within s) f" proof-
   1.330 +  from assms guess f' unfolding differentiable_def has_derivative_within .. note f'=this
   1.331 +  then interpret bounded_linear f' by auto
   1.332 +  have *:"\<And>xa. x\<noteq>xa \<Longrightarrow> (f' \<circ> (\<lambda>y. y - x)) xa + norm (xa - x) *\<^sub>R ((1 / norm (xa - x)) *\<^sub>R (f xa - (f x + f' (xa - x)))) - ((f' \<circ> (\<lambda>y. y - x)) x + 0) = f xa - f x"
   1.333 +    using zero by auto
   1.334 +  have **:"continuous (at x within s) (f' \<circ> (\<lambda>y. y - x))"
   1.335 +    apply(rule continuous_within_compose) apply(rule continuous_intros)+
   1.336 +    by(rule linear_continuous_within[OF f'[THEN conjunct1]])
   1.337 +  show ?thesis unfolding continuous_within using f'[THEN conjunct2, THEN Lim_mul_norm_within]
   1.338 +    apply-apply(drule Lim_add) apply(rule **[unfolded continuous_within]) unfolding Lim_within and vector_dist_norm
   1.339 +    apply(rule,rule) apply(erule_tac x=e in allE) apply(erule impE|assumption)+ apply(erule exE,rule_tac x=d in exI)
   1.340 +    by(auto simp add:zero * elim!:allE) qed
   1.341 +
   1.342 +lemma differentiable_imp_continuous_at: "f differentiable at x \<Longrightarrow> continuous (at x) f"
   1.343 + by(rule differentiable_imp_continuous_within[of _ x UNIV, unfolded within_UNIV])
   1.344 +
   1.345 +lemma differentiable_imp_continuous_on: "f differentiable_on s \<Longrightarrow> continuous_on s f"
   1.346 +  unfolding differentiable_on_def continuous_on_eq_continuous_within
   1.347 +  using differentiable_imp_continuous_within by blast
   1.348 +
   1.349 +lemma has_derivative_within_subset:
   1.350 + "(f has_derivative f') (at x within s) \<Longrightarrow> t \<subseteq> s \<Longrightarrow> (f has_derivative f') (at x within t)"
   1.351 +  unfolding has_derivative_within using Lim_within_subset by blast
   1.352 +
   1.353 +lemma differentiable_within_subset:
   1.354 +  "f differentiable (at x within t) \<Longrightarrow> s \<subseteq> t \<Longrightarrow> f differentiable (at x within s)"
   1.355 +  unfolding differentiable_def using has_derivative_within_subset by blast
   1.356 +
   1.357 +lemma differentiable_on_subset: "f differentiable_on t \<Longrightarrow> s \<subseteq> t \<Longrightarrow> f differentiable_on s"
   1.358 +  unfolding differentiable_on_def using differentiable_within_subset by blast
   1.359 +
   1.360 +lemma differentiable_on_empty: "f differentiable_on {}"
   1.361 +  unfolding differentiable_on_def by auto
   1.362 +
   1.363 +subsection {* Several results are easier using a "multiplied-out" variant.              *)
   1.364 +(* (I got this idea from Dieudonne's proof of the chain rule). *}
   1.365 +
   1.366 +lemma has_derivative_within_alt:
   1.367 + "(f has_derivative f') (at x within s) \<longleftrightarrow> bounded_linear f' \<and>
   1.368 +  (\<forall>e>0. \<exists>d>0. \<forall>y\<in>s. norm(y - x) < d \<longrightarrow> norm(f(y) - f(x) - f'(y - x)) \<le> e * norm(y - x))" (is "?lhs \<longleftrightarrow> ?rhs")
   1.369 +proof assume ?lhs thus ?rhs unfolding has_derivative_within apply-apply(erule conjE,rule,assumption)
   1.370 +    unfolding Lim_within apply(rule,erule_tac x=e in allE,rule,erule impE,assumption)
   1.371 +    apply(erule exE,rule_tac x=d in exI) apply(erule conjE,rule,assumption,rule,rule) proof-
   1.372 +    fix x y e d assume as:"0 < e" "0 < d" "norm (y - x) < d" "\<forall>xa\<in>s. 0 < dist xa x \<and> dist xa x < d \<longrightarrow>
   1.373 +      dist ((1 / norm (xa - x)) *\<^sub>R (f xa - (f x + f' (xa - x)))) 0 < e" "y \<in> s" "bounded_linear f'"
   1.374 +    then interpret bounded_linear f' by auto
   1.375 +    show "norm (f y - f x - f' (y - x)) \<le> e * norm (y - x)" proof(cases "y=x")
   1.376 +      case True thus ?thesis using `bounded_linear f'` by(auto simp add: zero) next
   1.377 +      case False hence "norm (f y - (f x + f' (y - x))) < e * norm (y - x)" using as(4)[rule_format, OF `y\<in>s`]
   1.378 +	unfolding vector_dist_norm diff_0_right norm_mul using as(3)
   1.379 +	using pos_divide_less_eq[OF False[unfolded dist_nz], unfolded vector_dist_norm]
   1.380 +	by(auto simp add:linear_0 linear_sub group_simps)
   1.381 +      thus ?thesis by(auto simp add:group_simps) qed qed next
   1.382 +  assume ?rhs thus ?lhs unfolding has_derivative_within Lim_within apply-apply(erule conjE,rule,assumption)
   1.383 +    apply(rule,erule_tac x="e/2" in allE,rule,erule impE) defer apply(erule exE,rule_tac x=d in exI)
   1.384 +    apply(erule conjE,rule,assumption,rule,rule) unfolding vector_dist_norm diff_0_right norm_scaleR
   1.385 +    apply(erule_tac x=xa in ballE,erule impE) proof-
   1.386 +    fix e d y assume "bounded_linear f'" "0 < e" "0 < d" "y \<in> s" "0 < norm (y - x) \<and> norm (y - x) < d"
   1.387 +        "norm (f y - f x - f' (y - x)) \<le> e / 2 * norm (y - x)"
   1.388 +    thus "\<bar>1 / norm (y - x)\<bar> * norm (f y - (f x + f' (y - x))) < e"
   1.389 +      apply(rule_tac le_less_trans[of _ "e/2"]) by(auto intro!:mult_imp_div_pos_le simp add:group_simps) qed auto qed
   1.390 +
   1.391 +lemma has_derivative_at_alt:
   1.392 +  "(f has_derivative f') (at (x::real^'n::finite)) \<longleftrightarrow> bounded_linear f' \<and>
   1.393 +  (\<forall>e>0. \<exists>d>0. \<forall>y. norm(y - x) < d \<longrightarrow> norm(f y - f x - f'(y - x)) \<le> e * norm(y - x))"
   1.394 +  using has_derivative_within_alt[where s=UNIV] unfolding within_UNIV by auto
   1.395 +
   1.396 +subsection {* The chain rule. *}
   1.397 +
   1.398 +lemma diff_chain_within:
   1.399 +  assumes "(f has_derivative f') (at x within s)" "(g has_derivative g') (at (f x) within (f ` s))"
   1.400 +  shows "((g o f) has_derivative (g' o f'))(at x within s)"
   1.401 +  unfolding has_derivative_within_alt apply(rule,rule bounded_linear_compose[unfolded o_def[THEN sym]])
   1.402 +  apply(rule assms(2)[unfolded has_derivative_def,THEN conjE],assumption)
   1.403 +  apply(rule assms(1)[unfolded has_derivative_def,THEN conjE],assumption) proof(rule,rule)
   1.404 +  note assms = assms[unfolded has_derivative_within_alt]
   1.405 +  fix e::real assume "0<e"
   1.406 +  guess B1 using bounded_linear.pos_bounded[OF assms(1)[THEN conjunct1, rule_format]] .. note B1 = this
   1.407 +  guess B2 using bounded_linear.pos_bounded[OF assms(2)[THEN conjunct1, rule_format]] .. note B2 = this
   1.408 +  have *:"e / 2 / B2 > 0" using `e>0` B2 apply-apply(rule divide_pos_pos) by auto
   1.409 +  guess d1 using assms(1)[THEN conjunct2, rule_format, OF *] .. note d1 = this
   1.410 +  have *:"e / 2 / (1 + B1) > 0" using `e>0` B1 apply-apply(rule divide_pos_pos) by auto
   1.411 +  guess de using assms(2)[THEN conjunct2, rule_format, OF *] .. note de = this
   1.412 +  guess d2 using assms(1)[THEN conjunct2, rule_format, OF zero_less_one] .. note d2 = this
   1.413 +
   1.414 +  def d0 \<equiv> "(min d1 d2)/2" have d0:"d0>0" "d0 < d1" "d0 < d2" unfolding d0_def using d1 d2 by auto
   1.415 +  def d \<equiv> "(min d0 (de / (B1 + 1))) / 2" have "de * 2 / (B1 + 1) > de / (B1 + 1)" apply(rule divide_strict_right_mono) using B1 de by auto
   1.416 +  hence d:"d>0" "d < d1" "d < d2" "d < (de / (B1 + 1))" unfolding d_def using d0 d1 d2 de B1 by(auto intro!: divide_pos_pos simp add:min_less_iff_disj not_less)
   1.417 +
   1.418 +  show "\<exists>d>0. \<forall>y\<in>s. norm (y - x) < d \<longrightarrow> norm ((g \<circ> f) y - (g \<circ> f) x - (g' \<circ> f') (y - x)) \<le> e * norm (y - x)" apply(rule_tac x=d in exI)
   1.419 +    proof(rule,rule `d>0`,rule,rule) 
   1.420 +    fix y assume as:"y \<in> s" "norm (y - x) < d"
   1.421 +    hence 1:"norm (f y - f x - f' (y - x)) \<le> min (norm (y - x)) (e / 2 / B2 * norm (y - x))" using d1 d2 d by auto
   1.422 +
   1.423 +    have "norm (f y - f x) \<le> norm (f y - f x - f' (y - x)) + norm (f' (y - x))"
   1.424 +      using norm_triangle_sub[of "f y - f x" "f' (y - x)"] by(auto simp add:group_simps)
   1.425 +    also have "\<dots> \<le> norm (f y - f x - f' (y - x)) + B1 * norm (y - x)" apply(rule add_left_mono) using B1 by(auto simp add:group_simps)
   1.426 +    also have "\<dots> \<le> min (norm (y - x)) (e / 2 / B2 * norm (y - x)) + B1 * norm (y - x)" apply(rule add_right_mono) using d1 d2 d as by auto
   1.427 +    also have "\<dots> \<le> norm (y - x) + B1 * norm (y - x)" by auto
   1.428 +    also have "\<dots> = norm (y - x) * (1 + B1)" by(auto simp add:field_simps)
   1.429 +    finally have 3:"norm (f y - f x) \<le> norm (y - x) * (1 + B1)" by auto 
   1.430 +
   1.431 +    hence "norm (f y - f x) \<le> d * (1 + B1)" apply- apply(rule order_trans,assumption,rule mult_right_mono) using as B1 by auto 
   1.432 +    also have "\<dots> < de" using d B1 by(auto simp add:field_simps) 
   1.433 +    finally have "norm (g (f y) - g (f x) - g' (f y - f x)) \<le> e / 2 / (1 + B1) * norm (f y - f x)"
   1.434 +      apply-apply(rule de[THEN conjunct2,rule_format]) using `y\<in>s` using d as by auto 
   1.435 +    also have "\<dots> = (e / 2) * (1 / (1 + B1) * norm (f y - f x))" by auto 
   1.436 +    also have "\<dots> \<le> e / 2 * norm (y - x)" apply(rule mult_left_mono) using `e>0` and 3 using B1 and `e>0` by(auto simp add:divide_le_eq)
   1.437 +    finally have 4:"norm (g (f y) - g (f x) - g' (f y - f x)) \<le> e / 2 * norm (y - x)" by auto
   1.438 +    
   1.439 +    interpret g': bounded_linear g' using assms(2) by auto
   1.440 +    interpret f': bounded_linear f' using assms(1) by auto
   1.441 +    have "norm (- g' (f' (y - x)) + g' (f y - f x)) = norm (g' (f y - f x - f' (y - x)))"
   1.442 +      by(auto simp add:group_simps f'.diff g'.diff g'.add)
   1.443 +    also have "\<dots> \<le> B2 * norm (f y - f x - f' (y - x))" using B2 by(auto simp add:group_simps)
   1.444 +    also have "\<dots> \<le> B2 * (e / 2 / B2 * norm (y - x))" apply(rule mult_left_mono) using as d1 d2 d B2 by auto 
   1.445 +    also have "\<dots> \<le> e / 2 * norm (y - x)" using B2 by auto
   1.446 +    finally have 5:"norm (- g' (f' (y - x)) + g' (f y - f x)) \<le> e / 2 * norm (y - x)" by auto
   1.447 +    
   1.448 +    have "norm (g (f y) - g (f x) - g' (f y - f x)) + norm (g (f y) - g (f x) - g' (f' (y - x)) - (g (f y) - g (f x) - g' (f y - f x))) \<le> e * norm (y - x)" using 5 4 by auto
   1.449 +    thus "norm ((g \<circ> f) y - (g \<circ> f) x - (g' \<circ> f') (y - x)) \<le> e * norm (y - x)" unfolding o_def apply- apply(rule order_trans, rule norm_triangle_sub) by assumption qed qed
   1.450 +
   1.451 +lemma diff_chain_at:
   1.452 +  "(f has_derivative f') (at x) \<Longrightarrow> (g has_derivative g') (at (f x)) \<Longrightarrow> ((g o f) has_derivative (g' o f')) (at x)"
   1.453 +  using diff_chain_within[of f f' x UNIV g g'] using has_derivative_within_subset[of g g' "f x" UNIV "range f"] unfolding within_UNIV by auto
   1.454 +
   1.455 +subsection {* Composition rules stated just for differentiability. *}
   1.456 +
   1.457 +lemma differentiable_const[intro]: "(\<lambda>z. c) differentiable (net::'a::real_normed_vector net)"
   1.458 +  unfolding differentiable_def using has_derivative_const by auto
   1.459 +
   1.460 +lemma differentiable_id[intro]: "(\<lambda>z. z) differentiable (net::'a::real_normed_vector net)"
   1.461 +    unfolding differentiable_def using has_derivative_id by auto
   1.462 +
   1.463 +lemma differentiable_cmul[intro]: "f differentiable net \<Longrightarrow> (\<lambda>x. c *\<^sub>R f(x)) differentiable (net::'a::real_normed_vector net)"
   1.464 +  unfolding differentiable_def apply(erule exE, drule has_derivative_cmul) by auto
   1.465 +
   1.466 +lemma differentiable_neg[intro]: "f differentiable net \<Longrightarrow> (\<lambda>z. -(f z)) differentiable (net::'a::real_normed_vector net)"
   1.467 +  unfolding differentiable_def apply(erule exE, drule has_derivative_neg) by auto
   1.468 +
   1.469 +lemma differentiable_add: "f differentiable net \<Longrightarrow> g differentiable net
   1.470 +   \<Longrightarrow> (\<lambda>z. f z + g z) differentiable (net::'a::real_normed_vector net)"
   1.471 +    unfolding differentiable_def apply(erule exE)+ apply(rule_tac x="\<lambda>z. f' z + f'a z" in exI)
   1.472 +    apply(rule has_derivative_add) by auto
   1.473 +
   1.474 +lemma differentiable_sub: "f differentiable net \<Longrightarrow> g differentiable net
   1.475 +  \<Longrightarrow> (\<lambda>z. f z - g z) differentiable (net::'a::real_normed_vector net)"
   1.476 +  unfolding differentiable_def apply(erule exE)+ apply(rule_tac x="\<lambda>z. f' z - f'a z" in exI)
   1.477 +    apply(rule has_derivative_sub) by auto 
   1.478 +
   1.479 +lemma differentiable_setsum: fixes f::"'a \<Rightarrow> (real^'n::finite \<Rightarrow>real^'n)"
   1.480 +  assumes "finite s" "\<forall>a\<in>s. (f a) differentiable net"
   1.481 +  shows "(\<lambda>x. setsum (\<lambda>a. f a x) s) differentiable net" proof-
   1.482 +  guess f' using bchoice[OF assms(2)[unfolded differentiable_def]] ..
   1.483 +  thus ?thesis unfolding differentiable_def apply- apply(rule,rule has_derivative_setsum[where f'=f'],rule assms(1)) by auto qed
   1.484 +
   1.485 +lemma differentiable_setsum_numseg: fixes f::"_ \<Rightarrow> (real^'n::finite \<Rightarrow>real^'n)"
   1.486 +  shows "\<forall>i. m \<le> i \<and> i \<le> n \<longrightarrow> (f i) differentiable net \<Longrightarrow> (\<lambda>x. setsum (\<lambda>a. f a x) {m::nat..n}) differentiable net"
   1.487 +  apply(rule differentiable_setsum) using finite_atLeastAtMost[of n m] by auto
   1.488 +
   1.489 +lemma differentiable_chain_at:
   1.490 +  "f differentiable (at x) \<Longrightarrow> g differentiable (at(f x)) \<Longrightarrow> (g o f) differentiable (at x)"
   1.491 +  unfolding differentiable_def by(meson diff_chain_at)
   1.492 +
   1.493 +lemma differentiable_chain_within:
   1.494 +  "f differentiable (at x within s) \<Longrightarrow> g differentiable (at(f x) within (f ` s))
   1.495 +   \<Longrightarrow> (g o f) differentiable (at x within s)"
   1.496 +  unfolding differentiable_def by(meson diff_chain_within)
   1.497 +
   1.498 +subsection {* Uniqueness of derivative.                                                 *)
   1.499 +(*                                                                           *)
   1.500 +(* The general result is a bit messy because we need approachability of the  *)
   1.501 +(* limit point from any direction. But OK for nontrivial intervals etc. *}
   1.502 +    
   1.503 +lemma frechet_derivative_unique_within: fixes f::"real^'a::finite \<Rightarrow> real^'b::finite"
   1.504 +  assumes "(f has_derivative f') (at x within s)" "(f has_derivative f'') (at x within s)"
   1.505 +  "(\<forall>i::'a::finite. \<forall>e>0. \<exists>d. 0 < abs(d) \<and> abs(d) < e \<and> (x + d *\<^sub>R basis i) \<in> s)" shows "f' = f''" proof-
   1.506 +  note as = assms(1,2)[unfolded has_derivative_def]
   1.507 +  then interpret f': bounded_linear f' by auto from as interpret f'': bounded_linear f'' by auto
   1.508 +  have "x islimpt s" unfolding islimpt_approachable proof(rule,rule)
   1.509 +    guess a using UNIV_witness[where 'a='a] ..
   1.510 +    fix e::real assume "0<e" guess d using assms(3)[rule_format,OF`e>0`,of a] ..
   1.511 +    thus "\<exists>x'\<in>s. x' \<noteq> x \<and> dist x' x < e" apply(rule_tac x="x + d*\<^sub>R basis a" in bexI)
   1.512 +      using basis_nonzero[of a] norm_basis[of a] unfolding vector_dist_norm by auto qed
   1.513 +  hence *:"netlimit (at x within s) = x" apply-apply(rule netlimit_within) unfolding trivial_limit_within by simp
   1.514 +  show ?thesis  apply(rule linear_eq_stdbasis) unfolding linear_conv_bounded_linear
   1.515 +    apply(rule as(1,2)[THEN conjunct1])+ proof(rule,rule ccontr)
   1.516 +    fix i::'a def e \<equiv> "norm (f' (basis i) - f'' (basis i))"
   1.517 +    assume "f' (basis i) \<noteq> f'' (basis i)" hence "e>0" unfolding e_def by auto
   1.518 +    guess d using Lim_sub[OF as(1,2)[THEN conjunct2], unfolded * Lim_within,rule_format,OF `e>0`] .. note d=this
   1.519 +    guess c using assms(3)[rule_format,OF d[THEN conjunct1],of i] .. note c=this
   1.520 +    have *:"norm (- ((1 / \<bar>c\<bar>) *\<^sub>R f' (c *\<^sub>R basis i)) + (1 / \<bar>c\<bar>) *\<^sub>R f'' (c *\<^sub>R basis i)) = norm ((1 / abs c) *\<^sub>R (- (f' (c *\<^sub>R basis i)) + f'' (c *\<^sub>R basis i)))"
   1.521 +      unfolding scaleR_right_distrib by auto
   1.522 +    also have "\<dots> = norm ((1 / abs c) *\<^sub>R (c *\<^sub>R (- (f' (basis i)) + f'' (basis i))))"  
   1.523 +      unfolding f'.scaleR f''.scaleR unfolding scaleR_right_distrib scaleR_minus_right by auto
   1.524 +    also have "\<dots> = e" unfolding e_def norm_mul using c[THEN conjunct1] using norm_minus_cancel[of "f' (basis i) - f'' (basis i)"] by(auto simp add:group_simps)
   1.525 +    finally show False using c using d[THEN conjunct2,rule_format,of "x + c *\<^sub>R basis i"] using norm_basis[of i] unfolding vector_dist_norm 
   1.526 +      unfolding f'.scaleR f''.scaleR f'.add f''.add f'.diff f''.diff scaleR_scaleR scaleR_right_diff_distrib scaleR_right_distrib by auto qed qed
   1.527 +
   1.528 +lemma frechet_derivative_unique_at: fixes f::"real^'a::finite \<Rightarrow> real^'b::finite"
   1.529 +  shows "(f has_derivative f') (at x) \<Longrightarrow> (f has_derivative f'') (at x) \<Longrightarrow> f' = f''"
   1.530 +  apply(rule frechet_derivative_unique_within[of f f' x UNIV f'']) unfolding within_UNIV apply(assumption)+
   1.531 +  apply(rule,rule,rule) apply(rule_tac x="e/2" in exI) by auto
   1.532 + 
   1.533 +lemma "isCont f x = continuous (at x) f" unfolding isCont_def LIM_def
   1.534 +  unfolding continuous_at Lim_at unfolding dist_nz by auto
   1.535 +
   1.536 +lemma frechet_derivative_unique_within_closed_interval: fixes f::"real^'a::finite \<Rightarrow> real^'b::finite"
   1.537 +  assumes "\<forall>i. a$i < b$i" "x \<in> {a..b}" (is "x\<in>?I") and
   1.538 +  "(f has_derivative f' ) (at x within {a..b})" and
   1.539 +  "(f has_derivative f'') (at x within {a..b})"
   1.540 +  shows "f' = f''" apply(rule frechet_derivative_unique_within) apply(rule assms(3,4))+ proof(rule,rule,rule)
   1.541 +  fix e::real and i::'a assume "e>0"
   1.542 +  thus "\<exists>d. 0 < \<bar>d\<bar> \<and> \<bar>d\<bar> < e \<and> x + d *\<^sub>R basis i \<in> {a..b}" proof(cases "x$i=a$i")
   1.543 +    case True thus ?thesis apply(rule_tac x="(min (b$i - a$i)  e) / 2" in exI)
   1.544 +      using assms(1)[THEN spec[where x=i]] and `e>0` and assms(2)
   1.545 +      unfolding mem_interval by(auto simp add:field_simps) next
   1.546 +    note * = assms(2)[unfolded mem_interval,THEN spec[where x=i]]
   1.547 +    case False moreover have "a $ i < x $ i" using False * by auto
   1.548 +    moreover { have "a $ i * 2 + min (x $ i - a $ i) e \<le> a$i *2 + x$i - a$i" by auto
   1.549 +    also have "\<dots> = a$i + x$i" by auto also have "\<dots> \<le> 2 * x$i" using * by auto 
   1.550 +    finally have "a $ i * 2 + min (x $ i - a $ i) e \<le> x $ i * 2" by auto }
   1.551 +    moreover have "min (x $ i - a $ i) e \<ge> 0" using * and `e>0` by auto
   1.552 +    hence "x $ i * 2 \<le> b $ i * 2 + min (x $ i - a $ i) e" using * by auto
   1.553 +    ultimately show ?thesis apply(rule_tac x="- (min (x$i - a$i) e) / 2" in exI)
   1.554 +      using assms(1)[THEN spec[where x=i]] and `e>0` and assms(2)
   1.555 +      unfolding mem_interval by(auto simp add:field_simps) qed qed
   1.556 +
   1.557 +lemma frechet_derivative_unique_within_open_interval: fixes f::"real^'a::finite \<Rightarrow> real^'b::finite"
   1.558 +  assumes "x \<in> {a<..<b}" "(f has_derivative f' ) (at x within {a<..<b})"
   1.559 +                         "(f has_derivative f'') (at x within {a<..<b})"
   1.560 +  shows "f' = f''" apply(rule frechet_derivative_unique_within) apply(rule assms(2-3))+ proof(rule,rule,rule)
   1.561 +  fix e::real and i::'a assume "e>0"
   1.562 +  note * = assms(1)[unfolded mem_interval,THEN spec[where x=i]]
   1.563 +  have "a $ i < x $ i" using  * by auto
   1.564 +  moreover { have "a $ i * 2 + min (x $ i - a $ i) e \<le> a$i *2 + x$i - a$i" by auto
   1.565 +  also have "\<dots> = a$i + x$i" by auto also have "\<dots> < 2 * x$i" using * by auto 
   1.566 +  finally have "a $ i * 2 + min (x $ i - a $ i) e < x $ i * 2" by auto }
   1.567 +  moreover have "min (x $ i - a $ i) e \<ge> 0" using * and `e>0` by auto
   1.568 +  hence "x $ i * 2 < b $ i * 2 + min (x $ i - a $ i) e" using * by auto
   1.569 +  ultimately show "\<exists>d. 0 < \<bar>d\<bar> \<and> \<bar>d\<bar> < e \<and> x + d *\<^sub>R basis i \<in> {a<..<b}"
   1.570 +    apply(rule_tac x="- (min (x$i - a$i) e) / 2" in exI)
   1.571 +    using `e>0` and assms(1) unfolding mem_interval by(auto simp add:field_simps) qed
   1.572 +
   1.573 +lemma frechet_derivative_at: fixes f::"real^'a::finite \<Rightarrow> real^'b::finite"
   1.574 +  shows "(f has_derivative f') (at x) \<Longrightarrow> (f' = frechet_derivative f (at x))"
   1.575 +  apply(rule frechet_derivative_unique_at[of f],assumption)
   1.576 +  unfolding frechet_derivative_works[THEN sym] using differentiable_def by auto
   1.577 +
   1.578 +lemma frechet_derivative_within_closed_interval: fixes f::"real^'a::finite \<Rightarrow> real^'b::finite"
   1.579 +  assumes "\<forall>i. a$i < b$i" "x \<in> {a..b}" "(f has_derivative f') (at x within {a.. b})"
   1.580 +  shows "frechet_derivative f (at x within {a.. b}) = f'"
   1.581 +  apply(rule frechet_derivative_unique_within_closed_interval[where f=f]) 
   1.582 +  apply(rule assms(1,2))+ unfolding frechet_derivative_works[THEN sym]
   1.583 +  unfolding differentiable_def using assms(3) by auto 
   1.584 +
   1.585 +subsection {* Component of the differential must be zero if it exists at a local        *)
   1.586 +(* maximum or minimum for that corresponding component. *}
   1.587 +
   1.588 +lemma differential_zero_maxmin_component: fixes f::"real^'a::finite \<Rightarrow> real^'b::finite"
   1.589 +  assumes "0 < e" "((\<forall>y \<in> ball x e. (f y)$k \<le> (f x)$k) \<or> (\<forall>y\<in>ball x e. (f x)$k \<le> (f y)$k))"
   1.590 +  "f differentiable (at x)" shows "jacobian f (at x) $ k = 0" proof(rule ccontr)
   1.591 +  def D \<equiv> "jacobian f (at x)" assume "jacobian f (at x) $ k \<noteq> 0"
   1.592 +  then obtain j where j:"D$k$j \<noteq> 0" unfolding Cart_eq D_def by auto
   1.593 +  hence *:"abs (jacobian f (at x) $ k $ j) / 2 > 0" unfolding D_def by auto
   1.594 +  note as = assms(3)[unfolded jacobian_works has_derivative_at_alt]
   1.595 +  guess e' using as[THEN conjunct2,rule_format,OF *] .. note e' = this
   1.596 +  guess d using real_lbound_gt_zero[OF assms(1) e'[THEN conjunct1]] .. note d = this
   1.597 +  { fix c assume "abs c \<le> d" 
   1.598 +    hence *:"norm (x + c *\<^sub>R basis j - x) < e'" using norm_basis[of j] d by auto
   1.599 +    have "\<bar>(f (x + c *\<^sub>R basis j) - f x - D *v (c *\<^sub>R basis j)) $ k\<bar> \<le> norm (f (x + c *\<^sub>R basis j) - f x - D *v (c *\<^sub>R basis j))" by(rule component_le_norm)
   1.600 +    also have "\<dots> \<le> \<bar>D $ k $ j\<bar> / 2 * \<bar>c\<bar>" using e'[THEN conjunct2,rule_format,OF *] and norm_basis[of j] unfolding D_def[symmetric] by auto
   1.601 +    finally have "\<bar>(f (x + c *\<^sub>R basis j) - f x - D *v (c *\<^sub>R basis j)) $ k\<bar> \<le> \<bar>D $ k $ j\<bar> / 2 * \<bar>c\<bar>" by simp
   1.602 +    hence "\<bar>f (x + c *\<^sub>R basis j) $ k - f x $ k - c * D $ k $ j\<bar> \<le> \<bar>D $ k $ j\<bar> / 2 * \<bar>c\<bar>"
   1.603 +      unfolding vector_component_simps matrix_vector_mul_component unfolding smult_conv_scaleR[symmetric] 
   1.604 +      unfolding dot_rmult dot_basis unfolding smult_conv_scaleR by simp  } note * = this
   1.605 +  have "x + d *\<^sub>R basis j \<in> ball x e" "x - d *\<^sub>R basis j \<in> ball x e"
   1.606 +    unfolding mem_ball vector_dist_norm using norm_basis[of j] d by auto
   1.607 +  hence **:"((f (x - d *\<^sub>R basis j))$k \<le> (f x)$k \<and> (f (x + d *\<^sub>R basis j))$k \<le> (f x)$k) \<or>
   1.608 +         ((f (x - d *\<^sub>R basis j))$k \<ge> (f x)$k \<and> (f (x + d *\<^sub>R basis j))$k \<ge> (f x)$k)" using assms(2) by auto
   1.609 +  have ***:"\<And>y y1 y2 d dx::real. (y1\<le>y\<and>y2\<le>y) \<or> (y\<le>y1\<and>y\<le>y2) \<Longrightarrow> d < abs dx \<Longrightarrow> abs(y1 - y - - dx) \<le> d \<Longrightarrow> (abs (y2 - y - dx) \<le> d) \<Longrightarrow> False" by arith
   1.610 +  show False apply(rule ***[OF **, where dx="d * D $ k $ j" and d="\<bar>D $ k $ j\<bar> / 2 * \<bar>d\<bar>"]) 
   1.611 +    using *[of "-d"] and *[of d] and d[THEN conjunct1] and j unfolding mult_minus_left
   1.612 +    unfolding abs_mult diff_minus_eq_add scaleR.minus_left unfolding  group_simps by auto qed
   1.613 +
   1.614 +subsection {* In particular if we have a mapping into R^1. *}
   1.615 +
   1.616 +lemma differential_zero_maxmin: fixes f::"real^'a::finite \<Rightarrow> real"
   1.617 +  assumes "x \<in> s" "open s" "(f has_derivative f') (at x)"
   1.618 +  "(\<forall>y\<in>s. f y \<le> f x) \<or> (\<forall>y\<in>s. f x \<le> f y)"
   1.619 +  shows "f' = (\<lambda>v. 0)" proof-
   1.620 +  note deriv = assms(3)[unfolded has_derivative_at_vec1]
   1.621 +  obtain e where e:"e>0" "ball x e \<subseteq> s" using assms(2)[unfolded open_contains_ball] and assms(1) by auto
   1.622 +  hence **:"(jacobian (vec1 \<circ> f) (at x)) $ 1 = 0" using differential_zero_maxmin_component[of e x "\<lambda>x. vec1 (f x)" 1]
   1.623 +    unfolding dest_vec1_def[THEN sym] vec1_dest_vec1 using assms(4) and assms(3)[unfolded has_derivative_at_vec1 o_def]
   1.624 +    unfolding differentiable_def o_def by auto 
   1.625 +  have *:"jacobian (vec1 \<circ> f) (at x) = matrix (vec1 \<circ> f')" unfolding jacobian_def and frechet_derivative_at[OF deriv] ..
   1.626 +  have "vec1 \<circ> f' = (\<lambda>x. 0)" apply(rule) unfolding matrix_works[OF derivative_is_linear[OF deriv],THEN sym]
   1.627 +    unfolding Cart_eq matrix_vector_mul_component using **[unfolded *] by auto
   1.628 +  thus ?thesis apply-apply(rule,subst vec1_eq[THEN sym]) unfolding o_def apply(drule fun_cong) by auto qed
   1.629 +
   1.630 +subsection {* The traditional Rolle theorem in one dimension. *}
   1.631 +
   1.632 +lemma vec1_le[simp]:fixes a::real shows "vec1 a \<le> vec1 b \<longleftrightarrow> a \<le> b"
   1.633 +  unfolding vector_less_eq_def by auto
   1.634 +lemma vec1_less[simp]:fixes a::real shows "vec1 a < vec1 b \<longleftrightarrow> a < b"
   1.635 +  unfolding vector_less_def by auto 
   1.636 +
   1.637 +lemma rolle: fixes f::"real\<Rightarrow>real"
   1.638 +  assumes "a < b" "f a = f b" "continuous_on {a..b} f"
   1.639 +  "\<forall>x\<in>{a<..<b}. (f has_derivative f'(x)) (at x)"
   1.640 +  shows "\<exists>x\<in>{a<..<b}. f' x = (\<lambda>v. 0)" proof-
   1.641 +  have "\<exists>x\<in>{a<..<b}. ((\<forall>y\<in>{a<..<b}. f x \<le> f y) \<or> (\<forall>y\<in>{a<..<b}. f y \<le> f x))" proof-
   1.642 +    have "(a + b) / 2 \<in> {a .. b}" using assms(1) by auto hence *:"{a .. b}\<noteq>{}" by auto
   1.643 +    guess d using continuous_attains_sup[OF compact_real_interval * assms(3)] .. note d=this
   1.644 +    guess c using continuous_attains_inf[OF compact_real_interval * assms(3)] .. note c=this
   1.645 +    show ?thesis proof(cases "d\<in>{a<..<b} \<or> c\<in>{a<..<b}")
   1.646 +      case True thus ?thesis apply(erule_tac disjE) apply(rule_tac x=d in bexI)
   1.647 +	apply(rule_tac[3] x=c in bexI) using d c by auto next def e \<equiv> "(a + b) /2"
   1.648 +      case False hence "f d = f c" using d c assms(2) by auto
   1.649 +      hence "\<And>x. x\<in>{a..b} \<Longrightarrow> f x = f d" using c d apply- apply(erule_tac x=x in ballE)+ by auto
   1.650 +      thus ?thesis apply(rule_tac x=e in bexI) unfolding e_def using assms(1) by auto qed qed
   1.651 +  then guess x .. note x=this
   1.652 +  hence "f' x \<circ> dest_vec1 = (\<lambda>v. 0)" apply(rule_tac differential_zero_maxmin[of "vec1 x" "vec1 ` {a<..<b}" "f \<circ> dest_vec1" "(f' x) \<circ> dest_vec1"]) 
   1.653 +    unfolding vec1_interval defer apply(rule open_interval) 
   1.654 +    apply(rule assms(4)[unfolded has_derivative_at_dest_vec1[THEN sym],THEN bspec[where x=x]],assumption)
   1.655 +    unfolding o_def apply(erule disjE,rule disjI2) by(auto simp add: vector_less_def dest_vec1_def) 
   1.656 +  thus ?thesis apply(rule_tac x=x in bexI) unfolding o_def apply rule 
   1.657 +    apply(drule_tac x="vec1 v" in fun_cong) unfolding vec1_dest_vec1 using x(1) by auto qed
   1.658 +
   1.659 +subsection {* One-dimensional mean value theorem. *}
   1.660 +
   1.661 +lemma mvt: fixes f::"real \<Rightarrow> real"
   1.662 +  assumes "a < b" "continuous_on {a .. b} f" "\<forall>x\<in>{a<..<b}. (f has_derivative (f' x)) (at x)"
   1.663 +  shows "\<exists>x\<in>{a<..<b}. (f b - f a = (f' x) (b - a))" proof-
   1.664 +  have "\<exists>x\<in>{a<..<b}. (\<lambda>xa. f' x xa - (f b - f a) / (b - a) * xa) = (\<lambda>v. 0)"
   1.665 +    apply(rule rolle[OF assms(1), of "\<lambda>x. f x - (f b - f a) / (b - a) * x"]) defer
   1.666 +    apply(rule continuous_on_intros assms(2) continuous_on_cmul[where 'b=real, unfolded real_scaleR_def])+ proof
   1.667 +    fix x assume x:"x \<in> {a<..<b}"
   1.668 +    show "((\<lambda>x. f x - (f b - f a) / (b - a) * x) has_derivative (\<lambda>xa. f' x xa - (f b - f a) / (b - a) * xa)) (at x)"
   1.669 +      by(rule has_derivative_intros assms(3)[rule_format,OF x]
   1.670 +        has_derivative_cmul[where 'b=real, unfolded real_scaleR_def])+ 
   1.671 +  qed(insert assms(1), auto simp add:field_simps)
   1.672 +  then guess x .. thus ?thesis apply(rule_tac x=x in bexI) apply(drule fun_cong[of _ _ "b - a"]) by auto qed
   1.673 +
   1.674 +lemma mvt_simple: fixes f::"real \<Rightarrow> real"
   1.675 +  assumes "a<b"  "\<forall>x\<in>{a..b}. (f has_derivative f' x) (at x within {a..b})"
   1.676 +  shows "\<exists>x\<in>{a<..<b}. f b - f a = f' x (b - a)"
   1.677 +  apply(rule mvt) apply(rule assms(1), rule differentiable_imp_continuous_on)
   1.678 +  unfolding differentiable_on_def differentiable_def defer proof 
   1.679 +  fix x assume x:"x \<in> {a<..<b}" show "(f has_derivative f' x) (at x)" unfolding has_derivative_within_open[OF x open_interval_real,THEN sym] 
   1.680 +    apply(rule has_derivative_within_subset) apply(rule assms(2)[rule_format]) using x by auto qed(insert assms(2), auto)
   1.681 +
   1.682 +lemma mvt_very_simple: fixes f::"real \<Rightarrow> real"
   1.683 +  assumes "a \<le> b" "\<forall>x\<in>{a..b}. (f has_derivative f'(x)) (at x within {a..b})"
   1.684 +  shows "\<exists>x\<in>{a..b}. f b - f a = f' x (b - a)" proof(cases "a = b")
   1.685 +  interpret bounded_linear "f' b" using assms(2) assms(1) by auto
   1.686 +  case True thus ?thesis apply(rule_tac x=a in bexI)
   1.687 +    using assms(2)[THEN bspec[where x=a]] unfolding has_derivative_def
   1.688 +    unfolding True using zero by auto next
   1.689 +  case False thus ?thesis using mvt_simple[OF _ assms(2)] using assms(1) by auto qed
   1.690 +
   1.691 +subsection {* A nice generalization (see Havin's proof of 5.19 from Rudin's book). *}
   1.692 +
   1.693 +lemma inner_eq_dot: fixes a::"real^'n::finite"
   1.694 +  shows "a \<bullet> b = inner a b" unfolding inner_vector_def dot_def by auto
   1.695 +
   1.696 +lemma mvt_general: fixes f::"real\<Rightarrow>real^'n::finite"
   1.697 +  assumes "a<b" "continuous_on {a..b} f" "\<forall>x\<in>{a<..<b}. (f has_derivative f'(x)) (at x)"
   1.698 +  shows "\<exists>x\<in>{a<..<b}. norm(f b - f a) \<le> norm(f'(x) (b - a))" proof-
   1.699 +  have "\<exists>x\<in>{a<..<b}. (op \<bullet> (f b - f a) \<circ> f) b - (op \<bullet> (f b - f a) \<circ> f) a = (f b - f a) \<bullet> f' x (b - a)"
   1.700 +    apply(rule mvt) apply(rule assms(1))unfolding inner_eq_dot apply(rule continuous_on_inner continuous_on_intros assms(2))+ 
   1.701 +    unfolding o_def apply(rule,rule has_derivative_lift_dot) using assms(3) by auto
   1.702 +  then guess x .. note x=this
   1.703 +  show ?thesis proof(cases "f a = f b")
   1.704 +    case False have "norm (f b - f a) * norm (f b - f a) = norm (f b - f a)^2" by(simp add:class_semiring.semiring_rules)
   1.705 +    also have "\<dots> = (f b - f a) \<bullet> (f b - f a)" unfolding norm_pow_2 ..
   1.706 +    also have "\<dots> = (f b - f a) \<bullet> f' x (b - a)" using x by auto
   1.707 +    also have "\<dots> \<le> norm (f b - f a) * norm (f' x (b - a))" by(rule norm_cauchy_schwarz)
   1.708 +    finally show ?thesis using False x(1) by(auto simp add: real_mult_left_cancel) next
   1.709 +    case True thus ?thesis using assms(1) apply(rule_tac x="(a + b) /2" in bexI) by auto qed qed
   1.710 +
   1.711 +subsection {* Still more general bound theorem. *}
   1.712 +
   1.713 +lemma differentiable_bound: fixes f::"real^'a::finite \<Rightarrow> real^'b::finite"
   1.714 +  assumes "convex s" "\<forall>x\<in>s. (f has_derivative f'(x)) (at x within s)" "\<forall>x\<in>s. onorm(f' x) \<le> B" and x:"x\<in>s" and y:"y\<in>s"
   1.715 +  shows "norm(f x - f y) \<le> B * norm(x - y)" proof-
   1.716 +  let ?p = "\<lambda>u. x + u *\<^sub>R (y - x)"
   1.717 +  have *:"\<And>u. u\<in>{0..1} \<Longrightarrow> x + u *\<^sub>R (y - x) \<in> s"
   1.718 +    using assms(1)[unfolded convex_alt,rule_format,OF x y] unfolding scaleR_left_diff_distrib scaleR_right_diff_distrib by(auto simp add:group_simps)
   1.719 +  hence 1:"continuous_on {0..1} (f \<circ> ?p)" apply- apply(rule continuous_on_intros continuous_on_vmul)+
   1.720 +    unfolding continuous_on_eq_continuous_within apply(rule,rule differentiable_imp_continuous_within)
   1.721 +    unfolding differentiable_def apply(rule_tac x="f' xa" in exI)
   1.722 +    apply(rule has_derivative_within_subset) apply(rule assms(2)[rule_format]) by auto
   1.723 +  have 2:"\<forall>u\<in>{0<..<1}. ((f \<circ> ?p) has_derivative f' (x + u *\<^sub>R (y - x)) \<circ> (\<lambda>u. 0 + u *\<^sub>R (y - x))) (at u)" proof rule case goal1
   1.724 +    let ?u = "x + u *\<^sub>R (y - x)"
   1.725 +    have "(f \<circ> ?p has_derivative (f' ?u) \<circ> (\<lambda>u. 0 + u *\<^sub>R (y - x))) (at u within {0<..<1})" 
   1.726 +      apply(rule diff_chain_within) apply(rule has_derivative_intros)+ 
   1.727 +      apply(rule has_derivative_within_subset) apply(rule assms(2)[rule_format]) using goal1 * by auto
   1.728 +    thus ?case unfolding has_derivative_within_open[OF goal1 open_interval_real] by auto qed
   1.729 +  guess u using mvt_general[OF zero_less_one 1 2] .. note u = this
   1.730 +  have **:"\<And>x y. x\<in>s \<Longrightarrow> norm (f' x y) \<le> B * norm y" proof- case goal1
   1.731 +    have "norm (f' x y) \<le> onorm (f' x) * norm y"
   1.732 +      using onorm(1)[OF derivative_is_linear[OF assms(2)[rule_format,OF goal1]]] by assumption
   1.733 +    also have "\<dots> \<le> B * norm y" apply(rule mult_right_mono)
   1.734 +      using assms(3)[rule_format,OF goal1] by(auto simp add:field_simps)
   1.735 +    finally show ?case by simp qed
   1.736 +  have "norm (f x - f y) = norm ((f \<circ> (\<lambda>u. x + u *\<^sub>R (y - x))) 1 - (f \<circ> (\<lambda>u. x + u *\<^sub>R (y - x))) 0)"
   1.737 +    by(auto simp add:norm_minus_commute) 
   1.738 +  also have "\<dots> \<le> norm (f' (x + u *\<^sub>R (y - x)) (y - x))" using u by auto
   1.739 +  also have "\<dots> \<le> B * norm(y - x)" apply(rule **) using * and u by auto
   1.740 +  finally show ?thesis by(auto simp add:norm_minus_commute) qed 
   1.741 +
   1.742 +lemma onorm_vec1: fixes f::"real \<Rightarrow> real"
   1.743 +  shows "onorm (\<lambda>x. vec1 (f (dest_vec1 x))) = onorm f" proof-
   1.744 +  have "\<forall>x::real^1. norm x = 1 \<longleftrightarrow> x\<in>{vec1 -1, vec1 (1::real)}" unfolding forall_vec1 unfolding norm_vec1 by auto
   1.745 +  hence 1:"{x. norm x = 1} = {vec1 -1, vec1 (1::real)}" by(auto simp add:norm_vec1)
   1.746 +  have 2:"{norm (vec1 (f (dest_vec1 x))) |x. norm x = 1} = (\<lambda>x. norm (vec1 (f (dest_vec1 x)))) ` {x. norm x=1}" by auto
   1.747 +
   1.748 +  have "\<forall>x::real. norm x = 1 \<longleftrightarrow> x\<in>{-1, 1}" by auto hence 3:"{x. norm x = 1} = {-1, (1::real)}" by auto
   1.749 +  have 4:"{norm (f x) |x. norm x = 1} = (\<lambda>x. norm (f x)) ` {x. norm x=1}" by auto
   1.750 +  show ?thesis unfolding onorm_def 1 2 3 4 by(simp add:Sup_finite_Max norm_vec1) qed
   1.751 +
   1.752 +lemma differentiable_bound_real: fixes f::"real \<Rightarrow> real"
   1.753 +  assumes "convex s" "\<forall>x\<in>s. (f has_derivative f' x) (at x within s)" "\<forall>x\<in>s. onorm(f' x) \<le> B" and x:"x\<in>s" and y:"y\<in>s"
   1.754 +  shows "norm(f x - f y) \<le> B * norm(x - y)" 
   1.755 +  using differentiable_bound[of "vec1 ` s" "vec1 \<circ> f \<circ> dest_vec1" "\<lambda>x. vec1 \<circ> (f' (dest_vec1 x)) \<circ> dest_vec1" B "vec1 x" "vec1 y"]
   1.756 +  unfolding Ball_def forall_vec1 unfolding has_derivative_within_vec1_dest_vec1 image_iff 
   1.757 +  unfolding convex_vec1 unfolding o_def vec1_dest_vec1_simps onorm_vec1 using assms by auto
   1.758 + 
   1.759 +subsection {* In particular. *}
   1.760 +
   1.761 +lemma has_derivative_zero_constant: fixes f::"real\<Rightarrow>real"
   1.762 +  assumes "convex s" "\<forall>x\<in>s. (f has_derivative (\<lambda>h. 0)) (at x within s)"
   1.763 +  shows "\<exists>c. \<forall>x\<in>s. f x = c" proof(cases "s={}")
   1.764 +  case False then obtain x where "x\<in>s" by auto
   1.765 +  have "\<And>y. y\<in>s \<Longrightarrow> f x = f y" proof- case goal1
   1.766 +    thus ?case using differentiable_bound_real[OF assms(1-2), of 0 x y] and `x\<in>s`
   1.767 +    unfolding onorm_vec1[of "\<lambda>x. 0", THEN sym] onorm_const norm_vec1 by auto qed
   1.768 +  thus ?thesis apply(rule_tac x="f x" in exI) by auto qed auto
   1.769 +
   1.770 +lemma has_derivative_zero_unique: fixes f::"real\<Rightarrow>real"
   1.771 +  assumes "convex s" "a \<in> s" "f a = c" "\<forall>x\<in>s. (f has_derivative (\<lambda>h. 0)) (at x within s)" "x\<in>s"
   1.772 +  shows "f x = c" using has_derivative_zero_constant[OF assms(1,4)] using assms(2-3,5) by auto
   1.773 +
   1.774 +subsection {* Differentiability of inverse function (most basic form). *}
   1.775 +
   1.776 +lemma has_derivative_inverse_basic: fixes f::"real^'b::finite \<Rightarrow> real^'c::finite"
   1.777 +  assumes "(f has_derivative f') (at (g y))" "bounded_linear g'" "g' \<circ> f' = id" "continuous (at y) g"
   1.778 +  "open t" "y \<in> t" "\<forall>z\<in>t. f(g z) = z"
   1.779 +  shows "(g has_derivative g') (at y)" proof-
   1.780 +  interpret f': bounded_linear f' using assms unfolding has_derivative_def by auto
   1.781 +  interpret g': bounded_linear g' using assms by auto
   1.782 +  guess C using bounded_linear.pos_bounded[OF assms(2)] .. note C = this
   1.783 +(*  have fgid:"\<And>x. g' (f' x) = x" using assms(3) unfolding o_def id_def apply()*)
   1.784 +  have lem1:"\<forall>e>0. \<exists>d>0. \<forall>z. norm(z - y) < d \<longrightarrow> norm(g z - g y - g'(z - y)) \<le> e * norm(g z - g y)" proof(rule,rule) case goal1
   1.785 +    have *:"e / C > 0" apply(rule divide_pos_pos) using `e>0` C by auto
   1.786 +    guess d0 using assms(1)[unfolded has_derivative_at_alt,THEN conjunct2,rule_format,OF *] .. note d0=this
   1.787 +    guess d1 using assms(4)[unfolded continuous_at Lim_at,rule_format,OF d0[THEN conjunct1]] .. note d1=this
   1.788 +    guess d2 using assms(5)[unfolded open_dist,rule_format,OF assms(6)] .. note d2=this
   1.789 +    guess d using real_lbound_gt_zero[OF d1[THEN conjunct1] d2[THEN conjunct1]] .. note d=this
   1.790 +    thus ?case apply(rule_tac x=d in exI) apply rule defer proof(rule,rule)
   1.791 +      fix z assume as:"norm (z - y) < d" hence "z\<in>t" using d2 d unfolding vector_dist_norm by auto
   1.792 +      have "norm (g z - g y - g' (z - y)) \<le> norm (g' (f (g z) - y - f' (g z - g y)))"
   1.793 +        unfolding g'.diff f'.diff unfolding assms(3)[unfolded o_def id_def, THEN fun_cong] 
   1.794 +	unfolding assms(7)[rule_format,OF `z\<in>t`] apply(subst norm_minus_cancel[THEN sym]) by auto
   1.795 +      also have "\<dots> \<le> norm(f (g z) - y - f' (g z - g y)) * C" by(rule C[THEN conjunct2,rule_format]) 
   1.796 +      also have "\<dots> \<le> (e / C) * norm (g z - g y) * C" apply(rule mult_right_mono)
   1.797 +	apply(rule d0[THEN conjunct2,rule_format,unfolded assms(7)[rule_format,OF `y\<in>t`]]) apply(cases "z=y") defer
   1.798 +	apply(rule d1[THEN conjunct2, unfolded vector_dist_norm,rule_format]) using as d C d0 by auto
   1.799 +      also have "\<dots> \<le> e * norm (g z - g y)" using C by(auto simp add:field_simps)
   1.800 +      finally show "norm (g z - g y - g' (z - y)) \<le> e * norm (g z - g y)" by simp qed auto qed
   1.801 +  have *:"(0::real) < 1 / 2" by auto guess d using lem1[rule_format,OF *] .. note d=this def B\<equiv>"C*2"
   1.802 +  have "B>0" unfolding B_def using C by auto
   1.803 +  have lem2:"\<forall>z. norm(z - y) < d \<longrightarrow> norm(g z - g y) \<le> B * norm(z - y)" proof(rule,rule) case goal1
   1.804 +    have "norm (g z - g y) \<le> norm(g' (z - y)) + norm ((g z - g y) - g'(z - y))" by(rule norm_triangle_sub)
   1.805 +    also have "\<dots> \<le> norm(g' (z - y)) + 1 / 2 * norm (g z - g y)" apply(rule add_left_mono) using d and goal1 by auto
   1.806 +    also have "\<dots> \<le> norm (z - y) * C + 1 / 2 * norm (g z - g y)" apply(rule add_right_mono) using C by auto
   1.807 +    finally show ?case unfolding B_def by(auto simp add:field_simps) qed
   1.808 +  show ?thesis unfolding has_derivative_at_alt proof(rule,rule assms,rule,rule) case goal1
   1.809 +    hence *:"e/B >0" apply-apply(rule divide_pos_pos) using `B>0` by auto
   1.810 +    guess d' using lem1[rule_format,OF *] .. note d'=this
   1.811 +    guess k using real_lbound_gt_zero[OF d[THEN conjunct1] d'[THEN conjunct1]] .. note k=this
   1.812 +    show ?case apply(rule_tac x=k in exI,rule) defer proof(rule,rule) fix z assume as:"norm(z - y) < k"
   1.813 +      hence "norm (g z - g y - g' (z - y)) \<le> e / B * norm(g z - g y)" using d' k by auto
   1.814 +      also have "\<dots> \<le> e * norm(z - y)" unfolding mult_frac_num pos_divide_le_eq[OF `B>0`]
   1.815 +	using lem2[THEN spec[where x=z]] using k as using `e>0` by(auto simp add:field_simps)
   1.816 +      finally show "norm (g z - g y - g' (z - y)) \<le> e * norm (z - y)" by simp qed(insert k, auto) qed qed
   1.817 +
   1.818 +subsection {* Simply rewrite that based on the domain point x. *}
   1.819 +
   1.820 +lemma has_derivative_inverse_basic_x: fixes f::"real^'b::finite \<Rightarrow> real^'c::finite"
   1.821 +  assumes "(f has_derivative f') (at x)" "bounded_linear g'" "g' o f' = id"
   1.822 +  "continuous (at (f x)) g" "g(f x) = x" "open t" "f x \<in> t" "\<forall>y\<in>t. f(g y) = y"
   1.823 +  shows "(g has_derivative g') (at (f(x)))"
   1.824 +  apply(rule has_derivative_inverse_basic) using assms by auto
   1.825 +
   1.826 +subsection {* This is the version in Dieudonne', assuming continuity of f and g. *}
   1.827 +
   1.828 +lemma has_derivative_inverse_dieudonne: fixes f::"real^'a::finite \<Rightarrow> real^'b::finite"
   1.829 +  assumes "open s" "open (f ` s)" "continuous_on s f" "continuous_on (f ` s) g" "\<forall>x\<in>s. g(f x) = x"
   1.830 +  (**) "x\<in>s" "(f has_derivative f') (at x)"  "bounded_linear g'" "g' o f' = id"
   1.831 +  shows "(g has_derivative g') (at (f x))"
   1.832 +  apply(rule has_derivative_inverse_basic_x[OF assms(7-9) _ _ assms(2)])
   1.833 +  using assms(3-6) unfolding continuous_on_eq_continuous_at[OF assms(1)]  continuous_on_eq_continuous_at[OF assms(2)] by auto
   1.834 +
   1.835 +subsection {* Here's the simplest way of not assuming much about g. *}
   1.836 +
   1.837 +lemma has_derivative_inverse: fixes f::"real^'a::finite \<Rightarrow> real^'b::finite"
   1.838 +  assumes "compact s" "x \<in> s" "f x \<in> interior(f ` s)" "continuous_on s f"
   1.839 +  "\<forall>y\<in>s. g(f y) = y" "(f has_derivative f') (at x)" "bounded_linear g'" "g' \<circ> f' = id"
   1.840 +  shows "(g has_derivative g') (at (f x))" proof-
   1.841 +  { fix y assume "y\<in>interior (f ` s)" 
   1.842 +    then obtain x where "x\<in>s" and *:"y = f x" unfolding image_iff using interior_subset by auto
   1.843 +    have "f (g y) = y" unfolding * and assms(5)[rule_format,OF `x\<in>s`] .. } note * = this
   1.844 +  show ?thesis apply(rule has_derivative_inverse_basic_x[OF assms(6-8)])
   1.845 +    apply(rule continuous_on_interior[OF _ assms(3)]) apply(rule continuous_on_inverse[OF assms(4,1)])
   1.846 +    apply(rule assms(2,5) assms(5)[rule_format] open_interior assms(3))+ by(rule, rule *, assumption)  qed
   1.847 +
   1.848 +subsection {* Proving surjectivity via Brouwer fixpoint theorem. *}
   1.849 +
   1.850 +lemma brouwer_surjective: fixes f::"real^'n::finite \<Rightarrow> real^'n"
   1.851 +  assumes "compact t" "convex t"  "t \<noteq> {}" "continuous_on t f"
   1.852 +  "\<forall>x\<in>s. \<forall>y\<in>t. x + (y - f y) \<in> t" "x\<in>s"
   1.853 +  shows "\<exists>y\<in>t. f y = x" proof-
   1.854 +  have *:"\<And>x y. f y = x \<longleftrightarrow> x + (y - f y) = y" by(auto simp add:group_simps)
   1.855 +  show ?thesis  unfolding * apply(rule brouwer[OF assms(1-3), of "\<lambda>y. x + (y - f y)"])
   1.856 +    apply(rule continuous_on_intros assms)+ using assms(4-6) by auto qed
   1.857 +
   1.858 +lemma brouwer_surjective_cball: fixes f::"real^'n::finite \<Rightarrow> real^'n"
   1.859 +  assumes "0 < e" "continuous_on (cball a e) f"
   1.860 +  "\<forall>x\<in>s. \<forall>y\<in>cball a e. x + (y - f y) \<in> cball a e" "x\<in>s"
   1.861 +  shows "\<exists>y\<in>cball a e. f y = x" apply(rule brouwer_surjective) apply(rule compact_cball convex_cball)+
   1.862 +  unfolding cball_eq_empty using assms by auto 
   1.863 +
   1.864 +text {* See Sussmann: "Multidifferential calculus", Theorem 2.1.1 *}
   1.865 +
   1.866 +lemma sussmann_open_mapping: fixes f::"real^'a::finite \<Rightarrow> real^'b::finite"
   1.867 +  assumes "open s" "continuous_on s f" "x \<in> s" 
   1.868 +  "(f has_derivative f') (at x)" "bounded_linear g'" "f' \<circ> g' = id"
   1.869 +  (**) "t \<subseteq> s" "x \<in> interior t"
   1.870 +  shows "f x \<in> interior (f ` t)" proof- 
   1.871 +  interpret f':bounded_linear f' using assms unfolding has_derivative_def by auto
   1.872 +  interpret g':bounded_linear g' using assms by auto
   1.873 +  guess B using bounded_linear.pos_bounded[OF assms(5)] .. note B=this hence *:"1/(2*B)>0" by(auto intro!: divide_pos_pos)
   1.874 +  guess e0 using assms(4)[unfolded has_derivative_at_alt,THEN conjunct2,rule_format,OF *] .. note e0=this
   1.875 +  guess e1 using assms(8)[unfolded mem_interior_cball] .. note e1=this
   1.876 +  have *:"0<e0/B" "0<e1/B" apply(rule_tac[!] divide_pos_pos) using e0 e1 B by auto
   1.877 +  guess e using real_lbound_gt_zero[OF *] .. note e=this
   1.878 +  have "\<forall>z\<in>cball (f x) (e/2). \<exists>y\<in>cball (f x) e. f (x + g' (y - f x)) = z"
   1.879 +    apply(rule,rule brouwer_surjective_cball[where s="cball (f x) (e/2)"])
   1.880 +    prefer 3 apply(rule,rule) proof- 
   1.881 +    show "continuous_on (cball (f x) e) (\<lambda>y. f (x + g' (y - f x)))" unfolding g'.diff
   1.882 +      apply(rule continuous_on_compose[of _ _ f, unfolded o_def])
   1.883 +      apply(rule continuous_on_intros linear_continuous_on[OF assms(5)])+
   1.884 +      apply(rule continuous_on_subset[OF assms(2)]) apply(rule,unfold image_iff,erule bexE) proof-
   1.885 +      fix y z assume as:"y \<in>cball (f x) e"  "z = x + (g' y - g' (f x))"
   1.886 +      have "dist x z = norm (g' (f x) - g' y)" unfolding as(2) and vector_dist_norm by auto
   1.887 +      also have "\<dots> \<le> norm (f x - y) * B" unfolding g'.diff[THEN sym] using B by auto
   1.888 +      also have "\<dots> \<le> e * B" using as(1)[unfolded mem_cball vector_dist_norm] using B by auto
   1.889 +      also have "\<dots> \<le> e1" using e unfolding less_divide_eq using B by auto
   1.890 +      finally have "z\<in>cball x e1" unfolding mem_cball by force
   1.891 +      thus "z \<in> s" using e1 assms(7) by auto qed next
   1.892 +    fix y z assume as:"y \<in> cball (f x) (e / 2)" "z \<in> cball (f x) e"
   1.893 +    have "norm (g' (z - f x)) \<le> norm (z - f x) * B" using B by auto
   1.894 +    also have "\<dots> \<le> e * B" apply(rule mult_right_mono) using as(2)[unfolded mem_cball vector_dist_norm] and B unfolding norm_minus_commute by auto
   1.895 +    also have "\<dots> < e0" using e and B unfolding less_divide_eq by auto
   1.896 +    finally have *:"norm (x + g' (z - f x) - x) < e0" by auto
   1.897 +    have **:"f x + f' (x + g' (z - f x) - x) = z" using assms(6)[unfolded o_def id_def,THEN cong] by auto
   1.898 +    have "norm (f x - (y + (z - f (x + g' (z - f x))))) \<le> norm (f (x + g' (z - f x)) - z) + norm (f x - y)"
   1.899 +      using norm_triangle_ineq[of "f (x + g'(z - f x)) - z" "f x - y"] by(auto simp add:group_simps)
   1.900 +    also have "\<dots> \<le> 1 / (B * 2) * norm (g' (z - f x)) + norm (f x - y)" using e0[THEN conjunct2,rule_format,OF *] unfolding group_simps ** by auto 
   1.901 +    also have "\<dots> \<le> 1 / (B * 2) * norm (g' (z - f x)) + e/2" using as(1)[unfolded mem_cball vector_dist_norm] by auto
   1.902 +    also have "\<dots> \<le> 1 / (B * 2) * B * norm (z - f x) + e/2" using * and B by(auto simp add:field_simps)
   1.903 +    also have "\<dots> \<le> 1 / 2 * norm (z - f x) + e/2" by auto
   1.904 +    also have "\<dots> \<le> e/2 + e/2" apply(rule add_right_mono) using as(2)[unfolded mem_cball vector_dist_norm] unfolding norm_minus_commute by auto
   1.905 +    finally show "y + (z - f (x + g' (z - f x))) \<in> cball (f x) e" unfolding mem_cball vector_dist_norm by auto
   1.906 +  qed(insert e, auto) note lem = this
   1.907 +  show ?thesis unfolding mem_interior apply(rule_tac x="e/2" in exI)
   1.908 +    apply(rule,rule divide_pos_pos) prefer 3 proof 
   1.909 +    fix y assume "y \<in> ball (f x) (e/2)" hence *:"y\<in>cball (f x) (e/2)" by auto
   1.910 +    guess z using lem[rule_format,OF *] .. note z=this
   1.911 +    hence "norm (g' (z - f x)) \<le> norm (z - f x) * B" using B by(auto simp add:field_simps)
   1.912 +    also have "\<dots> \<le> e * B" apply(rule mult_right_mono) using z(1) unfolding mem_cball vector_dist_norm norm_minus_commute using B by auto
   1.913 +    also have "\<dots> \<le> e1"  using e B unfolding less_divide_eq by auto
   1.914 +    finally have "x + g'(z - f x) \<in> t" apply- apply(rule e1[THEN conjunct2,unfolded subset_eq,rule_format]) 
   1.915 +      unfolding mem_cball vector_dist_norm by auto
   1.916 +    thus "y \<in> f ` t" using z by auto qed(insert e, auto) qed
   1.917 +
   1.918 +text {* Hence the following eccentric variant of the inverse function theorem.    *)
   1.919 +(* This has no continuity assumptions, but we do need the inverse function.  *)
   1.920 +(* We could put f' o g = I but this happens to fit with the minimal linear   *)
   1.921 +(* algebra theory I've set up so far. *}
   1.922 +
   1.923 +lemma has_derivative_inverse_strong: fixes f::"real^'n::finite \<Rightarrow> real^'n"
   1.924 +  assumes "open s" "x \<in> s" "continuous_on s f"
   1.925 +  "\<forall>x\<in>s. g(f x) = x" "(f has_derivative f') (at x)" "f' o g' = id"
   1.926 +  shows "(g has_derivative g') (at (f x))" proof-
   1.927 +  have linf:"bounded_linear f'" using assms(5) unfolding has_derivative_def by auto
   1.928 +  hence ling:"bounded_linear g'" unfolding linear_conv_bounded_linear[THEN sym]
   1.929 +    apply- apply(rule right_inverse_linear) using assms(6) by auto 
   1.930 +  moreover have "g' \<circ> f' = id" using assms(6) linf ling unfolding linear_conv_bounded_linear[THEN sym]
   1.931 +    using linear_inverse_left by auto
   1.932 +  moreover have *:"\<forall>t\<subseteq>s. x\<in>interior t \<longrightarrow> f x \<in> interior (f ` t)" apply(rule,rule,rule,rule sussmann_open_mapping )
   1.933 +    apply(rule assms ling)+ by auto
   1.934 +  have "continuous (at (f x)) g" unfolding continuous_at Lim_at proof(rule,rule)
   1.935 +    fix e::real assume "e>0"
   1.936 +    hence "f x \<in> interior (f ` (ball x e \<inter> s))" using *[rule_format,of "ball x e \<inter> s"] `x\<in>s`
   1.937 +      by(auto simp add: interior_open[OF open_ball] interior_open[OF assms(1)])
   1.938 +    then guess d unfolding mem_interior .. note d=this
   1.939 +    show "\<exists>d>0. \<forall>y. 0 < dist y (f x) \<and> dist y (f x) < d \<longrightarrow> dist (g y) (g (f x)) < e"
   1.940 +      apply(rule_tac x=d in exI) apply(rule,rule d[THEN conjunct1]) proof(rule,rule) case goal1
   1.941 +      hence "g y \<in> g ` f ` (ball x e \<inter> s)" using d[THEN conjunct2,unfolded subset_eq,THEN bspec[where x=y]]
   1.942 +	by(auto simp add:dist_commute)
   1.943 +      hence "g y \<in> ball x e \<inter> s" using assms(4) by auto
   1.944 +      thus "dist (g y) (g (f x)) < e" using assms(4)[rule_format,OF `x\<in>s`] by(auto simp add:dist_commute) qed qed
   1.945 +  moreover have "f x \<in> interior (f ` s)" apply(rule sussmann_open_mapping)
   1.946 +    apply(rule assms ling)+ using interior_open[OF assms(1)] and `x\<in>s` by auto
   1.947 +  moreover have "\<And>y. y \<in> interior (f ` s) \<Longrightarrow> f (g y) = y" proof- case goal1
   1.948 +    hence "y\<in>f ` s" using interior_subset by auto then guess z unfolding image_iff ..
   1.949 +    thus ?case using assms(4) by auto qed
   1.950 +  ultimately show ?thesis apply- apply(rule has_derivative_inverse_basic_x[OF assms(5)]) using assms by auto qed 
   1.951 +
   1.952 +subsection {* A rewrite based on the other domain. *}
   1.953 +
   1.954 +lemma has_derivative_inverse_strong_x: fixes f::"real^'n::finite \<Rightarrow> real^'n"
   1.955 +  assumes "open s" "g y \<in> s" "continuous_on s f"
   1.956 +  "\<forall>x\<in>s. g(f x) = x" "(f has_derivative f') (at (g y))" "f' o g' = id" "f(g y) = y"
   1.957 +  shows "(g has_derivative g') (at y)"
   1.958 +  using has_derivative_inverse_strong[OF assms(1-6)] unfolding assms(7) by simp
   1.959 +
   1.960 +subsection {* On a region. *}
   1.961 +
   1.962 +lemma has_derivative_inverse_on: fixes f::"real^'n::finite \<Rightarrow> real^'n"
   1.963 +  assumes "open s" "\<forall>x\<in>s. (f has_derivative f'(x)) (at x)" "\<forall>x\<in>s. g(f x) = x" "f'(x) o g'(x) = id" "x\<in>s"
   1.964 +  shows "(g has_derivative g'(x)) (at (f x))"
   1.965 +  apply(rule has_derivative_inverse_strong[where g'="g' x" and f=f]) apply(rule assms)+
   1.966 +  unfolding continuous_on_eq_continuous_at[OF assms(1)]
   1.967 +  apply(rule,rule differentiable_imp_continuous_at) unfolding differentiable_def using assms by auto
   1.968 +
   1.969 +subsection {* Invertible derivative continous at a point implies local injectivity.     *)
   1.970 +(* It's only for this we need continuity of the derivative, except of course *)
   1.971 +(* if we want the fact that the inverse derivative is also continuous. So if *)
   1.972 +(* we know for some other reason that the inverse function exists, it's OK. *}
   1.973 +
   1.974 +lemma bounded_linear_sub: "bounded_linear f \<Longrightarrow> bounded_linear g ==> bounded_linear (\<lambda>x. f x - g x)"
   1.975 +  using bounded_linear_add[of f "\<lambda>x. - g x"] bounded_linear_minus[of g] by(auto simp add:group_simps)
   1.976 +
   1.977 +lemma has_derivative_locally_injective: fixes f::"real^'n::finite \<Rightarrow> real^'m::finite"
   1.978 +  assumes "a \<in> s" "open s" "bounded_linear g'" "g' o f'(a) = id"
   1.979 +  "\<forall>x\<in>s. (f has_derivative f'(x)) (at x)"
   1.980 +  "\<forall>e>0. \<exists>d>0. \<forall>x. dist a x < d \<longrightarrow> onorm(\<lambda>v. f' x v - f' a v) < e"
   1.981 +  obtains t where "a \<in> t" "open t" "\<forall>x\<in>t. \<forall>x'\<in>t. (f x' = f x) \<longrightarrow> (x' = x)" proof-
   1.982 +  interpret bounded_linear g' using assms by auto
   1.983 +  note f'g' = assms(4)[unfolded id_def o_def,THEN cong]
   1.984 +  have "g' (f' a 1) = 1" using f'g' by auto
   1.985 +  hence *:"0 < onorm g'" unfolding onorm_pos_lt[OF assms(3)[unfolded linear_linear]] by fastsimp
   1.986 +  def k \<equiv> "1 / onorm g' / 2" have *:"k>0" unfolding k_def using * by auto
   1.987 +  guess d1 using assms(6)[rule_format,OF *] .. note d1=this
   1.988 +  from `open s` obtain d2 where "d2>0" "ball a d2 \<subseteq> s" using `a\<in>s` ..
   1.989 +  obtain d2 where "d2>0" "ball a d2 \<subseteq> s" using assms(2,1) ..
   1.990 +  guess d2 using assms(2)[unfolded open_contains_ball,rule_format,OF `a\<in>s`] .. note d2=this
   1.991 +  guess d using real_lbound_gt_zero[OF d1[THEN conjunct1] d2[THEN conjunct1]] .. note d = this
   1.992 +  show ?thesis proof show "a\<in>ball a d" using d by auto
   1.993 +    show "\<forall>x\<in>ball a d. \<forall>x'\<in>ball a d. f x' = f x \<longrightarrow> x' = x" proof(intro strip)
   1.994 +      fix x y assume as:"x\<in>ball a d" "y\<in>ball a d" "f x = f y"
   1.995 +      def ph \<equiv> "\<lambda>w. w - g'(f w - f x)" have ph':"ph = g' \<circ> (\<lambda>w. f' a w - (f w - f x))"
   1.996 +	unfolding ph_def o_def unfolding diff using f'g' by(auto simp add:group_simps)
   1.997 +      have "norm (ph x - ph y) \<le> (1/2) * norm (x - y)"
   1.998 +	apply(rule differentiable_bound[OF convex_ball _ _ as(1-2), where f'="\<lambda>x v. v - g'(f' x v)"])
   1.999 +	apply(rule_tac[!] ballI) proof- fix u assume u:"u \<in> ball a d" hence "u\<in>s" using d d2 by auto
  1.1000 +	have *:"(\<lambda>v. v - g' (f' u v)) = g' \<circ> (\<lambda>w. f' a w - f' u w)" unfolding o_def and diff using f'g' by auto
  1.1001 +	show "(ph has_derivative (\<lambda>v. v - g' (f' u v))) (at u within ball a d)"
  1.1002 +	  unfolding ph' * apply(rule diff_chain_within) defer apply(rule bounded_linear.has_derivative[OF assms(3)])
  1.1003 +	  apply(rule has_derivative_intros) defer apply(rule has_derivative_sub[where g'="\<lambda>x.0",unfolded diff_0_right])
  1.1004 +	  apply(rule has_derivative_at_within) using assms(5) and `u\<in>s` `a\<in>s`
  1.1005 +	  by(auto intro!: has_derivative_intros derivative_linear)
  1.1006 +	have **:"bounded_linear (\<lambda>x. f' u x - f' a x)" "bounded_linear (\<lambda>x. f' a x - f' u x)" apply(rule_tac[!] bounded_linear_sub)
  1.1007 +	  apply(rule_tac[!] derivative_linear) using assms(5) `u\<in>s` `a\<in>s` by auto
  1.1008 +	have "onorm (\<lambda>v. v - g' (f' u v)) \<le> onorm g' * onorm (\<lambda>w. f' a w - f' u w)" unfolding * apply(rule onorm_compose)
  1.1009 +	  unfolding linear_conv_bounded_linear by(rule assms(3) **)+ 
  1.1010 +	also have "\<dots> \<le> onorm g' * k" apply(rule mult_left_mono) 
  1.1011 +	  using d1[THEN conjunct2,rule_format,of u] using onorm_neg[OF **(1)[unfolded linear_linear]]
  1.1012 +	  using d and u and onorm_pos_le[OF assms(3)[unfolded linear_linear]] by(auto simp add:group_simps) 
  1.1013 +	also have "\<dots> \<le> 1/2" unfolding k_def by auto
  1.1014 +	finally show "onorm (\<lambda>v. v - g' (f' u v)) \<le> 1 / 2" by assumption qed
  1.1015 +      moreover have "norm (ph y - ph x) = norm (y - x)" apply(rule arg_cong[where f=norm])
  1.1016 +	unfolding ph_def using diff unfolding as by auto
  1.1017 +      ultimately show "x = y" unfolding norm_minus_commute by auto qed qed auto qed
  1.1018 +
  1.1019 +subsection {* Uniformly convergent sequence of derivatives. *}
  1.1020 +
  1.1021 +lemma has_derivative_sequence_lipschitz_lemma: fixes f::"nat \<Rightarrow> real^'m::finite \<Rightarrow> real^'n::finite"
  1.1022 +  assumes "convex s" "\<forall>n. \<forall>x\<in>s. ((f n) has_derivative (f' n x)) (at x within s)"
  1.1023 +  "\<forall>n\<ge>N. \<forall>x\<in>s. \<forall>h. norm(f' n x h - g' x h) \<le> e * norm(h)"
  1.1024 +  shows "\<forall>m\<ge>N. \<forall>n\<ge>N. \<forall>x\<in>s. \<forall>y\<in>s. norm((f m x - f n x) - (f m y - f n y)) \<le> 2 * e * norm(x - y)" proof(default)+ 
  1.1025 +  fix m n x y assume as:"N\<le>m" "N\<le>n" "x\<in>s" "y\<in>s"
  1.1026 +  show "norm((f m x - f n x) - (f m y - f n y)) \<le> 2 * e * norm(x - y)"
  1.1027 +    apply(rule differentiable_bound[where f'="\<lambda>x h. f' m x h - f' n x h", OF assms(1) _ _ as(3-4)]) apply(rule_tac[!] ballI) proof-
  1.1028 +    fix x assume "x\<in>s" show "((\<lambda>a. f m a - f n a) has_derivative (\<lambda>h. f' m x h - f' n x h)) (at x within s)"
  1.1029 +      by(rule has_derivative_intros assms(2)[rule_format] `x\<in>s`)+
  1.1030 +    { fix h have "norm (f' m x h - f' n x h) \<le> norm (f' m x h - g' x h) + norm (f' n x h - g' x h)"
  1.1031 +	using norm_triangle_ineq[of "f' m x h - g' x h" "- f' n x h + g' x h"] unfolding norm_minus_commute by(auto simp add:group_simps) 
  1.1032 +      also have "\<dots> \<le> e * norm h+ e * norm h"  using assms(3)[rule_format,OF `N\<le>m` `x\<in>s`, of h] assms(3)[rule_format,OF `N\<le>n` `x\<in>s`, of h]
  1.1033 +	by(auto simp add:field_simps)
  1.1034 +      finally have "norm (f' m x h - f' n x h) \<le> 2 * e * norm h" by auto }
  1.1035 +    thus "onorm (\<lambda>h. f' m x h - f' n x h) \<le> 2 * e" apply-apply(rule onorm(2)) apply(rule linear_compose_sub)
  1.1036 +      unfolding linear_conv_bounded_linear using assms(2)[rule_format,OF `x\<in>s`, THEN derivative_linear] by auto qed qed
  1.1037 +
  1.1038 +lemma has_derivative_sequence_lipschitz: fixes f::"nat \<Rightarrow> real^'m::finite \<Rightarrow> real^'n::finite"
  1.1039 +  assumes "convex s" "\<forall>n. \<forall>x\<in>s. ((f n) has_derivative (f' n x)) (at x within s)"
  1.1040 +  "\<forall>e>0. \<exists>N. \<forall>n\<ge>N. \<forall>x\<in>s. \<forall>h. norm(f' n x h - g' x h) \<le> e * norm(h)" "0 < e"
  1.1041 +  shows "\<forall>e>0. \<exists>N. \<forall>m\<ge>N. \<forall>n\<ge>N. \<forall>x\<in>s. \<forall>y\<in>s. norm((f m x - f n x) - (f m y - f n y)) \<le> e * norm(x - y)" proof(rule,rule)
  1.1042 +  case goal1 have *:"2 * (1/2* e) = e" "1/2 * e >0" using `e>0` by auto
  1.1043 +  guess N using assms(3)[rule_format,OF *(2)] ..
  1.1044 +  thus ?case apply(rule_tac x=N in exI) apply(rule has_derivative_sequence_lipschitz_lemma[where e="1/2 *e", unfolded *]) using assms by auto qed
  1.1045 +
  1.1046 +lemma has_derivative_sequence: fixes f::"nat\<Rightarrow>real^'m::finite\<Rightarrow>real^'n::finite"
  1.1047 +  assumes "convex s" "\<forall>n. \<forall>x\<in>s. ((f n) has_derivative (f' n x)) (at x within s)"
  1.1048 +  "\<forall>e>0. \<exists>N. \<forall>n\<ge>N. \<forall>x\<in>s. \<forall>h. norm(f' n x h - g' x h) \<le> e * norm(h)"
  1.1049 +  "x0 \<in> s"  "((\<lambda>n. f n x0) ---> l) sequentially"
  1.1050 +  shows "\<exists>g. \<forall>x\<in>s. ((\<lambda>n. f n x) ---> g x) sequentially \<and> (g has_derivative g'(x)) (at x within s)" proof-
  1.1051 +  have lem1:"\<forall>e>0. \<exists>N. \<forall>m\<ge>N. \<forall>n\<ge>N. \<forall>x\<in>s. \<forall>y\<in>s. norm((f m x - f n x) - (f m y - f n y)) \<le> e * norm(x - y)"
  1.1052 +    apply(rule has_derivative_sequence_lipschitz[where e="42::nat"]) apply(rule assms)+ by auto
  1.1053 +  have "\<exists>g. \<forall>x\<in>s. ((\<lambda>n. f n x) ---> g x) sequentially" apply(rule bchoice) unfolding convergent_eq_cauchy proof
  1.1054 +    fix x assume "x\<in>s" show "Cauchy (\<lambda>n. f n x)" proof(cases "x=x0")
  1.1055 +      case True thus ?thesis using convergent_imp_cauchy[OF assms(5)] by auto next
  1.1056 +      case False show ?thesis unfolding Cauchy_def proof(rule,rule)
  1.1057 +	fix e::real assume "e>0" hence *:"e/2>0" "e/2/norm(x-x0)>0" using False by(auto intro!:divide_pos_pos)
  1.1058 +	guess M using convergent_imp_cauchy[OF assms(5), unfolded Cauchy_def, rule_format,OF *(1)] .. note M=this
  1.1059 +	guess N using lem1[rule_format,OF *(2)] .. note N = this
  1.1060 +	show " \<exists>M. \<forall>m\<ge>M. \<forall>n\<ge>M. dist (f m x) (f n x) < e" apply(rule_tac x="max M N" in exI) proof(default+)
  1.1061 +	  fix m n assume as:"max M N \<le>m" "max M N\<le>n"
  1.1062 +	  have "dist (f m x) (f n x) \<le> norm (f m x0 - f n x0) + norm (f m x - f n x - (f m x0 - f n x0))"
  1.1063 +	    unfolding vector_dist_norm by(rule norm_triangle_sub)
  1.1064 +	  also have "\<dots> \<le> norm (f m x0 - f n x0) + e / 2" using N[rule_format,OF _ _ `x\<in>s` `x0\<in>s`, of m n] and as and False by auto
  1.1065 +	  also have "\<dots> < e / 2 + e / 2" apply(rule add_strict_right_mono) using as and M[rule_format] unfolding vector_dist_norm by auto 
  1.1066 +	  finally show "dist (f m x) (f n x) < e" by auto qed qed qed qed
  1.1067 +  then guess g .. note g = this
  1.1068 +  have lem2:"\<forall>e>0. \<exists>N. \<forall>n\<ge>N. \<forall>x\<in>s. \<forall>y\<in>s. norm((f n x - f n y) - (g x - g y)) \<le> e * norm(x - y)" proof(rule,rule)
  1.1069 +    fix e::real assume *:"e>0" guess N using lem1[rule_format,OF *] .. note N=this
  1.1070 +    show "\<exists>N. \<forall>n\<ge>N. \<forall>x\<in>s. \<forall>y\<in>s. norm (f n x - f n y - (g x - g y)) \<le> e * norm (x - y)" apply(rule_tac x=N in exI) proof(default+)
  1.1071 +      fix n x y assume as:"N \<le> n" "x \<in> s" "y \<in> s"
  1.1072 +      have "eventually (\<lambda>xa. norm (f n x - f n y - (f xa x - f xa y)) \<le> e * norm (x - y)) sequentially" 
  1.1073 +	unfolding eventually_sequentially apply(rule_tac x=N in exI) proof(rule,rule)
  1.1074 +	fix m assume "N\<le>m" thus "norm (f n x - f n y - (f m x - f m y)) \<le> e * norm (x - y)"
  1.1075 +	  using N[rule_format, of n m x y] and as by(auto simp add:group_simps) qed
  1.1076 +      thus "norm (f n x - f n y - (g x - g y)) \<le> e * norm (x - y)" apply-
  1.1077 +	apply(rule Lim_norm_ubound[OF trivial_limit_sequentially, where f="\<lambda>m. (f n x - f n y) - (f m x - f m y)"])
  1.1078 +	apply(rule Lim_sub Lim_const g[rule_format] as)+ by assumption qed qed
  1.1079 +  show ?thesis unfolding has_derivative_within_alt apply(rule_tac x=g in exI)
  1.1080 +    apply(rule,rule,rule g[rule_format],assumption) proof fix x assume "x\<in>s"
  1.1081 +    have lem3:"\<forall>u. ((\<lambda>n. f' n x u) ---> g' x u) sequentially" unfolding Lim_sequentially proof(rule,rule,rule)
  1.1082 +      fix u and e::real assume "e>0" show "\<exists>N. \<forall>n\<ge>N. dist (f' n x u) (g' x u) < e" proof(cases "u=0")
  1.1083 +	case True guess N using assms(3)[rule_format,OF `e>0`] .. note N=this
  1.1084 +	show ?thesis apply(rule_tac x=N in exI) unfolding True 
  1.1085 +	  using N[rule_format,OF _ `x\<in>s`,of _ 0] and `e>0` by auto next
  1.1086 +	case False hence *:"e / 2 / norm u > 0" using `e>0` by(auto intro!: divide_pos_pos)
  1.1087 +	guess N using assms(3)[rule_format,OF *] .. note N=this
  1.1088 +	show ?thesis apply(rule_tac x=N in exI) proof(rule,rule) case goal1
  1.1089 +	  show ?case unfolding vector_dist_norm using N[rule_format,OF goal1 `x\<in>s`, of u] False `e>0`
  1.1090 +	    by (auto simp add:field_simps) qed qed qed
  1.1091 +    show "bounded_linear (g' x)" unfolding linear_linear linear_def apply(rule,rule,rule) defer proof(rule,rule)
  1.1092 +      fix x' y z::"real^'m" and c::real
  1.1093 +      note lin = assms(2)[rule_format,OF `x\<in>s`,THEN derivative_linear]
  1.1094 +      show "g' x (c *s x') = c *s g' x x'" apply(rule Lim_unique[OF trivial_limit_sequentially])
  1.1095 +	apply(rule lem3[rule_format]) unfolding smult_conv_scaleR 
  1.1096 +        unfolding lin[unfolded bounded_linear_def bounded_linear_axioms_def,THEN conjunct2,THEN conjunct1,rule_format]
  1.1097 +	apply(rule Lim_cmul) by(rule lem3[rule_format])
  1.1098 +      show "g' x (y + z) = g' x y + g' x z" apply(rule Lim_unique[OF trivial_limit_sequentially])
  1.1099 +	apply(rule lem3[rule_format]) unfolding lin[unfolded bounded_linear_def additive_def,THEN conjunct1,rule_format]
  1.1100 +        apply(rule Lim_add) by(rule lem3[rule_format])+ qed 
  1.1101 +    show "\<forall>e>0. \<exists>d>0. \<forall>y\<in>s. norm (y - x) < d \<longrightarrow> norm (g y - g x - g' x (y - x)) \<le> e * norm (y - x)" proof(rule,rule) case goal1
  1.1102 +      have *:"e/3>0" using goal1 by auto guess N1 using assms(3)[rule_format,OF *] .. note N1=this
  1.1103 +      guess N2 using lem2[rule_format,OF *] .. note N2=this
  1.1104 +      guess d1 using assms(2)[unfolded has_derivative_within_alt, rule_format,OF `x\<in>s`, of "max N1 N2",THEN conjunct2,rule_format,OF *] .. note d1=this
  1.1105 +      show ?case apply(rule_tac x=d1 in exI) apply(rule,rule d1[THEN conjunct1]) proof(rule,rule)
  1.1106 +	fix y assume as:"y \<in> s" "norm (y - x) < d1" let ?N ="max N1 N2"
  1.1107 +	have "norm (g y - g x - (f ?N y - f ?N x)) \<le> e /3 * norm (y - x)" apply(subst norm_minus_cancel[THEN sym])
  1.1108 +	  using N2[rule_format, OF _ `y\<in>s` `x\<in>s`, of ?N] by auto moreover
  1.1109 +	have "norm (f ?N y - f ?N x - f' ?N x (y - x)) \<le> e / 3 * norm (y - x)" using d1 and as by auto ultimately
  1.1110 +	have "norm (g y - g x - f' ?N x (y - x)) \<le> 2 * e / 3 * norm (y - x)" 
  1.1111 +	  using norm_triangle_le[of "g y - g x - (f ?N y - f ?N x)" "f ?N y - f ?N x - f' ?N x (y - x)" "2 * e / 3 * norm (y - x)"] 
  1.1112 +	  by (auto simp add:group_simps) moreover
  1.1113 +	have " norm (f' ?N x (y - x) - g' x (y - x)) \<le> e / 3 * norm (y - x)" using N1 `x\<in>s` by auto
  1.1114 +	ultimately show "norm (g y - g x - g' x (y - x)) \<le> e * norm (y - x)"
  1.1115 +	  using norm_triangle_le[of "g y - g x - f' (max N1 N2) x (y - x)" "f' (max N1 N2) x (y - x) - g' x (y - x)"] by(auto simp add:group_simps)
  1.1116 +	qed qed qed qed
  1.1117 +
  1.1118 +subsection {* Can choose to line up antiderivatives if we want. *}
  1.1119 +
  1.1120 +lemma has_antiderivative_sequence: fixes f::"nat\<Rightarrow> real^'m::finite \<Rightarrow> real^'n::finite"
  1.1121 +  assumes "convex s" "\<forall>n. \<forall>x\<in>s. ((f n) has_derivative (f' n x)) (at x within s)"
  1.1122 +  "\<forall>e>0. \<exists>N. \<forall>n\<ge>N. \<forall>x\<in>s. \<forall>h. norm(f' n x h - g' x h) \<le> e * norm h"
  1.1123 +  shows "\<exists>g. \<forall>x\<in>s. (g has_derivative g'(x)) (at x within s)" proof(cases "s={}")
  1.1124 +  case False then obtain a where "a\<in>s" by auto have *:"\<And>P Q. \<exists>g. \<forall>x\<in>s. P g x \<and> Q g x \<Longrightarrow> \<exists>g. \<forall>x\<in>s. Q g x" by auto
  1.1125 +  show ?thesis  apply(rule *) apply(rule has_derivative_sequence[OF assms(1) _ assms(3), of "\<lambda>n x. f n x + (f 0 a - f n a)"])
  1.1126 +    apply(rule,rule) apply(rule has_derivative_add_const, rule assms(2)[rule_format], assumption)  
  1.1127 +    apply(rule `a\<in>s`) by(auto intro!: Lim_const) qed auto
  1.1128 +
  1.1129 +lemma has_antiderivative_limit: fixes g'::"real^'m::finite \<Rightarrow> real^'m::finite \<Rightarrow> real^'n::finite"
  1.1130 +  assumes "convex s" "\<forall>e>0. \<exists>f f'. \<forall>x\<in>s. (f has_derivative (f' x)) (at x within s) \<and> (\<forall>h. norm(f' x h - g' x h) \<le> e * norm(h))"
  1.1131 +  shows "\<exists>g. \<forall>x\<in>s. (g has_derivative g'(x)) (at x within s)" proof-
  1.1132 +  have *:"\<forall>n. \<exists>f f'. \<forall>x\<in>s. (f has_derivative (f' x)) (at x within s) \<and> (\<forall>h. norm(f' x h - g' x h) \<le> inverse (real (Suc n)) * norm(h))"
  1.1133 +    apply(rule) using assms(2) apply(erule_tac x="inverse (real (Suc n))" in allE) by auto
  1.1134 +  guess f using *[THEN choice] .. note * = this guess f' using *[THEN choice] .. note f=this
  1.1135 +  show ?thesis apply(rule has_antiderivative_sequence[OF assms(1), of f f']) defer proof(rule,rule)
  1.1136 +    fix e::real assume "0<e" guess  N using reals_Archimedean[OF `e>0`] .. note N=this 
  1.1137 +    show "\<exists>N. \<forall>n\<ge>N. \<forall>x\<in>s. \<forall>h. norm (f' n x h - g' x h) \<le> e * norm h"  apply(rule_tac x=N in exI) proof(default+) case goal1
  1.1138 +      have *:"inverse (real (Suc n)) \<le> e" apply(rule order_trans[OF _ N[THEN less_imp_le]])
  1.1139 +	using goal1(1) by(auto simp add:field_simps) 
  1.1140 +      show ?case using f[rule_format,THEN conjunct2,OF goal1(2), of n, THEN spec[where x=h]] 
  1.1141 +	apply(rule order_trans) using N * apply(cases "h=0") by auto qed qed(insert f,auto) qed
  1.1142 +
  1.1143 +subsection {* Differentiation of a series. *}
  1.1144 +
  1.1145 +definition sums_seq :: "(nat \<Rightarrow> 'a::real_normed_vector) \<Rightarrow> 'a \<Rightarrow> (nat set) \<Rightarrow> bool"
  1.1146 +(infixl "sums'_seq" 12) where "(f sums_seq l) s \<equiv> ((\<lambda>n. setsum f (s \<inter> {0..n})) ---> l) sequentially"
  1.1147 +
  1.1148 +lemma has_derivative_series: fixes f::"nat \<Rightarrow> real^'m::finite \<Rightarrow> real^'n::finite"
  1.1149 +  assumes "convex s" "\<forall>n. \<forall>x\<in>s. ((f n) has_derivative (f' n x)) (at x within s)"
  1.1150 +  "\<forall>e>0. \<exists>N. \<forall>n\<ge>N. \<forall>x\<in>s. \<forall>h. norm(setsum (\<lambda>i. f' i x h) (k \<inter> {0..n}) - g' x h) \<le> e * norm(h)"
  1.1151 +  "x\<in>s" "((\<lambda>n. f n x) sums_seq l) k"
  1.1152 +  shows "\<exists>g. \<forall>x\<in>s. ((\<lambda>n. f n x) sums_seq (g x)) k \<and> (g has_derivative g'(x)) (at x within s)"
  1.1153 +  unfolding sums_seq_def apply(rule has_derivative_sequence[OF assms(1) _ assms(3)]) apply(rule,rule)
  1.1154 +  apply(rule has_derivative_setsum) defer apply(rule,rule assms(2)[rule_format],assumption)
  1.1155 +  using assms(4-5) unfolding sums_seq_def by auto
  1.1156 +
  1.1157 +subsection {* Derivative with composed bilinear function. *}
  1.1158 +
  1.1159 +lemma has_derivative_bilinear_within: fixes h::"real^'m::finite \<Rightarrow> real^'n::finite \<Rightarrow> real^'p::finite" and f::"real^'q::finite \<Rightarrow> real^'m"
  1.1160 +  assumes "(f has_derivative f') (at x within s)" "(g has_derivative g') (at x within s)" "bounded_bilinear h"
  1.1161 +  shows "((\<lambda>x. h (f x) (g x)) has_derivative (\<lambda>d. h (f x) (g' d) + h (f' d) (g x))) (at x within s)" proof-
  1.1162 +  have "(g ---> g x) (at x within s)" apply(rule differentiable_imp_continuous_within[unfolded continuous_within])
  1.1163 +    using assms(2) unfolding differentiable_def by auto moreover
  1.1164 +  interpret f':bounded_linear f' using assms unfolding has_derivative_def by auto
  1.1165 +  interpret g':bounded_linear g' using assms unfolding has_derivative_def by auto
  1.1166 +  interpret h:bounded_bilinear h using assms by auto
  1.1167 +  have "((\<lambda>y. f' (y - x)) ---> 0) (at x within s)" unfolding f'.zero[THEN sym]
  1.1168 +    apply(rule Lim_linear[of "\<lambda>y. y - x" 0 "at x within s" f']) using Lim_sub[OF Lim_within_id Lim_const, of x x s]
  1.1169 +    unfolding id_def using assms(1) unfolding has_derivative_def by auto
  1.1170 +  hence "((\<lambda>y. f x + f' (y - x)) ---> f x) (at x within s)"
  1.1171 +    using Lim_add[OF Lim_const, of "\<lambda>y. f' (y - x)" 0 "at x within s" "f x"] by auto ultimately
  1.1172 +  have *:"((\<lambda>x'. h (f x + f' (x' - x)) ((1/(norm (x' - x))) *\<^sub>R (g x' - (g x + g' (x' - x))))
  1.1173 +             + h ((1/ (norm (x' - x))) *\<^sub>R (f x' - (f x + f' (x' - x)))) (g x')) ---> h (f x) 0 + h 0 (g x)) (at x within s)"
  1.1174 +    apply-apply(rule Lim_add) apply(rule_tac[!] Lim_bilinear[OF _ _ assms(3)]) using assms(1-2)  unfolding has_derivative_within by auto
  1.1175 +  guess B using bounded_bilinear.pos_bounded[OF assms(3)] .. note B=this
  1.1176 +  guess C using f'.pos_bounded .. note C=this
  1.1177 +  guess D using g'.pos_bounded .. note D=this
  1.1178 +  have bcd:"B * C * D > 0" using B C D by (auto intro!: mult_pos_pos)
  1.1179 +  have **:"((\<lambda>y. (1/(norm(y - x))) *\<^sub>R (h (f'(y - x)) (g'(y - x)))) ---> 0) (at x within s)" unfolding Lim_within proof(rule,rule) case goal1
  1.1180 +    hence "e/(B*C*D)>0" using B C D by(auto intro!:divide_pos_pos mult_pos_pos)
  1.1181 +    thus ?case apply(rule_tac x="e/(B*C*D)" in exI,rule) proof(rule,rule,erule conjE)
  1.1182 +      fix y assume as:"y \<in> s" "0 < dist y x" "dist y x < e / (B * C * D)"
  1.1183 +      have "norm (h (f' (y - x)) (g' (y - x))) \<le> norm (f' (y - x)) * norm (g' (y - x)) * B" using B by auto
  1.1184 +      also have "\<dots> \<le> (norm (y - x) * C) * (D * norm (y - x)) * B" apply(rule mult_right_mono)
  1.1185 +	apply(rule pordered_semiring_class.mult_mono) using B C D by (auto simp add: field_simps intro!:mult_nonneg_nonneg)
  1.1186 +      also have "\<dots> = (B * C * D * norm (y - x)) * norm (y - x)" by(auto simp add:field_simps)
  1.1187 +      also have "\<dots> < e * norm (y - x)" apply(rule mult_strict_right_mono)
  1.1188 +	using as(3)[unfolded vector_dist_norm] and as(2) unfolding pos_less_divide_eq[OF bcd] by (auto simp add:field_simps)
  1.1189 +      finally show "dist ((1 / norm (y - x)) *\<^sub>R h (f' (y - x)) (g' (y - x))) 0 < e"
  1.1190 +	unfolding vector_dist_norm apply-apply(cases "y = x") by(auto simp add:field_simps) qed qed
  1.1191 +  have "bounded_linear (\<lambda>d. h (f x) (g' d) + h (f' d) (g x))" unfolding linear_linear linear_def
  1.1192 +    unfolding smult_conv_scaleR unfolding g'.add f'.scaleR f'.add g'.scaleR 
  1.1193 +    unfolding h.add_right h.add_left scaleR_right_distrib h.scaleR_left h.scaleR_right by auto
  1.1194 +  thus ?thesis using Lim_add[OF * **] unfolding has_derivative_within 
  1.1195 +    unfolding smult_conv_scaleR unfolding g'.add f'.scaleR f'.add g'.scaleR f'.diff g'.diff
  1.1196 +     h.add_right h.add_left scaleR_right_distrib h.scaleR_left h.scaleR_right h.diff_right h.diff_left
  1.1197 +    scaleR_right_diff_distrib h.zero_right h.zero_left by(auto simp add:field_simps) qed
  1.1198 +
  1.1199 +lemma has_derivative_bilinear_at: fixes h::"real^'m::finite \<Rightarrow> real^'n::finite \<Rightarrow> real^'p::finite" and f::"real^'p::finite \<Rightarrow> real^'m"
  1.1200 +  assumes "(f has_derivative f') (at x)" "(g has_derivative g') (at x)" "bounded_bilinear h"
  1.1201 +  shows "((\<lambda>x. h (f x) (g x)) has_derivative (\<lambda>d. h (f x) (g' d) + h (f' d) (g x))) (at x)"
  1.1202 +  using has_derivative_bilinear_within[of f f' x UNIV g g' h] unfolding within_UNIV using assms by auto
  1.1203 +
  1.1204 +subsection {* Considering derivative R(^1)->R^n as a vector. *}
  1.1205 +
  1.1206 +definition has_vector_derivative :: "(real \<Rightarrow> 'b::real_normed_vector) \<Rightarrow> ('b) \<Rightarrow> (real net \<Rightarrow> bool)"
  1.1207 +(infixl "has'_vector'_derivative" 12) where
  1.1208 + "(f has_vector_derivative f') net \<equiv> (f has_derivative (\<lambda>x. x *\<^sub>R f')) net"
  1.1209 +
  1.1210 +definition "vector_derivative f net \<equiv> (SOME f'. (f has_vector_derivative f') net)"
  1.1211 +
  1.1212 +lemma vector_derivative_works: fixes f::"real \<Rightarrow> 'a::real_normed_vector"
  1.1213 +  shows "f differentiable net \<longleftrightarrow> (f has_vector_derivative (vector_derivative f net)) net" (is "?l = ?r")
  1.1214 +proof assume ?l guess f' using `?l`[unfolded differentiable_def] .. note f' = this
  1.1215 +  then interpret bounded_linear f' by auto
  1.1216 +  thus ?r unfolding vector_derivative_def has_vector_derivative_def
  1.1217 +    apply-apply(rule someI_ex,rule_tac x="f' 1" in exI)
  1.1218 +    using f' unfolding scaleR[THEN sym] by auto
  1.1219 +next assume ?r thus ?l  unfolding vector_derivative_def has_vector_derivative_def differentiable_def by auto qed
  1.1220 +
  1.1221 +lemma vector_derivative_unique_at: fixes f::"real\<Rightarrow>real^'n::finite"
  1.1222 +  assumes "(f has_vector_derivative f') (at x)" "(f has_vector_derivative f'') (at x)" shows "f' = f''" proof-
  1.1223 +  have *:"(\<lambda>x. x *\<^sub>R f') \<circ> dest_vec1 = (\<lambda>x. x *\<^sub>R f'') \<circ> dest_vec1" apply(rule frechet_derivative_unique_at)
  1.1224 +    using assms[unfolded has_vector_derivative_def] unfolding has_derivative_at_dest_vec1[THEN sym] by auto
  1.1225 +  show ?thesis proof(rule ccontr) assume "f' \<noteq> f''" moreover
  1.1226 +    hence "((\<lambda>x. x *\<^sub>R f') \<circ> dest_vec1) (vec1 1) = ((\<lambda>x. x *\<^sub>R f'') \<circ> dest_vec1) (vec1 1)" using * by auto
  1.1227 +    ultimately show False unfolding o_def vec1_dest_vec1 by auto qed qed
  1.1228 +
  1.1229 +lemma vector_derivative_unique_within_closed_interval: fixes f::"real \<Rightarrow> real^'n::finite"
  1.1230 +  assumes "a < b" "x \<in> {a..b}"
  1.1231 +  "(f has_vector_derivative f') (at x within {a..b})"
  1.1232 +  "(f has_vector_derivative f'') (at x within {a..b})" shows "f' = f''" proof-
  1.1233 +  have *:"(\<lambda>x. x *\<^sub>R f') \<circ> dest_vec1 = (\<lambda>x. x *\<^sub>R f'') \<circ> dest_vec1"
  1.1234 +    apply(rule frechet_derivative_unique_within_closed_interval[of "vec1 a" "vec1 b"])
  1.1235 +    using assms(3-)[unfolded has_vector_derivative_def]
  1.1236 +    unfolding has_derivative_within_dest_vec1[THEN sym] vec1_interval using assms(1-2) by auto
  1.1237 +  show ?thesis proof(rule ccontr) assume "f' \<noteq> f''" moreover
  1.1238 +    hence "((\<lambda>x. x *\<^sub>R f') \<circ> dest_vec1) (vec1 1) = ((\<lambda>x. x *\<^sub>R f'') \<circ> dest_vec1) (vec1 1)" using * by auto
  1.1239 +    ultimately show False unfolding o_def vec1_dest_vec1 by auto qed qed
  1.1240 +
  1.1241 +lemma vector_derivative_at: fixes f::"real \<Rightarrow> real^'a::finite" shows
  1.1242 + "(f has_vector_derivative f') (at x) \<Longrightarrow> vector_derivative f (at x) = f'"
  1.1243 +  apply(rule vector_derivative_unique_at) defer apply assumption
  1.1244 +  unfolding vector_derivative_works[THEN sym] differentiable_def
  1.1245 +  unfolding has_vector_derivative_def by auto
  1.1246 +
  1.1247 +lemma vector_derivative_within_closed_interval: fixes f::"real \<Rightarrow> real^'a::finite"
  1.1248 +  assumes "a < b" "x \<in> {a..b}" "(f has_vector_derivative f') (at x within {a..b})"
  1.1249 +  shows "vector_derivative f (at x within {a..b}) = f'"
  1.1250 +  apply(rule vector_derivative_unique_within_closed_interval)
  1.1251 +  using vector_derivative_works[unfolded differentiable_def]
  1.1252 +  using assms by(auto simp add:has_vector_derivative_def)
  1.1253 +
  1.1254 +lemma has_vector_derivative_within_subset: fixes f::"real \<Rightarrow> real^'a::finite" shows
  1.1255 + "(f has_vector_derivative f') (at x within s) \<Longrightarrow> t \<subseteq> s \<Longrightarrow> (f has_vector_derivative f') (at x within t)"
  1.1256 +  unfolding has_vector_derivative_def apply(rule has_derivative_within_subset) by auto
  1.1257 +
  1.1258 +lemma has_vector_derivative_const: fixes c::"real^'n::finite" shows
  1.1259 + "((\<lambda>x. c) has_vector_derivative 0) net"
  1.1260 +  unfolding has_vector_derivative_def using has_derivative_const by auto
  1.1261 +
  1.1262 +lemma has_vector_derivative_id: "((\<lambda>x::real. x) has_vector_derivative 1) net"
  1.1263 +  unfolding has_vector_derivative_def using has_derivative_id by auto
  1.1264 +
  1.1265 +lemma has_vector_derivative_cmul: fixes f::"real \<Rightarrow> real^'a::finite"
  1.1266 +  shows "(f has_vector_derivative f') net \<Longrightarrow> ((\<lambda>x. c *\<^sub>R f x) has_vector_derivative (c *\<^sub>R f')) net"
  1.1267 +  unfolding has_vector_derivative_def apply(drule has_derivative_cmul) by(auto simp add:group_simps)
  1.1268 +
  1.1269 +lemma has_vector_derivative_cmul_eq: fixes f::"real \<Rightarrow> real^'a::finite" assumes "c \<noteq> 0"
  1.1270 +  shows "(((\<lambda>x. c *\<^sub>R f x) has_vector_derivative (c *\<^sub>R f')) net \<longleftrightarrow> (f has_vector_derivative f') net)"
  1.1271 +  apply rule apply(drule has_vector_derivative_cmul[where c="1/c"]) defer
  1.1272 +  apply(rule has_vector_derivative_cmul) using assms by auto
  1.1273 +
  1.1274 +lemma has_vector_derivative_neg:
  1.1275 + "(f has_vector_derivative f') net \<Longrightarrow> ((\<lambda>x. -(f x)) has_vector_derivative (- f')) net"
  1.1276 +  unfolding has_vector_derivative_def apply(drule has_derivative_neg) by auto
  1.1277 +
  1.1278 +lemma has_vector_derivative_add:
  1.1279 +  assumes "(f has_vector_derivative f') net" "(g has_vector_derivative g') net"
  1.1280 +  shows "((\<lambda>x. f(x) + g(x)) has_vector_derivative (f' + g')) net"
  1.1281 +  using has_derivative_add[OF assms[unfolded has_vector_derivative_def]]
  1.1282 +  unfolding has_vector_derivative_def unfolding scaleR_right_distrib by auto
  1.1283 +
  1.1284 +lemma has_vector_derivative_sub:
  1.1285 +  assumes "(f has_vector_derivative f') net" "(g has_vector_derivative g') net"
  1.1286 +  shows "((\<lambda>x. f(x) - g(x)) has_vector_derivative (f' - g')) net"
  1.1287 +  using has_derivative_sub[OF assms[unfolded has_vector_derivative_def]]
  1.1288 +  unfolding has_vector_derivative_def scaleR_right_diff_distrib by auto
  1.1289 +
  1.1290 +lemma has_vector_derivative_bilinear_within: fixes h::"real^'m::finite \<Rightarrow> real^'n::finite \<Rightarrow> real^'p::finite"
  1.1291 +  assumes "(f has_vector_derivative f') (at x within s)" "(g has_vector_derivative g') (at x within s)" "bounded_bilinear h"
  1.1292 +  shows "((\<lambda>x. h (f x) (g x)) has_vector_derivative (h (f x) g' + h f' (g x))) (at x within s)" proof-
  1.1293 +  interpret bounded_bilinear h using assms by auto 
  1.1294 +  show ?thesis using has_derivative_bilinear_within[OF assms(1-2)[unfolded has_vector_derivative_def has_derivative_within_dest_vec1[THEN sym]], where h=h]
  1.1295 +    unfolding o_def vec1_dest_vec1 has_vector_derivative_def
  1.1296 +    unfolding has_derivative_within_dest_vec1[unfolded o_def, where f="\<lambda>x. h (f x) (g x)" and f'="\<lambda>d. h (f x) (d *\<^sub>R g') + h (d *\<^sub>R f') (g x)"]
  1.1297 +    using assms(3) unfolding scaleR_right scaleR_left scaleR_right_distrib by auto qed
  1.1298 +
  1.1299 +lemma has_vector_derivative_bilinear_at: fixes h::"real^'m::finite \<Rightarrow> real^'n::finite \<Rightarrow> real^'p::finite"
  1.1300 +  assumes "(f has_vector_derivative f') (at x)" "(g has_vector_derivative g') (at x)" "bounded_bilinear h"
  1.1301 +  shows "((\<lambda>x. h (f x) (g x)) has_vector_derivative (h (f x) g' + h f' (g x))) (at x)"
  1.1302 +  apply(rule has_vector_derivative_bilinear_within[where s=UNIV, unfolded within_UNIV]) using assms by auto
  1.1303 +
  1.1304 +lemma has_vector_derivative_at_within: "(f has_vector_derivative f') (at x) \<Longrightarrow> (f has_vector_derivative f') (at x within s)"
  1.1305 +  unfolding has_vector_derivative_def apply(rule has_derivative_at_within) by auto
  1.1306 +
  1.1307 +lemma has_vector_derivative_transform_within:
  1.1308 +  assumes "0 < d" "x \<in> s" "\<forall>x'\<in>s. dist x' x < d \<longrightarrow> f x' = g x'" "(f has_vector_derivative f') (at x within s)"
  1.1309 +  shows "(g has_vector_derivative f') (at x within s)"
  1.1310 +  using assms unfolding has_vector_derivative_def by(rule has_derivative_transform_within)
  1.1311 +
  1.1312 +lemma has_vector_derivative_transform_at:
  1.1313 +  assumes "0 < d" "\<forall>x'. dist x' x < d \<longrightarrow> f x' = g x'" "(f has_vector_derivative f') (at x)"
  1.1314 +  shows "(g has_vector_derivative f') (at x)"
  1.1315 +  using assms unfolding has_vector_derivative_def by(rule has_derivative_transform_at)
  1.1316 +
  1.1317 +lemma has_vector_derivative_transform_within_open:
  1.1318 +  assumes "open s" "x \<in> s" "\<forall>y\<in>s. f y = g y" "(f has_vector_derivative f') (at x)"
  1.1319 +  shows "(g has_vector_derivative f') (at x)"
  1.1320 +  using assms unfolding has_vector_derivative_def by(rule has_derivative_transform_within_open)
  1.1321 +
  1.1322 +lemma vector_diff_chain_at:
  1.1323 +  assumes "(f has_vector_derivative f') (at x)" "(g has_vector_derivative g') (at (f x))"
  1.1324 +  shows "((g \<circ> f) has_vector_derivative (f' *\<^sub>R g')) (at x)"
  1.1325 +  using assms(2) unfolding has_vector_derivative_def apply- apply(drule diff_chain_at[OF assms(1)[unfolded has_vector_derivative_def]])
  1.1326 +  unfolding o_def scaleR.scaleR_left by auto
  1.1327 +
  1.1328 +lemma vector_diff_chain_within:
  1.1329 +  assumes "(f has_vector_derivative f') (at x within s)" "(g has_vector_derivative g') (at (f x) within f ` s)"
  1.1330 +  shows "((g o f) has_vector_derivative (f' *\<^sub>R g')) (at x within s)"
  1.1331 +  using assms(2) unfolding has_vector_derivative_def apply- apply(drule diff_chain_within[OF assms(1)[unfolded has_vector_derivative_def]])
  1.1332 +  unfolding o_def scaleR.scaleR_left by auto
  1.1333 +
  1.1334 +end
  1.1335 +