src/HOL/Multivariate_Analysis/Linear_Algebra.thy
author haftmann
Sun Mar 16 18:09:04 2014 +0100 (2014-03-16)
changeset 56166 9a241bc276cd
parent 55910 0a756571c7a4
child 56196 32b7eafc5a52
permissions -rw-r--r--
normalising simp rules for compound operators
     1 (*  Title:      HOL/Multivariate_Analysis/Linear_Algebra.thy
     2     Author:     Amine Chaieb, University of Cambridge
     3 *)
     4 
     5 header {* Elementary linear algebra on Euclidean spaces *}
     6 
     7 theory Linear_Algebra
     8 imports
     9   Euclidean_Space
    10   "~~/src/HOL/Library/Infinite_Set"
    11 begin
    12 
    13 lemma cond_application_beta: "(if b then f else g) x = (if b then f x else g x)"
    14   by auto
    15 
    16 notation inner (infix "\<bullet>" 70)
    17 
    18 lemma square_bound_lemma:
    19   fixes x :: real
    20   shows "x < (1 + x) * (1 + x)"
    21 proof -
    22   have "(x + 1/2)\<^sup>2 + 3/4 > 0"
    23     using zero_le_power2[of "x+1/2"] by arith
    24   then show ?thesis
    25     by (simp add: field_simps power2_eq_square)
    26 qed
    27 
    28 lemma square_continuous:
    29   fixes e :: real
    30   shows "e > 0 \<Longrightarrow> \<exists>d. 0 < d \<and> (\<forall>y. abs (y - x) < d \<longrightarrow> abs (y * y - x * x) < e)"
    31   using isCont_power[OF isCont_ident, of x, unfolded isCont_def LIM_eq, rule_format, of e 2]
    32   apply (auto simp add: power2_eq_square)
    33   apply (rule_tac x="s" in exI)
    34   apply auto
    35   apply (erule_tac x=y in allE)
    36   apply auto
    37   done
    38 
    39 text{* Hence derive more interesting properties of the norm. *}
    40 
    41 lemma norm_eq_0_dot: "norm x = 0 \<longleftrightarrow> x \<bullet> x = (0::real)"
    42   by simp (* TODO: delete *)
    43 
    44 lemma norm_triangle_sub:
    45   fixes x y :: "'a::real_normed_vector"
    46   shows "norm x \<le> norm y + norm (x - y)"
    47   using norm_triangle_ineq[of "y" "x - y"] by (simp add: field_simps)
    48 
    49 lemma norm_le: "norm x \<le> norm y \<longleftrightarrow> x \<bullet> x \<le> y \<bullet> y"
    50   by (simp add: norm_eq_sqrt_inner)
    51 
    52 lemma norm_lt: "norm x < norm y \<longleftrightarrow> x \<bullet> x < y \<bullet> y"
    53   by (simp add: norm_eq_sqrt_inner)
    54 
    55 lemma norm_eq: "norm x = norm y \<longleftrightarrow> x \<bullet> x = y \<bullet> y"
    56   apply (subst order_eq_iff)
    57   apply (auto simp: norm_le)
    58   done
    59 
    60 lemma norm_eq_1: "norm x = 1 \<longleftrightarrow> x \<bullet> x = 1"
    61   by (simp add: norm_eq_sqrt_inner)
    62 
    63 text{* Squaring equations and inequalities involving norms.  *}
    64 
    65 lemma dot_square_norm: "x \<bullet> x = (norm x)\<^sup>2"
    66   by (simp only: power2_norm_eq_inner) (* TODO: move? *)
    67 
    68 lemma norm_eq_square: "norm x = a \<longleftrightarrow> 0 \<le> a \<and> x \<bullet> x = a\<^sup>2"
    69   by (auto simp add: norm_eq_sqrt_inner)
    70 
    71 lemma real_abs_le_square_iff: "\<bar>x\<bar> \<le> \<bar>y\<bar> \<longleftrightarrow> (x::real)\<^sup>2 \<le> y\<^sup>2"
    72 proof
    73   assume "\<bar>x\<bar> \<le> \<bar>y\<bar>"
    74   then have "\<bar>x\<bar>\<^sup>2 \<le> \<bar>y\<bar>\<^sup>2" by (rule power_mono, simp)
    75   then show "x\<^sup>2 \<le> y\<^sup>2" by simp
    76 next
    77   assume "x\<^sup>2 \<le> y\<^sup>2"
    78   then have "sqrt (x\<^sup>2) \<le> sqrt (y\<^sup>2)" by (rule real_sqrt_le_mono)
    79   then show "\<bar>x\<bar> \<le> \<bar>y\<bar>" by simp
    80 qed
    81 
    82 lemma norm_le_square: "norm x \<le> a \<longleftrightarrow> 0 \<le> a \<and> x \<bullet> x \<le> a\<^sup>2"
    83   apply (simp add: dot_square_norm real_abs_le_square_iff[symmetric])
    84   using norm_ge_zero[of x]
    85   apply arith
    86   done
    87 
    88 lemma norm_ge_square: "norm x \<ge> a \<longleftrightarrow> a \<le> 0 \<or> x \<bullet> x \<ge> a\<^sup>2"
    89   apply (simp add: dot_square_norm real_abs_le_square_iff[symmetric])
    90   using norm_ge_zero[of x]
    91   apply arith
    92   done
    93 
    94 lemma norm_lt_square: "norm x < a \<longleftrightarrow> 0 < a \<and> x \<bullet> x < a\<^sup>2"
    95   by (metis not_le norm_ge_square)
    96 
    97 lemma norm_gt_square: "norm x > a \<longleftrightarrow> a < 0 \<or> x \<bullet> x > a\<^sup>2"
    98   by (metis norm_le_square not_less)
    99 
   100 text{* Dot product in terms of the norm rather than conversely. *}
   101 
   102 lemmas inner_simps = inner_add_left inner_add_right inner_diff_right inner_diff_left
   103   inner_scaleR_left inner_scaleR_right
   104 
   105 lemma dot_norm: "x \<bullet> y = ((norm (x + y))\<^sup>2 - (norm x)\<^sup>2 - (norm y)\<^sup>2) / 2"
   106   unfolding power2_norm_eq_inner inner_simps inner_commute by auto
   107 
   108 lemma dot_norm_neg: "x \<bullet> y = (((norm x)\<^sup>2 + (norm y)\<^sup>2) - (norm (x - y))\<^sup>2) / 2"
   109   unfolding power2_norm_eq_inner inner_simps inner_commute
   110   by (auto simp add: algebra_simps)
   111 
   112 text{* Equality of vectors in terms of @{term "op \<bullet>"} products.    *}
   113 
   114 lemma vector_eq: "x = y \<longleftrightarrow> x \<bullet> x = x \<bullet> y \<and> y \<bullet> y = x \<bullet> x"
   115   (is "?lhs \<longleftrightarrow> ?rhs")
   116 proof
   117   assume ?lhs
   118   then show ?rhs by simp
   119 next
   120   assume ?rhs
   121   then have "x \<bullet> x - x \<bullet> y = 0 \<and> x \<bullet> y - y \<bullet> y = 0"
   122     by simp
   123   then have "x \<bullet> (x - y) = 0 \<and> y \<bullet> (x - y) = 0"
   124     by (simp add: inner_diff inner_commute)
   125   then have "(x - y) \<bullet> (x - y) = 0"
   126     by (simp add: field_simps inner_diff inner_commute)
   127   then show "x = y" by simp
   128 qed
   129 
   130 lemma norm_triangle_half_r:
   131   "norm (y - x1) < e / 2 \<Longrightarrow> norm (y - x2) < e / 2 \<Longrightarrow> norm (x1 - x2) < e"
   132   using dist_triangle_half_r unfolding dist_norm[symmetric] by auto
   133 
   134 lemma norm_triangle_half_l:
   135   assumes "norm (x - y) < e / 2"
   136     and "norm (x' - y) < e / 2"
   137   shows "norm (x - x') < e"
   138   using dist_triangle_half_l[OF assms[unfolded dist_norm[symmetric]]]
   139   unfolding dist_norm[symmetric] .
   140 
   141 lemma norm_triangle_le: "norm x + norm y \<le> e \<Longrightarrow> norm (x + y) \<le> e"
   142   by (rule norm_triangle_ineq [THEN order_trans])
   143 
   144 lemma norm_triangle_lt: "norm x + norm y < e \<Longrightarrow> norm (x + y) < e"
   145   by (rule norm_triangle_ineq [THEN le_less_trans])
   146 
   147 lemma setsum_clauses:
   148   shows "setsum f {} = 0"
   149     and "finite S \<Longrightarrow> setsum f (insert x S) = (if x \<in> S then setsum f S else f x + setsum f S)"
   150   by (auto simp add: insert_absorb)
   151 
   152 lemma setsum_norm_le:
   153   fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
   154   assumes fg: "\<forall>x \<in> S. norm (f x) \<le> g x"
   155   shows "norm (setsum f S) \<le> setsum g S"
   156   by (rule order_trans [OF norm_setsum setsum_mono]) (simp add: fg)
   157 
   158 lemma setsum_norm_bound:
   159   fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
   160   assumes fS: "finite S"
   161     and K: "\<forall>x \<in> S. norm (f x) \<le> K"
   162   shows "norm (setsum f S) \<le> of_nat (card S) * K"
   163   using setsum_norm_le[OF K] setsum_constant[symmetric]
   164   by simp
   165 
   166 lemma setsum_group:
   167   assumes fS: "finite S" and fT: "finite T" and fST: "f ` S \<subseteq> T"
   168   shows "setsum (\<lambda>y. setsum g {x. x \<in> S \<and> f x = y}) T = setsum g S"
   169   apply (subst setsum_image_gen[OF fS, of g f])
   170   apply (rule setsum_mono_zero_right[OF fT fST])
   171   apply (auto intro: setsum_0')
   172   done
   173 
   174 lemma vector_eq_ldot: "(\<forall>x. x \<bullet> y = x \<bullet> z) \<longleftrightarrow> y = z"
   175 proof
   176   assume "\<forall>x. x \<bullet> y = x \<bullet> z"
   177   then have "\<forall>x. x \<bullet> (y - z) = 0"
   178     by (simp add: inner_diff)
   179   then have "(y - z) \<bullet> (y - z) = 0" ..
   180   then show "y = z" by simp
   181 qed simp
   182 
   183 lemma vector_eq_rdot: "(\<forall>z. x \<bullet> z = y \<bullet> z) \<longleftrightarrow> x = y"
   184 proof
   185   assume "\<forall>z. x \<bullet> z = y \<bullet> z"
   186   then have "\<forall>z. (x - y) \<bullet> z = 0"
   187     by (simp add: inner_diff)
   188   then have "(x - y) \<bullet> (x - y) = 0" ..
   189   then show "x = y" by simp
   190 qed simp
   191 
   192 
   193 subsection {* Orthogonality. *}
   194 
   195 context real_inner
   196 begin
   197 
   198 definition "orthogonal x y \<longleftrightarrow> x \<bullet> y = 0"
   199 
   200 lemma orthogonal_clauses:
   201   "orthogonal a 0"
   202   "orthogonal a x \<Longrightarrow> orthogonal a (c *\<^sub>R x)"
   203   "orthogonal a x \<Longrightarrow> orthogonal a (- x)"
   204   "orthogonal a x \<Longrightarrow> orthogonal a y \<Longrightarrow> orthogonal a (x + y)"
   205   "orthogonal a x \<Longrightarrow> orthogonal a y \<Longrightarrow> orthogonal a (x - y)"
   206   "orthogonal 0 a"
   207   "orthogonal x a \<Longrightarrow> orthogonal (c *\<^sub>R x) a"
   208   "orthogonal x a \<Longrightarrow> orthogonal (- x) a"
   209   "orthogonal x a \<Longrightarrow> orthogonal y a \<Longrightarrow> orthogonal (x + y) a"
   210   "orthogonal x a \<Longrightarrow> orthogonal y a \<Longrightarrow> orthogonal (x - y) a"
   211   unfolding orthogonal_def inner_add inner_diff by auto
   212 
   213 end
   214 
   215 lemma orthogonal_commute: "orthogonal x y \<longleftrightarrow> orthogonal y x"
   216   by (simp add: orthogonal_def inner_commute)
   217 
   218 
   219 subsection {* Linear functions. *}
   220 
   221 lemma linear_iff:
   222   "linear f \<longleftrightarrow> (\<forall>x y. f (x + y) = f x + f y) \<and> (\<forall>c x. f (c *\<^sub>R x) = c *\<^sub>R f x)"
   223   (is "linear f \<longleftrightarrow> ?rhs")
   224 proof
   225   assume "linear f" then interpret f: linear f .
   226   show "?rhs" by (simp add: f.add f.scaleR)
   227 next
   228   assume "?rhs" then show "linear f" by unfold_locales simp_all
   229 qed
   230 
   231 lemma linear_compose_cmul: "linear f \<Longrightarrow> linear (\<lambda>x. c *\<^sub>R f x)"
   232   by (simp add: linear_iff algebra_simps)
   233 
   234 lemma linear_compose_neg: "linear f \<Longrightarrow> linear (\<lambda>x. - f x)"
   235   by (simp add: linear_iff)
   236 
   237 lemma linear_compose_add: "linear f \<Longrightarrow> linear g \<Longrightarrow> linear (\<lambda>x. f x + g x)"
   238   by (simp add: linear_iff algebra_simps)
   239 
   240 lemma linear_compose_sub: "linear f \<Longrightarrow> linear g \<Longrightarrow> linear (\<lambda>x. f x - g x)"
   241   by (simp add: linear_iff algebra_simps)
   242 
   243 lemma linear_compose: "linear f \<Longrightarrow> linear g \<Longrightarrow> linear (g \<circ> f)"
   244   by (simp add: linear_iff)
   245 
   246 lemma linear_id: "linear id"
   247   by (simp add: linear_iff id_def)
   248 
   249 lemma linear_zero: "linear (\<lambda>x. 0)"
   250   by (simp add: linear_iff)
   251 
   252 lemma linear_compose_setsum:
   253   assumes fS: "finite S"
   254     and lS: "\<forall>a \<in> S. linear (f a)"
   255   shows "linear (\<lambda>x. setsum (\<lambda>a. f a x) S)"
   256   using lS
   257   apply (induct rule: finite_induct[OF fS])
   258   apply (auto simp add: linear_zero intro: linear_compose_add)
   259   done
   260 
   261 lemma linear_0: "linear f \<Longrightarrow> f 0 = 0"
   262   unfolding linear_iff
   263   apply clarsimp
   264   apply (erule allE[where x="0::'a"])
   265   apply simp
   266   done
   267 
   268 lemma linear_cmul: "linear f \<Longrightarrow> f (c *\<^sub>R x) = c *\<^sub>R f x"
   269   by (simp add: linear_iff)
   270 
   271 lemma linear_neg: "linear f \<Longrightarrow> f (- x) = - f x"
   272   using linear_cmul [where c="-1"] by simp
   273 
   274 lemma linear_add: "linear f \<Longrightarrow> f (x + y) = f x + f y"
   275   by (metis linear_iff)
   276 
   277 lemma linear_sub: "linear f \<Longrightarrow> f (x - y) = f x - f y"
   278   using linear_add [of f x "- y"] by (simp add: linear_neg)
   279 
   280 lemma linear_setsum:
   281   assumes lin: "linear f"
   282     and fin: "finite S"
   283   shows "f (setsum g S) = setsum (f \<circ> g) S"
   284   using fin
   285 proof induct
   286   case empty
   287   then show ?case
   288     by (simp add: linear_0[OF lin])
   289 next
   290   case (insert x F)
   291   have "f (setsum g (insert x F)) = f (g x + setsum g F)"
   292     using insert.hyps by simp
   293   also have "\<dots> = f (g x) + f (setsum g F)"
   294     using linear_add[OF lin] by simp
   295   also have "\<dots> = setsum (f \<circ> g) (insert x F)"
   296     using insert.hyps by simp
   297   finally show ?case .
   298 qed
   299 
   300 lemma linear_setsum_mul:
   301   assumes lin: "linear f"
   302     and fin: "finite S"
   303   shows "f (setsum (\<lambda>i. c i *\<^sub>R v i) S) = setsum (\<lambda>i. c i *\<^sub>R f (v i)) S"
   304   using linear_setsum[OF lin fin, of "\<lambda>i. c i *\<^sub>R v i" , unfolded o_def] linear_cmul[OF lin]
   305   by simp
   306 
   307 lemma linear_injective_0:
   308   assumes lin: "linear f"
   309   shows "inj f \<longleftrightarrow> (\<forall>x. f x = 0 \<longrightarrow> x = 0)"
   310 proof -
   311   have "inj f \<longleftrightarrow> (\<forall> x y. f x = f y \<longrightarrow> x = y)"
   312     by (simp add: inj_on_def)
   313   also have "\<dots> \<longleftrightarrow> (\<forall> x y. f x - f y = 0 \<longrightarrow> x - y = 0)"
   314     by simp
   315   also have "\<dots> \<longleftrightarrow> (\<forall> x y. f (x - y) = 0 \<longrightarrow> x - y = 0)"
   316     by (simp add: linear_sub[OF lin])
   317   also have "\<dots> \<longleftrightarrow> (\<forall> x. f x = 0 \<longrightarrow> x = 0)"
   318     by auto
   319   finally show ?thesis .
   320 qed
   321 
   322 
   323 subsection {* Bilinear functions. *}
   324 
   325 definition "bilinear f \<longleftrightarrow> (\<forall>x. linear (\<lambda>y. f x y)) \<and> (\<forall>y. linear (\<lambda>x. f x y))"
   326 
   327 lemma bilinear_ladd: "bilinear h \<Longrightarrow> h (x + y) z = h x z + h y z"
   328   by (simp add: bilinear_def linear_iff)
   329 
   330 lemma bilinear_radd: "bilinear h \<Longrightarrow> h x (y + z) = h x y + h x z"
   331   by (simp add: bilinear_def linear_iff)
   332 
   333 lemma bilinear_lmul: "bilinear h \<Longrightarrow> h (c *\<^sub>R x) y = c *\<^sub>R h x y"
   334   by (simp add: bilinear_def linear_iff)
   335 
   336 lemma bilinear_rmul: "bilinear h \<Longrightarrow> h x (c *\<^sub>R y) = c *\<^sub>R h x y"
   337   by (simp add: bilinear_def linear_iff)
   338 
   339 lemma bilinear_lneg: "bilinear h \<Longrightarrow> h (- x) y = - h x y"
   340   by (drule bilinear_lmul [of _ "- 1"]) simp
   341 
   342 lemma bilinear_rneg: "bilinear h \<Longrightarrow> h x (- y) = - h x y"
   343   by (drule bilinear_rmul [of _ _ "- 1"]) simp
   344 
   345 lemma (in ab_group_add) eq_add_iff: "x = x + y \<longleftrightarrow> y = 0"
   346   using add_imp_eq[of x y 0] by auto
   347 
   348 lemma bilinear_lzero:
   349   assumes "bilinear h"
   350   shows "h 0 x = 0"
   351   using bilinear_ladd [OF assms, of 0 0 x] by (simp add: eq_add_iff field_simps)
   352 
   353 lemma bilinear_rzero:
   354   assumes "bilinear h"
   355   shows "h x 0 = 0"
   356   using bilinear_radd [OF assms, of x 0 0 ] by (simp add: eq_add_iff field_simps)
   357 
   358 lemma bilinear_lsub: "bilinear h \<Longrightarrow> h (x - y) z = h x z - h y z"
   359   using bilinear_ladd [of h x "- y"] by (simp add: bilinear_lneg)
   360 
   361 lemma bilinear_rsub: "bilinear h \<Longrightarrow> h z (x - y) = h z x - h z y"
   362   using bilinear_radd [of h _ x "- y"] by (simp add: bilinear_rneg)
   363 
   364 lemma bilinear_setsum:
   365   assumes bh: "bilinear h"
   366     and fS: "finite S"
   367     and fT: "finite T"
   368   shows "h (setsum f S) (setsum g T) = setsum (\<lambda>(i,j). h (f i) (g j)) (S \<times> T) "
   369 proof -
   370   have "h (setsum f S) (setsum g T) = setsum (\<lambda>x. h (f x) (setsum g T)) S"
   371     apply (rule linear_setsum[unfolded o_def])
   372     using bh fS
   373     apply (auto simp add: bilinear_def)
   374     done
   375   also have "\<dots> = setsum (\<lambda>x. setsum (\<lambda>y. h (f x) (g y)) T) S"
   376     apply (rule setsum_cong, simp)
   377     apply (rule linear_setsum[unfolded o_def])
   378     using bh fT
   379     apply (auto simp add: bilinear_def)
   380     done
   381   finally show ?thesis
   382     unfolding setsum_cartesian_product .
   383 qed
   384 
   385 
   386 subsection {* Adjoints. *}
   387 
   388 definition "adjoint f = (SOME f'. \<forall>x y. f x \<bullet> y = x \<bullet> f' y)"
   389 
   390 lemma adjoint_unique:
   391   assumes "\<forall>x y. inner (f x) y = inner x (g y)"
   392   shows "adjoint f = g"
   393   unfolding adjoint_def
   394 proof (rule some_equality)
   395   show "\<forall>x y. inner (f x) y = inner x (g y)"
   396     by (rule assms)
   397 next
   398   fix h
   399   assume "\<forall>x y. inner (f x) y = inner x (h y)"
   400   then have "\<forall>x y. inner x (g y) = inner x (h y)"
   401     using assms by simp
   402   then have "\<forall>x y. inner x (g y - h y) = 0"
   403     by (simp add: inner_diff_right)
   404   then have "\<forall>y. inner (g y - h y) (g y - h y) = 0"
   405     by simp
   406   then have "\<forall>y. h y = g y"
   407     by simp
   408   then show "h = g" by (simp add: ext)
   409 qed
   410 
   411 text {* TODO: The following lemmas about adjoints should hold for any
   412 Hilbert space (i.e. complete inner product space).
   413 (see @{url "http://en.wikipedia.org/wiki/Hermitian_adjoint"})
   414 *}
   415 
   416 lemma adjoint_works:
   417   fixes f:: "'n::euclidean_space \<Rightarrow> 'm::euclidean_space"
   418   assumes lf: "linear f"
   419   shows "x \<bullet> adjoint f y = f x \<bullet> y"
   420 proof -
   421   have "\<forall>y. \<exists>w. \<forall>x. f x \<bullet> y = x \<bullet> w"
   422   proof (intro allI exI)
   423     fix y :: "'m" and x
   424     let ?w = "(\<Sum>i\<in>Basis. (f i \<bullet> y) *\<^sub>R i) :: 'n"
   425     have "f x \<bullet> y = f (\<Sum>i\<in>Basis. (x \<bullet> i) *\<^sub>R i) \<bullet> y"
   426       by (simp add: euclidean_representation)
   427     also have "\<dots> = (\<Sum>i\<in>Basis. (x \<bullet> i) *\<^sub>R f i) \<bullet> y"
   428       unfolding linear_setsum[OF lf finite_Basis]
   429       by (simp add: linear_cmul[OF lf])
   430     finally show "f x \<bullet> y = x \<bullet> ?w"
   431       by (simp add: inner_setsum_left inner_setsum_right mult_commute)
   432   qed
   433   then show ?thesis
   434     unfolding adjoint_def choice_iff
   435     by (intro someI2_ex[where Q="\<lambda>f'. x \<bullet> f' y = f x \<bullet> y"]) auto
   436 qed
   437 
   438 lemma adjoint_clauses:
   439   fixes f:: "'n::euclidean_space \<Rightarrow> 'm::euclidean_space"
   440   assumes lf: "linear f"
   441   shows "x \<bullet> adjoint f y = f x \<bullet> y"
   442     and "adjoint f y \<bullet> x = y \<bullet> f x"
   443   by (simp_all add: adjoint_works[OF lf] inner_commute)
   444 
   445 lemma adjoint_linear:
   446   fixes f:: "'n::euclidean_space \<Rightarrow> 'm::euclidean_space"
   447   assumes lf: "linear f"
   448   shows "linear (adjoint f)"
   449   by (simp add: lf linear_iff euclidean_eq_iff[where 'a='n] euclidean_eq_iff[where 'a='m]
   450     adjoint_clauses[OF lf] inner_distrib)
   451 
   452 lemma adjoint_adjoint:
   453   fixes f:: "'n::euclidean_space \<Rightarrow> 'm::euclidean_space"
   454   assumes lf: "linear f"
   455   shows "adjoint (adjoint f) = f"
   456   by (rule adjoint_unique, simp add: adjoint_clauses [OF lf])
   457 
   458 
   459 subsection {* Interlude: Some properties of real sets *}
   460 
   461 lemma seq_mono_lemma:
   462   assumes "\<forall>(n::nat) \<ge> m. (d n :: real) < e n"
   463     and "\<forall>n \<ge> m. e n \<le> e m"
   464   shows "\<forall>n \<ge> m. d n < e m"
   465   using assms
   466   apply auto
   467   apply (erule_tac x="n" in allE)
   468   apply (erule_tac x="n" in allE)
   469   apply auto
   470   done
   471 
   472 lemma infinite_enumerate:
   473   assumes fS: "infinite S"
   474   shows "\<exists>r. subseq r \<and> (\<forall>n. r n \<in> S)"
   475   unfolding subseq_def
   476   using enumerate_in_set[OF fS] enumerate_mono[of _ _ S] fS by auto
   477 
   478 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)"
   479   apply auto
   480   apply (rule_tac x="d/2" in exI)
   481   apply auto
   482   done
   483 
   484 lemma triangle_lemma:
   485   fixes x y z :: real
   486   assumes x: "0 \<le> x"
   487     and y: "0 \<le> y"
   488     and z: "0 \<le> z"
   489     and xy: "x\<^sup>2 \<le> y\<^sup>2 + z\<^sup>2"
   490   shows "x \<le> y + z"
   491 proof -
   492   have "y\<^sup>2 + z\<^sup>2 \<le> y\<^sup>2 + 2 *y * z + z\<^sup>2"
   493     using z y by (simp add: mult_nonneg_nonneg)
   494   with xy have th: "x\<^sup>2 \<le> (y + z)\<^sup>2"
   495     by (simp add: power2_eq_square field_simps)
   496   from y z have yz: "y + z \<ge> 0"
   497     by arith
   498   from power2_le_imp_le[OF th yz] show ?thesis .
   499 qed
   500 
   501 
   502 subsection {* A generic notion of "hull" (convex, affine, conic hull and closure). *}
   503 
   504 definition hull :: "('a set \<Rightarrow> bool) \<Rightarrow> 'a set \<Rightarrow> 'a set"  (infixl "hull" 75)
   505   where "S hull s = \<Inter>{t. S t \<and> s \<subseteq> t}"
   506 
   507 lemma hull_same: "S s \<Longrightarrow> S hull s = s"
   508   unfolding hull_def by auto
   509 
   510 lemma hull_in: "(\<And>T. Ball T S \<Longrightarrow> S (\<Inter>T)) \<Longrightarrow> S (S hull s)"
   511   unfolding hull_def Ball_def by auto
   512 
   513 lemma hull_eq: "(\<And>T. Ball T S \<Longrightarrow> S (\<Inter>T)) \<Longrightarrow> (S hull s) = s \<longleftrightarrow> S s"
   514   using hull_same[of S s] hull_in[of S s] by metis
   515 
   516 lemma hull_hull: "S hull (S hull s) = S hull s"
   517   unfolding hull_def by blast
   518 
   519 lemma hull_subset[intro]: "s \<subseteq> (S hull s)"
   520   unfolding hull_def by blast
   521 
   522 lemma hull_mono: "s \<subseteq> t \<Longrightarrow> (S hull s) \<subseteq> (S hull t)"
   523   unfolding hull_def by blast
   524 
   525 lemma hull_antimono: "\<forall>x. S x \<longrightarrow> T x \<Longrightarrow> (T hull s) \<subseteq> (S hull s)"
   526   unfolding hull_def by blast
   527 
   528 lemma hull_minimal: "s \<subseteq> t \<Longrightarrow> S t \<Longrightarrow> (S hull s) \<subseteq> t"
   529   unfolding hull_def by blast
   530 
   531 lemma subset_hull: "S t \<Longrightarrow> S hull s \<subseteq> t \<longleftrightarrow> s \<subseteq> t"
   532   unfolding hull_def by blast
   533 
   534 lemma hull_UNIV: "S hull UNIV = UNIV"
   535   unfolding hull_def by auto
   536 
   537 lemma hull_unique: "s \<subseteq> t \<Longrightarrow> S t \<Longrightarrow> (\<And>t'. s \<subseteq> t' \<Longrightarrow> S t' \<Longrightarrow> t \<subseteq> t') \<Longrightarrow> (S hull s = t)"
   538   unfolding hull_def by auto
   539 
   540 lemma hull_induct: "(\<And>x. x\<in> S \<Longrightarrow> P x) \<Longrightarrow> Q {x. P x} \<Longrightarrow> \<forall>x\<in> Q hull S. P x"
   541   using hull_minimal[of S "{x. P x}" Q]
   542   by (auto simp add: subset_eq)
   543 
   544 lemma hull_inc: "x \<in> S \<Longrightarrow> x \<in> P hull S"
   545   by (metis hull_subset subset_eq)
   546 
   547 lemma hull_union_subset: "(S hull s) \<union> (S hull t) \<subseteq> (S hull (s \<union> t))"
   548   unfolding Un_subset_iff by (metis hull_mono Un_upper1 Un_upper2)
   549 
   550 lemma hull_union:
   551   assumes T: "\<And>T. Ball T S \<Longrightarrow> S (\<Inter>T)"
   552   shows "S hull (s \<union> t) = S hull (S hull s \<union> S hull t)"
   553   apply rule
   554   apply (rule hull_mono)
   555   unfolding Un_subset_iff
   556   apply (metis hull_subset Un_upper1 Un_upper2 subset_trans)
   557   apply (rule hull_minimal)
   558   apply (metis hull_union_subset)
   559   apply (metis hull_in T)
   560   done
   561 
   562 lemma hull_redundant_eq: "a \<in> (S hull s) \<longleftrightarrow> (S hull (insert a s) = S hull s)"
   563   unfolding hull_def by blast
   564 
   565 lemma hull_redundant: "a \<in> (S hull s) \<Longrightarrow> (S hull (insert a s) = S hull s)"
   566   by (metis hull_redundant_eq)
   567 
   568 
   569 subsection {* Archimedean properties and useful consequences *}
   570 
   571 lemma real_arch_simple: "\<exists>n. x \<le> real (n::nat)"
   572   unfolding real_of_nat_def by (rule ex_le_of_nat)
   573 
   574 lemma real_arch_inv: "0 < e \<longleftrightarrow> (\<exists>n::nat. n \<noteq> 0 \<and> 0 < inverse (real n) \<and> inverse (real n) < e)"
   575   using reals_Archimedean
   576   apply (auto simp add: field_simps)
   577   apply (subgoal_tac "inverse (real n) > 0")
   578   apply arith
   579   apply simp
   580   done
   581 
   582 lemma real_pow_lbound: "0 \<le> x \<Longrightarrow> 1 + real n * x \<le> (1 + x) ^ n"
   583 proof (induct n)
   584   case 0
   585   then show ?case by simp
   586 next
   587   case (Suc n)
   588   then have h: "1 + real n * x \<le> (1 + x) ^ n"
   589     by simp
   590   from h have p: "1 \<le> (1 + x) ^ n"
   591     using Suc.prems by simp
   592   from h have "1 + real n * x + x \<le> (1 + x) ^ n + x"
   593     by simp
   594   also have "\<dots> \<le> (1 + x) ^ Suc n"
   595     apply (subst diff_le_0_iff_le[symmetric])
   596     apply (simp add: field_simps)
   597     using mult_left_mono[OF p Suc.prems]
   598     apply simp
   599     done
   600   finally show ?case
   601     by (simp add: real_of_nat_Suc field_simps)
   602 qed
   603 
   604 lemma real_arch_pow:
   605   fixes x :: real
   606   assumes x: "1 < x"
   607   shows "\<exists>n. y < x^n"
   608 proof -
   609   from x have x0: "x - 1 > 0"
   610     by arith
   611   from reals_Archimedean3[OF x0, rule_format, of y]
   612   obtain n :: nat where n: "y < real n * (x - 1)" by metis
   613   from x0 have x00: "x- 1 \<ge> 0" by arith
   614   from real_pow_lbound[OF x00, of n] n
   615   have "y < x^n" by auto
   616   then show ?thesis by metis
   617 qed
   618 
   619 lemma real_arch_pow2:
   620   fixes x :: real
   621   shows "\<exists>n. x < 2^ n"
   622   using real_arch_pow[of 2 x] by simp
   623 
   624 lemma real_arch_pow_inv:
   625   fixes x y :: real
   626   assumes y: "y > 0"
   627     and x1: "x < 1"
   628   shows "\<exists>n. x^n < y"
   629 proof (cases "x > 0")
   630   case True
   631   with x1 have ix: "1 < 1/x" by (simp add: field_simps)
   632   from real_arch_pow[OF ix, of "1/y"]
   633   obtain n where n: "1/y < (1/x)^n" by blast
   634   then show ?thesis using y `x > 0`
   635     by (auto simp add: field_simps power_divide)
   636 next
   637   case False
   638   with y x1 show ?thesis
   639     apply auto
   640     apply (rule exI[where x=1])
   641     apply auto
   642     done
   643 qed
   644 
   645 lemma forall_pos_mono:
   646   "(\<And>d e::real. d < e \<Longrightarrow> P d \<Longrightarrow> P e) \<Longrightarrow>
   647     (\<And>n::nat. n \<noteq> 0 \<Longrightarrow> P (inverse (real n))) \<Longrightarrow> (\<And>e. 0 < e \<Longrightarrow> P e)"
   648   by (metis real_arch_inv)
   649 
   650 lemma forall_pos_mono_1:
   651   "(\<And>d e::real. d < e \<Longrightarrow> P d \<Longrightarrow> P e) \<Longrightarrow>
   652     (\<And>n. P (inverse (real (Suc n)))) \<Longrightarrow> 0 < e \<Longrightarrow> P e"
   653   apply (rule forall_pos_mono)
   654   apply auto
   655   apply (atomize)
   656   apply (erule_tac x="n - 1" in allE)
   657   apply auto
   658   done
   659 
   660 lemma real_archimedian_rdiv_eq_0:
   661   assumes x0: "x \<ge> 0"
   662     and c: "c \<ge> 0"
   663     and xc: "\<forall>(m::nat)>0. real m * x \<le> c"
   664   shows "x = 0"
   665 proof (rule ccontr)
   666   assume "x \<noteq> 0"
   667   with x0 have xp: "x > 0" by arith
   668   from reals_Archimedean3[OF xp, rule_format, of c]
   669   obtain n :: nat where n: "c < real n * x"
   670     by blast
   671   with xc[rule_format, of n] have "n = 0"
   672     by arith
   673   with n c show False
   674     by simp
   675 qed
   676 
   677 
   678 subsection{* A bit of linear algebra. *}
   679 
   680 definition (in real_vector) subspace :: "'a set \<Rightarrow> bool"
   681   where "subspace S \<longleftrightarrow> 0 \<in> S \<and> (\<forall>x\<in> S. \<forall>y \<in>S. x + y \<in> S) \<and> (\<forall>c. \<forall>x \<in>S. c *\<^sub>R x \<in>S )"
   682 
   683 definition (in real_vector) "span S = (subspace hull S)"
   684 definition (in real_vector) "dependent S \<longleftrightarrow> (\<exists>a \<in> S. a \<in> span (S - {a}))"
   685 abbreviation (in real_vector) "independent s \<equiv> \<not> dependent s"
   686 
   687 text {* Closure properties of subspaces. *}
   688 
   689 lemma subspace_UNIV[simp]: "subspace UNIV"
   690   by (simp add: subspace_def)
   691 
   692 lemma (in real_vector) subspace_0: "subspace S \<Longrightarrow> 0 \<in> S"
   693   by (metis subspace_def)
   694 
   695 lemma (in real_vector) subspace_add: "subspace S \<Longrightarrow> x \<in> S \<Longrightarrow> y \<in> S \<Longrightarrow> x + y \<in> S"
   696   by (metis subspace_def)
   697 
   698 lemma (in real_vector) subspace_mul: "subspace S \<Longrightarrow> x \<in> S \<Longrightarrow> c *\<^sub>R x \<in> S"
   699   by (metis subspace_def)
   700 
   701 lemma subspace_neg: "subspace S \<Longrightarrow> x \<in> S \<Longrightarrow> - x \<in> S"
   702   by (metis scaleR_minus1_left subspace_mul)
   703 
   704 lemma subspace_sub: "subspace S \<Longrightarrow> x \<in> S \<Longrightarrow> y \<in> S \<Longrightarrow> x - y \<in> S"
   705   using subspace_add [of S x "- y"] by (simp add: subspace_neg)
   706 
   707 lemma (in real_vector) subspace_setsum:
   708   assumes sA: "subspace A"
   709     and fB: "finite B"
   710     and f: "\<forall>x\<in> B. f x \<in> A"
   711   shows "setsum f B \<in> A"
   712   using  fB f sA
   713   by (induct rule: finite_induct[OF fB])
   714     (simp add: subspace_def sA, auto simp add: sA subspace_add)
   715 
   716 lemma subspace_linear_image:
   717   assumes lf: "linear f"
   718     and sS: "subspace S"
   719   shows "subspace (f ` S)"
   720   using lf sS linear_0[OF lf]
   721   unfolding linear_iff subspace_def
   722   apply (auto simp add: image_iff)
   723   apply (rule_tac x="x + y" in bexI)
   724   apply auto
   725   apply (rule_tac x="c *\<^sub>R x" in bexI)
   726   apply auto
   727   done
   728 
   729 lemma subspace_linear_vimage: "linear f \<Longrightarrow> subspace S \<Longrightarrow> subspace (f -` S)"
   730   by (auto simp add: subspace_def linear_iff linear_0[of f])
   731 
   732 lemma subspace_linear_preimage: "linear f \<Longrightarrow> subspace S \<Longrightarrow> subspace {x. f x \<in> S}"
   733   by (auto simp add: subspace_def linear_iff linear_0[of f])
   734 
   735 lemma subspace_trivial: "subspace {0}"
   736   by (simp add: subspace_def)
   737 
   738 lemma (in real_vector) subspace_inter: "subspace A \<Longrightarrow> subspace B \<Longrightarrow> subspace (A \<inter> B)"
   739   by (simp add: subspace_def)
   740 
   741 lemma subspace_Times: "subspace A \<Longrightarrow> subspace B \<Longrightarrow> subspace (A \<times> B)"
   742   unfolding subspace_def zero_prod_def by simp
   743 
   744 text {* Properties of span. *}
   745 
   746 lemma (in real_vector) span_mono: "A \<subseteq> B \<Longrightarrow> span A \<subseteq> span B"
   747   by (metis span_def hull_mono)
   748 
   749 lemma (in real_vector) subspace_span: "subspace (span S)"
   750   unfolding span_def
   751   apply (rule hull_in)
   752   apply (simp only: subspace_def Inter_iff Int_iff subset_eq)
   753   apply auto
   754   done
   755 
   756 lemma (in real_vector) span_clauses:
   757   "a \<in> S \<Longrightarrow> a \<in> span S"
   758   "0 \<in> span S"
   759   "x\<in> span S \<Longrightarrow> y \<in> span S \<Longrightarrow> x + y \<in> span S"
   760   "x \<in> span S \<Longrightarrow> c *\<^sub>R x \<in> span S"
   761   by (metis span_def hull_subset subset_eq) (metis subspace_span subspace_def)+
   762 
   763 lemma span_unique:
   764   "S \<subseteq> T \<Longrightarrow> subspace T \<Longrightarrow> (\<And>T'. S \<subseteq> T' \<Longrightarrow> subspace T' \<Longrightarrow> T \<subseteq> T') \<Longrightarrow> span S = T"
   765   unfolding span_def by (rule hull_unique)
   766 
   767 lemma span_minimal: "S \<subseteq> T \<Longrightarrow> subspace T \<Longrightarrow> span S \<subseteq> T"
   768   unfolding span_def by (rule hull_minimal)
   769 
   770 lemma (in real_vector) span_induct:
   771   assumes x: "x \<in> span S"
   772     and P: "subspace P"
   773     and SP: "\<And>x. x \<in> S \<Longrightarrow> x \<in> P"
   774   shows "x \<in> P"
   775 proof -
   776   from SP have SP': "S \<subseteq> P"
   777     by (simp add: subset_eq)
   778   from x hull_minimal[where S=subspace, OF SP' P, unfolded span_def[symmetric]]
   779   show "x \<in> P"
   780     by (metis subset_eq)
   781 qed
   782 
   783 lemma span_empty[simp]: "span {} = {0}"
   784   apply (simp add: span_def)
   785   apply (rule hull_unique)
   786   apply (auto simp add: subspace_def)
   787   done
   788 
   789 lemma (in real_vector) independent_empty[intro]: "independent {}"
   790   by (simp add: dependent_def)
   791 
   792 lemma dependent_single[simp]: "dependent {x} \<longleftrightarrow> x = 0"
   793   unfolding dependent_def by auto
   794 
   795 lemma (in real_vector) independent_mono: "independent A \<Longrightarrow> B \<subseteq> A \<Longrightarrow> independent B"
   796   apply (clarsimp simp add: dependent_def span_mono)
   797   apply (subgoal_tac "span (B - {a}) \<le> span (A - {a})")
   798   apply force
   799   apply (rule span_mono)
   800   apply auto
   801   done
   802 
   803 lemma (in real_vector) span_subspace: "A \<subseteq> B \<Longrightarrow> B \<le> span A \<Longrightarrow>  subspace B \<Longrightarrow> span A = B"
   804   by (metis order_antisym span_def hull_minimal)
   805 
   806 lemma (in real_vector) span_induct':
   807   assumes SP: "\<forall>x \<in> S. P x"
   808     and P: "subspace {x. P x}"
   809   shows "\<forall>x \<in> span S. P x"
   810   using span_induct SP P by blast
   811 
   812 inductive_set (in real_vector) span_induct_alt_help for S:: "'a set"
   813 where
   814   span_induct_alt_help_0: "0 \<in> span_induct_alt_help S"
   815 | span_induct_alt_help_S:
   816     "x \<in> S \<Longrightarrow> z \<in> span_induct_alt_help S \<Longrightarrow>
   817       (c *\<^sub>R x + z) \<in> span_induct_alt_help S"
   818 
   819 lemma span_induct_alt':
   820   assumes h0: "h 0"
   821     and hS: "\<And>c x y. x \<in> S \<Longrightarrow> h y \<Longrightarrow> h (c *\<^sub>R x + y)"
   822   shows "\<forall>x \<in> span S. h x"
   823 proof -
   824   {
   825     fix x :: 'a
   826     assume x: "x \<in> span_induct_alt_help S"
   827     have "h x"
   828       apply (rule span_induct_alt_help.induct[OF x])
   829       apply (rule h0)
   830       apply (rule hS)
   831       apply assumption
   832       apply assumption
   833       done
   834   }
   835   note th0 = this
   836   {
   837     fix x
   838     assume x: "x \<in> span S"
   839     have "x \<in> span_induct_alt_help S"
   840     proof (rule span_induct[where x=x and S=S])
   841       show "x \<in> span S" by (rule x)
   842     next
   843       fix x
   844       assume xS: "x \<in> S"
   845       from span_induct_alt_help_S[OF xS span_induct_alt_help_0, of 1]
   846       show "x \<in> span_induct_alt_help S"
   847         by simp
   848     next
   849       have "0 \<in> span_induct_alt_help S" by (rule span_induct_alt_help_0)
   850       moreover
   851       {
   852         fix x y
   853         assume h: "x \<in> span_induct_alt_help S" "y \<in> span_induct_alt_help S"
   854         from h have "(x + y) \<in> span_induct_alt_help S"
   855           apply (induct rule: span_induct_alt_help.induct)
   856           apply simp
   857           unfolding add_assoc
   858           apply (rule span_induct_alt_help_S)
   859           apply assumption
   860           apply simp
   861           done
   862       }
   863       moreover
   864       {
   865         fix c x
   866         assume xt: "x \<in> span_induct_alt_help S"
   867         then have "(c *\<^sub>R x) \<in> span_induct_alt_help S"
   868           apply (induct rule: span_induct_alt_help.induct)
   869           apply (simp add: span_induct_alt_help_0)
   870           apply (simp add: scaleR_right_distrib)
   871           apply (rule span_induct_alt_help_S)
   872           apply assumption
   873           apply simp
   874           done }
   875       ultimately show "subspace (span_induct_alt_help S)"
   876         unfolding subspace_def Ball_def by blast
   877     qed
   878   }
   879   with th0 show ?thesis by blast
   880 qed
   881 
   882 lemma span_induct_alt:
   883   assumes h0: "h 0"
   884     and hS: "\<And>c x y. x \<in> S \<Longrightarrow> h y \<Longrightarrow> h (c *\<^sub>R x + y)"
   885     and x: "x \<in> span S"
   886   shows "h x"
   887   using span_induct_alt'[of h S] h0 hS x by blast
   888 
   889 text {* Individual closure properties. *}
   890 
   891 lemma span_span: "span (span A) = span A"
   892   unfolding span_def hull_hull ..
   893 
   894 lemma (in real_vector) span_superset: "x \<in> S \<Longrightarrow> x \<in> span S"
   895   by (metis span_clauses(1))
   896 
   897 lemma (in real_vector) span_0: "0 \<in> span S"
   898   by (metis subspace_span subspace_0)
   899 
   900 lemma span_inc: "S \<subseteq> span S"
   901   by (metis subset_eq span_superset)
   902 
   903 lemma (in real_vector) dependent_0:
   904   assumes "0 \<in> A"
   905   shows "dependent A"
   906   unfolding dependent_def
   907   apply (rule_tac x=0 in bexI)
   908   using assms span_0
   909   apply auto
   910   done
   911 
   912 lemma (in real_vector) span_add: "x \<in> span S \<Longrightarrow> y \<in> span S \<Longrightarrow> x + y \<in> span S"
   913   by (metis subspace_add subspace_span)
   914 
   915 lemma (in real_vector) span_mul: "x \<in> span S \<Longrightarrow> c *\<^sub>R x \<in> span S"
   916   by (metis subspace_span subspace_mul)
   917 
   918 lemma span_neg: "x \<in> span S \<Longrightarrow> - x \<in> span S"
   919   by (metis subspace_neg subspace_span)
   920 
   921 lemma span_sub: "x \<in> span S \<Longrightarrow> y \<in> span S \<Longrightarrow> x - y \<in> span S"
   922   by (metis subspace_span subspace_sub)
   923 
   924 lemma (in real_vector) span_setsum: "finite A \<Longrightarrow> \<forall>x \<in> A. f x \<in> span S \<Longrightarrow> setsum f A \<in> span S"
   925   by (rule subspace_setsum, rule subspace_span)
   926 
   927 lemma span_add_eq: "x \<in> span S \<Longrightarrow> x + y \<in> span S \<longleftrightarrow> y \<in> span S"
   928   by (metis add_minus_cancel scaleR_minus1_left subspace_def subspace_span)
   929 
   930 text {* Mapping under linear image. *}
   931 
   932 lemma span_linear_image:
   933   assumes lf: "linear f"
   934   shows "span (f ` S) = f ` (span S)"
   935 proof (rule span_unique)
   936   show "f ` S \<subseteq> f ` span S"
   937     by (intro image_mono span_inc)
   938   show "subspace (f ` span S)"
   939     using lf subspace_span by (rule subspace_linear_image)
   940 next
   941   fix T
   942   assume "f ` S \<subseteq> T" and "subspace T"
   943   then show "f ` span S \<subseteq> T"
   944     unfolding image_subset_iff_subset_vimage
   945     by (intro span_minimal subspace_linear_vimage lf)
   946 qed
   947 
   948 lemma span_union: "span (A \<union> B) = (\<lambda>(a, b). a + b) ` (span A \<times> span B)"
   949 proof (rule span_unique)
   950   show "A \<union> B \<subseteq> (\<lambda>(a, b). a + b) ` (span A \<times> span B)"
   951     by safe (force intro: span_clauses)+
   952 next
   953   have "linear (\<lambda>(a, b). a + b)"
   954     by (simp add: linear_iff scaleR_add_right)
   955   moreover have "subspace (span A \<times> span B)"
   956     by (intro subspace_Times subspace_span)
   957   ultimately show "subspace ((\<lambda>(a, b). a + b) ` (span A \<times> span B))"
   958     by (rule subspace_linear_image)
   959 next
   960   fix T
   961   assume "A \<union> B \<subseteq> T" and "subspace T"
   962   then show "(\<lambda>(a, b). a + b) ` (span A \<times> span B) \<subseteq> T"
   963     by (auto intro!: subspace_add elim: span_induct)
   964 qed
   965 
   966 text {* The key breakdown property. *}
   967 
   968 lemma span_singleton: "span {x} = range (\<lambda>k. k *\<^sub>R x)"
   969 proof (rule span_unique)
   970   show "{x} \<subseteq> range (\<lambda>k. k *\<^sub>R x)"
   971     by (fast intro: scaleR_one [symmetric])
   972   show "subspace (range (\<lambda>k. k *\<^sub>R x))"
   973     unfolding subspace_def
   974     by (auto intro: scaleR_add_left [symmetric])
   975 next
   976   fix T
   977   assume "{x} \<subseteq> T" and "subspace T"
   978   then show "range (\<lambda>k. k *\<^sub>R x) \<subseteq> T"
   979     unfolding subspace_def by auto
   980 qed
   981 
   982 lemma span_insert: "span (insert a S) = {x. \<exists>k. (x - k *\<^sub>R a) \<in> span S}"
   983 proof -
   984   have "span ({a} \<union> S) = {x. \<exists>k. (x - k *\<^sub>R a) \<in> span S}"
   985     unfolding span_union span_singleton
   986     apply safe
   987     apply (rule_tac x=k in exI, simp)
   988     apply (erule rev_image_eqI [OF SigmaI [OF rangeI]])
   989     apply auto
   990     done
   991   then show ?thesis by simp
   992 qed
   993 
   994 lemma span_breakdown:
   995   assumes bS: "b \<in> S"
   996     and aS: "a \<in> span S"
   997   shows "\<exists>k. a - k *\<^sub>R b \<in> span (S - {b})"
   998   using assms span_insert [of b "S - {b}"]
   999   by (simp add: insert_absorb)
  1000 
  1001 lemma span_breakdown_eq: "x \<in> span (insert a S) \<longleftrightarrow> (\<exists>k. x - k *\<^sub>R a \<in> span S)"
  1002   by (simp add: span_insert)
  1003 
  1004 text {* Hence some "reversal" results. *}
  1005 
  1006 lemma in_span_insert:
  1007   assumes a: "a \<in> span (insert b S)"
  1008     and na: "a \<notin> span S"
  1009   shows "b \<in> span (insert a S)"
  1010 proof -
  1011   from a obtain k where k: "a - k *\<^sub>R b \<in> span S"
  1012     unfolding span_insert by fast
  1013   show ?thesis
  1014   proof (cases "k = 0")
  1015     case True
  1016     with k have "a \<in> span S" by simp
  1017     with na show ?thesis by simp
  1018   next
  1019     case False
  1020     from k have "(- inverse k) *\<^sub>R (a - k *\<^sub>R b) \<in> span S"
  1021       by (rule span_mul)
  1022     then have "b - inverse k *\<^sub>R a \<in> span S"
  1023       using `k \<noteq> 0` by (simp add: scaleR_diff_right)
  1024     then show ?thesis
  1025       unfolding span_insert by fast
  1026   qed
  1027 qed
  1028 
  1029 lemma in_span_delete:
  1030   assumes a: "a \<in> span S"
  1031     and na: "a \<notin> span (S - {b})"
  1032   shows "b \<in> span (insert a (S - {b}))"
  1033   apply (rule in_span_insert)
  1034   apply (rule set_rev_mp)
  1035   apply (rule a)
  1036   apply (rule span_mono)
  1037   apply blast
  1038   apply (rule na)
  1039   done
  1040 
  1041 text {* Transitivity property. *}
  1042 
  1043 lemma span_redundant: "x \<in> span S \<Longrightarrow> span (insert x S) = span S"
  1044   unfolding span_def by (rule hull_redundant)
  1045 
  1046 lemma span_trans:
  1047   assumes x: "x \<in> span S"
  1048     and y: "y \<in> span (insert x S)"
  1049   shows "y \<in> span S"
  1050   using assms by (simp only: span_redundant)
  1051 
  1052 lemma span_insert_0[simp]: "span (insert 0 S) = span S"
  1053   by (simp only: span_redundant span_0)
  1054 
  1055 text {* An explicit expansion is sometimes needed. *}
  1056 
  1057 lemma span_explicit:
  1058   "span P = {y. \<exists>S u. finite S \<and> S \<subseteq> P \<and> setsum (\<lambda>v. u v *\<^sub>R v) S = y}"
  1059   (is "_ = ?E" is "_ = {y. ?h y}" is "_ = {y. \<exists>S u. ?Q S u y}")
  1060 proof -
  1061   {
  1062     fix x
  1063     assume "?h x"
  1064     then obtain S u where "finite S" and "S \<subseteq> P" and "setsum (\<lambda>v. u v *\<^sub>R v) S = x"
  1065       by blast
  1066     then have "x \<in> span P"
  1067       by (auto intro: span_setsum span_mul span_superset)
  1068   }
  1069   moreover
  1070   have "\<forall>x \<in> span P. ?h x"
  1071   proof (rule span_induct_alt')
  1072     show "?h 0"
  1073       by (rule exI[where x="{}"], simp)
  1074   next
  1075     fix c x y
  1076     assume x: "x \<in> P"
  1077     assume hy: "?h y"
  1078     from hy obtain S u where fS: "finite S" and SP: "S\<subseteq>P"
  1079       and u: "setsum (\<lambda>v. u v *\<^sub>R v) S = y" by blast
  1080     let ?S = "insert x S"
  1081     let ?u = "\<lambda>y. if y = x then (if x \<in> S then u y + c else c) else u y"
  1082     from fS SP x have th0: "finite (insert x S)" "insert x S \<subseteq> P"
  1083       by blast+
  1084     have "?Q ?S ?u (c*\<^sub>R x + y)"
  1085     proof cases
  1086       assume xS: "x \<in> S"
  1087       have "setsum (\<lambda>v. ?u v *\<^sub>R v) ?S = (\<Sum>v\<in>S - {x}. u v *\<^sub>R v) + (u x + c) *\<^sub>R x"
  1088         using xS by (simp add: setsum.remove [OF fS xS] insert_absorb)
  1089       also have "\<dots> = (\<Sum>v\<in>S. u v *\<^sub>R v) + c *\<^sub>R x"
  1090         by (simp add: setsum.remove [OF fS xS] algebra_simps)
  1091       also have "\<dots> = c*\<^sub>R x + y"
  1092         by (simp add: add_commute u)
  1093       finally have "setsum (\<lambda>v. ?u v *\<^sub>R v) ?S = c*\<^sub>R x + y" .
  1094       then show ?thesis using th0 by blast
  1095     next
  1096       assume xS: "x \<notin> S"
  1097       have th00: "(\<Sum>v\<in>S. (if v = x then c else u v) *\<^sub>R v) = y"
  1098         unfolding u[symmetric]
  1099         apply (rule setsum_cong2)
  1100         using xS
  1101         apply auto
  1102         done
  1103       show ?thesis using fS xS th0
  1104         by (simp add: th00 add_commute cong del: if_weak_cong)
  1105     qed
  1106     then show "?h (c*\<^sub>R x + y)"
  1107       by fast
  1108   qed
  1109   ultimately show ?thesis by blast
  1110 qed
  1111 
  1112 lemma dependent_explicit:
  1113   "dependent P \<longleftrightarrow> (\<exists>S u. finite S \<and> S \<subseteq> P \<and> (\<exists>v\<in>S. u v \<noteq> 0 \<and> setsum (\<lambda>v. u v *\<^sub>R v) S = 0))"
  1114   (is "?lhs = ?rhs")
  1115 proof -
  1116   {
  1117     assume dP: "dependent P"
  1118     then obtain a S u where aP: "a \<in> P" and fS: "finite S"
  1119       and SP: "S \<subseteq> P - {a}" and ua: "setsum (\<lambda>v. u v *\<^sub>R v) S = a"
  1120       unfolding dependent_def span_explicit by blast
  1121     let ?S = "insert a S"
  1122     let ?u = "\<lambda>y. if y = a then - 1 else u y"
  1123     let ?v = a
  1124     from aP SP have aS: "a \<notin> S"
  1125       by blast
  1126     from fS SP aP have th0: "finite ?S" "?S \<subseteq> P" "?v \<in> ?S" "?u ?v \<noteq> 0"
  1127       by auto
  1128     have s0: "setsum (\<lambda>v. ?u v *\<^sub>R v) ?S = 0"
  1129       using fS aS
  1130       apply simp
  1131       apply (subst (2) ua[symmetric])
  1132       apply (rule setsum_cong2)
  1133       apply auto
  1134       done
  1135     with th0 have ?rhs by fast
  1136   }
  1137   moreover
  1138   {
  1139     fix S u v
  1140     assume fS: "finite S"
  1141       and SP: "S \<subseteq> P"
  1142       and vS: "v \<in> S"
  1143       and uv: "u v \<noteq> 0"
  1144       and u: "setsum (\<lambda>v. u v *\<^sub>R v) S = 0"
  1145     let ?a = v
  1146     let ?S = "S - {v}"
  1147     let ?u = "\<lambda>i. (- u i) / u v"
  1148     have th0: "?a \<in> P" "finite ?S" "?S \<subseteq> P"
  1149       using fS SP vS by auto
  1150     have "setsum (\<lambda>v. ?u v *\<^sub>R v) ?S =
  1151       setsum (\<lambda>v. (- (inverse (u ?a))) *\<^sub>R (u v *\<^sub>R v)) S - ?u v *\<^sub>R v"
  1152       using fS vS uv by (simp add: setsum_diff1 divide_inverse field_simps)
  1153     also have "\<dots> = ?a"
  1154       unfolding scaleR_right.setsum [symmetric] u using uv by simp
  1155     finally have "setsum (\<lambda>v. ?u v *\<^sub>R v) ?S = ?a" .
  1156     with th0 have ?lhs
  1157       unfolding dependent_def span_explicit
  1158       apply -
  1159       apply (rule bexI[where x= "?a"])
  1160       apply (simp_all del: scaleR_minus_left)
  1161       apply (rule exI[where x= "?S"])
  1162       apply (auto simp del: scaleR_minus_left)
  1163       done
  1164   }
  1165   ultimately show ?thesis by blast
  1166 qed
  1167 
  1168 
  1169 lemma span_finite:
  1170   assumes fS: "finite S"
  1171   shows "span S = {y. \<exists>u. setsum (\<lambda>v. u v *\<^sub>R v) S = y}"
  1172   (is "_ = ?rhs")
  1173 proof -
  1174   {
  1175     fix y
  1176     assume y: "y \<in> span S"
  1177     from y obtain S' u where fS': "finite S'"
  1178       and SS': "S' \<subseteq> S"
  1179       and u: "setsum (\<lambda>v. u v *\<^sub>R v) S' = y"
  1180       unfolding span_explicit by blast
  1181     let ?u = "\<lambda>x. if x \<in> S' then u x else 0"
  1182     have "setsum (\<lambda>v. ?u v *\<^sub>R v) S = setsum (\<lambda>v. u v *\<^sub>R v) S'"
  1183       using SS' fS by (auto intro!: setsum_mono_zero_cong_right)
  1184     then have "setsum (\<lambda>v. ?u v *\<^sub>R v) S = y" by (metis u)
  1185     then have "y \<in> ?rhs" by auto
  1186   }
  1187   moreover
  1188   {
  1189     fix y u
  1190     assume u: "setsum (\<lambda>v. u v *\<^sub>R v) S = y"
  1191     then have "y \<in> span S" using fS unfolding span_explicit by auto
  1192   }
  1193   ultimately show ?thesis by blast
  1194 qed
  1195 
  1196 text {* This is useful for building a basis step-by-step. *}
  1197 
  1198 lemma independent_insert:
  1199   "independent (insert a S) \<longleftrightarrow>
  1200     (if a \<in> S then independent S else independent S \<and> a \<notin> span S)"
  1201   (is "?lhs \<longleftrightarrow> ?rhs")
  1202 proof (cases "a \<in> S")
  1203   case True
  1204   then show ?thesis
  1205     using insert_absorb[OF True] by simp
  1206 next
  1207   case False
  1208   show ?thesis
  1209   proof
  1210     assume i: ?lhs
  1211     then show ?rhs
  1212       using False
  1213       apply simp
  1214       apply (rule conjI)
  1215       apply (rule independent_mono)
  1216       apply assumption
  1217       apply blast
  1218       apply (simp add: dependent_def)
  1219       done
  1220   next
  1221     assume i: ?rhs
  1222     show ?lhs
  1223       using i False
  1224       apply (auto simp add: dependent_def)
  1225       by (metis in_span_insert insert_Diff insert_Diff_if insert_iff)
  1226   qed
  1227 qed
  1228 
  1229 text {* The degenerate case of the Exchange Lemma. *}
  1230 
  1231 lemma spanning_subset_independent:
  1232   assumes BA: "B \<subseteq> A"
  1233     and iA: "independent A"
  1234     and AsB: "A \<subseteq> span B"
  1235   shows "A = B"
  1236 proof
  1237   show "B \<subseteq> A" by (rule BA)
  1238 
  1239   from span_mono[OF BA] span_mono[OF AsB]
  1240   have sAB: "span A = span B" unfolding span_span by blast
  1241 
  1242   {
  1243     fix x
  1244     assume x: "x \<in> A"
  1245     from iA have th0: "x \<notin> span (A - {x})"
  1246       unfolding dependent_def using x by blast
  1247     from x have xsA: "x \<in> span A"
  1248       by (blast intro: span_superset)
  1249     have "A - {x} \<subseteq> A" by blast
  1250     then have th1: "span (A - {x}) \<subseteq> span A"
  1251       by (metis span_mono)
  1252     {
  1253       assume xB: "x \<notin> B"
  1254       from xB BA have "B \<subseteq> A - {x}"
  1255         by blast
  1256       then have "span B \<subseteq> span (A - {x})"
  1257         by (metis span_mono)
  1258       with th1 th0 sAB have "x \<notin> span A"
  1259         by blast
  1260       with x have False
  1261         by (metis span_superset)
  1262     }
  1263     then have "x \<in> B" by blast
  1264   }
  1265   then show "A \<subseteq> B" by blast
  1266 qed
  1267 
  1268 text {* The general case of the Exchange Lemma, the key to what follows. *}
  1269 
  1270 lemma exchange_lemma:
  1271   assumes f:"finite t"
  1272     and i: "independent s"
  1273     and sp: "s \<subseteq> span t"
  1274   shows "\<exists>t'. card t' = card t \<and> finite t' \<and> s \<subseteq> t' \<and> t' \<subseteq> s \<union> t \<and> s \<subseteq> span t'"
  1275   using f i sp
  1276 proof (induct "card (t - s)" arbitrary: s t rule: less_induct)
  1277   case less
  1278   note ft = `finite t` and s = `independent s` and sp = `s \<subseteq> span t`
  1279   let ?P = "\<lambda>t'. card t' = card t \<and> finite t' \<and> s \<subseteq> t' \<and> t' \<subseteq> s \<union> t \<and> s \<subseteq> span t'"
  1280   let ?ths = "\<exists>t'. ?P t'"
  1281   {
  1282     assume "s \<subseteq> t"
  1283     then have ?ths
  1284       by (metis ft Un_commute sp sup_ge1)
  1285   }
  1286   moreover
  1287   {
  1288     assume st: "t \<subseteq> s"
  1289     from spanning_subset_independent[OF st s sp] st ft span_mono[OF st]
  1290     have ?ths
  1291       by (metis Un_absorb sp)
  1292   }
  1293   moreover
  1294   {
  1295     assume st: "\<not> s \<subseteq> t" "\<not> t \<subseteq> s"
  1296     from st(2) obtain b where b: "b \<in> t" "b \<notin> s"
  1297       by blast
  1298     from b have "t - {b} - s \<subset> t - s"
  1299       by blast
  1300     then have cardlt: "card (t - {b} - s) < card (t - s)"
  1301       using ft by (auto intro: psubset_card_mono)
  1302     from b ft have ct0: "card t \<noteq> 0"
  1303       by auto
  1304     have ?ths
  1305     proof cases
  1306       assume stb: "s \<subseteq> span (t - {b})"
  1307       from ft have ftb: "finite (t - {b})"
  1308         by auto
  1309       from less(1)[OF cardlt ftb s stb]
  1310       obtain u where u: "card u = card (t - {b})" "s \<subseteq> u" "u \<subseteq> s \<union> (t - {b})" "s \<subseteq> span u"
  1311         and fu: "finite u" by blast
  1312       let ?w = "insert b u"
  1313       have th0: "s \<subseteq> insert b u"
  1314         using u by blast
  1315       from u(3) b have "u \<subseteq> s \<union> t"
  1316         by blast
  1317       then have th1: "insert b u \<subseteq> s \<union> t"
  1318         using u b by blast
  1319       have bu: "b \<notin> u"
  1320         using b u by blast
  1321       from u(1) ft b have "card u = (card t - 1)"
  1322         by auto
  1323       then have th2: "card (insert b u) = card t"
  1324         using card_insert_disjoint[OF fu bu] ct0 by auto
  1325       from u(4) have "s \<subseteq> span u" .
  1326       also have "\<dots> \<subseteq> span (insert b u)"
  1327         by (rule span_mono) blast
  1328       finally have th3: "s \<subseteq> span (insert b u)" .
  1329       from th0 th1 th2 th3 fu have th: "?P ?w"
  1330         by blast
  1331       from th show ?thesis by blast
  1332     next
  1333       assume stb: "\<not> s \<subseteq> span (t - {b})"
  1334       from stb obtain a where a: "a \<in> s" "a \<notin> span (t - {b})"
  1335         by blast
  1336       have ab: "a \<noteq> b"
  1337         using a b by blast
  1338       have at: "a \<notin> t"
  1339         using a ab span_superset[of a "t- {b}"] by auto
  1340       have mlt: "card ((insert a (t - {b})) - s) < card (t - s)"
  1341         using cardlt ft a b by auto
  1342       have ft': "finite (insert a (t - {b}))"
  1343         using ft by auto
  1344       {
  1345         fix x
  1346         assume xs: "x \<in> s"
  1347         have t: "t \<subseteq> insert b (insert a (t - {b}))"
  1348           using b by auto
  1349         from b(1) have "b \<in> span t"
  1350           by (simp add: span_superset)
  1351         have bs: "b \<in> span (insert a (t - {b}))"
  1352           apply (rule in_span_delete)
  1353           using a sp unfolding subset_eq
  1354           apply auto
  1355           done
  1356         from xs sp have "x \<in> span t"
  1357           by blast
  1358         with span_mono[OF t] have x: "x \<in> span (insert b (insert a (t - {b})))" ..
  1359         from span_trans[OF bs x] have "x \<in> span (insert a (t - {b}))" .
  1360       }
  1361       then have sp': "s \<subseteq> span (insert a (t - {b}))"
  1362         by blast
  1363       from less(1)[OF mlt ft' s sp'] obtain u where u:
  1364         "card u = card (insert a (t - {b}))"
  1365         "finite u" "s \<subseteq> u" "u \<subseteq> s \<union> insert a (t - {b})"
  1366         "s \<subseteq> span u" by blast
  1367       from u a b ft at ct0 have "?P u"
  1368         by auto
  1369       then show ?thesis by blast
  1370     qed
  1371   }
  1372   ultimately show ?ths by blast
  1373 qed
  1374 
  1375 text {* This implies corresponding size bounds. *}
  1376 
  1377 lemma independent_span_bound:
  1378   assumes f: "finite t"
  1379     and i: "independent s"
  1380     and sp: "s \<subseteq> span t"
  1381   shows "finite s \<and> card s \<le> card t"
  1382   by (metis exchange_lemma[OF f i sp] finite_subset card_mono)
  1383 
  1384 lemma finite_Atleast_Atmost_nat[simp]: "finite {f x |x. x\<in> (UNIV::'a::finite set)}"
  1385 proof -
  1386   have eq: "{f x |x. x\<in> UNIV} = f ` UNIV"
  1387     by auto
  1388   show ?thesis unfolding eq
  1389     apply (rule finite_imageI)
  1390     apply (rule finite)
  1391     done
  1392 qed
  1393 
  1394 
  1395 subsection {* Euclidean Spaces as Typeclass *}
  1396 
  1397 lemma independent_Basis: "independent Basis"
  1398   unfolding dependent_def
  1399   apply (subst span_finite)
  1400   apply simp
  1401   apply clarify
  1402   apply (drule_tac f="inner a" in arg_cong)
  1403   apply (simp add: inner_Basis inner_setsum_right eq_commute)
  1404   done
  1405 
  1406 lemma span_Basis [simp]: "span Basis = UNIV"
  1407   unfolding span_finite [OF finite_Basis]
  1408   by (fast intro: euclidean_representation)
  1409 
  1410 lemma in_span_Basis: "x \<in> span Basis"
  1411   unfolding span_Basis ..
  1412 
  1413 lemma Basis_le_norm: "b \<in> Basis \<Longrightarrow> \<bar>x \<bullet> b\<bar> \<le> norm x"
  1414   by (rule order_trans [OF Cauchy_Schwarz_ineq2]) simp
  1415 
  1416 lemma norm_bound_Basis_le: "b \<in> Basis \<Longrightarrow> norm x \<le> e \<Longrightarrow> \<bar>x \<bullet> b\<bar> \<le> e"
  1417   by (metis Basis_le_norm order_trans)
  1418 
  1419 lemma norm_bound_Basis_lt: "b \<in> Basis \<Longrightarrow> norm x < e \<Longrightarrow> \<bar>x \<bullet> b\<bar> < e"
  1420   by (metis Basis_le_norm le_less_trans)
  1421 
  1422 lemma norm_le_l1: "norm x \<le> (\<Sum>b\<in>Basis. \<bar>x \<bullet> b\<bar>)"
  1423   apply (subst euclidean_representation[of x, symmetric])
  1424   apply (rule order_trans[OF norm_setsum])
  1425   apply (auto intro!: setsum_mono)
  1426   done
  1427 
  1428 lemma setsum_norm_allsubsets_bound:
  1429   fixes f:: "'a \<Rightarrow> 'n::euclidean_space"
  1430   assumes fP: "finite P"
  1431     and fPs: "\<And>Q. Q \<subseteq> P \<Longrightarrow> norm (setsum f Q) \<le> e"
  1432   shows "(\<Sum>x\<in>P. norm (f x)) \<le> 2 * real DIM('n) * e"
  1433 proof -
  1434   have "(\<Sum>x\<in>P. norm (f x)) \<le> (\<Sum>x\<in>P. \<Sum>b\<in>Basis. \<bar>f x \<bullet> b\<bar>)"
  1435     by (rule setsum_mono) (rule norm_le_l1)
  1436   also have "(\<Sum>x\<in>P. \<Sum>b\<in>Basis. \<bar>f x \<bullet> b\<bar>) = (\<Sum>b\<in>Basis. \<Sum>x\<in>P. \<bar>f x \<bullet> b\<bar>)"
  1437     by (rule setsum_commute)
  1438   also have "\<dots> \<le> of_nat (card (Basis :: 'n set)) * (2 * e)"
  1439   proof (rule setsum_bounded)
  1440     fix i :: 'n
  1441     assume i: "i \<in> Basis"
  1442     have "norm (\<Sum>x\<in>P. \<bar>f x \<bullet> i\<bar>) \<le>
  1443       norm ((\<Sum>x\<in>P \<inter> - {x. f x \<bullet> i < 0}. f x) \<bullet> i) + norm ((\<Sum>x\<in>P \<inter> {x. f x \<bullet> i < 0}. f x) \<bullet> i)"
  1444       by (simp add: abs_real_def setsum_cases[OF fP] setsum_negf norm_triangle_ineq4 inner_setsum_left del: real_norm_def)
  1445     also have "\<dots> \<le> e + e"
  1446       unfolding real_norm_def
  1447       by (intro add_mono norm_bound_Basis_le i fPs) auto
  1448     finally show "(\<Sum>x\<in>P. \<bar>f x \<bullet> i\<bar>) \<le> 2*e" by simp
  1449   qed
  1450   also have "\<dots> = 2 * real DIM('n) * e"
  1451     by (simp add: real_of_nat_def)
  1452   finally show ?thesis .
  1453 qed
  1454 
  1455 
  1456 subsection {* Linearity and Bilinearity continued *}
  1457 
  1458 lemma linear_bounded:
  1459   fixes f:: "'a::euclidean_space \<Rightarrow> 'b::real_normed_vector"
  1460   assumes lf: "linear f"
  1461   shows "\<exists>B. \<forall>x. norm (f x) \<le> B * norm x"
  1462 proof
  1463   let ?B = "\<Sum>b\<in>Basis. norm (f b)"
  1464   show "\<forall>x. norm (f x) \<le> ?B * norm x"
  1465   proof
  1466     fix x :: 'a
  1467     let ?g = "\<lambda>b. (x \<bullet> b) *\<^sub>R f b"
  1468     have "norm (f x) = norm (f (\<Sum>b\<in>Basis. (x \<bullet> b) *\<^sub>R b))"
  1469       unfolding euclidean_representation ..
  1470     also have "\<dots> = norm (setsum ?g Basis)"
  1471       by (simp add: linear_setsum [OF lf] linear_cmul [OF lf])
  1472     finally have th0: "norm (f x) = norm (setsum ?g Basis)" .
  1473     have th: "\<forall>b\<in>Basis. norm (?g b) \<le> norm (f b) * norm x"
  1474     proof
  1475       fix i :: 'a
  1476       assume i: "i \<in> Basis"
  1477       from Basis_le_norm[OF i, of x]
  1478       show "norm (?g i) \<le> norm (f i) * norm x"
  1479         unfolding norm_scaleR
  1480         apply (subst mult_commute)
  1481         apply (rule mult_mono)
  1482         apply (auto simp add: field_simps)
  1483         done
  1484     qed
  1485     from setsum_norm_le[of _ ?g, OF th]
  1486     show "norm (f x) \<le> ?B * norm x"
  1487       unfolding th0 setsum_left_distrib by metis
  1488   qed
  1489 qed
  1490 
  1491 lemma linear_conv_bounded_linear:
  1492   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::real_normed_vector"
  1493   shows "linear f \<longleftrightarrow> bounded_linear f"
  1494 proof
  1495   assume "linear f"
  1496   then interpret f: linear f .
  1497   show "bounded_linear f"
  1498   proof
  1499     have "\<exists>B. \<forall>x. norm (f x) \<le> B * norm x"
  1500       using `linear f` by (rule linear_bounded)
  1501     then show "\<exists>K. \<forall>x. norm (f x) \<le> norm x * K"
  1502       by (simp add: mult_commute)
  1503   qed
  1504 next
  1505   assume "bounded_linear f"
  1506   then interpret f: bounded_linear f .
  1507   show "linear f" ..
  1508 qed
  1509 
  1510 lemma linear_bounded_pos:
  1511   fixes f:: "'a::euclidean_space \<Rightarrow> 'b::real_normed_vector"
  1512   assumes lf: "linear f"
  1513   shows "\<exists>B > 0. \<forall>x. norm (f x) \<le> B * norm x"
  1514 proof -
  1515   have "\<exists>B > 0. \<forall>x. norm (f x) \<le> norm x * B"
  1516     using lf unfolding linear_conv_bounded_linear
  1517     by (rule bounded_linear.pos_bounded)
  1518   then show ?thesis
  1519     by (simp only: mult_commute)
  1520 qed
  1521 
  1522 lemma bounded_linearI':
  1523   fixes f::"'a::euclidean_space \<Rightarrow> 'b::real_normed_vector"
  1524   assumes "\<And>x y. f (x + y) = f x + f y"
  1525     and "\<And>c x. f (c *\<^sub>R x) = c *\<^sub>R f x"
  1526   shows "bounded_linear f"
  1527   unfolding linear_conv_bounded_linear[symmetric]
  1528   by (rule linearI[OF assms])
  1529 
  1530 lemma bilinear_bounded:
  1531   fixes h:: "'m::euclidean_space \<Rightarrow> 'n::euclidean_space \<Rightarrow> 'k::real_normed_vector"
  1532   assumes bh: "bilinear h"
  1533   shows "\<exists>B. \<forall>x y. norm (h x y) \<le> B * norm x * norm y"
  1534 proof (clarify intro!: exI[of _ "\<Sum>i\<in>Basis. \<Sum>j\<in>Basis. norm (h i j)"])
  1535   fix x :: 'm
  1536   fix y :: 'n
  1537   have "norm (h x y) = norm (h (setsum (\<lambda>i. (x \<bullet> i) *\<^sub>R i) Basis) (setsum (\<lambda>i. (y \<bullet> i) *\<^sub>R i) Basis))"
  1538     apply (subst euclidean_representation[where 'a='m])
  1539     apply (subst euclidean_representation[where 'a='n])
  1540     apply rule
  1541     done
  1542   also have "\<dots> = norm (setsum (\<lambda> (i,j). h ((x \<bullet> i) *\<^sub>R i) ((y \<bullet> j) *\<^sub>R j)) (Basis \<times> Basis))"
  1543     unfolding bilinear_setsum[OF bh finite_Basis finite_Basis] ..
  1544   finally have th: "norm (h x y) = \<dots>" .
  1545   show "norm (h x y) \<le> (\<Sum>i\<in>Basis. \<Sum>j\<in>Basis. norm (h i j)) * norm x * norm y"
  1546     apply (auto simp add: setsum_left_distrib th setsum_cartesian_product)
  1547     apply (rule setsum_norm_le)
  1548     apply simp
  1549     apply (auto simp add: bilinear_rmul[OF bh] bilinear_lmul[OF bh]
  1550       field_simps simp del: scaleR_scaleR)
  1551     apply (rule mult_mono)
  1552     apply (auto simp add: zero_le_mult_iff Basis_le_norm)
  1553     apply (rule mult_mono)
  1554     apply (auto simp add: zero_le_mult_iff Basis_le_norm)
  1555     done
  1556 qed
  1557 
  1558 lemma bilinear_conv_bounded_bilinear:
  1559   fixes h :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space \<Rightarrow> 'c::real_normed_vector"
  1560   shows "bilinear h \<longleftrightarrow> bounded_bilinear h"
  1561 proof
  1562   assume "bilinear h"
  1563   show "bounded_bilinear h"
  1564   proof
  1565     fix x y z
  1566     show "h (x + y) z = h x z + h y z"
  1567       using `bilinear h` unfolding bilinear_def linear_iff by simp
  1568   next
  1569     fix x y z
  1570     show "h x (y + z) = h x y + h x z"
  1571       using `bilinear h` unfolding bilinear_def linear_iff by simp
  1572   next
  1573     fix r x y
  1574     show "h (scaleR r x) y = scaleR r (h x y)"
  1575       using `bilinear h` unfolding bilinear_def linear_iff
  1576       by simp
  1577   next
  1578     fix r x y
  1579     show "h x (scaleR r y) = scaleR r (h x y)"
  1580       using `bilinear h` unfolding bilinear_def linear_iff
  1581       by simp
  1582   next
  1583     have "\<exists>B. \<forall>x y. norm (h x y) \<le> B * norm x * norm y"
  1584       using `bilinear h` by (rule bilinear_bounded)
  1585     then show "\<exists>K. \<forall>x y. norm (h x y) \<le> norm x * norm y * K"
  1586       by (simp add: mult_ac)
  1587   qed
  1588 next
  1589   assume "bounded_bilinear h"
  1590   then interpret h: bounded_bilinear h .
  1591   show "bilinear h"
  1592     unfolding bilinear_def linear_conv_bounded_linear
  1593     using h.bounded_linear_left h.bounded_linear_right by simp
  1594 qed
  1595 
  1596 lemma bilinear_bounded_pos:
  1597   fixes h:: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space \<Rightarrow> 'c::real_normed_vector"
  1598   assumes bh: "bilinear h"
  1599   shows "\<exists>B > 0. \<forall>x y. norm (h x y) \<le> B * norm x * norm y"
  1600 proof -
  1601   have "\<exists>B > 0. \<forall>x y. norm (h x y) \<le> norm x * norm y * B"
  1602     using bh [unfolded bilinear_conv_bounded_bilinear]
  1603     by (rule bounded_bilinear.pos_bounded)
  1604   then show ?thesis
  1605     by (simp only: mult_ac)
  1606 qed
  1607 
  1608 
  1609 subsection {* We continue. *}
  1610 
  1611 lemma independent_bound:
  1612   fixes S :: "'a::euclidean_space set"
  1613   shows "independent S \<Longrightarrow> finite S \<and> card S \<le> DIM('a)"
  1614   using independent_span_bound[OF finite_Basis, of S] by auto
  1615 
  1616 lemma dependent_biggerset:
  1617   "(finite (S::('a::euclidean_space) set) \<Longrightarrow> card S > DIM('a)) \<Longrightarrow> dependent S"
  1618   by (metis independent_bound not_less)
  1619 
  1620 text {* Hence we can create a maximal independent subset. *}
  1621 
  1622 lemma maximal_independent_subset_extend:
  1623   fixes S :: "'a::euclidean_space set"
  1624   assumes sv: "S \<subseteq> V"
  1625     and iS: "independent S"
  1626   shows "\<exists>B. S \<subseteq> B \<and> B \<subseteq> V \<and> independent B \<and> V \<subseteq> span B"
  1627   using sv iS
  1628 proof (induct "DIM('a) - card S" arbitrary: S rule: less_induct)
  1629   case less
  1630   note sv = `S \<subseteq> V` and i = `independent S`
  1631   let ?P = "\<lambda>B. S \<subseteq> B \<and> B \<subseteq> V \<and> independent B \<and> V \<subseteq> span B"
  1632   let ?ths = "\<exists>x. ?P x"
  1633   let ?d = "DIM('a)"
  1634   show ?ths
  1635   proof (cases "V \<subseteq> span S")
  1636     case True
  1637     then show ?thesis
  1638       using sv i by blast
  1639   next
  1640     case False
  1641     then obtain a where a: "a \<in> V" "a \<notin> span S"
  1642       by blast
  1643     from a have aS: "a \<notin> S"
  1644       by (auto simp add: span_superset)
  1645     have th0: "insert a S \<subseteq> V"
  1646       using a sv by blast
  1647     from independent_insert[of a S]  i a
  1648     have th1: "independent (insert a S)"
  1649       by auto
  1650     have mlt: "?d - card (insert a S) < ?d - card S"
  1651       using aS a independent_bound[OF th1] by auto
  1652 
  1653     from less(1)[OF mlt th0 th1]
  1654     obtain B where B: "insert a S \<subseteq> B" "B \<subseteq> V" "independent B" " V \<subseteq> span B"
  1655       by blast
  1656     from B have "?P B" by auto
  1657     then show ?thesis by blast
  1658   qed
  1659 qed
  1660 
  1661 lemma maximal_independent_subset:
  1662   "\<exists>(B:: ('a::euclidean_space) set). B\<subseteq> V \<and> independent B \<and> V \<subseteq> span B"
  1663   by (metis maximal_independent_subset_extend[of "{}:: ('a::euclidean_space) set"]
  1664     empty_subsetI independent_empty)
  1665 
  1666 
  1667 text {* Notion of dimension. *}
  1668 
  1669 definition "dim V = (SOME n. \<exists>B. B \<subseteq> V \<and> independent B \<and> V \<subseteq> span B \<and> card B = n)"
  1670 
  1671 lemma basis_exists:
  1672   "\<exists>B. (B :: ('a::euclidean_space) set) \<subseteq> V \<and> independent B \<and> V \<subseteq> span B \<and> (card B = dim V)"
  1673   unfolding dim_def some_eq_ex[of "\<lambda>n. \<exists>B. B \<subseteq> V \<and> independent B \<and> V \<subseteq> span B \<and> (card B = n)"]
  1674   using maximal_independent_subset[of V] independent_bound
  1675   by auto
  1676 
  1677 text {* Consequences of independence or spanning for cardinality. *}
  1678 
  1679 lemma independent_card_le_dim:
  1680   fixes B :: "'a::euclidean_space set"
  1681   assumes "B \<subseteq> V"
  1682     and "independent B"
  1683   shows "card B \<le> dim V"
  1684 proof -
  1685   from basis_exists[of V] `B \<subseteq> V`
  1686   obtain B' where "independent B'"
  1687     and "B \<subseteq> span B'"
  1688     and "card B' = dim V"
  1689     by blast
  1690   with independent_span_bound[OF _ `independent B` `B \<subseteq> span B'`] independent_bound[of B']
  1691   show ?thesis by auto
  1692 qed
  1693 
  1694 lemma span_card_ge_dim:
  1695   fixes B :: "'a::euclidean_space set"
  1696   shows "B \<subseteq> V \<Longrightarrow> V \<subseteq> span B \<Longrightarrow> finite B \<Longrightarrow> dim V \<le> card B"
  1697   by (metis basis_exists[of V] independent_span_bound subset_trans)
  1698 
  1699 lemma basis_card_eq_dim:
  1700   fixes V :: "'a::euclidean_space set"
  1701   shows "B \<subseteq> V \<Longrightarrow> V \<subseteq> span B \<Longrightarrow> independent B \<Longrightarrow> finite B \<and> card B = dim V"
  1702   by (metis order_eq_iff independent_card_le_dim span_card_ge_dim independent_bound)
  1703 
  1704 lemma dim_unique:
  1705   fixes B :: "'a::euclidean_space set"
  1706   shows "B \<subseteq> V \<Longrightarrow> V \<subseteq> span B \<Longrightarrow> independent B \<Longrightarrow> card B = n \<Longrightarrow> dim V = n"
  1707   by (metis basis_card_eq_dim)
  1708 
  1709 text {* More lemmas about dimension. *}
  1710 
  1711 lemma dim_UNIV: "dim (UNIV :: 'a::euclidean_space set) = DIM('a)"
  1712   using independent_Basis
  1713   by (intro dim_unique[of Basis]) auto
  1714 
  1715 lemma dim_subset:
  1716   fixes S :: "'a::euclidean_space set"
  1717   shows "S \<subseteq> T \<Longrightarrow> dim S \<le> dim T"
  1718   using basis_exists[of T] basis_exists[of S]
  1719   by (metis independent_card_le_dim subset_trans)
  1720 
  1721 lemma dim_subset_UNIV:
  1722   fixes S :: "'a::euclidean_space set"
  1723   shows "dim S \<le> DIM('a)"
  1724   by (metis dim_subset subset_UNIV dim_UNIV)
  1725 
  1726 text {* Converses to those. *}
  1727 
  1728 lemma card_ge_dim_independent:
  1729   fixes B :: "'a::euclidean_space set"
  1730   assumes BV: "B \<subseteq> V"
  1731     and iB: "independent B"
  1732     and dVB: "dim V \<le> card B"
  1733   shows "V \<subseteq> span B"
  1734 proof
  1735   fix a
  1736   assume aV: "a \<in> V"
  1737   {
  1738     assume aB: "a \<notin> span B"
  1739     then have iaB: "independent (insert a B)"
  1740       using iB aV BV by (simp add: independent_insert)
  1741     from aV BV have th0: "insert a B \<subseteq> V"
  1742       by blast
  1743     from aB have "a \<notin>B"
  1744       by (auto simp add: span_superset)
  1745     with independent_card_le_dim[OF th0 iaB] dVB independent_bound[OF iB]
  1746     have False by auto
  1747   }
  1748   then show "a \<in> span B" by blast
  1749 qed
  1750 
  1751 lemma card_le_dim_spanning:
  1752   assumes BV: "(B:: ('a::euclidean_space) set) \<subseteq> V"
  1753     and VB: "V \<subseteq> span B"
  1754     and fB: "finite B"
  1755     and dVB: "dim V \<ge> card B"
  1756   shows "independent B"
  1757 proof -
  1758   {
  1759     fix a
  1760     assume a: "a \<in> B" "a \<in> span (B - {a})"
  1761     from a fB have c0: "card B \<noteq> 0"
  1762       by auto
  1763     from a fB have cb: "card (B - {a}) = card B - 1"
  1764       by auto
  1765     from BV a have th0: "B - {a} \<subseteq> V"
  1766       by blast
  1767     {
  1768       fix x
  1769       assume x: "x \<in> V"
  1770       from a have eq: "insert a (B - {a}) = B"
  1771         by blast
  1772       from x VB have x': "x \<in> span B"
  1773         by blast
  1774       from span_trans[OF a(2), unfolded eq, OF x']
  1775       have "x \<in> span (B - {a})" .
  1776     }
  1777     then have th1: "V \<subseteq> span (B - {a})"
  1778       by blast
  1779     have th2: "finite (B - {a})"
  1780       using fB by auto
  1781     from span_card_ge_dim[OF th0 th1 th2]
  1782     have c: "dim V \<le> card (B - {a})" .
  1783     from c c0 dVB cb have False by simp
  1784   }
  1785   then show ?thesis
  1786     unfolding dependent_def by blast
  1787 qed
  1788 
  1789 lemma card_eq_dim:
  1790   fixes B :: "'a::euclidean_space set"
  1791   shows "B \<subseteq> V \<Longrightarrow> card B = dim V \<Longrightarrow> finite B \<Longrightarrow> independent B \<longleftrightarrow> V \<subseteq> span B"
  1792   by (metis order_eq_iff card_le_dim_spanning card_ge_dim_independent)
  1793 
  1794 text {* More general size bound lemmas. *}
  1795 
  1796 lemma independent_bound_general:
  1797   fixes S :: "'a::euclidean_space set"
  1798   shows "independent S \<Longrightarrow> finite S \<and> card S \<le> dim S"
  1799   by (metis independent_card_le_dim independent_bound subset_refl)
  1800 
  1801 lemma dependent_biggerset_general:
  1802   fixes S :: "'a::euclidean_space set"
  1803   shows "(finite S \<Longrightarrow> card S > dim S) \<Longrightarrow> dependent S"
  1804   using independent_bound_general[of S] by (metis linorder_not_le)
  1805 
  1806 lemma dim_span:
  1807   fixes S :: "'a::euclidean_space set"
  1808   shows "dim (span S) = dim S"
  1809 proof -
  1810   have th0: "dim S \<le> dim (span S)"
  1811     by (auto simp add: subset_eq intro: dim_subset span_superset)
  1812   from basis_exists[of S]
  1813   obtain B where B: "B \<subseteq> S" "independent B" "S \<subseteq> span B" "card B = dim S"
  1814     by blast
  1815   from B have fB: "finite B" "card B = dim S"
  1816     using independent_bound by blast+
  1817   have bSS: "B \<subseteq> span S"
  1818     using B(1) by (metis subset_eq span_inc)
  1819   have sssB: "span S \<subseteq> span B"
  1820     using span_mono[OF B(3)] by (simp add: span_span)
  1821   from span_card_ge_dim[OF bSS sssB fB(1)] th0 show ?thesis
  1822     using fB(2) by arith
  1823 qed
  1824 
  1825 lemma subset_le_dim:
  1826   fixes S :: "'a::euclidean_space set"
  1827   shows "S \<subseteq> span T \<Longrightarrow> dim S \<le> dim T"
  1828   by (metis dim_span dim_subset)
  1829 
  1830 lemma span_eq_dim:
  1831   fixes S:: "'a::euclidean_space set"
  1832   shows "span S = span T \<Longrightarrow> dim S = dim T"
  1833   by (metis dim_span)
  1834 
  1835 lemma spans_image:
  1836   assumes lf: "linear f"
  1837     and VB: "V \<subseteq> span B"
  1838   shows "f ` V \<subseteq> span (f ` B)"
  1839   unfolding span_linear_image[OF lf] by (metis VB image_mono)
  1840 
  1841 lemma dim_image_le:
  1842   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  1843   assumes lf: "linear f"
  1844   shows "dim (f ` S) \<le> dim (S)"
  1845 proof -
  1846   from basis_exists[of S] obtain B where
  1847     B: "B \<subseteq> S" "independent B" "S \<subseteq> span B" "card B = dim S" by blast
  1848   from B have fB: "finite B" "card B = dim S"
  1849     using independent_bound by blast+
  1850   have "dim (f ` S) \<le> card (f ` B)"
  1851     apply (rule span_card_ge_dim)
  1852     using lf B fB
  1853     apply (auto simp add: span_linear_image spans_image subset_image_iff)
  1854     done
  1855   also have "\<dots> \<le> dim S"
  1856     using card_image_le[OF fB(1)] fB by simp
  1857   finally show ?thesis .
  1858 qed
  1859 
  1860 text {* Relation between bases and injectivity/surjectivity of map. *}
  1861 
  1862 lemma spanning_surjective_image:
  1863   assumes us: "UNIV \<subseteq> span S"
  1864     and lf: "linear f"
  1865     and sf: "surj f"
  1866   shows "UNIV \<subseteq> span (f ` S)"
  1867 proof -
  1868   have "UNIV \<subseteq> f ` UNIV"
  1869     using sf by (auto simp add: surj_def)
  1870   also have " \<dots> \<subseteq> span (f ` S)"
  1871     using spans_image[OF lf us] .
  1872   finally show ?thesis .
  1873 qed
  1874 
  1875 lemma independent_injective_image:
  1876   assumes iS: "independent S"
  1877     and lf: "linear f"
  1878     and fi: "inj f"
  1879   shows "independent (f ` S)"
  1880 proof -
  1881   {
  1882     fix a
  1883     assume a: "a \<in> S" "f a \<in> span (f ` S - {f a})"
  1884     have eq: "f ` S - {f a} = f ` (S - {a})"
  1885       using fi by (auto simp add: inj_on_def)
  1886     from a have "f a \<in> f ` span (S - {a})"
  1887       unfolding eq span_linear_image[OF lf, of "S - {a}"] by blast
  1888     then have "a \<in> span (S - {a})"
  1889       using fi by (auto simp add: inj_on_def)
  1890     with a(1) iS have False
  1891       by (simp add: dependent_def)
  1892   }
  1893   then show ?thesis
  1894     unfolding dependent_def by blast
  1895 qed
  1896 
  1897 text {* Picking an orthogonal replacement for a spanning set. *}
  1898 
  1899 (* FIXME : Move to some general theory ?*)
  1900 definition "pairwise R S \<longleftrightarrow> (\<forall>x \<in> S. \<forall>y\<in> S. x\<noteq>y \<longrightarrow> R x y)"
  1901 
  1902 lemma vector_sub_project_orthogonal:
  1903   fixes b x :: "'a::euclidean_space"
  1904   shows "b \<bullet> (x - ((b \<bullet> x) / (b \<bullet> b)) *\<^sub>R b) = 0"
  1905   unfolding inner_simps by auto
  1906 
  1907 lemma pairwise_orthogonal_insert:
  1908   assumes "pairwise orthogonal S"
  1909     and "\<And>y. y \<in> S \<Longrightarrow> orthogonal x y"
  1910   shows "pairwise orthogonal (insert x S)"
  1911   using assms unfolding pairwise_def
  1912   by (auto simp add: orthogonal_commute)
  1913 
  1914 lemma basis_orthogonal:
  1915   fixes B :: "'a::real_inner set"
  1916   assumes fB: "finite B"
  1917   shows "\<exists>C. finite C \<and> card C \<le> card B \<and> span C = span B \<and> pairwise orthogonal C"
  1918   (is " \<exists>C. ?P B C")
  1919   using fB
  1920 proof (induct rule: finite_induct)
  1921   case empty
  1922   then show ?case
  1923     apply (rule exI[where x="{}"])
  1924     apply (auto simp add: pairwise_def)
  1925     done
  1926 next
  1927   case (insert a B)
  1928   note fB = `finite B` and aB = `a \<notin> B`
  1929   from `\<exists>C. finite C \<and> card C \<le> card B \<and> span C = span B \<and> pairwise orthogonal C`
  1930   obtain C where C: "finite C" "card C \<le> card B"
  1931     "span C = span B" "pairwise orthogonal C" by blast
  1932   let ?a = "a - setsum (\<lambda>x. (x \<bullet> a / (x \<bullet> x)) *\<^sub>R x) C"
  1933   let ?C = "insert ?a C"
  1934   from C(1) have fC: "finite ?C"
  1935     by simp
  1936   from fB aB C(1,2) have cC: "card ?C \<le> card (insert a B)"
  1937     by (simp add: card_insert_if)
  1938   {
  1939     fix x k
  1940     have th0: "\<And>(a::'a) b c. a - (b - c) = c + (a - b)"
  1941       by (simp add: field_simps)
  1942     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"
  1943       apply (simp only: scaleR_right_diff_distrib th0)
  1944       apply (rule span_add_eq)
  1945       apply (rule span_mul)
  1946       apply (rule span_setsum[OF C(1)])
  1947       apply clarify
  1948       apply (rule span_mul)
  1949       apply (rule span_superset)
  1950       apply assumption
  1951       done
  1952   }
  1953   then have SC: "span ?C = span (insert a B)"
  1954     unfolding set_eq_iff span_breakdown_eq C(3)[symmetric] by auto
  1955   {
  1956     fix y
  1957     assume yC: "y \<in> C"
  1958     then have Cy: "C = insert y (C - {y})"
  1959       by blast
  1960     have fth: "finite (C - {y})"
  1961       using C by simp
  1962     have "orthogonal ?a y"
  1963       unfolding orthogonal_def
  1964       unfolding inner_diff inner_setsum_left right_minus_eq
  1965       unfolding setsum_diff1' [OF `finite C` `y \<in> C`]
  1966       apply (clarsimp simp add: inner_commute[of y a])
  1967       apply (rule setsum_0')
  1968       apply clarsimp
  1969       apply (rule C(4)[unfolded pairwise_def orthogonal_def, rule_format])
  1970       using `y \<in> C` by auto
  1971   }
  1972   with `pairwise orthogonal C` have CPO: "pairwise orthogonal ?C"
  1973     by (rule pairwise_orthogonal_insert)
  1974   from fC cC SC CPO have "?P (insert a B) ?C"
  1975     by blast
  1976   then show ?case by blast
  1977 qed
  1978 
  1979 lemma orthogonal_basis_exists:
  1980   fixes V :: "('a::euclidean_space) set"
  1981   shows "\<exists>B. independent B \<and> B \<subseteq> span V \<and> V \<subseteq> span B \<and> (card B = dim V) \<and> pairwise orthogonal B"
  1982 proof -
  1983   from basis_exists[of V] obtain B where
  1984     B: "B \<subseteq> V" "independent B" "V \<subseteq> span B" "card B = dim V"
  1985     by blast
  1986   from B have fB: "finite B" "card B = dim V"
  1987     using independent_bound by auto
  1988   from basis_orthogonal[OF fB(1)] obtain C where
  1989     C: "finite C" "card C \<le> card B" "span C = span B" "pairwise orthogonal C"
  1990     by blast
  1991   from C B have CSV: "C \<subseteq> span V"
  1992     by (metis span_inc span_mono subset_trans)
  1993   from span_mono[OF B(3)] C have SVC: "span V \<subseteq> span C"
  1994     by (simp add: span_span)
  1995   from card_le_dim_spanning[OF CSV SVC C(1)] C(2,3) fB
  1996   have iC: "independent C"
  1997     by (simp add: dim_span)
  1998   from C fB have "card C \<le> dim V"
  1999     by simp
  2000   moreover have "dim V \<le> card C"
  2001     using span_card_ge_dim[OF CSV SVC C(1)]
  2002     by (simp add: dim_span)
  2003   ultimately have CdV: "card C = dim V"
  2004     using C(1) by simp
  2005   from C B CSV CdV iC show ?thesis
  2006     by auto
  2007 qed
  2008 
  2009 lemma span_eq: "span S = span T \<longleftrightarrow> S \<subseteq> span T \<and> T \<subseteq> span S"
  2010   using span_inc[unfolded subset_eq] using span_mono[of T "span S"] span_mono[of S "span T"]
  2011   by (auto simp add: span_span)
  2012 
  2013 text {* Low-dimensional subset is in a hyperplane (weak orthogonal complement). *}
  2014 
  2015 lemma span_not_univ_orthogonal:
  2016   fixes S :: "'a::euclidean_space set"
  2017   assumes sU: "span S \<noteq> UNIV"
  2018   shows "\<exists>(a::'a). a \<noteq>0 \<and> (\<forall>x \<in> span S. a \<bullet> x = 0)"
  2019 proof -
  2020   from sU obtain a where a: "a \<notin> span S"
  2021     by blast
  2022   from orthogonal_basis_exists obtain B where
  2023     B: "independent B" "B \<subseteq> span S" "S \<subseteq> span B" "card B = dim S" "pairwise orthogonal B"
  2024     by blast
  2025   from B have fB: "finite B" "card B = dim S"
  2026     using independent_bound by auto
  2027   from span_mono[OF B(2)] span_mono[OF B(3)]
  2028   have sSB: "span S = span B"
  2029     by (simp add: span_span)
  2030   let ?a = "a - setsum (\<lambda>b. (a \<bullet> b / (b \<bullet> b)) *\<^sub>R b) B"
  2031   have "setsum (\<lambda>b. (a \<bullet> b / (b \<bullet> b)) *\<^sub>R b) B \<in> span S"
  2032     unfolding sSB
  2033     apply (rule span_setsum[OF fB(1)])
  2034     apply clarsimp
  2035     apply (rule span_mul)
  2036     apply (rule span_superset)
  2037     apply assumption
  2038     done
  2039   with a have a0:"?a  \<noteq> 0"
  2040     by auto
  2041   have "\<forall>x\<in>span B. ?a \<bullet> x = 0"
  2042   proof (rule span_induct')
  2043     show "subspace {x. ?a \<bullet> x = 0}"
  2044       by (auto simp add: subspace_def inner_add)
  2045   next
  2046     {
  2047       fix x
  2048       assume x: "x \<in> B"
  2049       from x have B': "B = insert x (B - {x})"
  2050         by blast
  2051       have fth: "finite (B - {x})"
  2052         using fB by simp
  2053       have "?a \<bullet> x = 0"
  2054         apply (subst B')
  2055         using fB fth
  2056         unfolding setsum_clauses(2)[OF fth]
  2057         apply simp unfolding inner_simps
  2058         apply (clarsimp simp add: inner_add inner_setsum_left)
  2059         apply (rule setsum_0', rule ballI)
  2060         unfolding inner_commute
  2061         apply (auto simp add: x field_simps
  2062           intro: B(5)[unfolded pairwise_def orthogonal_def, rule_format])
  2063         done
  2064     }
  2065     then show "\<forall>x \<in> B. ?a \<bullet> x = 0"
  2066       by blast
  2067   qed
  2068   with a0 show ?thesis
  2069     unfolding sSB by (auto intro: exI[where x="?a"])
  2070 qed
  2071 
  2072 lemma span_not_univ_subset_hyperplane:
  2073   fixes S :: "'a::euclidean_space set"
  2074   assumes SU: "span S \<noteq> UNIV"
  2075   shows "\<exists> a. a \<noteq>0 \<and> span S \<subseteq> {x. a \<bullet> x = 0}"
  2076   using span_not_univ_orthogonal[OF SU] by auto
  2077 
  2078 lemma lowdim_subset_hyperplane:
  2079   fixes S :: "'a::euclidean_space set"
  2080   assumes d: "dim S < DIM('a)"
  2081   shows "\<exists>(a::'a). a  \<noteq> 0 \<and> span S \<subseteq> {x. a \<bullet> x = 0}"
  2082 proof -
  2083   {
  2084     assume "span S = UNIV"
  2085     then have "dim (span S) = dim (UNIV :: ('a) set)"
  2086       by simp
  2087     then have "dim S = DIM('a)"
  2088       by (simp add: dim_span dim_UNIV)
  2089     with d have False by arith
  2090   }
  2091   then have th: "span S \<noteq> UNIV"
  2092     by blast
  2093   from span_not_univ_subset_hyperplane[OF th] show ?thesis .
  2094 qed
  2095 
  2096 text {* We can extend a linear basis-basis injection to the whole set. *}
  2097 
  2098 lemma linear_indep_image_lemma:
  2099   assumes lf: "linear f"
  2100     and fB: "finite B"
  2101     and ifB: "independent (f ` B)"
  2102     and fi: "inj_on f B"
  2103     and xsB: "x \<in> span B"
  2104     and fx: "f x = 0"
  2105   shows "x = 0"
  2106   using fB ifB fi xsB fx
  2107 proof (induct arbitrary: x rule: finite_induct[OF fB])
  2108   case 1
  2109   then show ?case by auto
  2110 next
  2111   case (2 a b x)
  2112   have fb: "finite b" using "2.prems" by simp
  2113   have th0: "f ` b \<subseteq> f ` (insert a b)"
  2114     apply (rule image_mono)
  2115     apply blast
  2116     done
  2117   from independent_mono[ OF "2.prems"(2) th0]
  2118   have ifb: "independent (f ` b)"  .
  2119   have fib: "inj_on f b"
  2120     apply (rule subset_inj_on [OF "2.prems"(3)])
  2121     apply blast
  2122     done
  2123   from span_breakdown[of a "insert a b", simplified, OF "2.prems"(4)]
  2124   obtain k where k: "x - k*\<^sub>R a \<in> span (b - {a})"
  2125     by blast
  2126   have "f (x - k*\<^sub>R a) \<in> span (f ` b)"
  2127     unfolding span_linear_image[OF lf]
  2128     apply (rule imageI)
  2129     using k span_mono[of "b - {a}" b]
  2130     apply blast
  2131     done
  2132   then have "f x - k*\<^sub>R f a \<in> span (f ` b)"
  2133     by (simp add: linear_sub[OF lf] linear_cmul[OF lf])
  2134   then have th: "-k *\<^sub>R f a \<in> span (f ` b)"
  2135     using "2.prems"(5) by simp
  2136   have xsb: "x \<in> span b"
  2137   proof (cases "k = 0")
  2138     case True
  2139     with k have "x \<in> span (b - {a})" by simp
  2140     then show ?thesis using span_mono[of "b - {a}" b]
  2141       by blast
  2142   next
  2143     case False
  2144     with span_mul[OF th, of "- 1/ k"]
  2145     have th1: "f a \<in> span (f ` b)"
  2146       by auto
  2147     from inj_on_image_set_diff[OF "2.prems"(3), of "insert a b " "{a}", symmetric]
  2148     have tha: "f ` insert a b - f ` {a} = f ` (insert a b - {a})" by blast
  2149     from "2.prems"(2) [unfolded dependent_def bex_simps(8), rule_format, of "f a"]
  2150     have "f a \<notin> span (f ` b)" using tha
  2151       using "2.hyps"(2)
  2152       "2.prems"(3) by auto
  2153     with th1 have False by blast
  2154     then show ?thesis by blast
  2155   qed
  2156   from "2.hyps"(3)[OF fb ifb fib xsb "2.prems"(5)] show "x = 0" .
  2157 qed
  2158 
  2159 text {* We can extend a linear mapping from basis. *}
  2160 
  2161 lemma linear_independent_extend_lemma:
  2162   fixes f :: "'a::real_vector \<Rightarrow> 'b::real_vector"
  2163   assumes fi: "finite B"
  2164     and ib: "independent B"
  2165   shows "\<exists>g.
  2166     (\<forall>x\<in> span B. \<forall>y\<in> span B. g (x + y) = g x + g y) \<and>
  2167     (\<forall>x\<in> span B. \<forall>c. g (c*\<^sub>R x) = c *\<^sub>R g x) \<and>
  2168     (\<forall>x\<in> B. g x = f x)"
  2169   using ib fi
  2170 proof (induct rule: finite_induct[OF fi])
  2171   case 1
  2172   then show ?case by auto
  2173 next
  2174   case (2 a b)
  2175   from "2.prems" "2.hyps" have ibf: "independent b" "finite b"
  2176     by (simp_all add: independent_insert)
  2177   from "2.hyps"(3)[OF ibf] obtain g where
  2178     g: "\<forall>x\<in>span b. \<forall>y\<in>span b. g (x + y) = g x + g y"
  2179     "\<forall>x\<in>span b. \<forall>c. g (c *\<^sub>R x) = c *\<^sub>R g x" "\<forall>x\<in>b. g x = f x" by blast
  2180   let ?h = "\<lambda>z. SOME k. (z - k *\<^sub>R a) \<in> span b"
  2181   {
  2182     fix z
  2183     assume z: "z \<in> span (insert a b)"
  2184     have th0: "z - ?h z *\<^sub>R a \<in> span b"
  2185       apply (rule someI_ex)
  2186       unfolding span_breakdown_eq[symmetric]
  2187       apply (rule z)
  2188       done
  2189     {
  2190       fix k
  2191       assume k: "z - k *\<^sub>R a \<in> span b"
  2192       have eq: "z - ?h z *\<^sub>R a - (z - k*\<^sub>R a) = (k - ?h z) *\<^sub>R a"
  2193         by (simp add: field_simps scaleR_left_distrib [symmetric])
  2194       from span_sub[OF th0 k] have khz: "(k - ?h z) *\<^sub>R a \<in> span b"
  2195         by (simp add: eq)
  2196       {
  2197         assume "k \<noteq> ?h z"
  2198         then have k0: "k - ?h z \<noteq> 0" by simp
  2199         from k0 span_mul[OF khz, of "1 /(k - ?h z)"]
  2200         have "a \<in> span b" by simp
  2201         with "2.prems"(1) "2.hyps"(2) have False
  2202           by (auto simp add: dependent_def)
  2203       }
  2204       then have "k = ?h z" by blast
  2205     }
  2206     with th0 have "z - ?h z *\<^sub>R a \<in> span b \<and> (\<forall>k. z - k *\<^sub>R a \<in> span b \<longrightarrow> k = ?h z)"
  2207       by blast
  2208   }
  2209   note h = this
  2210   let ?g = "\<lambda>z. ?h z *\<^sub>R f a + g (z - ?h z *\<^sub>R a)"
  2211   {
  2212     fix x y
  2213     assume x: "x \<in> span (insert a b)"
  2214       and y: "y \<in> span (insert a b)"
  2215     have tha: "\<And>(x::'a) y a k l. (x + y) - (k + l) *\<^sub>R a = (x - k *\<^sub>R a) + (y - l *\<^sub>R a)"
  2216       by (simp add: algebra_simps)
  2217     have addh: "?h (x + y) = ?h x + ?h y"
  2218       apply (rule conjunct2[OF h, rule_format, symmetric])
  2219       apply (rule span_add[OF x y])
  2220       unfolding tha
  2221       apply (metis span_add x y conjunct1[OF h, rule_format])
  2222       done
  2223     have "?g (x + y) = ?g x + ?g y"
  2224       unfolding addh tha
  2225       g(1)[rule_format,OF conjunct1[OF h, OF x] conjunct1[OF h, OF y]]
  2226       by (simp add: scaleR_left_distrib)}
  2227   moreover
  2228   {
  2229     fix x :: "'a"
  2230     fix c :: real
  2231     assume x: "x \<in> span (insert a b)"
  2232     have tha: "\<And>(x::'a) c k a. c *\<^sub>R x - (c * k) *\<^sub>R a = c *\<^sub>R (x - k *\<^sub>R a)"
  2233       by (simp add: algebra_simps)
  2234     have hc: "?h (c *\<^sub>R x) = c * ?h x"
  2235       apply (rule conjunct2[OF h, rule_format, symmetric])
  2236       apply (metis span_mul x)
  2237       apply (metis tha span_mul x conjunct1[OF h])
  2238       done
  2239     have "?g (c *\<^sub>R x) = c*\<^sub>R ?g x"
  2240       unfolding hc tha g(2)[rule_format, OF conjunct1[OF h, OF x]]
  2241       by (simp add: algebra_simps)
  2242   }
  2243   moreover
  2244   {
  2245     fix x
  2246     assume x: "x \<in> insert a b"
  2247     {
  2248       assume xa: "x = a"
  2249       have ha1: "1 = ?h a"
  2250         apply (rule conjunct2[OF h, rule_format])
  2251         apply (metis span_superset insertI1)
  2252         using conjunct1[OF h, OF span_superset, OF insertI1]
  2253         apply (auto simp add: span_0)
  2254         done
  2255       from xa ha1[symmetric] have "?g x = f x"
  2256         apply simp
  2257         using g(2)[rule_format, OF span_0, of 0]
  2258         apply simp
  2259         done
  2260     }
  2261     moreover
  2262     {
  2263       assume xb: "x \<in> b"
  2264       have h0: "0 = ?h x"
  2265         apply (rule conjunct2[OF h, rule_format])
  2266         apply (metis  span_superset x)
  2267         apply simp
  2268         apply (metis span_superset xb)
  2269         done
  2270       have "?g x = f x"
  2271         by (simp add: h0[symmetric] g(3)[rule_format, OF xb])
  2272     }
  2273     ultimately have "?g x = f x"
  2274       using x by blast
  2275   }
  2276   ultimately show ?case
  2277     apply -
  2278     apply (rule exI[where x="?g"])
  2279     apply blast
  2280     done
  2281 qed
  2282 
  2283 lemma linear_independent_extend:
  2284   fixes B :: "'a::euclidean_space set"
  2285   assumes iB: "independent B"
  2286   shows "\<exists>g. linear g \<and> (\<forall>x\<in>B. g x = f x)"
  2287 proof -
  2288   from maximal_independent_subset_extend[of B UNIV] iB
  2289   obtain C where C: "B \<subseteq> C" "independent C" "\<And>x. x \<in> span C"
  2290     by auto
  2291 
  2292   from C(2) independent_bound[of C] linear_independent_extend_lemma[of C f]
  2293   obtain g where g:
  2294     "(\<forall>x\<in> span C. \<forall>y\<in> span C. g (x + y) = g x + g y) \<and>
  2295      (\<forall>x\<in> span C. \<forall>c. g (c*\<^sub>R x) = c *\<^sub>R g x) \<and>
  2296      (\<forall>x\<in> C. g x = f x)" by blast
  2297   from g show ?thesis
  2298     unfolding linear_iff
  2299     using C
  2300     apply clarsimp
  2301     apply blast
  2302     done
  2303 qed
  2304 
  2305 text {* Can construct an isomorphism between spaces of same dimension. *}
  2306 
  2307 lemma subspace_isomorphism:
  2308   fixes S :: "'a::euclidean_space set"
  2309     and T :: "'b::euclidean_space set"
  2310   assumes s: "subspace S"
  2311     and t: "subspace T"
  2312     and d: "dim S = dim T"
  2313   shows "\<exists>f. linear f \<and> f ` S = T \<and> inj_on f S"
  2314 proof -
  2315   from basis_exists[of S] independent_bound
  2316   obtain B where B: "B \<subseteq> S" "independent B" "S \<subseteq> span B" "card B = dim S" and fB: "finite B"
  2317     by blast
  2318   from basis_exists[of T] independent_bound
  2319   obtain C where C: "C \<subseteq> T" "independent C" "T \<subseteq> span C" "card C = dim T" and fC: "finite C"
  2320     by blast
  2321   from B(4) C(4) card_le_inj[of B C] d
  2322   obtain f where f: "f ` B \<subseteq> C" "inj_on f B" using `finite B` `finite C`
  2323     by auto
  2324   from linear_independent_extend[OF B(2)]
  2325   obtain g where g: "linear g" "\<forall>x\<in> B. g x = f x"
  2326     by blast
  2327   from inj_on_iff_eq_card[OF fB, of f] f(2) have "card (f ` B) = card B"
  2328     by simp
  2329   with B(4) C(4) have ceq: "card (f ` B) = card C"
  2330     using d by simp
  2331   have "g ` B = f ` B"
  2332     using g(2) by (auto simp add: image_iff)
  2333   also have "\<dots> = C" using card_subset_eq[OF fC f(1) ceq] .
  2334   finally have gBC: "g ` B = C" .
  2335   have gi: "inj_on g B"
  2336     using f(2) g(2) by (auto simp add: inj_on_def)
  2337   note g0 = linear_indep_image_lemma[OF g(1) fB, unfolded gBC, OF C(2) gi]
  2338   {
  2339     fix x y
  2340     assume x: "x \<in> S" and y: "y \<in> S" and gxy: "g x = g y"
  2341     from B(3) x y have x': "x \<in> span B" and y': "y \<in> span B"
  2342       by blast+
  2343     from gxy have th0: "g (x - y) = 0"
  2344       by (simp add: linear_sub[OF g(1)])
  2345     have th1: "x - y \<in> span B"
  2346       using x' y' by (metis span_sub)
  2347     have "x = y"
  2348       using g0[OF th1 th0] by simp
  2349   }
  2350   then have giS: "inj_on g S"
  2351     unfolding inj_on_def by blast
  2352   from span_subspace[OF B(1,3) s] have "g ` S = span (g ` B)"
  2353     by (simp add: span_linear_image[OF g(1)])
  2354   also have "\<dots> = span C" unfolding gBC ..
  2355   also have "\<dots> = T" using span_subspace[OF C(1,3) t] .
  2356   finally have gS: "g ` S = T" .
  2357   from g(1) gS giS show ?thesis
  2358     by blast
  2359 qed
  2360 
  2361 text {* Linear functions are equal on a subspace if they are on a spanning set. *}
  2362 
  2363 lemma subspace_kernel:
  2364   assumes lf: "linear f"
  2365   shows "subspace {x. f x = 0}"
  2366   apply (simp add: subspace_def)
  2367   apply (simp add: linear_add[OF lf] linear_cmul[OF lf] linear_0[OF lf])
  2368   done
  2369 
  2370 lemma linear_eq_0_span:
  2371   assumes lf: "linear f" and f0: "\<forall>x\<in>B. f x = 0"
  2372   shows "\<forall>x \<in> span B. f x = 0"
  2373   using f0 subspace_kernel[OF lf]
  2374   by (rule span_induct')
  2375 
  2376 lemma linear_eq_0:
  2377   assumes lf: "linear f"
  2378     and SB: "S \<subseteq> span B"
  2379     and f0: "\<forall>x\<in>B. f x = 0"
  2380   shows "\<forall>x \<in> S. f x = 0"
  2381   by (metis linear_eq_0_span[OF lf] subset_eq SB f0)
  2382 
  2383 lemma linear_eq:
  2384   assumes lf: "linear f"
  2385     and lg: "linear g"
  2386     and S: "S \<subseteq> span B"
  2387     and fg: "\<forall> x\<in> B. f x = g x"
  2388   shows "\<forall>x\<in> S. f x = g x"
  2389 proof -
  2390   let ?h = "\<lambda>x. f x - g x"
  2391   from fg have fg': "\<forall>x\<in> B. ?h x = 0" by simp
  2392   from linear_eq_0[OF linear_compose_sub[OF lf lg] S fg']
  2393   show ?thesis by simp
  2394 qed
  2395 
  2396 lemma linear_eq_stdbasis:
  2397   assumes lf: "linear (f::'a::euclidean_space \<Rightarrow> _)"
  2398     and lg: "linear g"
  2399     and fg: "\<forall>b\<in>Basis. f b = g b"
  2400   shows "f = g"
  2401   using linear_eq[OF lf lg, of _ Basis] fg by auto
  2402 
  2403 text {* Similar results for bilinear functions. *}
  2404 
  2405 lemma bilinear_eq:
  2406   assumes bf: "bilinear f"
  2407     and bg: "bilinear g"
  2408     and SB: "S \<subseteq> span B"
  2409     and TC: "T \<subseteq> span C"
  2410     and fg: "\<forall>x\<in> B. \<forall>y\<in> C. f x y = g x y"
  2411   shows "\<forall>x\<in>S. \<forall>y\<in>T. f x y = g x y "
  2412 proof -
  2413   let ?P = "{x. \<forall>y\<in> span C. f x y = g x y}"
  2414   from bf bg have sp: "subspace ?P"
  2415     unfolding bilinear_def linear_iff subspace_def bf bg
  2416     by (auto simp add: span_0 bilinear_lzero[OF bf] bilinear_lzero[OF bg] span_add Ball_def
  2417       intro: bilinear_ladd[OF bf])
  2418 
  2419   have "\<forall>x \<in> span B. \<forall>y\<in> span C. f x y = g x y"
  2420     apply (rule span_induct' [OF _ sp])
  2421     apply (rule ballI)
  2422     apply (rule span_induct')
  2423     apply (simp add: fg)
  2424     apply (auto simp add: subspace_def)
  2425     using bf bg unfolding bilinear_def linear_iff
  2426     apply (auto simp add: span_0 bilinear_rzero[OF bf] bilinear_rzero[OF bg] span_add Ball_def
  2427       intro: bilinear_ladd[OF bf])
  2428     done
  2429   then show ?thesis
  2430     using SB TC by auto
  2431 qed
  2432 
  2433 lemma bilinear_eq_stdbasis:
  2434   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space \<Rightarrow> _"
  2435   assumes bf: "bilinear f"
  2436     and bg: "bilinear g"
  2437     and fg: "\<forall>i\<in>Basis. \<forall>j\<in>Basis. f i j = g i j"
  2438   shows "f = g"
  2439   using bilinear_eq[OF bf bg equalityD2[OF span_Basis] equalityD2[OF span_Basis] fg] by blast
  2440 
  2441 text {* Detailed theorems about left and right invertibility in general case. *}
  2442 
  2443 lemma linear_injective_left_inverse:
  2444   fixes f::"'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  2445   assumes lf: "linear f" and fi: "inj f"
  2446   shows "\<exists>g. linear g \<and> g o f = id"
  2447 proof -
  2448   from linear_independent_extend[OF independent_injective_image, OF independent_Basis, OF lf fi]
  2449   obtain h:: "'b \<Rightarrow> 'a" where h: "linear h" "\<forall>x \<in> f ` Basis. h x = inv f x"
  2450     by blast
  2451   from h(2) have th: "\<forall>i\<in>Basis. (h \<circ> f) i = id i"
  2452     using inv_o_cancel[OF fi, unfolded fun_eq_iff id_def o_def]
  2453     by auto
  2454   from linear_eq_stdbasis[OF linear_compose[OF lf h(1)] linear_id th]
  2455   have "h o f = id" .
  2456   then show ?thesis
  2457     using h(1) by blast
  2458 qed
  2459 
  2460 lemma linear_surjective_right_inverse:
  2461   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  2462   assumes lf: "linear f"
  2463     and sf: "surj f"
  2464   shows "\<exists>g. linear g \<and> f o g = id"
  2465 proof -
  2466   from linear_independent_extend[OF independent_Basis[where 'a='b],of "inv f"]
  2467   obtain h:: "'b \<Rightarrow> 'a" where h: "linear h" "\<forall>x\<in>Basis. h x = inv f x"
  2468     by blast
  2469   from h(2) have th: "\<forall>i\<in>Basis. (f o h) i = id i"
  2470     using sf by (auto simp add: surj_iff_all)
  2471   from linear_eq_stdbasis[OF linear_compose[OF h(1) lf] linear_id th]
  2472   have "f o h = id" .
  2473   then show ?thesis
  2474     using h(1) by blast
  2475 qed
  2476 
  2477 text {* An injective map @{typ "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"} is also surjective. *}
  2478 
  2479 lemma linear_injective_imp_surjective:
  2480   fixes f::"'a::euclidean_space \<Rightarrow> 'a::euclidean_space"
  2481   assumes lf: "linear f"
  2482     and fi: "inj f"
  2483   shows "surj f"
  2484 proof -
  2485   let ?U = "UNIV :: 'a set"
  2486   from basis_exists[of ?U] obtain B
  2487     where B: "B \<subseteq> ?U" "independent B" "?U \<subseteq> span B" "card B = dim ?U"
  2488     by blast
  2489   from B(4) have d: "dim ?U = card B"
  2490     by simp
  2491   have th: "?U \<subseteq> span (f ` B)"
  2492     apply (rule card_ge_dim_independent)
  2493     apply blast
  2494     apply (rule independent_injective_image[OF B(2) lf fi])
  2495     apply (rule order_eq_refl)
  2496     apply (rule sym)
  2497     unfolding d
  2498     apply (rule card_image)
  2499     apply (rule subset_inj_on[OF fi])
  2500     apply blast
  2501     done
  2502   from th show ?thesis
  2503     unfolding span_linear_image[OF lf] surj_def
  2504     using B(3) by blast
  2505 qed
  2506 
  2507 text {* And vice versa. *}
  2508 
  2509 lemma surjective_iff_injective_gen:
  2510   assumes fS: "finite S"
  2511     and fT: "finite T"
  2512     and c: "card S = card T"
  2513     and ST: "f ` S \<subseteq> T"
  2514   shows "(\<forall>y \<in> T. \<exists>x \<in> S. f x = y) \<longleftrightarrow> inj_on f S"
  2515   (is "?lhs \<longleftrightarrow> ?rhs")
  2516 proof
  2517   assume h: "?lhs"
  2518   {
  2519     fix x y
  2520     assume x: "x \<in> S"
  2521     assume y: "y \<in> S"
  2522     assume f: "f x = f y"
  2523     from x fS have S0: "card S \<noteq> 0"
  2524       by auto
  2525     have "x = y"
  2526     proof (rule ccontr)
  2527       assume xy: "\<not> ?thesis"
  2528       have th: "card S \<le> card (f ` (S - {y}))"
  2529         unfolding c
  2530         apply (rule card_mono)
  2531         apply (rule finite_imageI)
  2532         using fS apply simp
  2533         using h xy x y f unfolding subset_eq image_iff
  2534         apply auto
  2535         apply (case_tac "xa = f x")
  2536         apply (rule bexI[where x=x])
  2537         apply auto
  2538         done
  2539       also have " \<dots> \<le> card (S - {y})"
  2540         apply (rule card_image_le)
  2541         using fS by simp
  2542       also have "\<dots> \<le> card S - 1" using y fS by simp
  2543       finally show False using S0 by arith
  2544     qed
  2545   }
  2546   then show ?rhs
  2547     unfolding inj_on_def by blast
  2548 next
  2549   assume h: ?rhs
  2550   have "f ` S = T"
  2551     apply (rule card_subset_eq[OF fT ST])
  2552     unfolding card_image[OF h]
  2553     apply (rule c)
  2554     done
  2555   then show ?lhs by blast
  2556 qed
  2557 
  2558 lemma linear_surjective_imp_injective:
  2559   fixes f :: "'a::euclidean_space \<Rightarrow> 'a::euclidean_space"
  2560   assumes lf: "linear f"
  2561     and sf: "surj f"
  2562   shows "inj f"
  2563 proof -
  2564   let ?U = "UNIV :: 'a set"
  2565   from basis_exists[of ?U] obtain B
  2566     where B: "B \<subseteq> ?U" "independent B" "?U \<subseteq> span B" and d: "card B = dim ?U"
  2567     by blast
  2568   {
  2569     fix x
  2570     assume x: "x \<in> span B"
  2571     assume fx: "f x = 0"
  2572     from B(2) have fB: "finite B"
  2573       using independent_bound by auto
  2574     have fBi: "independent (f ` B)"
  2575       apply (rule card_le_dim_spanning[of "f ` B" ?U])
  2576       apply blast
  2577       using sf B(3)
  2578       unfolding span_linear_image[OF lf] surj_def subset_eq image_iff
  2579       apply blast
  2580       using fB apply blast
  2581       unfolding d[symmetric]
  2582       apply (rule card_image_le)
  2583       apply (rule fB)
  2584       done
  2585     have th0: "dim ?U \<le> card (f ` B)"
  2586       apply (rule span_card_ge_dim)
  2587       apply blast
  2588       unfolding span_linear_image[OF lf]
  2589       apply (rule subset_trans[where B = "f ` UNIV"])
  2590       using sf unfolding surj_def
  2591       apply blast
  2592       apply (rule image_mono)
  2593       apply (rule B(3))
  2594       apply (metis finite_imageI fB)
  2595       done
  2596     moreover have "card (f ` B) \<le> card B"
  2597       by (rule card_image_le, rule fB)
  2598     ultimately have th1: "card B = card (f ` B)"
  2599       unfolding d by arith
  2600     have fiB: "inj_on f B"
  2601       unfolding surjective_iff_injective_gen[OF fB finite_imageI[OF fB] th1 subset_refl, symmetric]
  2602       by blast
  2603     from linear_indep_image_lemma[OF lf fB fBi fiB x] fx
  2604     have "x = 0" by blast
  2605   }
  2606   then show ?thesis
  2607     unfolding linear_injective_0[OF lf]
  2608     using B(3)
  2609     by blast
  2610 qed
  2611 
  2612 text {* Hence either is enough for isomorphism. *}
  2613 
  2614 lemma left_right_inverse_eq:
  2615   assumes fg: "f \<circ> g = id"
  2616     and gh: "g \<circ> h = id"
  2617   shows "f = h"
  2618 proof -
  2619   have "f = f \<circ> (g \<circ> h)"
  2620     unfolding gh by simp
  2621   also have "\<dots> = (f \<circ> g) \<circ> h"
  2622     by (simp add: o_assoc)
  2623   finally show "f = h"
  2624     unfolding fg by simp
  2625 qed
  2626 
  2627 lemma isomorphism_expand:
  2628   "f \<circ> g = id \<and> g \<circ> f = id \<longleftrightarrow> (\<forall>x. f (g x) = x) \<and> (\<forall>x. g (f x) = x)"
  2629   by (simp add: fun_eq_iff o_def id_def)
  2630 
  2631 lemma linear_injective_isomorphism:
  2632   fixes f::"'a::euclidean_space \<Rightarrow> 'a::euclidean_space"
  2633   assumes lf: "linear f"
  2634     and fi: "inj f"
  2635   shows "\<exists>f'. linear f' \<and> (\<forall>x. f' (f x) = x) \<and> (\<forall>x. f (f' x) = x)"
  2636   unfolding isomorphism_expand[symmetric]
  2637   using linear_surjective_right_inverse[OF lf linear_injective_imp_surjective[OF lf fi]]
  2638     linear_injective_left_inverse[OF lf fi]
  2639   by (metis left_right_inverse_eq)
  2640 
  2641 lemma linear_surjective_isomorphism:
  2642   fixes f :: "'a::euclidean_space \<Rightarrow> 'a::euclidean_space"
  2643   assumes lf: "linear f"
  2644     and sf: "surj f"
  2645   shows "\<exists>f'. linear f' \<and> (\<forall>x. f' (f x) = x) \<and> (\<forall>x. f (f' x) = x)"
  2646   unfolding isomorphism_expand[symmetric]
  2647   using linear_surjective_right_inverse[OF lf sf]
  2648     linear_injective_left_inverse[OF lf linear_surjective_imp_injective[OF lf sf]]
  2649   by (metis left_right_inverse_eq)
  2650 
  2651 text {* Left and right inverses are the same for
  2652   @{typ "'a::euclidean_space \<Rightarrow> 'a::euclidean_space"}. *}
  2653 
  2654 lemma linear_inverse_left:
  2655   fixes f :: "'a::euclidean_space \<Rightarrow> 'a::euclidean_space"
  2656   assumes lf: "linear f"
  2657     and lf': "linear f'"
  2658   shows "f \<circ> f' = id \<longleftrightarrow> f' \<circ> f = id"
  2659 proof -
  2660   {
  2661     fix f f':: "'a \<Rightarrow> 'a"
  2662     assume lf: "linear f" "linear f'"
  2663     assume f: "f \<circ> f' = id"
  2664     from f have sf: "surj f"
  2665       apply (auto simp add: o_def id_def surj_def)
  2666       apply metis
  2667       done
  2668     from linear_surjective_isomorphism[OF lf(1) sf] lf f
  2669     have "f' \<circ> f = id"
  2670       unfolding fun_eq_iff o_def id_def by metis
  2671   }
  2672   then show ?thesis
  2673     using lf lf' by metis
  2674 qed
  2675 
  2676 text {* Moreover, a one-sided inverse is automatically linear. *}
  2677 
  2678 lemma left_inverse_linear:
  2679   fixes f :: "'a::euclidean_space \<Rightarrow> 'a::euclidean_space"
  2680   assumes lf: "linear f"
  2681     and gf: "g \<circ> f = id"
  2682   shows "linear g"
  2683 proof -
  2684   from gf have fi: "inj f"
  2685     apply (auto simp add: inj_on_def o_def id_def fun_eq_iff)
  2686     apply metis
  2687     done
  2688   from linear_injective_isomorphism[OF lf fi]
  2689   obtain h :: "'a \<Rightarrow> 'a" where h: "linear h" "\<forall>x. h (f x) = x" "\<forall>x. f (h x) = x"
  2690     by blast
  2691   have "h = g"
  2692     apply (rule ext) using gf h(2,3)
  2693     apply (simp add: o_def id_def fun_eq_iff)
  2694     apply metis
  2695     done
  2696   with h(1) show ?thesis by blast
  2697 qed
  2698 
  2699 
  2700 subsection {* Infinity norm *}
  2701 
  2702 definition "infnorm (x::'a::euclidean_space) = Sup {abs (x \<bullet> b) |b. b \<in> Basis}"
  2703 
  2704 lemma infnorm_set_image:
  2705   fixes x :: "'a::euclidean_space"
  2706   shows "{abs (x \<bullet> i) |i. i \<in> Basis} = (\<lambda>i. abs (x \<bullet> i)) ` Basis"
  2707   by blast
  2708 
  2709 lemma infnorm_Max:
  2710   fixes x :: "'a::euclidean_space"
  2711   shows "infnorm x = Max ((\<lambda>i. abs (x \<bullet> i)) ` Basis)"
  2712   by (simp add: infnorm_def infnorm_set_image cSup_eq_Max del: Sup_image_eq)
  2713 
  2714 lemma infnorm_set_lemma:
  2715   fixes x :: "'a::euclidean_space"
  2716   shows "finite {abs (x \<bullet> i) |i. i \<in> Basis}"
  2717     and "{abs (x \<bullet> i) |i. i \<in> Basis} \<noteq> {}"
  2718   unfolding infnorm_set_image
  2719   by auto
  2720 
  2721 lemma infnorm_pos_le:
  2722   fixes x :: "'a::euclidean_space"
  2723   shows "0 \<le> infnorm x"
  2724   by (simp add: infnorm_Max Max_ge_iff ex_in_conv)
  2725 
  2726 lemma infnorm_triangle:
  2727   fixes x :: "'a::euclidean_space"
  2728   shows "infnorm (x + y) \<le> infnorm x + infnorm y"
  2729 proof -
  2730   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"
  2731     by simp
  2732   show ?thesis
  2733     by (auto simp: infnorm_Max inner_add_left intro!: *)
  2734 qed
  2735 
  2736 lemma infnorm_eq_0:
  2737   fixes x :: "'a::euclidean_space"
  2738   shows "infnorm x = 0 \<longleftrightarrow> x = 0"
  2739 proof -
  2740   have "infnorm x \<le> 0 \<longleftrightarrow> x = 0"
  2741     unfolding infnorm_Max by (simp add: euclidean_all_zero_iff)
  2742   then show ?thesis
  2743     using infnorm_pos_le[of x] by simp
  2744 qed
  2745 
  2746 lemma infnorm_0: "infnorm 0 = 0"
  2747   by (simp add: infnorm_eq_0)
  2748 
  2749 lemma infnorm_neg: "infnorm (- x) = infnorm x"
  2750   unfolding infnorm_def
  2751   apply (rule cong[of "Sup" "Sup"])
  2752   apply blast
  2753   apply auto
  2754   done
  2755 
  2756 lemma infnorm_sub: "infnorm (x - y) = infnorm (y - x)"
  2757 proof -
  2758   have "y - x = - (x - y)" by simp
  2759   then show ?thesis
  2760     by (metis infnorm_neg)
  2761 qed
  2762 
  2763 lemma real_abs_sub_infnorm: "\<bar>infnorm x - infnorm y\<bar> \<le> infnorm (x - y)"
  2764 proof -
  2765   have th: "\<And>(nx::real) n ny. nx \<le> n + ny \<Longrightarrow> ny <= n + nx \<Longrightarrow> \<bar>nx - ny\<bar> \<le> n"
  2766     by arith
  2767   from infnorm_triangle[of "x - y" " y"] infnorm_triangle[of "x - y" "-x"]
  2768   have ths: "infnorm x \<le> infnorm (x - y) + infnorm y"
  2769     "infnorm y \<le> infnorm (x - y) + infnorm x"
  2770     by (simp_all add: field_simps infnorm_neg)
  2771   from th[OF ths] show ?thesis .
  2772 qed
  2773 
  2774 lemma real_abs_infnorm: "\<bar>infnorm x\<bar> = infnorm x"
  2775   using infnorm_pos_le[of x] by arith
  2776 
  2777 lemma Basis_le_infnorm:
  2778   fixes x :: "'a::euclidean_space"
  2779   shows "b \<in> Basis \<Longrightarrow> \<bar>x \<bullet> b\<bar> \<le> infnorm x"
  2780   by (simp add: infnorm_Max)
  2781 
  2782 lemma infnorm_mul: "infnorm (a *\<^sub>R x) = abs a * infnorm x"
  2783   unfolding infnorm_Max
  2784 proof (safe intro!: Max_eqI)
  2785   let ?B = "(\<lambda>i. \<bar>x \<bullet> i\<bar>) ` Basis"
  2786   {
  2787     fix b :: 'a
  2788     assume "b \<in> Basis"
  2789     then show "\<bar>a *\<^sub>R x \<bullet> b\<bar> \<le> \<bar>a\<bar> * Max ?B"
  2790       by (simp add: abs_mult mult_left_mono)
  2791   next
  2792     from Max_in[of ?B] obtain b where "b \<in> Basis" "Max ?B = \<bar>x \<bullet> b\<bar>"
  2793       by (auto simp del: Max_in)
  2794     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"
  2795       by (intro image_eqI[where x=b]) (auto simp: abs_mult)
  2796   }
  2797 qed simp
  2798 
  2799 lemma infnorm_mul_lemma: "infnorm (a *\<^sub>R x) \<le> \<bar>a\<bar> * infnorm x"
  2800   unfolding infnorm_mul ..
  2801 
  2802 lemma infnorm_pos_lt: "infnorm x > 0 \<longleftrightarrow> x \<noteq> 0"
  2803   using infnorm_pos_le[of x] infnorm_eq_0[of x] by arith
  2804 
  2805 text {* Prove that it differs only up to a bound from Euclidean norm. *}
  2806 
  2807 lemma infnorm_le_norm: "infnorm x \<le> norm x"
  2808   by (simp add: Basis_le_norm infnorm_Max)
  2809 
  2810 lemma (in euclidean_space) euclidean_inner: "inner x y = (\<Sum>b\<in>Basis. (x \<bullet> b) * (y \<bullet> b))"
  2811   by (subst (1 2) euclidean_representation[symmetric])
  2812     (simp add: inner_setsum_left inner_setsum_right setsum_cases inner_Basis ac_simps if_distrib)
  2813 
  2814 lemma norm_le_infnorm:
  2815   fixes x :: "'a::euclidean_space"
  2816   shows "norm x \<le> sqrt DIM('a) * infnorm x"
  2817 proof -
  2818   let ?d = "DIM('a)"
  2819   have "real ?d \<ge> 0"
  2820     by simp
  2821   then have d2: "(sqrt (real ?d))\<^sup>2 = real ?d"
  2822     by (auto intro: real_sqrt_pow2)
  2823   have th: "sqrt (real ?d) * infnorm x \<ge> 0"
  2824     by (simp add: zero_le_mult_iff infnorm_pos_le)
  2825   have th1: "x \<bullet> x \<le> (sqrt (real ?d) * infnorm x)\<^sup>2"
  2826     unfolding power_mult_distrib d2
  2827     unfolding real_of_nat_def
  2828     apply (subst euclidean_inner)
  2829     apply (subst power2_abs[symmetric])
  2830     apply (rule order_trans[OF setsum_bounded[where K="\<bar>infnorm x\<bar>\<^sup>2"]])
  2831     apply (auto simp add: power2_eq_square[symmetric])
  2832     apply (subst power2_abs[symmetric])
  2833     apply (rule power_mono)
  2834     apply (auto simp: infnorm_Max)
  2835     done
  2836   from real_le_lsqrt[OF inner_ge_zero th th1]
  2837   show ?thesis
  2838     unfolding norm_eq_sqrt_inner id_def .
  2839 qed
  2840 
  2841 lemma tendsto_infnorm [tendsto_intros]:
  2842   assumes "(f ---> a) F"
  2843   shows "((\<lambda>x. infnorm (f x)) ---> infnorm a) F"
  2844 proof (rule tendsto_compose [OF LIM_I assms])
  2845   fix r :: real
  2846   assume "r > 0"
  2847   then show "\<exists>s>0. \<forall>x. x \<noteq> a \<and> norm (x - a) < s \<longrightarrow> norm (infnorm x - infnorm a) < r"
  2848     by (metis real_norm_def le_less_trans real_abs_sub_infnorm infnorm_le_norm)
  2849 qed
  2850 
  2851 text {* Equality in Cauchy-Schwarz and triangle inequalities. *}
  2852 
  2853 lemma norm_cauchy_schwarz_eq: "x \<bullet> y = norm x * norm y \<longleftrightarrow> norm x *\<^sub>R y = norm y *\<^sub>R x"
  2854   (is "?lhs \<longleftrightarrow> ?rhs")
  2855 proof -
  2856   {
  2857     assume h: "x = 0"
  2858     then have ?thesis by simp
  2859   }
  2860   moreover
  2861   {
  2862     assume h: "y = 0"
  2863     then have ?thesis by simp
  2864   }
  2865   moreover
  2866   {
  2867     assume x: "x \<noteq> 0" and y: "y \<noteq> 0"
  2868     from inner_eq_zero_iff[of "norm y *\<^sub>R x - norm x *\<^sub>R y"]
  2869     have "?rhs \<longleftrightarrow>
  2870       (norm y * (norm y * norm x * norm x - norm x * (x \<bullet> y)) -
  2871         norm x * (norm y * (y \<bullet> x) - norm x * norm y * norm y) =  0)"
  2872       using x y
  2873       unfolding inner_simps
  2874       unfolding power2_norm_eq_inner[symmetric] power2_eq_square right_minus_eq
  2875       apply (simp add: inner_commute)
  2876       apply (simp add: field_simps)
  2877       apply metis
  2878       done
  2879     also have "\<dots> \<longleftrightarrow> (2 * norm x * norm y * (norm x * norm y - x \<bullet> y) = 0)" using x y
  2880       by (simp add: field_simps inner_commute)
  2881     also have "\<dots> \<longleftrightarrow> ?lhs" using x y
  2882       apply simp
  2883       apply metis
  2884       done
  2885     finally have ?thesis by blast
  2886   }
  2887   ultimately show ?thesis by blast
  2888 qed
  2889 
  2890 lemma norm_cauchy_schwarz_abs_eq:
  2891   "abs (x \<bullet> y) = norm x * norm y \<longleftrightarrow>
  2892     norm x *\<^sub>R y = norm y *\<^sub>R x \<or> norm x *\<^sub>R y = - norm y *\<^sub>R x"
  2893   (is "?lhs \<longleftrightarrow> ?rhs")
  2894 proof -
  2895   have th: "\<And>(x::real) a. a \<ge> 0 \<Longrightarrow> abs x = a \<longleftrightarrow> x = a \<or> x = - a"
  2896     by arith
  2897   have "?rhs \<longleftrightarrow> norm x *\<^sub>R y = norm y *\<^sub>R x \<or> norm (- x) *\<^sub>R y = norm y *\<^sub>R (- x)"
  2898     by simp
  2899   also have "\<dots> \<longleftrightarrow>(x \<bullet> y = norm x * norm y \<or> (- x) \<bullet> y = norm x * norm y)"
  2900     unfolding norm_cauchy_schwarz_eq[symmetric]
  2901     unfolding norm_minus_cancel norm_scaleR ..
  2902   also have "\<dots> \<longleftrightarrow> ?lhs"
  2903     unfolding th[OF mult_nonneg_nonneg, OF norm_ge_zero[of x] norm_ge_zero[of y]] inner_simps
  2904     by auto
  2905   finally show ?thesis ..
  2906 qed
  2907 
  2908 lemma norm_triangle_eq:
  2909   fixes x y :: "'a::real_inner"
  2910   shows "norm (x + y) = norm x + norm y \<longleftrightarrow> norm x *\<^sub>R y = norm y *\<^sub>R x"
  2911 proof -
  2912   {
  2913     assume x: "x = 0 \<or> y = 0"
  2914     then have ?thesis
  2915       by (cases "x = 0") simp_all
  2916   }
  2917   moreover
  2918   {
  2919     assume x: "x \<noteq> 0" and y: "y \<noteq> 0"
  2920     then have "norm x \<noteq> 0" "norm y \<noteq> 0"
  2921       by simp_all
  2922     then have n: "norm x > 0" "norm y > 0"
  2923       using norm_ge_zero[of x] norm_ge_zero[of y] by arith+
  2924     have th: "\<And>(a::real) b c. a + b + c \<noteq> 0 \<Longrightarrow> a = b + c \<longleftrightarrow> a\<^sup>2 = (b + c)\<^sup>2"
  2925       by algebra
  2926     have "norm (x + y) = norm x + norm y \<longleftrightarrow> (norm (x + y))\<^sup>2 = (norm x + norm y)\<^sup>2"
  2927       apply (rule th)
  2928       using n norm_ge_zero[of "x + y"]
  2929       apply arith
  2930       done
  2931     also have "\<dots> \<longleftrightarrow> norm x *\<^sub>R y = norm y *\<^sub>R x"
  2932       unfolding norm_cauchy_schwarz_eq[symmetric]
  2933       unfolding power2_norm_eq_inner inner_simps
  2934       by (simp add: power2_norm_eq_inner[symmetric] power2_eq_square inner_commute field_simps)
  2935     finally have ?thesis .
  2936   }
  2937   ultimately show ?thesis by blast
  2938 qed
  2939 
  2940 
  2941 subsection {* Collinearity *}
  2942 
  2943 definition collinear :: "'a::real_vector set \<Rightarrow> bool"
  2944   where "collinear S \<longleftrightarrow> (\<exists>u. \<forall>x \<in> S. \<forall> y \<in> S. \<exists>c. x - y = c *\<^sub>R u)"
  2945 
  2946 lemma collinear_empty: "collinear {}"
  2947   by (simp add: collinear_def)
  2948 
  2949 lemma collinear_sing: "collinear {x}"
  2950   by (simp add: collinear_def)
  2951 
  2952 lemma collinear_2: "collinear {x, y}"
  2953   apply (simp add: collinear_def)
  2954   apply (rule exI[where x="x - y"])
  2955   apply auto
  2956   apply (rule exI[where x=1], simp)
  2957   apply (rule exI[where x="- 1"], simp)
  2958   done
  2959 
  2960 lemma collinear_lemma: "collinear {0,x,y} \<longleftrightarrow> x = 0 \<or> y = 0 \<or> (\<exists>c. y = c *\<^sub>R x)"
  2961   (is "?lhs \<longleftrightarrow> ?rhs")
  2962 proof -
  2963   {
  2964     assume "x = 0 \<or> y = 0"
  2965     then have ?thesis
  2966       by (cases "x = 0") (simp_all add: collinear_2 insert_commute)
  2967   }
  2968   moreover
  2969   {
  2970     assume x: "x \<noteq> 0" and y: "y \<noteq> 0"
  2971     have ?thesis
  2972     proof
  2973       assume h: "?lhs"
  2974       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"
  2975         unfolding collinear_def by blast
  2976       from u[rule_format, of x 0] u[rule_format, of y 0]
  2977       obtain cx and cy where
  2978         cx: "x = cx *\<^sub>R u" and cy: "y = cy *\<^sub>R u"
  2979         by auto
  2980       from cx x have cx0: "cx \<noteq> 0" by auto
  2981       from cy y have cy0: "cy \<noteq> 0" by auto
  2982       let ?d = "cy / cx"
  2983       from cx cy cx0 have "y = ?d *\<^sub>R x"
  2984         by simp
  2985       then show ?rhs using x y by blast
  2986     next
  2987       assume h: "?rhs"
  2988       then obtain c where c: "y = c *\<^sub>R x"
  2989         using x y by blast
  2990       show ?lhs
  2991         unfolding collinear_def c
  2992         apply (rule exI[where x=x])
  2993         apply auto
  2994         apply (rule exI[where x="- 1"], simp)
  2995         apply (rule exI[where x= "-c"], simp)
  2996         apply (rule exI[where x=1], simp)
  2997         apply (rule exI[where x="1 - c"], simp add: scaleR_left_diff_distrib)
  2998         apply (rule exI[where x="c - 1"], simp add: scaleR_left_diff_distrib)
  2999         done
  3000     qed
  3001   }
  3002   ultimately show ?thesis by blast
  3003 qed
  3004 
  3005 lemma norm_cauchy_schwarz_equal: "abs (x \<bullet> y) = norm x * norm y \<longleftrightarrow> collinear {0, x, y}"
  3006   unfolding norm_cauchy_schwarz_abs_eq
  3007   apply (cases "x=0", simp_all add: collinear_2)
  3008   apply (cases "y=0", simp_all add: collinear_2 insert_commute)
  3009   unfolding collinear_lemma
  3010   apply simp
  3011   apply (subgoal_tac "norm x \<noteq> 0")
  3012   apply (subgoal_tac "norm y \<noteq> 0")
  3013   apply (rule iffI)
  3014   apply (cases "norm x *\<^sub>R y = norm y *\<^sub>R x")
  3015   apply (rule exI[where x="(1/norm x) * norm y"])
  3016   apply (drule sym)
  3017   unfolding scaleR_scaleR[symmetric]
  3018   apply (simp add: field_simps)
  3019   apply (rule exI[where x="(1/norm x) * - norm y"])
  3020   apply clarify
  3021   apply (drule sym)
  3022   unfolding scaleR_scaleR[symmetric]
  3023   apply (simp add: field_simps)
  3024   apply (erule exE)
  3025   apply (erule ssubst)
  3026   unfolding scaleR_scaleR
  3027   unfolding norm_scaleR
  3028   apply (subgoal_tac "norm x * c = \<bar>c\<bar> * norm x \<or> norm x * c = - \<bar>c\<bar> * norm x")
  3029   apply (auto simp add: field_simps)
  3030   done
  3031 
  3032 end
  3033