src/HOL/Multivariate_Analysis/Bounded_Linear_Function.thy
author hoelzl
Fri Jan 08 17:40:59 2016 +0100 (2016-01-08)
changeset 62101 26c0a70f78a3
parent 61975 b4b11391c676
child 62102 877463945ce9
permissions -rw-r--r--
add uniform spaces
     1 (*  Title:      HOL/Multivariate_Analysis/Bounded_Linear_Function.thy
     2     Author:     Fabian Immler, TU M√ľnchen
     3 *)
     4 
     5 section \<open>Bounded Linear Function\<close>
     6 
     7 theory Bounded_Linear_Function
     8 imports
     9   Topology_Euclidean_Space
    10   Operator_Norm
    11 begin
    12 
    13 subsection \<open>Intro rules for @{term bounded_linear}\<close>
    14 
    15 named_theorems bounded_linear_intros
    16 
    17 lemma onorm_inner_left:
    18   assumes "bounded_linear r"
    19   shows "onorm (\<lambda>x. r x \<bullet> f) \<le> onorm r * norm f"
    20 proof (rule onorm_bound)
    21   fix x
    22   have "norm (r x \<bullet> f) \<le> norm (r x) * norm f"
    23     by (simp add: Cauchy_Schwarz_ineq2)
    24   also have "\<dots> \<le> onorm r * norm x * norm f"
    25     by (intro mult_right_mono onorm assms norm_ge_zero)
    26   finally show "norm (r x \<bullet> f) \<le> onorm r * norm f * norm x"
    27     by (simp add: ac_simps)
    28 qed (intro mult_nonneg_nonneg norm_ge_zero onorm_pos_le assms)
    29 
    30 lemma onorm_inner_right:
    31   assumes "bounded_linear r"
    32   shows "onorm (\<lambda>x. f \<bullet> r x) \<le> norm f * onorm r"
    33   apply (subst inner_commute)
    34   apply (rule onorm_inner_left[OF assms, THEN order_trans])
    35   apply simp
    36   done
    37 
    38 lemmas [bounded_linear_intros] =
    39   bounded_linear_zero
    40   bounded_linear_add
    41   bounded_linear_const_mult
    42   bounded_linear_mult_const
    43   bounded_linear_scaleR_const
    44   bounded_linear_const_scaleR
    45   bounded_linear_ident
    46   bounded_linear_setsum
    47   bounded_linear_Pair
    48   bounded_linear_sub
    49   bounded_linear_fst_comp
    50   bounded_linear_snd_comp
    51   bounded_linear_inner_left_comp
    52   bounded_linear_inner_right_comp
    53 
    54 
    55 subsection \<open>declaration of derivative/continuous/tendsto introduction rules for bounded linear functions\<close>
    56 
    57 attribute_setup bounded_linear =
    58   \<open>Scan.succeed (Thm.declaration_attribute (fn thm =>
    59     fold (fn (r, s) => Named_Theorems.add_thm s (thm RS r))
    60       [
    61         (@{thm bounded_linear.has_derivative}, "Deriv.derivative_intros"),
    62         (@{thm bounded_linear.tendsto}, "Topological_Spaces.tendsto_intros"),
    63         (@{thm bounded_linear.continuous}, "Topological_Spaces.continuous_intros"),
    64         (@{thm bounded_linear.continuous_on}, "Topological_Spaces.continuous_intros"),
    65         (@{thm bounded_linear.uniformly_continuous_on}, "Topological_Spaces.continuous_intros"),
    66         (@{thm bounded_linear_compose}, "Bounded_Linear_Function.bounded_linear_intros")
    67       ]))\<close>
    68 
    69 attribute_setup bounded_bilinear =
    70   \<open>Scan.succeed (Thm.declaration_attribute (fn thm =>
    71     fold (fn (r, s) => Named_Theorems.add_thm s (thm RS r))
    72       [
    73         (@{thm bounded_bilinear.FDERIV}, "Deriv.derivative_intros"),
    74         (@{thm bounded_bilinear.tendsto}, "Topological_Spaces.tendsto_intros"),
    75         (@{thm bounded_bilinear.continuous}, "Topological_Spaces.continuous_intros"),
    76         (@{thm bounded_bilinear.continuous_on}, "Topological_Spaces.continuous_intros"),
    77         (@{thm bounded_linear_compose[OF bounded_bilinear.bounded_linear_left]},
    78           "Bounded_Linear_Function.bounded_linear_intros"),
    79         (@{thm bounded_linear_compose[OF bounded_bilinear.bounded_linear_right]},
    80           "Bounded_Linear_Function.bounded_linear_intros"),
    81         (@{thm bounded_linear.uniformly_continuous_on[OF bounded_bilinear.bounded_linear_left]},
    82           "Topological_Spaces.continuous_intros"),
    83         (@{thm bounded_linear.uniformly_continuous_on[OF bounded_bilinear.bounded_linear_right]},
    84           "Topological_Spaces.continuous_intros")
    85       ]))\<close>
    86 
    87 
    88 subsection \<open>type of bounded linear functions\<close>
    89 
    90 typedef (overloaded) ('a, 'b) blinfun ("(_ \<Rightarrow>\<^sub>L /_)" [22, 21] 21) =
    91   "{f::'a::real_normed_vector\<Rightarrow>'b::real_normed_vector. bounded_linear f}"
    92   morphisms blinfun_apply Blinfun
    93   by (blast intro: bounded_linear_intros)
    94 
    95 declare [[coercion
    96     "blinfun_apply :: ('a::real_normed_vector \<Rightarrow>\<^sub>L'b::real_normed_vector) \<Rightarrow> 'a \<Rightarrow> 'b"]]
    97 
    98 lemma bounded_linear_blinfun_apply[bounded_linear_intros]:
    99   "bounded_linear g \<Longrightarrow> bounded_linear (\<lambda>x. blinfun_apply f (g x))"
   100   by (metis blinfun_apply mem_Collect_eq bounded_linear_compose)
   101 
   102 setup_lifting type_definition_blinfun
   103 
   104 lemma blinfun_eqI: "(\<And>i. blinfun_apply x i = blinfun_apply y i) \<Longrightarrow> x = y"
   105   by transfer auto
   106 
   107 lemma bounded_linear_Blinfun_apply: "bounded_linear f \<Longrightarrow> blinfun_apply (Blinfun f) = f"
   108   by (auto simp: Blinfun_inverse)
   109 
   110 
   111 subsection \<open>type class instantiations\<close>
   112 
   113 instantiation blinfun :: (real_normed_vector, real_normed_vector) real_normed_vector
   114 begin
   115 
   116 lift_definition norm_blinfun :: "'a \<Rightarrow>\<^sub>L 'b \<Rightarrow> real" is onorm .
   117 
   118 lift_definition minus_blinfun :: "'a \<Rightarrow>\<^sub>L 'b \<Rightarrow> 'a \<Rightarrow>\<^sub>L 'b \<Rightarrow> 'a \<Rightarrow>\<^sub>L 'b"
   119   is "\<lambda>f g x. f x - g x"
   120   by (rule bounded_linear_sub)
   121 
   122 definition dist_blinfun :: "'a \<Rightarrow>\<^sub>L 'b \<Rightarrow> 'a \<Rightarrow>\<^sub>L 'b \<Rightarrow> real"
   123   where "dist_blinfun a b = norm (a - b)"
   124 
   125 definition [code del]:
   126   "(uniformity :: (('a \<Rightarrow>\<^sub>L 'b) \<times> ('a \<Rightarrow>\<^sub>L 'b)) filter) = (INF e:{0 <..}. principal {(x, y). dist x y < e})"
   127 
   128 definition open_blinfun :: "('a \<Rightarrow>\<^sub>L 'b) set \<Rightarrow> bool"
   129   where [code del]: "open_blinfun S = (\<forall>x\<in>S. \<forall>\<^sub>F (x', y) in uniformity. x' = x \<longrightarrow> y \<in> S)"
   130 
   131 lift_definition uminus_blinfun :: "'a \<Rightarrow>\<^sub>L 'b \<Rightarrow> 'a \<Rightarrow>\<^sub>L 'b" is "\<lambda>f x. - f x"
   132   by (rule bounded_linear_minus)
   133 
   134 lift_definition zero_blinfun :: "'a \<Rightarrow>\<^sub>L 'b" is "\<lambda>x. 0"
   135   by (rule bounded_linear_zero)
   136 
   137 lift_definition plus_blinfun :: "'a \<Rightarrow>\<^sub>L 'b \<Rightarrow> 'a \<Rightarrow>\<^sub>L 'b \<Rightarrow> 'a \<Rightarrow>\<^sub>L 'b"
   138   is "\<lambda>f g x. f x + g x"
   139   by (metis bounded_linear_add)
   140 
   141 lift_definition scaleR_blinfun::"real \<Rightarrow> 'a \<Rightarrow>\<^sub>L 'b \<Rightarrow> 'a \<Rightarrow>\<^sub>L 'b" is "\<lambda>r f x. r *\<^sub>R f x"
   142   by (metis bounded_linear_compose bounded_linear_scaleR_right)
   143 
   144 definition sgn_blinfun :: "'a \<Rightarrow>\<^sub>L 'b \<Rightarrow> 'a \<Rightarrow>\<^sub>L 'b"
   145   where "sgn_blinfun x = scaleR (inverse (norm x)) x"
   146 
   147 instance
   148   apply standard
   149   unfolding dist_blinfun_def open_blinfun_def sgn_blinfun_def uniformity_blinfun_def
   150   apply (rule refl | (transfer, force simp: onorm_triangle onorm_scaleR onorm_eq_0 algebra_simps))+
   151   done
   152 
   153 end
   154 
   155 lemma norm_blinfun_eqI:
   156   assumes "n \<le> norm (blinfun_apply f x) / norm x"
   157   assumes "\<And>x. norm (blinfun_apply f x) \<le> n * norm x"
   158   assumes "0 \<le> n"
   159   shows "norm f = n"
   160   by (auto simp: norm_blinfun_def
   161     intro!: antisym onorm_bound assms order_trans[OF _ le_onorm]
   162     bounded_linear_intros)
   163 
   164 lemma norm_blinfun: "norm (blinfun_apply f x) \<le> norm f * norm x"
   165   by transfer (rule onorm)
   166 
   167 lemma norm_blinfun_bound: "0 \<le> b \<Longrightarrow> (\<And>x. norm (blinfun_apply f x) \<le> b * norm x) \<Longrightarrow> norm f \<le> b"
   168   by transfer (rule onorm_bound)
   169 
   170 lemma bounded_bilinear_blinfun_apply[bounded_bilinear]: "bounded_bilinear blinfun_apply"
   171 proof
   172   fix f g::"'a \<Rightarrow>\<^sub>L 'b" and a b::'a and r::real
   173   show "(f + g) a = f a + g a" "(r *\<^sub>R f) a = r *\<^sub>R f a"
   174     by (transfer, simp)+
   175   interpret bounded_linear f for f::"'a \<Rightarrow>\<^sub>L 'b"
   176     by (auto intro!: bounded_linear_intros)
   177   show "f (a + b) = f a + f b" "f (r *\<^sub>R a) = r *\<^sub>R f a"
   178     by (simp_all add: add scaleR)
   179   show "\<exists>K. \<forall>a b. norm (blinfun_apply a b) \<le> norm a * norm b * K"
   180     by (auto intro!: exI[where x=1] norm_blinfun)
   181 qed
   182 
   183 interpretation blinfun: bounded_bilinear blinfun_apply
   184   by (rule bounded_bilinear_blinfun_apply)
   185 
   186 lemmas bounded_linear_apply_blinfun[intro, simp] = blinfun.bounded_linear_left
   187 
   188 
   189 context bounded_bilinear
   190 begin
   191 
   192 named_theorems bilinear_simps
   193 
   194 lemmas [bilinear_simps] =
   195   add_left
   196   add_right
   197   diff_left
   198   diff_right
   199   minus_left
   200   minus_right
   201   scaleR_left
   202   scaleR_right
   203   zero_left
   204   zero_right
   205   setsum_left
   206   setsum_right
   207 
   208 end
   209 
   210 
   211 instance blinfun :: (banach, banach) banach
   212 proof
   213   fix X::"nat \<Rightarrow> 'a \<Rightarrow>\<^sub>L 'b"
   214   assume "Cauchy X"
   215   {
   216     fix x::'a
   217     {
   218       fix x::'a
   219       assume "norm x \<le> 1"
   220       have "Cauchy (\<lambda>n. X n x)"
   221       proof (rule CauchyI)
   222         fix e::real
   223         assume "0 < e"
   224         from CauchyD[OF \<open>Cauchy X\<close> \<open>0 < e\<close>] obtain M
   225           where M: "\<And>m n. m \<ge> M \<Longrightarrow> n \<ge> M \<Longrightarrow> norm (X m - X n) < e"
   226           by auto
   227         show "\<exists>M. \<forall>m\<ge>M. \<forall>n\<ge>M. norm (X m x - X n x) < e"
   228         proof (safe intro!: exI[where x=M])
   229           fix m n
   230           assume le: "M \<le> m" "M \<le> n"
   231           have "norm (X m x - X n x) = norm ((X m - X n) x)"
   232             by (simp add: blinfun.bilinear_simps)
   233           also have "\<dots> \<le> norm (X m - X n) * norm x"
   234              by (rule norm_blinfun)
   235           also have "\<dots> \<le> norm (X m - X n) * 1"
   236             using \<open>norm x \<le> 1\<close> norm_ge_zero by (rule mult_left_mono)
   237           also have "\<dots> = norm (X m - X n)" by simp
   238           also have "\<dots> < e" using le by fact
   239           finally show "norm (X m x - X n x) < e" .
   240         qed
   241       qed
   242       hence "convergent (\<lambda>n. X n x)"
   243         by (metis Cauchy_convergent_iff)
   244     } note convergent_norm1 = this
   245     def y \<equiv> "x /\<^sub>R norm x"
   246     have y: "norm y \<le> 1" and xy: "x = norm x *\<^sub>R y"
   247       by (simp_all add: y_def inverse_eq_divide)
   248     have "convergent (\<lambda>n. norm x *\<^sub>R X n y)"
   249       by (intro bounded_bilinear.convergent[OF bounded_bilinear_scaleR] convergent_const
   250         convergent_norm1 y)
   251     also have "(\<lambda>n. norm x *\<^sub>R X n y) = (\<lambda>n. X n x)"
   252       by (subst xy) (simp add: blinfun.bilinear_simps)
   253     finally have "convergent (\<lambda>n. X n x)" .
   254   }
   255   then obtain v where v: "\<And>x. (\<lambda>n. X n x) \<longlonglongrightarrow> v x"
   256     unfolding convergent_def
   257     by metis
   258 
   259   have "Cauchy (\<lambda>n. norm (X n))"
   260   proof (rule CauchyI)
   261     fix e::real
   262     assume "e > 0"
   263     from CauchyD[OF \<open>Cauchy X\<close> \<open>0 < e\<close>] obtain M
   264       where M: "\<And>m n. m \<ge> M \<Longrightarrow> n \<ge> M \<Longrightarrow> norm (X m - X n) < e"
   265       by auto
   266     show "\<exists>M. \<forall>m\<ge>M. \<forall>n\<ge>M. norm (norm (X m) - norm (X n)) < e"
   267     proof (safe intro!: exI[where x=M])
   268       fix m n assume mn: "m \<ge> M" "n \<ge> M"
   269       have "norm (norm (X m) - norm (X n)) \<le> norm (X m - X n)"
   270         by (metis norm_triangle_ineq3 real_norm_def)
   271       also have "\<dots> < e" using mn by fact
   272       finally show "norm (norm (X m) - norm (X n)) < e" .
   273     qed
   274   qed
   275   then obtain K where K: "(\<lambda>n. norm (X n)) \<longlonglongrightarrow> K"
   276     unfolding Cauchy_convergent_iff convergent_def
   277     by metis
   278 
   279   have "bounded_linear v"
   280   proof
   281     fix x y and r::real
   282     from tendsto_add[OF v[of x] v [of y]] v[of "x + y", unfolded blinfun.bilinear_simps]
   283       tendsto_scaleR[OF tendsto_const[of r] v[of x]] v[of "r *\<^sub>R x", unfolded blinfun.bilinear_simps]
   284     show "v (x + y) = v x + v y" "v (r *\<^sub>R x) = r *\<^sub>R v x"
   285       by (metis (poly_guards_query) LIMSEQ_unique)+
   286     show "\<exists>K. \<forall>x. norm (v x) \<le> norm x * K"
   287     proof (safe intro!: exI[where x=K])
   288       fix x
   289       have "norm (v x) \<le> K * norm x"
   290         by (rule tendsto_le[OF _ tendsto_mult[OF K tendsto_const] tendsto_norm[OF v]])
   291           (auto simp: norm_blinfun)
   292       thus "norm (v x) \<le> norm x * K"
   293         by (simp add: ac_simps)
   294     qed
   295   qed
   296   hence Bv: "\<And>x. (\<lambda>n. X n x) \<longlonglongrightarrow> Blinfun v x"
   297     by (auto simp: bounded_linear_Blinfun_apply v)
   298 
   299   have "X \<longlonglongrightarrow> Blinfun v"
   300   proof (rule LIMSEQ_I)
   301     fix r::real assume "r > 0"
   302     def r' \<equiv> "r / 2"
   303     have "0 < r'" "r' < r" using \<open>r > 0\<close> by (simp_all add: r'_def)
   304     from CauchyD[OF \<open>Cauchy X\<close> \<open>r' > 0\<close>]
   305     obtain M where M: "\<And>m n. m \<ge> M \<Longrightarrow> n \<ge> M \<Longrightarrow> norm (X m - X n) < r'"
   306       by metis
   307     show "\<exists>no. \<forall>n\<ge>no. norm (X n - Blinfun v) < r"
   308     proof (safe intro!: exI[where x=M])
   309       fix n assume n: "M \<le> n"
   310       have "norm (X n - Blinfun v) \<le> r'"
   311       proof (rule norm_blinfun_bound)
   312         fix x
   313         have "eventually (\<lambda>m. m \<ge> M) sequentially"
   314           by (metis eventually_ge_at_top)
   315         hence ev_le: "eventually (\<lambda>m. norm (X n x - X m x) \<le> r' * norm x) sequentially"
   316         proof eventually_elim
   317           case (elim m)
   318           have "norm (X n x - X m x) = norm ((X n - X m) x)"
   319             by (simp add: blinfun.bilinear_simps)
   320           also have "\<dots> \<le> norm ((X n - X m)) * norm x"
   321             by (rule norm_blinfun)
   322           also have "\<dots> \<le> r' * norm x"
   323             using M[OF n elim] by (simp add: mult_right_mono)
   324           finally show ?case .
   325         qed
   326         have tendsto_v: "(\<lambda>m. norm (X n x - X m x)) \<longlonglongrightarrow> norm (X n x - Blinfun v x)"
   327           by (auto intro!: tendsto_intros Bv)
   328         show "norm ((X n - Blinfun v) x) \<le> r' * norm x"
   329           by (auto intro!: tendsto_ge_const tendsto_v ev_le simp: blinfun.bilinear_simps)
   330       qed (simp add: \<open>0 < r'\<close> less_imp_le)
   331       thus "norm (X n - Blinfun v) < r"
   332         by (metis \<open>r' < r\<close> le_less_trans)
   333     qed
   334   qed
   335   thus "convergent X"
   336     by (rule convergentI)
   337 qed
   338 
   339 subsection \<open>On Euclidean Space\<close>
   340 
   341 lemma Zfun_setsum:
   342   assumes "finite s"
   343   assumes f: "\<And>i. i \<in> s \<Longrightarrow> Zfun (f i) F"
   344   shows "Zfun (\<lambda>x. setsum (\<lambda>i. f i x) s) F"
   345   using assms by induct (auto intro!: Zfun_zero Zfun_add)
   346 
   347 lemma norm_blinfun_euclidean_le:
   348   fixes a::"'a::euclidean_space \<Rightarrow>\<^sub>L 'b::real_normed_vector"
   349   shows "norm a \<le> setsum (\<lambda>x. norm (a x)) Basis"
   350   apply (rule norm_blinfun_bound)
   351    apply (simp add: setsum_nonneg)
   352   apply (subst euclidean_representation[symmetric, where 'a='a])
   353   apply (simp only: blinfun.bilinear_simps setsum_left_distrib)
   354   apply (rule order.trans[OF norm_setsum setsum_mono])
   355   apply (simp add: abs_mult mult_right_mono ac_simps Basis_le_norm)
   356   done
   357 
   358 lemma tendsto_componentwise1:
   359   fixes a::"'a::euclidean_space \<Rightarrow>\<^sub>L 'b::real_normed_vector"
   360     and b::"'c \<Rightarrow> 'a \<Rightarrow>\<^sub>L 'b"
   361   assumes "(\<And>j. j \<in> Basis \<Longrightarrow> ((\<lambda>n. b n j) \<longlongrightarrow> a j) F)"
   362   shows "(b \<longlongrightarrow> a) F"
   363 proof -
   364   have "\<And>j. j \<in> Basis \<Longrightarrow> Zfun (\<lambda>x. norm (b x j - a j)) F"
   365     using assms unfolding tendsto_Zfun_iff Zfun_norm_iff .
   366   hence "Zfun (\<lambda>x. \<Sum>j\<in>Basis. norm (b x j - a j)) F"
   367     by (auto intro!: Zfun_setsum)
   368   thus ?thesis
   369     unfolding tendsto_Zfun_iff
   370     by (rule Zfun_le)
   371       (auto intro!: order_trans[OF norm_blinfun_euclidean_le] simp: blinfun.bilinear_simps)
   372 qed
   373 
   374 lift_definition
   375   blinfun_of_matrix::"('b::euclidean_space \<Rightarrow> 'a::euclidean_space \<Rightarrow> real) \<Rightarrow> 'a \<Rightarrow>\<^sub>L 'b"
   376   is "\<lambda>a x. \<Sum>i\<in>Basis. \<Sum>j\<in>Basis. ((x \<bullet> j) * a i j) *\<^sub>R i"
   377   by (intro bounded_linear_intros)
   378 
   379 lemma blinfun_of_matrix_works:
   380   fixes f::"'a::euclidean_space \<Rightarrow>\<^sub>L 'b::euclidean_space"
   381   shows "blinfun_of_matrix (\<lambda>i j. (f j) \<bullet> i) = f"
   382 proof (transfer, rule,  rule euclidean_eqI)
   383   fix f::"'a \<Rightarrow> 'b" and x::'a and b::'b assume "bounded_linear f" and b: "b \<in> Basis"
   384   then interpret bounded_linear f by simp
   385   have "(\<Sum>j\<in>Basis. \<Sum>i\<in>Basis. (x \<bullet> i * (f i \<bullet> j)) *\<^sub>R j) \<bullet> b
   386     = (\<Sum>j\<in>Basis. if j = b then (\<Sum>i\<in>Basis. (x \<bullet> i * (f i \<bullet> j))) else 0)"
   387     using b
   388     by (auto simp add: algebra_simps inner_setsum_left inner_Basis split: split_if intro!: setsum.cong)
   389   also have "\<dots> = (\<Sum>i\<in>Basis. (x \<bullet> i * (f i \<bullet> b)))"
   390     using b by (simp add: setsum.delta)
   391   also have "\<dots> = f x \<bullet> b"
   392     by (subst linear_componentwise[symmetric]) (unfold_locales, rule)
   393   finally show "(\<Sum>j\<in>Basis. \<Sum>i\<in>Basis. (x \<bullet> i * (f i \<bullet> j)) *\<^sub>R j) \<bullet> b = f x \<bullet> b" .
   394 qed
   395 
   396 lemma blinfun_of_matrix_apply:
   397   "blinfun_of_matrix a x = (\<Sum>i\<in>Basis. \<Sum>j\<in>Basis. ((x \<bullet> j) * a i j) *\<^sub>R i)"
   398   by transfer simp
   399 
   400 lemma blinfun_of_matrix_minus: "blinfun_of_matrix x - blinfun_of_matrix y = blinfun_of_matrix (x - y)"
   401   by transfer (auto simp: algebra_simps setsum_subtractf)
   402 
   403 lemma norm_blinfun_of_matrix:
   404   "norm (blinfun_of_matrix a) \<le> (\<Sum>i\<in>Basis. \<Sum>j\<in>Basis. \<bar>a i j\<bar>)"
   405   apply (rule norm_blinfun_bound)
   406    apply (simp add: setsum_nonneg)
   407   apply (simp only: blinfun_of_matrix_apply setsum_left_distrib)
   408   apply (rule order_trans[OF norm_setsum setsum_mono])
   409   apply (rule order_trans[OF norm_setsum setsum_mono])
   410   apply (simp add: abs_mult mult_right_mono ac_simps Basis_le_norm)
   411   done
   412 
   413 lemma tendsto_blinfun_of_matrix:
   414   assumes "\<And>i j. i \<in> Basis \<Longrightarrow> j \<in> Basis \<Longrightarrow> ((\<lambda>n. b n i j) \<longlongrightarrow> a i j) F"
   415   shows "((\<lambda>n. blinfun_of_matrix (b n)) \<longlongrightarrow> blinfun_of_matrix a) F"
   416 proof -
   417   have "\<And>i j. i \<in> Basis \<Longrightarrow> j \<in> Basis \<Longrightarrow> Zfun (\<lambda>x. norm (b x i j - a i j)) F"
   418     using assms unfolding tendsto_Zfun_iff Zfun_norm_iff .
   419   hence "Zfun (\<lambda>x. (\<Sum>i\<in>Basis. \<Sum>j\<in>Basis. \<bar>b x i j - a i j\<bar>)) F"
   420     by (auto intro!: Zfun_setsum)
   421   thus ?thesis
   422     unfolding tendsto_Zfun_iff blinfun_of_matrix_minus
   423     by (rule Zfun_le) (auto intro!: order_trans[OF norm_blinfun_of_matrix])
   424 qed
   425 
   426 lemma tendsto_componentwise:
   427   fixes a::"'a::euclidean_space \<Rightarrow>\<^sub>L 'b::euclidean_space"
   428     and b::"'c \<Rightarrow> 'a \<Rightarrow>\<^sub>L 'b"
   429   shows "(\<And>i j. i \<in> Basis \<Longrightarrow> j \<in> Basis \<Longrightarrow> ((\<lambda>n. b n j \<bullet> i) \<longlongrightarrow> a j \<bullet> i) F) \<Longrightarrow> (b \<longlongrightarrow> a) F"
   430   apply (subst blinfun_of_matrix_works[of a, symmetric])
   431   apply (subst blinfun_of_matrix_works[of "b x" for x, symmetric, abs_def])
   432   by (rule tendsto_blinfun_of_matrix)
   433 
   434 lemma
   435   continuous_blinfun_componentwiseI:
   436   fixes f:: "'b::t2_space \<Rightarrow> 'a::euclidean_space \<Rightarrow>\<^sub>L 'c::euclidean_space"
   437   assumes "\<And>i j. i \<in> Basis \<Longrightarrow> j \<in> Basis \<Longrightarrow> continuous F (\<lambda>x. (f x) j \<bullet> i)"
   438   shows "continuous F f"
   439   using assms by (auto simp: continuous_def intro!: tendsto_componentwise)
   440 
   441 lemma
   442   continuous_blinfun_componentwiseI1:
   443   fixes f:: "'b::t2_space \<Rightarrow> 'a::euclidean_space \<Rightarrow>\<^sub>L 'c::real_normed_vector"
   444   assumes "\<And>i. i \<in> Basis \<Longrightarrow> continuous F (\<lambda>x. f x i)"
   445   shows "continuous F f"
   446   using assms by (auto simp: continuous_def intro!: tendsto_componentwise1)
   447 
   448 lemma bounded_linear_blinfun_matrix: "bounded_linear (\<lambda>x. (x::_\<Rightarrow>\<^sub>L _) j \<bullet> i)"
   449   by (auto intro!: bounded_linearI' bounded_linear_intros)
   450 
   451 lemma continuous_blinfun_matrix:
   452   fixes f:: "'b::t2_space \<Rightarrow> 'a::real_normed_vector \<Rightarrow>\<^sub>L 'c::real_inner"
   453   assumes "continuous F f"
   454   shows "continuous F (\<lambda>x. (f x) j \<bullet> i)"
   455   by (rule bounded_linear.continuous[OF bounded_linear_blinfun_matrix assms])
   456 
   457 lemma continuous_on_blinfun_matrix:
   458   fixes f::"'a::t2_space \<Rightarrow> 'b::real_normed_vector \<Rightarrow>\<^sub>L 'c::real_inner"
   459   assumes "continuous_on S f"
   460   shows "continuous_on S (\<lambda>x. (f x) j \<bullet> i)"
   461   using assms
   462   by (auto simp: continuous_on_eq_continuous_within continuous_blinfun_matrix)
   463 
   464 lemma mult_if_delta:
   465   "(if P then (1::'a::comm_semiring_1) else 0) * q = (if P then q else 0)"
   466   by auto
   467 
   468 text \<open>TODO: generalize this and @{thm compact_lemma}?!\<close>
   469 lemma compact_blinfun_lemma:
   470   fixes f :: "nat \<Rightarrow> 'a::euclidean_space \<Rightarrow>\<^sub>L 'b::euclidean_space"
   471   assumes "bounded (range f)"
   472   shows "\<forall>d\<subseteq>Basis. \<exists>l::'a \<Rightarrow>\<^sub>L 'b. \<exists> r.
   473     subseq r \<and> (\<forall>e>0. eventually (\<lambda>n. \<forall>i\<in>d. dist (f (r n) i) (l i) < e) sequentially)"
   474 proof safe
   475   fix d :: "'a set"
   476   assume d: "d \<subseteq> Basis"
   477   with finite_Basis have "finite d"
   478     by (blast intro: finite_subset)
   479   from this d show "\<exists>l::'a \<Rightarrow>\<^sub>L 'b. \<exists>r. subseq r \<and>
   480     (\<forall>e>0. eventually (\<lambda>n. \<forall>i\<in>d. dist (f (r n) i) (l i) < e) sequentially)"
   481   proof (induct d)
   482     case empty
   483     then show ?case
   484       unfolding subseq_def by auto
   485   next
   486     case (insert k d)
   487     have k[intro]: "k \<in> Basis"
   488       using insert by auto
   489     have s': "bounded ((\<lambda>x. blinfun_apply x k) ` range f)"
   490       using \<open>bounded (range f)\<close>
   491       by (auto intro!: bounded_linear_image bounded_linear_intros)
   492     obtain l1::"'a \<Rightarrow>\<^sub>L 'b" and r1 where r1: "subseq r1"
   493       and lr1: "\<forall>e > 0. eventually (\<lambda>n. \<forall>i\<in>d. dist (f (r1 n) i) (l1 i) < e) sequentially"
   494       using insert(3) using insert(4) by auto
   495     have f': "\<forall>n. f (r1 n) k \<in> (\<lambda>x. blinfun_apply x k) ` range f"
   496       by simp
   497     have "bounded (range (\<lambda>i. f (r1 i) k))"
   498       by (metis (lifting) bounded_subset f' image_subsetI s')
   499     then obtain l2 r2
   500       where r2: "subseq r2"
   501       and lr2: "((\<lambda>i. f (r1 (r2 i)) k) \<longlongrightarrow> l2) sequentially"
   502       using bounded_imp_convergent_subsequence[of "\<lambda>i. f (r1 i) k"]
   503       by (auto simp: o_def)
   504     def r \<equiv> "r1 \<circ> r2"
   505     have r:"subseq r"
   506       using r1 and r2 unfolding r_def o_def subseq_def by auto
   507     moreover
   508     def l \<equiv> "blinfun_of_matrix (\<lambda>i j. if j = k then l2 \<bullet> i else l1 j \<bullet> i)::'a \<Rightarrow>\<^sub>L 'b"
   509     {
   510       fix e::real
   511       assume "e > 0"
   512       from lr1 \<open>e > 0\<close> have N1: "eventually (\<lambda>n. \<forall>i\<in>d. dist (f (r1 n)  i) (l1  i) < e) sequentially"
   513         by blast
   514       from lr2 \<open>e > 0\<close> have N2:"eventually (\<lambda>n. dist (f (r1 (r2 n))  k) l2 < e) sequentially"
   515         by (rule tendstoD)
   516       from r2 N1 have N1': "eventually (\<lambda>n. \<forall>i\<in>d. dist (f (r1 (r2 n))  i) (l1  i) < e) sequentially"
   517         by (rule eventually_subseq)
   518       have l2: "l k = l2"
   519         using insert.prems
   520         by (auto simp: blinfun_of_matrix.rep_eq inner_Basis l_def mult_if_delta
   521           scaleR_setsum_left[symmetric] setsum.delta' intro!: euclidean_eqI[where 'a='b])
   522       {
   523         fix i assume "i \<in> d"
   524         with insert have "i \<in> Basis" "i \<noteq> k" by auto
   525         hence l1: "l i = (l1 i)"
   526           by (auto simp: blinfun_of_matrix.rep_eq inner_Basis l_def mult_if_delta
   527             scaleR_setsum_left[symmetric] setsum.delta' intro!: euclidean_eqI[where 'a='b])
   528       } note l1 = this
   529       have "eventually (\<lambda>n. \<forall>i\<in>(insert k d). dist (f (r n)  i) (l  i) < e) sequentially"
   530         using N1' N2
   531         by eventually_elim (insert insert.prems, auto simp: r_def o_def l1 l2)
   532     }
   533     ultimately show ?case by auto
   534   qed
   535 qed
   536 
   537 lemma blinfun_euclidean_eqI: "(\<And>i. i \<in> Basis \<Longrightarrow> blinfun_apply x i = blinfun_apply y i) \<Longrightarrow> x = y"
   538   apply (auto intro!: blinfun_eqI)
   539   apply (subst (2) euclidean_representation[symmetric, where 'a='a])
   540   apply (subst (1) euclidean_representation[symmetric, where 'a='a])
   541   apply (simp add: blinfun.bilinear_simps)
   542   done
   543 
   544 text \<open>TODO: generalize (via @{thm compact_cball})?\<close>
   545 instance blinfun :: (euclidean_space, euclidean_space) heine_borel
   546 proof
   547   fix f :: "nat \<Rightarrow> 'a \<Rightarrow>\<^sub>L 'b"
   548   assume f: "bounded (range f)"
   549   then obtain l::"'a \<Rightarrow>\<^sub>L 'b" and r where r: "subseq r"
   550     and l: "\<forall>e>0. eventually (\<lambda>n. \<forall>i\<in>Basis. dist (f (r n) i) (l i) < e) sequentially"
   551     using compact_blinfun_lemma [OF f] by blast
   552   {
   553     fix e::real
   554     let ?d = "real_of_nat DIM('a) * real_of_nat DIM('b)"
   555     assume "e > 0"
   556     hence "e / ?d > 0" by (simp add: DIM_positive)
   557     with l have "eventually (\<lambda>n. \<forall>i\<in>Basis. dist (f (r n) i) (l i) < e / ?d) sequentially"
   558       by simp
   559     moreover
   560     {
   561       fix n
   562       assume n: "\<forall>i\<in>Basis. dist (f (r n) i) (l i) < e / ?d"
   563       have "norm (f (r n) - l) = norm (blinfun_of_matrix (\<lambda>i j. (f (r n) - l) j \<bullet> i))"
   564         unfolding blinfun_of_matrix_works ..
   565       also note norm_blinfun_of_matrix
   566       also have "(\<Sum>i\<in>Basis. \<Sum>j\<in>Basis. \<bar>(f (r n) - l) j \<bullet> i\<bar>) <
   567         (\<Sum>i\<in>(Basis::'b set). e / real_of_nat DIM('b))"
   568       proof (rule setsum_strict_mono)
   569         fix i::'b assume i: "i \<in> Basis"
   570         have "(\<Sum>j::'a\<in>Basis. \<bar>(f (r n) - l) j \<bullet> i\<bar>) < (\<Sum>j::'a\<in>Basis. e / ?d)"
   571         proof (rule setsum_strict_mono)
   572           fix j::'a assume j: "j \<in> Basis"
   573           have "\<bar>(f (r n) - l) j \<bullet> i\<bar> \<le> norm ((f (r n) - l) j)"
   574             by (simp add: Basis_le_norm i)
   575           also have "\<dots> < e / ?d"
   576             using n i j by (auto simp: dist_norm blinfun.bilinear_simps)
   577           finally show "\<bar>(f (r n) - l) j \<bullet> i\<bar> < e / ?d" by simp
   578         qed simp_all
   579         also have "\<dots> \<le> e / real_of_nat DIM('b)"
   580           by simp
   581         finally show "(\<Sum>j\<in>Basis. \<bar>(f (r n) - l) j \<bullet> i\<bar>) < e / real_of_nat DIM('b)"
   582           by simp
   583       qed simp_all
   584       also have "\<dots> \<le> e" by simp
   585       finally have "dist (f (r n)) l < e"
   586         by (auto simp: dist_norm)
   587     }
   588     ultimately have "eventually (\<lambda>n. dist (f (r n)) l < e) sequentially"
   589       using eventually_elim2 by force
   590   }
   591   then have *: "((f \<circ> r) \<longlongrightarrow> l) sequentially"
   592     unfolding o_def tendsto_iff by simp
   593   with r show "\<exists>l r. subseq r \<and> ((f \<circ> r) \<longlongrightarrow> l) sequentially"
   594     by auto
   595 qed
   596 
   597 
   598 subsection \<open>concrete bounded linear functions\<close>
   599 
   600 lemma transfer_bounded_bilinear_bounded_linearI:
   601   assumes "g = (\<lambda>i x. (blinfun_apply (f i) x))"
   602   shows "bounded_bilinear g = bounded_linear f"
   603 proof
   604   assume "bounded_bilinear g"
   605   then interpret bounded_bilinear f by (simp add: assms)
   606   show "bounded_linear f"
   607   proof (unfold_locales, safe intro!: blinfun_eqI)
   608     fix i
   609     show "f (x + y) i = (f x + f y) i" "f (r *\<^sub>R x) i = (r *\<^sub>R f x) i" for r x y
   610       by (auto intro!: blinfun_eqI simp: blinfun.bilinear_simps)
   611     from _ nonneg_bounded show "\<exists>K. \<forall>x. norm (f x) \<le> norm x * K"
   612       by (rule ex_reg) (auto intro!: onorm_bound simp: norm_blinfun.rep_eq ac_simps)
   613   qed
   614 qed (auto simp: assms intro!: blinfun.comp)
   615 
   616 lemma transfer_bounded_bilinear_bounded_linear[transfer_rule]:
   617   "(rel_fun (rel_fun op = (pcr_blinfun op = op =)) op =) bounded_bilinear bounded_linear"
   618   by (auto simp: pcr_blinfun_def cr_blinfun_def rel_fun_def OO_def
   619     intro!: transfer_bounded_bilinear_bounded_linearI)
   620 
   621 context bounded_bilinear
   622 begin
   623 
   624 lift_definition prod_left::"'b \<Rightarrow> 'a \<Rightarrow>\<^sub>L 'c" is "(\<lambda>b a. prod a b)"
   625   by (rule bounded_linear_left)
   626 declare prod_left.rep_eq[simp]
   627 
   628 lemma bounded_linear_prod_left[bounded_linear]: "bounded_linear prod_left"
   629   by transfer (rule flip)
   630 
   631 lift_definition prod_right::"'a \<Rightarrow> 'b \<Rightarrow>\<^sub>L 'c" is "(\<lambda>a b. prod a b)"
   632   by (rule bounded_linear_right)
   633 declare prod_right.rep_eq[simp]
   634 
   635 lemma bounded_linear_prod_right[bounded_linear]: "bounded_linear prod_right"
   636   by transfer (rule bounded_bilinear_axioms)
   637 
   638 end
   639 
   640 lift_definition id_blinfun::"'a::real_normed_vector \<Rightarrow>\<^sub>L 'a" is "\<lambda>x. x"
   641   by (rule bounded_linear_ident)
   642 
   643 lemmas blinfun_apply_id_blinfun[simp] = id_blinfun.rep_eq
   644 
   645 lemma norm_blinfun_id[simp]:
   646   "norm (id_blinfun::'a::{real_normed_vector, perfect_space} \<Rightarrow>\<^sub>L 'a) = 1"
   647   by transfer (auto simp: onorm_id)
   648 
   649 lemma norm_blinfun_id_le:
   650   "norm (id_blinfun::'a::real_normed_vector \<Rightarrow>\<^sub>L 'a) \<le> 1"
   651   by transfer (auto simp: onorm_id_le)
   652 
   653 
   654 lift_definition fst_blinfun::"('a::real_normed_vector \<times> 'b::real_normed_vector) \<Rightarrow>\<^sub>L 'a" is fst
   655   by (rule bounded_linear_fst)
   656 
   657 lemma blinfun_apply_fst_blinfun[simp]: "blinfun_apply fst_blinfun = fst"
   658   by transfer (rule refl)
   659 
   660 
   661 lift_definition snd_blinfun::"('a::real_normed_vector \<times> 'b::real_normed_vector) \<Rightarrow>\<^sub>L 'b" is snd
   662   by (rule bounded_linear_snd)
   663 
   664 lemma blinfun_apply_snd_blinfun[simp]: "blinfun_apply snd_blinfun = snd"
   665   by transfer (rule refl)
   666 
   667 
   668 lift_definition blinfun_compose::
   669   "'a::real_normed_vector \<Rightarrow>\<^sub>L 'b::real_normed_vector \<Rightarrow>
   670     'c::real_normed_vector \<Rightarrow>\<^sub>L 'a \<Rightarrow>
   671     'c \<Rightarrow>\<^sub>L 'b" (infixl "o\<^sub>L" 55) is "op o"
   672   parametric comp_transfer
   673   unfolding o_def
   674   by (rule bounded_linear_compose)
   675 
   676 lemma blinfun_apply_blinfun_compose[simp]: "(a o\<^sub>L b) c = a (b c)"
   677   by (simp add: blinfun_compose.rep_eq)
   678 
   679 lemma norm_blinfun_compose:
   680   "norm (f o\<^sub>L g) \<le> norm f * norm g"
   681   by transfer (rule onorm_compose)
   682 
   683 lemma bounded_bilinear_blinfun_compose[bounded_bilinear]: "bounded_bilinear op o\<^sub>L"
   684   by unfold_locales
   685     (auto intro!: blinfun_eqI exI[where x=1] simp: blinfun.bilinear_simps norm_blinfun_compose)
   686 
   687 
   688 lift_definition blinfun_inner_right::"'a::real_inner \<Rightarrow> 'a \<Rightarrow>\<^sub>L real" is "op \<bullet>"
   689   by (rule bounded_linear_inner_right)
   690 declare blinfun_inner_right.rep_eq[simp]
   691 
   692 lemma bounded_linear_blinfun_inner_right[bounded_linear]: "bounded_linear blinfun_inner_right"
   693   by transfer (rule bounded_bilinear_inner)
   694 
   695 
   696 lift_definition blinfun_inner_left::"'a::real_inner \<Rightarrow> 'a \<Rightarrow>\<^sub>L real" is "\<lambda>x y. y \<bullet> x"
   697   by (rule bounded_linear_inner_left)
   698 declare blinfun_inner_left.rep_eq[simp]
   699 
   700 lemma bounded_linear_blinfun_inner_left[bounded_linear]: "bounded_linear blinfun_inner_left"
   701   by transfer (rule bounded_bilinear.flip[OF bounded_bilinear_inner])
   702 
   703 
   704 lift_definition blinfun_scaleR_right::"real \<Rightarrow> 'a \<Rightarrow>\<^sub>L 'a::real_normed_vector" is "op *\<^sub>R"
   705   by (rule bounded_linear_scaleR_right)
   706 declare blinfun_scaleR_right.rep_eq[simp]
   707 
   708 lemma bounded_linear_blinfun_scaleR_right[bounded_linear]: "bounded_linear blinfun_scaleR_right"
   709   by transfer (rule bounded_bilinear_scaleR)
   710 
   711 
   712 lift_definition blinfun_scaleR_left::"'a::real_normed_vector \<Rightarrow> real \<Rightarrow>\<^sub>L 'a" is "\<lambda>x y. y *\<^sub>R x"
   713   by (rule bounded_linear_scaleR_left)
   714 lemmas [simp] = blinfun_scaleR_left.rep_eq
   715 
   716 lemma bounded_linear_blinfun_scaleR_left[bounded_linear]: "bounded_linear blinfun_scaleR_left"
   717   by transfer (rule bounded_bilinear.flip[OF bounded_bilinear_scaleR])
   718 
   719 
   720 lift_definition blinfun_mult_right::"'a \<Rightarrow> 'a \<Rightarrow>\<^sub>L 'a::real_normed_algebra" is "op *"
   721   by (rule bounded_linear_mult_right)
   722 declare blinfun_mult_right.rep_eq[simp]
   723 
   724 lemma bounded_linear_blinfun_mult_right[bounded_linear]: "bounded_linear blinfun_mult_right"
   725   by transfer (rule bounded_bilinear_mult)
   726 
   727 
   728 lift_definition blinfun_mult_left::"'a::real_normed_algebra \<Rightarrow> 'a \<Rightarrow>\<^sub>L 'a" is "\<lambda>x y. y * x"
   729   by (rule bounded_linear_mult_left)
   730 lemmas [simp] = blinfun_mult_left.rep_eq
   731 
   732 lemma bounded_linear_blinfun_mult_left[bounded_linear]: "bounded_linear blinfun_mult_left"
   733   by transfer (rule bounded_bilinear.flip[OF bounded_bilinear_mult])
   734 
   735 end