src/HOL/Analysis/Linear_Algebra.thy
author nipkow
Fri Dec 28 10:29:59 2018 +0100 (4 months ago)
changeset 69517 dc20f278e8f3
parent 69516 09bb8f470959
child 69597 ff784d5a5bfb
permissions -rw-r--r--
tuned style and headers
hoelzl@63627
     1
(*  Title:      HOL/Analysis/Linear_Algebra.thy
huffman@44133
     2
    Author:     Amine Chaieb, University of Cambridge
huffman@44133
     3
*)
huffman@44133
     4
nipkow@69517
     5
section \<open>Elementary Linear Algebra on Euclidean Spaces\<close>
huffman@44133
     6
huffman@44133
     7
theory Linear_Algebra
huffman@44133
     8
imports
huffman@44133
     9
  Euclidean_Space
wenzelm@66453
    10
  "HOL-Library.Infinite_Set"
huffman@44133
    11
begin
huffman@44133
    12
hoelzl@63886
    13
lemma linear_simps:
hoelzl@63886
    14
  assumes "bounded_linear f"
hoelzl@63886
    15
  shows
hoelzl@63886
    16
    "f (a + b) = f a + f b"
hoelzl@63886
    17
    "f (a - b) = f a - f b"
hoelzl@63886
    18
    "f 0 = 0"
hoelzl@63886
    19
    "f (- a) = - f a"
hoelzl@63886
    20
    "f (s *\<^sub>R v) = s *\<^sub>R (f v)"
hoelzl@63886
    21
proof -
hoelzl@63886
    22
  interpret f: bounded_linear f by fact
hoelzl@63886
    23
  show "f (a + b) = f a + f b" by (rule f.add)
hoelzl@63886
    24
  show "f (a - b) = f a - f b" by (rule f.diff)
hoelzl@63886
    25
  show "f 0 = 0" by (rule f.zero)
immler@68072
    26
  show "f (- a) = - f a" by (rule f.neg)
immler@68072
    27
  show "f (s *\<^sub>R v) = s *\<^sub>R (f v)" by (rule f.scale)
huffman@44133
    28
qed
huffman@44133
    29
lp15@68069
    30
lemma finite_Atleast_Atmost_nat[simp]: "finite {f x |x. x \<in> (UNIV::'a::finite set)}"
lp15@68069
    31
  using finite finite_image_set by blast
huffman@44133
    32
wenzelm@53406
    33
nipkow@68901
    34
subsection%unimportant \<open>More interesting properties of the norm\<close>
hoelzl@63050
    35
hoelzl@63050
    36
notation inner (infix "\<bullet>" 70)
hoelzl@63050
    37
nipkow@67399
    38
text\<open>Equality of vectors in terms of @{term "(\<bullet>)"} products.\<close>
hoelzl@63050
    39
hoelzl@63050
    40
lemma linear_componentwise:
hoelzl@63050
    41
  fixes f:: "'a::euclidean_space \<Rightarrow> 'b::real_inner"
hoelzl@63050
    42
  assumes lf: "linear f"
hoelzl@63050
    43
  shows "(f x) \<bullet> j = (\<Sum>i\<in>Basis. (x\<bullet>i) * (f i\<bullet>j))" (is "?lhs = ?rhs")
hoelzl@63050
    44
proof -
immler@68072
    45
  interpret linear f by fact
hoelzl@63050
    46
  have "?rhs = (\<Sum>i\<in>Basis. (x\<bullet>i) *\<^sub>R (f i))\<bullet>j"
nipkow@64267
    47
    by (simp add: inner_sum_left)
hoelzl@63050
    48
  then show ?thesis
immler@68072
    49
    by (simp add: euclidean_representation sum[symmetric] scale[symmetric])
hoelzl@63050
    50
qed
hoelzl@63050
    51
hoelzl@63050
    52
lemma vector_eq: "x = y \<longleftrightarrow> x \<bullet> x = x \<bullet> y \<and> y \<bullet> y = x \<bullet> x"
hoelzl@63050
    53
  (is "?lhs \<longleftrightarrow> ?rhs")
hoelzl@63050
    54
proof
hoelzl@63050
    55
  assume ?lhs
hoelzl@63050
    56
  then show ?rhs by simp
hoelzl@63050
    57
next
hoelzl@63050
    58
  assume ?rhs
hoelzl@63050
    59
  then have "x \<bullet> x - x \<bullet> y = 0 \<and> x \<bullet> y - y \<bullet> y = 0"
hoelzl@63050
    60
    by simp
hoelzl@63050
    61
  then have "x \<bullet> (x - y) = 0 \<and> y \<bullet> (x - y) = 0"
hoelzl@63050
    62
    by (simp add: inner_diff inner_commute)
hoelzl@63050
    63
  then have "(x - y) \<bullet> (x - y) = 0"
hoelzl@63050
    64
    by (simp add: field_simps inner_diff inner_commute)
hoelzl@63050
    65
  then show "x = y" by simp
hoelzl@63050
    66
qed
hoelzl@63050
    67
hoelzl@63050
    68
lemma norm_triangle_half_r:
hoelzl@63050
    69
  "norm (y - x1) < e / 2 \<Longrightarrow> norm (y - x2) < e / 2 \<Longrightarrow> norm (x1 - x2) < e"
hoelzl@63050
    70
  using dist_triangle_half_r unfolding dist_norm[symmetric] by auto
hoelzl@63050
    71
hoelzl@63050
    72
lemma norm_triangle_half_l:
hoelzl@63050
    73
  assumes "norm (x - y) < e / 2"
hoelzl@63050
    74
    and "norm (x' - y) < e / 2"
hoelzl@63050
    75
  shows "norm (x - x') < e"
hoelzl@63050
    76
  using dist_triangle_half_l[OF assms[unfolded dist_norm[symmetric]]]
hoelzl@63050
    77
  unfolding dist_norm[symmetric] .
hoelzl@63050
    78
lp15@66420
    79
lemma abs_triangle_half_r:
lp15@66420
    80
  fixes y :: "'a::linordered_field"
lp15@66420
    81
  shows "abs (y - x1) < e / 2 \<Longrightarrow> abs (y - x2) < e / 2 \<Longrightarrow> abs (x1 - x2) < e"
lp15@66420
    82
  by linarith
lp15@66420
    83
lp15@66420
    84
lemma abs_triangle_half_l:
lp15@66420
    85
  fixes y :: "'a::linordered_field"
lp15@66420
    86
  assumes "abs (x - y) < e / 2"
lp15@66420
    87
    and "abs (x' - y) < e / 2"
lp15@66420
    88
  shows "abs (x - x') < e"
lp15@66420
    89
  using assms by linarith
lp15@66420
    90
nipkow@64267
    91
lemma sum_clauses:
nipkow@64267
    92
  shows "sum f {} = 0"
nipkow@64267
    93
    and "finite S \<Longrightarrow> sum f (insert x S) = (if x \<in> S then sum f S else f x + sum f S)"
hoelzl@63050
    94
  by (auto simp add: insert_absorb)
hoelzl@63050
    95
hoelzl@63050
    96
lemma vector_eq_ldot: "(\<forall>x. x \<bullet> y = x \<bullet> z) \<longleftrightarrow> y = z"
hoelzl@63050
    97
proof
hoelzl@63050
    98
  assume "\<forall>x. x \<bullet> y = x \<bullet> z"
hoelzl@63050
    99
  then have "\<forall>x. x \<bullet> (y - z) = 0"
hoelzl@63050
   100
    by (simp add: inner_diff)
hoelzl@63050
   101
  then have "(y - z) \<bullet> (y - z) = 0" ..
hoelzl@63050
   102
  then show "y = z" by simp
hoelzl@63050
   103
qed simp
hoelzl@63050
   104
hoelzl@63050
   105
lemma vector_eq_rdot: "(\<forall>z. x \<bullet> z = y \<bullet> z) \<longleftrightarrow> x = y"
hoelzl@63050
   106
proof
hoelzl@63050
   107
  assume "\<forall>z. x \<bullet> z = y \<bullet> z"
hoelzl@63050
   108
  then have "\<forall>z. (x - y) \<bullet> z = 0"
hoelzl@63050
   109
    by (simp add: inner_diff)
hoelzl@63050
   110
  then have "(x - y) \<bullet> (x - y) = 0" ..
hoelzl@63050
   111
  then show "x = y" by simp
hoelzl@63050
   112
qed simp
hoelzl@63050
   113
hoelzl@63050
   114
nipkow@68901
   115
subsection \<open>Orthogonality\<close>
hoelzl@63050
   116
immler@67962
   117
definition%important (in real_inner) "orthogonal x y \<longleftrightarrow> x \<bullet> y = 0"
immler@67962
   118
hoelzl@63050
   119
context real_inner
hoelzl@63050
   120
begin
hoelzl@63050
   121
lp15@63072
   122
lemma orthogonal_self: "orthogonal x x \<longleftrightarrow> x = 0"
lp15@63072
   123
  by (simp add: orthogonal_def)
lp15@63072
   124
hoelzl@63050
   125
lemma orthogonal_clauses:
hoelzl@63050
   126
  "orthogonal a 0"
hoelzl@63050
   127
  "orthogonal a x \<Longrightarrow> orthogonal a (c *\<^sub>R x)"
hoelzl@63050
   128
  "orthogonal a x \<Longrightarrow> orthogonal a (- x)"
hoelzl@63050
   129
  "orthogonal a x \<Longrightarrow> orthogonal a y \<Longrightarrow> orthogonal a (x + y)"
hoelzl@63050
   130
  "orthogonal a x \<Longrightarrow> orthogonal a y \<Longrightarrow> orthogonal a (x - y)"
hoelzl@63050
   131
  "orthogonal 0 a"
hoelzl@63050
   132
  "orthogonal x a \<Longrightarrow> orthogonal (c *\<^sub>R x) a"
hoelzl@63050
   133
  "orthogonal x a \<Longrightarrow> orthogonal (- x) a"
hoelzl@63050
   134
  "orthogonal x a \<Longrightarrow> orthogonal y a \<Longrightarrow> orthogonal (x + y) a"
hoelzl@63050
   135
  "orthogonal x a \<Longrightarrow> orthogonal y a \<Longrightarrow> orthogonal (x - y) a"
hoelzl@63050
   136
  unfolding orthogonal_def inner_add inner_diff by auto
hoelzl@63050
   137
hoelzl@63050
   138
end
hoelzl@63050
   139
hoelzl@63050
   140
lemma orthogonal_commute: "orthogonal x y \<longleftrightarrow> orthogonal y x"
hoelzl@63050
   141
  by (simp add: orthogonal_def inner_commute)
hoelzl@63050
   142
lp15@63114
   143
lemma orthogonal_scaleR [simp]: "c \<noteq> 0 \<Longrightarrow> orthogonal (c *\<^sub>R x) = orthogonal x"
lp15@63114
   144
  by (rule ext) (simp add: orthogonal_def)
lp15@63114
   145
lp15@63114
   146
lemma pairwise_ortho_scaleR:
lp15@63114
   147
    "pairwise (\<lambda>i j. orthogonal (f i) (g j)) B
lp15@63114
   148
    \<Longrightarrow> pairwise (\<lambda>i j. orthogonal (a i *\<^sub>R f i) (a j *\<^sub>R g j)) B"
lp15@63114
   149
  by (auto simp: pairwise_def orthogonal_clauses)
lp15@63114
   150
lp15@63114
   151
lemma orthogonal_rvsum:
nipkow@64267
   152
    "\<lbrakk>finite s; \<And>y. y \<in> s \<Longrightarrow> orthogonal x (f y)\<rbrakk> \<Longrightarrow> orthogonal x (sum f s)"
lp15@63114
   153
  by (induction s rule: finite_induct) (auto simp: orthogonal_clauses)
lp15@63114
   154
lp15@63114
   155
lemma orthogonal_lvsum:
nipkow@64267
   156
    "\<lbrakk>finite s; \<And>x. x \<in> s \<Longrightarrow> orthogonal (f x) y\<rbrakk> \<Longrightarrow> orthogonal (sum f s) y"
lp15@63114
   157
  by (induction s rule: finite_induct) (auto simp: orthogonal_clauses)
lp15@63114
   158
lp15@63114
   159
lemma norm_add_Pythagorean:
lp15@63114
   160
  assumes "orthogonal a b"
lp15@63114
   161
    shows "norm(a + b) ^ 2 = norm a ^ 2 + norm b ^ 2"
lp15@63114
   162
proof -
lp15@63114
   163
  from assms have "(a - (0 - b)) \<bullet> (a - (0 - b)) = a \<bullet> a - (0 - b \<bullet> b)"
lp15@63114
   164
    by (simp add: algebra_simps orthogonal_def inner_commute)
lp15@63114
   165
  then show ?thesis
lp15@63114
   166
    by (simp add: power2_norm_eq_inner)
lp15@63114
   167
qed
lp15@63114
   168
nipkow@64267
   169
lemma norm_sum_Pythagorean:
lp15@63114
   170
  assumes "finite I" "pairwise (\<lambda>i j. orthogonal (f i) (f j)) I"
nipkow@64267
   171
    shows "(norm (sum f I))\<^sup>2 = (\<Sum>i\<in>I. (norm (f i))\<^sup>2)"
lp15@63114
   172
using assms
lp15@63114
   173
proof (induction I rule: finite_induct)
lp15@63114
   174
  case empty then show ?case by simp
lp15@63114
   175
next
lp15@63114
   176
  case (insert x I)
nipkow@64267
   177
  then have "orthogonal (f x) (sum f I)"
lp15@63114
   178
    by (metis pairwise_insert orthogonal_rvsum)
lp15@63114
   179
  with insert show ?case
lp15@63114
   180
    by (simp add: pairwise_insert norm_add_Pythagorean)
lp15@63114
   181
qed
lp15@63114
   182
hoelzl@63050
   183
nipkow@68901
   184
subsection \<open>Bilinear functions\<close>
hoelzl@63050
   185
immler@67962
   186
definition%important "bilinear f \<longleftrightarrow> (\<forall>x. linear (\<lambda>y. f x y)) \<and> (\<forall>y. linear (\<lambda>x. f x y))"
hoelzl@63050
   187
hoelzl@63050
   188
lemma bilinear_ladd: "bilinear h \<Longrightarrow> h (x + y) z = h x z + h y z"
hoelzl@63050
   189
  by (simp add: bilinear_def linear_iff)
hoelzl@63050
   190
hoelzl@63050
   191
lemma bilinear_radd: "bilinear h \<Longrightarrow> h x (y + z) = h x y + h x z"
hoelzl@63050
   192
  by (simp add: bilinear_def linear_iff)
hoelzl@63050
   193
hoelzl@63050
   194
lemma bilinear_lmul: "bilinear h \<Longrightarrow> h (c *\<^sub>R x) y = c *\<^sub>R h x y"
hoelzl@63050
   195
  by (simp add: bilinear_def linear_iff)
hoelzl@63050
   196
hoelzl@63050
   197
lemma bilinear_rmul: "bilinear h \<Longrightarrow> h x (c *\<^sub>R y) = c *\<^sub>R h x y"
hoelzl@63050
   198
  by (simp add: bilinear_def linear_iff)
hoelzl@63050
   199
hoelzl@63050
   200
lemma bilinear_lneg: "bilinear h \<Longrightarrow> h (- x) y = - h x y"
hoelzl@63050
   201
  by (drule bilinear_lmul [of _ "- 1"]) simp
hoelzl@63050
   202
hoelzl@63050
   203
lemma bilinear_rneg: "bilinear h \<Longrightarrow> h x (- y) = - h x y"
hoelzl@63050
   204
  by (drule bilinear_rmul [of _ _ "- 1"]) simp
hoelzl@63050
   205
hoelzl@63050
   206
lemma (in ab_group_add) eq_add_iff: "x = x + y \<longleftrightarrow> y = 0"
hoelzl@63050
   207
  using add_left_imp_eq[of x y 0] by auto
hoelzl@63050
   208
hoelzl@63050
   209
lemma bilinear_lzero:
hoelzl@63050
   210
  assumes "bilinear h"
hoelzl@63050
   211
  shows "h 0 x = 0"
hoelzl@63050
   212
  using bilinear_ladd [OF assms, of 0 0 x] by (simp add: eq_add_iff field_simps)
hoelzl@63050
   213
hoelzl@63050
   214
lemma bilinear_rzero:
hoelzl@63050
   215
  assumes "bilinear h"
hoelzl@63050
   216
  shows "h x 0 = 0"
hoelzl@63050
   217
  using bilinear_radd [OF assms, of x 0 0 ] by (simp add: eq_add_iff field_simps)
hoelzl@63050
   218
hoelzl@63050
   219
lemma bilinear_lsub: "bilinear h \<Longrightarrow> h (x - y) z = h x z - h y z"
hoelzl@63050
   220
  using bilinear_ladd [of h x "- y"] by (simp add: bilinear_lneg)
hoelzl@63050
   221
hoelzl@63050
   222
lemma bilinear_rsub: "bilinear h \<Longrightarrow> h z (x - y) = h z x - h z y"
hoelzl@63050
   223
  using bilinear_radd [of h _ x "- y"] by (simp add: bilinear_rneg)
hoelzl@63050
   224
nipkow@64267
   225
lemma bilinear_sum:
immler@68072
   226
  assumes "bilinear h"
nipkow@64267
   227
  shows "h (sum f S) (sum g T) = sum (\<lambda>(i,j). h (f i) (g j)) (S \<times> T) "
hoelzl@63050
   228
proof -
immler@68072
   229
  interpret l: linear "\<lambda>x. h x y" for y using assms by (simp add: bilinear_def)
immler@68072
   230
  interpret r: linear "\<lambda>y. h x y" for x using assms by (simp add: bilinear_def)
nipkow@64267
   231
  have "h (sum f S) (sum g T) = sum (\<lambda>x. h (f x) (sum g T)) S"
immler@68072
   232
    by (simp add: l.sum)
nipkow@64267
   233
  also have "\<dots> = sum (\<lambda>x. sum (\<lambda>y. h (f x) (g y)) T) S"
immler@68072
   234
    by (rule sum.cong) (simp_all add: r.sum)
hoelzl@63050
   235
  finally show ?thesis
nipkow@64267
   236
    unfolding sum.cartesian_product .
hoelzl@63050
   237
qed
hoelzl@63050
   238
hoelzl@63050
   239
nipkow@68901
   240
subsection \<open>Adjoints\<close>
hoelzl@63050
   241
immler@67962
   242
definition%important "adjoint f = (SOME f'. \<forall>x y. f x \<bullet> y = x \<bullet> f' y)"
hoelzl@63050
   243
hoelzl@63050
   244
lemma adjoint_unique:
hoelzl@63050
   245
  assumes "\<forall>x y. inner (f x) y = inner x (g y)"
hoelzl@63050
   246
  shows "adjoint f = g"
hoelzl@63050
   247
  unfolding adjoint_def
hoelzl@63050
   248
proof (rule some_equality)
hoelzl@63050
   249
  show "\<forall>x y. inner (f x) y = inner x (g y)"
hoelzl@63050
   250
    by (rule assms)
hoelzl@63050
   251
next
hoelzl@63050
   252
  fix h
hoelzl@63050
   253
  assume "\<forall>x y. inner (f x) y = inner x (h y)"
hoelzl@63050
   254
  then have "\<forall>x y. inner x (g y) = inner x (h y)"
hoelzl@63050
   255
    using assms by simp
hoelzl@63050
   256
  then have "\<forall>x y. inner x (g y - h y) = 0"
hoelzl@63050
   257
    by (simp add: inner_diff_right)
hoelzl@63050
   258
  then have "\<forall>y. inner (g y - h y) (g y - h y) = 0"
hoelzl@63050
   259
    by simp
hoelzl@63050
   260
  then have "\<forall>y. h y = g y"
hoelzl@63050
   261
    by simp
hoelzl@63050
   262
  then show "h = g" by (simp add: ext)
hoelzl@63050
   263
qed
hoelzl@63050
   264
hoelzl@63050
   265
text \<open>TODO: The following lemmas about adjoints should hold for any
wenzelm@63680
   266
  Hilbert space (i.e. complete inner product space).
wenzelm@68224
   267
  (see \<^url>\<open>https://en.wikipedia.org/wiki/Hermitian_adjoint\<close>)
hoelzl@63050
   268
\<close>
hoelzl@63050
   269
hoelzl@63050
   270
lemma adjoint_works:
hoelzl@63050
   271
  fixes f :: "'n::euclidean_space \<Rightarrow> 'm::euclidean_space"
hoelzl@63050
   272
  assumes lf: "linear f"
hoelzl@63050
   273
  shows "x \<bullet> adjoint f y = f x \<bullet> y"
hoelzl@63050
   274
proof -
immler@68072
   275
  interpret linear f by fact
hoelzl@63050
   276
  have "\<forall>y. \<exists>w. \<forall>x. f x \<bullet> y = x \<bullet> w"
hoelzl@63050
   277
  proof (intro allI exI)
hoelzl@63050
   278
    fix y :: "'m" and x
hoelzl@63050
   279
    let ?w = "(\<Sum>i\<in>Basis. (f i \<bullet> y) *\<^sub>R i) :: 'n"
hoelzl@63050
   280
    have "f x \<bullet> y = f (\<Sum>i\<in>Basis. (x \<bullet> i) *\<^sub>R i) \<bullet> y"
hoelzl@63050
   281
      by (simp add: euclidean_representation)
hoelzl@63050
   282
    also have "\<dots> = (\<Sum>i\<in>Basis. (x \<bullet> i) *\<^sub>R f i) \<bullet> y"
immler@68072
   283
      by (simp add: sum scale)
hoelzl@63050
   284
    finally show "f x \<bullet> y = x \<bullet> ?w"
nipkow@64267
   285
      by (simp add: inner_sum_left inner_sum_right mult.commute)
hoelzl@63050
   286
  qed
hoelzl@63050
   287
  then show ?thesis
hoelzl@63050
   288
    unfolding adjoint_def choice_iff
hoelzl@63050
   289
    by (intro someI2_ex[where Q="\<lambda>f'. x \<bullet> f' y = f x \<bullet> y"]) auto
hoelzl@63050
   290
qed
hoelzl@63050
   291
hoelzl@63050
   292
lemma adjoint_clauses:
hoelzl@63050
   293
  fixes f :: "'n::euclidean_space \<Rightarrow> 'm::euclidean_space"
hoelzl@63050
   294
  assumes lf: "linear f"
hoelzl@63050
   295
  shows "x \<bullet> adjoint f y = f x \<bullet> y"
hoelzl@63050
   296
    and "adjoint f y \<bullet> x = y \<bullet> f x"
hoelzl@63050
   297
  by (simp_all add: adjoint_works[OF lf] inner_commute)
hoelzl@63050
   298
hoelzl@63050
   299
lemma adjoint_linear:
hoelzl@63050
   300
  fixes f :: "'n::euclidean_space \<Rightarrow> 'm::euclidean_space"
hoelzl@63050
   301
  assumes lf: "linear f"
hoelzl@63050
   302
  shows "linear (adjoint f)"
hoelzl@63050
   303
  by (simp add: lf linear_iff euclidean_eq_iff[where 'a='n] euclidean_eq_iff[where 'a='m]
hoelzl@63050
   304
    adjoint_clauses[OF lf] inner_distrib)
hoelzl@63050
   305
hoelzl@63050
   306
lemma adjoint_adjoint:
hoelzl@63050
   307
  fixes f :: "'n::euclidean_space \<Rightarrow> 'm::euclidean_space"
hoelzl@63050
   308
  assumes lf: "linear f"
hoelzl@63050
   309
  shows "adjoint (adjoint f) = f"
hoelzl@63050
   310
  by (rule adjoint_unique, simp add: adjoint_clauses [OF lf])
hoelzl@63050
   311
hoelzl@63050
   312
hoelzl@63050
   313
subsection \<open>Archimedean properties and useful consequences\<close>
hoelzl@63050
   314
hoelzl@63050
   315
text\<open>Bernoulli's inequality\<close>
immler@68607
   316
proposition Bernoulli_inequality:
hoelzl@63050
   317
  fixes x :: real
hoelzl@63050
   318
  assumes "-1 \<le> x"
hoelzl@63050
   319
    shows "1 + n * x \<le> (1 + x) ^ n"
immler@68607
   320
proof (induct n)
hoelzl@63050
   321
  case 0
hoelzl@63050
   322
  then show ?case by simp
hoelzl@63050
   323
next
hoelzl@63050
   324
  case (Suc n)
hoelzl@63050
   325
  have "1 + Suc n * x \<le> 1 + (Suc n)*x + n * x^2"
hoelzl@63050
   326
    by (simp add: algebra_simps)
hoelzl@63050
   327
  also have "... = (1 + x) * (1 + n*x)"
hoelzl@63050
   328
    by (auto simp: power2_eq_square algebra_simps  of_nat_Suc)
hoelzl@63050
   329
  also have "... \<le> (1 + x) ^ Suc n"
hoelzl@63050
   330
    using Suc.hyps assms mult_left_mono by fastforce
hoelzl@63050
   331
  finally show ?case .
hoelzl@63050
   332
qed
hoelzl@63050
   333
hoelzl@63050
   334
corollary Bernoulli_inequality_even:
hoelzl@63050
   335
  fixes x :: real
hoelzl@63050
   336
  assumes "even n"
hoelzl@63050
   337
    shows "1 + n * x \<le> (1 + x) ^ n"
hoelzl@63050
   338
proof (cases "-1 \<le> x \<or> n=0")
hoelzl@63050
   339
  case True
hoelzl@63050
   340
  then show ?thesis
hoelzl@63050
   341
    by (auto simp: Bernoulli_inequality)
hoelzl@63050
   342
next
hoelzl@63050
   343
  case False
hoelzl@63050
   344
  then have "real n \<ge> 1"
hoelzl@63050
   345
    by simp
hoelzl@63050
   346
  with False have "n * x \<le> -1"
hoelzl@63050
   347
    by (metis linear minus_zero mult.commute mult.left_neutral mult_left_mono_neg neg_le_iff_le order_trans zero_le_one)
hoelzl@63050
   348
  then have "1 + n * x \<le> 0"
hoelzl@63050
   349
    by auto
hoelzl@63050
   350
  also have "... \<le> (1 + x) ^ n"
hoelzl@63050
   351
    using assms
hoelzl@63050
   352
    using zero_le_even_power by blast
hoelzl@63050
   353
  finally show ?thesis .
hoelzl@63050
   354
qed
hoelzl@63050
   355
hoelzl@63050
   356
corollary real_arch_pow:
hoelzl@63050
   357
  fixes x :: real
hoelzl@63050
   358
  assumes x: "1 < x"
hoelzl@63050
   359
  shows "\<exists>n. y < x^n"
hoelzl@63050
   360
proof -
hoelzl@63050
   361
  from x have x0: "x - 1 > 0"
hoelzl@63050
   362
    by arith
hoelzl@63050
   363
  from reals_Archimedean3[OF x0, rule_format, of y]
hoelzl@63050
   364
  obtain n :: nat where n: "y < real n * (x - 1)" by metis
hoelzl@63050
   365
  from x0 have x00: "x- 1 \<ge> -1" by arith
hoelzl@63050
   366
  from Bernoulli_inequality[OF x00, of n] n
hoelzl@63050
   367
  have "y < x^n" by auto
hoelzl@63050
   368
  then show ?thesis by metis
hoelzl@63050
   369
qed
hoelzl@63050
   370
hoelzl@63050
   371
corollary real_arch_pow_inv:
hoelzl@63050
   372
  fixes x y :: real
hoelzl@63050
   373
  assumes y: "y > 0"
hoelzl@63050
   374
    and x1: "x < 1"
hoelzl@63050
   375
  shows "\<exists>n. x^n < y"
hoelzl@63050
   376
proof (cases "x > 0")
hoelzl@63050
   377
  case True
hoelzl@63050
   378
  with x1 have ix: "1 < 1/x" by (simp add: field_simps)
hoelzl@63050
   379
  from real_arch_pow[OF ix, of "1/y"]
hoelzl@63050
   380
  obtain n where n: "1/y < (1/x)^n" by blast
hoelzl@63050
   381
  then show ?thesis using y \<open>x > 0\<close>
hoelzl@63050
   382
    by (auto simp add: field_simps)
hoelzl@63050
   383
next
hoelzl@63050
   384
  case False
hoelzl@63050
   385
  with y x1 show ?thesis
lp15@68069
   386
    by (metis less_le_trans not_less power_one_right)
hoelzl@63050
   387
qed
hoelzl@63050
   388
hoelzl@63050
   389
lemma forall_pos_mono:
hoelzl@63050
   390
  "(\<And>d e::real. d < e \<Longrightarrow> P d \<Longrightarrow> P e) \<Longrightarrow>
hoelzl@63050
   391
    (\<And>n::nat. n \<noteq> 0 \<Longrightarrow> P (inverse (real n))) \<Longrightarrow> (\<And>e. 0 < e \<Longrightarrow> P e)"
hoelzl@63050
   392
  by (metis real_arch_inverse)
hoelzl@63050
   393
hoelzl@63050
   394
lemma forall_pos_mono_1:
hoelzl@63050
   395
  "(\<And>d e::real. d < e \<Longrightarrow> P d \<Longrightarrow> P e) \<Longrightarrow>
hoelzl@63050
   396
    (\<And>n. P (inverse (real (Suc n)))) \<Longrightarrow> 0 < e \<Longrightarrow> P e"
hoelzl@63050
   397
  apply (rule forall_pos_mono)
hoelzl@63050
   398
  apply auto
hoelzl@63050
   399
  apply (metis Suc_pred of_nat_Suc)
hoelzl@63050
   400
  done
hoelzl@63050
   401
hoelzl@63050
   402
immler@67962
   403
subsection%unimportant \<open>Euclidean Spaces as Typeclass\<close>
huffman@44133
   404
hoelzl@50526
   405
lemma independent_Basis: "independent Basis"
immler@68072
   406
  by (rule independent_Basis)
hoelzl@50526
   407
huffman@53939
   408
lemma span_Basis [simp]: "span Basis = UNIV"
immler@68072
   409
  by (rule span_Basis)
huffman@44133
   410
hoelzl@50526
   411
lemma in_span_Basis: "x \<in> span Basis"
hoelzl@50526
   412
  unfolding span_Basis ..
hoelzl@50526
   413
wenzelm@53406
   414
immler@67962
   415
subsection%unimportant \<open>Linearity and Bilinearity continued\<close>
huffman@44133
   416
huffman@44133
   417
lemma linear_bounded:
wenzelm@56444
   418
  fixes f :: "'a::euclidean_space \<Rightarrow> 'b::real_normed_vector"
huffman@44133
   419
  assumes lf: "linear f"
huffman@44133
   420
  shows "\<exists>B. \<forall>x. norm (f x) \<le> B * norm x"
huffman@53939
   421
proof
immler@68072
   422
  interpret linear f by fact
hoelzl@50526
   423
  let ?B = "\<Sum>b\<in>Basis. norm (f b)"
huffman@53939
   424
  show "\<forall>x. norm (f x) \<le> ?B * norm x"
huffman@53939
   425
  proof
wenzelm@53406
   426
    fix x :: 'a
hoelzl@50526
   427
    let ?g = "\<lambda>b. (x \<bullet> b) *\<^sub>R f b"
hoelzl@50526
   428
    have "norm (f x) = norm (f (\<Sum>b\<in>Basis. (x \<bullet> b) *\<^sub>R b))"
hoelzl@50526
   429
      unfolding euclidean_representation ..
nipkow@64267
   430
    also have "\<dots> = norm (sum ?g Basis)"
immler@68072
   431
      by (simp add: sum scale)
nipkow@64267
   432
    finally have th0: "norm (f x) = norm (sum ?g Basis)" .
lp15@64773
   433
    have th: "norm (?g i) \<le> norm (f i) * norm x" if "i \<in> Basis" for i
lp15@64773
   434
    proof -
lp15@64773
   435
      from Basis_le_norm[OF that, of x]
huffman@53939
   436
      show "norm (?g i) \<le> norm (f i) * norm x"
lp15@68069
   437
        unfolding norm_scaleR  by (metis mult.commute mult_left_mono norm_ge_zero)
huffman@53939
   438
    qed
nipkow@64267
   439
    from sum_norm_le[of _ ?g, OF th]
huffman@53939
   440
    show "norm (f x) \<le> ?B * norm x"
nipkow@64267
   441
      unfolding th0 sum_distrib_right by metis
huffman@53939
   442
  qed
huffman@44133
   443
qed
huffman@44133
   444
huffman@44133
   445
lemma linear_conv_bounded_linear:
huffman@44133
   446
  fixes f :: "'a::euclidean_space \<Rightarrow> 'b::real_normed_vector"
huffman@44133
   447
  shows "linear f \<longleftrightarrow> bounded_linear f"
huffman@44133
   448
proof
huffman@44133
   449
  assume "linear f"
huffman@53939
   450
  then interpret f: linear f .
huffman@44133
   451
  show "bounded_linear f"
huffman@44133
   452
  proof
huffman@44133
   453
    have "\<exists>B. \<forall>x. norm (f x) \<le> B * norm x"
wenzelm@60420
   454
      using \<open>linear f\<close> by (rule linear_bounded)
wenzelm@49522
   455
    then show "\<exists>K. \<forall>x. norm (f x) \<le> norm x * K"
haftmann@57512
   456
      by (simp add: mult.commute)
huffman@44133
   457
  qed
huffman@44133
   458
next
huffman@44133
   459
  assume "bounded_linear f"
huffman@44133
   460
  then interpret f: bounded_linear f .
huffman@53939
   461
  show "linear f" ..
huffman@53939
   462
qed
huffman@53939
   463
paulson@61518
   464
lemmas linear_linear = linear_conv_bounded_linear[symmetric]
paulson@61518
   465
huffman@53939
   466
lemma linear_bounded_pos:
wenzelm@56444
   467
  fixes f :: "'a::euclidean_space \<Rightarrow> 'b::real_normed_vector"
huffman@53939
   468
  assumes lf: "linear f"
lp15@67982
   469
 obtains B where "B > 0" "\<And>x. norm (f x) \<le> B * norm x"
huffman@53939
   470
proof -
huffman@53939
   471
  have "\<exists>B > 0. \<forall>x. norm (f x) \<le> norm x * B"
huffman@53939
   472
    using lf unfolding linear_conv_bounded_linear
huffman@53939
   473
    by (rule bounded_linear.pos_bounded)
lp15@67982
   474
  with that show ?thesis
lp15@67982
   475
    by (auto simp: mult.commute)
huffman@44133
   476
qed
huffman@44133
   477
lp15@67982
   478
lemma linear_invertible_bounded_below_pos:
lp15@67982
   479
  fixes f :: "'a::real_normed_vector \<Rightarrow> 'b::euclidean_space"
lp15@67982
   480
  assumes "linear f" "linear g" "g \<circ> f = id"
lp15@67982
   481
  obtains B where "B > 0" "\<And>x. B * norm x \<le> norm(f x)"
lp15@67982
   482
proof -
lp15@67982
   483
  obtain B where "B > 0" and B: "\<And>x. norm (g x) \<le> B * norm x"
lp15@67982
   484
    using linear_bounded_pos [OF \<open>linear g\<close>] by blast
lp15@67982
   485
  show thesis
lp15@67982
   486
  proof
lp15@67982
   487
    show "0 < 1/B"
lp15@67982
   488
      by (simp add: \<open>B > 0\<close>)
lp15@67982
   489
    show "1/B * norm x \<le> norm (f x)" for x
lp15@67982
   490
    proof -
lp15@67982
   491
      have "1/B * norm x = 1/B * norm (g (f x))"
lp15@67982
   492
        using assms by (simp add: pointfree_idE)
lp15@67982
   493
      also have "\<dots> \<le> norm (f x)"
lp15@67982
   494
        using B [of "f x"] by (simp add: \<open>B > 0\<close> mult.commute pos_divide_le_eq)
lp15@67982
   495
      finally show ?thesis .
lp15@67982
   496
    qed
lp15@67982
   497
  qed
lp15@67982
   498
qed
lp15@67982
   499
lp15@67982
   500
lemma linear_inj_bounded_below_pos:
lp15@67982
   501
  fixes f :: "'a::real_normed_vector \<Rightarrow> 'b::euclidean_space"
lp15@67982
   502
  assumes "linear f" "inj f"
lp15@67982
   503
  obtains B where "B > 0" "\<And>x. B * norm x \<le> norm(f x)"
immler@68072
   504
  using linear_injective_left_inverse [OF assms]
immler@68072
   505
    linear_invertible_bounded_below_pos assms by blast
lp15@67982
   506
wenzelm@49522
   507
lemma bounded_linearI':
wenzelm@56444
   508
  fixes f ::"'a::euclidean_space \<Rightarrow> 'b::real_normed_vector"
wenzelm@53406
   509
  assumes "\<And>x y. f (x + y) = f x + f y"
wenzelm@53406
   510
    and "\<And>c x. f (c *\<^sub>R x) = c *\<^sub>R f x"
wenzelm@49522
   511
  shows "bounded_linear f"
immler@68072
   512
  using assms linearI linear_conv_bounded_linear by blast
huffman@44133
   513
huffman@44133
   514
lemma bilinear_bounded:
wenzelm@56444
   515
  fixes h :: "'m::euclidean_space \<Rightarrow> 'n::euclidean_space \<Rightarrow> 'k::real_normed_vector"
huffman@44133
   516
  assumes bh: "bilinear h"
huffman@44133
   517
  shows "\<exists>B. \<forall>x y. norm (h x y) \<le> B * norm x * norm y"
hoelzl@50526
   518
proof (clarify intro!: exI[of _ "\<Sum>i\<in>Basis. \<Sum>j\<in>Basis. norm (h i j)"])
wenzelm@53406
   519
  fix x :: 'm
wenzelm@53406
   520
  fix y :: 'n
nipkow@64267
   521
  have "norm (h x y) = norm (h (sum (\<lambda>i. (x \<bullet> i) *\<^sub>R i) Basis) (sum (\<lambda>i. (y \<bullet> i) *\<^sub>R i) Basis))"
lp15@68069
   522
    by (simp add: euclidean_representation)
nipkow@64267
   523
  also have "\<dots> = norm (sum (\<lambda> (i,j). h ((x \<bullet> i) *\<^sub>R i) ((y \<bullet> j) *\<^sub>R j)) (Basis \<times> Basis))"
immler@68072
   524
    unfolding bilinear_sum[OF bh] ..
hoelzl@50526
   525
  finally have th: "norm (h x y) = \<dots>" .
lp15@68069
   526
  have "\<And>i j. \<lbrakk>i \<in> Basis; j \<in> Basis\<rbrakk>
lp15@68069
   527
           \<Longrightarrow> \<bar>x \<bullet> i\<bar> * (\<bar>y \<bullet> j\<bar> * norm (h i j)) \<le> norm x * (norm y * norm (h i j))"
lp15@68069
   528
    by (auto simp add: zero_le_mult_iff Basis_le_norm mult_mono)
lp15@68069
   529
  then show "norm (h x y) \<le> (\<Sum>i\<in>Basis. \<Sum>j\<in>Basis. norm (h i j)) * norm x * norm y"
lp15@68069
   530
    unfolding sum_distrib_right th sum.cartesian_product
lp15@68069
   531
    by (clarsimp simp add: bilinear_rmul[OF bh] bilinear_lmul[OF bh]
lp15@68069
   532
      field_simps simp del: scaleR_scaleR intro!: sum_norm_le)
huffman@44133
   533
qed
huffman@44133
   534
huffman@44133
   535
lemma bilinear_conv_bounded_bilinear:
huffman@44133
   536
  fixes h :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space \<Rightarrow> 'c::real_normed_vector"
huffman@44133
   537
  shows "bilinear h \<longleftrightarrow> bounded_bilinear h"
huffman@44133
   538
proof
huffman@44133
   539
  assume "bilinear h"
huffman@44133
   540
  show "bounded_bilinear h"
huffman@44133
   541
  proof
wenzelm@53406
   542
    fix x y z
wenzelm@53406
   543
    show "h (x + y) z = h x z + h y z"
wenzelm@60420
   544
      using \<open>bilinear h\<close> unfolding bilinear_def linear_iff by simp
huffman@44133
   545
  next
wenzelm@53406
   546
    fix x y z
wenzelm@53406
   547
    show "h x (y + z) = h x y + h x z"
wenzelm@60420
   548
      using \<open>bilinear h\<close> unfolding bilinear_def linear_iff by simp
huffman@44133
   549
  next
lp15@68069
   550
    show "h (scaleR r x) y = scaleR r (h x y)" "h x (scaleR r y) = scaleR r (h x y)" for r x y
wenzelm@60420
   551
      using \<open>bilinear h\<close> unfolding bilinear_def linear_iff
lp15@68069
   552
      by simp_all
huffman@44133
   553
  next
huffman@44133
   554
    have "\<exists>B. \<forall>x y. norm (h x y) \<le> B * norm x * norm y"
wenzelm@60420
   555
      using \<open>bilinear h\<close> by (rule bilinear_bounded)
wenzelm@49522
   556
    then show "\<exists>K. \<forall>x y. norm (h x y) \<le> norm x * norm y * K"
haftmann@57514
   557
      by (simp add: ac_simps)
huffman@44133
   558
  qed
huffman@44133
   559
next
huffman@44133
   560
  assume "bounded_bilinear h"
huffman@44133
   561
  then interpret h: bounded_bilinear h .
huffman@44133
   562
  show "bilinear h"
huffman@44133
   563
    unfolding bilinear_def linear_conv_bounded_linear
wenzelm@49522
   564
    using h.bounded_linear_left h.bounded_linear_right by simp
huffman@44133
   565
qed
huffman@44133
   566
huffman@53939
   567
lemma bilinear_bounded_pos:
wenzelm@56444
   568
  fixes h :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space \<Rightarrow> 'c::real_normed_vector"
huffman@53939
   569
  assumes bh: "bilinear h"
huffman@53939
   570
  shows "\<exists>B > 0. \<forall>x y. norm (h x y) \<le> B * norm x * norm y"
huffman@53939
   571
proof -
huffman@53939
   572
  have "\<exists>B > 0. \<forall>x y. norm (h x y) \<le> norm x * norm y * B"
huffman@53939
   573
    using bh [unfolded bilinear_conv_bounded_bilinear]
huffman@53939
   574
    by (rule bounded_bilinear.pos_bounded)
huffman@53939
   575
  then show ?thesis
haftmann@57514
   576
    by (simp only: ac_simps)
huffman@53939
   577
qed
huffman@53939
   578
immler@68072
   579
lemma bounded_linear_imp_has_derivative: "bounded_linear f \<Longrightarrow> (f has_derivative f) net"
immler@68072
   580
  by (auto simp add: has_derivative_def linear_diff linear_linear linear_def
immler@68072
   581
      dest: bounded_linear.linear)
lp15@63469
   582
lp15@63469
   583
lemma linear_imp_has_derivative:
lp15@63469
   584
  fixes f :: "'a::euclidean_space \<Rightarrow> 'b::real_normed_vector"
lp15@63469
   585
  shows "linear f \<Longrightarrow> (f has_derivative f) net"
immler@68072
   586
  by (simp add: bounded_linear_imp_has_derivative linear_conv_bounded_linear)
lp15@63469
   587
lp15@63469
   588
lemma bounded_linear_imp_differentiable: "bounded_linear f \<Longrightarrow> f differentiable net"
lp15@63469
   589
  using bounded_linear_imp_has_derivative differentiable_def by blast
lp15@63469
   590
lp15@63469
   591
lemma linear_imp_differentiable:
lp15@63469
   592
  fixes f :: "'a::euclidean_space \<Rightarrow> 'b::real_normed_vector"
lp15@63469
   593
  shows "linear f \<Longrightarrow> f differentiable net"
immler@68072
   594
  by (metis linear_imp_has_derivative differentiable_def)
lp15@63469
   595
wenzelm@49522
   596
nipkow@68901
   597
subsection%unimportant \<open>We continue\<close>
huffman@44133
   598
huffman@44133
   599
lemma independent_bound:
wenzelm@53716
   600
  fixes S :: "'a::euclidean_space set"
wenzelm@53716
   601
  shows "independent S \<Longrightarrow> finite S \<and> card S \<le> DIM('a)"
immler@68072
   602
  by (metis dim_subset_UNIV finiteI_independent dim_span_eq_card_independent)
immler@68072
   603
immler@68072
   604
lemmas independent_imp_finite = finiteI_independent
huffman@44133
   605
lp15@61609
   606
corollary
paulson@60303
   607
  fixes S :: "'a::euclidean_space set"
paulson@60303
   608
  assumes "independent S"
immler@68072
   609
  shows independent_card_le:"card S \<le> DIM('a)"
immler@68072
   610
  using assms independent_bound by auto
lp15@63075
   611
wenzelm@49663
   612
lemma dependent_biggerset:
wenzelm@56444
   613
  fixes S :: "'a::euclidean_space set"
wenzelm@56444
   614
  shows "(finite S \<Longrightarrow> card S > DIM('a)) \<Longrightarrow> dependent S"
huffman@44133
   615
  by (metis independent_bound not_less)
huffman@44133
   616
wenzelm@60420
   617
text \<open>Picking an orthogonal replacement for a spanning set.\<close>
huffman@44133
   618
wenzelm@53406
   619
lemma vector_sub_project_orthogonal:
wenzelm@53406
   620
  fixes b x :: "'a::euclidean_space"
wenzelm@53406
   621
  shows "b \<bullet> (x - ((b \<bullet> x) / (b \<bullet> b)) *\<^sub>R b) = 0"
huffman@44133
   622
  unfolding inner_simps by auto
huffman@44133
   623
huffman@44528
   624
lemma pairwise_orthogonal_insert:
huffman@44528
   625
  assumes "pairwise orthogonal S"
wenzelm@49522
   626
    and "\<And>y. y \<in> S \<Longrightarrow> orthogonal x y"
huffman@44528
   627
  shows "pairwise orthogonal (insert x S)"
huffman@44528
   628
  using assms unfolding pairwise_def
huffman@44528
   629
  by (auto simp add: orthogonal_commute)
huffman@44528
   630
huffman@44133
   631
lemma basis_orthogonal:
wenzelm@53406
   632
  fixes B :: "'a::real_inner set"
huffman@44133
   633
  assumes fB: "finite B"
huffman@44133
   634
  shows "\<exists>C. finite C \<and> card C \<le> card B \<and> span C = span B \<and> pairwise orthogonal C"
huffman@44133
   635
  (is " \<exists>C. ?P B C")
wenzelm@49522
   636
  using fB
wenzelm@49522
   637
proof (induct rule: finite_induct)
wenzelm@49522
   638
  case empty
wenzelm@53406
   639
  then show ?case
wenzelm@53406
   640
    apply (rule exI[where x="{}"])
wenzelm@53406
   641
    apply (auto simp add: pairwise_def)
wenzelm@53406
   642
    done
huffman@44133
   643
next
wenzelm@49522
   644
  case (insert a B)
wenzelm@60420
   645
  note fB = \<open>finite B\<close> and aB = \<open>a \<notin> B\<close>
wenzelm@60420
   646
  from \<open>\<exists>C. finite C \<and> card C \<le> card B \<and> span C = span B \<and> pairwise orthogonal C\<close>
huffman@44133
   647
  obtain C where C: "finite C" "card C \<le> card B"
huffman@44133
   648
    "span C = span B" "pairwise orthogonal C" by blast
nipkow@64267
   649
  let ?a = "a - sum (\<lambda>x. (x \<bullet> a / (x \<bullet> x)) *\<^sub>R x) C"
huffman@44133
   650
  let ?C = "insert ?a C"
wenzelm@53406
   651
  from C(1) have fC: "finite ?C"
wenzelm@53406
   652
    by simp
wenzelm@49522
   653
  from fB aB C(1,2) have cC: "card ?C \<le> card (insert a B)"
wenzelm@49522
   654
    by (simp add: card_insert_if)
wenzelm@53406
   655
  {
wenzelm@53406
   656
    fix x k
wenzelm@49522
   657
    have th0: "\<And>(a::'a) b c. a - (b - c) = c + (a - b)"
wenzelm@49522
   658
      by (simp add: field_simps)
huffman@44133
   659
    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"
huffman@44133
   660
      apply (simp only: scaleR_right_diff_distrib th0)
huffman@44133
   661
      apply (rule span_add_eq)
immler@68072
   662
      apply (rule span_scale)
nipkow@64267
   663
      apply (rule span_sum)
immler@68072
   664
      apply (rule span_scale)
immler@68072
   665
      apply (rule span_base)
wenzelm@49522
   666
      apply assumption
wenzelm@53406
   667
      done
wenzelm@53406
   668
  }
huffman@44133
   669
  then have SC: "span ?C = span (insert a B)"
huffman@44133
   670
    unfolding set_eq_iff span_breakdown_eq C(3)[symmetric] by auto
wenzelm@53406
   671
  {
wenzelm@53406
   672
    fix y
wenzelm@53406
   673
    assume yC: "y \<in> C"
wenzelm@53406
   674
    then have Cy: "C = insert y (C - {y})"
wenzelm@53406
   675
      by blast
wenzelm@53406
   676
    have fth: "finite (C - {y})"
wenzelm@53406
   677
      using C by simp
huffman@44528
   678
    have "orthogonal ?a y"
huffman@44528
   679
      unfolding orthogonal_def
nipkow@64267
   680
      unfolding inner_diff inner_sum_left right_minus_eq
nipkow@64267
   681
      unfolding sum.remove [OF \<open>finite C\<close> \<open>y \<in> C\<close>]
huffman@44528
   682
      apply (clarsimp simp add: inner_commute[of y a])
nipkow@64267
   683
      apply (rule sum.neutral)
huffman@44528
   684
      apply clarsimp
huffman@44528
   685
      apply (rule C(4)[unfolded pairwise_def orthogonal_def, rule_format])
wenzelm@60420
   686
      using \<open>y \<in> C\<close> by auto
wenzelm@53406
   687
  }
wenzelm@60420
   688
  with \<open>pairwise orthogonal C\<close> have CPO: "pairwise orthogonal ?C"
huffman@44528
   689
    by (rule pairwise_orthogonal_insert)
wenzelm@53406
   690
  from fC cC SC CPO have "?P (insert a B) ?C"
wenzelm@53406
   691
    by blast
huffman@44133
   692
  then show ?case by blast
huffman@44133
   693
qed
huffman@44133
   694
huffman@44133
   695
lemma orthogonal_basis_exists:
huffman@44133
   696
  fixes V :: "('a::euclidean_space) set"
immler@68072
   697
  shows "\<exists>B. independent B \<and> B \<subseteq> span V \<and> V \<subseteq> span B \<and>
immler@68072
   698
  (card B = dim V) \<and> pairwise orthogonal B"
wenzelm@49663
   699
proof -
wenzelm@49522
   700
  from basis_exists[of V] obtain B where
wenzelm@53406
   701
    B: "B \<subseteq> V" "independent B" "V \<subseteq> span B" "card B = dim V"
immler@68073
   702
    by force
wenzelm@53406
   703
  from B have fB: "finite B" "card B = dim V"
wenzelm@53406
   704
    using independent_bound by auto
huffman@44133
   705
  from basis_orthogonal[OF fB(1)] obtain C where
wenzelm@53406
   706
    C: "finite C" "card C \<le> card B" "span C = span B" "pairwise orthogonal C"
wenzelm@53406
   707
    by blast
wenzelm@53406
   708
  from C B have CSV: "C \<subseteq> span V"
immler@68072
   709
    by (metis span_superset span_mono subset_trans)
wenzelm@53406
   710
  from span_mono[OF B(3)] C have SVC: "span V \<subseteq> span C"
wenzelm@53406
   711
    by (simp add: span_span)
huffman@44133
   712
  from card_le_dim_spanning[OF CSV SVC C(1)] C(2,3) fB
wenzelm@53406
   713
  have iC: "independent C"
huffman@44133
   714
    by (simp add: dim_span)
wenzelm@53406
   715
  from C fB have "card C \<le> dim V"
wenzelm@53406
   716
    by simp
wenzelm@53406
   717
  moreover have "dim V \<le> card C"
wenzelm@53406
   718
    using span_card_ge_dim[OF CSV SVC C(1)]
immler@68072
   719
    by simp
wenzelm@53406
   720
  ultimately have CdV: "card C = dim V"
wenzelm@53406
   721
    using C(1) by simp
wenzelm@53406
   722
  from C B CSV CdV iC show ?thesis
wenzelm@53406
   723
    by auto
huffman@44133
   724
qed
huffman@44133
   725
wenzelm@60420
   726
text \<open>Low-dimensional subset is in a hyperplane (weak orthogonal complement).\<close>
huffman@44133
   727
wenzelm@49522
   728
lemma span_not_univ_orthogonal:
wenzelm@53406
   729
  fixes S :: "'a::euclidean_space set"
huffman@44133
   730
  assumes sU: "span S \<noteq> UNIV"
wenzelm@56444
   731
  shows "\<exists>a::'a. a \<noteq> 0 \<and> (\<forall>x \<in> span S. a \<bullet> x = 0)"
wenzelm@49522
   732
proof -
wenzelm@53406
   733
  from sU obtain a where a: "a \<notin> span S"
wenzelm@53406
   734
    by blast
huffman@44133
   735
  from orthogonal_basis_exists obtain B where
immler@68072
   736
    B: "independent B" "B \<subseteq> span S" "S \<subseteq> span B"
immler@68072
   737
    "card B = dim S" "pairwise orthogonal B"
huffman@44133
   738
    by blast
wenzelm@53406
   739
  from B have fB: "finite B" "card B = dim S"
wenzelm@53406
   740
    using independent_bound by auto
huffman@44133
   741
  from span_mono[OF B(2)] span_mono[OF B(3)]
wenzelm@53406
   742
  have sSB: "span S = span B"
wenzelm@53406
   743
    by (simp add: span_span)
nipkow@64267
   744
  let ?a = "a - sum (\<lambda>b. (a \<bullet> b / (b \<bullet> b)) *\<^sub>R b) B"
nipkow@64267
   745
  have "sum (\<lambda>b. (a \<bullet> b / (b \<bullet> b)) *\<^sub>R b) B \<in> span S"
huffman@44133
   746
    unfolding sSB
nipkow@64267
   747
    apply (rule span_sum)
immler@68072
   748
    apply (rule span_scale)
immler@68072
   749
    apply (rule span_base)
wenzelm@49522
   750
    apply assumption
wenzelm@49522
   751
    done
wenzelm@53406
   752
  with a have a0:"?a  \<noteq> 0"
wenzelm@53406
   753
    by auto
lp15@68058
   754
  have "?a \<bullet> x = 0" if "x\<in>span B" for x
lp15@68058
   755
  proof (rule span_induct [OF that])
wenzelm@49522
   756
    show "subspace {x. ?a \<bullet> x = 0}"
wenzelm@49522
   757
      by (auto simp add: subspace_def inner_add)
wenzelm@49522
   758
  next
wenzelm@53406
   759
    {
wenzelm@53406
   760
      fix x
wenzelm@53406
   761
      assume x: "x \<in> B"
wenzelm@53406
   762
      from x have B': "B = insert x (B - {x})"
wenzelm@53406
   763
        by blast
wenzelm@53406
   764
      have fth: "finite (B - {x})"
wenzelm@53406
   765
        using fB by simp
huffman@44133
   766
      have "?a \<bullet> x = 0"
wenzelm@53406
   767
        apply (subst B')
wenzelm@53406
   768
        using fB fth
nipkow@64267
   769
        unfolding sum_clauses(2)[OF fth]
huffman@44133
   770
        apply simp unfolding inner_simps
nipkow@64267
   771
        apply (clarsimp simp add: inner_add inner_sum_left)
nipkow@64267
   772
        apply (rule sum.neutral, rule ballI)
wenzelm@63170
   773
        apply (simp only: inner_commute)
wenzelm@49711
   774
        apply (auto simp add: x field_simps
wenzelm@49711
   775
          intro: B(5)[unfolded pairwise_def orthogonal_def, rule_format])
wenzelm@53406
   776
        done
wenzelm@53406
   777
    }
lp15@68058
   778
    then show "?a \<bullet> x = 0" if "x \<in> B" for x
lp15@68058
   779
      using that by blast
lp15@68058
   780
    qed
wenzelm@53406
   781
  with a0 show ?thesis
wenzelm@53406
   782
    unfolding sSB by (auto intro: exI[where x="?a"])
huffman@44133
   783
qed
huffman@44133
   784
huffman@44133
   785
lemma span_not_univ_subset_hyperplane:
wenzelm@53406
   786
  fixes S :: "'a::euclidean_space set"
wenzelm@53406
   787
  assumes SU: "span S \<noteq> UNIV"
huffman@44133
   788
  shows "\<exists> a. a \<noteq>0 \<and> span S \<subseteq> {x. a \<bullet> x = 0}"
huffman@44133
   789
  using span_not_univ_orthogonal[OF SU] by auto
huffman@44133
   790
wenzelm@49663
   791
lemma lowdim_subset_hyperplane:
wenzelm@53406
   792
  fixes S :: "'a::euclidean_space set"
huffman@44133
   793
  assumes d: "dim S < DIM('a)"
wenzelm@56444
   794
  shows "\<exists>a::'a. a \<noteq> 0 \<and> span S \<subseteq> {x. a \<bullet> x = 0}"
wenzelm@49522
   795
proof -
wenzelm@53406
   796
  {
wenzelm@53406
   797
    assume "span S = UNIV"
wenzelm@53406
   798
    then have "dim (span S) = dim (UNIV :: ('a) set)"
wenzelm@53406
   799
      by simp
wenzelm@53406
   800
    then have "dim S = DIM('a)"
immler@68072
   801
      by (metis Euclidean_Space.dim_UNIV dim_span)
wenzelm@53406
   802
    with d have False by arith
wenzelm@53406
   803
  }
wenzelm@53406
   804
  then have th: "span S \<noteq> UNIV"
wenzelm@53406
   805
    by blast
huffman@44133
   806
  from span_not_univ_subset_hyperplane[OF th] show ?thesis .
huffman@44133
   807
qed
huffman@44133
   808
huffman@44133
   809
lemma linear_eq_stdbasis:
wenzelm@56444
   810
  fixes f :: "'a::euclidean_space \<Rightarrow> _"
wenzelm@56444
   811
  assumes lf: "linear f"
wenzelm@49663
   812
    and lg: "linear g"
lp15@68058
   813
    and fg: "\<And>b. b \<in> Basis \<Longrightarrow> f b = g b"
huffman@44133
   814
  shows "f = g"
immler@68072
   815
  using linear_eq_on_span[OF lf lg, of Basis] fg
immler@68072
   816
  by auto
immler@68072
   817
huffman@44133
   818
wenzelm@60420
   819
text \<open>Similar results for bilinear functions.\<close>
huffman@44133
   820
huffman@44133
   821
lemma bilinear_eq:
huffman@44133
   822
  assumes bf: "bilinear f"
wenzelm@49522
   823
    and bg: "bilinear g"
wenzelm@53406
   824
    and SB: "S \<subseteq> span B"
wenzelm@53406
   825
    and TC: "T \<subseteq> span C"
lp15@68058
   826
    and "x\<in>S" "y\<in>T"
lp15@68058
   827
    and fg: "\<And>x y. \<lbrakk>x \<in> B; y\<in> C\<rbrakk> \<Longrightarrow> f x y = g x y"
lp15@68058
   828
  shows "f x y = g x y"
wenzelm@49663
   829
proof -
huffman@44170
   830
  let ?P = "{x. \<forall>y\<in> span C. f x y = g x y}"
huffman@44133
   831
  from bf bg have sp: "subspace ?P"
huffman@53600
   832
    unfolding bilinear_def linear_iff subspace_def bf bg
immler@68072
   833
    by (auto simp add: span_zero bilinear_lzero[OF bf] bilinear_lzero[OF bg]
immler@68072
   834
        span_add Ball_def
wenzelm@49663
   835
      intro: bilinear_ladd[OF bf])
lp15@68058
   836
  have sfg: "\<And>x. x \<in> B \<Longrightarrow> subspace {a. f x a = g x a}"
huffman@44133
   837
    apply (auto simp add: subspace_def)
huffman@53600
   838
    using bf bg unfolding bilinear_def linear_iff
immler@68072
   839
      apply (auto simp add: span_zero bilinear_rzero[OF bf] bilinear_rzero[OF bg]
immler@68072
   840
        span_add Ball_def
wenzelm@49663
   841
      intro: bilinear_ladd[OF bf])
wenzelm@49522
   842
    done
lp15@68058
   843
  have "\<forall>y\<in> span C. f x y = g x y" if "x \<in> span B" for x
lp15@68058
   844
    apply (rule span_induct [OF that sp])
lp15@68062
   845
    using fg sfg span_induct by blast
wenzelm@53406
   846
  then show ?thesis
lp15@68058
   847
    using SB TC assms by auto
huffman@44133
   848
qed
huffman@44133
   849
wenzelm@49522
   850
lemma bilinear_eq_stdbasis:
wenzelm@53406
   851
  fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space \<Rightarrow> _"
huffman@44133
   852
  assumes bf: "bilinear f"
wenzelm@49522
   853
    and bg: "bilinear g"
lp15@68058
   854
    and fg: "\<And>i j. i \<in> Basis \<Longrightarrow> j \<in> Basis \<Longrightarrow> f i j = g i j"
huffman@44133
   855
  shows "f = g"
immler@68074
   856
  using bilinear_eq[OF bf bg equalityD2[OF span_Basis] equalityD2[OF span_Basis]] fg by blast
wenzelm@49522
   857
wenzelm@60420
   858
subsection \<open>Infinity norm\<close>
huffman@44133
   859
immler@67962
   860
definition%important "infnorm (x::'a::euclidean_space) = Sup {\<bar>x \<bullet> b\<bar> |b. b \<in> Basis}"
huffman@44133
   861
huffman@44133
   862
lemma infnorm_set_image:
wenzelm@53716
   863
  fixes x :: "'a::euclidean_space"
wenzelm@56444
   864
  shows "{\<bar>x \<bullet> i\<bar> |i. i \<in> Basis} = (\<lambda>i. \<bar>x \<bullet> i\<bar>) ` Basis"
hoelzl@50526
   865
  by blast
huffman@44133
   866
wenzelm@53716
   867
lemma infnorm_Max:
wenzelm@53716
   868
  fixes x :: "'a::euclidean_space"
wenzelm@56444
   869
  shows "infnorm x = Max ((\<lambda>i. \<bar>x \<bullet> i\<bar>) ` Basis)"
haftmann@62343
   870
  by (simp add: infnorm_def infnorm_set_image cSup_eq_Max)
hoelzl@51475
   871
huffman@44133
   872
lemma infnorm_set_lemma:
wenzelm@53716
   873
  fixes x :: "'a::euclidean_space"
wenzelm@56444
   874
  shows "finite {\<bar>x \<bullet> i\<bar> |i. i \<in> Basis}"
wenzelm@56444
   875
    and "{\<bar>x \<bullet> i\<bar> |i. i \<in> Basis} \<noteq> {}"
huffman@44133
   876
  unfolding infnorm_set_image
huffman@44133
   877
  by auto
huffman@44133
   878
wenzelm@53406
   879
lemma infnorm_pos_le:
wenzelm@53406
   880
  fixes x :: "'a::euclidean_space"
wenzelm@53406
   881
  shows "0 \<le> infnorm x"
hoelzl@51475
   882
  by (simp add: infnorm_Max Max_ge_iff ex_in_conv)
huffman@44133
   883
wenzelm@53406
   884
lemma infnorm_triangle:
wenzelm@53406
   885
  fixes x :: "'a::euclidean_space"
wenzelm@53406
   886
  shows "infnorm (x + y) \<le> infnorm x + infnorm y"
wenzelm@49522
   887
proof -
hoelzl@51475
   888
  have *: "\<And>a b c d :: real. \<bar>a\<bar> \<le> c \<Longrightarrow> \<bar>b\<bar> \<le> d \<Longrightarrow> \<bar>a + b\<bar> \<le> c + d"
hoelzl@51475
   889
    by simp
huffman@44133
   890
  show ?thesis
hoelzl@51475
   891
    by (auto simp: infnorm_Max inner_add_left intro!: *)
huffman@44133
   892
qed
huffman@44133
   893
wenzelm@53406
   894
lemma infnorm_eq_0:
wenzelm@53406
   895
  fixes x :: "'a::euclidean_space"
wenzelm@53406
   896
  shows "infnorm x = 0 \<longleftrightarrow> x = 0"
wenzelm@49522
   897
proof -
hoelzl@51475
   898
  have "infnorm x \<le> 0 \<longleftrightarrow> x = 0"
hoelzl@51475
   899
    unfolding infnorm_Max by (simp add: euclidean_all_zero_iff)
hoelzl@51475
   900
  then show ?thesis
hoelzl@51475
   901
    using infnorm_pos_le[of x] by simp
huffman@44133
   902
qed
huffman@44133
   903
huffman@44133
   904
lemma infnorm_0: "infnorm 0 = 0"
huffman@44133
   905
  by (simp add: infnorm_eq_0)
huffman@44133
   906
huffman@44133
   907
lemma infnorm_neg: "infnorm (- x) = infnorm x"
lp15@68062
   908
  unfolding infnorm_def by simp
huffman@44133
   909
huffman@44133
   910
lemma infnorm_sub: "infnorm (x - y) = infnorm (y - x)"
lp15@68062
   911
  by (metis infnorm_neg minus_diff_eq)
lp15@68062
   912
lp15@68062
   913
lemma absdiff_infnorm: "\<bar>infnorm x - infnorm y\<bar> \<le> infnorm (x - y)"
wenzelm@49522
   914
proof -
lp15@68062
   915
  have *: "\<And>(nx::real) n ny. nx \<le> n + ny \<Longrightarrow> ny \<le> n + nx \<Longrightarrow> \<bar>nx - ny\<bar> \<le> n"
huffman@44133
   916
    by arith
lp15@68062
   917
  show ?thesis
lp15@68062
   918
  proof (rule *)
lp15@68062
   919
    from infnorm_triangle[of "x - y" " y"] infnorm_triangle[of "x - y" "-x"]
lp15@68062
   920
    show "infnorm x \<le> infnorm (x - y) + infnorm y" "infnorm y \<le> infnorm (x - y) + infnorm x"
lp15@68062
   921
      by (simp_all add: field_simps infnorm_neg)
lp15@68062
   922
  qed
huffman@44133
   923
qed
huffman@44133
   924
wenzelm@53406
   925
lemma real_abs_infnorm: "\<bar>infnorm x\<bar> = infnorm x"
huffman@44133
   926
  using infnorm_pos_le[of x] by arith
huffman@44133
   927
hoelzl@50526
   928
lemma Basis_le_infnorm:
wenzelm@53406
   929
  fixes x :: "'a::euclidean_space"
wenzelm@53406
   930
  shows "b \<in> Basis \<Longrightarrow> \<bar>x \<bullet> b\<bar> \<le> infnorm x"
hoelzl@51475
   931
  by (simp add: infnorm_Max)
huffman@44133
   932
wenzelm@56444
   933
lemma infnorm_mul: "infnorm (a *\<^sub>R x) = \<bar>a\<bar> * infnorm x"
hoelzl@51475
   934
  unfolding infnorm_Max
hoelzl@51475
   935
proof (safe intro!: Max_eqI)
hoelzl@51475
   936
  let ?B = "(\<lambda>i. \<bar>x \<bullet> i\<bar>) ` Basis"
lp15@68062
   937
  { fix b :: 'a
wenzelm@53406
   938
    assume "b \<in> Basis"
wenzelm@53406
   939
    then show "\<bar>a *\<^sub>R x \<bullet> b\<bar> \<le> \<bar>a\<bar> * Max ?B"
wenzelm@53406
   940
      by (simp add: abs_mult mult_left_mono)
wenzelm@53406
   941
  next
wenzelm@53406
   942
    from Max_in[of ?B] obtain b where "b \<in> Basis" "Max ?B = \<bar>x \<bullet> b\<bar>"
wenzelm@53406
   943
      by (auto simp del: Max_in)
wenzelm@53406
   944
    then show "\<bar>a\<bar> * Max ((\<lambda>i. \<bar>x \<bullet> i\<bar>) ` Basis) \<in> (\<lambda>i. \<bar>a *\<^sub>R x \<bullet> i\<bar>) ` Basis"
wenzelm@53406
   945
      by (intro image_eqI[where x=b]) (auto simp: abs_mult)
wenzelm@53406
   946
  }
hoelzl@51475
   947
qed simp
hoelzl@51475
   948
wenzelm@53406
   949
lemma infnorm_mul_lemma: "infnorm (a *\<^sub>R x) \<le> \<bar>a\<bar> * infnorm x"
hoelzl@51475
   950
  unfolding infnorm_mul ..
huffman@44133
   951
huffman@44133
   952
lemma infnorm_pos_lt: "infnorm x > 0 \<longleftrightarrow> x \<noteq> 0"
huffman@44133
   953
  using infnorm_pos_le[of x] infnorm_eq_0[of x] by arith
huffman@44133
   954
wenzelm@60420
   955
text \<open>Prove that it differs only up to a bound from Euclidean norm.\<close>
huffman@44133
   956
huffman@44133
   957
lemma infnorm_le_norm: "infnorm x \<le> norm x"
hoelzl@51475
   958
  by (simp add: Basis_le_norm infnorm_Max)
hoelzl@50526
   959
wenzelm@53716
   960
lemma norm_le_infnorm:
wenzelm@53716
   961
  fixes x :: "'a::euclidean_space"
wenzelm@53716
   962
  shows "norm x \<le> sqrt DIM('a) * infnorm x"
lp15@68062
   963
  unfolding norm_eq_sqrt_inner id_def 
lp15@68062
   964
proof (rule real_le_lsqrt[OF inner_ge_zero])
lp15@68062
   965
  show "sqrt DIM('a) * infnorm x \<ge> 0"
huffman@44133
   966
    by (simp add: zero_le_mult_iff infnorm_pos_le)
lp15@68062
   967
  have "x \<bullet> x \<le> (\<Sum>b\<in>Basis. x \<bullet> b * (x \<bullet> b))"
lp15@68062
   968
    by (metis euclidean_inner order_refl)
lp15@68062
   969
  also have "... \<le> DIM('a) * \<bar>infnorm x\<bar>\<^sup>2"
lp15@68062
   970
    by (rule sum_bounded_above) (metis Basis_le_infnorm abs_le_square_iff power2_eq_square real_abs_infnorm)
lp15@68062
   971
  also have "... \<le> (sqrt DIM('a) * infnorm x)\<^sup>2"
lp15@68062
   972
    by (simp add: power_mult_distrib)
lp15@68062
   973
  finally show "x \<bullet> x \<le> (sqrt DIM('a) * infnorm x)\<^sup>2" .
huffman@44133
   974
qed
huffman@44133
   975
huffman@44646
   976
lemma tendsto_infnorm [tendsto_intros]:
wenzelm@61973
   977
  assumes "(f \<longlongrightarrow> a) F"
wenzelm@61973
   978
  shows "((\<lambda>x. infnorm (f x)) \<longlongrightarrow> infnorm a) F"
huffman@44646
   979
proof (rule tendsto_compose [OF LIM_I assms])
wenzelm@53406
   980
  fix r :: real
wenzelm@53406
   981
  assume "r > 0"
wenzelm@49522
   982
  then show "\<exists>s>0. \<forall>x. x \<noteq> a \<and> norm (x - a) < s \<longrightarrow> norm (infnorm x - infnorm a) < r"
lp15@68062
   983
    by (metis real_norm_def le_less_trans absdiff_infnorm infnorm_le_norm)
huffman@44646
   984
qed
huffman@44646
   985
wenzelm@60420
   986
text \<open>Equality in Cauchy-Schwarz and triangle inequalities.\<close>
huffman@44133
   987
wenzelm@53406
   988
lemma norm_cauchy_schwarz_eq: "x \<bullet> y = norm x * norm y \<longleftrightarrow> norm x *\<^sub>R y = norm y *\<^sub>R x"
wenzelm@53406
   989
  (is "?lhs \<longleftrightarrow> ?rhs")
lp15@68062
   990
proof (cases "x=0")
lp15@68062
   991
  case True
lp15@68062
   992
  then show ?thesis 
lp15@68062
   993
    by auto
lp15@68062
   994
next
lp15@68062
   995
  case False
lp15@68062
   996
  from inner_eq_zero_iff[of "norm y *\<^sub>R x - norm x *\<^sub>R y"]
lp15@68062
   997
  have "?rhs \<longleftrightarrow>
wenzelm@49522
   998
      (norm y * (norm y * norm x * norm x - norm x * (x \<bullet> y)) -
wenzelm@49522
   999
        norm x * (norm y * (y \<bullet> x) - norm x * norm y * norm y) =  0)"
lp15@68062
  1000
    using False unfolding inner_simps
lp15@68062
  1001
    by (auto simp add: power2_norm_eq_inner[symmetric] power2_eq_square inner_commute field_simps)
lp15@68062
  1002
  also have "\<dots> \<longleftrightarrow> (2 * norm x * norm y * (norm x * norm y - x \<bullet> y) = 0)" 
lp15@68062
  1003
    using False  by (simp add: field_simps inner_commute)
lp15@68062
  1004
  also have "\<dots> \<longleftrightarrow> ?lhs" 
lp15@68062
  1005
    using False by auto
lp15@68062
  1006
  finally show ?thesis by metis
huffman@44133
  1007
qed
huffman@44133
  1008
huffman@44133
  1009
lemma norm_cauchy_schwarz_abs_eq:
wenzelm@56444
  1010
  "\<bar>x \<bullet> y\<bar> = norm x * norm y \<longleftrightarrow>
wenzelm@53716
  1011
    norm x *\<^sub>R y = norm y *\<^sub>R x \<or> norm x *\<^sub>R y = - norm y *\<^sub>R x"
wenzelm@53406
  1012
  (is "?lhs \<longleftrightarrow> ?rhs")
wenzelm@49522
  1013
proof -
wenzelm@56444
  1014
  have th: "\<And>(x::real) a. a \<ge> 0 \<Longrightarrow> \<bar>x\<bar> = a \<longleftrightarrow> x = a \<or> x = - a"
wenzelm@53406
  1015
    by arith
huffman@44133
  1016
  have "?rhs \<longleftrightarrow> norm x *\<^sub>R y = norm y *\<^sub>R x \<or> norm (- x) *\<^sub>R y = norm y *\<^sub>R (- x)"
huffman@44133
  1017
    by simp
lp15@68062
  1018
  also have "\<dots> \<longleftrightarrow> (x \<bullet> y = norm x * norm y \<or> (- x) \<bullet> y = norm x * norm y)"
huffman@44133
  1019
    unfolding norm_cauchy_schwarz_eq[symmetric]
huffman@44133
  1020
    unfolding norm_minus_cancel norm_scaleR ..
huffman@44133
  1021
  also have "\<dots> \<longleftrightarrow> ?lhs"
wenzelm@53406
  1022
    unfolding th[OF mult_nonneg_nonneg, OF norm_ge_zero[of x] norm_ge_zero[of y]] inner_simps
wenzelm@53406
  1023
    by auto
huffman@44133
  1024
  finally show ?thesis ..
huffman@44133
  1025
qed
huffman@44133
  1026
huffman@44133
  1027
lemma norm_triangle_eq:
huffman@44133
  1028
  fixes x y :: "'a::real_inner"
wenzelm@53406
  1029
  shows "norm (x + y) = norm x + norm y \<longleftrightarrow> norm x *\<^sub>R y = norm y *\<^sub>R x"
lp15@68062
  1030
proof (cases "x = 0 \<or> y = 0")
lp15@68062
  1031
  case True
lp15@68062
  1032
  then show ?thesis 
lp15@68062
  1033
    by force
lp15@68062
  1034
next
lp15@68062
  1035
  case False
lp15@68062
  1036
  then have n: "norm x > 0" "norm y > 0"
lp15@68062
  1037
    by auto
lp15@68062
  1038
  have "norm (x + y) = norm x + norm y \<longleftrightarrow> (norm (x + y))\<^sup>2 = (norm x + norm y)\<^sup>2"
lp15@68062
  1039
    by simp
lp15@68062
  1040
  also have "\<dots> \<longleftrightarrow> norm x *\<^sub>R y = norm y *\<^sub>R x"
lp15@68062
  1041
    unfolding norm_cauchy_schwarz_eq[symmetric]
lp15@68062
  1042
    unfolding power2_norm_eq_inner inner_simps
lp15@68062
  1043
    by (simp add: power2_norm_eq_inner[symmetric] power2_eq_square inner_commute field_simps)
lp15@68062
  1044
  finally show ?thesis .
huffman@44133
  1045
qed
huffman@44133
  1046
wenzelm@49522
  1047
wenzelm@60420
  1048
subsection \<open>Collinearity\<close>
huffman@44133
  1049
immler@67962
  1050
definition%important collinear :: "'a::real_vector set \<Rightarrow> bool"
wenzelm@49522
  1051
  where "collinear S \<longleftrightarrow> (\<exists>u. \<forall>x \<in> S. \<forall> y \<in> S. \<exists>c. x - y = c *\<^sub>R u)"
huffman@44133
  1052
lp15@66287
  1053
lemma collinear_alt:
lp15@66287
  1054
     "collinear S \<longleftrightarrow> (\<exists>u v. \<forall>x \<in> S. \<exists>c. x = u + c *\<^sub>R v)" (is "?lhs = ?rhs")
lp15@66287
  1055
proof
lp15@66287
  1056
  assume ?lhs
lp15@66287
  1057
  then show ?rhs
lp15@66287
  1058
    unfolding collinear_def by (metis Groups.add_ac(2) diff_add_cancel)
lp15@66287
  1059
next
lp15@66287
  1060
  assume ?rhs
lp15@66287
  1061
  then obtain u v where *: "\<And>x. x \<in> S \<Longrightarrow> \<exists>c. x = u + c *\<^sub>R v"
lp15@66287
  1062
    by (auto simp: )
lp15@66287
  1063
  have "\<exists>c. x - y = c *\<^sub>R v" if "x \<in> S" "y \<in> S" for x y
lp15@66287
  1064
        by (metis *[OF \<open>x \<in> S\<close>] *[OF \<open>y \<in> S\<close>] scaleR_left.diff add_diff_cancel_left)
lp15@66287
  1065
  then show ?lhs
lp15@66287
  1066
    using collinear_def by blast
lp15@66287
  1067
qed
lp15@66287
  1068
lp15@66287
  1069
lemma collinear:
lp15@66287
  1070
  fixes S :: "'a::{perfect_space,real_vector} set"
lp15@66287
  1071
  shows "collinear S \<longleftrightarrow> (\<exists>u. u \<noteq> 0 \<and> (\<forall>x \<in> S. \<forall> y \<in> S. \<exists>c. x - y = c *\<^sub>R u))"
lp15@66287
  1072
proof -
lp15@66287
  1073
  have "\<exists>v. v \<noteq> 0 \<and> (\<forall>x\<in>S. \<forall>y\<in>S. \<exists>c. x - y = c *\<^sub>R v)"
lp15@66287
  1074
    if "\<forall>x\<in>S. \<forall>y\<in>S. \<exists>c. x - y = c *\<^sub>R u" "u=0" for u
lp15@66287
  1075
  proof -
lp15@66287
  1076
    have "\<forall>x\<in>S. \<forall>y\<in>S. x = y"
lp15@66287
  1077
      using that by auto
lp15@66287
  1078
    moreover
lp15@66287
  1079
    obtain v::'a where "v \<noteq> 0"
lp15@66287
  1080
      using UNIV_not_singleton [of 0] by auto
lp15@66287
  1081
    ultimately have "\<forall>x\<in>S. \<forall>y\<in>S. \<exists>c. x - y = c *\<^sub>R v"
lp15@66287
  1082
      by auto
lp15@66287
  1083
    then show ?thesis
lp15@66287
  1084
      using \<open>v \<noteq> 0\<close> by blast
lp15@66287
  1085
  qed
lp15@66287
  1086
  then show ?thesis
lp15@66287
  1087
    apply (clarsimp simp: collinear_def)
immler@68072
  1088
    by (metis scaleR_zero_right vector_fraction_eq_iff)
lp15@66287
  1089
qed
lp15@66287
  1090
lp15@63881
  1091
lemma collinear_subset: "\<lbrakk>collinear T; S \<subseteq> T\<rbrakk> \<Longrightarrow> collinear S"
lp15@63881
  1092
  by (meson collinear_def subsetCE)
lp15@63881
  1093
paulson@60762
  1094
lemma collinear_empty [iff]: "collinear {}"
wenzelm@53406
  1095
  by (simp add: collinear_def)
huffman@44133
  1096
paulson@60762
  1097
lemma collinear_sing [iff]: "collinear {x}"
huffman@44133
  1098
  by (simp add: collinear_def)
huffman@44133
  1099
paulson@60762
  1100
lemma collinear_2 [iff]: "collinear {x, y}"
huffman@44133
  1101
  apply (simp add: collinear_def)
huffman@44133
  1102
  apply (rule exI[where x="x - y"])
lp15@68062
  1103
  by (metis minus_diff_eq scaleR_left.minus scaleR_one)
huffman@44133
  1104
wenzelm@56444
  1105
lemma collinear_lemma: "collinear {0, x, y} \<longleftrightarrow> x = 0 \<or> y = 0 \<or> (\<exists>c. y = c *\<^sub>R x)"
wenzelm@53406
  1106
  (is "?lhs \<longleftrightarrow> ?rhs")
lp15@68062
  1107
proof (cases "x = 0 \<or> y = 0")
lp15@68062
  1108
  case True
lp15@68062
  1109
  then show ?thesis
lp15@68062
  1110
    by (auto simp: insert_commute)
lp15@68062
  1111
next
lp15@68062
  1112
  case False
lp15@68062
  1113
  show ?thesis 
lp15@68062
  1114
  proof
lp15@68062
  1115
    assume h: "?lhs"
lp15@68062
  1116
    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"
lp15@68062
  1117
      unfolding collinear_def by blast
lp15@68062
  1118
    from u[rule_format, of x 0] u[rule_format, of y 0]
lp15@68062
  1119
    obtain cx and cy where
lp15@68062
  1120
      cx: "x = cx *\<^sub>R u" and cy: "y = cy *\<^sub>R u"
lp15@68062
  1121
      by auto
lp15@68062
  1122
    from cx cy False have cx0: "cx \<noteq> 0" and cy0: "cy \<noteq> 0" by auto
lp15@68062
  1123
    let ?d = "cy / cx"
lp15@68062
  1124
    from cx cy cx0 have "y = ?d *\<^sub>R x"
lp15@68062
  1125
      by simp
lp15@68062
  1126
    then show ?rhs using False by blast
lp15@68062
  1127
  next
lp15@68062
  1128
    assume h: "?rhs"
lp15@68062
  1129
    then obtain c where c: "y = c *\<^sub>R x"
lp15@68062
  1130
      using False by blast
lp15@68062
  1131
    show ?lhs
lp15@68062
  1132
      unfolding collinear_def c
lp15@68062
  1133
      apply (rule exI[where x=x])
lp15@68062
  1134
      apply auto
lp15@68062
  1135
          apply (rule exI[where x="- 1"], simp)
lp15@68062
  1136
         apply (rule exI[where x= "-c"], simp)
huffman@44133
  1137
        apply (rule exI[where x=1], simp)
lp15@68062
  1138
       apply (rule exI[where x="1 - c"], simp add: scaleR_left_diff_distrib)
lp15@68062
  1139
      apply (rule exI[where x="c - 1"], simp add: scaleR_left_diff_distrib)
lp15@68062
  1140
      done
lp15@68062
  1141
  qed
huffman@44133
  1142
qed
huffman@44133
  1143
wenzelm@56444
  1144
lemma norm_cauchy_schwarz_equal: "\<bar>x \<bullet> y\<bar> = norm x * norm y \<longleftrightarrow> collinear {0, x, y}"
lp15@68062
  1145
proof (cases "x=0")
lp15@68062
  1146
  case True
lp15@68062
  1147
  then show ?thesis
lp15@68062
  1148
    by (auto simp: insert_commute)
lp15@68062
  1149
next
lp15@68062
  1150
  case False
lp15@68062
  1151
  then have nnz: "norm x \<noteq> 0"
lp15@68062
  1152
    by auto
lp15@68062
  1153
  show ?thesis
lp15@68062
  1154
  proof
lp15@68062
  1155
    assume "\<bar>x \<bullet> y\<bar> = norm x * norm y"
lp15@68062
  1156
    then show "collinear {0, x, y}"
lp15@68062
  1157
      unfolding norm_cauchy_schwarz_abs_eq collinear_lemma 
lp15@68062
  1158
      by (meson eq_vector_fraction_iff nnz)
lp15@68062
  1159
  next
lp15@68062
  1160
    assume "collinear {0, x, y}"
lp15@68062
  1161
    with False show "\<bar>x \<bullet> y\<bar> = norm x * norm y"
lp15@68062
  1162
      unfolding norm_cauchy_schwarz_abs_eq collinear_lemma  by (auto simp: abs_if)
lp15@68062
  1163
  qed
lp15@68062
  1164
qed
wenzelm@49522
  1165
immler@54776
  1166
end