# Theory Linear_Algebra

theory Linear_Algebra
imports Euclidean_Space Infinite_Set
```(*  Title:      HOL/Analysis/Linear_Algebra.thy
Author:     Amine Chaieb, University of Cambridge
*)

section ‹Elementary linear algebra on Euclidean spaces›

theory Linear_Algebra
imports
Euclidean_Space
"HOL-Library.Infinite_Set"
begin

lemma linear_simps:
assumes "bounded_linear f"
shows
"f (a + b) = f a + f b"
"f (a - b) = f a - f b"
"f 0 = 0"
"f (- a) = - f a"
"f (s *⇩R v) = s *⇩R (f v)"
proof -
interpret f: bounded_linear f by fact
show "f (a + b) = f a + f b" by (rule f.add)
show "f (a - b) = f a - f b" by (rule f.diff)
show "f 0 = 0" by (rule f.zero)
show "f (- a) = - f a" by (rule f.minus)
show "f (s *⇩R v) = s *⇩R (f v)" by (rule f.scaleR)
qed

lemma bounded_linearI:
assumes "⋀x y. f (x + y) = f x + f y"
and "⋀r x. f (r *⇩R x) = r *⇩R f x"
and "⋀x. norm (f x) ≤ norm x * K"
shows "bounded_linear f"
using assms by (rule bounded_linear_intro) (* FIXME: duplicate *)

subsection ‹A generic notion of "hull" (convex, affine, conic hull and closure).›

definition hull :: "('a set ⇒ bool) ⇒ 'a set ⇒ 'a set"  (infixl "hull" 75)
where "S hull s = ⋂{t. S t ∧ s ⊆ t}"

lemma hull_same: "S s ⟹ S hull s = s"
unfolding hull_def by auto

lemma hull_in: "(⋀T. Ball T S ⟹ S (⋂T)) ⟹ S (S hull s)"
unfolding hull_def Ball_def by auto

lemma hull_eq: "(⋀T. Ball T S ⟹ S (⋂T)) ⟹ (S hull s) = s ⟷ S s"
using hull_same[of S s] hull_in[of S s] by metis

lemma hull_hull [simp]: "S hull (S hull s) = S hull s"
unfolding hull_def by blast

lemma hull_subset[intro]: "s ⊆ (S hull s)"
unfolding hull_def by blast

lemma hull_mono: "s ⊆ t ⟹ (S hull s) ⊆ (S hull t)"
unfolding hull_def by blast

lemma hull_antimono: "∀x. S x ⟶ T x ⟹ (T hull s) ⊆ (S hull s)"
unfolding hull_def by blast

lemma hull_minimal: "s ⊆ t ⟹ S t ⟹ (S hull s) ⊆ t"
unfolding hull_def by blast

lemma subset_hull: "S t ⟹ S hull s ⊆ t ⟷ s ⊆ t"
unfolding hull_def by blast

lemma hull_UNIV [simp]: "S hull UNIV = UNIV"
unfolding hull_def by auto

lemma hull_unique: "s ⊆ t ⟹ S t ⟹ (⋀t'. s ⊆ t' ⟹ S t' ⟹ t ⊆ t') ⟹ (S hull s = t)"
unfolding hull_def by auto

lemma hull_induct: "(⋀x. x∈ S ⟹ P x) ⟹ Q {x. P x} ⟹ ∀x∈ Q hull S. P x"
using hull_minimal[of S "{x. P x}" Q]

lemma hull_inc: "x ∈ S ⟹ x ∈ P hull S"
by (metis hull_subset subset_eq)

lemma hull_Un_subset: "(S hull s) ∪ (S hull t) ⊆ (S hull (s ∪ t))"
unfolding Un_subset_iff by (metis hull_mono Un_upper1 Un_upper2)

lemma hull_Un:
assumes T: "⋀T. Ball T S ⟹ S (⋂T)"
shows "S hull (s ∪ t) = S hull (S hull s ∪ S hull t)"
apply (rule equalityI)
apply (meson hull_mono hull_subset sup.mono)
by (metis hull_Un_subset hull_hull hull_mono)

lemma hull_Un_left: "P hull (S ∪ T) = P hull (P hull S ∪ T)"
apply (rule equalityI)
apply (simp add: Un_commute hull_mono hull_subset sup.coboundedI2)
by (metis Un_subset_iff hull_hull hull_mono hull_subset)

lemma hull_Un_right: "P hull (S ∪ T) = P hull (S ∪ P hull T)"
by (metis hull_Un_left sup.commute)

lemma hull_insert:
"P hull (insert a S) = P hull (insert a (P hull S))"
by (metis hull_Un_right insert_is_Un)

lemma hull_redundant_eq: "a ∈ (S hull s) ⟷ S hull (insert a s) = S hull s"
unfolding hull_def by blast

lemma hull_redundant: "a ∈ (S hull s) ⟹ S hull (insert a s) = S hull s"
by (metis hull_redundant_eq)

subsection ‹Linear functions.›

lemma linear_iff:
"linear f ⟷ (∀x y. f (x + y) = f x + f y) ∧ (∀c x. f (c *⇩R x) = c *⇩R f x)"
(is "linear f ⟷ ?rhs")
proof
assume "linear f"
then interpret f: linear f .
next
assume "?rhs"
then show "linear f" by unfold_locales simp_all
qed

lemma linear_compose_cmul: "linear f ⟹ linear (λx. c *⇩R f x)"

lemma linear_compose_scaleR: "linear f ⟹ linear (λx. f x *⇩R c)"

lemma linear_compose_neg: "linear f ⟹ linear (λx. - f x)"

lemma linear_compose_add: "linear f ⟹ linear g ⟹ linear (λx. f x + g x)"

lemma linear_compose_sub: "linear f ⟹ linear g ⟹ linear (λx. f x - g x)"

lemma linear_compose: "linear f ⟹ linear g ⟹ linear (g ∘ f)"

lemma linear_id: "linear id"

lemma linear_zero: "linear (λx. 0)"

lemma linear_uminus: "linear uminus"

lemma linear_compose_sum:
assumes lS: "∀a ∈ S. linear (f a)"
shows "linear (λx. sum (λa. f a x) S)"
proof (cases "finite S")
case True
then show ?thesis
next
case False
then show ?thesis
qed

lemma linear_0: "linear f ⟹ f 0 = 0"
unfolding linear_iff
apply clarsimp
apply (erule allE[where x="0::'a"])
apply simp
done

lemma linear_cmul: "linear f ⟹ f (c *⇩R x) = c *⇩R f x"
by (rule linear.scaleR)

lemma linear_neg: "linear f ⟹ f (- x) = - f x"
using linear_cmul [where c="-1"] by simp

lemma linear_add: "linear f ⟹ f (x + y) = f x + f y"
by (metis linear_iff)

lemma linear_diff: "linear f ⟹ f (x - y) = f x - f y"

lemma linear_sum:
assumes f: "linear f"
shows "f (sum g S) = sum (f ∘ g) S"
proof (cases "finite S")
case True
then show ?thesis
next
case False
then show ?thesis
by (simp add: linear_0 [OF f])
qed

lemma linear_sum_mul:
assumes lin: "linear f"
shows "f (sum (λi. c i *⇩R v i) S) = sum (λi. c i *⇩R f (v i)) S"
using linear_sum[OF lin, of "λi. c i *⇩R v i" , unfolded o_def] linear_cmul[OF lin]
by simp

lemma linear_injective_0:
assumes lin: "linear f"
shows "inj f ⟷ (∀x. f x = 0 ⟶ x = 0)"
proof -
have "inj f ⟷ (∀ x y. f x = f y ⟶ x = y)"
also have "… ⟷ (∀ x y. f x - f y = 0 ⟶ x - y = 0)"
by simp
also have "… ⟷ (∀ x y. f (x - y) = 0 ⟶ x - y = 0)"
also have "… ⟷ (∀ x. f x = 0 ⟶ x = 0)"
by auto
finally show ?thesis .
qed

lemma linear_scaleR  [simp]: "linear (λx. scaleR c x)"

lemma linear_scaleR_left [simp]: "linear (λr. scaleR r x)"

lemma injective_scaleR: "c ≠ 0 ⟹ inj (λx::'a::real_vector. scaleR c x)"

assumes "linear f"
shows "f (a *⇩R x + b *⇩R y) = a *⇩R f x +  b *⇩R f y"
using linear_add[of f] linear_cmul[of f] assms by simp

subsection ‹Subspaces of vector spaces›

definition (in real_vector) subspace :: "'a set ⇒ bool"
where "subspace S ⟷ 0 ∈ S ∧ (∀x ∈ S. ∀y ∈ S. x + y ∈ S) ∧ (∀c. ∀x ∈ S. c *⇩R x ∈ S)"

definition (in real_vector) "span S = (subspace hull S)"
definition (in real_vector) "dependent S ⟷ (∃a ∈ S. a ∈ span (S - {a}))"
abbreviation (in real_vector) "independent s ≡ ¬ dependent s"

text ‹Closure properties of subspaces.›

lemma subspace_UNIV[simp]: "subspace UNIV"

lemma (in real_vector) subspace_0: "subspace S ⟹ 0 ∈ S"
by (metis subspace_def)

lemma (in real_vector) subspace_add: "subspace S ⟹ x ∈ S ⟹ y ∈ S ⟹ x + y ∈ S"
by (metis subspace_def)

lemma (in real_vector) subspace_mul: "subspace S ⟹ x ∈ S ⟹ c *⇩R x ∈ S"
by (metis subspace_def)

lemma subspace_neg: "subspace S ⟹ x ∈ S ⟹ - x ∈ S"
by (metis scaleR_minus1_left subspace_mul)

lemma subspace_diff: "subspace S ⟹ x ∈ S ⟹ y ∈ S ⟹ x - y ∈ S"

lemma (in real_vector) subspace_sum:
assumes sA: "subspace A"
and f: "⋀x. x ∈ B ⟹ f x ∈ A"
shows "sum f B ∈ A"
proof (cases "finite B")
case True
then show ?thesis
using f by induct (simp_all add: subspace_0 [OF sA] subspace_add [OF sA])
qed (simp add: subspace_0 [OF sA])

lemma subspace_trivial [iff]: "subspace {0}"

lemma (in real_vector) subspace_inter: "subspace A ⟹ subspace B ⟹ subspace (A ∩ B)"

lemma subspace_Times: "subspace A ⟹ subspace B ⟹ subspace (A × B)"
unfolding subspace_def zero_prod_def by simp

lemma subspace_sums: "⟦subspace S; subspace T⟧ ⟹ subspace {x + y|x y. x ∈ S ∧ y ∈ T}"
apply (intro conjI impI allI)
apply clarify

subsection ‹Properties of span›

lemma (in real_vector) span_mono: "A ⊆ B ⟹ span A ⊆ span B"
by (metis span_def hull_mono)

lemma (in real_vector) subspace_span [iff]: "subspace (span S)"
unfolding span_def
apply (rule hull_in)
apply (simp only: subspace_def Inter_iff Int_iff subset_eq)
apply auto
done

lemma (in real_vector) span_clauses:
"a ∈ S ⟹ a ∈ span S"
"0 ∈ span S"
"x∈ span S ⟹ y ∈ span S ⟹ x + y ∈ span S"
"x ∈ span S ⟹ c *⇩R x ∈ span S"
by (metis span_def hull_subset subset_eq) (metis subspace_span subspace_def)+

lemma span_unique:
"S ⊆ T ⟹ subspace T ⟹ (⋀T'. S ⊆ T' ⟹ subspace T' ⟹ T ⊆ T') ⟹ span S = T"
unfolding span_def by (rule hull_unique)

lemma span_minimal: "S ⊆ T ⟹ subspace T ⟹ span S ⊆ T"
unfolding span_def by (rule hull_minimal)

lemma span_UNIV [simp]: "span UNIV = UNIV"
by (intro span_unique) auto

lemma (in real_vector) span_induct:
assumes x: "x ∈ span S"
and P: "subspace (Collect P)"
and SP: "⋀x. x ∈ S ⟹ P x"
shows "P x"
proof -
from SP have SP': "S ⊆ Collect P"
from x hull_minimal[where S=subspace, OF SP' P, unfolded span_def[symmetric]]
show ?thesis
using subset_eq by force
qed

lemma span_empty[simp]: "span {} = {0}"
apply (rule hull_unique)
done

lemma (in real_vector) independent_empty [iff]: "independent {}"

lemma dependent_single[simp]: "dependent {x} ⟷ x = 0"
unfolding dependent_def by auto

lemma (in real_vector) independent_mono: "independent A ⟹ B ⊆ A ⟹ independent B"
apply (clarsimp simp add: dependent_def span_mono)
apply (subgoal_tac "span (B - {a}) ≤ span (A - {a})")
apply force
apply (rule span_mono)
apply auto
done

lemma (in real_vector) span_subspace: "A ⊆ B ⟹ B ≤ span A ⟹  subspace B ⟹ span A = B"
by (metis order_antisym span_def hull_minimal)

lemma (in real_vector) span_induct':
"∀x ∈ S. P x ⟹ subspace {x. P x} ⟹ ∀x ∈ span S. P x"
unfolding span_def by (rule hull_induct) auto

inductive_set (in real_vector) span_induct_alt_help for S :: "'a set"
where
span_induct_alt_help_0: "0 ∈ span_induct_alt_help S"
| span_induct_alt_help_S:
"x ∈ S ⟹ z ∈ span_induct_alt_help S ⟹
(c *⇩R x + z) ∈ span_induct_alt_help S"

lemma span_induct_alt':
assumes h0: "h 0"
and hS: "⋀c x y. x ∈ S ⟹ h y ⟹ h (c *⇩R x + y)"
shows "∀x ∈ span S. h x"
proof -
{
fix x :: 'a
assume x: "x ∈ span_induct_alt_help S"
have "h x"
apply (rule span_induct_alt_help.induct[OF x])
apply (rule h0)
apply (rule hS)
apply assumption
apply assumption
done
}
note th0 = this
{
fix x
assume x: "x ∈ span S"
have "x ∈ span_induct_alt_help S"
proof (rule span_induct[where x=x and S=S])
show "x ∈ span S" by (rule x)
next
fix x
assume xS: "x ∈ S"
from span_induct_alt_help_S[OF xS span_induct_alt_help_0, of 1]
show "x ∈ span_induct_alt_help S"
by simp
next
have "0 ∈ span_induct_alt_help S" by (rule span_induct_alt_help_0)
moreover
{
fix x y
assume h: "x ∈ span_induct_alt_help S" "y ∈ span_induct_alt_help S"
from h have "(x + y) ∈ span_induct_alt_help S"
apply (induct rule: span_induct_alt_help.induct)
apply simp
apply (rule span_induct_alt_help_S)
apply assumption
apply simp
done
}
moreover
{
fix c x
assume xt: "x ∈ span_induct_alt_help S"
then have "(c *⇩R x) ∈ span_induct_alt_help S"
apply (induct rule: span_induct_alt_help.induct)
apply (rule span_induct_alt_help_S)
apply assumption
apply simp
done }
ultimately show "subspace {a. a ∈ span_induct_alt_help S}"
unfolding subspace_def Ball_def by blast
qed
}
with th0 show ?thesis by blast
qed

lemma span_induct_alt:
assumes h0: "h 0"
and hS: "⋀c x y. x ∈ S ⟹ h y ⟹ h (c *⇩R x + y)"
and x: "x ∈ span S"
shows "h x"
using span_induct_alt'[of h S] h0 hS x by blast

text ‹Individual closure properties.›

lemma span_span: "span (span A) = span A"
unfolding span_def hull_hull ..

lemma (in real_vector) span_superset: "x ∈ S ⟹ x ∈ span S"
by (metis span_clauses(1))

lemma (in real_vector) span_0 [simp]: "0 ∈ span S"
by (metis subspace_span subspace_0)

lemma span_inc: "S ⊆ span S"
by (metis subset_eq span_superset)

lemma span_eq: "span S = span T ⟷ S ⊆ span T ∧ T ⊆ span S"
using span_inc[unfolded subset_eq] using span_mono[of T "span S"] span_mono[of S "span T"]

lemma (in real_vector) dependent_0:
assumes "0 ∈ A"
shows "dependent A"
unfolding dependent_def
using assms span_0
by blast

lemma (in real_vector) span_add: "x ∈ span S ⟹ y ∈ span S ⟹ x + y ∈ span S"

lemma (in real_vector) span_mul: "x ∈ span S ⟹ c *⇩R x ∈ span S"
by (metis subspace_span subspace_mul)

lemma span_neg: "x ∈ span S ⟹ - x ∈ span S"
by (metis subspace_neg subspace_span)

lemma span_diff: "x ∈ span S ⟹ y ∈ span S ⟹ x - y ∈ span S"
by (metis subspace_span subspace_diff)

lemma (in real_vector) span_sum: "(⋀x. x ∈ A ⟹ f x ∈ span S) ⟹ sum f A ∈ span S"
by (rule subspace_sum [OF subspace_span])

lemma span_add_eq: "x ∈ span S ⟹ x + y ∈ span S ⟷ y ∈ span S"
by (metis add_minus_cancel scaleR_minus1_left subspace_def subspace_span)

text ‹The key breakdown property.›

lemma span_singleton: "span {x} = range (λk. k *⇩R x)"
proof (rule span_unique)
show "{x} ⊆ range (λk. k *⇩R x)"
by (fast intro: scaleR_one [symmetric])
show "subspace (range (λk. k *⇩R x))"
unfolding subspace_def
next
fix T
assume "{x} ⊆ T" and "subspace T"
then show "range (λk. k *⇩R x) ⊆ T"
unfolding subspace_def by auto
qed

text ‹Mapping under linear image.›

lemma subspace_linear_image:
assumes lf: "linear f"
and sS: "subspace S"
shows "subspace (f ` S)"
using lf sS linear_0[OF lf]
unfolding linear_iff subspace_def
apply (rule_tac x="x + y" in bexI)
apply auto
apply (rule_tac x="c *⇩R x" in bexI)
apply auto
done

lemma subspace_linear_vimage: "linear f ⟹ subspace S ⟹ subspace (f -` S)"
by (auto simp add: subspace_def linear_iff linear_0[of f])

lemma subspace_linear_preimage: "linear f ⟹ subspace S ⟹ subspace {x. f x ∈ S}"
by (auto simp add: subspace_def linear_iff linear_0[of f])

lemma span_linear_image:
assumes lf: "linear f"
shows "span (f ` S) = f ` span S"
proof (rule span_unique)
show "f ` S ⊆ f ` span S"
by (intro image_mono span_inc)
show "subspace (f ` span S)"
using lf subspace_span by (rule subspace_linear_image)
next
fix T
assume "f ` S ⊆ T" and "subspace T"
then show "f ` span S ⊆ T"
unfolding image_subset_iff_subset_vimage
by (intro span_minimal subspace_linear_vimage lf)
qed

lemma spans_image:
assumes lf: "linear f"
and VB: "V ⊆ span B"
shows "f ` V ⊆ span (f ` B)"
unfolding span_linear_image[OF lf] by (metis VB image_mono)

lemma span_Un: "span (A ∪ B) = (λ(a, b). a + b) ` (span A × span B)"
proof (rule span_unique)
show "A ∪ B ⊆ (λ(a, b). a + b) ` (span A × span B)"
by safe (force intro: span_clauses)+
next
have "linear (λ(a, b). a + b)"
moreover have "subspace (span A × span B)"
by (intro subspace_Times subspace_span)
ultimately show "subspace ((λ(a, b). a + b) ` (span A × span B))"
by (rule subspace_linear_image)
next
fix T
assume "A ∪ B ⊆ T" and "subspace T"
then show "(λ(a, b). a + b) ` (span A × span B) ⊆ T"
by (auto intro!: subspace_add elim: span_induct)
qed

lemma span_insert: "span (insert a S) = {x. ∃k. (x - k *⇩R a) ∈ span S}"
proof -
have "span ({a} ∪ S) = {x. ∃k. (x - k *⇩R a) ∈ span S}"
unfolding span_Un span_singleton
apply safe
apply (rule_tac x=k in exI, simp)
apply (erule rev_image_eqI [OF SigmaI [OF rangeI]])
apply auto
done
then show ?thesis by simp
qed

lemma span_breakdown:
assumes bS: "b ∈ S"
and aS: "a ∈ span S"
shows "∃k. a - k *⇩R b ∈ span (S - {b})"
using assms span_insert [of b "S - {b}"]

lemma span_breakdown_eq: "x ∈ span (insert a S) ⟷ (∃k. x - k *⇩R a ∈ span S)"

text ‹Hence some "reversal" results.›

lemma in_span_insert:
assumes a: "a ∈ span (insert b S)"
and na: "a ∉ span S"
shows "b ∈ span (insert a S)"
proof -
from a obtain k where k: "a - k *⇩R b ∈ span S"
unfolding span_insert by fast
show ?thesis
proof (cases "k = 0")
case True
with k have "a ∈ span S" by simp
with na show ?thesis by simp
next
case False
from k have "(- inverse k) *⇩R (a - k *⇩R b) ∈ span S"
by (rule span_mul)
then have "b - inverse k *⇩R a ∈ span S"
using ‹k ≠ 0› by (simp add: scaleR_diff_right)
then show ?thesis
unfolding span_insert by fast
qed
qed

lemma in_span_delete:
assumes a: "a ∈ span S"
and na: "a ∉ span (S - {b})"
shows "b ∈ span (insert a (S - {b}))"
apply (rule in_span_insert)
apply (rule set_rev_mp)
apply (rule a)
apply (rule span_mono)
apply blast
apply (rule na)
done

text ‹Transitivity property.›

lemma span_redundant: "x ∈ span S ⟹ span (insert x S) = span S"
unfolding span_def by (rule hull_redundant)

lemma span_trans:
assumes x: "x ∈ span S"
and y: "y ∈ span (insert x S)"
shows "y ∈ span S"
using assms by (simp only: span_redundant)

lemma span_insert_0[simp]: "span (insert 0 S) = span S"
by (simp only: span_redundant span_0)

text ‹An explicit expansion is sometimes needed.›

lemma span_explicit:
"span P = {y. ∃S u. finite S ∧ S ⊆ P ∧ sum (λv. u v *⇩R v) S = y}"
(is "_ = ?E" is "_ = {y. ?h y}" is "_ = {y. ∃S u. ?Q S u y}")
proof -
{
fix x
assume "?h x"
then obtain S u where "finite S" and "S ⊆ P" and "sum (λv. u v *⇩R v) S = x"
by blast
then have "x ∈ span P"
by (auto intro: span_sum span_mul span_superset)
}
moreover
have "∀x ∈ span P. ?h x"
proof (rule span_induct_alt')
show "?h 0"
by (rule exI[where x="{}"], simp)
next
fix c x y
assume x: "x ∈ P"
assume hy: "?h y"
from hy obtain S u where fS: "finite S" and SP: "S⊆P"
and u: "sum (λv. u v *⇩R v) S = y" by blast
let ?S = "insert x S"
let ?u = "λy. if y = x then (if x ∈ S then u y + c else c) else u y"
from fS SP x have th0: "finite (insert x S)" "insert x S ⊆ P"
by blast+
have "?Q ?S ?u (c*⇩R x + y)"
proof cases
assume xS: "x ∈ S"
have "sum (λv. ?u v *⇩R v) ?S = (∑v∈S - {x}. u v *⇩R v) + (u x + c) *⇩R x"
using xS by (simp add: sum.remove [OF fS xS] insert_absorb)
also have "… = (∑v∈S. u v *⇩R v) + c *⇩R x"
by (simp add: sum.remove [OF fS xS] algebra_simps)
also have "… = c*⇩R x + y"
finally have "sum (λv. ?u v *⇩R v) ?S = c*⇩R x + y" .
then show ?thesis using th0 by blast
next
assume xS: "x ∉ S"
have th00: "(∑v∈S. (if v = x then c else u v) *⇩R v) = y"
unfolding u[symmetric]
apply (rule sum.cong)
using xS
apply auto
done
show ?thesis using fS xS th0
qed
then show "?h (c*⇩R x + y)"
by fast
qed
ultimately show ?thesis by blast
qed

lemma dependent_explicit:
"dependent P ⟷ (∃S u. finite S ∧ S ⊆ P ∧ (∃v∈S. u v ≠ 0 ∧ sum (λv. u v *⇩R v) S = 0))"
(is "?lhs = ?rhs")
proof -
{
assume dP: "dependent P"
then obtain a S u where aP: "a ∈ P" and fS: "finite S"
and SP: "S ⊆ P - {a}" and ua: "sum (λv. u v *⇩R v) S = a"
unfolding dependent_def span_explicit by blast
let ?S = "insert a S"
let ?u = "λy. if y = a then - 1 else u y"
let ?v = a
from aP SP have aS: "a ∉ S"
by blast
from fS SP aP have th0: "finite ?S" "?S ⊆ P" "?v ∈ ?S" "?u ?v ≠ 0"
by auto
have s0: "sum (λv. ?u v *⇩R v) ?S = 0"
using fS aS
apply simp
apply (subst (2) ua[symmetric])
apply (rule sum.cong)
apply auto
done
with th0 have ?rhs by fast
}
moreover
{
fix S u v
assume fS: "finite S"
and SP: "S ⊆ P"
and vS: "v ∈ S"
and uv: "u v ≠ 0"
and u: "sum (λv. u v *⇩R v) S = 0"
let ?a = v
let ?S = "S - {v}"
let ?u = "λi. (- u i) / u v"
have th0: "?a ∈ P" "finite ?S" "?S ⊆ P"
using fS SP vS by auto
have "sum (λv. ?u v *⇩R v) ?S =
sum (λv. (- (inverse (u ?a))) *⇩R (u v *⇩R v)) S - ?u v *⇩R v"
using fS vS uv by (simp add: sum_diff1 field_simps)
also have "… = ?a"
unfolding scaleR_right.sum [symmetric] u using uv by simp
finally have "sum (λv. ?u v *⇩R v) ?S = ?a" .
with th0 have ?lhs
unfolding dependent_def span_explicit
apply -
apply (rule bexI[where x= "?a"])
apply (simp_all del: scaleR_minus_left)
apply (rule exI[where x= "?S"])
apply (auto simp del: scaleR_minus_left)
done
}
ultimately show ?thesis by blast
qed

lemma dependent_finite:
assumes "finite S"
shows "dependent S ⟷ (∃u. (∃v ∈ S. u v ≠ 0) ∧ (∑v∈S. u v *⇩R v) = 0)"
(is "?lhs = ?rhs")
proof
assume ?lhs
then obtain T u v
where "finite T" "T ⊆ S" "v∈T" "u v ≠ 0" "(∑v∈T. u v *⇩R v) = 0"
by (force simp: dependent_explicit)
with assms show ?rhs
apply (rule_tac x="λv. if v ∈ T then u v else 0" in exI)
apply (auto simp: sum.mono_neutral_right)
done
next
assume ?rhs  with assms show ?lhs
qed

lemma span_alt:
"span B = {(∑x | f x ≠ 0. f x *⇩R x) | f. {x. f x ≠ 0} ⊆ B ∧ finite {x. f x ≠ 0}}"
unfolding span_explicit
apply safe
subgoal for x S u
by (intro exI[of _ "λx. if x ∈ S then u x else 0"])
(auto intro!: sum.mono_neutral_cong_right)
apply auto
done

lemma dependent_alt:
"dependent B ⟷
(∃X. finite {x. X x ≠ 0} ∧ {x. X x ≠ 0} ⊆ B ∧ (∑x|X x ≠ 0. X x *⇩R x) = 0 ∧ (∃x. X x ≠ 0))"
unfolding dependent_explicit
apply safe
subgoal for S u v
apply (intro exI[of _ "λx. if x ∈ S then u x else 0"])
apply (subst sum.mono_neutral_cong_left[where T=S])
apply (auto intro!: sum.mono_neutral_cong_right cong: rev_conj_cong)
done
apply auto
done

lemma independent_alt:
"independent B ⟷
(∀X. finite {x. X x ≠ 0} ⟶ {x. X x ≠ 0} ⊆ B ⟶ (∑x|X x ≠ 0. X x *⇩R x) = 0 ⟶ (∀x. X x = 0))"
unfolding dependent_alt by auto

lemma independentD_alt:
"independent B ⟹ finite {x. X x ≠ 0} ⟹ {x. X x ≠ 0} ⊆ B ⟹ (∑x|X x ≠ 0. X x *⇩R x) = 0 ⟹ X x = 0"
unfolding independent_alt by blast

lemma independentD_unique:
assumes B: "independent B"
and X: "finite {x. X x ≠ 0}" "{x. X x ≠ 0} ⊆ B"
and Y: "finite {x. Y x ≠ 0}" "{x. Y x ≠ 0} ⊆ B"
and "(∑x | X x ≠ 0. X x *⇩R x) = (∑x| Y x ≠ 0. Y x *⇩R x)"
shows "X = Y"
proof -
have "X x - Y x = 0" for x
using B
proof (rule independentD_alt)
have "{x. X x - Y x ≠ 0} ⊆ {x. X x ≠ 0} ∪ {x. Y x ≠ 0}"
by auto
then show "finite {x. X x - Y x ≠ 0}" "{x. X x - Y x ≠ 0} ⊆ B"
using X Y by (auto dest: finite_subset)
then have "(∑x | X x - Y x ≠ 0. (X x - Y x) *⇩R x) = (∑v∈{S. X S ≠ 0} ∪ {S. Y S ≠ 0}. (X v - Y v) *⇩R v)"
using X Y by (intro sum.mono_neutral_cong_left) auto
also have "… = (∑v∈{S. X S ≠ 0} ∪ {S. Y S ≠ 0}. X v *⇩R v) - (∑v∈{S. X S ≠ 0} ∪ {S. Y S ≠ 0}. Y v *⇩R v)"
by (simp add: scaleR_diff_left sum_subtractf assms)
also have "(∑v∈{S. X S ≠ 0} ∪ {S. Y S ≠ 0}. X v *⇩R v) = (∑v∈{S. X S ≠ 0}. X v *⇩R v)"
using X Y by (intro sum.mono_neutral_cong_right) auto
also have "(∑v∈{S. X S ≠ 0} ∪ {S. Y S ≠ 0}. Y v *⇩R v) = (∑v∈{S. Y S ≠ 0}. Y v *⇩R v)"
using X Y by (intro sum.mono_neutral_cong_right) auto
finally show "(∑x | X x - Y x ≠ 0. (X x - Y x) *⇩R x) = 0"
using assms by simp
qed
then show ?thesis
by auto
qed

text ‹This is useful for building a basis step-by-step.›

lemma independent_insert:
"independent (insert a S) ⟷
(if a ∈ S then independent S else independent S ∧ a ∉ span S)"
(is "?lhs ⟷ ?rhs")
proof (cases "a ∈ S")
case True
then show ?thesis
using insert_absorb[OF True] by simp
next
case False
show ?thesis
proof
assume i: ?lhs
then show ?rhs
using False
apply simp
apply (rule conjI)
apply (rule independent_mono)
apply assumption
apply blast
done
next
assume i: ?rhs
show ?lhs
using i False
by (metis in_span_insert insert_Diff_if insert_Diff_single insert_absorb)
qed
qed

lemma independent_Union_directed:
assumes directed: "⋀c d. c ∈ C ⟹ d ∈ C ⟹ c ⊆ d ∨ d ⊆ c"
assumes indep: "⋀c. c ∈ C ⟹ independent c"
shows "independent (⋃C)"
proof
assume "dependent (⋃C)"
then obtain u v S where S: "finite S" "S ⊆ ⋃C" "v ∈ S" "u v ≠ 0" "(∑v∈S. u v *⇩R v) = 0"
by (auto simp: dependent_explicit)

have "S ≠ {}"
using ‹v ∈ S› by auto
have "∃c∈C. S ⊆ c"
using ‹finite S› ‹S ≠ {}› ‹S ⊆ ⋃C›
proof (induction rule: finite_ne_induct)
case (insert i I)
then obtain c d where cd: "c ∈ C" "d ∈ C" and iI: "I ⊆ c" "i ∈ d"
by blast
from directed[OF cd] cd have "c ∪ d ∈ C"
by (auto simp: sup.absorb1 sup.absorb2)
with iI show ?case
by (intro bexI[of _ "c ∪ d"]) auto
qed auto
then obtain c where "c ∈ C" "S ⊆ c"
by auto
have "dependent c"
unfolding dependent_explicit
by (intro exI[of _ S] exI[of _ u] bexI[of _ v] conjI) fact+
with indep[OF ‹c ∈ C›] show False
by auto
qed

text ‹Hence we can create a maximal independent subset.›

lemma maximal_independent_subset_extend:
assumes "S ⊆ V" "independent S"
shows "∃B. S ⊆ B ∧ B ⊆ V ∧ independent B ∧ V ⊆ span B"
proof -
let ?C = "{B. S ⊆ B ∧ independent B ∧ B ⊆ V}"
have "∃M∈?C. ∀X∈?C. M ⊆ X ⟶ X = M"
proof (rule subset_Zorn)
fix C :: "'a set set" assume "subset.chain ?C C"
then have C: "⋀c. c ∈ C ⟹ c ⊆ V" "⋀c. c ∈ C ⟹ S ⊆ c" "⋀c. c ∈ C ⟹ independent c"
"⋀c d. c ∈ C ⟹ d ∈ C ⟹ c ⊆ d ∨ d ⊆ c"
unfolding subset.chain_def by blast+

show "∃U∈?C. ∀X∈C. X ⊆ U"
proof cases
assume "C = {}" with assms show ?thesis
by (auto intro!: exI[of _ S])
next
assume "C ≠ {}"
with C(2) have "S ⊆ ⋃C"
by auto
moreover have "independent (⋃C)"
by (intro independent_Union_directed C)
moreover have "⋃C ⊆ V"
using C by auto
ultimately show ?thesis
by auto
qed
qed
then obtain B where B: "independent B" "B ⊆ V" "S ⊆ B"
and max: "⋀S. independent S ⟹ S ⊆ V ⟹ B ⊆ S ⟹ S = B"
by auto
moreover
{ assume "¬ V ⊆ span B"
then obtain v where "v ∈ V" "v ∉ span B"
by auto
with B have "independent (insert v B)"
unfolding independent_insert by auto
from max[OF this] ‹v ∈ V› ‹B ⊆ V›
have "v ∈ B"
by auto
with ‹v ∉ span B› have False
by (auto intro: span_superset) }
ultimately show ?thesis
by (auto intro!: exI[of _ B])
qed

lemma maximal_independent_subset:
"∃B. B ⊆ V ∧ independent B ∧ V ⊆ span B"
by (metis maximal_independent_subset_extend[of "{}"] empty_subsetI independent_empty)

lemma span_finite:
assumes fS: "finite S"
shows "span S = {y. ∃u. sum (λv. u v *⇩R v) S = y}"
(is "_ = ?rhs")
proof -
{
fix y
assume y: "y ∈ span S"
from y obtain S' u where fS': "finite S'"
and SS': "S' ⊆ S"
and u: "sum (λv. u v *⇩R v) S' = y"
unfolding span_explicit by blast
let ?u = "λx. if x ∈ S' then u x else 0"
have "sum (λv. ?u v *⇩R v) S = sum (λv. u v *⇩R v) S'"
using SS' fS by (auto intro!: sum.mono_neutral_cong_right)
then have "sum (λv. ?u v *⇩R v) S = y" by (metis u)
then have "y ∈ ?rhs" by auto
}
moreover
{
fix y u
assume u: "sum (λv. u v *⇩R v) S = y"
then have "y ∈ span S" using fS unfolding span_explicit by auto
}
ultimately show ?thesis by blast
qed

lemma linear_independent_extend_subspace:
assumes "independent B"
shows "∃g. linear g ∧ (∀x∈B. g x = f x) ∧ range g = span (f`B)"
proof -
from maximal_independent_subset_extend[OF _ ‹independent B›, of UNIV]
obtain B' where "B ⊆ B'" "independent B'" "span B' = UNIV"
by (auto simp: top_unique)
have "∀y. ∃X. {x. X x ≠ 0} ⊆ B' ∧ finite {x. X x ≠ 0} ∧ y = (∑x|X x ≠ 0. X x *⇩R x)"
using ‹span B' = UNIV› unfolding span_alt by auto
then obtain X where X: "⋀y. {x. X y x ≠ 0} ⊆ B'" "⋀y. finite {x. X y x ≠ 0}"
"⋀y. y = (∑x|X y x ≠ 0. X y x *⇩R x)"
unfolding choice_iff by auto

have X_add: "X (x + y) = (λz. X x z + X y z)" for x y
using ‹independent B'›
proof (rule independentD_unique)
have "(∑z | X x z + X y z ≠ 0. (X x z + X y z) *⇩R z)
= (∑z∈{z. X x z ≠ 0} ∪ {z. X y z ≠ 0}. (X x z + X y z) *⇩R z)"
by (intro sum.mono_neutral_cong_left) (auto intro: X)
also have "… = (∑z∈{z. X x z ≠ 0}. X x z *⇩R z) + (∑z∈{z. X y z ≠ 0}. X y z *⇩R z)"
intro!: arg_cong2[where f="op +"]  sum.mono_neutral_cong_right X)
also have "… = x + y"
also have "… = (∑z | X (x + y) z ≠ 0. X (x + y) z *⇩R z)"
by (rule X(3))
finally show "(∑z | X (x + y) z ≠ 0. X (x + y) z *⇩R z) = (∑z | X x z + X y z ≠ 0. (X x z + X y z) *⇩R z)"
..
have "{z. X x z + X y z ≠ 0} ⊆ {z. X x z ≠ 0} ∪ {z. X y z ≠ 0}"
by auto
then show "finite {z. X x z + X y z ≠ 0}" "{xa. X x xa + X y xa ≠ 0} ⊆ B'"
"finite {xa. X (x + y) xa ≠ 0}" "{xa. X (x + y) xa ≠ 0} ⊆ B'"
using X(1) by (auto dest: finite_subset intro: X)
qed

have X_cmult: "X (c *⇩R x) = (λz. c * X x z)" for x c
using ‹independent B'›
proof (rule independentD_unique)
show "finite {z. X (c *⇩R x) z ≠ 0}" "{z. X (c *⇩R x) z ≠ 0} ⊆ B'"
"finite {z. c * X x z ≠ 0}" "{z. c * X x z ≠ 0} ⊆ B' "
using X(1,2) by auto
show "(∑z | X (c *⇩R x) z ≠ 0. X (c *⇩R x) z *⇩R z) = (∑z | c * X x z ≠ 0. (c * X x z) *⇩R z)"
unfolding scaleR_scaleR[symmetric] scaleR_sum_right[symmetric]
by (cases "c = 0") (auto simp: X(3)[symmetric])
qed

have X_B': "x ∈ B' ⟹ X x = (λz. if z = x then 1 else 0)" for x
using ‹independent B'›
by (rule independentD_unique[OF _ X(2) X(1)]) (auto intro: X simp: X(3)[symmetric])

define f' where "f' y = (if y ∈ B then f y else 0)" for y
define g where "g y = (∑x|X y x ≠ 0. X y x *⇩R f' x)" for y

have g_f': "x ∈ B' ⟹ g x = f' x" for x
by (auto simp: g_def X_B')

have "linear g"
proof
fix x y
have *: "(∑z | X x z + X y z ≠ 0. (X x z + X y z) *⇩R f' z)
= (∑z∈{z. X x z ≠ 0} ∪ {z. X y z ≠ 0}. (X x z + X y z) *⇩R f' z)"
by (intro sum.mono_neutral_cong_left) (auto intro: X)
show "g (x + y) = g x + g y"
intro!: arg_cong2[where f="op +"]  sum.mono_neutral_cong_right X)
next
show "g (r *⇩R x) = r *⇩R g x" for r x
by (auto simp add: g_def X_cmult scaleR_sum_right intro!: sum.mono_neutral_cong_left X)
qed
moreover have "∀x∈B. g x = f x"
using ‹B ⊆ B'› by (auto simp: g_f' f'_def)
moreover have "range g = span (f`B)"
unfolding ‹span B' = UNIV›[symmetric] span_linear_image[OF ‹linear g›, symmetric]
proof (rule span_subspace)
have "g ` B' ⊆ f`B ∪ {0}"
by (auto simp: g_f' f'_def)
also have "… ⊆ span (f`B)"
by (auto intro: span_superset span_0)
finally show "g ` B' ⊆ span (f`B)"
by auto
have "x ∈ B ⟹ f x = g x" for x
using ‹B ⊆ B'› by (auto simp add: g_f' f'_def)
then show "span (f ` B) ⊆ span (g ` B')"
using ‹B ⊆ B'› by (intro span_mono) auto
qed (rule subspace_span)
ultimately show ?thesis
by auto
qed

lemma linear_independent_extend:
"independent B ⟹ ∃g. linear g ∧ (∀x∈B. g x = f x)"
using linear_independent_extend_subspace[of B f] by auto

text ‹Linear functions are equal on a subspace if they are on a spanning set.›

lemma subspace_kernel:
assumes lf: "linear f"
shows "subspace {x. f x = 0}"
done

lemma linear_eq_0_span:
assumes lf: "linear f" and f0: "∀x∈B. f x = 0"
shows "∀x ∈ span B. f x = 0"
using f0 subspace_kernel[OF lf]
by (rule span_induct')

lemma linear_eq_0: "linear f ⟹ S ⊆ span B ⟹ ∀x∈B. f x = 0 ⟹ ∀x∈S. f x = 0"
using linear_eq_0_span[of f B] by auto

lemma linear_eq_span:  "linear f ⟹ linear g ⟹ ∀x∈B. f x = g x ⟹ ∀x ∈ span B. f x = g x"
using linear_eq_0_span[of "λx. f x - g x" B] by (auto simp: linear_compose_sub)

lemma linear_eq: "linear f ⟹ linear g ⟹ S ⊆ span B ⟹ ∀x∈B. f x = g x ⟹ ∀x∈S. f x = g x"
using linear_eq_span[of f g B] by auto

text ‹The degenerate case of the Exchange Lemma.›

lemma spanning_subset_independent:
assumes BA: "B ⊆ A"
and iA: "independent A"
and AsB: "A ⊆ span B"
shows "A = B"
proof
show "B ⊆ A" by (rule BA)

from span_mono[OF BA] span_mono[OF AsB]
have sAB: "span A = span B" unfolding span_span by blast

{
fix x
assume x: "x ∈ A"
from iA have th0: "x ∉ span (A - {x})"
unfolding dependent_def using x by blast
from x have xsA: "x ∈ span A"
by (blast intro: span_superset)
have "A - {x} ⊆ A" by blast
then have th1: "span (A - {x}) ⊆ span A"
by (metis span_mono)
{
assume xB: "x ∉ B"
from xB BA have "B ⊆ A - {x}"
by blast
then have "span B ⊆ span (A - {x})"
by (metis span_mono)
with th1 th0 sAB have "x ∉ span A"
by blast
with x have False
by (metis span_superset)
}
then have "x ∈ B" by blast
}
then show "A ⊆ B" by blast
qed

text ‹Relation between bases and injectivity/surjectivity of map.›

lemma spanning_surjective_image:
assumes us: "UNIV ⊆ span S"
and lf: "linear f"
and sf: "surj f"
shows "UNIV ⊆ span (f ` S)"
proof -
have "UNIV ⊆ f ` UNIV"
using sf by (auto simp add: surj_def)
also have " … ⊆ span (f ` S)"
using spans_image[OF lf us] .
finally show ?thesis .
qed

lemma independent_inj_on_image:
assumes iS: "independent S"
and lf: "linear f"
and fi: "inj_on f (span S)"
shows "independent (f ` S)"
proof -
{
fix a
assume a: "a ∈ S" "f a ∈ span (f ` S - {f a})"
have eq: "f ` S - {f a} = f ` (S - {a})"
using fi ‹a∈S› by (auto simp add: inj_on_def span_superset)
from a have "f a ∈ f ` span (S - {a})"
unfolding eq span_linear_image[OF lf, of "S - {a}"] by blast
then have "a ∈ span (S - {a})"
by (rule inj_on_image_mem_iff_alt[OF fi, rotated])
(insert span_mono[of "S - {a}" S], auto intro: span_superset ‹a∈S›)
with a(1) iS have False
}
then show ?thesis
unfolding dependent_def by blast
qed

lemma independent_injective_image:
"independent S ⟹ linear f ⟹ inj f ⟹ independent (f ` S)"
using independent_inj_on_image[of S f] by (auto simp: subset_inj_on)

text ‹Detailed theorems about left and right invertibility in general case.›

lemma linear_inj_on_left_inverse:
assumes lf: "linear f" and fi: "inj_on f (span S)"
shows "∃g. range g ⊆ span S ∧ linear g ∧ (∀x∈span S. g (f x) = x)"
proof -
obtain B where "independent B" "B ⊆ S" "S ⊆ span B"
using maximal_independent_subset[of S] by auto
then have "span S = span B"
unfolding span_eq by (auto simp: span_superset)
with linear_independent_extend_subspace[OF independent_inj_on_image, OF ‹independent B› lf] fi
obtain g where g: "linear g" "∀x∈f ` B. g x = inv_into B f x" "range g = span (inv_into B f ` f ` B)"
by fastforce
have fB: "inj_on f B"
using fi by (auto simp: ‹span S = span B› intro: subset_inj_on span_superset)

have "∀x∈span B. g (f x) = x"
proof (intro linear_eq_span)
show "linear (λx. x)" "linear (λx. g (f x))"
using linear_id linear_compose[OF ‹linear f› ‹linear g›] by (auto simp: id_def comp_def)
show "∀x ∈ B. g (f x) = x"
using g fi ‹span S = span B› by (auto simp: fB)
qed
moreover
have "inv_into B f ` f ` B ⊆ B"
by (auto simp: fB)
then have "range g ⊆ span S"
unfolding g ‹span S = span B› by (intro span_mono)
ultimately show ?thesis
using ‹span S = span B› ‹linear g› by auto
qed

lemma linear_injective_left_inverse: "linear f ⟹ inj f ⟹ ∃g. linear g ∧ g ∘ f = id"
using linear_inj_on_left_inverse[of f UNIV] by (auto simp: fun_eq_iff span_UNIV)

lemma linear_surj_right_inverse:
assumes lf: "linear f" and sf: "span T ⊆ f`span S"
shows "∃g. range g ⊆ span S ∧ linear g ∧ (∀x∈span T. f (g x) = x)"
proof -
obtain B where "independent B" "B ⊆ T" "T ⊆ span B"
using maximal_independent_subset[of T] by auto
then have "span T = span B"
unfolding span_eq by (auto simp: span_superset)

from linear_independent_extend_subspace[OF ‹independent B›, of "inv_into (span S) f"]
obtain g where g: "linear g" "∀x∈B. g x = inv_into (span S) f x" "range g = span (inv_into (span S) f`B)"
by auto
moreover have "x ∈ B ⟹ f (inv_into (span S) f x) = x" for x
using ‹B ⊆ T› ‹span T ⊆ f`span S› by (intro f_inv_into_f) (auto intro: span_superset)
ultimately have "∀x∈B. f (g x) = x"
by auto
then have "∀x∈span B. f (g x) = x"
using linear_id linear_compose[OF ‹linear g› ‹linear f›]
by (intro linear_eq_span) (auto simp: id_def comp_def)
moreover have "inv_into (span S) f ` B ⊆ span S"
using ‹B ⊆ T› ‹span T ⊆ f`span S› by (auto intro: inv_into_into span_superset)
then have "range g ⊆ span S"
unfolding g by (intro span_minimal subspace_span) auto
ultimately show ?thesis
using ‹linear g› ‹span T = span B› by auto
qed

lemma linear_surjective_right_inverse: "linear f ⟹ surj f ⟹ ∃g. linear g ∧ f ∘ g = id"
using linear_surj_right_inverse[of f UNIV UNIV]
by (auto simp: span_UNIV fun_eq_iff)

text ‹The general case of the Exchange Lemma, the key to what follows.›

lemma exchange_lemma:
assumes f:"finite t"
and i: "independent s"
and sp: "s ⊆ span t"
shows "∃t'. card t' = card t ∧ finite t' ∧ s ⊆ t' ∧ t' ⊆ s ∪ t ∧ s ⊆ span t'"
using f i sp
proof (induct "card (t - s)" arbitrary: s t rule: less_induct)
case less
note ft = ‹finite t› and s = ‹independent s› and sp = ‹s ⊆ span t›
let ?P = "λt'. card t' = card t ∧ finite t' ∧ s ⊆ t' ∧ t' ⊆ s ∪ t ∧ s ⊆ span t'"
let ?ths = "∃t'. ?P t'"
{
assume "s ⊆ t"
then have ?ths
by (metis ft Un_commute sp sup_ge1)
}
moreover
{
assume st: "t ⊆ s"
from spanning_subset_independent[OF st s sp] st ft span_mono[OF st]
have ?ths
by (metis Un_absorb sp)
}
moreover
{
assume st: "¬ s ⊆ t" "¬ t ⊆ s"
from st(2) obtain b where b: "b ∈ t" "b ∉ s"
by blast
from b have "t - {b} - s ⊂ t - s"
by blast
then have cardlt: "card (t - {b} - s) < card (t - s)"
using ft by (auto intro: psubset_card_mono)
from b ft have ct0: "card t ≠ 0"
by auto
have ?ths
proof cases
assume stb: "s ⊆ span (t - {b})"
from ft have ftb: "finite (t - {b})"
by auto
from less(1)[OF cardlt ftb s stb]
obtain u where u: "card u = card (t - {b})" "s ⊆ u" "u ⊆ s ∪ (t - {b})" "s ⊆ span u"
and fu: "finite u" by blast
let ?w = "insert b u"
have th0: "s ⊆ insert b u"
using u by blast
from u(3) b have "u ⊆ s ∪ t"
by blast
then have th1: "insert b u ⊆ s ∪ t"
using u b by blast
have bu: "b ∉ u"
using b u by blast
from u(1) ft b have "card u = (card t - 1)"
by auto
then have th2: "card (insert b u) = card t"
using card_insert_disjoint[OF fu bu] ct0 by auto
from u(4) have "s ⊆ span u" .
also have "… ⊆ span (insert b u)"
by (rule span_mono) blast
finally have th3: "s ⊆ span (insert b u)" .
from th0 th1 th2 th3 fu have th: "?P ?w"
by blast
from th show ?thesis by blast
next
assume stb: "¬ s ⊆ span (t - {b})"
from stb obtain a where a: "a ∈ s" "a ∉ span (t - {b})"
by blast
have ab: "a ≠ b"
using a b by blast
have at: "a ∉ t"
using a ab span_superset[of a "t- {b}"] by auto
have mlt: "card ((insert a (t - {b})) - s) < card (t - s)"
using cardlt ft a b by auto
have ft': "finite (insert a (t - {b}))"
using ft by auto
{
fix x
assume xs: "x ∈ s"
have t: "t ⊆ insert b (insert a (t - {b}))"
using b by auto
from b(1) have "b ∈ span t"
have bs: "b ∈ span (insert a (t - {b}))"
apply (rule in_span_delete)
using a sp unfolding subset_eq
apply auto
done
from xs sp have "x ∈ span t"
by blast
with span_mono[OF t] have x: "x ∈ span (insert b (insert a (t - {b})))" ..
from span_trans[OF bs x] have "x ∈ span (insert a (t - {b}))" .
}
then have sp': "s ⊆ span (insert a (t - {b}))"
by blast
from less(1)[OF mlt ft' s sp'] obtain u where u:
"card u = card (insert a (t - {b}))"
"finite u" "s ⊆ u" "u ⊆ s ∪ insert a (t - {b})"
"s ⊆ span u" by blast
from u a b ft at ct0 have "?P u"
by auto
then show ?thesis by blast
qed
}
ultimately show ?ths by blast
qed

text ‹This implies corresponding size bounds.›

lemma independent_span_bound:
assumes f: "finite t"
and i: "independent s"
and sp: "s ⊆ span t"
shows "finite s ∧ card s ≤ card t"
by (metis exchange_lemma[OF f i sp] finite_subset card_mono)

lemma finite_Atleast_Atmost_nat[simp]: "finite {f x |x. x∈ (UNIV::'a::finite set)}"
proof -
have eq: "{f x |x. x∈ UNIV} = f ` UNIV"
by auto
show ?thesis unfolding eq
apply (rule finite_imageI)
apply (rule finite)
done
qed

subsection ‹More interesting properties of the norm.›

lemma cond_application_beta: "(if b then f else g) x = (if b then f x else g x)"
by auto

notation inner (infix "∙" 70)

lemma square_bound_lemma:
fixes x :: real
shows "x < (1 + x) * (1 + x)"
proof -
have "(x + 1/2)⇧2 + 3/4 > 0"
using zero_le_power2[of "x+1/2"] by arith
then show ?thesis
qed

lemma square_continuous:
fixes e :: real
shows "e > 0 ⟹ ∃d. 0 < d ∧ (∀y. ¦y - x¦ < d ⟶ ¦y * y - x * x¦ < e)"
using isCont_power[OF continuous_ident, of x, unfolded isCont_def LIM_eq, rule_format, of e 2]

lemma norm_eq_0_dot: "norm x = 0 ⟷ x ∙ x = (0::real)"
by simp (* TODO: delete *)

lemma norm_triangle_sub:
fixes x y :: "'a::real_normed_vector"
shows "norm x ≤ norm y + norm (x - y)"
using norm_triangle_ineq[of "y" "x - y"] by (simp add: field_simps)

lemma norm_le: "norm x ≤ norm y ⟷ x ∙ x ≤ y ∙ y"

lemma norm_lt: "norm x < norm y ⟷ x ∙ x < y ∙ y"

lemma norm_eq: "norm x = norm y ⟷ x ∙ x = y ∙ y"
apply (subst order_eq_iff)
apply (auto simp: norm_le)
done

lemma norm_eq_1: "norm x = 1 ⟷ x ∙ x = 1"

text‹Equality of vectors in terms of @{term "op ∙"} products.›

lemma linear_componentwise:
fixes f:: "'a::euclidean_space ⇒ 'b::real_inner"
assumes lf: "linear f"
shows "(f x) ∙ j = (∑i∈Basis. (x∙i) * (f i∙j))" (is "?lhs = ?rhs")
proof -
have "?rhs = (∑i∈Basis. (x∙i) *⇩R (f i))∙j"
then show ?thesis
unfolding linear_sum_mul[OF lf, symmetric]
unfolding euclidean_representation ..
qed

lemma vector_eq: "x = y ⟷ x ∙ x = x ∙ y ∧ y ∙ y = x ∙ x"
(is "?lhs ⟷ ?rhs")
proof
assume ?lhs
then show ?rhs by simp
next
assume ?rhs
then have "x ∙ x - x ∙ y = 0 ∧ x ∙ y - y ∙ y = 0"
by simp
then have "x ∙ (x - y) = 0 ∧ y ∙ (x - y) = 0"
then have "(x - y) ∙ (x - y) = 0"
by (simp add: field_simps inner_diff inner_commute)
then show "x = y" by simp
qed

lemma norm_triangle_half_r:
"norm (y - x1) < e / 2 ⟹ norm (y - x2) < e / 2 ⟹ norm (x1 - x2) < e"
using dist_triangle_half_r unfolding dist_norm[symmetric] by auto

lemma norm_triangle_half_l:
assumes "norm (x - y) < e / 2"
and "norm (x' - y) < e / 2"
shows "norm (x - x') < e"
using dist_triangle_half_l[OF assms[unfolded dist_norm[symmetric]]]
unfolding dist_norm[symmetric] .

lemma norm_triangle_le: "norm x + norm y ≤ e ⟹ norm (x + y) ≤ e"
by (rule norm_triangle_ineq [THEN order_trans])

lemma norm_triangle_lt: "norm x + norm y < e ⟹ norm (x + y) < e"
by (rule norm_triangle_ineq [THEN le_less_trans])

lemma abs_triangle_half_r:
fixes y :: "'a::linordered_field"
shows "abs (y - x1) < e / 2 ⟹ abs (y - x2) < e / 2 ⟹ abs (x1 - x2) < e"
by linarith

lemma abs_triangle_half_l:
fixes y :: "'a::linordered_field"
assumes "abs (x - y) < e / 2"
and "abs (x' - y) < e / 2"
shows "abs (x - x') < e"
using assms by linarith

lemma sum_clauses:
shows "sum f {} = 0"
and "finite S ⟹ sum f (insert x S) = (if x ∈ S then sum f S else f x + sum f S)"

lemma sum_norm_bound:
fixes f :: "'a ⇒ 'b::real_normed_vector"
assumes K: "⋀x. x ∈ S ⟹ norm (f x) ≤ K"
shows "norm (sum f S) ≤ of_nat (card S)*K"
using sum_norm_le[OF K] sum_constant[symmetric]
by simp

lemma sum_group:
assumes fS: "finite S" and fT: "finite T" and fST: "f ` S ⊆ T"
shows "sum (λy. sum g {x. x ∈ S ∧ f x = y}) T = sum g S"
apply (subst sum_image_gen[OF fS, of g f])
apply (rule sum.mono_neutral_right[OF fT fST])
apply (auto intro: sum.neutral)
done

lemma vector_eq_ldot: "(∀x. x ∙ y = x ∙ z) ⟷ y = z"
proof
assume "∀x. x ∙ y = x ∙ z"
then have "∀x. x ∙ (y - z) = 0"
then have "(y - z) ∙ (y - z) = 0" ..
then show "y = z" by simp
qed simp

lemma vector_eq_rdot: "(∀z. x ∙ z = y ∙ z) ⟷ x = y"
proof
assume "∀z. x ∙ z = y ∙ z"
then have "∀z. (x - y) ∙ z = 0"
then have "(x - y) ∙ (x - y) = 0" ..
then show "x = y" by simp
qed simp

subsection ‹Orthogonality.›

context real_inner
begin

definition "orthogonal x y ⟷ x ∙ y = 0"

lemma orthogonal_self: "orthogonal x x ⟷ x = 0"

lemma orthogonal_clauses:
"orthogonal a 0"
"orthogonal a x ⟹ orthogonal a (c *⇩R x)"
"orthogonal a x ⟹ orthogonal a (- x)"
"orthogonal a x ⟹ orthogonal a y ⟹ orthogonal a (x + y)"
"orthogonal a x ⟹ orthogonal a y ⟹ orthogonal a (x - y)"
"orthogonal 0 a"
"orthogonal x a ⟹ orthogonal (c *⇩R x) a"
"orthogonal x a ⟹ orthogonal (- x) a"
"orthogonal x a ⟹ orthogonal y a ⟹ orthogonal (x + y) a"
"orthogonal x a ⟹ orthogonal y a ⟹ orthogonal (x - y) a"
unfolding orthogonal_def inner_add inner_diff by auto

end

lemma orthogonal_commute: "orthogonal x y ⟷ orthogonal y x"

lemma orthogonal_scaleR [simp]: "c ≠ 0 ⟹ orthogonal (c *⇩R x) = orthogonal x"
by (rule ext) (simp add: orthogonal_def)

lemma pairwise_ortho_scaleR:
"pairwise (λi j. orthogonal (f i) (g j)) B
⟹ pairwise (λi j. orthogonal (a i *⇩R f i) (a j *⇩R g j)) B"
by (auto simp: pairwise_def orthogonal_clauses)

lemma orthogonal_rvsum:
"⟦finite s; ⋀y. y ∈ s ⟹ orthogonal x (f y)⟧ ⟹ orthogonal x (sum f s)"
by (induction s rule: finite_induct) (auto simp: orthogonal_clauses)

lemma orthogonal_lvsum:
"⟦finite s; ⋀x. x ∈ s ⟹ orthogonal (f x) y⟧ ⟹ orthogonal (sum f s) y"
by (induction s rule: finite_induct) (auto simp: orthogonal_clauses)

assumes "orthogonal a b"
shows "norm(a + b) ^ 2 = norm a ^ 2 + norm b ^ 2"
proof -
from assms have "(a - (0 - b)) ∙ (a - (0 - b)) = a ∙ a - (0 - b ∙ b)"
by (simp add: algebra_simps orthogonal_def inner_commute)
then show ?thesis
qed

lemma norm_sum_Pythagorean:
assumes "finite I" "pairwise (λi j. orthogonal (f i) (f j)) I"
shows "(norm (sum f I))⇧2 = (∑i∈I. (norm (f i))⇧2)"
using assms
proof (induction I rule: finite_induct)
case empty then show ?case by simp
next
case (insert x I)
then have "orthogonal (f x) (sum f I)"
by (metis pairwise_insert orthogonal_rvsum)
with insert show ?case
qed

subsection ‹Bilinear functions.›

definition "bilinear f ⟷ (∀x. linear (λy. f x y)) ∧ (∀y. linear (λx. f x y))"

lemma bilinear_ladd: "bilinear h ⟹ h (x + y) z = h x z + h y z"

lemma bilinear_radd: "bilinear h ⟹ h x (y + z) = h x y + h x z"

lemma bilinear_lmul: "bilinear h ⟹ h (c *⇩R x) y = c *⇩R h x y"

lemma bilinear_rmul: "bilinear h ⟹ h x (c *⇩R y) = c *⇩R h x y"

lemma bilinear_lneg: "bilinear h ⟹ h (- x) y = - h x y"
by (drule bilinear_lmul [of _ "- 1"]) simp

lemma bilinear_rneg: "bilinear h ⟹ h x (- y) = - h x y"
by (drule bilinear_rmul [of _ _ "- 1"]) simp

lemma (in ab_group_add) eq_add_iff: "x = x + y ⟷ y = 0"
using add_left_imp_eq[of x y 0] by auto

lemma bilinear_lzero:
assumes "bilinear h"
shows "h 0 x = 0"

lemma bilinear_rzero:
assumes "bilinear h"
shows "h x 0 = 0"

lemma bilinear_lsub: "bilinear h ⟹ h (x - y) z = h x z - h y z"

lemma bilinear_rsub: "bilinear h ⟹ h z (x - y) = h z x - h z y"
using bilinear_radd [of h _ x "- y"] by (simp add: bilinear_rneg)

lemma bilinear_sum:
assumes bh: "bilinear h"
and fS: "finite S"
and fT: "finite T"
shows "h (sum f S) (sum g T) = sum (λ(i,j). h (f i) (g j)) (S × T) "
proof -
have "h (sum f S) (sum g T) = sum (λx. h (f x) (sum g T)) S"
apply (rule linear_sum[unfolded o_def])
using bh fS
done
also have "… = sum (λx. sum (λy. h (f x) (g y)) T) S"
apply (rule sum.cong, simp)
apply (rule linear_sum[unfolded o_def])
using bh fT
done
finally show ?thesis
unfolding sum.cartesian_product .
qed

definition "adjoint f = (SOME f'. ∀x y. f x ∙ y = x ∙ f' y)"

assumes "∀x y. inner (f x) y = inner x (g y)"
proof (rule some_equality)
show "∀x y. inner (f x) y = inner x (g y)"
by (rule assms)
next
fix h
assume "∀x y. inner (f x) y = inner x (h y)"
then have "∀x y. inner x (g y) = inner x (h y)"
using assms by simp
then have "∀x y. inner x (g y - h y) = 0"
then have "∀y. inner (g y - h y) (g y - h y) = 0"
by simp
then have "∀y. h y = g y"
by simp
then show "h = g" by (simp add: ext)
qed

Hilbert space (i.e. complete inner product space).
›

fixes f :: "'n::euclidean_space ⇒ 'm::euclidean_space"
assumes lf: "linear f"
shows "x ∙ adjoint f y = f x ∙ y"
proof -
have "∀y. ∃w. ∀x. f x ∙ y = x ∙ w"
proof (intro allI exI)
fix y :: "'m" and x
let ?w = "(∑i∈Basis. (f i ∙ y) *⇩R i) :: 'n"
have "f x ∙ y = f (∑i∈Basis. (x ∙ i) *⇩R i) ∙ y"
also have "… = (∑i∈Basis. (x ∙ i) *⇩R f i) ∙ y"
unfolding linear_sum[OF lf]
finally show "f x ∙ y = x ∙ ?w"
by (simp add: inner_sum_left inner_sum_right mult.commute)
qed
then show ?thesis
by (intro someI2_ex[where Q="λf'. x ∙ f' y = f x ∙ y"]) auto
qed

fixes f :: "'n::euclidean_space ⇒ 'm::euclidean_space"
assumes lf: "linear f"
shows "x ∙ adjoint f y = f x ∙ y"
and "adjoint f y ∙ x = y ∙ f x"

fixes f :: "'n::euclidean_space ⇒ 'm::euclidean_space"
assumes lf: "linear f"
by (simp add: lf linear_iff euclidean_eq_iff[where 'a='n] euclidean_eq_iff[where 'a='m]

fixes f :: "'n::euclidean_space ⇒ 'm::euclidean_space"
assumes lf: "linear f"

subsection ‹Interlude: Some properties of real sets›

lemma seq_mono_lemma:
assumes "∀(n::nat) ≥ m. (d n :: real) < e n"
and "∀n ≥ m. e n ≤ e m"
shows "∀n ≥ m. d n < e m"
using assms
apply auto
apply (erule_tac x="n" in allE)
apply (erule_tac x="n" in allE)
apply auto
done

lemma infinite_enumerate:
assumes fS: "infinite S"
shows "∃r::nat⇒nat. strict_mono r ∧ (∀n. r n ∈ S)"
unfolding strict_mono_def
using enumerate_in_set[OF fS] enumerate_mono[of _ _ S] fS by auto

lemma approachable_lt_le: "(∃(d::real) > 0. ∀x. f x < d ⟶ P x) ⟷ (∃d>0. ∀x. f x ≤ d ⟶ P x)"
apply auto
apply (rule_tac x="d/2" in exI)
apply auto
done

lemma approachable_lt_le2:  ―‹like the above, but pushes aside an extra formula›
"(∃(d::real) > 0. ∀x. Q x ⟶ f x < d ⟶ P x) ⟷ (∃d>0. ∀x. f x ≤ d ⟶ Q x ⟶ P x)"
apply auto
apply (rule_tac x="d/2" in exI, auto)
done

lemma triangle_lemma:
fixes x y z :: real
assumes x: "0 ≤ x"
and y: "0 ≤ y"
and z: "0 ≤ z"
and xy: "x⇧2 ≤ y⇧2 + z⇧2"
shows "x ≤ y + z"
proof -
have "y⇧2 + z⇧2 ≤ y⇧2 + 2 * y * z + z⇧2"
using z y by simp
with xy have th: "x⇧2 ≤ (y + z)⇧2"
from y z have yz: "y + z ≥ 0"
by arith
from power2_le_imp_le[OF th yz] show ?thesis .
qed

subsection ‹Archimedean properties and useful consequences›

text‹Bernoulli's inequality›
proposition Bernoulli_inequality:
fixes x :: real
assumes "-1 ≤ x"
shows "1 + n * x ≤ (1 + x) ^ n"
proof (induct n)
case 0
then show ?case by simp
next
case (Suc n)
have "1 + Suc n * x ≤ 1 + (Suc n)*x + n * x^2"
also have "... = (1 + x) * (1 + n*x)"
by (auto simp: power2_eq_square algebra_simps  of_nat_Suc)
also have "... ≤ (1 + x) ^ Suc n"
using Suc.hyps assms mult_left_mono by fastforce
finally show ?case .
qed

corollary Bernoulli_inequality_even:
fixes x :: real
assumes "even n"
shows "1 + n * x ≤ (1 + x) ^ n"
proof (cases "-1 ≤ x ∨ n=0")
case True
then show ?thesis
by (auto simp: Bernoulli_inequality)
next
case False
then have "real n ≥ 1"
by simp
with False have "n * x ≤ -1"
by (metis linear minus_zero mult.commute mult.left_neutral mult_left_mono_neg neg_le_iff_le order_trans zero_le_one)
then have "1 + n * x ≤ 0"
by auto
also have "... ≤ (1 + x) ^ n"
using assms
using zero_le_even_power by blast
finally show ?thesis .
qed

corollary real_arch_pow:
fixes x :: real
assumes x: "1 < x"
shows "∃n. y < x^n"
proof -
from x have x0: "x - 1 > 0"
by arith
from reals_Archimedean3[OF x0, rule_format, of y]
obtain n :: nat where n: "y < real n * (x - 1)" by metis
from x0 have x00: "x- 1 ≥ -1" by arith
from Bernoulli_inequality[OF x00, of n] n
have "y < x^n" by auto
then show ?thesis by metis
qed

corollary real_arch_pow_inv:
fixes x y :: real
assumes y: "y > 0"
and x1: "x < 1"
shows "∃n. x^n < y"
proof (cases "x > 0")
case True
with x1 have ix: "1 < 1/x" by (simp add: field_simps)
from real_arch_pow[OF ix, of "1/y"]
obtain n where n: "1/y < (1/x)^n" by blast
then show ?thesis using y ‹x > 0›
next
case False
with y x1 show ?thesis
apply auto
apply (rule exI[where x=1])
apply auto
done
qed

lemma forall_pos_mono:
"(⋀d e::real. d < e ⟹ P d ⟹ P e) ⟹
(⋀n::nat. n ≠ 0 ⟹ P (inverse (real n))) ⟹ (⋀e. 0 < e ⟹ P e)"
by (metis real_arch_inverse)

lemma forall_pos_mono_1:
"(⋀d e::real. d < e ⟹ P d ⟹ P e) ⟹
(⋀n. P (inverse (real (Suc n)))) ⟹ 0 < e ⟹ P e"
apply (rule forall_pos_mono)
apply auto
apply (metis Suc_pred of_nat_Suc)
done

subsection ‹Euclidean Spaces as Typeclass›

lemma independent_Basis: "independent Basis"
unfolding dependent_def
apply (subst span_finite)
apply simp
apply clarify
apply (drule_tac f="inner a" in arg_cong)
apply (simp add: inner_Basis inner_sum_right eq_commute)
done

lemma span_Basis [simp]: "span Basis = UNIV"
unfolding span_finite [OF finite_Basis]
by (fast intro: euclidean_representation)

lemma in_span_Basis: "x ∈ span Basis"
unfolding span_Basis ..

lemma Basis_le_norm: "b ∈ Basis ⟹ ¦x ∙ b¦ ≤ norm x"
by (rule order_trans [OF Cauchy_Schwarz_ineq2]) simp

lemma norm_bound_Basis_le: "b ∈ Basis ⟹ norm x ≤ e ⟹ ¦x ∙ b¦ ≤ e"
by (metis Basis_le_norm order_trans)

lemma norm_bound_Basis_lt: "b ∈ Basis ⟹ norm x < e ⟹ ¦x ∙ b¦ < e"
by (metis Basis_le_norm le_less_trans)

lemma norm_le_l1: "norm x ≤ (∑b∈Basis. ¦x ∙ b¦)"
apply (subst euclidean_representation[of x, symmetric])
apply (rule order_trans[OF norm_sum])
apply (auto intro!: sum_mono)
done

lemma sum_norm_allsubsets_bound:
fixes f :: "'a ⇒ 'n::euclidean_space"
assumes fP: "finite P"
and fPs: "⋀Q. Q ⊆ P ⟹ norm (sum f Q) ≤ e"
shows "(∑x∈P. norm (f x)) ≤ 2 * real DIM('n) * e"
proof -
have "(∑x∈P. norm (f x)) ≤ (∑x∈P. ∑b∈Basis. ¦f x ∙ b¦)"
by (rule sum_mono) (rule norm_le_l1)
also have "(∑x∈P. ∑b∈Basis. ¦f x ∙ b¦) = (∑b∈Basis. ∑x∈P. ¦f x ∙ b¦)"
by (rule sum.commute)
also have "… ≤ of_nat (card (Basis :: 'n set)) * (2 * e)"
proof (rule sum_bounded_above)
fix i :: 'n
assume i: "i ∈ Basis"
have "norm (∑x∈P. ¦f x ∙ i¦) ≤
norm ((∑x∈P ∩ - {x. f x ∙ i < 0}. f x) ∙ i) + norm ((∑x∈P ∩ {x. f x ∙ i < 0}. f x) ∙ i)"
by (simp add: abs_real_def sum.If_cases[OF fP] sum_negf norm_triangle_ineq4 inner_sum_left
del: real_norm_def)
also have "… ≤ e + e"
unfolding real_norm_def
by (intro add_mono norm_bound_Basis_le i fPs) auto
finally show "(∑x∈P. ¦f x ∙ i¦) ≤ 2*e" by simp
qed
also have "… = 2 * real DIM('n) * e" by simp
finally show ?thesis .
qed

subsection ‹Linearity and Bilinearity continued›

lemma linear_bounded:
fixes f :: "'a::euclidean_space ⇒ 'b::real_normed_vector"
assumes lf: "linear f"
shows "∃B. ∀x. norm (f x) ≤ B * norm x"
proof
let ?B = "∑b∈Basis. norm (f b)"
show "∀x. norm (f x) ≤ ?B * norm x"
proof
fix x :: 'a
let ?g = "λb. (x ∙ b) *⇩R f b"
have "norm (f x) = norm (f (∑b∈Basis. (x ∙ b) *⇩R b))"
unfolding euclidean_representation ..
also have "… = norm (sum ?g Basis)"
by (simp add: linear_sum [OF lf] linear_cmul [OF lf])
finally have th0: "norm (f x) = norm (sum ?g Basis)" .
have th: "norm (?g i) ≤ norm (f i) * norm x" if "i ∈ Basis" for i
proof -
from Basis_le_norm[OF that, of x]
show "norm (?g i) ≤ norm (f i) * norm x"
unfolding norm_scaleR
apply (subst mult.commute)
apply (rule mult_mono)
done
qed
from sum_norm_le[of _ ?g, OF th]
show "norm (f x) ≤ ?B * norm x"
unfolding th0 sum_distrib_right by metis
qed
qed

lemma linear_conv_bounded_linear:
fixes f :: "'a::euclidean_space ⇒ 'b::real_normed_vector"
shows "linear f ⟷ bounded_linear f"
proof
assume "linear f"
then interpret f: linear f .
show "bounded_linear f"
proof
have "∃B. ∀x. norm (f x) ≤ B * norm x"
using ‹linear f› by (rule linear_bounded)
then show "∃K. ∀x. norm (f x) ≤ norm x * K"
qed
next
assume "bounded_linear f"
then interpret f: bounded_linear f .
show "linear f" ..
qed

lemmas linear_linear = linear_conv_bounded_linear[symmetric]

lemma linear_bounded_pos:
fixes f :: "'a::euclidean_space ⇒ 'b::real_normed_vector"
assumes lf: "linear f"
shows "∃B > 0. ∀x. norm (f x) ≤ B * norm x"
proof -
have "∃B > 0. ∀x. norm (f x) ≤ norm x * B"
using lf unfolding linear_conv_bounded_linear
by (rule bounded_linear.pos_bounded)
then show ?thesis
by (simp only: mult.commute)
qed

lemma bounded_linearI':
fixes f ::"'a::euclidean_space ⇒ 'b::real_normed_vector"
assumes "⋀x y. f (x + y) = f x + f y"
and "⋀c x. f (c *⇩R x) = c *⇩R f x"
shows "bounded_linear f"
unfolding linear_conv_bounded_linear[symmetric]
by (rule linearI[OF assms])

lemma bilinear_bounded:
fixes h :: "'m::euclidean_space ⇒ 'n::euclidean_space ⇒ 'k::real_normed_vector"
assumes bh: "bilinear h"
shows "∃B. ∀x y. norm (h x y) ≤ B * norm x * norm y"
proof (clarify intro!: exI[of _ "∑i∈Basis. ∑j∈Basis. norm (h i j)"])
fix x :: 'm
fix y :: 'n
have "norm (h x y) = norm (h (sum (λi. (x ∙ i) *⇩R i) Basis) (sum (λi. (y ∙ i) *⇩R i) Basis))"
apply (subst euclidean_representation[where 'a='m])
apply (subst euclidean_representation[where 'a='n])
apply rule
done
also have "… = norm (sum (λ (i,j). h ((x ∙ i) *⇩R i) ((y ∙ j) *⇩R j)) (Basis × Basis))"
unfolding bilinear_sum[OF bh finite_Basis finite_Basis] ..
finally have th: "norm (h x y) = …" .
show "norm (h x y) ≤ (∑i∈Basis. ∑j∈Basis. norm (h i j)) * norm x * norm y"
apply (auto simp add: sum_distrib_right th sum.cartesian_product)
apply (rule sum_norm_le)
apply (auto simp add: bilinear_rmul[OF bh] bilinear_lmul[OF bh]
field_simps simp del: scaleR_scaleR)
apply (rule mult_mono)
apply (auto simp add: zero_le_mult_iff Basis_le_norm)
apply (rule mult_mono)
apply (auto simp add: zero_le_mult_iff Basis_le_norm)
done
qed

lemma bilinear_conv_bounded_bilinear:
fixes h :: "'a::euclidean_space ⇒ 'b::euclidean_space ⇒ 'c::real_normed_vector"
shows "bilinear h ⟷ bounded_bilinear h"
proof
assume "bilinear h"
show "bounded_bilinear h"
proof
fix x y z
show "h (x + y) z = h x z + h y z"
using ‹bilinear h› unfolding bilinear_def linear_iff by simp
next
fix x y z
show "h x (y + z) = h x y + h x z"
using ‹bilinear h› unfolding bilinear_def linear_iff by simp
next
fix r x y
show "h (scaleR r x) y = scaleR r (h x y)"
using ‹bilinear h› unfolding bilinear_def linear_iff
by simp
next
fix r x y
show "h x (scaleR r y) = scaleR r (h x y)"
using ‹bilinear h› unfolding bilinear_def linear_iff
by simp
next
have "∃B. ∀x y. norm (h x y) ≤ B * norm x * norm y"
using ‹bilinear h› by (rule bilinear_bounded)
then show "∃K. ∀x y. norm (h x y) ≤ norm x * norm y * K"
qed
next
assume "bounded_bilinear h"
then interpret h: bounded_bilinear h .
show "bilinear h"
unfolding bilinear_def linear_conv_bounded_linear
using h.bounded_linear_left h.bounded_linear_right by simp
qed

lemma bilinear_bounded_pos:
fixes h :: "'a::euclidean_space ⇒ 'b::euclidean_space ⇒ 'c::real_normed_vector"
assumes bh: "bilinear h"
shows "∃B > 0. ∀x y. norm (h x y) ≤ B * norm x * norm y"
proof -
have "∃B > 0. ∀x y. norm (h x y) ≤ norm x * norm y * B"
using bh [unfolded bilinear_conv_bounded_bilinear]
by (rule bounded_bilinear.pos_bounded)
then show ?thesis
by (simp only: ac_simps)
qed

lemma bounded_linear_imp_has_derivative:
"bounded_linear f ⟹ (f has_derivative f) net"
by (simp add: has_derivative_def bounded_linear.linear linear_diff)

lemma linear_imp_has_derivative:
fixes f :: "'a::euclidean_space ⇒ 'b::real_normed_vector"
shows "linear f ⟹ (f has_derivative f) net"
by (simp add: has_derivative_def linear_conv_bounded_linear linear_diff)

lemma bounded_linear_imp_differentiable: "bounded_linear f ⟹ f differentiable net"
using bounded_linear_imp_has_derivative differentiable_def by blast

lemma linear_imp_differentiable:
fixes f :: "'a::euclidean_space ⇒ 'b::real_normed_vector"
shows "linear f ⟹ f differentiable net"
by (metis linear_imp_has_derivative differentiable_def)

subsection ‹We continue.›

lemma independent_bound:
fixes S :: "'a::euclidean_space set"
shows "independent S ⟹ finite S ∧ card S ≤ DIM('a)"
using independent_span_bound[OF finite_Basis, of S] by auto

corollary
fixes S :: "'a::euclidean_space set"
assumes "independent S"
shows independent_imp_finite: "finite S" and independent_card_le:"card S ≤ DIM('a)"
using assms independent_bound by auto

lemma independent_explicit:
fixes B :: "'a::euclidean_space set"
shows "independent B ⟷
finite B ∧ (∀c. (∑v∈B. c v *⇩R v) = 0 ⟶ (∀v ∈ B. c v = 0))"
apply (cases "finite B")
apply (force simp: dependent_finite)
using independent_bound
apply auto
done

lemma dependent_biggerset:
fixes S :: "'a::euclidean_space set"
shows "(finite S ⟹ card S > DIM('a)) ⟹ dependent S"
by (metis independent_bound not_less)

text ‹Notion of dimension.›

definition "dim V = (SOME n. ∃B. B ⊆ V ∧ independent B ∧ V ⊆ span B ∧ card B = n)"

lemma basis_exists:
"∃B. (B :: ('a::euclidean_space) set) ⊆ V ∧ independent B ∧ V ⊆ span B ∧ (card B = dim V)"
unfolding dim_def some_eq_ex[of "λn. ∃B. B ⊆ V ∧ independent B ∧ V ⊆ span B ∧ (card B = n)"]
using maximal_independent_subset[of V] independent_bound
by auto

corollary dim_le_card:
fixes s :: "'a::euclidean_space set"
shows "finite s ⟹ dim s ≤ card s"
by (metis basis_exists card_mono)

text ‹Consequences of independence or spanning for cardinality.›

lemma independent_card_le_dim:
fixes B :: "'a::euclidean_space set"
assumes "B ⊆ V"
and "independent B"
shows "card B ≤ dim V"
proof -
from basis_exists[of V] ‹B ⊆ V›
obtain B' where "independent B'"
and "B ⊆ span B'"
and "card B' = dim V"
by blast
with independent_span_bound[OF _ ‹independent B› ‹B ⊆ span B'›] independent_bound[of B']
show ?thesis by auto
qed

lemma span_card_ge_dim:
fixes B :: "'a::euclidean_space set"
shows "B ⊆ V ⟹ V ⊆ span B ⟹ finite B ⟹ dim V ≤ card B"
by (metis basis_exists[of V] independent_span_bound subset_trans)

lemma basis_card_eq_dim:
fixes V :: "'a::euclidean_space set"
shows "B ⊆ V ⟹ V ⊆ span B ⟹ independent B ⟹ finite B ∧ card B = dim V"
by (metis order_eq_iff independent_card_le_dim span_card_ge_dim independent_bound)

lemma dim_unique:
fixes B :: "'a::euclidean_space set"
shows "B ⊆ V ⟹ V ⊆ span B ⟹ independent B ⟹ card B = n ⟹ dim V = n"
by (metis basis_card_eq_dim)

lemma dim_UNIV [simp]: "dim (UNIV :: 'a::euclidean_space set) = DIM('a)"
using independent_Basis
by (intro dim_unique[of Basis]) auto

lemma dim_subset:
fixes S :: "'a::euclidean_space set"
shows "S ⊆ T ⟹ dim S ≤ dim T"
using basis_exists[of T] basis_exists[of S]
by (metis independent_card_le_dim subset_trans)

lemma dim_subset_UNIV:
fixes S :: "'a::euclidean_space set"
shows "dim S ≤ DIM('a)"
by (metis dim_subset subset_UNIV dim_UNIV)

text ‹Converses to those.›

lemma card_ge_dim_independent:
fixes B :: "'a::euclidean_space set"
assumes BV: "B ⊆ V"
and iB: "independent B"
and dVB: "dim V ≤ card B"
shows "V ⊆ span B"
proof
fix a
assume aV: "a ∈ V"
{
assume aB: "a ∉ span B"
then have iaB: "independent (insert a B)"
using iB aV BV by (simp add: independent_insert)
from aV BV have th0: "insert a B ⊆ V"
by blast
from aB have "a ∉B"
with independent_card_le_dim[OF th0 iaB] dVB independent_bound[OF iB]
have False by auto
}
then show "a ∈ span B" by blast
qed

lemma card_le_dim_spanning:
assumes BV: "(B:: ('a::euclidean_space) set) ⊆ V"
and VB: "V ⊆ span B"
and fB: "finite B"
and dVB: "dim V ≥ card B"
shows "independent B"
proof -
{
fix a
assume a: "a ∈ B" "a ∈ span (B - {a})"
from a fB have c0: "card B ≠ 0"
by auto
from a fB have cb: "card (B - {a}) = card B - 1"
by auto
from BV a have th0: "B - {a} ⊆ V"
by blast
{
fix x
assume x: "x ∈ V"
from a have eq: "insert a (B - {a}) = B"
by blast
from x VB have x': "x ∈ span B"
by blast
from span_trans[OF a(2), unfolded eq, OF x']
have "x ∈ span (B - {a})" .
}
then have th1: "V ⊆ span (B - {a})"
by blast
have th2: "finite (B - {a})"
using fB by auto
from span_card_ge_dim[OF th0 th1 th2]
have c: "dim V ≤ card (B - {a})" .
from c c0 dVB cb have False by simp
}
then show ?thesis
unfolding dependent_def by blast
qed

lemma card_eq_dim:
fixes B :: "'a::euclidean_space set"
shows "B ⊆ V ⟹ card B = dim V ⟹ finite B ⟹ independent B ⟷ V ⊆ span B"
by (metis order_eq_iff card_le_dim_spanning card_ge_dim_independent)

text ‹More general size bound lemmas.›

lemma independent_bound_general:
fixes S :: "'a::euclidean_space set"
shows "independent S ⟹ finite S ∧ card S ≤ dim S"
by (metis independent_card_le_dim independent_bound subset_refl)

lemma dependent_biggerset_general:
fixes S :: "'a::euclidean_space set"
shows "(finite S ⟹ card S > dim S) ⟹ dependent S"
using independent_bound_general[of S] by (metis linorder_not_le)

lemma dim_span [simp]:
fixes S :: "'a::euclidean_space set"
shows "dim (span S) = dim S"
proof -
have th0: "dim S ≤ dim (span S)"
by (auto simp add: subset_eq intro: dim_subset span_superset)
from basis_exists[of S]
obtain B where B: "B ⊆ S" "independent B" "S ⊆ span B" "card B = dim S"
by blast
from B have fB: "finite B" "card B = dim S"
using independent_bound by blast+
have bSS: "B ⊆ span S"
using B(1) by (metis subset_eq span_inc)
have sssB: "span S ⊆ span B"
using span_mono[OF B(3)] by (simp add: span_span)
from span_card_ge_dim[OF bSS sssB fB(1)] th0 show ?thesis
using fB(2) by arith
qed

lemma subset_le_dim:
fixes S :: "'a::euclidean_space set"
shows "S ⊆ span T ⟹ dim S ≤ dim T"
by (metis dim_span dim_subset)

lemma span_eq_dim:
fixes S :: "'a::euclidean_space set"
shows "span S = span T ⟹ dim S = dim T"
by (metis dim_span)

lemma dim_image_le:
fixes f :: "'a::euclidean_space ⇒ 'b::euclidean_space"
assumes lf: "linear f"
shows "dim (f ` S) ≤ dim (S)"
proof -
from basis_exists[of S] obtain B where
B: "B ⊆ S" "independent B" "S ⊆ span B" "card B = dim S" by blast
from B have fB: "finite B" "card B = dim S"
using independent_bound by blast+
have "dim (f ` S) ≤ card (f ` B)"
apply (rule span_card_ge_dim)
using lf B fB
apply (auto simp add: span_linear_image spans_image subset_image_iff)
done
also have "… ≤ dim S"
using card_image_le[OF fB(1)] fB by simp
finally show ?thesis .
qed

text ‹Picking an orthogonal replacement for a spanning set.›

lemma vector_sub_project_orthogonal:
fixes b x :: "'a::euclidean_space"
shows "b ∙ (x - ((b ∙ x) / (b ∙ b)) *⇩R b) = 0"
unfolding inner_simps by auto

lemma pairwise_orthogonal_insert:
assumes "pairwise orthogonal S"
and "⋀y. y ∈ S ⟹ orthogonal x y"
shows "pairwise orthogonal (insert x S)"
using assms unfolding pairwise_def

lemma basis_orthogonal:
fixes B :: "'a::real_inner set"
assumes fB: "finite B"
shows "∃C. finite C ∧ card C ≤ card B ∧ span C = span B ∧ pairwise orthogonal C"
(is " ∃C. ?P B C")
using fB
proof (induct rule: finite_induct)
case empty
then show ?case
apply (rule exI[where x="{}"])
done
next
case (insert a B)
note fB = ‹finite B› and aB = ‹a ∉ B›
from ‹∃C. finite C ∧ card C ≤ card B ∧ span C = span B ∧ pairwise orthogonal C›
obtain C where C: "finite C" "card C ≤ card B"
"span C = span B" "pairwise orthogonal C" by blast
let ?a = "a - sum (λx. (x ∙ a / (x ∙ x)) *⇩R x) C"
let ?C = "insert ?a C"
from C(1) have fC: "finite ?C"
by simp
from fB aB C(1,2) have cC: "card ?C ≤ card (insert a B)"
{
fix x k
have th0: "⋀(a::'a) b c. a - (b - c) = c + (a - b)"
have "x - k *⇩R (a - (∑x∈C. (x ∙ a / (x ∙ x)) *⇩R x)) ∈ span C ⟷ x - k *⇩R a ∈ span C"
apply (simp only: scaleR_right_diff_distrib th0)
apply (rule span_mul)
apply (rule span_sum)
apply (rule span_mul)
apply (rule span_superset)
apply assumption
done
}
then have SC: "span ?C = span (insert a B)"
unfolding set_eq_iff span_breakdown_eq C(3)[symmetric] by auto
{
fix y
assume yC: "y ∈ C"
then have Cy: "C = insert y (C - {y})"
by blast
have fth: "finite (C - {y})"
using C by simp
have "orthogonal ?a y"
unfolding orthogonal_def
unfolding inner_diff inner_sum_left right_minus_eq
unfolding sum.remove [OF ‹finite C› ‹y ∈ C›]
apply (clarsimp simp add: inner_commute[of y a])
apply (rule sum.neutral)
apply clarsimp
apply (rule C(4)[unfolded pairwise_def orthogonal_def, rule_format])
using ‹y ∈ C› by auto
}
with ‹pairwise orthogonal C› have CPO: "pairwise orthogonal ?C"
by (rule pairwise_orthogonal_insert)
from fC cC SC CPO have "?P (insert a B) ?C"
by blast
then show ?case by blast
qed

lemma orthogonal_basis_exists:
fixes V :: "('a::euclidean_space) set"
shows "∃B. independent B ∧ B ⊆ span V ∧ V ⊆ span B ∧ (card B = dim V) ∧ pairwise orthogonal B"
proof -
from basis_exists[of V] obtain B where
B: "B ⊆ V" "independent B" "V ⊆ span B" "card B = dim V"
by blast
from B have fB: "finite B" "card B = dim V"
using independent_bound by auto
from basis_orthogonal[OF fB(1)] obtain C where
C: "finite C" "card C ≤ card B" "span C = span B" "pairwise orthogonal C"
by blast
from C B have CSV: "C ⊆ span V"
by (metis span_inc span_mono subset_trans)
from span_mono[OF B(3)] C have SVC: "span V ⊆ span C"
from card_le_dim_spanning[OF CSV SVC C(1)] C(2,3) fB
have iC: "independent C"
from C fB have "card C ≤ dim V"
by simp
moreover have "dim V ≤ card C"
using span_card_ge_dim[OF CSV SVC C(1)]
ultimately have CdV: "card C = dim V"
using C(1) by simp
from C B CSV CdV iC show ?thesis
by auto
qed

text ‹Low-dimensional subset is in a hyperplane (weak orthogonal complement).›

lemma span_not_univ_orthogonal:
fixes S :: "'a::euclidean_space set"
assumes sU: "span S ≠ UNIV"
shows "∃a::'a. a ≠ 0 ∧ (∀x ∈ span S. a ∙ x = 0)"
proof -
from sU obtain a where a: "a ∉ span S"
by blast
from orthogonal_basis_exists obtain B where
B: "independent B" "B ⊆ span S" "S ⊆ span B" "card B = dim S" "pairwise orthogonal B"
by blast
from B have fB: "finite B" "card B = dim S"
using independent_bound by auto
from span_mono[OF B(2)] span_mono[OF B(3)]
have sSB: "span S = span B"
let ?a = "a - sum (λb. (a ∙ b / (b ∙ b)) *⇩R b) B"
have "sum (λb. (a ∙ b / (b ∙ b)) *⇩R b) B ∈ span S"
unfolding sSB
apply (rule span_sum)
apply (rule span_mul)
apply (rule span_superset)
apply assumption
done
with a have a0:"?a  ≠ 0"
by auto
have "∀x∈span B. ?a ∙ x = 0"
proof (rule span_induct')
show "subspace {x. ?a ∙ x = 0}"
next
{
fix x
assume x: "x ∈ B"
from x have B': "B = insert x (B - {x})"
by blast
have fth: "finite (B - {x})"
using fB by simp
have "?a ∙ x = 0"
apply (subst B')
using fB fth
unfolding sum_clauses(2)[OF fth]
apply simp unfolding inner_simps
apply (rule sum.neutral, rule ballI)
apply (simp only: inner_commute)
apply (auto simp add: x field_simps
intro: B(5)[unfolded pairwise_def orthogonal_def, rule_format])
done
}
then show "∀x ∈ B. ?a ∙ x = 0"
by blast
qed
with a0 show ?thesis
unfolding sSB by (auto intro: exI[where x="?a"])
qed

lemma span_not_univ_subset_hyperplane:
fixes S :: "'a::euclidean_space set"
assumes SU: "span S ≠ UNIV"
shows "∃ a. a ≠0 ∧ span S ⊆ {x. a ∙ x = 0}"
using span_not_univ_orthogonal[OF SU] by auto

lemma lowdim_subset_hyperplane:
fixes S :: "'a::euclidean_space set"
assumes d: "dim S < DIM('a)"
shows "∃a::'a. a ≠ 0 ∧ span S ⊆ {x. a ∙ x = 0}"
proof -
{
assume "span S = UNIV"
then have "dim (span S) = dim (UNIV :: ('a) set)"
by simp
then have "dim S = DIM('a)"
with d have False by arith
}
then have th: "span S ≠ UNIV"
by blast
from span_not_univ_subset_hyperplane[OF th] show ?thesis .
qed

text ‹We can extend a linear basis-basis injection to the whole set.›

lemma linear_indep_image_lemma:
assumes lf: "linear f"
and fB: "finite B"
and ifB: "independent (f ` B)"
and fi: "inj_on f B"
and xsB: "x ∈ span B"
and fx: "f x = 0"
shows "x = 0"
using fB ifB fi xsB fx
proof (induct arbitrary: x rule: finite_induct[OF fB])
case 1
then show ?case by auto
next
case (2 a b x)
have fb: "finite b" using "2.prems" by simp
have th0: "f ` b ⊆ f ` (insert a b)"
apply (rule image_mono)
apply blast
done
from independent_mono[ OF "2.prems"(2) th0]
have ifb: "independent (f ` b)"  .
have fib: "inj_on f b"
apply (rule subset_inj_on [OF "2.prems"(3)])
apply blast
done
from span_breakdown[of a "insert a b", simplified, OF "2.prems"(4)]
obtain k where k: "x - k*⇩R a ∈ span (b - {a})"
by blast
have "f (x - k*⇩R a) ∈ span (f ` b)"
unfolding span_linear_image[OF lf]
apply (rule imageI)
using k span_mono[of "b - {a}" b]
apply blast
done
then have "f x - k*⇩R f a ∈ span (f ` b)"
by (simp add: linear_diff[OF lf] linear_cmul[OF lf])
then have th: "-k *⇩R f a ∈ span (f ` b)"
using "2.prems"(5) by simp
have xsb: "x ∈ span b"
proof (cases "k = 0")
case True
with k have "x ∈ span (b - {a})" by simp
then show ?thesis using span_mono[of "b - {a}" b]
by blast
next
case False
with span_mul[OF th, of "- 1/ k"]
have th1: "f a ∈ span (f ` b)"
by auto
from inj_on_image_set_diff[OF "2.prems"(3), of "insert a b " "{a}", symmetric]
have tha: "f ` insert a b - f ` {a} = f ` (insert a b - {a})" by blast
from "2.prems"(2) [unfolded dependent_def bex_simps(8), rule_format, of "f a"]
have "f a ∉ span (f ` b)" using tha
using "2.hyps"(2)
"2.prems"(3) by auto
with th1 have False by blast
then show ?thesis by blast
qed
from "2.hyps"(3)[OF fb ifb fib xsb "2.prems"(5)] show "x = 0" .
qed

text ‹Can construct an isomorphism between spaces of same dimension.›

lemma subspace_isomorphism:
fixes S :: "'a::euclidean_space set"
and T :: "'b::euclidean_space set"
assumes s: "subspace S"
and t: "subspace T"
and d: "dim S = dim T"
shows "∃f. linear f ∧ f ` S = T ∧ inj_on f S"
proof -
from basis_exists[of S] independent_bound
obtain B where B: "B ⊆ S" "independent B" "S ⊆ span B" "card B = dim S" and fB: "finite B"
by blast
from basis_exists[of T] independent_bound
obtain C where C: "C ⊆ T" "independent C" "T ⊆ span C" "card C = dim T" and fC: "finite C"
by blast
from B(4) C(4) card_le_inj[of B C] d
obtain f where f: "f ` B ⊆ C" "inj_on f B" using ‹finite B› ‹finite C›
by auto
from linear_independent_extend[OF B(2)]
obtain g where g: "linear g" "∀x∈ B. g x = f x"
by blast
from inj_on_iff_eq_card[OF fB, of f] f(2) have "card (f ` B) = card B"
by simp
with B(4) C(4) have ceq: "card (f ` B) = card C"
using d by simp
have "g ` B = f ` B"
using g(2) by (auto simp add: image_iff)
also have "… = C" using card_subset_eq[OF fC f(1) ceq] .
finally have gBC: "g ` B = C" .
have gi: "inj_on g B"
using f(2) g(2) by (auto simp add: inj_on_def)
note g0 = linear_indep_image_lemma[OF g(1) fB, unfolded gBC, OF C(2) gi]
{
fix x y
assume x: "x ∈ S" and y: "y ∈ S" and gxy: "g x = g y"
from B(3) x y have x': "x ∈ span B" and y': "y ∈ span B"
by blast+
from gxy have th0: "g (x - y) = 0"
have th1: "x - y ∈ span B"
using x' y' by (metis span_diff)
have "x = y"
using g0[OF th1 th0] by simp
}
then have giS: "inj_on g S"
unfolding inj_on_def by blast
from span_subspace[OF B(1,3) s] have "g ` S = span (g ` B)"
also have "… = span C" unfolding gBC ..
also have "… = T" using span_subspace[OF C(1,3) t] .
finally have gS: "g ` S = T" .
from g(1) gS giS show ?thesis
by blast
qed

lemma linear_eq_stdbasis:
fixes f :: "'a::euclidean_space ⇒ _"
assumes lf: "linear f"
and lg: "linear g"
and fg: "∀b∈Basis. f b = g b"
shows "f = g"
using linear_eq[OF lf lg, of _ Basis] fg by auto

text ‹Similar results for bilinear functions.›

lemma bilinear_eq:
assumes bf: "bilinear f"
and bg: "bilinear g"
and SB: "S ⊆ span B"
and TC: "T ⊆ span C"
and fg: "∀x∈ B. ∀y∈ C. f x y = g x y"
shows "∀x∈S. ∀y∈T. f x y = g x y "
proof -
let ?P = "{x. ∀y∈ span C. f x y = g x y}"
from bf bg have sp: "subspace ?P"
unfolding bilinear_def linear_iff subspace_def bf bg

have "∀x ∈ span B. ∀y∈ span C. f x y = g x y"
apply (rule span_induct' [OF _ sp])
apply (rule ballI)
apply (rule span_induct')
using bf bg unfolding bilinear_def linear_iff
done
then show ?thesis
using SB TC by auto
qed

lemma bilinear_eq_stdbasis:
fixes f :: "'a::euclidean_space ⇒ 'b::euclidean_space ⇒ _"
assumes bf: "bilinear f"
and bg: "bilinear g"
and fg: "∀i∈Basis. ∀j∈Basis. f i j = g i j"
shows "f = g"
using bilinear_eq[OF bf bg equalityD2[OF span_Basis] equalityD2[OF span_Basis] fg] by blast

text ‹An injective map @{typ "'a::euclidean_space ⇒ 'b::euclidean_space"} is also surjective.›

lemma linear_injective_imp_surjective:
fixes f :: "'a::euclidean_space ⇒ 'a::euclidean_space"
assumes lf: "linear f"
and fi: "inj f"
shows "surj f"
proof -
let ?U = "UNIV :: 'a set"
from basis_exists[of ?U] obtain B
where B: "B ⊆ ?U" "independent B" "?U ⊆ span B" "card B = dim ?U"
by blast
from B(4) have d: "dim ?U = card B"
by simp
have th: "?U ⊆ span (f ` B)"
apply (rule card_ge_dim_independent)
apply blast
apply (rule independent_injective_image[OF B(2) lf fi])
apply (rule order_eq_refl)
apply (rule sym)
unfolding d
apply (rule card_image)
apply (rule subset_inj_on[OF fi])
apply blast
done
from th show ?thesis
unfolding span_linear_image[OF lf] surj_def
using B(3) by blast
qed

text ‹And vice versa.›

lemma surjective_iff_injective_gen:
assumes fS: "finite S"
and fT: "finite T"
and c: "card S = card T"
and ST: "f ` S ⊆ T"
shows "(∀y ∈ T. ∃x ∈ S. f x = y) ⟷ inj_on f S"
(is "?lhs ⟷ ?rhs")
proof
assume h: "?lhs"
{
fix x y
assume x: "x ∈ S"
assume y: "y ∈ S"
assume f: "f x = f y"
from x fS have S0: "card S ≠ 0"
by auto
have "x = y"
proof (rule ccontr)
assume xy: "¬ ?thesis"
have th: "card S ≤ card (f ` (S - {y}))"
unfolding c
apply (rule card_mono)
apply (rule finite_imageI)
using fS apply simp
using h xy x y f unfolding subset_eq image_iff
apply auto
apply (case_tac "xa = f x")
apply (rule bexI[where x=x])
apply auto
done
also have " … ≤ card (S - {y})"
apply (rule card_image_le)
using fS by simp
also have "… ≤ card S - 1" using y fS by simp
finally show False using S0 by arith
qed
}
then show ?rhs
unfolding inj_on_def by blast
next
assume h: ?rhs
have "f ` S = T"
apply (rule card_subset_eq[OF fT ST])
unfolding card_image[OF h]
apply (rule c)
done
then show ?lhs by blast
qed

lemma linear_surjective_imp_injective:
fixes f :: "'a::euclidean_space ⇒ 'a::euclidean_space"
assumes lf: "linear f"
and sf: "surj f"
shows "inj f"
proof -
let ?U = "UNIV :: 'a set"
from basis_exists[of ?U] obtain B
where B: "B ⊆ ?U" "independent B" "?U ⊆ span B" and d: "card B = dim ?U"
by blast
{
fix x
assume x: "x ∈ span B"
assume fx: "f x = 0"
from B(2) have fB: "finite B"
using independent_bound by auto
have fBi: "independent (f ` B)"
apply (rule card_le_dim_spanning[of "f ` B" ?U])
apply blast
using sf B(3)
unfolding span_linear_image[OF lf] surj_def subset_eq image_iff
apply blast
using fB apply blast
unfolding d[symmetric]
apply (rule card_image_le)
apply (rule fB)
done
have th0: "dim ?U ≤ card (f ` B)"
apply (rule span_card_ge_dim)
apply blast
unfolding span_linear_image[OF lf]
apply (rule subset_trans[where B = "f ` UNIV"])
using sf unfolding surj_def
apply blast
apply (rule image_mono)
apply (rule B(3))
apply (metis finite_imageI fB)
done
moreover have "card (f ` B) ≤ card B"
by (rule card_image_le, rule fB)
ultimately have th1: "card B = card (f ` B)"
unfolding d by arith
have fiB: "inj_on f B"
unfolding surjective_iff_injective_gen[OF fB finite_imageI[OF fB] th1 subset_refl, symmetric]
by blast
from linear_indep_image_lemma[OF lf fB fBi fiB x] fx
have "x = 0" by blast
}
then show ?thesis
unfolding linear_injective_0[OF lf]
using B(3)
by blast
qed

text ‹Hence either is enough for isomorphism.›

lemma left_right_inverse_eq:
assumes fg: "f ∘ g = id"
and gh: "g ∘ h = id"
shows "f = h"
proof -
have "f = f ∘ (g ∘ h)"
unfolding gh by simp
also have "… = (f ∘ g) ∘ h"
finally show "f = h"
unfolding fg by simp
qed

lemma isomorphism_expand:
"f ∘ g = id ∧ g ∘ f = id ⟷ (∀x. f (g x) = x) ∧ (∀x. g (f x) = x)"
by (simp add: fun_eq_iff o_def id_def)

lemma linear_injective_isomorphism:
fixes f :: "'a::euclidean_space ⇒ 'a::euclidean_space"
assumes lf: "linear f"
and fi: "inj f"
shows "∃f'. linear f' ∧ (∀x. f' (f x) = x) ∧ (∀x. f (f' x) = x)"
unfolding isomorphism_expand[symmetric]
using linear_surjective_right_inverse[OF lf linear_injective_imp_surjective[OF lf fi]]
linear_injective_left_inverse[OF lf fi]
by (metis left_right_inverse_eq)

lemma linear_surjective_isomorphism:
fixes f :: "'a::euclidean_space ⇒ 'a::euclidean_space"
assumes lf: "linear f"
and sf: "surj f"
shows "∃f'. linear f' ∧ (∀x. f' (f x) = x) ∧ (∀x. f (f' x) = x)"
unfolding isomorphism_expand[symmetric]
using linear_surjective_right_inverse[OF lf sf]
linear_injective_left_inverse[OF lf linear_surjective_imp_injective[OF lf sf]]
by (metis left_right_inverse_eq)

text ‹Left and right inverses are the same for
@{typ "'a::euclidean_space ⇒ 'a::euclidean_space"}.›

lemma linear_inverse_left:
fixes f :: "'a::euclidean_space ⇒ 'a::euclidean_space"
assumes lf: "linear f"
and lf': "linear f'"
shows "f ∘ f' = id ⟷ f' ∘ f = id"
proof -
{
fix f f':: "'a ⇒ 'a"
assume lf: "linear f" "linear f'"
assume f: "f ∘ f' = id"
from f have sf: "surj f"
apply (auto simp add: o_def id_def surj_def)
apply metis
done
from linear_surjective_isomorphism[OF lf(1) sf] lf f
have "f' ∘ f = id"
unfolding fun_eq_iff o_def id_def by metis
}
then show ?thesis
using lf lf' by metis
qed

text ‹Moreover, a one-sided inverse is automatically linear.›

lemma left_inverse_linear:
fixes f :: "'a::euclidean_space ⇒ 'a::euclidean_space"
assumes lf: "linear f"
and gf: "g ∘ f = id"
shows "linear g"
proof -
from gf have fi: "inj f"
apply (auto simp add: inj_on_def o_def id_def fun_eq_iff)
apply metis
done
from linear_injective_isomorphism[OF lf fi]
obtain h :: "'a ⇒ 'a" where h: "linear h" "∀x. h (f x) = x" "∀x. f (h x) = x"
by blast
have "h = g"
apply (rule ext) using gf h(2,3)
apply (simp add: o_def id_def fun_eq_iff)
apply metis
done
with h(1) show ?thesis by blast
qed

lemma inj_linear_imp_inv_linear:
fixes f :: "'a::euclidean_space ⇒ 'a::euclidean_space"
assumes "linear f" "inj f" shows "linear (inv f)"
using assms inj_iff left_inverse_linear by blast

subsection ‹Infinity norm›

definition "infnorm (x::'a::euclidean_space) = Sup {¦x ∙ b¦ |b. b ∈ Basis}"

lemma infnorm_set_image:
fixes x :: "'a::euclidean_space"
shows "{¦x ∙ i¦ |i. i ∈ Basis} = (λi. ¦x ∙ i¦) ` Basis"
by blast

lemma infnorm_Max:
fixes x :: "'a::euclidean_space"
shows "infnorm x = Max ((λi. ¦x ∙ i¦) ` Basis)"
by (simp add: infnorm_def infnorm_set_image cSup_eq_Max)

lemma infnorm_set_lemma:
fixes x :: "'a::euclidean_space"
shows "finite {¦x ∙ i¦ |i. i ∈ Basis}"
and "{¦x ∙ i¦ |i. i ∈ Basis} ≠ {}"
unfolding infnorm_set_image
by auto

lemma infnorm_pos_le:
fixes x :: "'a::euclidean_space"
shows "0 ≤ infnorm x"
by (simp add: infnorm_Max Max_ge_iff ex_in_conv)

lemma infnorm_triangle:
fixes x :: "'a::euclidean_space"
shows "infnorm (x + y) ≤ infnorm x + infnorm y"
proof -
have *: "⋀a b c d :: real. ¦a¦ ≤ c ⟹ ¦b¦ ≤ d ⟹ ¦a + b¦ ≤ c + d"
by simp
show ?thesis
by (auto simp: infnorm_Max inner_add_left intro!: *)
qed

lemma infnorm_eq_0:
fixes x :: "'a::euclidean_space"
shows "infnorm x = 0 ⟷ x = 0"
proof -
have "infnorm x ≤ 0 ⟷ x = 0"
unfolding infnorm_Max by (simp add: euclidean_all_zero_iff)
then show ?thesis
using infnorm_pos_le[of x] by simp
qed

lemma infnorm_0: "infnorm 0 = 0"

lemma infnorm_neg: "infnorm (- x) = infnorm x"
unfolding infnorm_def
apply (rule cong[of "Sup" "Sup"])
apply blast
apply auto
done

lemma infnorm_sub: "infnorm (x - y) = infnorm (y - x)"
proof -
have "y - x = - (x - y)" by simp
then show ?thesis
by (metis infnorm_neg)
qed

lemma real_abs_sub_infnorm: "¦infnorm x - infnorm y¦ ≤ infnorm (x - y)"
proof -
have th: "⋀(nx::real) n ny. nx ≤ n + ny ⟹ ny ≤ n + nx ⟹ ¦nx - ny¦ ≤ n"
by arith
from infnorm_triangle[of "x - y" " y"] infnorm_triangle[of "x - y" "-x"]
have ths: "infnorm x ≤ infnorm (x - y) + infnorm y"
"infnorm y ≤ infnorm (x - y) + infnorm x"
from th[OF ths] show ?thesis .
qed

lemma real_abs_infnorm: "¦infnorm x¦ = infnorm x"
using infnorm_pos_le[of x] by arith

lemma Basis_le_infnorm:
fixes x :: "'a::euclidean_space"
shows "b ∈ Basis ⟹ ¦x ∙ b¦ ≤ infnorm x"

lemma infnorm_mul: "infnorm (a *⇩R x) = ¦a¦ * infnorm x"
unfolding infnorm_Max
proof (safe intro!: Max_eqI)
let ?B = "(λi. ¦x ∙ i¦) ` Basis"
{
fix b :: 'a
assume "b ∈ Basis"
then show "¦a *⇩R x ∙ b¦ ≤ ¦a¦ * Max ?B"
next
from Max_in[of ?B] obtain b where "b ∈ Basis" "Max ?B = ¦x ∙ b¦"
by (auto simp del: Max_in)
then show "¦a¦ * Max ((λi. ¦x ∙ i¦) ` Basis) ∈ (λi. ¦a *⇩R x ∙ i¦) ` Basis"
by (intro image_eqI[where x=b]) (auto simp: abs_mult)
}
qed simp

lemma infnorm_mul_lemma: "infnorm (a *⇩R x) ≤ ¦a¦ * infnorm x"
unfolding infnorm_mul ..

lemma infnorm_pos_lt: "infnorm x > 0 ⟷ x ≠ 0"
using infnorm_pos_le[of x] infnorm_eq_0[of x] by arith

text ‹Prove that it differs only up to a bound from Euclidean norm.›

lemma infnorm_le_norm: "infnorm x ≤ norm x"

lemma (in euclidean_space) euclidean_inner: "inner x y = (∑b∈Basis. (x ∙ b) * (y ∙ b))"
by (subst (1 2) euclidean_representation [symmetric])

lemma norm_le_infnorm:
fixes x :: "'a::euclidean_space"
shows "norm x ≤ sqrt DIM('a) * infnorm x"
proof -
let ?d = "DIM('a)"
have "real ?d ≥ 0"
by simp
then have d2: "(sqrt (real ?d))⇧2 = real ?d"
by (auto intro: real_sqrt_pow2)
have th: "sqrt (real ?d) * infnorm x ≥ 0"
have th1: "x ∙ x ≤ (sqrt (real ?d) * infnorm x)⇧2"
unfolding power_mult_distrib d2
apply (subst euclidean_inner)
apply (subst power2_abs[symmetric])
apply (rule order_trans[OF sum_bounded_above[where K="¦infnorm x¦⇧2"]])
apply (subst power2_abs[symmetric])
apply (rule power_mono)
apply (auto simp: infnorm_Max)
done
from real_le_lsqrt[OF inner_ge_zero th th1]
show ?thesis
unfolding norm_eq_sqrt_inner id_def .
qed

lemma tendsto_infnorm [tendsto_intros]:
assumes "(f ⤏ a) F"
shows "((λx. infnorm (f x)) ⤏ infnorm a) F"
proof (rule tendsto_compose [OF LIM_I assms])
fix r :: real
assume "r > 0"
then show "∃s>0. ∀x. x ≠ a ∧ norm (x - a) < s ⟶ norm (infnorm x - infnorm a) < r"
by (metis real_norm_def le_less_trans real_abs_sub_infnorm infnorm_le_norm)
qed

text ‹Equality in Cauchy-Schwarz and triangle inequalities.›

lemma norm_cauchy_schwarz_eq: "x ∙ y = norm x * norm y ⟷ norm x *⇩R y = norm y *⇩R x"
(is "?lhs ⟷ ?rhs")
proof -
{
assume h: "x = 0"
then have ?thesis by simp
}
moreover
{
assume h: "y = 0"
then have ?thesis by simp
}
moreover
{
assume x: "x ≠ 0" and y: "y ≠ 0"
from inner_eq_zero_iff[of "norm y *⇩R x - norm x *⇩R y"]
have "?rhs ⟷
(norm y * (norm y * norm x * norm x - norm x * (x ∙ y)) -
norm x * (norm y * (y ∙ x) - norm x * norm y * norm y) =  0)"
using x y
unfolding inner_simps
unfolding power2_norm_eq_inner[symmetric] power2_eq_square right_minus_eq
apply metis
done
also have "… ⟷ (2 * norm x * norm y * (norm x * norm y - x ∙ y) = 0)" using x y
also have "… ⟷ ?lhs" using x y
apply simp
apply metis
done
finally have ?thesis by blast
}
ultimately show ?thesis by blast
qed

lemma norm_cauchy_schwarz_abs_eq:
"¦x ∙ y¦ = norm x * norm y ⟷
norm x *⇩R y = norm y *⇩R x ∨ norm x *⇩R y = - norm y *⇩R x"
(is "?lhs ⟷ ?rhs")
proof -
have th: "⋀(x::real) a. a ≥ 0 ⟹ ¦x¦ = a ⟷ x = a ∨ x = - a"
by arith
have "?rhs ⟷ norm x *⇩R y = norm y *⇩R x ∨ norm (- x) *⇩R y = norm y *⇩R (- x)"
by simp
also have "… ⟷(x ∙ y = norm x * norm y ∨ (- x) ∙ y = norm x * norm y)"
unfolding norm_cauchy_schwarz_eq[symmetric]
unfolding norm_minus_cancel norm_scaleR ..
also have "… ⟷ ?lhs"
unfolding th[OF mult_nonneg_nonneg, OF norm_ge_zero[of x] norm_ge_zero[of y]] inner_simps
by auto
finally show ?thesis ..
qed

lemma norm_triangle_eq:
fixes x y :: "'a::real_inner"
shows "norm (x + y) = norm x + norm y ⟷ norm x *⇩R y = norm y *⇩R x"
proof -
{
assume x: "x = 0 ∨ y = 0"
then have ?thesis
by (cases "x = 0") simp_all
}
moreover
{
assume x: "x ≠ 0" and y: "y ≠ 0"
then have "norm x ≠ 0" "norm y ≠ 0"
by simp_all
then have n: "norm x > 0" "norm y > 0"
using norm_ge_zero[of x] norm_ge_zero[of y] by arith+
have th: "⋀(a::real) b c. a + b + c ≠ 0 ⟹ a = b + c ⟷ a⇧2 = (b + c)⇧2"
by algebra
have "norm (x + y) = norm x + norm y ⟷ (norm (x + y))⇧2 = (norm x + norm y)⇧2"
apply (rule th)
using n norm_ge_zero[of "x + y"]
apply arith
done
also have "… ⟷ norm x *⇩R y = norm y *⇩R x"
unfolding norm_cauchy_schwarz_eq[symmetric]
unfolding power2_norm_eq_inner inner_simps
by (simp add: power2_norm_eq_inner[symmetric] power2_eq_square inner_commute field_simps)
finally have ?thesis .
}
ultimately show ?thesis by blast
qed

subsection ‹Collinearity›

definition collinear :: "'a::real_vector set ⇒ bool"
where "collinear S ⟷ (∃u. ∀x ∈ S. ∀ y ∈ S. ∃c. x - y = c *⇩R u)"

lemma collinear_alt:
"collinear S ⟷ (∃u v. ∀x ∈ S. ∃c. x = u + c *⇩R v)" (is "?lhs = ?rhs")
proof
assume ?lhs
then show ?rhs
next
assume ?rhs
then obtain u v where *: "⋀x. x ∈ S ⟹ ∃c. x = u + c *⇩R v"
by (auto simp: )
have "∃c. x - y = c *⇩R v" if "x ∈ S" "y ∈ S" for x y
by (metis *[OF ‹x ∈ S›] *[OF ‹y ∈ S›] scaleR_left.diff add_diff_cancel_left)
then show ?lhs
using collinear_def by blast
qed

lemma collinear:
fixes S :: "'a::{perfect_space,real_vector} set"
shows "collinear S ⟷ (∃u. u ≠ 0 ∧ (∀x ∈ S. ∀ y ∈ S. ∃c. x - y = c *⇩R u))"
proof -
have "∃v. v ≠ 0 ∧ (∀x∈S. ∀y∈S. ∃c. x - y = c *⇩R v)"
if "∀x∈S. ∀y∈S. ∃c. x - y = c *⇩R u" "u=0" for u
proof -
have "∀x∈S. ∀y∈S. x = y"
using that by auto
moreover
obtain v::'a where "v ≠ 0"
using UNIV_not_singleton [of 0] by auto
ultimately have "∀x∈S. ∀y∈S. ∃c. x - y = c *⇩R v"
by auto
then show ?thesis
using ‹v ≠ 0› by blast
qed
then show ?thesis
apply (clarsimp simp: collinear_def)
by (metis real_vector.scale_zero_right vector_fraction_eq_iff)
qed

lemma collinear_subset: "⟦collinear T; S ⊆ T⟧ ⟹ collinear S"
by (meson collinear_def subsetCE)

lemma collinear_empty [iff]: "collinear {}"

lemma collinear_sing [iff]: "collinear {x}"

lemma collinear_2 [iff]: "collinear {x, y}"
apply (rule exI[where x="x - y"])
apply auto
apply (rule exI[where x=1], simp)
apply (rule exI[where x="- 1"], simp)
done

lemma collinear_lemma: "collinear {0, x, y} ⟷ x = 0 ∨ y = 0 ∨ (∃c. y = c *⇩R x)"
(is "?lhs ⟷ ?rhs")
proof -
{
assume "x = 0 ∨ y = 0"
then have ?thesis
by (cases "x = 0") (simp_all add: collinear_2 insert_commute)
}
moreover
{
assume x: "x ≠ 0" and y: "y ≠ 0"
have ?thesis
proof
assume h: "?lhs"
then obtain u where u: "∀ x∈ {0,x,y}. ∀y∈ {0,x,y}. ∃c. x - y = c *⇩R u"
unfolding collinear_def by blast
from u[rule_format, of x 0] u[rule_format, of y 0]
obtain cx and cy where
cx: "x = cx *⇩R u" and cy: "y = cy *⇩R u"
by auto
from cx x have cx0: "cx ≠ 0" by auto
from cy y have cy0: "cy ≠ 0" by auto
let ?d = "cy / cx"
from cx cy cx0 have "y = ?d *⇩R x"
by simp
then show ?rhs using x y by blast
next
assume h: "?rhs"
then obtain c where c: "y = c *⇩R x"
using x y by blast
show ?lhs
unfolding collinear_def c
apply (rule exI[where x=x])
apply auto
apply (rule exI[where x="- 1"], simp)
apply (rule exI[where x= "-c"], simp)
apply (rule exI[where x=1], simp)
apply (rule exI[where x="1 - c"], simp add: scaleR_left_diff_distrib)
apply (rule exI[where x="c - 1"], simp add: scaleR_left_diff_distrib)
done
qed
}
ultimately show ?thesis by blast
qed

lemma norm_cauchy_schwarz_equal: "¦x ∙ y¦ = norm x * norm y ⟷ collinear {0, x, y}"
unfolding norm_cauchy_schwarz_abs_eq
apply (cases "x=0", simp_all)
apply (cases "y=0", simp_all add: insert_commute)
unfolding collinear_lemma
apply simp
apply (subgoal_tac "norm x ≠ 0")
apply (subgoal_tac "norm y ≠ 0")
apply (rule iffI)
apply (cases "norm x *⇩R y = norm y *⇩R x")
apply (rule exI[where x="(1/norm x) * norm y"])
apply (drule sym)
unfolding scaleR_scaleR[symmetric]
apply (rule exI[where x="(1/norm x) * - norm y"])
apply clarify
apply (drule sym)
unfolding scaleR_scaleR[symmetric]