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