src/HOL/Multivariate_Analysis/Euclidean_Space.thy
author wenzelm
Wed Dec 29 17:34:41 2010 +0100 (2010-12-29)
changeset 41413 64cd30d6b0b8
parent 40786 0a54cfc9add3
child 41863 e5104b436ea1
permissions -rw-r--r--
explicit file specifications -- avoid secondary load path;
     1 (*  Title:      Library/Multivariate_Analysis/Euclidean_Space.thy
     2     Author:     Amine Chaieb, University of Cambridge
     3 *)
     4 
     5 header {* (Real) Vectors in Euclidean space, and elementary linear algebra.*}
     6 
     7 theory Euclidean_Space
     8 imports
     9   Complex_Main
    10   "~~/src/HOL/Decision_Procs/Dense_Linear_Order"
    11   "~~/src/HOL/Library/Infinite_Set"
    12   "~~/src/HOL/Library/Inner_Product"
    13   L2_Norm
    14   "~~/src/HOL/Library/Convex"
    15 uses
    16   "~~/src/HOL/Library/positivstellensatz.ML"  (* FIXME duplicate use!? *)
    17   ("normarith.ML")
    18 begin
    19 
    20 lemma cond_application_beta: "(if b then f else g) x = (if b then f x else g x)"
    21   by auto
    22 
    23 notation inner (infix "\<bullet>" 70)
    24 
    25 subsection {* A connectedness or intermediate value lemma with several applications. *}
    26 
    27 lemma connected_real_lemma:
    28   fixes f :: "real \<Rightarrow> 'a::metric_space"
    29   assumes ab: "a \<le> b" and fa: "f a \<in> e1" and fb: "f b \<in> e2"
    30   and dst: "\<And>e x. a <= x \<Longrightarrow> x <= b \<Longrightarrow> 0 < e ==> \<exists>d > 0. \<forall>y. abs(y - x) < d \<longrightarrow> dist(f y) (f x) < e"
    31   and e1: "\<forall>y \<in> e1. \<exists>e > 0. \<forall>y'. dist y' y < e \<longrightarrow> y' \<in> e1"
    32   and e2: "\<forall>y \<in> e2. \<exists>e > 0. \<forall>y'. dist y' y < e \<longrightarrow> y' \<in> e2"
    33   and e12: "~(\<exists>x \<ge> a. x <= b \<and> f x \<in> e1 \<and> f x \<in> e2)"
    34   shows "\<exists>x \<ge> a. x <= b \<and> f x \<notin> e1 \<and> f x \<notin> e2" (is "\<exists> x. ?P x")
    35 proof-
    36   let ?S = "{c. \<forall>x \<ge> a. x <= c \<longrightarrow> f x \<in> e1}"
    37   have Se: " \<exists>x. x \<in> ?S" apply (rule exI[where x=a]) by (auto simp add: fa)
    38   have Sub: "\<exists>y. isUb UNIV ?S y"
    39     apply (rule exI[where x= b])
    40     using ab fb e12 by (auto simp add: isUb_def setle_def)
    41   from reals_complete[OF Se Sub] obtain l where
    42     l: "isLub UNIV ?S l"by blast
    43   have alb: "a \<le> l" "l \<le> b" using l ab fa fb e12
    44     apply (auto simp add: isLub_def leastP_def isUb_def setle_def setge_def)
    45     by (metis linorder_linear)
    46   have ale1: "\<forall>z \<ge> a. z < l \<longrightarrow> f z \<in> e1" using l
    47     apply (auto simp add: isLub_def leastP_def isUb_def setle_def setge_def)
    48     by (metis linorder_linear not_le)
    49     have th1: "\<And>z x e d :: real. z <= x + e \<Longrightarrow> e < d ==> z < x \<or> abs(z - x) < d" by arith
    50     have th2: "\<And>e x:: real. 0 < e ==> ~(x + e <= x)" by arith
    51     have th3: "\<And>d::real. d > 0 \<Longrightarrow> \<exists>e > 0. e < d" by dlo
    52     {assume le2: "f l \<in> e2"
    53       from le2 fa fb e12 alb have la: "l \<noteq> a" by metis
    54       hence lap: "l - a > 0" using alb by arith
    55       from e2[rule_format, OF le2] obtain e where
    56         e: "e > 0" "\<forall>y. dist y (f l) < e \<longrightarrow> y \<in> e2" by metis
    57       from dst[OF alb e(1)] obtain d where
    58         d: "d > 0" "\<forall>y. \<bar>y - l\<bar> < d \<longrightarrow> dist (f y) (f l) < e" by metis
    59       have "\<exists>d'. d' < d \<and> d' >0 \<and> l - d' > a" using lap d(1)
    60         apply ferrack by arith
    61       then obtain d' where d': "d' > 0" "d' < d" "l - d' > a" by metis
    62       from d e have th0: "\<forall>y. \<bar>y - l\<bar> < d \<longrightarrow> f y \<in> e2" by metis
    63       from th0[rule_format, of "l - d'"] d' have "f (l - d') \<in> e2" by auto
    64       moreover
    65       have "f (l - d') \<in> e1" using ale1[rule_format, of "l -d'"] d' by auto
    66       ultimately have False using e12 alb d' by auto}
    67     moreover
    68     {assume le1: "f l \<in> e1"
    69     from le1 fa fb e12 alb have lb: "l \<noteq> b" by metis
    70       hence blp: "b - l > 0" using alb by arith
    71       from e1[rule_format, OF le1] obtain e where
    72         e: "e > 0" "\<forall>y. dist y (f l) < e \<longrightarrow> y \<in> e1" by metis
    73       from dst[OF alb e(1)] obtain d where
    74         d: "d > 0" "\<forall>y. \<bar>y - l\<bar> < d \<longrightarrow> dist (f y) (f l) < e" by metis
    75       have "\<exists>d'. d' < d \<and> d' >0" using d(1) by dlo
    76       then obtain d' where d': "d' > 0" "d' < d" by metis
    77       from d e have th0: "\<forall>y. \<bar>y - l\<bar> < d \<longrightarrow> f y \<in> e1" by auto
    78       hence "\<forall>y. l \<le> y \<and> y \<le> l + d' \<longrightarrow> f y \<in> e1" using d' by auto
    79       with ale1 have "\<forall>y. a \<le> y \<and> y \<le> l + d' \<longrightarrow> f y \<in> e1" by auto
    80       with l d' have False
    81         by (auto simp add: isLub_def isUb_def setle_def setge_def leastP_def) }
    82     ultimately show ?thesis using alb by metis
    83 qed
    84 
    85 text{* One immediately useful corollary is the existence of square roots! --- Should help to get rid of all the development of square-root for reals as a special case *}
    86 
    87 lemma square_bound_lemma: "(x::real) < (1 + x) * (1 + x)"
    88 proof-
    89   have "(x + 1/2)^2 + 3/4 > 0" using zero_le_power2[of "x+1/2"] by arith
    90   thus ?thesis by (simp add: field_simps power2_eq_square)
    91 qed
    92 
    93 lemma square_continuous: "0 < (e::real) ==> \<exists>d. 0 < d \<and> (\<forall>y. abs(y - x) < d \<longrightarrow> abs(y * y - x * x) < e)"
    94   using isCont_power[OF isCont_ident, of 2, unfolded isCont_def LIM_eq, rule_format, of e x] apply (auto simp add: power2_eq_square)
    95   apply (rule_tac x="s" in exI)
    96   apply auto
    97   apply (erule_tac x=y in allE)
    98   apply auto
    99   done
   100 
   101 lemma real_le_lsqrt: "0 <= x \<Longrightarrow> 0 <= y \<Longrightarrow> x <= y^2 ==> sqrt x <= y"
   102   using real_sqrt_le_iff[of x "y^2"] by simp
   103 
   104 lemma real_le_rsqrt: "x^2 \<le> y \<Longrightarrow> x \<le> sqrt y"
   105   using real_sqrt_le_mono[of "x^2" y] by simp
   106 
   107 lemma real_less_rsqrt: "x^2 < y \<Longrightarrow> x < sqrt y"
   108   using real_sqrt_less_mono[of "x^2" y] by simp
   109 
   110 lemma sqrt_even_pow2: assumes n: "even n"
   111   shows "sqrt(2 ^ n) = 2 ^ (n div 2)"
   112 proof-
   113   from n obtain m where m: "n = 2*m" unfolding even_mult_two_ex ..
   114   from m  have "sqrt(2 ^ n) = sqrt ((2 ^ m) ^ 2)"
   115     by (simp only: power_mult[symmetric] mult_commute)
   116   then show ?thesis  using m by simp
   117 qed
   118 
   119 lemma real_div_sqrt: "0 <= x ==> x / sqrt(x) = sqrt(x)"
   120   apply (cases "x = 0", simp_all)
   121   using sqrt_divide_self_eq[of x]
   122   apply (simp add: inverse_eq_divide field_simps)
   123   done
   124 
   125 text{* Hence derive more interesting properties of the norm. *}
   126 
   127 (* FIXME: same as norm_scaleR
   128 lemma norm_mul[simp]: "norm(a *\<^sub>R x) = abs(a) * norm x"
   129   by (simp add: norm_vector_def setL2_right_distrib abs_mult)
   130 *)
   131 
   132 lemma norm_eq_0_dot: "(norm x = 0) \<longleftrightarrow> (inner x x = (0::real))"
   133   by (simp add: setL2_def power2_eq_square)
   134 
   135 lemma norm_cauchy_schwarz:
   136   shows "inner x y <= norm x * norm y"
   137   using Cauchy_Schwarz_ineq2[of x y] by auto
   138 
   139 lemma norm_cauchy_schwarz_abs:
   140   shows "\<bar>inner x y\<bar> \<le> norm x * norm y"
   141   by (rule Cauchy_Schwarz_ineq2)
   142 
   143 lemma norm_triangle_sub:
   144   fixes x y :: "'a::real_normed_vector"
   145   shows "norm x \<le> norm y  + norm (x - y)"
   146   using norm_triangle_ineq[of "y" "x - y"] by (simp add: field_simps)
   147 
   148 lemma real_abs_norm: "\<bar>norm x\<bar> = norm x"
   149   by (rule abs_norm_cancel)
   150 lemma real_abs_sub_norm: "\<bar>norm x - norm y\<bar> <= norm(x - y)"
   151   by (rule norm_triangle_ineq3)
   152 lemma norm_le: "norm(x) <= norm(y) \<longleftrightarrow> x \<bullet> x <= y \<bullet> y"
   153   by (simp add: norm_eq_sqrt_inner) 
   154 lemma norm_lt: "norm(x) < norm(y) \<longleftrightarrow> x \<bullet> x < y \<bullet> y"
   155   by (simp add: norm_eq_sqrt_inner)
   156 lemma norm_eq: "norm(x) = norm (y) \<longleftrightarrow> x \<bullet> x = y \<bullet> y"
   157   apply(subst order_eq_iff) unfolding norm_le by auto
   158 lemma norm_eq_1: "norm(x) = 1 \<longleftrightarrow> x \<bullet> x = 1"
   159   unfolding norm_eq_sqrt_inner by auto
   160 
   161 text{* Squaring equations and inequalities involving norms.  *}
   162 
   163 lemma dot_square_norm: "x \<bullet> x = norm(x)^2"
   164   by (simp add: norm_eq_sqrt_inner)
   165 
   166 lemma norm_eq_square: "norm(x) = a \<longleftrightarrow> 0 <= a \<and> x \<bullet> x = a^2"
   167   by (auto simp add: norm_eq_sqrt_inner)
   168 
   169 lemma real_abs_le_square_iff: "\<bar>x\<bar> \<le> \<bar>y\<bar> \<longleftrightarrow> (x::real)^2 \<le> y^2"
   170 proof
   171   assume "\<bar>x\<bar> \<le> \<bar>y\<bar>"
   172   then have "\<bar>x\<bar>\<twosuperior> \<le> \<bar>y\<bar>\<twosuperior>" by (rule power_mono, simp)
   173   then show "x\<twosuperior> \<le> y\<twosuperior>" by simp
   174 next
   175   assume "x\<twosuperior> \<le> y\<twosuperior>"
   176   then have "sqrt (x\<twosuperior>) \<le> sqrt (y\<twosuperior>)" by (rule real_sqrt_le_mono)
   177   then show "\<bar>x\<bar> \<le> \<bar>y\<bar>" by simp
   178 qed
   179 
   180 lemma norm_le_square: "norm(x) <= a \<longleftrightarrow> 0 <= a \<and> x \<bullet> x <= a^2"
   181   apply (simp add: dot_square_norm real_abs_le_square_iff[symmetric])
   182   using norm_ge_zero[of x]
   183   apply arith
   184   done
   185 
   186 lemma norm_ge_square: "norm(x) >= a \<longleftrightarrow> a <= 0 \<or> x \<bullet> x >= a ^ 2"
   187   apply (simp add: dot_square_norm real_abs_le_square_iff[symmetric])
   188   using norm_ge_zero[of x]
   189   apply arith
   190   done
   191 
   192 lemma norm_lt_square: "norm(x) < a \<longleftrightarrow> 0 < a \<and> x \<bullet> x < a^2"
   193   by (metis not_le norm_ge_square)
   194 lemma norm_gt_square: "norm(x) > a \<longleftrightarrow> a < 0 \<or> x \<bullet> x > a^2"
   195   by (metis norm_le_square not_less)
   196 
   197 text{* Dot product in terms of the norm rather than conversely. *}
   198 
   199 lemmas inner_simps = inner.add_left inner.add_right inner.diff_right inner.diff_left 
   200 inner.scaleR_left inner.scaleR_right
   201 
   202 lemma dot_norm: "x \<bullet> y = (norm(x + y) ^2 - norm x ^ 2 - norm y ^ 2) / 2"
   203   unfolding power2_norm_eq_inner inner_simps inner_commute by auto 
   204 
   205 lemma dot_norm_neg: "x \<bullet> y = ((norm x ^ 2 + norm y ^ 2) - norm(x - y) ^ 2) / 2"
   206   unfolding power2_norm_eq_inner inner_simps inner_commute by(auto simp add:algebra_simps)
   207 
   208 text{* Equality of vectors in terms of @{term "op \<bullet>"} products.    *}
   209 
   210 lemma vector_eq: "x = y \<longleftrightarrow> x \<bullet> x = x \<bullet> y \<and> y \<bullet> y = x \<bullet> x" (is "?lhs \<longleftrightarrow> ?rhs")
   211 proof
   212   assume ?lhs then show ?rhs by simp
   213 next
   214   assume ?rhs
   215   then have "x \<bullet> x - x \<bullet> y = 0 \<and> x \<bullet> y - y \<bullet> y = 0" by simp
   216   hence "x \<bullet> (x - y) = 0 \<and> y \<bullet> (x - y) = 0" by (simp add: inner_simps inner_commute)
   217   then have "(x - y) \<bullet> (x - y) = 0" by (simp add: field_simps inner_simps inner_commute)
   218   then show "x = y" by (simp)
   219 qed
   220 
   221 subsection{* General linear decision procedure for normed spaces. *}
   222 
   223 lemma norm_cmul_rule_thm:
   224   fixes x :: "'a::real_normed_vector"
   225   shows "b >= norm(x) ==> \<bar>c\<bar> * b >= norm(scaleR c x)"
   226   unfolding norm_scaleR
   227   apply (erule mult_left_mono)
   228   apply simp
   229   done
   230 
   231   (* FIXME: Move all these theorems into the ML code using lemma antiquotation *)
   232 lemma norm_add_rule_thm:
   233   fixes x1 x2 :: "'a::real_normed_vector"
   234   shows "norm x1 \<le> b1 \<Longrightarrow> norm x2 \<le> b2 \<Longrightarrow> norm (x1 + x2) \<le> b1 + b2"
   235   by (rule order_trans [OF norm_triangle_ineq add_mono])
   236 
   237 lemma ge_iff_diff_ge_0: "(a::'a::linordered_ring) \<ge> b == a - b \<ge> 0"
   238   by (simp add: field_simps)
   239 
   240 lemma pth_1:
   241   fixes x :: "'a::real_normed_vector"
   242   shows "x == scaleR 1 x" by simp
   243 
   244 lemma pth_2:
   245   fixes x :: "'a::real_normed_vector"
   246   shows "x - y == x + -y" by (atomize (full)) simp
   247 
   248 lemma pth_3:
   249   fixes x :: "'a::real_normed_vector"
   250   shows "- x == scaleR (-1) x" by simp
   251 
   252 lemma pth_4:
   253   fixes x :: "'a::real_normed_vector"
   254   shows "scaleR 0 x == 0" and "scaleR c 0 = (0::'a)" by simp_all
   255 
   256 lemma pth_5:
   257   fixes x :: "'a::real_normed_vector"
   258   shows "scaleR c (scaleR d x) == scaleR (c * d) x" by simp
   259 
   260 lemma pth_6:
   261   fixes x :: "'a::real_normed_vector"
   262   shows "scaleR c (x + y) == scaleR c x + scaleR c y"
   263   by (simp add: scaleR_right_distrib)
   264 
   265 lemma pth_7:
   266   fixes x :: "'a::real_normed_vector"
   267   shows "0 + x == x" and "x + 0 == x" by simp_all
   268 
   269 lemma pth_8:
   270   fixes x :: "'a::real_normed_vector"
   271   shows "scaleR c x + scaleR d x == scaleR (c + d) x"
   272   by (simp add: scaleR_left_distrib)
   273 
   274 lemma pth_9:
   275   fixes x :: "'a::real_normed_vector" shows
   276   "(scaleR c x + z) + scaleR d x == scaleR (c + d) x + z"
   277   "scaleR c x + (scaleR d x + z) == scaleR (c + d) x + z"
   278   "(scaleR c x + w) + (scaleR d x + z) == scaleR (c + d) x + (w + z)"
   279   by (simp_all add: algebra_simps)
   280 
   281 lemma pth_a:
   282   fixes x :: "'a::real_normed_vector"
   283   shows "scaleR 0 x + y == y" by simp
   284 
   285 lemma pth_b:
   286   fixes x :: "'a::real_normed_vector" shows
   287   "scaleR c x + scaleR d y == scaleR c x + scaleR d y"
   288   "(scaleR c x + z) + scaleR d y == scaleR c x + (z + scaleR d y)"
   289   "scaleR c x + (scaleR d y + z) == scaleR c x + (scaleR d y + z)"
   290   "(scaleR c x + w) + (scaleR d y + z) == scaleR c x + (w + (scaleR d y + z))"
   291   by (simp_all add: algebra_simps)
   292 
   293 lemma pth_c:
   294   fixes x :: "'a::real_normed_vector" shows
   295   "scaleR c x + scaleR d y == scaleR d y + scaleR c x"
   296   "(scaleR c x + z) + scaleR d y == scaleR d y + (scaleR c x + z)"
   297   "scaleR c x + (scaleR d y + z) == scaleR d y + (scaleR c x + z)"
   298   "(scaleR c x + w) + (scaleR d y + z) == scaleR d y + ((scaleR c x + w) + z)"
   299   by (simp_all add: algebra_simps)
   300 
   301 lemma pth_d:
   302   fixes x :: "'a::real_normed_vector"
   303   shows "x + 0 == x" by simp
   304 
   305 lemma norm_imp_pos_and_ge:
   306   fixes x :: "'a::real_normed_vector"
   307   shows "norm x == n \<Longrightarrow> norm x \<ge> 0 \<and> n \<ge> norm x"
   308   by atomize auto
   309 
   310 lemma real_eq_0_iff_le_ge_0: "(x::real) = 0 == x \<ge> 0 \<and> -x \<ge> 0" by arith
   311 
   312 lemma norm_pths:
   313   fixes x :: "'a::real_normed_vector" shows
   314   "x = y \<longleftrightarrow> norm (x - y) \<le> 0"
   315   "x \<noteq> y \<longleftrightarrow> \<not> (norm (x - y) \<le> 0)"
   316   using norm_ge_zero[of "x - y"] by auto
   317 
   318 use "normarith.ML"
   319 
   320 method_setup norm = {* Scan.succeed (SIMPLE_METHOD' o NormArith.norm_arith_tac)
   321 *} "Proves simple linear statements about vector norms"
   322 
   323 
   324 text{* Hence more metric properties. *}
   325 
   326 lemma dist_triangle_alt:
   327   fixes x y z :: "'a::metric_space"
   328   shows "dist y z <= dist x y + dist x z"
   329 by (rule dist_triangle3)
   330 
   331 lemma dist_pos_lt:
   332   fixes x y :: "'a::metric_space"
   333   shows "x \<noteq> y ==> 0 < dist x y"
   334 by (simp add: zero_less_dist_iff)
   335 
   336 lemma dist_nz:
   337   fixes x y :: "'a::metric_space"
   338   shows "x \<noteq> y \<longleftrightarrow> 0 < dist x y"
   339 by (simp add: zero_less_dist_iff)
   340 
   341 lemma dist_triangle_le:
   342   fixes x y z :: "'a::metric_space"
   343   shows "dist x z + dist y z <= e \<Longrightarrow> dist x y <= e"
   344 by (rule order_trans [OF dist_triangle2])
   345 
   346 lemma dist_triangle_lt:
   347   fixes x y z :: "'a::metric_space"
   348   shows "dist x z + dist y z < e ==> dist x y < e"
   349 by (rule le_less_trans [OF dist_triangle2])
   350 
   351 lemma dist_triangle_half_l:
   352   fixes x1 x2 y :: "'a::metric_space"
   353   shows "dist x1 y < e / 2 \<Longrightarrow> dist x2 y < e / 2 \<Longrightarrow> dist x1 x2 < e"
   354 by (rule dist_triangle_lt [where z=y], simp)
   355 
   356 lemma dist_triangle_half_r:
   357   fixes x1 x2 y :: "'a::metric_space"
   358   shows "dist y x1 < e / 2 \<Longrightarrow> dist y x2 < e / 2 \<Longrightarrow> dist x1 x2 < e"
   359 by (rule dist_triangle_half_l, simp_all add: dist_commute)
   360 
   361 
   362 lemma norm_triangle_half_r:
   363   shows "norm (y - x1) < e / 2 \<Longrightarrow> norm (y - x2) < e / 2 \<Longrightarrow> norm (x1 - x2) < e"
   364   using dist_triangle_half_r unfolding dist_norm[THEN sym] by auto
   365 
   366 lemma norm_triangle_half_l: assumes "norm (x - y) < e / 2" "norm (x' - (y)) < e / 2" 
   367   shows "norm (x - x') < e"
   368   using dist_triangle_half_l[OF assms[unfolded dist_norm[THEN sym]]]
   369   unfolding dist_norm[THEN sym] .
   370 
   371 lemma norm_triangle_le: "norm(x) + norm y <= e ==> norm(x + y) <= e"
   372   by (metis order_trans norm_triangle_ineq)
   373 
   374 lemma norm_triangle_lt: "norm(x) + norm(y) < e ==> norm(x + y) < e"
   375   by (metis basic_trans_rules(21) norm_triangle_ineq)
   376 
   377 lemma dist_triangle_add:
   378   fixes x y x' y' :: "'a::real_normed_vector"
   379   shows "dist (x + y) (x' + y') <= dist x x' + dist y y'"
   380   by norm
   381 
   382 lemma dist_triangle_add_half:
   383   fixes x x' y y' :: "'a::real_normed_vector"
   384   shows "dist x x' < e / 2 \<Longrightarrow> dist y y' < e / 2 \<Longrightarrow> dist(x + y) (x' + y') < e"
   385   by norm
   386 
   387 lemma setsum_clauses:
   388   shows "setsum f {} = 0"
   389   and "finite S \<Longrightarrow> setsum f (insert x S) =
   390                  (if x \<in> S then setsum f S else f x + setsum f S)"
   391   by (auto simp add: insert_absorb)
   392 
   393 lemma setsum_norm:
   394   fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
   395   assumes fS: "finite S"
   396   shows "norm (setsum f S) <= setsum (\<lambda>x. norm(f x)) S"
   397 proof(induct rule: finite_induct[OF fS])
   398   case 1 thus ?case by simp
   399 next
   400   case (2 x S)
   401   from "2.hyps" have "norm (setsum f (insert x S)) \<le> norm (f x) + norm (setsum f S)" by (simp add: norm_triangle_ineq)
   402   also have "\<dots> \<le> norm (f x) + setsum (\<lambda>x. norm(f x)) S"
   403     using "2.hyps" by simp
   404   finally  show ?case  using "2.hyps" by simp
   405 qed
   406 
   407 lemma setsum_norm_le:
   408   fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
   409   assumes fS: "finite S"
   410   and fg: "\<forall>x \<in> S. norm (f x) \<le> g x"
   411   shows "norm (setsum f S) \<le> setsum g S"
   412 proof-
   413   from fg have "setsum (\<lambda>x. norm(f x)) S <= setsum g S"
   414     by - (rule setsum_mono, simp)
   415   then show ?thesis using setsum_norm[OF fS, of f] fg
   416     by arith
   417 qed
   418 
   419 lemma setsum_norm_bound:
   420   fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
   421   assumes fS: "finite S"
   422   and K: "\<forall>x \<in> S. norm (f x) \<le> K"
   423   shows "norm (setsum f S) \<le> of_nat (card S) * K"
   424   using setsum_norm_le[OF fS K] setsum_constant[symmetric]
   425   by simp
   426 
   427 lemma setsum_group:
   428   assumes fS: "finite S" and fT: "finite T" and fST: "f ` S \<subseteq> T"
   429   shows "setsum (\<lambda>y. setsum g {x. x\<in> S \<and> f x = y}) T = setsum g S"
   430   apply (subst setsum_image_gen[OF fS, of g f])
   431   apply (rule setsum_mono_zero_right[OF fT fST])
   432   by (auto intro: setsum_0')
   433 
   434 lemma dot_lsum: "finite S \<Longrightarrow> setsum f S \<bullet> y = setsum (\<lambda>x. f x \<bullet> y) S "
   435   apply(induct rule: finite_induct) by(auto simp add: inner_simps)
   436 
   437 lemma dot_rsum: "finite S \<Longrightarrow> y \<bullet> setsum f S = setsum (\<lambda>x. y \<bullet> f x) S "
   438   apply(induct rule: finite_induct) by(auto simp add: inner_simps)
   439 
   440 lemma vector_eq_ldot: "(\<forall>x. x \<bullet> y = x \<bullet> z) \<longleftrightarrow> y = z"
   441 proof
   442   assume "\<forall>x. x \<bullet> y = x \<bullet> z"
   443   hence "\<forall>x. x \<bullet> (y - z) = 0" by (simp add: inner_simps)
   444   hence "(y - z) \<bullet> (y - z) = 0" ..
   445   thus "y = z" by simp
   446 qed simp
   447 
   448 lemma vector_eq_rdot: "(\<forall>z. x \<bullet> z = y \<bullet> z) \<longleftrightarrow> x = y"
   449 proof
   450   assume "\<forall>z. x \<bullet> z = y \<bullet> z"
   451   hence "\<forall>z. (x - y) \<bullet> z = 0" by (simp add: inner_simps)
   452   hence "(x - y) \<bullet> (x - y) = 0" ..
   453   thus "x = y" by simp
   454 qed simp
   455 
   456 subsection{* Orthogonality. *}
   457 
   458 context real_inner
   459 begin
   460 
   461 definition "orthogonal x y \<longleftrightarrow> (x \<bullet> y = 0)"
   462 
   463 lemma orthogonal_clauses:
   464   "orthogonal a 0"
   465   "orthogonal a x \<Longrightarrow> orthogonal a (c *\<^sub>R x)"
   466   "orthogonal a x \<Longrightarrow> orthogonal a (-x)"
   467   "orthogonal a x \<Longrightarrow> orthogonal a y \<Longrightarrow> orthogonal a (x + y)"
   468   "orthogonal a x \<Longrightarrow> orthogonal a y \<Longrightarrow> orthogonal a (x - y)"
   469   "orthogonal 0 a"
   470   "orthogonal x a \<Longrightarrow> orthogonal (c *\<^sub>R x) a"
   471   "orthogonal x a \<Longrightarrow> orthogonal (-x) a"
   472   "orthogonal x a \<Longrightarrow> orthogonal y a \<Longrightarrow> orthogonal (x + y) a"
   473   "orthogonal x a \<Longrightarrow> orthogonal y a \<Longrightarrow> orthogonal (x - y) a"
   474   unfolding orthogonal_def inner_simps inner_add_left inner_add_right inner_diff_left inner_diff_right (*FIXME*) by auto
   475  
   476 end
   477 
   478 lemma orthogonal_commute: "orthogonal x y \<longleftrightarrow> orthogonal y x"
   479   by (simp add: orthogonal_def inner_commute)
   480 
   481 subsection{* Linear functions. *}
   482 
   483 definition
   484   linear :: "('a::real_vector \<Rightarrow> 'b::real_vector) \<Rightarrow> bool" where
   485   "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)"
   486 
   487 lemma linearI: assumes "\<And>x y. f (x + y) = f x + f y" "\<And>c x. f (c *\<^sub>R x) = c *\<^sub>R f x"
   488   shows "linear f" using assms unfolding linear_def by auto
   489 
   490 lemma linear_compose_cmul: "linear f ==> linear (\<lambda>x. c *\<^sub>R f x)"
   491   by (simp add: linear_def algebra_simps)
   492 
   493 lemma linear_compose_neg: "linear f ==> linear (\<lambda>x. -(f(x)))"
   494   by (simp add: linear_def)
   495 
   496 lemma linear_compose_add: "linear f \<Longrightarrow> linear g ==> linear (\<lambda>x. f(x) + g(x))"
   497   by (simp add: linear_def algebra_simps)
   498 
   499 lemma linear_compose_sub: "linear f \<Longrightarrow> linear g ==> linear (\<lambda>x. f x - g x)"
   500   by (simp add: linear_def algebra_simps)
   501 
   502 lemma linear_compose: "linear f \<Longrightarrow> linear g ==> linear (g o f)"
   503   by (simp add: linear_def)
   504 
   505 lemma linear_id: "linear id" by (simp add: linear_def id_def)
   506 
   507 lemma linear_zero: "linear (\<lambda>x. 0)" by (simp add: linear_def)
   508 
   509 lemma linear_compose_setsum:
   510   assumes fS: "finite S" and lS: "\<forall>a \<in> S. linear (f a)"
   511   shows "linear(\<lambda>x. setsum (\<lambda>a. f a x) S)"
   512   using lS
   513   apply (induct rule: finite_induct[OF fS])
   514   by (auto simp add: linear_zero intro: linear_compose_add)
   515 
   516 lemma linear_0: "linear f \<Longrightarrow> f 0 = 0"
   517   unfolding linear_def
   518   apply clarsimp
   519   apply (erule allE[where x="0::'a"])
   520   apply simp
   521   done
   522 
   523 lemma linear_cmul: "linear f ==> f(c *\<^sub>R x) = c *\<^sub>R f x" by (simp add: linear_def)
   524 
   525 lemma linear_neg: "linear f ==> f (-x) = - f x"
   526   using linear_cmul [where c="-1"] by simp
   527 
   528 lemma linear_add: "linear f ==> f(x + y) = f x + f y" by (metis linear_def)
   529 
   530 lemma linear_sub: "linear f ==> f(x - y) = f x - f y"
   531   by (simp add: diff_minus linear_add linear_neg)
   532 
   533 lemma linear_setsum:
   534   assumes lf: "linear f" and fS: "finite S"
   535   shows "f (setsum g S) = setsum (f o g) S"
   536 proof (induct rule: finite_induct[OF fS])
   537   case 1 thus ?case by (simp add: linear_0[OF lf])
   538 next
   539   case (2 x F)
   540   have "f (setsum g (insert x F)) = f (g x + setsum g F)" using "2.hyps"
   541     by simp
   542   also have "\<dots> = f (g x) + f (setsum g F)" using linear_add[OF lf] by simp
   543   also have "\<dots> = setsum (f o g) (insert x F)" using "2.hyps" by simp
   544   finally show ?case .
   545 qed
   546 
   547 lemma linear_setsum_mul:
   548   assumes lf: "linear f" and fS: "finite S"
   549   shows "f (setsum (\<lambda>i. c i *\<^sub>R v i) S) = setsum (\<lambda>i. c i *\<^sub>R f (v i)) S"
   550   using linear_setsum[OF lf fS, of "\<lambda>i. c i *\<^sub>R v i" , unfolded o_def]
   551   linear_cmul[OF lf] by simp
   552 
   553 lemma linear_injective_0:
   554   assumes lf: "linear f"
   555   shows "inj f \<longleftrightarrow> (\<forall>x. f x = 0 \<longrightarrow> x = 0)"
   556 proof-
   557   have "inj f \<longleftrightarrow> (\<forall> x y. f x = f y \<longrightarrow> x = y)" by (simp add: inj_on_def)
   558   also have "\<dots> \<longleftrightarrow> (\<forall> x y. f x - f y = 0 \<longrightarrow> x - y = 0)" by simp
   559   also have "\<dots> \<longleftrightarrow> (\<forall> x y. f (x - y) = 0 \<longrightarrow> x - y = 0)"
   560     by (simp add: linear_sub[OF lf])
   561   also have "\<dots> \<longleftrightarrow> (\<forall> x. f x = 0 \<longrightarrow> x = 0)" by auto
   562   finally show ?thesis .
   563 qed
   564 
   565 subsection{* Bilinear functions. *}
   566 
   567 definition "bilinear f \<longleftrightarrow> (\<forall>x. linear(\<lambda>y. f x y)) \<and> (\<forall>y. linear(\<lambda>x. f x y))"
   568 
   569 lemma bilinear_ladd: "bilinear h ==> h (x + y) z = (h x z) + (h y z)"
   570   by (simp add: bilinear_def linear_def)
   571 lemma bilinear_radd: "bilinear h ==> h x (y + z) = (h x y) + (h x z)"
   572   by (simp add: bilinear_def linear_def)
   573 
   574 lemma bilinear_lmul: "bilinear h ==> h (c *\<^sub>R x) y = c *\<^sub>R (h x y)"
   575   by (simp add: bilinear_def linear_def)
   576 
   577 lemma bilinear_rmul: "bilinear h ==> h x (c *\<^sub>R y) = c *\<^sub>R (h x y)"
   578   by (simp add: bilinear_def linear_def)
   579 
   580 lemma bilinear_lneg: "bilinear h ==> h (- x) y = -(h x y)"
   581   by (simp only: scaleR_minus1_left [symmetric] bilinear_lmul)
   582 
   583 lemma bilinear_rneg: "bilinear h ==> h x (- y) = - h x y"
   584   by (simp only: scaleR_minus1_left [symmetric] bilinear_rmul)
   585 
   586 lemma  (in ab_group_add) eq_add_iff: "x = x + y \<longleftrightarrow> y = 0"
   587   using add_imp_eq[of x y 0] by auto
   588 
   589 lemma bilinear_lzero:
   590   assumes bh: "bilinear h" shows "h 0 x = 0"
   591   using bilinear_ladd[OF bh, of 0 0 x]
   592     by (simp add: eq_add_iff field_simps)
   593 
   594 lemma bilinear_rzero:
   595   assumes bh: "bilinear h" shows "h x 0 = 0"
   596   using bilinear_radd[OF bh, of x 0 0 ]
   597     by (simp add: eq_add_iff field_simps)
   598 
   599 lemma bilinear_lsub: "bilinear h ==> h (x - y) z = h x z - h y z"
   600   by (simp  add: diff_minus bilinear_ladd bilinear_lneg)
   601 
   602 lemma bilinear_rsub: "bilinear h ==> h z (x - y) = h z x - h z y"
   603   by (simp  add: diff_minus bilinear_radd bilinear_rneg)
   604 
   605 lemma bilinear_setsum:
   606   assumes bh: "bilinear h" and fS: "finite S" and fT: "finite T"
   607   shows "h (setsum f S) (setsum g T) = setsum (\<lambda>(i,j). h (f i) (g j)) (S \<times> T) "
   608 proof-
   609   have "h (setsum f S) (setsum g T) = setsum (\<lambda>x. h (f x) (setsum g T)) S"
   610     apply (rule linear_setsum[unfolded o_def])
   611     using bh fS by (auto simp add: bilinear_def)
   612   also have "\<dots> = setsum (\<lambda>x. setsum (\<lambda>y. h (f x) (g y)) T) S"
   613     apply (rule setsum_cong, simp)
   614     apply (rule linear_setsum[unfolded o_def])
   615     using bh fT by (auto simp add: bilinear_def)
   616   finally show ?thesis unfolding setsum_cartesian_product .
   617 qed
   618 
   619 subsection{* Adjoints. *}
   620 
   621 definition "adjoint f = (SOME f'. \<forall>x y. f x \<bullet> y = x \<bullet> f' y)"
   622 
   623 lemma adjoint_unique:
   624   assumes "\<forall>x y. inner (f x) y = inner x (g y)"
   625   shows "adjoint f = g"
   626 unfolding adjoint_def
   627 proof (rule some_equality)
   628   show "\<forall>x y. inner (f x) y = inner x (g y)" using assms .
   629 next
   630   fix h assume "\<forall>x y. inner (f x) y = inner x (h y)"
   631   hence "\<forall>x y. inner x (g y) = inner x (h y)" using assms by simp
   632   hence "\<forall>x y. inner x (g y - h y) = 0" by (simp add: inner_diff_right)
   633   hence "\<forall>y. inner (g y - h y) (g y - h y) = 0" by simp
   634   hence "\<forall>y. h y = g y" by simp
   635   thus "h = g" by (simp add: ext)
   636 qed
   637 
   638 lemma choice_iff: "(\<forall>x. \<exists>y. P x y) \<longleftrightarrow> (\<exists>f. \<forall>x. P x (f x))" by metis
   639 
   640 subsection{* Interlude: Some properties of real sets *}
   641 
   642 lemma seq_mono_lemma: assumes "\<forall>(n::nat) \<ge> m. (d n :: real) < e n" and "\<forall>n \<ge> m. e n <= e m"
   643   shows "\<forall>n \<ge> m. d n < e m"
   644   using prems apply auto
   645   apply (erule_tac x="n" in allE)
   646   apply (erule_tac x="n" in allE)
   647   apply auto
   648   done
   649 
   650 
   651 lemma infinite_enumerate: assumes fS: "infinite S"
   652   shows "\<exists>r. subseq r \<and> (\<forall>n. r n \<in> S)"
   653 unfolding subseq_def
   654 using enumerate_in_set[OF fS] enumerate_mono[of _ _ S] fS by auto
   655 
   656 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)"
   657 apply auto
   658 apply (rule_tac x="d/2" in exI)
   659 apply auto
   660 done
   661 
   662 
   663 lemma triangle_lemma:
   664   assumes x: "0 <= (x::real)" and y:"0 <= y" and z: "0 <= z" and xy: "x^2 <= y^2 + z^2"
   665   shows "x <= y + z"
   666 proof-
   667   have "y^2 + z^2 \<le> y^2 + 2*y*z + z^2" using z y by (simp add: mult_nonneg_nonneg)
   668   with xy have th: "x ^2 \<le> (y+z)^2" by (simp add: power2_eq_square field_simps)
   669   from y z have yz: "y + z \<ge> 0" by arith
   670   from power2_le_imp_le[OF th yz] show ?thesis .
   671 qed
   672 
   673 text {* TODO: move to NthRoot *}
   674 lemma sqrt_add_le_add_sqrt:
   675   assumes x: "0 \<le> x" and y: "0 \<le> y"
   676   shows "sqrt (x + y) \<le> sqrt x + sqrt y"
   677 apply (rule power2_le_imp_le)
   678 apply (simp add: real_sum_squared_expand add_nonneg_nonneg x y)
   679 apply (simp add: mult_nonneg_nonneg x y)
   680 apply (simp add: add_nonneg_nonneg x y)
   681 done
   682 
   683 subsection {* A generic notion of "hull" (convex, affine, conic hull and closure). *}
   684 
   685 definition hull :: "'a set set \<Rightarrow> 'a set \<Rightarrow> 'a set" (infixl "hull" 75) where
   686   "S hull s = Inter {t. t \<in> S \<and> s \<subseteq> t}"
   687 
   688 lemma hull_same: "s \<in> S \<Longrightarrow> S hull s = s"
   689   unfolding hull_def by auto
   690 
   691 lemma hull_in: "(\<And>T. T \<subseteq> S ==> Inter T \<in> S) ==> (S hull s) \<in> S"
   692 unfolding hull_def subset_iff by auto
   693 
   694 lemma hull_eq: "(\<And>T. T \<subseteq> S ==> Inter T \<in> S) ==> (S hull s) = s \<longleftrightarrow> s \<in> S"
   695 using hull_same[of s S] hull_in[of S s] by metis
   696 
   697 
   698 lemma hull_hull: "S hull (S hull s) = S hull s"
   699   unfolding hull_def by blast
   700 
   701 lemma hull_subset[intro]: "s \<subseteq> (S hull s)"
   702   unfolding hull_def by blast
   703 
   704 lemma hull_mono: " s \<subseteq> t ==> (S hull s) \<subseteq> (S hull t)"
   705   unfolding hull_def by blast
   706 
   707 lemma hull_antimono: "S \<subseteq> T ==> (T hull s) \<subseteq> (S hull s)"
   708   unfolding hull_def by blast
   709 
   710 lemma hull_minimal: "s \<subseteq> t \<Longrightarrow> t \<in> S ==> (S hull s) \<subseteq> t"
   711   unfolding hull_def by blast
   712 
   713 lemma subset_hull: "t \<in> S ==> S hull s \<subseteq> t \<longleftrightarrow>  s \<subseteq> t"
   714   unfolding hull_def by blast
   715 
   716 lemma hull_unique: "s \<subseteq> t \<Longrightarrow> t \<in> S \<Longrightarrow> (\<And>t'. s \<subseteq> t' \<Longrightarrow> t' \<in> S ==> t \<subseteq> t')
   717            ==> (S hull s = t)"
   718 unfolding hull_def by auto
   719 
   720 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"
   721   using hull_minimal[of S "{x. P x}" Q]
   722   by (auto simp add: subset_eq Collect_def mem_def)
   723 
   724 lemma hull_inc: "x \<in> S \<Longrightarrow> x \<in> P hull S" by (metis hull_subset subset_eq)
   725 
   726 lemma hull_union_subset: "(S hull s) \<union> (S hull t) \<subseteq> (S hull (s \<union> t))"
   727 unfolding Un_subset_iff by (metis hull_mono Un_upper1 Un_upper2)
   728 
   729 lemma hull_union: assumes T: "\<And>T. T \<subseteq> S ==> Inter T \<in> S"
   730   shows "S hull (s \<union> t) = S hull (S hull s \<union> S hull t)"
   731 apply rule
   732 apply (rule hull_mono)
   733 unfolding Un_subset_iff
   734 apply (metis hull_subset Un_upper1 Un_upper2 subset_trans)
   735 apply (rule hull_minimal)
   736 apply (metis hull_union_subset)
   737 apply (metis hull_in T)
   738 done
   739 
   740 lemma hull_redundant_eq: "a \<in> (S hull s) \<longleftrightarrow> (S hull (insert a s) = S hull s)"
   741   unfolding hull_def by blast
   742 
   743 lemma hull_redundant: "a \<in> (S hull s) ==> (S hull (insert a s) = S hull s)"
   744 by (metis hull_redundant_eq)
   745 
   746 text{* Archimedian properties and useful consequences. *}
   747 
   748 lemma real_arch_simple: "\<exists>n. x <= real (n::nat)"
   749   using reals_Archimedean2[of x] apply auto by (rule_tac x="Suc n" in exI, auto)
   750 lemmas real_arch_lt = reals_Archimedean2
   751 
   752 lemmas real_arch = reals_Archimedean3
   753 
   754 lemma real_arch_inv: "0 < e \<longleftrightarrow> (\<exists>n::nat. n \<noteq> 0 \<and> 0 < inverse (real n) \<and> inverse (real n) < e)"
   755   using reals_Archimedean
   756   apply (auto simp add: field_simps)
   757   apply (subgoal_tac "inverse (real n) > 0")
   758   apply arith
   759   apply simp
   760   done
   761 
   762 lemma real_pow_lbound: "0 <= x ==> 1 + real n * x <= (1 + x) ^ n"
   763 proof(induct n)
   764   case 0 thus ?case by simp
   765 next
   766   case (Suc n)
   767   hence h: "1 + real n * x \<le> (1 + x) ^ n" by simp
   768   from h have p: "1 \<le> (1 + x) ^ n" using Suc.prems by simp
   769   from h have "1 + real n * x + x \<le> (1 + x) ^ n + x" by simp
   770   also have "\<dots> \<le> (1 + x) ^ Suc n" apply (subst diff_le_0_iff_le[symmetric])
   771     apply (simp add: field_simps)
   772     using mult_left_mono[OF p Suc.prems] by simp
   773   finally show ?case  by (simp add: real_of_nat_Suc field_simps)
   774 qed
   775 
   776 lemma real_arch_pow: assumes x: "1 < (x::real)" shows "\<exists>n. y < x^n"
   777 proof-
   778   from x have x0: "x - 1 > 0" by arith
   779   from real_arch[OF x0, rule_format, of y]
   780   obtain n::nat where n:"y < real n * (x - 1)" by metis
   781   from x0 have x00: "x- 1 \<ge> 0" by arith
   782   from real_pow_lbound[OF x00, of n] n
   783   have "y < x^n" by auto
   784   then show ?thesis by metis
   785 qed
   786 
   787 lemma real_arch_pow2: "\<exists>n. (x::real) < 2^ n"
   788   using real_arch_pow[of 2 x] by simp
   789 
   790 lemma real_arch_pow_inv: assumes y: "(y::real) > 0" and x1: "x < 1"
   791   shows "\<exists>n. x^n < y"
   792 proof-
   793   {assume x0: "x > 0"
   794     from x0 x1 have ix: "1 < 1/x" by (simp add: field_simps)
   795     from real_arch_pow[OF ix, of "1/y"]
   796     obtain n where n: "1/y < (1/x)^n" by blast
   797     then
   798     have ?thesis using y x0 by (auto simp add: field_simps power_divide) }
   799   moreover
   800   {assume "\<not> x > 0" with y x1 have ?thesis apply auto by (rule exI[where x=1], auto)}
   801   ultimately show ?thesis by metis
   802 qed
   803 
   804 lemma forall_pos_mono: "(\<And>d e::real. d < e \<Longrightarrow> P d ==> P e) \<Longrightarrow> (\<And>n::nat. n \<noteq> 0 ==> P(inverse(real n))) \<Longrightarrow> (\<And>e. 0 < e ==> P e)"
   805   by (metis real_arch_inv)
   806 
   807 lemma forall_pos_mono_1: "(\<And>d e::real. d < e \<Longrightarrow> P d ==> P e) \<Longrightarrow> (\<And>n. P(inverse(real (Suc n)))) ==> 0 < e ==> P e"
   808   apply (rule forall_pos_mono)
   809   apply auto
   810   apply (atomize)
   811   apply (erule_tac x="n - 1" in allE)
   812   apply auto
   813   done
   814 
   815 lemma real_archimedian_rdiv_eq_0: assumes x0: "x \<ge> 0" and c: "c \<ge> 0" and xc: "\<forall>(m::nat)>0. real m * x \<le> c"
   816   shows "x = 0"
   817 proof-
   818   {assume "x \<noteq> 0" with x0 have xp: "x > 0" by arith
   819     from real_arch[OF xp, rule_format, of c] obtain n::nat where n: "c < real n * x"  by blast
   820     with xc[rule_format, of n] have "n = 0" by arith
   821     with n c have False by simp}
   822   then show ?thesis by blast
   823 qed
   824 
   825 subsection {* Geometric progression *}
   826 
   827 lemma sum_gp_basic: "((1::'a::{field}) - x) * setsum (\<lambda>i. x^i) {0 .. n} = (1 - x^(Suc n))"
   828   (is "?lhs = ?rhs")
   829 proof-
   830   {assume x1: "x = 1" hence ?thesis by simp}
   831   moreover
   832   {assume x1: "x\<noteq>1"
   833     hence x1': "x - 1 \<noteq> 0" "1 - x \<noteq> 0" "x - 1 = - (1 - x)" "- (1 - x) \<noteq> 0" by auto
   834     from geometric_sum[OF x1, of "Suc n", unfolded x1']
   835     have "(- (1 - x)) * setsum (\<lambda>i. x^i) {0 .. n} = - (1 - x^(Suc n))"
   836       unfolding atLeastLessThanSuc_atLeastAtMost
   837       using x1' apply (auto simp only: field_simps)
   838       apply (simp add: field_simps)
   839       done
   840     then have ?thesis by (simp add: field_simps) }
   841   ultimately show ?thesis by metis
   842 qed
   843 
   844 lemma sum_gp_multiplied: assumes mn: "m <= n"
   845   shows "((1::'a::{field}) - x) * setsum (op ^ x) {m..n} = x^m - x^ Suc n"
   846   (is "?lhs = ?rhs")
   847 proof-
   848   let ?S = "{0..(n - m)}"
   849   from mn have mn': "n - m \<ge> 0" by arith
   850   let ?f = "op + m"
   851   have i: "inj_on ?f ?S" unfolding inj_on_def by auto
   852   have f: "?f ` ?S = {m..n}"
   853     using mn apply (auto simp add: image_iff Bex_def) by arith
   854   have th: "op ^ x o op + m = (\<lambda>i. x^m * x^i)"
   855     by (rule ext, simp add: power_add power_mult)
   856   from setsum_reindex[OF i, of "op ^ x", unfolded f th setsum_right_distrib[symmetric]]
   857   have "?lhs = x^m * ((1 - x) * setsum (op ^ x) {0..n - m})" by simp
   858   then show ?thesis unfolding sum_gp_basic using mn
   859     by (simp add: field_simps power_add[symmetric])
   860 qed
   861 
   862 lemma sum_gp: "setsum (op ^ (x::'a::{field})) {m .. n} =
   863    (if n < m then 0 else if x = 1 then of_nat ((n + 1) - m)
   864                     else (x^ m - x^ (Suc n)) / (1 - x))"
   865 proof-
   866   {assume nm: "n < m" hence ?thesis by simp}
   867   moreover
   868   {assume "\<not> n < m" hence nm: "m \<le> n" by arith
   869     {assume x: "x = 1"  hence ?thesis by simp}
   870     moreover
   871     {assume x: "x \<noteq> 1" hence nz: "1 - x \<noteq> 0" by simp
   872       from sum_gp_multiplied[OF nm, of x] nz have ?thesis by (simp add: field_simps)}
   873     ultimately have ?thesis by metis
   874   }
   875   ultimately show ?thesis by metis
   876 qed
   877 
   878 lemma sum_gp_offset: "setsum (op ^ (x::'a::{field})) {m .. m+n} =
   879   (if x = 1 then of_nat n + 1 else x^m * (1 - x^Suc n) / (1 - x))"
   880   unfolding sum_gp[of x m "m + n"] power_Suc
   881   by (simp add: field_simps power_add)
   882 
   883 
   884 subsection{* A bit of linear algebra. *}
   885 
   886 definition (in real_vector)
   887   subspace :: "'a set \<Rightarrow> bool" where
   888   "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 )"
   889 
   890 definition (in real_vector) "span S = (subspace hull S)"
   891 definition (in real_vector) "dependent S \<longleftrightarrow> (\<exists>a \<in> S. a \<in> span(S - {a}))"
   892 abbreviation (in real_vector) "independent s == ~(dependent s)"
   893 
   894 text {* Closure properties of subspaces. *}
   895 
   896 lemma subspace_UNIV[simp]: "subspace(UNIV)" by (simp add: subspace_def)
   897 
   898 lemma (in real_vector) subspace_0: "subspace S ==> 0 \<in> S" by (metis subspace_def)
   899 
   900 lemma (in real_vector) subspace_add: "subspace S \<Longrightarrow> x \<in> S \<Longrightarrow> y \<in> S ==> x + y \<in> S"
   901   by (metis subspace_def)
   902 
   903 lemma (in real_vector) subspace_mul: "subspace S \<Longrightarrow> x \<in> S \<Longrightarrow> c *\<^sub>R x \<in> S"
   904   by (metis subspace_def)
   905 
   906 lemma subspace_neg: "subspace S \<Longrightarrow> x \<in> S \<Longrightarrow> - x \<in> S"
   907   by (metis scaleR_minus1_left subspace_mul)
   908 
   909 lemma subspace_sub: "subspace S \<Longrightarrow> x \<in> S \<Longrightarrow> y \<in> S \<Longrightarrow> x - y \<in> S"
   910   by (metis diff_minus subspace_add subspace_neg)
   911 
   912 lemma (in real_vector) subspace_setsum:
   913   assumes sA: "subspace A" and fB: "finite B"
   914   and f: "\<forall>x\<in> B. f x \<in> A"
   915   shows "setsum f B \<in> A"
   916   using  fB f sA
   917   apply(induct rule: finite_induct[OF fB])
   918   by (simp add: subspace_def sA, auto simp add: sA subspace_add)
   919 
   920 lemma subspace_linear_image:
   921   assumes lf: "linear f" and sS: "subspace S"
   922   shows "subspace(f ` S)"
   923   using lf sS linear_0[OF lf]
   924   unfolding linear_def subspace_def
   925   apply (auto simp add: image_iff)
   926   apply (rule_tac x="x + y" in bexI, auto)
   927   apply (rule_tac x="c *\<^sub>R x" in bexI, auto)
   928   done
   929 
   930 lemma subspace_linear_preimage: "linear f ==> subspace S ==> subspace {x. f x \<in> S}"
   931   by (auto simp add: subspace_def linear_def linear_0[of f])
   932 
   933 lemma subspace_trivial: "subspace {0}"
   934   by (simp add: subspace_def)
   935 
   936 lemma (in real_vector) subspace_inter: "subspace A \<Longrightarrow> subspace B ==> subspace (A \<inter> B)"
   937   by (simp add: subspace_def)
   938 
   939 lemma (in real_vector) span_mono: "A \<subseteq> B ==> span A \<subseteq> span B"
   940   by (metis span_def hull_mono)
   941 
   942 lemma (in real_vector) subspace_span: "subspace(span S)"
   943   unfolding span_def
   944   apply (rule hull_in[unfolded mem_def])
   945   apply (simp only: subspace_def Inter_iff Int_iff subset_eq)
   946   apply auto
   947   apply (erule_tac x="X" in ballE)
   948   apply (simp add: mem_def)
   949   apply blast
   950   apply (erule_tac x="X" in ballE)
   951   apply (erule_tac x="X" in ballE)
   952   apply (erule_tac x="X" in ballE)
   953   apply (clarsimp simp add: mem_def)
   954   apply simp
   955   apply simp
   956   apply simp
   957   apply (erule_tac x="X" in ballE)
   958   apply (erule_tac x="X" in ballE)
   959   apply (simp add: mem_def)
   960   apply simp
   961   apply simp
   962   done
   963 
   964 lemma (in real_vector) span_clauses:
   965   "a \<in> S ==> a \<in> span S"
   966   "0 \<in> span S"
   967   "x\<in> span S \<Longrightarrow> y \<in> span S ==> x + y \<in> span S"
   968   "x \<in> span S \<Longrightarrow> c *\<^sub>R x \<in> span S"
   969   by (metis span_def hull_subset subset_eq)
   970      (metis subspace_span subspace_def)+
   971 
   972 lemma (in real_vector) span_induct: assumes SP: "\<And>x. x \<in> S ==> P x"
   973   and P: "subspace P" and x: "x \<in> span S" shows "P x"
   974 proof-
   975   from SP have SP': "S \<subseteq> P" by (simp add: mem_def subset_eq)
   976   from P have P': "P \<in> subspace" by (simp add: mem_def)
   977   from x hull_minimal[OF SP' P', unfolded span_def[symmetric]]
   978   show "P x" by (metis mem_def subset_eq)
   979 qed
   980 
   981 lemma span_empty[simp]: "span {} = {0}"
   982   apply (simp add: span_def)
   983   apply (rule hull_unique)
   984   apply (auto simp add: mem_def subspace_def)
   985   unfolding mem_def[of "0::'a", symmetric]
   986   apply simp
   987   done
   988 
   989 lemma (in real_vector) independent_empty[intro]: "independent {}"
   990   by (simp add: dependent_def)
   991 
   992 lemma dependent_single[simp]:
   993   "dependent {x} \<longleftrightarrow> x = 0"
   994   unfolding dependent_def by auto
   995 
   996 lemma (in real_vector) independent_mono: "independent A \<Longrightarrow> B \<subseteq> A ==> independent B"
   997   apply (clarsimp simp add: dependent_def span_mono)
   998   apply (subgoal_tac "span (B - {a}) \<le> span (A - {a})")
   999   apply force
  1000   apply (rule span_mono)
  1001   apply auto
  1002   done
  1003 
  1004 lemma (in real_vector) span_subspace: "A \<subseteq> B \<Longrightarrow> B \<le> span A \<Longrightarrow>  subspace B \<Longrightarrow> span A = B"
  1005   by (metis order_antisym span_def hull_minimal mem_def)
  1006 
  1007 lemma (in real_vector) span_induct': assumes SP: "\<forall>x \<in> S. P x"
  1008   and P: "subspace P" shows "\<forall>x \<in> span S. P x"
  1009   using span_induct SP P by blast
  1010 
  1011 inductive (in real_vector) span_induct_alt_help for S:: "'a \<Rightarrow> bool"
  1012   where
  1013   span_induct_alt_help_0: "span_induct_alt_help S 0"
  1014   | span_induct_alt_help_S: "x \<in> S \<Longrightarrow> span_induct_alt_help S z \<Longrightarrow> span_induct_alt_help S (c *\<^sub>R x + z)"
  1015 
  1016 lemma span_induct_alt':
  1017   assumes h0: "h 0" and hS: "\<And>c x y. x \<in> S \<Longrightarrow> h y \<Longrightarrow> h (c *\<^sub>R x + y)" shows "\<forall>x \<in> span S. h x"
  1018 proof-
  1019   {fix x:: "'a" assume x: "span_induct_alt_help S x"
  1020     have "h x"
  1021       apply (rule span_induct_alt_help.induct[OF x])
  1022       apply (rule h0)
  1023       apply (rule hS, assumption, assumption)
  1024       done}
  1025   note th0 = this
  1026   {fix x assume x: "x \<in> span S"
  1027 
  1028     have "span_induct_alt_help S x"
  1029       proof(rule span_induct[where x=x and S=S])
  1030         show "x \<in> span S" using x .
  1031       next
  1032         fix x assume xS : "x \<in> S"
  1033           from span_induct_alt_help_S[OF xS span_induct_alt_help_0, of 1]
  1034           show "span_induct_alt_help S x" by simp
  1035         next
  1036         have "span_induct_alt_help S 0" by (rule span_induct_alt_help_0)
  1037         moreover
  1038         {fix x y assume h: "span_induct_alt_help S x" "span_induct_alt_help S y"
  1039           from h
  1040           have "span_induct_alt_help S (x + y)"
  1041             apply (induct rule: span_induct_alt_help.induct)
  1042             apply simp
  1043             unfolding add_assoc
  1044             apply (rule span_induct_alt_help_S)
  1045             apply assumption
  1046             apply simp
  1047             done}
  1048         moreover
  1049         {fix c x assume xt: "span_induct_alt_help S x"
  1050           then have "span_induct_alt_help S (c *\<^sub>R x)"
  1051             apply (induct rule: span_induct_alt_help.induct)
  1052             apply (simp add: span_induct_alt_help_0)
  1053             apply (simp add: scaleR_right_distrib)
  1054             apply (rule span_induct_alt_help_S)
  1055             apply assumption
  1056             apply simp
  1057             done
  1058         }
  1059         ultimately show "subspace (span_induct_alt_help S)"
  1060           unfolding subspace_def mem_def Ball_def by blast
  1061       qed}
  1062   with th0 show ?thesis by blast
  1063 qed
  1064 
  1065 lemma span_induct_alt:
  1066   assumes h0: "h 0" and hS: "\<And>c x y. x \<in> S \<Longrightarrow> h y \<Longrightarrow> h (c *\<^sub>R x + y)" and x: "x \<in> span S"
  1067   shows "h x"
  1068 using span_induct_alt'[of h S] h0 hS x by blast
  1069 
  1070 text {* Individual closure properties. *}
  1071 
  1072 lemma span_span: "span (span A) = span A"
  1073   unfolding span_def hull_hull ..
  1074 
  1075 lemma (in real_vector) span_superset: "x \<in> S ==> x \<in> span S" by (metis span_clauses(1))
  1076 
  1077 lemma (in real_vector) span_0: "0 \<in> span S" by (metis subspace_span subspace_0)
  1078 
  1079 lemma span_inc: "S \<subseteq> span S"
  1080   by (metis subset_eq span_superset)
  1081 
  1082 lemma (in real_vector) dependent_0: assumes "0\<in>A" shows "dependent A"
  1083   unfolding dependent_def apply(rule_tac x=0 in bexI)
  1084   using assms span_0 by auto
  1085 
  1086 lemma (in real_vector) span_add: "x \<in> span S \<Longrightarrow> y \<in> span S ==> x + y \<in> span S"
  1087   by (metis subspace_add subspace_span)
  1088 
  1089 lemma (in real_vector) span_mul: "x \<in> span S ==> (c *\<^sub>R x) \<in> span S"
  1090   by (metis subspace_span subspace_mul)
  1091 
  1092 lemma span_neg: "x \<in> span S ==> - x \<in> span S"
  1093   by (metis subspace_neg subspace_span)
  1094 
  1095 lemma span_sub: "x \<in> span S \<Longrightarrow> y \<in> span S ==> x - y \<in> span S"
  1096   by (metis subspace_span subspace_sub)
  1097 
  1098 lemma (in real_vector) span_setsum: "finite A \<Longrightarrow> \<forall>x \<in> A. f x \<in> span S ==> setsum f A \<in> span S"
  1099   by (rule subspace_setsum, rule subspace_span)
  1100 
  1101 lemma span_add_eq: "x \<in> span S \<Longrightarrow> x + y \<in> span S \<longleftrightarrow> y \<in> span S"
  1102   apply (auto simp only: span_add span_sub)
  1103   apply (subgoal_tac "(x + y) - x \<in> span S", simp)
  1104   by (simp only: span_add span_sub)
  1105 
  1106 text {* Mapping under linear image. *}
  1107 
  1108 lemma span_linear_image: assumes lf: "linear f"
  1109   shows "span (f ` S) = f ` (span S)"
  1110 proof-
  1111   {fix x
  1112     assume x: "x \<in> span (f ` S)"
  1113     have "x \<in> f ` span S"
  1114       apply (rule span_induct[where x=x and S = "f ` S"])
  1115       apply (clarsimp simp add: image_iff)
  1116       apply (frule span_superset)
  1117       apply blast
  1118       apply (simp only: mem_def)
  1119       apply (rule subspace_linear_image[OF lf])
  1120       apply (rule subspace_span)
  1121       apply (rule x)
  1122       done}
  1123   moreover
  1124   {fix x assume x: "x \<in> span S"
  1125     have th0:"(\<lambda>a. f a \<in> span (f ` S)) = {x. f x \<in> span (f ` S)}" apply (rule set_eqI)
  1126       unfolding mem_def Collect_def ..
  1127     have "f x \<in> span (f ` S)"
  1128       apply (rule span_induct[where S=S])
  1129       apply (rule span_superset)
  1130       apply simp
  1131       apply (subst th0)
  1132       apply (rule subspace_linear_preimage[OF lf subspace_span, of "f ` S"])
  1133       apply (rule x)
  1134       done}
  1135   ultimately show ?thesis by blast
  1136 qed
  1137 
  1138 text {* The key breakdown property. *}
  1139 
  1140 lemma span_breakdown:
  1141   assumes bS: "b \<in> S" and aS: "a \<in> span S"
  1142   shows "\<exists>k. a - k *\<^sub>R b \<in> span (S - {b})" (is "?P a")
  1143 proof-
  1144   {fix x assume xS: "x \<in> S"
  1145     {assume ab: "x = b"
  1146       then have "?P x"
  1147         apply simp
  1148         apply (rule exI[where x="1"], simp)
  1149         by (rule span_0)}
  1150     moreover
  1151     {assume ab: "x \<noteq> b"
  1152       then have "?P x"  using xS
  1153         apply -
  1154         apply (rule exI[where x=0])
  1155         apply (rule span_superset)
  1156         by simp}
  1157     ultimately have "?P x" by blast}
  1158   moreover have "subspace ?P"
  1159     unfolding subspace_def
  1160     apply auto
  1161     apply (simp add: mem_def)
  1162     apply (rule exI[where x=0])
  1163     using span_0[of "S - {b}"]
  1164     apply (simp add: mem_def)
  1165     apply (clarsimp simp add: mem_def)
  1166     apply (rule_tac x="k + ka" in exI)
  1167     apply (subgoal_tac "x + y - (k + ka) *\<^sub>R b = (x - k*\<^sub>R b) + (y - ka *\<^sub>R b)")
  1168     apply (simp only: )
  1169     apply (rule span_add[unfolded mem_def])
  1170     apply assumption+
  1171     apply (simp add: algebra_simps)
  1172     apply (clarsimp simp add: mem_def)
  1173     apply (rule_tac x= "c*k" in exI)
  1174     apply (subgoal_tac "c *\<^sub>R x - (c * k) *\<^sub>R b = c*\<^sub>R (x - k*\<^sub>R b)")
  1175     apply (simp only: )
  1176     apply (rule span_mul[unfolded mem_def])
  1177     apply assumption
  1178     by (simp add: algebra_simps)
  1179   ultimately show "?P a" using aS span_induct[where S=S and P= "?P"] by metis
  1180 qed
  1181 
  1182 lemma span_breakdown_eq:
  1183   "x \<in> span (insert a S) \<longleftrightarrow> (\<exists>k. (x - k *\<^sub>R a) \<in> span S)" (is "?lhs \<longleftrightarrow> ?rhs")
  1184 proof-
  1185   {assume x: "x \<in> span (insert a S)"
  1186     from x span_breakdown[of "a" "insert a S" "x"]
  1187     have ?rhs apply clarsimp
  1188       apply (rule_tac x= "k" in exI)
  1189       apply (rule set_rev_mp[of _ "span (S - {a})" _])
  1190       apply assumption
  1191       apply (rule span_mono)
  1192       apply blast
  1193       done}
  1194   moreover
  1195   { fix k assume k: "x - k *\<^sub>R a \<in> span S"
  1196     have eq: "x = (x - k *\<^sub>R a) + k *\<^sub>R a" by simp
  1197     have "(x - k *\<^sub>R a) + k *\<^sub>R a \<in> span (insert a S)"
  1198       apply (rule span_add)
  1199       apply (rule set_rev_mp[of _ "span S" _])
  1200       apply (rule k)
  1201       apply (rule span_mono)
  1202       apply blast
  1203       apply (rule span_mul)
  1204       apply (rule span_superset)
  1205       apply blast
  1206       done
  1207     then have ?lhs using eq by metis}
  1208   ultimately show ?thesis by blast
  1209 qed
  1210 
  1211 text {* Hence some "reversal" results. *}
  1212 
  1213 lemma in_span_insert:
  1214   assumes a: "a \<in> span (insert b S)" and na: "a \<notin> span S"
  1215   shows "b \<in> span (insert a S)"
  1216 proof-
  1217   from span_breakdown[of b "insert b S" a, OF insertI1 a]
  1218   obtain k where k: "a - k*\<^sub>R b \<in> span (S - {b})" by auto
  1219   {assume k0: "k = 0"
  1220     with k have "a \<in> span S"
  1221       apply (simp)
  1222       apply (rule set_rev_mp)
  1223       apply assumption
  1224       apply (rule span_mono)
  1225       apply blast
  1226       done
  1227     with na  have ?thesis by blast}
  1228   moreover
  1229   {assume k0: "k \<noteq> 0"
  1230     have eq: "b = (1/k) *\<^sub>R a - ((1/k) *\<^sub>R a - b)" by simp
  1231     from k0 have eq': "(1/k) *\<^sub>R (a - k*\<^sub>R b) = (1/k) *\<^sub>R a - b"
  1232       by (simp add: algebra_simps)
  1233     from k have "(1/k) *\<^sub>R (a - k*\<^sub>R b) \<in> span (S - {b})"
  1234       by (rule span_mul)
  1235     hence th: "(1/k) *\<^sub>R a - b \<in> span (S - {b})"
  1236       unfolding eq' .
  1237 
  1238     from k
  1239     have ?thesis
  1240       apply (subst eq)
  1241       apply (rule span_sub)
  1242       apply (rule span_mul)
  1243       apply (rule span_superset)
  1244       apply blast
  1245       apply (rule set_rev_mp)
  1246       apply (rule th)
  1247       apply (rule span_mono)
  1248       using na by blast}
  1249   ultimately show ?thesis by blast
  1250 qed
  1251 
  1252 lemma in_span_delete:
  1253   assumes a: "a \<in> span S"
  1254   and na: "a \<notin> span (S-{b})"
  1255   shows "b \<in> span (insert a (S - {b}))"
  1256   apply (rule in_span_insert)
  1257   apply (rule set_rev_mp)
  1258   apply (rule a)
  1259   apply (rule span_mono)
  1260   apply blast
  1261   apply (rule na)
  1262   done
  1263 
  1264 text {* Transitivity property. *}
  1265 
  1266 lemma span_trans:
  1267   assumes x: "x \<in> span S" and y: "y \<in> span (insert x S)"
  1268   shows "y \<in> span S"
  1269 proof-
  1270   from span_breakdown[of x "insert x S" y, OF insertI1 y]
  1271   obtain k where k: "y -k*\<^sub>R x \<in> span (S - {x})" by auto
  1272   have eq: "y = (y - k *\<^sub>R x) + k *\<^sub>R x" by simp
  1273   show ?thesis
  1274     apply (subst eq)
  1275     apply (rule span_add)
  1276     apply (rule set_rev_mp)
  1277     apply (rule k)
  1278     apply (rule span_mono)
  1279     apply blast
  1280     apply (rule span_mul)
  1281     by (rule x)
  1282 qed
  1283 
  1284 lemma span_insert_0[simp]: "span (insert 0 S) = span S"
  1285   using span_mono[of S "insert 0 S"] by (auto intro: span_trans span_0)
  1286 
  1287 text {* An explicit expansion is sometimes needed. *}
  1288 
  1289 lemma span_explicit:
  1290   "span P = {y. \<exists>S u. finite S \<and> S \<subseteq> P \<and> setsum (\<lambda>v. u v *\<^sub>R v) S = y}"
  1291   (is "_ = ?E" is "_ = {y. ?h y}" is "_ = {y. \<exists>S u. ?Q S u y}")
  1292 proof-
  1293   {fix x assume x: "x \<in> ?E"
  1294     then obtain S u where fS: "finite S" and SP: "S\<subseteq>P" and u: "setsum (\<lambda>v. u v *\<^sub>R v) S = x"
  1295       by blast
  1296     have "x \<in> span P"
  1297       unfolding u[symmetric]
  1298       apply (rule span_setsum[OF fS])
  1299       using span_mono[OF SP]
  1300       by (auto intro: span_superset span_mul)}
  1301   moreover
  1302   have "\<forall>x \<in> span P. x \<in> ?E"
  1303     unfolding mem_def Collect_def
  1304   proof(rule span_induct_alt')
  1305     show "?h 0"
  1306       apply (rule exI[where x="{}"]) by simp
  1307   next
  1308     fix c x y
  1309     assume x: "x \<in> P" and hy: "?h y"
  1310     from hy obtain S u where fS: "finite S" and SP: "S\<subseteq>P"
  1311       and u: "setsum (\<lambda>v. u v *\<^sub>R v) S = y" by blast
  1312     let ?S = "insert x S"
  1313     let ?u = "\<lambda>y. if y = x then (if x \<in> S then u y + c else c)
  1314                   else u y"
  1315     from fS SP x have th0: "finite (insert x S)" "insert x S \<subseteq> P" by blast+
  1316     {assume xS: "x \<in> S"
  1317       have S1: "S = (S - {x}) \<union> {x}"
  1318         and Sss:"finite (S - {x})" "finite {x}" "(S -{x}) \<inter> {x} = {}" using xS fS by auto
  1319       have "setsum (\<lambda>v. ?u v *\<^sub>R v) ?S =(\<Sum>v\<in>S - {x}. u v *\<^sub>R v) + (u x + c) *\<^sub>R x"
  1320         using xS
  1321         by (simp add: setsum_Un_disjoint[OF Sss, unfolded S1[symmetric]]
  1322           setsum_clauses(2)[OF fS] cong del: if_weak_cong)
  1323       also have "\<dots> = (\<Sum>v\<in>S. u v *\<^sub>R v) + c *\<^sub>R x"
  1324         apply (simp add: setsum_Un_disjoint[OF Sss, unfolded S1[symmetric]])
  1325         by (simp add: algebra_simps)
  1326       also have "\<dots> = c*\<^sub>R x + y"
  1327         by (simp add: add_commute u)
  1328       finally have "setsum (\<lambda>v. ?u v *\<^sub>R v) ?S = c*\<^sub>R x + y" .
  1329     then have "?Q ?S ?u (c*\<^sub>R x + y)" using th0 by blast}
  1330   moreover
  1331   {assume xS: "x \<notin> S"
  1332     have th00: "(\<Sum>v\<in>S. (if v = x then c else u v) *\<^sub>R v) = y"
  1333       unfolding u[symmetric]
  1334       apply (rule setsum_cong2)
  1335       using xS by auto
  1336     have "?Q ?S ?u (c*\<^sub>R x + y)" using fS xS th0
  1337       by (simp add: th00 setsum_clauses add_commute cong del: if_weak_cong)}
  1338   ultimately have "?Q ?S ?u (c*\<^sub>R x + y)"
  1339     by (cases "x \<in> S", simp, simp)
  1340     then show "?h (c*\<^sub>R x + y)"
  1341       apply -
  1342       apply (rule exI[where x="?S"])
  1343       apply (rule exI[where x="?u"]) by metis
  1344   qed
  1345   ultimately show ?thesis by blast
  1346 qed
  1347 
  1348 lemma dependent_explicit:
  1349   "dependent P \<longleftrightarrow> (\<exists>S u. finite S \<and> S \<subseteq> P \<and> (\<exists>v\<in>S. u v \<noteq> 0 \<and> setsum (\<lambda>v. u v *\<^sub>R v) S = 0))" (is "?lhs = ?rhs")
  1350 proof-
  1351   {assume dP: "dependent P"
  1352     then obtain a S u where aP: "a \<in> P" and fS: "finite S"
  1353       and SP: "S \<subseteq> P - {a}" and ua: "setsum (\<lambda>v. u v *\<^sub>R v) S = a"
  1354       unfolding dependent_def span_explicit by blast
  1355     let ?S = "insert a S"
  1356     let ?u = "\<lambda>y. if y = a then - 1 else u y"
  1357     let ?v = a
  1358     from aP SP have aS: "a \<notin> S" by blast
  1359     from fS SP aP have th0: "finite ?S" "?S \<subseteq> P" "?v \<in> ?S" "?u ?v \<noteq> 0" by auto
  1360     have s0: "setsum (\<lambda>v. ?u v *\<^sub>R v) ?S = 0"
  1361       using fS aS
  1362       apply (simp add: setsum_clauses field_simps)
  1363       apply (subst (2) ua[symmetric])
  1364       apply (rule setsum_cong2)
  1365       by auto
  1366     with th0 have ?rhs
  1367       apply -
  1368       apply (rule exI[where x= "?S"])
  1369       apply (rule exI[where x= "?u"])
  1370       by clarsimp}
  1371   moreover
  1372   {fix S u v assume fS: "finite S"
  1373       and SP: "S \<subseteq> P" and vS: "v \<in> S" and uv: "u v \<noteq> 0"
  1374     and u: "setsum (\<lambda>v. u v *\<^sub>R v) S = 0"
  1375     let ?a = v
  1376     let ?S = "S - {v}"
  1377     let ?u = "\<lambda>i. (- u i) / u v"
  1378     have th0: "?a \<in> P" "finite ?S" "?S \<subseteq> P"       using fS SP vS by auto
  1379     have "setsum (\<lambda>v. ?u v *\<^sub>R v) ?S = setsum (\<lambda>v. (- (inverse (u ?a))) *\<^sub>R (u v *\<^sub>R v)) S - ?u v *\<^sub>R v"
  1380       using fS vS uv
  1381       by (simp add: setsum_diff1 divide_inverse field_simps)
  1382     also have "\<dots> = ?a"
  1383       unfolding scaleR_right.setsum [symmetric] u
  1384       using uv by simp
  1385     finally  have "setsum (\<lambda>v. ?u v *\<^sub>R v) ?S = ?a" .
  1386     with th0 have ?lhs
  1387       unfolding dependent_def span_explicit
  1388       apply -
  1389       apply (rule bexI[where x= "?a"])
  1390       apply (simp_all del: scaleR_minus_left)
  1391       apply (rule exI[where x= "?S"])
  1392       by (auto simp del: scaleR_minus_left)}
  1393   ultimately show ?thesis by blast
  1394 qed
  1395 
  1396 
  1397 lemma span_finite:
  1398   assumes fS: "finite S"
  1399   shows "span S = {y. \<exists>u. setsum (\<lambda>v. u v *\<^sub>R v) S = y}"
  1400   (is "_ = ?rhs")
  1401 proof-
  1402   {fix y assume y: "y \<in> span S"
  1403     from y obtain S' u where fS': "finite S'" and SS': "S' \<subseteq> S" and
  1404       u: "setsum (\<lambda>v. u v *\<^sub>R v) S' = y" unfolding span_explicit by blast
  1405     let ?u = "\<lambda>x. if x \<in> S' then u x else 0"
  1406     have "setsum (\<lambda>v. ?u v *\<^sub>R v) S = setsum (\<lambda>v. u v *\<^sub>R v) S'"
  1407       using SS' fS by (auto intro!: setsum_mono_zero_cong_right)
  1408     hence "setsum (\<lambda>v. ?u v *\<^sub>R v) S = y" by (metis u)
  1409     hence "y \<in> ?rhs" by auto}
  1410   moreover
  1411   {fix y u assume u: "setsum (\<lambda>v. u v *\<^sub>R v) S = y"
  1412     then have "y \<in> span S" using fS unfolding span_explicit by auto}
  1413   ultimately show ?thesis by blast
  1414 qed
  1415 
  1416 lemma Int_Un_cancel: "(A \<union> B) \<inter> A = A" "(A \<union> B) \<inter> B = B" by auto
  1417 
  1418 lemma span_union: "span (A \<union> B) = (\<lambda>(a, b). a + b) ` (span A \<times> span B)"
  1419 proof safe
  1420   fix x assume "x \<in> span (A \<union> B)"
  1421   then obtain S u where S: "finite S" "S \<subseteq> A \<union> B" and x: "x = (\<Sum>v\<in>S. u v *\<^sub>R v)"
  1422     unfolding span_explicit by auto
  1423 
  1424   let ?Sa = "\<Sum>v\<in>S\<inter>A. u v *\<^sub>R v"
  1425   let ?Sb = "(\<Sum>v\<in>S\<inter>(B - A). u v *\<^sub>R v)"
  1426   show "x \<in> (\<lambda>(a, b). a + b) ` (span A \<times> span B)"
  1427   proof
  1428     show "x = (case (?Sa, ?Sb) of (a, b) \<Rightarrow> a + b)"
  1429       unfolding x using S
  1430       by (simp, subst setsum_Un_disjoint[symmetric]) (auto intro!: setsum_cong)
  1431 
  1432     from S have "?Sa \<in> span A" unfolding span_explicit
  1433       by (auto intro!: exI[of _ "S \<inter> A"])
  1434     moreover from S have "?Sb \<in> span B" unfolding span_explicit
  1435       by (auto intro!: exI[of _ "S \<inter> (B - A)"])
  1436     ultimately show "(?Sa, ?Sb) \<in> span A \<times> span B" by simp
  1437   qed
  1438 next
  1439   fix a b assume "a \<in> span A" and "b \<in> span B"
  1440   then obtain Sa ua Sb ub where span:
  1441     "finite Sa" "Sa \<subseteq> A" "a = (\<Sum>v\<in>Sa. ua v *\<^sub>R v)"
  1442     "finite Sb" "Sb \<subseteq> B" "b = (\<Sum>v\<in>Sb. ub v *\<^sub>R v)"
  1443     unfolding span_explicit by auto
  1444   let "?u v" = "(if v \<in> Sa then ua v else 0) + (if v \<in> Sb then ub v else 0)"
  1445   from span have "finite (Sa \<union> Sb)" "Sa \<union> Sb \<subseteq> A \<union> B"
  1446     and "a + b = (\<Sum>v\<in>(Sa\<union>Sb). ?u v *\<^sub>R v)"
  1447     unfolding setsum_addf scaleR_left_distrib
  1448     by (auto simp add: if_distrib cond_application_beta setsum_cases Int_Un_cancel)
  1449   thus "a + b \<in> span (A \<union> B)"
  1450     unfolding span_explicit by (auto intro!: exI[of _ ?u])
  1451 qed
  1452 
  1453 text {* This is useful for building a basis step-by-step. *}
  1454 
  1455 lemma independent_insert:
  1456   "independent(insert a S) \<longleftrightarrow>
  1457       (if a \<in> S then independent S
  1458                 else independent S \<and> a \<notin> span S)" (is "?lhs \<longleftrightarrow> ?rhs")
  1459 proof-
  1460   {assume aS: "a \<in> S"
  1461     hence ?thesis using insert_absorb[OF aS] by simp}
  1462   moreover
  1463   {assume aS: "a \<notin> S"
  1464     {assume i: ?lhs
  1465       then have ?rhs using aS
  1466         apply simp
  1467         apply (rule conjI)
  1468         apply (rule independent_mono)
  1469         apply assumption
  1470         apply blast
  1471         by (simp add: dependent_def)}
  1472     moreover
  1473     {assume i: ?rhs
  1474       have ?lhs using i aS
  1475         apply simp
  1476         apply (auto simp add: dependent_def)
  1477         apply (case_tac "aa = a", auto)
  1478         apply (subgoal_tac "insert a S - {aa} = insert a (S - {aa})")
  1479         apply simp
  1480         apply (subgoal_tac "a \<in> span (insert aa (S - {aa}))")
  1481         apply (subgoal_tac "insert aa (S - {aa}) = S")
  1482         apply simp
  1483         apply blast
  1484         apply (rule in_span_insert)
  1485         apply assumption
  1486         apply blast
  1487         apply blast
  1488         done}
  1489     ultimately have ?thesis by blast}
  1490   ultimately show ?thesis by blast
  1491 qed
  1492 
  1493 text {* The degenerate case of the Exchange Lemma. *}
  1494 
  1495 lemma mem_delete: "x \<in> (A - {a}) \<longleftrightarrow> x \<noteq> a \<and> x \<in> A"
  1496   by blast
  1497 
  1498 lemma spanning_subset_independent:
  1499   assumes BA: "B \<subseteq> A" and iA: "independent A"
  1500   and AsB: "A \<subseteq> span B"
  1501   shows "A = B"
  1502 proof
  1503   from BA show "B \<subseteq> A" .
  1504 next
  1505   from span_mono[OF BA] span_mono[OF AsB]
  1506   have sAB: "span A = span B" unfolding span_span by blast
  1507 
  1508   {fix x assume x: "x \<in> A"
  1509     from iA have th0: "x \<notin> span (A - {x})"
  1510       unfolding dependent_def using x by blast
  1511     from x have xsA: "x \<in> span A" by (blast intro: span_superset)
  1512     have "A - {x} \<subseteq> A" by blast
  1513     hence th1:"span (A - {x}) \<subseteq> span A" by (metis span_mono)
  1514     {assume xB: "x \<notin> B"
  1515       from xB BA have "B \<subseteq> A -{x}" by blast
  1516       hence "span B \<subseteq> span (A - {x})" by (metis span_mono)
  1517       with th1 th0 sAB have "x \<notin> span A" by blast
  1518       with x have False by (metis span_superset)}
  1519     then have "x \<in> B" by blast}
  1520   then show "A \<subseteq> B" by blast
  1521 qed
  1522 
  1523 text {* The general case of the Exchange Lemma, the key to what follows. *}
  1524 
  1525 lemma exchange_lemma:
  1526   assumes f:"finite t" and i: "independent s"
  1527   and sp:"s \<subseteq> span t"
  1528   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'"
  1529 using f i sp
  1530 proof(induct "card (t - s)" arbitrary: s t rule: less_induct)
  1531   case less
  1532   note ft = `finite t` and s = `independent s` and sp = `s \<subseteq> span t`
  1533   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'"
  1534   let ?ths = "\<exists>t'. ?P t'"
  1535   {assume st: "s \<subseteq> t"
  1536     from st ft span_mono[OF st] have ?ths apply - apply (rule exI[where x=t])
  1537       by (auto intro: span_superset)}
  1538   moreover
  1539   {assume st: "t \<subseteq> s"
  1540 
  1541     from spanning_subset_independent[OF st s sp]
  1542       st ft span_mono[OF st] have ?ths apply - apply (rule exI[where x=t])
  1543       by (auto intro: span_superset)}
  1544   moreover
  1545   {assume st: "\<not> s \<subseteq> t" "\<not> t \<subseteq> s"
  1546     from st(2) obtain b where b: "b \<in> t" "b \<notin> s" by blast
  1547       from b have "t - {b} - s \<subset> t - s" by blast
  1548       then have cardlt: "card (t - {b} - s) < card (t - s)" using ft
  1549         by (auto intro: psubset_card_mono)
  1550       from b ft have ct0: "card t \<noteq> 0" by auto
  1551     {assume stb: "s \<subseteq> span(t -{b})"
  1552       from ft have ftb: "finite (t -{b})" by auto
  1553       from less(1)[OF cardlt ftb s stb]
  1554       obtain u where u: "card u = card (t-{b})" "s \<subseteq> u" "u \<subseteq> s \<union> (t - {b})" "s \<subseteq> span u" and fu: "finite u" by blast
  1555       let ?w = "insert b u"
  1556       have th0: "s \<subseteq> insert b u" using u by blast
  1557       from u(3) b have "u \<subseteq> s \<union> t" by blast
  1558       then have th1: "insert b u \<subseteq> s \<union> t" using u b by blast
  1559       have bu: "b \<notin> u" using b u by blast
  1560       from u(1) ft b have "card u = (card t - 1)" by auto
  1561       then
  1562       have th2: "card (insert b u) = card t"
  1563         using card_insert_disjoint[OF fu bu] ct0 by auto
  1564       from u(4) have "s \<subseteq> span u" .
  1565       also have "\<dots> \<subseteq> span (insert b u)" apply (rule span_mono) by blast
  1566       finally have th3: "s \<subseteq> span (insert b u)" .
  1567       from th0 th1 th2 th3 fu have th: "?P ?w"  by blast
  1568       from th have ?ths by blast}
  1569     moreover
  1570     {assume stb: "\<not> s \<subseteq> span(t -{b})"
  1571       from stb obtain a where a: "a \<in> s" "a \<notin> span (t - {b})" by blast
  1572       have ab: "a \<noteq> b" using a b by blast
  1573       have at: "a \<notin> t" using a ab span_superset[of a "t- {b}"] by auto
  1574       have mlt: "card ((insert a (t - {b})) - s) < card (t - s)"
  1575         using cardlt ft a b by auto
  1576       have ft': "finite (insert a (t - {b}))" using ft by auto
  1577       {fix x assume xs: "x \<in> s"
  1578         have t: "t \<subseteq> (insert b (insert a (t -{b})))" using b by auto
  1579         from b(1) have "b \<in> span t" by (simp add: span_superset)
  1580         have bs: "b \<in> span (insert a (t - {b}))" apply(rule in_span_delete)
  1581           using  a sp unfolding subset_eq by auto
  1582         from xs sp have "x \<in> span t" by blast
  1583         with span_mono[OF t]
  1584         have x: "x \<in> span (insert b (insert a (t - {b})))" ..
  1585         from span_trans[OF bs x] have "x \<in> span (insert a (t - {b}))"  .}
  1586       then have sp': "s \<subseteq> span (insert a (t - {b}))" by blast
  1587 
  1588       from less(1)[OF mlt ft' s sp'] obtain u where
  1589         u: "card u = card (insert a (t -{b}))" "finite u" "s \<subseteq> u" "u \<subseteq> s \<union> insert a (t -{b})"
  1590         "s \<subseteq> span u" by blast
  1591       from u a b ft at ct0 have "?P u" by auto
  1592       then have ?ths by blast }
  1593     ultimately have ?ths by blast
  1594   }
  1595   ultimately
  1596   show ?ths  by blast
  1597 qed
  1598 
  1599 text {* This implies corresponding size bounds. *}
  1600 
  1601 lemma independent_span_bound:
  1602   assumes f: "finite t" and i: "independent s" and sp:"s \<subseteq> span t"
  1603   shows "finite s \<and> card s \<le> card t"
  1604   by (metis exchange_lemma[OF f i sp] finite_subset card_mono)
  1605 
  1606 
  1607 lemma finite_Atleast_Atmost_nat[simp]: "finite {f x |x. x\<in> (UNIV::'a::finite set)}"
  1608 proof-
  1609   have eq: "{f x |x. x\<in> UNIV} = f ` UNIV" by auto
  1610   show ?thesis unfolding eq
  1611     apply (rule finite_imageI)
  1612     apply (rule finite)
  1613     done
  1614 qed
  1615 
  1616 subsection{* Euclidean Spaces as Typeclass*}
  1617 
  1618 class real_basis = real_vector +
  1619   fixes basis :: "nat \<Rightarrow> 'a"
  1620   assumes span_basis: "span (range basis) = UNIV"
  1621   assumes dimension_exists: "\<exists>d>0.
  1622     basis ` {d..} = {0} \<and>
  1623     independent (basis ` {..<d}) \<and>
  1624     inj_on basis {..<d}"
  1625 
  1626 definition (in real_basis) dimension :: "'a itself \<Rightarrow> nat" where
  1627   "dimension x =
  1628     (THE d. basis ` {d..} = {0} \<and> independent (basis ` {..<d}) \<and> inj_on basis {..<d})"
  1629 
  1630 syntax "_type_dimension" :: "type => nat" ("(1DIM/(1'(_')))")
  1631 
  1632 translations "DIM('t)" == "CONST dimension (TYPE('t))"
  1633 
  1634 lemma (in real_basis) dimensionI:
  1635   assumes "\<And>d. \<lbrakk> 0 < d; basis ` {d..} = {0}; independent (basis ` {..<d});
  1636     inj_on basis {..<d} \<rbrakk> \<Longrightarrow> P d"
  1637   shows "P DIM('a)"
  1638 proof -
  1639   obtain d where "0 < d" and d: "basis ` {d..} = {0} \<and>
  1640       independent (basis ` {..<d}) \<and> inj_on basis {..<d}" (is "?P d")
  1641     using dimension_exists by auto
  1642   show ?thesis unfolding dimension_def
  1643   proof (rule theI2)
  1644     fix d' assume "?P d'"
  1645     with d have "basis d' = 0" "basis d = 0" and
  1646       "d < d' \<Longrightarrow> basis d \<noteq> 0"
  1647       "d' < d \<Longrightarrow> basis d' \<noteq> 0"
  1648       using dependent_0 by auto
  1649     thus "d' = d" by (cases rule: linorder_cases) auto
  1650     moreover have "P d" using assms[of d] `0 < d` d by auto
  1651     ultimately show "P d'" by simp
  1652   next show "?P d" using `?P d` .
  1653   qed
  1654 qed
  1655 
  1656 lemma (in real_basis) dimension_eq:
  1657   assumes "\<And>i. i < d \<Longrightarrow> basis i \<noteq> 0"
  1658   assumes "\<And>i. d \<le> i \<Longrightarrow> basis i = 0"
  1659   shows "DIM('a) = d"
  1660 proof (rule dimensionI)
  1661   let ?b = "basis :: nat \<Rightarrow> 'a"
  1662   fix d' assume *: "?b ` {d'..} = {0}" "independent (?b ` {..<d'})"
  1663   show "d' = d"
  1664   proof (cases rule: linorder_cases)
  1665     assume "d' < d" hence "basis d' \<noteq> 0" using assms by auto
  1666     with * show ?thesis by auto
  1667   next
  1668     assume "d < d'" hence "basis d \<noteq> 0" using * dependent_0 by auto
  1669     with assms(2) `d < d'` show ?thesis by auto
  1670   qed
  1671 qed
  1672 
  1673 lemma (in real_basis)
  1674   shows basis_finite: "basis ` {DIM('a)..} = {0}"
  1675   and independent_basis: "independent (basis ` {..<DIM('a)})"
  1676   and DIM_positive[intro]: "(DIM('a) :: nat) > 0"
  1677   and basis_inj[simp, intro]: "inj_on basis {..<DIM('a)}"
  1678   by (auto intro!: dimensionI)
  1679 
  1680 lemma (in real_basis) basis_eq_0_iff: "basis j = 0 \<longleftrightarrow> DIM('a) \<le> j"
  1681 proof
  1682   show "DIM('a) \<le> j \<Longrightarrow> basis j = 0" using basis_finite by auto
  1683 next
  1684   have "j < DIM('a) \<Longrightarrow> basis j \<noteq> 0"
  1685     using independent_basis by (auto intro!: dependent_0)
  1686   thus "basis j = 0 \<Longrightarrow> DIM('a) \<le> j" unfolding not_le[symmetric] by blast
  1687 qed
  1688 
  1689 lemma (in real_basis) range_basis:
  1690     "range basis = insert 0 (basis ` {..<DIM('a)})"
  1691 proof -
  1692   have *: "UNIV = {..<DIM('a)} \<union> {DIM('a)..}" by auto
  1693   show ?thesis unfolding * image_Un basis_finite by auto
  1694 qed
  1695 
  1696 lemma (in real_basis) range_basis_finite[intro]:
  1697     "finite (range basis)"
  1698   unfolding range_basis by auto
  1699 
  1700 lemma (in real_basis) basis_neq_0[intro]:
  1701   assumes "i<DIM('a)" shows "(basis i) \<noteq> 0"
  1702 proof(rule ccontr) assume "\<not> basis i \<noteq> (0::'a)"
  1703   hence "(0::'a) \<in> basis ` {..<DIM('a)}" using assms by auto
  1704   from dependent_0[OF this] show False using independent_basis by auto
  1705 qed
  1706 
  1707 lemma (in real_basis) basis_zero[simp]:
  1708   assumes"i \<ge> DIM('a)" shows "basis i = 0"
  1709 proof-
  1710   have "(basis i::'a) \<in> basis ` {DIM('a)..}" using assms by auto
  1711   thus ?thesis unfolding basis_finite by fastsimp
  1712 qed
  1713 
  1714 lemma basis_representation:
  1715   "\<exists>u. x = (\<Sum>v\<in>basis ` {..<DIM('a)}. u v *\<^sub>R (v\<Colon>'a\<Colon>real_basis))"
  1716 proof -
  1717   have "x\<in>UNIV" by auto from this[unfolded span_basis[THEN sym]]
  1718   have "\<exists>u. (\<Sum>v\<in>basis ` {..<DIM('a)}. u v *\<^sub>R v) = x"
  1719     unfolding range_basis span_insert_0 apply(subst (asm) span_finite) by auto
  1720   thus ?thesis by fastsimp
  1721 qed
  1722 
  1723 lemma span_basis'[simp]:"span ((basis::nat=>'a) ` {..<DIM('a::real_basis)}) = UNIV"
  1724   apply(subst span_basis[symmetric]) unfolding range_basis by auto
  1725 
  1726 lemma card_basis[simp]:"card ((basis::nat=>'a) ` {..<DIM('a::real_basis)}) = DIM('a)"
  1727   apply(subst card_image) using basis_inj by auto
  1728 
  1729 lemma in_span_basis: "(x::'a::real_basis) \<in> span (basis ` {..<DIM('a)})"
  1730   unfolding span_basis' ..
  1731 
  1732 lemma independent_eq_inj_on:
  1733   fixes D :: nat and f :: "nat \<Rightarrow> 'c::real_vector" assumes *: "inj_on f {..<D}"
  1734   shows "independent (f ` {..<D}) \<longleftrightarrow> (\<forall>a u. a < D \<longrightarrow> (\<Sum>i\<in>{..<D}-{a}. u (f i) *\<^sub>R f i) \<noteq> f a)"
  1735 proof -
  1736   from * have eq: "\<And>i. i < D \<Longrightarrow> f ` {..<D} - {f i} = f`({..<D} - {i})"
  1737     and inj: "\<And>i. inj_on f ({..<D} - {i})"
  1738     by (auto simp: inj_on_def)
  1739   have *: "\<And>i. finite (f ` {..<D} - {i})" by simp
  1740   show ?thesis unfolding dependent_def span_finite[OF *]
  1741     by (auto simp: eq setsum_reindex[OF inj])
  1742 qed
  1743 
  1744 class real_basis_with_inner = real_inner + real_basis
  1745 begin
  1746 
  1747 definition euclidean_component (infixl "$$" 90) where
  1748   "x $$ i = inner (basis i) x"
  1749 
  1750 definition Chi (binder "\<chi>\<chi> " 10) where
  1751   "(\<chi>\<chi> i. f i) = (\<Sum>i<DIM('a). f i *\<^sub>R basis i)"
  1752 
  1753 lemma basis_at_neq_0[intro]:
  1754   "i < DIM('a) \<Longrightarrow> basis i $$ i \<noteq> 0"
  1755   unfolding euclidean_component_def by (auto intro!: basis_neq_0)
  1756 
  1757 lemma euclidean_component_ge[simp]:
  1758   assumes "i \<ge> DIM('a)" shows "x $$ i = 0"
  1759   unfolding euclidean_component_def basis_zero[OF assms] by auto
  1760 
  1761 lemma euclidean_scaleR:
  1762   shows "(a *\<^sub>R x) $$ i = a * (x$$i)"
  1763   unfolding euclidean_component_def by auto
  1764 
  1765 end
  1766 
  1767 interpretation euclidean_component: additive "\<lambda>x. euclidean_component x i"
  1768 proof qed (simp add: euclidean_component_def inner_right.add)
  1769 
  1770 subsection{* Euclidean Spaces as Typeclass *}
  1771 
  1772 class euclidean_space = real_basis_with_inner +
  1773   assumes basis_orthonormal: "\<forall>i<DIM('a). \<forall>j<DIM('a).
  1774     inner (basis i) (basis j) = (if i = j then 1 else 0)"
  1775 
  1776 lemma (in euclidean_space) dot_basis:
  1777   "inner (basis i) (basis j) = (if i = j \<and> i<DIM('a) then 1 else 0)"
  1778 proof (cases "(i < DIM('a) \<and> j < DIM('a))")
  1779   case False
  1780   hence "basis i = 0 \<or> basis j = 0"
  1781     using basis_finite by fastsimp
  1782   hence "inner (basis i) (basis j) = 0" by (rule disjE) simp_all
  1783   thus ?thesis using False by auto
  1784 next
  1785   case True thus ?thesis using basis_orthonormal by auto
  1786 qed
  1787 
  1788 lemma (in euclidean_space) basis_component[simp]:
  1789   "basis i $$ j = (if i = j \<and> i < DIM('a) then 1 else 0)"
  1790   unfolding euclidean_component_def dot_basis by auto
  1791 
  1792 lemmas euclidean_simps =
  1793   euclidean_component.add
  1794   euclidean_component.diff
  1795   euclidean_scaleR
  1796   euclidean_component.minus
  1797   euclidean_component.setsum
  1798   basis_component
  1799 
  1800 lemma euclidean_representation:
  1801   "(x\<Colon>'a\<Colon>euclidean_space) = (\<Sum>i\<in>{..<DIM('a)}. (x$$i) *\<^sub>R basis i)"
  1802 proof-
  1803   from basis_representation[of x] guess u ..
  1804   hence *:"x = (\<Sum>i\<in>{..<DIM('a)}. u (basis i) *\<^sub>R (basis i))"
  1805     by (simp add: setsum_reindex)
  1806   show ?thesis apply(subst *)
  1807   proof(safe intro!: setsum_cong2)
  1808     fix i assume i: "i < DIM('a)"
  1809     hence "x$$i = (\<Sum>x<DIM('a). (if i = x then u (basis x) else 0))"
  1810       by (auto simp: euclidean_simps * intro!: setsum_cong)
  1811     also have "... = u (basis i)" using i by (auto simp: setsum_cases)
  1812     finally show "u (basis i) *\<^sub>R basis i = x $$ i *\<^sub>R basis i" by simp
  1813   qed
  1814 qed
  1815 
  1816 lemma euclidean_eq:
  1817   fixes x y :: "'a\<Colon>euclidean_space"
  1818   shows "x = y \<longleftrightarrow> (\<forall>i<DIM('a). x$$i = y$$i)" (is "?l = ?r")
  1819 proof safe
  1820   assume "\<forall>i<DIM('a). x $$ i = y $$ i"
  1821   thus "x = y"
  1822     by (subst (1 2) euclidean_representation) auto
  1823 qed
  1824 
  1825 lemma euclidean_lambda_beta[simp]:
  1826   "((\<chi>\<chi> i. f i)::'a::euclidean_space) $$ j = (if j < DIM('a) then f j else 0)"
  1827   by (auto simp: euclidean_simps Chi_def if_distrib setsum_cases
  1828            intro!: setsum_cong)
  1829 
  1830 lemma euclidean_lambda_beta':
  1831   "j < DIM('a) \<Longrightarrow> ((\<chi>\<chi> i. f i)::'a::euclidean_space) $$ j = f j"
  1832   by simp
  1833 
  1834 lemma euclidean_lambda_beta'':"(\<forall>j < DIM('a::euclidean_space). P j (((\<chi>\<chi> i. f i)::'a) $$ j)) \<longleftrightarrow>
  1835   (\<forall>j < DIM('a::euclidean_space). P j (f j))" by auto
  1836 
  1837 lemma euclidean_beta_reduce[simp]:
  1838   "(\<chi>\<chi> i. x $$ i) = (x::'a::euclidean_space)"
  1839   by (subst euclidean_eq) (simp add: euclidean_lambda_beta)
  1840 
  1841 lemma euclidean_lambda_beta_0[simp]:
  1842     "((\<chi>\<chi> i. f i)::'a::euclidean_space) $$ 0 = f 0"
  1843   by (simp add: DIM_positive)
  1844 
  1845 lemma euclidean_inner:
  1846   "x \<bullet> (y::'a) = (\<Sum>i<DIM('a::euclidean_space). (x $$ i) \<bullet> (y $$ i))"
  1847 proof -
  1848   have "x \<bullet> y = (\<Sum>i<DIM('a). x $$ i *\<^sub>R basis i) \<bullet>
  1849                 (\<Sum>i<DIM('a). y $$ i *\<^sub>R (basis i :: 'a))"
  1850     by (subst (1 2) euclidean_representation) simp
  1851   also have "\<dots> = (\<Sum>i<DIM('a::euclidean_space). (x $$ i) \<bullet> (y $$ i))"
  1852     unfolding inner_left.setsum inner_right.setsum
  1853     by (auto simp add: dot_basis if_distrib setsum_cases intro!: setsum_cong)
  1854   finally show ?thesis .
  1855 qed
  1856 
  1857 lemma norm_basis[simp]:"norm (basis i::'a::euclidean_space) = (if i<DIM('a) then 1 else 0)"
  1858   unfolding norm_eq_sqrt_inner dot_basis by auto
  1859 
  1860 lemma component_le_norm: "\<bar>x$$i\<bar> \<le> norm (x::'a::euclidean_space)"
  1861   unfolding euclidean_component_def
  1862   apply(rule order_trans[OF real_inner_class.Cauchy_Schwarz_ineq2]) by auto
  1863 
  1864 lemma norm_bound_component_le: "norm (x::'a::euclidean_space) \<le> e \<Longrightarrow> \<bar>x$$i\<bar> <= e"
  1865   by (metis component_le_norm order_trans)
  1866 
  1867 lemma norm_bound_component_lt: "norm (x::'a::euclidean_space) < e \<Longrightarrow> \<bar>x$$i\<bar> < e"
  1868   by (metis component_le_norm basic_trans_rules(21))
  1869 
  1870 lemma norm_le_l1: "norm (x::'a::euclidean_space) \<le> (\<Sum>i<DIM('a). \<bar>x $$ i\<bar>)"
  1871   apply (subst euclidean_representation[of x])
  1872   apply (rule order_trans[OF setsum_norm])
  1873   by (auto intro!: setsum_mono)
  1874 
  1875 lemma setsum_norm_allsubsets_bound:
  1876   fixes f:: "'a \<Rightarrow> 'n::euclidean_space"
  1877   assumes fP: "finite P" and fPs: "\<And>Q. Q \<subseteq> P \<Longrightarrow> norm (setsum f Q) \<le> e"
  1878   shows "setsum (\<lambda>x. norm (f x)) P \<le> 2 * real DIM('n) *  e"
  1879 proof-
  1880   let ?d = "real DIM('n)"
  1881   let ?nf = "\<lambda>x. norm (f x)"
  1882   let ?U = "{..<DIM('n)}"
  1883   have th0: "setsum (\<lambda>x. setsum (\<lambda>i. \<bar>f x $$ i\<bar>) ?U) P = setsum (\<lambda>i. setsum (\<lambda>x. \<bar>f x $$ i\<bar>) P) ?U"
  1884     by (rule setsum_commute)
  1885   have th1: "2 * ?d * e = of_nat (card ?U) * (2 * e)" by (simp add: real_of_nat_def)
  1886   have "setsum ?nf P \<le> setsum (\<lambda>x. setsum (\<lambda>i. \<bar>f x $$ i\<bar>) ?U) P"
  1887     apply (rule setsum_mono)    by (rule norm_le_l1)
  1888   also have "\<dots> \<le> 2 * ?d * e"
  1889     unfolding th0 th1
  1890   proof(rule setsum_bounded)
  1891     fix i assume i: "i \<in> ?U"
  1892     let ?Pp = "{x. x\<in> P \<and> f x $$ i \<ge> 0}"
  1893     let ?Pn = "{x. x \<in> P \<and> f x $$ i < 0}"
  1894     have thp: "P = ?Pp \<union> ?Pn" by auto
  1895     have thp0: "?Pp \<inter> ?Pn ={}" by auto
  1896     have PpP: "?Pp \<subseteq> P" and PnP: "?Pn \<subseteq> P" by blast+
  1897     have Ppe:"setsum (\<lambda>x. \<bar>f x $$ i\<bar>) ?Pp \<le> e"
  1898       using component_le_norm[of "setsum (\<lambda>x. f x) ?Pp" i]  fPs[OF PpP]
  1899       unfolding euclidean_component.setsum by(auto intro: abs_le_D1)
  1900     have Pne: "setsum (\<lambda>x. \<bar>f x $$ i\<bar>) ?Pn \<le> e"
  1901       using component_le_norm[of "setsum (\<lambda>x. - f x) ?Pn" i]  fPs[OF PnP]
  1902       unfolding euclidean_component.setsum euclidean_component.minus
  1903       by(auto simp add: setsum_negf intro: abs_le_D1)
  1904     have "setsum (\<lambda>x. \<bar>f x $$ i\<bar>) P = setsum (\<lambda>x. \<bar>f x $$ i\<bar>) ?Pp + setsum (\<lambda>x. \<bar>f x $$ i\<bar>) ?Pn"
  1905       apply (subst thp)
  1906       apply (rule setsum_Un_zero)
  1907       using fP thp0 by auto
  1908     also have "\<dots> \<le> 2*e" using Pne Ppe by arith
  1909     finally show "setsum (\<lambda>x. \<bar>f x $$ i\<bar>) P \<le> 2*e" .
  1910   qed
  1911   finally show ?thesis .
  1912 qed
  1913 
  1914 lemma choice_iff': "(\<forall>x<d. \<exists>y. P x y) \<longleftrightarrow> (\<exists>f. \<forall>x<d. P x (f x))" by metis
  1915 
  1916 lemma lambda_skolem': "(\<forall>i<DIM('a::euclidean_space). \<exists>x. P i x) \<longleftrightarrow>
  1917    (\<exists>x::'a. \<forall>i<DIM('a). P i (x$$i))" (is "?lhs \<longleftrightarrow> ?rhs")
  1918 proof-
  1919   let ?S = "{..<DIM('a)}"
  1920   {assume H: "?rhs"
  1921     then have ?lhs by auto}
  1922   moreover
  1923   {assume H: "?lhs"
  1924     then obtain f where f:"\<forall>i<DIM('a). P i (f i)" unfolding choice_iff' by metis
  1925     let ?x = "(\<chi>\<chi> i. (f i)) :: 'a"
  1926     {fix i assume i:"i<DIM('a)"
  1927       with f have "P i (f i)" by metis
  1928       then have "P i (?x$$i)" using i by auto
  1929     }
  1930     hence "\<forall>i<DIM('a). P i (?x$$i)" by metis
  1931     hence ?rhs by metis }
  1932   ultimately show ?thesis by metis
  1933 qed
  1934 
  1935 subsection {* An ordering on euclidean spaces that will allow us to talk about intervals *}
  1936 
  1937 class ordered_euclidean_space = ord + euclidean_space +
  1938   assumes eucl_le: "x \<le> y \<longleftrightarrow> (\<forall>i < DIM('a). x $$ i \<le> y $$ i)"
  1939   and eucl_less: "x < y \<longleftrightarrow> (\<forall>i < DIM('a). x $$ i < y $$ i)"
  1940 
  1941 lemma eucl_less_not_refl[simp, intro!]: "\<not> x < (x::'a::ordered_euclidean_space)"
  1942   unfolding eucl_less[where 'a='a] by auto
  1943 
  1944 lemma euclidean_trans[trans]:
  1945   fixes x y z :: "'a::ordered_euclidean_space"
  1946   shows "x < y \<Longrightarrow> y < z \<Longrightarrow> x < z"
  1947   and "x \<le> y \<Longrightarrow> y < z \<Longrightarrow> x < z"
  1948   and "x \<le> y \<Longrightarrow> y \<le> z \<Longrightarrow> x \<le> z"
  1949   by (force simp: eucl_less[where 'a='a] eucl_le[where 'a='a])+
  1950 
  1951 subsection {* Linearity and Bilinearity continued *}
  1952 
  1953 lemma linear_bounded:
  1954   fixes f:: "'a::euclidean_space \<Rightarrow> 'b::real_normed_vector"
  1955   assumes lf: "linear f"
  1956   shows "\<exists>B. \<forall>x. norm (f x) \<le> B * norm x"
  1957 proof-
  1958   let ?S = "{..<DIM('a)}"
  1959   let ?B = "setsum (\<lambda>i. norm(f(basis i))) ?S"
  1960   have fS: "finite ?S" by simp
  1961   {fix x:: "'a"
  1962     let ?g = "(\<lambda> i. (x$$i) *\<^sub>R (basis i) :: 'a)"
  1963     have "norm (f x) = norm (f (setsum (\<lambda>i. (x$$i) *\<^sub>R (basis i)) ?S))"
  1964       apply(subst euclidean_representation[of x]) ..
  1965     also have "\<dots> = norm (setsum (\<lambda> i. (x$$i) *\<^sub>R f (basis i)) ?S)"
  1966       using linear_setsum[OF lf fS, of ?g, unfolded o_def] linear_cmul[OF lf] by auto
  1967     finally have th0: "norm (f x) = norm (setsum (\<lambda>i. (x$$i) *\<^sub>R f (basis i))?S)" .
  1968     {fix i assume i: "i \<in> ?S"
  1969       from component_le_norm[of x i]
  1970       have "norm ((x$$i) *\<^sub>R f (basis i :: 'a)) \<le> norm (f (basis i)) * norm x"
  1971       unfolding norm_scaleR
  1972       apply (simp only: mult_commute)
  1973       apply (rule mult_mono)
  1974       by (auto simp add: field_simps) }
  1975     then have th: "\<forall>i\<in> ?S. norm ((x$$i) *\<^sub>R f (basis i :: 'a)) \<le> norm (f (basis i)) * norm x" by metis
  1976     from setsum_norm_le[OF fS, of "\<lambda>i. (x$$i) *\<^sub>R (f (basis i))", OF th]
  1977     have "norm (f x) \<le> ?B * norm x" unfolding th0 setsum_left_distrib by metis}
  1978   then show ?thesis by blast
  1979 qed
  1980 
  1981 lemma linear_bounded_pos:
  1982   fixes f:: "'a::euclidean_space \<Rightarrow> 'b::real_normed_vector"
  1983   assumes lf: "linear f"
  1984   shows "\<exists>B > 0. \<forall>x. norm (f x) \<le> B * norm x"
  1985 proof-
  1986   from linear_bounded[OF lf] obtain B where
  1987     B: "\<forall>x. norm (f x) \<le> B * norm x" by blast
  1988   let ?K = "\<bar>B\<bar> + 1"
  1989   have Kp: "?K > 0" by arith
  1990     { assume C: "B < 0"
  1991       have "((\<chi>\<chi> i. 1)::'a) \<noteq> 0" unfolding euclidean_eq[where 'a='a]
  1992         by(auto intro!:exI[where x=0] simp add:euclidean_component.zero)
  1993       hence "norm ((\<chi>\<chi> i. 1)::'a) > 0" by auto
  1994       with C have "B * norm ((\<chi>\<chi> i. 1)::'a) < 0"
  1995         by (simp add: mult_less_0_iff)
  1996       with B[rule_format, of "(\<chi>\<chi> i. 1)::'a"] norm_ge_zero[of "f ((\<chi>\<chi> i. 1)::'a)"] have False by simp
  1997     }
  1998     then have Bp: "B \<ge> 0" by ferrack
  1999     {fix x::"'a"
  2000       have "norm (f x) \<le> ?K *  norm x"
  2001       using B[rule_format, of x] norm_ge_zero[of x] norm_ge_zero[of "f x"] Bp
  2002       apply (auto simp add: field_simps split add: abs_split)
  2003       apply (erule order_trans, simp)
  2004       done
  2005   }
  2006   then show ?thesis using Kp by blast
  2007 qed
  2008 
  2009 lemma linear_conv_bounded_linear:
  2010   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::real_normed_vector"
  2011   shows "linear f \<longleftrightarrow> bounded_linear f"
  2012 proof
  2013   assume "linear f"
  2014   show "bounded_linear f"
  2015   proof
  2016     fix x y show "f (x + y) = f x + f y"
  2017       using `linear f` unfolding linear_def by simp
  2018   next
  2019     fix r x show "f (scaleR r x) = scaleR r (f x)"
  2020       using `linear f` unfolding linear_def by simp
  2021   next
  2022     have "\<exists>B. \<forall>x. norm (f x) \<le> B * norm x"
  2023       using `linear f` by (rule linear_bounded)
  2024     thus "\<exists>K. \<forall>x. norm (f x) \<le> norm x * K"
  2025       by (simp add: mult_commute)
  2026   qed
  2027 next
  2028   assume "bounded_linear f"
  2029   then interpret f: bounded_linear f .
  2030   show "linear f"
  2031     by (simp add: f.add f.scaleR linear_def)
  2032 qed
  2033 
  2034 lemma bounded_linearI': fixes f::"'a::euclidean_space \<Rightarrow> 'b::real_normed_vector"
  2035   assumes "\<And>x y. f (x + y) = f x + f y" "\<And>c x. f (c *\<^sub>R x) = c *\<^sub>R f x"
  2036   shows "bounded_linear f" unfolding linear_conv_bounded_linear[THEN sym]
  2037   by(rule linearI[OF assms])
  2038 
  2039 
  2040 lemma bilinear_bounded:
  2041   fixes h:: "'m::euclidean_space \<Rightarrow> 'n::euclidean_space \<Rightarrow> 'k::real_normed_vector"
  2042   assumes bh: "bilinear h"
  2043   shows "\<exists>B. \<forall>x y. norm (h x y) \<le> B * norm x * norm y"
  2044 proof-
  2045   let ?M = "{..<DIM('m)}"
  2046   let ?N = "{..<DIM('n)}"
  2047   let ?B = "setsum (\<lambda>(i,j). norm (h (basis i) (basis j))) (?M \<times> ?N)"
  2048   have fM: "finite ?M" and fN: "finite ?N" by simp_all
  2049   {fix x:: "'m" and  y :: "'n"
  2050     have "norm (h x y) = norm (h (setsum (\<lambda>i. (x$$i) *\<^sub>R basis i) ?M) (setsum (\<lambda>i. (y$$i) *\<^sub>R basis i) ?N))" 
  2051       apply(subst euclidean_representation[where 'a='m])
  2052       apply(subst euclidean_representation[where 'a='n]) ..
  2053     also have "\<dots> = norm (setsum (\<lambda> (i,j). h ((x$$i) *\<^sub>R basis i) ((y$$j) *\<^sub>R basis j)) (?M \<times> ?N))"  
  2054       unfolding bilinear_setsum[OF bh fM fN] ..
  2055     finally have th: "norm (h x y) = \<dots>" .
  2056     have "norm (h x y) \<le> ?B * norm x * norm y"
  2057       apply (simp add: setsum_left_distrib th)
  2058       apply (rule setsum_norm_le)
  2059       using fN fM
  2060       apply simp
  2061       apply (auto simp add: bilinear_rmul[OF bh] bilinear_lmul[OF bh] field_simps simp del: scaleR_scaleR)
  2062       apply (rule mult_mono)
  2063       apply (auto simp add: zero_le_mult_iff component_le_norm)
  2064       apply (rule mult_mono)
  2065       apply (auto simp add: zero_le_mult_iff component_le_norm)
  2066       done}
  2067   then show ?thesis by metis
  2068 qed
  2069 
  2070 lemma bilinear_bounded_pos:
  2071   fixes h:: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space \<Rightarrow> 'c::real_normed_vector"
  2072   assumes bh: "bilinear h"
  2073   shows "\<exists>B > 0. \<forall>x y. norm (h x y) \<le> B * norm x * norm y"
  2074 proof-
  2075   from bilinear_bounded[OF bh] obtain B where
  2076     B: "\<forall>x y. norm (h x y) \<le> B * norm x * norm y" by blast
  2077   let ?K = "\<bar>B\<bar> + 1"
  2078   have Kp: "?K > 0" by arith
  2079   have KB: "B < ?K" by arith
  2080   {fix x::'a and y::'b
  2081     from KB Kp
  2082     have "B * norm x * norm y \<le> ?K * norm x * norm y"
  2083       apply -
  2084       apply (rule mult_right_mono, rule mult_right_mono)
  2085       by auto
  2086     then have "norm (h x y) \<le> ?K * norm x * norm y"
  2087       using B[rule_format, of x y] by simp}
  2088   with Kp show ?thesis by blast
  2089 qed
  2090 
  2091 lemma bilinear_conv_bounded_bilinear:
  2092   fixes h :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space \<Rightarrow> 'c::real_normed_vector"
  2093   shows "bilinear h \<longleftrightarrow> bounded_bilinear h"
  2094 proof
  2095   assume "bilinear h"
  2096   show "bounded_bilinear h"
  2097   proof
  2098     fix x y z show "h (x + y) z = h x z + h y z"
  2099       using `bilinear h` unfolding bilinear_def linear_def by simp
  2100   next
  2101     fix x y z show "h x (y + z) = h x y + h x z"
  2102       using `bilinear h` unfolding bilinear_def linear_def by simp
  2103   next
  2104     fix r x y show "h (scaleR r x) y = scaleR r (h x y)"
  2105       using `bilinear h` unfolding bilinear_def linear_def
  2106       by simp
  2107   next
  2108     fix r x y show "h x (scaleR r y) = scaleR r (h x y)"
  2109       using `bilinear h` unfolding bilinear_def linear_def
  2110       by simp
  2111   next
  2112     have "\<exists>B. \<forall>x y. norm (h x y) \<le> B * norm x * norm y"
  2113       using `bilinear h` by (rule bilinear_bounded)
  2114     thus "\<exists>K. \<forall>x y. norm (h x y) \<le> norm x * norm y * K"
  2115       by (simp add: mult_ac)
  2116   qed
  2117 next
  2118   assume "bounded_bilinear h"
  2119   then interpret h: bounded_bilinear h .
  2120   show "bilinear h"
  2121     unfolding bilinear_def linear_conv_bounded_linear
  2122     using h.bounded_linear_left h.bounded_linear_right
  2123     by simp
  2124 qed
  2125 
  2126 subsection {* We continue. *}
  2127 
  2128 lemma independent_bound:
  2129   fixes S:: "('a::euclidean_space) set"
  2130   shows "independent S \<Longrightarrow> finite S \<and> card S <= DIM('a::euclidean_space)"
  2131   using independent_span_bound[of "(basis::nat=>'a) ` {..<DIM('a)}" S] by auto
  2132 
  2133 lemma dependent_biggerset: "(finite (S::('a::euclidean_space) set) ==> card S > DIM('a)) ==> dependent S"
  2134   by (metis independent_bound not_less)
  2135 
  2136 text {* Hence we can create a maximal independent subset. *}
  2137 
  2138 lemma maximal_independent_subset_extend:
  2139   assumes sv: "(S::('a::euclidean_space) set) \<subseteq> V" and iS: "independent S"
  2140   shows "\<exists>B. S \<subseteq> B \<and> B \<subseteq> V \<and> independent B \<and> V \<subseteq> span B"
  2141   using sv iS
  2142 proof(induct "DIM('a) - card S" arbitrary: S rule: less_induct)
  2143   case less
  2144   note sv = `S \<subseteq> V` and i = `independent S`
  2145   let ?P = "\<lambda>B. S \<subseteq> B \<and> B \<subseteq> V \<and> independent B \<and> V \<subseteq> span B"
  2146   let ?ths = "\<exists>x. ?P x"
  2147   let ?d = "DIM('a)"
  2148   {assume "V \<subseteq> span S"
  2149     then have ?ths  using sv i by blast }
  2150   moreover
  2151   {assume VS: "\<not> V \<subseteq> span S"
  2152     from VS obtain a where a: "a \<in> V" "a \<notin> span S" by blast
  2153     from a have aS: "a \<notin> S" by (auto simp add: span_superset)
  2154     have th0: "insert a S \<subseteq> V" using a sv by blast
  2155     from independent_insert[of a S]  i a
  2156     have th1: "independent (insert a S)" by auto
  2157     have mlt: "?d - card (insert a S) < ?d - card S"
  2158       using aS a independent_bound[OF th1]
  2159       by auto
  2160 
  2161     from less(1)[OF mlt th0 th1]
  2162     obtain B where B: "insert a S \<subseteq> B" "B \<subseteq> V" "independent B" " V \<subseteq> span B"
  2163       by blast
  2164     from B have "?P B" by auto
  2165     then have ?ths by blast}
  2166   ultimately show ?ths by blast
  2167 qed
  2168 
  2169 lemma maximal_independent_subset:
  2170   "\<exists>(B:: ('a::euclidean_space) set). B\<subseteq> V \<and> independent B \<and> V \<subseteq> span B"
  2171   by (metis maximal_independent_subset_extend[of "{}:: ('a::euclidean_space) set"] empty_subsetI independent_empty)
  2172 
  2173 
  2174 text {* Notion of dimension. *}
  2175 
  2176 definition "dim V = (SOME n. \<exists>B. B \<subseteq> V \<and> independent B \<and> V \<subseteq> span B \<and> (card B = n))"
  2177 
  2178 lemma basis_exists:  "\<exists>B. (B :: ('a::euclidean_space) set) \<subseteq> V \<and> independent B \<and> V \<subseteq> span B \<and> (card B = dim V)"
  2179 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)"]
  2180 using maximal_independent_subset[of V] independent_bound
  2181 by auto
  2182 
  2183 text {* Consequences of independence or spanning for cardinality. *}
  2184 
  2185 lemma independent_card_le_dim: 
  2186   assumes "(B::('a::euclidean_space) set) \<subseteq> V" and "independent B" shows "card B \<le> dim V"
  2187 proof -
  2188   from basis_exists[of V] `B \<subseteq> V`
  2189   obtain B' where "independent B'" and "B \<subseteq> span B'" and "card B' = dim V" by blast
  2190   with independent_span_bound[OF _ `independent B` `B \<subseteq> span B'`] independent_bound[of B']
  2191   show ?thesis by auto
  2192 qed
  2193 
  2194 lemma span_card_ge_dim:  "(B::('a::euclidean_space) set) \<subseteq> V \<Longrightarrow> V \<subseteq> span B \<Longrightarrow> finite B \<Longrightarrow> dim V \<le> card B"
  2195   by (metis basis_exists[of V] independent_span_bound subset_trans)
  2196 
  2197 lemma basis_card_eq_dim:
  2198   "B \<subseteq> (V:: ('a::euclidean_space) set) \<Longrightarrow> V \<subseteq> span B \<Longrightarrow> independent B \<Longrightarrow> finite B \<and> card B = dim V"
  2199   by (metis order_eq_iff independent_card_le_dim span_card_ge_dim independent_bound)
  2200 
  2201 lemma dim_unique: "(B::('a::euclidean_space) set) \<subseteq> V \<Longrightarrow> V \<subseteq> span B \<Longrightarrow> independent B \<Longrightarrow> card B = n \<Longrightarrow> dim V = n"
  2202   by (metis basis_card_eq_dim)
  2203 
  2204 text {* More lemmas about dimension. *}
  2205 
  2206 lemma dim_UNIV: "dim (UNIV :: ('a::euclidean_space) set) = DIM('a)"
  2207   apply (rule dim_unique[of "(basis::nat=>'a) ` {..<DIM('a)}"])
  2208   using independent_basis by auto
  2209 
  2210 lemma dim_subset:
  2211   "(S:: ('a::euclidean_space) set) \<subseteq> T \<Longrightarrow> dim S \<le> dim T"
  2212   using basis_exists[of T] basis_exists[of S]
  2213   by (metis independent_card_le_dim subset_trans)
  2214 
  2215 lemma dim_subset_UNIV: "dim (S:: ('a::euclidean_space) set) \<le> DIM('a)"
  2216   by (metis dim_subset subset_UNIV dim_UNIV)
  2217 
  2218 text {* Converses to those. *}
  2219 
  2220 lemma card_ge_dim_independent:
  2221   assumes BV:"(B::('a::euclidean_space) set) \<subseteq> V" and iB:"independent B" and dVB:"dim V \<le> card B"
  2222   shows "V \<subseteq> span B"
  2223 proof-
  2224   {fix a assume aV: "a \<in> V"
  2225     {assume aB: "a \<notin> span B"
  2226       then have iaB: "independent (insert a B)" using iB aV  BV by (simp add: independent_insert)
  2227       from aV BV have th0: "insert a B \<subseteq> V" by blast
  2228       from aB have "a \<notin>B" by (auto simp add: span_superset)
  2229       with independent_card_le_dim[OF th0 iaB] dVB independent_bound[OF iB] have False by auto }
  2230     then have "a \<in> span B"  by blast}
  2231   then show ?thesis by blast
  2232 qed
  2233 
  2234 lemma card_le_dim_spanning:
  2235   assumes BV: "(B:: ('a::euclidean_space) set) \<subseteq> V" and VB: "V \<subseteq> span B"
  2236   and fB: "finite B" and dVB: "dim V \<ge> card B"
  2237   shows "independent B"
  2238 proof-
  2239   {fix a assume a: "a \<in> B" "a \<in> span (B -{a})"
  2240     from a fB have c0: "card B \<noteq> 0" by auto
  2241     from a fB have cb: "card (B -{a}) = card B - 1" by auto
  2242     from BV a have th0: "B -{a} \<subseteq> V" by blast
  2243     {fix x assume x: "x \<in> V"
  2244       from a have eq: "insert a (B -{a}) = B" by blast
  2245       from x VB have x': "x \<in> span B" by blast
  2246       from span_trans[OF a(2), unfolded eq, OF x']
  2247       have "x \<in> span (B -{a})" . }
  2248     then have th1: "V \<subseteq> span (B -{a})" by blast
  2249     have th2: "finite (B -{a})" using fB by auto
  2250     from span_card_ge_dim[OF th0 th1 th2]
  2251     have c: "dim V \<le> card (B -{a})" .
  2252     from c c0 dVB cb have False by simp}
  2253   then show ?thesis unfolding dependent_def by blast
  2254 qed
  2255 
  2256 lemma card_eq_dim: "(B:: ('a::euclidean_space) set) \<subseteq> V \<Longrightarrow> card B = dim V \<Longrightarrow> finite B \<Longrightarrow> independent B \<longleftrightarrow> V \<subseteq> span B"
  2257   by (metis order_eq_iff card_le_dim_spanning
  2258     card_ge_dim_independent)
  2259 
  2260 text {* More general size bound lemmas. *}
  2261 
  2262 lemma independent_bound_general:
  2263   "independent (S:: ('a::euclidean_space) set) \<Longrightarrow> finite S \<and> card S \<le> dim S"
  2264   by (metis independent_card_le_dim independent_bound subset_refl)
  2265 
  2266 lemma dependent_biggerset_general: "(finite (S:: ('a::euclidean_space) set) \<Longrightarrow> card S > dim S) \<Longrightarrow> dependent S"
  2267   using independent_bound_general[of S] by (metis linorder_not_le)
  2268 
  2269 lemma dim_span: "dim (span (S:: ('a::euclidean_space) set)) = dim S"
  2270 proof-
  2271   have th0: "dim S \<le> dim (span S)"
  2272     by (auto simp add: subset_eq intro: dim_subset span_superset)
  2273   from basis_exists[of S]
  2274   obtain B where B: "B \<subseteq> S" "independent B" "S \<subseteq> span B" "card B = dim S" by blast
  2275   from B have fB: "finite B" "card B = dim S" using independent_bound by blast+
  2276   have bSS: "B \<subseteq> span S" using B(1) by (metis subset_eq span_inc)
  2277   have sssB: "span S \<subseteq> span B" using span_mono[OF B(3)] by (simp add: span_span)
  2278   from span_card_ge_dim[OF bSS sssB fB(1)] th0 show ?thesis
  2279     using fB(2)  by arith
  2280 qed
  2281 
  2282 lemma subset_le_dim: "(S:: ('a::euclidean_space) set) \<subseteq> span T \<Longrightarrow> dim S \<le> dim T"
  2283   by (metis dim_span dim_subset)
  2284 
  2285 lemma span_eq_dim: "span (S:: ('a::euclidean_space) set) = span T ==> dim S = dim T"
  2286   by (metis dim_span)
  2287 
  2288 lemma spans_image:
  2289   assumes lf: "linear f" and VB: "V \<subseteq> span B"
  2290   shows "f ` V \<subseteq> span (f ` B)"
  2291   unfolding span_linear_image[OF lf]
  2292   by (metis VB image_mono)
  2293 
  2294 lemma dim_image_le:
  2295   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  2296   assumes lf: "linear f" shows "dim (f ` S) \<le> dim (S)"
  2297 proof-
  2298   from basis_exists[of S] obtain B where
  2299     B: "B \<subseteq> S" "independent B" "S \<subseteq> span B" "card B = dim S" by blast
  2300   from B have fB: "finite B" "card B = dim S" using independent_bound by blast+
  2301   have "dim (f ` S) \<le> card (f ` B)"
  2302     apply (rule span_card_ge_dim)
  2303     using lf B fB by (auto simp add: span_linear_image spans_image subset_image_iff)
  2304   also have "\<dots> \<le> dim S" using card_image_le[OF fB(1)] fB by simp
  2305   finally show ?thesis .
  2306 qed
  2307 
  2308 text {* Relation between bases and injectivity/surjectivity of map. *}
  2309 
  2310 lemma spanning_surjective_image:
  2311   assumes us: "UNIV \<subseteq> span S"
  2312   and lf: "linear f" and sf: "surj f"
  2313   shows "UNIV \<subseteq> span (f ` S)"
  2314 proof-
  2315   have "UNIV \<subseteq> f ` UNIV" using sf by (auto simp add: surj_def)
  2316   also have " \<dots> \<subseteq> span (f ` S)" using spans_image[OF lf us] .
  2317 finally show ?thesis .
  2318 qed
  2319 
  2320 lemma independent_injective_image:
  2321   assumes iS: "independent S" and lf: "linear f" and fi: "inj f"
  2322   shows "independent (f ` S)"
  2323 proof-
  2324   {fix a assume a: "a \<in> S" "f a \<in> span (f ` S - {f a})"
  2325     have eq: "f ` S - {f a} = f ` (S - {a})" using fi
  2326       by (auto simp add: inj_on_def)
  2327     from a have "f a \<in> f ` span (S -{a})"
  2328       unfolding eq span_linear_image[OF lf, of "S - {a}"]  by blast
  2329     hence "a \<in> span (S -{a})" using fi by (auto simp add: inj_on_def)
  2330     with a(1) iS  have False by (simp add: dependent_def) }
  2331   then show ?thesis unfolding dependent_def by blast
  2332 qed
  2333 
  2334 text {* Picking an orthogonal replacement for a spanning set. *}
  2335 
  2336     (* FIXME : Move to some general theory ?*)
  2337 definition "pairwise R S \<longleftrightarrow> (\<forall>x \<in> S. \<forall>y\<in> S. x\<noteq>y \<longrightarrow> R x y)"
  2338 
  2339 lemma vector_sub_project_orthogonal: "(b::'a::euclidean_space) \<bullet> (x - ((b \<bullet> x) / (b \<bullet> b)) *\<^sub>R b) = 0"
  2340   unfolding inner_simps by auto
  2341 
  2342 lemma basis_orthogonal:
  2343   fixes B :: "('a::euclidean_space) set"
  2344   assumes fB: "finite B"
  2345   shows "\<exists>C. finite C \<and> card C \<le> card B \<and> span C = span B \<and> pairwise orthogonal C"
  2346   (is " \<exists>C. ?P B C")
  2347 proof(induct rule: finite_induct[OF fB])
  2348   case 1 thus ?case apply (rule exI[where x="{}"]) by (auto simp add: pairwise_def)
  2349 next
  2350   case (2 a B)
  2351   note fB = `finite B` and aB = `a \<notin> B`
  2352   from `\<exists>C. finite C \<and> card C \<le> card B \<and> span C = span B \<and> pairwise orthogonal C`
  2353   obtain C where C: "finite C" "card C \<le> card B"
  2354     "span C = span B" "pairwise orthogonal C" by blast
  2355   let ?a = "a - setsum (\<lambda>x. (x \<bullet> a / (x \<bullet> x)) *\<^sub>R x) C"
  2356   let ?C = "insert ?a C"
  2357   from C(1) have fC: "finite ?C" by simp
  2358   from fB aB C(1,2) have cC: "card ?C \<le> card (insert a B)" by (simp add: card_insert_if)
  2359   {fix x k
  2360     have th0: "\<And>(a::'a) b c. a - (b - c) = c + (a - b)" by (simp add: field_simps)
  2361     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"
  2362       apply (simp only: scaleR_right_diff_distrib th0)
  2363       apply (rule span_add_eq)
  2364       apply (rule span_mul)
  2365       apply (rule span_setsum[OF C(1)])
  2366       apply clarify
  2367       apply (rule span_mul)
  2368       by (rule span_superset)}
  2369   then have SC: "span ?C = span (insert a B)"
  2370     unfolding set_eq_iff span_breakdown_eq C(3)[symmetric] by auto
  2371   thm pairwise_def
  2372   {fix x y assume xC: "x \<in> ?C" and yC: "y \<in> ?C" and xy: "x \<noteq> y"
  2373     {assume xa: "x = ?a" and ya: "y = ?a"
  2374       have "orthogonal x y" using xa ya xy by blast}
  2375     moreover
  2376     {assume xa: "x = ?a" and ya: "y \<noteq> ?a" "y \<in> C"
  2377       from ya have Cy: "C = insert y (C - {y})" by blast
  2378       have fth: "finite (C - {y})" using C by simp
  2379       have "orthogonal x y"
  2380         using xa ya
  2381         unfolding orthogonal_def xa inner_simps diff_eq_0_iff_eq
  2382         apply simp
  2383         apply (subst Cy)
  2384         using C(1) fth
  2385         apply (simp only: setsum_clauses)
  2386         apply (auto simp add: inner_simps inner_commute[of y a] dot_lsum[OF fth])
  2387         apply (rule setsum_0')
  2388         apply clarsimp
  2389         apply (rule C(4)[unfolded pairwise_def orthogonal_def, rule_format])
  2390         by auto}
  2391     moreover
  2392     {assume xa: "x \<noteq> ?a" "x \<in> C" and ya: "y = ?a"
  2393       from xa have Cx: "C = insert x (C - {x})" by blast
  2394       have fth: "finite (C - {x})" using C by simp
  2395       have "orthogonal x y"
  2396         using xa ya
  2397         unfolding orthogonal_def ya inner_simps diff_eq_0_iff_eq
  2398         apply simp
  2399         apply (subst Cx)
  2400         using C(1) fth
  2401         apply (simp only: setsum_clauses)
  2402         apply (subst inner_commute[of x])
  2403         apply (auto simp add: inner_simps inner_commute[of x a] dot_rsum[OF fth])
  2404         apply (rule setsum_0')
  2405         apply clarsimp
  2406         apply (rule C(4)[unfolded pairwise_def orthogonal_def, rule_format])
  2407         by auto}
  2408     moreover
  2409     {assume xa: "x \<in> C" and ya: "y \<in> C"
  2410       have "orthogonal x y" using xa ya xy C(4) unfolding pairwise_def by blast}
  2411     ultimately have "orthogonal x y" using xC yC by blast}
  2412   then have CPO: "pairwise orthogonal ?C" unfolding pairwise_def by blast
  2413   from fC cC SC CPO have "?P (insert a B) ?C" by blast
  2414   then show ?case by blast
  2415 qed
  2416 
  2417 lemma orthogonal_basis_exists:
  2418   fixes V :: "('a::euclidean_space) set"
  2419   shows "\<exists>B. independent B \<and> B \<subseteq> span V \<and> V \<subseteq> span B \<and> (card B = dim V) \<and> pairwise orthogonal B"
  2420 proof-
  2421   from basis_exists[of V] obtain B where B: "B \<subseteq> V" "independent B" "V \<subseteq> span B" "card B = dim V" by blast
  2422   from B have fB: "finite B" "card B = dim V" using independent_bound by auto
  2423   from basis_orthogonal[OF fB(1)] obtain C where
  2424     C: "finite C" "card C \<le> card B" "span C = span B" "pairwise orthogonal C" by blast
  2425   from C B
  2426   have CSV: "C \<subseteq> span V" by (metis span_inc span_mono subset_trans)
  2427   from span_mono[OF B(3)]  C have SVC: "span V \<subseteq> span C" by (simp add: span_span)
  2428   from card_le_dim_spanning[OF CSV SVC C(1)] C(2,3) fB
  2429   have iC: "independent C" by (simp add: dim_span)
  2430   from C fB have "card C \<le> dim V" by simp
  2431   moreover have "dim V \<le> card C" using span_card_ge_dim[OF CSV SVC C(1)]
  2432     by (simp add: dim_span)
  2433   ultimately have CdV: "card C = dim V" using C(1) by simp
  2434   from C B CSV CdV iC show ?thesis by auto
  2435 qed
  2436 
  2437 lemma span_eq: "span S = span T \<longleftrightarrow> S \<subseteq> span T \<and> T \<subseteq> span S"
  2438   using span_inc[unfolded subset_eq] using span_mono[of T "span S"] span_mono[of S "span T"]
  2439   by(auto simp add: span_span)
  2440 
  2441 text {* Low-dimensional subset is in a hyperplane (weak orthogonal complement). *}
  2442 
  2443 lemma span_not_univ_orthogonal: fixes S::"('a::euclidean_space) set"
  2444   assumes sU: "span S \<noteq> UNIV"
  2445   shows "\<exists>(a::'a). a \<noteq>0 \<and> (\<forall>x \<in> span S. a \<bullet> x = 0)"
  2446 proof-
  2447   from sU obtain a where a: "a \<notin> span S" by blast
  2448   from orthogonal_basis_exists obtain B where
  2449     B: "independent B" "B \<subseteq> span S" "S \<subseteq> span B" "card B = dim S" "pairwise orthogonal B"
  2450     by blast
  2451   from B have fB: "finite B" "card B = dim S" using independent_bound by auto
  2452   from span_mono[OF B(2)] span_mono[OF B(3)]
  2453   have sSB: "span S = span B" by (simp add: span_span)
  2454   let ?a = "a - setsum (\<lambda>b. (a \<bullet> b / (b \<bullet> b)) *\<^sub>R b) B"
  2455   have "setsum (\<lambda>b. (a \<bullet> b / (b \<bullet> b)) *\<^sub>R b) B \<in> span S"
  2456     unfolding sSB
  2457     apply (rule span_setsum[OF fB(1)])
  2458     apply clarsimp
  2459     apply (rule span_mul)
  2460     by (rule span_superset)
  2461   with a have a0:"?a  \<noteq> 0" by auto
  2462   have "\<forall>x\<in>span B. ?a \<bullet> x = 0"
  2463   proof(rule span_induct')
  2464     show "subspace (\<lambda>x. ?a \<bullet> x = 0)" by (auto simp add: subspace_def mem_def inner_simps)
  2465 next
  2466     {fix x assume x: "x \<in> B"
  2467       from x have B': "B = insert x (B - {x})" by blast
  2468       have fth: "finite (B - {x})" using fB by simp
  2469       have "?a \<bullet> x = 0"
  2470         apply (subst B') using fB fth
  2471         unfolding setsum_clauses(2)[OF fth]
  2472         apply simp unfolding inner_simps
  2473         apply (clarsimp simp add: inner_simps dot_lsum)
  2474         apply (rule setsum_0', rule ballI)
  2475         unfolding inner_commute
  2476         by (auto simp add: x field_simps intro: B(5)[unfolded pairwise_def orthogonal_def, rule_format])}
  2477     then show "\<forall>x \<in> B. ?a \<bullet> x = 0" by blast
  2478   qed
  2479   with a0 show ?thesis unfolding sSB by (auto intro: exI[where x="?a"])
  2480 qed
  2481 
  2482 lemma span_not_univ_subset_hyperplane:
  2483   assumes SU: "span S \<noteq> (UNIV ::('a::euclidean_space) set)"
  2484   shows "\<exists> a. a \<noteq>0 \<and> span S \<subseteq> {x. a \<bullet> x = 0}"
  2485   using span_not_univ_orthogonal[OF SU] by auto
  2486 
  2487 lemma lowdim_subset_hyperplane: fixes S::"('a::euclidean_space) set"
  2488   assumes d: "dim S < DIM('a)"
  2489   shows "\<exists>(a::'a). a  \<noteq> 0 \<and> span S \<subseteq> {x. a \<bullet> x = 0}"
  2490 proof-
  2491   {assume "span S = UNIV"
  2492     hence "dim (span S) = dim (UNIV :: ('a) set)" by simp
  2493     hence "dim S = DIM('a)" by (simp add: dim_span dim_UNIV)
  2494     with d have False by arith}
  2495   hence th: "span S \<noteq> UNIV" by blast
  2496   from span_not_univ_subset_hyperplane[OF th] show ?thesis .
  2497 qed
  2498 
  2499 text {* We can extend a linear basis-basis injection to the whole set. *}
  2500 
  2501 lemma linear_indep_image_lemma:
  2502   assumes lf: "linear f" and fB: "finite B"
  2503   and ifB: "independent (f ` B)"
  2504   and fi: "inj_on f B" and xsB: "x \<in> span B"
  2505   and fx: "f x = 0"
  2506   shows "x = 0"
  2507   using fB ifB fi xsB fx
  2508 proof(induct arbitrary: x rule: finite_induct[OF fB])
  2509   case 1 thus ?case by (auto simp add:  span_empty)
  2510 next
  2511   case (2 a b x)
  2512   have fb: "finite b" using "2.prems" by simp
  2513   have th0: "f ` b \<subseteq> f ` (insert a b)"
  2514     apply (rule image_mono) by blast
  2515   from independent_mono[ OF "2.prems"(2) th0]
  2516   have ifb: "independent (f ` b)"  .
  2517   have fib: "inj_on f b"
  2518     apply (rule subset_inj_on [OF "2.prems"(3)])
  2519     by blast
  2520   from span_breakdown[of a "insert a b", simplified, OF "2.prems"(4)]
  2521   obtain k where k: "x - k*\<^sub>R a \<in> span (b -{a})" by blast
  2522   have "f (x - k*\<^sub>R a) \<in> span (f ` b)"
  2523     unfolding span_linear_image[OF lf]
  2524     apply (rule imageI)
  2525     using k span_mono[of "b-{a}" b] by blast
  2526   hence "f x - k*\<^sub>R f a \<in> span (f ` b)"
  2527     by (simp add: linear_sub[OF lf] linear_cmul[OF lf])
  2528   hence th: "-k *\<^sub>R f a \<in> span (f ` b)"
  2529     using "2.prems"(5) by simp
  2530   {assume k0: "k = 0"
  2531     from k0 k have "x \<in> span (b -{a})" by simp
  2532     then have "x \<in> span b" using span_mono[of "b-{a}" b]
  2533       by blast}
  2534   moreover
  2535   {assume k0: "k \<noteq> 0"
  2536     from span_mul[OF th, of "- 1/ k"] k0
  2537     have th1: "f a \<in> span (f ` b)"
  2538       by auto
  2539     from inj_on_image_set_diff[OF "2.prems"(3), of "insert a b " "{a}", symmetric]
  2540     have tha: "f ` insert a b - f ` {a} = f ` (insert a b - {a})" by blast
  2541     from "2.prems"(2)[unfolded dependent_def bex_simps(10), rule_format, of "f a"]
  2542     have "f a \<notin> span (f ` b)" using tha
  2543       using "2.hyps"(2)
  2544       "2.prems"(3) by auto
  2545     with th1 have False by blast
  2546     then have "x \<in> span b" by blast}
  2547   ultimately have xsb: "x \<in> span b" by blast
  2548   from "2.hyps"(3)[OF fb ifb fib xsb "2.prems"(5)]
  2549   show "x = 0" .
  2550 qed
  2551 
  2552 text {* We can extend a linear mapping from basis. *}
  2553 
  2554 lemma linear_independent_extend_lemma:
  2555   fixes f :: "'a::real_vector \<Rightarrow> 'b::real_vector"
  2556   assumes fi: "finite B" and ib: "independent B"
  2557   shows "\<exists>g. (\<forall>x\<in> span B. \<forall>y\<in> span B. g (x + y) = g x + g y)
  2558            \<and> (\<forall>x\<in> span B. \<forall>c. g (c*\<^sub>R x) = c *\<^sub>R g x)
  2559            \<and> (\<forall>x\<in> B. g x = f x)"
  2560 using ib fi
  2561 proof(induct rule: finite_induct[OF fi])
  2562   case 1 thus ?case by (auto simp add: span_empty)
  2563 next
  2564   case (2 a b)
  2565   from "2.prems" "2.hyps" have ibf: "independent b" "finite b"
  2566     by (simp_all add: independent_insert)
  2567   from "2.hyps"(3)[OF ibf] obtain g where
  2568     g: "\<forall>x\<in>span b. \<forall>y\<in>span b. g (x + y) = g x + g y"
  2569     "\<forall>x\<in>span b. \<forall>c. g (c *\<^sub>R x) = c *\<^sub>R g x" "\<forall>x\<in>b. g x = f x" by blast
  2570   let ?h = "\<lambda>z. SOME k. (z - k *\<^sub>R a) \<in> span b"
  2571   {fix z assume z: "z \<in> span (insert a b)"
  2572     have th0: "z - ?h z *\<^sub>R a \<in> span b"
  2573       apply (rule someI_ex)
  2574       unfolding span_breakdown_eq[symmetric]
  2575       using z .
  2576     {fix k assume k: "z - k *\<^sub>R a \<in> span b"
  2577       have eq: "z - ?h z *\<^sub>R a - (z - k*\<^sub>R a) = (k - ?h z) *\<^sub>R a"
  2578         by (simp add: field_simps scaleR_left_distrib [symmetric])
  2579       from span_sub[OF th0 k]
  2580       have khz: "(k - ?h z) *\<^sub>R a \<in> span b" by (simp add: eq)
  2581       {assume "k \<noteq> ?h z" hence k0: "k - ?h z \<noteq> 0" by simp
  2582         from k0 span_mul[OF khz, of "1 /(k - ?h z)"]
  2583         have "a \<in> span b" by simp
  2584         with "2.prems"(1) "2.hyps"(2) have False
  2585           by (auto simp add: dependent_def)}
  2586       then have "k = ?h z" by blast}
  2587     with th0 have "z - ?h z *\<^sub>R a \<in> span b \<and> (\<forall>k. z - k *\<^sub>R a \<in> span b \<longrightarrow> k = ?h z)" by blast}
  2588   note h = this
  2589   let ?g = "\<lambda>z. ?h z *\<^sub>R f a + g (z - ?h z *\<^sub>R a)"
  2590   {fix x y assume x: "x \<in> span (insert a b)" and y: "y \<in> span (insert a b)"
  2591     have tha: "\<And>(x::'a) y a k l. (x + y) - (k + l) *\<^sub>R a = (x - k *\<^sub>R a) + (y - l *\<^sub>R a)"
  2592       by (simp add: algebra_simps)
  2593     have addh: "?h (x + y) = ?h x + ?h y"
  2594       apply (rule conjunct2[OF h, rule_format, symmetric])
  2595       apply (rule span_add[OF x y])
  2596       unfolding tha
  2597       by (metis span_add x y conjunct1[OF h, rule_format])
  2598     have "?g (x + y) = ?g x + ?g y"
  2599       unfolding addh tha
  2600       g(1)[rule_format,OF conjunct1[OF h, OF x] conjunct1[OF h, OF y]]
  2601       by (simp add: scaleR_left_distrib)}
  2602   moreover
  2603   {fix x:: "'a" and c:: real  assume x: "x \<in> span (insert a b)"
  2604     have tha: "\<And>(x::'a) c k a. c *\<^sub>R x - (c * k) *\<^sub>R a = c *\<^sub>R (x - k *\<^sub>R a)"
  2605       by (simp add: algebra_simps)
  2606     have hc: "?h (c *\<^sub>R x) = c * ?h x"
  2607       apply (rule conjunct2[OF h, rule_format, symmetric])
  2608       apply (metis span_mul x)
  2609       by (metis tha span_mul x conjunct1[OF h])
  2610     have "?g (c *\<^sub>R x) = c*\<^sub>R ?g x"
  2611       unfolding hc tha g(2)[rule_format, OF conjunct1[OF h, OF x]]
  2612       by (simp add: algebra_simps)}
  2613   moreover
  2614   {fix x assume x: "x \<in> (insert a b)"
  2615     {assume xa: "x = a"
  2616       have ha1: "1 = ?h a"
  2617         apply (rule conjunct2[OF h, rule_format])
  2618         apply (metis span_superset insertI1)
  2619         using conjunct1[OF h, OF span_superset, OF insertI1]
  2620         by (auto simp add: span_0)
  2621 
  2622       from xa ha1[symmetric] have "?g x = f x"
  2623         apply simp
  2624         using g(2)[rule_format, OF span_0, of 0]
  2625         by simp}
  2626     moreover
  2627     {assume xb: "x \<in> b"
  2628       have h0: "0 = ?h x"
  2629         apply (rule conjunct2[OF h, rule_format])
  2630         apply (metis  span_superset x)
  2631         apply simp
  2632         apply (metis span_superset xb)
  2633         done
  2634       have "?g x = f x"
  2635         by (simp add: h0[symmetric] g(3)[rule_format, OF xb])}
  2636     ultimately have "?g x = f x" using x by blast }
  2637   ultimately show ?case apply - apply (rule exI[where x="?g"]) by blast
  2638 qed
  2639 
  2640 lemma linear_independent_extend:
  2641   assumes iB: "independent (B:: ('a::euclidean_space) set)"
  2642   shows "\<exists>g. linear g \<and> (\<forall>x\<in>B. g x = f x)"
  2643 proof-
  2644   from maximal_independent_subset_extend[of B UNIV] iB
  2645   obtain C where C: "B \<subseteq> C" "independent C" "\<And>x. x \<in> span C" by auto
  2646 
  2647   from C(2) independent_bound[of C] linear_independent_extend_lemma[of C f]
  2648   obtain g where g: "(\<forall>x\<in> span C. \<forall>y\<in> span C. g (x + y) = g x + g y)
  2649            \<and> (\<forall>x\<in> span C. \<forall>c. g (c*\<^sub>R x) = c *\<^sub>R g x)
  2650            \<and> (\<forall>x\<in> C. g x = f x)" by blast
  2651   from g show ?thesis unfolding linear_def using C
  2652     apply clarsimp by blast
  2653 qed
  2654 
  2655 text {* Can construct an isomorphism between spaces of same dimension. *}
  2656 
  2657 lemma card_le_inj: assumes fA: "finite A" and fB: "finite B"
  2658   and c: "card A \<le> card B" shows "(\<exists>f. f ` A \<subseteq> B \<and> inj_on f A)"
  2659 using fB c
  2660 proof(induct arbitrary: B rule: finite_induct[OF fA])
  2661   case 1 thus ?case by simp
  2662 next
  2663   case (2 x s t)
  2664   thus ?case
  2665   proof(induct rule: finite_induct[OF "2.prems"(1)])
  2666     case 1    then show ?case by simp
  2667   next
  2668     case (2 y t)
  2669     from "2.prems"(1,2,5) "2.hyps"(1,2) have cst:"card s \<le> card t" by simp
  2670     from "2.prems"(3) [OF "2.hyps"(1) cst] obtain f where
  2671       f: "f ` s \<subseteq> t \<and> inj_on f s" by blast
  2672     from f "2.prems"(2) "2.hyps"(2) show ?case
  2673       apply -
  2674       apply (rule exI[where x = "\<lambda>z. if z = x then y else f z"])
  2675       by (auto simp add: inj_on_def)
  2676   qed
  2677 qed
  2678 
  2679 lemma card_subset_eq: assumes fB: "finite B" and AB: "A \<subseteq> B" and
  2680   c: "card A = card B"
  2681   shows "A = B"
  2682 proof-
  2683   from fB AB have fA: "finite A" by (auto intro: finite_subset)
  2684   from fA fB have fBA: "finite (B - A)" by auto
  2685   have e: "A \<inter> (B - A) = {}" by blast
  2686   have eq: "A \<union> (B - A) = B" using AB by blast
  2687   from card_Un_disjoint[OF fA fBA e, unfolded eq c]
  2688   have "card (B - A) = 0" by arith
  2689   hence "B - A = {}" unfolding card_eq_0_iff using fA fB by simp
  2690   with AB show "A = B" by blast
  2691 qed
  2692 
  2693 lemma subspace_isomorphism:
  2694   assumes s: "subspace (S:: ('a::euclidean_space) set)"
  2695   and t: "subspace (T :: ('b::euclidean_space) set)"
  2696   and d: "dim S = dim T"
  2697   shows "\<exists>f. linear f \<and> f ` S = T \<and> inj_on f S"
  2698 proof-
  2699   from basis_exists[of S] independent_bound obtain B where
  2700     B: "B \<subseteq> S" "independent B" "S \<subseteq> span B" "card B = dim S" and fB: "finite B" by blast
  2701   from basis_exists[of T] independent_bound obtain C where
  2702     C: "C \<subseteq> T" "independent C" "T \<subseteq> span C" "card C = dim T" and fC: "finite C" by blast
  2703   from B(4) C(4) card_le_inj[of B C] d obtain f where
  2704     f: "f ` B \<subseteq> C" "inj_on f B" using `finite B` `finite C` by auto
  2705   from linear_independent_extend[OF B(2)] obtain g where
  2706     g: "linear g" "\<forall>x\<in> B. g x = f x" by blast
  2707   from inj_on_iff_eq_card[OF fB, of f] f(2)
  2708   have "card (f ` B) = card B" by simp
  2709   with B(4) C(4) have ceq: "card (f ` B) = card C" using d
  2710     by simp
  2711   have "g ` B = f ` B" using g(2)
  2712     by (auto simp add: image_iff)
  2713   also have "\<dots> = C" using card_subset_eq[OF fC f(1) ceq] .
  2714   finally have gBC: "g ` B = C" .
  2715   have gi: "inj_on g B" using f(2) g(2)
  2716     by (auto simp add: inj_on_def)
  2717   note g0 = linear_indep_image_lemma[OF g(1) fB, unfolded gBC, OF C(2) gi]
  2718   {fix x y assume x: "x \<in> S" and y: "y \<in> S" and gxy:"g x = g y"
  2719     from B(3) x y have x': "x \<in> span B" and y': "y \<in> span B" by blast+
  2720     from gxy have th0: "g (x - y) = 0" by (simp add: linear_sub[OF g(1)])
  2721     have th1: "x - y \<in> span B" using x' y' by (metis span_sub)
  2722     have "x=y" using g0[OF th1 th0] by simp }
  2723   then have giS: "inj_on g S"
  2724     unfolding inj_on_def by blast
  2725   from span_subspace[OF B(1,3) s]
  2726   have "g ` S = span (g ` B)" by (simp add: span_linear_image[OF g(1)])
  2727   also have "\<dots> = span C" unfolding gBC ..
  2728   also have "\<dots> = T" using span_subspace[OF C(1,3) t] .
  2729   finally have gS: "g ` S = T" .
  2730   from g(1) gS giS show ?thesis by blast
  2731 qed
  2732 
  2733 text {* Linear functions are equal on a subspace if they are on a spanning set. *}
  2734 
  2735 lemma subspace_kernel:
  2736   assumes lf: "linear f"
  2737   shows "subspace {x. f x = 0}"
  2738 apply (simp add: subspace_def)
  2739 by (simp add: linear_add[OF lf] linear_cmul[OF lf] linear_0[OF lf])
  2740 
  2741 lemma linear_eq_0_span:
  2742   assumes lf: "linear f" and f0: "\<forall>x\<in>B. f x = 0"
  2743   shows "\<forall>x \<in> span B. f x = 0"
  2744 proof
  2745   fix x assume x: "x \<in> span B"
  2746   let ?P = "\<lambda>x. f x = 0"
  2747   from subspace_kernel[OF lf] have "subspace ?P" unfolding Collect_def .
  2748   with x f0 span_induct[of B "?P" x] show "f x = 0" by blast
  2749 qed
  2750 
  2751 lemma linear_eq_0:
  2752   assumes lf: "linear f" and SB: "S \<subseteq> span B" and f0: "\<forall>x\<in>B. f x = 0"
  2753   shows "\<forall>x \<in> S. f x = 0"
  2754   by (metis linear_eq_0_span[OF lf] subset_eq SB f0)
  2755 
  2756 lemma linear_eq:
  2757   assumes lf: "linear f" and lg: "linear g" and S: "S \<subseteq> span B"
  2758   and fg: "\<forall> x\<in> B. f x = g x"
  2759   shows "\<forall>x\<in> S. f x = g x"
  2760 proof-
  2761   let ?h = "\<lambda>x. f x - g x"
  2762   from fg have fg': "\<forall>x\<in> B. ?h x = 0" by simp
  2763   from linear_eq_0[OF linear_compose_sub[OF lf lg] S fg']
  2764   show ?thesis by simp
  2765 qed
  2766 
  2767 lemma linear_eq_stdbasis:
  2768   assumes lf: "linear (f::'a::euclidean_space \<Rightarrow> _)" and lg: "linear g"
  2769   and fg: "\<forall>i<DIM('a::euclidean_space). f (basis i) = g(basis i)"
  2770   shows "f = g"
  2771 proof-
  2772   let ?U = "{..<DIM('a)}"
  2773   let ?I = "(basis::nat=>'a) ` {..<DIM('a)}"
  2774   {fix x assume x: "x \<in> (UNIV :: 'a set)"
  2775     from equalityD2[OF span_basis'[where 'a='a]]
  2776     have IU: " (UNIV :: 'a set) \<subseteq> span ?I" by blast
  2777     have "f x = g x" apply(rule linear_eq[OF lf lg IU,rule_format]) using fg x by auto }
  2778   then show ?thesis by (auto intro: ext)
  2779 qed
  2780 
  2781 text {* Similar results for bilinear functions. *}
  2782 
  2783 lemma bilinear_eq:
  2784   assumes bf: "bilinear f"
  2785   and bg: "bilinear g"
  2786   and SB: "S \<subseteq> span B" and TC: "T \<subseteq> span C"
  2787   and fg: "\<forall>x\<in> B. \<forall>y\<in> C. f x y = g x y"
  2788   shows "\<forall>x\<in>S. \<forall>y\<in>T. f x y = g x y "
  2789 proof-
  2790   let ?P = "\<lambda>x. \<forall>y\<in> span C. f x y = g x y"
  2791   from bf bg have sp: "subspace ?P"
  2792     unfolding bilinear_def linear_def subspace_def bf bg
  2793     by(auto simp add: span_0 mem_def bilinear_lzero[OF bf] bilinear_lzero[OF bg] span_add Ball_def intro:  bilinear_ladd[OF bf])
  2794 
  2795   have "\<forall>x \<in> span B. \<forall>y\<in> span C. f x y = g x y"
  2796     apply -
  2797     apply (rule ballI)
  2798     apply (rule span_induct[of B ?P])
  2799     defer
  2800     apply (rule sp)
  2801     apply assumption
  2802     apply (clarsimp simp add: Ball_def)
  2803     apply (rule_tac P="\<lambda>y. f xa y = g xa y" and S=C in span_induct)
  2804     using fg
  2805     apply (auto simp add: subspace_def)
  2806     using bf bg unfolding bilinear_def linear_def
  2807     by(auto simp add: span_0 mem_def bilinear_rzero[OF bf] bilinear_rzero[OF bg] span_add Ball_def intro:  bilinear_ladd[OF bf])
  2808   then show ?thesis using SB TC by (auto intro: ext)
  2809 qed
  2810 
  2811 lemma bilinear_eq_stdbasis: fixes f::"'a::euclidean_space \<Rightarrow> 'b::euclidean_space \<Rightarrow> _"
  2812   assumes bf: "bilinear f"
  2813   and bg: "bilinear g"
  2814   and fg: "\<forall>i<DIM('a). \<forall>j<DIM('b). f (basis i) (basis j) = g (basis i) (basis j)"
  2815   shows "f = g"
  2816 proof-
  2817   from fg have th: "\<forall>x \<in> (basis ` {..<DIM('a)}). \<forall>y\<in> (basis ` {..<DIM('b)}). f x y = g x y" by blast
  2818   from bilinear_eq[OF bf bg equalityD2[OF span_basis'] equalityD2[OF span_basis'] th]
  2819   show ?thesis by (blast intro: ext)
  2820 qed
  2821 
  2822 text {* Detailed theorems about left and right invertibility in general case. *}
  2823 
  2824 lemma linear_injective_left_inverse: fixes f::"'a::euclidean_space => 'b::euclidean_space"
  2825   assumes lf: "linear f" and fi: "inj f"
  2826   shows "\<exists>g. linear g \<and> g o f = id"
  2827 proof-
  2828   from linear_independent_extend[OF independent_injective_image, OF independent_basis, OF lf fi]
  2829   obtain h:: "'b => 'a" where h: "linear h"
  2830     " \<forall>x \<in> f ` basis ` {..<DIM('a)}. h x = inv f x" by blast
  2831   from h(2)
  2832   have th: "\<forall>i<DIM('a). (h \<circ> f) (basis i) = id (basis i)"
  2833     using inv_o_cancel[OF fi, unfolded fun_eq_iff id_def o_def]
  2834     by auto
  2835 
  2836   from linear_eq_stdbasis[OF linear_compose[OF lf h(1)] linear_id th]
  2837   have "h o f = id" .
  2838   then show ?thesis using h(1) by blast
  2839 qed
  2840 
  2841 lemma linear_surjective_right_inverse: fixes f::"'a::euclidean_space => 'b::euclidean_space"
  2842   assumes lf: "linear f" and sf: "surj f"
  2843   shows "\<exists>g. linear g \<and> f o g = id"
  2844 proof-
  2845   from linear_independent_extend[OF independent_basis[where 'a='b],of "inv f"]
  2846   obtain h:: "'b \<Rightarrow> 'a" where
  2847     h: "linear h" "\<forall> x\<in> basis ` {..<DIM('b)}. h x = inv f x" by blast
  2848   from h(2)
  2849   have th: "\<forall>i<DIM('b). (f o h) (basis i) = id (basis i)"
  2850     using sf by(auto simp add: surj_iff_all)
  2851   from linear_eq_stdbasis[OF linear_compose[OF h(1) lf] linear_id th]
  2852   have "f o h = id" .
  2853   then show ?thesis using h(1) by blast
  2854 qed
  2855 
  2856 text {* An injective map @{typ "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"} is also surjective. *}
  2857 
  2858 lemma linear_injective_imp_surjective:  fixes f::"'a::euclidean_space => 'a::euclidean_space"
  2859   assumes lf: "linear f" and fi: "inj f"
  2860   shows "surj f"
  2861 proof-
  2862   let ?U = "UNIV :: 'a set"
  2863   from basis_exists[of ?U] obtain B
  2864     where B: "B \<subseteq> ?U" "independent B" "?U \<subseteq> span B" "card B = dim ?U"
  2865     by blast
  2866   from B(4) have d: "dim ?U = card B" by simp
  2867   have th: "?U \<subseteq> span (f ` B)"
  2868     apply (rule card_ge_dim_independent)
  2869     apply blast
  2870     apply (rule independent_injective_image[OF B(2) lf fi])
  2871     apply (rule order_eq_refl)
  2872     apply (rule sym)
  2873     unfolding d
  2874     apply (rule card_image)
  2875     apply (rule subset_inj_on[OF fi])
  2876     by blast
  2877   from th show ?thesis
  2878     unfolding span_linear_image[OF lf] surj_def
  2879     using B(3) by blast
  2880 qed
  2881 
  2882 text {* And vice versa. *}
  2883 
  2884 lemma surjective_iff_injective_gen:
  2885   assumes fS: "finite S" and fT: "finite T" and c: "card S = card T"
  2886   and ST: "f ` S \<subseteq> T"
  2887   shows "(\<forall>y \<in> T. \<exists>x \<in> S. f x = y) \<longleftrightarrow> inj_on f S" (is "?lhs \<longleftrightarrow> ?rhs")
  2888 proof-
  2889   {assume h: "?lhs"
  2890     {fix x y assume x: "x \<in> S" and y: "y \<in> S" and f: "f x = f y"
  2891       from x fS have S0: "card S \<noteq> 0" by auto
  2892       {assume xy: "x \<noteq> y"
  2893         have th: "card S \<le> card (f ` (S - {y}))"
  2894           unfolding c
  2895           apply (rule card_mono)
  2896           apply (rule finite_imageI)
  2897           using fS apply simp
  2898           using h xy x y f unfolding subset_eq image_iff
  2899           apply auto
  2900           apply (case_tac "xa = f x")
  2901           apply (rule bexI[where x=x])
  2902           apply auto
  2903           done
  2904         also have " \<dots> \<le> card (S -{y})"
  2905           apply (rule card_image_le)
  2906           using fS by simp
  2907         also have "\<dots> \<le> card S - 1" using y fS by simp
  2908         finally have False  using S0 by arith }
  2909       then have "x = y" by blast}
  2910     then have ?rhs unfolding inj_on_def by blast}
  2911   moreover
  2912   {assume h: ?rhs
  2913     have "f ` S = T"
  2914       apply (rule card_subset_eq[OF fT ST])
  2915       unfolding card_image[OF h] using c .
  2916     then have ?lhs by blast}
  2917   ultimately show ?thesis by blast
  2918 qed
  2919 
  2920 lemma linear_surjective_imp_injective: fixes f::"'a::euclidean_space => 'a::euclidean_space"
  2921   assumes lf: "linear f" and sf: "surj f"
  2922   shows "inj f"
  2923 proof-
  2924   let ?U = "UNIV :: 'a set"
  2925   from basis_exists[of ?U] obtain B
  2926     where B: "B \<subseteq> ?U" "independent B" "?U \<subseteq> span B" and d: "card B = dim ?U"
  2927     by blast
  2928   {fix x assume x: "x \<in> span B" and fx: "f x = 0"
  2929     from B(2) have fB: "finite B" using independent_bound by auto
  2930     have fBi: "independent (f ` B)"
  2931       apply (rule card_le_dim_spanning[of "f ` B" ?U])
  2932       apply blast
  2933       using sf B(3)
  2934       unfolding span_linear_image[OF lf] surj_def subset_eq image_iff
  2935       apply blast
  2936       using fB apply blast
  2937       unfolding d[symmetric]
  2938       apply (rule card_image_le)
  2939       apply (rule fB)
  2940       done
  2941     have th0: "dim ?U \<le> card (f ` B)"
  2942       apply (rule span_card_ge_dim)
  2943       apply blast
  2944       unfolding span_linear_image[OF lf]
  2945       apply (rule subset_trans[where B = "f ` UNIV"])
  2946       using sf unfolding surj_def apply blast
  2947       apply (rule image_mono)
  2948       apply (rule B(3))
  2949       apply (metis finite_imageI fB)
  2950       done
  2951 
  2952     moreover have "card (f ` B) \<le> card B"
  2953       by (rule card_image_le, rule fB)
  2954     ultimately have th1: "card B = card (f ` B)" unfolding d by arith
  2955     have fiB: "inj_on f B"
  2956       unfolding surjective_iff_injective_gen[OF fB finite_imageI[OF fB] th1 subset_refl, symmetric] by blast
  2957     from linear_indep_image_lemma[OF lf fB fBi fiB x] fx
  2958     have "x = 0" by blast}
  2959   note th = this
  2960   from th show ?thesis unfolding linear_injective_0[OF lf]
  2961     using B(3) by blast
  2962 qed
  2963 
  2964 text {* Hence either is enough for isomorphism. *}
  2965 
  2966 lemma left_right_inverse_eq:
  2967   assumes fg: "f o g = id" and gh: "g o h = id"
  2968   shows "f = h"
  2969 proof-
  2970   have "f = f o (g o h)" unfolding gh by simp
  2971   also have "\<dots> = (f o g) o h" by (simp add: o_assoc)
  2972   finally show "f = h" unfolding fg by simp
  2973 qed
  2974 
  2975 lemma isomorphism_expand:
  2976   "f o g = id \<and> g o f = id \<longleftrightarrow> (\<forall>x. f(g x) = x) \<and> (\<forall>x. g(f x) = x)"
  2977   by (simp add: fun_eq_iff o_def id_def)
  2978 
  2979 lemma linear_injective_isomorphism: fixes f::"'a::euclidean_space => 'a::euclidean_space"
  2980   assumes lf: "linear f" and fi: "inj f"
  2981   shows "\<exists>f'. linear f' \<and> (\<forall>x. f' (f x) = x) \<and> (\<forall>x. f (f' x) = x)"
  2982 unfolding isomorphism_expand[symmetric]
  2983 using linear_surjective_right_inverse[OF lf linear_injective_imp_surjective[OF lf fi]] linear_injective_left_inverse[OF lf fi]
  2984 by (metis left_right_inverse_eq)
  2985 
  2986 lemma linear_surjective_isomorphism: fixes f::"'a::euclidean_space => 'a::euclidean_space"
  2987   assumes lf: "linear f" and sf: "surj f"
  2988   shows "\<exists>f'. linear f' \<and> (\<forall>x. f' (f x) = x) \<and> (\<forall>x. f (f' x) = x)"
  2989 unfolding isomorphism_expand[symmetric]
  2990 using linear_surjective_right_inverse[OF lf sf] linear_injective_left_inverse[OF lf linear_surjective_imp_injective[OF lf sf]]
  2991 by (metis left_right_inverse_eq)
  2992 
  2993 text {* Left and right inverses are the same for @{typ "'a::euclidean_space => 'a::euclidean_space"}. *}
  2994 
  2995 lemma linear_inverse_left: fixes f::"'a::euclidean_space => 'a::euclidean_space"
  2996   assumes lf: "linear f" and lf': "linear f'"
  2997   shows "f o f' = id \<longleftrightarrow> f' o f = id"
  2998 proof-
  2999   {fix f f':: "'a => 'a"
  3000     assume lf: "linear f" "linear f'" and f: "f o f' = id"
  3001     from f have sf: "surj f"
  3002       apply (auto simp add: o_def id_def surj_def)
  3003       by metis
  3004     from linear_surjective_isomorphism[OF lf(1) sf] lf f
  3005     have "f' o f = id" unfolding fun_eq_iff o_def id_def
  3006       by metis}
  3007   then show ?thesis using lf lf' by metis
  3008 qed
  3009 
  3010 text {* Moreover, a one-sided inverse is automatically linear. *}
  3011 
  3012 lemma left_inverse_linear: fixes f::"'a::euclidean_space => 'a::euclidean_space"
  3013   assumes lf: "linear f" and gf: "g o f = id"
  3014   shows "linear g"
  3015 proof-
  3016   from gf have fi: "inj f" apply (auto simp add: inj_on_def o_def id_def fun_eq_iff)
  3017     by metis
  3018   from linear_injective_isomorphism[OF lf fi]
  3019   obtain h:: "'a \<Rightarrow> 'a" where
  3020     h: "linear h" "\<forall>x. h (f x) = x" "\<forall>x. f (h x) = x" by blast
  3021   have "h = g" apply (rule ext) using gf h(2,3)
  3022     apply (simp add: o_def id_def fun_eq_iff)
  3023     by metis
  3024   with h(1) show ?thesis by blast
  3025 qed
  3026 
  3027 subsection {* Infinity norm *}
  3028 
  3029 definition "infnorm (x::'a::euclidean_space) = Sup {abs(x$$i) |i. i<DIM('a)}"
  3030 
  3031 lemma numseg_dimindex_nonempty: "\<exists>i. i \<in> (UNIV :: 'n set)"
  3032   by auto
  3033 
  3034 lemma infnorm_set_image:
  3035   "{abs((x::'a::euclidean_space)$$i) |i. i<DIM('a)} =
  3036   (\<lambda>i. abs(x$$i)) ` {..<DIM('a)}" by blast
  3037 
  3038 lemma infnorm_set_lemma:
  3039   shows "finite {abs((x::'a::euclidean_space)$$i) |i. i<DIM('a)}"
  3040   and "{abs(x$$i) |i. i<DIM('a::euclidean_space)} \<noteq> {}"
  3041   unfolding infnorm_set_image
  3042   by auto
  3043 
  3044 lemma infnorm_pos_le: "0 \<le> infnorm (x::'a::euclidean_space)"
  3045   unfolding infnorm_def
  3046   unfolding Sup_finite_ge_iff[ OF infnorm_set_lemma]
  3047   unfolding infnorm_set_image
  3048   by auto
  3049 
  3050 lemma infnorm_triangle: "infnorm ((x::'a::euclidean_space) + y) \<le> infnorm x + infnorm y"
  3051 proof-
  3052   have th: "\<And>x y (z::real). x - y <= z \<longleftrightarrow> x - z <= y" by arith
  3053   have th1: "\<And>S f. f ` S = { f i| i. i \<in> S}" by blast
  3054   have th2: "\<And>x (y::real). abs(x + y) - abs(x) <= abs(y)" by arith
  3055   have *:"\<And>i. i \<in> {..<DIM('a)} \<longleftrightarrow> i <DIM('a)" by auto
  3056   show ?thesis
  3057   unfolding infnorm_def unfolding  Sup_finite_le_iff[ OF infnorm_set_lemma[where 'a='a]]
  3058   apply (subst diff_le_eq[symmetric])
  3059   unfolding Sup_finite_ge_iff[ OF infnorm_set_lemma]
  3060   unfolding infnorm_set_image bex_simps
  3061   apply (subst th)
  3062   unfolding th1 *
  3063   unfolding Sup_finite_ge_iff[ OF infnorm_set_lemma[where 'a='a]]
  3064   unfolding infnorm_set_image ball_simps bex_simps
  3065   unfolding euclidean_simps by (metis th2)
  3066 qed
  3067 
  3068 lemma infnorm_eq_0: "infnorm x = 0 \<longleftrightarrow> (x::_::euclidean_space) = 0"
  3069 proof-
  3070   have "infnorm x <= 0 \<longleftrightarrow> x = 0"
  3071     unfolding infnorm_def
  3072     unfolding Sup_finite_le_iff[OF infnorm_set_lemma]
  3073     unfolding infnorm_set_image ball_simps
  3074     apply(subst (1) euclidean_eq) unfolding euclidean_component.zero
  3075     by auto
  3076   then show ?thesis using infnorm_pos_le[of x] by simp
  3077 qed
  3078 
  3079 lemma infnorm_0: "infnorm 0 = 0"
  3080   by (simp add: infnorm_eq_0)
  3081 
  3082 lemma infnorm_neg: "infnorm (- x) = infnorm x"
  3083   unfolding infnorm_def
  3084   apply (rule cong[of "Sup" "Sup"])
  3085   apply blast by(auto simp add: euclidean_simps)
  3086 
  3087 lemma infnorm_sub: "infnorm (x - y) = infnorm (y - x)"
  3088 proof-
  3089   have "y - x = - (x - y)" by simp
  3090   then show ?thesis  by (metis infnorm_neg)
  3091 qed
  3092 
  3093 lemma real_abs_sub_infnorm: "\<bar> infnorm x - infnorm y\<bar> \<le> infnorm (x - y)"
  3094 proof-
  3095   have th: "\<And>(nx::real) n ny. nx <= n + ny \<Longrightarrow> ny <= n + nx ==> \<bar>nx - ny\<bar> <= n"
  3096     by arith
  3097   from infnorm_triangle[of "x - y" " y"] infnorm_triangle[of "x - y" "-x"]
  3098   have ths: "infnorm x \<le> infnorm (x - y) + infnorm y"
  3099     "infnorm y \<le> infnorm (x - y) + infnorm x"
  3100     by (simp_all add: field_simps infnorm_neg diff_minus[symmetric])
  3101   from th[OF ths]  show ?thesis .
  3102 qed
  3103 
  3104 lemma real_abs_infnorm: " \<bar>infnorm x\<bar> = infnorm x"
  3105   using infnorm_pos_le[of x] by arith
  3106 
  3107 lemma component_le_infnorm:
  3108   shows "\<bar>x$$i\<bar> \<le> infnorm (x::'a::euclidean_space)"
  3109 proof(cases "i<DIM('a)")
  3110   case False thus ?thesis using infnorm_pos_le by auto
  3111 next case True
  3112   let ?U = "{..<DIM('a)}"
  3113   let ?S = "{\<bar>x$$i\<bar> |i. i<DIM('a)}"
  3114   have fS: "finite ?S" unfolding image_Collect[symmetric]
  3115     apply (rule finite_imageI) by simp
  3116   have S0: "?S \<noteq> {}" by blast
  3117   have th1: "\<And>S f. f ` S = { f i| i. i \<in> S}" by blast
  3118   show ?thesis unfolding infnorm_def  
  3119     apply(subst Sup_finite_ge_iff) using Sup_finite_in[OF fS S0]
  3120     using infnorm_set_image using True by auto
  3121 qed
  3122 
  3123 lemma infnorm_mul_lemma: "infnorm(a *\<^sub>R x) <= \<bar>a\<bar> * infnorm x"
  3124   apply (subst infnorm_def)
  3125   unfolding Sup_finite_le_iff[OF infnorm_set_lemma]
  3126   unfolding infnorm_set_image ball_simps euclidean_scaleR abs_mult
  3127   using component_le_infnorm[of x] by(auto intro: mult_mono) 
  3128 
  3129 lemma infnorm_mul: "infnorm(a *\<^sub>R x) = abs a * infnorm x"
  3130 proof-
  3131   {assume a0: "a = 0" hence ?thesis by (simp add: infnorm_0) }
  3132   moreover
  3133   {assume a0: "a \<noteq> 0"
  3134     from a0 have th: "(1/a) *\<^sub>R (a *\<^sub>R x) = x" by simp
  3135     from a0 have ap: "\<bar>a\<bar> > 0" by arith
  3136     from infnorm_mul_lemma[of "1/a" "a *\<^sub>R x"]
  3137     have "infnorm x \<le> 1/\<bar>a\<bar> * infnorm (a*\<^sub>R x)"
  3138       unfolding th by simp
  3139     with ap have "\<bar>a\<bar> * infnorm x \<le> \<bar>a\<bar> * (1/\<bar>a\<bar> * infnorm (a *\<^sub>R x))" by (simp add: field_simps)
  3140     then have "\<bar>a\<bar> * infnorm x \<le> infnorm (a*\<^sub>R x)"
  3141       using ap by (simp add: field_simps)
  3142     with infnorm_mul_lemma[of a x] have ?thesis by arith }
  3143   ultimately show ?thesis by blast
  3144 qed
  3145 
  3146 lemma infnorm_pos_lt: "infnorm x > 0 \<longleftrightarrow> x \<noteq> 0"
  3147   using infnorm_pos_le[of x] infnorm_eq_0[of x] by arith
  3148 
  3149 text {* Prove that it differs only up to a bound from Euclidean norm. *}
  3150 
  3151 lemma infnorm_le_norm: "infnorm x \<le> norm x"
  3152   unfolding infnorm_def Sup_finite_le_iff[OF infnorm_set_lemma]
  3153   unfolding infnorm_set_image  ball_simps
  3154   by (metis component_le_norm)
  3155 
  3156 lemma card_enum: "card {1 .. n} = n" by auto
  3157 
  3158 lemma norm_le_infnorm: "norm(x) <= sqrt(real DIM('a)) * infnorm(x::'a::euclidean_space)"
  3159 proof-
  3160   let ?d = "DIM('a)"
  3161   have "real ?d \<ge> 0" by simp
  3162   hence d2: "(sqrt (real ?d))^2 = real ?d"
  3163     by (auto intro: real_sqrt_pow2)
  3164   have th: "sqrt (real ?d) * infnorm x \<ge> 0"
  3165     by (simp add: zero_le_mult_iff infnorm_pos_le)
  3166   have th1: "x \<bullet> x \<le> (sqrt (real ?d) * infnorm x)^2"
  3167     unfolding power_mult_distrib d2
  3168     unfolding real_of_nat_def apply(subst euclidean_inner)
  3169     apply (subst power2_abs[symmetric])
  3170     apply(rule order_trans[OF setsum_bounded[where K="\<bar>infnorm x\<bar>\<twosuperior>"]])
  3171     apply(auto simp add: power2_eq_square[symmetric])
  3172     apply (subst power2_abs[symmetric])
  3173     apply (rule power_mono)
  3174     unfolding infnorm_def  Sup_finite_ge_iff[OF infnorm_set_lemma]
  3175     unfolding infnorm_set_image bex_simps apply(rule_tac x=i in bexI) by auto
  3176   from real_le_lsqrt[OF inner_ge_zero th th1]
  3177   show ?thesis unfolding norm_eq_sqrt_inner id_def .
  3178 qed
  3179 
  3180 text {* Equality in Cauchy-Schwarz and triangle inequalities. *}
  3181 
  3182 lemma norm_cauchy_schwarz_eq: "x \<bullet> y = norm x * norm y \<longleftrightarrow> norm x *\<^sub>R y = norm y *\<^sub>R x" (is "?lhs \<longleftrightarrow> ?rhs")
  3183 proof-
  3184   {assume h: "x = 0"
  3185     hence ?thesis by simp}
  3186   moreover
  3187   {assume h: "y = 0"
  3188     hence ?thesis by simp}
  3189   moreover
  3190   {assume x: "x \<noteq> 0" and y: "y \<noteq> 0"
  3191     from inner_eq_zero_iff[of "norm y *\<^sub>R x - norm x *\<^sub>R y"]
  3192     have "?rhs \<longleftrightarrow> (norm y * (norm y * norm x * norm x - norm x * (x \<bullet> y)) - norm x * (norm y * (y \<bullet> x) - norm x * norm y * norm y) =  0)"
  3193       using x y
  3194       unfolding inner_simps
  3195       unfolding power2_norm_eq_inner[symmetric] power2_eq_square diff_eq_0_iff_eq apply (simp add: inner_commute)
  3196       apply (simp add: field_simps) by metis
  3197     also have "\<dots> \<longleftrightarrow> (2 * norm x * norm y * (norm x * norm y - x \<bullet> y) = 0)" using x y
  3198       by (simp add: field_simps inner_commute)
  3199     also have "\<dots> \<longleftrightarrow> ?lhs" using x y
  3200       apply simp
  3201       by metis
  3202     finally have ?thesis by blast}
  3203   ultimately show ?thesis by blast
  3204 qed
  3205 
  3206 lemma norm_cauchy_schwarz_abs_eq:
  3207   shows "abs(x \<bullet> y) = norm x * norm y \<longleftrightarrow>
  3208                 norm x *\<^sub>R y = norm y *\<^sub>R x \<or> norm(x) *\<^sub>R y = - norm y *\<^sub>R x" (is "?lhs \<longleftrightarrow> ?rhs")
  3209 proof-
  3210   have th: "\<And>(x::real) a. a \<ge> 0 \<Longrightarrow> abs x = a \<longleftrightarrow> x = a \<or> x = - a" by arith
  3211   have "?rhs \<longleftrightarrow> norm x *\<^sub>R y = norm y *\<^sub>R x \<or> norm (- x) *\<^sub>R y = norm y *\<^sub>R (- x)"
  3212     by simp
  3213   also have "\<dots> \<longleftrightarrow>(x \<bullet> y = norm x * norm y \<or>
  3214      (-x) \<bullet> y = norm x * norm y)"
  3215     unfolding norm_cauchy_schwarz_eq[symmetric]
  3216     unfolding norm_minus_cancel norm_scaleR ..
  3217   also have "\<dots> \<longleftrightarrow> ?lhs"
  3218     unfolding th[OF mult_nonneg_nonneg, OF norm_ge_zero[of x] norm_ge_zero[of y]] inner_simps by auto
  3219   finally show ?thesis ..
  3220 qed
  3221 
  3222 lemma norm_triangle_eq:
  3223   fixes x y :: "'a::real_inner"
  3224   shows "norm(x + y) = norm x + norm y \<longleftrightarrow> norm x *\<^sub>R y = norm y *\<^sub>R x"
  3225 proof-
  3226   {assume x: "x =0 \<or> y =0"
  3227     hence ?thesis by (cases "x=0", simp_all)}
  3228   moreover
  3229   {assume x: "x \<noteq> 0" and y: "y \<noteq> 0"
  3230     hence "norm x \<noteq> 0" "norm y \<noteq> 0"
  3231       by simp_all
  3232     hence n: "norm x > 0" "norm y > 0"
  3233       using norm_ge_zero[of x] norm_ge_zero[of y]
  3234       by arith+
  3235     have th: "\<And>(a::real) b c. a + b + c \<noteq> 0 ==> (a = b + c \<longleftrightarrow> a^2 = (b + c)^2)" by algebra
  3236     have "norm(x + y) = norm x + norm y \<longleftrightarrow> norm(x + y)^ 2 = (norm x + norm y) ^2"
  3237       apply (rule th) using n norm_ge_zero[of "x + y"]
  3238       by arith
  3239     also have "\<dots> \<longleftrightarrow> norm x *\<^sub>R y = norm y *\<^sub>R x"
  3240       unfolding norm_cauchy_schwarz_eq[symmetric]
  3241       unfolding power2_norm_eq_inner inner_simps
  3242       by (simp add: power2_norm_eq_inner[symmetric] power2_eq_square inner_commute field_simps)
  3243     finally have ?thesis .}
  3244   ultimately show ?thesis by blast
  3245 qed
  3246 
  3247 subsection {* Collinearity *}
  3248 
  3249 definition
  3250   collinear :: "'a::real_vector set \<Rightarrow> bool" where
  3251   "collinear S \<longleftrightarrow> (\<exists>u. \<forall>x \<in> S. \<forall> y \<in> S. \<exists>c. x - y = c *\<^sub>R u)"
  3252 
  3253 lemma collinear_empty:  "collinear {}" by (simp add: collinear_def)
  3254 
  3255 lemma collinear_sing: "collinear {x}"
  3256   by (simp add: collinear_def)
  3257 
  3258 lemma collinear_2: "collinear {x, y}"
  3259   apply (simp add: collinear_def)
  3260   apply (rule exI[where x="x - y"])
  3261   apply auto
  3262   apply (rule exI[where x=1], simp)
  3263   apply (rule exI[where x="- 1"], simp)
  3264   done
  3265 
  3266 lemma collinear_lemma: "collinear {0,x,y} \<longleftrightarrow> x = 0 \<or> y = 0 \<or> (\<exists>c. y = c *\<^sub>R x)" (is "?lhs \<longleftrightarrow> ?rhs")
  3267 proof-
  3268   {assume "x=0 \<or> y = 0" hence ?thesis
  3269       by (cases "x = 0", simp_all add: collinear_2 insert_commute)}
  3270   moreover
  3271   {assume x: "x \<noteq> 0" and y: "y \<noteq> 0"
  3272     {assume h: "?lhs"
  3273       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" unfolding collinear_def by blast
  3274       from u[rule_format, of x 0] u[rule_format, of y 0]
  3275       obtain cx and cy where
  3276         cx: "x = cx *\<^sub>R u" and cy: "y = cy *\<^sub>R u"
  3277         by auto
  3278       from cx x have cx0: "cx \<noteq> 0" by auto
  3279       from cy y have cy0: "cy \<noteq> 0" by auto
  3280       let ?d = "cy / cx"
  3281       from cx cy cx0 have "y = ?d *\<^sub>R x"
  3282         by simp
  3283       hence ?rhs using x y by blast}
  3284     moreover
  3285     {assume h: "?rhs"
  3286       then obtain c where c: "y = c *\<^sub>R x" using x y by blast
  3287       have ?lhs unfolding collinear_def c
  3288         apply (rule exI[where x=x])
  3289         apply auto
  3290         apply (rule exI[where x="- 1"], simp)
  3291         apply (rule exI[where x= "-c"], simp)
  3292         apply (rule exI[where x=1], simp)
  3293         apply (rule exI[where x="1 - c"], simp add: scaleR_left_diff_distrib)
  3294         apply (rule exI[where x="c - 1"], simp add: scaleR_left_diff_distrib)
  3295         done}
  3296     ultimately have ?thesis by blast}
  3297   ultimately show ?thesis by blast
  3298 qed
  3299 
  3300 lemma norm_cauchy_schwarz_equal:
  3301   shows "abs(x \<bullet> y) = norm x * norm y \<longleftrightarrow> collinear {0,x,y}"
  3302 unfolding norm_cauchy_schwarz_abs_eq
  3303 apply (cases "x=0", simp_all add: collinear_2)
  3304 apply (cases "y=0", simp_all add: collinear_2 insert_commute)
  3305 unfolding collinear_lemma
  3306 apply simp
  3307 apply (subgoal_tac "norm x \<noteq> 0")
  3308 apply (subgoal_tac "norm y \<noteq> 0")
  3309 apply (rule iffI)
  3310 apply (cases "norm x *\<^sub>R y = norm y *\<^sub>R x")
  3311 apply (rule exI[where x="(1/norm x) * norm y"])
  3312 apply (drule sym)
  3313 unfolding scaleR_scaleR[symmetric]
  3314 apply (simp add: field_simps)
  3315 apply (rule exI[where x="(1/norm x) * - norm y"])
  3316 apply clarify
  3317 apply (drule sym)
  3318 unfolding scaleR_scaleR[symmetric]
  3319 apply (simp add: field_simps)
  3320 apply (erule exE)
  3321 apply (erule ssubst)
  3322 unfolding scaleR_scaleR
  3323 unfolding norm_scaleR
  3324 apply (subgoal_tac "norm x * c = \<bar>c\<bar> * norm x \<or> norm x * c = - \<bar>c\<bar> * norm x")
  3325 apply (case_tac "c <= 0", simp add: field_simps)
  3326 apply (simp add: field_simps)
  3327 apply (case_tac "c <= 0", simp add: field_simps)
  3328 apply (simp add: field_simps)
  3329 apply simp
  3330 apply simp
  3331 done
  3332 
  3333 subsection "Instantiate @{typ real} and @{typ complex} as typeclass @{text ordered_euclidean_space}."
  3334 
  3335 instantiation real :: real_basis_with_inner
  3336 begin
  3337 definition [simp]: "basis i = (if i = 0 then (1::real) else 0)"
  3338 
  3339 lemma basis_real_range: "basis ` {..<1} = {1::real}" by auto
  3340 
  3341 instance proof
  3342   let ?b = "basis::nat \<Rightarrow> real"
  3343 
  3344   from basis_real_range have "independent (?b ` {..<1})" by auto
  3345   thus "\<exists>d>0. ?b ` {d..} = {0} \<and> independent (?b ` {..<d}) \<and> inj_on ?b {..<d}"
  3346     by (auto intro!: exI[of _ 1] inj_onI)
  3347 
  3348   { fix x::real
  3349     have "x \<in> span (range ?b)"
  3350       using span_mul[of 1 "range ?b" x] span_clauses(1)[of 1 "range ?b"]
  3351       by auto }
  3352   thus "span (range ?b) = UNIV" by auto
  3353 qed
  3354 end
  3355 
  3356 lemma DIM_real[simp]: "DIM(real) = 1"
  3357   by (rule dimension_eq) (auto simp: basis_real_def)
  3358 
  3359 instance real::ordered_euclidean_space proof qed(auto simp add:euclidean_component_def)
  3360 
  3361 lemma Eucl_real_simps[simp]:
  3362   "(x::real) $$ 0 = x"
  3363   "(\<chi>\<chi> i. f i) = ((f 0)::real)"
  3364   "\<And>i. i > 0 \<Longrightarrow> x $$ i = 0"
  3365   defer apply(subst euclidean_eq) apply safe
  3366   unfolding euclidean_lambda_beta'
  3367   unfolding euclidean_component_def by auto
  3368 
  3369 instantiation complex :: real_basis_with_inner
  3370 begin
  3371 definition "basis i = (if i = 0 then 1 else if i = 1 then ii else 0)"
  3372 
  3373 lemma complex_basis[simp]:"basis 0 = (1::complex)" "basis 1 = ii" "basis (Suc 0) = ii"
  3374   unfolding basis_complex_def by auto
  3375 
  3376 instance
  3377 proof
  3378   let ?b = "basis::nat \<Rightarrow> complex"
  3379   have [simp]: "(range ?b) = {0, basis 0, basis 1}"
  3380     by (auto simp: basis_complex_def split: split_if_asm)
  3381   { fix z::complex
  3382     have "z \<in> span (range ?b)"
  3383       by (auto simp: span_finite complex_equality
  3384         intro!: exI[of _ "\<lambda>i. if i = 1 then Re z else if i = ii then Im z else 0"]) }
  3385   thus "span (range ?b) = UNIV" by auto
  3386 
  3387   have "{..<2} = {0, 1::nat}" by auto
  3388   hence *: "?b ` {..<2} = {1, ii}"
  3389     by (auto simp add: basis_complex_def)
  3390   moreover have "1 \<notin> span {\<i>}"
  3391     by (simp add: span_finite complex_equality complex_scaleR_def)
  3392   hence "independent (?b ` {..<2})"
  3393     by (simp add: * basis_complex_def independent_empty independent_insert)
  3394   ultimately show "\<exists>d>0. ?b ` {d..} = {0} \<and> independent (?b ` {..<d}) \<and> inj_on ?b {..<d}"
  3395     by (auto intro!: exI[of _ 2] inj_onI simp: basis_complex_def split: split_if_asm)
  3396 qed
  3397 end
  3398 
  3399 lemma DIM_complex[simp]: "DIM(complex) = 2"
  3400   by (rule dimension_eq) (auto simp: basis_complex_def)
  3401 
  3402 instance complex :: euclidean_space
  3403   proof qed (auto simp add: basis_complex_def inner_complex_def)
  3404 
  3405 section {* Products Spaces *}
  3406 
  3407 instantiation prod :: (real_basis, real_basis) real_basis
  3408 begin
  3409 
  3410 definition "basis i = (if i < DIM('a) then (basis i, 0) else (0, basis (i - DIM('a))))"
  3411 
  3412 instance
  3413 proof
  3414   let ?b = "basis :: nat \<Rightarrow> 'a \<times> 'b"
  3415   let ?b_a = "basis :: nat \<Rightarrow> 'a"
  3416   let ?b_b = "basis :: nat \<Rightarrow> 'b"
  3417 
  3418   note image_range =
  3419     image_add_atLeastLessThan[symmetric, of 0 "DIM('a)" "DIM('b)", simplified]
  3420 
  3421   have split_range:
  3422     "{..<DIM('b) + DIM('a)} = {..<DIM('a)} \<union> {DIM('a)..<DIM('b) + DIM('a)}"
  3423     by auto
  3424   have *: "?b ` {DIM('a)..<DIM('b) + DIM('a)} = {0} \<times> (?b_b ` {..<DIM('b)})"
  3425     "?b ` {..<DIM('a)} = (?b_a ` {..<DIM('a)}) \<times> {0}"
  3426     unfolding image_range image_image basis_prod_def_raw range_basis
  3427     by (auto simp: zero_prod_def basis_eq_0_iff)
  3428   hence b_split:
  3429     "?b ` {..<DIM('b) + DIM('a)} = (?b_a ` {..<DIM('a)}) \<times> {0} \<union> {0} \<times> (?b_b ` {..<DIM('b)})" (is "_ = ?prod")
  3430     by (subst split_range) (simp add: image_Un)
  3431 
  3432   have b_0: "?b ` {DIM('b) + DIM('a)..} = {0}" unfolding basis_prod_def_raw
  3433     by (auto simp: zero_prod_def image_iff basis_eq_0_iff elim!: ballE[of _ _ "DIM('a) + DIM('b)"])
  3434 
  3435   have split_UNIV:
  3436     "UNIV = {..<DIM('b) + DIM('a)} \<union> {DIM('b)+DIM('a)..}"
  3437     by auto
  3438 
  3439   have range_b: "range ?b = ?prod \<union> {0}"
  3440     by (subst split_UNIV) (simp add: image_Un b_split b_0)
  3441 
  3442   have prod: "\<And>f A B. setsum f (A \<times> B) = (\<Sum>a\<in>A. \<Sum>b\<in>B. f (a, b))"
  3443     by (simp add: setsum_cartesian_product)
  3444 
  3445   show "span (range ?b) = UNIV"
  3446     unfolding span_explicit range_b
  3447   proof safe
  3448     fix a::'a and b::'b
  3449     from in_span_basis[of a] in_span_basis[of b]
  3450     obtain Sa ua Sb ub where span:
  3451         "finite Sa" "Sa \<subseteq> basis ` {..<DIM('a)}" "a = (\<Sum>v\<in>Sa. ua v *\<^sub>R v)"
  3452         "finite Sb" "Sb \<subseteq> basis ` {..<DIM('b)}" "b = (\<Sum>v\<in>Sb. ub v *\<^sub>R v)"
  3453       unfolding span_explicit by auto
  3454 
  3455     let ?S = "((Sa - {0}) \<times> {0} \<union> {0} \<times> (Sb - {0}))"
  3456     have *:
  3457       "?S \<inter> {v. fst v = 0} \<inter> {v. snd v = 0} = {}"
  3458       "?S \<inter> - {v. fst v = 0} \<inter> {v. snd v = 0} = (Sa - {0}) \<times> {0}"
  3459       "?S \<inter> {v. fst v = 0} \<inter> - {v. snd v = 0} = {0} \<times> (Sb - {0})"
  3460       by (auto simp: zero_prod_def)
  3461     show "\<exists>S u. finite S \<and> S \<subseteq> ?prod \<union> {0} \<and> (\<Sum>v\<in>S. u v *\<^sub>R v) = (a, b)"
  3462       apply (rule exI[of _ ?S])
  3463       apply (rule exI[of _ "\<lambda>(v, w). (if w = 0 then ua v else 0) + (if v = 0 then ub w else 0)"])
  3464       using span
  3465       apply (simp add: prod_case_unfold setsum_addf if_distrib cond_application_beta setsum_cases prod *)
  3466       by (auto simp add: setsum_prod intro!: setsum_mono_zero_cong_left)
  3467   qed simp
  3468 
  3469   show "\<exists>d>0. ?b ` {d..} = {0} \<and> independent (?b ` {..<d}) \<and> inj_on ?b {..<d}"
  3470     apply (rule exI[of _ "DIM('b) + DIM('a)"]) unfolding b_0
  3471   proof (safe intro!: DIM_positive del: notI)
  3472     show inj_on: "inj_on ?b {..<DIM('b) + DIM('a)}" unfolding split_range
  3473       using inj_on_iff[OF basis_inj[where 'a='a]] inj_on_iff[OF basis_inj[where 'a='b]]
  3474       by (auto intro!: inj_onI simp: basis_prod_def basis_eq_0_iff)
  3475 
  3476     show "independent (?b ` {..<DIM('b) + DIM('a)})"
  3477       unfolding independent_eq_inj_on[OF inj_on]
  3478     proof safe
  3479       fix i u assume i_upper: "i < DIM('b) + DIM('a)" and
  3480           "(\<Sum>j\<in>{..<DIM('b) + DIM('a)} - {i}. u (?b j) *\<^sub>R ?b j) = ?b i" (is "?SUM = _")
  3481       let ?left = "{..<DIM('a)}" and ?right = "{DIM('a)..<DIM('b) + DIM('a)}"
  3482       show False
  3483       proof cases
  3484         assume "i < DIM('a)"
  3485         hence "(basis i, 0) = ?SUM" unfolding `?SUM = ?b i` unfolding basis_prod_def by auto
  3486         also have "\<dots> = (\<Sum>j\<in>?left - {i}. u (?b j) *\<^sub>R ?b j) +
  3487           (\<Sum>j\<in>?right. u (?b j) *\<^sub>R ?b j)"
  3488           using `i < DIM('a)` by (subst setsum_Un_disjoint[symmetric]) (auto intro!: setsum_cong)
  3489         also have "\<dots> =  (\<Sum>j\<in>?left - {i}. u (?b_a j, 0) *\<^sub>R (?b_a j, 0)) +
  3490           (\<Sum>j\<in>?right. u (0, ?b_b (j-DIM('a))) *\<^sub>R (0, ?b_b (j-DIM('a))))"
  3491           unfolding basis_prod_def by auto
  3492         finally have "basis i = (\<Sum>j\<in>?left - {i}. u (?b_a j, 0) *\<^sub>R ?b_a j)"
  3493           by (simp add: setsum_prod)
  3494         moreover
  3495         note independent_basis[where 'a='a, unfolded independent_eq_inj_on[OF basis_inj]]
  3496         note this[rule_format, of i "\<lambda>v. u (v, 0)"]
  3497         ultimately show False using `i < DIM('a)` by auto
  3498       next
  3499         let ?i = "i - DIM('a)"
  3500         assume not: "\<not> i < DIM('a)" hence "DIM('a) \<le> i" by auto
  3501         hence "?i < DIM('b)" using `i < DIM('b) + DIM('a)` by auto
  3502 
  3503         have inj_on: "inj_on (\<lambda>j. j - DIM('a)) {DIM('a)..<DIM('b) + DIM('a)}"
  3504           by (auto intro!: inj_onI)
  3505         with i_upper not have *: "{..<DIM('b)} - {?i} = (\<lambda>j. j-DIM('a))`(?right - {i})"
  3506           by (auto simp: inj_on_image_set_diff image_minus_const_atLeastLessThan_nat)
  3507 
  3508         have "(0, basis ?i) = ?SUM" unfolding `?SUM = ?b i`
  3509           unfolding basis_prod_def using not `?i < DIM('b)` by auto
  3510         also have "\<dots> = (\<Sum>j\<in>?left. u (?b j) *\<^sub>R ?b j) +
  3511           (\<Sum>j\<in>?right - {i}. u (?b j) *\<^sub>R ?b j)"
  3512           using not by (subst setsum_Un_disjoint[symmetric]) (auto intro!: setsum_cong)
  3513         also have "\<dots> =  (\<Sum>j\<in>?left. u (?b_a j, 0) *\<^sub>R (?b_a j, 0)) +
  3514           (\<Sum>j\<in>?right - {i}. u (0, ?b_b (j-DIM('a))) *\<^sub>R (0, ?b_b (j-DIM('a))))"
  3515           unfolding basis_prod_def by auto
  3516         finally have "basis ?i = (\<Sum>j\<in>{..<DIM('b)} - {?i}. u (0, ?b_b j) *\<^sub>R ?b_b j)"
  3517           unfolding *
  3518           by (subst setsum_reindex[OF inj_on[THEN subset_inj_on]])
  3519              (auto simp: setsum_prod)
  3520         moreover
  3521         note independent_basis[where 'a='b, unfolded independent_eq_inj_on[OF basis_inj]]
  3522         note this[rule_format, of ?i "\<lambda>v. u (0, v)"]
  3523         ultimately show False using `?i < DIM('b)` by auto
  3524       qed
  3525     qed
  3526   qed
  3527 qed
  3528 end
  3529 
  3530 lemma DIM_prod[simp]: "DIM('a \<times> 'b) = DIM('b::real_basis) + DIM('a::real_basis)"
  3531   by (rule dimension_eq) (auto simp: basis_prod_def zero_prod_def basis_eq_0_iff)
  3532 
  3533 instance prod :: (euclidean_space, euclidean_space) euclidean_space
  3534 proof (default, safe)
  3535   let ?b = "basis :: nat \<Rightarrow> 'a \<times> 'b"
  3536   fix i j assume "i < DIM('a \<times> 'b)" "j < DIM('a \<times> 'b)"
  3537   thus "?b i \<bullet> ?b j = (if i = j then 1 else 0)"
  3538     unfolding basis_prod_def by (auto simp: dot_basis)
  3539 qed
  3540 
  3541 instantiation prod :: (ordered_euclidean_space, ordered_euclidean_space) ordered_euclidean_space
  3542 begin
  3543 
  3544 definition "x \<le> (y::('a\<times>'b)) \<longleftrightarrow> (\<forall>i<DIM('a\<times>'b). x $$ i \<le> y $$ i)"
  3545 definition "x < (y::('a\<times>'b)) \<longleftrightarrow> (\<forall>i<DIM('a\<times>'b). x $$ i < y $$ i)"
  3546 
  3547 instance proof qed (auto simp: less_prod_def less_eq_prod_def)
  3548 end
  3549 
  3550 
  3551 end