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