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