src/HOL/Analysis/Linear_Algebra.thy
author immler
Thu Dec 27 21:00:50 2018 +0100 (4 months ago)
changeset 69510 0f31dd2e540d
parent 68901 4824cc40f42e
child 69513 42e08052dae8
permissions -rw-r--r--
generalized to big sum
     1 (*  Title:      HOL/Analysis/Linear_Algebra.thy
     2     Author:     Amine Chaieb, University of Cambridge
     3 *)
     4 
     5 section \<open>Elementary linear algebra on Euclidean spaces\<close>
     6 
     7 theory Linear_Algebra
     8 imports
     9   Euclidean_Space
    10   "HOL-Library.Infinite_Set"
    11 begin
    12 
    13 lemma linear_simps:
    14   assumes "bounded_linear f"
    15   shows
    16     "f (a + b) = f a + f b"
    17     "f (a - b) = f a - f b"
    18     "f 0 = 0"
    19     "f (- a) = - f a"
    20     "f (s *\<^sub>R v) = s *\<^sub>R (f v)"
    21 proof -
    22   interpret f: bounded_linear f by fact
    23   show "f (a + b) = f a + f b" by (rule f.add)
    24   show "f (a - b) = f a - f b" by (rule f.diff)
    25   show "f 0 = 0" by (rule f.zero)
    26   show "f (- a) = - f a" by (rule f.neg)
    27   show "f (s *\<^sub>R v) = s *\<^sub>R (f v)" by (rule f.scale)
    28 qed
    29 
    30 lemma finite_Atleast_Atmost_nat[simp]: "finite {f x |x. x \<in> (UNIV::'a::finite set)}"
    31   using finite finite_image_set by blast
    32 
    33 
    34 subsection%unimportant \<open>More interesting properties of the norm\<close>
    35 
    36 notation inner (infix "\<bullet>" 70)
    37 
    38 text\<open>Equality of vectors in terms of @{term "(\<bullet>)"} products.\<close>
    39 
    40 lemma linear_componentwise:
    41   fixes f:: "'a::euclidean_space \<Rightarrow> 'b::real_inner"
    42   assumes lf: "linear f"
    43   shows "(f x) \<bullet> j = (\<Sum>i\<in>Basis. (x\<bullet>i) * (f i\<bullet>j))" (is "?lhs = ?rhs")
    44 proof -
    45   interpret linear f by fact
    46   have "?rhs = (\<Sum>i\<in>Basis. (x\<bullet>i) *\<^sub>R (f i))\<bullet>j"
    47     by (simp add: inner_sum_left)
    48   then show ?thesis
    49     by (simp add: euclidean_representation sum[symmetric] scale[symmetric])
    50 qed
    51 
    52 lemma vector_eq: "x = y \<longleftrightarrow> x \<bullet> x = x \<bullet> y \<and> y \<bullet> y = x \<bullet> x"
    53   (is "?lhs \<longleftrightarrow> ?rhs")
    54 proof
    55   assume ?lhs
    56   then show ?rhs by simp
    57 next
    58   assume ?rhs
    59   then have "x \<bullet> x - x \<bullet> y = 0 \<and> x \<bullet> y - y \<bullet> y = 0"
    60     by simp
    61   then have "x \<bullet> (x - y) = 0 \<and> y \<bullet> (x - y) = 0"
    62     by (simp add: inner_diff inner_commute)
    63   then have "(x - y) \<bullet> (x - y) = 0"
    64     by (simp add: field_simps inner_diff inner_commute)
    65   then show "x = y" by simp
    66 qed
    67 
    68 lemma norm_triangle_half_r:
    69   "norm (y - x1) < e / 2 \<Longrightarrow> norm (y - x2) < e / 2 \<Longrightarrow> norm (x1 - x2) < e"
    70   using dist_triangle_half_r unfolding dist_norm[symmetric] by auto
    71 
    72 lemma norm_triangle_half_l:
    73   assumes "norm (x - y) < e / 2"
    74     and "norm (x' - y) < e / 2"
    75   shows "norm (x - x') < e"
    76   using dist_triangle_half_l[OF assms[unfolded dist_norm[symmetric]]]
    77   unfolding dist_norm[symmetric] .
    78 
    79 lemma norm_triangle_le: "norm x + norm y \<le> e \<Longrightarrow> norm (x + y) \<le> e"
    80   by (rule norm_triangle_ineq [THEN order_trans])
    81 
    82 lemma norm_triangle_lt: "norm x + norm y < e \<Longrightarrow> norm (x + y) < e"
    83   by (rule norm_triangle_ineq [THEN le_less_trans])
    84 
    85 lemma abs_triangle_half_r:
    86   fixes y :: "'a::linordered_field"
    87   shows "abs (y - x1) < e / 2 \<Longrightarrow> abs (y - x2) < e / 2 \<Longrightarrow> abs (x1 - x2) < e"
    88   by linarith
    89 
    90 lemma abs_triangle_half_l:
    91   fixes y :: "'a::linordered_field"
    92   assumes "abs (x - y) < e / 2"
    93     and "abs (x' - y) < e / 2"
    94   shows "abs (x - x') < e"
    95   using assms by linarith
    96 
    97 lemma sum_clauses:
    98   shows "sum f {} = 0"
    99     and "finite S \<Longrightarrow> sum f (insert x S) = (if x \<in> S then sum f S else f x + sum f S)"
   100   by (auto simp add: insert_absorb)
   101 
   102 lemma sum_norm_bound:
   103   fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
   104   assumes K: "\<And>x. x \<in> S \<Longrightarrow> norm (f x) \<le> K"
   105   shows "norm (sum f S) \<le> of_nat (card S)*K"
   106   using sum_norm_le[OF K] sum_constant[symmetric]
   107   by simp
   108 
   109 lemma vector_eq_ldot: "(\<forall>x. x \<bullet> y = x \<bullet> z) \<longleftrightarrow> y = z"
   110 proof
   111   assume "\<forall>x. x \<bullet> y = x \<bullet> z"
   112   then have "\<forall>x. x \<bullet> (y - z) = 0"
   113     by (simp add: inner_diff)
   114   then have "(y - z) \<bullet> (y - z) = 0" ..
   115   then show "y = z" by simp
   116 qed simp
   117 
   118 lemma vector_eq_rdot: "(\<forall>z. x \<bullet> z = y \<bullet> z) \<longleftrightarrow> x = y"
   119 proof
   120   assume "\<forall>z. x \<bullet> z = y \<bullet> z"
   121   then have "\<forall>z. (x - y) \<bullet> z = 0"
   122     by (simp add: inner_diff)
   123   then have "(x - y) \<bullet> (x - y) = 0" ..
   124   then show "x = y" by simp
   125 qed simp
   126 
   127 
   128 subsection \<open>Orthogonality\<close>
   129 
   130 definition%important (in real_inner) "orthogonal x y \<longleftrightarrow> x \<bullet> y = 0"
   131 
   132 context real_inner
   133 begin
   134 
   135 lemma orthogonal_self: "orthogonal x x \<longleftrightarrow> x = 0"
   136   by (simp add: orthogonal_def)
   137 
   138 lemma orthogonal_clauses:
   139   "orthogonal a 0"
   140   "orthogonal a x \<Longrightarrow> orthogonal a (c *\<^sub>R x)"
   141   "orthogonal a x \<Longrightarrow> orthogonal a (- x)"
   142   "orthogonal a x \<Longrightarrow> orthogonal a y \<Longrightarrow> orthogonal a (x + y)"
   143   "orthogonal a x \<Longrightarrow> orthogonal a y \<Longrightarrow> orthogonal a (x - y)"
   144   "orthogonal 0 a"
   145   "orthogonal x a \<Longrightarrow> orthogonal (c *\<^sub>R x) a"
   146   "orthogonal x a \<Longrightarrow> orthogonal (- x) a"
   147   "orthogonal x a \<Longrightarrow> orthogonal y a \<Longrightarrow> orthogonal (x + y) a"
   148   "orthogonal x a \<Longrightarrow> orthogonal y a \<Longrightarrow> orthogonal (x - y) a"
   149   unfolding orthogonal_def inner_add inner_diff by auto
   150 
   151 end
   152 
   153 lemma orthogonal_commute: "orthogonal x y \<longleftrightarrow> orthogonal y x"
   154   by (simp add: orthogonal_def inner_commute)
   155 
   156 lemma orthogonal_scaleR [simp]: "c \<noteq> 0 \<Longrightarrow> orthogonal (c *\<^sub>R x) = orthogonal x"
   157   by (rule ext) (simp add: orthogonal_def)
   158 
   159 lemma pairwise_ortho_scaleR:
   160     "pairwise (\<lambda>i j. orthogonal (f i) (g j)) B
   161     \<Longrightarrow> pairwise (\<lambda>i j. orthogonal (a i *\<^sub>R f i) (a j *\<^sub>R g j)) B"
   162   by (auto simp: pairwise_def orthogonal_clauses)
   163 
   164 lemma orthogonal_rvsum:
   165     "\<lbrakk>finite s; \<And>y. y \<in> s \<Longrightarrow> orthogonal x (f y)\<rbrakk> \<Longrightarrow> orthogonal x (sum f s)"
   166   by (induction s rule: finite_induct) (auto simp: orthogonal_clauses)
   167 
   168 lemma orthogonal_lvsum:
   169     "\<lbrakk>finite s; \<And>x. x \<in> s \<Longrightarrow> orthogonal (f x) y\<rbrakk> \<Longrightarrow> orthogonal (sum f s) y"
   170   by (induction s rule: finite_induct) (auto simp: orthogonal_clauses)
   171 
   172 lemma norm_add_Pythagorean:
   173   assumes "orthogonal a b"
   174     shows "norm(a + b) ^ 2 = norm a ^ 2 + norm b ^ 2"
   175 proof -
   176   from assms have "(a - (0 - b)) \<bullet> (a - (0 - b)) = a \<bullet> a - (0 - b \<bullet> b)"
   177     by (simp add: algebra_simps orthogonal_def inner_commute)
   178   then show ?thesis
   179     by (simp add: power2_norm_eq_inner)
   180 qed
   181 
   182 lemma norm_sum_Pythagorean:
   183   assumes "finite I" "pairwise (\<lambda>i j. orthogonal (f i) (f j)) I"
   184     shows "(norm (sum f I))\<^sup>2 = (\<Sum>i\<in>I. (norm (f i))\<^sup>2)"
   185 using assms
   186 proof (induction I rule: finite_induct)
   187   case empty then show ?case by simp
   188 next
   189   case (insert x I)
   190   then have "orthogonal (f x) (sum f I)"
   191     by (metis pairwise_insert orthogonal_rvsum)
   192   with insert show ?case
   193     by (simp add: pairwise_insert norm_add_Pythagorean)
   194 qed
   195 
   196 
   197 subsection \<open>Bilinear functions\<close>
   198 
   199 definition%important "bilinear f \<longleftrightarrow> (\<forall>x. linear (\<lambda>y. f x y)) \<and> (\<forall>y. linear (\<lambda>x. f x y))"
   200 
   201 lemma bilinear_ladd: "bilinear h \<Longrightarrow> h (x + y) z = h x z + h y z"
   202   by (simp add: bilinear_def linear_iff)
   203 
   204 lemma bilinear_radd: "bilinear h \<Longrightarrow> h x (y + z) = h x y + h x z"
   205   by (simp add: bilinear_def linear_iff)
   206 
   207 lemma bilinear_lmul: "bilinear h \<Longrightarrow> h (c *\<^sub>R x) y = c *\<^sub>R h x y"
   208   by (simp add: bilinear_def linear_iff)
   209 
   210 lemma bilinear_rmul: "bilinear h \<Longrightarrow> h x (c *\<^sub>R y) = c *\<^sub>R h x y"
   211   by (simp add: bilinear_def linear_iff)
   212 
   213 lemma bilinear_lneg: "bilinear h \<Longrightarrow> h (- x) y = - h x y"
   214   by (drule bilinear_lmul [of _ "- 1"]) simp
   215 
   216 lemma bilinear_rneg: "bilinear h \<Longrightarrow> h x (- y) = - h x y"
   217   by (drule bilinear_rmul [of _ _ "- 1"]) simp
   218 
   219 lemma (in ab_group_add) eq_add_iff: "x = x + y \<longleftrightarrow> y = 0"
   220   using add_left_imp_eq[of x y 0] by auto
   221 
   222 lemma bilinear_lzero:
   223   assumes "bilinear h"
   224   shows "h 0 x = 0"
   225   using bilinear_ladd [OF assms, of 0 0 x] by (simp add: eq_add_iff field_simps)
   226 
   227 lemma bilinear_rzero:
   228   assumes "bilinear h"
   229   shows "h x 0 = 0"
   230   using bilinear_radd [OF assms, of x 0 0 ] by (simp add: eq_add_iff field_simps)
   231 
   232 lemma bilinear_lsub: "bilinear h \<Longrightarrow> h (x - y) z = h x z - h y z"
   233   using bilinear_ladd [of h x "- y"] by (simp add: bilinear_lneg)
   234 
   235 lemma bilinear_rsub: "bilinear h \<Longrightarrow> h z (x - y) = h z x - h z y"
   236   using bilinear_radd [of h _ x "- y"] by (simp add: bilinear_rneg)
   237 
   238 lemma bilinear_sum:
   239   assumes "bilinear h"
   240   shows "h (sum f S) (sum g T) = sum (\<lambda>(i,j). h (f i) (g j)) (S \<times> T) "
   241 proof -
   242   interpret l: linear "\<lambda>x. h x y" for y using assms by (simp add: bilinear_def)
   243   interpret r: linear "\<lambda>y. h x y" for x using assms by (simp add: bilinear_def)
   244   have "h (sum f S) (sum g T) = sum (\<lambda>x. h (f x) (sum g T)) S"
   245     by (simp add: l.sum)
   246   also have "\<dots> = sum (\<lambda>x. sum (\<lambda>y. h (f x) (g y)) T) S"
   247     by (rule sum.cong) (simp_all add: r.sum)
   248   finally show ?thesis
   249     unfolding sum.cartesian_product .
   250 qed
   251 
   252 
   253 subsection \<open>Adjoints\<close>
   254 
   255 definition%important "adjoint f = (SOME f'. \<forall>x y. f x \<bullet> y = x \<bullet> f' y)"
   256 
   257 lemma adjoint_unique:
   258   assumes "\<forall>x y. inner (f x) y = inner x (g y)"
   259   shows "adjoint f = g"
   260   unfolding adjoint_def
   261 proof (rule some_equality)
   262   show "\<forall>x y. inner (f x) y = inner x (g y)"
   263     by (rule assms)
   264 next
   265   fix h
   266   assume "\<forall>x y. inner (f x) y = inner x (h y)"
   267   then have "\<forall>x y. inner x (g y) = inner x (h y)"
   268     using assms by simp
   269   then have "\<forall>x y. inner x (g y - h y) = 0"
   270     by (simp add: inner_diff_right)
   271   then have "\<forall>y. inner (g y - h y) (g y - h y) = 0"
   272     by simp
   273   then have "\<forall>y. h y = g y"
   274     by simp
   275   then show "h = g" by (simp add: ext)
   276 qed
   277 
   278 text \<open>TODO: The following lemmas about adjoints should hold for any
   279   Hilbert space (i.e. complete inner product space).
   280   (see \<^url>\<open>https://en.wikipedia.org/wiki/Hermitian_adjoint\<close>)
   281 \<close>
   282 
   283 lemma adjoint_works:
   284   fixes f :: "'n::euclidean_space \<Rightarrow> 'm::euclidean_space"
   285   assumes lf: "linear f"
   286   shows "x \<bullet> adjoint f y = f x \<bullet> y"
   287 proof -
   288   interpret linear f by fact
   289   have "\<forall>y. \<exists>w. \<forall>x. f x \<bullet> y = x \<bullet> w"
   290   proof (intro allI exI)
   291     fix y :: "'m" and x
   292     let ?w = "(\<Sum>i\<in>Basis. (f i \<bullet> y) *\<^sub>R i) :: 'n"
   293     have "f x \<bullet> y = f (\<Sum>i\<in>Basis. (x \<bullet> i) *\<^sub>R i) \<bullet> y"
   294       by (simp add: euclidean_representation)
   295     also have "\<dots> = (\<Sum>i\<in>Basis. (x \<bullet> i) *\<^sub>R f i) \<bullet> y"
   296       by (simp add: sum scale)
   297     finally show "f x \<bullet> y = x \<bullet> ?w"
   298       by (simp add: inner_sum_left inner_sum_right mult.commute)
   299   qed
   300   then show ?thesis
   301     unfolding adjoint_def choice_iff
   302     by (intro someI2_ex[where Q="\<lambda>f'. x \<bullet> f' y = f x \<bullet> y"]) auto
   303 qed
   304 
   305 lemma adjoint_clauses:
   306   fixes f :: "'n::euclidean_space \<Rightarrow> 'm::euclidean_space"
   307   assumes lf: "linear f"
   308   shows "x \<bullet> adjoint f y = f x \<bullet> y"
   309     and "adjoint f y \<bullet> x = y \<bullet> f x"
   310   by (simp_all add: adjoint_works[OF lf] inner_commute)
   311 
   312 lemma adjoint_linear:
   313   fixes f :: "'n::euclidean_space \<Rightarrow> 'm::euclidean_space"
   314   assumes lf: "linear f"
   315   shows "linear (adjoint f)"
   316   by (simp add: lf linear_iff euclidean_eq_iff[where 'a='n] euclidean_eq_iff[where 'a='m]
   317     adjoint_clauses[OF lf] inner_distrib)
   318 
   319 lemma adjoint_adjoint:
   320   fixes f :: "'n::euclidean_space \<Rightarrow> 'm::euclidean_space"
   321   assumes lf: "linear f"
   322   shows "adjoint (adjoint f) = f"
   323   by (rule adjoint_unique, simp add: adjoint_clauses [OF lf])
   324 
   325 
   326 subsection%unimportant \<open>Interlude: Some properties of real sets\<close>
   327 
   328 lemma seq_mono_lemma:
   329   assumes "\<forall>(n::nat) \<ge> m. (d n :: real) < e n"
   330     and "\<forall>n \<ge> m. e n \<le> e m"
   331   shows "\<forall>n \<ge> m. d n < e m"
   332   using assms by force
   333 
   334 lemma infinite_enumerate:
   335   assumes fS: "infinite S"
   336   shows "\<exists>r::nat\<Rightarrow>nat. strict_mono r \<and> (\<forall>n. r n \<in> S)"
   337   unfolding strict_mono_def
   338   using enumerate_in_set[OF fS] enumerate_mono[of _ _ S] fS by auto
   339 
   340 lemma approachable_lt_le: "(\<exists>(d::real) > 0. \<forall>x. f x < d \<longrightarrow> P x) \<longleftrightarrow> (\<exists>d>0. \<forall>x. f x \<le> d \<longrightarrow> P x)"
   341   apply auto
   342   apply (rule_tac x="d/2" in exI)
   343   apply auto
   344   done
   345 
   346 lemma approachable_lt_le2:  \<comment> \<open>like the above, but pushes aside an extra formula\<close>
   347     "(\<exists>(d::real) > 0. \<forall>x. Q x \<longrightarrow> f x < d \<longrightarrow> P x) \<longleftrightarrow> (\<exists>d>0. \<forall>x. f x \<le> d \<longrightarrow> Q x \<longrightarrow> P x)"
   348   apply auto
   349   apply (rule_tac x="d/2" in exI, auto)
   350   done
   351 
   352 lemma triangle_lemma:
   353   fixes x y z :: real
   354   assumes x: "0 \<le> x"
   355     and y: "0 \<le> y"
   356     and z: "0 \<le> z"
   357     and xy: "x\<^sup>2 \<le> y\<^sup>2 + z\<^sup>2"
   358   shows "x \<le> y + z"
   359 proof -
   360   have "y\<^sup>2 + z\<^sup>2 \<le> y\<^sup>2 + 2 * y * z + z\<^sup>2"
   361     using z y by simp
   362   with xy have th: "x\<^sup>2 \<le> (y + z)\<^sup>2"
   363     by (simp add: power2_eq_square field_simps)
   364   from y z have yz: "y + z \<ge> 0"
   365     by arith
   366   from power2_le_imp_le[OF th yz] show ?thesis .
   367 qed
   368 
   369 
   370 
   371 subsection \<open>Archimedean properties and useful consequences\<close>
   372 
   373 text\<open>Bernoulli's inequality\<close>
   374 proposition Bernoulli_inequality:
   375   fixes x :: real
   376   assumes "-1 \<le> x"
   377     shows "1 + n * x \<le> (1 + x) ^ n"
   378 proof (induct n)
   379   case 0
   380   then show ?case by simp
   381 next
   382   case (Suc n)
   383   have "1 + Suc n * x \<le> 1 + (Suc n)*x + n * x^2"
   384     by (simp add: algebra_simps)
   385   also have "... = (1 + x) * (1 + n*x)"
   386     by (auto simp: power2_eq_square algebra_simps  of_nat_Suc)
   387   also have "... \<le> (1 + x) ^ Suc n"
   388     using Suc.hyps assms mult_left_mono by fastforce
   389   finally show ?case .
   390 qed
   391 
   392 corollary Bernoulli_inequality_even:
   393   fixes x :: real
   394   assumes "even n"
   395     shows "1 + n * x \<le> (1 + x) ^ n"
   396 proof (cases "-1 \<le> x \<or> n=0")
   397   case True
   398   then show ?thesis
   399     by (auto simp: Bernoulli_inequality)
   400 next
   401   case False
   402   then have "real n \<ge> 1"
   403     by simp
   404   with False have "n * x \<le> -1"
   405     by (metis linear minus_zero mult.commute mult.left_neutral mult_left_mono_neg neg_le_iff_le order_trans zero_le_one)
   406   then have "1 + n * x \<le> 0"
   407     by auto
   408   also have "... \<le> (1 + x) ^ n"
   409     using assms
   410     using zero_le_even_power by blast
   411   finally show ?thesis .
   412 qed
   413 
   414 corollary real_arch_pow:
   415   fixes x :: real
   416   assumes x: "1 < x"
   417   shows "\<exists>n. y < x^n"
   418 proof -
   419   from x have x0: "x - 1 > 0"
   420     by arith
   421   from reals_Archimedean3[OF x0, rule_format, of y]
   422   obtain n :: nat where n: "y < real n * (x - 1)" by metis
   423   from x0 have x00: "x- 1 \<ge> -1" by arith
   424   from Bernoulli_inequality[OF x00, of n] n
   425   have "y < x^n" by auto
   426   then show ?thesis by metis
   427 qed
   428 
   429 corollary real_arch_pow_inv:
   430   fixes x y :: real
   431   assumes y: "y > 0"
   432     and x1: "x < 1"
   433   shows "\<exists>n. x^n < y"
   434 proof (cases "x > 0")
   435   case True
   436   with x1 have ix: "1 < 1/x" by (simp add: field_simps)
   437   from real_arch_pow[OF ix, of "1/y"]
   438   obtain n where n: "1/y < (1/x)^n" by blast
   439   then show ?thesis using y \<open>x > 0\<close>
   440     by (auto simp add: field_simps)
   441 next
   442   case False
   443   with y x1 show ?thesis
   444     by (metis less_le_trans not_less power_one_right)
   445 qed
   446 
   447 lemma forall_pos_mono:
   448   "(\<And>d e::real. d < e \<Longrightarrow> P d \<Longrightarrow> P e) \<Longrightarrow>
   449     (\<And>n::nat. n \<noteq> 0 \<Longrightarrow> P (inverse (real n))) \<Longrightarrow> (\<And>e. 0 < e \<Longrightarrow> P e)"
   450   by (metis real_arch_inverse)
   451 
   452 lemma forall_pos_mono_1:
   453   "(\<And>d e::real. d < e \<Longrightarrow> P d \<Longrightarrow> P e) \<Longrightarrow>
   454     (\<And>n. P (inverse (real (Suc n)))) \<Longrightarrow> 0 < e \<Longrightarrow> P e"
   455   apply (rule forall_pos_mono)
   456   apply auto
   457   apply (metis Suc_pred of_nat_Suc)
   458   done
   459 
   460 
   461 subsection%unimportant \<open>Euclidean Spaces as Typeclass\<close>
   462 
   463 lemma independent_Basis: "independent Basis"
   464   by (rule independent_Basis)
   465 
   466 lemma span_Basis [simp]: "span Basis = UNIV"
   467   by (rule span_Basis)
   468 
   469 lemma in_span_Basis: "x \<in> span Basis"
   470   unfolding span_Basis ..
   471 
   472 
   473 subsection%unimportant \<open>Linearity and Bilinearity continued\<close>
   474 
   475 lemma linear_bounded:
   476   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::real_normed_vector"
   477   assumes lf: "linear f"
   478   shows "\<exists>B. \<forall>x. norm (f x) \<le> B * norm x"
   479 proof
   480   interpret linear f by fact
   481   let ?B = "\<Sum>b\<in>Basis. norm (f b)"
   482   show "\<forall>x. norm (f x) \<le> ?B * norm x"
   483   proof
   484     fix x :: 'a
   485     let ?g = "\<lambda>b. (x \<bullet> b) *\<^sub>R f b"
   486     have "norm (f x) = norm (f (\<Sum>b\<in>Basis. (x \<bullet> b) *\<^sub>R b))"
   487       unfolding euclidean_representation ..
   488     also have "\<dots> = norm (sum ?g Basis)"
   489       by (simp add: sum scale)
   490     finally have th0: "norm (f x) = norm (sum ?g Basis)" .
   491     have th: "norm (?g i) \<le> norm (f i) * norm x" if "i \<in> Basis" for i
   492     proof -
   493       from Basis_le_norm[OF that, of x]
   494       show "norm (?g i) \<le> norm (f i) * norm x"
   495         unfolding norm_scaleR  by (metis mult.commute mult_left_mono norm_ge_zero)
   496     qed
   497     from sum_norm_le[of _ ?g, OF th]
   498     show "norm (f x) \<le> ?B * norm x"
   499       unfolding th0 sum_distrib_right by metis
   500   qed
   501 qed
   502 
   503 lemma linear_conv_bounded_linear:
   504   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::real_normed_vector"
   505   shows "linear f \<longleftrightarrow> bounded_linear f"
   506 proof
   507   assume "linear f"
   508   then interpret f: linear f .
   509   show "bounded_linear f"
   510   proof
   511     have "\<exists>B. \<forall>x. norm (f x) \<le> B * norm x"
   512       using \<open>linear f\<close> by (rule linear_bounded)
   513     then show "\<exists>K. \<forall>x. norm (f x) \<le> norm x * K"
   514       by (simp add: mult.commute)
   515   qed
   516 next
   517   assume "bounded_linear f"
   518   then interpret f: bounded_linear f .
   519   show "linear f" ..
   520 qed
   521 
   522 lemmas linear_linear = linear_conv_bounded_linear[symmetric]
   523 
   524 lemma linear_bounded_pos:
   525   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::real_normed_vector"
   526   assumes lf: "linear f"
   527  obtains B where "B > 0" "\<And>x. norm (f x) \<le> B * norm x"
   528 proof -
   529   have "\<exists>B > 0. \<forall>x. norm (f x) \<le> norm x * B"
   530     using lf unfolding linear_conv_bounded_linear
   531     by (rule bounded_linear.pos_bounded)
   532   with that show ?thesis
   533     by (auto simp: mult.commute)
   534 qed
   535 
   536 lemma linear_invertible_bounded_below_pos:
   537   fixes f :: "'a::real_normed_vector \<Rightarrow> 'b::euclidean_space"
   538   assumes "linear f" "linear g" "g \<circ> f = id"
   539   obtains B where "B > 0" "\<And>x. B * norm x \<le> norm(f x)"
   540 proof -
   541   obtain B where "B > 0" and B: "\<And>x. norm (g x) \<le> B * norm x"
   542     using linear_bounded_pos [OF \<open>linear g\<close>] by blast
   543   show thesis
   544   proof
   545     show "0 < 1/B"
   546       by (simp add: \<open>B > 0\<close>)
   547     show "1/B * norm x \<le> norm (f x)" for x
   548     proof -
   549       have "1/B * norm x = 1/B * norm (g (f x))"
   550         using assms by (simp add: pointfree_idE)
   551       also have "\<dots> \<le> norm (f x)"
   552         using B [of "f x"] by (simp add: \<open>B > 0\<close> mult.commute pos_divide_le_eq)
   553       finally show ?thesis .
   554     qed
   555   qed
   556 qed
   557 
   558 lemma linear_inj_bounded_below_pos:
   559   fixes f :: "'a::real_normed_vector \<Rightarrow> 'b::euclidean_space"
   560   assumes "linear f" "inj f"
   561   obtains B where "B > 0" "\<And>x. B * norm x \<le> norm(f x)"
   562   using linear_injective_left_inverse [OF assms]
   563     linear_invertible_bounded_below_pos assms by blast
   564 
   565 lemma bounded_linearI':
   566   fixes f ::"'a::euclidean_space \<Rightarrow> 'b::real_normed_vector"
   567   assumes "\<And>x y. f (x + y) = f x + f y"
   568     and "\<And>c x. f (c *\<^sub>R x) = c *\<^sub>R f x"
   569   shows "bounded_linear f"
   570   using assms linearI linear_conv_bounded_linear by blast
   571 
   572 lemma bilinear_bounded:
   573   fixes h :: "'m::euclidean_space \<Rightarrow> 'n::euclidean_space \<Rightarrow> 'k::real_normed_vector"
   574   assumes bh: "bilinear h"
   575   shows "\<exists>B. \<forall>x y. norm (h x y) \<le> B * norm x * norm y"
   576 proof (clarify intro!: exI[of _ "\<Sum>i\<in>Basis. \<Sum>j\<in>Basis. norm (h i j)"])
   577   fix x :: 'm
   578   fix y :: 'n
   579   have "norm (h x y) = norm (h (sum (\<lambda>i. (x \<bullet> i) *\<^sub>R i) Basis) (sum (\<lambda>i. (y \<bullet> i) *\<^sub>R i) Basis))"
   580     by (simp add: euclidean_representation)
   581   also have "\<dots> = norm (sum (\<lambda> (i,j). h ((x \<bullet> i) *\<^sub>R i) ((y \<bullet> j) *\<^sub>R j)) (Basis \<times> Basis))"
   582     unfolding bilinear_sum[OF bh] ..
   583   finally have th: "norm (h x y) = \<dots>" .
   584   have "\<And>i j. \<lbrakk>i \<in> Basis; j \<in> Basis\<rbrakk>
   585            \<Longrightarrow> \<bar>x \<bullet> i\<bar> * (\<bar>y \<bullet> j\<bar> * norm (h i j)) \<le> norm x * (norm y * norm (h i j))"
   586     by (auto simp add: zero_le_mult_iff Basis_le_norm mult_mono)
   587   then show "norm (h x y) \<le> (\<Sum>i\<in>Basis. \<Sum>j\<in>Basis. norm (h i j)) * norm x * norm y"
   588     unfolding sum_distrib_right th sum.cartesian_product
   589     by (clarsimp simp add: bilinear_rmul[OF bh] bilinear_lmul[OF bh]
   590       field_simps simp del: scaleR_scaleR intro!: sum_norm_le)
   591 qed
   592 
   593 lemma bilinear_conv_bounded_bilinear:
   594   fixes h :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space \<Rightarrow> 'c::real_normed_vector"
   595   shows "bilinear h \<longleftrightarrow> bounded_bilinear h"
   596 proof
   597   assume "bilinear h"
   598   show "bounded_bilinear h"
   599   proof
   600     fix x y z
   601     show "h (x + y) z = h x z + h y z"
   602       using \<open>bilinear h\<close> unfolding bilinear_def linear_iff by simp
   603   next
   604     fix x y z
   605     show "h x (y + z) = h x y + h x z"
   606       using \<open>bilinear h\<close> unfolding bilinear_def linear_iff by simp
   607   next
   608     show "h (scaleR r x) y = scaleR r (h x y)" "h x (scaleR r y) = scaleR r (h x y)" for r x y
   609       using \<open>bilinear h\<close> unfolding bilinear_def linear_iff
   610       by simp_all
   611   next
   612     have "\<exists>B. \<forall>x y. norm (h x y) \<le> B * norm x * norm y"
   613       using \<open>bilinear h\<close> by (rule bilinear_bounded)
   614     then show "\<exists>K. \<forall>x y. norm (h x y) \<le> norm x * norm y * K"
   615       by (simp add: ac_simps)
   616   qed
   617 next
   618   assume "bounded_bilinear h"
   619   then interpret h: bounded_bilinear h .
   620   show "bilinear h"
   621     unfolding bilinear_def linear_conv_bounded_linear
   622     using h.bounded_linear_left h.bounded_linear_right by simp
   623 qed
   624 
   625 lemma bilinear_bounded_pos:
   626   fixes h :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space \<Rightarrow> 'c::real_normed_vector"
   627   assumes bh: "bilinear h"
   628   shows "\<exists>B > 0. \<forall>x y. norm (h x y) \<le> B * norm x * norm y"
   629 proof -
   630   have "\<exists>B > 0. \<forall>x y. norm (h x y) \<le> norm x * norm y * B"
   631     using bh [unfolded bilinear_conv_bounded_bilinear]
   632     by (rule bounded_bilinear.pos_bounded)
   633   then show ?thesis
   634     by (simp only: ac_simps)
   635 qed
   636 
   637 lemma bounded_linear_imp_has_derivative: "bounded_linear f \<Longrightarrow> (f has_derivative f) net"
   638   by (auto simp add: has_derivative_def linear_diff linear_linear linear_def
   639       dest: bounded_linear.linear)
   640 
   641 lemma linear_imp_has_derivative:
   642   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::real_normed_vector"
   643   shows "linear f \<Longrightarrow> (f has_derivative f) net"
   644   by (simp add: bounded_linear_imp_has_derivative linear_conv_bounded_linear)
   645 
   646 lemma bounded_linear_imp_differentiable: "bounded_linear f \<Longrightarrow> f differentiable net"
   647   using bounded_linear_imp_has_derivative differentiable_def by blast
   648 
   649 lemma linear_imp_differentiable:
   650   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::real_normed_vector"
   651   shows "linear f \<Longrightarrow> f differentiable net"
   652   by (metis linear_imp_has_derivative differentiable_def)
   653 
   654 
   655 subsection%unimportant \<open>We continue\<close>
   656 
   657 lemma independent_bound:
   658   fixes S :: "'a::euclidean_space set"
   659   shows "independent S \<Longrightarrow> finite S \<and> card S \<le> DIM('a)"
   660   by (metis dim_subset_UNIV finiteI_independent dim_span_eq_card_independent)
   661 
   662 lemmas independent_imp_finite = finiteI_independent
   663 
   664 corollary
   665   fixes S :: "'a::euclidean_space set"
   666   assumes "independent S"
   667   shows independent_card_le:"card S \<le> DIM('a)"
   668   using assms independent_bound by auto
   669 
   670 lemma dependent_biggerset:
   671   fixes S :: "'a::euclidean_space set"
   672   shows "(finite S \<Longrightarrow> card S > DIM('a)) \<Longrightarrow> dependent S"
   673   by (metis independent_bound not_less)
   674 
   675 text \<open>Picking an orthogonal replacement for a spanning set.\<close>
   676 
   677 lemma vector_sub_project_orthogonal:
   678   fixes b x :: "'a::euclidean_space"
   679   shows "b \<bullet> (x - ((b \<bullet> x) / (b \<bullet> b)) *\<^sub>R b) = 0"
   680   unfolding inner_simps by auto
   681 
   682 lemma pairwise_orthogonal_insert:
   683   assumes "pairwise orthogonal S"
   684     and "\<And>y. y \<in> S \<Longrightarrow> orthogonal x y"
   685   shows "pairwise orthogonal (insert x S)"
   686   using assms unfolding pairwise_def
   687   by (auto simp add: orthogonal_commute)
   688 
   689 lemma basis_orthogonal:
   690   fixes B :: "'a::real_inner set"
   691   assumes fB: "finite B"
   692   shows "\<exists>C. finite C \<and> card C \<le> card B \<and> span C = span B \<and> pairwise orthogonal C"
   693   (is " \<exists>C. ?P B C")
   694   using fB
   695 proof (induct rule: finite_induct)
   696   case empty
   697   then show ?case
   698     apply (rule exI[where x="{}"])
   699     apply (auto simp add: pairwise_def)
   700     done
   701 next
   702   case (insert a B)
   703   note fB = \<open>finite B\<close> and aB = \<open>a \<notin> B\<close>
   704   from \<open>\<exists>C. finite C \<and> card C \<le> card B \<and> span C = span B \<and> pairwise orthogonal C\<close>
   705   obtain C where C: "finite C" "card C \<le> card B"
   706     "span C = span B" "pairwise orthogonal C" by blast
   707   let ?a = "a - sum (\<lambda>x. (x \<bullet> a / (x \<bullet> x)) *\<^sub>R x) C"
   708   let ?C = "insert ?a C"
   709   from C(1) have fC: "finite ?C"
   710     by simp
   711   from fB aB C(1,2) have cC: "card ?C \<le> card (insert a B)"
   712     by (simp add: card_insert_if)
   713   {
   714     fix x k
   715     have th0: "\<And>(a::'a) b c. a - (b - c) = c + (a - b)"
   716       by (simp add: field_simps)
   717     have "x - k *\<^sub>R (a - (\<Sum>x\<in>C. (x \<bullet> a / (x \<bullet> x)) *\<^sub>R x)) \<in> span C \<longleftrightarrow> x - k *\<^sub>R a \<in> span C"
   718       apply (simp only: scaleR_right_diff_distrib th0)
   719       apply (rule span_add_eq)
   720       apply (rule span_scale)
   721       apply (rule span_sum)
   722       apply (rule span_scale)
   723       apply (rule span_base)
   724       apply assumption
   725       done
   726   }
   727   then have SC: "span ?C = span (insert a B)"
   728     unfolding set_eq_iff span_breakdown_eq C(3)[symmetric] by auto
   729   {
   730     fix y
   731     assume yC: "y \<in> C"
   732     then have Cy: "C = insert y (C - {y})"
   733       by blast
   734     have fth: "finite (C - {y})"
   735       using C by simp
   736     have "orthogonal ?a y"
   737       unfolding orthogonal_def
   738       unfolding inner_diff inner_sum_left right_minus_eq
   739       unfolding sum.remove [OF \<open>finite C\<close> \<open>y \<in> C\<close>]
   740       apply (clarsimp simp add: inner_commute[of y a])
   741       apply (rule sum.neutral)
   742       apply clarsimp
   743       apply (rule C(4)[unfolded pairwise_def orthogonal_def, rule_format])
   744       using \<open>y \<in> C\<close> by auto
   745   }
   746   with \<open>pairwise orthogonal C\<close> have CPO: "pairwise orthogonal ?C"
   747     by (rule pairwise_orthogonal_insert)
   748   from fC cC SC CPO have "?P (insert a B) ?C"
   749     by blast
   750   then show ?case by blast
   751 qed
   752 
   753 lemma orthogonal_basis_exists:
   754   fixes V :: "('a::euclidean_space) set"
   755   shows "\<exists>B. independent B \<and> B \<subseteq> span V \<and> V \<subseteq> span B \<and>
   756   (card B = dim V) \<and> pairwise orthogonal B"
   757 proof -
   758   from basis_exists[of V] obtain B where
   759     B: "B \<subseteq> V" "independent B" "V \<subseteq> span B" "card B = dim V"
   760     by force
   761   from B have fB: "finite B" "card B = dim V"
   762     using independent_bound by auto
   763   from basis_orthogonal[OF fB(1)] obtain C where
   764     C: "finite C" "card C \<le> card B" "span C = span B" "pairwise orthogonal C"
   765     by blast
   766   from C B have CSV: "C \<subseteq> span V"
   767     by (metis span_superset span_mono subset_trans)
   768   from span_mono[OF B(3)] C have SVC: "span V \<subseteq> span C"
   769     by (simp add: span_span)
   770   from card_le_dim_spanning[OF CSV SVC C(1)] C(2,3) fB
   771   have iC: "independent C"
   772     by (simp add: dim_span)
   773   from C fB have "card C \<le> dim V"
   774     by simp
   775   moreover have "dim V \<le> card C"
   776     using span_card_ge_dim[OF CSV SVC C(1)]
   777     by simp
   778   ultimately have CdV: "card C = dim V"
   779     using C(1) by simp
   780   from C B CSV CdV iC show ?thesis
   781     by auto
   782 qed
   783 
   784 text \<open>Low-dimensional subset is in a hyperplane (weak orthogonal complement).\<close>
   785 
   786 lemma span_not_univ_orthogonal:
   787   fixes S :: "'a::euclidean_space set"
   788   assumes sU: "span S \<noteq> UNIV"
   789   shows "\<exists>a::'a. a \<noteq> 0 \<and> (\<forall>x \<in> span S. a \<bullet> x = 0)"
   790 proof -
   791   from sU obtain a where a: "a \<notin> span S"
   792     by blast
   793   from orthogonal_basis_exists obtain B where
   794     B: "independent B" "B \<subseteq> span S" "S \<subseteq> span B"
   795     "card B = dim S" "pairwise orthogonal B"
   796     by blast
   797   from B have fB: "finite B" "card B = dim S"
   798     using independent_bound by auto
   799   from span_mono[OF B(2)] span_mono[OF B(3)]
   800   have sSB: "span S = span B"
   801     by (simp add: span_span)
   802   let ?a = "a - sum (\<lambda>b. (a \<bullet> b / (b \<bullet> b)) *\<^sub>R b) B"
   803   have "sum (\<lambda>b. (a \<bullet> b / (b \<bullet> b)) *\<^sub>R b) B \<in> span S"
   804     unfolding sSB
   805     apply (rule span_sum)
   806     apply (rule span_scale)
   807     apply (rule span_base)
   808     apply assumption
   809     done
   810   with a have a0:"?a  \<noteq> 0"
   811     by auto
   812   have "?a \<bullet> x = 0" if "x\<in>span B" for x
   813   proof (rule span_induct [OF that])
   814     show "subspace {x. ?a \<bullet> x = 0}"
   815       by (auto simp add: subspace_def inner_add)
   816   next
   817     {
   818       fix x
   819       assume x: "x \<in> B"
   820       from x have B': "B = insert x (B - {x})"
   821         by blast
   822       have fth: "finite (B - {x})"
   823         using fB by simp
   824       have "?a \<bullet> x = 0"
   825         apply (subst B')
   826         using fB fth
   827         unfolding sum_clauses(2)[OF fth]
   828         apply simp unfolding inner_simps
   829         apply (clarsimp simp add: inner_add inner_sum_left)
   830         apply (rule sum.neutral, rule ballI)
   831         apply (simp only: inner_commute)
   832         apply (auto simp add: x field_simps
   833           intro: B(5)[unfolded pairwise_def orthogonal_def, rule_format])
   834         done
   835     }
   836     then show "?a \<bullet> x = 0" if "x \<in> B" for x
   837       using that by blast
   838     qed
   839   with a0 show ?thesis
   840     unfolding sSB by (auto intro: exI[where x="?a"])
   841 qed
   842 
   843 lemma span_not_univ_subset_hyperplane:
   844   fixes S :: "'a::euclidean_space set"
   845   assumes SU: "span S \<noteq> UNIV"
   846   shows "\<exists> a. a \<noteq>0 \<and> span S \<subseteq> {x. a \<bullet> x = 0}"
   847   using span_not_univ_orthogonal[OF SU] by auto
   848 
   849 lemma lowdim_subset_hyperplane:
   850   fixes S :: "'a::euclidean_space set"
   851   assumes d: "dim S < DIM('a)"
   852   shows "\<exists>a::'a. a \<noteq> 0 \<and> span S \<subseteq> {x. a \<bullet> x = 0}"
   853 proof -
   854   {
   855     assume "span S = UNIV"
   856     then have "dim (span S) = dim (UNIV :: ('a) set)"
   857       by simp
   858     then have "dim S = DIM('a)"
   859       by (metis Euclidean_Space.dim_UNIV dim_span)
   860     with d have False by arith
   861   }
   862   then have th: "span S \<noteq> UNIV"
   863     by blast
   864   from span_not_univ_subset_hyperplane[OF th] show ?thesis .
   865 qed
   866 
   867 lemma linear_eq_stdbasis:
   868   fixes f :: "'a::euclidean_space \<Rightarrow> _"
   869   assumes lf: "linear f"
   870     and lg: "linear g"
   871     and fg: "\<And>b. b \<in> Basis \<Longrightarrow> f b = g b"
   872   shows "f = g"
   873   using linear_eq_on_span[OF lf lg, of Basis] fg
   874   by auto
   875 
   876 
   877 text \<open>Similar results for bilinear functions.\<close>
   878 
   879 lemma bilinear_eq:
   880   assumes bf: "bilinear f"
   881     and bg: "bilinear g"
   882     and SB: "S \<subseteq> span B"
   883     and TC: "T \<subseteq> span C"
   884     and "x\<in>S" "y\<in>T"
   885     and fg: "\<And>x y. \<lbrakk>x \<in> B; y\<in> C\<rbrakk> \<Longrightarrow> f x y = g x y"
   886   shows "f x y = g x y"
   887 proof -
   888   let ?P = "{x. \<forall>y\<in> span C. f x y = g x y}"
   889   from bf bg have sp: "subspace ?P"
   890     unfolding bilinear_def linear_iff subspace_def bf bg
   891     by (auto simp add: span_zero bilinear_lzero[OF bf] bilinear_lzero[OF bg]
   892         span_add Ball_def
   893       intro: bilinear_ladd[OF bf])
   894   have sfg: "\<And>x. x \<in> B \<Longrightarrow> subspace {a. f x a = g x a}"
   895     apply (auto simp add: subspace_def)
   896     using bf bg unfolding bilinear_def linear_iff
   897       apply (auto simp add: span_zero bilinear_rzero[OF bf] bilinear_rzero[OF bg]
   898         span_add Ball_def
   899       intro: bilinear_ladd[OF bf])
   900     done
   901   have "\<forall>y\<in> span C. f x y = g x y" if "x \<in> span B" for x
   902     apply (rule span_induct [OF that sp])
   903     using fg sfg span_induct by blast
   904   then show ?thesis
   905     using SB TC assms by auto
   906 qed
   907 
   908 lemma bilinear_eq_stdbasis:
   909   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space \<Rightarrow> _"
   910   assumes bf: "bilinear f"
   911     and bg: "bilinear g"
   912     and fg: "\<And>i j. i \<in> Basis \<Longrightarrow> j \<in> Basis \<Longrightarrow> f i j = g i j"
   913   shows "f = g"
   914   using bilinear_eq[OF bf bg equalityD2[OF span_Basis] equalityD2[OF span_Basis]] fg by blast
   915 
   916 subsection \<open>Infinity norm\<close>
   917 
   918 definition%important "infnorm (x::'a::euclidean_space) = Sup {\<bar>x \<bullet> b\<bar> |b. b \<in> Basis}"
   919 
   920 lemma infnorm_set_image:
   921   fixes x :: "'a::euclidean_space"
   922   shows "{\<bar>x \<bullet> i\<bar> |i. i \<in> Basis} = (\<lambda>i. \<bar>x \<bullet> i\<bar>) ` Basis"
   923   by blast
   924 
   925 lemma infnorm_Max:
   926   fixes x :: "'a::euclidean_space"
   927   shows "infnorm x = Max ((\<lambda>i. \<bar>x \<bullet> i\<bar>) ` Basis)"
   928   by (simp add: infnorm_def infnorm_set_image cSup_eq_Max)
   929 
   930 lemma infnorm_set_lemma:
   931   fixes x :: "'a::euclidean_space"
   932   shows "finite {\<bar>x \<bullet> i\<bar> |i. i \<in> Basis}"
   933     and "{\<bar>x \<bullet> i\<bar> |i. i \<in> Basis} \<noteq> {}"
   934   unfolding infnorm_set_image
   935   by auto
   936 
   937 lemma infnorm_pos_le:
   938   fixes x :: "'a::euclidean_space"
   939   shows "0 \<le> infnorm x"
   940   by (simp add: infnorm_Max Max_ge_iff ex_in_conv)
   941 
   942 lemma infnorm_triangle:
   943   fixes x :: "'a::euclidean_space"
   944   shows "infnorm (x + y) \<le> infnorm x + infnorm y"
   945 proof -
   946   have *: "\<And>a b c d :: real. \<bar>a\<bar> \<le> c \<Longrightarrow> \<bar>b\<bar> \<le> d \<Longrightarrow> \<bar>a + b\<bar> \<le> c + d"
   947     by simp
   948   show ?thesis
   949     by (auto simp: infnorm_Max inner_add_left intro!: *)
   950 qed
   951 
   952 lemma infnorm_eq_0:
   953   fixes x :: "'a::euclidean_space"
   954   shows "infnorm x = 0 \<longleftrightarrow> x = 0"
   955 proof -
   956   have "infnorm x \<le> 0 \<longleftrightarrow> x = 0"
   957     unfolding infnorm_Max by (simp add: euclidean_all_zero_iff)
   958   then show ?thesis
   959     using infnorm_pos_le[of x] by simp
   960 qed
   961 
   962 lemma infnorm_0: "infnorm 0 = 0"
   963   by (simp add: infnorm_eq_0)
   964 
   965 lemma infnorm_neg: "infnorm (- x) = infnorm x"
   966   unfolding infnorm_def by simp
   967 
   968 lemma infnorm_sub: "infnorm (x - y) = infnorm (y - x)"
   969   by (metis infnorm_neg minus_diff_eq)
   970 
   971 lemma absdiff_infnorm: "\<bar>infnorm x - infnorm y\<bar> \<le> infnorm (x - y)"
   972 proof -
   973   have *: "\<And>(nx::real) n ny. nx \<le> n + ny \<Longrightarrow> ny \<le> n + nx \<Longrightarrow> \<bar>nx - ny\<bar> \<le> n"
   974     by arith
   975   show ?thesis
   976   proof (rule *)
   977     from infnorm_triangle[of "x - y" " y"] infnorm_triangle[of "x - y" "-x"]
   978     show "infnorm x \<le> infnorm (x - y) + infnorm y" "infnorm y \<le> infnorm (x - y) + infnorm x"
   979       by (simp_all add: field_simps infnorm_neg)
   980   qed
   981 qed
   982 
   983 lemma real_abs_infnorm: "\<bar>infnorm x\<bar> = infnorm x"
   984   using infnorm_pos_le[of x] by arith
   985 
   986 lemma Basis_le_infnorm:
   987   fixes x :: "'a::euclidean_space"
   988   shows "b \<in> Basis \<Longrightarrow> \<bar>x \<bullet> b\<bar> \<le> infnorm x"
   989   by (simp add: infnorm_Max)
   990 
   991 lemma infnorm_mul: "infnorm (a *\<^sub>R x) = \<bar>a\<bar> * infnorm x"
   992   unfolding infnorm_Max
   993 proof (safe intro!: Max_eqI)
   994   let ?B = "(\<lambda>i. \<bar>x \<bullet> i\<bar>) ` Basis"
   995   { fix b :: 'a
   996     assume "b \<in> Basis"
   997     then show "\<bar>a *\<^sub>R x \<bullet> b\<bar> \<le> \<bar>a\<bar> * Max ?B"
   998       by (simp add: abs_mult mult_left_mono)
   999   next
  1000     from Max_in[of ?B] obtain b where "b \<in> Basis" "Max ?B = \<bar>x \<bullet> b\<bar>"
  1001       by (auto simp del: Max_in)
  1002     then show "\<bar>a\<bar> * Max ((\<lambda>i. \<bar>x \<bullet> i\<bar>) ` Basis) \<in> (\<lambda>i. \<bar>a *\<^sub>R x \<bullet> i\<bar>) ` Basis"
  1003       by (intro image_eqI[where x=b]) (auto simp: abs_mult)
  1004   }
  1005 qed simp
  1006 
  1007 lemma infnorm_mul_lemma: "infnorm (a *\<^sub>R x) \<le> \<bar>a\<bar> * infnorm x"
  1008   unfolding infnorm_mul ..
  1009 
  1010 lemma infnorm_pos_lt: "infnorm x > 0 \<longleftrightarrow> x \<noteq> 0"
  1011   using infnorm_pos_le[of x] infnorm_eq_0[of x] by arith
  1012 
  1013 text \<open>Prove that it differs only up to a bound from Euclidean norm.\<close>
  1014 
  1015 lemma infnorm_le_norm: "infnorm x \<le> norm x"
  1016   by (simp add: Basis_le_norm infnorm_Max)
  1017 
  1018 lemma norm_le_infnorm:
  1019   fixes x :: "'a::euclidean_space"
  1020   shows "norm x \<le> sqrt DIM('a) * infnorm x"
  1021   unfolding norm_eq_sqrt_inner id_def 
  1022 proof (rule real_le_lsqrt[OF inner_ge_zero])
  1023   show "sqrt DIM('a) * infnorm x \<ge> 0"
  1024     by (simp add: zero_le_mult_iff infnorm_pos_le)
  1025   have "x \<bullet> x \<le> (\<Sum>b\<in>Basis. x \<bullet> b * (x \<bullet> b))"
  1026     by (metis euclidean_inner order_refl)
  1027   also have "... \<le> DIM('a) * \<bar>infnorm x\<bar>\<^sup>2"
  1028     by (rule sum_bounded_above) (metis Basis_le_infnorm abs_le_square_iff power2_eq_square real_abs_infnorm)
  1029   also have "... \<le> (sqrt DIM('a) * infnorm x)\<^sup>2"
  1030     by (simp add: power_mult_distrib)
  1031   finally show "x \<bullet> x \<le> (sqrt DIM('a) * infnorm x)\<^sup>2" .
  1032 qed
  1033 
  1034 lemma tendsto_infnorm [tendsto_intros]:
  1035   assumes "(f \<longlongrightarrow> a) F"
  1036   shows "((\<lambda>x. infnorm (f x)) \<longlongrightarrow> infnorm a) F"
  1037 proof (rule tendsto_compose [OF LIM_I assms])
  1038   fix r :: real
  1039   assume "r > 0"
  1040   then show "\<exists>s>0. \<forall>x. x \<noteq> a \<and> norm (x - a) < s \<longrightarrow> norm (infnorm x - infnorm a) < r"
  1041     by (metis real_norm_def le_less_trans absdiff_infnorm infnorm_le_norm)
  1042 qed
  1043 
  1044 text \<open>Equality in Cauchy-Schwarz and triangle inequalities.\<close>
  1045 
  1046 lemma norm_cauchy_schwarz_eq: "x \<bullet> y = norm x * norm y \<longleftrightarrow> norm x *\<^sub>R y = norm y *\<^sub>R x"
  1047   (is "?lhs \<longleftrightarrow> ?rhs")
  1048 proof (cases "x=0")
  1049   case True
  1050   then show ?thesis 
  1051     by auto
  1052 next
  1053   case False
  1054   from inner_eq_zero_iff[of "norm y *\<^sub>R x - norm x *\<^sub>R y"]
  1055   have "?rhs \<longleftrightarrow>
  1056       (norm y * (norm y * norm x * norm x - norm x * (x \<bullet> y)) -
  1057         norm x * (norm y * (y \<bullet> x) - norm x * norm y * norm y) =  0)"
  1058     using False unfolding inner_simps
  1059     by (auto simp add: power2_norm_eq_inner[symmetric] power2_eq_square inner_commute field_simps)
  1060   also have "\<dots> \<longleftrightarrow> (2 * norm x * norm y * (norm x * norm y - x \<bullet> y) = 0)" 
  1061     using False  by (simp add: field_simps inner_commute)
  1062   also have "\<dots> \<longleftrightarrow> ?lhs" 
  1063     using False by auto
  1064   finally show ?thesis by metis
  1065 qed
  1066 
  1067 lemma norm_cauchy_schwarz_abs_eq:
  1068   "\<bar>x \<bullet> y\<bar> = norm x * norm y \<longleftrightarrow>
  1069     norm x *\<^sub>R y = norm y *\<^sub>R x \<or> norm x *\<^sub>R y = - norm y *\<^sub>R x"
  1070   (is "?lhs \<longleftrightarrow> ?rhs")
  1071 proof -
  1072   have th: "\<And>(x::real) a. a \<ge> 0 \<Longrightarrow> \<bar>x\<bar> = a \<longleftrightarrow> x = a \<or> x = - a"
  1073     by arith
  1074   have "?rhs \<longleftrightarrow> norm x *\<^sub>R y = norm y *\<^sub>R x \<or> norm (- x) *\<^sub>R y = norm y *\<^sub>R (- x)"
  1075     by simp
  1076   also have "\<dots> \<longleftrightarrow> (x \<bullet> y = norm x * norm y \<or> (- x) \<bullet> y = norm x * norm y)"
  1077     unfolding norm_cauchy_schwarz_eq[symmetric]
  1078     unfolding norm_minus_cancel norm_scaleR ..
  1079   also have "\<dots> \<longleftrightarrow> ?lhs"
  1080     unfolding th[OF mult_nonneg_nonneg, OF norm_ge_zero[of x] norm_ge_zero[of y]] inner_simps
  1081     by auto
  1082   finally show ?thesis ..
  1083 qed
  1084 
  1085 lemma norm_triangle_eq:
  1086   fixes x y :: "'a::real_inner"
  1087   shows "norm (x + y) = norm x + norm y \<longleftrightarrow> norm x *\<^sub>R y = norm y *\<^sub>R x"
  1088 proof (cases "x = 0 \<or> y = 0")
  1089   case True
  1090   then show ?thesis 
  1091     by force
  1092 next
  1093   case False
  1094   then have n: "norm x > 0" "norm y > 0"
  1095     by auto
  1096   have "norm (x + y) = norm x + norm y \<longleftrightarrow> (norm (x + y))\<^sup>2 = (norm x + norm y)\<^sup>2"
  1097     by simp
  1098   also have "\<dots> \<longleftrightarrow> norm x *\<^sub>R y = norm y *\<^sub>R x"
  1099     unfolding norm_cauchy_schwarz_eq[symmetric]
  1100     unfolding power2_norm_eq_inner inner_simps
  1101     by (simp add: power2_norm_eq_inner[symmetric] power2_eq_square inner_commute field_simps)
  1102   finally show ?thesis .
  1103 qed
  1104 
  1105 
  1106 subsection \<open>Collinearity\<close>
  1107 
  1108 definition%important collinear :: "'a::real_vector set \<Rightarrow> bool"
  1109   where "collinear S \<longleftrightarrow> (\<exists>u. \<forall>x \<in> S. \<forall> y \<in> S. \<exists>c. x - y = c *\<^sub>R u)"
  1110 
  1111 lemma collinear_alt:
  1112      "collinear S \<longleftrightarrow> (\<exists>u v. \<forall>x \<in> S. \<exists>c. x = u + c *\<^sub>R v)" (is "?lhs = ?rhs")
  1113 proof
  1114   assume ?lhs
  1115   then show ?rhs
  1116     unfolding collinear_def by (metis Groups.add_ac(2) diff_add_cancel)
  1117 next
  1118   assume ?rhs
  1119   then obtain u v where *: "\<And>x. x \<in> S \<Longrightarrow> \<exists>c. x = u + c *\<^sub>R v"
  1120     by (auto simp: )
  1121   have "\<exists>c. x - y = c *\<^sub>R v" if "x \<in> S" "y \<in> S" for x y
  1122         by (metis *[OF \<open>x \<in> S\<close>] *[OF \<open>y \<in> S\<close>] scaleR_left.diff add_diff_cancel_left)
  1123   then show ?lhs
  1124     using collinear_def by blast
  1125 qed
  1126 
  1127 lemma collinear:
  1128   fixes S :: "'a::{perfect_space,real_vector} set"
  1129   shows "collinear S \<longleftrightarrow> (\<exists>u. u \<noteq> 0 \<and> (\<forall>x \<in> S. \<forall> y \<in> S. \<exists>c. x - y = c *\<^sub>R u))"
  1130 proof -
  1131   have "\<exists>v. v \<noteq> 0 \<and> (\<forall>x\<in>S. \<forall>y\<in>S. \<exists>c. x - y = c *\<^sub>R v)"
  1132     if "\<forall>x\<in>S. \<forall>y\<in>S. \<exists>c. x - y = c *\<^sub>R u" "u=0" for u
  1133   proof -
  1134     have "\<forall>x\<in>S. \<forall>y\<in>S. x = y"
  1135       using that by auto
  1136     moreover
  1137     obtain v::'a where "v \<noteq> 0"
  1138       using UNIV_not_singleton [of 0] by auto
  1139     ultimately have "\<forall>x\<in>S. \<forall>y\<in>S. \<exists>c. x - y = c *\<^sub>R v"
  1140       by auto
  1141     then show ?thesis
  1142       using \<open>v \<noteq> 0\<close> by blast
  1143   qed
  1144   then show ?thesis
  1145     apply (clarsimp simp: collinear_def)
  1146     by (metis scaleR_zero_right vector_fraction_eq_iff)
  1147 qed
  1148 
  1149 lemma collinear_subset: "\<lbrakk>collinear T; S \<subseteq> T\<rbrakk> \<Longrightarrow> collinear S"
  1150   by (meson collinear_def subsetCE)
  1151 
  1152 lemma collinear_empty [iff]: "collinear {}"
  1153   by (simp add: collinear_def)
  1154 
  1155 lemma collinear_sing [iff]: "collinear {x}"
  1156   by (simp add: collinear_def)
  1157 
  1158 lemma collinear_2 [iff]: "collinear {x, y}"
  1159   apply (simp add: collinear_def)
  1160   apply (rule exI[where x="x - y"])
  1161   by (metis minus_diff_eq scaleR_left.minus scaleR_one)
  1162 
  1163 lemma collinear_lemma: "collinear {0, x, y} \<longleftrightarrow> x = 0 \<or> y = 0 \<or> (\<exists>c. y = c *\<^sub>R x)"
  1164   (is "?lhs \<longleftrightarrow> ?rhs")
  1165 proof (cases "x = 0 \<or> y = 0")
  1166   case True
  1167   then show ?thesis
  1168     by (auto simp: insert_commute)
  1169 next
  1170   case False
  1171   show ?thesis 
  1172   proof
  1173     assume h: "?lhs"
  1174     then obtain u where u: "\<forall> x\<in> {0,x,y}. \<forall>y\<in> {0,x,y}. \<exists>c. x - y = c *\<^sub>R u"
  1175       unfolding collinear_def by blast
  1176     from u[rule_format, of x 0] u[rule_format, of y 0]
  1177     obtain cx and cy where
  1178       cx: "x = cx *\<^sub>R u" and cy: "y = cy *\<^sub>R u"
  1179       by auto
  1180     from cx cy False have cx0: "cx \<noteq> 0" and cy0: "cy \<noteq> 0" by auto
  1181     let ?d = "cy / cx"
  1182     from cx cy cx0 have "y = ?d *\<^sub>R x"
  1183       by simp
  1184     then show ?rhs using False by blast
  1185   next
  1186     assume h: "?rhs"
  1187     then obtain c where c: "y = c *\<^sub>R x"
  1188       using False by blast
  1189     show ?lhs
  1190       unfolding collinear_def c
  1191       apply (rule exI[where x=x])
  1192       apply auto
  1193           apply (rule exI[where x="- 1"], simp)
  1194          apply (rule exI[where x= "-c"], simp)
  1195         apply (rule exI[where x=1], simp)
  1196        apply (rule exI[where x="1 - c"], simp add: scaleR_left_diff_distrib)
  1197       apply (rule exI[where x="c - 1"], simp add: scaleR_left_diff_distrib)
  1198       done
  1199   qed
  1200 qed
  1201 
  1202 lemma norm_cauchy_schwarz_equal: "\<bar>x \<bullet> y\<bar> = norm x * norm y \<longleftrightarrow> collinear {0, x, y}"
  1203 proof (cases "x=0")
  1204   case True
  1205   then show ?thesis
  1206     by (auto simp: insert_commute)
  1207 next
  1208   case False
  1209   then have nnz: "norm x \<noteq> 0"
  1210     by auto
  1211   show ?thesis
  1212   proof
  1213     assume "\<bar>x \<bullet> y\<bar> = norm x * norm y"
  1214     then show "collinear {0, x, y}"
  1215       unfolding norm_cauchy_schwarz_abs_eq collinear_lemma 
  1216       by (meson eq_vector_fraction_iff nnz)
  1217   next
  1218     assume "collinear {0, x, y}"
  1219     with False show "\<bar>x \<bullet> y\<bar> = norm x * norm y"
  1220       unfolding norm_cauchy_schwarz_abs_eq collinear_lemma  by (auto simp: abs_if)
  1221   qed
  1222 qed
  1223 
  1224 end