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